KVIrc  4.9.2
DeveloperAPIs
KviConfigurationFile.h
Go to the documentation of this file.
1 #ifndef _KVI_CONFIG_H_INCLUDED_
2 #define _KVI_CONFIG_H_INCLUDED_
3 
4 //=============================================================================
5 //
6 // File : KviConfigurationFile.h
7 // Creation date : Thu Jan 14 1999 18:01:22 by Szymon Stefanek
8 //
9 // This file is part of the KVIrc IRC client distribution
10 // Copyright (C) 1999-2010 Szymon Stefanek (pragma at kvirc dot net)
11 //
12 // This program is FREE software. You can redistribute it and/or
13 // modify it under the terms of the GNU General Public License
14 // as published by the Free Software Foundation; either version 2
15 // of the License, or (at your option) any later version.
16 //
17 // This program is distributed in the HOPE that it will be USEFUL,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 // See the GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program. If not, write to the Free Software Foundation,
24 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 //
26 //=============================================================================
27 
28 #include "KviCString.h"
29 #include "kvi_settings.h"
30 #include "KviHeapObject.h"
31 #include "KviPointerHashTable.h"
32 
33 #include <QList>
34 #include <QColor>
35 #include <QFont>
36 #include <QRect>
37 #include <QStringList>
38 
39 #define KVI_CONFIG_DEFAULT_GROUP "KVIrc"
40 
41 class KviPixmap;
43 
47 
49 {
50 public:
51  enum FileMode
52  {
53  Read = 1,
54  Write = 2,
55  ReadWrite = 3
56  };
57 
58 public:
59  KviConfigurationFile(const QString & filename, FileMode f /* = ReadWrite*/, bool bLocal8Bit = false);
60  KviConfigurationFile(const char * filename, FileMode f /* = ReadWrite*/, bool bLocal8Bit = false);
62 
63 private:
66  QString m_szFileName;
67  bool m_bDirty;
68  QString m_szStrBuffer;
69  QString m_szGroup;
72 
73 private:
74  bool load();
75  bool save();
76  KviConfigurationFileGroup * getCurrentGroup();
77 
78 public:
79  //
80  // Useful when saving...
81  // Normally this class does not save empty groups
82  // and setGroup() is never a config modification.
83  // If the group is not existing it will be effectively
84  // created only at the first attempt to read from it or write to it.
85  // With this flag set to true the KviConfigurationFile class will
86  // write down also the empty groups, and calls to setGroup()
87  // will create the groups if not existing yet (and set the config data
88  // as modified).
89  //
90  void preserveEmptyGroups(bool bPreserve) { m_bPreserveEmptyGroups = bPreserve; };
91  const QString & fileName() { return m_szFileName; };
92  bool readOnly() { return m_bReadOnly; };
93  void setReadOnly(bool bReadOnly) { m_bReadOnly = bReadOnly; };
94  bool dirty() { return m_bDirty; };
95  bool ensureWritable();
96  //
97  // This sets the save path for the config file
98  // In this way you can load a system-wide read-only config file
99  // as default configuration, alter its settings and save it to the
100  // user local configuration directory
101  void setSavePath(const QString & savePath) { m_szFileName = savePath; };
103 
104  void clearDirtyFlag() { m_bDirty = false; };
105  void clear();
106  void clearGroup(const QString & szGroup);
107  void clearKey(const QString & szKey);
108  unsigned int groupsCount() { return m_pDict->count(); };
109  bool sync() { return save(); };
110  bool hasKey(const QString & szKey);
111  bool hasGroup(const QString & szGroup);
112  void setGroup(const QString & szGroup);
113  //void getContentsString(KviCString &buffer);
114  const QString & group() { return m_szGroup; };
115  void writeEntry(const QString & szKey, const QString & szValue);
116  void writeEntry(const QString & szKey, const char * szValue)
117  {
118  writeEntry(szKey, QString::fromUtf8(szValue));
119  };
120  QString readEntry(const QString & szKey, const QString & szDefault = QString());
121  // HACK for KviOptions.. (FIXME)
122  QString readKviCStringEntry(const QString & szKey, const KviCString & szDefault)
123  {
124  return readEntry(szKey, szDefault.ptr());
125  };
126  //void writeEntry(const char *szKey,KviCString &szValue);
127  //const char * readEntry(const char *szKey,KviCString &szDefault);
128  void writeEntry(const QString & szKey, const KviPixmap & pixmap);
129  KviPixmap readPixmapEntry(const QString & szKey, const KviPixmap & pixDef);
130  void writeEntry(const QString & szKey, const KviMessageTypeSettings & msg);
131  KviMessageTypeSettings readMsgTypeEntry(const QString & szKey, const KviMessageTypeSettings & msgDef);
132  void writeEntry(const QString & szKey, const QColor & clr);
133  QColor readColorEntry(const QString & szKey, const QColor & clr);
134  void writeEntry(const QString & szKey, QFont & fnt);
135  QFont readFontEntry(const QString & szKey, const QFont & fnt);
136  void writeEntry(const QString & szKey, bool bTrue);
137  bool readBoolEntry(const QString & szKey, bool bTrue);
138  QRect readRectEntry(const QString & szKey, const QRect & rct);
139  void writeEntry(const QString & szKey, const QRect & rct);
140  QStringList readStringListEntry(const QString & szKey, const QStringList & list = QStringList());
141  void writeEntry(const QString & szKey, const QStringList & list);
142  QList<int> readIntListEntry(const QString &, const QList<int> & list);
143  void writeEntry(const QString & szKey, const QList<int> & list);
144  //void writeEntry(const char *szKey,unsigned long lValue);
145  //unsigned long readULongEntry(const char *szKey,unsigned long lDefault);
146  //void writeEntry(const char *szKey,long lValue);
147  //long readLongEntry(const char *szKey,long lDefault);
148  void writeEntry(const QString & szKey, int iValue);
149  int readIntEntry(const QString & szKey, int iDefault);
150  void writeEntry(const QString & szKey, unsigned short int usValue);
151  unsigned short int readUShortEntry(const QString & szKey, unsigned short int usDefault);
152  void writeEntry(const QString & szKey, unsigned int iValue);
153  unsigned int readUIntEntry(const QString & szKey, unsigned int iDefault);
154  void writeEntry(const QString & szKey, char iValue);
155  char readCharEntry(const QString & szKey, char iDefault);
156  void writeEntry(const QString & szKey, unsigned char iValue);
157  unsigned char readUCharEntry(const QString & szKey, unsigned char iDefault);
158 
159  static void getFontProperties(KviCString & buffer, QFont * fnt);
160  static void setFontProperties(KviCString & str, QFont * fnt);
161 
162 #ifdef COMPILE_ON_WINDOWS
163  // On windows we need to override new and delete operators
164  // to ensure that always the right new/delete pair is called for an object instance
165  // This bug is present in all the classes exported by a module that
166  // can be instantiated/destroyed from external modules.
167  // (this is a well known bug described in Q122675 of MSDN)
168  void * operator new(size_t tSize);
169  void operator delete(void * p);
170 #endif
171 };
172 
173 #endif
Definition: KviHeapObject.h:124
Definition: KviCString.h:105
QString m_szStrBuffer
Definition: KviConfigurationFile.h:68
bool readOnly()
Definition: KviConfigurationFile.h:92
#define f
Definition: detector.cpp:70
Definition: KviConfigurationFile.h:48
void setSavePath(const QString &savePath)
Definition: KviConfigurationFile.h:101
A fast pointer hash table iterator implementation.
Definition: KviPointerHashTable.h:393
KviPointerHashTable< QString, QString > KviConfigurationFileGroup
Definition: KviConfigurationFile.h:42
btnDict clear()
Pointer Hash Table.
Definition: KviPixmap.h:33
void preserveEmptyGroups(bool bPreserve)
Definition: KviConfigurationFile.h:90
Definition: KviMessageTypeSettings.h:47
void setReadOnly(bool bReadOnly)
Definition: KviConfigurationFile.h:93
bool save(KviThemeInfo &options, bool bSaveIcons)
Definition: KviOptions.cpp:986
KviPointerHashTableIterator< QString, QString > KviConfigurationFileGroupIterator
Definition: KviConfigurationFile.h:45
KviPointerHashTable< QString, KviConfigurationFileGroup > * dict()
Definition: KviConfigurationFile.h:102
bool m_bLocal8Bit
Definition: KviConfigurationFile.h:64
QString m_szFileName
Definition: KviConfigurationFile.h:66
const QString & fileName()
Definition: KviConfigurationFile.h:91
KviPointerHashTableIterator< QString, KviConfigurationFileGroup > KviConfigurationFileIterator
Definition: KviConfigurationFile.h:46
bool sync()
Definition: KviConfigurationFile.h:109
bool dirty()
Definition: KviConfigurationFile.h:94
const QString & group()
Definition: KviConfigurationFile.h:114
Heap Object.
KviPointerHashTable< QString, KviConfigurationFileGroup > * m_pDict
Definition: KviConfigurationFile.h:65
bool m_bPreserveEmptyGroups
Definition: KviConfigurationFile.h:70
unsigned int groupsCount()
Definition: KviConfigurationFile.h:108
bool m_bReadOnly
Definition: KviConfigurationFile.h:71
void writeEntry(const QString &szKey, const char *szValue)
Definition: KviConfigurationFile.h:116
FileMode
Definition: KviConfigurationFile.h:51
This file contains compile time settings.
QString readKviCStringEntry(const QString &szKey, const KviCString &szDefault)
Definition: KviConfigurationFile.h:122
char * ptr() const
Definition: KviCString.h:172
#define p
Definition: detector.cpp:80
#define KVILIB_API
Definition: kvi_settings.h:125
QString m_szGroup
Definition: KviConfigurationFile.h:69
bool m_bDirty
Definition: KviConfigurationFile.h:67
void clearDirtyFlag()
Definition: KviConfigurationFile.h:104