1
/***************************************************************************
2
* Copyright Ravikiran Rajagopal 2003 *
3
* ravi@ee.eng.ohio-state.edu *
4
* Copyright (c) 1998 Stefan Taferner <taferner@kde.org> *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License (version 2) as *
8
* published by the Free Software Foundation. *
10
***************************************************************************/
21
#include <QHBoxLayout>
23
#include <QVBoxLayout>
24
#include <QDragEnterEvent>
25
#include <QMouseEvent>
26
#include <QScrollArea>
28
#include "installer.h"
31
#include <kfiledialog.h>
32
#include <kglobalsettings.h>
34
#include <kmessagebox.h>
36
#include <kpushbutton.h>
37
#include <kstandarddirs.h>
39
#include <kservicetypetrader.h>
40
#include <kio/netaccess.h>
41
#include <knewstuff3/downloaddialog.h>
43
ThemeListBox::ThemeListBox(QWidget *parent)
49
void ThemeListBox::dragEnterEvent(QDragEnterEvent* event)
51
event->setAccepted((event->source() != this) && KUrl::List::canDecode(event->mimeData()));
54
void ThemeListBox::dragMoveEvent(QDragMoveEvent* event)
56
event->setAccepted((event->source() != this) && KUrl::List::canDecode(event->mimeData()));
59
void ThemeListBox::dropEvent(QDropEvent* event)
61
KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
64
emit filesDropped(urls);
68
void ThemeListBox::mousePressEvent(QMouseEvent *e)
70
if ((e->buttons() & Qt::LeftButton) != 0)
72
mOldPos = e->globalPos();;
74
int cur = row(itemAt(e->pos()));
76
mDragFile = text2path[item(cur)->text()];
78
KListWidget::mousePressEvent(e);
81
void ThemeListBox::mouseMoveEvent(QMouseEvent *e)
83
if (((e->buttons() & Qt::LeftButton) != 0) && !mDragFile.isEmpty())
85
int delay = KGlobalSettings::dndEventDelay();
86
QPoint newPos = e->globalPos();
87
if(newPos.x() > mOldPos.x()+delay || newPos.x() < mOldPos.x()-delay ||
88
newPos.y() > mOldPos.y()+delay || newPos.y() < mOldPos.y()-delay)
91
url.setPath(mDragFile);
94
QDrag *drag = new QDrag(this);
95
QMimeData *mime = new QMimeData();
96
urls.populateMimeData(mime);
97
drag->setMimeData(mime);
101
KListWidget::mouseMoveEvent(e);
104
//-----------------------------------------------------------------------------
105
SplashInstaller::SplashInstaller (QWidget *aParent, const char *aName, bool aInit)
106
: QWidget(aParent), mGui(!aInit)
108
setObjectName(aName);
109
KGlobal::dirs()->addResourceType("ksplashthemes", "data", "ksplash/Themes");
114
QHBoxLayout* hbox = new QHBoxLayout( this );
115
hbox->setMargin( 0 );
117
QVBoxLayout* leftbox = new QVBoxLayout( );
118
hbox->addLayout( leftbox );
119
hbox->setStretchFactor( leftbox, 1 );
121
mThemesList = new ThemeListBox(this);
122
mThemesList->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding );
123
connect(mThemesList, SIGNAL(currentRowChanged(int)), SLOT(slotSetTheme(int)));
124
connect(mThemesList, SIGNAL(filesDropped(const KUrl::List&)), SLOT(slotFilesDropped(const KUrl::List&)));
125
leftbox->addWidget(mThemesList);
127
mBtnNew = new KPushButton( KIcon("get-hot-new-stuff"), i18n("Get New Themes..."), this );
128
mBtnNew->setToolTip(i18n("Get new themes from the Internet"));
129
mBtnNew->setWhatsThis(i18n("You need to be connected to the Internet to use this action. A dialog will display a list of themes from the http://www.kde.org website. Clicking the Install button associated with a theme will install this theme locally."));
130
leftbox->addWidget( mBtnNew );
131
connect(mBtnNew, SIGNAL(clicked()), SLOT(slotNew()));
133
mBtnAdd = new KPushButton( KIcon("document-import"), i18n("Install Theme File..."), this );
134
mBtnAdd->setToolTip(i18n("Install a theme archive file you already have locally"));
135
mBtnAdd->setWhatsThis(i18n("If you already have a theme archive locally, this button will unpack it and make it available for KDE applications"));
136
leftbox->addWidget( mBtnAdd );
137
connect(mBtnAdd, SIGNAL(clicked()), SLOT(slotAdd()));
139
mBtnRemove = new KPushButton( KIcon("edit-delete"), i18n("Remove Theme"), this );
140
mBtnRemove->setToolTip(i18n("Remove the selected theme from your disk"));
141
mBtnRemove->setWhatsThis(i18n("This will remove the selected theme from your disk."));
142
mBtnRemove->setEnabled( false );
143
leftbox->addWidget( mBtnRemove );
144
connect(mBtnRemove, SIGNAL(clicked()), SLOT(slotRemove()));
146
mBtnTest = new KPushButton( KIcon("document-preview"), i18n("Test Theme"), this );
147
mBtnTest->setToolTip(i18n("Test the selected theme"));
148
mBtnTest->setWhatsThis(i18n("This will test the selected theme."));
149
mBtnTest->setEnabled( false );
150
leftbox->addWidget( mBtnTest );
151
connect(mBtnTest, SIGNAL(clicked()), SLOT(slotTest()));
153
QVBoxLayout* rightbox = new QVBoxLayout( );
154
hbox->addLayout( rightbox );
155
hbox->setStretchFactor( rightbox, 3 );
157
QScrollArea* scrollarea = new QScrollArea(this);
158
scrollarea->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
159
mPreview = new QLabel(this);
160
scrollarea->setWidget(mPreview);
161
mPreview->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
162
mPreview->setMinimumSize(QSize(320,240));
163
mPreview->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
164
rightbox->addWidget(scrollarea);
165
rightbox->setStretchFactor( scrollarea, 3 );
167
mText = new QTextEdit(this);
168
mText->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
169
mText->setMinimumSize(mText->sizeHint().width(), 7 * mText->fontMetrics().height());
170
mText->setReadOnly(true);
171
rightbox->addWidget(mText);
172
rightbox->setStretchFactor( mText, 1 );
180
//-----------------------------------------------------------------------------
181
SplashInstaller::~SplashInstaller()
185
int SplashInstaller::addTheme(const QString &path, const QString &name)
187
//kDebug() << "SplashInstaller::addTheme: " << path << " " << name;
188
QString tmp(i18n( name.toUtf8() ));
189
int i = mThemesList->count();
190
while((i > 0) && (mThemesList->item(i-1)->text() > tmp))
192
if ((i > 0) && (mThemesList->item(i-1)->text() == tmp))
194
mThemesList->insertItem(i , tmp);
195
mThemesList->text2path.insert( tmp, path+'/'+name );
199
// Copy theme package into themes directory
200
void SplashInstaller::addNewTheme(const KUrl &srcURL)
202
const QString dir = KGlobal::dirs()->saveLocation("ksplashthemes");
205
QStringList themeNames;
206
QString filename = srcURL.fileName();
207
int i = filename.lastIndexOf('.');
208
// Convert extension to lower case.
210
filename = filename.left(i)+filename.mid(i).toLower();
211
url.setPath(KStandardDirs::locateLocal("tmp",filename));
213
// Remove file from temporary directory if it aleady exists - usually the result of a failed install.
214
if ( KIO::NetAccess::exists( url, KIO::NetAccess::SourceSide, 0 ) )
215
KIO::NetAccess::del( url, 0 );
217
if (srcURL.fileName().toLower() == "theme.rc" ) // uncompressed theme selected
220
// Get the name of the Theme from the theme.rc file
221
KConfig _cnf(srcURL.path());
222
KConfigGroup cnf(&_cnf, QString("KSplash Theme: %1").arg(themeName) );
224
// copy directory of theme.rc to ThemesDir
225
KIO::NetAccess::dircopy(KUrl(srcURL.directory()), KUrl(dir + themeName));
227
themeNames << themeName;
231
bool rc = KIO::NetAccess::file_copy(srcURL, url, 0);
234
kWarning() << "Failed to copy theme " << srcURL.fileName()
235
<< " into temporary directory " << url.path() << endl;
239
// Extract into theme directory: we may have multiple themes in one tarball!
240
KTar tarFile(url.path());
241
if (!tarFile.open(QIODevice::ReadOnly))
243
kWarning() << "Unable to open archive: " << url.path();
244
KIO::NetAccess::del( url, 0 );
247
KArchiveDirectory const *ad = tarFile.directory();
249
// Find first directory entry.
250
const QStringList entries = ad->entries();
251
foreach(const QString& s, entries)
253
if( ad->entry(s)->isDirectory() )
255
// each directory may contain one theme
259
if (themeNames.count() < 1)
261
kWarning() << "No directory in archive: " << url.path();
263
KIO::NetAccess::del( url, 0 );
267
// copy the theme into the "ksplashthemes" directory
271
KIO::NetAccess::del( url, 0 );
276
mThemesList->setCurrentRow(findTheme(themeNames.first()));
277
if (mThemesList->currentItem())
279
mThemesList->currentItem()->setSelected(true);
283
//-----------------------------------------------------------------------------
284
void SplashInstaller::readThemesList()
286
mThemesList->clear();
289
const QStringList entryList = KGlobal::dirs()->resourceDirs("ksplashthemes");
290
//kDebug() << "readThemesList: " << entryList;
293
QStringList::ConstIterator name;
294
for(name = entryList.constBegin(); name != entryList.constEnd(); name++)
299
subdirs = dir.entryList( QDir::Dirs );
300
// kDebug() << "readThemesList: " << subdirs;
301
// TODO: Make sure it contains a *.rc file.
302
for (QStringList::const_iterator l = subdirs.constBegin(); l != subdirs.constEnd(); l++ )
303
if ( !(*l).startsWith(QString(".")) )
305
mThemesList->blockSignals( true ); // Don't activate any theme until all themes are loaded.
306
addTheme(dir.path(),*l);
307
mThemesList->blockSignals( false );
312
//-----------------------------------------------------------------------------
313
void SplashInstaller::defaults()
315
mThemesList->setCurrentRow(findTheme("Default"));
316
emit changed( true );
319
void SplashInstaller::load()
321
KConfig _cnf( "ksplashrc" );
322
KConfigGroup cnf(&_cnf, "KSplash");
323
QString curTheme = cnf.readEntry("Theme","Default");
324
mThemesList->setCurrentRow(findTheme(curTheme));
325
emit changed( false );
328
//-----------------------------------------------------------------------------
329
void SplashInstaller::save()
331
KConfig _cnf( "ksplashrc" );
332
KConfigGroup cnf(&_cnf, "KSplash");
333
int cur = mThemesList->currentRow();
336
QString path = mThemesList->item(cur)->text();
337
if ( mThemesList->text2path.contains( path ) )
338
path = mThemesList->text2path[path];
339
cur = path.lastIndexOf('/');
340
cnf.writeEntry("Theme", path.mid(cur+1) );
341
// save also the engine, so that it's known at KDE startup which splash implementation to use
342
cnf.writeEntry("Engine", mEngineOfSelected );
344
emit changed( false );
347
//-----------------------------------------------------------------------------
348
void SplashInstaller::slotRemove()
350
int cur = mThemesList->currentRow();
355
const QString themeName = mThemesList->item(cur)->text();
356
const QString themeDir = mThemesList->text2path[themeName];
357
if (!themeDir.isEmpty())
360
url.setPath(themeDir);
361
if (KMessageBox::warningContinueCancel(this,i18n("Delete folder %1 and its contents?", themeDir),"",KGuiItem(i18n("&Delete"),"edit-delete"))==KMessageBox::Continue)
362
rc = KIO::NetAccess::del(url,this);
368
KMessageBox::sorry(this, i18n("Failed to remove theme '%1'", themeName));
371
//mThemesList->removeItem(cur);
373
cur = (cur >= mThemesList->count())?mThemesList->count()-1:cur;
374
mThemesList->setCurrentRow(cur);
378
//-----------------------------------------------------------------------------
379
void SplashInstaller::slotSetTheme(int id)
382
QString path = QString();
387
mPreview->setText(QString());
388
mText->setText(QString());
393
QString error = i18n("(Could not load theme)");
394
path = mThemesList->item(id)->text();
395
if ( mThemesList->text2path.contains( path ) )
396
path = mThemesList->text2path[path];
402
// Make sure the correct plugin is installed.
403
int i = path.lastIndexOf('/');
405
themeName = path.mid(i+1);
406
url.setPath( path + "/Theme.rc" );
407
if (!KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0))
409
url.setPath( path + "/Theme.RC" );
410
if (!KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0))
412
url.setPath( path + "/theme.rc" );
413
if (!KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0))
414
url.setPath( path + '/' + themeName + ".rc" );
417
if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0))
419
KConfig _cnf(url.path());
420
KConfigGroup cnf(&_cnf, QString("KSplash Theme: %1").arg(themeName) );
422
// Get theme information.
424
if ( cnf.hasKey( "Name" ) )
425
infoTxt += i18n( "<b>Name:</b> %1", cnf.readEntry( "Name", i18nc( "Unknown name", "Unknown" ) ) ) + "<br />";
426
if ( cnf.hasKey( "Description" ) )
427
infoTxt += i18n( "<b>Description:</b> %1", cnf.readEntry( "Description", i18nc( "Unknown description", "Unknown" ) ) ) + "<br />";
428
if ( cnf.hasKey( "Version" ) )
429
infoTxt += i18n( "<b>Version:</b> %1", cnf.readEntry( "Version", i18nc( "Unknown version", "Unknown" ) ) ) + "<br />";
430
if ( cnf.hasKey( "Author" ) )
431
infoTxt += i18n( "<b>Author:</b> %1", cnf.readEntry( "Author", i18nc( "Unknown author", "Unknown" ) ) ) + "<br />";
432
if ( cnf.hasKey( "Homepage" ) )
433
infoTxt += i18n( "<b>Homepage:</b> %1", cnf.readEntry( "Homepage", i18nc( "Unknown homepage", "Unknown" ) ) ) + "<br />";
436
QString pluginName( cnf.readEntry( "Engine", "KSplashX" ).trimmed() );
437
if( pluginName == "Simple" || pluginName == "None" || pluginName == "KSplashX" )
438
enabled = true; // these are not plugins
439
else if ((KServiceTypeTrader::self()->query("KSplash/Plugin", QString("[X-KSplash-PluginName] == '%1'").arg(pluginName))).isEmpty())
442
error = i18n("This theme requires the plugin %1 which is not installed.", pluginName);
445
enabled = true; // Hooray, there is at least one plugin which can handle this theme.
446
mEngineOfSelected = pluginName;
450
error = i18n("Could not load theme configuration file.");
453
mBtnTest->setEnabled(enabled && themeName != "None" );
454
mText->setHtml(infoTxt);
457
url.setPath( path + '/' + "Preview.png" );
458
if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0))
459
mPreview->setPixmap(QPixmap(url.path()));
461
mPreview->setText(i18n("(Could not load theme)"));
462
KMessageBox::sorry(this, error);
466
url.setPath( path + '/' + "Preview.png" );
467
if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0))
468
mPreview->setPixmap(QPixmap(url.path()));
470
mPreview->setText(i18n("No preview available."));
474
mBtnRemove->setEnabled( !path.isEmpty() && QFileInfo(path).isWritable());
477
//-----------------------------------------------------------------------------
478
void SplashInstaller::slotNew()
480
KNS3::DownloadDialog dialog("ksplash.knsrc", this);
482
if (!dialog.changedEntries().isEmpty())
486
//-----------------------------------------------------------------------------
487
void SplashInstaller::slotAdd()
490
if (path.isEmpty()) path = QDir::homePath();
492
KFileDialog dlg(path, "*.tgz *.tar.gz *.tar.bz2 theme.rc|" + i18n( "KSplash Theme Files" ), this);
493
dlg.setCaption(i18n("Add Theme"));
497
path = dlg.baseUrl().url();
498
addNewTheme(dlg.selectedUrl());
501
//-----------------------------------------------------------------------------
502
void SplashInstaller::slotFilesDropped(const KUrl::List &urls)
504
for(KUrl::List::ConstIterator it = urls.constBegin();
510
//-----------------------------------------------------------------------------
511
int SplashInstaller::findTheme( const QString &theme )
513
// theme is untranslated, but the listbox contains translated items
514
QString tmp(i18n( theme.toUtf8() ));
515
int id = mThemesList->count()-1;
519
if (mThemesList->item(id)->text() == tmp)
527
//-----------------------------------------------------------------------------
528
void SplashInstaller::slotTest()
530
int i = mThemesList->currentRow();
533
QString themeName = mThemesList->text2path[mThemesList->item(i)->text()];
534
int r = themeName.lastIndexOf('/');
536
themeName = themeName.mid(r+1);
538
// special handling for none and simple splashscreens
539
if( mEngineOfSelected == "None" )
541
else if( mEngineOfSelected == "Simple" )
544
proc << "ksplashsimple" << themeName << "--test";
546
KMessageBox::error(this,i18n("Failed to successfully test the splash screen."));
549
else if( mEngineOfSelected == "KSplashX" )
552
proc << "ksplashx" << themeName << "--test";
554
KMessageBox::error(this,i18n("Failed to successfully test the splash screen."));
557
else // KSplashML engines
560
proc << "ksplash" << "--test" << "--theme" << themeName;
562
KMessageBox::error(this,i18n("Failed to successfully test the splash screen."));
566
//-----------------------------------------------------------------------------
567
#include "installer.moc"