summaryrefslogtreecommitdiffstats
path: root/kdecore/kconfigbackend.h
blob: 696062bf41b2351f9e674096d72db5b5f807ab44 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
   This file is part of the KDE libraries
   Copyright (c) 1999 Preston Brown <pbrown@kde.org>
   Portions copyright (c) 1997 Matthias Kalle Dalheimer <kalle@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 _KCONFIGBACKEND_H
#define _KCONFIGBACKEND_H

#include "kconfigdata.h"
#include <kconfigbase.h>
#include <klockfile.h>
#include <klocale.h>
#include "kdelibs_export.h"

class QFile;
class KConfigBackEndPrivate;

/**
 * Abstract base class for KDE configuration file loading/saving.
 *
 * This class forms the base for all classes that implement some
 * manner of loading/saving to configuration files.  It is an
 * abstract base class, meaning that you cannot directly instantiate
 * objects of this class. As of right now, the only back end available
 * is one to read/write to INI-style files, but in the future, other
 * formats may be available, such as XML or a database.
 *
 * @author Preston Brown <pbrown@kde.org>,
 *         Matthias Kalle Dalheimer <kalle@kde.org>
 * @short KDE Configuration file loading/saving abstract base class
 */
class KDECORE_EXPORT KConfigBackEnd
{
  friend class KConfig;
  friend class KSharedConfig;
public:
  /**
   * Constructs a configuration back end.
   *
   * @param _config Specifies the configuration object which values
   *        will be passed to as they are read, or from where values
   *        to be written to will be obtained from.
   * @param _fileName The name of the file in which config
   *        data is stored.  All registered configuration directories
   *        will be looked in in order of decreasing relevance.
   * @param _resType the resource type of the fileName specified, _if_
   *        it is not an absolute path (otherwise this parameter is ignored).
   * @param _useKDEGlobals If true, the user's system-wide kdeglobals file
   *        will be imported into the config object.  If false, only
   *        the filename specified will be dealt with.
   */
  KConfigBackEnd(KConfigBase *_config, const QString &_fileName,
		 const char * _resType, bool _useKDEGlobals);

  /**
   * Destructs the configuration backend.
   */
  virtual ~KConfigBackEnd();

  /**
   * Parses all configuration files for a configuration object.  This
   * method must be reimplemented by the derived classes.
   *
   * @returns Whether or not parsing was successful.
   */
  virtual bool parseConfigFiles() = 0;

  /**
   * Writes configuration data to file(s).  This method must be
   * reimplemented by the derived classes.
   *
   * @param bMerge Specifies whether the old config file already
   *        on disk should be merged in with the data in memory.  If true,
   *        data is read off the disk and merged.  If false, the on-disk
   *        file is removed and only in-memory data is written out.
   */
  virtual void sync(bool bMerge = true) = 0;

  /**
   * Changes the filenames associated with this back end.  You should
   * probably reparse your config info after doing this.
   *
   * @param _fileName the new filename to use
   * @param _resType the resource type of the fileName specified, _if_
   *        it is not an absolute path (otherwise this parameter is ignored).
   * @param _useKDEGlobals specifies whether or not to also parse the
   *        global KDE configuration files.
   */
  void changeFileName(const QString &_fileName, const char * _resType,
		      bool _useKDEGlobals);

  /**
   * Returns the state of the app-config object.
   *
   * @see KConfig::getConfigState
   */
  virtual KConfigBase::ConfigState getConfigState() const
    { return mConfigState; }

  /**
   * Returns the filename as passed to the constructor.
   * @return the filename as passed to the constructor.
   */
  QString fileName() const { return mfileName; }

  /**
   * Returns the resource type as passed to the constructor.
   * @return the resource type as passed to the constructor.
   */
  const char * resource() const { return resType; }

  /**
   * Set the locale string that defines the current language.
   * @param _localeString the identifier of the language
   * @see KLocale
   */
  void setLocaleString(const QCString &_localeString) { localeString = _localeString; }

  /**
   * Set the file mode for newly created files.
   * @param mode the filemode (as in chmod)
   */
  void setFileWriteMode(int mode);

  /**
   * Check whether the config files are writable.
   * @param warnUser Warn the user if the configuration files are not writable.
   * @return Indicates that all of the configuration files used are writable.
   * @since 3.2
   */
  bool checkConfigFilesWritable(bool warnUser);

  /**
   * Returns a lock file object for the configuration file
   * @param bGlobal If true, returns a lock file object for kdeglobals
   * @since 3.3
   */
  KLockFile::Ptr lockFile( bool bGlobal = false );

#ifdef KDE_NO_COMPAT
private:
#endif
  /**
   * @deprecated Use fileName() instead
   */
  KDE_DEPRECATED QString filename() const { return mfileName; }

protected:
  KConfigBase *pConfig;

  QString mfileName;
  QCString resType;
  bool useKDEGlobals : 1;
  bool bFileImmutable : 1;
  QCString localeString;
  QString mLocalFileName;
  QString mGlobalFileName;
  KConfigBase::ConfigState mConfigState;
  int mFileMode;

protected:
  virtual void virtual_hook( int id, void* data );
protected:
  class KConfigBackEndPrivate;
  KConfigBackEndPrivate *d;
};


/**
 * Class for KDE INI-style configuration file loading/saving.
 *
 * @author Preston Brown <pbrown@kde.org>,
 *         Matthias Kalle Dalheimer <kalle@kde.org>
 */
class KDECORE_EXPORT KConfigINIBackEnd : public KConfigBackEnd
{

public:
  /**
   * Constructs an ini-style configuration back end.
   *
   * @param _config Specifies the configuration object which values
   *        will be passed to as they are read, or from where values
   *        to be written to will be obtained from.
   * @param _fileName The name of the file in which config
   *        data is stored.  All registered configuration directories
   *        will be looked in in order of decreasing relevance.
   * @param _resType the resource type of the fileName specified, _if_
   *        it is not an absolute path (otherwise this parameter is ignored).
   * @param _useKDEGlobals If true, the user's system-wide kdeglobals file
   *        will be imported into the config object.  If false, only
   *        the filename specified will be dealt with.
   */
  KConfigINIBackEnd(KConfigBase *_config, const QString &_fileName,
		    const char * _resType, bool _useKDEGlobals = true)
    : KConfigBackEnd(_config, _fileName, _resType, _useKDEGlobals) {}

  /**
   * Destructs the configuration backend.
   */
  virtual ~KConfigINIBackEnd() {}

  /**
   * Parses all INI-style configuration files for a config object.
   *
   * @returns Whether or not parsing was successful.
   */
  bool parseConfigFiles();

  /**
   * Writes configuration data to file(s).
   * @param bMerge Specifies whether the old config file already
   *        on disk should be merged in with the data in memory.  If true,
   *        data is read off the disk and merged.  If false, the on-disk
   *        file is removed and only in-memory data is written out.
   */
  virtual void sync(bool bMerge = true);

protected:
  /**
   * Parses one configuration file.
   *
   * @param rFile The configuration file to parse
   * @param pWriteBackMap If specified, points to a KEntryMap where
   *        the data read from the file should be stored, instead of
   *        inserting them directly into the configuration object.
   *        Use this area as a "scratchpad" when you need to know what is
   *        on disk but don't want to effect the configuration object.
   * @param bGlobal Specifies whether entries should be marked as
   *        belonging to the global KDE configuration file rather
   *        than the application-specific KDE configuration file(s).
   * @param bDefault Specifies whether entries should be marked as
   *        being default values.
   */
  void parseSingleConfigFile(QFile& rFile, KEntryMap *pWriteBackMap = 0L,
			     bool bGlobal = false, bool bDefault = false);

  // Kubuntu patch, 2006-08-03
  // looks up a key in with KLocale
  // see https://launchpad.net/distros/ubuntu/+spec/langpacks-desktopfiles-kde
  void translateKey(KLocale& locale, QCString currentGroup, QCString key);

  /**
   * Writes configuration file back.
   *
   * @param filename The name of the file to write.
   * @param bGlobal Specifies whether to write only entries which
   *        are marked as belonging to the global KDE config file.
   *        If this is false, it skips those entries.
   * @param bMerge Specifies whether the old config file already
   *        on disk should be merged in with the data in memory.  If true,
   *        data is read off the disk and merged.  If false, the on-disk
   *        file is removed and only in-memory data is written out.
   * @return Whether some entries are left to be written to other
   *         files.
   */
  bool writeConfigFile(QString filename, bool bGlobal = false, bool bMerge = true);

  /** Get the entry map.
   *
   * @param map the entries will be stored in this object.
   * @param bGlobal Specifies whether to get only entries which
   *        are marked as belonging to the global KDE config file.
   *        If this is false, it skips those entries.
   * @param mergeFile if not null, the dirty entries for this file will
   * be merged.
   *
   * @return Whether there will be some entries left for writing to other
   * files.
   */
  bool getEntryMap(KEntryMap &map, bool bGlobal, QFile *mergeFile);

  /** Write the entries in @e aTempMap to the file stream.*/
  void writeEntries(FILE *pStream, const KEntryMap &aTempMap);

protected:
  virtual void virtual_hook( int id, void* data );
private:
  class KConfigINIBackEndPrivate;
  KConfigINIBackEndPrivate *not_d;
};

#endif