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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/*
* This file is part of the KDE project
*
* Copyright (c) 2005 Cyrille Berger <cberger@cberger.net>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library 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 "wdgscriptsmanager.h"
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqheader.h>
#include <tqobjectlist.h>
#include <tqtooltip.h>
#include <kapplication.h>
#include <tdeversion.h>
#include <kfiledialog.h>
#include <kiconloader.h>
#include <klistview.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpushbutton.h>
#include <kstandarddirs.h>
#include <ktoolbar.h>
#if KDE_IS_VERSION(3, 4, 0)
// The KNewStuffSecure we use internaly for the GetHotNewStuff-functionality
// was introduced with KDE 3.4.
#define KROSS_SUPPORT_NEWSTUFF
#endif
#ifdef KROSS_SUPPORT_NEWSTUFF
#include <knewstuff/provider.h>
#include <knewstuff/engine.h>
#include <knewstuff/downloaddialog.h>
#include <knewstuff/knewstuffsecure.h>
#endif
#include "scriptguiclient.h"
#include "scriptaction.h"
namespace Kross { namespace Api {
#ifdef KROSS_SUPPORT_NEWSTUFF
class ScriptNewStuff : public KNewStuffSecure
{
public:
ScriptNewStuff(ScriptGUIClient* scripguiclient, const TQString& type, TQWidget *parentWidget = 0)
: KNewStuffSecure(type, parentWidget)
, m_scripguiclient(scripguiclient) {}
virtual ~ScriptNewStuff() {}
private:
ScriptGUIClient* m_scripguiclient;
virtual void installResource() { m_scripguiclient->installScriptPackage( m_tarName ); }
};
#endif
class ListItem : public TQListViewItem
{
private:
ScriptActionCollection* m_collection;
ScriptAction::Ptr m_action;
public:
ListItem(TQListView* parentview, ScriptActionCollection* collection)
: TQListViewItem(parentview), m_collection(collection), m_action(0) {}
ListItem(ListItem* parentitem, TQListViewItem* afteritem, ScriptAction::Ptr action)
: TQListViewItem(parentitem, afteritem), m_collection( parentitem->collection() ), m_action(action) {}
ScriptAction::Ptr action() const { return m_action; }
ScriptActionCollection* collection() const { return m_collection; }
//ScriptActionMenu* actionMenu() const { return m_menu; }
};
class ToolTip : public TQToolTip
{
public:
ToolTip(KListView* parent) : TQToolTip(parent->viewport()), m_parent(parent) {}
virtual ~ToolTip () { remove(m_parent->viewport()); }
protected:
virtual void maybeTip(const TQPoint& p) {
ListItem* item = dynamic_cast<ListItem*>( m_parent->itemAt(p) );
if(item) {
TQRect r( m_parent->itemRect(item) );
if(r.isValid() && item->action()) {
tip(r, TQString("<qt>%1</qt>").arg(item->action()->toolTip()));
}
}
}
private:
KListView* m_parent;
};
class WdgScriptsManagerPrivate
{
friend class WdgScriptsManager;
ScriptGUIClient* m_scripguiclient;
ToolTip* m_tooltip;
#ifdef KROSS_SUPPORT_NEWSTUFF
ScriptNewStuff* newstuff;
#endif
//enum { LoadBtn = 0, UnloadBtn, InstallBtn, UninstallBtn, ExecBtn, NewStuffBtn };
};
WdgScriptsManager::WdgScriptsManager(ScriptGUIClient* scr, TQWidget* parent, const char* name, WFlags fl )
: WdgScriptsManagerBase(parent, name, fl)
, d(new WdgScriptsManagerPrivate)
{
d->m_scripguiclient = scr;
d->m_tooltip = new ToolTip(scriptsList);
#ifdef KROSS_SUPPORT_NEWSTUFF
d->newstuff = 0;
#endif
scriptsList->header()->hide();
//scriptsList->header()->setClickEnabled(false);
scriptsList->setAllColumnsShowFocus(true);
//scriptsList->setRootIsDecorated(true);
scriptsList->setSorting(-1);
scriptsList->addColumn("text");
//scriptsList->setColumnWidthMode(1, TQListView::Manual);
slotFillScriptsList();
slotSelectionChanged(0);
connect(scriptsList, TQT_SIGNAL(selectionChanged(TQListViewItem*)), this, TQT_SLOT(slotSelectionChanged(TQListViewItem*)));
btnExec->setIconSet(TDEGlobal::instance()->iconLoader()->loadIconSet( "exec", KIcon::MainToolbar, 16 ));
connect(btnExec, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotExecuteScript()));
btnLoad->setIconSet(TDEGlobal::instance()->iconLoader()->loadIconSet( "fileopen", KIcon::MainToolbar, 16 ));
connect(btnLoad, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotLoadScript()));
btnUnload->setIconSet(TDEGlobal::instance()->iconLoader()->loadIconSet( "fileclose", KIcon::MainToolbar, 16 ));
connect(btnUnload, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotUnloadScript()));
btnInstall->setIconSet(TDEGlobal::instance()->iconLoader()->loadIconSet( "fileimport", KIcon::MainToolbar, 16 ));
connect(btnInstall, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotInstallScript()));
btnUninstall->setIconSet(TDEGlobal::instance()->iconLoader()->loadIconSet( "fileclose", KIcon::MainToolbar, 16 ));
connect(btnUninstall, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotUninstallScript()));
#ifdef KROSS_SUPPORT_NEWSTUFF
btnNewStuff->setIconSet(TDEGlobal::instance()->iconLoader()->loadIconSet( "knewstuff", KIcon::MainToolbar, 16 ));
connect(btnNewStuff, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotGetNewScript()));
#endif
/*
toolBar->setIconText( KToolBar::IconTextRight );
toolBar->insertButton("exec", WdgScriptsManagerPrivate::ExecBtn, false, i18n("Execute"));
toolBar->addConnection(WdgScriptsManagerPrivate::ExecBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotExecuteScript()));
toolBar->insertLineSeparator();
toolBar->insertButton("fileopen", WdgScriptsManagerPrivate::LoadBtn, true, i18n("Load"));
toolBar->addConnection(WdgScriptsManagerPrivate::LoadBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotLoadScript()));
toolBar->insertButton("fileclose", WdgScriptsManagerPrivate::UnloadBtn, false, i18n("Unload"));
toolBar->addConnection(WdgScriptsManagerPrivate::UnloadBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotUnloadScript()));
toolBar->insertLineSeparator();
toolBar->insertButton("fileimport", WdgScriptsManagerPrivate::InstallBtn, true, i18n("Install"));
toolBar->addConnection(WdgScriptsManagerPrivate::InstallBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotInstallScript()));
toolBar->insertButton("fileclose", WdgScriptsManagerPrivate::UninstallBtn, false, i18n("Uninstall"));
toolBar->addConnection(WdgScriptsManagerPrivate::UninstallBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotUninstallScript()));
#ifdef KROSS_SUPPORT_NEWSTUFF
toolBar->insertLineSeparator();
toolBar->insertButton("knewstuff", WdgScriptsManagerPrivate::NewStuffBtn, true, i18n("Get More Scripts"));
toolBar->addConnection(WdgScriptsManagerPrivate::NewStuffBtn, TQT_SIGNAL(clicked()), this, TQT_SLOT(slotGetNewScript()));
#endif
*/
connect(scr, TQT_SIGNAL( collectionChanged(ScriptActionCollection*) ),
this, TQT_SLOT( slotFillScriptsList() ));
}
WdgScriptsManager::~WdgScriptsManager()
{
delete d->m_tooltip;
delete d;
}
void WdgScriptsManager::slotFillScriptsList()
{
scriptsList->clear();
addItem( d->m_scripguiclient->getActionCollection("executedscripts") );
addItem( d->m_scripguiclient->getActionCollection("loadedscripts") );
addItem( d->m_scripguiclient->getActionCollection("installedscripts") );
}
void WdgScriptsManager::addItem(ScriptActionCollection* collection)
{
if(! collection)
return;
ListItem* i = new ListItem(scriptsList, collection);
i->setText(0, collection->actionMenu()->text());
i->setOpen(true);
TQValueList<ScriptAction::Ptr> list = collection->actions();
TQListViewItem* lastitem = 0;
for(TQValueList<ScriptAction::Ptr>::Iterator it = list.begin(); it != list.end(); ++it)
lastitem = addItem(*it, i, lastitem);
}
TQListViewItem* WdgScriptsManager::addItem(ScriptAction::Ptr action, TQListViewItem* parentitem, TQListViewItem* afteritem)
{
if(! action)
return 0;
ListItem* i = new ListItem(dynamic_cast<ListItem*>(parentitem), afteritem, action);
i->setText(0, action->text()); // FIXME: i18nise it for ko2.0
//i->setText(1, action->getDescription()); // FIXME: i18nise it for ko2.0
//i->setText(2, action->name());
TQPixmap pm;
if(action->hasIcon()) {
KIconLoader* icons = TDEGlobal::iconLoader();
pm = icons->loadIconSet(action->icon(), KIcon::Small).pixmap(TQIconSet::Small, TQIconSet::Active);
}
else {
pm = action->iconSet(KIcon::Small, 16).pixmap(TQIconSet::Small, TQIconSet::Active);
}
if(! pm.isNull())
i->setPixmap(0, pm); // display the icon
return i;
}
void WdgScriptsManager::slotSelectionChanged(TQListViewItem* item)
{
ListItem* i = dynamic_cast<ListItem*>(item);
Kross::Api::ScriptActionCollection* installedcollection = d->m_scripguiclient->getActionCollection("installedscripts");
//toolBar->setItemEnabled(WdgScriptsManagerPrivate::ExecBtn, i && i->action());
//toolBar->setItemEnabled(WdgScriptsManagerPrivate::UninstallBtn, i && i->action() && i->collection() == installedcollection);
//toolBar->setItemEnabled(WdgScriptsManagerPrivate::UnloadBtn, i && i->action() && i->collection() != installedcollection);
btnExec->setEnabled(i && i->action());
btnUnload->setEnabled(i && i->action() && i->collection() != installedcollection);
btnUninstall->setEnabled(i && i->action() && i->collection() == installedcollection);
}
void WdgScriptsManager::slotLoadScript()
{
if(d->m_scripguiclient->loadScriptFile())
slotFillScriptsList();
}
void WdgScriptsManager::slotInstallScript()
{
KFileDialog* filedialog = new KFileDialog(
TQString(), // startdir
"*.tar.gz *.tgz *.bz2", // filter
this, // parent widget
"WdgScriptsManagerInstallFileDialog", // name
true // modal
);
filedialog->setCaption( i18n("Install Script Package") );
if(! filedialog->exec())
return;
if(! d->m_scripguiclient->installScriptPackage( filedialog->selectedURL().path() )) {
krosswarning("Failed to install scriptpackage");
return;
}
slotFillScriptsList();
}
void WdgScriptsManager::slotUninstallScript()
{
ListItem* item = dynamic_cast<ListItem*>( scriptsList->currentItem() );
if( !item || !item->action() )
return;
Kross::Api::ScriptActionCollection* installedcollection = d->m_scripguiclient->getActionCollection("installedscripts");
if( !item->collection() || item->collection() != installedcollection)
return;
const TQString packagepath = item->action()->getPackagePath();
if( !packagepath)
return;
if( KMessageBox::warningContinueCancel(0,
i18n("Uninstall the script package \"%1\" and delete the package's folder \"%2\"?")
.arg(item->action()->text()).arg(packagepath),
i18n("Uninstall")) != KMessageBox::Continue )
{
return;
}
if(! d->m_scripguiclient->uninstallScriptPackage(packagepath)) {
krosswarning("Failed to uninstall scriptpackage");
return;
}
slotFillScriptsList();
}
void WdgScriptsManager::slotExecuteScript()
{
ListItem* item = dynamic_cast<ListItem*>( scriptsList->currentItem() );
if(item && item->action())
item->action()->activate();
}
void WdgScriptsManager::slotUnloadScript()
{
ListItem* item = dynamic_cast<ListItem*>( scriptsList->currentItem() );
if(item && item->action()) {
item->collection()->detach( item->action() );
slotFillScriptsList();
}
}
void WdgScriptsManager::slotGetNewScript()
{
#ifdef KROSS_SUPPORT_NEWSTUFF
const TQString appname = TDEApplication::kApplication()->name();
const TQString type = TQString("%1/script").arg(appname);
if(! d->newstuff) {
d->newstuff = new ScriptNewStuff(d->m_scripguiclient, type);
connect(d->newstuff, TQT_SIGNAL(installFinished()), this, TQT_SLOT(slotResourceInstalled()));
}
KNS::Engine *engine = new KNS::Engine(d->newstuff, type, this);
KNS::DownloadDialog *d = new KNS::DownloadDialog( engine, this );
d->setType(type);
KNS::ProviderLoader *p = new KNS::ProviderLoader(this);
TQObject::connect(p, TQT_SIGNAL(providersLoaded(Provider::List*)),
d, TQT_SLOT(slotProviders(Provider::List*)));
p->load(type, TQString("http://download.kde.org/khotnewstuff/%1scripts-providers.xml").arg(appname));
d->exec();
#endif
}
void WdgScriptsManager::slotResourceInstalled()
{
// Delete KNewStuff's configuration entries. These entries reflect what has
// already been installed. As we cannot yet keep them in sync after uninstalling
// scripts, we deactivate the check marks entirely.
TDEGlobal::config()->deleteGroup("KNewStuffStatus");
}
}}
#include "wdgscriptsmanager.moc"
|