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