]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/general/previewssettingspage.cpp
c2f21dfab20b0ed2df16ac3ba002a8ff0703fb80
[dolphin.git] / src / settings / general / previewssettingspage.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "previewssettingspage.h"
8
9 #include "dolphin_generalsettings.h"
10 #include "configurepreviewplugindialog.h"
11 #include "settings/serviceitemdelegate.h"
12 #include "settings/servicemodel.h"
13
14 #include <KIO/PreviewJob>
15 #include <KLocalizedString>
16 #include <KServiceTypeTrader>
17
18 #include <QHBoxLayout>
19 #include <QLabel>
20 #include <QListView>
21 #include <QPainter>
22 #include <QShowEvent>
23 #include <QSortFilterProxyModel>
24 #include <QSpinBox>
25
26 // default settings
27 namespace {
28 const int DefaultMaxLocalPreviewSize = 0; // 0 MB
29 const int DefaultMaxRemotePreviewSize = 0; // 0 MB
30 }
31
32 PreviewsSettingsPage::PreviewsSettingsPage(QWidget* parent) :
33 SettingsPageBase(parent),
34 m_initialized(false),
35 m_listView(nullptr),
36 m_enabledPreviewPlugins(),
37 m_localFileSizeBox(nullptr),
38 m_remoteFileSizeBox(nullptr)
39 {
40 QVBoxLayout* topLayout = new QVBoxLayout(this);
41
42 QLabel* showPreviewsLabel = new QLabel(i18nc("@title:group", "Show previews in the view for:"), this);
43
44 m_listView = new QListView(this);
45
46 ServiceItemDelegate* delegate = new ServiceItemDelegate(m_listView, m_listView);
47 connect(delegate, &ServiceItemDelegate::requestServiceConfiguration,
48 this, &PreviewsSettingsPage::configureService);
49
50 ServiceModel* serviceModel = new ServiceModel(this);
51 QSortFilterProxyModel* proxyModel = new QSortFilterProxyModel(this);
52 proxyModel->setSourceModel(serviceModel);
53 proxyModel->setSortRole(Qt::DisplayRole);
54 proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
55
56 m_listView->setModel(proxyModel);
57 m_listView->setItemDelegate(delegate);
58 m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
59
60 QLabel* localFileSizeLabel = new QLabel(i18n("Skip previews for local files above:"), this);
61
62 m_localFileSizeBox = new QSpinBox(this);
63 m_localFileSizeBox->setSingleStep(1);
64 m_localFileSizeBox->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
65 m_localFileSizeBox->setRange(0, 9999999); /* MB */
66 m_localFileSizeBox->setSpecialValueText(i18n("No limit"));
67
68 QHBoxLayout* localFileSizeBoxLayout = new QHBoxLayout();
69 localFileSizeBoxLayout->addWidget(localFileSizeLabel);
70 localFileSizeBoxLayout->addStretch(0);
71 localFileSizeBoxLayout->addWidget(m_localFileSizeBox);
72
73 QLabel* remoteFileSizeLabel = new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
74
75 m_remoteFileSizeBox = new QSpinBox(this);
76 m_remoteFileSizeBox->setSingleStep(1);
77 m_remoteFileSizeBox->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
78 m_remoteFileSizeBox->setRange(0, 9999999); /* MB */
79 m_remoteFileSizeBox->setSpecialValueText(i18n("No previews"));
80
81 QHBoxLayout* remoteFileSizeBoxLayout = new QHBoxLayout();
82 remoteFileSizeBoxLayout->addWidget(remoteFileSizeLabel);
83 remoteFileSizeBoxLayout->addStretch(0);
84 remoteFileSizeBoxLayout->addWidget(m_remoteFileSizeBox);
85
86 topLayout->addWidget(showPreviewsLabel);
87 topLayout->addWidget(m_listView);
88 topLayout->addLayout(localFileSizeBoxLayout);
89 topLayout->addLayout(remoteFileSizeBoxLayout);
90
91 loadSettings();
92
93 connect(m_listView, &QListView::clicked, this, &PreviewsSettingsPage::changed);
94 connect(m_localFileSizeBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &PreviewsSettingsPage::changed);
95 connect(m_remoteFileSizeBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &PreviewsSettingsPage::changed);
96 }
97
98 PreviewsSettingsPage::~PreviewsSettingsPage()
99 {
100 }
101
102 void PreviewsSettingsPage::applySettings()
103 {
104 const QAbstractItemModel* model = m_listView->model();
105 const int rowCount = model->rowCount();
106 if (rowCount > 0) {
107 m_enabledPreviewPlugins.clear();
108 for (int i = 0; i < rowCount; ++i) {
109 const QModelIndex index = model->index(i, 0);
110 const bool checked = model->data(index, Qt::CheckStateRole).toBool();
111 if (checked) {
112 const QString enabledPlugin = model->data(index, Qt::UserRole).toString();
113 m_enabledPreviewPlugins.append(enabledPlugin);
114 }
115 }
116 }
117
118 KConfigGroup globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
119 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
120
121 if (!m_localFileSizeBox->value()) {
122 globalConfig.deleteEntry("MaximumSize", KConfigBase::Normal | KConfigBase::Global);
123 } else {
124 const qulonglong maximumLocalSize = static_cast<qulonglong>(m_localFileSizeBox->value()) * 1024 * 1024;
125 globalConfig.writeEntry("MaximumSize",
126 maximumLocalSize,
127 KConfigBase::Normal | KConfigBase::Global);
128 }
129
130 const qulonglong maximumRemoteSize = static_cast<qulonglong>(m_remoteFileSizeBox->value()) * 1024 * 1024;
131 globalConfig.writeEntry("MaximumRemoteSize",
132 maximumRemoteSize,
133 KConfigBase::Normal | KConfigBase::Global);
134 globalConfig.sync();
135 }
136
137 void PreviewsSettingsPage::restoreDefaults()
138 {
139 m_localFileSizeBox->setValue(DefaultMaxLocalPreviewSize);
140 m_remoteFileSizeBox->setValue(DefaultMaxRemotePreviewSize);
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(QStringLiteral("ThumbCreator"));
168 foreach (const KService::Ptr& service, plugins) {
169 const bool configurable = service->property(QStringLiteral("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 const KConfigGroup globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
186 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
187
188 const qulonglong defaultLocalPreview = static_cast<qulonglong>(DefaultMaxLocalPreviewSize) * 1024 * 1024;
189 const qulonglong maxLocalByteSize = globalConfig.readEntry("MaximumSize", defaultLocalPreview);
190 const int maxLocalMByteSize = maxLocalByteSize / (1024 * 1024);
191 m_localFileSizeBox->setValue(maxLocalMByteSize);
192
193 const qulonglong defaultRemotePreview = static_cast<qulonglong>(DefaultMaxRemotePreviewSize) * 1024 * 1024;
194 const qulonglong maxRemoteByteSize = globalConfig.readEntry("MaximumRemoteSize", defaultRemotePreview);
195 const int maxRemoteMByteSize = maxRemoteByteSize / (1024 * 1024);
196 m_remoteFileSizeBox->setValue(maxRemoteMByteSize);
197 }