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>
27 #include <KLocalizedString>
28 #include <KServiceTypeTrader>
31 #include <settings/serviceitemdelegate.h>
32 #include <settings/servicemodel.h>
36 #include <QHBoxLayout>
44 #include <QSortFilterProxyModel>
45 #include <QVBoxLayout>
49 const bool UseThumbnails
= true;
50 const int MaxRemotePreviewSize
= 0; // 0 MB
53 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
* parent
) :
54 SettingsPageBase(parent
),
57 m_enabledPreviewPlugins(),
58 m_remoteFileSizeBox(0)
60 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
62 QLabel
* showPreviewsLabel
= new QLabel(i18nc("@title:group", "Show previews for:"), this);
64 m_listView
= new QListView(this);
66 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
67 connect(delegate
, &ServiceItemDelegate::requestServiceConfiguration
,
68 this, &PreviewsSettingsPage::configureService
);
70 ServiceModel
* serviceModel
= new ServiceModel(this);
71 QSortFilterProxyModel
* proxyModel
= new QSortFilterProxyModel(this);
72 proxyModel
->setSourceModel(serviceModel
);
73 proxyModel
->setSortRole(Qt::DisplayRole
);
75 m_listView
->setModel(proxyModel
);
76 m_listView
->setItemDelegate(delegate
);
77 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
79 QLabel
* remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
81 m_remoteFileSizeBox
= new QSpinBox(this);
82 m_remoteFileSizeBox
->setSingleStep(1);
83 m_remoteFileSizeBox
->setSuffix(QLatin1String(" MB"));
84 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
86 QHBoxLayout
* fileSizeBoxLayout
= new QHBoxLayout(this);
87 fileSizeBoxLayout
->addWidget(remoteFileSizeLabel
, 0, Qt::AlignRight
);
88 fileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
90 topLayout
->addWidget(showPreviewsLabel
);
91 topLayout
->addWidget(m_listView
);
92 topLayout
->addLayout(fileSizeBoxLayout
);
96 connect(m_listView
, &QListView::clicked
, this, &PreviewsSettingsPage::changed
);
97 connect(m_remoteFileSizeBox
, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged
), this, &PreviewsSettingsPage::changed
);
100 PreviewsSettingsPage::~PreviewsSettingsPage()
104 void PreviewsSettingsPage::applySettings()
106 const QAbstractItemModel
* model
= m_listView
->model();
107 const int rowCount
= model
->rowCount();
109 m_enabledPreviewPlugins
.clear();
110 for (int i
= 0; i
< rowCount
; ++i
) {
111 const QModelIndex index
= model
->index(i
, 0);
112 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
114 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
115 m_enabledPreviewPlugins
.append(enabledPlugin
);
120 KConfigGroup
globalConfig(KSharedConfig::openConfig(), QLatin1String("PreviewSettings"));
121 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
123 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
124 globalConfig
.writeEntry("MaximumRemoteSize",
126 KConfigBase::Normal
| KConfigBase::Global
);
130 void PreviewsSettingsPage::restoreDefaults()
132 m_remoteFileSizeBox
->setValue(MaxRemotePreviewSize
);
135 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
137 if (!event
->spontaneous() && !m_initialized
) {
138 loadPreviewPlugins();
139 m_initialized
= true;
141 SettingsPageBase::showEvent(event
);
144 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
146 const QAbstractItemModel
* model
= index
.model();
147 const QString pluginName
= model
->data(index
).toString();
148 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
150 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
151 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
155 void PreviewsSettingsPage::loadPreviewPlugins()
157 QAbstractItemModel
* model
= m_listView
->model();
159 const KService::List plugins
= KServiceTypeTrader::self()->query(QLatin1String("ThumbCreator"));
160 foreach (const KService::Ptr
& service
, plugins
) {
161 const bool configurable
= service
->property("Configurable", QVariant::Bool
).toBool();
162 const bool show
= m_enabledPreviewPlugins
.contains(service
->desktopEntryName());
165 const QModelIndex index
= model
->index(0, 0);
166 model
->setData(index
, show
, Qt::CheckStateRole
);
167 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
168 model
->setData(index
, service
->name(), Qt::DisplayRole
);
169 model
->setData(index
, service
->desktopEntryName(), ServiceModel::DesktopEntryNameRole
);
172 model
->sort(Qt::DisplayRole
);
175 void PreviewsSettingsPage::loadSettings()
177 KConfigGroup
globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
178 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", QStringList()
179 << QLatin1String("directorythumbnail")
180 << QLatin1String("imagethumbnail")
181 << QLatin1String("jpegthumbnail"));
183 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(MaxRemotePreviewSize
) * 1024 * 1024;
184 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
185 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
186 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);