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 <KServiceTypeTrader>
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 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
49 connect(delegate
, &ServiceItemDelegate::requestServiceConfiguration
,
50 this, &PreviewsSettingsPage::configureService
);
52 ServiceModel
* serviceModel
= new ServiceModel(this);
53 QSortFilterProxyModel
* proxyModel
= new QSortFilterProxyModel(this);
54 proxyModel
->setSourceModel(serviceModel
);
55 proxyModel
->setSortRole(Qt::DisplayRole
);
56 proxyModel
->setSortCaseSensitivity(Qt::CaseInsensitive
);
58 m_listView
->setModel(proxyModel
);
59 m_listView
->setItemDelegate(delegate
);
60 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
61 m_listView
->setUniformItemSizes(true);
63 QLabel
* localFileSizeLabel
= new QLabel(i18n("Skip previews for local files above:"), this);
65 m_localFileSizeBox
= new QSpinBox(this);
66 m_localFileSizeBox
->setSingleStep(1);
67 m_localFileSizeBox
->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
68 m_localFileSizeBox
->setRange(0, 9999999); /* MB */
69 m_localFileSizeBox
->setSpecialValueText(i18n("No limit"));
71 QHBoxLayout
* localFileSizeBoxLayout
= new QHBoxLayout();
72 localFileSizeBoxLayout
->addWidget(localFileSizeLabel
);
73 localFileSizeBoxLayout
->addStretch(0);
74 localFileSizeBoxLayout
->addWidget(m_localFileSizeBox
);
76 QLabel
* remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
78 m_remoteFileSizeBox
= new QSpinBox(this);
79 m_remoteFileSizeBox
->setSingleStep(1);
80 m_remoteFileSizeBox
->setSuffix(i18nc("Mebibytes; used as a suffix in a spinbox showing e.g. '3 MiB'", " MiB"));
81 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
82 m_remoteFileSizeBox
->setSpecialValueText(i18n("No previews"));
84 QHBoxLayout
* remoteFileSizeBoxLayout
= new QHBoxLayout();
85 remoteFileSizeBoxLayout
->addWidget(remoteFileSizeLabel
);
86 remoteFileSizeBoxLayout
->addStretch(0);
87 remoteFileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
89 topLayout
->addWidget(showPreviewsLabel
);
90 topLayout
->addWidget(m_listView
);
91 topLayout
->addLayout(localFileSizeBoxLayout
);
92 topLayout
->addLayout(remoteFileSizeBoxLayout
);
96 connect(m_listView
, &QListView::clicked
, this, &PreviewsSettingsPage::changed
);
97 connect(m_localFileSizeBox
, &QSpinBox::valueChanged
, this, &PreviewsSettingsPage::changed
);
98 connect(m_remoteFileSizeBox
, &QSpinBox::valueChanged
, this, &PreviewsSettingsPage::changed
);
101 PreviewsSettingsPage::~PreviewsSettingsPage()
105 void PreviewsSettingsPage::applySettings()
107 const QAbstractItemModel
* model
= m_listView
->model();
108 const int rowCount
= model
->rowCount();
110 m_enabledPreviewPlugins
.clear();
111 for (int i
= 0; i
< rowCount
; ++i
) {
112 const QModelIndex index
= model
->index(i
, 0);
113 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
115 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
116 m_enabledPreviewPlugins
.append(enabledPlugin
);
121 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
122 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
124 if (!m_localFileSizeBox
->value()) {
125 globalConfig
.deleteEntry("MaximumSize", KConfigBase::Normal
| KConfigBase::Global
);
127 const qulonglong maximumLocalSize
= static_cast<qulonglong
>(m_localFileSizeBox
->value()) * 1024 * 1024;
128 globalConfig
.writeEntry("MaximumSize",
130 KConfigBase::Normal
| KConfigBase::Global
);
133 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
134 globalConfig
.writeEntry("MaximumRemoteSize",
136 KConfigBase::Normal
| KConfigBase::Global
);
141 void PreviewsSettingsPage::restoreDefaults()
143 m_localFileSizeBox
->setValue(DefaultMaxLocalPreviewSize
);
144 m_remoteFileSizeBox
->setValue(DefaultMaxRemotePreviewSize
);
147 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
149 if (!event
->spontaneous() && !m_initialized
) {
150 loadPreviewPlugins();
151 m_initialized
= true;
153 SettingsPageBase::showEvent(event
);
156 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
158 const QAbstractItemModel
* model
= index
.model();
159 const QString pluginName
= model
->data(index
).toString();
160 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
162 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
163 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
167 void PreviewsSettingsPage::loadPreviewPlugins()
169 QAbstractItemModel
* model
= m_listView
->model();
171 const KService::List plugins
= KServiceTypeTrader::self()->query(QStringLiteral("ThumbCreator"));
172 for (const KService::Ptr
& service
: plugins
) {
173 const bool configurable
= service
->property(QStringLiteral("Configurable"), QVariant::Bool
).toBool();
174 const bool show
= m_enabledPreviewPlugins
.contains(service
->desktopEntryName());
177 const QModelIndex index
= model
->index(0, 0);
178 model
->setData(index
, show
, Qt::CheckStateRole
);
179 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
180 model
->setData(index
, service
->name(), Qt::DisplayRole
);
181 model
->setData(index
, service
->desktopEntryName(), ServiceModel::DesktopEntryNameRole
);
184 model
->sort(Qt::DisplayRole
);
187 void PreviewsSettingsPage::loadSettings()
189 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
190 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
192 const qulonglong defaultLocalPreview
= static_cast<qulonglong
>(DefaultMaxLocalPreviewSize
) * 1024 * 1024;
193 const qulonglong maxLocalByteSize
= globalConfig
.readEntry("MaximumSize", defaultLocalPreview
);
194 const int maxLocalMByteSize
= maxLocalByteSize
/ (1024 * 1024);
195 m_localFileSizeBox
->setValue(maxLocalMByteSize
);
197 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(DefaultMaxRemotePreviewSize
) * 1024 * 1024;
198 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
199 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
200 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);