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>
17 #include <KServiceTypeTrader>
19 #include <QHBoxLayout>
25 #include <QSortFilterProxyModel>
30 const int DefaultMaxLocalPreviewSize
= 0; // 0 MB
31 const int DefaultMaxRemotePreviewSize
= 0; // 0 MB
34 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
* parent
) :
35 SettingsPageBase(parent
),
38 m_enabledPreviewPlugins(),
39 m_localFileSizeBox(nullptr),
40 m_remoteFileSizeBox(nullptr)
42 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
44 QLabel
* showPreviewsLabel
= new QLabel(i18nc("@title:group", "Show previews in the view for:"), this);
46 m_listView
= new QListView(this);
47 QScroller::grabGesture(m_listView
->viewport(), QScroller::TouchGesture
);
49 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 87)
50 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
51 connect(delegate
, &ServiceItemDelegate::requestServiceConfiguration
,
52 this, &PreviewsSettingsPage::configureService
);
53 m_listView
->setItemDelegate(delegate
);
56 ServiceModel
* serviceModel
= new ServiceModel(this);
57 QSortFilterProxyModel
* proxyModel
= new QSortFilterProxyModel(this);
58 proxyModel
->setSourceModel(serviceModel
);
59 proxyModel
->setSortRole(Qt::DisplayRole
);
60 proxyModel
->setSortCaseSensitivity(Qt::CaseInsensitive
);
62 m_listView
->setModel(proxyModel
);
63 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
64 m_listView
->setUniformItemSizes(true);
66 QLabel
* localFileSizeLabel
= new QLabel(i18n("Skip previews for local files above:"), this);
68 m_localFileSizeBox
= new QSpinBox(this);
69 m_localFileSizeBox
->setSingleStep(1);
70 m_localFileSizeBox
->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
71 m_localFileSizeBox
->setRange(0, 9999999); /* MB */
72 m_localFileSizeBox
->setSpecialValueText(i18n("No limit"));
74 QHBoxLayout
* localFileSizeBoxLayout
= new QHBoxLayout();
75 localFileSizeBoxLayout
->addWidget(localFileSizeLabel
);
76 localFileSizeBoxLayout
->addStretch(0);
77 localFileSizeBoxLayout
->addWidget(m_localFileSizeBox
);
79 QLabel
* remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
81 m_remoteFileSizeBox
= new QSpinBox(this);
82 m_remoteFileSizeBox
->setSingleStep(1);
83 m_remoteFileSizeBox
->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
84 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
85 m_remoteFileSizeBox
->setSpecialValueText(i18n("No previews"));
87 QHBoxLayout
* remoteFileSizeBoxLayout
= new QHBoxLayout();
88 remoteFileSizeBoxLayout
->addWidget(remoteFileSizeLabel
);
89 remoteFileSizeBoxLayout
->addStretch(0);
90 remoteFileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
92 topLayout
->addWidget(showPreviewsLabel
);
93 topLayout
->addWidget(m_listView
);
94 topLayout
->addLayout(localFileSizeBoxLayout
);
95 topLayout
->addLayout(remoteFileSizeBoxLayout
);
99 connect(m_listView
, &QListView::clicked
, this, &PreviewsSettingsPage::changed
);
100 connect(m_localFileSizeBox
, &QSpinBox::valueChanged
, this, &PreviewsSettingsPage::changed
);
101 connect(m_remoteFileSizeBox
, &QSpinBox::valueChanged
, this, &PreviewsSettingsPage::changed
);
104 PreviewsSettingsPage::~PreviewsSettingsPage()
108 void PreviewsSettingsPage::applySettings()
110 const QAbstractItemModel
* model
= m_listView
->model();
111 const int rowCount
= model
->rowCount();
113 m_enabledPreviewPlugins
.clear();
114 for (int i
= 0; i
< rowCount
; ++i
) {
115 const QModelIndex index
= model
->index(i
, 0);
116 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
118 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
119 m_enabledPreviewPlugins
.append(enabledPlugin
);
124 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
125 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
127 if (!m_localFileSizeBox
->value()) {
128 globalConfig
.deleteEntry("MaximumSize", KConfigBase::Normal
| KConfigBase::Global
);
130 const qulonglong maximumLocalSize
= static_cast<qulonglong
>(m_localFileSizeBox
->value()) * 1024 * 1024;
131 globalConfig
.writeEntry("MaximumSize",
133 KConfigBase::Normal
| KConfigBase::Global
);
136 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
137 globalConfig
.writeEntry("MaximumRemoteSize",
139 KConfigBase::Normal
| KConfigBase::Global
);
144 void PreviewsSettingsPage::restoreDefaults()
146 m_localFileSizeBox
->setValue(DefaultMaxLocalPreviewSize
);
147 m_remoteFileSizeBox
->setValue(DefaultMaxRemotePreviewSize
);
150 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
152 if (!event
->spontaneous() && !m_initialized
) {
153 loadPreviewPlugins();
154 m_initialized
= true;
156 SettingsPageBase::showEvent(event
);
159 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 87)
160 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
162 const QAbstractItemModel
* model
= index
.model();
163 const QString pluginName
= model
->data(index
).toString();
164 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
166 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
167 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
172 void PreviewsSettingsPage::loadPreviewPlugins()
174 QAbstractItemModel
* model
= m_listView
->model();
176 const QVector
<KPluginMetaData
> plugins
= KIO::PreviewJob::availableThumbnailerPlugins();
177 for (const KPluginMetaData
&plugin
: plugins
) {
178 const bool show
= m_enabledPreviewPlugins
.contains(plugin
.pluginId());
181 const QModelIndex index
= model
->index(0, 0);
182 model
->setData(index
, show
, Qt::CheckStateRole
);
183 model
->setData(index
, plugin
.name(), Qt::DisplayRole
);
184 model
->setData(index
, plugin
.pluginId(), ServiceModel::DesktopEntryNameRole
);
186 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 87)
187 const bool configurable
= plugin
.value(QStringLiteral("Configurable"), false);
188 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
192 model
->sort(Qt::DisplayRole
);
195 void PreviewsSettingsPage::loadSettings()
197 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
198 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
200 const qulonglong defaultLocalPreview
= static_cast<qulonglong
>(DefaultMaxLocalPreviewSize
) * 1024 * 1024;
201 const qulonglong maxLocalByteSize
= globalConfig
.readEntry("MaximumSize", defaultLocalPreview
);
202 const int maxLocalMByteSize
= maxLocalByteSize
/ (1024 * 1024);
203 m_localFileSizeBox
->setValue(maxLocalMByteSize
);
205 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(DefaultMaxRemotePreviewSize
) * 1024 * 1024;
206 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
207 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
208 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);