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