1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 
29 #include <vos/mutex.hxx>
30 #include <vcl/svapp.hxx>
31 
32 #include <SwXTextDefaults.hxx>
33 #include <SwStyleNameMapper.hxx>
34 #include <fchrfmt.hxx>
35 #include <charfmt.hxx>
36 #include <docstyle.hxx>
37 #include <doc.hxx>
38 #include <docsh.hxx>
39 #include <unomap.hxx>
40 #include <unomid.h>
41 #include <paratr.hxx>
42 #include <unoprnms.hxx>
43 #include <unocrsrhelper.hxx>
44 #include <hintids.hxx>
45 
46 #include <unomid.h>
47 
48 
49 using rtl::OUString;
50 using namespace rtl;
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::lang;
55 
56 
SwXTextDefaults(SwDoc * pNewDoc)57 SwXTextDefaults::SwXTextDefaults ( SwDoc * pNewDoc ) :
58     m_pPropSet( aSwMapProvider.GetPropertySet( PROPERTY_MAP_TEXT_DEFAULT ) ),
59     m_pDoc   ( pNewDoc )
60 {
61 }
62 
63 
~SwXTextDefaults()64 SwXTextDefaults::~SwXTextDefaults ()
65 {
66 }
67 
68 
getPropertySetInfo()69 uno::Reference< XPropertySetInfo > SAL_CALL SwXTextDefaults::getPropertySetInfo(  )
70 		throw(RuntimeException)
71 {
72     static uno::Reference < XPropertySetInfo > xRef = m_pPropSet->getPropertySetInfo();
73     return xRef;
74 }
75 
76 
setPropertyValue(const OUString & rPropertyName,const Any & aValue)77 void SAL_CALL SwXTextDefaults::setPropertyValue( const OUString& rPropertyName, const Any& aValue )
78 		throw(UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
79 {
80     vos::OGuard aGuard( Application::GetSolarMutex());
81     if (!m_pDoc)
82         throw RuntimeException();
83     const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap()->getByName( rPropertyName );
84     if (!pMap)
85         throw UnknownPropertyException(OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Unknown property: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
86     if ( pMap->nFlags & PropertyAttribute::READONLY)
87         throw PropertyVetoException ( OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Property is read-only: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
88 
89     const SfxPoolItem& rItem = m_pDoc->GetDefault(pMap->nWID);
90     if (RES_PAGEDESC == pMap->nWID && MID_PAGEDESC_PAGEDESCNAME == pMap->nMemberId)
91     {
92         SfxItemSet aSet( m_pDoc->GetAttrPool(), RES_PAGEDESC, RES_PAGEDESC );
93         aSet.Put(rItem);
94         SwUnoCursorHelper::SetPageDesc( aValue, *m_pDoc, aSet );
95         m_pDoc->SetDefault(aSet.Get(RES_PAGEDESC));
96     }
97     else if ((RES_PARATR_DROP == pMap->nWID && MID_DROPCAP_CHAR_STYLE_NAME == pMap->nMemberId) ||
98              (RES_TXTATR_CHARFMT == pMap->nWID))
99     {
100         OUString uStyle;
101         if(aValue >>= uStyle)
102         {
103             String sStyle;
104             SwStyleNameMapper::FillUIName(uStyle, sStyle, nsSwGetPoolIdFromName::GET_POOLID_CHRFMT, sal_True );
105             SwDocStyleSheet* pStyle =
106                 (SwDocStyleSheet*)m_pDoc->GetDocShell()->GetStyleSheetPool()->Find(sStyle, SFX_STYLE_FAMILY_CHAR);
107             SwFmtDrop* pDrop = 0;
108             SwFmtCharFmt *pCharFmt = 0;
109             if(pStyle)
110             {
111 				rtl::Reference< SwDocStyleSheet > xStyle( new SwDocStyleSheet( *(SwDocStyleSheet*)pStyle ) );
112                 if (RES_PARATR_DROP == pMap->nWID)
113                 {
114                     pDrop = (SwFmtDrop*)rItem.Clone();   // because rItem ist const...
115                     pDrop->SetCharFmt(xStyle->GetCharFmt());
116                     m_pDoc->SetDefault(*pDrop);
117                 }
118                 else // RES_TXTATR_CHARFMT == pMap->nWID
119                 {
120                     pCharFmt = (SwFmtCharFmt*)rItem.Clone();   // because rItem ist const...
121                     pCharFmt->SetCharFmt(xStyle->GetCharFmt());
122                     m_pDoc->SetDefault(*pCharFmt);
123                 }
124             }
125             else
126                 throw lang::IllegalArgumentException();
127             delete pDrop;
128             delete pCharFmt;
129         }
130         else
131             throw lang::IllegalArgumentException();
132     }
133     else
134     {
135         SfxPoolItem * pNewItem = rItem.Clone();
136         pNewItem->PutValue( aValue, pMap->nMemberId);
137         m_pDoc->SetDefault(*pNewItem);
138         delete pNewItem;
139     }
140 }
141 
142 
getPropertyValue(const OUString & rPropertyName)143 Any SAL_CALL SwXTextDefaults::getPropertyValue( const OUString& rPropertyName )
144 		throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
145 {
146     vos::OGuard aGuard( Application::GetSolarMutex());
147     if (!m_pDoc)
148         throw RuntimeException();
149     const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap()->getByName( rPropertyName );
150     if (!pMap)
151         throw UnknownPropertyException(OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Unknown property: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
152     Any aRet;
153     const SfxPoolItem& rItem = m_pDoc->GetDefault(pMap->nWID);
154 	rItem.QueryValue( aRet, pMap->nMemberId );
155     return aRet;
156 }
157 
158 
addPropertyChangeListener(const OUString &,const uno::Reference<XPropertyChangeListener> &)159 void SAL_CALL SwXTextDefaults::addPropertyChangeListener( const OUString& /*rPropertyName*/, const uno::Reference< XPropertyChangeListener >& /*xListener*/ )
160 		throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
161 {
162     DBG_WARNING ( "not implemented" );
163 }
164 
165 
removePropertyChangeListener(const OUString &,const uno::Reference<XPropertyChangeListener> &)166 void SAL_CALL SwXTextDefaults::removePropertyChangeListener( const OUString& /*rPropertyName*/, const uno::Reference< XPropertyChangeListener >& /*xListener*/ )
167 		throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
168 {
169     DBG_WARNING ( "not implemented" );
170 }
171 
172 
addVetoableChangeListener(const OUString &,const uno::Reference<XVetoableChangeListener> &)173 void SAL_CALL SwXTextDefaults::addVetoableChangeListener( const OUString& /*rPropertyName*/, const uno::Reference< XVetoableChangeListener >& /*xListener*/ )
174 		throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
175 {
176     DBG_WARNING ( "not implemented" );
177 }
178 
179 
removeVetoableChangeListener(const OUString &,const uno::Reference<XVetoableChangeListener> &)180 void SAL_CALL SwXTextDefaults::removeVetoableChangeListener( const OUString& /*rPropertyName*/, const uno::Reference< XVetoableChangeListener >& /*xListener*/ )
181 		throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
182 {
183     DBG_WARNING ( "not implemented" );
184 }
185 
186 
187 // XPropertyState
getPropertyState(const OUString & rPropertyName)188 PropertyState SAL_CALL SwXTextDefaults::getPropertyState( const OUString& rPropertyName )
189         throw(UnknownPropertyException, RuntimeException)
190 {
191     vos::OGuard aGuard( Application::GetSolarMutex());
192     PropertyState eRet = PropertyState_DIRECT_VALUE;
193     if (!m_pDoc)
194         throw RuntimeException();
195     const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap()->getByName( rPropertyName );
196     if (!pMap)
197         throw UnknownPropertyException(OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Unknown property: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
198 
199     const SfxPoolItem& rItem = m_pDoc->GetDefault(pMap->nWID);
200     if (IsStaticDefaultItem ( &rItem ) )
201     	eRet = PropertyState_DEFAULT_VALUE;
202     return eRet;
203 }
204 
205 
getPropertyStates(const Sequence<OUString> & rPropertyNames)206 Sequence< PropertyState > SAL_CALL SwXTextDefaults::getPropertyStates( const Sequence< OUString >& rPropertyNames )
207         throw(UnknownPropertyException, RuntimeException)
208 {
209     const sal_Int32 nCount = rPropertyNames.getLength();
210     const OUString * pNames = rPropertyNames.getConstArray();
211     Sequence < PropertyState > aRet ( nCount );
212     PropertyState *pState = aRet.getArray();
213 
214     for ( sal_Int32 nIndex = 0; nIndex < nCount; nIndex++)
215         pState[nIndex] = getPropertyState( pNames[nIndex] );
216 
217     return aRet;
218 }
219 
220 
setPropertyToDefault(const OUString & rPropertyName)221 void SAL_CALL SwXTextDefaults::setPropertyToDefault( const OUString& rPropertyName )
222         throw(UnknownPropertyException, RuntimeException)
223 {
224     if (!m_pDoc)
225         throw RuntimeException();
226     const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap()->getByName( rPropertyName );
227     if (!pMap)
228         throw UnknownPropertyException(OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Unknown property: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
229     if ( pMap->nFlags & PropertyAttribute::READONLY)
230         throw RuntimeException( OUString ( RTL_CONSTASCII_USTRINGPARAM ( "setPropertyToDefault: property is read-only: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
231     SfxItemPool& rSet (m_pDoc->GetAttrPool());
232     rSet.ResetPoolDefaultItem ( pMap->nWID );
233 }
234 
235 
getPropertyDefault(const OUString & rPropertyName)236 Any SAL_CALL SwXTextDefaults::getPropertyDefault( const OUString& rPropertyName )
237         throw(UnknownPropertyException, WrappedTargetException, RuntimeException)
238 {
239     if (!m_pDoc)
240         throw RuntimeException();
241     const SfxItemPropertySimpleEntry *pMap = m_pPropSet->getPropertyMap()->getByName( rPropertyName );
242     if (!pMap)
243         throw UnknownPropertyException(OUString ( RTL_CONSTASCII_USTRINGPARAM ( "Unknown property: " ) ) + rPropertyName, static_cast < cppu::OWeakObject * > ( this ) );
244     Any aRet;
245     SfxItemPool& rSet (m_pDoc->GetAttrPool());
246     const SfxPoolItem *pItem = rSet.GetPoolDefaultItem ( pMap->nWID );
247     pItem->QueryValue( aRet, pMap->nMemberId );
248     return aRet;
249 }
250 
251 
getImplementationName()252 rtl::OUString SAL_CALL SwXTextDefaults::getImplementationName(  )
253     throw (RuntimeException)
254 {
255     return C2U("SwXTextDefaults");
256 }
257 
258 
supportsService(const::rtl::OUString & rServiceName)259 sal_Bool SAL_CALL SwXTextDefaults::supportsService( const ::rtl::OUString& rServiceName )
260     throw (RuntimeException)
261 {
262     return  rServiceName == C2U("com.sun.star.text.Defaults") ||
263             rServiceName == C2U("com.sun.star.style.CharacterProperties") ||
264             rServiceName == C2U("com.sun.star.style.CharacterPropertiesAsian") ||
265             rServiceName == C2U("com.sun.star.style.CharacterPropertiesComplex") ||
266             rServiceName == C2U("com.sun.star.style.ParagraphProperties") ||
267             rServiceName == C2U("com.sun.star.style.ParagraphPropertiesAsian") ||
268             rServiceName == C2U("com.sun.star.style.ParagraphPropertiesComplex");
269 }
270 
271 
getSupportedServiceNames()272 uno::Sequence< ::rtl::OUString > SAL_CALL SwXTextDefaults::getSupportedServiceNames(  )
273     throw (RuntimeException)
274 {
275     uno::Sequence< OUString > aRet(7);
276 	OUString* pArr = aRet.getArray();
277     *pArr++ = C2U("com.sun.star.text.Defaults");
278     *pArr++ = C2U("com.sun.star.style.CharacterProperties");
279     *pArr++ = C2U("com.sun.star.style.CharacterPropertiesAsian");
280     *pArr++ = C2U("com.sun.star.style.CharacterPropertiesComplex");
281     *pArr++ = C2U("com.sun.star.style.ParagraphProperties");
282     *pArr++ = C2U("com.sun.star.style.ParagraphPropertiesAsian");
283     *pArr++ = C2U("com.sun.star.style.ParagraphPropertiesComplex");
284 	return aRet;
285 }
286 
287 
288 
289