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_unotools.hxx"
30 #ifndef GCC
31 #endif
32 
33 #ifndef _unotools_JAVAPTIONS_HXX
34 #include <unotools/javaoptions.hxx>
35 #endif
36 #include <com/sun/star/uno/Any.h>
37 #include <com/sun/star/uno/Sequence.hxx>
38 #include <rtl/logfile.hxx>
39 
40 
41 using namespace ::com::sun::star::uno;
42 using namespace ::rtl;
43 
44 #define C2U(cChar) OUString::createFromAscii(cChar)
45 #define CFG_READONLY_DEFAULT    sal_False
46 /* -----------------------------10.04.01 12:39--------------------------------
47 
48  ---------------------------------------------------------------------------*/
49 class SvtExecAppletsItem_Impl : public utl::ConfigItem
50 {
51     sal_Bool  bExecute;
52     sal_Bool  bRO;
53 public:
54     SvtExecAppletsItem_Impl();
55 
56     virtual void    Commit();
57 	void Notify( const Sequence< rtl::OUString >&  );
58 
59     sal_Bool IsExecuteApplets() const {return bExecute;}
60     void     SetExecuteApplets(sal_Bool bSet);
61     sal_Bool IsReadOnly() const {return bRO;}
62 };
63 /* -----------------------------10.02.2003 07:46------------------------------
64 
65  ---------------------------------------------------------------------------*/
66 void SvtExecAppletsItem_Impl::SetExecuteApplets(sal_Bool bSet)
67 {
68     OSL_ENSURE(!bRO, "SvtExecAppletsItem_Impl::SetExecuteApplets()\nYou tried to write on a readonly value!\n");
69     if (!bRO)
70     {
71         bExecute = bSet;
72         SetModified();
73     }
74 }
75 /* -----------------------------18.05.01 14:44--------------------------------
76 
77  ---------------------------------------------------------------------------*/
78 SvtExecAppletsItem_Impl::SvtExecAppletsItem_Impl() :
79         utl::ConfigItem(C2U("Office.Common/Java/Applet")),
80         bExecute       (sal_False                       ),
81         bRO            (CFG_READONLY_DEFAULT            )
82 {
83     RTL_LOGFILE_CONTEXT(aLog, "unotools SvtExecAppletsItem_Impl::SvtExecAppletsItem_Impl()");
84 
85     Sequence< OUString > aNames(1);
86     aNames.getArray()[0] = C2U("Enable");
87     Sequence< Any > aValues = GetProperties(aNames);
88     Sequence< sal_Bool > aROStates = GetReadOnlyStates(aNames);
89     const Any* pValues = aValues.getConstArray();
90     const sal_Bool* pROStates = aROStates.getConstArray();
91     if(aValues.getLength() && aROStates.getLength() && pValues[0].hasValue())
92     {
93         bExecute = *(sal_Bool*)pValues[0].getValue();
94         bRO = pROStates[0];
95     }
96 }
97 void    SvtExecAppletsItem_Impl::Commit()
98 {
99     if (bRO)
100         return;
101 
102     Sequence< OUString > aNames(1);
103     aNames.getArray()[0] = C2U("Enable");
104     Sequence< Any > aValues(1);
105     aValues.getArray()[0].setValue(&bExecute, ::getBooleanCppuType());
106     PutProperties(aNames, aValues);
107 }
108 
109 void SvtExecAppletsItem_Impl::Notify( const Sequence< rtl::OUString >&  )
110 {
111 	// no listeners supported yet
112 }
113 
114 struct SvtJavaOptions_Impl
115 {
116     SvtExecAppletsItem_Impl aExecItem;
117     Sequence<OUString>      aPropertyNames;
118     sal_Bool                bEnabled;
119     sal_Bool                bSecurity;
120     sal_Int32               nNetAccess;
121     rtl::OUString           sUserClassPath;
122 
123     sal_Bool                bROEnabled;
124     sal_Bool                bROSecurity;
125     sal_Bool                bRONetAccess;
126     sal_Bool                bROUserClassPath;
127 
128     SvtJavaOptions_Impl() :
129         aPropertyNames(4),
130         bEnabled            (sal_False),
131         bSecurity           (sal_False),
132         nNetAccess          (0),
133         bROEnabled          (CFG_READONLY_DEFAULT),
134         bROSecurity         (CFG_READONLY_DEFAULT),
135         bRONetAccess        (CFG_READONLY_DEFAULT),
136         bROUserClassPath    (CFG_READONLY_DEFAULT)
137         {
138             OUString* pNames = aPropertyNames.getArray();
139             pNames[0] = C2U("Enable");
140             pNames[1] = C2U("Security");
141             pNames[2] = C2U("NetAccess");
142             pNames[3] = C2U("UserClassPath");
143         }
144 };
145 /* -----------------------------18.05.01 13:28--------------------------------
146 
147  ---------------------------------------------------------------------------*/
148 SvtJavaOptions::SvtJavaOptions() :
149     utl::ConfigItem(C2U("Office.Java/VirtualMachine")),
150     pImpl(new SvtJavaOptions_Impl)
151 {
152     RTL_LOGFILE_CONTEXT(aLog, "unotools SvtJavaOptions::SvtJavaOptions()");
153 
154     Sequence< Any > aValues = GetProperties(pImpl->aPropertyNames);
155     Sequence< sal_Bool > aROStates = GetReadOnlyStates(pImpl->aPropertyNames);
156     const Any* pValues = aValues.getConstArray();
157     const sal_Bool* pROStates = aROStates.getConstArray();
158     if ( aValues.getLength() == pImpl->aPropertyNames.getLength() && aROStates.getLength() == pImpl->aPropertyNames.getLength() )
159 	{
160         for ( int nProp = 0; nProp < pImpl->aPropertyNames.getLength(); nProp++ )
161 		{
162             if( pValues[nProp].hasValue() )
163 			{
164                 switch ( nProp )
165                 {
166                     case 0: pImpl->bEnabled = *(sal_Bool*)pValues[nProp].getValue(); break;
167                     case 1: pImpl->bSecurity = *(sal_Bool*)pValues[nProp].getValue();break;
168                     case 2: pValues[nProp] >>= pImpl->nNetAccess; break;
169                     case 3: pValues[nProp] >>= pImpl->sUserClassPath; break;
170                 }
171             }
172         }
173         pImpl->bROEnabled = pROStates[0];
174         pImpl->bROSecurity = pROStates[1];
175         pImpl->bRONetAccess = pROStates[2];
176         pImpl->bROUserClassPath = pROStates[3];
177     }
178 }
179 /* -----------------------------18.05.01 13:28--------------------------------
180 
181  ---------------------------------------------------------------------------*/
182 SvtJavaOptions::~SvtJavaOptions()
183 {
184     delete pImpl;
185 }
186 /*-- 18.05.01 13:28:35---------------------------------------------------
187 
188   -----------------------------------------------------------------------*/
189 void    SvtJavaOptions::Commit()
190 {
191     pImpl->aExecItem.Commit();
192 
193     sal_Int32 nOrgCount = pImpl->aPropertyNames.getLength();
194     Sequence< OUString > aNames(nOrgCount);
195     Sequence< Any > aValues(nOrgCount);
196     sal_Int32 nRealCount = 0;
197 
198 	const Type& rType = ::getBooleanCppuType();
199     for(int nProp = 0; nProp < nOrgCount; nProp++)
200 	{
201 		switch(nProp)
202 		{
203             case  0:
204             {
205                 if (!pImpl->bROEnabled)
206                 {
207                     aValues[nRealCount].setValue(&pImpl->bEnabled, rType);
208                     aNames[nRealCount] = pImpl->aPropertyNames[nProp];
209                     ++nRealCount;
210                 }
211             }
212             break;
213             case  1:
214             {
215                 if (!pImpl->bROSecurity)
216                 {
217                     aValues[nRealCount].setValue(&pImpl->bSecurity, rType);
218                     aNames[nRealCount] = pImpl->aPropertyNames[nProp];
219                     ++nRealCount;
220                 }
221             }
222             break;
223             case  2:
224             {
225                 if (!pImpl->bRONetAccess)
226                 {
227                     aValues[nRealCount] <<= pImpl->nNetAccess;
228                     aNames[nRealCount] = pImpl->aPropertyNames[nProp];
229                     ++nRealCount;
230                 }
231             }
232             break;
233             case  3:
234             {
235                 if (!pImpl->bROUserClassPath)
236                 {
237                     aValues[nRealCount] <<= pImpl->sUserClassPath;
238                     aNames[nRealCount] = pImpl->aPropertyNames[nProp];
239                     ++nRealCount;
240                 }
241             }
242             break;
243 		}
244 	}
245     aValues.realloc(nRealCount);
246     aNames.realloc(nRealCount);
247     PutProperties(aNames,aValues);
248 }
249 /*-- 18.05.01 13:28:35---------------------------------------------------
250 
251   -----------------------------------------------------------------------*/
252 sal_Bool        SvtJavaOptions::IsEnabled() const
253 {
254     return pImpl->bEnabled;
255 }
256 /*-- 18.05.01 13:28:35---------------------------------------------------
257 
258   -----------------------------------------------------------------------*/
259 sal_Bool        SvtJavaOptions::IsSecurity()const
260 {
261     return pImpl->bSecurity;
262 }
263 /*-- 18.05.01 13:28:35---------------------------------------------------
264 
265   -----------------------------------------------------------------------*/
266 sal_Int32       SvtJavaOptions::GetNetAccess() const
267 {
268     return pImpl->nNetAccess;
269 }
270 /*-- 18.05.01 13:28:36---------------------------------------------------
271 
272   -----------------------------------------------------------------------*/
273 rtl::OUString&  SvtJavaOptions::GetUserClassPath()const
274 {
275     return pImpl->sUserClassPath;
276 }
277 /*-- 18.05.01 13:28:37---------------------------------------------------
278 
279   -----------------------------------------------------------------------*/
280 void SvtJavaOptions::SetEnabled(sal_Bool bSet)
281 {
282     OSL_ENSURE(!pImpl->bROEnabled, "SvtJavaOptions::SetEnabled()\nYou tried to write on a readonly value!\n");
283     if(!pImpl->bROEnabled && pImpl->bEnabled != bSet)
284     {
285         pImpl->bEnabled = bSet;
286         SetModified();
287     }
288 }
289 /*-- 18.05.01 13:28:38---------------------------------------------------
290 
291   -----------------------------------------------------------------------*/
292 void SvtJavaOptions::SetSecurity(sal_Bool bSet)
293 {
294     OSL_ENSURE(!pImpl->bROSecurity, "SvtJavaOptions::SetSecurity()\nYou tried to write on a readonly value!\n");
295     if(!pImpl->bROSecurity && pImpl->bSecurity != bSet)
296     {
297         pImpl->bSecurity = bSet;
298         SetModified();
299     }
300 }
301 /*-- 18.05.01 13:28:38---------------------------------------------------
302 
303   -----------------------------------------------------------------------*/
304 void SvtJavaOptions::SetNetAccess(sal_Int32 nSet)
305 {
306     OSL_ENSURE(!pImpl->bRONetAccess, "SvtJavaOptions::SetNetAccess()\nYou tried to write on a readonly value!\n");
307     if(!pImpl->bRONetAccess && pImpl->nNetAccess != nSet)
308     {
309         pImpl->nNetAccess = nSet;
310         SetModified();
311     }
312 }
313 /*-- 18.05.01 13:28:38---------------------------------------------------
314 
315   -----------------------------------------------------------------------*/
316 void SvtJavaOptions::SetUserClassPath(const rtl::OUString& rSet)
317 {
318     OSL_ENSURE(!pImpl->bROUserClassPath, "SvtJavaOptions::SetUserClassPath()\nYou tried to write on a readonly value!\n");
319     if(!pImpl->bROUserClassPath && pImpl->sUserClassPath != rSet)
320     {
321         pImpl->sUserClassPath = rSet;
322         SetModified();
323     }
324 }
325 
326 /*-- 18.05.01 14:34:32---------------------------------------------------
327 
328   -----------------------------------------------------------------------*/
329 sal_Bool        SvtJavaOptions::IsExecuteApplets() const
330 {
331     return pImpl->aExecItem.IsExecuteApplets();
332 }
333 /*-- 18.05.01 14:34:32---------------------------------------------------
334 
335   -----------------------------------------------------------------------*/
336 void SvtJavaOptions::SetExecuteApplets(sal_Bool bSet)
337 {
338     if(!pImpl->aExecItem.IsReadOnly() && pImpl->aExecItem.IsExecuteApplets() != bSet)
339     {
340         pImpl->aExecItem.SetExecuteApplets(bSet);
341         SetModified();
342     }
343 }
344 /*--10.02.2003 08:40---------------------------------------------------
345 
346 -----------------------------------------------------------------------*/
347 sal_Bool SvtJavaOptions::IsReadOnly( EOption eOption ) const
348 {
349     sal_Bool bRO = sal_True;
350     switch(eOption)
351     {
352         case E_ENABLED :
353             bRO = pImpl->bROEnabled;
354             break;
355         case E_SECURITY :
356             bRO = pImpl->bROSecurity;
357             break;
358         case E_NETACCESS :
359             bRO = pImpl->bRONetAccess;
360             break;
361         case E_USERCLASSPATH :
362             bRO = pImpl->bROUserClassPath;
363             break;
364         case E_EXECUTEAPPLETS :
365             bRO = pImpl->aExecItem.IsReadOnly();
366             break;
367     }
368     return bRO;
369 }
370