LibreOffice Module unotools (master) 1
viewoptions.cxx
Go to the documentation of this file.
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
21#include <com/sun/star/uno/Any.hxx>
22
23#include <com/sun/star/beans/NamedValue.hpp>
24#include <com/sun/star/container/XNameContainer.hpp>
25#include <com/sun/star/container/XNameAccess.hpp>
26#include <com/sun/star/beans/XPropertySet.hpp>
30#include <utility>
32
33constexpr OUStringLiteral PACKAGE_VIEWS = u"org.openoffice.Office.Views";
34constexpr OUStringLiteral PROPERTY_WINDOWSTATE = u"WindowState";
35constexpr OUStringLiteral PROPERTY_PAGEID = u"PageID";
36constexpr OUStringLiteral PROPERTY_VISIBLE = u"Visible";
37constexpr OUStringLiteral PROPERTY_USERDATA = u"UserData";
38
39
40SvtViewOptions::SvtViewOptions( EViewType eType, OUString sViewName )
41 : m_eViewType ( eType )
42 , m_sViewName (std::move( sViewName ))
43{
44 (void)m_eViewType; // so the release build does not complain, since we only use it in assert
45 // we must know, which view type we must support
46 switch( eType )
47 {
48 case EViewType::Dialog: m_sListName = "Dialogs"; break;
49 case EViewType::TabDialog: m_sListName = "TabDialogs"; break;
50 case EViewType::TabPage: m_sListName = "TabPages"; break;
51 case EViewType::Window: m_sListName = "Windows"; break;
52 default: assert(false);
53 }
55 return;
56
57 try
58 {
60 ::comphelper::getProcessComponentContext(),
63 css::uno::UNO_QUERY);
64 if (m_xRoot.is())
65 m_xRoot->getByName(m_sListName) >>= m_xSet;
66 }
67 catch(const css::uno::Exception&)
68 {
69 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
70 m_xRoot.clear();
71 m_xSet.clear();
72 }
73}
74
75// public method
76
77/*-************************************************************************************************************
78 @short checks for already existing entries
79 @descr If user don't know, if an entry already exist - he can get this information by calling this method.
80
81 @seealso member m_aList
82
83 @param "sName", name of entry to check exist state
84 @return true , if item exist
85 false, otherwise
86*//*-*************************************************************************************************************/
88{
89 bool bExists = false;
90
91 try
92 {
93 if (m_xSet.is())
94 bExists = m_xSet->hasByName(m_sViewName);
95 }
96 catch(const css::uno::Exception&)
97 {
98 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
99 bExists = false;
100 }
101
102 return bExists;
103}
104
105// public method
106
107/*-************************************************************************************************************
108 @short delete entry
109 @descr Use it to delete set entry by given name.
110
111 @seealso member m_aList
112
113 @param "sName", name of entry to delete it
114*//*-*************************************************************************************************************/
116{
117 try
118 {
119 css::uno::Reference< css::container::XNameContainer > xSet(m_xSet, css::uno::UNO_QUERY_THROW);
120 xSet->removeByName(m_sViewName);
122 }
123 catch(const css::container::NoSuchElementException&)
124 { }
125 catch(const css::uno::Exception&)
126 {
127 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
128 }
129}
130
131// public method
132
133/*-************************************************************************************************************
134 @short read/write access to cache view items and her properties
135 @descr Follow methods support read/write access to all cache view items.
136
137 @seealso member m_sList
138*//*-*************************************************************************************************************/
140{
141 OUString sWindowState;
142 try
143 {
144 css::uno::Reference< css::beans::XPropertySet > xNode(
146 css::uno::UNO_QUERY);
147 if (xNode.is())
148 xNode->getPropertyValue(PROPERTY_WINDOWSTATE) >>= sWindowState;
149 }
150 catch(const css::uno::Exception&)
151 {
152 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
153 sWindowState.clear();
154 }
155
156 return sWindowState;
157}
158
159
160// public method
161
162/*-************************************************************************************************************
163 @short ctor
164 @descr We use it to open right configuration file and let configuration objects fill her caches.
165 Then we read all existing entries from right list and cached it inside our object too.
166 Normally we should enable notifications for changes on these values too ... but these feature
167 isn't full implemented in the moment.
168
169 @seealso baseclass ::utl::ConfigItem
170 @seealso method Notify()
171*//*-*************************************************************************************************************/
172void SvtViewOptions::SetWindowState( const OUString& sState )
173{
174 try
175 {
176 css::uno::Reference< css::beans::XPropertySet > xNode(
178 css::uno::UNO_QUERY_THROW);
179 xNode->setPropertyValue(PROPERTY_WINDOWSTATE, css::uno::Any(sState));
181 }
182 catch(const css::uno::Exception&)
183 {
184 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
185 }
186}
187
188// public method
189
191{
192 // Safe impossible cases.
193 // These call isn't allowed for dialogs, tab-pages or windows!
194 assert( m_eViewType == EViewType::TabDialog && "SvtViewOptions::GetPageID()\nCall not allowed for Dialogs, TabPages or Windows! I do nothing!" );
195
196 OUString sID;
197 try
198 {
199 css::uno::Reference< css::beans::XPropertySet > xNode(
201 css::uno::UNO_QUERY);
202 if (xNode.is())
203 xNode->getPropertyValue(PROPERTY_PAGEID) >>= sID;
204 }
205 catch(const css::uno::Exception&)
206 {
207 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
208 }
209
210 return sID;
211}
212
213
214// public method
215
216void SvtViewOptions::SetPageID(const OUString& rID)
217{
218 // Safe impossible cases.
219 // These call isn't allowed for dialogs, tab-pages or windows!
220 assert( m_eViewType == EViewType::TabDialog && "SvtViewOptions::SetPageID()\nCall not allowed for Dialogs, TabPages or Windows! I do nothing!" );
221
222 try
223 {
224 css::uno::Reference< css::beans::XPropertySet > xNode(
226 css::uno::UNO_QUERY_THROW);
227 xNode->setPropertyValue(PROPERTY_PAGEID, css::uno::Any(rID));
229 }
230 catch(const css::uno::Exception&)
231 {
232 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
233 }
234}
235
236
237// public method
238
240{
241 // Safe impossible cases.
242 // These call isn't allowed for dialogs, tab-dialogs or tab-pages!
243 assert( m_eViewType == EViewType::Window && "SvtViewOptions::IsVisible()\nCall not allowed for Dialogs, TabDialogs or TabPages! I do nothing!" );
244
245 return GetVisible() == STATE_TRUE;
246}
247
249{
250 State eState = STATE_NONE;
251 try
252 {
253 css::uno::Reference< css::beans::XPropertySet > xNode(
255 css::uno::UNO_QUERY);
256 if (xNode.is())
257 {
258 bool bVisible = false;
259 if (xNode->getPropertyValue(PROPERTY_VISIBLE) >>= bVisible)
260 {
261 eState = bVisible ? STATE_TRUE : STATE_FALSE;
262 }
263 }
264 }
265 catch(const css::uno::Exception&)
266 {
267 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
268 }
269 return eState;
270}
271
272// public method
273
274void SvtViewOptions::SetVisible( bool bVisible )
275{
276 // Safe impossible cases.
277 // These call isn't allowed for dialogs, tab-dialogs or tab-pages!
278 assert(m_eViewType == EViewType::Window && "SvtViewOptions::SetVisible()\nCall not allowed for Dialogs, TabDialogs or TabPages! I do nothing!" );
279
280 try
281 {
282 css::uno::Reference< css::beans::XPropertySet > xNode(
284 css::uno::UNO_QUERY_THROW);
285 xNode->setPropertyValue(PROPERTY_VISIBLE, css::uno::Any(bVisible));
287 }
288 catch(const css::uno::Exception&)
289 {
290 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
291 }
292}
293
294// public method
295
297{
298 // Safe impossible cases.
299 // These call isn't allowed for dialogs, tab-dialogs or tab-pages!
300 assert( m_eViewType == EViewType::Window && "SvtViewOptions::IsVisible()\nCall not allowed for Dialogs, TabDialogs or TabPages! I do nothing!" );
301
302 return GetVisible() != STATE_NONE;
303}
304
305css::uno::Sequence< css::beans::NamedValue > SvtViewOptions::GetUserData() const
306{
307 try
308 {
309 css::uno::Reference< css::container::XNameAccess > xNode(
311 css::uno::UNO_QUERY); // no _THROW ! because we don't create missing items here. So we have to live with zero references .-)
312 css::uno::Reference< css::container::XNameAccess > xUserData;
313 if (xNode.is())
314 xNode->getByName(PROPERTY_USERDATA) >>= xUserData;
315 if (xUserData.is())
316 {
317 const css::uno::Sequence<OUString> lNames = xUserData->getElementNames();
318 sal_Int32 c = lNames.getLength();
319 css::uno::Sequence< css::beans::NamedValue > lUserData(c);
320
321 std::transform(lNames.begin(), lNames.end(), lUserData.getArray(),
322 [&xUserData](const OUString& rName) -> css::beans::NamedValue {
323 return { rName, xUserData->getByName(rName) }; });
324
325 return lUserData;
326 }
327 }
328 catch(const css::uno::Exception&)
329 {
330 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
331 }
332
333 return css::uno::Sequence< css::beans::NamedValue >();
334}
335
336void SvtViewOptions::SetUserData( const css::uno::Sequence< css::beans::NamedValue >& lData )
337{
338 try
339 {
340 css::uno::Reference< css::container::XNameAccess > xNode(
342 css::uno::UNO_QUERY_THROW);
343 css::uno::Reference< css::container::XNameContainer > xUserData;
344 xNode->getByName(PROPERTY_USERDATA) >>= xUserData;
345 if (xUserData.is())
346 {
347 for (const css::beans::NamedValue& rData : lData)
348 {
349 if (xUserData->hasByName(rData.Name))
350 xUserData->replaceByName(rData.Name, rData.Value);
351 else
352 xUserData->insertByName(rData.Name, rData.Value);
353 }
354 }
356 }
357 catch(const css::uno::Exception&)
358 {
359 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
360 }
361}
362
363css::uno::Any SvtViewOptions::GetUserItem( const OUString& sItemName ) const
364{
365 css::uno::Any aItem;
366 try
367 {
368 css::uno::Reference< css::container::XNameAccess > xNode(
370 css::uno::UNO_QUERY);
371 css::uno::Reference< css::container::XNameAccess > xUserData;
372 if (xNode.is())
373 xNode->getByName(PROPERTY_USERDATA) >>= xUserData;
374 if (xUserData.is())
375 aItem = xUserData->getByName(sItemName);
376 }
377 catch(const css::container::NoSuchElementException&)
378 { aItem.clear(); }
379 catch(const css::uno::Exception&)
380 {
381 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
382 aItem.clear();
383 }
384
385 return aItem;
386}
387
388void SvtViewOptions::SetUserItem( const OUString& sItemName ,
389 const css::uno::Any& aValue )
390{
391 try
392 {
393 css::uno::Reference< css::container::XNameAccess > xNode(
395 css::uno::UNO_QUERY_THROW);
396 css::uno::Reference< css::container::XNameContainer > xUserData;
397 xNode->getByName(PROPERTY_USERDATA) >>= xUserData;
398 if (xUserData.is())
399 {
400 if (xUserData->hasByName(sItemName))
401 xUserData->replaceByName(sItemName, aValue);
402 else
403 xUserData->insertByName(sItemName, aValue);
404 }
406 }
407 catch(const css::uno::Exception&)
408 {
409 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
410 }
411}
412
413
414
415/*-************************************************************************************************************
416 @short create new set node with default values on disk
417 @descr To create a new UserData item - the super node of these property must already exist!
418 You can call this method to create these new entry with default values and change UserData then.
419
420 @seealso method impl_writeDirectProp()
421
422 @param "sNode", name of new entry
423*//*-*************************************************************************************************************/
424css::uno::Reference< css::uno::XInterface > SvtViewOptions::impl_getSetNode( const OUString& sNode ,
425 bool bCreateIfMissing) const
426{
427 css::uno::Reference< css::uno::XInterface > xNode;
428
429 try
430 {
431 if (bCreateIfMissing)
433 else
434 {
435 if (m_xSet.is() && m_xSet->hasByName(sNode) )
436 m_xSet->getByName(sNode) >>= xNode;
437 }
438 }
439 catch(const css::container::NoSuchElementException&)
440 { xNode.clear(); }
441 catch(const css::uno::Exception&)
442 {
443 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
444 xNode.clear();
445 }
446
447 return xNode;
448}
449
450/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString m_sViewName
css::uno::Reference< css::container::XNameAccess > m_xRoot
State GetVisible() const
void SetPageID(const OUString &rID)
css::uno::Any GetUserItem(const OUString &sName) const
void SetUserItem(const OUString &sName, const css::uno::Any &aValue)
bool HasVisible() const
Return true if the "Visible" property actually has a non-nil value.
bool IsVisible() const
void SetWindowState(const OUString &sState)
void SetUserData(const css::uno::Sequence< css::beans::NamedValue > &lData)
OUString GetPageID() const
css::uno::Reference< css::uno::XInterface > impl_getSetNode(const OUString &sNode, bool bCreateIfMissing) const
EViewType m_eViewType
specify which list of views in configuration is used! This can't be a static value!...
css::uno::Reference< css::container::XNameAccess > m_xSet
OUString m_sListName
OUString GetWindowState() const
void SetVisible(bool bState)
css::uno::Sequence< css::beans::NamedValue > GetUserData() const
bool Exists() const
Definition: viewoptions.cxx:87
SvtViewOptions(EViewType eType, OUString sViewName)
Definition: viewoptions.cxx:40
static css::uno::Reference< css::uno::XInterface > makeSureSetNodeExists(const css::uno::Reference< css::uno::XInterface > &xCFG, const OUString &sRelPathToSet, const OUString &sSetNode)
static css::uno::Reference< css::uno::XInterface > openConfig(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const OUString &sPackage, EConfigurationModes eMode)
static void flush(const css::uno::Reference< css::uno::XInterface > &xCFG)
static bool IsFuzzing()
Definition: configmgr.cxx:181
#define TOOLS_WARN_EXCEPTION(area, stream)
float u
DocumentType eType
bool bVisible
constexpr OUStringLiteral PROPERTY_VISIBLE
Definition: viewoptions.cxx:36
constexpr OUStringLiteral PROPERTY_USERDATA
Definition: viewoptions.cxx:37
constexpr OUStringLiteral PROPERTY_WINDOWSTATE
Definition: viewoptions.cxx:34
constexpr OUStringLiteral PACKAGE_VIEWS
Definition: viewoptions.cxx:33
constexpr OUStringLiteral PROPERTY_PAGEID
Definition: viewoptions.cxx:35
EViewType
Definition: viewoptions.hxx:35