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>
35 #include <QSortFilterProxyModel>
40 const int MaxRemotePreviewSize
= 0; // 0 MB
43 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
* parent
) :
44 SettingsPageBase(parent
),
47 m_enabledPreviewPlugins(),
48 m_remoteFileSizeBox(nullptr)
50 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
52 QLabel
* showPreviewsLabel
= new QLabel(i18nc("@title:group", "Show previews for:"), this);
54 m_listView
= new QListView(this);
56 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
57 connect(delegate
, &ServiceItemDelegate::requestServiceConfiguration
,
58 this, &PreviewsSettingsPage::configureService
);
60 ServiceModel
* serviceModel
= new ServiceModel(this);
61 QSortFilterProxyModel
* proxyModel
= new QSortFilterProxyModel(this);
62 proxyModel
->setSourceModel(serviceModel
);
63 proxyModel
->setSortRole(Qt::DisplayRole
);
65 m_listView
->setModel(proxyModel
);
66 m_listView
->setItemDelegate(delegate
);
67 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
69 QLabel
* remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
71 m_remoteFileSizeBox
= new QSpinBox(this);
72 m_remoteFileSizeBox
->setSingleStep(1);
73 m_remoteFileSizeBox
->setSuffix(QStringLiteral(" MB"));
74 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
76 QHBoxLayout
* fileSizeBoxLayout
= new QHBoxLayout();
77 fileSizeBoxLayout
->addWidget(remoteFileSizeLabel
, 0, Qt::AlignRight
);
78 fileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
80 topLayout
->addWidget(showPreviewsLabel
);
81 topLayout
->addWidget(m_listView
);
82 topLayout
->addLayout(fileSizeBoxLayout
);
86 connect(m_listView
, &QListView::clicked
, this, &PreviewsSettingsPage::changed
);
87 connect(m_remoteFileSizeBox
, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged
), this, &PreviewsSettingsPage::changed
);
90 PreviewsSettingsPage::~PreviewsSettingsPage()
94 void PreviewsSettingsPage::applySettings()
96 const QAbstractItemModel
* model
= m_listView
->model();
97 const int rowCount
= model
->rowCount();
99 m_enabledPreviewPlugins
.clear();
100 for (int i
= 0; i
< rowCount
; ++i
) {
101 const QModelIndex index
= model
->index(i
, 0);
102 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
104 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
105 m_enabledPreviewPlugins
.append(enabledPlugin
);
110 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
111 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
113 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
114 globalConfig
.writeEntry("MaximumRemoteSize",
116 KConfigBase::Normal
| KConfigBase::Global
);
120 void PreviewsSettingsPage::restoreDefaults()
122 m_remoteFileSizeBox
->setValue(MaxRemotePreviewSize
);
125 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
127 if (!event
->spontaneous() && !m_initialized
) {
128 loadPreviewPlugins();
129 m_initialized
= true;
131 SettingsPageBase::showEvent(event
);
134 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
136 const QAbstractItemModel
* model
= index
.model();
137 const QString pluginName
= model
->data(index
).toString();
138 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
140 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
141 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
145 void PreviewsSettingsPage::loadPreviewPlugins()
147 QAbstractItemModel
* model
= m_listView
->model();
149 const KService::List plugins
= KServiceTypeTrader::self()->query(QStringLiteral("ThumbCreator"));
150 foreach (const KService::Ptr
& service
, plugins
) {
151 const bool configurable
= service
->property(QStringLiteral("Configurable"), QVariant::Bool
).toBool();
152 const bool show
= m_enabledPreviewPlugins
.contains(service
->desktopEntryName());
155 const QModelIndex index
= model
->index(0, 0);
156 model
->setData(index
, show
, Qt::CheckStateRole
);
157 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
158 model
->setData(index
, service
->name(), Qt::DisplayRole
);
159 model
->setData(index
, service
->desktopEntryName(), ServiceModel::DesktopEntryNameRole
);
162 model
->sort(Qt::DisplayRole
);
165 void PreviewsSettingsPage::loadSettings()
167 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
168 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
170 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(MaxRemotePreviewSize
) * 1024 * 1024;
171 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
172 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
173 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);