Update ooo320-m1
[ooovba.git] / svtools / source / productregistration / productregistration.cxx
blob33bf1ce1c8a2e17554ca773448593d097fdc8216
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: productregistration.cxx,v $
10 * $Revision: 1.10 $
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_svtools.hxx"
34 #include "productregistration.hxx"
35 #include "regoptions.hxx"
36 #include "registrationdlg.hxx"
37 #ifndef _SVTOOLS_HRC
38 #include <svtools/svtools.hrc>
39 #endif
40 #include "cppuhelper/factory.hxx"
41 #include <cppuhelper/implbase1.hxx>
42 #include <com/sun/star/lang/XInitialization.hpp>
43 #include <com/sun/star/system/XSystemShellExecute.hpp>
44 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
45 #include <com/sun/star/frame/DispatchResultState.hpp>
46 #include <com/sun/star/frame/XDesktop.hpp>
47 #include <com/sun/star/beans/XMaterialHolder.hpp>
48 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
49 #include <toolkit/helper/vclunohelper.hxx>
50 #include <vcl/svapp.hxx>
51 #include <vcl/msgbox.hxx>
52 #include <osl/diagnose.h>
54 #include <algorithm>
55 #include <functional>
56 #include <memory>
58 #define PRODREG_IMPLNAME "com.sun.star.comp.setup.ProductRegistration"
59 #define PRODREG_SERVNAME "com.sun.star.setup.ProductRegistration"
61 using namespace ::com::sun::star::uno;
62 using namespace ::com::sun::star::lang;
63 using namespace ::com::sun::star::registry;
64 using namespace ::com::sun::star::container;
66 using rtl::OUString;
68 //........................................................................
69 namespace svt
71 //........................................................................
73 using namespace ::com::sun::star::task;
74 using namespace ::com::sun::star::system;
75 using namespace ::com::sun::star::beans;
76 using namespace ::com::sun::star::frame;
77 using namespace ::com::sun::star::awt;
79 //-------------------------------------------------------------------
81 struct EqualsOUString : public ::std::unary_function< OUString, sal_Bool >
83 const OUString& m_rCompare;
84 EqualsOUString( const OUString& _rCompare ) : m_rCompare( _rCompare ) { }
86 sal_Bool operator() ( const OUString& _rCompare )
88 return m_rCompare.equals( _rCompare );
92 //====================================================================
93 //= OProductRegistration
94 //====================================================================
96 //--------------------------------------------------------------------
97 OProductRegistration::OProductRegistration( const Reference< XMultiServiceFactory >& _rxORB )
98 :m_xORB( _rxORB )
102 //--------------------------------------------------------------------
103 Reference< XInterface > OProductRegistration::Create( const Reference< XMultiServiceFactory >& _rxORB )
105 return static_cast< ::cppu::OWeakObject* >( new OProductRegistration( _rxORB ) );
108 //--------------------------------------------------------------------
109 OUString SAL_CALL OProductRegistration::getImplementationName_Static( )
111 return OUString::createFromAscii( PRODREG_IMPLNAME );
114 //--------------------------------------------------------------------
115 Sequence< OUString > SAL_CALL OProductRegistration::getSupportedServiceNames_Static( ) throw (RuntimeException)
117 Sequence< OUString > aServiceNames( 1 );
118 aServiceNames[ 0 ] = OUString::createFromAscii( PRODREG_SERVNAME );
119 return aServiceNames;
122 //--------------------------------------------------------------------
123 OUString SAL_CALL OProductRegistration::getImplementationName( ) throw (RuntimeException)
125 return getImplementationName_Static( );
128 //--------------------------------------------------------------------
129 sal_Bool SAL_CALL OProductRegistration::supportsService( const OUString& _rServiceName ) throw (RuntimeException)
131 Sequence< OUString > aServiceNames( getSupportedServiceNames( ) );
132 const OUString* pNames = aServiceNames.getConstArray( );
133 const OUString* pNamesEnd = aServiceNames.getConstArray( ) + aServiceNames.getLength();
135 const OUString* pFound = ::std::find_if(
136 pNames,
137 pNamesEnd,
138 EqualsOUString( _rServiceName )
140 return pFound != pNamesEnd;
143 //--------------------------------------------------------------------
144 Sequence< OUString > SAL_CALL OProductRegistration::getSupportedServiceNames( ) throw (RuntimeException)
146 return getSupportedServiceNames_Static( );
149 //--------------------------------------------------------------------
150 static Reference< XFrame > lcl_getActiveFrame( const Reference< XMultiServiceFactory >& xFactory )
154 Reference< XDesktop > xDesktop(
155 xFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop") ) ),
156 UNO_QUERY_THROW );
158 Reference< XFrame > xFrame(xDesktop->getCurrentFrame());
159 if( ! xFrame.is() )
161 // Perhaps the frames collection of the desktop knows about an "active frame"?
162 Reference< XFramesSupplier > xFrames( xDesktop, UNO_QUERY_THROW );
163 xFrame = xFrames->getActiveFrame();
166 return xFrame;
168 catch(const Exception& )
170 OSL_ENSURE( sal_False, "lcl_getActiveFrame: caught an exception!" );
171 return Reference< XFrame >();
175 //--------------------------------------------------------------------
176 static Window* lcl_getPreferredDialogParent( const Reference< XFrame >& xFrame )
178 Window* pReturn = Application::GetDefDialogParent();
182 if ( xFrame.is() )
184 Reference< XWindow > xWindow = xFrame->getContainerWindow();
185 if ( xWindow.is() )
186 pReturn = VCLUnoHelper::GetWindow( xWindow );
189 catch( const Exception& )
191 OSL_ENSURE( sal_False, "lcl_getPreferredDialogParent: caught an exception!" );
194 return pReturn;
197 //--------------------------------------------------------------------
198 static bool lcl_isEvalVersion( const Reference< XMultiServiceFactory >& _rxORB )
200 bool bIsEvaluationVersion = false;
204 Reference < XMaterialHolder > xHolder(
205 _rxORB->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.tab.tabreg" ) ) ),
206 UNO_QUERY
209 if ( xHolder.is() )
211 Any aData = xHolder->getMaterial();
212 Sequence < NamedValue > aSeq;
214 if ( aData >>= aSeq )
216 // it's an evaluation version - a non-eval version wouldn't provide this "material"
217 bIsEvaluationVersion = true;
221 catch( const Exception& )
223 OSL_ENSURE( false, "lcl_isEvalVersion: caught an exception!" );
226 return bIsEvaluationVersion;
229 //--------------------------------------------------------------------
230 static bool lcl_doNewStyleRegistration( const Reference< XMultiServiceFactory >& xFactory, bool online )
234 Reference< XMultiServiceFactory > xConfigProvider(
235 xFactory->createInstance(
236 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationProvider" ) )
238 UNO_QUERY_THROW
241 PropertyValue aNodePath;
242 aNodePath.Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "nodepath" ) );
243 aNodePath.Value = makeAny( OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Jobs/Events" ) ) );
245 Sequence< Any > lArguments(1);
246 lArguments[0] = makeAny( aNodePath );
248 Reference< XHierarchicalNameAccess > xNameAccess(
249 xConfigProvider->createInstanceWithArguments(
250 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.configuration.ConfigurationAccess" ) ),
251 lArguments
253 UNO_QUERY_THROW
256 if( ! xNameAccess->hasByHierarchicalName( OUString( RTL_CONSTASCII_USTRINGPARAM( "onRegisterNow/JobList" ) ) ) )
257 return false;
259 Reference< XJobExecutor > xJobExecutor(
260 xFactory->createInstance(
261 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.task.JobExecutor" ) )
263 UNO_QUERY_THROW
266 xJobExecutor->trigger( online ? OUString( RTL_CONSTASCII_USTRINGPARAM( "onRegisterNow" ) ) :
267 OUString( RTL_CONSTASCII_USTRINGPARAM( "onRegisterLater" ) ) );
269 return true;
271 catch( const Exception& )
273 OSL_ENSURE( false, "lcl_getOnlineRegistrationDispatch: caught an exception!" );
274 return false;
278 //--------------------------------------------------------------------
279 void SAL_CALL OProductRegistration::trigger( const OUString& _rEvent ) throw (RuntimeException)
281 bool registerOnline = false;
283 switch ( classify( _rEvent ) )
285 case etRegistrationRequired:
286 registerOnline = true;
287 break;
289 default:
290 break;
293 // prefer new style registration
294 if( ! lcl_doNewStyleRegistration(m_xORB, registerOnline ) && registerOnline )
295 doOnlineRegistration();
298 //--------------------------------------------------------------------
299 Any SAL_CALL OProductRegistration::execute( const Sequence< NamedValue >& ) throw (IllegalArgumentException, Exception, RuntimeException)
301 Any aReturn;
303 static sal_Bool bFirstEncounter( sal_True );
304 if ( bFirstEncounter )
305 { // during this session, this event was never triggered before ....
306 bFirstEncounter = sal_False;
308 sal_Bool bDeactivateJob = sal_True;
310 // our config options
311 RegOptions aRegOptions;
312 // check them for the permissions for the dialog
313 RegOptions::DialogPermission ePermission( aRegOptions.getDialogPermission() );
315 if ( RegOptions::dpDisabled != ePermission )
316 { // the dialog is _not_ disabled
318 // for this session, I'm no interested in the dialog registration anymore
319 aRegOptions.markSessionDone( );
321 if ( ( RegOptions::dpNotThisSession == ePermission ) // first trigger session not reached
322 || ( RegOptions::dpRemindLater == ePermission ) // or at a later reminder date
324 { // the dialog should be executed during one of the next sessions
325 bDeactivateJob = sal_False;
327 else
329 // if we're here, the dialog should be executed during this session
330 OSL_ENSURE( RegOptions::dpThisSession == ePermission, "OProductRegistration::execute: invalid permissions!" );
333 // this is some kind of HACK.
334 // This registration dialog is intended to appear very very early during the
335 // first office start after installation. Unfortunately, this is so early
336 // that even SFX is not yet loaded, thus the SfxHelp class is not yet available,
337 // thus, there is no help during the lifetime of the dialog.
338 // To fake this, we explicitly load the necessary services when the user
339 // really requests help herein.
340 // #110791# - 2003-06-11 - fs@openoffice.org
341 Reference < XInitialization > xOfficeWrapper(
342 m_xORB->createInstance(
343 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.office.OfficeWrapper" ) )
345 UNO_QUERY
347 if ( xOfficeWrapper.is() )
348 xOfficeWrapper->initialize( Sequence < Any >() );
351 std::auto_ptr<ResMgr> pResMgr (ResMgr::CreateResMgr (
352 CREATEVERSIONRESMGR_NAME(productregistration)));
354 Reference< XFrame > xFrame = lcl_getActiveFrame( m_xORB );
355 // execute it
356 RegistrationDialog aDialog (
357 lcl_getPreferredDialogParent( xFrame ),
358 ResId( DLG_REGISTRATION_REQUEST, *pResMgr.get() ),
359 lcl_isEvalVersion( m_xORB ) );
360 aDialog.Execute();
362 bool registerOnline = false;
364 switch ( aDialog.getResponse() )
366 case RegistrationDialog::urRegisterNow:
367 registerOnline = true;
368 break;
370 case RegistrationDialog::urRegisterLater:
371 bDeactivateJob = sal_False;
372 // remind again in seven days from now on ...
373 aRegOptions.activateReminder( 7 );
374 break;
376 case RegistrationDialog::urRegisterNever:
377 case RegistrationDialog::urAlreadyRegistered:
378 // never register or already registered
379 // -> deactivate the job, and nothing else
380 break;
382 default:
383 OSL_ENSURE( sal_False, "OProductRegistration::execute: invalid response from the dialog!" );
386 // prefer new style registration
387 if( ! lcl_doNewStyleRegistration(m_xORB, registerOnline) && registerOnline )
388 doOnlineRegistration();
392 Sequence< NamedValue > aJobResponse( 1 );
393 aJobResponse[0].Name = OUString::createFromAscii( "Deactivate" );
394 aJobResponse[0].Value <<= bDeactivateJob;
395 aReturn <<= aJobResponse;
398 return aReturn;
401 //--------------------------------------------------------------------
402 void OProductRegistration::doOnlineRegistration( )
404 sal_Bool bSuccess = sal_False;
407 // create the Desktop component which can load components
408 Reference< XSystemShellExecute > xSystemShell(
409 m_xORB->createInstance( OUString::createFromAscii( "com.sun.star.system.SystemShellExecute" ) ),
410 UNO_QUERY
412 OSL_ENSURE( xSystemShell.is(), "OProductRegistration::doOnlineRegistration: invalid SystemExecute component!" );
414 // access the configuration to retrieve the URL we shall use for registration
415 RegOptions aOptions;
416 OUString sRegistrationURL( aOptions.getRegistrationURL( ) );
417 OSL_ENSURE( sRegistrationURL.getLength(), "OProductRegistration::doOnlineRegistration: invalid URL found!" );
419 if ( xSystemShell.is() && sRegistrationURL.getLength() )
421 xSystemShell->execute( sRegistrationURL, OUString(), SystemShellExecuteFlags::DEFAULTS );
422 bSuccess = sal_True;
425 catch( const Exception& )
428 if ( !bSuccess )
430 std::auto_ptr<ResMgr> pResMgr (ResMgr::CreateResMgr (
431 CREATEVERSIONRESMGR_NAME(productregistration)));
433 ErrorBox aRegistrationError(
434 Application::GetDefDialogParent(),
435 ResId( ERRBOX_REG_NOSYSBROWSER, *pResMgr.get() ));
436 aRegistrationError.Execute();
438 // try again later
439 RegOptions aRegOptions;
440 aRegOptions.activateReminder( 7 );
444 //--------------------------------------------------------------------
445 OProductRegistration::EventType OProductRegistration::classify( const OUString& _rEventDesc )
447 EventType eReturn = etUnknown;
448 if ( _rEventDesc.equalsAscii( "RegistrationRequired" ) )
450 eReturn = etRegistrationRequired;
452 return eReturn;
455 //........................................................................
456 } // namespace svt
457 //........................................................................
459 extern "C"
461 SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment (
462 const sal_Char ** ppEnvTypeName, uno_Environment ** /* ppEnv */)
464 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
467 SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_writeInfo (
468 void * /* pServiceManager */, void * pRegistryKey)
470 if (pRegistryKey)
472 Reference< XRegistryKey > xRegistryKey (
473 reinterpret_cast< XRegistryKey* >( pRegistryKey ));
474 Reference< XRegistryKey > xNewKey;
476 xNewKey = xRegistryKey->createKey(
477 OUString::createFromAscii( "/" PRODREG_IMPLNAME "/UNO/SERVICES" ));
478 xNewKey->createKey(
479 OUString::createFromAscii( PRODREG_SERVNAME ));
481 return sal_True;
483 return sal_False;
486 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory (
487 const sal_Char * pImplementationName, void * pServiceManager, void * /* pRegistryKey */)
489 void * pResult = 0;
490 if (pServiceManager)
492 Reference< XSingleServiceFactory > xFactory;
493 if (svt::OProductRegistration::getImplementationName_Static().compareToAscii (pImplementationName) == 0)
495 xFactory = cppu::createSingleFactory (
496 reinterpret_cast< XMultiServiceFactory* >(pServiceManager),
497 svt::OProductRegistration::getImplementationName_Static(),
498 svt::OProductRegistration::Create,
499 svt::OProductRegistration::getSupportedServiceNames_Static());
501 if (xFactory.is())
503 xFactory->acquire();
504 pResult = xFactory.get();
507 return pResult;
510 } // extern "C"