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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/***************************************************************************
copyright : (C) 2006 by Robby Stephenson
email : robby@periapsis.org
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of version 2 of the GNU General Public License as *
* published by the Free Software Foundation; *
* *
***************************************************************************/
#include "manager.h"
#include "newscript.h"
#include "../filehandler.h"
#include "../tellico_debug.h"
#include "../tellico_utils.h"
#include "../tellico_kernel.h"
#include "../fetch/fetch.h"
#include <kurl.h>
#include <ktar.h>
#include <tdeglobal.h>
#include <tdeio/netaccess.h>
#include <tdeconfig.h>
#include <tdetempfile.h>
#include <tdeio/job.h>
#include <tdefileitem.h>
#include <tdeversion.h>
#include <knewstuff/entry.h>
#include <kstandarddirs.h>
#include <tqfileinfo.h>
#include <tqdir.h>
#include <tqptrstack.h>
#include <tqvaluestack.h>
#include <tqwidget.h>
#include <sys/types.h>
#include <sys/stat.h>
using Tellico::NewStuff::Manager;
Manager::Manager(TQObject* parent_) : TQObject(parent_), m_tempFile(0) {
m_infoList.setAutoDelete(true);
}
Manager::~Manager() {
delete m_tempFile;
m_tempFile = 0;
}
bool Manager::installTemplate(const KURL& url_, const TQString& entryName_) {
FileHandler::FileRef* ref = FileHandler::fileRef(url_);
TQString xslFile;
TQStringList allFiles;
bool success = true;
// is there a better way to figure out if the url points to a XSL file or a tar archive
// than just trying to open it?
KTar archive(ref->fileName());
if(archive.open(IO_ReadOnly)) {
const KArchiveDirectory* archiveDir = archive.directory();
archiveDir->copyTo(Tellico::saveLocation(TQString::fromLatin1("entry-templates/")));
allFiles = archiveFiles(archiveDir);
// remember files installed for template
xslFile = findXSL(archiveDir);
} else { // assume it's an xsl file
TQString name = entryName_;
if(name.isEmpty()) {
name = url_.fileName();
}
if(!name.endsWith(TQString::fromLatin1(".xsl"))) {
name += TQString::fromLatin1(".xsl");
}
KURL dest;
dest.setPath(Tellico::saveLocation(TQString::fromLatin1("entry-templates/")) + name);
success = true;
if(TQFile::exists(dest.path())) {
myDebug() << "Manager::installTemplate() - " << dest.path() << " exists!" << endl;
success = false;
} else if(TDEIO::NetAccess::file_copy(url_, dest)) {
xslFile = dest.fileName();
allFiles += xslFile;
}
}
if(xslFile.isEmpty()) {
success = false;
} else {
// remove ".xsl"
xslFile.truncate(xslFile.length()-4);
TDEConfigGroup config(TDEGlobal::config(), "KNewStuffFiles");
config.writeEntry(xslFile, allFiles);
m_urlNameMap.insert(url_, xslFile);
}
checkCommonFile();
delete ref;
return success;
}
bool Manager::removeTemplate(const TQString& name_) {
TDEConfigGroup fileGroup(TDEGlobal::config(), "KNewStuffFiles");
TQStringList files = fileGroup.readListEntry(name_);
// at least, delete xsl file
if(files.isEmpty()) {
kdWarning() << "Manager::deleteTemplate() no file list found for " << name_ << endl;
files += name_;
}
bool success = true;
TQString path = Tellico::saveLocation(TQString::fromLatin1("entry-templates/"));
for(TQStringList::ConstIterator it = files.begin(); it != files.end(); ++it) {
if((*it).endsWith(TQChar('/'))) {
// ok to not delete all directories
TQDir().rmdir(path + *it);
} else {
success = success && TQFile(path + *it).remove();
if(!success) {
myDebug() << "Manager::removeTemplate() - failed to remove " << (path+*it) << endl;
}
}
}
// remove config entries even if unsuccessful
fileGroup.deleteEntry(name_);
TDEConfigGroup statusGroup(TDEGlobal::config(), "KNewStuffStatus");
TQString entryName = statusGroup.readEntry(name_);
statusGroup.deleteEntry(name_);
statusGroup.deleteEntry(entryName);
return success;
}
bool Manager::installScript(const KURL& url_) {
FileHandler::FileRef* ref = FileHandler::fileRef(url_);
KTar archive(ref->fileName());
if(!archive.open(IO_ReadOnly)) {
myDebug() << "Manager::installScript() - can't open tar file" << endl;
return false;
}
const KArchiveDirectory* archiveDir = archive.directory();
TQString exeFile = findEXE(archiveDir);
if(exeFile.isEmpty()) {
myDebug() << "Manager::installScript() - no exe file found" << endl;
return false;
}
TQFileInfo exeInfo(exeFile);
DataSourceInfo* info = new DataSourceInfo();
TQString copyTarget = Tellico::saveLocation(TQString::fromLatin1("data-sources/"));
TQString scriptFolder;
// package could have a top-level directory or not
// it should have a directory...
const KArchiveEntry* firstEntry = archiveDir->entry(archiveDir->entries().first());
if(firstEntry->isFile()) {
copyTarget += exeInfo.baseName(true) + '/';
if(TQFile::exists(copyTarget)) {
KURL u;
u.setPath(scriptFolder);
myLog() << "Manager::installScript() - deleting " << scriptFolder << endl;
TDEIO::NetAccess::del(u, Kernel::self()->widget());
info->isUpdate = true;
}
scriptFolder = copyTarget;
} else {
scriptFolder = copyTarget + firstEntry->name() + '/';
if(TQFile::exists(copyTarget + exeFile)) {
info->isUpdate = true;
}
}
// overwrites stuff there
archiveDir->copyTo(copyTarget);
info->specFile = scriptFolder + exeInfo.baseName(true) + ".spec";
if(!TQFile::exists(info->specFile)) {
myDebug() << "Manager::installScript() - no spec file for script! " << info->specFile << endl;
delete info;
return false;
}
info->sourceName = exeFile;
info->sourceExec = copyTarget + exeFile;
m_infoList.append(info);
KURL dest;
dest.setPath(info->sourceExec);
KFileItem item(KFileItem::Unknown, KFileItem::Unknown, dest, true);
::chmod(TQFile::encodeName(dest.path()), item.permissions() | S_IXUSR);
{
TDEConfig spec(info->specFile, false, false);
// update name
info->sourceName = spec.readEntry("Name", info->sourceName);
spec.writePathEntry("ExecPath", info->sourceExec);
spec.writeEntry("NewStuffName", info->sourceName);
spec.writeEntry("DeleteOnRemove", true);
}
{
TDEConfigGroup config(TDEGlobal::config(), "KNewStuffFiles");
config.writeEntry(info->sourceName, archiveFiles(archiveDir));
m_urlNameMap.insert(url_, info->sourceName);
}
// myDebug() << "Manager::installScript() - exeFile = " << exeFile << endl;
// myDebug() << "Manager::installScript() - sourceExec = " << info->sourceExec << endl;
// myDebug() << "Manager::installScript() - sourceName = " << info->sourceName << endl;
// myDebug() << "Manager::installScript() - specFile = " << info->specFile << endl;
delete ref;
return true;
}
bool Manager::removeScript(const TQString& name_) {
TDEConfigGroup fileGroup(TDEGlobal::config(), "KNewStuffFiles");
TQStringList files = fileGroup.readListEntry(name_);
bool success = true;
TQString path = Tellico::saveLocation(TQString::fromLatin1("data-sources/"));
for(TQStringList::ConstIterator it = files.begin(); it != files.end(); ++it) {
if((*it).endsWith(TQChar('/'))) {
// ok to not delete all directories
TQDir().rmdir(path + *it);
} else {
success = success && TQFile(path + *it).remove();
if(!success) {
myDebug() << "Manager::removeScript() - failed to remove " << (path+*it) << endl;
}
}
}
// remove config entries even if unsuccessful
fileGroup.deleteEntry(name_);
TDEConfigGroup statusGroup(TDEGlobal::config(), "KNewStuffStatus");
TQString entryName = statusGroup.readEntry(name_);
statusGroup.deleteEntry(name_);
statusGroup.deleteEntry(entryName);
return success;
}
Tellico::NewStuff::InstallStatus Manager::installStatus(KNS::Entry* entry_) {
TDEConfigGroup config(TDEGlobal::config(), "KNewStuffStatus");
TQString datestring = config.readEntry(entry_->name());
if(datestring.isEmpty()) {
return NotInstalled;
}
TQDate date = TQDate::fromString(datestring, Qt::ISODate);
if(!date.isValid()) {
return NotInstalled;
}
if(date < entry_->releaseDate()) {
return OldVersion;
}
// also check that executable files exists
TDEConfigGroup fileGroup(TDEGlobal::config(), "KNewStuffFiles");
TQStringList files = fileGroup.readListEntry(entry_->name());
TQString path = Tellico::saveLocation(TQString::fromLatin1("data-sources/"));
for(TQStringList::ConstIterator it = files.begin(); it != files.end(); ++it) {
if(!TQFile::exists(path + *it)) {
return NotInstalled;
}
}
return Current;
}
TQStringList Manager::archiveFiles(const KArchiveDirectory* dir_, const TQString& path_) {
TQStringList list;
const TQStringList dirEntries = dir_->entries();
for(TQStringList::ConstIterator it = dirEntries.begin(); it != dirEntries.end(); ++it) {
const TQString& entry = *it;
const KArchiveEntry* curEntry = dir_->entry(entry);
if(curEntry->isFile()) {
list += path_ + entry;
} else if(curEntry->isDirectory()) {
list += archiveFiles(static_cast<const KArchiveDirectory*>(curEntry), path_ + entry + '/');
// add directory AFTER contents, since we delete from the top down later
list += path_ + entry + '/';
}
}
return list;
}
// don't recurse, the .xsl must be in top directory
TQString Manager::findXSL(const KArchiveDirectory* dir_) {
const TQStringList entries = dir_->entries();
for(TQStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it) {
const TQString& entry = *it;
if(entry.endsWith(TQString::fromLatin1(".xsl"))) {
return entry;
}
}
return TQString();
}
TQString Manager::findEXE(const KArchiveDirectory* dir_) {
TQPtrStack<KArchiveDirectory> dirStack;
TQValueStack<TQString> dirNameStack;
dirStack.push(dir_);
dirNameStack.push(TQString());
do {
const TQString dirName = dirNameStack.pop();
const KArchiveDirectory* curDir = dirStack.pop();
const TQStringList entries = curDir->entries();
for(TQStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it) {
const TQString& entry = *it;
const KArchiveEntry* archEntry = curDir->entry(entry);
if(archEntry->isFile() && (archEntry->permissions() & S_IEXEC)) {
return dirName + entry;
} else if(archEntry->isDirectory()) {
dirStack.push(static_cast<const KArchiveDirectory*>(archEntry));
dirNameStack.push(dirName + entry + '/');
}
}
} while(!dirStack.isEmpty());
return TQString();
}
void Manager::install(DataType type_, KNS::Entry* entry_) {
if(m_tempFile) {
delete m_tempFile;
}
m_tempFile = new KTempFile();
m_tempFile->setAutoDelete(true);
KURL destination;
destination.setPath(m_tempFile->name());
TDEIO::FileCopyJob* job = TDEIO::file_copy(entry_->payload(), destination, -1, true);
connect(job, TQT_SIGNAL(result(TDEIO::Job*)), TQT_SLOT(slotDownloadJobResult(TDEIO::Job*)));
m_jobMap.insert(job, EntryPair(entry_, type_));
}
void Manager::slotDownloadJobResult(TDEIO::Job* job_) {
TDEIO::FileCopyJob* job = static_cast<TDEIO::FileCopyJob*>(job_);
if(job->error()) {
GUI::CursorSaver cs(TQt::arrowCursor);
delete m_tempFile;
m_tempFile = 0;
job->showErrorDialog(Kernel::self()->widget());
emit signalInstalled(0); // still need to notify dialog that install failed
return;
}
KNS::Entry* entry = m_jobMap[job_].first;
DataType type = m_jobMap[job_].second;
bool deleteTempFile = true;
if(type == EntryTemplate) {
installTemplate(job->destURL(), entry->name());
} else {
#if KDE_IS_VERSION(3,3,90)
// needed so the GPG signature can be checked
NewScript* newScript = new NewScript(this, Kernel::self()->widget());
connect(newScript, TQT_SIGNAL(installFinished()), this, TQT_SLOT(slotInstallFinished()));
// need to delete temp file if install was not a success
// if it was a success, it gets deleted later
deleteTempFile = !newScript->install(job->destURL().path());
m_scriptEntryMap.insert(newScript, entry);
#endif
// if failed, emit empty signal now
if(deleteTempFile) {
emit signalInstalled(0);
}
}
if(deleteTempFile) {
delete m_tempFile;
m_tempFile = 0;
}
}
void Manager::slotInstallFinished() {
const NewScript* newScript = ::tqqt_cast<const NewScript*>(sender());
if(newScript && newScript->successfulInstall()) {
const TQString name = m_urlNameMap[newScript->url()];
KNS::Entry* entry = m_scriptEntryMap[newScript];
TDEConfigGroup config(TDEGlobal::config(), "KNewStuffStatus");
// have to keep a config entry that maps the name of the file to the name
// of the newstuff entry, so we can track which entries are installed
// name and entry-name() are probably the same for scripts, but not a problem
config.writeEntry(name, entry->name());
config.writeEntry(entry->name(), TQString(entry->releaseDate().toString(Qt::ISODate)));
config.sync();
emit signalInstalled(entry);
} else {
emit signalInstalled(0);
kdWarning() << "Manager::slotInstallFinished() - Failed to install" << endl;
}
delete m_tempFile;
m_tempFile = 0;
}
bool Manager::checkCommonFile() {
// look for a file that gets installed to know the installation directory
// need to check timestamps
TQString userDataDir = Tellico::saveLocation(TQString());
TQString userCommonFile = userDataDir + '/' + TQString::fromLatin1("tellico-common.xsl");
if(TQFile::exists(userCommonFile)) {
// check timestamps
// pics/tellico.png is not likely to be in a user directory
TQString installDir = TDEGlobal::dirs()->findResourceDir("appdata", TQString::fromLatin1("pics/tellico.png"));
TQString installCommonFile = installDir + '/' + TQString::fromLatin1("tellico-common.xsl");
#ifndef NDEBUG
if(userCommonFile == installCommonFile) {
kdWarning() << "Manager::checkCommonFile() - install location is same as user location" << endl;
}
#endif
TQFileInfo installInfo(installCommonFile);
TQFileInfo userInfo(userCommonFile);
if(installInfo.lastModified() > userInfo.lastModified()) {
// the installed file has been modified more recently than the user's
// remove user's tellico-common.xsl file so it gets copied again
myLog() << "Manager::checkCommonFile() - removing " << userCommonFile << endl;
myLog() << "Manager::checkCommonFile() - copying " << installCommonFile << endl;
TQFile::remove(userCommonFile);
} else {
return true;
}
}
KURL src, dest;
src.setPath(TDEGlobal::dirs()->findResource("appdata", TQString::fromLatin1("tellico-common.xsl")));
dest.setPath(userCommonFile);
return TDEIO::NetAccess::file_copy(src, dest);
}
#include "manager.moc"
|