Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svtools/source/config/viewoptions.cxx
Line
Count
Source
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 <com/sun/star/uno/Any.hxx>
21
22
#include <com/sun/star/beans/NamedValue.hpp>
23
#include <com/sun/star/container/XNameContainer.hpp>
24
#include <com/sun/star/container/XNameAccess.hpp>
25
#include <com/sun/star/beans/XPropertySet.hpp>
26
#include <svtools/viewoptions.hxx>
27
#include <unotools/configmgr.hxx>
28
#include <comphelper/configuration.hxx>
29
#include <comphelper/configurationhelper.hxx>
30
#include <comphelper/processfactory.hxx>
31
#include <utility>
32
#include <comphelper/diagnose_ex.hxx>
33
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;
39
40
41
SvtViewOptions::SvtViewOptions( EViewType eType, OUString sViewName )
42
298k
    :   m_eViewType ( eType     )
43
298k
    ,   m_sViewName (std::move( sViewName ))
44
298k
{
45
298k
    (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
47
298k
    switch( eType )
48
298k
    {
49
0
        case EViewType::Dialog: m_sListName = "Dialogs"; break;
50
0
        case EViewType::TabDialog: m_sListName = "TabDialogs"; break;
51
0
        case EViewType::TabPage: m_sListName = "TabPages"; break;
52
298k
        case EViewType::Window: m_sListName = "Windows"; break;
53
0
        default: assert(false);
54
298k
    }
55
298k
    if (comphelper::IsFuzzing())
56
298k
        return;
57
58
0
    try
59
0
    {
60
0
        m_xRoot.set( ::comphelper::ConfigurationHelper::openConfig(
61
0
                            ::comphelper::getProcessComponentContext(),
62
0
                            PACKAGE_VIEWS,
63
0
                            ::comphelper::EConfigurationModes::Standard),
64
0
                     css::uno::UNO_QUERY);
65
0
        if (m_xRoot.is())
66
0
            m_xRoot->getByName(m_sListName) >>= m_xSet;
67
0
    }
68
0
    catch(const css::uno::Exception&)
69
0
        {
70
0
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
71
0
            m_xRoot.clear();
72
0
            m_xSet.clear();
73
0
        }
74
0
}
75
76
//  public method
77
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.
81
82
    @seealso        member m_aList
83
84
    @param          "sName", name of entry to check exist state
85
    @return         true , if item exist
86
                    false, otherwise
87
*//*-*************************************************************************************************************/
88
bool SvtViewOptions::Exists() const
89
265k
{
90
265k
    bool bExists = false;
91
92
265k
    try
93
265k
    {
94
265k
        if (m_xSet.is())
95
0
            bExists = m_xSet->hasByName(m_sViewName);
96
265k
    }
97
265k
    catch(const css::uno::Exception&)
98
265k
        {
99
0
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
100
0
            bExists = false;
101
0
        }
102
103
265k
    return bExists;
104
265k
}
105
106
//  public method
107
108
/*-************************************************************************************************************
109
    @short          delete entry
110
    @descr          Use it to delete set entry by given name.
111
112
    @seealso        member m_aList
113
114
    @param          "sName", name of entry to delete it
115
*//*-*************************************************************************************************************/
116
void SvtViewOptions::Delete()
117
0
{
118
0
    try
119
0
    {
120
0
        css::uno::Reference< css::container::XNameContainer > xSet(m_xSet, css::uno::UNO_QUERY_THROW);
121
0
        xSet->removeByName(m_sViewName);
122
0
        ::comphelper::ConfigurationHelper::flush(m_xRoot);
123
0
    }
124
0
    catch(const css::container::NoSuchElementException&)
125
0
        { }
126
0
    catch(const css::uno::Exception&)
127
0
        {
128
0
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
129
0
        }
130
0
}
131
132
//  public method
133
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.
137
138
    @seealso        member m_sList
139
*//*-*************************************************************************************************************/
140
vcl::WindowData SvtViewOptions::GetWindowState() const
141
132k
{
142
132k
    OUString sWindowState;
143
132k
    try
144
132k
    {
145
132k
        css::uno::Reference< css::beans::XPropertySet > xNode(
146
132k
            impl_getSetNode(m_sViewName, false),
147
132k
            css::uno::UNO_QUERY);
148
132k
        if (xNode.is())
149
0
            xNode->getPropertyValue(PROPERTY_WINDOWSTATE) >>= sWindowState;
150
132k
    }
151
132k
    catch(const css::uno::Exception&)
152
132k
        {
153
0
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
154
0
            sWindowState.clear();
155
0
        }
156
157
132k
    return vcl::WindowData(sWindowState);
158
132k
}
159
160
161
//  public method
162
163
/*-************************************************************************************************************
164
    @short          ctor
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.
169
170
    @seealso        baseclass ::utl::ConfigItem
171
    @seealso        method Notify()
172
*//*-*************************************************************************************************************/
173
174
void SvtViewOptions::SetWindowState(const vcl::WindowData& rState)
175
0
{
176
0
    const OUString sState = rState.toStr();
177
178
0
    try
179
0
    {
180
0
        css::uno::Reference< css::beans::XPropertySet > xNode(
181
0
            impl_getSetNode(m_sViewName, true),
182
0
            css::uno::UNO_QUERY_THROW);
183
0
        xNode->setPropertyValue(PROPERTY_WINDOWSTATE, css::uno::Any(sState));
184
0
        ::comphelper::ConfigurationHelper::flush(m_xRoot);
185
0
    }
186
0
    catch(const css::uno::Exception&)
187
0
        {
188
0
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
189
0
        }
190
0
}
191
192
//  public method
193
194
OUString SvtViewOptions::GetPageID() const
195
0
{
196
    // Safe impossible cases.
197
    // These call isn't allowed for dialogs, tab-pages or windows!
198
0
    assert( m_eViewType == EViewType::TabDialog && "SvtViewOptions::GetPageID()\nCall not allowed for Dialogs, TabPages or Windows! I do nothing!" );
199
200
0
    OUString sID;
201
0
    try
202
0
    {
203
0
        css::uno::Reference< css::beans::XPropertySet > xNode(
204
0
            impl_getSetNode(m_sViewName, false),
205
0
            css::uno::UNO_QUERY);
206
0
        if (xNode.is())
207
0
            xNode->getPropertyValue(PROPERTY_PAGEID) >>= sID;
208
0
    }
209
0
    catch(const css::uno::Exception&)
210
0
        {
211
0
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
212
0
        }
213
214
0
    return sID;
215
0
}
216
217
218
//  public method
219
220
void SvtViewOptions::SetPageID(const OUString& rID)
221
0
{
222
    // Safe impossible cases.
223
    // These call isn't allowed for dialogs, tab-pages or windows!
224
0
    assert( m_eViewType == EViewType::TabDialog && "SvtViewOptions::SetPageID()\nCall not allowed for Dialogs, TabPages or Windows! I do nothing!" );
225
226
0
    try
227
0
    {
228
0
        css::uno::Reference< css::beans::XPropertySet > xNode(
229
0
            impl_getSetNode(m_sViewName, true),
230
0
            css::uno::UNO_QUERY_THROW);
231
0
        xNode->setPropertyValue(PROPERTY_PAGEID, css::uno::Any(rID));
232
0
        ::comphelper::ConfigurationHelper::flush(m_xRoot);
233
0
    }
234
0
    catch(const css::uno::Exception&)
235
0
        {
236
0
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
237
0
        }
238
0
}
239
240
241
//  public method
242
243
bool SvtViewOptions::IsVisible() const
244
0
{
245
    // Safe impossible cases.
246
    // These call isn't allowed for dialogs, tab-dialogs or tab-pages!
247
0
    assert( m_eViewType == EViewType::Window && "SvtViewOptions::IsVisible()\nCall not allowed for Dialogs, TabDialogs or TabPages! I do nothing!" );
248
249
0
    return GetVisible() == STATE_TRUE;
250
0
}
251
252
SvtViewOptions::State SvtViewOptions::GetVisible() const
253
0
{
254
0
    State eState = STATE_NONE;
255
0
    try
256
0
    {
257
0
        css::uno::Reference< css::beans::XPropertySet > xNode(
258
0
            impl_getSetNode(m_sViewName, false),
259
0
            css::uno::UNO_QUERY);
260
0
        if (xNode.is())
261
0
        {
262
0
            bool bVisible = false;
263
0
            if (xNode->getPropertyValue(PROPERTY_VISIBLE) >>= bVisible)
264
0
            {
265
0
                eState = bVisible ? STATE_TRUE : STATE_FALSE;
266
0
            }
267
0
        }
268
0
    }
269
0
    catch(const css::uno::Exception&)
270
0
        {
271
0
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
272
0
        }
273
0
    return eState;
274
0
}
275
276
//  public method
277
278
void SvtViewOptions::SetVisible( bool bVisible )
279
0
{
280
    // Safe impossible cases.
281
    // These call isn't allowed for dialogs, tab-dialogs or tab-pages!
282
0
    assert(m_eViewType == EViewType::Window && "SvtViewOptions::SetVisible()\nCall not allowed for Dialogs, TabDialogs or TabPages! I do nothing!" );
283
284
0
    try
285
0
    {
286
0
        css::uno::Reference< css::beans::XPropertySet > xNode(
287
0
            impl_getSetNode(m_sViewName, true),
288
0
            css::uno::UNO_QUERY_THROW);
289
0
        xNode->setPropertyValue(PROPERTY_VISIBLE, css::uno::Any(bVisible));
290
0
        ::comphelper::ConfigurationHelper::flush(m_xRoot);
291
0
    }
292
0
    catch(const css::uno::Exception&)
293
0
        {
294
0
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
295
0
        }
296
0
}
297
298
//  public method
299
300
bool SvtViewOptions::HasVisible() const
301
0
{
302
    // Safe impossible cases.
303
    // These call isn't allowed for dialogs, tab-dialogs or tab-pages!
304
0
    assert( m_eViewType == EViewType::Window && "SvtViewOptions::IsVisible()\nCall not allowed for Dialogs, TabDialogs or TabPages! I do nothing!" );
305
306
0
    return GetVisible() != STATE_NONE;
307
0
}
308
309
css::uno::Sequence< css::beans::NamedValue > SvtViewOptions::GetUserData() const
310
132k
{
311
132k
    try
312
132k
    {
313
132k
        css::uno::Reference< css::container::XNameAccess > xNode(
314
132k
            impl_getSetNode(m_sViewName, false),
315
132k
            css::uno::UNO_QUERY); // no _THROW ! because we don't create missing items here. So we have to live with zero references .-)
316
132k
        css::uno::Reference< css::container::XNameAccess > xUserData;
317
132k
        if (xNode.is())
318
0
            xNode->getByName(PROPERTY_USERDATA) >>= xUserData;
319
132k
        if (xUserData.is())
320
0
        {
321
0
            const css::uno::Sequence<OUString> lNames = xUserData->getElementNames();
322
0
            sal_Int32 c = lNames.getLength();
323
0
            css::uno::Sequence< css::beans::NamedValue > lUserData(c);
324
325
0
            std::transform(lNames.begin(), lNames.end(), lUserData.getArray(),
326
0
                [&xUserData](const OUString& rName) -> css::beans::NamedValue {
327
0
                    return { rName, xUserData->getByName(rName) }; });
328
329
0
            return lUserData;
330
0
        }
331
132k
    }
332
132k
    catch(const css::uno::Exception&)
333
132k
        {
334
0
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
335
0
        }
336
337
132k
    return css::uno::Sequence< css::beans::NamedValue >();
338
132k
}
339
340
void SvtViewOptions::SetUserData( const css::uno::Sequence< css::beans::NamedValue >& lData )
341
0
{
342
0
    try
343
0
    {
344
0
        css::uno::Reference< css::container::XNameAccess > xNode(
345
0
            impl_getSetNode(m_sViewName, true),
346
0
            css::uno::UNO_QUERY_THROW);
347
0
        css::uno::Reference< css::container::XNameContainer > xUserData;
348
0
        xNode->getByName(PROPERTY_USERDATA) >>= xUserData;
349
0
        if (xUserData.is())
350
0
        {
351
0
            for (const css::beans::NamedValue& rData : lData)
352
0
            {
353
0
                if (xUserData->hasByName(rData.Name))
354
0
                    xUserData->replaceByName(rData.Name, rData.Value);
355
0
                else
356
0
                    xUserData->insertByName(rData.Name, rData.Value);
357
0
            }
358
0
        }
359
0
        ::comphelper::ConfigurationHelper::flush(m_xRoot);
360
0
    }
361
0
    catch(const css::uno::Exception&)
362
0
        {
363
0
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
364
0
        }
365
0
}
366
367
css::uno::Any SvtViewOptions::GetUserItem( const OUString& sItemName ) const
368
16.5k
{
369
16.5k
    css::uno::Any aItem;
370
16.5k
    try
371
16.5k
    {
372
16.5k
        css::uno::Reference< css::container::XNameAccess > xNode(
373
16.5k
            impl_getSetNode(m_sViewName, false),
374
16.5k
            css::uno::UNO_QUERY);
375
16.5k
        css::uno::Reference< css::container::XNameAccess > xUserData;
376
16.5k
        if (xNode.is())
377
0
            xNode->getByName(PROPERTY_USERDATA) >>= xUserData;
378
16.5k
        if (xUserData.is())
379
0
            aItem = xUserData->getByName(sItemName);
380
16.5k
    }
381
16.5k
    catch(const css::container::NoSuchElementException&)
382
16.5k
        { aItem.clear(); }
383
16.5k
    catch(const css::uno::Exception&)
384
16.5k
        {
385
0
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
386
0
            aItem.clear();
387
0
        }
388
389
16.5k
    return aItem;
390
16.5k
}
391
392
void SvtViewOptions::SetUserItem( const OUString& sItemName  ,
393
                                  const css::uno::Any&   aValue )
394
16.5k
{
395
16.5k
    try
396
16.5k
    {
397
16.5k
        css::uno::Reference< css::container::XNameAccess > xNode(
398
16.5k
            impl_getSetNode(m_sViewName, true),
399
16.5k
            css::uno::UNO_QUERY_THROW);
400
16.5k
        css::uno::Reference< css::container::XNameContainer > xUserData;
401
16.5k
        xNode->getByName(PROPERTY_USERDATA) >>= xUserData;
402
16.5k
        if (xUserData.is())
403
0
        {
404
0
            if (xUserData->hasByName(sItemName))
405
0
                xUserData->replaceByName(sItemName, aValue);
406
0
            else
407
0
                xUserData->insertByName(sItemName, aValue);
408
0
        }
409
16.5k
        ::comphelper::ConfigurationHelper::flush(m_xRoot);
410
16.5k
    }
411
16.5k
    catch(const css::uno::Exception&)
412
16.5k
        {
413
16.5k
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
414
16.5k
        }
415
16.5k
}
416
417
418
419
/*-************************************************************************************************************
420
    @short          create new set node with default values on disk
421
    @descr          To create a new UserData item - the super node of these property must already exist!
422
                    You can call this method to create these new entry with default values and change UserData then.
423
424
    @seealso        method impl_writeDirectProp()
425
426
    @param          "sNode", name of new entry
427
*//*-*************************************************************************************************************/
428
css::uno::Reference< css::uno::XInterface > SvtViewOptions::impl_getSetNode( const OUString& sNode           ,
429
                                                                                            bool         bCreateIfMissing) const
430
298k
{
431
298k
    css::uno::Reference< css::uno::XInterface > xNode;
432
433
298k
    try
434
298k
    {
435
298k
        if (bCreateIfMissing)
436
16.5k
            xNode = ::comphelper::ConfigurationHelper::makeSureSetNodeExists(m_xRoot, m_sListName, sNode);
437
281k
        else
438
281k
        {
439
281k
            if (m_xSet.is() && m_xSet->hasByName(sNode) )
440
0
                m_xSet->getByName(sNode) >>= xNode;
441
281k
        }
442
298k
    }
443
298k
    catch(const css::container::NoSuchElementException&)
444
298k
        { xNode.clear(); }
445
298k
    catch(const css::uno::Exception&)
446
298k
        {
447
16.5k
            TOOLS_WARN_EXCEPTION("unotools", "Unexpected exception");
448
16.5k
            xNode.clear();
449
16.5k
        }
450
451
298k
    return xNode;
452
298k
}
453
454
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */