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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/***************************************************************************
* Copyright (C) 2005 by Joris Guisson *
* joris.guisson@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <tqfile.h>
#include <textstream.h>
#include <kparts/componentfactory.h>
#include <util/log.h>
#include <util/error.h>
#include <util/fileops.h>
#include <util/waitjob.h>
#include <torrent/globals.h>
#include <interfaces/guiinterface.h>
#include "pluginmanager.h"
#include "pluginmanagerprefpage.h"
using namespace bt;
namespace kt
{
PluginManager::PluginManager(CoreInterface* core,GUIInterface* gui) : core(core),gui(gui)
{
unloaded.setAutoDelete(false);
plugins.setAutoDelete(false);
prefpage = 0;
pltoload.append("Info Widget");
pltoload.append("Search");
}
PluginManager::~PluginManager()
{
delete prefpage;
unloaded.setAutoDelete(true);
plugins.setAutoDelete(true);
}
void PluginManager::loadPluginList()
{
KTrader::OfferList offers = KTrader::self()->query("KTorrent/Plugin");
KTrader::OfferList::ConstIterator iter;
for(iter = offers.begin(); iter != offers.end(); ++iter)
{
KService::Ptr service = *iter;
int errCode = 0;
Plugin* plugin =
KParts::ComponentFactory::createInstanceFromService<kt::Plugin>
(service, 0, 0, TQStringList(),&errCode);
if (!plugin)
continue;
if (!plugin->versionCheck(kt::VERSION_STRING))
{
Out(SYS_GEN|LOG_NOTICE) <<
TQString("Plugin %1 version does not match KTorrent version, unloading it.")
.arg(service->library()) << endl;
delete plugin;
// unload the library again, no need to have it loaded
KLibLoader::self()->unloadLibrary(service->library().local8Bit());
continue;
}
unloaded.insert(plugin->getName(),plugin);
if (pltoload.contains(plugin->getName()))
load(plugin->getName());
}
if (!prefpage)
{
prefpage = new PluginManagerPrefPage(this);
gui->addPrefPage(prefpage);
}
prefpage->updatePluginList();
}
void PluginManager::load(const TQString & name)
{
Plugin* p = unloaded.find(name);
if (!p)
return;
Out(SYS_GEN|LOG_NOTICE) << "Loading plugin "<< p->getName() << endl;
p->setCore(core);
p->setGUI(gui);
p->load();
gui->mergePluginGui(p);
unloaded.erase(name);
plugins.insert(p->getName(),p);
p->loaded = true;
if (!cfg_file.isNull())
saveConfigFile(cfg_file);
}
void PluginManager::unload(const TQString & name)
{
Plugin* p = plugins.find(name);
if (!p)
return;
// first shut it down properly
bt::WaitJob* wjob = new WaitJob(2000);
try
{
p->shutdown(wjob);
if (wjob->needToWait())
bt::WaitJob::execute(wjob);
else
delete wjob;
}
catch (Error & err)
{
Out(SYS_GEN|LOG_NOTICE) << "Error when unloading plugin: " << err.toString() << endl;
}
gui->removePluginGui(p);
p->unload();
plugins.erase(name);
unloaded.insert(p->getName(),p);
p->loaded = false;
if (!cfg_file.isNull())
saveConfigFile(cfg_file);
}
void PluginManager::loadAll()
{
bt::PtrMap<TQString,Plugin>::iterator i = unloaded.begin();
while (i != unloaded.end())
{
Plugin* p = i->second;
p->setCore(core);
p->setGUI(gui);
p->load();
gui->mergePluginGui(p);
plugins.insert(p->getName(),p);
p->loaded = true;
i++;
}
unloaded.clear();
if (!cfg_file.isNull())
saveConfigFile(cfg_file);
}
void PluginManager::unloadAll(bool save)
{
// first properly shutdown all plugins
bt::WaitJob* wjob = new WaitJob(2000);
try
{
bt::PtrMap<TQString,Plugin>::iterator i = plugins.begin();
while (i != plugins.end())
{
Plugin* p = i->second;
p->shutdown(wjob);
i++;
}
if (wjob->needToWait())
bt::WaitJob::execute(wjob);
else
delete wjob;
}
catch (Error & err)
{
Out(SYS_GEN|LOG_NOTICE) << "Error when unloading all plugins: " << err.toString() << endl;
}
// then unload them
bt::PtrMap<TQString,Plugin>::iterator i = plugins.begin();
while (i != plugins.end())
{
Plugin* p = i->second;
gui->removePluginGui(p);
p->unload();
unloaded.insert(p->getName(),p);
p->loaded = false;
i++;
}
plugins.clear();
if (save && !cfg_file.isNull())
saveConfigFile(cfg_file);
}
void PluginManager::updateGuiPlugins()
{
bt::PtrMap<TQString,Plugin>::iterator i = plugins.begin();
while (i != plugins.end())
{
Plugin* p = i->second;
p->guiUpdate();
i++;
}
}
void PluginManager::fillPluginList(TQPtrList<Plugin> & plist)
{
bt::PtrMap<TQString,Plugin>::iterator i = plugins.begin();
while (i != plugins.end())
{
Plugin* p = i->second;
plist.append(p);
i++;
}
i = unloaded.begin();
while (i != unloaded.end())
{
Plugin* p = i->second;
plist.append(p);
i++;
}
}
bool PluginManager::isLoaded(const TQString & name) const
{
const Plugin* p = plugins.find(name);
return p != 0;
}
void PluginManager::loadConfigFile(const TQString & file)
{
cfg_file = file;
// make a default config file if doesn't exist
if (!bt::Exists(file))
{
writeDefaultConfigFile(file);
return;
}
TQFile f(file);
if (!f.open(IO_ReadOnly))
{
Out(SYS_GEN|LOG_DEBUG) << "Cannot open file " << file << " : " << TQString(f.errorString()) << endl;
return;
}
pltoload.clear();
TQTextStream in(&f);
while (!in.atEnd())
{
TQString l = in.readLine();
if (l.isNull())
break;
pltoload.append(l);
}
}
void PluginManager::saveConfigFile(const TQString & file)
{
cfg_file = file;
TQFile f(file);
if (!f.open(IO_WriteOnly))
{
Out(SYS_GEN|LOG_DEBUG) << "Cannot open file " << file << " : " << TQString(f.errorString()) << endl;
return;
}
TQTextStream out(&f);
bt::PtrMap<TQString,Plugin>::iterator i = plugins.begin();
while (i != plugins.end())
{
Plugin* p = i->second;
out << p->getName() << endl;
i++;
}
}
void PluginManager::writeDefaultConfigFile(const TQString & file)
{
// by default we will load the infowidget and searchplugin
TQFile f(file);
if (!f.open(IO_WriteOnly))
{
Out(SYS_GEN|LOG_DEBUG) << "Cannot open file " << file << " : " << TQString(f.errorString()) << endl;
return;
}
TQTextStream out(&f);
out << "Info Widget" << endl << "Search" << endl;
pltoload.clear();
pltoload.append("Info Widget");
pltoload.append("Search");
}
}
|