~ben-kietzman/ubuntu/quantal/mountmanager/fix-for-598070

« back to all changes in this revision

Viewing changes to sources/gui/pluginsmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2008-08-20 10:22:14 UTC
  • Revision ID: james.westby@ubuntu.com-20080820102214-fv93myu0ncb1503r
Tags: upstream-0.2.4
ImportĀ upstreamĀ versionĀ 0.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//MountManager - the program for easy mounting of storage devices in Linux
 
2
//Copyright (C) 2007-2008 Tikhonov Sergey
 
3
//
 
4
//This file is part of MountManager Gui
 
5
//
 
6
//This program is free software; you can redistribute it and/or
 
7
//modify it under the terms of the GNU General Public License
 
8
//as published by the Free Software Foundation; either version 2
 
9
//of the License, or (at your option) any later version.
 
10
//
 
11
//This program is distributed in the hope that it will be useful,
 
12
//but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
//GNU General Public License for more details.
 
15
//
 
16
//You should have received a copy of the GNU General Public License
 
17
//along with this program; if not, write to the Free Software
 
18
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
#include <QtGui/QSplitter>
 
20
#include <QtGui/QLabel>
 
21
#include <QtGui/QPushButton>
 
22
#include <QtGui/QVBoxLayout>
 
23
#include <QtGui/QHBoxLayout>
 
24
#include <QtGui/QDockWidget>
 
25
#include <QtGui/QListWidget>
 
26
#include <QtGui/QToolBar>
 
27
#include <QtGui/QMenu>
 
28
#include <QtCore/QDir>
 
29
#include <QtCore/QSettings>
 
30
#include <QtCore/QUrl>
 
31
#include <QtCore/QPluginLoader>
 
32
#include <QDebug>
 
33
#include <QDesktopServices>
 
34
#include "pluginsmanager.h"
 
35
#include "plugininterface.h"
 
36
#include "const.h"
 
37
 
 
38
PluginsManager::PluginsManager(QWidget *parent) : QDialog(parent) {
 
39
        pluginLoader = new QPluginLoader(parent);
 
40
        
 
41
        splitter = new QSplitter;
 
42
        
 
43
        listWidget = new QListWidget;
 
44
        connect(listWidget,SIGNAL(currentItemChanged(QListWidgetItem *,QListWidgetItem*)),
 
45
                                this,SLOT(currentItemChangedSlot(QListWidgetItem *,QListWidgetItem *)));
 
46
        
 
47
        rightWidget = new QWidget;
 
48
        
 
49
        headerLabel = new QLabel;
 
50
        
 
51
        descriptionLabel = new QLabel;
 
52
        descriptionLabel->setWordWrap(true);
 
53
        descriptionLabel->setAlignment(Qt::AlignTop);
 
54
        
 
55
        enableButton = new QPushButton(tr("Enable"));
 
56
        enableButton->setIcon(QIcon(ICONS_PATH"enable.png"));
 
57
        enableButton->setEnabled(false);
 
58
        connect(enableButton,SIGNAL(clicked()),this,SLOT(enablePlugin()));
 
59
        
 
60
        disableButton = new QPushButton(tr("Disable"));
 
61
        disableButton->setIcon(QIcon(ICONS_PATH"cancel.png"));
 
62
        disableButton->setEnabled(false);
 
63
        connect(disableButton,SIGNAL(clicked()),this,SLOT(disablePlugin()));
 
64
        
 
65
        showButton = new QPushButton(tr("Show"));
 
66
        showButton->setEnabled(false);
 
67
        connect(showButton,SIGNAL(clicked()),this,SLOT(showPlugin()));
 
68
 
 
69
        downloadButton = new QPushButton(tr("Download plugins"));
 
70
        connect(downloadButton,SIGNAL(clicked()),this,SLOT(downloadPlugins()));
 
71
        
 
72
        closeButton = new QPushButton(tr("Close"));
 
73
        connect(closeButton,SIGNAL(clicked()),this,SLOT(hide()));
 
74
 
 
75
        QHBoxLayout *topLayout = new QHBoxLayout;
 
76
        topLayout->addWidget(enableButton);
 
77
        topLayout->addWidget(disableButton);
 
78
        topLayout->addWidget(showButton);
 
79
        topLayout->addStretch();
 
80
        topLayout->addWidget(downloadButton);   
 
81
 
 
82
        QVBoxLayout *rightWidgetLayout = new QVBoxLayout;
 
83
        rightWidgetLayout->addWidget(headerLabel);
 
84
        rightWidgetLayout->addWidget(descriptionLabel,1);
 
85
        rightWidget->setLayout(rightWidgetLayout);
 
86
        
 
87
        splitter->addWidget(listWidget);
 
88
        splitter->addWidget(rightWidget);
 
89
        splitter->setStretchFactor(1,1);
 
90
        splitter->setCollapsible(0,false);
 
91
        splitter->setCollapsible(1,false);
 
92
        
 
93
        QHBoxLayout *buttonsLayout = new QHBoxLayout;
 
94
        buttonsLayout->addStretch();
 
95
        buttonsLayout->addWidget(closeButton);
 
96
        
 
97
        QVBoxLayout *mainLayout = new QVBoxLayout;
 
98
        mainLayout->addLayout(topLayout);
 
99
        mainLayout->addWidget(splitter);
 
100
        mainLayout->addLayout(buttonsLayout);
 
101
        setLayout(mainLayout);
 
102
 
 
103
        setWindowTitle(tr("Plugins manager"));
 
104
 
 
105
        update();
 
106
        resize(600,300);
 
107
}
 
108
 
 
109
PluginsManager::~PluginsManager() {
 
110
        delete enableButton;
 
111
        delete disableButton;
 
112
        delete showButton;
 
113
        delete downloadButton;
 
114
        delete pluginLoader;
 
115
        delete closeButton;
 
116
        delete listWidget;
 
117
        delete headerLabel;
 
118
        delete descriptionLabel;
 
119
        delete rightWidget;
 
120
        delete splitter;
 
121
}
 
122
 
 
123
void PluginsManager::update() {
 
124
        QSettings settings(ORGANIZATION,PROGRAM_NAME);
 
125
        settings.beginGroup("Plugins");
 
126
        
 
127
        // Scan plugins directory and load available plugins, append them to the list pluginsList
 
128
        QDir pluginsDir(PLUGINS_PATH);
 
129
        foreach (QString pluginName,pluginsDir.entryList(QDir::Files)) {
 
130
                if (!settings.contains(pluginName)) {
 
131
                        pluginLoader->setFileName(pluginsDir.absoluteFilePath(pluginName));
 
132
                        if (MountManagerPlugin *plugin = qobject_cast<MountManagerPlugin *>(pluginLoader->instance())) {
 
133
                                QListWidgetItem *newItem = new QListWidgetItem;
 
134
                                newItem->setText(plugin->pluginName());
 
135
                                QString pluginTypesString;
 
136
                                if (plugin->dialog() != 0)
 
137
                                        pluginTypesString += ',' + tr("dialog");
 
138
                                if (plugin->dockWidget() != 0)
 
139
                                        pluginTypesString += ',' + tr("panel");
 
140
                                if (plugin->menu() != 0)
 
141
                                        pluginTypesString += ',' + tr("menu");
 
142
                                if (plugin->toolBar() != 0)
 
143
                                        pluginTypesString += ',' + tr("tool bar");
 
144
                                pluginTypesString.remove(0,1);
 
145
                                newItem->setData(PluginType,pluginTypesString);
 
146
                                newItem->setData(PluginDescription,plugin->pluginDescription());
 
147
                                newItem->setData(PluginFileName,pluginName);
 
148
                                listWidget->addItem(newItem);
 
149
                                pluginsList.append(plugin);
 
150
                                enablePlugin(newItem);
 
151
                        } else
 
152
                                qDebug() << QString("Plugin with file name: %1 is invalid.").arg(pluginName);
 
153
                }
 
154
                else {
 
155
                        QStringList data;
 
156
                        data = settings.value(pluginName).toStringList();
 
157
                        if (data.count() != 5)
 
158
                                continue;
 
159
                        QListWidgetItem *newItem = new QListWidgetItem;
 
160
                        newItem->setText(data[1]);
 
161
                        newItem->setData(PluginType,data[2]);
 
162
                        newItem->setData(PluginDescription,data[3]);
 
163
                        newItem->setData(PluginFileName,data[0]);
 
164
                        newItem->setData(PluginEnabled,data[4]);
 
165
                        listWidget->addItem(newItem);
 
166
                        if (data[4] == "true") {
 
167
                                pluginLoader->setFileName(QString(PLUGINS_PATH) + data[0]);
 
168
                                if (MountManagerPlugin *plugin = qobject_cast<MountManagerPlugin *>(pluginLoader->instance())) {
 
169
                                        pluginsList.append(plugin);
 
170
                                }
 
171
                                enablePlugin(newItem);
 
172
                        } else {
 
173
                                MountManagerPlugin *plugin = 0;
 
174
                                pluginsList.append(plugin);
 
175
                                newItem->setIcon(QIcon(ICONS_PATH"cancel.png"));
 
176
                        }
 
177
                }
 
178
        }
 
179
 
 
180
        if (pluginsList.isEmpty())
 
181
                headerLabel->setText("<center><b>" + tr("There are no plugins") + "</b></center>");
 
182
        listWidget->setCurrentRow(0);
 
183
        
 
184
        settings.endGroup();
 
185
}
 
186
 
 
187
void PluginsManager::currentItemChangedSlot(QListWidgetItem *currentItem,QListWidgetItem *previousItem) {
 
188
        if (currentItem == 0)
 
189
                currentItem = previousItem;
 
190
        if (currentItem == 0)
 
191
                return;
 
192
        showButton->setEnabled(true);
 
193
        if (currentItem->data(PluginEnabled).toBool()) {
 
194
                enableButton->setEnabled(false);
 
195
                disableButton->setEnabled(true);
 
196
        } else {
 
197
                enableButton->setEnabled(true);
 
198
                disableButton->setEnabled(false);
 
199
        }
 
200
        
 
201
        headerLabel->setText("<center><b>" + currentItem->text() + "</b></center>");
 
202
        descriptionLabel->setText("<b>" + tr("Type") + ":</b> " + currentItem->data(PluginType).toString()
 
203
                                                 + "<br><b>" + tr("Description") + ":</b> " + currentItem->data(PluginDescription).toString());
 
204
}
 
205
 
 
206
QList<MountManagerPlugin *>& PluginsManager::plugins() {
 
207
        return pluginsList;
 
208
}
 
209
 
 
210
void PluginsManager::saveSettings() {
 
211
        QSettings settings(ORGANIZATION,PROGRAM_NAME);
 
212
        settings.beginGroup("Plugins");
 
213
        int count = listWidget->count();
 
214
        for (int i = count-1; i >= 0; i--) {
 
215
                QListWidgetItem *item = listWidget->item(i);
 
216
                QStringList data;
 
217
                data << item->data(PluginFileName).toString() << item->text() << item->data(PluginType).toString() << item->data(PluginDescription).toString() << item->data(PluginEnabled).toString();
 
218
                settings.setValue(item->data(PluginFileName).toString(),data);
 
219
        }
 
220
        settings.endGroup();
 
221
}
 
222
 
 
223
void PluginsManager::enablePlugin(QListWidgetItem *item) {
 
224
        if (item == 0)
 
225
                item = listWidget->currentItem();
 
226
        // if plugin wasn't loaded yet
 
227
        if (pluginsList[listWidget->row(item)] == 0) {
 
228
                pluginLoader->setFileName(QString(PLUGINS_PATH) + item->data(PluginFileName).toString());
 
229
                pluginsList[listWidget->row(item)] = qobject_cast<MountManagerPlugin *>(pluginLoader->instance());
 
230
        }
 
231
        item->setData(PluginEnabled,true);
 
232
        item->setIcon(QIcon(ICONS_PATH"enable.png"));
 
233
        currentItemChangedSlot(item,0);
 
234
        emit (pluginEnabled(listWidget->row(item)));
 
235
}
 
236
 
 
237
void PluginsManager::disablePlugin() {
 
238
        QListWidgetItem *item = listWidget->currentItem();
 
239
        item->setData(PluginEnabled,false);
 
240
        item->setIcon(QIcon(ICONS_PATH"cancel.png"));
 
241
        currentItemChangedSlot(item,0);
 
242
        emit (pluginDisabled(listWidget->row(item)));
 
243
}
 
244
 
 
245
void PluginsManager::showPlugin() {
 
246
        QListWidgetItem *item = listWidget->currentItem();
 
247
        if (!item->data(PluginEnabled).toBool())
 
248
                enablePlugin(item);
 
249
        MountManagerPlugin *plugin = pluginsList[listWidget->row(item)];
 
250
        if (plugin->dialog() != 0)
 
251
                plugin->dialog()->show();
 
252
        if (plugin->dockWidget() != 0)
 
253
                plugin->dockWidget()->show();
 
254
        if (plugin->menu() != 0)
 
255
                plugin->menu()->show();
 
256
        if (plugin->toolBar() != 0)
 
257
                plugin->toolBar()->show();
 
258
}
 
259
 
 
260
bool PluginsManager::isPluginEnabled(MountManagerPlugin *plugin) {
 
261
        QListWidgetItem *item = listWidget->item(pluginsList.indexOf(plugin));
 
262
        return item->data(PluginEnabled).toBool();
 
263
}
 
264
 
 
265
void PluginsManager::downloadPlugins() {
 
266
        if (!QDesktopServices::openUrl(QUrl(PLUGINS_URL)))
 
267
                qDebug() << tr("Cannot to open browser. Open the plugins page yourself: %1").arg(PLUGINS_URL);
 
268
}
 
 
b'\\ No newline at end of file'