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 "configurepreviewplugindialog.h"
10 #include "dolphin_generalsettings.h"
11 #include "settings/serviceitemdelegate.h"
12 #include "settings/servicemodel.h"
14 #include <KIO/PreviewJob>
15 #include <KLocalizedString>
16 #include <KPluginMetaData>
18 #include <QHBoxLayout>
23 #include <QSortFilterProxyModel>
29 const int DefaultMaxLocalPreviewSize
= 0; // 0 MB
30 const int DefaultMaxRemotePreviewSize
= 0; // 0 MB
33 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
*parent
)
34 : SettingsPageBase(parent
)
35 , m_initialized(false)
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
, this, &PreviewsSettingsPage::configureService
);
51 m_listView
->setItemDelegate(delegate
);
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
);
60 m_listView
->setModel(proxyModel
);
61 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
62 m_listView
->setUniformItemSizes(true);
64 QLabel
*localFileSizeLabel
= new QLabel(i18n("Skip previews for local files above:"), this);
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"));
72 QHBoxLayout
*localFileSizeBoxLayout
= new QHBoxLayout();
73 localFileSizeBoxLayout
->addWidget(localFileSizeLabel
);
74 localFileSizeBoxLayout
->addStretch(0);
75 localFileSizeBoxLayout
->addWidget(m_localFileSizeBox
);
77 QLabel
*remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
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"));
85 QHBoxLayout
*remoteFileSizeBoxLayout
= new QHBoxLayout();
86 remoteFileSizeBoxLayout
->addWidget(remoteFileSizeLabel
);
87 remoteFileSizeBoxLayout
->addStretch(0);
88 remoteFileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
90 topLayout
->addWidget(showPreviewsLabel
);
91 topLayout
->addWidget(m_listView
);
92 topLayout
->addLayout(localFileSizeBoxLayout
);
93 topLayout
->addLayout(remoteFileSizeBoxLayout
);
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
);
102 PreviewsSettingsPage::~PreviewsSettingsPage()
106 void PreviewsSettingsPage::applySettings()
108 const QAbstractItemModel
*model
= m_listView
->model();
109 const int rowCount
= model
->rowCount();
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();
116 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
117 m_enabledPreviewPlugins
.append(enabledPlugin
);
122 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
123 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
125 if (!m_localFileSizeBox
->value()) {
126 globalConfig
.deleteEntry("MaximumSize", KConfigBase::Normal
| KConfigBase::Global
);
128 const qulonglong maximumLocalSize
= static_cast<qulonglong
>(m_localFileSizeBox
->value()) * 1024 * 1024;
129 globalConfig
.writeEntry("MaximumSize", maximumLocalSize
, KConfigBase::Normal
| KConfigBase::Global
);
132 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
133 globalConfig
.writeEntry("MaximumRemoteSize", maximumRemoteSize
, KConfigBase::Normal
| KConfigBase::Global
);
138 void PreviewsSettingsPage::restoreDefaults()
140 m_localFileSizeBox
->setValue(DefaultMaxLocalPreviewSize
);
141 m_remoteFileSizeBox
->setValue(DefaultMaxRemotePreviewSize
);
144 void PreviewsSettingsPage::showEvent(QShowEvent
*event
)
146 if (!event
->spontaneous() && !m_initialized
) {
147 loadPreviewPlugins();
148 m_initialized
= true;
150 SettingsPageBase::showEvent(event
);
153 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 87)
154 void PreviewsSettingsPage::configureService(const QModelIndex
&index
)
156 const QAbstractItemModel
*model
= index
.model();
157 const QString pluginName
= model
->data(index
).toString();
158 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
160 ConfigurePreviewPluginDialog
*dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
161 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
166 void PreviewsSettingsPage::loadPreviewPlugins()
168 QAbstractItemModel
*model
= m_listView
->model();
170 const QVector
<KPluginMetaData
> plugins
= KIO::PreviewJob::availableThumbnailerPlugins();
171 for (const KPluginMetaData
&plugin
: plugins
) {
172 const bool show
= m_enabledPreviewPlugins
.contains(plugin
.pluginId());
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
);
180 #if KIOWIDGETS_BUILD_DEPRECATED_SINCE(5, 87)
181 const bool configurable
= plugin
.value(QStringLiteral("Configurable"), false);
182 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
186 model
->sort(Qt::DisplayRole
);
189 void PreviewsSettingsPage::loadSettings()
191 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
192 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
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
);
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
);
205 #include "moc_previewssettingspage.cpp"