author | llornkcor <llornkcor> | 2004-07-13 10:35:23 (UTC) |
---|---|---|
committer | llornkcor <llornkcor> | 2004-07-13 10:35:23 (UTC) |
commit | 37e0e077b51841971c3d4c25b287f4d39e750e2d (patch) (side-by-side diff) | |
tree | c1b499dd6e544207c6ff7047acd8170d6ea63f94 /noncore/apps/zsafe/extra | |
parent | 5d2ec2e9b1dce49e914c260fe16a82ddccbaa92d (diff) | |
download | opie-37e0e077b51841971c3d4c25b287f4d39e750e2d.zip opie-37e0e077b51841971c3d4c25b287f4d39e750e2d.tar.gz opie-37e0e077b51841971c3d4c25b287f4d39e750e2d.tar.bz2 |
fix ups, and move qsettings files away
-rw-r--r-- | noncore/apps/zsafe/extra/qsettings.cpp | 156 | ||||
-rw-r--r-- | noncore/apps/zsafe/extra/qsettings.h | 56 |
2 files changed, 212 insertions, 0 deletions
diff --git a/noncore/apps/zsafe/extra/qsettings.cpp b/noncore/apps/zsafe/extra/qsettings.cpp new file mode 100644 index 0000000..def2e2d --- a/dev/null +++ b/noncore/apps/zsafe/extra/qsettings.cpp @@ -0,0 +1,156 @@ +/* +** $Id$ +*/ + +#include "qsettings.h" +#include <stdio.h> +#include <qfile.h> +#include <qtextstream.h> + + +QSettings::QSettings(const QString &_fn) +{ + qWarning("Settings "+_fn); + // read the prefs from the file + fn = _fn; + + QFile f(_fn); + if ( f.open(IO_ReadOnly) ) { // file opened successfully + QTextStream t( &f ); // use a text stream + QString s; + while ( !t.eof() ) { // until end of file... + s = t.readLine(); // line of text excluding '\n' + char buf[256]; + sprintf (buf, "%s", (const char *) s); + int pos = s.find (" = "); + QString key = s.left (pos); + QString val = s.right (s.length() - pos - 3); + writeEntry (key, val); + + sprintf (buf, "%s|%s", (const char *)key, (const char *)val); + } + f.close(); + } + + +} + +QSettings::~QSettings() +{ + // write out the prefs to the file + QAsciiDictIterator <QString> it( prefs ); // iterator for dict + QFile f(fn); + f.open(IO_WriteOnly); + QTextStream ts( &f ); + + while ( it.current() ) + { + QString *key = new QString(it.currentKey()); + char buf[256]; + sprintf (buf, "%s", (const char *) *( it.current())); + QString *val = new QString(buf); + sprintf (buf, "%s %s", (const char *)*key, (const char *)*val); + ts << *key << " = " << *val << endl; + ++it; + } + + f.close(); + prefs.clear(); +} + +void QSettings::insertSearchPath (System sys, const QString &str) +{ + fn = str; +} + +QString QSettings::readEntry (const QString &key, const QString &def) +{ + + QString *s = prefs.find((const char *)key); + if (!s) + return def; + else + return *s; + +} + +int QSettings::readNumEntry (const QString &key, int def) +{ + QString *s = prefs[key]; + if (!s) return def; + return s->toInt(); +} + +bool QSettings::readBoolEntry (const QString &key, bool def) +{ + QString *s = prefs[key]; + if (!s) return def; + if (*s == "1") + return true; + else + return false; +} + +bool QSettings::writeEntry (const QString &key, int val) +{ + char buf[64]; + sprintf (buf, "%d", val); + QString *v = new QString(buf); + prefs.replace ((const char *)key, v); + return true; +} + +bool QSettings::writeEntry (const QString &key, bool val) +{ + QString *v; + if (val) + v = new QString("1"); + else + v = new QString("0"); + prefs.replace ((const char *)key, v); + return true; +} + +bool QSettings::writeEntry (const QString &key, const QString &val) +{ + QString *v = new QString (val); + prefs.replace ((const char *)key, v); + return true; +} + +bool QSettings::writeEntry (const QString &key, const char *val) +{ + QString *v = new QString (val); + prefs.replace ((const char *)key, v); + return true; +} + +bool QSettings::removeEntry (const QString &key) +{ + prefs.remove (key); + return true; +} + +QStringList QSettings::entryList (const QString &key) const +{ + qDebug("entryList: "+key); + QStringList list; + if(!prefs.isEmpty()) { + QAsciiDictIterator <QString> it( prefs ); // iterator for dict + qDebug("ready"); + while ( it.current() ) + { + char buf[512]; + sprintf(buf, "%s", (const char *) *( it.current())); + QString *val = new QString(buf); + sprintf(buf, "%s -> %s\n", it.currentKey(), (const char *)*val ); + QString *cat = new QString(it.currentKey()); + if (cat->contains("-field", FALSE)) + list.append (it.currentKey()); + ++it; + } + } + qWarning("Return here"); + return list; +} + diff --git a/noncore/apps/zsafe/extra/qsettings.h b/noncore/apps/zsafe/extra/qsettings.h new file mode 100644 index 0000000..95e87b5 --- a/dev/null +++ b/noncore/apps/zsafe/extra/qsettings.h @@ -0,0 +1,56 @@ +/* +** $Id$ +*/ + +#ifndef _QSETTINGS_H_ +#define _QSETTINGS_H_ + +#include <qstring.h> +#include <qstringlist.h> +#include <qasciidict.h> + +// class to hold one category item + +class QSettings +{ +public: + enum Format { + Native = 0, + Ini + }; + enum System { + Unix = 0, + Windows, + Mac + }; + enum Scope { + User, + Global + }; + + QSettings(const QString &file=0); + ~QSettings(); + + void insertSearchPath (System, const QString &); + + QString readEntry (const QString &, const QString &def = QString::null); + int readNumEntry (const QString &, int def = 0); + bool readBoolEntry (const QString &, bool def = 0); + + bool writeEntry (const QString &, int); + bool writeEntry (const QString &, bool); + bool writeEntry (const QString &, const QString &); + bool writeEntry (const QString &, const char *); + + bool removeEntry (const QString &); + + QStringList entryList (const QString &) const; + +protected: + +private: + QAsciiDict <QString> prefs; // key, value dictonary + QString fn; // preference filename + +}; +#endif // _QSETTINGS_H_ |