Back to index

scribus-ng  1.3.4.dfsg+svn20071115
configuration.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 /* This is the Scribus Short Words configuratin implementation.
00008 There will be interface for the future Scribus central plugin
00009 config center. maybe :)
00010 
00011 This code is based on the Scribus-Vlna plug in rewritten for
00012 international use.
00013 
00014 2004 Petr Vanek <petr@yarpen.cz>
00015 with contributors.
00016 
00017 This program is free software - see LICENSE file in the distribution
00018 or documentation
00019 */
00020 
00021 #include "scconfig.h"
00022 #include "configuration.h"
00023 #include "configuration.moc"
00024 #include "scpaths.h"
00025 #include "version.h"
00026 
00027 #include "langmgr.h"
00028 #include "prefsmanager.h"
00029 #include "prefscontext.h"
00030 #include "prefsfile.h"
00031 #include <qdir.h>
00032 #include <qstringlist.h>
00033 
00034 
00035 SWConfig::SWConfig()
00036 {
00037        prefs = PrefsManager::instance()->prefsFile->getPluginContext("short-words");
00038        action = prefs->getUInt("action", 0);
00039        //userConfig = prefs->getUInt("userConfig", 0);
00040        //editor = prefs->get("editor", "");
00041 }
00042 
00043 void SWConfig::saveConfig()
00044 {
00045        prefs->set("action", action);
00046        //prefs->set("userConfig", userConfig);
00047        //prefs->set("editor", editor);
00048 }
00049 
00050 QStringList SWConfig::getShortWordsFromFile(QString lang, QString filename)
00051 {
00052        // all shorts for one language
00053        QString shorts = "";
00054        // one line in cfg. file
00055        QString aRow;
00056        // cfg (doesn't) exists for the selected language indicator
00057        bool success = false;
00058        // path to the cfg. file
00059        QFile f;
00060 
00061        f.setName(filename);
00062        if (!f.exists())
00063        {
00064               qDebug("Short Words config file not found");
00065               return QStringList();
00066        }
00067        if (f.open(IO_ReadOnly))
00068        {
00069               QTextStream t(&f);
00070               while (!t.eof())
00071               {
00072                      aRow = t.readLine();
00073                      if (aRow.left(2) == lang)
00074                      {
00075                             success = true;
00076                             shorts += aRow.remove(0, 3);
00077                      }
00078               }
00079               f.close();
00080        }
00081        if (success)
00082               return QStringList::split(",", shorts);
00083        return QStringList();
00084 }
00085 
00086 QStringList SWConfig::getShortWords(QString lang)
00087 {
00088        //QStringList allShorts;
00089        if (QFile::exists(RC_PATH_USR))
00090               return getShortWordsFromFile(lang, RC_PATH_USR);
00091        return getShortWordsFromFile(lang, RC_PATH);
00092        /*if (userConfig && QFile::exists(RC_PATH_USR))
00093               return getShortWordsFromFile(lang, RC_PATH_USR);
00094        if (!userConfig && QFile::exists(RC_PATH_USR))
00095               allShorts = getShortWordsFromFile(lang, RC_PATH_USR);
00096        return allShorts + getShortWordsFromFile(lang, RC_PATH);*/
00097 }
00098 
00099 QString SWConfig::getAvailableLanguagesFromFile(QString filename)
00100 {
00101        QStringList langs;
00102        QStringList nations;
00103        QString aRow;
00104 
00105        QFile f(filename);
00106        if (f.open(IO_ReadOnly))
00107        {
00108               QTextStream t(&f);
00109               while (!t.eof())
00110               {
00111                      aRow = t.readLine();
00112                      if (aRow.left(1) != "#" && aRow.length() != 0 && aRow.left(1) != " " && !langs.contains(aRow.left(2)))
00113                      {
00114                             nations.append(getLangFromCode(aRow.left(2)));
00115                             langs.append(aRow.left(2));
00116                      }
00117               }
00118               f.close();
00119        }
00120        else
00121        {
00122               return QString();
00123        }
00124        if (filename == RC_PATH_USR)
00125               return QObject::tr("Custom (optional) configuration: ", "short words plugin") + " " + nations.join(", ");
00126        if (filename == RC_PATH)
00127               return QObject::tr("Standard configuration: ", "short words plugin") + " " + nations.join(", ");
00128        return nations.join(", "); // save return only
00129 }
00130 
00131 QString SWConfig::getAvailableLanguages()
00132 {
00133        QString allConfig = getAvailableLanguagesFromFile(RC_PATH);
00134        if (QFile::exists(RC_PATH_USR))
00135               allConfig += "<br>" + getAvailableLanguagesFromFile(RC_PATH_USR) + "";
00136        return  allConfig;
00137 }
00138 
00139 QString SWConfig::getLangCodeFromHyph(QString hyphenCode)
00140 {
00141        hyphenCode.remove(0, 5);
00142        return hyphenCode.remove(2, 10);
00143 }
00144 
00145 QString SWConfig::getLangFromCode(QString code)
00146 {
00147        QMap<QString,QString>::Iterator it;
00148        QString lang;
00149        LanguageManager langmgr;
00150        langmgr.init(false);
00151        return langmgr.getLangFromAbbrev(code, true);
00152 }