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