settings.cpp

00001 // -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*-
00002 /*
00003  * settings.cpp
00004  *
00005  * Copyright (C)  2003  Zack Rusin <zack@kde.org>
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00020  * 02110-1301  USA
00021  */
00022 #include "settings.h"
00023 
00024 #include "broker.h"
00025 
00026 #include <kglobal.h>
00027 #include <klocale.h>
00028 #include <kconfig.h>
00029 #include <kdebug.h>
00030 
00031 #include <qmap.h>
00032 #include <qstringlist.h>
00033 
00034 namespace KSpell2
00035 {
00036 class Settings::Private
00037 {
00038 public:
00039     Broker*  broker; //can't be a Ptr since we don't want to hold a ref on it
00040     KSharedConfig::Ptr config;
00041     bool     modified;
00042 
00043     QString defaultLanguage;
00044     QString defaultClient;
00045 
00046     bool checkUppercase;
00047     bool skipRunTogether;
00048     bool backgroundCheckerEnabled;
00049 
00050     QMap<QString, bool> ignore;
00051 };
00052 
00053 Settings::Settings( Broker *broker, KSharedConfig *config )
00054 {
00055     d = new Private;
00056     d->broker = broker;
00057 
00058     Q_ASSERT( config );
00059     d->config = config;
00060 
00061     d->modified = false;
00062     loadConfig();
00063 }
00064 
00065 Settings::~Settings()
00066 {
00067     delete d; d = 0;
00068 }
00069 
00070 KSharedConfig *Settings::sharedConfig() const
00071 {
00072     return d->config;
00073 }
00074 
00075 void Settings::setDefaultLanguage( const QString& lang )
00076 {
00077     QStringList cs = d->broker->languages();
00078     if ( cs.find( lang ) != cs.end() &&
00079          d->defaultLanguage != lang ) {
00080         d->defaultLanguage = lang;
00081         readIgnoreList();
00082         d->modified = true;
00083         d->broker->changed();
00084     }
00085 }
00086 
00087 QString Settings::defaultLanguage() const
00088 {
00089     return d->defaultLanguage;
00090 }
00091 
00092 void Settings::setDefaultClient( const QString& client )
00093 {
00094     //Different from setDefaultLanguage because
00095     //the number of clients can't be even close
00096     //as big as the number of languages
00097     if ( d->broker->clients().contains( client ) ) {
00098         d->defaultClient = client;
00099         d->modified = true;
00100         d->broker->changed();
00101     }
00102 }
00103 
00104 QString Settings::defaultClient() const
00105 {
00106     return d->defaultClient;
00107 }
00108 
00109 void Settings::setCheckUppercase( bool check )
00110 {
00111     if ( d->checkUppercase != check ) {
00112         d->modified = true;
00113         d->checkUppercase = check;
00114     }
00115 }
00116 
00117 bool Settings::checkUppercase() const
00118 {
00119     return d->checkUppercase;
00120 }
00121 
00122 void Settings::setSkipRunTogether( bool skip )
00123 {
00124     if ( d->skipRunTogether != skip ) {
00125         d->modified = true;
00126         d->skipRunTogether = skip;
00127     }
00128 }
00129 
00130 bool Settings::skipRunTogether() const
00131 {
00132     return d->skipRunTogether;
00133 }
00134 
00135 void Settings::setBackgroundCheckerEnabled( bool enable )
00136 {
00137     if ( d->backgroundCheckerEnabled != enable ) {
00138         d->modified = true;
00139         d->backgroundCheckerEnabled = enable;
00140     }
00141 }
00142 
00143 bool Settings::backgroundCheckerEnabled() const
00144 {
00145     return d->backgroundCheckerEnabled;
00146 }
00147 
00148 void Settings::setCurrentIgnoreList( const QStringList& ignores )
00149 {
00150     setQuietIgnoreList( ignores );
00151     d->modified = true;
00152 }
00153 
00154 void Settings::setQuietIgnoreList( const QStringList& ignores )
00155 {
00156     d->ignore = QMap<QString, bool>();//clear out
00157     for ( QStringList::const_iterator itr = ignores.begin();
00158           itr != ignores.end(); ++itr ) {
00159         d->ignore.insert( *itr, true );
00160     }
00161 }
00162 
00163 QStringList Settings::currentIgnoreList() const
00164 {
00165     return d->ignore.keys();
00166 }
00167 
00168 void Settings::addWordToIgnore( const QString& word )
00169 {
00170     if ( !d->ignore.contains( word ) ) {
00171         d->modified = true;
00172         d->ignore.insert( word, true );
00173     }
00174 }
00175 
00176 bool Settings::ignore( const QString& word )
00177 {
00178     return d->ignore.contains( word );
00179 }
00180 
00181 void Settings::readIgnoreList()
00182 {
00183     KConfigGroup conf( d->config, "Spelling" );
00184     QString ignoreEntry = QString( "ignore_%1" ).arg( d->defaultLanguage );
00185     QStringList ignores = conf.readListEntry( ignoreEntry );
00186     setQuietIgnoreList( ignores );
00187 }
00188 
00189 void Settings::save()
00190 {
00191     if ( d->modified ) {
00192         KConfigGroup conf( d->config, "Spelling" );
00193         conf.writeEntry( "defaultClient", d->defaultClient );
00194         conf.writeEntry( "defaultLanguage", d->defaultLanguage );
00195         conf.writeEntry( "checkUppercase", d->checkUppercase );
00196         conf.writeEntry( "skipRunTogether", d->skipRunTogether );
00197         conf.writeEntry( "backgroundCheckerEnabled", d->backgroundCheckerEnabled );
00198         conf.writeEntry( QString( "ignore_%1" ).arg( d->defaultLanguage ),
00199                          d->ignore.keys() );
00200         conf.sync();
00201     }
00202 }
00203 
00204 void Settings::loadConfig()
00205 {
00206     KConfigGroup conf( d->config, "Spelling" );
00207     d->defaultClient = conf.readEntry( "defaultClient",
00208                                         QString::null );
00209     d->defaultLanguage = conf.readEntry(
00210         "defaultLanguage", KGlobal::locale()->language() );
00211 
00212     //same defaults are in the default filter (filter.cpp)
00213     d->checkUppercase = conf.readBoolEntry(
00214         "checkUppercase", true );
00215 
00216     d->skipRunTogether = conf.readBoolEntry(
00217         "skipRunTogether", true );
00218 
00219     d->backgroundCheckerEnabled = conf.readBoolEntry(
00220         "backgroundCheckerEnabled", true );
00221 
00222     readIgnoreList();
00223 }
00224 
00225 
00226 }
KDE Home | KDE Accessibility Home | Description of Access Keys