root / kadu-core / configuration / configuration-file.cpp @ cec880ad
History | View | Annotate | Download (22.4 kB)
| 1 | /***************************************************************************
|
|---|---|
| 2 | * * |
| 3 | * This program is free software; you can redistribute it and/or modify * |
| 4 | * it under the terms of the GNU General Public License as published by * |
| 5 | * the Free Software Foundation; either version 2 of the License, or * |
| 6 | * (at your option) any later version. * |
| 7 | * * |
| 8 | ***************************************************************************/ |
| 9 | |
| 10 | #include <QtCore/QDateTime> |
| 11 | #include <QtCore/QDir> |
| 12 | #include <QtCore/QFile> |
| 13 | #include <QtCore/QMutex> |
| 14 | #include <QtCore/QTextStream> |
| 15 | #include <QtGui/QApplication> |
| 16 | |
| 17 | #include <errno.h> |
| 18 | |
| 19 | #include "configuration/xml-configuration-file.h" |
| 20 | #include "misc/misc.h" |
| 21 | |
| 22 | #include "debug.h" |
| 23 | |
| 24 | #include "configuration-file.h" |
| 25 | |
| 26 | QMutex GlobalMutex; |
| 27 | |
| 28 | PlainConfigFile::PlainConfigFile(const QString &filename) : filename(filename), groups(), activeGroupName(), activeGroup(0) |
| 29 | {
|
| 30 | read(); |
| 31 | } |
| 32 | |
| 33 | PlainConfigFile::PlainConfigFile(const PlainConfigFile &c) : filename(c.filename), groups(c.groups), activeGroupName(), activeGroup(0) |
| 34 | {
|
| 35 | } |
| 36 | |
| 37 | PlainConfigFile &PlainConfigFile::operator=(const PlainConfigFile &c) |
| 38 | {
|
| 39 | filename = c.filename; |
| 40 | groups = c.groups; |
| 41 | activeGroupName = QString::null; |
| 42 | activeGroup= 0;
|
| 43 | return *this; |
| 44 | } |
| 45 | |
| 46 | void PlainConfigFile::read()
|
| 47 | {
|
| 48 | kdebugmf(KDEBUG_FUNCTION_START, "%s\n", qPrintable(filename));
|
| 49 | QFile file(filename); |
| 50 | QString line; |
| 51 | |
| 52 | if (file.open(QIODevice::ReadOnly))
|
| 53 | {
|
| 54 | QTextStream stream(&file); |
| 55 | stream.setCodec(codec_latin2); |
| 56 | while (!stream.atEnd())
|
| 57 | {
|
| 58 | line = stream.readLine().trimmed(); |
| 59 | if (line.startsWith("[") && line.endsWith("]")) |
| 60 | {
|
| 61 | QString name=line.mid(1, line.length() - 2).trimmed(); |
| 62 | if (activeGroupName!=name)
|
| 63 | {
|
| 64 | activeGroupName=name; |
| 65 | activeGroup=&groups[name]; |
| 66 | } |
| 67 | } |
| 68 | else if (activeGroupName.length()) |
| 69 | {
|
| 70 | QString name = line.section('=', 0, 0); |
| 71 | QString value = line.right(line.length()-name.length()-1).replace("\\n", "\n"); |
| 72 | name = name.trimmed(); |
| 73 | |
| 74 | if (line.contains('=') && !name.isEmpty() && !value.isEmpty()) |
| 75 | (*activeGroup)[name]=value; |
| 76 | } |
| 77 | } |
| 78 | file.close(); |
| 79 | } |
| 80 | kdebugf2(); |
| 81 | } |
| 82 | |
| 83 | //#include <sys/time.h>
|
| 84 | void PlainConfigFile::write() const |
| 85 | {
|
| 86 | kdebugf(); |
| 87 | |
| 88 | /* struct timeval t1,t2;
|
| 89 | gettimeofday(&t1, NULL); |
| 90 | for(int j=0; j<100; ++j) |
| 91 | {*/
|
| 92 | |
| 93 | QFile file(filename); |
| 94 | QString line; |
| 95 | QStringList out; |
| 96 | QString format1("[%1]\n");
|
| 97 | QString format2("%1=%2\n");
|
| 98 | |
| 99 | if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
|
| 100 | {
|
| 101 | kdebugm(KDEBUG_INFO, "file opened '%s'\n", qPrintable(file.fileName()));
|
| 102 | QTextStream stream(&file); |
| 103 | stream.setCodec(codec_latin2); |
| 104 | foreach(const QString &key, groups.keys())
|
| 105 | {
|
| 106 | // kdebugm(KDEBUG_DUMP, ">> %s\n", (i.key()));
|
| 107 | out.append(format1.arg(key)); |
| 108 | foreach(const QString &dataKey, groups[key].keys())
|
| 109 | {
|
| 110 | QString q = groups[key][dataKey]; |
| 111 | out.append(format2.arg(dataKey).arg(q.replace('\n', "\\n"))); |
| 112 | // kdebugm(KDEBUG_DUMP, ">>>>> %s %s\n", qPrintable(key()), qPrintable(q));
|
| 113 | } |
| 114 | out.append("\n");
|
| 115 | } |
| 116 | stream << out.join(QString::null); |
| 117 | file.close(); |
| 118 | } |
| 119 | else
|
| 120 | {
|
| 121 | fprintf(stderr, "cannot open '%s': %s\n", qPrintable(file.fileName()), qPrintable(file.errorString()));
|
| 122 | fflush(stderr); |
| 123 | } |
| 124 | |
| 125 | /* }
|
| 126 | gettimeofday(&t2, NULL); |
| 127 | kdebugm(KDEBUG_INFO, "czas: %ld\n", (t2.tv_usec-t1.tv_usec)+(t2.tv_sec*1000000)-(t1.tv_sec*1000000)); |
| 128 | */ |
| 129 | kdebugf2(); |
| 130 | } |
| 131 | |
| 132 | QStringList PlainConfigFile::getGroupList() const
|
| 133 | {
|
| 134 | return QStringList(groups.keys());
|
| 135 | } |
| 136 | |
| 137 | void PlainConfigFile::sync() const |
| 138 | {
|
| 139 | write(); |
| 140 | } |
| 141 | |
| 142 | QMap<QString, QString>& PlainConfigFile::getGroupSection(const QString& name)
|
| 143 | {
|
| 144 | kdebugf(); |
| 145 | return groups[name];
|
| 146 | } |
| 147 | |
| 148 | bool PlainConfigFile::changeEntry(const QString &group, const QString &name, const QString &value) |
| 149 | {
|
| 150 | // kdebugm(KDEBUG_FUNCTION_START, "PlainConfigFile::changeEntry(%s, %s, %s) %p\n", qPrintable(group), qPrintable(name), qPrintable(value), this);
|
| 151 | if (activeGroupName!=group)
|
| 152 | {
|
| 153 | activeGroupName=group; |
| 154 | activeGroup=&(groups[group]); |
| 155 | } |
| 156 | bool ret=activeGroup->contains(name);
|
| 157 | (*activeGroup)[name]=value; |
| 158 | //
|
| 159 | return ret;
|
| 160 | } |
| 161 | |
| 162 | QString PlainConfigFile::getEntry(const QString &group, const QString &name, bool *ok) const |
| 163 | {
|
| 164 | // kdebugm(KDEBUG_FUNCTION_START, "PlainConfigFile::getEntry(%s, %s) %p\n", qPrintable(group), qPrintable(name), this);
|
| 165 | if (activeGroupName!=group)
|
| 166 | {
|
| 167 | if (!groups.contains(group))
|
| 168 | {
|
| 169 | if (ok)
|
| 170 | *ok=false;
|
| 171 | return QString::null;
|
| 172 | } |
| 173 | activeGroupName=group; |
| 174 | activeGroup=&((QMap<QString, QString>)groups[group]); |
| 175 | } |
| 176 | if (ok)
|
| 177 | *ok=activeGroup->contains(name); |
| 178 | if (activeGroup->contains(name))
|
| 179 | return (*activeGroup)[name];
|
| 180 | else
|
| 181 | return QString::null;
|
| 182 | } |
| 183 | |
| 184 | void PlainConfigFile::writeEntry(const QString &group, const QString &name, const QVariant &value) |
| 185 | {
|
| 186 | changeEntry(group, name, value.toString()); |
| 187 | } |
| 188 | |
| 189 | void PlainConfigFile::writeEntry(const QString &group,const QString &name, const QString &value) |
| 190 | {
|
| 191 | changeEntry(group, name, value); |
| 192 | } |
| 193 | |
| 194 | void PlainConfigFile::writeEntry(const QString &group,const QString &name, const char *value) |
| 195 | {
|
| 196 | changeEntry(group, name, QString::fromLocal8Bit(value)); |
| 197 | } |
| 198 | |
| 199 | void PlainConfigFile::writeEntry(const QString &group,const QString &name, const int value) |
| 200 | {
|
| 201 | changeEntry(group, name, QString::number(value)); |
| 202 | } |
| 203 | |
| 204 | void PlainConfigFile::writeEntry(const QString &group,const QString &name, const double value) |
| 205 | {
|
| 206 | changeEntry(group, name, QString::number(value, 'f'));
|
| 207 | } |
| 208 | |
| 209 | void PlainConfigFile::writeEntry(const QString &group,const QString &name, const bool value) |
| 210 | {
|
| 211 | changeEntry(group, name, value ? "true" : "false"); |
| 212 | } |
| 213 | |
| 214 | void PlainConfigFile::writeEntry(const QString &group,const QString &name, const QRect &value) |
| 215 | {
|
| 216 | changeEntry(group, name, QString("%1,%2,%3,%4").arg(value.left()).arg(value.top()).
|
| 217 | arg(value.width()).arg(value.height())); |
| 218 | } |
| 219 | |
| 220 | void PlainConfigFile::writeEntry(const QString &group,const QString &name, const QSize &value) |
| 221 | {
|
| 222 | changeEntry(group, name, QString("%1,%2").arg(value.width()).arg(value.height()));
|
| 223 | } |
| 224 | |
| 225 | void PlainConfigFile::writeEntry(const QString &group,const QString &name, const QColor &value) |
| 226 | {
|
| 227 | changeEntry(group, name, value.name()); |
| 228 | } |
| 229 | |
| 230 | void PlainConfigFile::writeEntry(const QString &group,const QString &name, const QFont &value) |
| 231 | {
|
| 232 | changeEntry(group, name, value.family() + ',' + QString::number(value.pointSize()));
|
| 233 | } |
| 234 | |
| 235 | void PlainConfigFile::writeEntry(const QString &group,const QString &name, const QPoint &value) |
| 236 | {
|
| 237 | changeEntry(group, name, QString("%1,%2").arg(value.x()).arg(value.y()));
|
| 238 | } |
| 239 | |
| 240 | template <class T> |
| 241 | T PlainConfigFile::readEntry(const QString &group, const QString &name, const T &def ) const |
| 242 | {
|
| 243 | QVariant string = qVariantFromValue( getEntry(group, name));
|
| 244 | if (string.canConvert<T>()) |
| 245 | return string.value<T>(); |
| 246 | return def;
|
| 247 | } |
| 248 | |
| 249 | QString PlainConfigFile::readEntry(const QString &group,const QString &name, const QString &def) const |
| 250 | {
|
| 251 | QString string = getEntry(group, name);
|
| 252 | if (string == QString::null) |
| 253 | return def;
|
| 254 | return string; |
| 255 | } |
| 256 | |
| 257 | unsigned int PlainConfigFile::readUnsignedNumEntry(const QString &group,const QString &name, unsigned int def) const |
| 258 | {
|
| 259 | bool ok;
|
| 260 | QString string = getEntry(group, name);
|
| 261 | if (string == QString::null) |
| 262 | return def;
|
| 263 | unsigned int num = string.toUInt(&ok); |
| 264 | if (!ok)
|
| 265 | return def;
|
| 266 | return num;
|
| 267 | } |
| 268 | |
| 269 | int PlainConfigFile::readNumEntry(const QString &group,const QString &name, int def) const |
| 270 | {
|
| 271 | bool ok;
|
| 272 | QString string = getEntry(group, name);
|
| 273 | if (string == QString::null) |
| 274 | return def;
|
| 275 | int num = string.toInt(&ok); |
| 276 | if (!ok)
|
| 277 | return def;
|
| 278 | return num;
|
| 279 | } |
| 280 | |
| 281 | double PlainConfigFile::readDoubleNumEntry(const QString &group,const QString &name, double def) const |
| 282 | {
|
| 283 | bool ok;
|
| 284 | QString string = getEntry(group, name);
|
| 285 | if (string == QString::null) |
| 286 | return def;
|
| 287 | double num = string.toDouble(&ok); |
| 288 | if (!ok)
|
| 289 | return def;
|
| 290 | return num;
|
| 291 | } |
| 292 | |
| 293 | bool PlainConfigFile::readBoolEntry(const QString &group,const QString &name, bool def) const |
| 294 | {
|
| 295 | QString string = getEntry(group, name);
|
| 296 | if (string == QString::null) |
| 297 | return def;
|
| 298 | return string=="true"; |
| 299 | } |
| 300 | |
| 301 | QRect PlainConfigFile::readRectEntry(const QString &group,const QString &name, const QRect *def) const |
| 302 | {
|
| 303 | QString string = getEntry(group, name);
|
| 304 | |
| 305 | if (string == QString::null) |
| 306 | return def ? *def : QRect(0, 0, 0, 0); |
| 307 | |
| 308 | return stringToRect(string, def); |
| 309 | } |
| 310 | |
| 311 | QSize PlainConfigFile::readSizeEntry(const QString &group,const QString &name, const QSize *def) const |
| 312 | {
|
| 313 | QString string = getEntry(group, name);
|
| 314 | QStringList stringlist; |
| 315 | QSize size(0,0); |
| 316 | int w, h;
|
| 317 | bool ok;
|
| 318 | |
| 319 | if (string == QString::null) |
| 320 | return def ? *def : size;
|
| 321 | stringlist = string.split(',', QString::SkipEmptyParts); |
| 322 | if (stringlist.count() != 2) |
| 323 | return def ? *def : size;
|
| 324 | w = stringlist[0].toInt(&ok); if (!ok) return def ? *def : size; |
| 325 | h = stringlist[1].toInt(&ok); if (!ok) return def ? *def : size; |
| 326 | size.setWidth(w); |
| 327 | size.setHeight(h); |
| 328 | return size;
|
| 329 | } |
| 330 | |
| 331 | QColor PlainConfigFile::readColorEntry(const QString &group,const QString &name, const QColor *def) const |
| 332 | {
|
| 333 | QColor col(0,0,0); |
| 334 | QString str = getEntry(group, name); |
| 335 | if (str==QString::null)
|
| 336 | return def ? *def : col;
|
| 337 | else
|
| 338 | {
|
| 339 | if (!str.contains(',')) |
| 340 | return QColor(str);
|
| 341 | |
| 342 | //stary zapis kolor�w, w 0.5.0 mo�na b�dzie wywali�
|
| 343 | bool ok;
|
| 344 | QStringList stringlist = str.split(',', QString::SkipEmptyParts);
|
| 345 | if (stringlist.count() != 3) |
| 346 | return def ? *def : col;
|
| 347 | int r = stringlist[0].toInt(&ok); if (!ok) return def ? *def : col; |
| 348 | int g = stringlist[1].toInt(&ok); if (!ok) return def ? *def : col; |
| 349 | int b = stringlist[2].toInt(&ok); if (!ok) return def ? *def : col; |
| 350 | col.setRgb(r, g, b); |
| 351 | return col;
|
| 352 | } |
| 353 | } |
| 354 | |
| 355 | |
| 356 | QFont PlainConfigFile::readFontEntry(const QString &group,const QString &name, const QFont *def) const |
| 357 | {
|
| 358 | QString string = getEntry(group, name);
|
| 359 | QStringList stringlist; |
| 360 | QFont font; |
| 361 | bool ok;
|
| 362 | |
| 363 | if (string == QString::null) |
| 364 | return def ? *def : QApplication::font();
|
| 365 | stringlist = string.split(',', QString::SkipEmptyParts); |
| 366 | if (stringlist.count() < 2) |
| 367 | return def ? *def : QApplication::font();
|
| 368 | font.setFamily(stringlist[0]);
|
| 369 | font.setPointSize(stringlist[1].toInt(&ok));
|
| 370 | if (!ok)
|
| 371 | return def ? *def : QApplication::font();
|
| 372 | return font;
|
| 373 | } |
| 374 | |
| 375 | void PlainConfigFile::removeVariable(const QString &group, const QString &name) |
| 376 | {
|
| 377 | if (activeGroupName != group)
|
| 378 | {
|
| 379 | activeGroupName = group; |
| 380 | activeGroup= &(groups[group]); |
| 381 | } |
| 382 | |
| 383 | if (activeGroup->contains(name))
|
| 384 | activeGroup->remove(name); |
| 385 | } |
| 386 | |
| 387 | QPoint PlainConfigFile::readPointEntry(const QString &group,const QString &name, const QPoint *def) const |
| 388 | {
|
| 389 | QString string = getEntry(group, name);
|
| 390 | QStringList stringlist; |
| 391 | QPoint point(0,0); |
| 392 | int x, y;
|
| 393 | bool ok;
|
| 394 | |
| 395 | if (string == QString::null) |
| 396 | return def ? *def : point;
|
| 397 | stringlist = string.split(',', QString::SkipEmptyParts); |
| 398 | if (stringlist.count() != 2) |
| 399 | return def ? *def : point;
|
| 400 | x = stringlist[0].toInt(&ok); if (!ok) return def ? *def : point; |
| 401 | y = stringlist[1].toInt(&ok); if (!ok) return def ? *def : point; |
| 402 | point.setX(x); |
| 403 | point.setY(y); |
| 404 | return point;
|
| 405 | } |
| 406 | |
| 407 | void PlainConfigFile::addVariable(const QString &group, const QString &name, const QString &defvalue) |
| 408 | {
|
| 409 | if (getEntry(group, name).isEmpty())
|
| 410 | writeEntry(group,name,defvalue); |
| 411 | } |
| 412 | void PlainConfigFile::addVariable(const QString &group, const QString &name, const char *defvalue) |
| 413 | {
|
| 414 | if (getEntry(group, name).isEmpty())
|
| 415 | writeEntry(group,name,defvalue); |
| 416 | } |
| 417 | void PlainConfigFile::addVariable(const QString &group, const QString &name, const int defvalue) |
| 418 | {
|
| 419 | if (getEntry(group, name).isEmpty())
|
| 420 | writeEntry(group,name,defvalue); |
| 421 | } |
| 422 | void PlainConfigFile::addVariable(const QString &group, const QString &name, const double defvalue) |
| 423 | {
|
| 424 | if (getEntry(group, name).isEmpty())
|
| 425 | writeEntry(group,name,defvalue); |
| 426 | } |
| 427 | void PlainConfigFile::addVariable(const QString &group, const QString &name, const bool defvalue) |
| 428 | {
|
| 429 | if (getEntry(group, name).isEmpty())
|
| 430 | writeEntry(group,name,defvalue); |
| 431 | } |
| 432 | void PlainConfigFile::addVariable(const QString &group, const QString &name, const QRect &defvalue) |
| 433 | {
|
| 434 | if (getEntry(group, name).isEmpty())
|
| 435 | writeEntry(group,name,defvalue); |
| 436 | } |
| 437 | void PlainConfigFile::addVariable(const QString &group, const QString &name, const QSize &defvalue) |
| 438 | {
|
| 439 | if (getEntry(group, name).isEmpty())
|
| 440 | writeEntry(group,name,defvalue); |
| 441 | } |
| 442 | void PlainConfigFile::addVariable(const QString &group, const QString &name, const QColor &defvalue) |
| 443 | {
|
| 444 | if (getEntry(group, name).isEmpty())
|
| 445 | writeEntry(group,name,defvalue); |
| 446 | } |
| 447 | void PlainConfigFile::addVariable(const QString &group, const QString &name, const QFont &defvalue) |
| 448 | {
|
| 449 | if (getEntry(group, name).isEmpty())
|
| 450 | writeEntry(group,name,defvalue); |
| 451 | } |
| 452 | void PlainConfigFile::addVariable(const QString &group, const QString &name, const QPoint &defvalue) |
| 453 | {
|
| 454 | if (getEntry(group, name).isEmpty())
|
| 455 | writeEntry(group,name,defvalue); |
| 456 | } |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | ConfigFile::ConfigFile(const QString &filename) : filename(filename)
|
| 462 | {
|
| 463 | } |
| 464 | |
| 465 | void ConfigFile::sync() const |
| 466 | {
|
| 467 | xml_config_file->sync(); |
| 468 | } |
| 469 | |
| 470 | bool ConfigFile::changeEntry(const QString &group, const QString &name, const QString &value) |
| 471 | {
|
| 472 | GlobalMutex.lock(); |
| 473 | |
| 474 | // kdebugm(KDEBUG_FUNCTION_START, "ConfigFile::changeEntry(%s, %s, %s) %p\n", qPrintable(group), qPrintable(name), qPrintable(value), this);
|
| 475 | QDomElement root_elem = xml_config_file->rootElement(); |
| 476 | QDomElement deprecated_elem = xml_config_file->accessElement(root_elem, "Deprecated");
|
| 477 | QDomElement config_file_elem = xml_config_file->accessElementByProperty( |
| 478 | deprecated_elem, "ConfigFile", "name", filename.section("/", -1)); |
| 479 | QDomElement group_elem = xml_config_file->accessElementByProperty( |
| 480 | config_file_elem, "Group", "name", group); |
| 481 | QDomElement entry_elem = xml_config_file->accessElementByProperty( |
| 482 | group_elem, "Entry", "name", name); |
| 483 | entry_elem.setAttribute("value", value);
|
| 484 | |
| 485 | GlobalMutex.unlock(); |
| 486 | |
| 487 | return true; |
| 488 | } |
| 489 | |
| 490 | QString ConfigFile::getEntry(const QString &group, const QString &name, bool *ok) const |
| 491 | {
|
| 492 | GlobalMutex.lock(); |
| 493 | |
| 494 | bool resOk;
|
| 495 | QString result = QString::null; |
| 496 | |
| 497 | // kdebugm(KDEBUG_FUNCTION_START, "ConfigFile::getEntry(%s, %s) %p\n", qPrintable(group), qPrintable(name), this);
|
| 498 | {
|
| 499 | QDomElement root_elem = xml_config_file->rootElement(); |
| 500 | QDomElement deprecated_elem = xml_config_file->findElement(root_elem, "Deprecated");
|
| 501 | if (!deprecated_elem.isNull())
|
| 502 | {
|
| 503 | QDomElement config_file_elem = xml_config_file->findElementByProperty( |
| 504 | deprecated_elem, "ConfigFile", "name", filename.section("/", -1)); |
| 505 | if (!config_file_elem.isNull())
|
| 506 | {
|
| 507 | QDomElement group_elem = xml_config_file->findElementByProperty( |
| 508 | config_file_elem, "Group", "name", group); |
| 509 | if (!group_elem.isNull())
|
| 510 | {
|
| 511 | QDomElement entry_elem = |
| 512 | xml_config_file->findElementByProperty( |
| 513 | group_elem, "Entry", "name", name); |
| 514 | if (!entry_elem.isNull())
|
| 515 | {
|
| 516 | resOk = true;
|
| 517 | result = entry_elem.attribute("value");
|
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | resOk = false;
|
| 525 | if (ok)
|
| 526 | *ok = resOk; |
| 527 | |
| 528 | GlobalMutex.unlock(); |
| 529 | return result;
|
| 530 | } |
| 531 | |
| 532 | void ConfigFile::writeEntry(const QString &group,const QString &name, const QString &value) |
| 533 | {
|
| 534 | changeEntry(group, name, value); |
| 535 | } |
| 536 | |
| 537 | void ConfigFile::writeEntry(const QString &group,const QString &name, const char *value) |
| 538 | {
|
| 539 | changeEntry(group, name, QString::fromLocal8Bit(value)); |
| 540 | } |
| 541 | |
| 542 | void ConfigFile::writeEntry(const QString &group,const QString &name, const int value) |
| 543 | {
|
| 544 | changeEntry(group, name, QString::number(value)); |
| 545 | } |
| 546 | |
| 547 | void ConfigFile::writeEntry(const QString &group,const QString &name, const double value) |
| 548 | {
|
| 549 | changeEntry(group, name, QString::number(value, 'f'));
|
| 550 | } |
| 551 | |
| 552 | void ConfigFile::writeEntry(const QString &group,const QString &name, const bool value) |
| 553 | {
|
| 554 | changeEntry(group, name, value ? "true" : "false"); |
| 555 | } |
| 556 | |
| 557 | void ConfigFile::writeEntry(const QString &group,const QString &name, const QRect &value) |
| 558 | {
|
| 559 | changeEntry(group, name, rectToString(value)); |
| 560 | } |
| 561 | |
| 562 | void ConfigFile::writeEntry(const QString &group,const QString &name, const QSize &value) |
| 563 | {
|
| 564 | changeEntry(group, name, QString("%1,%2").arg(value.width()).arg(value.height()));
|
| 565 | } |
| 566 | |
| 567 | void ConfigFile::writeEntry(const QString &group,const QString &name, const QColor &value) |
| 568 | {
|
| 569 | changeEntry(group, name, value.name()); |
| 570 | } |
| 571 | |
| 572 | void ConfigFile::writeEntry(const QString &group,const QString &name, const QFont &value) |
| 573 | {
|
| 574 | changeEntry(group, name, value.toString()); |
| 575 | } |
| 576 | |
| 577 | void ConfigFile::writeEntry(const QString &group,const QString &name, const QPoint &value) |
| 578 | {
|
| 579 | changeEntry(group, name, QString("%1,%2").arg(value.x()).arg(value.y()));
|
| 580 | } |
| 581 | |
| 582 | QString ConfigFile::readEntry(const QString &group,const QString &name, const QString &def) const |
| 583 | {
|
| 584 | QString string = getEntry(group, name);
|
| 585 | if (string == QString::null) |
| 586 | return def;
|
| 587 | return string; |
| 588 | } |
| 589 | |
| 590 | unsigned int ConfigFile::readUnsignedNumEntry(const QString &group,const QString &name, unsigned int def) const |
| 591 | {
|
| 592 | bool ok;
|
| 593 | QString string = getEntry(group, name);
|
| 594 | if (string == QString::null) |
| 595 | return def;
|
| 596 | unsigned int num = string.toUInt(&ok); |
| 597 | if (!ok)
|
| 598 | return def;
|
| 599 | return num;
|
| 600 | } |
| 601 | |
| 602 | int ConfigFile::readNumEntry(const QString &group,const QString &name, int def) const |
| 603 | {
|
| 604 | bool ok;
|
| 605 | QString string = getEntry(group, name);
|
| 606 | if (string == QString::null) |
| 607 | return def;
|
| 608 | int num = string.toInt(&ok); |
| 609 | if (!ok)
|
| 610 | return def;
|
| 611 | return num;
|
| 612 | } |
| 613 | |
| 614 | double ConfigFile::readDoubleNumEntry(const QString &group,const QString &name, double def) const |
| 615 | {
|
| 616 | bool ok;
|
| 617 | QString string = getEntry(group, name);
|
| 618 | if (string == QString::null) |
| 619 | return def;
|
| 620 | double num = string.toDouble(&ok); |
| 621 | if (!ok)
|
| 622 | return def;
|
| 623 | return num;
|
| 624 | } |
| 625 | |
| 626 | bool ConfigFile::readBoolEntry(const QString &group,const QString &name, bool def) const |
| 627 | {
|
| 628 | QString string = getEntry(group, name);
|
| 629 | if (string == QString::null) |
| 630 | return def;
|
| 631 | return string=="true"; |
| 632 | } |
| 633 | |
| 634 | QRect ConfigFile::readRectEntry(const QString &group,const QString &name, const QRect *def) const |
| 635 | {
|
| 636 | QString string = getEntry(group, name);
|
| 637 | QStringList stringlist; |
| 638 | QRect rect(0,0,0,0); |
| 639 | int l, t, w, h;
|
| 640 | bool ok;
|
| 641 | |
| 642 | if (string == QString::null) |
| 643 | return def ? *def : rect;
|
| 644 | stringlist = string.split(',', QString::SkipEmptyParts); |
| 645 | if (stringlist.count() != 4) |
| 646 | return def ? *def : rect;
|
| 647 | l = stringlist[0].toInt(&ok); if (!ok) return def ? *def : rect; |
| 648 | t = stringlist[1].toInt(&ok); if (!ok) return def ? *def : rect; |
| 649 | w = stringlist[2].toInt(&ok); if (!ok) return def ? *def : rect; |
| 650 | h = stringlist[3].toInt(&ok); if (!ok) return def ? *def : rect; |
| 651 | rect.setRect(l, t, w, h); |
| 652 | return rect;
|
| 653 | } |
| 654 | |
| 655 | QSize ConfigFile::readSizeEntry(const QString &group,const QString &name, const QSize *def) const |
| 656 | {
|
| 657 | QString string = getEntry(group, name);
|
| 658 | QStringList stringlist; |
| 659 | QSize size(0,0); |
| 660 | int w, h;
|
| 661 | bool ok;
|
| 662 | |
| 663 | if (string == QString::null) |
| 664 | return def ? *def : size;
|
| 665 | stringlist = string.split(',', QString::SkipEmptyParts); |
| 666 | if (stringlist.count() != 2) |
| 667 | return def ? *def : size;
|
| 668 | w = stringlist[0].toInt(&ok); if (!ok) return def ? *def : size; |
| 669 | h = stringlist[1].toInt(&ok); if (!ok) return def ? *def : size; |
| 670 | size.setWidth(w); |
| 671 | size.setHeight(h); |
| 672 | return size;
|
| 673 | } |
| 674 | |
| 675 | QColor ConfigFile::readColorEntry(const QString &group,const QString &name, const QColor *def) const |
| 676 | {
|
| 677 | QColor col(0,0,0); |
| 678 | QString str = getEntry(group, name); |
| 679 | if (str==QString::null)
|
| 680 | return def ? *def : col;
|
| 681 | else
|
| 682 | {
|
| 683 | if (!str.contains(',')) |
| 684 | return QColor(str);
|
| 685 | |
| 686 | //stary zapis kolor�w, w 0.5.0 mo�na b�dzie wywali�
|
| 687 | bool ok;
|
| 688 | QStringList stringlist = str.split(',', QString::SkipEmptyParts);
|
| 689 | if (stringlist.count() != 3) |
| 690 | return def ? *def : col;
|
| 691 | int r = stringlist[0].toInt(&ok); if (!ok) return def ? *def : col; |
| 692 | int g = stringlist[1].toInt(&ok); if (!ok) return def ? *def : col; |
| 693 | int b = stringlist[2].toInt(&ok); if (!ok) return def ? *def : col; |
| 694 | col.setRgb(r, g, b); |
| 695 | return col;
|
| 696 | } |
| 697 | } |
| 698 | |
| 699 | |
| 700 | QFont ConfigFile::readFontEntry(const QString &group,const QString &name, const QFont *def) const |
| 701 | {
|
| 702 | QString string = getEntry(group, name);
|
| 703 | if (string == QString::null) |
| 704 | return def ? *def : QApplication::font();
|
| 705 | QFont font; |
| 706 | if(font.fromString(string)) |
| 707 | return font;
|
| 708 | return def ? *def : QApplication::font();
|
| 709 | } |
| 710 | |
| 711 | QPoint ConfigFile::readPointEntry(const QString &group,const QString &name, const QPoint *def) const |
| 712 | {
|
| 713 | QString string = getEntry(group, name);
|
| 714 | QStringList stringlist; |
| 715 | QPoint point(0,0); |
| 716 | int x, y;
|
| 717 | bool ok;
|
| 718 | |
| 719 | if (string == QString::null) |
| 720 | return def ? *def : point;
|
| 721 | stringlist = string.split(',', QString::SkipEmptyParts); |
| 722 | if (stringlist.count() != 2) |
| 723 | return def ? *def : point;
|
| 724 | x = stringlist[0].toInt(&ok); if (!ok) return def ? *def : point; |
| 725 | y = stringlist[1].toInt(&ok); if (!ok) return def ? *def : point; |
| 726 | point.setX(x); |
| 727 | point.setY(y); |
| 728 | return point;
|
| 729 | } |
| 730 | |
| 731 | void ConfigFile::removeVariable(const QString &group, const QString &name) |
| 732 | {
|
| 733 | GlobalMutex.lock(); |
| 734 | |
| 735 | QDomElement root_elem = xml_config_file->rootElement(); |
| 736 | QDomElement deprecated_elem = xml_config_file->accessElement(root_elem, "Deprecated");
|
| 737 | QDomElement config_file_elem = xml_config_file->accessElementByProperty( |
| 738 | deprecated_elem, "ConfigFile", "name", filename.section("/", -1)); |
| 739 | QDomElement group_elem = xml_config_file->accessElementByProperty( |
| 740 | config_file_elem, "Group", "name", group); |
| 741 | QDomElement entry_elem = xml_config_file->accessElementByProperty( |
| 742 | group_elem, "Entry", "name", name); |
| 743 | group_elem.removeChild(entry_elem); |
| 744 | |
| 745 | GlobalMutex.unlock(); |
| 746 | } |
| 747 | |
| 748 | void ConfigFile::addVariable(const QString &group, const QString &name, const QString &defvalue) |
| 749 | {
|
| 750 | if (getEntry(group, name).isEmpty())
|
| 751 | writeEntry(group,name,defvalue); |
| 752 | } |
| 753 | void ConfigFile::addVariable(const QString &group, const QString &name, const char *defvalue) |
| 754 | {
|
| 755 | if (getEntry(group, name).isEmpty())
|
| 756 | writeEntry(group,name,defvalue); |
| 757 | } |
| 758 | void ConfigFile::addVariable(const QString &group, const QString &name, const int defvalue) |
| 759 | {
|
| 760 | if (getEntry(group, name).isEmpty())
|
| 761 | writeEntry(group,name,defvalue); |
| 762 | } |
| 763 | void ConfigFile::addVariable(const QString &group, const QString &name, const double defvalue) |
| 764 | {
|
| 765 | if (getEntry(group, name).isEmpty())
|
| 766 | writeEntry(group,name,defvalue); |
| 767 | } |
| 768 | void ConfigFile::addVariable(const QString &group, const QString &name, const bool defvalue) |
| 769 | {
|
| 770 | if (getEntry(group, name).isEmpty())
|
| 771 | writeEntry(group,name,defvalue); |
| 772 | } |
| 773 | void ConfigFile::addVariable(const QString &group, const QString &name, const QRect &defvalue) |
| 774 | {
|
| 775 | if (getEntry(group, name).isEmpty())
|
| 776 | writeEntry(group,name,defvalue); |
| 777 | } |
| 778 | void ConfigFile::addVariable(const QString &group, const QString &name, const QSize &defvalue) |
| 779 | {
|
| 780 | if (getEntry(group, name).isEmpty())
|
| 781 | writeEntry(group,name,defvalue); |
| 782 | } |
| 783 | void ConfigFile::addVariable(const QString &group, const QString &name, const QColor &defvalue) |
| 784 | {
|
| 785 | if (getEntry(group, name).isEmpty())
|
| 786 | writeEntry(group,name,defvalue); |
| 787 | } |
| 788 | void ConfigFile::addVariable(const QString &group, const QString &name, const QFont &defvalue) |
| 789 | {
|
| 790 | if (getEntry(group, name).isEmpty())
|
| 791 | writeEntry(group,name,defvalue); |
| 792 | } |
| 793 | void ConfigFile::addVariable(const QString &group, const QString &name, const QPoint &defvalue) |
| 794 | {
|
| 795 | if (getEntry(group, name).isEmpty())
|
| 796 | writeEntry(group,name,defvalue); |
| 797 | } |
| 798 | |
| 799 | ConfigFile *config_file_ptr; |