1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <unotools/viewoptions.hxx>
21 #include <com/sun/star/uno/Any.hxx>
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>
27 #include <unotools/configmgr.hxx>
28 #include <comphelper/configurationhelper.hxx>
29 #include <comphelper/processfactory.hxx>
31 #include <comphelper/diagnose_ex.hxx>
33 constexpr OUStringLiteral PACKAGE_VIEWS
= u
"org.openoffice.Office.Views";
34 constexpr OUStringLiteral PROPERTY_WINDOWSTATE
= u
"WindowState";
35 constexpr OUStringLiteral PROPERTY_PAGEID
= u
"PageID";
36 constexpr OUStringLiteral PROPERTY_VISIBLE
= u
"Visible";
37 constexpr OUStringLiteral PROPERTY_USERDATA
= u
"UserData";
40 SvtViewOptions::SvtViewOptions( EViewType eType
, OUString sViewName
)
41 : m_eViewType ( eType
)
42 , m_sViewName (std::move( sViewName
))
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
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);
54 if (utl::ConfigManager::IsFuzzing())
59 m_xRoot
.set( ::comphelper::ConfigurationHelper::openConfig(
60 ::comphelper::getProcessComponentContext(),
62 ::comphelper::EConfigurationModes::Standard
),
65 m_xRoot
->getByName(m_sListName
) >>= m_xSet
;
67 catch(const css::uno::Exception
&)
69 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
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.
81 @seealso member m_aList
83 @param "sName", name of entry to check exist state
84 @return true , if item exist
86 *//*-*************************************************************************************************************/
87 bool SvtViewOptions::Exists() const
94 bExists
= m_xSet
->hasByName(m_sViewName
);
96 catch(const css::uno::Exception
&)
98 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
107 /*-************************************************************************************************************
109 @descr Use it to delete set entry by given name.
111 @seealso member m_aList
113 @param "sName", name of entry to delete it
114 *//*-*************************************************************************************************************/
115 void SvtViewOptions::Delete()
119 css::uno::Reference
< css::container::XNameContainer
> xSet(m_xSet
, css::uno::UNO_QUERY_THROW
);
120 xSet
->removeByName(m_sViewName
);
121 ::comphelper::ConfigurationHelper::flush(m_xRoot
);
123 catch(const css::container::NoSuchElementException
&)
125 catch(const css::uno::Exception
&)
127 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
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.
137 @seealso member m_sList
138 *//*-*************************************************************************************************************/
139 OUString
SvtViewOptions::GetWindowState() const
141 OUString sWindowState
;
144 css::uno::Reference
< css::beans::XPropertySet
> xNode(
145 impl_getSetNode(m_sViewName
, false),
146 css::uno::UNO_QUERY
);
148 xNode
->getPropertyValue(PROPERTY_WINDOWSTATE
) >>= sWindowState
;
150 catch(const css::uno::Exception
&)
152 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
153 sWindowState
.clear();
162 /*-************************************************************************************************************
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.
169 @seealso baseclass ::utl::ConfigItem
170 @seealso method Notify()
171 *//*-*************************************************************************************************************/
172 void SvtViewOptions::SetWindowState( const OUString
& sState
)
176 css::uno::Reference
< css::beans::XPropertySet
> xNode(
177 impl_getSetNode(m_sViewName
, true),
178 css::uno::UNO_QUERY_THROW
);
179 xNode
->setPropertyValue(PROPERTY_WINDOWSTATE
, css::uno::Any(sState
));
180 ::comphelper::ConfigurationHelper::flush(m_xRoot
);
182 catch(const css::uno::Exception
&)
184 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
190 OUString
SvtViewOptions::GetPageID() const
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!" );
199 css::uno::Reference
< css::beans::XPropertySet
> xNode(
200 impl_getSetNode(m_sViewName
, false),
201 css::uno::UNO_QUERY
);
203 xNode
->getPropertyValue(PROPERTY_PAGEID
) >>= sID
;
205 catch(const css::uno::Exception
&)
207 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
216 void SvtViewOptions::SetPageID(const OUString
& rID
)
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!" );
224 css::uno::Reference
< css::beans::XPropertySet
> xNode(
225 impl_getSetNode(m_sViewName
, true),
226 css::uno::UNO_QUERY_THROW
);
227 xNode
->setPropertyValue(PROPERTY_PAGEID
, css::uno::Any(rID
));
228 ::comphelper::ConfigurationHelper::flush(m_xRoot
);
230 catch(const css::uno::Exception
&)
232 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
239 bool SvtViewOptions::IsVisible() const
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!" );
245 return GetVisible() == STATE_TRUE
;
248 SvtViewOptions::State
SvtViewOptions::GetVisible() const
250 State eState
= STATE_NONE
;
253 css::uno::Reference
< css::beans::XPropertySet
> xNode(
254 impl_getSetNode(m_sViewName
, false),
255 css::uno::UNO_QUERY
);
258 bool bVisible
= false;
259 if (xNode
->getPropertyValue(PROPERTY_VISIBLE
) >>= bVisible
)
261 eState
= bVisible
? STATE_TRUE
: STATE_FALSE
;
265 catch(const css::uno::Exception
&)
267 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
274 void SvtViewOptions::SetVisible( bool bVisible
)
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!" );
282 css::uno::Reference
< css::beans::XPropertySet
> xNode(
283 impl_getSetNode(m_sViewName
, true),
284 css::uno::UNO_QUERY_THROW
);
285 xNode
->setPropertyValue(PROPERTY_VISIBLE
, css::uno::Any(bVisible
));
286 ::comphelper::ConfigurationHelper::flush(m_xRoot
);
288 catch(const css::uno::Exception
&)
290 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
296 bool SvtViewOptions::HasVisible() const
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!" );
302 return GetVisible() != STATE_NONE
;
305 css::uno::Sequence
< css::beans::NamedValue
> SvtViewOptions::GetUserData() const
309 css::uno::Reference
< css::container::XNameAccess
> xNode(
310 impl_getSetNode(m_sViewName
, false),
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
;
314 xNode
->getByName(PROPERTY_USERDATA
) >>= xUserData
;
317 const css::uno::Sequence
<OUString
> lNames
= xUserData
->getElementNames();
318 sal_Int32 c
= lNames
.getLength();
319 css::uno::Sequence
< css::beans::NamedValue
> lUserData(c
);
321 std::transform(lNames
.begin(), lNames
.end(), lUserData
.getArray(),
322 [&xUserData
](const OUString
& rName
) -> css::beans::NamedValue
{
323 return { rName
, xUserData
->getByName(rName
) }; });
328 catch(const css::uno::Exception
&)
330 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
333 return css::uno::Sequence
< css::beans::NamedValue
>();
336 void SvtViewOptions::SetUserData( const css::uno::Sequence
< css::beans::NamedValue
>& lData
)
340 css::uno::Reference
< css::container::XNameAccess
> xNode(
341 impl_getSetNode(m_sViewName
, true),
342 css::uno::UNO_QUERY_THROW
);
343 css::uno::Reference
< css::container::XNameContainer
> xUserData
;
344 xNode
->getByName(PROPERTY_USERDATA
) >>= xUserData
;
347 for (const css::beans::NamedValue
& rData
: lData
)
349 if (xUserData
->hasByName(rData
.Name
))
350 xUserData
->replaceByName(rData
.Name
, rData
.Value
);
352 xUserData
->insertByName(rData
.Name
, rData
.Value
);
355 ::comphelper::ConfigurationHelper::flush(m_xRoot
);
357 catch(const css::uno::Exception
&)
359 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
363 css::uno::Any
SvtViewOptions::GetUserItem( const OUString
& sItemName
) const
368 css::uno::Reference
< css::container::XNameAccess
> xNode(
369 impl_getSetNode(m_sViewName
, false),
370 css::uno::UNO_QUERY
);
371 css::uno::Reference
< css::container::XNameAccess
> xUserData
;
373 xNode
->getByName(PROPERTY_USERDATA
) >>= xUserData
;
375 aItem
= xUserData
->getByName(sItemName
);
377 catch(const css::container::NoSuchElementException
&)
379 catch(const css::uno::Exception
&)
381 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
388 void SvtViewOptions::SetUserItem( const OUString
& sItemName
,
389 const css::uno::Any
& aValue
)
393 css::uno::Reference
< css::container::XNameAccess
> xNode(
394 impl_getSetNode(m_sViewName
, true),
395 css::uno::UNO_QUERY_THROW
);
396 css::uno::Reference
< css::container::XNameContainer
> xUserData
;
397 xNode
->getByName(PROPERTY_USERDATA
) >>= xUserData
;
400 if (xUserData
->hasByName(sItemName
))
401 xUserData
->replaceByName(sItemName
, aValue
);
403 xUserData
->insertByName(sItemName
, aValue
);
405 ::comphelper::ConfigurationHelper::flush(m_xRoot
);
407 catch(const css::uno::Exception
&)
409 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
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.
420 @seealso method impl_writeDirectProp()
422 @param "sNode", name of new entry
423 *//*-*************************************************************************************************************/
424 css::uno::Reference
< css::uno::XInterface
> SvtViewOptions::impl_getSetNode( const OUString
& sNode
,
425 bool bCreateIfMissing
) const
427 css::uno::Reference
< css::uno::XInterface
> xNode
;
431 if (bCreateIfMissing
)
432 xNode
= ::comphelper::ConfigurationHelper::makeSureSetNodeExists(m_xRoot
, m_sListName
, sNode
);
435 if (m_xSet
.is() && m_xSet
->hasByName(sNode
) )
436 m_xSet
->getByName(sNode
) >>= xNode
;
439 catch(const css::container::NoSuchElementException
&)
441 catch(const css::uno::Exception
&)
443 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
450 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */