-rw-r--r-- | library/config.cpp | 2 |
1 files changed, 0 insertions, 2 deletions
diff --git a/library/config.cpp b/library/config.cpp index b28c771..8b60f60 100644 --- a/library/config.cpp +++ b/library/config.cpp | |||
@@ -1,215 +1,213 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | #include <qdir.h> | 21 | #include <qdir.h> |
22 | #include <qfile.h> | ||
23 | #include <qfileinfo.h> | ||
24 | #include <qmessagebox.h> | 22 | #include <qmessagebox.h> |
25 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) | 23 | #if QT_VERSION <= 230 && defined(QT_NO_CODECS) |
26 | #include <qtextcodec.h> | 24 | #include <qtextcodec.h> |
27 | #endif | 25 | #endif |
28 | #include <qtextstream.h> | 26 | #include <qtextstream.h> |
29 | 27 | ||
30 | #include <sys/stat.h> | 28 | #include <sys/stat.h> |
31 | #include <sys/types.h> | 29 | #include <sys/types.h> |
32 | #include <fcntl.h> | 30 | #include <fcntl.h> |
33 | #include <stdlib.h> | 31 | #include <stdlib.h> |
34 | #include <unistd.h> | 32 | #include <unistd.h> |
35 | 33 | ||
36 | #define QTOPIA_INTERNAL_LANGLIST | 34 | #define QTOPIA_INTERNAL_LANGLIST |
37 | #include "config.h" | 35 | #include "config.h" |
38 | #include "global.h" | 36 | #include "global.h" |
39 | 37 | ||
40 | 38 | ||
41 | /*! | 39 | /*! |
42 | \internal | 40 | \internal |
43 | */ | 41 | */ |
44 | QString Config::configFilename(const QString& name, Domain d) | 42 | QString Config::configFilename(const QString& name, Domain d) |
45 | { | 43 | { |
46 | switch (d) { | 44 | switch (d) { |
47 | case File: | 45 | case File: |
48 | return name; | 46 | return name; |
49 | case User: { | 47 | case User: { |
50 | QDir dir = (QString(getenv("HOME")) + "/Settings"); | 48 | QDir dir = (QString(getenv("HOME")) + "/Settings"); |
51 | if ( !dir.exists() ) | 49 | if ( !dir.exists() ) |
52 | mkdir(dir.path().local8Bit(),0700); | 50 | mkdir(dir.path().local8Bit(),0700); |
53 | return dir.path() + "/" + name + ".conf"; | 51 | return dir.path() + "/" + name + ".conf"; |
54 | } | 52 | } |
55 | } | 53 | } |
56 | return name; | 54 | return name; |
57 | } | 55 | } |
58 | 56 | ||
59 | /*! | 57 | /*! |
60 | \class Config config.h | 58 | \class Config config.h |
61 | \brief The Config class provides for saving application cofniguration state. | 59 | \brief The Config class provides for saving application cofniguration state. |
62 | 60 | ||
63 | You should keep a Config in existence only while you do not want others | 61 | You should keep a Config in existence only while you do not want others |
64 | to be able to change the state. There is no locking currently, but there | 62 | to be able to change the state. There is no locking currently, but there |
65 | may be in the future. | 63 | may be in the future. |
66 | */ | 64 | */ |
67 | 65 | ||
68 | /*! | 66 | /*! |
69 | \enum Config::ConfigGroup | 67 | \enum Config::ConfigGroup |
70 | \internal | 68 | \internal |
71 | */ | 69 | */ |
72 | 70 | ||
73 | /*! | 71 | /*! |
74 | \enum Config::Domain | 72 | \enum Config::Domain |
75 | 73 | ||
76 | \value File | 74 | \value File |
77 | \value User | 75 | \value User |
78 | 76 | ||
79 | See Config for details. | 77 | See Config for details. |
80 | */ | 78 | */ |
81 | 79 | ||
82 | /*! | 80 | /*! |
83 | Constructs a config that will load or create a configuration with the | 81 | Constructs a config that will load or create a configuration with the |
84 | given \a name in the given \a domain. | 82 | given \a name in the given \a domain. |
85 | 83 | ||
86 | You must call setGroup() before doing much else with the Config. | 84 | You must call setGroup() before doing much else with the Config. |
87 | 85 | ||
88 | In the default Domain, \e User, | 86 | In the default Domain, \e User, |
89 | the configuration is user-specific. \a name should not contain "/" in | 87 | the configuration is user-specific. \a name should not contain "/" in |
90 | this case, and in general should be the name of the C++ class that is | 88 | this case, and in general should be the name of the C++ class that is |
91 | primarily responsible for maintaining the configuration. | 89 | primarily responsible for maintaining the configuration. |
92 | 90 | ||
93 | In the File Domain, \a name is an absolute filename. | 91 | In the File Domain, \a name is an absolute filename. |
94 | */ | 92 | */ |
95 | Config::Config( const QString &name, Domain domain ) | 93 | Config::Config( const QString &name, Domain domain ) |
96 | : filename( configFilename(name,domain) ) | 94 | : filename( configFilename(name,domain) ) |
97 | { | 95 | { |
98 | git = groups.end(); | 96 | git = groups.end(); |
99 | read(); | 97 | read(); |
100 | QStringList l = Global::languageList(); | 98 | QStringList l = Global::languageList(); |
101 | lang = l[0]; | 99 | lang = l[0]; |
102 | glang = l[1]; | 100 | glang = l[1]; |
103 | } | 101 | } |
104 | 102 | ||
105 | 103 | ||
106 | // Sharp ROM compatibility | 104 | // Sharp ROM compatibility |
107 | Config::Config ( const QString &name, bool what ) | 105 | Config::Config ( const QString &name, bool what ) |
108 | : filename( configFilename(name,what ? User : File) ) | 106 | : filename( configFilename(name,what ? User : File) ) |
109 | { | 107 | { |
110 | git = groups.end(); | 108 | git = groups.end(); |
111 | read(); | 109 | read(); |
112 | QStringList l = Global::languageList(); | 110 | QStringList l = Global::languageList(); |
113 | lang = l[0]; | 111 | lang = l[0]; |
114 | glang = l[1]; | 112 | glang = l[1]; |
115 | } | 113 | } |
116 | 114 | ||
117 | /*! | 115 | /*! |
118 | Writes any changes to disk and destroys the in-memory object. | 116 | Writes any changes to disk and destroys the in-memory object. |
119 | */ | 117 | */ |
120 | Config::~Config() | 118 | Config::~Config() |
121 | { | 119 | { |
122 | if ( changed ) | 120 | if ( changed ) |
123 | write(); | 121 | write(); |
124 | } | 122 | } |
125 | 123 | ||
126 | /*! | 124 | /*! |
127 | Returns whether the current group has an entry called \a key. | 125 | Returns whether the current group has an entry called \a key. |
128 | */ | 126 | */ |
129 | bool Config::hasKey( const QString &key ) const | 127 | bool Config::hasKey( const QString &key ) const |
130 | { | 128 | { |
131 | if ( groups.end() == git ) | 129 | if ( groups.end() == git ) |
132 | return FALSE; | 130 | return FALSE; |
133 | ConfigGroup::ConstIterator it = ( *git ).find( key ); | 131 | ConfigGroup::ConstIterator it = ( *git ).find( key ); |
134 | return it != ( *git ).end(); | 132 | return it != ( *git ).end(); |
135 | } | 133 | } |
136 | 134 | ||
137 | /*! | 135 | /*! |
138 | Sets the current group for subsequent reading and writing of | 136 | Sets the current group for subsequent reading and writing of |
139 | entries to \a gname. Grouping allows the application to partition the namespace. | 137 | entries to \a gname. Grouping allows the application to partition the namespace. |
140 | 138 | ||
141 | This function must be called prior to any reading or writing | 139 | This function must be called prior to any reading or writing |
142 | of entries. | 140 | of entries. |
143 | 141 | ||
144 | The \a gname must not be empty. | 142 | The \a gname must not be empty. |
145 | */ | 143 | */ |
146 | void Config::setGroup( const QString &gname ) | 144 | void Config::setGroup( const QString &gname ) |
147 | { | 145 | { |
148 | QMap< QString, ConfigGroup>::Iterator it = groups.find( gname ); | 146 | QMap< QString, ConfigGroup>::Iterator it = groups.find( gname ); |
149 | if ( it == groups.end() ) { | 147 | if ( it == groups.end() ) { |
150 | git = groups.insert( gname, ConfigGroup() ); | 148 | git = groups.insert( gname, ConfigGroup() ); |
151 | changed = TRUE; | 149 | changed = TRUE; |
152 | return; | 150 | return; |
153 | } | 151 | } |
154 | git = it; | 152 | git = it; |
155 | } | 153 | } |
156 | 154 | ||
157 | /*! | 155 | /*! |
158 | Writes a (\a key, \a value) entry to the current group. | 156 | Writes a (\a key, \a value) entry to the current group. |
159 | 157 | ||
160 | \sa readEntry() | 158 | \sa readEntry() |
161 | */ | 159 | */ |
162 | void Config::writeEntry( const QString &key, const char* value ) | 160 | void Config::writeEntry( const QString &key, const char* value ) |
163 | { | 161 | { |
164 | writeEntry(key,QString(value)); | 162 | writeEntry(key,QString(value)); |
165 | } | 163 | } |
166 | 164 | ||
167 | /*! | 165 | /*! |
168 | Writes a (\a key, \a value) entry to the current group. | 166 | Writes a (\a key, \a value) entry to the current group. |
169 | 167 | ||
170 | \sa readEntry() | 168 | \sa readEntry() |
171 | */ | 169 | */ |
172 | void Config::writeEntry( const QString &key, const QString &value ) | 170 | void Config::writeEntry( const QString &key, const QString &value ) |
173 | { | 171 | { |
174 | if ( git == groups.end() ) { | 172 | if ( git == groups.end() ) { |
175 | qWarning( "no group set" ); | 173 | qWarning( "no group set" ); |
176 | return; | 174 | return; |
177 | } | 175 | } |
178 | if ( (*git)[key] != value ) { | 176 | if ( (*git)[key] != value ) { |
179 | ( *git ).insert( key, value ); | 177 | ( *git ).insert( key, value ); |
180 | changed = TRUE; | 178 | changed = TRUE; |
181 | } | 179 | } |
182 | } | 180 | } |
183 | 181 | ||
184 | /* | 182 | /* |
185 | Note that the degree of protection offered by the encryption here is | 183 | Note that the degree of protection offered by the encryption here is |
186 | only sufficient to avoid the most casual observation of the configuration | 184 | only sufficient to avoid the most casual observation of the configuration |
187 | files. People with access to the files can write down the contents and | 185 | files. People with access to the files can write down the contents and |
188 | decrypt it using this source code. | 186 | decrypt it using this source code. |
189 | 187 | ||
190 | Conceivably, and at some burden to the user, this encryption could | 188 | Conceivably, and at some burden to the user, this encryption could |
191 | be improved. | 189 | be improved. |
192 | */ | 190 | */ |
193 | static QString encipher(const QString& plain) | 191 | static QString encipher(const QString& plain) |
194 | { | 192 | { |
195 | // mainly, we make it long | 193 | // mainly, we make it long |
196 | QString cipher; | 194 | QString cipher; |
197 | int mix=28730492; | 195 | int mix=28730492; |
198 | for (int i=0; i<(int)plain.length(); i++) { | 196 | for (int i=0; i<(int)plain.length(); i++) { |
199 | int u = plain[i].unicode(); | 197 | int u = plain[i].unicode(); |
200 | int c = u ^ mix; | 198 | int c = u ^ mix; |
201 | QString x = QString::number(c,36); | 199 | QString x = QString::number(c,36); |
202 | cipher.append(QChar('a'+x.length())); | 200 | cipher.append(QChar('a'+x.length())); |
203 | cipher.append(x); | 201 | cipher.append(x); |
204 | mix *= u; | 202 | mix *= u; |
205 | } | 203 | } |
206 | return cipher; | 204 | return cipher; |
207 | } | 205 | } |
208 | 206 | ||
209 | static QString decipher(const QString& cipher) | 207 | static QString decipher(const QString& cipher) |
210 | { | 208 | { |
211 | QString plain; | 209 | QString plain; |
212 | int mix=28730492; | 210 | int mix=28730492; |
213 | for (int i=0; i<(int)cipher.length();) { | 211 | for (int i=0; i<(int)cipher.length();) { |
214 | int l = cipher[i].unicode()-'a'; | 212 | int l = cipher[i].unicode()-'a'; |
215 | QString x = cipher.mid(i+1,l); i+=l+1; | 213 | QString x = cipher.mid(i+1,l); i+=l+1; |