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
= (!m_fileItem
.isNull() || m_selection
.isEmpty()) ? m_shownUrl
: m_selection
[0].url();
185 if (!file
.isValid()) {
189 const int selectionCount
= m_selection
.count();
190 if (m_fileItem
.isNull() && (selectionCount
> 1)) {
191 KIconLoader iconLoader
;
192 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
193 KIconLoader::NoGroup
,
194 KIconLoader::SizeEnormous
);
195 m_preview
->setPixmap(icon
);
196 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectionCount
));
197 } else if (!applyPlace(file
)) {
198 // try to get a preview pixmap from the item...
202 m_pendingPreview
= true;
204 KIconEffect iconEffect
;
205 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(), KIconLoader::Desktop
, KIconLoader::DisabledState
);
206 m_preview
->setPixmap(disabledPixmap
);
208 KIO::PreviewJob
* job
= KIO::filePreview(list
,
215 job
->setIgnoreMaximumSize(true);
217 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
218 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
219 connect(job
, SIGNAL(failed(const KFileItem
&)),
220 this, SLOT(showIcon(const KFileItem
&)));
222 m_nameLabel
->setText(file
.fileName());
228 void InfoSidebarPage::slotTimeout()
230 m_shownUrl
= m_urlCandidate
;
234 void InfoSidebarPage::showIcon(const KFileItem
& item
)
236 m_pendingPreview
= false;
237 if (!applyPlace(item
.url())) {
238 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
242 void InfoSidebarPage::showPreview(const KFileItem
& item
,
243 const QPixmap
& pixmap
)
246 if (m_pendingPreview
) {
247 m_preview
->setPixmap(pixmap
);
248 m_pendingPreview
= false;
252 void InfoSidebarPage::slotFileRenamed(const QString
& source
, const QString
& dest
)
254 if (m_shownUrl
== KUrl(source
)) {
255 // the currently shown file has been renamed, hence update the item information
256 // for the renamed file
257 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(dest
));
258 requestDelayedItemInfo(item
);
262 void InfoSidebarPage::slotFilesAdded(const QString
& directory
)
264 if (m_shownUrl
== KUrl(directory
)) {
265 // If the 'trash' icon changes because the trash has been emptied or got filled,
266 // the signal filesAdded("trash:/") will be emitted.
267 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
268 requestDelayedItemInfo(item
);
272 void InfoSidebarPage::slotFilesChanged(const QStringList
& files
)
274 foreach (const QString
& fileName
, files
) {
275 if (m_shownUrl
== KUrl(fileName
)) {
282 void InfoSidebarPage::slotFilesRemoved(const QStringList
& files
)
284 foreach (const QString
& fileName
, files
) {
285 if (m_shownUrl
== KUrl(fileName
)) {
286 // the currently shown item has been removed, show
287 // the parent directory as fallback
295 void InfoSidebarPage::slotEnteredDirectory(const QString
& directory
)
297 if (m_shownUrl
== KUrl(directory
)) {
298 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
299 requestDelayedItemInfo(item
);
303 void InfoSidebarPage::slotLeftDirectory(const QString
& directory
)
305 if (m_shownUrl
== KUrl(directory
)) {
306 // The signal 'leftDirectory' is also emitted when a media
307 // has been unmounted. In this case no directory change will be
308 // done in Dolphin, but the Information Panel must be updated to
309 // indicate an invalid directory.
315 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
317 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
318 int count
= placesModel
->rowCount();
320 for (int i
= 0; i
< count
; ++i
) {
321 QModelIndex index
= placesModel
->index(i
, 0);
323 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
324 m_nameLabel
->setText(placesModel
->text(index
));
325 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
333 void InfoSidebarPage::cancelRequest()
338 void InfoSidebarPage::showMetaInfo()
340 m_metaTextLabel
->clear();
342 if ((m_selection
.size() <= 1) || !m_fileItem
.isNull()) {
344 if (m_fileItem
.isNull()) {
345 // no pending request is ongoing
346 const KUrl url
= (m_selection
.size() == 1) ? m_selection
.first().url() : m_shownUrl
;
347 fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, url
);
350 fileItem
= m_fileItem
;
353 if (fileItem
.isDir()) {
354 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
355 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
357 m_metaTextLabel
->add(i18nc("@label", "Type:"), fileItem
.mimeComment());
359 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem
.size()));
360 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
362 if (fileItem
.isLocalFile()) {
363 // TODO: See convertMetaInfo below, find a way to display only interesting information
365 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
366 KFileMetaInfo::TechnicalInfo
|
367 KFileMetaInfo::ContentInfo
;
368 const QString path
= fileItem
.url().path();
369 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
370 if (fileMetaInfo
.isValid()) {
371 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
372 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
373 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
376 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
377 const QVariant
& value
= metaInfoItem
.value();
378 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
379 m_metaTextLabel
->add(labelText
, value
.toString());
387 if (m_metaDataWidget
!= 0) {
388 m_metaDataWidget
->setFile(fileItem
.targetUrl());
391 if (m_metaDataWidget
!= 0) {
393 foreach (const KFileItem
& item
, m_selection
) {
394 urls
.append(item
.targetUrl());
396 m_metaDataWidget
->setFiles(urls
);
399 quint64 totalSize
= 0;
400 foreach (const KFileItem
& item
, m_selection
) {
401 // Only count the size of files, not dirs to match what
402 // DolphinViewContainer::selectionStatusBarText() does.
403 if (!item
.isDir() && !item
.isLink()) {
404 totalSize
+= item
.size();
407 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
411 bool InfoSidebarPage::convertMetaInfo(const QString
& key
, QString
& text
) const
413 // TODO: This code prevents that interesting meta information might be hidden
414 // and only bypasses the current problem that not all the meta information should
415 // be shown to the user. Check whether it's possible with Nepomuk to show
416 // all "user relevant" information in a readable way...
423 // sorted list of keys, where its data should be shown
424 static const MetaKey keys
[] = {
425 { "audio.album", "Album:" },
426 { "audio.artist", "Artist:" },
427 { "audio.title", "Title:" },
430 // do a binary search for the key...
432 int bottom
= sizeof(keys
) / sizeof(MetaKey
) - 1;
433 while (top
<= bottom
) {
434 const int middle
= (top
+ bottom
) / 2;
435 const int result
= key
.compare(keys
[middle
].key
);
438 } else if (result
> 0) {
441 text
= keys
[middle
].text
;
449 void InfoSidebarPage::init()
451 const int spacing
= KDialog::spacingHint();
453 m_timer
= new QTimer(this);
454 m_timer
->setSingleShot(true);
455 connect(m_timer
, SIGNAL(timeout()),
456 this, SLOT(slotTimeout()));
458 QVBoxLayout
* layout
= new QVBoxLayout
;
459 layout
->setSpacing(spacing
);
462 m_nameLabel
= new QLabel(this);
463 QFont font
= m_nameLabel
->font();
465 m_nameLabel
->setFont(font
);
466 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
467 m_nameLabel
->setWordWrap(true);
468 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
471 m_preview
= new PixmapViewer(this);
472 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
473 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
475 if (MetaDataWidget::metaDataAvailable()) {
476 // rating, comment and tags
477 m_metaDataWidget
= new MetaDataWidget(this);
478 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
481 // general meta text information
482 m_metaTextLabel
= new MetaTextLabel(this);
483 m_metaTextLabel
->setMinimumWidth(spacing
);
484 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
486 layout
->addWidget(m_nameLabel
);
487 layout
->addWidget(new KSeparator(this));
488 layout
->addWidget(m_preview
);
489 layout
->addWidget(new KSeparator(this));
490 if (m_metaDataWidget
!= 0) {
491 layout
->addWidget(m_metaDataWidget
);
492 layout
->addWidget(new KSeparator(this));
494 layout
->addWidget(m_metaTextLabel
);
496 // ensure that widgets in the information side bar are aligned towards the top
497 layout
->addStretch(1);
500 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
501 QDBusConnection::sessionBus(), this);
502 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
503 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
504 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
505 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
506 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
507 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
509 m_initialized
= true;
512 #include "infosidebarpage.moc"