KDECore
loader.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- 00021 #include "loader_p.h" 00022 #include "settings_p.h" 00023 #include "client_p.h" 00024 #include "spellerplugin_p.h" 00025 00026 #include <klocale.h> 00027 #include <kservicetypetrader.h> 00028 00029 #include <kconfig.h> 00030 #include <kdebug.h> 00031 00032 #include <QtCore/QHash> 00033 #include <QtCore/QMap> 00034 00035 #define DEFAULT_CONFIG_FILE "sonnetrc" 00036 00037 namespace Sonnet 00038 { 00039 00040 class Loader::Private 00041 { 00042 public: 00043 KService::List plugins; 00044 Settings *settings; 00045 00046 // <language, Clients with that language > 00047 QMap<QString, QList<Client*> > languageClients; 00048 QStringList clients; 00049 00050 QStringList languagesNameCache; 00051 }; 00052 00053 K_GLOBAL_STATIC(Loader, s_loader) 00054 00055 Loader *Loader::openLoader() 00056 { 00057 if (s_loader.isDestroyed()) { 00058 return 0; 00059 } 00060 00061 return s_loader; 00062 } 00063 00064 Loader::Loader() 00065 :d(new Private) 00066 { 00067 d->settings = new Settings(this); 00068 KConfig config(QString::fromLatin1(DEFAULT_CONFIG_FILE)); 00069 d->settings->restore(&config); 00070 loadPlugins(); 00071 } 00072 00073 Loader::~Loader() 00074 { 00075 //kDebug()<<"Removing loader : "<< this; 00076 d->plugins.clear(); 00077 delete d->settings; d->settings = 0; 00078 delete d; 00079 } 00080 00081 SpellerPlugin *Loader::createSpeller(const QString& language, 00082 const QString& clientName) const 00083 { 00084 QString pclient = clientName; 00085 QString plang = language; 00086 00087 if (plang.isEmpty()) { 00088 plang = d->settings->defaultLanguage(); 00089 } 00090 00091 const QList<Client*> lClients = d->languageClients[plang]; 00092 00093 if (lClients.isEmpty()) { 00094 kError()<<"No language dictionaries for the language : " 00095 << plang <<endl; 00096 return 0; 00097 } 00098 00099 QListIterator<Client*> itr(lClients); 00100 while (itr.hasNext()) { 00101 Client* item = itr.next(); 00102 if (!pclient.isEmpty()) { 00103 if (pclient == item->name()) { 00104 SpellerPlugin *dict = item->createSpeller(plang); 00105 return dict; 00106 } 00107 } else { 00108 //the first one is the one with the highest 00109 //reliability 00110 SpellerPlugin *dict = item->createSpeller(plang); 00111 return dict; 00112 } 00113 } 00114 00115 return 0; 00116 } 00117 00118 QStringList Loader::clients() const 00119 { 00120 return d->clients; 00121 } 00122 00123 QStringList Loader::languages() const 00124 { 00125 return d->languageClients.keys(); 00126 } 00127 00128 QString Loader::languageNameForCode(const QString &langCode) const 00129 { 00130 QString currentDictionary = langCode, // e.g. en_GB-ize-wo_accents 00131 lISOName, // language ISO name 00132 cISOName, // country ISO name 00133 variantName, // dictionary variant name e.g. w_accents 00134 localizedLang, // localized language 00135 localizedCountry; // localized country 00136 QByteArray variantEnglish; // dictionary variant in English 00137 00138 int underscorePos, // position of "_" char 00139 minusPos, // position of "-" char 00140 variantCount = 0; // used to iterate over variantList 00141 00142 struct variantListType 00143 { 00144 const char* variantShortName; 00145 const char* variantEnglishName; 00146 }; 00147 00148 const variantListType variantList[] = { 00149 { "40", I18N_NOOP2("dictionary variant", "40") }, // what does 40 mean? 00150 { "60", I18N_NOOP2("dictionary variant", "60") }, // what does 60 mean? 00151 { "80", I18N_NOOP2("dictionary variant", "80") }, // what does 80 mean? 00152 { "ise", I18N_NOOP2("dictionary variant", "-ise suffixes") }, 00153 { "ize", I18N_NOOP2("dictionary variant", "-ize suffixes") }, 00154 { "ise-w_accents", I18N_NOOP2("dictionary variant", "-ise suffixes and with accents") }, 00155 { "ise-wo_accents", I18N_NOOP2("dictionary variant", "-ise suffixes and without accents") }, 00156 { "ize-w_accents", I18N_NOOP2("dictionary variant", "-ize suffixes and with accents") }, 00157 { "ize-wo_accents", I18N_NOOP2("dictionary variant", "-ize suffixes and without accents") }, 00158 { "lrg", I18N_NOOP2("dictionary variant", "large") }, 00159 { "med", I18N_NOOP2("dictionary variant", "medium") }, 00160 { "sml", I18N_NOOP2("dictionary variant", "small") }, 00161 { "variant_0", I18N_NOOP2("dictionary variant", "variant 0") }, 00162 { "variant_1", I18N_NOOP2("dictionary variant", "variant 1") }, 00163 { "variant_2", I18N_NOOP2("dictionary variant", "variant 2") }, 00164 { "wo_accents", I18N_NOOP2("dictionary variant", "without accents") }, 00165 { "w_accents", I18N_NOOP2("dictionary variant", "with accents") }, 00166 { "ye", I18N_NOOP2("dictionary variant", "with ye") }, 00167 { "yeyo", I18N_NOOP2("dictionary variant", "with yeyo") }, 00168 { "yo", I18N_NOOP2("dictionary variant", "with yo") }, 00169 { "extended", I18N_NOOP2("dictionary variant", "extended") }, 00170 { 0, 0 } 00171 }; 00172 00173 minusPos = currentDictionary.indexOf(QLatin1Char('-')); 00174 underscorePos = currentDictionary.indexOf(QLatin1Char('_')); 00175 if (underscorePos != -1 && underscorePos <= 3) { 00176 cISOName = currentDictionary.mid(underscorePos + 1, 2); 00177 lISOName = currentDictionary.left(underscorePos); 00178 if ( minusPos != -1 ) 00179 variantName = currentDictionary.right( 00180 currentDictionary.length() - minusPos - 1); 00181 } else { 00182 if ( minusPos != -1 ) { 00183 variantName = currentDictionary.right( 00184 currentDictionary.length() - minusPos - 1); 00185 lISOName = currentDictionary.left(minusPos); 00186 } 00187 else 00188 lISOName = currentDictionary; 00189 } 00190 localizedLang = KGlobal::locale()->languageCodeToName(lISOName); 00191 if (localizedLang.isEmpty()) 00192 localizedLang = lISOName; 00193 if (!cISOName.isEmpty()) { 00194 if (!KGlobal::locale()->countryCodeToName(cISOName).isEmpty()) 00195 localizedCountry = KGlobal::locale()->countryCodeToName(cISOName); 00196 else 00197 localizedCountry = cISOName; 00198 } 00199 if (!variantName.isEmpty()) { 00200 while (variantList[variantCount].variantShortName != 0) 00201 if (QLatin1String(variantList[variantCount].variantShortName) == variantName) 00202 break; 00203 else 00204 variantCount++; 00205 if (variantList[variantCount].variantShortName != 0) 00206 variantEnglish = variantList[variantCount].variantEnglishName; 00207 else 00208 variantEnglish = variantName.toLatin1(); 00209 } 00210 if (!cISOName.isEmpty() && !variantName.isEmpty()) 00211 return i18nc( 00212 "dictionary name. %1-language, %2-country and %3 variant name", 00213 "%1 (%2) [%3]", localizedLang, localizedCountry, 00214 i18nc( "dictionary variant", variantEnglish)); 00215 else if (!cISOName.isEmpty()) 00216 return i18nc( 00217 "dictionary name. %1-language and %2-country name", 00218 "%1 (%2)", localizedLang, localizedCountry); 00219 else if (!variantName.isEmpty()) 00220 return i18nc( 00221 "dictionary name. %1-language and %2-variant name", 00222 "%1 [%2]", localizedLang, 00223 i18nc("dictionary variant", variantEnglish)); 00224 else 00225 return localizedLang; 00226 } 00227 00228 QStringList Loader::languageNames() const 00229 { 00230 /* For whatever reason languages() might change. So, 00231 * to be in sync with it let's do the following check. 00232 */ 00233 if (d->languagesNameCache.count() == languages().count() ) 00234 return d->languagesNameCache; 00235 00236 QStringList allLocalizedDictionaries; 00237 const QStringList allDictionaries = languages(); 00238 00239 for (QStringList::ConstIterator it = allDictionaries.begin(); 00240 it != allDictionaries.end(); ++it) { 00241 allLocalizedDictionaries.append(languageNameForCode(*it)); 00242 } 00243 // cache the list 00244 d->languagesNameCache = allLocalizedDictionaries; 00245 return allLocalizedDictionaries; 00246 } 00247 00248 Settings* Loader::settings() const 00249 { 00250 return d->settings; 00251 } 00252 00253 void Loader::loadPlugins() 00254 { 00255 d->plugins = KServiceTypeTrader::self()->query(QString::fromLatin1("Sonnet/SpellClient")); 00256 00257 for (KService::List::const_iterator itr = d->plugins.constBegin(); 00258 itr != d->plugins.constEnd(); ++itr ) { 00259 loadPlugin((*itr)); 00260 } 00261 } 00262 00263 void Loader::loadPlugin(const KSharedPtr<KService> &service) 00264 { 00265 QString error; 00266 00267 Client *client = service->createInstance<Client>(this, 00268 QVariantList(), 00269 &error); 00270 00271 if (client) { 00272 const QStringList languages = client->languages(); 00273 d->clients.append(client->name()); 00274 00275 for (QStringList::const_iterator itr = languages.begin(); 00276 itr != languages.end(); ++itr) { 00277 if (!d->languageClients[*itr].isEmpty() && 00278 client->reliability() < 00279 d->languageClients[*itr].first()->reliability()) 00280 d->languageClients[*itr].append(client); 00281 else 00282 d->languageClients[*itr].prepend(client); 00283 } 00284 00285 //kDebug() << "Successfully loaded plugin:" << service->entryPath(); 00286 } else { 00287 kDebug() << error; 00288 } 00289 } 00290 00291 void Loader::changed() 00292 { 00293 emit configurationChanged(); 00294 } 00295 00296 } 00297 00298 #include "loader_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:28:14 by doxygen 1.7.5.1 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:28:14 by doxygen 1.7.5.1 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.