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 m_selection
= selection
;
99 const int count
= selection
.count();
104 if ((count
== 1) && !selection
.first().url().isEmpty()) {
105 m_urlCandidate
= selection
.first().url();
107 m_timer
->start(TimerDelay
);
111 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem
& item
)
113 if (!m_initialized
) {
119 m_fileItem
= KFileItem();
121 // The cursor is above the viewport. If files are selected,
122 // show information regarding the selection.
123 if (m_selection
.size() > 0) {
124 m_pendingPreview
= false;
125 m_timer
->start(TimerDelay
);
127 } else if (!item
.url().isEmpty()) {
128 m_urlCandidate
= item
.url();
130 m_timer
->start(TimerDelay
);
134 void InfoSidebarPage::showEvent(QShowEvent
* event
)
136 SidebarPage::showEvent(event
);
137 if (!event
->spontaneous()) {
138 if (!m_initialized
) {
139 // do a delayed initialization so that no performance
140 // penalty is given when Dolphin is started with a closed
148 void InfoSidebarPage::resizeEvent(QResizeEvent
* event
)
151 // If the text inside the name label or the info label cannot
152 // get wrapped, then the maximum width of the label is increased
153 // so that the width of the information sidebar gets increased.
154 // To prevent this, the maximum width is adjusted to
155 // the current width of the sidebar.
156 const int maxWidth
= event
->size().width() - KDialog::spacingHint() * 4;
157 m_nameLabel
->setMaximumWidth(maxWidth
);
159 // try to increase the preview as large as possible
160 m_preview
->setSizeHint(QSize(maxWidth
, maxWidth
));
161 m_urlCandidate
= m_shownUrl
; // reset the URL candidate if a resizing is done
162 m_timer
->start(TimerDelay
);
165 SidebarPage::resizeEvent(event
);
168 void InfoSidebarPage::showItemInfo()
176 const KUrl file
= (!m_fileItem
.isNull() || m_selection
.isEmpty()) ? m_shownUrl
: m_selection
[0].url();
177 if (!file
.isValid()) {
181 const int selectionCount
= m_selection
.count();
182 if (m_fileItem
.isNull() && (selectionCount
> 1)) {
183 KIconLoader iconLoader
;
184 QPixmap icon
= iconLoader
.loadIcon("dialog-information",
185 KIconLoader::NoGroup
,
186 KIconLoader::SizeEnormous
);
187 m_preview
->setPixmap(icon
);
188 m_nameLabel
->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectionCount
));
189 } else if (!applyPlace(file
)) {
190 // try to get a preview pixmap from the item...
194 m_pendingPreview
= true;
196 KIconEffect iconEffect
;
197 QPixmap disabledPixmap
= iconEffect
.apply(m_preview
->pixmap(), KIconLoader::Desktop
, KIconLoader::DisabledState
);
198 m_preview
->setPixmap(disabledPixmap
);
200 KIO::PreviewJob
* job
= KIO::filePreview(list
,
207 job
->setIgnoreMaximumSize(true);
209 connect(job
, SIGNAL(gotPreview(const KFileItem
&, const QPixmap
&)),
210 this, SLOT(showPreview(const KFileItem
&, const QPixmap
&)));
211 connect(job
, SIGNAL(failed(const KFileItem
&)),
212 this, SLOT(showIcon(const KFileItem
&)));
214 m_nameLabel
->setText(file
.fileName());
220 void InfoSidebarPage::slotTimeout()
222 m_shownUrl
= m_urlCandidate
;
226 void InfoSidebarPage::showIcon(const KFileItem
& item
)
228 m_pendingPreview
= false;
229 if (!applyPlace(item
.url())) {
230 m_preview
->setPixmap(item
.pixmap(KIconLoader::SizeEnormous
));
234 void InfoSidebarPage::showPreview(const KFileItem
& item
,
235 const QPixmap
& pixmap
)
238 if (m_pendingPreview
) {
239 m_preview
->setPixmap(pixmap
);
240 m_pendingPreview
= false;
244 void InfoSidebarPage::slotFileRenamed(const QString
& source
, const QString
& dest
)
246 if (m_shownUrl
== KUrl(source
)) {
247 // the currently shown file has been renamed, hence update the item information
248 // for the renamed file
249 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(dest
));
250 requestDelayedItemInfo(item
);
254 void InfoSidebarPage::slotFilesAdded(const QString
& directory
)
256 if (m_shownUrl
== KUrl(directory
)) {
257 // If the 'trash' icon changes because the trash has been emptied or got filled,
258 // the signal filesAdded("trash:/") will be emitted.
259 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
260 requestDelayedItemInfo(item
);
264 void InfoSidebarPage::slotFilesChanged(const QStringList
& files
)
266 foreach (const QString
& fileName
, files
) {
267 if (m_shownUrl
== KUrl(fileName
)) {
274 void InfoSidebarPage::slotFilesRemoved(const QStringList
& files
)
276 foreach (const QString
& fileName
, files
) {
277 if (m_shownUrl
== KUrl(fileName
)) {
278 // the currently shown item has been removed, show
279 // the parent directory as fallback
287 void InfoSidebarPage::slotEnteredDirectory(const QString
& directory
)
289 if (m_shownUrl
== KUrl(directory
)) {
290 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
291 requestDelayedItemInfo(item
);
295 void InfoSidebarPage::slotLeftDirectory(const QString
& directory
)
297 if (m_shownUrl
== KUrl(directory
)) {
298 // The signal 'leftDirectory' is also emitted when a media
299 // has been unmounted. In this case no directory change will be
300 // done in Dolphin, but the Information Panel must be updated to
301 // indicate an invalid directory.
307 bool InfoSidebarPage::applyPlace(const KUrl
& url
)
309 KFilePlacesModel
* placesModel
= DolphinSettings::instance().placesModel();
310 int count
= placesModel
->rowCount();
312 for (int i
= 0; i
< count
; ++i
) {
313 QModelIndex index
= placesModel
->index(i
, 0);
315 if (url
.equals(placesModel
->url(index
), KUrl::CompareWithoutTrailingSlash
)) {
316 m_nameLabel
->setText(placesModel
->text(index
));
317 m_preview
->setPixmap(placesModel
->icon(index
).pixmap(128, 128));
325 void InfoSidebarPage::cancelRequest()
330 void InfoSidebarPage::showMetaInfo()
332 m_metaTextLabel
->clear();
334 if ((m_selection
.size() <= 1) || !m_fileItem
.isNull()) {
336 if (m_fileItem
.isNull()) {
337 // no pending request is ongoing
338 const KUrl url
= (m_selection
.size() == 1) ? m_selection
.first().url() : m_shownUrl
;
339 fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, url
);
342 fileItem
= m_fileItem
;
345 if (fileItem
.isDir()) {
346 m_metaTextLabel
->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
347 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
349 m_metaTextLabel
->add(i18nc("@label", "Type:"), fileItem
.mimeComment());
351 m_metaTextLabel
->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem
.size()));
352 m_metaTextLabel
->add(i18nc("@label", "Modified:"), fileItem
.timeString());
354 if (fileItem
.isLocalFile()) {
355 // TODO: See convertMetaInfo below, find a way to display only interesting information
357 const KFileMetaInfo::WhatFlags flags
= KFileMetaInfo::Fastest
|
358 KFileMetaInfo::TechnicalInfo
|
359 KFileMetaInfo::ContentInfo
;
360 const QString path
= fileItem
.url().path();
361 const KFileMetaInfo
fileMetaInfo(path
, QString(), flags
);
362 if (fileMetaInfo
.isValid()) {
363 const QHash
<QString
, KFileMetaInfoItem
>& items
= fileMetaInfo
.items();
364 QHash
<QString
, KFileMetaInfoItem
>::const_iterator it
= items
.constBegin();
365 const QHash
<QString
, KFileMetaInfoItem
>::const_iterator end
= items
.constEnd();
368 const KFileMetaInfoItem
& metaInfoItem
= it
.value();
369 const QVariant
& value
= metaInfoItem
.value();
370 if (value
.isValid() && convertMetaInfo(metaInfoItem
.name(), labelText
)) {
371 m_metaTextLabel
->add(labelText
, value
.toString());
379 if (m_metaDataWidget
!= 0) {
380 m_metaDataWidget
->setFile(fileItem
.targetUrl());
383 if (m_metaDataWidget
!= 0) {
385 foreach (const KFileItem
& item
, m_selection
) {
386 urls
.append(item
.targetUrl());
388 m_metaDataWidget
->setFiles(urls
);
391 quint64 totalSize
= 0;
392 foreach (const KFileItem
& item
, m_selection
) {
393 // Only count the size of files, not dirs to match what
394 // DolphinViewContainer::selectionStatusBarText() does.
395 if (!item
.isDir() && !item
.isLink()) {
396 totalSize
+= item
.size();
399 m_metaTextLabel
->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize
));
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 void InfoSidebarPage::init()
443 const int spacing
= KDialog::spacingHint();
445 m_timer
= new QTimer(this);
446 m_timer
->setSingleShot(true);
447 connect(m_timer
, SIGNAL(timeout()),
448 this, SLOT(slotTimeout()));
450 QVBoxLayout
* layout
= new QVBoxLayout
;
451 layout
->setSpacing(spacing
);
454 m_nameLabel
= new QLabel(this);
455 QFont font
= m_nameLabel
->font();
457 m_nameLabel
->setFont(font
);
458 m_nameLabel
->setAlignment(Qt::AlignHCenter
);
459 m_nameLabel
->setWordWrap(true);
460 m_nameLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
463 m_preview
= new PixmapViewer(this);
464 m_preview
->setMinimumWidth(KIconLoader::SizeEnormous
+ KIconLoader::SizeMedium
);
465 m_preview
->setMinimumHeight(KIconLoader::SizeEnormous
);
467 if (MetaDataWidget::metaDataAvailable()) {
468 // rating, comment and tags
469 m_metaDataWidget
= new MetaDataWidget(this);
470 m_metaDataWidget
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
473 // general meta text information
474 m_metaTextLabel
= new MetaTextLabel(this);
475 m_metaTextLabel
->setMinimumWidth(spacing
);
476 m_metaTextLabel
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
478 layout
->addWidget(m_nameLabel
);
479 layout
->addWidget(new KSeparator(this));
480 layout
->addWidget(m_preview
);
481 layout
->addWidget(new KSeparator(this));
482 if (m_metaDataWidget
!= 0) {
483 layout
->addWidget(m_metaDataWidget
);
484 layout
->addWidget(new KSeparator(this));
486 layout
->addWidget(m_metaTextLabel
);
488 // ensure that widgets in the information side bar are aligned towards the top
489 layout
->addStretch(1);
492 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
493 QDBusConnection::sessionBus(), this);
494 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
495 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
496 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
497 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
498 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
499 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
501 m_initialized
= true;
504 #include "infosidebarpage.moc"