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>
30 #include <KServiceTypeTrader>
33 #include <settings/serviceitemdelegate.h>
34 #include <settings/servicemodel.h>
38 #include <QHBoxLayout>
45 #include <QSortFilterProxyModel>
46 #include <QVBoxLayout>
50 const bool UseThumbnails
= true;
51 const int MaxRemotePreviewSize
= 0; // 0 MB
54 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
* parent
) :
55 SettingsPageBase(parent
),
58 m_enabledPreviewPlugins(),
59 m_remoteFileSizeBox(0)
61 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
63 QLabel
* showPreviewsLabel
= new QLabel(i18nc("@title:group", "Show previews for:"), this);
65 m_listView
= new QListView(this);
67 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
68 connect(delegate
, &ServiceItemDelegate::requestServiceConfiguration
,
69 this, &PreviewsSettingsPage::configureService
);
71 ServiceModel
* serviceModel
= new ServiceModel(this);
72 QSortFilterProxyModel
* proxyModel
= new QSortFilterProxyModel(this);
73 proxyModel
->setSourceModel(serviceModel
);
74 proxyModel
->setSortRole(Qt::DisplayRole
);
76 m_listView
->setModel(proxyModel
);
77 m_listView
->setItemDelegate(delegate
);
78 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
80 QLabel
* remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
82 m_remoteFileSizeBox
= new KIntSpinBox(this);
83 m_remoteFileSizeBox
->setSingleStep(1);
84 m_remoteFileSizeBox
->setSuffix(QLatin1String(" MB"));
85 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
87 QHBoxLayout
* fileSizeBoxLayout
= new QHBoxLayout(this);
88 fileSizeBoxLayout
->addWidget(remoteFileSizeLabel
, 0, Qt::AlignRight
);
89 fileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
91 topLayout
->addSpacing(KDialog::spacingHint());
92 topLayout
->addWidget(showPreviewsLabel
);
93 topLayout
->addWidget(m_listView
);
94 topLayout
->addLayout(fileSizeBoxLayout
);
98 connect(m_listView
, &QListView::clicked
, this, &PreviewsSettingsPage::changed
);
99 connect(m_remoteFileSizeBox
, static_cast<void(KIntSpinBox::*)(int)>(&KIntSpinBox::valueChanged
), this, &PreviewsSettingsPage::changed
);
102 PreviewsSettingsPage::~PreviewsSettingsPage()
106 void PreviewsSettingsPage::applySettings()
108 const QAbstractItemModel
* model
= m_listView
->model();
109 const int rowCount
= model
->rowCount();
111 m_enabledPreviewPlugins
.clear();
112 for (int i
= 0; i
< rowCount
; ++i
) {
113 const QModelIndex index
= model
->index(i
, 0);
114 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
116 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
117 m_enabledPreviewPlugins
.append(enabledPlugin
);
122 KConfigGroup
globalConfig(KGlobal::config(), QLatin1String("PreviewSettings"));
123 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
125 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
126 globalConfig
.writeEntry("MaximumRemoteSize",
128 KConfigBase::Normal
| KConfigBase::Global
);
132 void PreviewsSettingsPage::restoreDefaults()
134 m_remoteFileSizeBox
->setValue(MaxRemotePreviewSize
);
137 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
139 if (!event
->spontaneous() && !m_initialized
) {
140 loadPreviewPlugins();
141 m_initialized
= true;
143 SettingsPageBase::showEvent(event
);
146 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
148 const QAbstractItemModel
* model
= index
.model();
149 const QString pluginName
= model
->data(index
).toString();
150 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
152 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
153 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
157 void PreviewsSettingsPage::loadPreviewPlugins()
159 QAbstractItemModel
* model
= m_listView
->model();
161 const KService::List plugins
= KServiceTypeTrader::self()->query(QLatin1String("ThumbCreator"));
162 foreach (const KService::Ptr
& service
, plugins
) {
163 const bool configurable
= service
->property("Configurable", QVariant::Bool
).toBool();
164 const bool show
= m_enabledPreviewPlugins
.contains(service
->desktopEntryName());
167 const QModelIndex index
= model
->index(0, 0);
168 model
->setData(index
, show
, Qt::CheckStateRole
);
169 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
170 model
->setData(index
, service
->name(), Qt::DisplayRole
);
171 model
->setData(index
, service
->desktopEntryName(), ServiceModel::DesktopEntryNameRole
);
174 model
->sort(Qt::DisplayRole
);
177 void PreviewsSettingsPage::loadSettings()
179 KConfigGroup
globalConfig(KGlobal::config(), "PreviewSettings");
180 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", QStringList()
181 << QLatin1String("directorythumbnail")
182 << QLatin1String("imagethumbnail")
183 << QLatin1String("jpegthumbnail"));
185 // If the user is upgrading from KDE <= 4.6, we must check if he had the 'jpegrotatedthumbnail' plugin enabled.
186 // This plugin does not exist any more in KDE >= 4.7, so we have to replace it with the 'jpegthumbnail' plugin.
188 // Note that the upgrade to the correct plugin is done already in KFilePreviewGenerator. However, if Konqueror is
189 // opened in web browsing mode and the Settings dialog is opened, we might end up here before KFilePreviewGenerator's
190 // constructor is ever called -> the plugin replacement should be done here as well.
191 if (m_enabledPreviewPlugins
.contains(QLatin1String("jpegrotatedthumbnail"))) {
192 m_enabledPreviewPlugins
.removeAll(QLatin1String("jpegrotatedthumbnail"));
193 m_enabledPreviewPlugins
.append(QLatin1String("jpegthumbnail"));
194 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
198 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(MaxRemotePreviewSize
) * 1024 * 1024;
199 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
200 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
201 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);