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/configuration.hxx>
29 #include <comphelper/configurationhelper.hxx>
30 #include <comphelper/processfactory.hxx>
32 #include <comphelper/diagnose_ex.hxx>
34 constexpr OUStringLiteral PACKAGE_VIEWS
= u
"org.openoffice.Office.Views";
35 constexpr OUString PROPERTY_WINDOWSTATE
= u
"WindowState"_ustr
;
36 constexpr OUString PROPERTY_PAGEID
= u
"PageID"_ustr
;
37 constexpr OUString PROPERTY_VISIBLE
= u
"Visible"_ustr
;
38 constexpr OUString PROPERTY_USERDATA
= u
"UserData"_ustr
;
41 SvtViewOptions::SvtViewOptions( EViewType eType
, OUString sViewName
)
42 : m_eViewType ( eType
)
43 , m_sViewName (std::move( sViewName
))
45 (void)m_eViewType
; // so the release build does not complain, since we only use it in assert
46 // we must know, which view type we must support
49 case EViewType::Dialog
: m_sListName
= "Dialogs"; break;
50 case EViewType::TabDialog
: m_sListName
= "TabDialogs"; break;
51 case EViewType::TabPage
: m_sListName
= "TabPages"; break;
52 case EViewType::Window
: m_sListName
= "Windows"; break;
53 default: assert(false);
55 if (comphelper::IsFuzzing())
60 m_xRoot
.set( ::comphelper::ConfigurationHelper::openConfig(
61 ::comphelper::getProcessComponentContext(),
63 ::comphelper::EConfigurationModes::Standard
),
66 m_xRoot
->getByName(m_sListName
) >>= m_xSet
;
68 catch(const css::uno::Exception
&)
70 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
78 /*-************************************************************************************************************
79 @short checks for already existing entries
80 @descr If user don't know, if an entry already exist - he can get this information by calling this method.
82 @seealso member m_aList
84 @param "sName", name of entry to check exist state
85 @return true , if item exist
87 *//*-*************************************************************************************************************/
88 bool SvtViewOptions::Exists() const
95 bExists
= m_xSet
->hasByName(m_sViewName
);
97 catch(const css::uno::Exception
&)
99 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
108 /*-************************************************************************************************************
110 @descr Use it to delete set entry by given name.
112 @seealso member m_aList
114 @param "sName", name of entry to delete it
115 *//*-*************************************************************************************************************/
116 void SvtViewOptions::Delete()
120 css::uno::Reference
< css::container::XNameContainer
> xSet(m_xSet
, css::uno::UNO_QUERY_THROW
);
121 xSet
->removeByName(m_sViewName
);
122 ::comphelper::ConfigurationHelper::flush(m_xRoot
);
124 catch(const css::container::NoSuchElementException
&)
126 catch(const css::uno::Exception
&)
128 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
134 /*-************************************************************************************************************
135 @short read/write access to cache view items and her properties
136 @descr Follow methods support read/write access to all cache view items.
138 @seealso member m_sList
139 *//*-*************************************************************************************************************/
140 OUString
SvtViewOptions::GetWindowState() const
142 OUString sWindowState
;
145 css::uno::Reference
< css::beans::XPropertySet
> xNode(
146 impl_getSetNode(m_sViewName
, false),
147 css::uno::UNO_QUERY
);
149 xNode
->getPropertyValue(PROPERTY_WINDOWSTATE
) >>= sWindowState
;
151 catch(const css::uno::Exception
&)
153 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
154 sWindowState
.clear();
163 /*-************************************************************************************************************
165 @descr We use it to open right configuration file and let configuration objects fill her caches.
166 Then we read all existing entries from right list and cached it inside our object too.
167 Normally we should enable notifications for changes on these values too ... but these feature
168 isn't full implemented in the moment.
170 @seealso baseclass ::utl::ConfigItem
171 @seealso method Notify()
172 *//*-*************************************************************************************************************/
173 void SvtViewOptions::SetWindowState( const OUString
& sState
)
177 css::uno::Reference
< css::beans::XPropertySet
> xNode(
178 impl_getSetNode(m_sViewName
, true),
179 css::uno::UNO_QUERY_THROW
);
180 xNode
->setPropertyValue(PROPERTY_WINDOWSTATE
, css::uno::Any(sState
));
181 ::comphelper::ConfigurationHelper::flush(m_xRoot
);
183 catch(const css::uno::Exception
&)
185 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
191 OUString
SvtViewOptions::GetPageID() const
193 // Safe impossible cases.
194 // These call isn't allowed for dialogs, tab-pages or windows!
195 assert( m_eViewType
== EViewType::TabDialog
&& "SvtViewOptions::GetPageID()\nCall not allowed for Dialogs, TabPages or Windows! I do nothing!" );
200 css::uno::Reference
< css::beans::XPropertySet
> xNode(
201 impl_getSetNode(m_sViewName
, false),
202 css::uno::UNO_QUERY
);
204 xNode
->getPropertyValue(PROPERTY_PAGEID
) >>= sID
;
206 catch(const css::uno::Exception
&)
208 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
217 void SvtViewOptions::SetPageID(const OUString
& rID
)
219 // Safe impossible cases.
220 // These call isn't allowed for dialogs, tab-pages or windows!
221 assert( m_eViewType
== EViewType::TabDialog
&& "SvtViewOptions::SetPageID()\nCall not allowed for Dialogs, TabPages or Windows! I do nothing!" );
225 css::uno::Reference
< css::beans::XPropertySet
> xNode(
226 impl_getSetNode(m_sViewName
, true),
227 css::uno::UNO_QUERY_THROW
);
228 xNode
->setPropertyValue(PROPERTY_PAGEID
, css::uno::Any(rID
));
229 ::comphelper::ConfigurationHelper::flush(m_xRoot
);
231 catch(const css::uno::Exception
&)
233 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
240 bool SvtViewOptions::IsVisible() const
242 // Safe impossible cases.
243 // These call isn't allowed for dialogs, tab-dialogs or tab-pages!
244 assert( m_eViewType
== EViewType::Window
&& "SvtViewOptions::IsVisible()\nCall not allowed for Dialogs, TabDialogs or TabPages! I do nothing!" );
246 return GetVisible() == STATE_TRUE
;
249 SvtViewOptions::State
SvtViewOptions::GetVisible() const
251 State eState
= STATE_NONE
;
254 css::uno::Reference
< css::beans::XPropertySet
> xNode(
255 impl_getSetNode(m_sViewName
, false),
256 css::uno::UNO_QUERY
);
259 bool bVisible
= false;
260 if (xNode
->getPropertyValue(PROPERTY_VISIBLE
) >>= bVisible
)
262 eState
= bVisible
? STATE_TRUE
: STATE_FALSE
;
266 catch(const css::uno::Exception
&)
268 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
275 void SvtViewOptions::SetVisible( bool bVisible
)
277 // Safe impossible cases.
278 // These call isn't allowed for dialogs, tab-dialogs or tab-pages!
279 assert(m_eViewType
== EViewType::Window
&& "SvtViewOptions::SetVisible()\nCall not allowed for Dialogs, TabDialogs or TabPages! I do nothing!" );
283 css::uno::Reference
< css::beans::XPropertySet
> xNode(
284 impl_getSetNode(m_sViewName
, true),
285 css::uno::UNO_QUERY_THROW
);
286 xNode
->setPropertyValue(PROPERTY_VISIBLE
, css::uno::Any(bVisible
));
287 ::comphelper::ConfigurationHelper::flush(m_xRoot
);
289 catch(const css::uno::Exception
&)
291 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
297 bool SvtViewOptions::HasVisible() const
299 // Safe impossible cases.
300 // These call isn't allowed for dialogs, tab-dialogs or tab-pages!
301 assert( m_eViewType
== EViewType::Window
&& "SvtViewOptions::IsVisible()\nCall not allowed for Dialogs, TabDialogs or TabPages! I do nothing!" );
303 return GetVisible() != STATE_NONE
;
306 css::uno::Sequence
< css::beans::NamedValue
> SvtViewOptions::GetUserData() const
310 css::uno::Reference
< css::container::XNameAccess
> xNode(
311 impl_getSetNode(m_sViewName
, false),
312 css::uno::UNO_QUERY
); // no _THROW ! because we don't create missing items here. So we have to live with zero references .-)
313 css::uno::Reference
< css::container::XNameAccess
> xUserData
;
315 xNode
->getByName(PROPERTY_USERDATA
) >>= xUserData
;
318 const css::uno::Sequence
<OUString
> lNames
= xUserData
->getElementNames();
319 sal_Int32 c
= lNames
.getLength();
320 css::uno::Sequence
< css::beans::NamedValue
> lUserData(c
);
322 std::transform(lNames
.begin(), lNames
.end(), lUserData
.getArray(),
323 [&xUserData
](const OUString
& rName
) -> css::beans::NamedValue
{
324 return { rName
, xUserData
->getByName(rName
) }; });
329 catch(const css::uno::Exception
&)
331 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
334 return css::uno::Sequence
< css::beans::NamedValue
>();
337 void SvtViewOptions::SetUserData( const css::uno::Sequence
< css::beans::NamedValue
>& lData
)
341 css::uno::Reference
< css::container::XNameAccess
> xNode(
342 impl_getSetNode(m_sViewName
, true),
343 css::uno::UNO_QUERY_THROW
);
344 css::uno::Reference
< css::container::XNameContainer
> xUserData
;
345 xNode
->getByName(PROPERTY_USERDATA
) >>= xUserData
;
348 for (const css::beans::NamedValue
& rData
: lData
)
350 if (xUserData
->hasByName(rData
.Name
))
351 xUserData
->replaceByName(rData
.Name
, rData
.Value
);
353 xUserData
->insertByName(rData
.Name
, rData
.Value
);
356 ::comphelper::ConfigurationHelper::flush(m_xRoot
);
358 catch(const css::uno::Exception
&)
360 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
364 css::uno::Any
SvtViewOptions::GetUserItem( const OUString
& sItemName
) const
369 css::uno::Reference
< css::container::XNameAccess
> xNode(
370 impl_getSetNode(m_sViewName
, false),
371 css::uno::UNO_QUERY
);
372 css::uno::Reference
< css::container::XNameAccess
> xUserData
;
374 xNode
->getByName(PROPERTY_USERDATA
) >>= xUserData
;
376 aItem
= xUserData
->getByName(sItemName
);
378 catch(const css::container::NoSuchElementException
&)
380 catch(const css::uno::Exception
&)
382 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
389 void SvtViewOptions::SetUserItem( const OUString
& sItemName
,
390 const css::uno::Any
& aValue
)
394 css::uno::Reference
< css::container::XNameAccess
> xNode(
395 impl_getSetNode(m_sViewName
, true),
396 css::uno::UNO_QUERY_THROW
);
397 css::uno::Reference
< css::container::XNameContainer
> xUserData
;
398 xNode
->getByName(PROPERTY_USERDATA
) >>= xUserData
;
401 if (xUserData
->hasByName(sItemName
))
402 xUserData
->replaceByName(sItemName
, aValue
);
404 xUserData
->insertByName(sItemName
, aValue
);
406 ::comphelper::ConfigurationHelper::flush(m_xRoot
);
408 catch(const css::uno::Exception
&)
410 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
416 /*-************************************************************************************************************
417 @short create new set node with default values on disk
418 @descr To create a new UserData item - the super node of these property must already exist!
419 You can call this method to create these new entry with default values and change UserData then.
421 @seealso method impl_writeDirectProp()
423 @param "sNode", name of new entry
424 *//*-*************************************************************************************************************/
425 css::uno::Reference
< css::uno::XInterface
> SvtViewOptions::impl_getSetNode( const OUString
& sNode
,
426 bool bCreateIfMissing
) const
428 css::uno::Reference
< css::uno::XInterface
> xNode
;
432 if (bCreateIfMissing
)
433 xNode
= ::comphelper::ConfigurationHelper::makeSureSetNodeExists(m_xRoot
, m_sListName
, sNode
);
436 if (m_xSet
.is() && m_xSet
->hasByName(sNode
) )
437 m_xSet
->getByName(sNode
) >>= xNode
;
440 catch(const css::container::NoSuchElementException
&)
442 catch(const css::uno::Exception
&)
444 TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
451 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */