• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Plasma

openwidgetassistant.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2008 Aaron Seigo <aseigo@kde.org>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library/Lesser General Public License
00006  *   version 2, or (at your option) any later version, as published by the
00007  *   Free Software Foundation
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library/Lesser General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "openwidgetassistant_p.h"
00021 
00022 #include <QLabel>
00023 #include <QVBoxLayout>
00024 
00025 #include <KDebug>
00026 #include <kfilewidget.h>
00027 #include <KListWidget>
00028 #include <KMessageBox>
00029 #include <KService>
00030 #include <KServiceTypeTrader>
00031 #include <KStandardDirs>
00032 
00033 #include <Plasma/PackageStructure>
00034 
00035 namespace Plasma
00036 {
00037 
00038 OpenWidgetAssistant::OpenWidgetAssistant(QWidget *parent)
00039     : KAssistantDialog(parent),
00040       m_fileDialog(0),
00041       m_filePageWidget(0)
00042 {
00043     QWidget *selectWidget = new QWidget(this);
00044     QVBoxLayout *selectLayout = new QVBoxLayout(selectWidget);
00045     QLabel *selectLabel = new QLabel(selectWidget);
00046     selectLabel->setText(i18n("Select the type of widget to install from the list below."));
00047     m_widgetTypeList = new KListWidget(selectWidget);
00048     m_widgetTypeList->setSelectionMode(QAbstractItemView::SingleSelection);
00049     //m_widgetTypeList->setSelectionBehavior(QAbstractItemView::SelectItems);
00050     connect(m_widgetTypeList, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(next()));
00051     connect(m_widgetTypeList, SIGNAL(itemSelectionChanged ()), this, SLOT(slotItemChanged()));
00052 
00053     QString constraint("'Applet' in [X-Plasma-ComponentTypes] and exist [X-Plasma-PackageFormat]");
00054     KService::List offers = KServiceTypeTrader::self()->query("Plasma/ScriptEngine", constraint);
00055 
00056     QListWidgetItem * item = new QListWidgetItem(KIcon("plasma"), i18n("Plasmoid: Native plasma widget"), m_widgetTypeList);
00057     item->setSelected(true);
00058     m_widgetTypeList->setCurrentItem(item);
00059 
00060     foreach (const KService::Ptr &offer, offers) {
00061         QString text(offer->name());
00062         if (!offer->comment().isEmpty()) {
00063             text.append(": ").append(offer->comment());
00064         }
00065 
00066         item = new QListWidgetItem(text, m_widgetTypeList);
00067         item->setData(PackageStructureRole, offer->property("X-KDE-PluginInfo-Name"));
00068 
00069         if (!offer->icon().isEmpty()) {
00070             item->setIcon(KIcon(offer->icon()));
00071         }
00072     }
00073 
00074     selectLayout->addWidget(selectLabel);
00075     selectLayout->addWidget(m_widgetTypeList);
00076 
00077     m_typePage = new KPageWidgetItem(selectWidget, i18n("Install New Widget From File"));
00078     m_typePage->setIcon(KIcon("plasma"));
00079     addPage(m_typePage);
00080 
00081     m_filePageWidget = new QWidget(this);
00082     m_filePage = new KPageWidgetItem(m_filePageWidget, i18n("Select File"));
00083     addPage(m_filePage);
00084 
00085     connect(this, SIGNAL(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)), SLOT(prepPage(KPageWidgetItem*,KPageWidgetItem*)));
00086     enableButton(KDialog::Help, false);
00087     //connect( this, SIGNAL( helpClicked() ), this, SLOT( slotHelpClicked() ) );
00088     m_widgetTypeList->setFocus();
00089     resize(QSize(560, 400).expandedTo(minimumSizeHint()));
00090 }
00091 
00092 void OpenWidgetAssistant::slotItemChanged()
00093 {
00094     enableButton(KDialog::User2, !m_widgetTypeList->selectedItems().isEmpty());
00095 }
00096 
00097 void OpenWidgetAssistant::prepPage(KPageWidgetItem *current, KPageWidgetItem *before)
00098 {
00099     Q_UNUSED(before);
00100     if (m_widgetTypeList->selectedItems().isEmpty()) {
00101         return;
00102     }
00103 
00104     if (current != m_filePage) {
00105         return;
00106     }
00107 
00108     if (!m_fileDialog) {
00109         QVBoxLayout *layout = new QVBoxLayout(m_filePageWidget);
00110         m_fileDialog = new KFileWidget(KUrl(), m_filePageWidget);
00111         m_fileDialog->setOperationMode(KFileWidget::Opening);
00112         m_fileDialog->setMode(KFile::File | KFile::ExistingOnly);
00113         connect(this, SIGNAL(user1Clicked()), m_fileDialog, SLOT(slotOk()));
00114         connect(m_fileDialog, SIGNAL(accepted()), this, SLOT(finished()));
00115         //m_fileDialog->setWindowFlags(Qt::Widget);
00116         layout->addWidget(m_fileDialog);
00117     }
00118 
00119     QListWidgetItem *item = m_widgetTypeList->selectedItems().first();
00120     Q_ASSERT(item);
00121 
00122     QString type = item->data(PackageStructureRole).toString();
00123 
00124     m_fileDialog->setFilter(QString());
00125     if (!type.isEmpty()) {
00126         QString constraint = QString("'%1' == [X-KDE-PluginInfo-Name]").arg(type);
00127         KService::List offers = KServiceTypeTrader::self()->query("Plasma/PackageStructure", constraint);
00128 
00129         kDebug() << "looking for a Plasma/PackageStructure with" << constraint << type;
00130         Q_ASSERT(offers.count() > 0);
00131 
00132         m_packageStructureService = offers.first();
00133         QStringList mimes = m_packageStructureService->property("X-Plasma-PackageFileMimetypes").toStringList();
00134 
00135         if (mimes.count() > 0) {
00136             m_fileDialog->setMimeFilter(mimes);
00137         } else {
00138             QString filter = m_packageStructureService->property("X-Plasma-PackageFileFilter").toString();
00139             if (!filter.isEmpty()) {
00140                 m_fileDialog->setFilter(+ '|' + m_packageStructureService->name());
00141             }
00142         }
00143     } else {
00144         QStringList mimes;
00145         mimes << "application/x-plasma";
00146         m_fileDialog->setMimeFilter(mimes);
00147     }
00148 }
00149 
00150 void OpenWidgetAssistant::slotHelpClicked()
00151 {
00152     //enable it when doc will created
00153 }
00154 
00155 void OpenWidgetAssistant::finished()
00156 {
00157     m_fileDialog->accept(); // how interesting .. accept() must be called before the state is set
00158     QString packageFilePath = m_fileDialog->selectedFile();
00159     if (packageFilePath.isEmpty()) {
00160         //TODO: user visible error handling
00161         kDebug() << "hm. no file path?";
00162         return;
00163     }
00164 
00165     kDebug() << "selected uri is" << packageFilePath << "of type" << m_fileDialog->currentFilter();
00166     PackageStructure *installer = 0;
00167     if (m_packageStructureService) {
00168         QString error;
00169         installer = m_packageStructureService->createInstance<Plasma::PackageStructure>(0, QVariantList(), &error);
00170         if (!installer) {
00171             kDebug() << "Could not load requested PackageStructure installer "
00172                      << m_packageStructureService << ". Error given: " << error;
00173             KMessageBox::error(
00174                 this,
00175                 i18n("Could not load the required installer %1. "
00176                      "The error given was: %2",
00177                      m_packageStructureService, error),
00178                 i18n("Installation Failure"));
00179             return;
00180         }
00181     } else {
00182         installer = new PackageStructure;
00183     }
00184 
00185     QString root = KStandardDirs::locateLocal("data", "plasma/plasmoids/");
00186     kDebug() << "installing" << packageFilePath << "to root dir of" << root;
00187 
00188     if (!installer->installPackage(packageFilePath, root)) {
00189         KMessageBox::error(this, i18n("Installing the package %1 failed.", packageFilePath),
00190                            i18n("Installation Failure"));
00191     }
00192 
00193     delete installer;
00194 }
00195 
00196 } // Plasma namespace
00197 
00198 #include "openwidgetassistant_p.moc"

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal