1
//MountManager - the program for easy mounting of storage devices in Linux
2
//Copyright (C) 2007-2008 Tikhonov Sergey
4
//This file is part of MountManager Gui
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.
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.
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>
33
#include <QDesktopServices>
34
#include "pluginsmanager.h"
35
#include "plugininterface.h"
38
PluginsManager::PluginsManager(QWidget *parent) : QDialog(parent) {
39
pluginLoader = new QPluginLoader(parent);
41
splitter = new QSplitter;
43
listWidget = new QListWidget;
44
connect(listWidget,SIGNAL(currentItemChanged(QListWidgetItem *,QListWidgetItem*)),
45
this,SLOT(currentItemChangedSlot(QListWidgetItem *,QListWidgetItem *)));
47
rightWidget = new QWidget;
49
headerLabel = new QLabel;
51
descriptionLabel = new QLabel;
52
descriptionLabel->setWordWrap(true);
53
descriptionLabel->setAlignment(Qt::AlignTop);
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()));
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()));
65
showButton = new QPushButton(tr("Show"));
66
showButton->setEnabled(false);
67
connect(showButton,SIGNAL(clicked()),this,SLOT(showPlugin()));
69
downloadButton = new QPushButton(tr("Download plugins"));
70
connect(downloadButton,SIGNAL(clicked()),this,SLOT(downloadPlugins()));
72
closeButton = new QPushButton(tr("Close"));
73
connect(closeButton,SIGNAL(clicked()),this,SLOT(hide()));
75
QHBoxLayout *topLayout = new QHBoxLayout;
76
topLayout->addWidget(enableButton);
77
topLayout->addWidget(disableButton);
78
topLayout->addWidget(showButton);
79
topLayout->addStretch();
80
topLayout->addWidget(downloadButton);
82
QVBoxLayout *rightWidgetLayout = new QVBoxLayout;
83
rightWidgetLayout->addWidget(headerLabel);
84
rightWidgetLayout->addWidget(descriptionLabel,1);
85
rightWidget->setLayout(rightWidgetLayout);
87
splitter->addWidget(listWidget);
88
splitter->addWidget(rightWidget);
89
splitter->setStretchFactor(1,1);
90
splitter->setCollapsible(0,false);
91
splitter->setCollapsible(1,false);
93
QHBoxLayout *buttonsLayout = new QHBoxLayout;
94
buttonsLayout->addStretch();
95
buttonsLayout->addWidget(closeButton);
97
QVBoxLayout *mainLayout = new QVBoxLayout;
98
mainLayout->addLayout(topLayout);
99
mainLayout->addWidget(splitter);
100
mainLayout->addLayout(buttonsLayout);
101
setLayout(mainLayout);
103
setWindowTitle(tr("Plugins manager"));
109
PluginsManager::~PluginsManager() {
111
delete disableButton;
113
delete downloadButton;
118
delete descriptionLabel;
123
void PluginsManager::update() {
124
QSettings settings(ORGANIZATION,PROGRAM_NAME);
125
settings.beginGroup("Plugins");
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);
152
qDebug() << QString("Plugin with file name: %1 is invalid.").arg(pluginName);
156
data = settings.value(pluginName).toStringList();
157
if (data.count() != 5)
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);
171
enablePlugin(newItem);
173
MountManagerPlugin *plugin = 0;
174
pluginsList.append(plugin);
175
newItem->setIcon(QIcon(ICONS_PATH"cancel.png"));
180
if (pluginsList.isEmpty())
181
headerLabel->setText("<center><b>" + tr("There are no plugins") + "</b></center>");
182
listWidget->setCurrentRow(0);
187
void PluginsManager::currentItemChangedSlot(QListWidgetItem *currentItem,QListWidgetItem *previousItem) {
188
if (currentItem == 0)
189
currentItem = previousItem;
190
if (currentItem == 0)
192
showButton->setEnabled(true);
193
if (currentItem->data(PluginEnabled).toBool()) {
194
enableButton->setEnabled(false);
195
disableButton->setEnabled(true);
197
enableButton->setEnabled(true);
198
disableButton->setEnabled(false);
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());
206
QList<MountManagerPlugin *>& PluginsManager::plugins() {
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);
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);
223
void PluginsManager::enablePlugin(QListWidgetItem *item) {
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());
231
item->setData(PluginEnabled,true);
232
item->setIcon(QIcon(ICONS_PATH"enable.png"));
233
currentItemChangedSlot(item,0);
234
emit (pluginEnabled(listWidget->row(item)));
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)));
245
void PluginsManager::showPlugin() {
246
QListWidgetItem *item = listWidget->currentItem();
247
if (!item->data(PluginEnabled).toBool())
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();
260
bool PluginsManager::isPluginEnabled(MountManagerPlugin *plugin) {
261
QListWidgetItem *item = listWidget->item(pluginsList.indexOf(plugin));
262
return item->data(PluginEnabled).toBool();
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);
b'\\ No newline at end of file'