Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

preferences.cpp

Go to the documentation of this file.
00001 
00002 #ifndef USEQPE
00003 
00004 #include "preferences.h"
00005 
00006 #include <qfile.h>
00007 #include <qtextstream.h>
00008 
00009 Config::Config(const QString& _fn) : fname(_fn)
00010 {
00011 //    qDebug("Config::Config:%s", (const char*)fname);
00012     QFile fl(fname);
00013     if (fl.open(IO_ReadOnly))
00014     {
00015         QTextStream t(&fl);
00016         QString key, value;
00017         while (!t.eof())
00018         {
00019             QString data = t.readLine();
00020             int colon = data.find(':');
00021             if (colon > 0)
00022             {
00023                 QString key = data.left(colon);
00024                 QString value = data.right(data.length()-colon-1);
00025                 values[key] = value;
00026             }
00027         }
00028         fl.close();
00029     }
00030 //    read entries into values
00031 }
00032 Config::~Config()
00033 {
00034 //    qDebug("Config::~Config:%s", (const char*)fname);
00035     QFile fl(fname);
00036     if (fl.open(IO_WriteOnly))
00037     {
00038         QTextStream t(&fl);
00039         for (QMap<QString,QString>::Iterator iter = values.begin();
00040              iter != values.end();
00041              iter++)
00042         {
00043             t << iter.key() << ':' << iter.data() << '\n';
00044         }
00045         fl.close();
00046     }
00047 }
00048 QString Config::readEntry(const QString& key, const QString& deflt)
00049 {
00050     QMap<QString,QString>::Iterator iter = values.find(key);
00051     if (iter != values.end())
00052     {
00053         return iter.data();
00054     }
00055     else
00056     {
00057         return deflt;
00058     }
00059 }
00060 bool Config::readBoolEntry(const QString& key, const bool deflt)
00061 {
00062     bool ok;
00063     QMap<QString,QString>::Iterator iter = values.find(key);
00064     if (iter != values.end())
00065     {
00066         int ret = iter.data().toInt(&ok);
00067         return ((ok) ? !!ret : deflt);
00068     }
00069     else
00070     {
00071         return deflt;
00072     }
00073 }
00074 int Config::readNumEntry(const QString& key, const int deflt)
00075 {
00076     bool ok;
00077     QMap<QString,QString>::Iterator iter = values.find(key);
00078     if (iter != values.end())
00079     {
00080         int ret = iter.data().toInt(&ok);
00081         return ((ok) ? ret : deflt);
00082     }
00083     else
00084     {
00085         return deflt;
00086     }
00087 }
00088 void Config::writeEntry(const QString& key, const QString& value)
00089 {
00090     values[key] = value;
00091 }
00092 
00093 void Config::writeEntry(const QString& key, const bool value)
00094 {
00095     values[key] = (value) ? "1" : "0";
00096 }
00097 
00098 void Config::writeEntry(const QString& key, const int value)
00099 {
00100     QString rhs;
00101     rhs.setNum(value);
00102     values[key] = rhs;
00103 }
00104 
00105 #endif

Generated on Sat Nov 5 16:16:56 2005 for OPIE by  doxygen 1.4.2