]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
Add unit test for #149736 (an old bug where a javascript handler closes the window...
[dolphin.git] / src / infosidebarpage.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 "infosidebarpage.h"
21
22 #include <config-nepomuk.h>
23
24 #include <kfileplacesmodel.h>
25 #include <klocale.h>
26 #include <kstandarddirs.h>
27 #include <kio/previewjob.h>
28 #include <kfileitem.h>
29 #include <kdialog.h>
30 #include <kglobalsettings.h>
31 #include <kfilemetainfo.h>
32 #include <kvbox.h>
33 #include <kseparator.h>
34 #include <kiconloader.h>
35
36 #include <QEvent>
37 #include <QInputDialog>
38 #include <QLabel>
39 #include <QPixmap>
40 #include <QResizeEvent>
41 #include <QTimer>
42 #include <QVBoxLayout>
43
44 #include "dolphinsettings.h"
45 #include "metadatawidget.h"
46 #include "pixmapviewer.h"
47
48 InfoSidebarPage::InfoSidebarPage(QWidget* parent) :
49 SidebarPage(parent),
50 m_pendingPreview(false),
51 m_shownUrl(),
52 m_urlCandidate(),
53 m_fileItem(),
54 m_preview(0),
55 m_nameLabel(0),
56 m_infoLabel(0),
57 m_metadataWidget(0)
58 {
59 const int spacing = KDialog::spacingHint();
60
61 m_timer = new QTimer(this);
62 m_timer->setSingleShot(true);
63 connect(m_timer, SIGNAL(timeout()),
64 this, SLOT(slotTimeout()));
65
66 QVBoxLayout* layout = new QVBoxLayout;
67 layout->setSpacing(spacing);
68
69 // preview
70 m_preview = new PixmapViewer(this);
71 m_preview->setMinimumWidth(KIconLoader::SizeEnormous);
72 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
73
74 // name
75 m_nameLabel = new QLabel(this);
76 m_nameLabel->setTextFormat(Qt::RichText);
77 m_nameLabel->setAlignment(m_nameLabel->alignment() | Qt::AlignHCenter);
78 m_nameLabel->setWordWrap(true);
79
80 // general information
81 m_infoLabel = new QLabel(this);
82 m_infoLabel->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
83 m_infoLabel->setTextFormat(Qt::RichText);
84 m_infoLabel->setWordWrap(true);
85 m_infoLabel->setFont(KGlobalSettings::smallestReadableFont());
86
87 if (MetaDataWidget::metaDataAvailable()) {
88 m_metadataWidget = new MetaDataWidget(this);
89 }
90
91 layout->addItem(new QSpacerItem(spacing, spacing, QSizePolicy::Preferred, QSizePolicy::Fixed));
92 layout->addWidget(m_preview);
93 layout->addWidget(m_nameLabel);
94 layout->addWidget(new KSeparator(this));
95 layout->addWidget(m_infoLabel);
96 if (m_metadataWidget != 0) {
97 layout->addWidget(new KSeparator(this));
98 layout->addWidget(m_metadataWidget);
99 }
100 // ensure that widgets in the information side bar are aligned towards the top
101 layout->addStretch(1);
102 setLayout(layout);
103 }
104
105 InfoSidebarPage::~InfoSidebarPage()
106 {
107 }
108
109 QSize InfoSidebarPage::sizeHint() const
110 {
111 QSize size = SidebarPage::sizeHint();
112 size.setWidth(minimumSizeHint().width());
113 return size;
114 }
115
116 void InfoSidebarPage::setUrl(const KUrl& url)
117 {
118 if (url.isValid() && !m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
119 cancelRequest();
120 m_shownUrl = url;
121 showItemInfo();
122 }
123 }
124
125 void InfoSidebarPage::setSelection(const KFileItemList& selection)
126 {
127 SidebarPage::setSelection(selection);
128 if (selection.size() == 1) {
129 const KUrl url = selection.first().url();
130 if (!url.isEmpty()) {
131 m_urlCandidate = url;
132 }
133 }
134 m_timer->start(TimerDelay);
135 }
136
137 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem& item)
138 {
139 cancelRequest();
140
141 m_fileItem = KFileItem();
142
143 if (!item.isNull() && (selection().size() <= 1)) {
144 const KUrl url = item.url();
145 if (!url.isEmpty()) {
146 m_urlCandidate = url;
147 m_fileItem = item;
148 m_timer->start(TimerDelay);
149 }
150 }
151 }
152
153 void InfoSidebarPage::showEvent(QShowEvent* event)
154 {
155 SidebarPage::showEvent(event);
156 if (event->spontaneous()) {
157 return;
158 }
159 showItemInfo();
160 }
161
162 void InfoSidebarPage::resizeEvent(QResizeEvent* event)
163 {
164 // If the text inside the name label or the info label cannot
165 // get wrapped, then the maximum width of the label is increased
166 // so that the width of the information sidebar gets increased.
167 // To prevent this, the maximum width is adjusted to
168 // the current width of the sidebar.
169 const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
170 m_nameLabel->setMaximumWidth(maxWidth);
171 m_infoLabel->setMaximumWidth(maxWidth);
172
173 // try to increase the preview as large as possible
174 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
175
176 // ---
177 // TODO: The following workaround was not necessary with Qt 4.3.x
178 // but is mandatory with the current Qt4.4 beta version. Check with
179 // Qt 4.4.0 whether the issue is Dolphin specific
180 // (see PixmapViewer::setSizeHint()) or a regression in Qt.
181 QVBoxLayout* vBox = static_cast<QVBoxLayout*>(layout());
182 vBox->removeWidget(m_preview);
183 vBox->insertWidget(0, m_preview);
184 // ---
185
186 m_urlCandidate = m_shownUrl; // reset the URL candidate if a resizing is done
187 m_timer->start(TimerDelay);
188
189 SidebarPage::resizeEvent(event);
190 }
191
192 void InfoSidebarPage::showItemInfo()
193 {
194 if (!isVisible()) {
195 return;
196 }
197
198 cancelRequest();
199
200 const KFileItemList& selectedItems = selection();
201
202 KUrl file;
203 if (selectedItems.isEmpty()) {
204 file = m_shownUrl;
205 } else {
206 file = selectedItems[0].url();
207 }
208 if (!file.isValid()) {
209 return;
210 }
211 const int itemCount = selectedItems.count();
212 if (itemCount > 1) {
213 KIconLoader iconLoader;
214 QPixmap icon = iconLoader.loadIcon("system-run",
215 KIconLoader::NoGroup,
216 m_preview->width());
217 m_preview->setPixmap(icon);
218 m_nameLabel->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectedItems.count()));
219 } else if (!applyPlace(file)) {
220 // try to get a preview pixmap from the item...
221 KUrl::List list;
222 list.append(file);
223
224 m_pendingPreview = true;
225 m_preview->setPixmap(QPixmap());
226
227 KIO::PreviewJob* job = KIO::filePreview(list,
228 m_preview->width(),
229 m_preview->height(),
230 0,
231 0,
232 true,
233 false);
234 job->setIgnoreMaximumSize(true);
235
236 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
237 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
238 connect(job, SIGNAL(failed(const KFileItem&)),
239 this, SLOT(showIcon(const KFileItem&)));
240
241 QString text("<b>");
242 text.append(file.fileName());
243 text.append("</b>");
244 m_nameLabel->setText(text);
245 }
246
247 showMetaInfo();
248 }
249
250 void InfoSidebarPage::slotTimeout()
251 {
252 m_shownUrl = m_urlCandidate;
253 showItemInfo();
254 }
255
256 void InfoSidebarPage::showIcon(const KFileItem& item)
257 {
258 m_pendingPreview = false;
259 if (!applyPlace(item.url())) {
260 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
261 }
262 }
263
264 void InfoSidebarPage::showPreview(const KFileItem& item,
265 const QPixmap& pixmap)
266 {
267 Q_UNUSED(item);
268 if (m_pendingPreview) {
269 m_preview->setPixmap(pixmap);
270 m_pendingPreview = false;
271 }
272 }
273
274 bool InfoSidebarPage::applyPlace(const KUrl& url)
275 {
276 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
277 int count = placesModel->rowCount();
278
279 for (int i = 0; i < count; ++i) {
280 QModelIndex index = placesModel->index(i, 0);
281
282 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
283 QString text("<b>");
284 text.append(placesModel->text(index));
285 text.append("</b>");
286 m_nameLabel->setText(text);
287
288 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
289 return true;
290 }
291 }
292
293 return false;
294 }
295
296 void InfoSidebarPage::cancelRequest()
297 {
298 m_timer->stop();
299 m_pendingPreview = false;
300 }
301
302 void InfoSidebarPage::showMetaInfo()
303 {
304 QString text;
305
306 const KFileItemList& selectedItems = selection();
307 if (selectedItems.size() <= 1) {
308 KFileItem fileItem;
309 if (m_fileItem.isNull()) {
310 // no pending request is ongoing
311 fileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
312 fileItem.refresh();
313 } else {
314 fileItem = m_fileItem;
315 }
316
317 if (fileItem.isDir()) {
318 addInfoLine(text, i18nc("@label", "Type:"), i18nc("@label", "Folder"));
319 } else {
320 addInfoLine(text, i18nc("@label", "Type:"), fileItem.mimeComment());
321
322 addInfoLine(text, i18nc("@label", "Size:"), KIO::convertSize(fileItem.size()));
323 addInfoLine(text, i18nc("@label", "Modified:"), fileItem.timeString());
324
325 // TODO: See convertMetaInfo below, find a way to display only interesting information
326 // in a readable way
327 const KFileMetaInfo::WhatFlags flags = KFileMetaInfo::Fastest |
328 KFileMetaInfo::TechnicalInfo |
329 KFileMetaInfo::ContentInfo |
330 KFileMetaInfo::Thumbnail;
331 const QString path = fileItem.url().url();
332 const KFileMetaInfo metaInfo(path, QString(), flags);
333 if (metaInfo.isValid()) {
334 const QHash<QString, KFileMetaInfoItem>& items = metaInfo.items();
335 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
336 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
337 QString labelText;
338 while (it != end) {
339 const KFileMetaInfoItem& metaInfo = it.value();
340 const QVariant& value = metaInfo.value();
341 if (value.isValid() && convertMetaInfo(metaInfo.name(), labelText)) {
342 addInfoLine(text, labelText, value.toString());
343 }
344 ++it;
345 }
346 }
347 }
348
349 if (MetaDataWidget::metaDataAvailable()) {
350 m_metadataWidget->setFile(fileItem.url());
351 }
352 } else {
353 if (MetaDataWidget::metaDataAvailable()) {
354 KUrl::List urls;
355 foreach (const KFileItem& item, selectedItems) {
356 urls.append(item.url());
357 }
358 m_metadataWidget->setFiles(urls);
359 }
360
361 unsigned long int totalSize = 0;
362 foreach (const KFileItem& item, selectedItems) {
363 // Only count the size of files, not dirs; to match what
364 // DolphinViewContainer::selectionStatusBarText does.
365 if (!item.isDir() && !item.isLink()) {
366 totalSize += item.size();
367 }
368 }
369 addInfoLine(text, i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
370 }
371 m_infoLabel->setText(text);
372 }
373
374 void InfoSidebarPage::addInfoLine(QString& text,
375 const QString& labelText,
376 const QString& infoText)
377 {
378 if (!text.isEmpty()) {
379 text += "<br/>";
380 }
381 text += QString("<b>%1</b> %2").arg(labelText).arg(infoText);
382 }
383
384 bool InfoSidebarPage::convertMetaInfo(const QString& key, QString& text) const
385 {
386 // TODO: This code prevents that interesting meta information might be hidden
387 // and only bypasses the current problem that not all the meta information should
388 // be shown to the user. Check whether it's possible with Nepomuk to show
389 // all "user relevant" information in a readable way...
390
391 struct MetaKey {
392 const char* key;
393 const char* text;
394 };
395
396 // sorted list of keys, where its data should be shown
397 static const MetaKey keys[] = {
398 { "audio.album", "Album:" },
399 { "audio.artist", "Artist:" },
400 { "audio.title", "Title:" },
401 };
402
403 // do a binary search for the key...
404 int top = 0;
405 int bottom = sizeof(keys) / sizeof(MetaKey) - 1;
406 while (top <= bottom) {
407 const int middle = (top + bottom) / 2;
408 const int result = key.compare(keys[middle].key);
409 if (result < 0) {
410 bottom = middle - 1;
411 } else if (result > 0) {
412 top = middle + 1;
413 } else {
414 text = keys[middle].text;
415 return true;
416 }
417 }
418
419 return false;
420 }
421
422 #include "infosidebarpage.moc"