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