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 in the view 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
);
64 proxyModel
->setSortCaseSensitivity(Qt::CaseInsensitive
);
66 m_listView
->setModel(proxyModel
);
67 m_listView
->setItemDelegate(delegate
);
68 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
70 QLabel
* remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
72 m_remoteFileSizeBox
= new QSpinBox(this);
73 m_remoteFileSizeBox
->setSingleStep(1);
74 m_remoteFileSizeBox
->setSuffix(QStringLiteral(" MB"));
75 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
77 QHBoxLayout
* fileSizeBoxLayout
= new QHBoxLayout();
78 fileSizeBoxLayout
->addWidget(remoteFileSizeLabel
, 0, Qt::AlignRight
);
79 fileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
81 topLayout
->addWidget(showPreviewsLabel
);
82 topLayout
->addWidget(m_listView
);
83 topLayout
->addLayout(fileSizeBoxLayout
);
87 connect(m_listView
, &QListView::clicked
, this, &PreviewsSettingsPage::changed
);
88 connect(m_remoteFileSizeBox
, QOverload
<int>::of(&QSpinBox::valueChanged
), this, &PreviewsSettingsPage::changed
);
91 PreviewsSettingsPage::~PreviewsSettingsPage()
95 void PreviewsSettingsPage::applySettings()
97 const QAbstractItemModel
* model
= m_listView
->model();
98 const int rowCount
= model
->rowCount();
100 m_enabledPreviewPlugins
.clear();
101 for (int i
= 0; i
< rowCount
; ++i
) {
102 const QModelIndex index
= model
->index(i
, 0);
103 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
105 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
106 m_enabledPreviewPlugins
.append(enabledPlugin
);
111 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
112 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
114 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
115 globalConfig
.writeEntry("MaximumRemoteSize",
117 KConfigBase::Normal
| KConfigBase::Global
);
121 void PreviewsSettingsPage::restoreDefaults()
123 m_remoteFileSizeBox
->setValue(MaxRemotePreviewSize
);
126 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
128 if (!event
->spontaneous() && !m_initialized
) {
129 loadPreviewPlugins();
130 m_initialized
= true;
132 SettingsPageBase::showEvent(event
);
135 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
137 const QAbstractItemModel
* model
= index
.model();
138 const QString pluginName
= model
->data(index
).toString();
139 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
141 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
142 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
146 void PreviewsSettingsPage::loadPreviewPlugins()
148 QAbstractItemModel
* model
= m_listView
->model();
150 const KService::List plugins
= KServiceTypeTrader::self()->query(QStringLiteral("ThumbCreator"));
151 foreach (const KService::Ptr
& service
, plugins
) {
152 const bool configurable
= service
->property(QStringLiteral("Configurable"), QVariant::Bool
).toBool();
153 const bool show
= m_enabledPreviewPlugins
.contains(service
->desktopEntryName());
156 const QModelIndex index
= model
->index(0, 0);
157 model
->setData(index
, show
, Qt::CheckStateRole
);
158 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
159 model
->setData(index
, service
->name(), Qt::DisplayRole
);
160 model
->setData(index
, service
->desktopEntryName(), ServiceModel::DesktopEntryNameRole
);
163 model
->sort(Qt::DisplayRole
);
166 void PreviewsSettingsPage::loadSettings()
168 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
169 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
171 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(MaxRemotePreviewSize
) * 1024 * 1024;
172 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
173 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
174 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);