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_scripting.hxx"
26 #include <cppuhelper/implementationentry.hxx>
27 
28 #include <hash_map>
29 
30 #include <osl/file.hxx>
31 #include <cppuhelper/implbase1.hxx>
32 
33 #include <com/sun/star/beans/XPropertyContainer.hpp>
34 #include <com/sun/star/beans/PropertyAttribute.hpp>
35 
36 #include <drafts/com/sun/star/script/framework/storage/XScriptStorageManager.hpp>
37 
38 #include <util/util.hxx>
39 #include "ScriptInfo.hxx"
40 
41 using namespace ::rtl;
42 using namespace com::sun::star;
43 using namespace ::com::sun::star::uno;
44 using namespace ::drafts::com::sun::star::script::framework;
45 using namespace ::drafts::com::sun::star::script::framework::storage;
46 
47 namespace scripting_impl
48 {
49 
50 typedef ::std::hash_map < ::rtl::OUString, css::uno::Any, ::rtl::OUStringHash,
51     ::std::equal_to< ::rtl::OUString > > PropertySet_hash;
52 
53 class PropertySetImpl : public ::cppu::WeakImplHelper1< css::beans::XPropertySet >
54 {
55 
56 public:
57 
58     PropertySetImpl();
59     ~PropertySetImpl();
60 
61 // XPropertySet implementation
62     virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL
63         getPropertySetInfo()
64         throw ( css::uno::RuntimeException );
65     virtual void SAL_CALL setPropertyValue( const ::rtl::OUString& aPropertyName,
66         const css::uno::Any& aValue )
67         throw ( css::beans::UnknownPropertyException,
68             css::beans::PropertyVetoException,
69             css::lang::IllegalArgumentException,
70             css::lang::WrappedTargetException,
71             css::uno::RuntimeException );
72     virtual css::uno::Any SAL_CALL getPropertyValue( const ::rtl::OUString& PropertyName )
73         throw ( css::beans::UnknownPropertyException,
74             css::lang::WrappedTargetException,
75             css::uno::RuntimeException );
76     virtual void SAL_CALL addPropertyChangeListener( const ::rtl::OUString& aPropertyName,
77         const css::uno::Reference< css::beans::XPropertyChangeListener >& xListener )
78         throw ( css::beans::UnknownPropertyException,
79             css::lang::WrappedTargetException,
80             css::uno::RuntimeException );
81     virtual void SAL_CALL removePropertyChangeListener(
82         const ::rtl::OUString& aPropertyName,
83         const css::uno::Reference< css::beans::XPropertyChangeListener >& aListener )
84         throw ( css::beans::UnknownPropertyException,
85             css::lang::WrappedTargetException,
86             css::uno::RuntimeException );
87     virtual void SAL_CALL addVetoableChangeListener(
88         const ::rtl::OUString& PropertyName,
89         const css::uno::Reference< css::beans::XVetoableChangeListener >& aListener )
90         throw ( css::beans::UnknownPropertyException,
91             css::lang::WrappedTargetException,
92             css::uno::RuntimeException );
93     virtual void SAL_CALL removeVetoableChangeListener(
94         const ::rtl::OUString& PropertyName,
95         const css::uno::Reference< css::beans::XVetoableChangeListener >& aListener )
96         throw ( css::beans::UnknownPropertyException,
97             css::lang::WrappedTargetException,
98             css::uno::RuntimeException );
99 
100 private:
101     friend class ScriptInfo;
102 
103     css::uno::Reference< css::uno::XComponentContext > m_xContext;
104     void PropertySetImpl::privateSetPropertyValue( const ::rtl::OUString& aPropertyName, const Any& aValue )
105     throw ( beans::UnknownPropertyException, beans::PropertyVetoException,
106             lang::IllegalArgumentException, lang::WrappedTargetException,
107             RuntimeException );
108 
109     osl::Mutex m_mutex;
110     PropertySet_hash m_propertyMap;
111 };
112 
PropertySetImpl()113 PropertySetImpl::PropertySetImpl()
114 {
115    OSL_TRACE( "<PropertySetImpl ctor called\n" );
116 }
117 
~PropertySetImpl()118 PropertySetImpl::~PropertySetImpl()
119 {
120     OSL_TRACE( "<PropertySetImpl dtor called\n>" );
121 }
122 
getPropertySetInfo()123 Reference< beans::XPropertySetInfo > SAL_CALL PropertySetImpl::getPropertySetInfo( )
124     throw ( RuntimeException )
125 {
126     return Reference< beans::XPropertySetInfo > (); // Not supported
127 }
128 
setPropertyValue(const::rtl::OUString & aPropertyName,const Any & aValue)129 void SAL_CALL PropertySetImpl::setPropertyValue( const ::rtl::OUString& aPropertyName,
130     const Any& aValue )
131     throw ( beans::UnknownPropertyException, beans::PropertyVetoException,
132             lang::IllegalArgumentException, lang::WrappedTargetException,
133             RuntimeException )
134 {
135     throw RuntimeException(
136         OUSTR( "PropertySetImpl::setPropertyValue: method not supported. Read-only PropertySet" ),
137         Reference< XInterface >() );
138 }
139 
privateSetPropertyValue(const::rtl::OUString & aPropertyName,const Any & aValue)140 void PropertySetImpl::privateSetPropertyValue( const ::rtl::OUString& aPropertyName,
141     const Any& aValue )
142     throw ( beans::UnknownPropertyException, beans::PropertyVetoException,
143             lang::IllegalArgumentException, lang::WrappedTargetException,
144             RuntimeException )
145 {
146     ::osl::Guard< osl::Mutex > aGuard( m_mutex );
147     m_propertyMap[ aPropertyName ] = aValue;
148 }
149 
150 //*************************************************************************
getPropertyValue(const::rtl::OUString & PropertyName)151 Any SAL_CALL PropertySetImpl::getPropertyValue( const ::rtl::OUString& PropertyName )
152     throw ( beans::UnknownPropertyException,
153             lang::WrappedTargetException, RuntimeException )
154 {
155     if ( m_propertyMap.find( PropertyName ) == m_propertyMap.end() )
156     {
157         throw RuntimeException(
158 
159             OUSTR( "PropertySetImpl::getPropertyValue: invalid PropertyName ").concat(
160                 PropertyName),
161             Reference< XInterface >() );
162     }
163 
164     ::osl::Guard< osl::Mutex > aGuard( m_mutex );
165     Any returnValue = m_propertyMap[ PropertyName ];
166 
167     return returnValue;
168 }
169 
170 //*************************************************************************
addPropertyChangeListener(const::rtl::OUString & aPropertyName,const Reference<beans::XPropertyChangeListener> & xListener)171 void SAL_CALL PropertySetImpl::addPropertyChangeListener(
172     const ::rtl::OUString& aPropertyName,
173     const Reference< beans::XPropertyChangeListener >& xListener )
174     throw ( beans::UnknownPropertyException, lang::WrappedTargetException,
175             RuntimeException )
176 {
177     throw RuntimeException(
178         OUSTR( "PropertySetImpl::addPropertyChangeListener: method not supported" ),
179         Reference< XInterface >() );
180 }
181 
182 //*************************************************************************
removePropertyChangeListener(const::rtl::OUString & aPropertyName,const Reference<beans::XPropertyChangeListener> & aListener)183 void SAL_CALL PropertySetImpl::removePropertyChangeListener(
184     const ::rtl::OUString& aPropertyName,
185     const Reference< beans::XPropertyChangeListener >& aListener )
186     throw ( beans::UnknownPropertyException, lang::WrappedTargetException,
187             RuntimeException )
188 {
189     throw RuntimeException(
190         OUSTR( "PropertySetImpl::removePropertyChangeListener: method not supported" ),
191         Reference< XInterface >() );
192 }
193 
194 //*************************************************************************
addVetoableChangeListener(const::rtl::OUString & PropertyName,const Reference<beans::XVetoableChangeListener> & aListener)195 void SAL_CALL PropertySetImpl::addVetoableChangeListener(
196     const ::rtl::OUString& PropertyName,
197     const Reference< beans::XVetoableChangeListener >& aListener )
198     throw ( beans::UnknownPropertyException, lang::WrappedTargetException,
199             RuntimeException )
200 {
201     throw RuntimeException(
202         OUSTR( "PropertySetImpl::addVetoableChangeListener: method not supported" ),
203         Reference< XInterface >() );
204 }
205 
206 //*************************************************************************
removeVetoableChangeListener(const::rtl::OUString & PropertyName,const Reference<beans::XVetoableChangeListener> & aListener)207 void SAL_CALL PropertySetImpl::removeVetoableChangeListener(
208     const ::rtl::OUString& PropertyName,
209     const Reference< beans::XVetoableChangeListener >& aListener )
210     throw ( beans::UnknownPropertyException, lang::WrappedTargetException,
211             RuntimeException )
212 {
213     throw RuntimeException(
214         OUSTR( "PropertySetImpl::removeVetoableChangeListener: method not supported" ),
215         Reference< XInterface >() );
216 }
217 
218 
219 //*************************************************************************
ScriptInfo(const ScriptData & scriptData,sal_Int32 storageID)220 ScriptInfo::ScriptInfo( const ScriptData & scriptData, sal_Int32 storageID )
221         : m_scriptData( scriptData ), m_storageID( storageID )
222 {
223     OSL_TRACE( "< ++++++ ScriptInfo ctor called >\n" );
224     OSL_TRACE( "< ++++++ parcelURI=%s>\n",::rtl::OUStringToOString(m_scriptData.parcelURI , RTL_TEXTENCODING_ASCII_US ).pData->buffer );
225 }
226 //*************************************************************************
~ScriptInfo()227 ScriptInfo::~ScriptInfo()
228 {
229     OSL_TRACE( "< ScriptInfo dtor called >\n" );
230 }
231 //*************************************************************************
getLogicalName()232 OUString SAL_CALL ScriptInfo::getLogicalName(  ) throw ( RuntimeException )
233 {
234     OSL_TRACE( "ScriptInfo::getLogicalName() " );
235     return m_scriptData.logicalname;
236 }
237 
238 //*************************************************************************
getDescription()239 OUString SAL_CALL ScriptInfo::getDescription(  ) throw ( RuntimeException )
240 {
241     OUString rs_desc;
242     // TDB need to determine locale here, hardcoded at the moment
243     // to english
244 
245     OUString localeLang = OUString::createFromAscii( "en" );
246     strpair_map::const_iterator str_it =
247         m_scriptData.locales.find( localeLang );
248 
249     if( str_it == m_scriptData.locales.end() )
250     {
251         OSL_TRACE( "No description set in meta-data" );
252         return rs_desc;
253     }
254     rs_desc = str_it->second.second;
255     return rs_desc;
256 }
257 
258 //*************************************************************************
getLanguage()259 OUString SAL_CALL ScriptInfo::getLanguage(  ) throw ( RuntimeException )
260 {
261     OSL_TRACE( "ScriptInfo::getLanguage() " );
262     return m_scriptData.language;
263 }
264 
265 //*************************************************************************
getFunctionName()266 OUString SAL_CALL ScriptInfo::getFunctionName(  ) throw ( RuntimeException )
267 {
268     OSL_TRACE( "ScriptInfo::getFunctionName() " );
269     return m_scriptData.functionname;
270 }
271 
272 //*************************************************************************
getParcelURI()273 OUString SAL_CALL ScriptInfo::getParcelURI(  ) throw ( RuntimeException )
274 {
275     return m_scriptData.parcelURI;
276 }
277 
278 //*************************************************************************
getLanguageProperties()279 Reference< beans::XPropertySet > SAL_CALL ScriptInfo::getLanguageProperties(  )
280     throw ( RuntimeException )
281 {
282     PropertySetImpl* propSetImpl = new PropertySetImpl();
283     Reference< beans::XPropertySet > xPropSet = propSetImpl;
284 
285     props_vec::const_iterator pv_it = m_scriptData.languagedepprops.begin();
286     props_vec::const_iterator pv_itend = m_scriptData.languagedepprops.end();
287 
288     for( ; pv_it != pv_itend; ++pv_it )
289     {
290         try
291         {
292             propSetImpl->privateSetPropertyValue( pv_it->first, makeAny( pv_it->second ) );
293         }
294         catch( Exception& e )
295         {
296             OUString msg = OUSTR(
297                 "ScriptInfo::getLanguage caught exception while setting property," );
298             msg = msg.concat( OUSTR( " PropertryName: " ) ).concat( pv_it->first );
299             msg = msg.concat( OUSTR( " \nException message is: " ) );
300             msg = msg.concat( e.Message );
301             throw RuntimeException( msg , Reference< XInterface >() );
302         }
303     }
304 
305     return xPropSet;
306 }
307 //*************************************************************************
getFileSetNames()308 css::uno::Sequence< ::rtl::OUString > SAL_CALL ScriptInfo::getFileSetNames()
309 throw ( css::uno::RuntimeException )
310 {
311     OSL_TRACE("ScriptInfo::getFileSetNames");
312     Sequence< OUString > results;
313     filesets_map::iterator fsm_it = m_scriptData.filesets.begin();
314     filesets_map::iterator fsm_itend = m_scriptData.filesets.end();
315     if( fsm_it == fsm_itend )
316     {
317         OSL_TRACE( "ScriptInfo::getFileSetNames: no filesets" );
318         return results;
319     }
320     results.realloc( m_scriptData.filesets.size() );
321     for ( sal_Int32 count = 0; fsm_it != fsm_itend; ++fsm_it )
322     {
323         OUString fileSetName = fsm_it->first;
324         OSL_TRACE( "ScriptInfo::getFileSetNames: adding name %s",
325             ::rtl::OUStringToOString( fileSetName,
326                 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
327         results[ count++ ] = fileSetName;
328     }
329     return results;
330 }
331 //*************************************************************************
332 css::uno::Sequence< ::rtl::OUString > SAL_CALL
getFilesInFileSet(const::rtl::OUString & fileSetName)333 ScriptInfo::getFilesInFileSet( const ::rtl::OUString & fileSetName )
334 throw ( css::uno::RuntimeException )
335 {
336     Sequence< OUString > results;
337     filesets_map::iterator fsm_it = m_scriptData.filesets.find( fileSetName );
338     filesets_map::iterator fsm_itend = m_scriptData.filesets.end();
339     if( fsm_it == fsm_itend )
340     {
341         OSL_TRACE( "ScriptInfo::getFilesInFileSet: no fileset named %s",
342             ::rtl::OUStringToOString( fileSetName,
343                 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
344         return results;
345     }
346 
347     strpairvec_map files = fsm_it->second.second;
348     strpairvec_map::iterator spvm_it = files.begin();
349     strpairvec_map::iterator spvm_itend = files.end();
350     if( spvm_it == spvm_itend )
351     {
352         OSL_TRACE( "ScriptInfo::getFilesInFileSet: no files in fileset %s",
353             ::rtl::OUStringToOString( fileSetName,
354                 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
355         return results;
356     }
357     results.realloc( files.size() );
358     for( sal_Int32 count = 0; spvm_it != spvm_itend ; ++spvm_it )
359     {
360         OUString fileName = spvm_it->first;
361         OSL_TRACE( "ScriptInfo::getFilesInFileSet: adding file %s",
362             ::rtl::OUStringToOString( fileName,
363                 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
364         results[ count++ ] = fileName;
365     }
366     return results;
367 }
368 //*************************************************************************
369 }
370