KDECore
settings.cpp
Go to the documentation of this file.00001
00022 #include "settings_p.h"
00023
00024 #include "loader_p.h"
00025
00026 #include <kconfig.h>
00027 #include <kconfiggroup.h>
00028 #include <kdebug.h>
00029 #include <kglobal.h>
00030 #include <klocale.h>
00031
00032 #include <QtCore/QMap>
00033 #include <QtCore/QMutableStringListIterator>
00034
00035 namespace Sonnet
00036 {
00037 class Settings::Private
00038 {
00039 public:
00040 Loader* loader;
00041 bool modified;
00042
00043 QString defaultLanguage;
00044 QString defaultClient;
00045
00046 bool checkUppercase;
00047 bool skipRunTogether;
00048 bool backgroundCheckerEnabled;
00049
00050 int disablePercentage;
00051 int disableWordCount;
00052
00053 QMap<QString, bool> ignore;
00054 };
00055
00056 Settings::Settings(Loader *loader)
00057 :d(new Private)
00058 {
00059 d->loader = loader;
00060
00061 d->modified = false;
00062 }
00063
00064 Settings::~Settings()
00065 {
00066 delete d;
00067 }
00068
00069 void Settings::setDefaultLanguage(const QString &lang)
00070 {
00071 QStringList cs = d->loader->languages();
00072 if (cs.indexOf(lang) != -1 &&
00073 d->defaultLanguage != lang) {
00074 d->defaultLanguage = lang;
00075
00076 d->modified = true;
00077 d->loader->changed();
00078 }
00079 }
00080
00081 QString Settings::defaultLanguage() const
00082 {
00083 return d->defaultLanguage;
00084 }
00085
00086 void Settings::setDefaultClient(const QString &client)
00087 {
00088
00089
00090
00091 if (d->loader->clients().contains(client)) {
00092 d->defaultClient = client;
00093 d->modified = true;
00094 d->loader->changed();
00095 }
00096 }
00097
00098 QString Settings::defaultClient() const
00099 {
00100 return d->defaultClient;
00101 }
00102
00103 void Settings::setCheckUppercase(bool check)
00104 {
00105 if (d->checkUppercase != check) {
00106 d->modified = true;
00107 d->checkUppercase = check;
00108 }
00109 }
00110
00111 bool Settings::checkUppercase() const
00112 {
00113 return d->checkUppercase;
00114 }
00115
00116 void Settings::setSkipRunTogether(bool skip)
00117 {
00118 if (d->skipRunTogether != skip) {
00119 d->modified = true;
00120 d->skipRunTogether = skip;
00121 }
00122 }
00123
00124 bool Settings::skipRunTogether() const
00125 {
00126 return d->skipRunTogether;
00127 }
00128
00129 void Settings::setBackgroundCheckerEnabled(bool enable)
00130 {
00131 if (d->backgroundCheckerEnabled != enable) {
00132 d->modified = true;
00133 d->backgroundCheckerEnabled = enable;
00134 }
00135 }
00136
00137 bool Settings::backgroundCheckerEnabled() const
00138 {
00139 return d->backgroundCheckerEnabled;
00140 }
00141
00142 void Settings::setCurrentIgnoreList(const QStringList &ignores)
00143 {
00144 setQuietIgnoreList(ignores);
00145 d->modified = true;
00146 }
00147
00148 void Settings::setQuietIgnoreList(const QStringList &ignores)
00149 {
00150 d->ignore = QMap<QString, bool>();
00151 for (QStringList::const_iterator itr = ignores.begin();
00152 itr != ignores.end(); ++itr) {
00153 d->ignore.insert(*itr, true);
00154 }
00155 }
00156
00157 QStringList Settings::currentIgnoreList() const
00158 {
00159 return d->ignore.keys();
00160 }
00161
00162 void Settings::addWordToIgnore(const QString &word)
00163 {
00164 if (!d->ignore.contains(word)) {
00165 d->modified = true;
00166 d->ignore.insert( word, true );
00167 }
00168 }
00169
00170 bool Settings::ignore( const QString& word )
00171 {
00172 return d->ignore.contains( word );
00173 }
00174
00175 void Settings::readIgnoreList(KConfig *config)
00176 {
00177 KConfigGroup conf(config, "Spelling");
00178 QString ignoreEntry = QString( "ignore_%1" ).arg(d->defaultLanguage);
00179 QStringList ignores = conf.readEntry(ignoreEntry, QStringList());
00180 setQuietIgnoreList(ignores);
00181 }
00182
00183 int Settings::disablePercentageWordError() const
00184 {
00185 return d->disablePercentage;
00186 }
00187
00188 int Settings::disableWordErrorCount() const
00189 {
00190 return d->disableWordCount;
00191 }
00192
00193 void Settings::save(KConfig *config)
00194 {
00195 KConfigGroup conf(config, "Spelling");
00196 conf.writeEntry("defaultClient", d->defaultClient);
00197 conf.writeEntry("defaultLanguage", d->defaultLanguage);
00198 conf.writeEntry("checkUppercase", d->checkUppercase);
00199 conf.writeEntry("skipRunTogether", d->skipRunTogether);
00200 conf.writeEntry("backgroundCheckerEnabled", d->backgroundCheckerEnabled);
00201 QString defaultLanguage = QString( "ignore_%1" ).arg(d->defaultLanguage);
00202 if(conf.hasKey(defaultLanguage) && d->ignore.isEmpty())
00203 conf.deleteEntry(defaultLanguage);
00204 else if(!d->ignore.isEmpty())
00205 conf.writeEntry(QString( "ignore_%1" ).arg(d->defaultLanguage),
00206 d->ignore.keys() );
00207
00208 conf.sync();
00209 }
00210
00211 void Settings::restore(KConfig *config)
00212 {
00213 KConfigGroup conf(config, "Spelling");
00214 d->defaultClient = conf.readEntry("defaultClient",
00215 QString());
00216 d->defaultLanguage = conf.readEntry(
00217 "defaultLanguage", KGlobal::locale()->language());
00218
00219
00220 d->checkUppercase = conf.readEntry(
00221 "checkUppercase", true);
00222
00223 d->skipRunTogether = conf.readEntry(
00224 "skipRunTogether", true);
00225
00226 d->backgroundCheckerEnabled = conf.readEntry(
00227 "backgroundCheckerEnabled", true);
00228
00229 d->disablePercentage = conf.readEntry("Sonnet_AsYouTypeDisablePercentage", 42);
00230 d->disableWordCount = conf.readEntry("Sonnet_AsYouTypeDisableWordCount", 100);
00231
00232 readIgnoreList(config);
00233 }
00234
00235
00236 bool Settings::modified() const
00237 {
00238 return d->modified;
00239 }
00240
00241 void Settings::setModified(bool modified)
00242 {
00243 d->modified = modified;
00244 }
00245
00246 }
00247