1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "previewssettingspage.h"
22 #include "dolphin_generalsettings.h"
23 #include "configurepreviewplugindialog.h"
24 #include "settings/serviceitemdelegate.h"
25 #include "settings/servicemodel.h"
27 #include <KIO/PreviewJob>
28 #include <KLocalizedString>
29 #include <KServiceTypeTrader>
31 #include <QHBoxLayout>
36 #include <QSortFilterProxyModel>
41 const int DefaultMaxLocalPreviewSize
= 0; // 0 MB
42 const int DefaultMaxRemotePreviewSize
= 0; // 0 MB
45 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
* parent
) :
46 SettingsPageBase(parent
),
49 m_enabledPreviewPlugins(),
50 m_localFileSizeBox(nullptr),
51 m_remoteFileSizeBox(nullptr)
53 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
55 QLabel
* showPreviewsLabel
= new QLabel(i18nc("@title:group", "Show previews in the view for:"), this);
57 m_listView
= new QListView(this);
59 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
60 connect(delegate
, &ServiceItemDelegate::requestServiceConfiguration
,
61 this, &PreviewsSettingsPage::configureService
);
63 ServiceModel
* serviceModel
= new ServiceModel(this);
64 QSortFilterProxyModel
* proxyModel
= new QSortFilterProxyModel(this);
65 proxyModel
->setSourceModel(serviceModel
);
66 proxyModel
->setSortRole(Qt::DisplayRole
);
67 proxyModel
->setSortCaseSensitivity(Qt::CaseInsensitive
);
69 m_listView
->setModel(proxyModel
);
70 m_listView
->setItemDelegate(delegate
);
71 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
73 QLabel
* localFileSizeLabel
= new QLabel(i18n("Skip previews for local files above:"), this);
75 m_localFileSizeBox
= new QSpinBox(this);
76 m_localFileSizeBox
->setSingleStep(1);
77 m_localFileSizeBox
->setSuffix(QStringLiteral(" MB"));
78 m_localFileSizeBox
->setRange(0, 9999999); /* MB */
79 m_localFileSizeBox
->setSpecialValueText(i18n("No limit"));
81 QHBoxLayout
* localFileSizeBoxLayout
= new QHBoxLayout();
82 localFileSizeBoxLayout
->addWidget(localFileSizeLabel
);
83 localFileSizeBoxLayout
->addStretch(0);
84 localFileSizeBoxLayout
->addWidget(m_localFileSizeBox
);
86 QLabel
* remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
88 m_remoteFileSizeBox
= new QSpinBox(this);
89 m_remoteFileSizeBox
->setSingleStep(1);
90 m_remoteFileSizeBox
->setSuffix(QStringLiteral(" MB"));
91 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
92 m_remoteFileSizeBox
->setSpecialValueText(i18n("No previews"));
94 QHBoxLayout
* remoteFileSizeBoxLayout
= new QHBoxLayout();
95 remoteFileSizeBoxLayout
->addWidget(remoteFileSizeLabel
);
96 remoteFileSizeBoxLayout
->addStretch(0);
97 remoteFileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
99 topLayout
->addWidget(showPreviewsLabel
);
100 topLayout
->addWidget(m_listView
);
101 topLayout
->addLayout(localFileSizeBoxLayout
);
102 topLayout
->addLayout(remoteFileSizeBoxLayout
);
106 connect(m_listView
, &QListView::clicked
, this, &PreviewsSettingsPage::changed
);
107 connect(m_localFileSizeBox
, QOverload
<int>::of(&QSpinBox::valueChanged
), this, &PreviewsSettingsPage::changed
);
108 connect(m_remoteFileSizeBox
, QOverload
<int>::of(&QSpinBox::valueChanged
), this, &PreviewsSettingsPage::changed
);
111 PreviewsSettingsPage::~PreviewsSettingsPage()
115 void PreviewsSettingsPage::applySettings()
117 const QAbstractItemModel
* model
= m_listView
->model();
118 const int rowCount
= model
->rowCount();
120 m_enabledPreviewPlugins
.clear();
121 for (int i
= 0; i
< rowCount
; ++i
) {
122 const QModelIndex index
= model
->index(i
, 0);
123 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
125 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
126 m_enabledPreviewPlugins
.append(enabledPlugin
);
131 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
132 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
134 if (!m_localFileSizeBox
->value()) {
135 globalConfig
.deleteEntry("MaximumSize", KConfigBase::Normal
| KConfigBase::Global
);
137 const qulonglong maximumLocalSize
= static_cast<qulonglong
>(m_localFileSizeBox
->value()) * 1024 * 1024;
138 globalConfig
.writeEntry("MaximumSize",
140 KConfigBase::Normal
| KConfigBase::Global
);
143 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
144 globalConfig
.writeEntry("MaximumRemoteSize",
146 KConfigBase::Normal
| KConfigBase::Global
);
150 void PreviewsSettingsPage::restoreDefaults()
152 m_localFileSizeBox
->setValue(DefaultMaxLocalPreviewSize
);
153 m_remoteFileSizeBox
->setValue(DefaultMaxRemotePreviewSize
);
156 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
158 if (!event
->spontaneous() && !m_initialized
) {
159 loadPreviewPlugins();
160 m_initialized
= true;
162 SettingsPageBase::showEvent(event
);
165 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
167 const QAbstractItemModel
* model
= index
.model();
168 const QString pluginName
= model
->data(index
).toString();
169 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
171 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
172 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
176 void PreviewsSettingsPage::loadPreviewPlugins()
178 QAbstractItemModel
* model
= m_listView
->model();
180 const KService::List plugins
= KServiceTypeTrader::self()->query(QStringLiteral("ThumbCreator"));
181 foreach (const KService::Ptr
& service
, plugins
) {
182 const bool configurable
= service
->property(QStringLiteral("Configurable"), QVariant::Bool
).toBool();
183 const bool show
= m_enabledPreviewPlugins
.contains(service
->desktopEntryName());
186 const QModelIndex index
= model
->index(0, 0);
187 model
->setData(index
, show
, Qt::CheckStateRole
);
188 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
189 model
->setData(index
, service
->name(), Qt::DisplayRole
);
190 model
->setData(index
, service
->desktopEntryName(), ServiceModel::DesktopEntryNameRole
);
193 model
->sort(Qt::DisplayRole
);
196 void PreviewsSettingsPage::loadSettings()
198 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
199 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
201 const qulonglong defaultLocalPreview
= static_cast<qulonglong
>(DefaultMaxLocalPreviewSize
) * 1024 * 1024;
202 const qulonglong maxLocalByteSize
= globalConfig
.readEntry("MaximumSize", defaultLocalPreview
);
203 const int maxLocalMByteSize
= maxLocalByteSize
/ (1024 * 1024);
204 m_localFileSizeBox
->setValue(maxLocalMByteSize
);
206 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(DefaultMaxRemotePreviewSize
) * 1024 * 1024;
207 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
208 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
209 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);