1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
/**
* broker.cpp
*
* Copyright (C) 2003 Zack Rusin <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "broker.h"
#include "settings.h"
#include "client.h"
#include "defaultdictionary.h"
#include "dictionary.h"
#include <tdeparts/plugin.h>
#include <tdeparts/componentfactory.h>
#include <kplugininfo.h>
#include <ktrader.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <tqptrdict.h>
#include <tqmap.h>
#define DEFAULT_CONFIG_FILE "tdespellrc"
namespace KSpell2
{
class Broker::Private
{
public:
KPluginInfo::List plugins;
Settings *settings;
// <language, Clients with that language >
TQMap<TQString, TQPtrList<Client> > languageClients;
TQStringList clients;
DefaultDictionary *defaultDictionary;
};
TQPtrDict<Broker> *Broker::s_brokers = 0;
Broker *Broker::openBroker( TDESharedConfig *config )
{
TDESharedConfig::Ptr preventDeletion;
if ( !config ) {
preventDeletion = TDESharedConfig::openConfig( DEFAULT_CONFIG_FILE );
} else
preventDeletion = config;
if ( s_brokers ) {
Broker *broker = s_brokers->find( preventDeletion );
if ( broker )
return broker;
}
Broker *broker = new Broker( preventDeletion );
return broker;
}
Broker::Broker( TDESharedConfig *config )
{
TDESharedConfig::Ptr preventDeletion( config );
Q_UNUSED( preventDeletion );
if ( !s_brokers )
s_brokers = new TQPtrDict<Broker>;
s_brokers->insert( config, this );
d = new Private;
d->settings = new Settings( this, config );
loadPlugins();
d->defaultDictionary = new DefaultDictionary( d->settings->defaultLanguage(),
this );
}
Broker::~Broker()
{
kdDebug()<<"Removing broker : "<< this << endl;
s_brokers->remove( d->settings->sharedConfig() );
KPluginInfo::List::iterator it = d->plugins.begin();
while ( it != d->plugins.end() ) {
KPluginInfo *pluginInfo = *it;
it = d->plugins.remove( it );
delete pluginInfo;
}
delete d->settings; d->settings = 0;
delete d; d = 0;
}
DefaultDictionary* Broker::defaultDictionary() const
{
return d->defaultDictionary;
}
Dictionary* Broker::dictionary( const TQString& language, const TQString& clientName ) const
{
TQString pclient = clientName;
TQString plang = language;
bool ddefault = false;
if ( plang.isEmpty() ) {
plang = d->settings->defaultLanguage();
}
if ( clientName == d->settings->defaultClient() &&
plang == d->settings->defaultLanguage() ) {
ddefault = true;
}
TQPtrList<Client> lClients = d->languageClients[ plang ];
if ( lClients.isEmpty() ) {
kdError()<<"No language dictionaries for the language : "<< plang <<endl;
return 0;
}
TQPtrListIterator<Client> itr( lClients );
while ( itr.current() ) {
if ( !pclient.isEmpty() ) {
if ( pclient == itr.current()->name() ) {
Dictionary *dict = itr.current()->dictionary( plang );
if ( dict ) //remove the if if the assert proves ok
dict->m_default = ddefault;
return dict;
}
} else {
//the first one is the one with the highest
//reliability
Dictionary *dict = itr.current()->dictionary( plang );
Q_ASSERT( dict );
if ( dict ) //remove the if if the assert proves ok
dict->m_default = ddefault;
return dict;
}
++itr;
}
return 0;
}
TQStringList Broker::clients() const
{
return d->clients;
}
TQStringList Broker::languages() const
{
return d->languageClients.keys();
}
Settings* Broker::settings() const
{
return d->settings;
}
void Broker::loadPlugins()
{
d->plugins = KPluginInfo::fromServices(
TDETrader::self()->query( "KSpell/Client" ) );
for ( KPluginInfo::List::Iterator itr = d->plugins.begin();
itr != d->plugins.end(); ++itr ) {
loadPlugin( ( *itr )->pluginName() );
}
}
void Broker::loadPlugin( const TQString& pluginId )
{
int error = 0;
kdDebug()<<"Loading plugin " << pluginId << endl;
Client *client = KParts::ComponentFactory::createInstanceFromQuery<Client>(
TQString::fromLatin1( "KSpell/Client" ),
TQString::fromLatin1( "[X-TDE-PluginInfo-Name]=='%1'" ).arg( pluginId ),
this, 0, TQStringList(), &error );
if ( client )
{
TQStringList languages = client->languages();
d->clients.append( client->name() );
for ( TQStringList::Iterator itr = languages.begin();
itr != languages.end(); ++itr ) {
if ( !d->languageClients[ *itr ].isEmpty() &&
client->reliability() < d->languageClients[ *itr ].first()->reliability() )
d->languageClients[ *itr ].append( client );
else
d->languageClients[ *itr ].prepend( client );
}
kdDebug() << k_funcinfo << "Successfully loaded plugin '"
<< pluginId << "'" << endl;
}
else
{
switch( error )
{
case KParts::ComponentFactory::ErrNoServiceFound:
kdDebug() << k_funcinfo << "No service implementing the given mimetype "
<< "and fullfilling the given constraint expression can be found."
<< endl;
break;
case KParts::ComponentFactory::ErrServiceProvidesNoLibrary:
kdDebug() << "the specified service provides no shared library." << endl;
break;
case KParts::ComponentFactory::ErrNoLibrary:
kdDebug() << "the specified library could not be loaded." << endl;
break;
case KParts::ComponentFactory::ErrNoFactory:
kdDebug() << "the library does not export a factory for creating components."
<< endl;
break;
case KParts::ComponentFactory::ErrNoComponent:
kdDebug() << "the factory does not support creating "
<< "components of the specified type."
<< endl;
break;
}
kdDebug() << k_funcinfo << "Loading plugin '" << pluginId
<< "' failed, KLibLoader reported error: '" << endl
<< KLibLoader::self()->lastErrorMessage() << "'" << endl;
}
}
void Broker::changed()
{
emit configurationChanged();
}
}
#include "broker.moc"
|