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 #include "oox/helper/modelobjecthelper.hxx"
29 
30 #include <com/sun/star/awt/Gradient.hpp>
31 #include <com/sun/star/container/XNameContainer.hpp>
32 #include <com/sun/star/drawing/LineDash.hpp>
33 #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 #include "oox/helper/containerhelper.hxx"
36 #include "oox/helper/helper.hxx"
37 
38 namespace oox {
39 
40 // ============================================================================
41 
42 using namespace ::com::sun::star::awt;
43 using namespace ::com::sun::star::drawing;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::uno;
46 
47 using ::rtl::OUString;
48 
49 // ============================================================================
50 
51 ObjectContainer::ObjectContainer( const Reference< XMultiServiceFactory >& rxModelFactory, const OUString& rServiceName ) :
52     mxModelFactory( rxModelFactory ),
53     maServiceName( rServiceName ),
54     mnIndex( 0 )
55 {
56     OSL_ENSURE( mxModelFactory.is(), "ObjectContainer::ObjectContainer - missing service factory" );
57 }
58 
59 ObjectContainer::~ObjectContainer()
60 {
61 }
62 
63 bool ObjectContainer::hasObject( const OUString& rObjName ) const
64 {
65     createContainer();
66     return mxContainer.is() && mxContainer->hasByName( rObjName );
67 }
68 
69 Any ObjectContainer::getObject( const OUString& rObjName ) const
70 {
71     createContainer();
72     if( mxContainer.is() ) try
73     {
74         return mxContainer->getByName( rObjName );
75     }
76     catch( Exception& )
77     {
78     }
79     return Any();
80 }
81 
82 OUString ObjectContainer::insertObject( const OUString& rObjName, const Any& rObj, bool bInsertByUnusedName )
83 {
84     createContainer();
85     if( mxContainer.is() )
86     {
87         if( bInsertByUnusedName )
88             return ContainerHelper::insertByUnusedName( mxContainer, rObjName + OUString::valueOf( ++mnIndex ), ' ', rObj );
89         if( ContainerHelper::insertByName( mxContainer, rObjName, rObj ) )
90             return rObjName;
91     }
92     return OUString();
93 }
94 
95 void ObjectContainer::createContainer() const
96 {
97     if( !mxContainer.is() && mxModelFactory.is() ) try
98     {
99         mxContainer.set( mxModelFactory->createInstance( maServiceName ), UNO_QUERY_THROW );
100         mxModelFactory.clear();
101     }
102     catch( Exception& )
103     {
104     }
105     OSL_ENSURE( mxContainer.is(), "ObjectContainer::createContainer - container not found" );
106 }
107 
108 // ============================================================================
109 
110 ModelObjectHelper::ModelObjectHelper( const Reference< XMultiServiceFactory >& rxModelFactory ) :
111     maMarkerContainer(    rxModelFactory, CREATE_OUSTRING( "com.sun.star.drawing.MarkerTable" ) ),
112     maDashContainer(      rxModelFactory, CREATE_OUSTRING( "com.sun.star.drawing.DashTable" ) ),
113     maGradientContainer(  rxModelFactory, CREATE_OUSTRING( "com.sun.star.drawing.GradientTable" ) ),
114     maBitmapUrlContainer( rxModelFactory, CREATE_OUSTRING( "com.sun.star.drawing.BitmapTable" ) ),
115     maDashNameBase(      CREATE_OUSTRING( "msLineDash " ) ),
116     maGradientNameBase(  CREATE_OUSTRING( "msFillGradient " ) ),
117     maBitmapUrlNameBase( CREATE_OUSTRING( "msFillBitmap " ) )
118 {
119 }
120 
121 bool ModelObjectHelper::hasLineMarker( const OUString& rMarkerName ) const
122 {
123     return maMarkerContainer.hasObject( rMarkerName );
124 }
125 
126 bool ModelObjectHelper::insertLineMarker( const OUString& rMarkerName, const PolyPolygonBezierCoords& rMarker )
127 {
128     OSL_ENSURE( rMarker.Coordinates.hasElements(), "ModelObjectHelper::insertLineMarker - line marker without coordinates" );
129     if( rMarker.Coordinates.hasElements() )
130         return maMarkerContainer.insertObject( rMarkerName, Any( rMarker ), false ).getLength() > 0;
131     return false;
132 }
133 
134 OUString ModelObjectHelper::insertLineDash( const LineDash& rDash )
135 {
136     return maDashContainer.insertObject( maDashNameBase, Any( rDash ), true );
137 }
138 
139 OUString ModelObjectHelper::insertFillGradient( const Gradient& rGradient )
140 {
141     return maGradientContainer.insertObject( maGradientNameBase, Any( rGradient ), true );
142 }
143 
144 OUString ModelObjectHelper::insertFillBitmapUrl( const OUString& rGraphicUrl )
145 {
146     if( rGraphicUrl.getLength() > 0 )
147         return maBitmapUrlContainer.insertObject( maBitmapUrlNameBase, Any( rGraphicUrl ), true );
148     return OUString();
149 }
150 
151 // ============================================================================
152 
153 } // namespace oox
154