]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/previewssettingspage.cpp
Allow configuring different maximum sizes for remote and local files
[dolphin.git] / src / settings / previewssettingspage.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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 #include "dolphinsettings.h"
22
23 #include "dolphin_generalsettings.h"
24
25 #include <kconfiggroup.h>
26 #include <kdialog.h>
27 #include <kglobal.h>
28 #include <klocale.h>
29 #include <KNumInput>
30 #include <kservicetypetrader.h>
31 #include <kservice.h>
32
33 #include <QCheckBox>
34 #include <QGroupBox>
35 #include <QLabel>
36 #include <QListWidget>
37 #include <QRadioButton>
38 #include <QShowEvent>
39 #include <QSlider>
40 #include <QGridLayout>
41
42 #include <kdebug.h>
43
44 // default settings
45 namespace {
46 const bool UseThumbnails = true;
47 const int MaxLocalPreviewSize = 5; // 5 MB
48 const int MaxRemotePreviewSize = 0; // 0 MB
49 }
50
51 PreviewsSettingsPage::PreviewsSettingsPage(QWidget* parent) :
52 SettingsPageBase(parent),
53 m_initialized(false),
54 m_previewPluginsList(0),
55 m_enabledPreviewPlugins(),
56 m_localFileSizeBox(0),
57 m_remoteFileSizeBox(0)
58 {
59 kDebug() << "--------- constructing!";
60 QVBoxLayout* topLayout = new QVBoxLayout(this);
61 topLayout->setSpacing(KDialog::spacingHint());
62 topLayout->setMargin(KDialog::marginHint());
63
64 QLabel* listDescription = new QLabel(i18nc("@label", "Show previews for:"), this);
65
66 m_previewPluginsList = new QListWidget(this);
67 m_previewPluginsList->setSortingEnabled(true);
68 m_previewPluginsList->setSelectionMode(QAbstractItemView::NoSelection);
69 connect(m_previewPluginsList, SIGNAL(itemClicked(QListWidgetItem*)),
70 this, SIGNAL(changed()));
71
72 QLabel* localFileSizeLabel = new QLabel(i18nc("@label", "Local file size maximum:"), this);
73
74 m_localFileSizeBox = new KIntSpinBox(this);
75 m_localFileSizeBox->setSingleStep(1);
76 m_localFileSizeBox->setSuffix(QLatin1String(" MB"));
77 m_localFileSizeBox->setRange(0, 9999); /* MB */
78 connect(m_localFileSizeBox, SIGNAL(valueChanged(int)),
79 this, SIGNAL(changed()));
80
81 QLabel* remoteFileSizeLabel = new QLabel(i18nc("@label", "Remote file size maximum:"), this);
82 m_remoteFileSizeBox = new KIntSpinBox(this);
83 m_remoteFileSizeBox->setSingleStep(1);
84 m_remoteFileSizeBox->setSuffix(QLatin1String(" MB"));
85 m_remoteFileSizeBox->setRange(0, 9999); /* MB */
86 connect(m_remoteFileSizeBox, SIGNAL(valueChanged(int)),
87 this, SIGNAL(changed()));
88
89 QGridLayout* gridLayout = new QGridLayout();
90 gridLayout->addWidget(localFileSizeLabel, 0, 0);
91 gridLayout->addWidget(m_localFileSizeBox, 0, 1);
92 gridLayout->addWidget(remoteFileSizeLabel, 1, 0);
93 gridLayout->addWidget(m_remoteFileSizeBox, 1, 1);
94
95 topLayout->addWidget(listDescription);
96 topLayout->addWidget(m_previewPluginsList);
97 topLayout->addLayout(gridLayout);
98
99 loadSettings();
100 }
101
102
103 PreviewsSettingsPage::~PreviewsSettingsPage()
104 {
105 }
106
107 void PreviewsSettingsPage::applySettings()
108 {
109 m_enabledPreviewPlugins.clear();
110 const int count = m_previewPluginsList->count();
111 for (int i = 0; i < count; ++i) {
112 const QListWidgetItem* item = m_previewPluginsList->item(i);
113 if (item->checkState() == Qt::Checked) {
114 const QString enabledPlugin = item->data(Qt::UserRole).toString();
115 m_enabledPreviewPlugins.append(enabledPlugin);
116 }
117 }
118
119 KConfigGroup globalConfig(KGlobal::config(), QLatin1String("PreviewSettings"));
120 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
121
122 globalConfig.writeEntry("MaximumSize",
123 m_localFileSizeBox->value() * 1024 * 1024,
124 KConfigBase::Normal | KConfigBase::Global);
125 globalConfig.writeEntry("RemoteMaximumSize",
126 m_remoteFileSizeBox->value() * 1024 * 1024,
127 KConfigBase::Normal | KConfigBase::Global);
128 globalConfig.sync();
129 }
130
131 void PreviewsSettingsPage::restoreDefaults()
132 {
133 m_localFileSizeBox->setValue(MaxLocalPreviewSize);
134 m_remoteFileSizeBox->setValue(MaxRemotePreviewSize);
135 }
136
137 void PreviewsSettingsPage::showEvent(QShowEvent* event)
138 {
139 if (!event->spontaneous() && !m_initialized) {
140 QMetaObject::invokeMethod(this, "loadPreviewPlugins", Qt::QueuedConnection);
141 m_initialized = true;
142 }
143 SettingsPageBase::showEvent(event);
144 }
145
146 void PreviewsSettingsPage::loadPreviewPlugins()
147 {
148 const KService::List plugins = KServiceTypeTrader::self()->query(QLatin1String("ThumbCreator"));
149 foreach (const KSharedPtr<KService>& service, plugins) {
150 QListWidgetItem* item = new QListWidgetItem(service->name(),
151 m_previewPluginsList);
152 item->setData(Qt::UserRole, service->desktopEntryName());
153 const bool show = m_enabledPreviewPlugins.contains(service->desktopEntryName());
154 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
155 }
156 }
157
158 void PreviewsSettingsPage::loadSettings()
159 {
160 KConfigGroup globalConfig(KGlobal::config(), "PreviewSettings");
161 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", QStringList()
162 << QLatin1String("directorythumbnail")
163 << QLatin1String("imagethumbnail")
164 << QLatin1String("jpegthumbnail"));
165 kDebug() << "----------------- enabled plugins:";
166 foreach (const QString& plugin, m_enabledPreviewPlugins) {
167 kDebug() << plugin;
168 }
169
170 // TODO: The default value of 5 MB must match with the default value inside
171 // kdelibs/kio/kio/previewjob.cpp. Maybe a static getter method in PreviewJob
172 // should be added for getting the default size?
173 const int maxLocalByteSize = globalConfig.readEntry("MaximumSize", MaxLocalPreviewSize * 1024 * 1024);
174 const int maxLocalMByteSize = maxLocalByteSize / (1024 * 1024);
175 m_localFileSizeBox->setValue(maxLocalMByteSize);
176
177 const int maxRemoteByteSize = globalConfig.readEntry("MaximumSize", MaxRemotePreviewSize * 1024 * 1024);
178 const int maxRemoteMByteSize = maxRemoteByteSize / (1024 * 1024);
179 m_localFileSizeBox->setValue(maxRemoteMByteSize);
180 }
181
182 #include "previewssettingspage.moc"