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