2008-02-29 09:10:14 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Aaron Seigo <aseigo@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library/Lesser General Public License
|
|
|
|
* version 2, or (at your option) any later version, as published by the
|
|
|
|
* Free Software Foundation
|
|
|
|
*
|
|
|
|
* 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 Library/Lesser 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 "plasma/appletbrowser/openwidgetassistant_p.h"
|
|
|
|
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
#include <KDebug>
|
|
|
|
#include <kfilewidget.h>
|
|
|
|
#include <KListWidget>
|
2008-09-30 01:52:02 +02:00
|
|
|
#include <KMessageBox>
|
2008-02-29 09:10:14 +01:00
|
|
|
#include <KService>
|
|
|
|
#include <KServiceTypeTrader>
|
|
|
|
#include <KStandardDirs>
|
|
|
|
|
|
|
|
#include "plasma/packagestructure.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
OpenWidgetAssistant::OpenWidgetAssistant(QWidget *parent)
|
|
|
|
: KAssistantDialog(parent),
|
|
|
|
m_fileDialog(0),
|
|
|
|
m_filePageWidget(0)
|
|
|
|
{
|
|
|
|
QWidget *selectWidget = new QWidget(this);
|
|
|
|
QVBoxLayout *selectLayout = new QVBoxLayout(selectWidget);
|
|
|
|
QLabel *selectLabel = new QLabel(selectWidget);
|
2008-04-21 23:20:03 +02:00
|
|
|
selectLabel->setText(i18n("Select the type of widget to install from the list below."));
|
2008-02-29 09:10:14 +01:00
|
|
|
m_widgetTypeList = new KListWidget(selectWidget);
|
|
|
|
m_widgetTypeList->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
|
//m_widgetTypeList->setSelectionBehavior(QAbstractItemView::SelectItems);
|
2008-03-02 16:37:50 +01:00
|
|
|
connect(m_widgetTypeList, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(next()));
|
2008-06-07 13:31:16 +02:00
|
|
|
connect(m_widgetTypeList, SIGNAL(itemSelectionChanged ()), this, SLOT(slotItemChanged()));
|
2008-02-29 09:10:14 +01:00
|
|
|
|
|
|
|
QString constraint("'Applet' in [X-Plasma-ComponentTypes] and exist [X-Plasma-PackageFormat]");
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/ScriptEngine", constraint);
|
|
|
|
|
|
|
|
QListWidgetItem * item = new QListWidgetItem(KIcon("plasma"), i18n("Plasmoid: Native plasma widget"), m_widgetTypeList);
|
|
|
|
item->setSelected(true);
|
|
|
|
m_widgetTypeList->setCurrentItem(item);
|
|
|
|
|
|
|
|
foreach (const KService::Ptr &offer, offers) {
|
|
|
|
QString text(offer->name());
|
|
|
|
if (!offer->comment().isEmpty()) {
|
|
|
|
text.append(": ").append(offer->comment());
|
|
|
|
}
|
|
|
|
|
|
|
|
item = new QListWidgetItem(text, m_widgetTypeList);
|
|
|
|
item->setData(PackageStructureRole, offer->property("X-KDE-PluginInfo-Name"));
|
|
|
|
|
|
|
|
if (!offer->icon().isEmpty()) {
|
|
|
|
item->setIcon(KIcon(offer->icon()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
selectLayout->addWidget(selectLabel);
|
|
|
|
selectLayout->addWidget(m_widgetTypeList);
|
|
|
|
|
|
|
|
m_typePage = new KPageWidgetItem(selectWidget, i18n("Install New Widget From File"));
|
|
|
|
m_typePage->setIcon(KIcon("plasma"));
|
|
|
|
addPage(m_typePage);
|
|
|
|
|
|
|
|
m_filePageWidget = new QWidget(this);
|
|
|
|
m_filePage = new KPageWidgetItem(m_filePageWidget, i18n("Select File"));
|
|
|
|
addPage(m_filePage);
|
|
|
|
|
|
|
|
connect(this, SIGNAL(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)), SLOT(prepPage(KPageWidgetItem*,KPageWidgetItem*)));
|
2008-10-12 02:18:08 +02:00
|
|
|
enableButton(KDialog::Help, false);
|
2008-04-25 13:02:58 +02:00
|
|
|
//connect( this, SIGNAL( helpClicked() ), this, SLOT( slotHelpClicked() ) );
|
2008-03-02 16:37:50 +01:00
|
|
|
m_widgetTypeList->setFocus();
|
|
|
|
resize(QSize(560, 400).expandedTo(minimumSizeHint()));
|
2008-02-29 09:10:14 +01:00
|
|
|
}
|
|
|
|
|
2008-06-07 13:31:16 +02:00
|
|
|
void OpenWidgetAssistant::slotItemChanged()
|
|
|
|
{
|
2008-10-12 02:18:08 +02:00
|
|
|
enableButton(KDialog::User2, !m_widgetTypeList->selectedItems().isEmpty());
|
2008-06-07 13:31:16 +02:00
|
|
|
}
|
|
|
|
|
2008-02-29 09:10:14 +01:00
|
|
|
void OpenWidgetAssistant::prepPage(KPageWidgetItem *current, KPageWidgetItem *before)
|
|
|
|
{
|
2008-10-12 02:18:08 +02:00
|
|
|
Q_UNUSED(before);
|
|
|
|
if (m_widgetTypeList->selectedItems().isEmpty()) {
|
2008-06-07 13:31:16 +02:00
|
|
|
return;
|
2008-10-12 02:18:08 +02:00
|
|
|
}
|
2008-02-29 09:10:14 +01:00
|
|
|
|
|
|
|
if (current != m_filePage) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_fileDialog) {
|
|
|
|
QVBoxLayout *layout = new QVBoxLayout(m_filePageWidget);
|
|
|
|
m_fileDialog = new KFileWidget(KUrl(), m_filePageWidget);
|
|
|
|
m_fileDialog->setOperationMode(KFileWidget::Opening);
|
|
|
|
m_fileDialog->setMode(KFile::File | KFile::ExistingOnly);
|
2008-09-30 01:52:02 +02:00
|
|
|
connect(this, SIGNAL(user1Clicked()), m_fileDialog, SLOT(slotOk()));
|
|
|
|
connect(m_fileDialog, SIGNAL(accepted()), this, SLOT(finished()));
|
2008-02-29 09:10:14 +01:00
|
|
|
//m_fileDialog->setWindowFlags(Qt::Widget);
|
|
|
|
layout->addWidget(m_fileDialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
QListWidgetItem *item = m_widgetTypeList->selectedItems().first();
|
|
|
|
Q_ASSERT(item);
|
|
|
|
|
|
|
|
QString type = item->data(PackageStructureRole).toString();
|
|
|
|
|
|
|
|
m_fileDialog->setFilter(QString());
|
|
|
|
if (!type.isEmpty()) {
|
|
|
|
QString constraint = QString("'%1' == [X-KDE-PluginInfo-Name]").arg(type);
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("Plasma/PackageStructure", constraint);
|
|
|
|
|
|
|
|
kDebug() << "looking for a Plasma/PackageStructure with" << constraint << type;
|
|
|
|
Q_ASSERT(offers.count() > 0);
|
|
|
|
|
|
|
|
m_packageStructureService = offers.first();
|
|
|
|
QStringList mimes = m_packageStructureService->property("X-Plasma-PackageFileMimetypes").toStringList();
|
|
|
|
|
|
|
|
if (mimes.count() > 0) {
|
|
|
|
m_fileDialog->setMimeFilter(mimes);
|
|
|
|
} else {
|
|
|
|
QString filter = m_packageStructureService->property("X-Plasma-PackageFileFilter").toString();
|
|
|
|
if (!filter.isEmpty()) {
|
2008-10-12 02:18:08 +02:00
|
|
|
m_fileDialog->setFilter(+ '|' + m_packageStructureService->name());
|
2008-02-29 09:10:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2008-03-06 02:54:01 +01:00
|
|
|
QStringList mimes;
|
|
|
|
mimes << "application/x-plasma";
|
|
|
|
m_fileDialog->setMimeFilter(mimes);
|
2008-02-29 09:10:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-25 13:02:58 +02:00
|
|
|
void OpenWidgetAssistant::slotHelpClicked()
|
|
|
|
{
|
|
|
|
//enable it when doc will created
|
|
|
|
}
|
|
|
|
|
2008-02-29 09:10:14 +01:00
|
|
|
void OpenWidgetAssistant::finished()
|
|
|
|
{
|
|
|
|
m_fileDialog->accept(); // how interesting .. accept() must be called before the state is set
|
2008-09-30 01:52:02 +02:00
|
|
|
QString packageFilePath = m_fileDialog->selectedFile();
|
|
|
|
if (packageFilePath.isEmpty()) {
|
2008-02-29 09:10:14 +01:00
|
|
|
//TODO: user visible error handling
|
2008-09-30 01:52:02 +02:00
|
|
|
kDebug() << "hm. no file path?";
|
2008-02-29 09:10:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-09-30 01:52:02 +02:00
|
|
|
kDebug() << "selected uri is" << packageFilePath << "of type" << m_fileDialog->currentFilter();
|
2008-03-30 01:55:21 +01:00
|
|
|
PackageStructure *installer = 0;
|
|
|
|
if (m_packageStructureService) {
|
|
|
|
QString error;
|
|
|
|
installer = m_packageStructureService->createInstance<Plasma::PackageStructure>(0, QVariantList(), &error);
|
|
|
|
if (!installer) {
|
|
|
|
kDebug() << "Could not load requested PackageStructure installer "
|
|
|
|
<< m_packageStructureService << ". Error given: " << error;
|
2008-10-12 13:01:00 +02:00
|
|
|
KMessageBox::error(
|
|
|
|
this,
|
|
|
|
i18n("Could not load the required installer %1. "
|
|
|
|
"The error given was: %2",
|
|
|
|
m_packageStructureService, error),
|
|
|
|
i18n("Installation Failure"));
|
2008-03-30 01:55:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
installer = new PackageStructure;
|
2008-02-29 09:10:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString root = KStandardDirs::locateLocal("data", "plasma/plasmoids/");
|
2008-09-30 01:52:02 +02:00
|
|
|
kDebug() << "installing" << packageFilePath << "to root dir of" << root;
|
2008-02-29 09:10:14 +01:00
|
|
|
|
2008-10-01 01:21:51 +02:00
|
|
|
if (!installer->installPackage(packageFilePath, root)) {
|
2008-09-30 01:52:02 +02:00
|
|
|
KMessageBox::error(this, i18n("Installing the package %1 failed.", packageFilePath),
|
|
|
|
i18n("Installation Failure"));
|
|
|
|
}
|
2008-10-01 01:21:51 +02:00
|
|
|
|
|
|
|
delete installer;
|
2008-02-29 09:10:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "openwidgetassistant_p.moc"
|