~renatofilho/buteo-syncfw/more-verbose

« back to all changes in this revision

Viewing changes to libsyncprofile/Profile.h

  • Committer: Sergey Gerasimenko
  • Date: 2010-06-29 12:51:21 UTC
  • Revision ID: git-v1:cd8dab07b102ac96752ece4f3cde5fc62697d717
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of buteo-syncfw package
 
3
 *
 
4
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 
5
 *
 
6
 * Contact: Sateesh Kavuri <sateesh.kavuri@nokia.com>
 
7
 *
 
8
 * This library is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU Lesser General Public License
 
10
 * version 2.1 as published by the Free Software Foundation.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
 * 02110-1301 USA
 
21
 *
 
22
 */
 
23
 
 
24
 
 
25
#ifndef PROFILE_H
 
26
#define PROFILE_H
 
27
 
 
28
#include <QList>
 
29
#include <QMap>
 
30
#include <QString>
 
31
#include <QStringList>
 
32
#include "ProfileField.h"
 
33
 
 
34
class QDomDocument;
 
35
class QDomElement;
 
36
 
 
37
namespace Buteo {
 
38
    
 
39
class ProfileTest;
 
40
class ProfilePrivate;
 
41
 
 
42
    
 
43
/*! \brief This class represents a single profile, a collection of settings or
 
44
 *  data releated to some entity.
 
45
 *
 
46
 * A profile can contain keys, fields and other profiles as sub-profiles.
 
47
 * Functions for accessing all these in different ways are provided. A profile
 
48
 * object can be created from XML and exported to XML, but otherwise the class
 
49
 * interface does not use XML. New classes can be derived from this class for
 
50
 * different profile types to add helper functions for accessing specific keys
 
51
 * and fields known by the profile type.
 
52
 */
 
53
class Profile
 
54
{
 
55
public:
 
56
 
 
57
    //! String constants for different profile type names.
 
58
    static const QString TYPE_CLIENT;
 
59
    static const QString TYPE_SERVER;
 
60
    static const QString TYPE_STORAGE;
 
61
    static const QString TYPE_SERVICE;
 
62
    static const QString TYPE_SYNC;
 
63
 
 
64
    /*! \brief Constructs a Profile object with given name and type.
 
65
     *
 
66
     * \param aName Profile name.
 
67
     * \param aType Profile type. Prefer using predefined constants like
 
68
     *  TYPE_SYNC.
 
69
     */
 
70
    Profile(const QString &aName, const QString &aType);
 
71
 
 
72
    /*! \brief Constructs a Profile object from XML.
 
73
     *
 
74
     * \param aRoot Root element of the profile XML.
 
75
     */
 
76
    explicit Profile(const QDomElement &aRoot);
 
77
 
 
78
    /*! \brief Copy constructor.
 
79
     *
 
80
     * \param aSource Copy source.
 
81
     */
 
82
    Profile(const Profile &aSource);
 
83
 
 
84
    /*! \brief Creates a clone of the profile.
 
85
     *
 
86
     * \return The clone.
 
87
     */
 
88
    virtual Profile *clone() const;
 
89
 
 
90
    //! \brief Destructor.
 
91
    virtual ~Profile();
 
92
 
 
93
    /*! \brief Gets the name of the profile.
 
94
     *
 
95
     * \return Profile name.
 
96
     */
 
97
    QString name() const;
 
98
 
 
99
    /*! \brief Sets the name of the profile.
 
100
     *
 
101
     * \param aName Name of the profile.
 
102
     */
 
103
    virtual void setName(const QString &aName);
 
104
 
 
105
    /*! \brief Gets the type of the profile.
 
106
     *
 
107
     * \return Profile type.
 
108
     */
 
109
    QString type() const;
 
110
 
 
111
    /*! \brief Creates a XML representation of the profile.
 
112
     *
 
113
     * \param aDoc Parent document for the created XML elements. The elements
 
114
     *  are not inserted to the document by this function, but the document is
 
115
     *  required to create the elements.
 
116
     * \param aLocalOnly Should only local profile elements be present in the
 
117
     *  generated XML. If this is true, elements merged from sub-profiles are
 
118
     *  not included.
 
119
     * \return Generated XML node tree.
 
120
     */
 
121
    virtual QDomElement toXml(QDomDocument &aDoc, bool aLocalOnly = true) const;
 
122
 
 
123
    /*! \brief Outputs a XML representation of the profile to a string.
 
124
     *
 
125
     * Merged sub-profile data is also included in the output string.
 
126
     * \return Generated XML string.
 
127
     */
 
128
    QString toString() const;
 
129
 
 
130
    /*! \brief Gets the value of the given key.
 
131
     *
 
132
     * \param aName Name of the key to read.
 
133
     * \param aDefault Default value.
 
134
     * \return Value of the key. If the key was not found, the default is
 
135
     *  returned. If there are multiple instances of the key with the given
 
136
     *  name, the value of the first instance is returned.
 
137
     */
 
138
    QString key(const QString &aName, const QString &aDefault = QString()) const;
 
139
 
 
140
    /*! \brief Gets all keys and their values.
 
141
     *
 
142
     * \return Map of key names/values.
 
143
     */
 
144
    QMap<QString, QString> allKeys() const;
 
145
 
 
146
    /*! \brief Gets all keys that are not related to storages.
 
147
     *
 
148
     * \return Map of key names/values.
 
149
     */
 
150
    QMap<QString, QString> allNonStorageKeys() const;
 
151
 
 
152
    /*! \brief Gets the value of the given boolean key.
 
153
     *
 
154
     * Returns true if the key exists and its value equals "true". If
 
155
     * the key does not exist, the default value is returned.
 
156
     * \param aName Name of the key to read.
 
157
     * \param aDefault Value to return if the key does not exist.
 
158
     * \return The boolean value of the key.
 
159
     */
 
160
    bool boolKey(const QString &aName, bool aDefault = false) const;
 
161
 
 
162
    /*! \brief Gets the values of all keys with the given name.
 
163
     *
 
164
     * If the key does not exist at all, an empty list is returned.
 
165
     * \param aName Name of the key to read.
 
166
     * \return List of values associated with the key.
 
167
     */
 
168
    QStringList keyValues(const QString &aName) const;
 
169
 
 
170
    /*! \brief Gets the names of all keys.
 
171
     *
 
172
     * \return List of key names.
 
173
     */
 
174
    QStringList keyNames() const;
 
175
 
 
176
    /*! \brief Sets the value of a key.
 
177
     *
 
178
     * If the key does not exist yet, it is created.
 
179
     * \param aName Name of the key.
 
180
     * \param aValue Value of the key.
 
181
     */
 
182
    void setKey(const QString &aName, const QString &aValue);
 
183
 
 
184
    /*! \brief Sets multiple values for a key.
 
185
     *
 
186
     * All previous (local) values of the key are removed. A key entry for
 
187
     * each of the provided values is then created.
 
188
     * \param aName Name of the key.
 
189
     * \param aValues Values for the key.
 
190
     */
 
191
    void setKeyValues(const QString &aName, const QStringList &aValues);
 
192
 
 
193
    /*! \brief Sets the value of a boolean key.
 
194
     *
 
195
     * The key value is set to "true" of "false". If the key does not exist
 
196
     * yet, it is created.
 
197
     * \param aName Name of the key.
 
198
     * \param aValue Value of the key.
 
199
     */
 
200
    void setBoolKey(const QString &aName, bool aValue);
 
201
 
 
202
    /*! \brief Removes a key from profile. All instances of the key are removed.
 
203
     *
 
204
     * \param aName Name of the key to remove.
 
205
     */
 
206
    void removeKey(const QString &aName);
 
207
 
 
208
    /*! \brief Gets the field with the given name.
 
209
     *
 
210
     * If the field does not exist, NULL is returned.
 
211
     * To get/set the value associated with the field, use the key handling
 
212
     * functions with the name of the field.
 
213
     * \param aName Name of the field.
 
214
     * \return Pointer to the field.
 
215
     */
 
216
    const ProfileField *field(const QString &aName) const;
 
217
 
 
218
    /*! \brief Gets all fields.
 
219
     *
 
220
     * \return List of pointers to the fields.
 
221
     */
 
222
    QList<const ProfileField*> allFields() const;
 
223
 
 
224
    /*! \brief Gets all visible fields of the profile.
 
225
     *
 
226
     * Each field can define its visibility. This functions returns only
 
227
     * fields that are visible.
 
228
     * \return List of pointers to the visible fields.
 
229
     */
 
230
    QList<const ProfileField*> visibleFields() const;
 
231
 
 
232
    /*! \brief Checks if the profile is valid.
 
233
     *
 
234
     * A profile is valid if:
 
235
     * 1. Name and type are set (not empty).
 
236
     * 2. For each field there is a key with the same name, and the key value
 
237
     *  (or all values, if multiple keys with the same name exist) is valid for
 
238
     *  the field.
 
239
     * 3. All sub-profiles are valid according to these three rules.
 
240
     * \return Is the profile valid.
 
241
     */
 
242
    bool isValid() const;
 
243
 
 
244
    /*! \brief Gets the names of all sub-profiles with the given type.
 
245
     *
 
246
     * \param aType Type of sub-profiles to get. If this is empty, all
 
247
     *  sub-profile names are returned.
 
248
     * \return Names of the sub-profiles.
 
249
     */
 
250
    QStringList subProfileNames(const QString &aType = "") const;
 
251
 
 
252
    /*! \brief Gets a sub-profile with the given name and type.
 
253
     *
 
254
     * \param aName Name of the sub-profile to get.
 
255
     * \param aType Type of the sub-profile to get. If the type is empty,
 
256
     *  any type is accepted.
 
257
     * \return The first sub-profile that matches the criteria. NULL if no such
 
258
     *  sub-profile was found. The returned sub-profile is owned by the main
 
259
     *  profile and the user must not delete it.
 
260
     */
 
261
    Profile *subProfile(const QString &aName, const QString &aType = "");
 
262
 
 
263
    const Profile *subProfile(const QString &aName, const QString &aType = "") const;
 
264
 
 
265
    /*! \brief Gets a sub-profile by key value.
 
266
     *
 
267
     * Returns the first sub-profile that has a key with the given value.
 
268
     * \param aKey Name of the key.
 
269
     * \param aValue Required value of the key.
 
270
     * \param aType Type of the sub-profile. If empty, any type can match.
 
271
     * \param aEnabledOnly Should only enabled sub-profiles be compared.
 
272
     * \return First matching sub-profile, NULL if no match.
 
273
     */
 
274
    const Profile *subProfileByKeyValue(const QString &aKey,
 
275
                                        const QString &aValue,
 
276
                                        const QString &aType,
 
277
                                        bool aEnabledOnly) const;
 
278
 
 
279
    /*! \brief Gets all sub-profiles.
 
280
     *
 
281
     * \return List of sub-profiles. The returned sub-profiles are owned by the main
 
282
     *  profile and the user must not delete them.
 
283
     */
 
284
    QList<Profile*> allSubProfiles();
 
285
 
 
286
    QList<const Profile*> allSubProfiles() const;
 
287
 
 
288
    /*! \brief Merges a profile to this profile.
 
289
     *
 
290
     * The source profile and all its sub-profiles are merged as direct
 
291
     * sub-profiles of this profile. This function is mainly used by the
 
292
     * ProfileManager, when it constructs a single profile from multiple
 
293
     * sub-profile files.
 
294
     * \param aSource Profile to merge.
 
295
     */
 
296
    void merge(const Profile &aSource);
 
297
 
 
298
    /*! \brief Checks if the profile is fully constructed by loading all
 
299
     * sub-profiles from separate profile files.
 
300
     *
 
301
     * A profile can have sub-profiles defined directly inside it, but
 
302
     * typically the sub-profiles are made complete by checking if there is
 
303
     * a separate profile file with the same name and type, and then loading
 
304
     * and merging the keys and fields defined in these files to the ones
 
305
     * defined directly in the main profile.
 
306
     * \return Is the profile fully loaded.
 
307
     */
 
308
    bool isLoaded() const;
 
309
 
 
310
    /*! \brief Sets if the profile is fully loaded.
 
311
     *
 
312
     * This function is used by the ProfileManager. The purpose of this flag
 
313
     * is to avoid loading the same sub-profile multiple times, if there are
 
314
     * more than one references to it in the sub-profile tree.
 
315
     * \param aLoaded Is the profile loaded.
 
316
     */
 
317
    void setLoaded(bool aLoaded);
 
318
 
 
319
    /*! \brief Returns if the profile is enabled.
 
320
     *
 
321
     * \return Is the profile enabled.
 
322
     */
 
323
    virtual bool isEnabled() const;
 
324
 
 
325
    /*! \brief Set is the profile is enabled.
 
326
     *
 
327
     * \param aEnabled New enabled status.
 
328
     */
 
329
    void setEnabled(bool aEnabled);
 
330
 
 
331
    /*! \brief Checks if the profile is hidden.
 
332
     *
 
333
     * A hidden profile should not be visible in sync ui.
 
334
     * \return True if hidden.
 
335
     */
 
336
    bool isHidden() const;
 
337
 
 
338
    /*! \brief Checks if the profile is protected.
 
339
     *
 
340
     * A protected profile can not be removed using the ProfileManager.
 
341
     * \return True if protected.
 
342
     */
 
343
    bool isProtected() const;
 
344
 
 
345
private:
 
346
 
 
347
    Profile& operator=(const Profile &aRhs);
 
348
 
 
349
    ProfilePrivate *d_ptr;
 
350
 
 
351
    friend class ProfileTest;
 
352
};
 
353
 
 
354
}
 
355
 
 
356
#endif // PROFILE_H