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/dolphinsettings.h>
34 #include <settings/serviceitemdelegate.h>
35 #include <settings/servicemodel.h>
45 #include <QSortFilterProxyModel>
46 #include <QGridLayout>
47 #include <QVBoxLayout>
51 const bool UseThumbnails
= true;
52 const int MaxLocalPreviewSize
= 5; // 5 MB
53 const int MaxRemotePreviewSize
= 0; // 0 MB
56 PreviewsSettingsPage::PreviewsSettingsPage(QWidget
* parent
) :
57 SettingsPageBase(parent
),
60 m_enabledPreviewPlugins(),
61 m_localFileSizeBox(0),
62 m_remoteFileSizeBox(0)
64 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
65 topLayout
->setSpacing(KDialog::spacingHint());
66 topLayout
->setMargin(KDialog::marginHint());
68 // Create group box "Show previews for:"
69 QGroupBox
* listBox
= new QGroupBox(i18nc("@title:group", "Show previews for"), this);
71 m_listView
= new QListView(this);
73 ServiceItemDelegate
* delegate
= new ServiceItemDelegate(m_listView
, m_listView
);
74 connect(delegate
, SIGNAL(requestServiceConfiguration(QModelIndex
)),
75 this, SLOT(configureService(QModelIndex
)));
77 ServiceModel
* serviceModel
= new ServiceModel(this);
78 QSortFilterProxyModel
* proxyModel
= new QSortFilterProxyModel(this);
79 proxyModel
->setSourceModel(serviceModel
);
80 proxyModel
->setSortRole(Qt::DisplayRole
);
82 m_listView
->setModel(proxyModel
);
83 m_listView
->setItemDelegate(delegate
);
84 m_listView
->setVerticalScrollMode(QListView::ScrollPerPixel
);
86 QVBoxLayout
* listBoxLayout
= new QVBoxLayout(listBox
);
87 listBoxLayout
->addWidget(m_listView
);
89 // Create group box "Don't create previews for"
90 QGroupBox
* fileSizeBox
= new QGroupBox(i18nc("@title:group", "Do not create previews for"), this);
92 QLabel
* localFileSizeLabel
= new QLabel(i18nc("@label Don't create previews for: <Local files above:> XX MByte",
93 "Local files above:"), this);
95 m_localFileSizeBox
= new KIntSpinBox(this);
96 m_localFileSizeBox
->setSingleStep(1);
97 m_localFileSizeBox
->setSuffix(QLatin1String(" MB"));
98 m_localFileSizeBox
->setRange(0, 9999999); /* MB */
100 QLabel
* remoteFileSizeLabel
= new QLabel(i18nc("@label Don't create previews for: <Remote files above:> XX MByte",
101 "Remote files above:"), this);
103 m_remoteFileSizeBox
= new KIntSpinBox(this);
104 m_remoteFileSizeBox
->setSingleStep(1);
105 m_remoteFileSizeBox
->setSuffix(QLatin1String(" MB"));
106 m_remoteFileSizeBox
->setRange(0, 9999999); /* MB */
108 QGridLayout
* fileSizeBoxLayout
= new QGridLayout(fileSizeBox
);
109 fileSizeBoxLayout
->addWidget(localFileSizeLabel
, 0, 0, Qt::AlignRight
);
110 fileSizeBoxLayout
->addWidget(m_localFileSizeBox
, 0, 1);
111 fileSizeBoxLayout
->addWidget(remoteFileSizeLabel
, 1, 0, Qt::AlignRight
);
112 fileSizeBoxLayout
->addWidget(m_remoteFileSizeBox
, 1, 1);
114 topLayout
->addWidget(listBox
);
115 topLayout
->addWidget(fileSizeBox
);
119 connect(m_listView
, SIGNAL(clicked(QModelIndex
)), this, SIGNAL(changed()));
120 connect(m_localFileSizeBox
, SIGNAL(valueChanged(int)), this, SIGNAL(changed()));
121 connect(m_remoteFileSizeBox
, SIGNAL(valueChanged(int)), this, SIGNAL(changed()));
124 PreviewsSettingsPage::~PreviewsSettingsPage()
128 void PreviewsSettingsPage::applySettings()
130 const QAbstractItemModel
* model
= m_listView
->model();
131 const int rowCount
= model
->rowCount();
133 m_enabledPreviewPlugins
.clear();
134 for (int i
= 0; i
< rowCount
; ++i
) {
135 const QModelIndex index
= model
->index(i
, 0);
136 const bool checked
= model
->data(index
, Qt::CheckStateRole
).toBool();
138 const QString enabledPlugin
= model
->data(index
, Qt::UserRole
).toString();
139 m_enabledPreviewPlugins
.append(enabledPlugin
);
144 KConfigGroup
globalConfig(KGlobal::config(), QLatin1String("PreviewSettings"));
145 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
147 const qulonglong maximumLocalSize
= static_cast<qulonglong
>(m_localFileSizeBox
->value()) * 1024 * 1024;
148 globalConfig
.writeEntry("MaximumSize",
150 KConfigBase::Normal
| KConfigBase::Global
);
151 const qulonglong maximumRemoteSize
= static_cast<qulonglong
>(m_remoteFileSizeBox
->value()) * 1024 * 1024;
152 globalConfig
.writeEntry("MaximumRemoteSize",
154 KConfigBase::Normal
| KConfigBase::Global
);
158 void PreviewsSettingsPage::restoreDefaults()
160 m_localFileSizeBox
->setValue(MaxLocalPreviewSize
);
161 m_remoteFileSizeBox
->setValue(MaxRemotePreviewSize
);
164 void PreviewsSettingsPage::showEvent(QShowEvent
* event
)
166 if (!event
->spontaneous() && !m_initialized
) {
167 loadPreviewPlugins();
168 m_initialized
= true;
170 SettingsPageBase::showEvent(event
);
173 void PreviewsSettingsPage::configureService(const QModelIndex
& index
)
175 const QAbstractItemModel
* model
= index
.model();
176 const QString pluginName
= model
->data(index
).toString();
177 const QString desktopEntryName
= model
->data(index
, ServiceModel::DesktopEntryNameRole
).toString();
179 ConfigurePreviewPluginDialog
* dialog
= new ConfigurePreviewPluginDialog(pluginName
, desktopEntryName
, this);
180 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
184 void PreviewsSettingsPage::loadPreviewPlugins()
186 QAbstractItemModel
* model
= m_listView
->model();
188 const KService::List plugins
= KServiceTypeTrader::self()->query(QLatin1String("ThumbCreator"));
189 foreach (const KSharedPtr
<KService
>& service
, plugins
) {
190 const bool configurable
= service
->property("Configurable", QVariant::Bool
).toBool();
191 const bool show
= m_enabledPreviewPlugins
.contains(service
->desktopEntryName());
194 const QModelIndex index
= model
->index(0, 0);
195 model
->setData(index
, show
, Qt::CheckStateRole
);
196 model
->setData(index
, configurable
, ServiceModel::ConfigurableRole
);
197 model
->setData(index
, service
->name(), Qt::DisplayRole
);
198 model
->setData(index
, service
->desktopEntryName(), ServiceModel::DesktopEntryNameRole
);
201 model
->sort(Qt::DisplayRole
);
204 void PreviewsSettingsPage::loadSettings()
206 KConfigGroup
globalConfig(KGlobal::config(), "PreviewSettings");
207 m_enabledPreviewPlugins
= globalConfig
.readEntry("Plugins", QStringList()
208 << QLatin1String("directorythumbnail")
209 << QLatin1String("imagethumbnail")
210 << QLatin1String("jpegthumbnail"));
212 // If the user is upgrading from KDE <= 4.6, we must check if he had the 'jpegrotatedthumbnail' plugin enabled.
213 // This plugin does not exist any more in KDE >= 4.7, so we have to replace it with the 'jpegthumbnail' plugin.
215 // Note that the upgrade to the correct plugin is done already in KFilePreviewGenerator. However, if Konqueror is
216 // opened in web browsing mode and the Settings dialog is opened, we might end up here before KFilePreviewGenerator's
217 // constructor is ever called -> the plugin replacement should be done here as well.
218 if(m_enabledPreviewPlugins
.contains(QLatin1String("jpegrotatedthumbnail"))) {
219 m_enabledPreviewPlugins
.removeAll(QLatin1String("jpegrotatedthumbnail"));
220 m_enabledPreviewPlugins
.append(QLatin1String("jpegthumbnail"));
221 globalConfig
.writeEntry("Plugins", m_enabledPreviewPlugins
);
225 const qulonglong defaultLocalPreview
= static_cast<qulonglong
>(MaxLocalPreviewSize
) * 1024 * 1024;
226 const qulonglong maxLocalByteSize
= globalConfig
.readEntry("MaximumSize", defaultLocalPreview
);
227 const int maxLocalMByteSize
= maxLocalByteSize
/ (1024 * 1024);
228 m_localFileSizeBox
->setValue(maxLocalMByteSize
);
230 const qulonglong defaultRemotePreview
= static_cast<qulonglong
>(MaxRemotePreviewSize
) * 1024 * 1024;
231 const qulonglong maxRemoteByteSize
= globalConfig
.readEntry("MaximumRemoteSize", defaultRemotePreview
);
232 const int maxRemoteMByteSize
= maxRemoteByteSize
/ (1024 * 1024);
233 m_remoteFileSizeBox
->setValue(maxRemoteMByteSize
);
236 #include "previewssettingspage.moc"