]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/general/previewssettingspage.cpp
Remove unused #include
[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 #include "settings/serviceitemdelegate.h"
25 #include "settings/servicemodel.h"
26
27 #include <KIO/PreviewJob>
28 #include <KLocalizedString>
29 #include <KServiceTypeTrader>
30
31 #include <QHBoxLayout>
32 #include <QLabel>
33 #include <QListView>
34 #include <QPainter>
35 #include <QSortFilterProxyModel>
36 #include <QSpinBox>
37
38 // default settings
39 namespace {
40 const int MaxRemotePreviewSize = 0; // 0 MB
41 }
42
43 PreviewsSettingsPage::PreviewsSettingsPage(QWidget* parent) :
44 SettingsPageBase(parent),
45 m_initialized(false),
46 m_listView(nullptr),
47 m_enabledPreviewPlugins(),
48 m_remoteFileSizeBox(nullptr)
49 {
50 QVBoxLayout* topLayout = new QVBoxLayout(this);
51
52 QLabel* showPreviewsLabel = new QLabel(i18nc("@title:group", "Show previews for:"), this);
53
54 m_listView = new QListView(this);
55
56 ServiceItemDelegate* delegate = new ServiceItemDelegate(m_listView, m_listView);
57 connect(delegate, &ServiceItemDelegate::requestServiceConfiguration,
58 this, &PreviewsSettingsPage::configureService);
59
60 ServiceModel* serviceModel = new ServiceModel(this);
61 QSortFilterProxyModel* proxyModel = new QSortFilterProxyModel(this);
62 proxyModel->setSourceModel(serviceModel);
63 proxyModel->setSortRole(Qt::DisplayRole);
64
65 m_listView->setModel(proxyModel);
66 m_listView->setItemDelegate(delegate);
67 m_listView->setVerticalScrollMode(QListView::ScrollPerPixel);
68
69 QLabel* remoteFileSizeLabel = new QLabel(i18nc("@label", "Skip previews for remote files above:"), this);
70
71 m_remoteFileSizeBox = new QSpinBox(this);
72 m_remoteFileSizeBox->setSingleStep(1);
73 m_remoteFileSizeBox->setSuffix(QStringLiteral(" MB"));
74 m_remoteFileSizeBox->setRange(0, 9999999); /* MB */
75
76 QHBoxLayout* fileSizeBoxLayout = new QHBoxLayout();
77 fileSizeBoxLayout->addWidget(remoteFileSizeLabel, 0, Qt::AlignRight);
78 fileSizeBoxLayout->addWidget(m_remoteFileSizeBox);
79
80 topLayout->addWidget(showPreviewsLabel);
81 topLayout->addWidget(m_listView);
82 topLayout->addLayout(fileSizeBoxLayout);
83
84 loadSettings();
85
86 connect(m_listView, &QListView::clicked, this, &PreviewsSettingsPage::changed);
87 connect(m_remoteFileSizeBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &PreviewsSettingsPage::changed);
88 }
89
90 PreviewsSettingsPage::~PreviewsSettingsPage()
91 {
92 }
93
94 void PreviewsSettingsPage::applySettings()
95 {
96 const QAbstractItemModel* model = m_listView->model();
97 const int rowCount = model->rowCount();
98 if (rowCount > 0) {
99 m_enabledPreviewPlugins.clear();
100 for (int i = 0; i < rowCount; ++i) {
101 const QModelIndex index = model->index(i, 0);
102 const bool checked = model->data(index, Qt::CheckStateRole).toBool();
103 if (checked) {
104 const QString enabledPlugin = model->data(index, Qt::UserRole).toString();
105 m_enabledPreviewPlugins.append(enabledPlugin);
106 }
107 }
108 }
109
110 KConfigGroup globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
111 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
112
113 const qulonglong maximumRemoteSize = static_cast<qulonglong>(m_remoteFileSizeBox->value()) * 1024 * 1024;
114 globalConfig.writeEntry("MaximumRemoteSize",
115 maximumRemoteSize,
116 KConfigBase::Normal | KConfigBase::Global);
117 globalConfig.sync();
118 }
119
120 void PreviewsSettingsPage::restoreDefaults()
121 {
122 m_remoteFileSizeBox->setValue(MaxRemotePreviewSize);
123 }
124
125 void PreviewsSettingsPage::showEvent(QShowEvent* event)
126 {
127 if (!event->spontaneous() && !m_initialized) {
128 loadPreviewPlugins();
129 m_initialized = true;
130 }
131 SettingsPageBase::showEvent(event);
132 }
133
134 void PreviewsSettingsPage::configureService(const QModelIndex& index)
135 {
136 const QAbstractItemModel* model = index.model();
137 const QString pluginName = model->data(index).toString();
138 const QString desktopEntryName = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
139
140 ConfigurePreviewPluginDialog* dialog = new ConfigurePreviewPluginDialog(pluginName, desktopEntryName, this);
141 dialog->setAttribute(Qt::WA_DeleteOnClose);
142 dialog->show();
143 }
144
145 void PreviewsSettingsPage::loadPreviewPlugins()
146 {
147 QAbstractItemModel* model = m_listView->model();
148
149 const KService::List plugins = KServiceTypeTrader::self()->query(QStringLiteral("ThumbCreator"));
150 foreach (const KService::Ptr& service, plugins) {
151 const bool configurable = service->property(QStringLiteral("Configurable"), QVariant::Bool).toBool();
152 const bool show = m_enabledPreviewPlugins.contains(service->desktopEntryName());
153
154 model->insertRow(0);
155 const QModelIndex index = model->index(0, 0);
156 model->setData(index, show, Qt::CheckStateRole);
157 model->setData(index, configurable, ServiceModel::ConfigurableRole);
158 model->setData(index, service->name(), Qt::DisplayRole);
159 model->setData(index, service->desktopEntryName(), ServiceModel::DesktopEntryNameRole);
160 }
161
162 model->sort(Qt::DisplayRole);
163 }
164
165 void PreviewsSettingsPage::loadSettings()
166 {
167 const KConfigGroup globalConfig(KSharedConfig::openConfig(), QStringLiteral("PreviewSettings"));
168 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", KIO::PreviewJob::defaultPlugins());
169
170 const qulonglong defaultRemotePreview = static_cast<qulonglong>(MaxRemotePreviewSize) * 1024 * 1024;
171 const qulonglong maxRemoteByteSize = globalConfig.readEntry("MaximumRemoteSize", defaultRemotePreview);
172 const int maxRemoteMByteSize = maxRemoteByteSize / (1024 * 1024);
173 m_remoteFileSizeBox->setValue(maxRemoteMByteSize);
174 }
175