merge the formfield patch from ooo-build
[ooovba.git] / desktop / source / so_comp / oemjob.cxx
blobf5f54bc1442058baa561d4a7a3e9d57f5c5e02d4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: oemjob.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_desktop.hxx"
34 #include "oemjob.hxx"
35 #include <rtl/bootstrap.hxx>
36 #include <rtl/ustrbuf.hxx>
37 #include <osl/file.hxx>
38 #include <unotools/bootstrap.hxx>
39 #include <tools/config.hxx>
40 #include <com/sun/star/frame/XDesktop.hpp>
41 #include <com/sun/star/frame/XFrame.hpp>
42 #include <com/sun/star/frame/XModel.hpp>
43 #include <com/sun/star/util/XCloseable.hpp>
44 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
45 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
46 #include <com/sun/star/beans/NamedValue.hpp>
48 using namespace rtl;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::beans;
52 using namespace ::com::sun::star::ui::dialogs;
53 using namespace ::com::sun::star::frame;
54 using namespace ::com::sun::star::util;
56 namespace desktop{
58 char const OEM_PRELOAD_SECTION[] = "Bootstrap";
59 char const OEM_PRELOAD[] = "Preload";
60 char const STR_TRUE[] = "1";
61 char const STR_FALSE[] = "0";
63 const char* OEMPreloadJob::interfaces[] =
65 "com.sun.star.task.XJob",
66 NULL,
68 const char* OEMPreloadJob::implementationName = "com.sun.star.comp.desktop.OEMPreloadJob";
69 const char* OEMPreloadJob::serviceName = "com.sun.star.office.OEMPreloadJob";
71 OUString OEMPreloadJob::GetImplementationName()
73 return OUString( RTL_CONSTASCII_USTRINGPARAM( implementationName));
76 Sequence< OUString > OEMPreloadJob::GetSupportedServiceNames()
78 sal_Int32 nSize = (sizeof( interfaces ) / sizeof( const char *)) - 1;
79 Sequence< OUString > aResult( nSize );
81 for( sal_Int32 i = 0; i < nSize; i++ )
82 aResult[i] = OUString::createFromAscii( interfaces[i] );
83 return aResult;
86 Reference< XInterface > SAL_CALL OEMPreloadJob::CreateInstance(
87 const Reference< XMultiServiceFactory >& rSMgr )
89 static osl::Mutex aMutex;
90 osl::MutexGuard guard( aMutex );
91 return (XComponent*) ( new OEMPreloadJob( rSMgr ) );
94 OEMPreloadJob::OEMPreloadJob( const Reference< XMultiServiceFactory >& xFactory ) :
95 m_aListeners( m_aMutex ),
96 m_xServiceManager( xFactory )
100 OEMPreloadJob::~OEMPreloadJob()
104 // XComponent
105 void SAL_CALL OEMPreloadJob::dispose() throw ( RuntimeException )
107 EventObject aObject;
108 aObject.Source = (XComponent*)this;
109 m_aListeners.disposeAndClear( aObject );
112 void SAL_CALL OEMPreloadJob::addEventListener( const Reference< XEventListener > & aListener) throw ( RuntimeException )
114 m_aListeners.addInterface( aListener );
117 void SAL_CALL OEMPreloadJob::removeEventListener( const Reference< XEventListener > & aListener ) throw ( RuntimeException )
119 m_aListeners.removeInterface( aListener );
122 // XServiceInfo
123 ::rtl::OUString SAL_CALL OEMPreloadJob::getImplementationName()
124 throw ( RuntimeException )
126 return OEMPreloadJob::GetImplementationName();
129 sal_Bool SAL_CALL OEMPreloadJob::supportsService( const ::rtl::OUString& rServiceName )
130 throw ( RuntimeException )
132 sal_Int32 nSize = sizeof( interfaces ) / sizeof( const char *);
134 for( sal_Int32 i = 0; i < nSize; i++ )
135 if ( rServiceName.equalsAscii( interfaces[i] ))
136 return sal_True;
137 return sal_False;
140 Sequence< ::rtl::OUString > SAL_CALL OEMPreloadJob::getSupportedServiceNames()
141 throw ( RuntimeException )
143 return OEMPreloadJob::GetSupportedServiceNames();
146 // XJob
147 Any SAL_CALL OEMPreloadJob::execute(const Sequence<NamedValue>&)
148 throw ( RuntimeException )
150 sal_Bool bCont = sal_False;
151 // are we an OEM version at all?
152 if (checkOEMPreloadFlag())
154 // create OEM preload service dialog
155 Reference <XExecutableDialog> xDialog( m_xServiceManager->createInstance(
156 OUString::createFromAscii("org.openoffice.comp.preload.OEMPreloadWizard")),
157 UNO_QUERY );
158 if ( xDialog.is() ){
159 // execute OEM preload dialog and check return value
160 if ( xDialog->execute() == ExecutableDialogResults::OK ) {
161 // user accepted.
162 // make sure the job does not get called again.
163 bCont = sal_True;
164 disableOEMPreloadFlag();
165 } else {
166 // user declined...
167 // terminate.
169 Reference< XDesktop > xDesktop( m_xServiceManager->createInstance(
170 OUString::createFromAscii("com.sun.star.frame.Desktop")),
171 UNO_QUERY );
172 xDesktop->terminate();
175 OUString aName;
176 OUString aEnvType;
177 Reference<XFrame> rFrame;
178 Reference<XModel> rModel;
179 Reference<XCloseable> rClose;
180 for (int i=0; i<args.getLength(); i++)
182 if (args[i].Name.equalsAscii("EnvType"))
183 args[i].Value >>= aEnvType;
184 else if (args[i].Name.equalsAscii("Frame")) {
185 args[i].Value >>= rFrame;
186 rClose = Reference<XCloseable>(rFrame, UNO_QUERY);
188 else if (args[i].Name.equalsAscii("Model")) {
189 args[i].Value >>= rModel;
190 rClose = Reference<XCloseable>(rModel, UNO_QUERY);
193 if (rClose.is()) rClose->close(sal_True);
195 bCont = sal_False;
198 } else {
199 // don't try again
200 bCont = sal_True;
203 NamedValue nv;
204 nv.Name = OUString::createFromAscii("Deactivate");
205 nv.Value <<= bDeactivate;
206 Sequence<NamedValue> s(1);
207 s[0] = nv;
209 Any r;
210 r <<= bCont;
211 return r;
215 static sal_Bool existsURL( OUString const& _sURL )
217 using namespace osl;
218 DirectoryItem aDirItem;
220 if (_sURL.getLength() != 0)
221 return ( DirectoryItem::get( _sURL, aDirItem ) == DirectoryItem::E_None );
223 return sal_False;
227 // locate soffice.ini/.rc file
228 static OUString locateIniFile()
230 OUString aUserDataPath;
231 OUString aSofficeIniFileURL;
233 // Retrieve the default file URL for the soffice.ini/rc
234 Bootstrap().getIniName( aSofficeIniFileURL );
236 if ( utl::Bootstrap::locateUserData( aUserDataPath ) == utl::Bootstrap::PATH_EXISTS )
238 const char CONFIG_DIR[] = "/config";
240 sal_Int32 nIndex = aSofficeIniFileURL.lastIndexOf( '/');
241 if ( nIndex > 0 )
243 OUString aUserSofficeIniFileURL;
244 OUStringBuffer aBuffer( aUserDataPath );
245 aBuffer.appendAscii( CONFIG_DIR );
246 aBuffer.append( aSofficeIniFileURL.copy( nIndex ));
247 aUserSofficeIniFileURL = aBuffer.makeStringAndClear();
249 if ( existsURL( aUserSofficeIniFileURL ))
250 return aUserSofficeIniFileURL;
253 // Fallback try to use the soffice.ini/rc from program folder
254 return aSofficeIniFileURL;
257 // check whether the OEMPreload flag was set in soffice.ini/.rc
258 sal_Bool OEMPreloadJob::checkOEMPreloadFlag()
260 OUString aSofficeIniFileURL;
261 aSofficeIniFileURL = locateIniFile();
262 Config aConfig(aSofficeIniFileURL);
263 aConfig.SetGroup( OEM_PRELOAD_SECTION );
264 ByteString sResult = aConfig.ReadKey( OEM_PRELOAD );
265 if ( sResult == STR_TRUE )
266 return sal_True;
267 else
268 return sal_False;
271 void OEMPreloadJob::disableOEMPreloadFlag()
273 OUString aSofficeIniFileURL = locateIniFile();
274 if ( aSofficeIniFileURL.getLength() > 0 )
276 Config aConfig(aSofficeIniFileURL);
277 aConfig.SetGroup( OEM_PRELOAD_SECTION );
278 aConfig.WriteKey( OEM_PRELOAD, STR_FALSE );
279 aConfig.Flush();
283 } // namespace desktop