]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
Don't request any meta data, if the requested URL is equal to the currently shown...
[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 <kdialog.h>
25 #include <kdirnotify.h>
26 #include <kfileplacesmodel.h>
27 #include <klocale.h>
28 #include <kstandarddirs.h>
29 #include <kio/previewjob.h>
30 #include <kfileitem.h>
31 #include <kglobalsettings.h>
32 #include <kfilemetainfo.h>
33 #include <kiconeffect.h>
34 #include <kseparator.h>
35 #include <kiconloader.h>
36
37 #include <QEvent>
38 #include <QInputDialog>
39 #include <QLabel>
40 #include <QPainter>
41 #include <QPixmap>
42 #include <QResizeEvent>
43 #include <QTimer>
44 #include <QVBoxLayout>
45
46 #include "dolphinsettings.h"
47 #include "metadatawidget.h"
48 #include "metatextlabel.h"
49 #include "pixmapviewer.h"
50
51 InfoSidebarPage::InfoSidebarPage(QWidget* parent) :
52 SidebarPage(parent),
53 m_initialized(false),
54 m_pendingPreview(false),
55 m_infoTimer(0),
56 m_outdatedPreviewTimer(0),
57 m_shownUrl(),
58 m_urlCandidate(),
59 m_fileItem(),
60 m_selection(),
61 m_nameLabel(0),
62 m_preview(0),
63 m_metaDataWidget(0),
64 m_metaTextLabel(0)
65 {
66 }
67
68 InfoSidebarPage::~InfoSidebarPage()
69 {
70 }
71
72 QSize InfoSidebarPage::sizeHint() const
73 {
74 QSize size = SidebarPage::sizeHint();
75 size.setWidth(minimumSizeHint().width());
76 return size;
77 }
78
79 void InfoSidebarPage::setUrl(const KUrl& url)
80 {
81 SidebarPage::setUrl(url);
82 if (url.isValid() && !isEqualToShownUrl(url)) {
83 if (m_initialized) {
84 cancelRequest();
85 m_shownUrl = url;
86 showItemInfo();
87 } else {
88 m_shownUrl = url;
89 }
90 }
91 }
92
93 void InfoSidebarPage::setSelection(const KFileItemList& selection)
94 {
95 if (!m_initialized) {
96 return;
97 }
98
99 if ((selection.count() == 0) && (m_selection.count() == 0)) {
100 // The selection has not really changed, only the current index.
101 // QItemSelectionModel emits a signal in this case and it is less
102 // expensive doing the check this way instead of patching
103 // DolphinView::emitSelectionChanged().
104 return;
105 }
106
107 m_selection = selection;
108
109 const int count = selection.count();
110 if (count == 0) {
111 if (!isEqualToShownUrl(url())) {
112 m_shownUrl = url();
113 showItemInfo();
114 }
115 } else {
116 if ((count == 1) && !selection.first().url().isEmpty()) {
117 m_urlCandidate = selection.first().url();
118 }
119 m_infoTimer->start();
120 }
121 }
122
123 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem& item)
124 {
125 if (!m_initialized) {
126 return;
127 }
128
129 cancelRequest();
130
131 m_fileItem = KFileItem();
132 if (item.isNull()) {
133 // The cursor is above the viewport. If files are selected,
134 // show information regarding the selection.
135 if (m_selection.size() > 0) {
136 m_pendingPreview = false;
137 m_infoTimer->start();
138 }
139 } else {
140 const KUrl url = item.url();
141 if (url.isValid() && !isEqualToShownUrl(url)) {
142 m_urlCandidate = item.url();
143 m_fileItem = item;
144 m_infoTimer->start();
145 }
146 }
147 }
148
149 void InfoSidebarPage::showEvent(QShowEvent* event)
150 {
151 SidebarPage::showEvent(event);
152 if (!event->spontaneous()) {
153 if (!m_initialized) {
154 // do a delayed initialization so that no performance
155 // penalty is given when Dolphin is started with a closed
156 // Information Panel
157 init();
158 }
159 showItemInfo();
160 }
161 }
162
163 void InfoSidebarPage::resizeEvent(QResizeEvent* event)
164 {
165 if (m_initialized) {
166 // If the text inside the name label or the info label cannot
167 // get wrapped, then the maximum width of the label is increased
168 // so that the width of the information sidebar gets increased.
169 // To prevent this, the maximum width is adjusted to
170 // the current width of the sidebar.
171 const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
172 m_nameLabel->setMaximumWidth(maxWidth);
173
174 // try to increase the preview as large as possible
175 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
176 m_urlCandidate = m_shownUrl; // reset the URL candidate if a resizing is done
177 m_infoTimer->start();
178 }
179
180 SidebarPage::resizeEvent(event);
181 }
182
183 void InfoSidebarPage::showItemInfo()
184 {
185 if (!isVisible()) {
186 return;
187 }
188
189 cancelRequest();
190
191 if (showMultipleSelectionInfo()) {
192 KIconLoader iconLoader;
193 QPixmap icon = iconLoader.loadIcon("dialog-information",
194 KIconLoader::NoGroup,
195 KIconLoader::SizeEnormous);
196 m_preview->setPixmap(icon);
197 m_nameLabel->setText(i18ncp("@info", "%1 item selected", "%1 items selected", m_selection.count()));
198 } else {
199 const KFileItem item = fileItem();
200 const KUrl itemUrl = item.url();
201 if (!applyPlace(itemUrl)) {
202 // try to get a preview pixmap from the item...
203 m_pendingPreview = true;
204
205 // Mark the currently shown preview as outdated. This is done
206 // with a small delay to prevent a flickering when the next preview
207 // can be shown within a short timeframe.
208 m_outdatedPreviewTimer->start();
209
210 KIO::PreviewJob* job = KIO::filePreview(KUrl::List() << itemUrl,
211 m_preview->width(),
212 m_preview->height(),
213 0,
214 0,
215 true,
216 false);
217 job->setIgnoreMaximumSize(true);
218
219 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
220 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
221 connect(job, SIGNAL(failed(const KFileItem&)),
222 this, SLOT(showIcon(const KFileItem&)));
223
224 m_nameLabel->setText(itemUrl.fileName());
225 }
226 }
227
228 showMetaInfo();
229 }
230
231 void InfoSidebarPage::slotInfoTimeout()
232 {
233 m_shownUrl = m_urlCandidate;
234 showItemInfo();
235 }
236
237 void InfoSidebarPage::markOutdatedPreview()
238 {
239 KIconEffect iconEffect;
240 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(),
241 KIconLoader::Desktop,
242 KIconLoader::DisabledState);
243 m_preview->setPixmap(disabledPixmap);
244 }
245
246 void InfoSidebarPage::showIcon(const KFileItem& item)
247 {
248 m_outdatedPreviewTimer->stop();
249 m_pendingPreview = false;
250 if (!applyPlace(item.url())) {
251 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
252 }
253 }
254
255 void InfoSidebarPage::showPreview(const KFileItem& item,
256 const QPixmap& pixmap)
257 {
258 m_outdatedPreviewTimer->stop();
259
260 Q_UNUSED(item);
261 if (m_pendingPreview) {
262 m_preview->setPixmap(pixmap);
263 m_pendingPreview = false;
264 }
265 }
266
267 void InfoSidebarPage::slotFileRenamed(const QString& source, const QString& dest)
268 {
269 if (m_shownUrl == KUrl(source)) {
270 // the currently shown file has been renamed, hence update the item information
271 // for the renamed file
272 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(dest));
273 requestDelayedItemInfo(item);
274 }
275 }
276
277 void InfoSidebarPage::slotFilesAdded(const QString& directory)
278 {
279 if (m_shownUrl == KUrl(directory)) {
280 // If the 'trash' icon changes because the trash has been emptied or got filled,
281 // the signal filesAdded("trash:/") will be emitted.
282 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
283 requestDelayedItemInfo(item);
284 }
285 }
286
287 void InfoSidebarPage::slotFilesChanged(const QStringList& files)
288 {
289 foreach (const QString& fileName, files) {
290 if (m_shownUrl == KUrl(fileName)) {
291 showItemInfo();
292 break;
293 }
294 }
295 }
296
297 void InfoSidebarPage::slotFilesRemoved(const QStringList& files)
298 {
299 foreach (const QString& fileName, files) {
300 if (m_shownUrl == KUrl(fileName)) {
301 // the currently shown item has been removed, show
302 // the parent directory as fallback
303 m_shownUrl = url();
304 showItemInfo();
305 break;
306 }
307 }
308 }
309
310 void InfoSidebarPage::slotEnteredDirectory(const QString& directory)
311 {
312 if (m_shownUrl == KUrl(directory)) {
313 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
314 requestDelayedItemInfo(item);
315 }
316 }
317
318 void InfoSidebarPage::slotLeftDirectory(const QString& directory)
319 {
320 if (m_shownUrl == KUrl(directory)) {
321 // The signal 'leftDirectory' is also emitted when a media
322 // has been unmounted. In this case no directory change will be
323 // done in Dolphin, but the Information Panel must be updated to
324 // indicate an invalid directory.
325 m_shownUrl = url();
326 showItemInfo();
327 }
328 }
329
330 bool InfoSidebarPage::applyPlace(const KUrl& url)
331 {
332 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
333 int count = placesModel->rowCount();
334
335 for (int i = 0; i < count; ++i) {
336 QModelIndex index = placesModel->index(i, 0);
337
338 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
339 m_nameLabel->setText(placesModel->text(index));
340 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
341 return true;
342 }
343 }
344
345 return false;
346 }
347
348 void InfoSidebarPage::cancelRequest()
349 {
350 m_infoTimer->stop();
351 }
352
353 void InfoSidebarPage::showMetaInfo()
354 {
355 m_metaTextLabel->clear();
356
357 if (showMultipleSelectionInfo()) {
358 if (m_metaDataWidget != 0) {
359 KUrl::List urls;
360 foreach (const KFileItem& item, m_selection) {
361 urls.append(item.targetUrl());
362 }
363 m_metaDataWidget->setFiles(urls);
364 }
365
366 quint64 totalSize = 0;
367 foreach (const KFileItem& item, m_selection) {
368 // Only count the size of files, not dirs to match what
369 // DolphinViewContainer::selectionStatusBarText() does.
370 if (!item.isDir() && !item.isLink()) {
371 totalSize += item.size();
372 }
373 }
374 m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
375 } else {
376 const KFileItem item = fileItem();
377 if (item.isDir()) {
378 m_metaTextLabel->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
379 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
380 } else {
381 m_metaTextLabel->add(i18nc("@label", "Type:"), item.mimeComment());
382
383 m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(item.size()));
384 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
385
386 if (item.isLocalFile()) {
387 // TODO: See convertMetaInfo below, find a way to display only interesting information
388 // in a readable way
389 const KFileMetaInfo::WhatFlags flags = KFileMetaInfo::Fastest |
390 KFileMetaInfo::TechnicalInfo |
391 KFileMetaInfo::ContentInfo;
392 const QString path = item.url().path();
393 const KFileMetaInfo fileMetaInfo(path, QString(), flags);
394 if (fileMetaInfo.isValid()) {
395 const QHash<QString, KFileMetaInfoItem>& items = fileMetaInfo.items();
396 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
397 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
398 QString labelText;
399 while (it != end) {
400 const KFileMetaInfoItem& metaInfoItem = it.value();
401 const QVariant& value = metaInfoItem.value();
402 if (value.isValid() && convertMetaInfo(metaInfoItem.name(), labelText)) {
403 m_metaTextLabel->add(labelText, value.toString());
404 }
405 ++it;
406 }
407 }
408 }
409 }
410
411 if (m_metaDataWidget != 0) {
412 m_metaDataWidget->setFile(item.targetUrl());
413 }
414 }
415 }
416
417 bool InfoSidebarPage::convertMetaInfo(const QString& key, QString& text) const
418 {
419 // TODO: This code prevents that interesting meta information might be hidden
420 // and only bypasses the current problem that not all the meta information should
421 // be shown to the user. Check whether it's possible with Nepomuk to show
422 // all "user relevant" information in a readable way...
423
424 struct MetaKey {
425 const char* key;
426 const char* text;
427 };
428
429 // sorted list of keys, where its data should be shown
430 static const MetaKey keys[] = {
431 { "audio.album", "Album:" },
432 { "audio.artist", "Artist:" },
433 { "audio.title", "Title:" },
434 };
435
436 // do a binary search for the key...
437 int top = 0;
438 int bottom = sizeof(keys) / sizeof(MetaKey) - 1;
439 while (top <= bottom) {
440 const int middle = (top + bottom) / 2;
441 const int result = key.compare(keys[middle].key);
442 if (result < 0) {
443 bottom = middle - 1;
444 } else if (result > 0) {
445 top = middle + 1;
446 } else {
447 text = keys[middle].text;
448 return true;
449 }
450 }
451
452 return false;
453 }
454
455 KFileItem InfoSidebarPage::fileItem() const
456 {
457 if (!m_fileItem.isNull()) {
458 return m_fileItem;
459 }
460
461 if (!m_selection.isEmpty()) {
462 Q_ASSERT(m_selection.count() == 1);
463 return m_selection.first();
464 }
465
466 // no item is hovered and no selection has been done: provide
467 // an item for the directory represented by m_shownUrl
468 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
469 item.refresh();
470 return item;
471 }
472
473 bool InfoSidebarPage::showMultipleSelectionInfo() const
474 {
475 return m_fileItem.isNull() && (m_selection.count() > 1);
476 }
477
478 bool InfoSidebarPage::isEqualToShownUrl(const KUrl& url) const
479 {
480 return m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash);
481 }
482
483 void InfoSidebarPage::init()
484 {
485 const int spacing = KDialog::spacingHint();
486
487 m_infoTimer = new QTimer(this);
488 m_infoTimer->setInterval(300);
489 m_infoTimer->setSingleShot(true);
490 connect(m_infoTimer, SIGNAL(timeout()),
491 this, SLOT(slotInfoTimeout()));
492
493 // Initialize timer for disabling an outdated preview with a small
494 // delay. This prevents flickering if the new preview can be generated
495 // within a very small timeframe.
496 m_outdatedPreviewTimer = new QTimer(this);
497 m_outdatedPreviewTimer->setInterval(300);
498 m_outdatedPreviewTimer->setSingleShot(true);
499 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
500 this, SLOT(markOutdatedPreview()));
501
502 QVBoxLayout* layout = new QVBoxLayout;
503 layout->setSpacing(spacing);
504
505 // name
506 m_nameLabel = new QLabel(this);
507 QFont font = m_nameLabel->font();
508 font.setBold(true);
509 m_nameLabel->setFont(font);
510 m_nameLabel->setAlignment(Qt::AlignHCenter);
511 m_nameLabel->setWordWrap(true);
512 m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
513
514 // preview
515 m_preview = new PixmapViewer(this);
516 m_preview->setMinimumWidth(KIconLoader::SizeEnormous + KIconLoader::SizeMedium);
517 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
518
519 if (MetaDataWidget::metaDataAvailable()) {
520 // rating, comment and tags
521 m_metaDataWidget = new MetaDataWidget(this);
522 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
523 }
524
525 // general meta text information
526 m_metaTextLabel = new MetaTextLabel(this);
527 m_metaTextLabel->setMinimumWidth(spacing);
528 m_metaTextLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
529
530 layout->addWidget(m_nameLabel);
531 layout->addWidget(new KSeparator(this));
532 layout->addWidget(m_preview);
533 layout->addWidget(new KSeparator(this));
534 if (m_metaDataWidget != 0) {
535 layout->addWidget(m_metaDataWidget);
536 layout->addWidget(new KSeparator(this));
537 }
538 layout->addWidget(m_metaTextLabel);
539
540 // ensure that widgets in the information side bar are aligned towards the top
541 layout->addStretch(1);
542 setLayout(layout);
543
544 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
545 QDBusConnection::sessionBus(), this);
546 connect(dirNotify, SIGNAL(FileRenamed(QString, QString)), SLOT(slotFileRenamed(QString, QString)));
547 connect(dirNotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
548 connect(dirNotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
549 connect(dirNotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
550 connect(dirNotify, SIGNAL(enteredDirectory(QString)), SLOT(slotEnteredDirectory(QString)));
551 connect(dirNotify, SIGNAL(leftDirectory(QString)), SLOT(slotLeftDirectory(QString)));
552
553 m_initialized = true;
554 }
555
556 #include "infosidebarpage.moc"