~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to libs/plasmagenericshell/widgetsexplorer/openwidgetassistant.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2008 Aaron Seigo <aseigo@kde.org>
 
3
 *
 
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
 
8
 *
 
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
 
13
 *
 
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.
 
18
 */
 
19
 
 
20
#include "openwidgetassistant_p.h"
 
21
 
 
22
#include <QLabel>
 
23
#include <QVBoxLayout>
 
24
 
 
25
#include <KDebug>
 
26
#include <kfilewidget.h>
 
27
#include <KListWidget>
 
28
#include <KMessageBox>
 
29
#include <KService>
 
30
#include <KServiceTypeTrader>
 
31
#include <KStandardDirs>
 
32
 
 
33
#include <Plasma/PackageStructure>
 
34
 
 
35
namespace Plasma
 
36
{
 
37
 
 
38
OpenWidgetAssistant::OpenWidgetAssistant(QWidget *parent)
 
39
    : KAssistantDialog(parent),
 
40
      m_fileDialog(0),
 
41
      m_filePageWidget(0)
 
42
{
 
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()));
 
52
 
 
53
    QString constraint("'Applet' in [X-Plasma-ComponentTypes] and exist [X-Plasma-PackageFormat]");
 
54
    KService::List offers = KServiceTypeTrader::self()->query("Plasma/ScriptEngine", constraint);
 
55
 
 
56
    QListWidgetItem * item = new QListWidgetItem(KIcon("plasma"), i18n("Plasmoid: Native plasma widget"), m_widgetTypeList);
 
57
    item->setSelected(true);
 
58
    m_widgetTypeList->setCurrentItem(item);
 
59
 
 
60
    foreach (const KService::Ptr &offer, offers) {
 
61
        QString text(offer->name());
 
62
        if (!offer->comment().isEmpty()) {
 
63
            text.append(": ").append(offer->comment());
 
64
        }
 
65
 
 
66
        item = new QListWidgetItem(text, m_widgetTypeList);
 
67
        item->setData(PackageStructureRole, offer->property("X-KDE-PluginInfo-Name"));
 
68
 
 
69
        if (!offer->icon().isEmpty()) {
 
70
            item->setIcon(KIcon(offer->icon()));
 
71
        }
 
72
    }
 
73
 
 
74
    selectLayout->addWidget(selectLabel);
 
75
    selectLayout->addWidget(m_widgetTypeList);
 
76
 
 
77
    m_typePage = new KPageWidgetItem(selectWidget, i18n("Install New Widget From File"));
 
78
    m_typePage->setIcon(KIcon("plasma"));
 
79
    addPage(m_typePage);
 
80
 
 
81
    m_filePageWidget = new QWidget(this);
 
82
    m_filePage = new KPageWidgetItem(m_filePageWidget, i18n("Select File"));
 
83
    addPage(m_filePage);
 
84
 
 
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()));
 
90
}
 
91
 
 
92
void OpenWidgetAssistant::slotItemChanged()
 
93
{
 
94
    enableButton(KDialog::User2, !m_widgetTypeList->selectedItems().isEmpty());
 
95
}
 
96
 
 
97
void OpenWidgetAssistant::prepPage(KPageWidgetItem *current, KPageWidgetItem *before)
 
98
{
 
99
    Q_UNUSED(before);
 
100
    if (m_widgetTypeList->selectedItems().isEmpty()) {
 
101
        return;
 
102
    }
 
103
 
 
104
    if (current != m_filePage) {
 
105
        return;
 
106
    }
 
107
 
 
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);
 
117
    }
 
118
 
 
119
    QListWidgetItem *item = m_widgetTypeList->selectedItems().first();
 
120
    Q_ASSERT(item);
 
121
 
 
122
    QString type = item->data(PackageStructureRole).toString();
 
123
 
 
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);
 
128
 
 
129
        kDebug() << "looking for a Plasma/PackageStructure with" << constraint << type;
 
130
        Q_ASSERT(offers.count() > 0);
 
131
 
 
132
        m_packageStructureService = offers.first();
 
133
        QStringList mimes = m_packageStructureService->property("X-Plasma-PackageFileMimetypes").toStringList();
 
134
 
 
135
        if (mimes.count() > 0) {
 
136
            m_fileDialog->setMimeFilter(mimes);
 
137
        } else {
 
138
            QString filter = m_packageStructureService->property("X-Plasma-PackageFileFilter").toString();
 
139
 
 
140
            if (!filter.isEmpty()) {
 
141
                m_fileDialog->setFilter(filter + '|' + m_packageStructureService->name());
 
142
            }
 
143
        }
 
144
    } else {
 
145
        QStringList mimes;
 
146
        mimes << "application/x-plasma";
 
147
        m_fileDialog->setMimeFilter(mimes);
 
148
    }
 
149
}
 
150
 
 
151
void OpenWidgetAssistant::slotHelpClicked()
 
152
{
 
153
    //enable it when doc will created
 
154
}
 
155
 
 
156
void OpenWidgetAssistant::finished()
 
157
{
 
158
    m_fileDialog->accept(); // how interesting .. accept() must be called before the state is set
 
159
    QString packageFilePath = m_fileDialog->selectedFile();
 
160
    if (packageFilePath.isEmpty()) {
 
161
        //TODO: user visible error handling
 
162
        kDebug() << "hm. no file path?";
 
163
        return;
 
164
    }
 
165
 
 
166
    kDebug() << "selected uri is" << packageFilePath << "of type" << m_fileDialog->currentFilter();
 
167
    PackageStructure *installer = 0;
 
168
    if (m_packageStructureService) {
 
169
        QString error;
 
170
        installer = m_packageStructureService->createInstance<Plasma::PackageStructure>(0, QVariantList(), &error);
 
171
        if (!installer) {
 
172
            kDebug() << "Could not load requested PackageStructure installer "
 
173
                     << m_packageStructureService << ". Error given: " << error;
 
174
            KMessageBox::error(
 
175
                this,
 
176
                i18n("Could not load the required installer %1. "
 
177
                     "The error given was: %2",
 
178
                     m_packageStructureService, error),
 
179
                i18n("Installation Failure"));
 
180
            return;
 
181
        }
 
182
    } else {
 
183
        installer = new PackageStructure;
 
184
    }
 
185
 
 
186
    QString root = KStandardDirs::locateLocal("data", "plasma/plasmoids/");
 
187
    kDebug() << "installing" << packageFilePath << "to root dir of" << root;
 
188
 
 
189
    if (!installer->installPackage(packageFilePath, root)) {
 
190
        KMessageBox::error(this, i18n("Installing the package %1 failed.", packageFilePath),
 
191
                           i18n("Installation Failure"));
 
192
    }
 
193
 
 
194
    delete installer;
 
195
}
 
196
 
 
197
} // Plasma namespace
 
198
 
 
199
#include "openwidgetassistant_p.moc"