]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/general/previewssettingspage.cpp
PreviewSettingsPage: update JPG preview plugin settings if needed
[dolphin.git] / src / settings / general / previewssettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
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 General Public License *
15 * 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 "previewssettingspage.h"
21
22 #include "dolphin_generalsettings.h"
23 #include "configurepreviewplugindialog.h"
24
25 #include <KConfigGroup>
26 #include <KDialog>
27 #include <KGlobal>
28 #include <KLocale>
29 #include <KNumInput>
30 #include <KServiceTypeTrader>
31 #include <KService>
32
33 #include <settings/dolphinsettings.h>
34 #include <settings/serviceitemdelegate.h>
35 #include <settings/servicemodel.h>
36
37 #include <QCheckBox>
38 #include <QGroupBox>
39 #include <QLabel>
40 #include <QListView>
41 #include <QPainter>
42 #include <QScrollBar>
43 #include <QShowEvent>
44 #include <QSlider>
45 #include <QSortFilterProxyModel>
46 #include <QGridLayout>
47 #include <QVBoxLayout>
48
49 // default settings
50 namespace {
51 const bool UseThumbnails = true;
52 const int MaxLocalPreviewSize = 5; // 5 MB
53 const int MaxRemotePreviewSize = 0; // 0 MB
54 }
55
56 PreviewsSettingsPage::PreviewsSettingsPage(QWidget* parent) :
57 SettingsPageBase(parent),
58 m_initialized(false),
59 m_listView(0),
60 m_enabledPreviewPlugins(),
61 m_localFileSizeBox(0),
62 m_remoteFileSizeBox(0)
63 {
64 QVBoxLayout* topLayout = new QVBoxLayout(this);
65 topLayout->setSpacing(KDialog::spacingHint());
66 topLayout->setMargin(KDialog::marginHint());
67
68 // Create group box "Show previews for:"
69 QGroupBox* listBox = new QGroupBox(i18nc("@title:group", "Show previews for"), this);
70
71 m_listView = new QListView(this);
72
73 ServiceItemDelegate* delegate = new ServiceItemDelegate(m_listView, m_listView);
74 connect(delegate, SIGNAL(requestServiceConfiguration(QModelIndex)),
75 this, SLOT(configureService(QModelIndex)));
76
77 ServiceModel* serviceModel = new ServiceModel(this);
78 QSortFilterProxyModel* proxyModel = new QSortFilterProxyModel(this);
79 proxyModel->setSourceModel(serviceModel);
80 proxyModel->setSortRole(Qt::DisplayRole);
81
82 m_listView->setModel(proxyModel);
83 m_listView->setItemDelegate(delegate);
84 m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
85
86 QVBoxLayout* listBoxLayout = new QVBoxLayout(listBox);
87 listBoxLayout->addWidget(m_listView);
88
89 // Create group box "Don't create previews for"
90 QGroupBox* fileSizeBox = new QGroupBox(i18nc("@title:group", "Do not create previews for"), this);
91
92 QLabel* localFileSizeLabel = new QLabel(i18nc("@label Don't create previews for: <Local files above:> XX MByte",
93 "Local files above:"), this);
94
95 m_localFileSizeBox = new KIntSpinBox(this);
96 m_localFileSizeBox->setSingleStep(1);
97 m_localFileSizeBox->setSuffix(QLatin1String(" MB"));
98 m_localFileSizeBox->setRange(0, 9999); /* MB */
99
100 QLabel* remoteFileSizeLabel = new QLabel(i18nc("@label Don't create previews for: <Remote files above:> XX MByte",
101 "Remote files above:"), this);
102
103 m_remoteFileSizeBox = new KIntSpinBox(this);
104 m_remoteFileSizeBox->setSingleStep(1);
105 m_remoteFileSizeBox->setSuffix(QLatin1String(" MB"));
106 m_remoteFileSizeBox->setRange(0, 9999); /* MB */
107
108 QGridLayout* fileSizeBoxLayout = new QGridLayout(fileSizeBox);
109 fileSizeBoxLayout->addWidget(localFileSizeLabel, 0, 0, Qt::AlignRight);
110 fileSizeBoxLayout->addWidget(m_localFileSizeBox, 0, 1);
111 fileSizeBoxLayout->addWidget(remoteFileSizeLabel, 1, 0, Qt::AlignRight);
112 fileSizeBoxLayout->addWidget(m_remoteFileSizeBox, 1, 1);
113
114 topLayout->addWidget(listBox);
115 topLayout->addWidget(fileSizeBox);
116
117 loadSettings();
118
119 connect(m_listView, SIGNAL(clicked(QModelIndex)), this, SIGNAL(changed()));
120 connect(m_localFileSizeBox, SIGNAL(valueChanged(int)), this, SIGNAL(changed()));
121 connect(m_remoteFileSizeBox, SIGNAL(valueChanged(int)), this, SIGNAL(changed()));
122 }
123
124 PreviewsSettingsPage::~PreviewsSettingsPage()
125 {
126 }
127
128 void PreviewsSettingsPage::applySettings()
129 {
130 const QAbstractItemModel* model = m_listView->model();
131 const int rowCount = model->rowCount();
132 if (rowCount > 0) {
133 m_enabledPreviewPlugins.clear();
134 for (int i = 0; i < rowCount; ++i) {
135 const QModelIndex index = model->index(i, 0);
136 const bool checked = model->data(index, Qt::CheckStateRole).toBool();
137 if (checked) {
138 const QString enabledPlugin = model->data(index, Qt::UserRole).toString();
139 m_enabledPreviewPlugins.append(enabledPlugin);
140 }
141 }
142 }
143
144 KConfigGroup globalConfig(KGlobal::config(), QLatin1String("PreviewSettings"));
145 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
146
147 globalConfig.writeEntry("MaximumSize",
148 m_localFileSizeBox->value() * 1024 * 1024,
149 KConfigBase::Normal | KConfigBase::Global);
150 globalConfig.writeEntry("MaximumRemoteSize",
151 m_remoteFileSizeBox->value() * 1024 * 1024,
152 KConfigBase::Normal | KConfigBase::Global);
153 globalConfig.sync();
154 }
155
156 void PreviewsSettingsPage::restoreDefaults()
157 {
158 m_localFileSizeBox->setValue(MaxLocalPreviewSize);
159 m_remoteFileSizeBox->setValue(MaxRemotePreviewSize);
160 }
161
162 void PreviewsSettingsPage::showEvent(QShowEvent* event)
163 {
164 if (!event->spontaneous() && !m_initialized) {
165 loadPreviewPlugins();
166 m_initialized = true;
167 }
168 SettingsPageBase::showEvent(event);
169 }
170
171 void PreviewsSettingsPage::configureService(const QModelIndex& index)
172 {
173 const QAbstractItemModel* model = index.model();
174 const QString pluginName = model->data(index).toString();
175 const QString desktopEntryName = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
176
177 ConfigurePreviewPluginDialog* dialog = new ConfigurePreviewPluginDialog(pluginName, desktopEntryName, this);
178 dialog->setAttribute(Qt::WA_DeleteOnClose);
179 dialog->show();
180 }
181
182 void PreviewsSettingsPage::loadPreviewPlugins()
183 {
184 QAbstractItemModel* model = m_listView->model();
185
186 const KService::List plugins = KServiceTypeTrader::self()->query(QLatin1String("ThumbCreator"));
187 foreach (const KSharedPtr<KService>& service, plugins) {
188 const bool configurable = service->property("Configurable", QVariant::Bool).toBool();
189 const bool show = m_enabledPreviewPlugins.contains(service->desktopEntryName());
190
191 model->insertRow(0);
192 const QModelIndex index = model->index(0, 0);
193 model->setData(index, show, Qt::CheckStateRole);
194 model->setData(index, configurable, ServiceModel::ConfigurableRole);
195 model->setData(index, service->name(), Qt::DisplayRole);
196 model->setData(index, service->desktopEntryName(), ServiceModel::DesktopEntryNameRole);
197 }
198
199 model->sort(Qt::DisplayRole);
200 }
201
202 void PreviewsSettingsPage::loadSettings()
203 {
204 KConfigGroup globalConfig(KGlobal::config(), "PreviewSettings");
205 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", QStringList()
206 << QLatin1String("directorythumbnail")
207 << QLatin1String("imagethumbnail")
208 << QLatin1String("jpegthumbnail"));
209
210 // If the user is upgrading from KDE <= 4.6, we must check if he had the 'jpegrotatedthumbnail' plugin enabled.
211 // This plugin does not exist any more in KDE >= 4.7, so we have to replace it with the 'jpegthumbnail' plugin.
212 //
213 // Note that the upgrade to the correct plugin is done already in KFilePreviewGenerator. However, if Konqueror is
214 // opened in web browsing mode and the Settings dialog is opened, we might end up here before KFilePreviewGenerator's
215 // constructor is ever called -> the plugin replacement should be done here as well.
216 if(m_enabledPreviewPlugins.contains(QLatin1String("jpegrotatedthumbnail"))) {
217 m_enabledPreviewPlugins.removeAll(QLatin1String("jpegrotatedthumbnail"));
218 m_enabledPreviewPlugins.append(QLatin1String("jpegthumbnail"));
219 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
220 globalConfig.sync();
221 }
222
223 const int maxLocalByteSize = globalConfig.readEntry("MaximumSize", MaxLocalPreviewSize * 1024 * 1024);
224 const int maxLocalMByteSize = maxLocalByteSize / (1024 * 1024);
225 m_localFileSizeBox->setValue(maxLocalMByteSize);
226
227 const int maxRemoteByteSize = globalConfig.readEntry("MaximumRemoteSize", MaxRemotePreviewSize * 1024 * 1024);
228 const int maxRemoteMByteSize = maxRemoteByteSize / (1024 * 1024);
229 m_remoteFileSizeBox->setValue(maxRemoteMByteSize);
230 }
231
232 #include "previewssettingspage.moc"