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