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"
25 #include <KConfigGroup>
26 #include <KLocalizedString>
27 #include <KServiceTypeTrader>
30 #include <settings/serviceitemdelegate.h>
31 #include <settings/servicemodel.h>
35 #include <QHBoxLayout>
43 #include <QSortFilterProxyModel>
44 #include <QVBoxLayout>
48 const bool UseThumbnails
= true;
49 const int MaxRemotePreviewSize
= 0; // 0 MB
52 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
* parent
) :
53 SettingsPageBase(parent
),
56 m_enabledPreviewPlugins(),
57 m_remoteFileSizeBox(0)
59 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
61 QLabel
* showPreviewsLabel
= new QLabel(i18nc("@title:group", "Show previews for:"), this);
63 m_listView
= new QListView(this);
65 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
66 connect(delegate
, &ServiceItemDelegate::requestServiceConfiguration
,
67 this, &PreviewsSettingsPage::configureService
);
69 ServiceModel
* serviceModel
= new ServiceModel(this);
70 QSortFilterProxyModel
* proxyModel
= new QSortFilterProxyModel(this);
71 proxyModel
->setSourceModel(serviceModel
);
72 proxyModel
->setSortRole(Qt::DisplayRole
);
74 m_listView
->setModel(proxyModel
);
75 m_listView
->setItemDelegate(delegate
);
76 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
78 QLabel
* remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
80 m_remoteFileSizeBox
= new QSpinBox(this);
81 m_remoteFileSizeBox
->setSingleStep(1);
82 m_remoteFileSizeBox
->setSuffix(QLatin1String(" MB"));
83 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
85 QHBoxLayout
* fileSizeBoxLayout
= new QHBoxLayout(this);
86 fileSizeBoxLayout
->addWidget(remoteFileSizeLabel
, 0, Qt::AlignRight
);
87 fileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
89 topLayout
->addWidget(showPreviewsLabel
);
90 topLayout
->addWidget(m_listView
);
91 topLayout
->addLayout(fileSizeBoxLayout
);
95 connect(m_listView
, &QListView::clicked
, this, &PreviewsSettingsPage::changed
);
96 connect(m_remoteFileSizeBox
, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged
), this, &PreviewsSettingsPage::changed
);
99 PreviewsSettingsPage::~PreviewsSettingsPage()
103 void PreviewsSettingsPage::applySettings()
105 const QAbstractItemModel
* model
= m_listView
->model();
106 const int rowCount
= model
->rowCount();
108 m_enabledPreviewPlugins
.clear();
109 for (int i
= 0; i
< rowCount
; ++i
) {
110 const QModelIndex index
= model
->index(i
, 0);
111 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
113 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
114 m_enabledPreviewPlugins
.append(enabledPlugin
);
119 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QLatin1String("PreviewSettings"));
120 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
122 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
123 globalConfig
.writeEntry("MaximumRemoteSize",
125 KConfigBase::Normal
| KConfigBase::Global
);
129 void PreviewsSettingsPage::restoreDefaults()
131 m_remoteFileSizeBox
->setValue(MaxRemotePreviewSize
);
134 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
136 if (!event
->spontaneous() && !m_initialized
) {
137 loadPreviewPlugins();
138 m_initialized
= true;
140 SettingsPageBase::showEvent(event
);
143 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
145 const QAbstractItemModel
* model
= index
.model();
146 const QString pluginName
= model
->data(index
).toString();
147 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
149 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
150 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
154 void PreviewsSettingsPage::loadPreviewPlugins()
156 QAbstractItemModel
* model
= m_listView
->model();
158 const KService::List plugins
= KServiceTypeTrader::self()->query(QLatin1String("ThumbCreator"));
159 foreach (const KService::Ptr
& service
, plugins
) {
160 const bool configurable
= service
->property("Configurable", QVariant::Bool
).toBool();
161 const bool show
= m_enabledPreviewPlugins
.contains(service
->desktopEntryName());
164 const QModelIndex index
= model
->index(0, 0);
165 model
->setData(index
, show
, Qt::CheckStateRole
);
166 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
167 model
->setData(index
, service
->name(), Qt::DisplayRole
);
168 model
->setData(index
, service
->desktopEntryName(), ServiceModel::DesktopEntryNameRole
);
171 model
->sort(Qt::DisplayRole
);
174 void PreviewsSettingsPage::loadSettings()
176 KConfigGroup
globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
177 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", QStringList()
178 << QLatin1String("directorythumbnail")
179 << QLatin1String("imagethumbnail")
180 << QLatin1String("jpegthumbnail"));
182 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(MaxRemotePreviewSize
) * 1024 * 1024;
183 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
184 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
185 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);