• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.5 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • sonnet
loader.cpp
Go to the documentation of this file.
1 // -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*-
21 #include "loader_p.h"
22 #include "settings_p.h"
23 #include "client_p.h"
24 #include "spellerplugin_p.h"
25 
26 #include <klocale.h>
27 #include <kservicetypetrader.h>
28 
29 #include <kconfig.h>
30 #include <kdebug.h>
31 
32 #include <QtCore/QHash>
33 #include <QtCore/QMap>
34 
35 #define DEFAULT_CONFIG_FILE "sonnetrc"
36 
37 namespace Sonnet
38 {
39 
40 class Loader::Private
41 {
42 public:
43  KService::List plugins;
44  Settings *settings;
45 
46  // <language, Clients with that language >
47  QMap<QString, QList<Client*> > languageClients;
48  QStringList clients;
49 
50  QStringList languagesNameCache;
51 };
52 
53 K_GLOBAL_STATIC(Loader, s_loader)
54 
55 Loader *Loader::openLoader()
56 {
57  if (s_loader.isDestroyed()) {
58  return 0;
59  }
60 
61  return s_loader;
62 }
63 
64 Loader::Loader()
65  :d(new Private)
66 {
67  d->settings = new Settings(this);
68  KConfig config(QString::fromLatin1(DEFAULT_CONFIG_FILE));
69  d->settings->restore(&config);
70  loadPlugins();
71 }
72 
73 Loader::~Loader()
74 {
75  //kDebug()<<"Removing loader : "<< this;
76  d->plugins.clear();
77  delete d->settings; d->settings = 0;
78  delete d;
79 }
80 
81 SpellerPlugin *Loader::createSpeller(const QString& language,
82  const QString& clientName) const
83 {
84  QString pclient = clientName;
85  QString plang = language;
86  bool ddefault = false;
87 
88  if (plang.isEmpty()) {
89  plang = d->settings->defaultLanguage();
90  }
91  if (clientName == d->settings->defaultClient() &&
92  plang == d->settings->defaultLanguage()) {
93  ddefault = true;
94  }
95 
96  const QList<Client*> lClients = d->languageClients[plang];
97 
98  if (lClients.isEmpty()) {
99  kError()<<"No language dictionaries for the language : "
100  << plang <<endl;
101  return 0;
102  }
103 
104  QListIterator<Client*> itr(lClients);
105  while (itr.hasNext()) {
106  Client* item = itr.next();
107  if (!pclient.isEmpty()) {
108  if (pclient == item->name()) {
109  SpellerPlugin *dict = item->createSpeller(plang);
110  return dict;
111  }
112  } else {
113  //the first one is the one with the highest
114  //reliability
115  SpellerPlugin *dict = item->createSpeller(plang);
116  return dict;
117  }
118  }
119 
120  return 0;
121 }
122 
123 QStringList Loader::clients() const
124 {
125  return d->clients;
126 }
127 
128 QStringList Loader::languages() const
129 {
130  return d->languageClients.keys();
131 }
132 
133 QString Loader::languageNameForCode(const QString &langCode) const
134 {
135  QString currentDictionary = langCode, // e.g. en_GB-ize-wo_accents
136  lISOName, // language ISO name
137  cISOName, // country ISO name
138  variantName, // dictionary variant name e.g. w_accents
139  localizedLang, // localized language
140  localizedCountry; // localized country
141  QByteArray variantEnglish; // dictionary variant in English
142 
143  int underscorePos, // position of "_" char
144  minusPos, // position of "-" char
145  variantCount = 0; // used to iterate over variantList
146 
147  struct variantListType
148  {
149  const char* variantShortName;
150  const char* variantEnglishName;
151  };
152 
153  const variantListType variantList[] = {
154  { "40", I18N_NOOP2("dictionary variant", "40") }, // what does 40 mean?
155  { "60", I18N_NOOP2("dictionary variant", "60") }, // what does 60 mean?
156  { "80", I18N_NOOP2("dictionary variant", "80") }, // what does 80 mean?
157  { "ise", I18N_NOOP2("dictionary variant", "-ise suffixes") },
158  { "ize", I18N_NOOP2("dictionary variant", "-ize suffixes") },
159  { "ise-w_accents", I18N_NOOP2("dictionary variant", "-ise suffixes and with accents") },
160  { "ise-wo_accents", I18N_NOOP2("dictionary variant", "-ise suffixes and without accents") },
161  { "ize-w_accents", I18N_NOOP2("dictionary variant", "-ize suffixes and with accents") },
162  { "ize-wo_accents", I18N_NOOP2("dictionary variant", "-ize suffixes and without accents") },
163  { "lrg", I18N_NOOP2("dictionary variant", "large") },
164  { "med", I18N_NOOP2("dictionary variant", "medium") },
165  { "sml", I18N_NOOP2("dictionary variant", "small") },
166  { "variant_0", I18N_NOOP2("dictionary variant", "variant 0") },
167  { "variant_1", I18N_NOOP2("dictionary variant", "variant 1") },
168  { "variant_2", I18N_NOOP2("dictionary variant", "variant 2") },
169  { "wo_accents", I18N_NOOP2("dictionary variant", "without accents") },
170  { "w_accents", I18N_NOOP2("dictionary variant", "with accents") },
171  { "ye", I18N_NOOP2("dictionary variant", "with ye") },
172  { "yeyo", I18N_NOOP2("dictionary variant", "with yeyo") },
173  { "yo", I18N_NOOP2("dictionary variant", "with yo") },
174  { "extended", I18N_NOOP2("dictionary variant", "extended") },
175  { 0, 0 }
176  };
177 
178  minusPos = currentDictionary.indexOf(QLatin1Char('-'));
179  underscorePos = currentDictionary.indexOf(QLatin1Char('_'));
180  if (underscorePos != -1 && underscorePos <= 3) {
181  cISOName = currentDictionary.mid(underscorePos + 1, 2);
182  lISOName = currentDictionary.left(underscorePos);
183  if ( minusPos != -1 )
184  variantName = currentDictionary.right(
185  currentDictionary.length() - minusPos - 1);
186  } else {
187  if ( minusPos != -1 ) {
188  variantName = currentDictionary.right(
189  currentDictionary.length() - minusPos - 1);
190  lISOName = currentDictionary.left(minusPos);
191  }
192  else
193  lISOName = currentDictionary;
194  }
195  localizedLang = KGlobal::locale()->languageCodeToName(lISOName);
196  if (localizedLang.isEmpty())
197  localizedLang = lISOName;
198  if (!cISOName.isEmpty()) {
199  if (!KGlobal::locale()->countryCodeToName(cISOName).isEmpty())
200  localizedCountry = KGlobal::locale()->countryCodeToName(cISOName);
201  else
202  localizedCountry = cISOName;
203  }
204  if (!variantName.isEmpty()) {
205  while (variantList[variantCount].variantShortName != 0)
206  if (QLatin1String(variantList[variantCount].variantShortName) == variantName)
207  break;
208  else
209  variantCount++;
210  if (variantList[variantCount].variantShortName != 0)
211  variantEnglish = variantList[variantCount].variantEnglishName;
212  else
213  variantEnglish = variantName.toLatin1();
214  }
215  if (!cISOName.isEmpty() && !variantName.isEmpty())
216  return i18nc(
217  "dictionary name. %1-language, %2-country and %3 variant name",
218  "%1 (%2) [%3]", localizedLang, localizedCountry,
219  i18nc( "dictionary variant", variantEnglish));
220  else if (!cISOName.isEmpty())
221  return i18nc(
222  "dictionary name. %1-language and %2-country name",
223  "%1 (%2)", localizedLang, localizedCountry);
224  else if (!variantName.isEmpty())
225  return i18nc(
226  "dictionary name. %1-language and %2-variant name",
227  "%1 [%2]", localizedLang,
228  i18nc("dictionary variant", variantEnglish));
229  else
230  return localizedLang;
231 }
232 
233 QStringList Loader::languageNames() const
234 {
235  /* For whatever reason languages() might change. So,
236  * to be in sync with it let's do the following check.
237  */
238  if (d->languagesNameCache.count() == languages().count() )
239  return d->languagesNameCache;
240 
241  QStringList allLocalizedDictionaries;
242  const QStringList allDictionaries = languages();
243 
244  for (QStringList::ConstIterator it = allDictionaries.begin();
245  it != allDictionaries.end(); ++it) {
246  allLocalizedDictionaries.append(languageNameForCode(*it));
247  }
248  // cache the list
249  d->languagesNameCache = allLocalizedDictionaries;
250  return allLocalizedDictionaries;
251 }
252 
253 Settings* Loader::settings() const
254 {
255  return d->settings;
256 }
257 
258 void Loader::loadPlugins()
259 {
260  d->plugins = KServiceTypeTrader::self()->query(QString::fromLatin1("Sonnet/SpellClient"));
261 
262  for (KService::List::const_iterator itr = d->plugins.constBegin();
263  itr != d->plugins.constEnd(); ++itr ) {
264  loadPlugin((*itr));
265  }
266 }
267 
268 void Loader::loadPlugin(const KSharedPtr<KService> &service)
269 {
270  QString error;
271 
272  Client *client = service->createInstance<Client>(this,
273  QVariantList(),
274  &error);
275 
276  if (client) {
277  const QStringList languages = client->languages();
278  d->clients.append(client->name());
279 
280  for (QStringList::const_iterator itr = languages.begin();
281  itr != languages.end(); ++itr) {
282  if (!d->languageClients[*itr].isEmpty() &&
283  client->reliability() <
284  d->languageClients[*itr].first()->reliability())
285  d->languageClients[*itr].append(client);
286  else
287  d->languageClients[*itr].prepend(client);
288  }
289 
290  //kDebug() << "Successfully loaded plugin:" << service->entryPath();
291  } else {
292  kDebug() << error;
293  }
294 }
295 
296 void Loader::changed()
297 {
298  emit configurationChanged();
299 }
300 
301 }
302 
303 #include "loader_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Fri Nov 16 2012 14:49:07 by doxygen 1.8.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.8.5 API Reference

Skip menu "kdelibs-4.8.5 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal