]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/general/previewssettingspage.cpp
Provide ability to configure size cut-off for local file 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 #include "settings/serviceitemdelegate.h"
25 #include "settings/servicemodel.h"
26
27 #include <KIO/PreviewJob>
28 #include <KLocalizedString>
29 #include <KServiceTypeTrader>
30
31 #include <QHBoxLayout>
32 #include <QLabel>
33 #include <QListView>
34 #include <QPainter>
35 #include <QShowEvent>
36 #include <QSortFilterProxyModel>
37 #include <QSpinBox>
38
39 // default settings
40 namespace {
41 const int DefaultMaxLocalPreviewSize = 0; // 0 MB
42 const int DefaultMaxRemotePreviewSize = 0; // 0 MB
43 }
44
45 PreviewsSettingsPage::PreviewsSettingsPage(QWidget* parent) :
46 SettingsPageBase(parent),
47 m_initialized(false),
48 m_listView(nullptr),
49 m_enabledPreviewPlugins(),
50 m_localFileSizeBox(nullptr),
51 m_remoteFileSizeBox(nullptr)
52 {
53 QVBoxLayout* topLayout = new QVBoxLayout(this);
54
55 QLabel* showPreviewsLabel = new QLabel(i18nc("@title:group", "Show previews in the view for:"), this);
56
57 m_listView = new QListView(this);
58
59 ServiceItemDelegate* delegate = new ServiceItemDelegate(m_listView, m_listView);
60 connect(delegate, &ServiceItemDelegate::requestServiceConfiguration,
61 this, &PreviewsSettingsPage::configureService);
62
63 ServiceModel* serviceModel = new ServiceModel(this);
64 QSortFilterProxyModel* proxyModel = new QSortFilterProxyModel(this);
65 proxyModel->setSourceModel(serviceModel);
66 proxyModel->setSortRole(Qt::DisplayRole);
67 proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
68
69 m_listView->setModel(proxyModel);
70 m_listView->setItemDelegate(delegate);
71 m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
72
73 QLabel* localFileSizeLabel = new QLabel(i18n("Skip previews for local files above:"), this);
74
75 m_localFileSizeBox = new QSpinBox(this);
76 m_localFileSizeBox->setSingleStep(1);
77 m_localFileSizeBox->setSuffix(QStringLiteral(" MB"));
78 m_localFileSizeBox->setRange(0, 9999999); /* MB */
79 m_localFileSizeBox->setSpecialValueText(tr("No limit"));
80
81 QHBoxLayout* localFileSizeBoxLayout = new QHBoxLayout();
82 localFileSizeBoxLayout->addWidget(localFileSizeLabel);
83 localFileSizeBoxLayout->addStretch(0);
84 localFileSizeBoxLayout->addWidget(m_localFileSizeBox);
85
86 QLabel* remoteFileSizeLabel = new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
87
88 m_remoteFileSizeBox = new QSpinBox(this);
89 m_remoteFileSizeBox->setSingleStep(1);
90 m_remoteFileSizeBox->setSuffix(QStringLiteral(" MB"));
91 m_remoteFileSizeBox->setRange(0, 9999999); /* MB */
92 m_remoteFileSizeBox->setSpecialValueText(tr("No previews"));
93
94 QHBoxLayout* remoteFileSizeBoxLayout = new QHBoxLayout();
95 remoteFileSizeBoxLayout->addWidget(remoteFileSizeLabel);
96 remoteFileSizeBoxLayout->addStretch(0);
97 remoteFileSizeBoxLayout->addWidget(m_remoteFileSizeBox);
98
99 topLayout->addWidget(showPreviewsLabel);
100 topLayout->addWidget(m_listView);
101 topLayout->addLayout(localFileSizeBoxLayout);
102 topLayout->addLayout(remoteFileSizeBoxLayout);
103
104 loadSettings();
105
106 connect(m_listView, &QListView::clicked, this, &PreviewsSettingsPage::changed);
107 connect(m_localFileSizeBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &PreviewsSettingsPage::changed);
108 connect(m_remoteFileSizeBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &PreviewsSettingsPage::changed);
109 }
110
111 PreviewsSettingsPage::~PreviewsSettingsPage()
112 {
113 }
114
115 void PreviewsSettingsPage::applySettings()
116 {
117 const QAbstractItemModel* model = m_listView->model();
118 const int rowCount = model->rowCount();
119 if (rowCount > 0) {
120 m_enabledPreviewPlugins.clear();
121 for (int i = 0; i < rowCount; ++i) {
122 const QModelIndex index = model->index(i, 0);
123 const bool checked = model->data(index, Qt::CheckStateRole).toBool();
124 if (checked) {
125 const QString enabledPlugin = model->data(index, Qt::UserRole).toString();
126 m_enabledPreviewPlugins.append(enabledPlugin);
127 }
128 }
129 }
130
131 KConfigGroup globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
132 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
133
134 const qulonglong maximumLocalSize = static_cast<qulonglong>(m_localFileSizeBox->value()) * 1024 * 1024;
135 globalConfig.writeEntry("MaximumSize",
136 maximumLocalSize,
137 KConfigBase::Normal | KConfigBase::Global);
138
139 const qulonglong maximumRemoteSize = static_cast<qulonglong>(m_remoteFileSizeBox->value()) * 1024 * 1024;
140 globalConfig.writeEntry("MaximumRemoteSize",
141 maximumRemoteSize,
142 KConfigBase::Normal | KConfigBase::Global);
143 globalConfig.sync();
144 }
145
146 void PreviewsSettingsPage::restoreDefaults()
147 {
148 m_localFileSizeBox->setValue(DefaultMaxLocalPreviewSize);
149 m_remoteFileSizeBox->setValue(DefaultMaxRemotePreviewSize);
150 }
151
152 void PreviewsSettingsPage::showEvent(QShowEvent* event)
153 {
154 if (!event->spontaneous() && !m_initialized) {
155 loadPreviewPlugins();
156 m_initialized = true;
157 }
158 SettingsPageBase::showEvent(event);
159 }
160
161 void PreviewsSettingsPage::configureService(const QModelIndex& index)
162 {
163 const QAbstractItemModel* model = index.model();
164 const QString pluginName = model->data(index).toString();
165 const QString desktopEntryName = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
166
167 ConfigurePreviewPluginDialog* dialog = new ConfigurePreviewPluginDialog(pluginName, desktopEntryName, this);
168 dialog->setAttribute(Qt::WA_DeleteOnClose);
169 dialog->show();
170 }
171
172 void PreviewsSettingsPage::loadPreviewPlugins()
173 {
174 QAbstractItemModel* model = m_listView->model();
175
176 const KService::List plugins = KServiceTypeTrader::self()->query(QStringLiteral("ThumbCreator"));
177 foreach (const KService::Ptr& service, plugins) {
178 const bool configurable = service->property(QStringLiteral("Configurable"), QVariant::Bool).toBool();
179 const bool show = m_enabledPreviewPlugins.contains(service->desktopEntryName());
180
181 model->insertRow(0);
182 const QModelIndex index = model->index(0, 0);
183 model->setData(index, show, Qt::CheckStateRole);
184 model->setData(index, configurable, ServiceModel::ConfigurableRole);
185 model->setData(index, service->name(), Qt::DisplayRole);
186 model->setData(index, service->desktopEntryName(), ServiceModel::DesktopEntryNameRole);
187 }
188
189 model->sort(Qt::DisplayRole);
190 }
191
192 void PreviewsSettingsPage::loadSettings()
193 {
194 const KConfigGroup globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
195 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
196
197 const qulonglong defaultLocalPreview = static_cast<qulonglong>(DefaultMaxLocalPreviewSize) * 1024 * 1024;
198 const qulonglong maxLocalByteSize = globalConfig.readEntry("MaximumSize", defaultLocalPreview);
199 const int maxLocalMByteSize = maxLocalByteSize / (1024 * 1024);
200 m_localFileSizeBox->setValue(maxLocalMByteSize);
201
202 const qulonglong defaultRemotePreview = static_cast<qulonglong>(DefaultMaxRemotePreviewSize) * 1024 * 1024;
203 const qulonglong maxRemoteByteSize = globalConfig.readEntry("MaximumRemoteSize", defaultRemotePreview);
204 const int maxRemoteMByteSize = maxRemoteByteSize / (1024 * 1024);
205 m_remoteFileSizeBox->setValue(maxRemoteMByteSize);
206 }