1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 /* This is the Scribus Short Words configuratin implementation.
8 There will be interface for the future Scribus central plugin
9 config center. maybe :)
10
11 This code is based on the Scribus-Vlna plug in rewritten for
12 international use.
13
14 2004 Petr Vanek <petr@yarpen.cz>
15 with contributors.
16
17 This program is free software - see LICENSE file in the distribution
18 or documentation
19 */
20
21 #include "scconfig.h"
22 #include "configuration.h"
23 //#include "configuration.moc"
24 #include "scpaths.h"
25 #include "version.h"
26
27 #include "langmgr.h"
28 #include "prefsmanager.h"
29 #include "prefscontext.h"
30 #include "prefsfile.h"
31 #include <QDir>
32 #include <QStringList>
33
34
SWConfig()35 SWConfig::SWConfig()
36 {
37 prefs = PrefsManager::instance().prefsFile->getPluginContext("short-words");
38 action = prefs->getUInt("action", 0);
39 //userConfig = prefs->getUInt("userConfig", 0);
40 //editor = prefs->get("editor", "");
41 useStyle = prefs->getBool("useStyle", true);
42 currentLanguage = prefs->get("currentLanguage", "en");
43 }
44
saveConfig()45 void SWConfig::saveConfig()
46 {
47 prefs->set("action", action);
48 //prefs->set("userConfig", userConfig);
49 //prefs->set("editor", editor);
50 prefs->set("useStyle", useStyle);
51 prefs->set("currentLanguage", currentLanguage);
52 }
53
getShortWordsFromFile(const QString & lang,const QString & filename)54 QStringList SWConfig::getShortWordsFromFile(const QString& lang, const QString& filename)
55 {
56 // path to the cfg. file
57 QFile f(filename);
58 if (!f.exists())
59 {
60 qDebug("Short Words config file not found");
61 return QStringList();
62 }
63
64 // all shorts for one language
65 QString shorts;
66 // one line in cfg. file
67 QString aRow;
68 // cfg (doesn't) exists for the selected language indicator
69 bool success = false;
70
71 if (f.open(QIODevice::ReadOnly))
72 {
73 QTextStream t(&f);
74 while (!t.atEnd())
75 {
76 aRow = t.readLine();
77 if (aRow.left(2) == lang.left(2))
78 {
79 success = true;
80 shorts += aRow.remove(0, 3);
81 }
82 }
83 f.close();
84 }
85 if (success)
86 return shorts.split(",", Qt::SkipEmptyParts);
87 return QStringList();
88 }
89
getShortWords(const QString & lang)90 QStringList SWConfig::getShortWords(const QString& lang)
91 {
92 if (QFile::exists(RC_PATH_USR))
93 return getShortWordsFromFile(lang, RC_PATH_USR);
94 return getShortWordsFromFile(lang, RC_PATH);
95 }
96
getAvailableLanguageCodes(const QString & filename)97 QStringList SWConfig::getAvailableLanguageCodes(const QString& filename)
98 {
99 QFile f(filename);
100 if (!f.open(QIODevice::ReadOnly))
101 return QStringList();
102
103 QTextStream t(&f);
104 QStringList nations;
105 QString aRow, code;
106 while (!t.atEnd())
107 {
108 aRow = t.readLine();
109 code = aRow.left(2);
110 if (aRow.left(1) != "#" && aRow.length() != 0 && aRow.left(1) != " " && !nations.contains(code))
111 {
112 nations.append(code);
113 }
114 }
115 f.close();
116
117 return nations;
118 }
119
getAvailableLanguagesList()120 QStringList SWConfig::getAvailableLanguagesList()
121 {
122 QStringList allConfig;
123
124 if (QFile::exists(RC_PATH_USR))
125 allConfig = getAvailableLanguageCodes(RC_PATH_USR);
126 else
127 allConfig = getAvailableLanguageCodes(RC_PATH);
128 return allConfig;
129 }
130
getAvailableLanguages()131 QString SWConfig::getAvailableLanguages()
132 {
133 QStringList allCodes, allConfig;
134 allCodes = getAvailableLanguageCodes(RC_PATH);
135 allConfig << QObject::tr("Standard configuration: ", "short words plugin") << "<p>";
136 allConfig << getLanguageStringsFromCodes(allCodes).join(", ");
137 if (QFile::exists(RC_PATH_USR))
138 {
139 allCodes = getAvailableLanguageCodes(RC_PATH_USR);
140 allConfig << "<p>" << QObject::tr("Custom (optional) configuration: ", "short words plugin") << "<p>";
141 allConfig << getLanguageStringsFromCodes(allCodes).join(", ");
142 }
143 return allConfig.join("");
144 }
145
getLanguageStringsFromCodes(const QStringList & codes)146 QStringList SWConfig::getLanguageStringsFromCodes(const QStringList& codes)
147 {
148 QStringList languages;
149
150 for (int i = 0; i < codes.count(); ++i)
151 {
152 const QString& code = codes.at(i);
153 QString lang = LanguageManager::instance()->getLangFromAbbrev(code, true);
154 if (lang.length() > 0)
155 languages.append(lang);
156 }
157
158 return languages;
159 }
160