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 :
21 : //_______________________________________________
22 : // include own header
23 : #include <jobs/helponstartup.hxx>
24 : #include <threadhelp/resetableguard.hxx>
25 : #include <loadenv/targethelper.hxx>
26 : #include <services.h>
27 :
28 : //_______________________________________________
29 : // include others
30 : #include <comphelper/configurationhelper.hxx>
31 : #include <comphelper/sequenceashashmap.hxx>
32 : #include <unotools/configmgr.hxx>
33 : #include <vcl/svapp.hxx>
34 : #include <vcl/help.hxx>
35 : #include <rtl/ustrbuf.hxx>
36 :
37 : //_______________________________________________
38 : // include interfaces
39 : #include <com/sun/star/frame/FrameSearchFlag.hpp>
40 : #include <com/sun/star/frame/ModuleManager.hpp>
41 : #include <com/sun/star/frame/XFramesSupplier.hpp>
42 : #include <com/sun/star/frame/Desktop.hpp>
43 :
44 :
45 : namespace framework{
46 :
47 :
48 : // path to module config
49 0 : static ::rtl::OUString CFG_PACKAGE_MODULES ("/org.openoffice.Setup/Office/Factories");
50 0 : static ::rtl::OUString CFG_PACKAGE_SETUP ("/org.openoffice.Setup");
51 0 : static ::rtl::OUString CFG_PACKAGE_COMMON ("/org.openoffice.Office.Common");
52 0 : static ::rtl::OUString CFG_PATH_L10N ("L10N");
53 0 : static ::rtl::OUString CFG_PATH_HELP ("Help");
54 0 : static ::rtl::OUString CFG_KEY_LOCALE ("ooLocale");
55 0 : static ::rtl::OUString CFG_KEY_HELPSYSTEM ("System");
56 :
57 : // props of job environment
58 0 : static ::rtl::OUString PROP_ENVIRONMENT ("Environment");
59 0 : static ::rtl::OUString PROP_JOBCONFIG ("JobConfig");
60 0 : static ::rtl::OUString PROP_ENVTYPE ("EnvType");
61 0 : static ::rtl::OUString PROP_MODEL ("Model");
62 :
63 : // props of module config
64 0 : static ::rtl::OUString PROP_HELP_BASEURL ("ooSetupFactoryHelpBaseURL");
65 0 : static ::rtl::OUString PROP_AUTOMATIC_HELP ("ooSetupFactoryHelpOnOpen");
66 :
67 : // special value of job environment
68 0 : static ::rtl::OUString ENVTYPE_DOCUMENTEVENT ("DOCUMENTEVENT");
69 :
70 : //-----------------------------------------------
71 :
72 0 : DEFINE_XSERVICEINFO_MULTISERVICE_2(HelpOnStartup ,
73 : ::cppu::OWeakObject ,
74 : SERVICENAME_JOB ,
75 : IMPLEMENTATIONNAME_HELPONSTARTUP)
76 :
77 0 : DEFINE_INIT_SERVICE(HelpOnStartup,
78 : {
79 : /* Attention
80 : I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
81 : to create a new instance of this class by our own supported service factory.
82 : see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further informations!
83 : */
84 : // create some needed uno services and cache it
85 : m_xModuleManager = css::frame::ModuleManager::create( m_xContext );
86 :
87 : m_xDesktop = css::frame::Desktop::create(m_xContext);
88 :
89 : m_xConfig = css::uno::Reference< css::container::XNameAccess >(
90 : ::comphelper::ConfigurationHelper::openConfig(
91 : m_xContext,
92 : CFG_PACKAGE_MODULES,
93 : ::comphelper::ConfigurationHelper::E_READONLY),
94 : css::uno::UNO_QUERY_THROW);
95 :
96 : // ask for office locale
97 : ::comphelper::ConfigurationHelper::readDirectKey(
98 : m_xContext,
99 : CFG_PACKAGE_SETUP,
100 : CFG_PATH_L10N,
101 : CFG_KEY_LOCALE,
102 : ::comphelper::ConfigurationHelper::E_READONLY) >>= m_sLocale;
103 :
104 : // detect system
105 : ::comphelper::ConfigurationHelper::readDirectKey(
106 : m_xContext,
107 : CFG_PACKAGE_COMMON,
108 : CFG_PATH_HELP,
109 : CFG_KEY_HELPSYSTEM,
110 : ::comphelper::ConfigurationHelper::E_READONLY) >>= m_sSystem;
111 :
112 : // Start listening for disposing events of these services,
113 : // so we can react e.g. for an office shutdown
114 : css::uno::Reference< css::lang::XComponent > xComponent;
115 : xComponent = css::uno::Reference< css::lang::XComponent >(m_xModuleManager, css::uno::UNO_QUERY);
116 : if (xComponent.is())
117 : xComponent->addEventListener(static_cast< css::lang::XEventListener* >(this));
118 : xComponent = css::uno::Reference< css::lang::XComponent >(m_xDesktop, css::uno::UNO_QUERY);
119 : if (xComponent.is())
120 : xComponent->addEventListener(static_cast< css::lang::XEventListener* >(this));
121 : xComponent = css::uno::Reference< css::lang::XComponent >(m_xConfig, css::uno::UNO_QUERY);
122 : if (xComponent.is())
123 : xComponent->addEventListener(static_cast< css::lang::XEventListener* >(this));
124 : }
125 : )
126 :
127 : //-----------------------------------------------
128 0 : HelpOnStartup::HelpOnStartup(const css::uno::Reference< css::uno::XComponentContext >& xContext)
129 : : ThreadHelpBase( )
130 0 : , m_xContext (xContext)
131 : {
132 0 : }
133 :
134 : //-----------------------------------------------
135 0 : HelpOnStartup::~HelpOnStartup()
136 : {
137 0 : }
138 :
139 : //-----------------------------------------------
140 : // css.task.XJob
141 0 : css::uno::Any SAL_CALL HelpOnStartup::execute(const css::uno::Sequence< css::beans::NamedValue >& lArguments)
142 : throw(css::lang::IllegalArgumentException,
143 : css::uno::Exception ,
144 : css::uno::RuntimeException )
145 : {
146 : // Analyze the given arguments; try to locate a model there and
147 : // classify it's used application module.
148 0 : ::rtl::OUString sModule = its_getModuleIdFromEnv(lArguments);
149 :
150 : // Attention: We are bound to events for openeing any document inside the office.
151 : // That includes e.g. the help module itself. But we have to do nothing then!
152 0 : if (sModule.isEmpty())
153 0 : return css::uno::Any();
154 :
155 : // check current state of the help module
156 : // a) help isnt open => show default page for the detected module
157 : // b) help shows any other default page(!) => show default page for the detected module
158 : // c) help shows any other content => do nothing (user travelled to any other content and leaved the set of default pages)
159 0 : ::rtl::OUString sCurrentHelpURL = its_getCurrentHelpURL();
160 0 : sal_Bool bCurrentHelpURLIsAnyDefaultURL = its_isHelpUrlADefaultOne(sCurrentHelpURL);
161 0 : sal_Bool bShowIt = sal_False;
162 :
163 : // a)
164 0 : if (sCurrentHelpURL.isEmpty())
165 0 : bShowIt = sal_True;
166 : else
167 : // b)
168 0 : if (bCurrentHelpURLIsAnyDefaultURL)
169 0 : bShowIt = sal_True;
170 :
171 0 : if (bShowIt)
172 : {
173 : // retrieve the help URL for the detected application module
174 0 : ::rtl::OUString sModuleDependendHelpURL = its_checkIfHelpEnabledAndGetURL(sModule);
175 0 : if (!sModuleDependendHelpURL.isEmpty())
176 : {
177 : // Show this help page.
178 : // Note: The help window brings itself to front ...
179 0 : Help* pHelp = Application::GetHelp();
180 0 : if (pHelp)
181 0 : pHelp->Start(sModuleDependendHelpURL, 0);
182 0 : }
183 : }
184 :
185 0 : return css::uno::Any();
186 : }
187 :
188 : //-----------------------------------------------
189 0 : void SAL_CALL HelpOnStartup::disposing(const css::lang::EventObject& aEvent)
190 : throw(css::uno::RuntimeException)
191 : {
192 : // SAFE ->
193 0 : ResetableGuard aLock(m_aLock);
194 :
195 0 : if (aEvent.Source == m_xModuleManager)
196 0 : m_xModuleManager.clear();
197 : else
198 0 : if (aEvent.Source == m_xDesktop)
199 0 : m_xDesktop.clear();
200 : else
201 0 : if (aEvent.Source == m_xConfig)
202 0 : m_xConfig.clear();
203 :
204 0 : aLock.unlock();
205 : // <- SAFE
206 0 : }
207 :
208 : //-----------------------------------------------
209 0 : ::rtl::OUString HelpOnStartup::its_getModuleIdFromEnv(const css::uno::Sequence< css::beans::NamedValue >& lArguments)
210 : {
211 0 : ::comphelper::SequenceAsHashMap lArgs (lArguments);
212 0 : ::comphelper::SequenceAsHashMap lEnvironment = lArgs.getUnpackedValueOrDefault(PROP_ENVIRONMENT, css::uno::Sequence< css::beans::NamedValue >());
213 0 : ::comphelper::SequenceAsHashMap lJobConfig = lArgs.getUnpackedValueOrDefault(PROP_JOBCONFIG , css::uno::Sequence< css::beans::NamedValue >());
214 :
215 : // check for right environment.
216 : // If its not a DocumentEvent, which triggered this job,
217 : // we cant work correctly! => return immediatly and do nothing
218 0 : ::rtl::OUString sEnvType = lEnvironment.getUnpackedValueOrDefault(PROP_ENVTYPE, ::rtl::OUString());
219 0 : if (!sEnvType.equals(ENVTYPE_DOCUMENTEVENT))
220 0 : return ::rtl::OUString();
221 :
222 0 : css::uno::Reference< css::frame::XModel > xDoc = lEnvironment.getUnpackedValueOrDefault(PROP_MODEL, css::uno::Reference< css::frame::XModel >());
223 0 : if (!xDoc.is())
224 0 : return ::rtl::OUString();
225 :
226 : // be sure that we work on top level documents only, which are registered
227 : // on the desktop instance. Ignore e.g. life previews, which are top frames too ...
228 : // but not registered at this global desktop instance.
229 0 : css::uno::Reference< css::frame::XDesktop > xDesktopCheck;
230 0 : css::uno::Reference< css::frame::XFrame > xFrame ;
231 0 : css::uno::Reference< css::frame::XController > xController = xDoc->getCurrentController();
232 0 : if (xController.is())
233 0 : xFrame = xController->getFrame();
234 0 : if (xFrame.is() && xFrame->isTop())
235 0 : xDesktopCheck = css::uno::Reference< css::frame::XDesktop >(xFrame->getCreator(), css::uno::UNO_QUERY);
236 0 : if (!xDesktopCheck.is())
237 0 : return ::rtl::OUString();
238 :
239 : // OK - now we are sure this document is a top level document.
240 : // Classify it.
241 : // SAFE ->
242 0 : ResetableGuard aLock(m_aLock);
243 0 : css::uno::Reference< css::frame::XModuleManager2 > xModuleManager = m_xModuleManager;
244 0 : aLock.unlock();
245 : // <- SAFE
246 :
247 0 : ::rtl::OUString sModuleId;
248 : try
249 : {
250 0 : sModuleId = xModuleManager->identify(xDoc);
251 : }
252 0 : catch(const css::uno::RuntimeException&)
253 0 : { throw; }
254 0 : catch(const css::uno::Exception&)
255 0 : { sModuleId = ::rtl::OUString(); }
256 :
257 0 : return sModuleId;
258 : }
259 :
260 : //-----------------------------------------------
261 0 : ::rtl::OUString HelpOnStartup::its_getCurrentHelpURL()
262 : {
263 : // SAFE ->
264 0 : ResetableGuard aLock(m_aLock);
265 0 : css::uno::Reference< css::frame::XDesktop2 > xDesktop = m_xDesktop;
266 0 : aLock.unlock();
267 : // <- SAFE
268 :
269 0 : if (!xDesktop.is())
270 0 : return ::rtl::OUString();
271 :
272 0 : css::uno::Reference< css::frame::XFrame > xHelp = xDesktop->findFrame(SPECIALTARGET_HELPTASK, css::frame::FrameSearchFlag::CHILDREN);
273 0 : if (!xHelp.is())
274 0 : return ::rtl::OUString();
275 :
276 0 : ::rtl::OUString sCurrentHelpURL;
277 : try
278 : {
279 0 : css::uno::Reference< css::frame::XFramesSupplier > xHelpRoot (xHelp , css::uno::UNO_QUERY_THROW);
280 0 : css::uno::Reference< css::container::XIndexAccess > xHelpChildren(xHelpRoot->getFrames(), css::uno::UNO_QUERY_THROW);
281 :
282 0 : css::uno::Reference< css::frame::XFrame > xHelpChild ;
283 0 : css::uno::Reference< css::frame::XController > xHelpView ;
284 0 : css::uno::Reference< css::frame::XModel > xHelpContent;
285 :
286 0 : xHelpChildren->getByIndex(0) >>= xHelpChild;
287 0 : if (xHelpChild.is())
288 0 : xHelpView = xHelpChild->getController();
289 0 : if (xHelpView.is())
290 0 : xHelpContent = xHelpView->getModel();
291 0 : if (xHelpContent.is())
292 0 : sCurrentHelpURL = xHelpContent->getURL();
293 : }
294 0 : catch(const css::uno::RuntimeException&)
295 0 : { throw; }
296 0 : catch(const css::uno::Exception&)
297 0 : { sCurrentHelpURL = ::rtl::OUString(); }
298 :
299 0 : return sCurrentHelpURL;
300 : }
301 :
302 : //-----------------------------------------------
303 0 : ::sal_Bool HelpOnStartup::its_isHelpUrlADefaultOne(const ::rtl::OUString& sHelpURL)
304 : {
305 0 : if (sHelpURL.isEmpty())
306 0 : return sal_False;
307 :
308 : // SAFE ->
309 0 : ResetableGuard aLock(m_aLock);
310 0 : css::uno::Reference< css::container::XNameAccess > xConfig = m_xConfig;
311 0 : ::rtl::OUString sLocale = m_sLocale;
312 0 : ::rtl::OUString sSystem = m_sSystem;
313 0 : aLock.unlock();
314 : // <- SAFE
315 :
316 0 : if (!xConfig.is())
317 0 : return sal_False;
318 :
319 : // check given help url against all default ones
320 0 : const css::uno::Sequence< ::rtl::OUString > lModules = xConfig->getElementNames();
321 0 : const ::rtl::OUString* pModules = lModules.getConstArray();
322 0 : ::sal_Int32 c = lModules.getLength();
323 0 : ::sal_Int32 i = 0;
324 :
325 0 : for (i=0; i<c; ++i)
326 : {
327 : try
328 : {
329 0 : css::uno::Reference< css::container::XNameAccess > xModuleConfig;
330 0 : xConfig->getByName(pModules[i]) >>= xModuleConfig;
331 0 : if (!xModuleConfig.is())
332 0 : continue;
333 :
334 0 : ::rtl::OUString sHelpBaseURL;
335 0 : xModuleConfig->getByName(PROP_HELP_BASEURL) >>= sHelpBaseURL;
336 0 : ::rtl::OUString sHelpURLForModule = HelpOnStartup::ist_createHelpURL(sHelpBaseURL, sLocale, sSystem);
337 0 : if (sHelpURL.equals(sHelpURLForModule))
338 0 : return sal_True;
339 : }
340 0 : catch(const css::uno::RuntimeException&)
341 0 : { throw; }
342 0 : catch(const css::uno::Exception&)
343 : {}
344 : }
345 :
346 0 : return sal_False;
347 : }
348 :
349 : //-----------------------------------------------
350 0 : ::rtl::OUString HelpOnStartup::its_checkIfHelpEnabledAndGetURL(const ::rtl::OUString& sModule)
351 : {
352 : // SAFE ->
353 0 : ResetableGuard aLock(m_aLock);
354 0 : css::uno::Reference< css::container::XNameAccess > xConfig = m_xConfig;
355 0 : ::rtl::OUString sLocale = m_sLocale;
356 0 : ::rtl::OUString sSystem = m_sSystem;
357 0 : aLock.unlock();
358 : // <- SAFE
359 :
360 0 : ::rtl::OUString sHelpURL;
361 :
362 : try
363 : {
364 0 : css::uno::Reference< css::container::XNameAccess > xModuleConfig;
365 0 : if (xConfig.is())
366 0 : xConfig->getByName(sModule) >>= xModuleConfig;
367 :
368 0 : sal_Bool bHelpEnabled = sal_False;
369 0 : if (xModuleConfig.is())
370 0 : xModuleConfig->getByName(PROP_AUTOMATIC_HELP) >>= bHelpEnabled;
371 :
372 0 : if (bHelpEnabled)
373 : {
374 0 : ::rtl::OUString sHelpBaseURL;
375 0 : xModuleConfig->getByName(PROP_HELP_BASEURL) >>= sHelpBaseURL;
376 0 : sHelpURL = HelpOnStartup::ist_createHelpURL(sHelpBaseURL, sLocale, sSystem);
377 0 : }
378 : }
379 0 : catch(const css::uno::RuntimeException&)
380 0 : { throw; }
381 0 : catch(const css::uno::Exception&)
382 0 : { sHelpURL = ::rtl::OUString(); }
383 :
384 0 : return sHelpURL;
385 : }
386 :
387 : //-----------------------------------------------
388 0 : ::rtl::OUString HelpOnStartup::ist_createHelpURL(const ::rtl::OUString& sBaseURL,
389 : const ::rtl::OUString& sLocale ,
390 : const ::rtl::OUString& sSystem )
391 : {
392 0 : ::rtl::OUStringBuffer sHelpURL(256);
393 0 : sHelpURL.append (sBaseURL );
394 0 : sHelpURL.appendAscii("?Language=");
395 0 : sHelpURL.append (sLocale );
396 0 : sHelpURL.appendAscii("&System=" );
397 0 : sHelpURL.append (sSystem );
398 :
399 0 : return sHelpURL.makeStringAndClear();
400 : }
401 :
402 0 : } // namespace framework
403 :
404 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|