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