xref: /trunk/main/desktop/source/so_comp/oemjob.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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()
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)
106 {
107     m_aListeners.addInterface( aListener );
108 }
109 
removeEventListener(const Reference<XEventListener> & aListener)110 void SAL_CALL OEMPreloadJob::removeEventListener( const Reference< XEventListener > & aListener )
111 {
112     m_aListeners.removeInterface( aListener );
113 }
114 
115 // XServiceInfo
getImplementationName()116 ::rtl::OUString SAL_CALL OEMPreloadJob::getImplementationName()
117 {
118     return OEMPreloadJob::GetImplementationName();
119 }
120 
supportsService(const::rtl::OUString & rServiceName)121 sal_Bool SAL_CALL OEMPreloadJob::supportsService( const ::rtl::OUString& rServiceName )
122 {
123     sal_Int32 nSize = sizeof( interfaces ) / sizeof( const char *);
124 
125     for( sal_Int32 i = 0; i < nSize; i++ )
126         if ( rServiceName.equalsAscii( interfaces[i] ))
127             return sal_True;
128     return sal_False;
129 }
130 
getSupportedServiceNames()131 Sequence< ::rtl::OUString > SAL_CALL OEMPreloadJob::getSupportedServiceNames()
132 {
133     return OEMPreloadJob::GetSupportedServiceNames();
134 }
135 
136 // XJob
execute(const Sequence<NamedValue> &)137 Any SAL_CALL OEMPreloadJob::execute(const Sequence<NamedValue>&)
138 {
139     sal_Bool bCont = sal_False;
140     // are we an OEM version at all?
141     if (checkOEMPreloadFlag())
142     {
143         // create OEM preload service dialog
144         Reference <XExecutableDialog> xDialog( m_xServiceManager->createInstance(
145             OUString::createFromAscii("org.openoffice.comp.preload.OEMPreloadWizard")),
146             UNO_QUERY );
147         if ( xDialog.is() ){
148             // execute OEM preload dialog and check return value
149             if ( xDialog->execute() == ExecutableDialogResults::OK ) {
150                 // user accepted.
151                 // make sure the job does not get called again.
152                 bCont = sal_True;
153                 disableOEMPreloadFlag();
154             } else {
155                 // user declined...
156                 // terminate.
157                 /*
158                 Reference< XDesktop > xDesktop( m_xServiceManager->createInstance(
159                     OUString::createFromAscii("com.sun.star.frame.Desktop")),
160                     UNO_QUERY );
161                 xDesktop->terminate();
162                 */
163                 /*
164                 OUString aName;
165                 OUString aEnvType;
166                 Reference<XFrame> rFrame;
167                 Reference<XModel> rModel;
168                 Reference<XCloseable> rClose;
169                 for (int i=0; i<args.getLength(); i++)
170                 {
171                     if (args[i].Name.equalsAscii("EnvType"))
172                         args[i].Value >>= aEnvType;
173                     else if (args[i].Name.equalsAscii("Frame")) {
174                         args[i].Value >>= rFrame;
175                         rClose = Reference<XCloseable>(rFrame, UNO_QUERY);
176                     }
177                     else if (args[i].Name.equalsAscii("Model")) {
178                         args[i].Value >>= rModel;
179                         rClose = Reference<XCloseable>(rModel, UNO_QUERY);
180                     }
181                 }
182                 if (rClose.is()) rClose->close(sal_True);
183                 */
184                 bCont = sal_False;
185             }
186         }
187     } else {
188         // don't try again
189         bCont = sal_True;
190     }
191     /*
192     NamedValue nv;
193     nv.Name  = OUString::createFromAscii("Deactivate");
194     nv.Value <<=  bDeactivate;
195     Sequence<NamedValue> s(1);
196     s[0] = nv;
197     */
198     Any r;
199     r <<= bCont;
200     return r;
201 }
202 
203 
existsURL(OUString const & _sURL)204 static sal_Bool existsURL( OUString const& _sURL )
205 {
206     using namespace osl;
207     DirectoryItem aDirItem;
208 
209     if (_sURL.getLength() != 0)
210         return ( DirectoryItem::get( _sURL, aDirItem ) == DirectoryItem::E_None );
211 
212     return sal_False;
213 }
214 
215 
216 // locate soffice.ini/.rc file
locateIniFile()217 static OUString locateIniFile()
218 {
219     OUString aUserDataPath;
220     OUString aSofficeIniFileURL;
221 
222     // Retrieve the default file URL for the soffice.ini/rc
223     Bootstrap().getIniName( aSofficeIniFileURL );
224 
225     if ( utl::Bootstrap::locateUserData( aUserDataPath ) == utl::Bootstrap::PATH_EXISTS )
226     {
227         const char CONFIG_DIR[] = "/config";
228 
229         sal_Int32 nIndex = aSofficeIniFileURL.lastIndexOf( '/');
230         if ( nIndex > 0 )
231         {
232             OUString        aUserSofficeIniFileURL;
233             OUStringBuffer  aBuffer( aUserDataPath );
234             aBuffer.appendAscii( CONFIG_DIR );
235             aBuffer.append( aSofficeIniFileURL.copy( nIndex ));
236             aUserSofficeIniFileURL = aBuffer.makeStringAndClear();
237 
238             if ( existsURL( aUserSofficeIniFileURL ))
239                 return aUserSofficeIniFileURL;
240         }
241     }
242     // Fallback try to use the soffice.ini/rc from program folder
243     return aSofficeIniFileURL;
244 }
245 
246 // check whether the OEMPreload flag was set in soffice.ini/.rc
checkOEMPreloadFlag()247 sal_Bool OEMPreloadJob::checkOEMPreloadFlag()
248 {
249     OUString aSofficeIniFileURL;
250     aSofficeIniFileURL = locateIniFile();
251     Config aConfig(aSofficeIniFileURL);
252     aConfig.SetGroup( OEM_PRELOAD_SECTION );
253     ByteString sResult = aConfig.ReadKey( OEM_PRELOAD );
254     if ( sResult == STR_TRUE )
255         return sal_True;
256     else
257         return sal_False;
258 }
259 
disableOEMPreloadFlag()260 void OEMPreloadJob::disableOEMPreloadFlag()
261 {
262     OUString aSofficeIniFileURL = locateIniFile();
263     if ( aSofficeIniFileURL.getLength() > 0 )
264     {
265         Config aConfig(aSofficeIniFileURL);
266         aConfig.SetGroup( OEM_PRELOAD_SECTION );
267         aConfig.WriteKey( OEM_PRELOAD, STR_FALSE );
268         aConfig.Flush();
269     }
270 }
271 
272 } // namespace desktop
273