xref: /aoo4110/main/desktop/source/so_comp/oemjob.cxx (revision b1cdbd2c)
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_desktop.hxx"
26 
27 #include "oemjob.hxx"
28 #include <rtl/bootstrap.hxx>
29 #include <rtl/ustrbuf.hxx>
30 #include <osl/file.hxx>
31 #include <unotools/bootstrap.hxx>
32 #include <tools/config.hxx>
33 #include <com/sun/star/frame/XDesktop.hpp>
34 #include <com/sun/star/frame/XFrame.hpp>
35 #include <com/sun/star/frame/XModel.hpp>
36 #include <com/sun/star/util/XCloseable.hpp>
37 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
38 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
39 #include <com/sun/star/beans/NamedValue.hpp>
40 
41 using namespace rtl;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::beans;
45 using namespace ::com::sun::star::ui::dialogs;
46 using namespace ::com::sun::star::frame;
47 using namespace ::com::sun::star::util;
48 
49 namespace desktop{
50 
51 char const OEM_PRELOAD_SECTION[] = "Bootstrap";
52 char const OEM_PRELOAD[]		 = "Preload";
53 char const STR_TRUE[]			 = "1";
54 char const STR_FALSE[]			 = "0";
55 
56 const char* OEMPreloadJob::interfaces[] =
57 {
58     "com.sun.star.task.XJob",
59     NULL,
60 };
61 const char* OEMPreloadJob::implementationName = "com.sun.star.comp.desktop.OEMPreloadJob";
62 const char* OEMPreloadJob::serviceName = "com.sun.star.office.OEMPreloadJob";
63 
GetImplementationName()64 OUString OEMPreloadJob::GetImplementationName()
65 {
66 	return OUString( RTL_CONSTASCII_USTRINGPARAM( implementationName));
67 }
68 
GetSupportedServiceNames()69 Sequence< OUString > OEMPreloadJob::GetSupportedServiceNames()
70 {
71 	sal_Int32 nSize = (sizeof( interfaces ) / sizeof( const char *)) - 1;
72 	Sequence< OUString > aResult( nSize );
73 
74 	for( sal_Int32 i = 0; i < nSize; i++ )
75 		aResult[i] = OUString::createFromAscii( interfaces[i] );
76 	return aResult;
77 }
78 
CreateInstance(const Reference<XMultiServiceFactory> & rSMgr)79 Reference< XInterface >  SAL_CALL OEMPreloadJob::CreateInstance(
80     const Reference< XMultiServiceFactory >& rSMgr )
81 {
82 	static osl::Mutex	aMutex;
83 		osl::MutexGuard guard( aMutex );
84 		return (XComponent*) ( new OEMPreloadJob( rSMgr ) );
85 }
86 
OEMPreloadJob(const Reference<XMultiServiceFactory> & xFactory)87 OEMPreloadJob::OEMPreloadJob( const Reference< XMultiServiceFactory >& xFactory ) :
88 	m_aListeners( m_aMutex ),
89 	m_xServiceManager( xFactory )
90 {
91 }
92 
~OEMPreloadJob()93 OEMPreloadJob::~OEMPreloadJob()
94 {
95 }
96 
97 // XComponent
dispose()98 void SAL_CALL OEMPreloadJob::dispose() throw ( RuntimeException )
99 {
100     EventObject aObject;
101     aObject.Source = (XComponent*)this;
102     m_aListeners.disposeAndClear( aObject );
103 }
104 
addEventListener(const Reference<XEventListener> & aListener)105 void SAL_CALL OEMPreloadJob::addEventListener( const Reference< XEventListener > & aListener) throw ( RuntimeException )
106 {
107     m_aListeners.addInterface( aListener );
108 }
109 
removeEventListener(const Reference<XEventListener> & aListener)110 void SAL_CALL OEMPreloadJob::removeEventListener( const Reference< XEventListener > & aListener ) throw ( RuntimeException )
111 {
112     m_aListeners.removeInterface( aListener );
113 }
114 
115 // XServiceInfo
getImplementationName()116 ::rtl::OUString SAL_CALL OEMPreloadJob::getImplementationName()
117 throw ( RuntimeException )
118 {
119 	return OEMPreloadJob::GetImplementationName();
120 }
121 
supportsService(const::rtl::OUString & rServiceName)122 sal_Bool SAL_CALL OEMPreloadJob::supportsService( const ::rtl::OUString& rServiceName )
123 throw ( RuntimeException )
124 {
125 	sal_Int32 nSize = sizeof( interfaces ) / sizeof( const char *);
126 
127 	for( sal_Int32 i = 0; i < nSize; i++ )
128 		if ( rServiceName.equalsAscii( interfaces[i] ))
129 			return sal_True;
130 	return sal_False;
131 }
132 
getSupportedServiceNames()133 Sequence< ::rtl::OUString > SAL_CALL OEMPreloadJob::getSupportedServiceNames()
134 throw ( RuntimeException )
135 {
136 	return OEMPreloadJob::GetSupportedServiceNames();
137 }
138 
139 // XJob
execute(const Sequence<NamedValue> &)140 Any SAL_CALL OEMPreloadJob::execute(const Sequence<NamedValue>&)
141 throw ( RuntimeException )
142 {
143     sal_Bool bCont = sal_False;
144     // are we an OEM version at all?
145     if (checkOEMPreloadFlag())
146     {
147         // create OEM preload service dialog
148         Reference <XExecutableDialog> xDialog( m_xServiceManager->createInstance(
149             OUString::createFromAscii("org.openoffice.comp.preload.OEMPreloadWizard")),
150             UNO_QUERY );
151         if ( xDialog.is() ){
152 	        // execute OEM preload dialog and check return value
153             if ( xDialog->execute() == ExecutableDialogResults::OK ) {
154                 // user accepted.
155                 // make sure the job does not get called again.
156                 bCont = sal_True;
157                 disableOEMPreloadFlag();
158             } else {
159                 // user declined...
160                 // terminate.
161                 /*
162 		        Reference< XDesktop > xDesktop( m_xServiceManager->createInstance(
163                     OUString::createFromAscii("com.sun.star.frame.Desktop")),
164                     UNO_QUERY );
165 		        xDesktop->terminate();
166                 */
167                 /*
168                 OUString aName;
169                 OUString aEnvType;
170                 Reference<XFrame> rFrame;
171                 Reference<XModel> rModel;
172                 Reference<XCloseable> rClose;
173                 for (int i=0; i<args.getLength(); i++)
174                 {
175                     if (args[i].Name.equalsAscii("EnvType"))
176                         args[i].Value >>= aEnvType;
177                     else if (args[i].Name.equalsAscii("Frame")) {
178                         args[i].Value >>= rFrame;
179                         rClose = Reference<XCloseable>(rFrame, UNO_QUERY);
180                     }
181                     else if (args[i].Name.equalsAscii("Model")) {
182                         args[i].Value >>= rModel;
183                         rClose = Reference<XCloseable>(rModel, UNO_QUERY);
184                     }
185                 }
186                 if (rClose.is()) rClose->close(sal_True);
187                 */
188                 bCont = sal_False;
189             }
190         }
191     } else {
192         // don't try again
193         bCont = sal_True;
194     }
195     /*
196     NamedValue nv;
197     nv.Name  = OUString::createFromAscii("Deactivate");
198     nv.Value <<=  bDeactivate;
199     Sequence<NamedValue> s(1);
200     s[0] = nv;
201     */
202     Any r;
203     r <<= bCont;
204     return r;
205 }
206 
207 
existsURL(OUString const & _sURL)208 static sal_Bool existsURL( OUString const& _sURL )
209 {
210     using namespace osl;
211 	DirectoryItem aDirItem;
212 
213     if (_sURL.getLength() != 0)
214         return ( DirectoryItem::get( _sURL, aDirItem ) == DirectoryItem::E_None );
215 
216     return sal_False;
217 }
218 
219 
220 // locate soffice.ini/.rc file
locateIniFile()221 static OUString locateIniFile()
222 {
223 	OUString aUserDataPath;
224 	OUString aSofficeIniFileURL;
225 
226 	// Retrieve the default file URL for the soffice.ini/rc
227 	Bootstrap().getIniName( aSofficeIniFileURL );
228 
229 	if ( utl::Bootstrap::locateUserData( aUserDataPath ) == utl::Bootstrap::PATH_EXISTS )
230 	{
231 		const char CONFIG_DIR[] = "/config";
232 
233 		sal_Int32 nIndex = aSofficeIniFileURL.lastIndexOf( '/');
234 		if ( nIndex > 0 )
235 		{
236 			OUString		aUserSofficeIniFileURL;
237 			OUStringBuffer	aBuffer( aUserDataPath );
238 			aBuffer.appendAscii( CONFIG_DIR );
239 			aBuffer.append( aSofficeIniFileURL.copy( nIndex ));
240 			aUserSofficeIniFileURL = aBuffer.makeStringAndClear();
241 
242 			if ( existsURL( aUserSofficeIniFileURL ))
243 				return aUserSofficeIniFileURL;
244 		}
245 	}
246 	// Fallback try to use the soffice.ini/rc from program folder
247 	return aSofficeIniFileURL;
248 }
249 
250 // check whether the OEMPreload flag was set in soffice.ini/.rc
checkOEMPreloadFlag()251 sal_Bool OEMPreloadJob::checkOEMPreloadFlag()
252 {
253 	OUString aSofficeIniFileURL;
254 	aSofficeIniFileURL = locateIniFile();
255 	Config aConfig(aSofficeIniFileURL);
256 	aConfig.SetGroup( OEM_PRELOAD_SECTION );
257 	ByteString sResult = aConfig.ReadKey( OEM_PRELOAD );
258 	if ( sResult == STR_TRUE )
259 		return sal_True;
260 	else
261 		return sal_False;
262 }
263 
disableOEMPreloadFlag()264 void OEMPreloadJob::disableOEMPreloadFlag()
265 {
266 	OUString aSofficeIniFileURL = locateIniFile();
267 	if ( aSofficeIniFileURL.getLength() > 0 )
268 	{
269 		Config aConfig(aSofficeIniFileURL);
270 		aConfig.SetGroup( OEM_PRELOAD_SECTION );
271 		aConfig.WriteKey( OEM_PRELOAD, STR_FALSE );
272 		aConfig.Flush();
273 	}
274 }
275 
276 } // namespace desktop
277