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);
62 topLayout
->setSpacing(KDialog::spacingHint());
63 topLayout
->setMargin(KDialog::marginHint());
65 // Create group box "Show previews for:"
66 QGroupBox
* listBox
= new QGroupBox(i18nc("@title:group", "Show previews for"), this);
68 m_listView
= new QListView(this);
70 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
71 connect(delegate
, SIGNAL(requestServiceConfiguration(QModelIndex
)),
72 this, SLOT(configureService(QModelIndex
)));
74 ServiceModel
* serviceModel
= new ServiceModel(this);
75 QSortFilterProxyModel
* proxyModel
= new QSortFilterProxyModel(this);
76 proxyModel
->setSourceModel(serviceModel
);
77 proxyModel
->setSortRole(Qt::DisplayRole
);
79 m_listView
->setModel(proxyModel
);
80 m_listView
->setItemDelegate(delegate
);
81 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
83 QVBoxLayout
* listBoxLayout
= new QVBoxLayout(listBox
);
84 listBoxLayout
->setSpacing(KDialog::spacingHint());
85 listBoxLayout
->setMargin(KDialog::marginHint());
86 listBoxLayout
->addWidget(m_listView
);
88 QLabel
* remoteFileSizeLabel
= new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
90 m_remoteFileSizeBox
= new KIntSpinBox(this);
91 m_remoteFileSizeBox
->setSingleStep(1);
92 m_remoteFileSizeBox
->setSuffix(QLatin1String(" MB"));
93 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
95 QHBoxLayout
* fileSizeBoxLayout
= new QHBoxLayout(this);
96 fileSizeBoxLayout
->addWidget(remoteFileSizeLabel
, 0, Qt::AlignRight
);
97 fileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
);
99 topLayout
->addWidget(listBox
);
100 topLayout
->addLayout(fileSizeBoxLayout
);
104 connect(m_listView
, SIGNAL(clicked(QModelIndex
)), this, SIGNAL(changed()));
105 connect(m_remoteFileSizeBox
, SIGNAL(valueChanged(int)), this, SIGNAL(changed()));
108 PreviewsSettingsPage::~PreviewsSettingsPage()
112 void PreviewsSettingsPage::applySettings()
114 const QAbstractItemModel
* model
= m_listView
->model();
115 const int rowCount
= model
->rowCount();
117 m_enabledPreviewPlugins
.clear();
118 for (int i
= 0; i
< rowCount
; ++i
) {
119 const QModelIndex index
= model
->index(i
, 0);
120 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
122 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
123 m_enabledPreviewPlugins
.append(enabledPlugin
);
128 KConfigGroup
globalConfig(KGlobal::config(), QLatin1String("PreviewSettings"));
129 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
131 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
132 globalConfig
.writeEntry("MaximumRemoteSize",
134 KConfigBase::Normal
| KConfigBase::Global
);
138 void PreviewsSettingsPage::restoreDefaults()
140 m_remoteFileSizeBox
->setValue(MaxRemotePreviewSize
);
143 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
145 if (!event
->spontaneous() && !m_initialized
) {
146 loadPreviewPlugins();
147 m_initialized
= true;
149 SettingsPageBase::showEvent(event
);
152 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
154 const QAbstractItemModel
* model
= index
.model();
155 const QString pluginName
= model
->data(index
).toString();
156 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
158 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
159 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
163 void PreviewsSettingsPage::loadPreviewPlugins()
165 QAbstractItemModel
* model
= m_listView
->model();
167 const KService::List plugins
= KServiceTypeTrader::self()->query(QLatin1String("ThumbCreator"));
168 foreach (const KSharedPtr
<KService
>& service
, plugins
) {
169 const bool configurable
= service
->property("Configurable", QVariant::Bool
).toBool();
170 const bool show
= m_enabledPreviewPlugins
.contains(service
->desktopEntryName());
173 const QModelIndex index
= model
->index(0, 0);
174 model
->setData(index
, show
, Qt::CheckStateRole
);
175 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
176 model
->setData(index
, service
->name(), Qt::DisplayRole
);
177 model
->setData(index
, service
->desktopEntryName(), ServiceModel::DesktopEntryNameRole
);
180 model
->sort(Qt::DisplayRole
);
183 void PreviewsSettingsPage::loadSettings()
185 KConfigGroup
globalConfig(KGlobal::config(), "PreviewSettings");
186 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", QStringList()
187 << QLatin1String("directorythumbnail")
188 << QLatin1String("imagethumbnail")
189 << QLatin1String("jpegthumbnail"));
191 // If the user is upgrading from KDE <= 4.6, we must check if he had the 'jpegrotatedthumbnail' plugin enabled.
192 // This plugin does not exist any more in KDE >= 4.7, so we have to replace it with the 'jpegthumbnail' plugin.
194 // Note that the upgrade to the correct plugin is done already in KFilePreviewGenerator. However, if Konqueror is
195 // opened in web browsing mode and the Settings dialog is opened, we might end up here before KFilePreviewGenerator's
196 // constructor is ever called -> the plugin replacement should be done here as well.
197 if(m_enabledPreviewPlugins
.contains(QLatin1String("jpegrotatedthumbnail"))) {
198 m_enabledPreviewPlugins
.removeAll(QLatin1String("jpegrotatedthumbnail"));
199 m_enabledPreviewPlugins
.append(QLatin1String("jpegthumbnail"));
200 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
204 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(MaxRemotePreviewSize
) * 1024 * 1024;
205 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
206 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
207 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);
210 #include "previewssettingspage.moc"