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 1 : ::rtl::Reference< TheExtensionManager > TheExtensionManager::s_ExtMgr;
47 :
48 :
49 : // TheExtensionManager
50 :
51 :
52 0 : TheExtensionManager::TheExtensionManager( const uno::Reference< awt::XWindow > &xParent,
53 : const uno::Reference< uno::XComponentContext > &xContext ) :
54 : m_xContext( xContext ),
55 : m_xParent( xParent ),
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 : m_pUpdReqDialog.disposeAndClear();
102 0 : m_pExtMgrDialog.disposeAndClear();
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 = VclPtr<UpdateRequiredDialog>::Create( nullptr, this );
116 0 : delete m_pExecuteCmdQueue;
117 0 : m_pExecuteCmdQueue = new ExtensionCmdQueue( m_pUpdReqDialog.get(), this, m_xContext );
118 0 : createPackageList();
119 : }
120 : }
121 0 : else if ( !m_pExtMgrDialog )
122 : {
123 0 : if (m_xParent.is())
124 0 : m_pExtMgrDialog = VclPtr<ExtMgrDialog>::Create( VCLUnoHelper::GetWindow(m_xParent), this );
125 : else
126 0 : m_pExtMgrDialog = VclPtr<ExtMgrDialog>::Create( nullptr, this, Dialog::InitFlag::NoParent );
127 0 : delete m_pExecuteCmdQueue;
128 0 : m_pExecuteCmdQueue = new ExtensionCmdQueue( m_pExtMgrDialog.get(), this, m_xContext );
129 0 : m_pExtMgrDialog->setGetExtensionsURL( m_sGetExtensionsURL );
130 0 : createPackageList();
131 0 : }
132 0 : }
133 :
134 :
135 0 : void TheExtensionManager::Show()
136 : {
137 0 : const SolarMutexGuard guard;
138 :
139 0 : getDialog()->Show();
140 0 : }
141 :
142 :
143 0 : void TheExtensionManager::SetText( const OUString &rTitle )
144 : {
145 0 : const SolarMutexGuard guard;
146 :
147 0 : getDialog()->SetText( rTitle );
148 0 : }
149 :
150 :
151 0 : void TheExtensionManager::ToTop( ToTopFlags nFlags )
152 : {
153 0 : const SolarMutexGuard guard;
154 :
155 0 : getDialog()->ToTop( nFlags );
156 0 : }
157 :
158 :
159 0 : bool TheExtensionManager::Close()
160 : {
161 0 : if ( m_pExtMgrDialog )
162 0 : return m_pExtMgrDialog->Close();
163 0 : else if ( m_pUpdReqDialog )
164 0 : return m_pUpdReqDialog->Close();
165 : else
166 0 : return true;
167 : }
168 :
169 :
170 0 : sal_Int16 TheExtensionManager::execute()
171 : {
172 0 : sal_Int16 nRet = 0;
173 :
174 0 : if ( m_pUpdReqDialog )
175 : {
176 0 : nRet = m_pUpdReqDialog->Execute();
177 0 : m_pUpdReqDialog.disposeAndClear();
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 : m_pExtMgrDialog.disposeAndClear();
265 0 : m_pUpdReqDialog.disposeAndClear();
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 )
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 : m_pExtMgrDialog.disposeAndClear();
430 0 : m_pUpdReqDialog.disposeAndClear();
431 : }
432 0 : s_ExtMgr.clear();
433 : }
434 0 : }
435 :
436 :
437 : // XTerminateListener
438 0 : void TheExtensionManager::queryTermination( ::lang::EventObject const & )
439 : throw ( frame::TerminationVetoException, uno::RuntimeException, std::exception )
440 : {
441 0 : DialogHelper *pDialogHelper = getDialogHelper();
442 :
443 0 : if ( m_pExecuteCmdQueue->isBusy() || ( pDialogHelper && pDialogHelper->isBusy() ) )
444 : {
445 0 : ToTop( ToTopFlags::RestoreWhenMin );
446 : throw frame::TerminationVetoException(
447 : "The office cannot be closed while the Extension Manager is running",
448 0 : static_cast<frame::XTerminateListener*>(this));
449 : }
450 : else
451 : {
452 0 : if ( m_pExtMgrDialog )
453 0 : m_pExtMgrDialog->Close();
454 0 : if ( m_pUpdReqDialog )
455 0 : m_pUpdReqDialog->Close();
456 : }
457 0 : }
458 :
459 :
460 0 : void TheExtensionManager::notifyTermination( ::lang::EventObject const & rEvt )
461 : throw ( uno::RuntimeException, std::exception )
462 : {
463 0 : disposing( rEvt );
464 0 : }
465 :
466 :
467 : // XModifyListener
468 0 : void TheExtensionManager::modified( ::lang::EventObject const & /*rEvt*/ )
469 : throw ( uno::RuntimeException, std::exception )
470 : {
471 0 : getDialogHelper()->prepareChecking();
472 0 : createPackageList();
473 0 : getDialogHelper()->checkEntries();
474 0 : }
475 :
476 :
477 0 : ::rtl::Reference< TheExtensionManager > TheExtensionManager::get( const uno::Reference< uno::XComponentContext > &xContext,
478 : const uno::Reference< awt::XWindow > &xParent,
479 : const OUString & extensionURL )
480 : {
481 0 : if ( s_ExtMgr.is() )
482 : {
483 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
484 0 : if ( !extensionURL.isEmpty() )
485 0 : s_ExtMgr->installPackage( extensionURL, true );
486 0 : return s_ExtMgr;
487 : }
488 :
489 0 : ::rtl::Reference<TheExtensionManager> that( new TheExtensionManager( xParent, xContext ) );
490 :
491 0 : const SolarMutexGuard guard;
492 0 : if ( ! s_ExtMgr.is() )
493 : {
494 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
495 0 : s_ExtMgr = that;
496 : }
497 :
498 0 : if ( !extensionURL.isEmpty() )
499 0 : s_ExtMgr->installPackage( extensionURL, true );
500 :
501 0 : return s_ExtMgr;
502 : }
503 :
504 3 : } //namespace dp_gui
505 :
506 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|