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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/***************************************************************************
scripttreeview.cpp - description
-------------------
begin : Thu Sep 16 2003
copyright : (C) 2003-2004 by Andras Mantia <[email protected]>
***************************************************************************/
/***************************************************************************
* *
* 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; version 2 of the License. *
* *
***************************************************************************/
//kde includes
#include <tdeapplication.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdepopupmenu.h>
#include <kprocess.h>
#include <krun.h>
#include <kstandarddirs.h>
#include <ktar.h>
#include <ktempdir.h>
#include <tdetempfile.h>
#include <kurl.h>
#include <kdebug.h>
//qt includes
#include <tqdir.h>
#include <tqdom.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqlabel.h>
#include <tqlineedit.h>
#include <tqregexp.h>
#include <tqstringlist.h>
#include <tqtextedit.h>
//other includes
#include <libxml/xmlmemory.h>
#include <libxml/debugXML.h>
#include <libxml/HTMLtree.h>
#include <libxml/xmlIO.h>
#include <libxml/xinclude.h>
#include <libxml/catalog.h>
#include <libxslt/xslt.h>
#include <libxslt/xsltInternals.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
//app includes
#include "scripttreeview.h"
#include "resource.h"
#include "quantacommon.h"
#include "tagmaildlg.h"
extern int xmlLoadExtDtdDefaultValue;
ScriptTreeView::ScriptTreeView(TQWidget *parent, const char *name )
: BaseTreeView(parent,name)
{
setSaveOpenFolder(true);
addColumn(i18n("Scripts"), -1);
addColumn("");
KURL url;
url.setPath(qConfig.globalDataDir + resourceDir + "scripts/");
BaseTreeBranch *m_globalDir;
m_globalDir = new BaseTreeBranch(this, url, i18n("Global Scripts"), SmallIcon("run"), true);
addBranch(m_globalDir);
url.setPath(locateLocal("data", resourceDir + "scripts/"));
BaseTreeBranch *m_localDir;
m_localDir = new BaseTreeBranch(this, url, i18n("Local Scripts"), SmallIcon("run"), true);
addBranch(m_localDir);
// here you define which files should not be visible for the users
const TQString excludeString = ".*\\.info$|.*\\.css$|.*\\.xsl$";
m_globalDir->excludeFilterRx.setPattern(excludeString);
m_localDir->excludeFilterRx.setPattern(excludeString);
m_fileMenu = new TDEPopupMenu(this);
m_fileMenu->insertItem(SmallIcon("info"), i18n("&Description"), this, TQT_SLOT(slotProperties()));
m_fileMenu->insertItem(SmallIcon("run"), i18n("&Run Script"), this, TQT_SLOT(slotRun()));
m_fileMenu->insertSeparator();
m_fileMenu->insertItem(i18n("&Edit Script"), this, TQT_SLOT(slotEditScript()));
m_fileMenu->insertItem(i18n("Edit in &Quanta"), this, TQT_SLOT(slotEditInQuanta()));
m_fileMenu->insertItem(i18n("Edi&t Description"), this, TQT_SLOT(slotEditDescription()));
m_fileMenu->insertSeparator();
m_fileMenu->insertItem(UserIcon("ball"), i18n("&Assign Action"), this, TQT_SLOT(slotAssignAction()));
m_fileMenu->insertItem(SmallIcon("mail_send"), i18n("&Send in Email..."), this, TQT_SLOT(slotSendScriptInMail()));
m_fileMenu->insertItem(SmallIcon("network"), i18n("&Upload Script..."), this, TQT_SLOT(slotUploadScript()));
m_folderMenu = new TDEPopupMenu(this);
m_downloadMenuId = m_folderMenu->insertItem(SmallIcon("network"), i18n("&Download Script..."), this, TQT_SIGNAL(downloadScript()));
connect(this, TQT_SIGNAL(contextMenu(TDEListView*, TQListViewItem*, const TQPoint&)),
this, TQT_SLOT(slotMenu(TDEListView*, TQListViewItem*, const TQPoint&)));
restoreLayout( kapp->config(), className() );
// the restored size of the first column might be too large for the current content
// we set it to 10 and the listview will adjust it to the size of the largest entry
setColumnWidth(0, 10);
}
ScriptTreeView::~ScriptTreeView()
{
}
void ScriptTreeView::slotMenu(TDEListView *, TQListViewItem *item, const TQPoint &point)
{
if (!item)
{
m_folderMenu->setItemVisible(m_downloadMenuId, true);
m_folderMenu->popup(point);
return;
}
setSelected(item, true);
KFileTreeViewItem *curItem = currentKFileTreeViewItem();
if (!curItem->isDir())
{
m_fileMenu->popup(point);
} else
{
if (curItem == curItem->branch()->root())
{
m_folderMenu->setItemVisible(m_downloadMenuId, true);
} else
{
m_folderMenu->setItemVisible(m_downloadMenuId, false);
}
m_folderMenu->popup(point);
}
}
void ScriptTreeView::slotSelectFile(TQListViewItem *item)
{
if (item) {
if ( !currentKFileTreeViewItem()->isDir() )
{
KURL urlToOpen = infoFile(currentURL(), true);
emit openFileInPreview(urlToOpen);
}
}
}
void ScriptTreeView::slotEditDescription()
{
if ( !currentKFileTreeViewItem()->isDir() )
{
KURL urlToOpen = infoFile(currentURL());
emit showPreviewWidget(false);
emit openFile(urlToOpen);
}
}
void ScriptTreeView::slotEditScript()
{
if ( !currentKFileTreeViewItem()->isDir() )
{
emit showPreviewWidget(false);
KURL urlToOpen = currentURL();
KURL infoUrl = infoFile(urlToOpen);
TQString editApp = infoOptionValue(infoUrl, "editor");
if (editApp.isEmpty())
emit openFile(urlToOpen);
else
{
TDEProcess *proc = new TDEProcess();
*proc << editApp << urlToOpen.path();
proc->start(TDEProcess::DontCare);
}
}
}
void ScriptTreeView::slotRun()
{
if ( !currentKFileTreeViewItem()->isDir() )
{
KURL urlToOpen = currentURL();
KURL infoUrl = infoFile(urlToOpen);
TQString execApp = infoOptionValue(infoUrl, "interpreter");
if (execApp.isEmpty())
{
KURL::List list;
list.append(urlToOpen);
KRun::displayOpenWithDialog(list);
}
else
{
TDEProcess *proc = new TDEProcess();
TQStringList argsList = TQStringList::split(' ', execApp);
*proc << argsList;
*proc << urlToOpen.path();
proc->start(TDEProcess::DontCare);
}
}
}
void ScriptTreeView::slotEditInQuanta()
{
if ( !currentKFileTreeViewItem()->isDir() )
{
KURL urlToOpen = currentURL();
emit showPreviewWidget(false);
emit openFile(urlToOpen);
}
}
void ScriptTreeView::slotAssignAction()
{
if ( !currentKFileTreeViewItem()->isDir() )
{
KURL url = currentURL();
KURL infoURL = infoFile(url);
TQString execApp = infoOptionValue(infoURL, "interpreter");
if (execApp.isEmpty())
execApp = "sh";
url.setPath(url.path().replace(locateLocal("data", resourceDir + "scripts/"), "%scriptdir/"));
url.setPath(url.path().replace(qConfig.globalDataDir + resourceDir + "scripts/", "%scriptdir/"));
emit assignActionToScript(url, execApp);
}
}
TQString ScriptTreeView::createScriptTarball()
{
KURL url = currentURL();
KURL infoURL = infoFile(url);
KTempDir* tempDir = new KTempDir(tmpDir);
tempDir->setAutoDelete(true);
tempDirList.append(tempDir);
TQString tempFileName=tempDir->name() + url.fileName() + ".tgz";
//pack the .tag files and the description.rc into a .tgz file
KTar tar(tempFileName, "application/x-gzip");
tar.open(IO_WriteOnly);
KURL::List files;
files.append(url);
files.append(infoURL);
files.append(KURL().fromPathOrURL(qConfig.globalDataDir + resourceDir + "scripts/info.xsl"));
for ( KURL::List::Iterator it_f = files.begin(); it_f != files.end(); ++it_f )
{
TQFile file((*it_f).path());
file.open(IO_ReadOnly);
TQByteArray bArray = file.readAll();
tar.writeFile((*it_f).fileName(), "user", "group", bArray.size(), bArray.data());
file.close();
}
tar.close();
return tempFileName;
}
void ScriptTreeView::slotSendScriptInMail()
{
if ( !currentKFileTreeViewItem()->isDir() )
{
TQStringList attachmentFile;
attachmentFile += createScriptTarball();
TagMailDlg *mailDlg = new TagMailDlg( this, i18n("Send script in email").ascii() );
TQString toStr;
TQString message = i18n("Hi,\n This is a Quanta Plus [http://quanta.kdewebdev.org] script tarball.\n\nHave fun.\n");
TQString titleStr;
TQString subjectStr;
mailDlg->TitleLabel->setText(i18n("Content:"));
/* mailDlg->titleEdit->setFixedHeight(60);
mailDlg->titleEdit->setVScrollBarMode(TQTextEdit::Auto);
mailDlg->titleEdit->setHScrollBarMode(TQTextEdit::Auto);*/
if ( mailDlg->exec() )
{
if ( !mailDlg->lineEmail->text().isEmpty())
{
toStr = mailDlg->lineEmail->text();
subjectStr = (mailDlg->lineSubject->text().isEmpty())?i18n("Quanta Plus Script"):mailDlg->lineSubject->text();
if ( !mailDlg->titleEdit->text().isEmpty())
message = mailDlg->titleEdit->text();
} else
{
KMessageBox::error(this,i18n("No destination address was specified.\n Sending is aborted."),i18n("Error Sending Email"));
delete mailDlg;
return;
}
kapp->invokeMailer(toStr, TQString(), TQString(), subjectStr, message, TQString(), attachmentFile);
}
delete mailDlg;
}
}
void ScriptTreeView::slotUploadScript()
{
if ( !currentKFileTreeViewItem()->isDir() )
{
TQString fileName = createScriptTarball();
emit uploadScript(fileName);
}
}
KURL ScriptTreeView::infoFile(const KURL& url, bool htmlVersion)
{
KURL returnUrl = url;
TQString fileName = returnUrl.fileName();
//fileName.truncate(fileName.length() - TQFileInfo(fileName).extension().length() - 1);
fileName.append(".info");
returnUrl.setFileName(fileName);
if (!TQFileInfo(returnUrl.path()).exists())
{
TQFile f(returnUrl.path());
if (f.open(IO_WriteOnly))
{
TQTextStream str(&f);
str.setEncoding(TQTextStream::UnicodeUTF8);
str << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl;
str << "<?xml-stylesheet type=\"text/xsl\" href=\"info.xsl\" ?>" << endl;
str << "<!DOCTYPE QuantaScriptInfo>" << endl;
str << "<INFO>" << endl;
str << " <options editor=\"\" interpreter=\"\" />" << endl;
str << " <name>" << url.fileName() << "</name>" << endl;
str << " <author></author>" << endl;
str << " <email></email>" << endl;
str << " <website></website>" << endl;
str << " <version></version>" << endl;
str << " <license></license>" << endl;
str << " <about></about>" << endl;
str << "</INFO>" << endl;
f.close();
}
}
if (htmlVersion)
{
KTempFile *tempInfoFile = 0L;
if (!TQFileInfo(returnUrl.path()).exists())
{
tempInfoFile = new KTempFile(tmpDir);
tempInfoFile->setAutoDelete(true);
returnUrl = KURL::fromPathOrURL(tempInfoFile->name());
TQTextStream str(tempInfoFile->file());
str.setEncoding(TQTextStream::UnicodeUTF8);
str << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl;
str << "<?xml-stylesheet type=\"text/xsl\" href=\"info.xsl\" ?>" << endl;
str << "<!DOCTYPE QuantaScriptInfo>" << endl;
str << "<INFO>" << endl;
str << " <options editor=\"\" interpreter=\"\" />" << endl;
str << " <name>" << url.fileName() << "</name>" << endl;
str << " <author></author>" << endl;
str << " <email></email>" << endl;
str << " <website></website>" << endl;
str << " <version></version>" << endl;
str << " <license></license>" << endl;
str << " <about></about>" << endl;
str << "</INFO>" << endl;
tempInfoFile->close();
tempFileList.append(tempInfoFile);
}
KTempFile *tempFile = new KTempFile(tmpDir);
tempFile->setAutoDelete(true);
//apply the stylesheet
xsltStylesheetPtr cur = NULL;
xmlDocPtr doc, res;
xmlSubstituteEntitiesDefault(1);
xmlLoadExtDtdDefaultValue = 1;
TQString xslFile = qConfig.globalDataDir + resourceDir + "scripts/info.xsl";
cur = xsltParseStylesheetFile(xmlCharStrndup(xslFile.utf8(), xslFile.utf8().length()));
doc = xmlParseFile(returnUrl.path().utf8());
res = xsltApplyStylesheet(cur, doc, 0);
xsltSaveResultToFile(tempFile->fstream(), res, cur);
xsltFreeStylesheet(cur);
xmlFreeDoc(res);
xmlFreeDoc(doc);
xsltCleanupGlobals();
xmlCleanupParser();
tempFile->close();
tempFileList.append(tempFile);
return KURL().fromPathOrURL(tempFile->name());
} else
return returnUrl;
}
TQString ScriptTreeView::infoOptionValue(const KURL& infoURL, const TQString& optionName)
{
TQString value;
TQFile f(infoURL.path());
if (f.open(IO_ReadOnly))
{
TQDomDocument doc;
doc.setContent(&f);
f.close();
TQDomNodeList nodes = doc.elementsByTagName("options");
if (nodes.count() > 0)
{
TQDomElement el = nodes.item(0).toElement();
value = el.attribute(optionName);
}
}
return value;
}
void ScriptTreeView::slotProperties()
{
KFileTreeViewItem *item = currentKFileTreeViewItem();
if (item)
slotSelectFile(item);
}
#include "scripttreeview.moc"
|