summaryrefslogtreecommitdiffstats
path: root/lib/kofficecore/KoOasisSettings.h
blob: 4a213ca4245d521dcdaba9cac8fc179c7d372603 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* This file is part of the KDE project
   Copyright (C) 2004 Laurent Montel <montel@kde.org>
                      David Faure <faure@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/


#ifndef KOOASISSETTINGS_H
#define KOOASISSETTINGS_H

#include <tqdom.h>
#include <koffice_export.h>

/**
 * @brief Parse settings.xml file.
 *
 * This class helps parsing the settings.xml file of an OASIS document.
 *
 * For reference, the structure of settings.xml looks like:
 * <pre>
 *   \<office:settings\>
 *      \<config:config-item-set config:name="configure-settings"\>
 *      ....
 *      \</config:config-item-set\>
 *      \<config:config-item-set config:name="view-settings"\>
 *         \<config:config-item-map-indexed config:name="Views"\>
 *           \<config:config-item-map-entry\>
 *             \<config:config-item config:name="SnapLinesDrawing" config:type="string"\>value\</config:config-item\>
 *               ....
 *                \<config:config-item-map-named config:name="Tables"\>
 *                  \<config:config-item-map-entry config:name="Sheet1"\>
 *                    \<config:config-item config:name="CursorPositionX"\>
 *                    ......
 *                  \</config:config-item-map-entry\>
 *                  \<config:config-item-map-entry config:name="Sheet2"\>
 *                  ....
 *                  \</config:config-item-map-entry\>
 *                \</config:config-item-map-named\>
 *           .....
 *           \</config:config-item-map-entry\>
 *         \</config:config-item-map-indexed\>
 *         \<config:config-item-map-indexed config:name="Interface"\>
 *         .......
 *         \</config:config-item-map-indexed\>
 *      \</config:config-item-set\>
 *   \</office:settings\>
 * </pre>
 * Basically, an item-set is a set of named \<config-item\>s and/or maps.
 * There are two kinds of maps (by-index or by-name), and entries in the
 * maps contain \<config-item\>s too, or nested maps.
 *
 * The API of KoOasisSettings allows the caller to look for a given item-set
 * or item-map once, and then lookup multiple items inside it.
 * It also allows "drilling down" inside the tree in case of nesting.
 */
class KOFFICECORE_EXPORT KoOasisSettings
{
public:
    /**
     * Normal KoOasisSettings constructor, for an OASIS settings.xml
     */
    explicit KoOasisSettings( const TQDomDocument& doc );

    /**
     * KoOasisSettings constructor for an OpenOffice-1.1 file
     */
    KoOasisSettings( const TQDomDocument& doc, const char* officeNSURI, const char* configNSURI );

    class Items;

    /**
     * Returns the toplevel item-set named @p itemSetName.
     * If not found, the returned items instance is null.
     */
    Items itemSet( const TQString& itemSetName ) const;

    class IndexedMap;
    class NamedMap;
    /// Represents a collection of items (config-item or maps).
    class KOFFICECORE_EXPORT Items
    {
        friend class KoOasisSettings;
        friend class IndexedMap;
        friend class NamedMap;
        Items( const TQDomElement& elem, const KoOasisSettings* settings )
            : m_element( elem ), m_settings( settings ) {}
    public:
        bool isNull() const { return m_element.isNull(); }

        /**
         * Look for the config-item-map-indexed named @p itemMapName and return it.
         *
         * An indexed map is an array (or sequence), i.e. items are supposed to
         * be retrieved by index. This is useful for e.g. "view 0", "view 1" etc.
         */
        IndexedMap indexedMap( const TQString& itemMapName ) const;

        /**
         * Look for the config-item-map-named named @p mapItemName and return it.
         *
         * A named map is a map where items are retrieved by entry name, @see selectItemMapEntry
         * @return false if no such map was found
         */
        NamedMap namedMap( const TQString& itemMapName ) const;

        int parseConfigItemInt( const TQString& configName, int defValue = 0 ) const;
        double parseConfigItemDouble( const TQString& configName, double defValue = 0 ) const;
        TQString parseConfigItemString( const TQString& configName, const TQString& defValue = TQString() ) const;
        bool parseConfigItemBool( const TQString& configName, bool defValue = false ) const;
        short parseConfigItemShort( const TQString& configName, short defValue = 0 ) const;
        long parseConfigItemLong( const TQString& configName, long defValue = 0 ) const;
    private:
        /// @internal
        TQString findConfigItem( const TQString& item, bool* ok ) const;
        /// @internal
        TQString findConfigItem( const TQDomElement& element, const TQString& item, bool* ok ) const;

        TQDomElement m_element;
        const KoOasisSettings* m_settings;
    };

    /// Internal base class for IndexedMap and NamedMap
    class Map
    {
    public:
        bool isNull() const { return m_element.isNull(); }
    protected:
        Map( const TQDomElement& elem, const KoOasisSettings* settings )
            : m_element( elem ), m_settings( settings ) {}
        const TQDomElement m_element;
        const KoOasisSettings* m_settings;
    };

    class KOFFICECORE_EXPORT IndexedMap : public Map
    {
        friend class Items;
        IndexedMap( const TQDomElement& elem, const KoOasisSettings* settings )
            : Map( elem, settings ) {}
    public:
        /// Returns an entry in an indexed map
        Items entry( int entryIndex ) const;
    };

    class KOFFICECORE_EXPORT NamedMap : public Map
    {
        friend class Items;
        NamedMap( const TQDomElement& elem, const KoOasisSettings* settings )
            : Map( elem, settings ) {}
    public:
        /// Returns an entry in a named map
        Items entry( const TQString& entryName ) const;
    };

private:
    friend class Items;
    friend class IndexedMap;
    friend class NamedMap;
    const TQDomElement m_settingsElement;
    const char* m_configNSURI;

    class Private;
    Private* d;
};

#endif