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

tdespell2

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

tdespell2

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

tdespell2

Skip menu "tdespell2"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdespell2 by doxygen 1.9.1
This website is maintained by Timothy Pearson.