TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tdeconfigbackend.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. This file is part of the KDE libraries
  3. Copyright (c) 1999 Preston Brown <pbrown@kde.org>
  4. Portions copyright (c) 1997 Matthias Kalle Dalheimer <kalle@kde.org>
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public License
  14. along with this library; see the file COPYING.LIB. If not, write to
  15. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. Boston, MA 02110-1301, USA.
  17. */
  18. #ifndef _KCONFIGBACKEND_H
  19. #define _KCONFIGBACKEND_H
  20. #include "tdeconfigdata.h"
  21. #include <tdeconfigbase.h>
  22. #include <klockfile.h>
  23. #include <klocale.h>
  24. #include "tdelibs_export.h"
  25. class TQFile;
  26. class TDEConfigBackEndPrivate;
  27. /**
  28. * Abstract base class for KDE configuration file loading/saving.
  29. *
  30. * This class forms the base for all classes that implement some
  31. * manner of loading/saving to configuration files. It is an
  32. * abstract base class, meaning that you cannot directly instantiate
  33. * objects of this class. As of right now, the only back end available
  34. * is one to read/write to INI-style files, but in the future, other
  35. * formats may be available, such as XML or a database.
  36. *
  37. * @author Preston Brown <pbrown@kde.org>,
  38. * Matthias Kalle Dalheimer <kalle@kde.org>
  39. * @short KDE Configuration file loading/saving abstract base class
  40. */
  41. class TDECORE_EXPORT TDEConfigBackEnd
  42. {
  43. friend class TDEConfig;
  44. friend class TDESharedConfig;
  45. public:
  46. /**
  47. * Constructs a configuration back end.
  48. *
  49. * @param _config Specifies the configuration object which values
  50. * will be passed to as they are read, or from where values
  51. * to be written to will be obtained from.
  52. * @param _fileName The name of the file in which config
  53. * data is stored. All registered configuration directories
  54. * will be looked in in order of decreasing relevance.
  55. * @param _resType the resource type of the fileName specified, _if_
  56. * it is not an absolute path (otherwise this parameter is ignored).
  57. * @param _useKDEGlobals If true, the user's system-wide kdeglobals file
  58. * will be imported into the config object. If false, only
  59. * the filename specified will be dealt with.
  60. */
  61. TDEConfigBackEnd(TDEConfigBase *_config, const TQString &_fileName,
  62. const char * _resType, bool _useKDEGlobals);
  63. /**
  64. * Destructs the configuration backend.
  65. */
  66. virtual ~TDEConfigBackEnd();
  67. /**
  68. * Parses all configuration files for a configuration object. This
  69. * method must be reimplemented by the derived classes.
  70. *
  71. * @returns Whether or not parsing was successful.
  72. */
  73. virtual bool parseConfigFiles() = 0;
  74. /**
  75. * Writes configuration data to file(s). This method must be
  76. * reimplemented by the derived classes.
  77. *
  78. * @param bMerge Specifies whether the old config file already
  79. * on disk should be merged in with the data in memory. If true,
  80. * data is read off the disk and merged. If false, the on-disk
  81. * file is removed and only in-memory data is written out.
  82. */
  83. virtual void sync(bool bMerge = true) = 0;
  84. /**
  85. * Changes the filenames associated with this back end. You should
  86. * probably reparse your config info after doing this.
  87. *
  88. * @param _fileName the new filename to use
  89. * @param _resType the resource type of the fileName specified, _if_
  90. * it is not an absolute path (otherwise this parameter is ignored).
  91. * @param _useKDEGlobals specifies whether or not to also parse the
  92. * global KDE configuration files.
  93. */
  94. void changeFileName(const TQString &_fileName, const char * _resType,
  95. bool _useKDEGlobals);
  96. /**
  97. * Returns the state of the app-config object.
  98. *
  99. * @see TDEConfig::getConfigState
  100. */
  101. virtual TDEConfigBase::ConfigState getConfigState() const
  102. { return mConfigState; }
  103. /**
  104. * Returns the filename as passed to the constructor.
  105. * @return the filename as passed to the constructor.
  106. */
  107. TQString fileName() const { return mfileName; }
  108. /**
  109. * Returns the resource type as passed to the constructor.
  110. * @return the resource type as passed to the constructor.
  111. */
  112. const char * resource() const { return resType; }
  113. /**
  114. * Set the locale string that defines the current language.
  115. * @param _localeString the identifier of the language
  116. * @see TDELocale
  117. */
  118. void setLocaleString(const TQCString &_localeString) { localeString = _localeString; }
  119. /**
  120. * Set the file mode for newly created files.
  121. * @param mode the filemode (as in chmod)
  122. */
  123. void setFileWriteMode(int mode);
  124. /**
  125. * Check whether the config files are writable.
  126. * @param warnUser Warn the user if the configuration files are not writable.
  127. * @return Indicates that all of the configuration files used are writable.
  128. * @since 3.2
  129. */
  130. bool checkConfigFilesWritable(bool warnUser);
  131. /**
  132. * Returns a lock file object for the configuration file
  133. * @param bGlobal If true, returns a lock file object for kdeglobals
  134. * @since 3.3
  135. */
  136. TDELockFile::Ptr lockFile( bool bGlobal = false );
  137. #ifdef KDE_NO_COMPAT
  138. private:
  139. #endif
  140. /**
  141. * @deprecated Use fileName() instead
  142. */
  143. KDE_DEPRECATED TQString filename() const { return mfileName; }
  144. protected:
  145. TDEConfigBase *pConfig;
  146. TQString mfileName;
  147. TQCString resType;
  148. bool useKDEGlobals : 1;
  149. bool bFileImmutable : 1;
  150. TQCString localeString;
  151. TQString mLocalFileName;
  152. TQString mGlobalFileName;
  153. TDEConfigBase::ConfigState mConfigState;
  154. int mFileMode;
  155. protected:
  156. virtual void virtual_hook( int id, void* data );
  157. protected:
  158. class TDEConfigBackEndPrivate;
  159. TDEConfigBackEndPrivate *d;
  160. };
  161. /**
  162. * Class for KDE INI-style configuration file loading/saving.
  163. *
  164. * @author Preston Brown <pbrown@kde.org>,
  165. * Matthias Kalle Dalheimer <kalle@kde.org>
  166. */
  167. class TDECORE_EXPORT TDEConfigINIBackEnd : public TDEConfigBackEnd
  168. {
  169. public:
  170. /**
  171. * Constructs an ini-style configuration back end.
  172. *
  173. * @param _config Specifies the configuration object which values
  174. * will be passed to as they are read, or from where values
  175. * to be written to will be obtained from.
  176. * @param _fileName The name of the file in which config
  177. * data is stored. All registered configuration directories
  178. * will be looked in in order of decreasing relevance.
  179. * @param _resType the resource type of the fileName specified, _if_
  180. * it is not an absolute path (otherwise this parameter is ignored).
  181. * @param _useKDEGlobals If true, the user's system-wide kdeglobals file
  182. * will be imported into the config object. If false, only
  183. * the filename specified will be dealt with.
  184. */
  185. TDEConfigINIBackEnd(TDEConfigBase *_config, const TQString &_fileName,
  186. const char * _resType, bool _useKDEGlobals = true)
  187. : TDEConfigBackEnd(_config, _fileName, _resType, _useKDEGlobals) {}
  188. /**
  189. * Destructs the configuration backend.
  190. */
  191. virtual ~TDEConfigINIBackEnd() {}
  192. /**
  193. * Parses all INI-style configuration files for a config object.
  194. *
  195. * @returns Whether or not parsing was successful.
  196. */
  197. bool parseConfigFiles();
  198. /**
  199. * Writes configuration data to file(s).
  200. * @param bMerge Specifies whether the old config file already
  201. * on disk should be merged in with the data in memory. If true,
  202. * data is read off the disk and merged. If false, the on-disk
  203. * file is removed and only in-memory data is written out.
  204. */
  205. virtual void sync(bool bMerge = true);
  206. protected:
  207. /**
  208. * Parses one configuration file.
  209. *
  210. * @param rFile The configuration file to parse
  211. * @param pWriteBackMap If specified, points to a KEntryMap where
  212. * the data read from the file should be stored, instead of
  213. * inserting them directly into the configuration object.
  214. * Use this area as a "scratchpad" when you need to know what is
  215. * on disk but don't want to effect the configuration object.
  216. * @param bGlobal Specifies whether entries should be marked as
  217. * belonging to the global KDE configuration file rather
  218. * than the application-specific KDE configuration file(s).
  219. * @param bDefault Specifies whether entries should be marked as
  220. * being default values.
  221. */
  222. void parseSingleConfigFile(TQFile& rFile, KEntryMap *pWriteBackMap = 0L,
  223. bool bGlobal = false, bool bDefault = false);
  224. // Kubuntu patch, 2006-08-03
  225. // looks up a key in with TDELocale
  226. // see https://launchpad.net/distros/ubuntu/+spec/langpacks-desktopfiles-kde
  227. void translateKey(TDELocale& locale, TQCString currentGroup, TQCString key);
  228. /**
  229. * Writes configuration file back.
  230. *
  231. * @param filename The name of the file to write.
  232. * @param bGlobal Specifies whether to write only entries which
  233. * are marked as belonging to the global KDE config file.
  234. * If this is false, it skips those entries.
  235. * @param bMerge Specifies whether the old config file already
  236. * on disk should be merged in with the data in memory. If true,
  237. * data is read off the disk and merged. If false, the on-disk
  238. * file is removed and only in-memory data is written out.
  239. * @return Whether some entries are left to be written to other
  240. * files.
  241. */
  242. bool writeConfigFile(TQString filename, bool bGlobal = false, bool bMerge = true);
  243. /** Get the entry map.
  244. *
  245. * @param map the entries will be stored in this object.
  246. * @param bGlobal Specifies whether to get only entries which
  247. * are marked as belonging to the global KDE config file.
  248. * If this is false, it skips those entries.
  249. * @param mergeFile if not null, the dirty entries for this file will
  250. * be merged.
  251. *
  252. * @return Whether there will be some entries left for writing to other
  253. * files.
  254. */
  255. bool getEntryMap(KEntryMap &map, bool bGlobal, TQFile *mergeFile);
  256. /** Write the entries in @e aTempMap to the file stream.*/
  257. void writeEntries(FILE *pStream, const KEntryMap &aTempMap);
  258. protected:
  259. virtual void virtual_hook( int id, void* data );
  260. private:
  261. class TDEConfigINIBackEndPrivate;
  262. TDEConfigINIBackEndPrivate *not_d;
  263. };
  264. #endif