1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 ***************************************************************************/
20 #include "infosidebarpage.h"
22 #include <config-nepomuk.h>
25 #include <kdirnotify.h>
26 #include <kfileplacesmodel.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>
38 #include <QInputDialog>
42 #include <QResizeEvent>
44 #include <QVBoxLayout>
46 #include "dolphinsettings.h"
47 #include "metadatawidget.h"
48 #include "metatextlabel.h"
49 #include "pixmapviewer.h"
51 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
54 m_pendingPreview(false),
66 InfoSidebarPage::~InfoSidebarPage()
70 QSize
InfoSidebarPage::sizeHint() const
72 QSize size
= SidebarPage::sizeHint();
73 size
.setWidth(minimumSizeHint().width());
77 void InfoSidebarPage::setUrl(const KUrl
& url
)
79 SidebarPage::setUrl(url
);
80 if (url
.isValid() && !m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
91 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
97 if ((selection
.count() == 0) && (m_selection
.count() == 0)) {
98 // The selection has not really changed, only the current index.
99 // QItemSelectionModel emits a signal in this case and it is less
100 // expensive doing the check this way instead of patching
101 // DolphinView::emitSelectionChanged().
105 m_selection
= selection
;
107 const int count
= selection
.count();
112 if ((count
== 1) && !selection
.first().url().isEmpty()) {
113 m_urlCandidate
= selection
.first().url();
115 m_timer
->start(TimerDelay
);
119 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem
& item
)
121 if (!m_initialized
) {
127 m_fileItem
= KFileItem();
129 // The cursor is above the viewport. If files are selected,
130 // show information regarding the selection.
131 if (m_selection
.size() > 0) {
132 m_pendingPreview
= false;
133 m_timer
->start(TimerDelay
);
135 } else if (!item
.url().isEmpty()) {
136 m_urlCandidate
= item
.url();
138 m_timer
->start(TimerDelay
);
142 void InfoSidebarPage::showEvent(QShowEvent
* event
)
144 SidebarPage::showEvent(event
);
145 if (!event
->spontaneous()) {
146 if (!m_initialized
) {
147 // do a delayed initialization so that no performance
148 // penalty is given when Dolphin is started with a closed
156 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
159 // If the text inside the name label or the info label cannot
160 // get wrapped, then the maximum width of the label is increased
161 // so that the width of the information sidebar gets increased.
162 // To prevent this, the maximum width is adjusted to
163 // the current width of the sidebar.
164 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
165 m_nameLabel
->setMaximumWidth(maxWidth
);
167 // try to increase the preview as large as possible
168 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
169 m_urlCandidate
= m_shownUrl
; // reset the URL candidate if a resizing is done
170 m_timer
->start(TimerDelay
);
173 SidebarPage::resizeEvent(event
);
176 void InfoSidebarPage::showItemInfo()
184 const KUrl file
= fileUrl();
185 if (!file
.isValid()) {
189 if (showMultipleSelectionInfo()) {
190 KIconLoader iconLoader
;
191 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
192 KIconLoader::NoGroup
,
193 KIconLoader::SizeEnormous
);
194 m_preview
->setPixmap(icon
);
195 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", m_selection
.count()));
196 } else if (!applyPlace(file
)) {
197 // try to get a preview pixmap from the item...
201 m_pendingPreview
= true;
203 KIconEffect iconEffect
;
204 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(), KIconLoader::Desktop
, KIconLoader::DisabledState
);
205 m_preview
->setPixmap(disabledPixmap
);
207 KIO::PreviewJob
* job
= KIO::filePreview(list
,
214 job
->setIgnoreMaximumSize(true);
216 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
217 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
218 connect(job
, SIGNAL(failed(const KFileItem
&)),
219 this, SLOT(showIcon(const KFileItem
&)));
221 m_nameLabel
->setText(file
.fileName());
227 void InfoSidebarPage::slotTimeout()
229 m_shownUrl
= m_urlCandidate
;
233 void InfoSidebarPage::showIcon(const KFileItem
& item
)
235 m_pendingPreview
= false;
236 if (!applyPlace(item
.url())) {
237 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
241 void InfoSidebarPage::showPreview(const KFileItem
& item
,
242 const QPixmap
& pixmap
)
245 if (m_pendingPreview
) {
246 m_preview
->setPixmap(pixmap
);
247 m_pendingPreview
= false;
251 void InfoSidebarPage::slotFileRenamed(const QString
& source
, const QString
& dest
)
253 if (m_shownUrl
== KUrl(source
)) {
254 // the currently shown file has been renamed, hence update the item information
255 // for the renamed file
256 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(dest
));
257 requestDelayedItemInfo(item
);
261 void InfoSidebarPage::slotFilesAdded(const QString
& directory
)
263 if (m_shownUrl
== KUrl(directory
)) {
264 // If the 'trash' icon changes because the trash has been emptied or got filled,
265 // the signal filesAdded("trash:/") will be emitted.
266 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
267 requestDelayedItemInfo(item
);
271 void InfoSidebarPage::slotFilesChanged(const QStringList
& files
)
273 foreach (const QString
& fileName
, files
) {
274 if (m_shownUrl
== KUrl(fileName
)) {
281 void InfoSidebarPage::slotFilesRemoved(const QStringList
& files
)
283 foreach (const QString
& fileName
, files
) {
284 if (m_shownUrl
== KUrl(fileName
)) {
285 // the currently shown item has been removed, show
286 // the parent directory as fallback
294 void InfoSidebarPage::slotEnteredDirectory(const QString
& directory
)
296 if (m_shownUrl
== KUrl(directory
)) {
297 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
298 requestDelayedItemInfo(item
);
302 void InfoSidebarPage::slotLeftDirectory(const QString
& directory
)
304 if (m_shownUrl
== KUrl(directory
)) {
305 // The signal 'leftDirectory' is also emitted when a media
306 // has been unmounted. In this case no directory change will be
307 // done in Dolphin, but the Information Panel must be updated to
308 // indicate an invalid directory.
314 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
316 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
317 int count
= placesModel
->rowCount();
319 for (int i
= 0; i
< count
; ++i
) {
320 QModelIndex index
= placesModel
->index(i
, 0);
322 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
323 m_nameLabel
->setText(placesModel
->text(index
));
324 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
332 void InfoSidebarPage::cancelRequest()
337 void InfoSidebarPage::showMetaInfo()
339 m_metaTextLabel
->clear();
341 if (showMultipleSelectionInfo()) {
342 if (m_metaDataWidget
!= 0) {
344 foreach (const KFileItem
& item
, m_selection
) {
345 urls
.append(item
.targetUrl());
347 m_metaDataWidget
->setFiles(urls
);
350 quint64 totalSize
= 0;
351 foreach (const KFileItem
& item
, m_selection
) {
352 // Only count the size of files, not dirs to match what
353 // DolphinViewContainer::selectionStatusBarText() does.
354 if (!item
.isDir() && !item
.isLink()) {
355 totalSize
+= item
.size();
358 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
360 KFileItem
fileItem(KFileItem::Unknown
, KFileItem::Unknown
, fileUrl());
363 if (fileItem
.isDir()) {
364 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
365 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
367 m_metaTextLabel
->add(i18nc("@label", "Type:"), fileItem
.mimeComment());
369 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem
.size()));
370 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
372 if (fileItem
.isLocalFile()) {
373 // TODO: See convertMetaInfo below, find a way to display only interesting information
375 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
376 KFileMetaInfo::TechnicalInfo
|
377 KFileMetaInfo::ContentInfo
;
378 const QString path
= fileItem
.url().path();
379 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
380 if (fileMetaInfo
.isValid()) {
381 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
382 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
383 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
386 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
387 const QVariant
& value
= metaInfoItem
.value();
388 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
389 m_metaTextLabel
->add(labelText
, value
.toString());
397 if (m_metaDataWidget
!= 0) {
398 m_metaDataWidget
->setFile(fileItem
.targetUrl());
403 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
405 // TODO: This code prevents that interesting meta information might be hidden
406 // and only bypasses the current problem that not all the meta information should
407 // be shown to the user. Check whether it's possible with Nepomuk to show
408 // all "user relevant" information in a readable way...
415 // sorted list of keys, where its data should be shown
416 static const MetaKey keys
[] = {
417 { "audio.album", "Album:" },
418 { "audio.artist", "Artist:" },
419 { "audio.title", "Title:" },
422 // do a binary search for the key...
424 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
425 while (top
<= bottom
) {
426 const int middle
= (top
+ bottom
) / 2;
427 const int result
= key
.compare(keys
[middle
].key
);
430 } else if (result
> 0) {
433 text
= keys
[middle
].text
;
441 KUrl
InfoSidebarPage::fileUrl() const
443 return (!m_fileItem
.isNull() || m_selection
.isEmpty()) ? m_shownUrl
: m_selection
[0].url();
446 bool InfoSidebarPage::showMultipleSelectionInfo() const
448 return m_fileItem
.isNull() && (m_selection
.count() > 1);
451 void InfoSidebarPage::init()
453 const int spacing
= KDialog::spacingHint();
455 m_timer
= new QTimer(this);
456 m_timer
->setSingleShot(true);
457 connect(m_timer
, SIGNAL(timeout()),
458 this, SLOT(slotTimeout()));
460 QVBoxLayout
* layout
= new QVBoxLayout
;
461 layout
->setSpacing(spacing
);
464 m_nameLabel
= new QLabel(this);
465 QFont font
= m_nameLabel
->font();
467 m_nameLabel
->setFont(font
);
468 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
469 m_nameLabel
->setWordWrap(true);
470 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
473 m_preview
= new PixmapViewer(this);
474 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
475 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
477 if (MetaDataWidget::metaDataAvailable()) {
478 // rating, comment and tags
479 m_metaDataWidget
= new MetaDataWidget(this);
480 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
483 // general meta text information
484 m_metaTextLabel
= new MetaTextLabel(this);
485 m_metaTextLabel
->setMinimumWidth(spacing
);
486 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
488 layout
->addWidget(m_nameLabel
);
489 layout
->addWidget(new KSeparator(this));
490 layout
->addWidget(m_preview
);
491 layout
->addWidget(new KSeparator(this));
492 if (m_metaDataWidget
!= 0) {
493 layout
->addWidget(m_metaDataWidget
);
494 layout
->addWidget(new KSeparator(this));
496 layout
->addWidget(m_metaTextLabel
);
498 // ensure that widgets in the information side bar are aligned towards the top
499 layout
->addStretch(1);
502 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
503 QDBusConnection::sessionBus(), this);
504 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
505 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
506 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
507 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
508 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
509 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
511 m_initialized
= true;
514 #include "infosidebarpage.moc"