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>
23 #include <QSortFilterProxyModel>
28 const int DefaultMaxLocalPreviewSize
= 0; // 0 MB
29 const int DefaultMaxRemotePreviewSize
= 0; // 0 MB
32 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
* parent
) :
33 SettingsPageBase(parent
),
36 m_enabledPreviewPlugins(),
37 m_localFileSizeBox(nullptr),
38 m_remoteFileSizeBox(nullptr)
40 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
42 QLabel
* showPreviewsLabel
= new QLabel(i18nc("@title:group", "Show previews in the view for:"), this);
44 m_listView
= new QListView(this);
46 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
47 connect(delegate
, &ServiceItemDelegate::requestServiceConfiguration
,
48 this, &PreviewsSettingsPage::configureService
);
50 ServiceModel
* serviceModel
= new ServiceModel(this);
51 QSortFilterProxyModel
* proxyModel
= new QSortFilterProxyModel(this);
52 proxyModel
->setSourceModel(serviceModel
);
53 proxyModel
->setSortRole(Qt::DisplayRole
);
54 proxyModel
->setSortCaseSensitivity(Qt::CaseInsensitive
);
56 m_listView
->setModel(proxyModel
);
57 m_listView
->setItemDelegate(delegate
);
58 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
60 QLabel
* localFileSizeLabel
= new QLabel(i18n("Skip previews for local files above:"), this);
62 m_localFileSizeBox
= new QSpinBox(this);
63 m_localFileSizeBox
->setSingleStep(1);
64 m_localFileSizeBox
->setSuffix(QStringLiteral(" MB"));
65 m_localFileSizeBox
->setRange(0, 9999999); /* MB */
66 m_localFileSizeBox
->setSpecialValueText(i18n("No limit"));
68 QHBoxLayout
* localFileSizeBoxLayout
= new QHBoxLayout();
69 localFileSizeBoxLayout
->addWidget(localFileSizeLabel
);
70 localFileSizeBoxLayout
->addStretch(0);
71 localFileSizeBoxLayout
->addWidget(m_localFileSizeBox
);
73 QLabel
* remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
75 m_remoteFileSizeBox
= new QSpinBox(this);
76 m_remoteFileSizeBox
->setSingleStep(1);
77 m_remoteFileSizeBox
->setSuffix(QStringLiteral(" MB"));
78 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
79 m_remoteFileSizeBox
->setSpecialValueText(i18n("No previews"));
81 QHBoxLayout
* remoteFileSizeBoxLayout
= new QHBoxLayout();
82 remoteFileSizeBoxLayout
->addWidget(remoteFileSizeLabel
);
83 remoteFileSizeBoxLayout
->addStretch(0);
84 remoteFileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
86 topLayout
->addWidget(showPreviewsLabel
);
87 topLayout
->addWidget(m_listView
);
88 topLayout
->addLayout(localFileSizeBoxLayout
);
89 topLayout
->addLayout(remoteFileSizeBoxLayout
);
93 connect(m_listView
, &QListView::clicked
, this, &PreviewsSettingsPage::changed
);
94 connect(m_localFileSizeBox
, QOverload
<int>::of(&QSpinBox::valueChanged
), this, &PreviewsSettingsPage::changed
);
95 connect(m_remoteFileSizeBox
, QOverload
<int>::of(&QSpinBox::valueChanged
), this, &PreviewsSettingsPage::changed
);
98 PreviewsSettingsPage::~PreviewsSettingsPage()
102 void PreviewsSettingsPage::applySettings()
104 const QAbstractItemModel
* model
= m_listView
->model();
105 const int rowCount
= model
->rowCount();
107 m_enabledPreviewPlugins
.clear();
108 for (int i
= 0; i
< rowCount
; ++i
) {
109 const QModelIndex index
= model
->index(i
, 0);
110 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
112 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
113 m_enabledPreviewPlugins
.append(enabledPlugin
);
118 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
119 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
121 if (!m_localFileSizeBox
->value()) {
122 globalConfig
.deleteEntry("MaximumSize", KConfigBase::Normal
| KConfigBase::Global
);
124 const qulonglong maximumLocalSize
= static_cast<qulonglong
>(m_localFileSizeBox
->value()) * 1024 * 1024;
125 globalConfig
.writeEntry("MaximumSize",
127 KConfigBase::Normal
| KConfigBase::Global
);
130 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
131 globalConfig
.writeEntry("MaximumRemoteSize",
133 KConfigBase::Normal
| KConfigBase::Global
);
137 void PreviewsSettingsPage::restoreDefaults()
139 m_localFileSizeBox
->setValue(DefaultMaxLocalPreviewSize
);
140 m_remoteFileSizeBox
->setValue(DefaultMaxRemotePreviewSize
);
143 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
145 if (!event
->spontaneous() && !m_initialized
) {
146 loadPreviewPlugins();
147 m_initialized
= true;
149 SettingsPageBase::showEvent(event
);
152 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
154 const QAbstractItemModel
* model
= index
.model();
155 const QString pluginName
= model
->data(index
).toString();
156 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
158 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
159 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
163 void PreviewsSettingsPage::loadPreviewPlugins()
165 QAbstractItemModel
* model
= m_listView
->model();
167 const KService::List plugins
= KServiceTypeTrader::self()->query(QStringLiteral("ThumbCreator"));
168 foreach (const KService::Ptr
& service
, plugins
) {
169 const bool configurable
= service
->property(QStringLiteral("Configurable"), QVariant::Bool
).toBool();
170 const bool show
= m_enabledPreviewPlugins
.contains(service
->desktopEntryName());
173 const QModelIndex index
= model
->index(0, 0);
174 model
->setData(index
, show
, Qt::CheckStateRole
);
175 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
176 model
->setData(index
, service
->name(), Qt::DisplayRole
);
177 model
->setData(index
, service
->desktopEntryName(), ServiceModel::DesktopEntryNameRole
);
180 model
->sort(Qt::DisplayRole
);
183 void PreviewsSettingsPage::loadSettings()
185 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
186 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
188 const qulonglong defaultLocalPreview
= static_cast<qulonglong
>(DefaultMaxLocalPreviewSize
) * 1024 * 1024;
189 const qulonglong maxLocalByteSize
= globalConfig
.readEntry("MaximumSize", defaultLocalPreview
);
190 const int maxLocalMByteSize
= maxLocalByteSize
/ (1024 * 1024);
191 m_localFileSizeBox
->setValue(maxLocalMByteSize
);
193 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(DefaultMaxRemotePreviewSize
) * 1024 * 1024;
194 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
195 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
196 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);