prefsfile.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <QFile>
00028 #include <QFileInfo>
00029 #include <QString>
00030 #include <QXmlInputSource>
00031 #include "prefsfile.h"
00032 #include "prefsreader.h"
00033 #include "prefstable.h"
00034
00035 PrefsFile::PrefsFile()
00036 {
00037 prefsFilePath = "";
00038 ioEnabled = false;
00039 }
00040
00041 PrefsFile::PrefsFile(const QString& pFilePath, bool write)
00042 {
00043 prefsFilePath = pFilePath;
00044 ioEnabled = write;
00045 if (ioEnabled)
00046 canWrite();
00047 load();
00048 }
00049
00050 PrefsContext* PrefsFile::getContext(const QString& contextName, bool persistent)
00051 {
00052 if (!contexts.contains(contextName))
00053 contexts[contextName] = new PrefsContext(contextName, persistent);
00054 return contexts[contextName];
00055 }
00056
00057 PrefsContext* PrefsFile::getPluginContext(const QString& contextName, bool persistent)
00058 {
00059 if (!pluginContexts.contains(contextName))
00060 pluginContexts[contextName] = new PrefsContext(contextName, persistent);
00061 return pluginContexts[contextName];
00062 }
00063
00064 void PrefsFile::load()
00065 {
00066 PrefsReader* handler = new PrefsReader(&contexts, &pluginContexts);
00067 QFile* rc = new QFile(prefsFilePath);
00068 QXmlInputSource* source = new QXmlInputSource(rc);
00069 QXmlSimpleReader* reader = new QXmlSimpleReader();
00070 reader->setContentHandler(handler);
00071 reader->parse(source);
00072 delete reader;
00073 delete source;
00074 delete rc;
00075 delete handler;
00076 }
00077
00078 void PrefsFile::write()
00079 {
00080 if ((!ioEnabled) || ((contexts.size() == 0) && (pluginContexts.size() == 0)))
00081 return;
00082 QFile* prefsXML = new QFile(prefsFilePath);
00083 if (prefsXML->open(QIODevice::WriteOnly))
00084 {
00085 QTextStream stream(prefsXML);
00086 stream.setCodec("UTF-8");
00087 stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
00088 stream << "<preferences>\n";
00089 if (contexts.size() > 0)
00090 {
00091 stream << "\t<level name=\"application\">\n";
00092 writeContexts(&contexts, stream);
00093 stream << "\t</level>\n";
00094 }
00095 if (pluginContexts.size() > 0)
00096 {
00097 stream << "\t<level name=\"plugin\">\n";
00098 writeContexts(&pluginContexts, stream);
00099 stream << "\t</level>\n";
00100 }
00101 stream << "</preferences>\n";
00102
00103 prefsXML->close();
00104 }
00105 delete prefsXML;
00106 }
00107
00108 void PrefsFile::writeContexts(ContextMap* contextMap, QTextStream& stream)
00109 {
00110 ContextMap::Iterator it;
00111 for (it = contextMap->begin(); it != contextMap->end(); ++it)
00112 {
00113 if ((it.value()->isPersistent()) && (!it.value()->isEmpty()))
00114 {
00115 stream << "\t\t<context name=\"" + replaceIllegalChars(it.key()) + "\">\n";
00116 AttributeMap::Iterator it2;
00117 PrefsContext* tmpCon = it.value();
00118 for (it2 = tmpCon->values.begin(); it2 != tmpCon->values.end(); ++it2)
00119 {
00120 stream << "\t\t\t<attribute key=\"" + replaceIllegalChars(it2.key()) + "\" ";
00121 stream << "value=\"" + replaceIllegalChars(it2.value()) + "\"/>\n";
00122 }
00123 TableMap::Iterator it3;
00124 for (it3 = tmpCon->tables.begin(); it3 != tmpCon->tables.end(); ++it3)
00125 {
00126 stream << "\t\t\t<table name=\"" + replaceIllegalChars(it3.key()) + "\">\n";
00127 PrefsTable* t = it3.value();
00128 for (int i = 0; i < t->height(); ++i)
00129 {
00130 stream << QString("\t\t\t\t<row index=\"%1\">\n").arg(i);
00131 for (int j = 0; j < t->width(); ++j)
00132 {
00133 stream << QString("\t\t\t\t\t<col index=\"%1\">").arg(j);
00134 stream << replaceIllegalChars(t->get(i, j, "__NOT__SET__")) << "</col>\n";
00135 }
00136 stream << "\t\t\t\t</row>\n";
00137 }
00138 stream << "\t\t\t</table>\n";
00139 }
00140 stream << "\t\t</context>\n";
00141 }
00142 }
00143 }
00144
00145 QString PrefsFile::replaceIllegalChars(const QString& text)
00146 {
00147 QString s = text;
00148 s.replace("&", "&");
00149 s.replace("<", "<");
00150 s.replace(">", ">");
00151 s.replace("\"", """);
00152 s.replace("\'", "'");
00153 return s;
00154 }
00155
00156 void PrefsFile::canWrite()
00157 {
00158 if (ioEnabled)
00159 {
00160 QFile f(prefsFilePath);
00161 QFileInfo fi(f);
00162 if (fi.exists())
00163 ioEnabled = fi.isWritable();
00164 else
00165 {
00166 QFile f2(prefsFilePath.left(prefsFilePath.lastIndexOf("/")));
00167 QFileInfo fi2(f2);
00168 ioEnabled = fi2.isWritable();
00169 }
00170 }
00171 }
00172
00173 PrefsFile::~PrefsFile()
00174 {
00175 ContextMap::Iterator it;
00176 for (it = contexts.begin(); it != contexts.end(); ++it)
00177 delete it.value();
00178 for (it = pluginContexts.begin(); it != pluginContexts.end(); ++it)
00179 delete it.value();
00180 }