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