Back to index

scribus-ng  1.3.4.dfsg+svn20071115
prefsfile.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /***************************************************************************
00008  *   Copyright (C) 2004 by Riku Leino                                      *
00009  *   tsoots@gmail.com                                                      *
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  *   This program is distributed in the hope that it will be useful,       *
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00019  *   GNU General Public License for more details.                          *
00020  *                                                                         *
00021  *   You should have received a copy of the GNU General Public License     *
00022  *   along with this program; if not, write to the                         *
00023  *   Free Software Foundation, Inc.,                                       *
00024  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00025  ***************************************************************************/
00026 
00027 #include <qfile.h>
00028 #include <qfileinfo.h>
00029 #include <qstring.h>
00030 #include <qxml.h>
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; // No prefs file path set -> can't write or no prefs to write
00082        QFile* prefsXML = new QFile(prefsFilePath);
00083        if (prefsXML->open(IO_WriteOnly))
00084        {
00085               QTextStream stream(prefsXML);
00086               stream.setEncoding(QTextStream::UnicodeUTF8);
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.data()->isPersistent()) && (!it.data()->isEmpty()))
00114               {
00115                      stream << "\t\t<context name=\"" + replaceIllegalChars(it.key()) + "\">\n";
00116                      AttributeMap::Iterator it2;
00117                      PrefsContext* tmpCon = it.data();
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.data()) + "\"/>\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.data();
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("&", "&amp;");
00149        s.replace("<", "&lt;");
00150        s.replace(">", "&gt;");
00151        s.replace("\"", "&quot;");
00152        s.replace("\'", "&apos;");
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.findRev("/")));
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.data();
00178        for (it = pluginContexts.begin(); it != pluginContexts.end(); ++it)
00179               delete it.data();
00180 }