not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / shells / common / openwidgetassistant.cpp
bloba94c7407fae5586e92e66dc5156d5b715ac55b8f
1 /*
2 * Copyright (C) 2008 Aaron Seigo <aseigo@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library/Lesser General Public License
6 * version 2, or (at your option) any later version, as published by the
7 * Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library/Lesser General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "openwidgetassistant_p.h"
22 #include <QLabel>
23 #include <QVBoxLayout>
25 #include <KDebug>
26 #include <kfilewidget.h>
27 #include <KListWidget>
28 #include <KMessageBox>
29 #include <KService>
30 #include <KServiceTypeTrader>
31 #include <KStandardDirs>
33 #include <Plasma/PackageStructure>
35 namespace Plasma
38 OpenWidgetAssistant::OpenWidgetAssistant(QWidget *parent)
39 : KAssistantDialog(parent),
40 m_fileDialog(0),
41 m_filePageWidget(0)
43 QWidget *selectWidget = new QWidget(this);
44 QVBoxLayout *selectLayout = new QVBoxLayout(selectWidget);
45 QLabel *selectLabel = new QLabel(selectWidget);
46 selectLabel->setText(i18n("Select the type of widget to install from the list below."));
47 m_widgetTypeList = new KListWidget(selectWidget);
48 m_widgetTypeList->setSelectionMode(QAbstractItemView::SingleSelection);
49 //m_widgetTypeList->setSelectionBehavior(QAbstractItemView::SelectItems);
50 connect(m_widgetTypeList, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(next()));
51 connect(m_widgetTypeList, SIGNAL(itemSelectionChanged ()), this, SLOT(slotItemChanged()));
53 QString constraint("'Applet' in [X-Plasma-ComponentTypes] and exist [X-Plasma-PackageFormat]");
54 KService::List offers = KServiceTypeTrader::self()->query("Plasma/ScriptEngine", constraint);
56 QListWidgetItem * item = new QListWidgetItem(KIcon("plasma"), i18n("Plasmoid: Native plasma widget"), m_widgetTypeList);
57 item->setSelected(true);
58 m_widgetTypeList->setCurrentItem(item);
60 foreach (const KService::Ptr &offer, offers) {
61 QString text(offer->name());
62 if (!offer->comment().isEmpty()) {
63 text.append(": ").append(offer->comment());
66 item = new QListWidgetItem(text, m_widgetTypeList);
67 item->setData(PackageStructureRole, offer->property("X-KDE-PluginInfo-Name"));
69 if (!offer->icon().isEmpty()) {
70 item->setIcon(KIcon(offer->icon()));
74 selectLayout->addWidget(selectLabel);
75 selectLayout->addWidget(m_widgetTypeList);
77 m_typePage = new KPageWidgetItem(selectWidget, i18n("Install New Widget From File"));
78 m_typePage->setIcon(KIcon("plasma"));
79 addPage(m_typePage);
81 m_filePageWidget = new QWidget(this);
82 m_filePage = new KPageWidgetItem(m_filePageWidget, i18n("Select File"));
83 addPage(m_filePage);
85 connect(this, SIGNAL(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)), SLOT(prepPage(KPageWidgetItem*,KPageWidgetItem*)));
86 enableButton(KDialog::Help, false);
87 //connect( this, SIGNAL( helpClicked() ), this, SLOT( slotHelpClicked() ) );
88 m_widgetTypeList->setFocus();
89 resize(QSize(560, 400).expandedTo(minimumSizeHint()));
92 void OpenWidgetAssistant::slotItemChanged()
94 enableButton(KDialog::User2, !m_widgetTypeList->selectedItems().isEmpty());
97 void OpenWidgetAssistant::prepPage(KPageWidgetItem *current, KPageWidgetItem *before)
99 Q_UNUSED(before);
100 if (m_widgetTypeList->selectedItems().isEmpty()) {
101 return;
104 if (current != m_filePage) {
105 return;
108 if (!m_fileDialog) {
109 QVBoxLayout *layout = new QVBoxLayout(m_filePageWidget);
110 m_fileDialog = new KFileWidget(KUrl(), m_filePageWidget);
111 m_fileDialog->setOperationMode(KFileWidget::Opening);
112 m_fileDialog->setMode(KFile::File | KFile::ExistingOnly);
113 connect(this, SIGNAL(user1Clicked()), m_fileDialog, SLOT(slotOk()));
114 connect(m_fileDialog, SIGNAL(accepted()), this, SLOT(finished()));
115 //m_fileDialog->setWindowFlags(Qt::Widget);
116 layout->addWidget(m_fileDialog);
119 QListWidgetItem *item = m_widgetTypeList->selectedItems().first();
120 Q_ASSERT(item);
122 QString type = item->data(PackageStructureRole).toString();
124 m_fileDialog->setFilter(QString());
125 if (!type.isEmpty()) {
126 QString constraint = QString("'%1' == [X-KDE-PluginInfo-Name]").arg(type);
127 KService::List offers = KServiceTypeTrader::self()->query("Plasma/PackageStructure", constraint);
129 kDebug() << "looking for a Plasma/PackageStructure with" << constraint << type;
130 Q_ASSERT(offers.count() > 0);
132 m_packageStructureService = offers.first();
133 QStringList mimes = m_packageStructureService->property("X-Plasma-PackageFileMimetypes").toStringList();
135 if (mimes.count() > 0) {
136 m_fileDialog->setMimeFilter(mimes);
137 } else {
138 QString filter = m_packageStructureService->property("X-Plasma-PackageFileFilter").toString();
139 if (!filter.isEmpty()) {
140 m_fileDialog->setFilter(+ '|' + m_packageStructureService->name());
143 } else {
144 QStringList mimes;
145 mimes << "application/x-plasma";
146 m_fileDialog->setMimeFilter(mimes);
150 void OpenWidgetAssistant::slotHelpClicked()
152 //enable it when doc will created
155 void OpenWidgetAssistant::finished()
157 m_fileDialog->accept(); // how interesting .. accept() must be called before the state is set
158 QString packageFilePath = m_fileDialog->selectedFile();
159 if (packageFilePath.isEmpty()) {
160 //TODO: user visible error handling
161 kDebug() << "hm. no file path?";
162 return;
165 kDebug() << "selected uri is" << packageFilePath << "of type" << m_fileDialog->currentFilter();
166 PackageStructure *installer = 0;
167 if (m_packageStructureService) {
168 QString error;
169 installer = m_packageStructureService->createInstance<Plasma::PackageStructure>(0, QVariantList(), &error);
170 if (!installer) {
171 kDebug() << "Could not load requested PackageStructure installer "
172 << m_packageStructureService << ". Error given: " << error;
173 KMessageBox::error(
174 this,
175 i18n("Could not load the required installer %1. "
176 "The error given was: %2",
177 m_packageStructureService, error),
178 i18n("Installation Failure"));
179 return;
181 } else {
182 installer = new PackageStructure;
185 QString root = KStandardDirs::locateLocal("data", "plasma/plasmoids/");
186 kDebug() << "installing" << packageFilePath << "to root dir of" << root;
188 if (!installer->installPackage(packageFilePath, root)) {
189 KMessageBox::error(this, i18n("Installing the package %1 failed.", packageFilePath),
190 i18n("Installation Failure"));
193 delete installer;
196 } // Plasma namespace
198 #include "openwidgetassistant_p.moc"