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(tr("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(tr("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 const qulonglong maximumLocalSize
= static_cast<qulonglong
>(m_localFileSizeBox
->value()) * 1024 * 1024;
135 globalConfig
.writeEntry("MaximumSize",
137 KConfigBase::Normal
| KConfigBase::Global
);
139 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
140 globalConfig
.writeEntry("MaximumRemoteSize",
142 KConfigBase::Normal
| KConfigBase::Global
);
146 void PreviewsSettingsPage::restoreDefaults()
148 m_localFileSizeBox
->setValue(DefaultMaxLocalPreviewSize
);
149 m_remoteFileSizeBox
->setValue(DefaultMaxRemotePreviewSize
);
152 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
154 if (!event
->spontaneous() && !m_initialized
) {
155 loadPreviewPlugins();
156 m_initialized
= true;
158 SettingsPageBase::showEvent(event
);
161 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
163 const QAbstractItemModel
* model
= index
.model();
164 const QString pluginName
= model
->data(index
).toString();
165 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
167 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
168 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
172 void PreviewsSettingsPage::loadPreviewPlugins()
174 QAbstractItemModel
* model
= m_listView
->model();
176 const KService::List plugins
= KServiceTypeTrader::self()->query(QStringLiteral("ThumbCreator"));
177 foreach (const KService::Ptr
& service
, plugins
) {
178 const bool configurable
= service
->property(QStringLiteral("Configurable"), QVariant::Bool
).toBool();
179 const bool show
= m_enabledPreviewPlugins
.contains(service
->desktopEntryName());
182 const QModelIndex index
= model
->index(0, 0);
183 model
->setData(index
, show
, Qt::CheckStateRole
);
184 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
185 model
->setData(index
, service
->name(), Qt::DisplayRole
);
186 model
->setData(index
, service
->desktopEntryName(), ServiceModel::DesktopEntryNameRole
);
189 model
->sort(Qt::DisplayRole
);
192 void PreviewsSettingsPage::loadSettings()
194 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
195 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
197 const qulonglong defaultLocalPreview
= static_cast<qulonglong
>(DefaultMaxLocalPreviewSize
) * 1024 * 1024;
198 const qulonglong maxLocalByteSize
= globalConfig
.readEntry("MaximumSize", defaultLocalPreview
);
199 const int maxLocalMByteSize
= maxLocalByteSize
/ (1024 * 1024);
200 m_localFileSizeBox
->setValue(maxLocalMByteSize
);
202 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(DefaultMaxRemotePreviewSize
) * 1024 * 1024;
203 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
204 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
205 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);