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