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 <sal/config.h>
21 
22 #include <svtools/helpopt.hxx>
23 #include <unotools/configitem.hxx>
24 #include <tools/debug.hxx>
25 #include <com/sun/star/uno/Any.hxx>
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <comphelper/sequence.hxx>
28 #include <vcl/help.hxx>
29 #include <osl/mutex.hxx>
30 #include <sal/log.hxx>
31 
32 #include "itemholder2.hxx"
33 
34 using namespace utl;
35 using namespace com::sun::star::uno;
36 using namespace com::sun::star;
37 
38 namespace {
39     //global
40     std::weak_ptr<SvtHelpOptions_Impl> g_pHelpOptions;
41 }
42 
43 enum class HelpProperty
44 {
45     ExtendedHelp    = 0,
46     HelpTips        = 1,
47     Locale          = 2,
48     System          = 3,
49     StyleSheet      = 4,
50     OfflineHelpPopUp = 5
51 };
52 
53 class SvtHelpOptions_Impl : public utl::ConfigItem
54 {
55     bool            bExtendedHelp;
56     bool            bHelpTips;
57     bool            bOfflineHelpPopUp;
58     OUString        aLocale;
59     OUString        aSystem;
60     OUString        sHelpStyleSheet;
61 
62     static Sequence< OUString > const & GetPropertyNames();
63 
64     virtual void    ImplCommit() final override;
65 
66 public:
67                     SvtHelpOptions_Impl();
68                     ~SvtHelpOptions_Impl() override;
69 
70     virtual void    Notify( const css::uno::Sequence< OUString >& aPropertyNames ) override;
71     void            Load( const css::uno::Sequence< OUString>& aPropertyNames);
72 
SetExtendedHelp(bool b)73     void            SetExtendedHelp( bool b )           { bExtendedHelp= b; SetModified(); }
IsExtendedHelp() const74     bool            IsExtendedHelp() const                  { return bExtendedHelp; }
SetHelpTips(bool b)75     void            SetHelpTips( bool b )               { bHelpTips = b; SetModified(); }
IsHelpTips() const76     bool            IsHelpTips() const                      { return bHelpTips; }
SetOfflineHelpPopUp(bool b)77     void            SetOfflineHelpPopUp(bool b) { bOfflineHelpPopUp = b; SetModified();}
IsOfflineHelpPopUp() const78     bool            IsOfflineHelpPopUp() const { return bOfflineHelpPopUp;}
GetSystem() const79     const OUString& GetSystem() const                       { return aSystem; }
80 
GetHelpStyleSheet() const81     const OUString& GetHelpStyleSheet()const{return sHelpStyleSheet;}
SetHelpStyleSheet(const OUString & rStyleSheet)82     void            SetHelpStyleSheet(const OUString& rStyleSheet){sHelpStyleSheet = rStyleSheet; SetModified();}
83 
84     static ::osl::Mutex & getInitMutex();
85 };
86 
GetPropertyNames()87 Sequence< OUString > const & SvtHelpOptions_Impl::GetPropertyNames()
88 {
89     static Sequence<OUString> const aNames
90     {
91         "ExtendedTip",
92         "Tip",
93         "Locale",
94         "System",
95         "HelpStyleSheet",
96         "BuiltInHelpNotInstalledPopUp"
97     };
98 
99     return aNames;
100 }
101 
getInitMutex()102 ::osl::Mutex & SvtHelpOptions_Impl::getInitMutex()
103 {
104     static ::osl::Mutex ourMutex;
105 
106     return ourMutex;
107 }
108 
SvtHelpOptions_Impl()109 SvtHelpOptions_Impl::SvtHelpOptions_Impl()
110     : ConfigItem( "Office.Common/Help" )
111     , bExtendedHelp( false )
112     , bHelpTips( true )
113     , bOfflineHelpPopUp( true)
114 {
115     Sequence< OUString > aNames = GetPropertyNames();
116     Load( aNames );
117     EnableNotification( aNames );
118 }
119 
~SvtHelpOptions_Impl()120 SvtHelpOptions_Impl::~SvtHelpOptions_Impl()
121 {
122     if ( IsModified() )
123         Commit();
124 }
125 
Load(const uno::Sequence<OUString> & rPropertyNames)126 void  SvtHelpOptions_Impl::Load(const uno::Sequence< OUString>& rPropertyNames)
127 {
128     const uno::Sequence< OUString> aInternalPropertyNames( GetPropertyNames());
129     Sequence< Any > aValues = GetProperties( rPropertyNames );
130     const Any* pValues = aValues.getConstArray();
131     DBG_ASSERT( aValues.getLength() == rPropertyNames.getLength(), "GetProperties failed" );
132     if ( aValues.getLength() != rPropertyNames.getLength() )
133         return;
134 
135     for ( int nProp = 0; nProp < rPropertyNames.getLength(); nProp++ )
136     {
137         assert(pValues[nProp].hasValue() && "property value missing");
138         if ( pValues[nProp].hasValue() )
139         {
140             bool bTmp;
141             OUString aTmpStr;
142             sal_Int32 nTmpInt = 0;
143             if ( pValues[nProp] >>= bTmp )
144               {
145                 switch ( static_cast< HelpProperty >(
146                     comphelper::findValue(aInternalPropertyNames, rPropertyNames[nProp]) ) )
147                 {
148                     case HelpProperty::ExtendedHelp:
149                         bExtendedHelp = bTmp;
150                         break;
151                     case HelpProperty::HelpTips:
152                         bHelpTips = bTmp;
153                         break;
154                     case HelpProperty::OfflineHelpPopUp:
155                         bOfflineHelpPopUp = bTmp;
156                         break;
157                     default:
158                         SAL_WARN( "svtools.config", "Wrong Member!" );
159                         break;
160                 }
161             }
162             else if ( pValues[nProp] >>= aTmpStr )
163             {
164                 switch ( static_cast< HelpProperty >(nProp) )
165                 {
166                     case HelpProperty::Locale:
167                         aLocale = aTmpStr;
168                         break;
169 
170                     case HelpProperty::System:
171                         aSystem = aTmpStr;
172                         break;
173                     case HelpProperty::StyleSheet:
174                         sHelpStyleSheet = aTmpStr;
175                     break;
176                     default:
177                         SAL_WARN( "svtools.config", "Wrong Member!" );
178                         break;
179                 }
180             }
181             else if ( pValues[nProp] >>= nTmpInt )
182             {
183                 SAL_WARN( "svtools.config", "Wrong Member!" );
184             }
185             else
186             {
187                 SAL_WARN( "svtools.config", "Wrong Type!" );
188             }
189         }
190     }
191     if ( IsHelpTips() != Help::IsQuickHelpEnabled() )
192         IsHelpTips() ? Help::EnableQuickHelp() : Help::DisableQuickHelp();
193     if ( IsExtendedHelp() != Help::IsBalloonHelpEnabled() )
194         IsExtendedHelp() ? Help::EnableBalloonHelp() : Help::DisableBalloonHelp();
195 }
196 
ImplCommit()197 void SvtHelpOptions_Impl::ImplCommit()
198 {
199     Sequence< OUString > aNames = GetPropertyNames();
200     Sequence< Any > aValues( aNames.getLength() );
201     Any* pValues = aValues.getArray();
202     for ( int nProp = 0; nProp < aNames.getLength(); nProp++ )
203     {
204         switch ( static_cast< HelpProperty >(nProp) )
205         {
206             case HelpProperty::ExtendedHelp:
207                 pValues[nProp] <<= bExtendedHelp;
208                 break;
209 
210             case HelpProperty::HelpTips:
211                 pValues[nProp] <<= bHelpTips;
212                 break;
213 
214             case HelpProperty::Locale:
215                 pValues[nProp] <<= aLocale;
216                 break;
217 
218             case HelpProperty::System:
219                 pValues[nProp] <<= aSystem;
220                 break;
221             case HelpProperty::StyleSheet:
222                 pValues[nProp] <<= sHelpStyleSheet;
223             break;
224             case HelpProperty::OfflineHelpPopUp:
225               pValues[nProp] <<= bOfflineHelpPopUp;
226               break;
227 
228         }
229     }
230 
231     PutProperties( aNames, aValues );
232 }
233 
Notify(const Sequence<OUString> & aPropertyNames)234 void SvtHelpOptions_Impl::Notify( const Sequence<OUString>& aPropertyNames )
235 {
236     Load( aPropertyNames );
237 }
238 
SvtHelpOptions()239 SvtHelpOptions::SvtHelpOptions()
240 {
241     // Global access, must be guarded (multithreading)
242     ::osl::MutexGuard aGuard( SvtHelpOptions_Impl::getInitMutex() );
243 
244     pImpl = g_pHelpOptions.lock();
245     if ( !pImpl )
246     {
247         pImpl = std::make_shared<SvtHelpOptions_Impl>();
248         g_pHelpOptions = pImpl;
249         svtools::ItemHolder2::holdConfigItem(EItem::HelpOptions);
250     }
251 }
252 
~SvtHelpOptions()253 SvtHelpOptions::~SvtHelpOptions()
254 {
255     // Global access, must be guarded (multithreading)
256     ::osl::MutexGuard aGuard( SvtHelpOptions_Impl::getInitMutex() );
257 
258     pImpl.reset();
259 }
260 
SetExtendedHelp(bool b)261 void SvtHelpOptions::SetExtendedHelp( bool b )
262 {
263     pImpl->SetExtendedHelp( b );
264 }
265 
IsExtendedHelp() const266 bool SvtHelpOptions::IsExtendedHelp() const
267 {
268     return pImpl->IsExtendedHelp();
269 }
SetOfflineHelpPopUp(bool b)270 void SvtHelpOptions::SetOfflineHelpPopUp (bool b )
271 {
272     pImpl->SetOfflineHelpPopUp( b );
273 }
274 
IsOfflineHelpPopUp() const275 bool SvtHelpOptions::IsOfflineHelpPopUp() const
276 {
277     return pImpl->IsOfflineHelpPopUp();
278 }
SetHelpTips(bool b)279 void SvtHelpOptions::SetHelpTips( bool b )
280 {
281     pImpl->SetHelpTips( b );
282 }
283 
IsHelpTips() const284 bool SvtHelpOptions::IsHelpTips() const
285 {
286     return pImpl->IsHelpTips();
287 }
288 
GetSystem() const289 OUString const & SvtHelpOptions::GetSystem() const
290 {
291     return pImpl->GetSystem();
292 }
293 
GetHelpStyleSheet() const294 const OUString&   SvtHelpOptions::GetHelpStyleSheet()const
295 {
296     return pImpl->GetHelpStyleSheet();
297 }
298 
SetHelpStyleSheet(const OUString & rStyleSheet)299 void  SvtHelpOptions::SetHelpStyleSheet(const OUString& rStyleSheet)
300 {
301     pImpl->SetHelpStyleSheet(rStyleSheet);
302 }
303 
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
305