1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 
31 #include <comphelper/servicedecl.hxx>
32 #include <cppuhelper/implbase1.hxx>
33 #include <com/sun/star/graphic/XGraphicObject.hpp>
34 #include <com/sun/star/lang/IllegalArgumentException.hpp>
35 #include <svtools/grfmgr.hxx>
36 
37 using namespace com::sun::star;
38 
39 namespace unographic {
40 
41 typedef ::cppu::WeakImplHelper1< graphic::XGraphicObject > GObjectAccess_BASE;
42  // Simple uno wrapper around the GraphicObject class to allow basic
43  // access. ( and solves a horrible cyclic link problem between
44  // goodies/toolkit/extensions )
45 class GObjectImpl : public GObjectAccess_BASE
46 {
47      ::osl::Mutex m_aMutex;
48      std::auto_ptr< GraphicObject > mpGObject;
49 public:
50      GObjectImpl( uno::Sequence< uno::Any > const & args, uno::Reference< uno::XComponentContext > const & xComponentContext ) throw (uno::RuntimeException);
51 
52      // XGraphicObject
53     virtual uno::Reference< graphic::XGraphic > SAL_CALL getGraphic() throw (uno::RuntimeException);
54     virtual void SAL_CALL setGraphic( const uno::Reference< graphic::XGraphic >& _graphic ) throw (uno::RuntimeException);
55     ::rtl::OUString SAL_CALL getUniqueID() throw (uno::RuntimeException);
56 };
57 
58 GObjectImpl::GObjectImpl( uno::Sequence< uno::Any > const & args, uno::Reference< uno::XComponentContext > const & /*xComponentContext*/ ) throw (uno::RuntimeException)
59 {
60     if ( args.getLength() == 1 )
61     {
62         rtl::OUString sId;
63         if ( !( args[ 0 ] >>= sId ) || sId.getLength() == 0 )
64             throw lang::IllegalArgumentException();
65         ByteString bsId( sId.getStr(), RTL_TEXTENCODING_UTF8 );
66         mpGObject.reset( new GraphicObject( bsId ) );
67     }
68     else
69        mpGObject.reset( new GraphicObject() );
70 }
71 
72 uno::Reference< graphic::XGraphic > SAL_CALL GObjectImpl::getGraphic() throw (uno::RuntimeException)
73 {
74     ::osl::MutexGuard aGuard( m_aMutex );
75     if ( !mpGObject.get() )
76         throw uno::RuntimeException();
77     return mpGObject->GetGraphic().GetXGraphic();
78 }
79 
80 void SAL_CALL GObjectImpl::setGraphic( const uno::Reference< graphic::XGraphic >& _graphic ) throw (uno::RuntimeException)
81 {
82     ::osl::MutexGuard aGuard( m_aMutex );
83     if ( !mpGObject.get() )
84         throw uno::RuntimeException();
85     Graphic aGraphic( _graphic );
86     mpGObject->SetGraphic( aGraphic );
87 }
88 
89 ::rtl::OUString SAL_CALL GObjectImpl::getUniqueID() throw (uno::RuntimeException)
90 {
91     ::osl::MutexGuard aGuard( m_aMutex );
92     rtl::OUString sId;
93     if ( mpGObject.get() )
94         sId = String( mpGObject->GetUniqueID().GetBuffer(), RTL_TEXTENCODING_ASCII_US );
95     return sId;
96 }
97 
98 
99 namespace sdecl = comphelper::service_decl;
100 sdecl::class_<GObjectImpl, sdecl::with_args<true> > serviceBI;
101 extern sdecl::ServiceDecl const serviceDecl( serviceBI, "com.sun.star.graphic.GraphicObject", "com.sun.star.graphic.GraphicObject" );
102 
103 }
104