2 * SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "previewssettingspage.h"
9 #include "dolphin_generalsettings.h"
10 #include "settings/servicemodel.h"
12 #include <KContextualHelpButton>
13 #include <KIO/PreviewJob>
14 #include <KLocalizedString>
15 #include <KPluginMetaData>
18 #include <QFormLayout>
19 #include <QHBoxLayout>
24 #include <QSortFilterProxyModel>
30 const int DefaultMaxLocalPreviewSize
= 0; // 0 MB
31 const int DefaultMaxRemotePreviewSize
= 0; // 0 MB
32 const bool EnableRemoteFolderThumbnail
= false;
35 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
*parent
)
36 : SettingsPageBase(parent
)
37 , m_initialized(false)
39 , m_enabledPreviewPlugins()
40 , m_localFileSizeBox(nullptr)
41 , m_remoteFileSizeBox(nullptr)
42 , m_enableRemoteFolderThumbnail(nullptr)
44 QVBoxLayout
*topLayout
= new QVBoxLayout(this);
46 QLabel
*showPreviewsLabel
= new QLabel(i18nc("@title:group", "Show previews in the view for:"), this);
48 m_listView
= new QListView(this);
49 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
51 ServiceModel
*serviceModel
= new ServiceModel(this);
52 QSortFilterProxyModel
*proxyModel
= new QSortFilterProxyModel(this);
53 proxyModel
->setSourceModel(serviceModel
);
54 proxyModel
->setSortRole(Qt::DisplayRole
);
55 proxyModel
->setSortCaseSensitivity(Qt::CaseInsensitive
);
57 m_listView
->setModel(proxyModel
);
58 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
59 m_listView
->setUniformItemSizes(true);
61 // i18n: This label forms a full sentence together with the spinbox content.
62 // Depending on the option chosen in the spinbox, it reads "Show previews for [files below n MiB]"
63 // or "Show previews for [files of any size]".
64 QLabel
*localFileSizeLabel
= new QLabel(i18nc("@label:spinbox", "Show previews for"), this);
66 m_localFileSizeBox
= new QSpinBox(this);
67 m_localFileSizeBox
->setSingleStep(1);
68 m_localFileSizeBox
->setPrefix(i18nc("used as a prefix in a spinbox showing e.g. 'Show previews for [files below 3 MiB]'", "files below "));
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(i18nc("e.g. 'Show previews for [files of any size]'", "files of any size"));
73 QHBoxLayout
*localFileSizeBoxLayout
= new QHBoxLayout();
74 localFileSizeBoxLayout
->addWidget(localFileSizeLabel
);
75 localFileSizeBoxLayout
->addWidget(m_localFileSizeBox
);
77 QLabel
*remoteFileSizeLabel
= new QLabel(i18nc("@label:spinbox", "Show previews for"), this);
79 m_remoteFileSizeBox
= new QSpinBox(this);
80 m_remoteFileSizeBox
->setSingleStep(1);
81 m_remoteFileSizeBox
->setPrefix(i18nc("used as a prefix in a spinbox showing e.g. 'Show previews for [files below 3 MiB]'", "files below "));
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(i18nc("e.g. 'Show previews for [no file]'", "no file"));
86 QHBoxLayout
*remoteFileSizeBoxLayout
= new QHBoxLayout();
87 remoteFileSizeBoxLayout
->addWidget(remoteFileSizeLabel
);
88 remoteFileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
90 // Enable remote folder thumbnail option
91 m_enableRemoteFolderThumbnail
= new QCheckBox(i18nc("@option:check", "Show previews for folders"), this);
93 // Make the "Enable preview for remote folder" enabled only when "Remote file limit" is superior to 0
94 m_enableRemoteFolderThumbnail
->setEnabled(m_remoteFileSizeBox
->value() > 0);
95 connect(m_remoteFileSizeBox
, &QSpinBox::valueChanged
, this, [this](int i
) {
96 m_enableRemoteFolderThumbnail
->setEnabled(i
> 0);
99 const auto helpButtonInfo
= xi18nc("@info",
100 "<para>Creating <emphasis>previews</emphasis> for remote folders is "
101 "very intensive in terms of network resource usage.</para>"
102 "<para>Disable this if navigating remote folders in Dolphin "
103 "is slow or when accessing storage over metered connections.</para>");
104 auto contextualHelpButton
= new KContextualHelpButton
{helpButtonInfo
, m_enableRemoteFolderThumbnail
, this};
106 QHBoxLayout
*enableRemoteFolderThumbnailLayout
= new QHBoxLayout();
107 enableRemoteFolderThumbnailLayout
->addWidget(m_enableRemoteFolderThumbnail
);
108 enableRemoteFolderThumbnailLayout
->addWidget(contextualHelpButton
);
110 QFormLayout
*formLayout
= new QFormLayout();
112 QLabel
*localGroupLabel
= new QLabel(i18nc("@title:group", "Local storage:"));
113 // Makes sure it has the same height as the labeled sizeBoxLayout
114 localGroupLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::MinimumExpanding
);
115 formLayout
->addRow(localGroupLabel
, localFileSizeBoxLayout
);
117 QLabel
*remoteGroupLabel
= new QLabel(i18nc("@title:group", "Remote storage:"));
118 remoteGroupLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::MinimumExpanding
);
119 formLayout
->addRow(remoteGroupLabel
, remoteFileSizeBoxLayout
);
121 formLayout
->addRow(QString(), enableRemoteFolderThumbnailLayout
);
123 topLayout
->addWidget(showPreviewsLabel
);
124 topLayout
->addWidget(m_listView
);
125 topLayout
->addLayout(formLayout
);
127 // So that m_listView takes up all available space
128 topLayout
->setStretchFactor(m_listView
, 1);
132 connect(m_listView
, &QListView::clicked
, this, &PreviewsSettingsPage::changed
);
133 connect(m_localFileSizeBox
, &QSpinBox::valueChanged
, this, &PreviewsSettingsPage::changed
);
134 connect(m_remoteFileSizeBox
, &QSpinBox::valueChanged
, this, &PreviewsSettingsPage::changed
);
135 connect(m_enableRemoteFolderThumbnail
, &QCheckBox::toggled
, this, &PreviewsSettingsPage::changed
);
138 PreviewsSettingsPage::~PreviewsSettingsPage()
142 void PreviewsSettingsPage::applySettings()
144 const QAbstractItemModel
*model
= m_listView
->model();
145 const int rowCount
= model
->rowCount();
147 m_enabledPreviewPlugins
.clear();
148 for (int i
= 0; i
< rowCount
; ++i
) {
149 const QModelIndex index
= model
->index(i
, 0);
150 const bool checked
= model
->data(index
, Qt::CheckStateRole
).value
<Qt::CheckState
>() == Qt::Checked
;
152 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
153 m_enabledPreviewPlugins
.append(enabledPlugin
);
158 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
159 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
161 if (!m_localFileSizeBox
->value()) {
162 globalConfig
.deleteEntry("MaximumSize", KConfigBase::Normal
| KConfigBase::Global
);
164 const qulonglong maximumLocalSize
= static_cast<qulonglong
>(m_localFileSizeBox
->value()) * 1024 * 1024;
165 globalConfig
.writeEntry("MaximumSize", maximumLocalSize
, KConfigBase::Normal
| KConfigBase::Global
);
168 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
169 globalConfig
.writeEntry("MaximumRemoteSize", maximumRemoteSize
, KConfigBase::Normal
| KConfigBase::Global
);
171 globalConfig
.writeEntry("EnableRemoteFolderThumbnail", m_enableRemoteFolderThumbnail
->isChecked(), KConfigBase::Normal
| KConfigBase::Global
);
176 void PreviewsSettingsPage::restoreDefaults()
178 m_localFileSizeBox
->setValue(DefaultMaxLocalPreviewSize
);
179 m_remoteFileSizeBox
->setValue(DefaultMaxRemotePreviewSize
);
180 m_enableRemoteFolderThumbnail
->setChecked(EnableRemoteFolderThumbnail
);
183 void PreviewsSettingsPage::showEvent(QShowEvent
*event
)
185 if (!event
->spontaneous() && !m_initialized
) {
186 loadPreviewPlugins();
187 m_initialized
= true;
189 SettingsPageBase::showEvent(event
);
192 void PreviewsSettingsPage::loadPreviewPlugins()
194 QAbstractItemModel
*model
= m_listView
->model();
196 const QVector
<KPluginMetaData
> plugins
= KIO::PreviewJob::availableThumbnailerPlugins();
197 for (const KPluginMetaData
&plugin
: plugins
) {
198 const bool show
= m_enabledPreviewPlugins
.contains(plugin
.pluginId());
201 const QModelIndex index
= model
->index(0, 0);
202 model
->setData(index
, show
? Qt::Checked
: Qt::Unchecked
, Qt::CheckStateRole
);
203 model
->setData(index
, plugin
.name(), Qt::DisplayRole
);
204 model
->setData(index
, plugin
.pluginId(), ServiceModel::DesktopEntryNameRole
);
207 model
->sort(Qt::DisplayRole
);
210 void PreviewsSettingsPage::loadSettings()
212 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
213 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
215 const qulonglong defaultLocalPreview
= static_cast<qulonglong
>(DefaultMaxLocalPreviewSize
) * 1024 * 1024;
216 const qulonglong maxLocalByteSize
= globalConfig
.readEntry("MaximumSize", defaultLocalPreview
);
217 const int maxLocalMByteSize
= maxLocalByteSize
/ (1024 * 1024);
218 m_localFileSizeBox
->setValue(maxLocalMByteSize
);
220 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(DefaultMaxRemotePreviewSize
) * 1024 * 1024;
221 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
222 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
223 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);
225 m_enableRemoteFolderThumbnail
->setChecked(globalConfig
.readEntry("EnableRemoteFolderThumbnail", EnableRemoteFolderThumbnail
));
228 #include "moc_previewssettingspage.cpp"