Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "vcl/svapp.hxx"
21 : #include "vcl/msgbox.hxx"
22 :
23 : #include "osl/mutex.hxx"
24 :
25 : #include <toolkit/helper/vclunohelper.hxx>
26 :
27 : #include "com/sun/star/beans/XPropertySet.hpp"
28 : #include "com/sun/star/configuration/theDefaultProvider.hpp"
29 : #include "com/sun/star/frame/Desktop.hpp"
30 :
31 : #include "dp_gui_dialog2.hxx"
32 : #include "dp_gui_extensioncmdqueue.hxx"
33 : #include "dp_gui_theextmgr.hxx"
34 : #include "dp_identifier.hxx"
35 : #include "dp_update.hxx"
36 :
37 : #define USER_PACKAGE_MANAGER OUString("user")
38 : #define SHARED_PACKAGE_MANAGER OUString("shared")
39 : #define BUNDLED_PACKAGE_MANAGER OUString("bundled")
40 :
41 : using namespace ::com::sun::star;
42 : using ::rtl::OUString;
43 :
44 : namespace dp_gui {
45 :
46 : //------------------------------------------------------------------------------
47 :
48 0 : ::rtl::Reference< TheExtensionManager > TheExtensionManager::s_ExtMgr;
49 :
50 : //------------------------------------------------------------------------------
51 : // TheExtensionManager
52 : //------------------------------------------------------------------------------
53 :
54 0 : TheExtensionManager::TheExtensionManager( Window *pParent,
55 : const uno::Reference< uno::XComponentContext > &xContext ) :
56 : m_xContext( xContext ),
57 : m_pParent( pParent ),
58 : m_pExtMgrDialog( NULL ),
59 : m_pUpdReqDialog( NULL ),
60 0 : m_pExecuteCmdQueue( NULL )
61 : {
62 0 : m_xExtensionManager = deployment::ExtensionManager::get( xContext );
63 0 : m_xExtensionManager->addModifyListener( this );
64 :
65 : uno::Reference< lang::XMultiServiceFactory > xConfig(
66 0 : configuration::theDefaultProvider::get(xContext));
67 0 : uno::Any args[1];
68 : beans::PropertyValue aValue( OUString("nodepath"), 0, uno::Any( OUString("/org.openoffice.Office.OptionsDialog/Nodes") ),
69 0 : beans::PropertyState_DIRECT_VALUE );
70 0 : args[0] <<= aValue;
71 : m_xNameAccessNodes = uno::Reference< container::XNameAccess >(
72 0 : xConfig->createInstanceWithArguments( OUString("com.sun.star.configuration.ConfigurationAccess"),
73 0 : uno::Sequence< uno::Any >( args, 1 )), uno::UNO_QUERY_THROW);
74 :
75 : // get the 'get more extensions here' url
76 0 : uno::Reference< container::XNameAccess > xNameAccessRepositories;
77 : beans::PropertyValue aValue2( OUString("nodepath"), 0, uno::Any( OUString("/org.openoffice.Office.ExtensionManager/ExtensionRepositories") ),
78 0 : beans::PropertyState_DIRECT_VALUE );
79 0 : args[0] <<= aValue2;
80 : xNameAccessRepositories = uno::Reference< container::XNameAccess > (
81 0 : xConfig->createInstanceWithArguments( OUString("com.sun.star.configuration.ConfigurationAccess"),
82 0 : uno::Sequence< uno::Any >( args, 1 )), uno::UNO_QUERY_THROW);
83 : try
84 : { //throws css::container::NoSuchElementException, css::lang::WrappedTargetException
85 0 : uno::Any value = xNameAccessRepositories->getByName( OUString( "WebsiteLink" ) );
86 0 : m_sGetExtensionsURL = value.get< OUString > ();
87 : }
88 0 : catch ( const uno::Exception& )
89 : {}
90 :
91 0 : if ( dp_misc::office_is_running() )
92 : {
93 : // the registration should be done after the construction has been ended
94 : // otherwise an exception prevents object creation, but it is registered as a listener
95 0 : m_xDesktop.set( frame::Desktop::create(xContext), uno::UNO_QUERY_THROW );
96 0 : m_xDesktop->addTerminateListener( this );
97 0 : }
98 0 : }
99 :
100 : //------------------------------------------------------------------------------
101 0 : TheExtensionManager::~TheExtensionManager()
102 : {
103 0 : delete m_pUpdReqDialog;
104 0 : delete m_pExtMgrDialog;
105 0 : delete m_pExecuteCmdQueue;
106 0 : }
107 :
108 : //------------------------------------------------------------------------------
109 0 : void TheExtensionManager::createDialog( const bool bCreateUpdDlg )
110 : {
111 0 : const SolarMutexGuard guard;
112 :
113 0 : if ( bCreateUpdDlg )
114 : {
115 0 : if ( !m_pUpdReqDialog )
116 : {
117 0 : m_pUpdReqDialog = new UpdateRequiredDialog( NULL, this );
118 0 : delete m_pExecuteCmdQueue;
119 0 : m_pExecuteCmdQueue = new ExtensionCmdQueue( (DialogHelper*) m_pUpdReqDialog, this, m_xContext );
120 0 : createPackageList();
121 : }
122 : }
123 0 : else if ( !m_pExtMgrDialog )
124 : {
125 0 : m_pExtMgrDialog = new ExtMgrDialog( m_pParent, this );
126 0 : delete m_pExecuteCmdQueue;
127 0 : m_pExecuteCmdQueue = new ExtensionCmdQueue( (DialogHelper*) m_pExtMgrDialog, this, m_xContext );
128 0 : m_pExtMgrDialog->setGetExtensionsURL( m_sGetExtensionsURL );
129 0 : createPackageList();
130 0 : }
131 0 : }
132 :
133 : //------------------------------------------------------------------------------
134 0 : void TheExtensionManager::Show()
135 : {
136 0 : const SolarMutexGuard guard;
137 :
138 0 : getDialog()->Show();
139 0 : }
140 :
141 : //------------------------------------------------------------------------------
142 0 : void TheExtensionManager::SetText( const ::rtl::OUString &rTitle )
143 : {
144 0 : const SolarMutexGuard guard;
145 :
146 0 : getDialog()->SetText( rTitle );
147 0 : }
148 :
149 : //------------------------------------------------------------------------------
150 0 : void TheExtensionManager::ToTop( sal_uInt16 nFlags )
151 : {
152 0 : const SolarMutexGuard guard;
153 :
154 0 : getDialog()->ToTop( nFlags );
155 0 : }
156 :
157 : //------------------------------------------------------------------------------
158 0 : bool TheExtensionManager::Close()
159 : {
160 0 : if ( m_pExtMgrDialog )
161 0 : return m_pExtMgrDialog->Close();
162 0 : else if ( m_pUpdReqDialog )
163 0 : return m_pUpdReqDialog->Close();
164 : else
165 0 : return true;
166 : }
167 :
168 : //------------------------------------------------------------------------------
169 0 : sal_Int16 TheExtensionManager::execute()
170 : {
171 0 : sal_Int16 nRet = 0;
172 :
173 0 : if ( m_pUpdReqDialog )
174 : {
175 0 : nRet = m_pUpdReqDialog->Execute();
176 0 : delete m_pUpdReqDialog;
177 0 : m_pUpdReqDialog = NULL;
178 : }
179 :
180 0 : return nRet;
181 : }
182 :
183 : //------------------------------------------------------------------------------
184 0 : bool TheExtensionManager::isVisible()
185 : {
186 0 : return getDialog()->IsVisible();
187 : }
188 :
189 : //------------------------------------------------------------------------------
190 0 : bool TheExtensionManager::checkUpdates( bool /* bShowUpdateOnly */, bool /*bParentVisible*/ )
191 : {
192 0 : std::vector< uno::Reference< deployment::XPackage > > vEntries;
193 0 : uno::Sequence< uno::Sequence< uno::Reference< deployment::XPackage > > > xAllPackages;
194 :
195 : try {
196 0 : xAllPackages = m_xExtensionManager->getAllExtensions( uno::Reference< task::XAbortChannel >(),
197 0 : uno::Reference< ucb::XCommandEnvironment >() );
198 0 : } catch ( const deployment::DeploymentException & ) {
199 0 : return false;
200 0 : } catch ( const ucb::CommandFailedException & ) {
201 0 : return false;
202 0 : } catch ( const ucb::CommandAbortedException & ) {
203 0 : return false;
204 0 : } catch ( const lang::IllegalArgumentException & e ) {
205 0 : throw uno::RuntimeException( e.Message, e.Context );
206 : }
207 :
208 0 : for ( sal_Int32 i = 0; i < xAllPackages.getLength(); ++i )
209 : {
210 0 : uno::Reference< deployment::XPackage > xPackage = dp_misc::getExtensionWithHighestVersion(xAllPackages[i]);
211 : OSL_ASSERT(xPackage.is());
212 0 : if ( xPackage.is() )
213 : {
214 0 : vEntries.push_back( xPackage );
215 : }
216 0 : }
217 :
218 0 : m_pExecuteCmdQueue->checkForUpdates( vEntries );
219 0 : return true;
220 : }
221 :
222 : //------------------------------------------------------------------------------
223 0 : bool TheExtensionManager::installPackage( const OUString &rPackageURL, bool bWarnUser )
224 : {
225 0 : if ( rPackageURL.isEmpty() )
226 0 : return false;
227 :
228 0 : createDialog( false );
229 :
230 0 : bool bInstall = true;
231 0 : bool bInstallForAll = false;
232 :
233 : // DV! missing function is read only repository from extension manager
234 0 : if ( !bWarnUser && ! m_xExtensionManager->isReadOnlyRepository( SHARED_PACKAGE_MANAGER ) )
235 0 : bInstall = getDialogHelper()->installForAllUsers( bInstallForAll );
236 :
237 0 : if ( !bInstall )
238 0 : return false;
239 :
240 0 : if ( bInstallForAll )
241 0 : m_pExecuteCmdQueue->addExtension( rPackageURL, SHARED_PACKAGE_MANAGER, false );
242 : else
243 0 : m_pExecuteCmdQueue->addExtension( rPackageURL, USER_PACKAGE_MANAGER, bWarnUser );
244 :
245 0 : return true;
246 : }
247 :
248 : //------------------------------------------------------------------------------
249 0 : bool TheExtensionManager::queryTermination()
250 : {
251 0 : if ( dp_misc::office_is_running() )
252 0 : return true;
253 : // the standalone application unopkg must not close ( and quit ) the dialog
254 : // when there are still actions in the queue
255 0 : return true;
256 : }
257 :
258 : //------------------------------------------------------------------------------
259 0 : void TheExtensionManager::terminateDialog()
260 : {
261 0 : if ( ! dp_misc::office_is_running() )
262 : {
263 0 : const SolarMutexGuard guard;
264 0 : delete m_pExtMgrDialog;
265 0 : m_pExtMgrDialog = NULL;
266 0 : delete m_pUpdReqDialog;
267 0 : m_pUpdReqDialog = NULL;
268 0 : Application::Quit();
269 : }
270 0 : }
271 :
272 : //------------------------------------------------------------------------------
273 0 : void TheExtensionManager::createPackageList()
274 : {
275 0 : uno::Sequence< uno::Sequence< uno::Reference< deployment::XPackage > > > xAllPackages;
276 :
277 : try {
278 0 : xAllPackages = m_xExtensionManager->getAllExtensions( uno::Reference< task::XAbortChannel >(),
279 0 : uno::Reference< ucb::XCommandEnvironment >() );
280 0 : } catch ( const deployment::DeploymentException & ) {
281 : return;
282 0 : } catch ( const ucb::CommandFailedException & ) {
283 : return;
284 0 : } catch ( const ucb::CommandAbortedException & ) {
285 : return;
286 0 : } catch ( const lang::IllegalArgumentException & e ) {
287 0 : throw uno::RuntimeException( e.Message, e.Context );
288 : }
289 :
290 0 : for ( sal_Int32 i = 0; i < xAllPackages.getLength(); ++i )
291 : {
292 0 : uno::Sequence< uno::Reference< deployment::XPackage > > xPackageList = xAllPackages[i];
293 :
294 0 : for ( sal_Int32 j = 0; j < xPackageList.getLength(); ++j )
295 : {
296 0 : uno::Reference< deployment::XPackage > xPackage = xPackageList[j];
297 0 : if ( xPackage.is() )
298 : {
299 0 : PackageState eState = getPackageState( xPackage );
300 0 : getDialogHelper()->addPackageToList( xPackage );
301 : // When the package is enabled, we can stop here, otherwise we have to look for
302 : // another version of this package
303 0 : if ( ( eState == REGISTERED ) || ( eState == NOT_AVAILABLE ) )
304 : break;
305 : }
306 0 : }
307 0 : }
308 :
309 0 : uno::Sequence< uno::Reference< deployment::XPackage > > xNoLicPackages;
310 0 : xNoLicPackages = m_xExtensionManager->getExtensionsWithUnacceptedLicenses( SHARED_PACKAGE_MANAGER,
311 0 : uno::Reference< ucb::XCommandEnvironment >() );
312 0 : for ( sal_Int32 i = 0; i < xNoLicPackages.getLength(); ++i )
313 : {
314 0 : uno::Reference< deployment::XPackage > xPackage = xNoLicPackages[i];
315 0 : if ( xPackage.is() )
316 : {
317 0 : getDialogHelper()->addPackageToList( xPackage, true );
318 : }
319 0 : }
320 : }
321 :
322 : //------------------------------------------------------------------------------
323 0 : PackageState TheExtensionManager::getPackageState( const uno::Reference< deployment::XPackage > &xPackage ) const
324 : {
325 : try {
326 : beans::Optional< beans::Ambiguous< sal_Bool > > option(
327 0 : xPackage->isRegistered( uno::Reference< task::XAbortChannel >(),
328 0 : uno::Reference< ucb::XCommandEnvironment >() ) );
329 0 : if ( option.IsPresent )
330 : {
331 0 : ::beans::Ambiguous< sal_Bool > const & reg = option.Value;
332 0 : if ( reg.IsAmbiguous )
333 0 : return AMBIGUOUS;
334 : else
335 0 : return reg.Value ? REGISTERED : NOT_REGISTERED;
336 : }
337 : else
338 0 : return NOT_AVAILABLE;
339 : }
340 0 : catch ( const uno::RuntimeException & ) {
341 0 : throw;
342 : }
343 0 : catch (const uno::Exception & exc) {
344 : (void) exc;
345 : OSL_FAIL( ::rtl::OUStringToOString( exc.Message, RTL_TEXTENCODING_UTF8 ).getStr() );
346 0 : return NOT_AVAILABLE;
347 : }
348 : }
349 :
350 : //------------------------------------------------------------------------------
351 0 : bool TheExtensionManager::isReadOnly( const uno::Reference< deployment::XPackage > &xPackage ) const
352 : {
353 0 : if ( m_xExtensionManager.is() && xPackage.is() )
354 : {
355 0 : return m_xExtensionManager->isReadOnlyRepository( xPackage->getRepositoryName() );
356 : }
357 : else
358 0 : return true;
359 : }
360 :
361 : //------------------------------------------------------------------------------
362 : // The function investigates if the extension supports options.
363 0 : bool TheExtensionManager::supportsOptions( const uno::Reference< deployment::XPackage > &xPackage ) const
364 : {
365 0 : bool bOptions = false;
366 :
367 0 : if ( ! xPackage->isBundle() )
368 0 : return false;
369 :
370 0 : beans::Optional< OUString > aId = xPackage->getIdentifier();
371 :
372 : //a bundle must always have an id
373 : OSL_ASSERT( aId.IsPresent );
374 :
375 : //iterate over all available nodes
376 0 : uno::Sequence< OUString > seqNames = m_xNameAccessNodes->getElementNames();
377 :
378 0 : for ( int i = 0; i < seqNames.getLength(); i++ )
379 : {
380 0 : uno::Any anyNode = m_xNameAccessNodes->getByName( seqNames[i] );
381 : //If we have a node then then it must contain the set of leaves. This is part of OptionsDialog.xcs
382 0 : uno::Reference< XInterface> xIntNode = anyNode.get< uno::Reference< XInterface > >();
383 0 : uno::Reference< container::XNameAccess > xNode( xIntNode, uno::UNO_QUERY_THROW );
384 :
385 0 : uno::Any anyLeaves = xNode->getByName( OUString("Leaves") );
386 0 : uno::Reference< XInterface > xIntLeaves = anyLeaves.get< uno::Reference< XInterface > >();
387 0 : uno::Reference< container::XNameAccess > xLeaves( xIntLeaves, uno::UNO_QUERY_THROW );
388 :
389 : //iterate over all available leaves
390 0 : uno::Sequence< OUString > seqLeafNames = xLeaves->getElementNames();
391 0 : for ( int j = 0; j < seqLeafNames.getLength(); j++ )
392 : {
393 0 : uno::Any anyLeaf = xLeaves->getByName( seqLeafNames[j] );
394 0 : uno::Reference< XInterface > xIntLeaf = anyLeaf.get< uno::Reference< XInterface > >();
395 0 : uno::Reference< beans::XPropertySet > xLeaf( xIntLeaf, uno::UNO_QUERY_THROW );
396 : //investigate the Id property if it matches the extension identifier which
397 : //has been passed in.
398 0 : uno::Any anyValue = xLeaf->getPropertyValue( OUString("Id") );
399 :
400 0 : OUString sId = anyValue.get< OUString >();
401 0 : if ( sId == aId.Value )
402 : {
403 0 : bOptions = true;
404 : break;
405 : }
406 0 : }
407 0 : if ( bOptions )
408 : break;
409 0 : }
410 0 : return bOptions;
411 : }
412 :
413 : //------------------------------------------------------------------------------
414 : // XEventListener
415 0 : void TheExtensionManager::disposing( lang::EventObject const & rEvt )
416 : throw ( uno::RuntimeException )
417 : {
418 0 : bool shutDown = (rEvt.Source == m_xDesktop);
419 :
420 0 : if ( shutDown && m_xDesktop.is() )
421 : {
422 0 : m_xDesktop->removeTerminateListener( this );
423 0 : m_xDesktop.clear();
424 : }
425 :
426 0 : if ( shutDown )
427 : {
428 0 : if ( dp_misc::office_is_running() )
429 : {
430 0 : const SolarMutexGuard guard;
431 0 : delete m_pExtMgrDialog;
432 0 : m_pExtMgrDialog = NULL;
433 0 : delete m_pUpdReqDialog;
434 0 : m_pUpdReqDialog = NULL;
435 : }
436 0 : s_ExtMgr.clear();
437 : }
438 0 : }
439 :
440 : //------------------------------------------------------------------------------
441 : // XTerminateListener
442 0 : void TheExtensionManager::queryTermination( ::lang::EventObject const & )
443 : throw ( frame::TerminationVetoException, uno::RuntimeException )
444 : {
445 0 : DialogHelper *pDialogHelper = getDialogHelper();
446 :
447 0 : if ( m_pExecuteCmdQueue->isBusy() || ( pDialogHelper && pDialogHelper->isBusy() ) )
448 : {
449 0 : ToTop( TOTOP_RESTOREWHENMIN );
450 : throw frame::TerminationVetoException(
451 : OUString("The office cannot be closed while the Extension Manager is running"),
452 0 : uno::Reference<XInterface>(static_cast<frame::XTerminateListener*>(this), uno::UNO_QUERY));
453 : }
454 : else
455 : {
456 0 : if ( m_pExtMgrDialog )
457 0 : m_pExtMgrDialog->Close();
458 0 : if ( m_pUpdReqDialog )
459 0 : m_pUpdReqDialog->Close();
460 : }
461 0 : }
462 :
463 : //------------------------------------------------------------------------------
464 0 : void TheExtensionManager::notifyTermination( ::lang::EventObject const & rEvt )
465 : throw ( uno::RuntimeException )
466 : {
467 0 : disposing( rEvt );
468 0 : }
469 :
470 : //------------------------------------------------------------------------------
471 : // XModifyListener
472 0 : void TheExtensionManager::modified( ::lang::EventObject const & /*rEvt*/ )
473 : throw ( uno::RuntimeException )
474 : {
475 0 : getDialogHelper()->prepareChecking();
476 0 : createPackageList();
477 0 : getDialogHelper()->checkEntries();
478 0 : }
479 :
480 : //------------------------------------------------------------------------------
481 0 : ::rtl::Reference< TheExtensionManager > TheExtensionManager::get( const uno::Reference< uno::XComponentContext > &xContext,
482 : const uno::Reference< awt::XWindow > &xParent,
483 : const OUString & extensionURL )
484 : {
485 0 : if ( s_ExtMgr.is() )
486 : {
487 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
488 0 : if ( !extensionURL.isEmpty() )
489 0 : s_ExtMgr->installPackage( extensionURL, true );
490 0 : return s_ExtMgr;
491 : }
492 :
493 0 : Window* pParent = DIALOG_NO_PARENT;
494 0 : if (xParent.is())
495 0 : pParent = VCLUnoHelper::GetWindow(xParent);
496 :
497 0 : ::rtl::Reference<TheExtensionManager> that( new TheExtensionManager( pParent, xContext ) );
498 :
499 0 : const SolarMutexGuard guard;
500 0 : if ( ! s_ExtMgr.is() )
501 : {
502 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
503 0 : s_ExtMgr = that;
504 : }
505 :
506 0 : if ( !extensionURL.isEmpty() )
507 0 : s_ExtMgr->installPackage( extensionURL, true );
508 :
509 0 : return s_ExtMgr;
510 : }
511 :
512 0 : } //namespace dp_gui
513 :
514 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|