1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
// include own header
#include <jobs/helponstartup.hxx>
#include <services.h>
#include <targets.h>
#include <officecfg/Office/Common.hxx>
#include <officecfg/Setup.hxx>
// include others
#include <comphelper/sequenceashashmap.hxx>
#include <vcl/svapp.hxx>
#include <vcl/help.hxx>
// include interfaces
#include <com/sun/star/frame/FrameSearchFlag.hpp>
#include <com/sun/star/frame/ModuleManager.hpp>
#include <com/sun/star/frame/XFramesSupplier.hpp>
#include <com/sun/star/frame/Desktop.hpp>
namespace framework{
DEFINE_XSERVICEINFO_MULTISERVICE_2(HelpOnStartup ,
::cppu::OWeakObject ,
SERVICENAME_JOB ,
IMPLEMENTATIONNAME_HELPONSTARTUP)
DEFINE_INIT_SERVICE(HelpOnStartup,
{
/* Attention
I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
to create a new instance of this class by our own supported service factory.
see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further information!
*/
// create some needed uno services and cache it
m_xModuleManager = css::frame::ModuleManager::create( m_xContext );
m_xDesktop = css::frame::Desktop::create(m_xContext);
// ask for office locale
m_sLocale = officecfg::Setup::L10N::ooLocale::get(m_xContext);
// detect system
m_sSystem = officecfg::Office::Common::Help::System::get(m_xContext);
// Start listening for disposing events of these services,
// so we can react e.g. for an office shutdown
css::uno::Reference< css::lang::XComponent > xComponent;
xComponent.set(m_xModuleManager, css::uno::UNO_QUERY);
if (xComponent.is())
xComponent->addEventListener(static_cast< css::lang::XEventListener* >(this));
if (m_xDesktop.is())
m_xDesktop->addEventListener(static_cast< css::lang::XEventListener* >(this));
xComponent.set(m_xConfig, css::uno::UNO_QUERY);
if (xComponent.is())
xComponent->addEventListener(static_cast< css::lang::XEventListener* >(this));
}
)
HelpOnStartup::HelpOnStartup(const css::uno::Reference< css::uno::XComponentContext >& xContext)
: m_xContext (xContext)
{
}
HelpOnStartup::~HelpOnStartup()
{
}
// css.task.XJob
css::uno::Any SAL_CALL HelpOnStartup::execute(const css::uno::Sequence< css::beans::NamedValue >& lArguments)
{
// Analyze the given arguments; try to locate a model there and
// classify it's used application module.
OUString sModule = its_getModuleIdFromEnv(lArguments);
// Attention: we are bound to events for opening any document inside the office.
// That includes e.g. the help module itself. But we have to do nothing then!
if (sModule.isEmpty())
return css::uno::Any();
// check current state of the help module
// a) help isn't open => show default page for the detected module
// b) help shows any other default page(!) => show default page for the detected module
// c) help shows any other content => do nothing (user travelled to any other content and leaved the set of default pages)
OUString sCurrentHelpURL = its_getCurrentHelpURL();
bool bCurrentHelpURLIsAnyDefaultURL = its_isHelpUrlADefaultOne(sCurrentHelpURL);
bool bShowIt = false;
// a)
if (sCurrentHelpURL.isEmpty())
bShowIt = true;
// b)
else if (bCurrentHelpURLIsAnyDefaultURL)
bShowIt = true;
if (bShowIt)
{
// retrieve the help URL for the detected application module
OUString sModuleDependentHelpURL = its_checkIfHelpEnabledAndGetURL(sModule);
if (!sModuleDependentHelpURL.isEmpty())
{
// Show this help page.
// Note: The help window brings itself to front ...
Help* pHelp = Application::GetHelp();
if (pHelp)
pHelp->Start(sModuleDependentHelpURL, static_cast<vcl::Window*>(nullptr));
}
}
return css::uno::Any();
}
void SAL_CALL HelpOnStartup::disposing(const css::lang::EventObject& aEvent)
{
osl::MutexGuard g(m_mutex);
if (aEvent.Source == m_xModuleManager)
m_xModuleManager.clear();
else if (aEvent.Source == m_xDesktop)
m_xDesktop.clear();
else if (aEvent.Source == m_xConfig)
m_xConfig.clear();
}
OUString HelpOnStartup::its_getModuleIdFromEnv(const css::uno::Sequence< css::beans::NamedValue >& lArguments)
{
::comphelper::SequenceAsHashMap lArgs (lArguments);
::comphelper::SequenceAsHashMap lEnvironment = lArgs.getUnpackedValueOrDefault("Environment", css::uno::Sequence< css::beans::NamedValue >());
// check for right environment.
// If it's not a DocumentEvent, which triggered this job,
// we can't work correctly! => return immediately and do nothing
OUString sEnvType = lEnvironment.getUnpackedValueOrDefault("EnvType", OUString());
if (sEnvType != "DOCUMENTEVENT")
return OUString();
css::uno::Reference< css::frame::XModel > xDoc = lEnvironment.getUnpackedValueOrDefault("Model", css::uno::Reference< css::frame::XModel >());
if (!xDoc.is())
return OUString();
// be sure that we work on top level documents only, which are registered
// on the desktop instance. Ignore e.g. life previews, which are top frames too ...
// but not registered at this global desktop instance.
css::uno::Reference< css::frame::XDesktop > xDesktopCheck;
css::uno::Reference< css::frame::XFrame > xFrame;
css::uno::Reference< css::frame::XController > xController = xDoc->getCurrentController();
if (xController.is())
xFrame = xController->getFrame();
if (xFrame.is() && xFrame->isTop())
xDesktopCheck.set(xFrame->getCreator(), css::uno::UNO_QUERY);
if (!xDesktopCheck.is())
return OUString();
// OK - now we are sure this document is a top level document.
// Classify it.
// SAFE ->
osl::ClearableMutexGuard aLock(m_mutex);
css::uno::Reference< css::frame::XModuleManager2 > xModuleManager = m_xModuleManager;
aLock.clear();
// <- SAFE
OUString sModuleId;
try
{
sModuleId = xModuleManager->identify(xDoc);
}
catch(const css::uno::RuntimeException&)
{ throw; }
catch(const css::uno::Exception&)
{ sModuleId.clear(); }
return sModuleId;
}
OUString HelpOnStartup::its_getCurrentHelpURL()
{
// SAFE ->
osl::ClearableMutexGuard aLock(m_mutex);
css::uno::Reference< css::frame::XDesktop2 > xDesktop = m_xDesktop;
aLock.clear();
// <- SAFE
if (!xDesktop.is())
return OUString();
css::uno::Reference< css::frame::XFrame > xHelp = xDesktop->findFrame(SPECIALTARGET_HELPTASK, css::frame::FrameSearchFlag::CHILDREN);
if (!xHelp.is())
return OUString();
OUString sCurrentHelpURL;
try
{
css::uno::Reference< css::frame::XFramesSupplier > xHelpRoot (xHelp , css::uno::UNO_QUERY_THROW);
css::uno::Reference< css::container::XIndexAccess > xHelpChildren(xHelpRoot->getFrames(), css::uno::UNO_QUERY_THROW);
css::uno::Reference< css::frame::XFrame > xHelpChild;
css::uno::Reference< css::frame::XController > xHelpView;
css::uno::Reference< css::frame::XModel > xHelpContent;
xHelpChildren->getByIndex(0) >>= xHelpChild;
if (xHelpChild.is())
xHelpView = xHelpChild->getController();
if (xHelpView.is())
xHelpContent = xHelpView->getModel();
if (xHelpContent.is())
sCurrentHelpURL = xHelpContent->getURL();
}
catch(const css::uno::RuntimeException&)
{ throw; }
catch(const css::uno::Exception&)
{ sCurrentHelpURL.clear(); }
return sCurrentHelpURL;
}
bool HelpOnStartup::its_isHelpUrlADefaultOne(const OUString& sHelpURL)
{
if (sHelpURL.isEmpty())
return false;
// SAFE ->
osl::ClearableMutexGuard aLock(m_mutex);
css::uno::Reference< css::container::XNameAccess > xConfig = m_xConfig;
OUString sLocale = m_sLocale;
OUString sSystem = m_sSystem;
aLock.clear();
// <- SAFE
if (!xConfig.is())
return false;
// check given help url against all default ones
const css::uno::Sequence< OUString > lModules = xConfig->getElementNames();
const OUString* pModules = lModules.getConstArray();
::sal_Int32 c = lModules.getLength();
::sal_Int32 i = 0;
for (i=0; i<c; ++i)
{
try
{
css::uno::Reference< css::container::XNameAccess > xModuleConfig;
xConfig->getByName(pModules[i]) >>= xModuleConfig;
if (!xModuleConfig.is())
continue;
OUString sHelpBaseURL;
xModuleConfig->getByName("ooSetupFactoryHelpBaseURL") >>= sHelpBaseURL;
OUString sHelpURLForModule = HelpOnStartup::ist_createHelpURL(sHelpBaseURL, sLocale, sSystem);
if (sHelpURL == sHelpURLForModule)
return true;
}
catch(const css::uno::RuntimeException&)
{ throw; }
catch(const css::uno::Exception&)
{}
}
return false;
}
OUString HelpOnStartup::its_checkIfHelpEnabledAndGetURL(const OUString& sModule)
{
// SAFE ->
osl::ClearableMutexGuard aLock(m_mutex);
css::uno::Reference< css::container::XNameAccess > xConfig = m_xConfig;
OUString sLocale = m_sLocale;
OUString sSystem = m_sSystem;
aLock.clear();
// <- SAFE
OUString sHelpURL;
try
{
css::uno::Reference< css::container::XNameAccess > xModuleConfig;
if (xConfig.is())
xConfig->getByName(sModule) >>= xModuleConfig;
bool bHelpEnabled = false;
if (xModuleConfig.is())
xModuleConfig->getByName("ooSetupFactoryHelpOnOpen") >>= bHelpEnabled;
if (bHelpEnabled)
{
OUString sHelpBaseURL;
xModuleConfig->getByName("ooSetupFactoryHelpBaseURL") >>= sHelpBaseURL;
sHelpURL = HelpOnStartup::ist_createHelpURL(sHelpBaseURL, sLocale, sSystem);
}
}
catch(const css::uno::RuntimeException&)
{ throw; }
catch(const css::uno::Exception&)
{ sHelpURL.clear(); }
return sHelpURL;
}
OUString HelpOnStartup::ist_createHelpURL(const OUString& sBaseURL,
const OUString& sLocale ,
const OUString& sSystem )
{
return sBaseURL + "?Language=" + sLocale + "&System=" + sSystem;
}
} // namespace framework
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|