• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kspell2
 

kspell2

  • kspell2
broker.cpp
1 // -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*-
22 #include "broker.h"
23 #include "settings.h"
24 #include "client.h"
25 #include "defaultdictionary.h"
26 #include "dictionary.h"
27 
28 #include <kparts/plugin.h>
29 #include <kparts/componentfactory.h>
30 
31 #include <kplugininfo.h>
32 #include <ktrader.h>
33 #include <kconfig.h>
34 
35 #include <kdebug.h>
36 
37 #include <tqptrdict.h>
38 #include <tqmap.h>
39 
40 #define DEFAULT_CONFIG_FILE "kspellrc"
41 
42 namespace KSpell2
43 {
44 
45 class Broker::Private
46 {
47 public:
48  KPluginInfo::List plugins;
49  Settings *settings;
50 
51  // <language, Clients with that language >
52  TQMap<TQString, TQPtrList<Client> > languageClients;
53  TQStringList clients;
54  DefaultDictionary *defaultDictionary;
55 };
56 
57 TQPtrDict<Broker> *Broker::s_brokers = 0;
58 
59 Broker *Broker::openBroker( KSharedConfig *config )
60 {
61  KSharedConfig::Ptr preventDeletion;
62  if ( !config ) {
63  preventDeletion = KSharedConfig::openConfig( DEFAULT_CONFIG_FILE );
64  } else
65  preventDeletion = config;
66 
67  if ( s_brokers ) {
68  Broker *broker = s_brokers->find( preventDeletion );
69  if ( broker )
70  return broker;
71  }
72 
73  Broker *broker = new Broker( preventDeletion );
74  return broker;
75 }
76 
77 Broker::Broker( KSharedConfig *config )
78 {
79  KSharedConfig::Ptr preventDeletion( config );
80  Q_UNUSED( preventDeletion );
81 
82  if ( !s_brokers )
83  s_brokers = new TQPtrDict<Broker>;
84  s_brokers->insert( config, this );
85 
86  d = new Private;
87  d->settings = new Settings( this, config );
88  loadPlugins();
89 
90  d->defaultDictionary = new DefaultDictionary( d->settings->defaultLanguage(),
91  this );
92 }
93 
94 Broker::~Broker()
95 {
96  kdDebug()<<"Removing broker : "<< this << endl;
97  s_brokers->remove( d->settings->sharedConfig() );
98  KPluginInfo::List::iterator it = d->plugins.begin();
99  while ( it != d->plugins.end() ) {
100  KPluginInfo *pluginInfo = *it;
101  it = d->plugins.remove( it );
102  delete pluginInfo;
103  }
104 
105  delete d->settings; d->settings = 0;
106  delete d; d = 0;
107 }
108 
109 DefaultDictionary* Broker::defaultDictionary() const
110 {
111  return d->defaultDictionary;
112 }
113 
114 Dictionary* Broker::dictionary( const TQString& language, const TQString& clientName ) const
115 {
116  TQString pclient = clientName;
117  TQString plang = language;
118  bool ddefault = false;
119 
120  if ( plang.isEmpty() ) {
121  plang = d->settings->defaultLanguage();
122  }
123  if ( clientName == d->settings->defaultClient() &&
124  plang == d->settings->defaultLanguage() ) {
125  ddefault = true;
126  }
127 
128  TQPtrList<Client> lClients = d->languageClients[ plang ];
129 
130  if ( lClients.isEmpty() ) {
131  kdError()<<"No language dictionaries for the language : "<< plang <<endl;
132  return 0;
133  }
134 
135  TQPtrListIterator<Client> itr( lClients );
136  while ( itr.current() ) {
137  if ( !pclient.isEmpty() ) {
138  if ( pclient == itr.current()->name() ) {
139  Dictionary *dict = itr.current()->dictionary( plang );
140  if ( dict ) //remove the if if the assert proves ok
141  dict->m_default = ddefault;
142  return dict;
143  }
144  } else {
145  //the first one is the one with the highest
146  //reliability
147  Dictionary *dict = itr.current()->dictionary( plang );
148  Q_ASSERT( dict );
149  if ( dict ) //remove the if if the assert proves ok
150  dict->m_default = ddefault;
151  return dict;
152  }
153  ++itr;
154  }
155 
156  return 0;
157 }
158 
159 TQStringList Broker::clients() const
160 {
161  return d->clients;
162 }
163 
164 TQStringList Broker::languages() const
165 {
166  return d->languageClients.keys();
167 }
168 
169 Settings* Broker::settings() const
170 {
171  return d->settings;
172 }
173 
174 void Broker::loadPlugins()
175 {
176  d->plugins = KPluginInfo::fromServices(
177  KTrader::self()->query( "KSpell/Client" ) );
178 
179  for ( KPluginInfo::List::Iterator itr = d->plugins.begin();
180  itr != d->plugins.end(); ++itr ) {
181  loadPlugin( ( *itr )->pluginName() );
182  }
183 }
184 
185 void Broker::loadPlugin( const TQString& pluginId )
186 {
187  int error = 0;
188 
189  kdDebug()<<"Loading plugin " << pluginId << endl;
190 
191  Client *client = KParts::ComponentFactory::createInstanceFromQuery<Client>(
192  TQString::fromLatin1( "KSpell/Client" ),
193  TQString::fromLatin1( "[X-KDE-PluginInfo-Name]=='%1'" ).arg( pluginId ),
194  this, 0, TQStringList(), &error );
195 
196  if ( client )
197  {
198  TQStringList languages = client->languages();
199  d->clients.append( client->name() );
200 
201  for ( TQStringList::Iterator itr = languages.begin();
202  itr != languages.end(); ++itr ) {
203  if ( !d->languageClients[ *itr ].isEmpty() &&
204  client->reliability() < d->languageClients[ *itr ].first()->reliability() )
205  d->languageClients[ *itr ].append( client );
206  else
207  d->languageClients[ *itr ].prepend( client );
208  }
209 
210  kdDebug() << k_funcinfo << "Successfully loaded plugin '"
211  << pluginId << "'" << endl;
212  }
213  else
214  {
215  switch( error )
216  {
217  case KParts::ComponentFactory::ErrNoServiceFound:
218  kdDebug() << k_funcinfo << "No service implementing the given mimetype "
219  << "and fullfilling the given constraint expression can be found."
220  << endl;
221  break;
222  case KParts::ComponentFactory::ErrServiceProvidesNoLibrary:
223  kdDebug() << "the specified service provides no shared library." << endl;
224  break;
225  case KParts::ComponentFactory::ErrNoLibrary:
226  kdDebug() << "the specified library could not be loaded." << endl;
227  break;
228  case KParts::ComponentFactory::ErrNoFactory:
229  kdDebug() << "the library does not export a factory for creating components."
230  << endl;
231  break;
232  case KParts::ComponentFactory::ErrNoComponent:
233  kdDebug() << "the factory does not support creating "
234  << "components of the specified type."
235  << endl;
236  break;
237  }
238 
239  kdDebug() << k_funcinfo << "Loading plugin '" << pluginId
240  << "' failed, KLibLoader reported error: '" << endl
241  << KLibLoader::self()->lastErrorMessage() << "'" << endl;
242  }
243 }
244 
245 void Broker::changed()
246 {
247  emit configurationChanged();
248 }
249 
250 }
251 
252 #include "broker.moc"
KSpell2::Broker::dictionary
Dictionary * dictionary(const TQString &language=TQString::null, const TQString &client=TQString::null) const
Returns dictionary for the given language and preferred client.
Definition: broker.cpp:114
KSpell2::Broker::defaultDictionary
DefaultDictionary * defaultDictionary() const
Function returns the so-called DefaultDictionary.
Definition: broker.cpp:109
KSpell2::Broker::clients
TQStringList clients() const
Returns names of all supported clients (e.g.
Definition: broker.cpp:159
KSpell2::Client
The fact that this class inherits from TQObject makes me hugely unhappy.
Definition: client.h:41
KSpell2::Broker::languages
TQStringList languages() const
Returns a list of supported languages.
Definition: broker.cpp:164
KSpell2::Dictionary
Class is returned by from Broker.
Definition: dictionary.h:37
KSpell2::Broker::configurationChanged
void configurationChanged()
Signal is emitted whenever the Settings object associated with this Broker changes.
KSpell2
kspell_hspellclient.h
Definition: backgroundchecker.h:28
KSpell2::Broker
Class used to deal with dictionaries.
Definition: broker.h:48
KSpell2::Broker::settings
Settings * settings() const
Returns the Settings object used by the broker.
Definition: broker.cpp:169
KSpell2::Broker::openBroker
static Broker * openBroker(KSharedConfig *config=0)
Constructs the broker.
Definition: broker.cpp:59

kspell2

Skip menu "kspell2"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kspell2

Skip menu "kspell2"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kspell2 by doxygen 1.8.11
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |