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