]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/general/previewssettingspage.cpp
Removed unused KGlobal includes and use KIO/Job instead of KIO/JobClasses
[dolphin.git] / src / settings / general / previewssettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "previewssettingspage.h"
21
22 #include "dolphin_generalsettings.h"
23 #include "configurepreviewplugindialog.h"
24
25 #include <KConfigGroup>
26 #include <KLocalizedString>
27 #include <KServiceTypeTrader>
28 #include <KService>
29
30 #include <settings/serviceitemdelegate.h>
31 #include <settings/servicemodel.h>
32
33 #include <QCheckBox>
34 #include <QGroupBox>
35 #include <QHBoxLayout>
36 #include <QLabel>
37 #include <QListView>
38 #include <QPainter>
39 #include <QScrollBar>
40 #include <QShowEvent>
41 #include <QSlider>
42 #include <QSpinBox>
43 #include <QSortFilterProxyModel>
44 #include <QVBoxLayout>
45
46 // default settings
47 namespace {
48 const bool UseThumbnails = true;
49 const int MaxRemotePreviewSize = 0; // 0 MB
50 }
51
52 PreviewsSettingsPage::PreviewsSettingsPage(QWidget* parent) :
53 SettingsPageBase(parent),
54 m_initialized(false),
55 m_listView(0),
56 m_enabledPreviewPlugins(),
57 m_remoteFileSizeBox(0)
58 {
59 QVBoxLayout* topLayout = new QVBoxLayout(this);
60
61 QLabel* showPreviewsLabel = new QLabel(i18nc("@title:group", "Show previews for:"), this);
62
63 m_listView = new QListView(this);
64
65 ServiceItemDelegate* delegate = new ServiceItemDelegate(m_listView, m_listView);
66 connect(delegate, &ServiceItemDelegate::requestServiceConfiguration,
67 this, &PreviewsSettingsPage::configureService);
68
69 ServiceModel* serviceModel = new ServiceModel(this);
70 QSortFilterProxyModel* proxyModel = new QSortFilterProxyModel(this);
71 proxyModel->setSourceModel(serviceModel);
72 proxyModel->setSortRole(Qt::DisplayRole);
73
74 m_listView->setModel(proxyModel);
75 m_listView->setItemDelegate(delegate);
76 m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
77
78 QLabel* remoteFileSizeLabel = new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
79
80 m_remoteFileSizeBox = new QSpinBox(this);
81 m_remoteFileSizeBox->setSingleStep(1);
82 m_remoteFileSizeBox->setSuffix(QLatin1String(" MB"));
83 m_remoteFileSizeBox->setRange(0, 9999999); /* MB */
84
85 QHBoxLayout* fileSizeBoxLayout = new QHBoxLayout(this);
86 fileSizeBoxLayout->addWidget(remoteFileSizeLabel, 0, Qt::AlignRight);
87 fileSizeBoxLayout->addWidget(m_remoteFileSizeBox);
88
89 topLayout->addWidget(showPreviewsLabel);
90 topLayout->addWidget(m_listView);
91 topLayout->addLayout(fileSizeBoxLayout);
92
93 loadSettings();
94
95 connect(m_listView, &QListView::clicked, this, &PreviewsSettingsPage::changed);
96 connect(m_remoteFileSizeBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &PreviewsSettingsPage::changed);
97 }
98
99 PreviewsSettingsPage::~PreviewsSettingsPage()
100 {
101 }
102
103 void PreviewsSettingsPage::applySettings()
104 {
105 const QAbstractItemModel* model = m_listView->model();
106 const int rowCount = model->rowCount();
107 if (rowCount > 0) {
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();
112 if (checked) {
113 const QString enabledPlugin = model->data(index, Qt::UserRole).toString();
114 m_enabledPreviewPlugins.append(enabledPlugin);
115 }
116 }
117 }
118
119 KConfigGroup globalConfig(KSharedConfig::openConfig(), QLatin1String("PreviewSettings"));
120 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
121
122 const qulonglong maximumRemoteSize = static_cast<qulonglong>(m_remoteFileSizeBox->value()) * 1024 * 1024;
123 globalConfig.writeEntry("MaximumRemoteSize",
124 maximumRemoteSize,
125 KConfigBase::Normal | KConfigBase::Global);
126 globalConfig.sync();
127 }
128
129 void PreviewsSettingsPage::restoreDefaults()
130 {
131 m_remoteFileSizeBox->setValue(MaxRemotePreviewSize);
132 }
133
134 void PreviewsSettingsPage::showEvent(QShowEvent* event)
135 {
136 if (!event->spontaneous() && !m_initialized) {
137 loadPreviewPlugins();
138 m_initialized = true;
139 }
140 SettingsPageBase::showEvent(event);
141 }
142
143 void PreviewsSettingsPage::configureService(const QModelIndex& index)
144 {
145 const QAbstractItemModel* model = index.model();
146 const QString pluginName = model->data(index).toString();
147 const QString desktopEntryName = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
148
149 ConfigurePreviewPluginDialog* dialog = new ConfigurePreviewPluginDialog(pluginName, desktopEntryName, this);
150 dialog->setAttribute(Qt::WA_DeleteOnClose);
151 dialog->show();
152 }
153
154 void PreviewsSettingsPage::loadPreviewPlugins()
155 {
156 QAbstractItemModel* model = m_listView->model();
157
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());
162
163 model->insertRow(0);
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);
169 }
170
171 model->sort(Qt::DisplayRole);
172 }
173
174 void PreviewsSettingsPage::loadSettings()
175 {
176 KConfigGroup globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
177 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", QStringList()
178 << QLatin1String("directorythumbnail")
179 << QLatin1String("imagethumbnail")
180 << QLatin1String("jpegthumbnail"));
181
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);
186 }
187