]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
Use the new fancy UDS_NEPOMUK_URI entry
[dolphin.git] / src / panels / information / informationpanelcontent.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 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 "informationpanelcontent.h"
21
22 #include <config-nepomuk.h>
23
24 #include <kdialog.h>
25 #include <kfileitem.h>
26 #include <kfileplacesmodel.h>
27 #include <kio/previewjob.h>
28 #include <kiconeffect.h>
29 #include <kiconloader.h>
30 #include <klocale.h>
31 #include <kmenu.h>
32 #include <kseparator.h>
33
34 #include <Phonon/BackendCapabilities>
35 #include <Phonon/MediaObject>
36 #include <Phonon/SeekSlider>
37
38 #include <QEvent>
39 #include <QLabel>
40 #include <QPixmap>
41 #include <QResizeEvent>
42 #include <QScrollArea>
43 #include <QTextLayout>
44 #include <QTextLine>
45 #include <QTimer>
46 #include <QVBoxLayout>
47
48 #include "dolphin_informationpanelsettings.h"
49 #include "settings/dolphinsettings.h"
50 #include "metadatawidget.h"
51 #include "metatextlabel.h"
52 #include "phononwidget.h"
53 #include "pixmapviewer.h"
54
55 #ifdef HAVE_NEPOMUK
56 #define DISABLE_NEPOMUK_LEGACY
57 #include <Nepomuk/Resource>
58 #include <Nepomuk/Types/Property>
59 #include <Nepomuk/Variant>
60 #endif
61
62 /**
63 * Helper function for sorting items with qSort() in
64 * InformationPanelContent::contextMenu().
65 */
66 bool lessThan(const QAction* action1, const QAction* action2)
67 {
68 return action1->text() < action2->text();
69 }
70
71
72 InformationPanelContent::InformationPanelContent(QWidget* parent) :
73 Panel(parent),
74 m_item(),
75 m_pendingPreview(false),
76 m_outdatedPreviewTimer(0),
77 m_nameLabel(0),
78 m_preview(0),
79 m_previewSeparator(0),
80 m_phononWidget(0),
81 m_metaDataWidget(0),
82 m_metaDataSeparator(0),
83 m_metaTextArea(0),
84 m_metaTextLabel(0)
85 {
86 parent->installEventFilter(this);
87
88 // Initialize timer for disabling an outdated preview with a small
89 // delay. This prevents flickering if the new preview can be generated
90 // within a very small timeframe.
91 m_outdatedPreviewTimer = new QTimer(this);
92 m_outdatedPreviewTimer->setInterval(300);
93 m_outdatedPreviewTimer->setSingleShot(true);
94 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
95 this, SLOT(markOutdatedPreview()));
96
97 QVBoxLayout* layout = new QVBoxLayout;
98 layout->setSpacing(KDialog::spacingHint());
99
100 // name
101 m_nameLabel = new QLabel(parent);
102 QFont font = m_nameLabel->font();
103 font.setBold(true);
104 m_nameLabel->setFont(font);
105 m_nameLabel->setAlignment(Qt::AlignHCenter);
106 m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
107 m_nameLabel->setMaximumWidth(KIconLoader::SizeEnormous);
108
109 // preview
110 const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
111
112 m_preview = new PixmapViewer(parent);
113 m_preview->setMinimumWidth(minPreviewWidth);
114 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
115
116 m_phononWidget = new PhononWidget(parent);
117 m_phononWidget->setMinimumWidth(minPreviewWidth);
118 connect(m_phononWidget, SIGNAL(playingStarted()),
119 this, SLOT(slotPlayingStarted()));
120 connect(m_phononWidget, SIGNAL(playingStopped()),
121 this, SLOT(slotPlayingStopped()));
122
123 m_previewSeparator = new KSeparator(parent);
124
125 const bool showPreview = InformationPanelSettings::showPreview();
126 m_preview->setVisible(showPreview);
127 m_previewSeparator->setVisible(showPreview);
128
129 if (MetaDataWidget::metaDataAvailable()) {
130 // rating, comment and tags
131 m_metaDataWidget = new MetaDataWidget(parent);
132 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
133 m_metaDataWidget->setMaximumWidth(KIconLoader::SizeEnormous);
134
135 const bool showRating = InformationPanelSettings::showRating();
136 const bool showComment = InformationPanelSettings::showComment();
137 const bool showTags = InformationPanelSettings::showTags();
138
139 m_metaDataWidget->setRatingVisible(showRating);
140 m_metaDataWidget->setCommentVisible(showComment);
141 m_metaDataWidget->setTagsVisible(showTags);
142
143 m_metaDataSeparator = new KSeparator(this);
144 m_metaDataSeparator->setVisible(showRating || showComment || showTags);
145 }
146
147 // general meta text information
148 m_metaTextLabel = new MetaTextLabel(parent);
149 m_metaTextLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
150
151 m_metaTextArea = new QScrollArea(parent);
152 m_metaTextArea->setWidget(m_metaTextLabel);
153 m_metaTextArea->setWidgetResizable(true);
154 m_metaTextArea->setFrameShape(QFrame::NoFrame);
155
156 QWidget* viewport = m_metaTextArea->viewport();
157 viewport->installEventFilter(this);
158
159 QPalette palette = viewport->palette();
160 palette.setColor(viewport->backgroundRole(), QColor(Qt::transparent));
161 viewport->setPalette(palette);
162
163 layout->addWidget(m_nameLabel);
164 layout->addWidget(new KSeparator(this));
165 layout->addWidget(m_preview);
166 layout->addWidget(m_phononWidget);
167 layout->addWidget(m_previewSeparator);
168 if (m_metaDataWidget != 0) {
169 layout->addWidget(m_metaDataWidget);
170 layout->addWidget(m_metaDataSeparator);
171 }
172 layout->addWidget(m_metaTextArea);
173 parent->setLayout(layout);
174 }
175
176 InformationPanelContent::~InformationPanelContent()
177 {
178 InformationPanelSettings::self()->writeConfig();
179 }
180
181 void InformationPanelContent::showItem(const KFileItem& item)
182 {
183 m_pendingPreview = false;
184
185 const KUrl itemUrl = item.url();
186 if (!applyPlace(itemUrl)) {
187 // try to get a preview pixmap from the item...
188 m_pendingPreview = true;
189
190 // Mark the currently shown preview as outdated. This is done
191 // with a small delay to prevent a flickering when the next preview
192 // can be shown within a short timeframe. This timer is not started
193 // for directories, as directory previews might fail and return the
194 // same icon.
195 if (!item.isDir()) {
196 m_outdatedPreviewTimer->start();
197 }
198
199 KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << item,
200 m_preview->width(),
201 m_preview->height(),
202 0,
203 0,
204 false,
205 true);
206
207 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
208 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
209 connect(job, SIGNAL(failed(const KFileItem&)),
210 this, SLOT(showIcon(const KFileItem&)));
211
212 setNameLabelText(itemUrl.fileName());
213 }
214
215 m_metaTextLabel->clear();
216 if (item.isDir()) {
217 m_metaTextLabel->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
218 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
219 } else {
220 m_metaTextLabel->add(i18nc("@label", "Type:"), item.mimeComment());
221
222 m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(item.size()));
223 m_metaTextLabel->add(i18nc("@label", "Modified:"), item.timeString());
224
225 #ifdef HAVE_NEPOMUK
226 if ( item.nepomukUri().isValid() ) {
227 KConfig config("kmetainformationrc", KConfig::NoGlobals);
228 KConfigGroup settings = config.group("Show");
229 initMetaInfoSettings(settings);
230
231 Nepomuk::Resource res(item.url());
232
233 QHash<QUrl, Nepomuk::Variant> properties = res.properties();
234 QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
235 while (it != properties.constEnd()) {
236 Nepomuk::Types::Property prop(it.key());
237 if (settings.readEntry(prop.name(), true)) {
238 // TODO #1: use Nepomuk::formatValue(res, prop) if available
239 // instead of it.value().toString()
240 // TODO #2: using tunedLabel() is a workaround for KDE 4.3 until
241 // we get translated labels
242 m_metaTextLabel->add(tunedLabel(prop.label()) + ':', it.value().toString());
243 }
244 ++it;
245 }
246 }
247 #endif
248 }
249
250 if (m_metaDataWidget != 0) {
251 if ( item.nepomukUri().isValid() ) {
252 m_metaDataWidget->setFile(item.nepomukUri());
253 m_metaDataWidget->show();
254 }
255 else {
256 m_metaDataWidget->hide();
257 }
258 }
259
260 if (InformationPanelSettings::showPreview()) {
261 const QString mimeType = item.mimetype();
262 const bool usePhonon = Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType) &&
263 (mimeType != "image/png"); // TODO: workaround, as Phonon
264 // thinks it supports PNG images
265 if (usePhonon) {
266 m_phononWidget->show();
267 PhononWidget::Mode mode = mimeType.startsWith(QLatin1String("video"))
268 ? PhononWidget::Video
269 : PhononWidget::Audio;
270 m_phononWidget->setMode(mode);
271 m_phononWidget->setUrl(item.url());
272 if ((mode == PhononWidget::Video) && m_preview->isVisible()) {
273 m_phononWidget->setVideoSize(m_preview->size());
274 }
275 } else {
276 m_phononWidget->hide();
277 m_preview->setVisible(true);
278 }
279 } else {
280 m_phononWidget->hide();
281 }
282
283 m_item = item;
284 }
285
286 void InformationPanelContent::showItems(const KFileItemList& items)
287 {
288 m_pendingPreview = false;
289
290 KIconLoader iconLoader;
291 QPixmap icon = iconLoader.loadIcon("dialog-information",
292 KIconLoader::NoGroup,
293 KIconLoader::SizeEnormous);
294 m_preview->setPixmap(icon);
295 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items.count()));
296
297 if (m_metaDataWidget != 0) {
298 KUrl::List urls;
299 foreach (const KFileItem& item, items) {
300 if ( item.nepomukUri().isValid() )
301 urls.append(item.nepomukUri());
302 }
303 m_metaDataWidget->setFiles(urls);
304 m_metaDataWidget->setVisible(!urls.isEmpty());
305 }
306
307 quint64 totalSize = 0;
308 foreach (const KFileItem& item, items) {
309 // Only count the size of files, not dirs to match what
310 // DolphinViewContainer::selectionStatusBarText() does.
311 if (!item.isDir() && !item.isLink()) {
312 totalSize += item.size();
313 }
314 }
315 m_metaTextLabel->clear();
316 m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
317
318 m_phononWidget->hide();
319
320 m_item = KFileItem();
321 }
322
323 bool InformationPanelContent::eventFilter(QObject* obj, QEvent* event)
324 {
325 if (event->type() == QEvent::Resize) {
326 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
327 if (obj == m_metaTextArea->viewport()) {
328 // The size of the meta text area has changed. Adjust the fixed
329 // width in a way that no horizontal scrollbar needs to be shown.
330 m_metaTextLabel->setFixedWidth(resizeEvent->size().width());
331 } else if (obj == parent()) {
332 // If the text inside the name label or the info label cannot
333 // get wrapped, then the maximum width of the label is increased
334 // so that the width of the information panel gets increased.
335 // To prevent this, the maximum width is adjusted to
336 // the current width of the panel.
337 const int maxWidth = resizeEvent->size().width() - KDialog::spacingHint() * 4;
338 m_nameLabel->setMaximumWidth(maxWidth);
339
340 // The metadata widget also contains a text widget which may return
341 // a large preferred width.
342 if (m_metaDataWidget != 0) {
343 m_metaDataWidget->setMaximumWidth(maxWidth);
344 }
345
346 // try to increase the preview as large as possible
347 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
348
349 if (m_phononWidget->isVisible() && (m_phononWidget->mode() == PhononWidget::Video)) {
350 // assure that the size of the video player is the same as the preview size
351 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
352 }
353 }
354 }
355 return Panel::eventFilter(obj, event);
356 }
357
358 void InformationPanelContent::configureSettings()
359 {
360 #ifdef HAVE_NEPOMUK
361 if (m_item.isNull() ||
362 !m_item.nepomukUri().isValid()) {
363 return;
364 }
365
366 KMenu popup(this);
367
368 QAction* previewAction = popup.addAction(i18nc("@action:inmenu", "Preview"));
369 previewAction->setIcon(KIcon("view-preview"));
370 previewAction->setCheckable(true);
371 previewAction->setChecked(InformationPanelSettings::showPreview());
372
373 const bool metaDataAvailable = MetaDataWidget::metaDataAvailable();
374
375 QAction* ratingAction = popup.addAction(i18nc("@action:inmenu", "Rating"));
376 ratingAction->setIcon(KIcon("rating"));
377 ratingAction->setCheckable(true);
378 ratingAction->setChecked(InformationPanelSettings::showRating());
379 ratingAction->setEnabled(metaDataAvailable);
380
381 QAction* commentAction = popup.addAction(i18nc("@action:inmenu", "Comment"));
382 commentAction->setIcon(KIcon("text-plain"));
383 commentAction->setCheckable(true);
384 commentAction->setChecked(InformationPanelSettings::showComment());
385 commentAction->setEnabled(metaDataAvailable);
386
387 QAction* tagsAction = popup.addAction(i18nc("@action:inmenu", "Tags"));
388 tagsAction->setCheckable(true);
389 tagsAction->setChecked(InformationPanelSettings::showTags());
390 tagsAction->setEnabled(metaDataAvailable);
391
392 KConfig config("kmetainformationrc", KConfig::NoGlobals);
393 KConfigGroup settings = config.group("Show");
394 initMetaInfoSettings(settings);
395
396 QList<QAction*> actions;
397
398 // Get all meta information labels that are available for
399 // the currently shown file item and add them to the popup.
400 Nepomuk::Resource res(m_item.url());
401 QHash<QUrl, Nepomuk::Variant> properties = res.properties();
402 QHash<QUrl, Nepomuk::Variant>::const_iterator it = properties.constBegin();
403 while (it != properties.constEnd()) {
404 Nepomuk::Types::Property prop(it.key());
405 const QString key = prop.name();
406
407 // Meta information provided by Nepomuk that is already
408 // available from KFileItem should not be configurable.
409 bool skip = (key == "fileExtension") ||
410 (key == "name") ||
411 (key == "sourceModified") ||
412 (key == "size") ||
413 (key == "mime type");
414 if (!skip) {
415 // Check whether there is already a meta information
416 // having the same label. In this case don't show it
417 // twice in the menu.
418 foreach (const QAction* action, actions) {
419 if (action->data().toString() == key) {
420 skip = true;
421 break;
422 }
423 }
424 }
425
426 if (!skip) {
427 const QString label = tunedLabel(prop.label());
428 QAction* action = new QAction(label, &popup);
429 action->setCheckable(true);
430 action->setChecked(settings.readEntry(key, true));
431 action->setData(key);
432 actions.append(action);
433 }
434
435 ++it;
436 }
437
438 if (!actions.isEmpty()) {
439 popup.addSeparator();
440
441 // add all items alphabetically sorted to the popup
442 qSort(actions.begin(), actions.end(), lessThan);
443 foreach (QAction* action, actions) {
444 popup.addAction(action);
445 }
446 }
447
448 // Open the popup and adjust the settings for the
449 // selected action.
450 QAction* action = popup.exec(QCursor::pos());
451 if (action == 0) {
452 return;
453 }
454
455 const bool isChecked = action->isChecked();
456 if (action == previewAction) {
457 m_preview->setVisible(isChecked);
458 m_previewSeparator->setVisible(isChecked);
459 InformationPanelSettings::setShowPreview(isChecked);
460 } else if (action == ratingAction) {
461 m_metaDataWidget->setRatingVisible(isChecked);
462 InformationPanelSettings::setShowRating(isChecked);
463 } else if (action == commentAction) {
464 m_metaDataWidget->setCommentVisible(isChecked);
465 InformationPanelSettings::setShowComment(isChecked);
466 } else if (action == tagsAction) {
467 m_metaDataWidget->setTagsVisible(isChecked);
468 InformationPanelSettings::setShowTags(isChecked);
469 } else {
470 settings.writeEntry(action->data().toString(), action->isChecked());
471 settings.sync();
472 }
473
474 if (m_metaDataWidget != 0) {
475 const bool visible = m_metaDataWidget->isRatingVisible() ||
476 m_metaDataWidget->isCommentVisible() ||
477 m_metaDataWidget->areTagsVisible();
478 m_metaDataSeparator->setVisible(visible);
479 }
480
481 showItem(m_item);
482 #endif
483 }
484
485 void InformationPanelContent::showIcon(const KFileItem& item)
486 {
487 m_outdatedPreviewTimer->stop();
488 m_pendingPreview = false;
489 if (!applyPlace(item.url())) {
490 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
491 }
492 }
493
494 void InformationPanelContent::showPreview(const KFileItem& item,
495 const QPixmap& pixmap)
496 {
497 m_outdatedPreviewTimer->stop();
498
499 Q_UNUSED(item);
500 if (m_pendingPreview) {
501 m_preview->setPixmap(pixmap);
502 m_pendingPreview = false;
503 }
504 }
505
506 void InformationPanelContent::markOutdatedPreview()
507 {
508 KIconEffect iconEffect;
509 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(),
510 KIconLoader::Desktop,
511 KIconLoader::DisabledState);
512 m_preview->setPixmap(disabledPixmap);
513 }
514
515 void InformationPanelContent::slotPlayingStarted()
516 {
517 m_preview->setVisible(m_phononWidget->mode() != PhononWidget::Video);
518 }
519
520 void InformationPanelContent::slotPlayingStopped()
521 {
522 m_preview->setVisible(true);
523 }
524
525 bool InformationPanelContent::applyPlace(const KUrl& url)
526 {
527 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
528 int count = placesModel->rowCount();
529
530 for (int i = 0; i < count; ++i) {
531 QModelIndex index = placesModel->index(i, 0);
532
533 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
534 setNameLabelText(placesModel->text(index));
535 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
536 return true;
537 }
538 }
539
540 return false;
541 }
542
543 void InformationPanelContent::setNameLabelText(const QString& text)
544 {
545 QTextOption textOption;
546 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
547
548 QTextLayout textLayout(text);
549 textLayout.setFont(m_nameLabel->font());
550 textLayout.setTextOption(textOption);
551
552 QString wrappedText;
553 wrappedText.reserve(text.length());
554
555 // wrap the text to fit into the width of m_nameLabel
556 textLayout.beginLayout();
557 QTextLine line = textLayout.createLine();
558 while (line.isValid()) {
559 line.setLineWidth(m_nameLabel->width());
560 wrappedText += text.mid(line.textStart(), line.textLength());
561
562 line = textLayout.createLine();
563 if (line.isValid()) {
564 wrappedText += QChar::LineSeparator;
565 }
566 }
567 textLayout.endLayout();
568
569 m_nameLabel->setText(wrappedText);
570 }
571
572 void InformationPanelContent::initMetaInfoSettings(KConfigGroup& group)
573 {
574 if (!group.readEntry("initialized", false)) {
575 // The resource file is read the first time. Assure
576 // that some meta information is disabled per default.
577
578 static const char* disabledProperties[] = {
579 "asText", "contentSize", "depth", "fileExtension",
580 "fileName", "fileSize", "isPartOf", "mimetype", "name",
581 "parentUrl", "plainTextContent", "sourceModified",
582 "size", "url",
583 0 // mandatory last entry
584 };
585
586 int i = 0;
587 while (disabledProperties[i] != 0) {
588 group.writeEntry(disabledProperties[i], false);
589 ++i;
590 }
591
592 // mark the group as initialized
593 group.writeEntry("initialized", true);
594 }
595 }
596
597 QString InformationPanelContent::tunedLabel(const QString& label) const
598 {
599 QString tunedLabel;
600 const int labelLength = label.length();
601 if (labelLength > 0) {
602 tunedLabel.reserve(labelLength);
603 tunedLabel = label[0].toUpper();
604 for (int i = 1; i < labelLength; ++i) {
605 if (label[i].isUpper() && !label[i - 1].isSpace() && !label[i - 1].isUpper()) {
606 tunedLabel += ' ';
607 tunedLabel += label[i].toLower();
608 } else {
609 tunedLabel += label[i];
610 }
611 }
612 }
613 return tunedLabel;
614 }
615
616 void InformationPanelContent::init()
617 {
618 }
619
620 #include "informationpanelcontent.moc"