]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/general/previewssettingspage.cpp
3441d2ecaf70035e7b6d81fa83ea2d9858179644
[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(i18n("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(i18n("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 if (!m_localFileSizeBox->value()) {
135 globalConfig.deleteEntry("MaximumSize", KConfigBase::Normal | KConfigBase::Global);
136 } else {
137 const qulonglong maximumLocalSize = static_cast<qulonglong>(m_localFileSizeBox->value()) * 1024 * 1024;
138 globalConfig.writeEntry("MaximumSize",
139 maximumLocalSize,
140 KConfigBase::Normal | KConfigBase::Global);
141 }
142
143 const qulonglong maximumRemoteSize = static_cast<qulonglong>(m_remoteFileSizeBox->value()) * 1024 * 1024;
144 globalConfig.writeEntry("MaximumRemoteSize",
145 maximumRemoteSize,
146 KConfigBase::Normal | KConfigBase::Global);
147 globalConfig.sync();
148 }
149
150 void PreviewsSettingsPage::restoreDefaults()
151 {
152 m_localFileSizeBox->setValue(DefaultMaxLocalPreviewSize);
153 m_remoteFileSizeBox->setValue(DefaultMaxRemotePreviewSize);
154 }
155
156 void PreviewsSettingsPage::showEvent(QShowEvent* event)
157 {
158 if (!event->spontaneous() && !m_initialized) {
159 loadPreviewPlugins();
160 m_initialized = true;
161 }
162 SettingsPageBase::showEvent(event);
163 }
164
165 void PreviewsSettingsPage::configureService(const QModelIndex& index)
166 {
167 const QAbstractItemModel* model = index.model();
168 const QString pluginName = model->data(index).toString();
169 const QString desktopEntryName = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
170
171 ConfigurePreviewPluginDialog* dialog = new ConfigurePreviewPluginDialog(pluginName, desktopEntryName, this);
172 dialog->setAttribute(Qt::WA_DeleteOnClose);
173 dialog->show();
174 }
175
176 void PreviewsSettingsPage::loadPreviewPlugins()
177 {
178 QAbstractItemModel* model = m_listView->model();
179
180 const KService::List plugins = KServiceTypeTrader::self()->query(QStringLiteral("ThumbCreator"));
181 foreach (const KService::Ptr& service, plugins) {
182 const bool configurable = service->property(QStringLiteral("Configurable"), QVariant::Bool).toBool();
183 const bool show = m_enabledPreviewPlugins.contains(service->desktopEntryName());
184
185 model->insertRow(0);
186 const QModelIndex index = model->index(0, 0);
187 model->setData(index, show, Qt::CheckStateRole);
188 model->setData(index, configurable, ServiceModel::ConfigurableRole);
189 model->setData(index, service->name(), Qt::DisplayRole);
190 model->setData(index, service->desktopEntryName(), ServiceModel::DesktopEntryNameRole);
191 }
192
193 model->sort(Qt::DisplayRole);
194 }
195
196 void PreviewsSettingsPage::loadSettings()
197 {
198 const KConfigGroup globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
199 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
200
201 const qulonglong defaultLocalPreview = static_cast<qulonglong>(DefaultMaxLocalPreviewSize) * 1024 * 1024;
202 const qulonglong maxLocalByteSize = globalConfig.readEntry("MaximumSize", defaultLocalPreview);
203 const int maxLocalMByteSize = maxLocalByteSize / (1024 * 1024);
204 m_localFileSizeBox->setValue(maxLocalMByteSize);
205
206 const qulonglong defaultRemotePreview = static_cast<qulonglong>(DefaultMaxRemotePreviewSize) * 1024 * 1024;
207 const qulonglong maxRemoteByteSize = globalConfig.readEntry("MaximumRemoteSize", defaultRemotePreview);
208 const int maxRemoteMByteSize = maxRemoteByteSize / (1024 * 1024);
209 m_remoteFileSizeBox->setValue(maxRemoteMByteSize);
210 }