]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
Improved KMetaData integration. The Dolphin info sidebar now uses KMetaData to allow...
[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 <config-kmetadata.h>
21
22 #include "infosidebarpage.h"
23 #include <assert.h>
24
25 #include <qlayout.h>
26 #include <qpixmap.h>
27 #include <qlabel.h>
28 #include <qtimer.h>
29 #include <qpushbutton.h>
30
31 #include <q3popupmenu.h>
32 #include <qpainter.h>
33 #include <qfontmetrics.h>
34 #include <Q3ValueList>
35 #include <QEvent>
36 #include <QInputDialog>
37
38 #include <kbookmarkmanager.h>
39 #include <klocale.h>
40 #include <kstandarddirs.h>
41 #include <kio/previewjob.h>
42 #include <kfileitem.h>
43 #include <kdialog.h>
44 #include <kglobalsettings.h>
45 #include <kfilemetainfo.h>
46 #include <kvbox.h>
47 #include <kratingwidget.h>
48 #include <kseparator.h>
49
50 #include "dolphinmainwindow.h"
51 #include "dolphinapplication.h"
52 #include "pixmapviewer.h"
53 #include "dolphinsettings.h"
54 #include "metadatawidget.h"
55
56 InfoSidebarPage::InfoSidebarPage(DolphinMainWindow* mainWindow, QWidget* parent) :
57 SidebarPage(mainWindow, parent),
58 m_multipleSelection(false),
59 m_pendingPreview(false),
60 m_timer(0),
61 m_preview(0),
62 m_name(0),
63 m_infos(0)
64 {
65 const int spacing = KDialog::spacingHint();
66
67 m_timer = new QTimer(this);
68 connect(m_timer, SIGNAL(timeout()),
69 this, SLOT(slotTimeout()));
70
71 QVBoxLayout* layout = new QVBoxLayout;
72 layout->setSpacing(spacing);
73
74 // preview
75 m_preview = new PixmapViewer(this);
76 m_preview->setMinimumWidth(K3Icon::SizeEnormous);
77 m_preview->setFixedHeight(K3Icon::SizeEnormous);
78
79 // name
80 m_name = new QLabel(this);
81 m_name->setTextFormat(Qt::RichText);
82 m_name->setAlignment(m_name->alignment() | Qt::AlignHCenter);
83 QFontMetrics fontMetrics(m_name->font());
84 m_name->setMinimumHeight(fontMetrics.height() * 3);
85 m_name->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
86
87 KSeparator* sep1 = new KSeparator(this);
88
89 // general information
90 m_infos = new QLabel(this);
91 m_infos->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
92 m_infos->setTextFormat(Qt::RichText);
93
94 KSeparator* sep2 = new KSeparator(this);
95
96 if ( MetaDataWidget::metaDataAvailable() )
97 m_metadataWidget = new MetaDataWidget( this );
98 else
99 m_metadataWidget = 0;
100
101 // actions
102 m_actionBox = new KVBox(this);
103 m_actionBox->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
104
105 // Add a dummy widget with no restriction regarding a vertical resizing.
106 // This assures that information is always top aligned.
107 QWidget* dummy = new QWidget(this);
108
109 layout->addItem(new QSpacerItem(spacing, spacing, QSizePolicy::Preferred, QSizePolicy::Fixed));
110 layout->addWidget(m_preview);
111 layout->addWidget(m_name);
112 layout->addWidget(sep1);
113 layout->addWidget(m_infos);
114 layout->addWidget(sep2);
115 if ( m_metadataWidget ) {
116 layout->addWidget( m_metadataWidget );
117 layout->addWidget( new KSeparator( this ) );
118 }
119 layout->addWidget(m_actionBox);
120 layout->addWidget(dummy);
121 setLayout(layout);
122 connect(mainWindow, SIGNAL(selectionChanged()),
123 this, SLOT(showItemInfo()));
124
125 connectToActiveView();
126 }
127
128 InfoSidebarPage::~InfoSidebarPage()
129 {
130 }
131
132 void InfoSidebarPage::activeViewChanged()
133 {
134 connectToActiveView();
135 }
136
137 void InfoSidebarPage::requestDelayedItemInfo(const KUrl& url)
138 {
139 cancelRequest();
140
141 if (!url.isEmpty() && !m_multipleSelection) {
142 m_urlCandidate = url;
143 m_timer->setSingleShot(true);
144 m_timer->start(300);
145 }
146 }
147
148 void InfoSidebarPage::requestItemInfo(const KUrl& url)
149 {
150 cancelRequest();
151
152 if (!url.isEmpty() && !m_multipleSelection) {
153 m_shownUrl = url;
154 showItemInfo();
155 }
156 }
157
158 void InfoSidebarPage::showItemInfo()
159 {
160 cancelRequest();
161
162 m_multipleSelection = false;
163
164 // show the preview...
165 DolphinView* view = mainWindow()->activeView();
166 const KFileItemList selectedItems = view->selectedItems();
167 KUrl file;
168 if (selectedItems.count() > 1) {
169 m_multipleSelection = true;
170 } else if(selectedItems.count() == 0) {
171 file = m_shownUrl;
172 } else {
173 file = selectedItems[0]->url();
174 }
175 if (m_multipleSelection) {
176 KIconLoader iconLoader;
177 QPixmap icon = iconLoader.loadIcon("exec",
178 K3Icon::NoGroup,
179 K3Icon::SizeEnormous);
180 m_preview->setPixmap(icon);
181 m_name->setText(i18n("%1 items selected",selectedItems.count()));
182 }
183 else if (!applyBookmark(file)) {
184 // try to get a preview pixmap from the item...
185 KUrl::List list;
186 list.append(file);
187
188 m_pendingPreview = true;
189 m_preview->setPixmap(QPixmap());
190
191 KIO::PreviewJob* job = KIO::filePreview(list,
192 m_preview->width(),
193 K3Icon::SizeEnormous,
194 0,
195 0,
196 true,
197 false);
198 job->setIgnoreMaximumSize(true);
199
200 connect(job, SIGNAL(gotPreview(const KFileItem*, const QPixmap&)),
201 this, SLOT(gotPreview(const KFileItem*, const QPixmap&)));
202 connect(job, SIGNAL(failed(const KFileItem*)),
203 this, SLOT(slotPreviewFailed(const KFileItem*)));
204
205 QString text("<b>");
206 text.append(file.fileName());
207 text.append("</b>");
208 m_name->setText(text);
209 }
210
211 createMetaInfo();
212 insertActions();
213 }
214
215 void InfoSidebarPage::slotTimeout()
216 {
217 m_shownUrl = m_urlCandidate;
218 showItemInfo();
219 }
220
221 void InfoSidebarPage::slotPreviewFailed(const KFileItem* item)
222 {
223 m_pendingPreview = false;
224 if (!applyBookmark(item->url())) {
225 m_preview->setPixmap(item->pixmap(K3Icon::SizeEnormous));
226 }
227 }
228
229 void InfoSidebarPage::gotPreview(const KFileItem* /* item */,
230 const QPixmap& pixmap)
231 {
232 if (m_pendingPreview) {
233 m_preview->setPixmap(pixmap);
234 m_pendingPreview = false;
235 }
236 }
237
238 void InfoSidebarPage::startService(int index)
239 {
240 DolphinView* view = mainWindow()->activeView();
241 if (view->hasSelection()) {
242 KUrl::List selectedUrls = view->selectedUrls();
243 KDEDesktopMimeType::executeService(selectedUrls, m_actionsVector[index]);
244 }
245 else {
246 KDEDesktopMimeType::executeService(m_shownUrl, m_actionsVector[index]);
247 }
248 }
249
250 void InfoSidebarPage::connectToActiveView()
251 {
252 cancelRequest();
253
254 DolphinView* view = mainWindow()->activeView();
255 connect(view, SIGNAL(requestItemInfo(const KUrl&)),
256 this, SLOT(requestDelayedItemInfo(const KUrl&)));
257 connect(view, SIGNAL(urlChanged(const KUrl&)),
258 this, SLOT(requestItemInfo(const KUrl&)));
259
260 m_shownUrl = view->url();
261 showItemInfo();
262 }
263
264 bool InfoSidebarPage::applyBookmark(const KUrl& url)
265 {
266 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
267 KBookmark bookmark = root.first();
268 while (!bookmark.isNull()) {
269 if (url.equals(bookmark.url(), KUrl::CompareWithoutTrailingSlash)) {
270 QString text("<b>");
271 text.append(bookmark.text());
272 text.append("</b>");
273 m_name->setText(text);
274
275 KIconLoader iconLoader;
276 QPixmap icon = iconLoader.loadIcon(bookmark.icon(),
277 K3Icon::NoGroup,
278 K3Icon::SizeEnormous);
279 m_preview->setPixmap(icon);
280 return true;
281 }
282 bookmark = root.next(bookmark);
283 }
284
285 return false;
286 }
287
288 void InfoSidebarPage::cancelRequest()
289 {
290 m_timer->stop();
291 m_pendingPreview = false;
292 }
293
294 void InfoSidebarPage::createMetaInfo()
295 {
296 beginInfoLines();
297 DolphinView* view = mainWindow()->activeView();
298 if (!view->hasSelection()) {
299 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
300 fileItem.refresh();
301
302 if (fileItem.isDir()) {
303 addInfoLine(i18n("Type:"), i18n("Directory"));
304 }
305 if ( MetaDataWidget::metaDataAvailable() )
306 m_metadataWidget->setFile( fileItem.url() );
307 }
308 else if (view->selectedItems().count() == 1) {
309 KFileItem* fileItem = view->selectedItems()[0];
310 addInfoLine(i18n("Type:"), fileItem->mimeComment());
311
312 QString sizeText(KIO::convertSize(fileItem->size()));
313 addInfoLine(i18n("Size:"), sizeText);
314 addInfoLine(i18n("Modified:"), fileItem->timeString());
315
316 const KFileMetaInfo& metaInfo = fileItem->metaInfo();
317 if (metaInfo.isValid()) {
318 QStringList keys = metaInfo.supportedKeys();
319 for (QStringList::Iterator it = keys.begin(); it != keys.end(); ++it) {
320 if (showMetaInfo(*it)) {
321 KFileMetaInfoItem metaInfoItem = metaInfo.item(*it);
322 addInfoLine(*it, metaInfoItem.value().toString());
323 }
324 }
325 }
326 if ( MetaDataWidget::metaDataAvailable() )
327 m_metadataWidget->setFile( fileItem->url() );
328 }
329 else {
330 m_metadataWidget->setFiles( view->selectedItems().urlList() );
331 unsigned long int totSize = 0;
332 foreach(KFileItem* item, view->selectedItems()) {
333 totSize += item->size(); //FIXME what to do with directories ? (same with the one-item-selected-code), item->size() does not return the size of the content : not very instinctive for users
334 }
335 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize));
336 }
337 endInfoLines();
338 }
339
340 void InfoSidebarPage::beginInfoLines()
341 {
342 m_infoLines = QString("");
343 }
344
345 void InfoSidebarPage::endInfoLines()
346 {
347 m_infos->setText(m_infoLines);
348 }
349
350 bool InfoSidebarPage::showMetaInfo(const QString& key) const
351 {
352 // sorted list of keys, where it's data should be shown
353 static const char* keys[] = {
354 "Album",
355 "Artist",
356 "Author",
357 "Bitrate",
358 "Date",
359 "Dimensions",
360 "Genre",
361 "Length",
362 "Lines",
363 "Pages",
364 "Title",
365 "Words"
366 };
367
368 // do a binary search for the key...
369 int top = 0;
370 int bottom = sizeof(keys) / sizeof(char*) - 1;
371 while (top < bottom) {
372 const int middle = (top + bottom) / 2;
373 const int result = key.compare(keys[middle]);
374 if (result < 0) {
375 bottom = middle - 1;
376 }
377 else if (result > 0) {
378 top = middle + 1;
379 }
380 else {
381 return true;
382 }
383 }
384
385 return false;
386 }
387
388 void InfoSidebarPage::addInfoLine(const QString& labelText, const QString& infoText)
389 {
390 if (!m_infoLines.isEmpty())
391 m_infoLines += "<br/>";
392 m_infoLines += QString("<b>%1</b> %2").arg(labelText).arg(infoText);
393 }
394
395 void InfoSidebarPage::insertActions()
396 {
397 // delete all existing action widgets
398 // TODO: just use children() from QObject...
399 Q3PtrListIterator<QWidget> deleteIter(m_actionWidgets);
400 QWidget* widget = 0;
401 while ((widget = deleteIter.current()) != 0) {
402 widget->close();
403 widget->deleteLater();
404 ++deleteIter;
405 }
406
407 m_actionWidgets.clear();
408 m_actionsVector.clear();
409
410 int actionsIndex = 0;
411
412 // The algorithm for searching the available actions works on a list
413 // of KFileItems. If no selection is given, a temporary KFileItem
414 // by the given Url 'url' is created and added to the list.
415 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
416 KFileItemList itemList = mainWindow()->activeView()->selectedItems();
417 if (itemList.isEmpty()) {
418 fileItem.refresh();
419 itemList.append(&fileItem);
420 }
421
422 // 'itemList' contains now all KFileItems, where an item information should be shown.
423 // TODO: the following algorithm is quite equal to DolphinContextMenu::insertActionItems().
424 // It's open yet whether they should be merged or whether they have to work slightly different.
425 QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
426 for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) {
427 QDir dir(*dirIt);
428 QStringList entries = dir.entryList(QStringList("*.desktop"), QDir::Files);
429
430 for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) {
431 KConfigGroup cfg(KSharedConfig::openConfig( *dirIt + *entryIt, KConfig::OnlyLocal ), "Desktop Entry" );
432 if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) {
433 const QStringList types = cfg.readEntry("ServiceTypes", QStringList(), ',');
434 for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) {
435 // check whether the mime type is equal or whether the
436 // mimegroup (e. g. image/*) is supported
437
438 bool insert = false;
439 if ((*it) == "all/allfiles") {
440 // The service type is valid for all files, but not for directories.
441 // Check whether the selected items only consist of files...
442 QListIterator<KFileItem*> mimeIt(itemList);
443 insert = true;
444 while (insert && mimeIt.hasNext()) {
445 KFileItem* item = mimeIt.next();
446 insert = !item->isDir();
447 }
448 }
449
450 if (!insert) {
451 // Check whether the MIME types of all selected files match
452 // to the mimetype of the service action. As soon as one MIME
453 // type does not match, no service menu is shown at all.
454 QListIterator<KFileItem*> mimeIt(itemList);
455 insert = true;
456 while (insert && mimeIt.hasNext()) {
457 KFileItem* item = mimeIt.next();
458 const QString mimeType(item->mimetype());
459 const QString mimeGroup(mimeType.left(mimeType.indexOf('/')));
460
461 insert = (*it == mimeType) ||
462 ((*it).right(1) == "*") &&
463 ((*it).left((*it).indexOf('/')) == mimeGroup);
464 }
465 }
466
467 if (insert) {
468 const QString submenuName = cfg.readEntry( "X-KDE-Submenu" );
469 Q3PopupMenu* popup = 0;
470 if (!submenuName.isEmpty()) {
471 // create a sub menu containing all actions
472 popup = new Q3PopupMenu();
473 connect(popup, SIGNAL(activated(int)),
474 this, SLOT(startService(int)));
475
476 QPushButton* button = new QPushButton(submenuName, m_actionBox);
477 button->setFlat(true);
478 button->setMenu(popup);
479 button->show();
480 m_actionWidgets.append(button);
481 }
482
483 Q3ValueList<KDEDesktopMimeType::Service> userServices =
484 KDEDesktopMimeType::userDefinedServices(*dirIt + *entryIt, true);
485
486 // iterate through all actions and add them to a widget
487 Q3ValueList<KDEDesktopMimeType::Service>::Iterator serviceIt;
488 for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) {
489 KDEDesktopMimeType::Service service = (*serviceIt);
490 if (popup == 0) {
491 ServiceButton* button = new ServiceButton(KIcon(service.m_strIcon),
492 service.m_strName,
493 m_actionBox,
494 actionsIndex);
495 connect(button, SIGNAL(requestServiceStart(int)),
496 this, SLOT(startService(int)));
497 m_actionWidgets.append(button);
498 button->show();
499 }
500 else {
501 popup->insertItem(KIcon(service.m_strIcon), service.m_strName, actionsIndex);
502 }
503
504 m_actionsVector.append(service);
505 ++actionsIndex;
506 }
507 }
508 }
509 }
510 }
511 }
512 }
513
514
515
516 ServiceButton::ServiceButton(const QIcon& icon,
517 const QString& text,
518 QWidget* parent,
519 int index) :
520 QPushButton(icon, text, parent),
521 m_hover(false),
522 m_index(index)
523 {
524 setEraseColor(palette().brush(QPalette::Background).color());
525 setFocusPolicy(Qt::NoFocus);
526 connect(this, SIGNAL(released()),
527 this, SLOT(slotReleased()));
528 }
529
530 ServiceButton::~ServiceButton()
531 {
532 }
533
534 void ServiceButton::paintEvent(QPaintEvent* event)
535 {
536 Q_UNUSED(event);
537 QPainter painter(this);
538 const int buttonWidth = width();
539 const int buttonHeight = height();
540
541 QColor backgroundColor;
542 QColor foregroundColor;
543 if (m_hover) {
544 backgroundColor = KGlobalSettings::highlightColor();
545 foregroundColor = KGlobalSettings::highlightedTextColor();
546 }
547 else {
548 backgroundColor = palette().brush(QPalette::Background).color();
549 foregroundColor = KGlobalSettings::buttonTextColor();
550 }
551
552 // draw button background
553 painter.setPen(Qt::NoPen);
554 painter.setBrush(backgroundColor);
555 painter.drawRect(0, 0, buttonWidth, buttonHeight);
556
557 const int spacing = KDialog::spacingHint();
558
559 // draw icon
560 int x = spacing;
561 const int y = (buttonHeight - K3Icon::SizeSmall) / 2;
562 const QIcon &set = icon();
563 if (!set.isNull()) {
564 painter.drawPixmap(x, y, set.pixmap(QIcon::Small, QIcon::Normal));
565 }
566 x += K3Icon::SizeSmall + spacing;
567
568 // draw text
569 painter.setPen(foregroundColor);
570
571 const int textWidth = buttonWidth - x;
572 QFontMetrics fontMetrics(font());
573 const bool clipped = fontMetrics.width(text()) >= textWidth;
574 //const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter;
575 painter.drawText(QRect(x, 0, textWidth, buttonHeight), Qt::AlignVCenter, text());
576
577 if (clipped) {
578 // Blend the right area of the text with the background, as the
579 // text is clipped.
580 // TODO #1: use alpha blending in Qt4 instead of drawing the text that often
581 // TODO #2: same code as in UrlNavigatorButton::drawButton() -> provide helper class?
582 const int blendSteps = 16;
583
584 QColor blendColor(backgroundColor);
585 const int redInc = (foregroundColor.red() - backgroundColor.red()) / blendSteps;
586 const int greenInc = (foregroundColor.green() - backgroundColor.green()) / blendSteps;
587 const int blueInc = (foregroundColor.blue() - backgroundColor.blue()) / blendSteps;
588 for (int i = 0; i < blendSteps; ++i) {
589 painter.setClipRect(QRect(x + textWidth - i, 0, 1, buttonHeight));
590 painter.setPen(blendColor);
591 painter.drawText(QRect(x, 0, textWidth, buttonHeight), Qt::AlignVCenter, text());
592
593 blendColor.setRgb(blendColor.red() + redInc,
594 blendColor.green() + greenInc,
595 blendColor.blue() + blueInc);
596 }
597 }
598 }
599
600 void ServiceButton::enterEvent(QEvent* event)
601 {
602 QPushButton::enterEvent(event);
603 m_hover = true;
604 update();
605 }
606
607 void ServiceButton::leaveEvent(QEvent* event)
608 {
609 QPushButton::leaveEvent(event);
610 m_hover = false;
611 update();
612 }
613
614 void ServiceButton::slotReleased()
615 {
616 emit requestServiceStart(m_index);
617 }
618
619 #include "infosidebarpage.moc"