]> cloud.milkyroute.net Git - dolphin.git/blob - src/settings/previewssettingspage.cpp
SVN_SILENT made messages (.desktop file)
[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 <khbox.h>
29 #include <klocale.h>
30 #include <KNumInput>
31 #include <kservicetypetrader.h>
32 #include <kservice.h>
33
34 #include <QCheckBox>
35 #include <QGroupBox>
36 #include <QLabel>
37 #include <QListWidget>
38 #include <QRadioButton>
39 #include <QShowEvent>
40 #include <QSlider>
41 #include <QBoxLayout>
42
43 // default settings
44 const bool USE_THUMBNAILS = true;
45 const int MAX_PREVIEW_SIZE = 5; // 5 MB
46
47 PreviewsSettingsPage::PreviewsSettingsPage(QWidget* parent) :
48 SettingsPageBase(parent),
49 m_initialized(false),
50 m_previewPluginsList(0),
51 m_enabledPreviewPlugins(),
52 m_maxPreviewSize(0),
53 m_spinBox(0),
54 m_useFileThumbnails(0)
55 {
56 QVBoxLayout* topLayout = new QVBoxLayout(this);
57 topLayout->setSpacing(KDialog::spacingHint());
58 topLayout->setMargin(KDialog::marginHint());
59
60 QLabel* listDescription = new QLabel(i18nc("@label", "Show previews for:"), this);
61
62 m_previewPluginsList = new QListWidget(this);
63 m_previewPluginsList->setSortingEnabled(true);
64 m_previewPluginsList->setSelectionMode(QAbstractItemView::NoSelection);
65 connect(m_previewPluginsList, SIGNAL(itemClicked(QListWidgetItem*)),
66 this, SIGNAL(changed()));
67
68 KHBox* hBox = new KHBox(this);
69 hBox->setSpacing(KDialog::spacingHint());
70
71 new QLabel(i18nc("@label:slider", "Maximum file size:"), hBox);
72 m_maxPreviewSize = new QSlider(Qt::Horizontal, hBox);
73 m_maxPreviewSize->setPageStep(10);
74 m_maxPreviewSize->setSingleStep(1);
75 m_maxPreviewSize->setTickPosition(QSlider::TicksBelow);
76 m_maxPreviewSize->setRange(1, 100); /* MB */
77
78 m_spinBox = new KIntSpinBox(hBox);
79 m_spinBox->setSingleStep(1);
80 m_spinBox->setSuffix(" MB");
81 m_spinBox->setRange(1, 100); /* MB */
82
83 connect(m_maxPreviewSize, SIGNAL(valueChanged(int)),
84 m_spinBox, SLOT(setValue(int)));
85 connect(m_spinBox, SIGNAL(valueChanged(int)),
86 m_maxPreviewSize, SLOT(setValue(int)));
87
88 connect(m_maxPreviewSize, SIGNAL(valueChanged(int)),
89 this, SIGNAL(changed()));
90 connect(m_spinBox, SIGNAL(valueChanged(int)),
91 this, SIGNAL(changed()));
92
93 m_useFileThumbnails = new QCheckBox(i18nc("@option:check", "Use thumbnails embedded in files"), this);
94 connect(m_useFileThumbnails, SIGNAL(toggled(bool)), this, SIGNAL(changed()));
95
96 topLayout->addWidget(listDescription);
97 topLayout->addWidget(m_previewPluginsList);
98 topLayout->addWidget(hBox);
99 topLayout->addWidget(m_useFileThumbnails);
100
101 loadSettings();
102 }
103
104
105 PreviewsSettingsPage::~PreviewsSettingsPage()
106 {
107 }
108
109 void PreviewsSettingsPage::applySettings()
110 {
111 m_enabledPreviewPlugins.clear();
112 const int count = m_previewPluginsList->count();
113 for (int i = 0; i < count; ++i) {
114 const QListWidgetItem* item = m_previewPluginsList->item(i);
115 if (item->checkState() == Qt::Checked) {
116 const QString enabledPlugin = item->data(Qt::UserRole).toString();
117 m_enabledPreviewPlugins.append(enabledPlugin);
118 }
119 }
120
121 KConfigGroup globalConfig(KGlobal::config(), "PreviewSettings");
122 globalConfig.writeEntry("Plugins", m_enabledPreviewPlugins);
123
124 const int byteCount = m_maxPreviewSize->value() * 1024 * 1024; // value() returns size in MB
125 globalConfig.writeEntry("MaximumSize",
126 byteCount,
127 KConfigBase::Normal | KConfigBase::Global);
128 globalConfig.writeEntry("UseFileThumbnails",
129 m_useFileThumbnails->isChecked(),
130 KConfigBase::Normal | KConfigBase::Global);
131 globalConfig.sync();
132 }
133
134 void PreviewsSettingsPage::restoreDefaults()
135 {
136 m_maxPreviewSize->setValue(MAX_PREVIEW_SIZE);
137 m_useFileThumbnails->setChecked(USE_THUMBNAILS);
138 }
139
140 void PreviewsSettingsPage::showEvent(QShowEvent* event)
141 {
142 if (!event->spontaneous() && !m_initialized) {
143 QMetaObject::invokeMethod(this, "loadPreviewPlugins", Qt::QueuedConnection);
144 m_initialized = true;
145 }
146 SettingsPageBase::showEvent(event);
147 }
148
149 void PreviewsSettingsPage::loadPreviewPlugins()
150 {
151 const KService::List plugins = KServiceTypeTrader::self()->query("ThumbCreator");
152 foreach (const KSharedPtr<KService>& service, plugins) {
153 QListWidgetItem* item = new QListWidgetItem(service->name(),
154 m_previewPluginsList);
155 item->setData(Qt::UserRole, service->desktopEntryName());
156 const bool show = m_enabledPreviewPlugins.contains(service->desktopEntryName());
157 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
158 }
159 }
160
161 void PreviewsSettingsPage::loadSettings()
162 {
163 KConfigGroup globalConfig(KGlobal::config(), "PreviewSettings");
164 m_enabledPreviewPlugins = globalConfig.readEntry("Plugins", QStringList()
165 << "directorythumbnail"
166 << "imagethumbnail"
167 << "jpegthumbnail");
168
169 // TODO: The default value of 5 MB must match with the default value inside
170 // kdelibs/kio/kio/previewjob.cpp. Maybe a static getter method in PreviewJob
171 // should be added for getting the default size?
172 const int min = 1; // MB
173 const int max = 100; // MB
174
175 const int maxByteSize = globalConfig.readEntry("MaximumSize", MAX_PREVIEW_SIZE * 1024 * 1024);
176 int maxMByteSize = maxByteSize / (1024 * 1024);
177 if (maxMByteSize < min) {
178 maxMByteSize = min;
179 } else if (maxMByteSize > max) {
180 maxMByteSize = max;
181 }
182 m_maxPreviewSize->setValue(maxMByteSize);
183
184 const bool useFileThumbnails = globalConfig.readEntry("UseFileThumbnails", USE_THUMBNAILS);
185 m_useFileThumbnails->setChecked(useFileThumbnails);
186 }
187
188 #include "previewssettingspage.moc"