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 : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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 <kglobal.h>
#include <kio/netaccess.h>
#include <kconfig.h>
#include <ktempfile.h>
#include <kio/job.h>
#include <kfileitem.h>
#include <kdeversion.h>
#include <knewstuff/entry.h>
#include <kstandarddirs.h>
#include <qfileinfo.h>
#include <qdir.h>
#include <qptrstack.h>
#include <qvaluestack.h>
#include <qwidget.h>
#include <sys/types.h>
#include <sys/stat.h>
using Tellico::NewStuff::Manager;
Manager::Manager(QObject* parent_) : QObject(parent_), m_tempFile(0) {
m_infoList.setAutoDelete(true);
}
Manager::~Manager() {
delete m_tempFile;
m_tempFile = 0;
}
bool Manager::installTemplate(const KURL& url_, const QString& entryName_) {
FileHandler::FileRef* ref = FileHandler::fileRef(url_);
QString xslFile;
QStringList 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(QString::fromLatin1("entry-templates/")));
allFiles = archiveFiles(archiveDir);
// remember files installed for template
xslFile = findXSL(archiveDir);
} else { // assume it's an xsl file
QString name = entryName_;
if(name.isEmpty()) {
name = url_.fileName();
}
if(!name.endsWith(QString::fromLatin1(".xsl"))) {
name += QString::fromLatin1(".xsl");
}
KURL dest;
dest.setPath(Tellico::saveLocation(QString::fromLatin1("entry-templates/")) + name);
success = true;
if(QFile::exists(dest.path())) {
myDebug() << "Manager::installTemplate() - " << dest.path() << " exists!" << endl;
success = false;
} else if(KIO::NetAccess::file_copy(url_, dest)) {
xslFile = dest.fileName();
allFiles += xslFile;
}
}
if(xslFile.isEmpty()) {
success = false;
} else {
// remove ".xsl"
xslFile.truncate(xslFile.length()-4);
KConfigGroup config(KGlobal::config(), "KNewStuffFiles");
config.writeEntry(xslFile, allFiles);
m_urlNameMap.insert(url_, xslFile);
}
checkCommonFile();
delete ref;
return success;
}
bool Manager::removeTemplate(const QString& name_) {
KConfigGroup fileGroup(KGlobal::config(), "KNewStuffFiles");
QStringList 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;
QString path = Tellico::saveLocation(QString::fromLatin1("entry-templates/"));
for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it) {
if((*it).endsWith(QChar('/'))) {
// ok to not delete all directories
QDir().rmdir(path + *it);
} else {
success = success && QFile(path + *it).remove();
if(!success) {
myDebug() << "Manager::removeTemplate() - failed to remove " << (path+*it) << endl;
}
}
}
// remove config entries even if unsuccessful
fileGroup.deleteEntry(name_);
KConfigGroup statusGroup(KGlobal::config(), "KNewStuffStatus");
QString 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();
QString exeFile = findEXE(archiveDir);
if(exeFile.isEmpty()) {
myDebug() << "Manager::installScript() - no exe file found" << endl;
return false;
}
QFileInfo exeInfo(exeFile);
DataSourceInfo* info = new DataSourceInfo();
QString copyTarget = Tellico::saveLocation(QString::fromLatin1("data-sources/"));
QString 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(QFile::exists(copyTarget)) {
KURL u;
u.setPath(scriptFolder);
myLog() << "Manager::installScript() - deleting " << scriptFolder << endl;
KIO::NetAccess::del(u, Kernel::self()->widget());
info->isUpdate = true;
}
scriptFolder = copyTarget;
} else {
scriptFolder = copyTarget + firstEntry->name() + '/';
if(QFile::exists(copyTarget + exeFile)) {
info->isUpdate = true;
}
}
// overwrites stuff there
archiveDir->copyTo(copyTarget);
info->specFile = scriptFolder + exeInfo.baseName(true) + ".spec";
if(!QFile::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(QFile::encodeName(dest.path()), item.permissions() | S_IXUSR);
{
KConfig 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);
}
{
KConfigGroup config(KGlobal::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 QString& name_) {
KConfigGroup fileGroup(KGlobal::config(), "KNewStuffFiles");
QStringList files = fileGroup.readListEntry(name_);
bool success = true;
QString path = Tellico::saveLocation(QString::fromLatin1("data-sources/"));
for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it) {
if((*it).endsWith(QChar('/'))) {
// ok to not delete all directories
QDir().rmdir(path + *it);
} else {
success = success && QFile(path + *it).remove();
if(!success) {
myDebug() << "Manager::removeScript() - failed to remove " << (path+*it) << endl;
}
}
}
// remove config entries even if unsuccessful
fileGroup.deleteEntry(name_);
KConfigGroup statusGroup(KGlobal::config(), "KNewStuffStatus");
QString entryName = statusGroup.readEntry(name_);
statusGroup.deleteEntry(name_);
statusGroup.deleteEntry(entryName);
return success;
}
Tellico::NewStuff::InstallStatus Manager::installStatus(KNS::Entry* entry_) {
KConfigGroup config(KGlobal::config(), "KNewStuffStatus");
QString datestring = config.readEntry(entry_->name());
if(datestring.isEmpty()) {
return NotInstalled;
}
QDate date = QDate::fromString(datestring, Qt::ISODate);
if(!date.isValid()) {
return NotInstalled;
}
if(date < entry_->releaseDate()) {
return OldVersion;
}
// also check that executable files exists
KConfigGroup fileGroup(KGlobal::config(), "KNewStuffFiles");
QStringList files = fileGroup.readListEntry(entry_->name());
QString path = Tellico::saveLocation(QString::fromLatin1("data-sources/"));
for(QStringList::ConstIterator it = files.begin(); it != files.end(); ++it) {
if(!QFile::exists(path + *it)) {
return NotInstalled;
}
}
return Current;
}
QStringList Manager::archiveFiles(const KArchiveDirectory* dir_, const QString& path_) {
QStringList list;
const QStringList dirEntries = dir_->entries();
for(QStringList::ConstIterator it = dirEntries.begin(); it != dirEntries.end(); ++it) {
const QString& 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
QString Manager::findXSL(const KArchiveDirectory* dir_) {
const QStringList entries = dir_->entries();
for(QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it) {
const QString& entry = *it;
if(entry.endsWith(QString::fromLatin1(".xsl"))) {
return entry;
}
}
return QString();
}
QString Manager::findEXE(const KArchiveDirectory* dir_) {
QPtrStack<KArchiveDirectory> dirStack;
QValueStack<QString> dirNameStack;
dirStack.push(dir_);
dirNameStack.push(QString());
do {
const QString dirName = dirNameStack.pop();
const KArchiveDirectory* curDir = dirStack.pop();
const QStringList entries = curDir->entries();
for(QStringList::ConstIterator it = entries.begin(); it != entries.end(); ++it) {
const QString& 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 QString();
}
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());
KIO::FileCopyJob* job = KIO::file_copy(entry_->payload(), destination, -1, true);
connect(job, SIGNAL(result(KIO::Job*)), SLOT(slotDownloadJobResult(KIO::Job*)));
m_jobMap.insert(job, EntryPair(entry_, type_));
}
void Manager::slotDownloadJobResult(KIO::Job* job_) {
KIO::FileCopyJob* job = static_cast<KIO::FileCopyJob*>(job_);
if(job->error()) {
GUI::CursorSaver cs(Qt::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, SIGNAL(installFinished()), this, 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 = ::qt_cast<const NewScript*>(sender());
if(newScript && newScript->successfulInstall()) {
const QString name = m_urlNameMap[newScript->url()];
KNS::Entry* entry = m_scriptEntryMap[newScript];
KConfigGroup config(KGlobal::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(), 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
QString userDataDir = Tellico::saveLocation(QString::null);
QString userCommonFile = userDataDir + '/' + QString::fromLatin1("tellico-common.xsl");
if(QFile::exists(userCommonFile)) {
// check timestamps
// pics/tellico.png is not likely to be in a user directory
QString installDir = KGlobal::dirs()->findResourceDir("appdata", QString::fromLatin1("pics/tellico.png"));
QString installCommonFile = installDir + '/' + QString::fromLatin1("tellico-common.xsl");
#ifndef NDEBUG
if(userCommonFile == installCommonFile) {
kdWarning() << "Manager::checkCommonFile() - install location is same as user location" << endl;
}
#endif
QFileInfo installInfo(installCommonFile);
QFileInfo 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;
QFile::remove(userCommonFile);
} else {
return true;
}
}
KURL src, dest;
src.setPath(KGlobal::dirs()->findResource("appdata", QString::fromLatin1("tellico-common.xsl")));
dest.setPath(userCommonFile);
return KIO::NetAccess::file_copy(src, dest);
}
#include "manager.moc"
|