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 "configurepreviewplugindialog.h"
11 #include "settings/serviceitemdelegate.h"
12 #include "settings/servicemodel.h"
14 #include <KIO/PreviewJob>
15 #include <KLocalizedString>
16 #include <KPluginMetaData>
18 #include <QHBoxLayout>
24 #include <QSortFilterProxyModel>
29 const int DefaultMaxLocalPreviewSize
= 0; // 0 MB
30 const int DefaultMaxRemotePreviewSize
= 0; // 0 MB
33 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
* parent
) :
34 SettingsPageBase(parent
),
37 m_enabledPreviewPlugins(),
38 m_localFileSizeBox(nullptr),
39 m_remoteFileSizeBox(nullptr)
41 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
43 QLabel
* showPreviewsLabel
= new QLabel(i18nc("@title:group", "Show previews in the view for:"), this);
45 m_listView
= new QListView(this);
46 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
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
);
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
);
61 m_listView
->setModel(proxyModel
);
62 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
63 m_listView
->setUniformItemSizes(true);
65 QLabel
* localFileSizeLabel
= new QLabel(i18n("Skip previews for local files above:"), this);
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"));
73 QHBoxLayout
* localFileSizeBoxLayout
= new QHBoxLayout();
74 localFileSizeBoxLayout
->addWidget(localFileSizeLabel
);
75 localFileSizeBoxLayout
->addStretch(0);
76 localFileSizeBoxLayout
->addWidget(m_localFileSizeBox
);
78 QLabel
* remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
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"));
86 QHBoxLayout
* remoteFileSizeBoxLayout
= new QHBoxLayout();
87 remoteFileSizeBoxLayout
->addWidget(remoteFileSizeLabel
);
88 remoteFileSizeBoxLayout
->addStretch(0);
89 remoteFileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
91 topLayout
->addWidget(showPreviewsLabel
);
92 topLayout
->addWidget(m_listView
);
93 topLayout
->addLayout(localFileSizeBoxLayout
);
94 topLayout
->addLayout(remoteFileSizeBoxLayout
);
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
);
103 PreviewsSettingsPage::~PreviewsSettingsPage()
107 void PreviewsSettingsPage::applySettings()
109 const QAbstractItemModel
* model
= m_listView
->model();
110 const int rowCount
= model
->rowCount();
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();
117 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
118 m_enabledPreviewPlugins
.append(enabledPlugin
);
123 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
124 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
126 if (!m_localFileSizeBox
->value()) {
127 globalConfig
.deleteEntry("MaximumSize", KConfigBase::Normal
| KConfigBase::Global
);
129 const qulonglong maximumLocalSize
= static_cast<qulonglong
>(m_localFileSizeBox
->value()) * 1024 * 1024;
130 globalConfig
.writeEntry("MaximumSize",
132 KConfigBase::Normal
| KConfigBase::Global
);
135 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
136 globalConfig
.writeEntry("MaximumRemoteSize",
138 KConfigBase::Normal
| KConfigBase::Global
);
143 void PreviewsSettingsPage::restoreDefaults()
145 m_localFileSizeBox
->setValue(DefaultMaxLocalPreviewSize
);
146 m_remoteFileSizeBox
->setValue(DefaultMaxRemotePreviewSize
);
149 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
151 if (!event
->spontaneous() && !m_initialized
) {
152 loadPreviewPlugins();
153 m_initialized
= true;
155 SettingsPageBase::showEvent(event
);
158 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 87)
159 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
161 const QAbstractItemModel
* model
= index
.model();
162 const QString pluginName
= model
->data(index
).toString();
163 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
165 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
166 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
171 void PreviewsSettingsPage::loadPreviewPlugins()
173 QAbstractItemModel
* model
= m_listView
->model();
175 const QVector
<KPluginMetaData
> plugins
= KIO::PreviewJob::availableThumbnailerPlugins();
176 for (const KPluginMetaData
&plugin
: plugins
) {
177 const bool show
= m_enabledPreviewPlugins
.contains(plugin
.pluginId());
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
);
185 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 87)
186 const bool configurable
= plugin
.value(QStringLiteral("Configurable"), false);
187 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
191 model
->sort(Qt::DisplayRole
);
194 void PreviewsSettingsPage::loadSettings()
196 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
197 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
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
);
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
);