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