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 <config-kmetadata.h>
22 #include "infosidebarpage.h"
28 #include <QPushButton>
31 #include <QFontMetrics>
33 #include <QInputDialog>
36 #include <kbookmarkmanager.h>
38 #include <kstandarddirs.h>
39 #include <kio/previewjob.h>
40 #include <kfileitem.h>
42 #include <kglobalsettings.h>
43 #include <kfilemetainfo.h>
45 #include <kseparator.h>
46 #include <kiconloader.h>
49 #include <kratingwidget.h>
52 #include "pixmapviewer.h"
53 #include "dolphinsettings.h"
54 #include "metadatawidget.h"
56 InfoSidebarPage::InfoSidebarPage(QWidget
* parent
) :
58 m_multipleSelection(false), //TODO:check if I'm needed
59 m_pendingPreview(false),
61 m_currentSelection(KFileItemList()),
66 const int spacing
= KDialog::spacingHint();
68 m_timer
= new QTimer(this);
69 connect(m_timer
, SIGNAL(timeout()),
70 this, SLOT(slotTimeout()));
72 QVBoxLayout
* layout
= new QVBoxLayout
;
73 layout
->setSpacing(spacing
);
76 m_preview
= new PixmapViewer(this);
77 m_preview
->setMinimumWidth(K3Icon::SizeEnormous
);
78 m_preview
->setFixedHeight(K3Icon::SizeEnormous
);
81 m_name
= new QLabel(this);
82 m_name
->setTextFormat(Qt::RichText
);
83 m_name
->setAlignment(m_name
->alignment() | Qt::AlignHCenter
);
84 QFontMetrics
fontMetrics(m_name
->font());
85 m_name
->setMinimumHeight(fontMetrics
.height() * 3);
86 m_name
->setSizePolicy(QSizePolicy::MinimumExpanding
, QSizePolicy::Maximum
);
88 KSeparator
* sep1
= new KSeparator(this);
90 // general information
91 m_infos
= new QLabel(this);
92 m_infos
->setSizePolicy(QSizePolicy::Minimum
, QSizePolicy::Fixed
);
93 m_infos
->setTextFormat(Qt::RichText
);
95 KSeparator
* sep2
= new KSeparator(this);
97 if ( MetaDataWidget::metaDataAvailable() )
98 m_metadataWidget
= new MetaDataWidget( this );
100 m_metadataWidget
= 0;
103 m_actionBox
= new KVBox(this);
104 m_actionBox
->setSizePolicy(QSizePolicy::Preferred
, QSizePolicy::Fixed
);
106 // Add a dummy widget with no restriction regarding a vertical resizing.
107 // This assures that information is always top aligned.
108 QWidget
* dummy
= new QWidget(this);
110 layout
->addItem(new QSpacerItem(spacing
, spacing
, QSizePolicy::Preferred
, QSizePolicy::Fixed
));
111 layout
->addWidget(m_preview
);
112 layout
->addWidget(m_name
);
113 layout
->addWidget(sep1
);
114 layout
->addWidget(m_infos
);
115 layout
->addWidget(sep2
);
116 if ( m_metadataWidget
) {
117 layout
->addWidget( m_metadataWidget
);
118 layout
->addWidget( new KSeparator( this ) );
120 layout
->addWidget(m_actionBox
);
121 layout
->addWidget(dummy
);
125 InfoSidebarPage::~InfoSidebarPage()
129 void InfoSidebarPage::setUrl(const KUrl
& url
)
131 if (!m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
138 void InfoSidebarPage::setSelection(const KFileItemList
& selection
)
141 m_currentSelection
= selection
;
142 m_multipleSelection
= (m_currentSelection
.size() > 1);
146 void InfoSidebarPage::requestDelayedItemInfo(const KUrl
& url
)
150 if (!url
.isEmpty() && !m_multipleSelection
) {
151 m_urlCandidate
= url
;
152 m_timer
->setSingleShot(true);
157 void InfoSidebarPage::showItemInfo()
161 KFileItemList selectedItems
= m_currentSelection
;
163 if(selectedItems
.count() == 0) {
166 file
= selectedItems
[0]->url();
168 if (m_multipleSelection
) {
169 KIconLoader iconLoader
;
170 QPixmap icon
= iconLoader
.loadIcon("exec",
172 K3Icon::SizeEnormous
);
173 m_preview
->setPixmap(icon
);
174 m_name
->setText(i18n("%1 items selected",selectedItems
.count()));
176 else if (!applyBookmark(file
)) {
177 // try to get a preview pixmap from the item...
181 m_pendingPreview
= true;
182 m_preview
->setPixmap(QPixmap());
184 KIO::PreviewJob
* job
= KIO::filePreview(list
,
186 K3Icon::SizeEnormous
,
191 job
->setIgnoreMaximumSize(true);
193 connect(job
, SIGNAL(gotPreview(const KFileItem
*, const QPixmap
&)),
194 this, SLOT(gotPreview(const KFileItem
*, const QPixmap
&)));
195 connect(job
, SIGNAL(failed(const KFileItem
*)),
196 this, SLOT(slotPreviewFailed(const KFileItem
*)));
199 text
.append(file
.fileName());
201 m_name
->setText(text
);
208 void InfoSidebarPage::slotTimeout()
210 m_shownUrl
= m_urlCandidate
;
214 void InfoSidebarPage::slotPreviewFailed(const KFileItem
* item
)
216 m_pendingPreview
= false;
217 if (!applyBookmark(item
->url())) {
218 m_preview
->setPixmap(item
->pixmap(K3Icon::SizeEnormous
));
222 void InfoSidebarPage::gotPreview(const KFileItem
* item
,
223 const QPixmap
& pixmap
)
226 if (m_pendingPreview
) {
227 m_preview
->setPixmap(pixmap
);
228 m_pendingPreview
= false;
232 void InfoSidebarPage::startService(int index
)
234 if (m_currentSelection
.count() > 0) {
235 // TODO: Use "at()" as soon as executeService is fixed to take a const param (BIC)
236 KDEDesktopMimeType::executeService(m_currentSelection
.urlList(), m_actionsVector
[index
]);
240 KDEDesktopMimeType::executeService(m_shownUrl
, m_actionsVector
[index
]);
244 bool InfoSidebarPage::applyBookmark(const KUrl
& url
)
246 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
247 KBookmark bookmark
= root
.first();
248 while (!bookmark
.isNull()) {
249 if (url
.equals(bookmark
.url(), KUrl::CompareWithoutTrailingSlash
)) {
251 text
.append(bookmark
.text());
253 m_name
->setText(text
);
255 KIconLoader iconLoader
;
256 QPixmap icon
= iconLoader
.loadIcon(bookmark
.icon(),
258 K3Icon::SizeEnormous
);
259 m_preview
->setPixmap(icon
);
262 bookmark
= root
.next(bookmark
);
268 void InfoSidebarPage::cancelRequest()
271 m_pendingPreview
= false;
274 void InfoSidebarPage::createMetaInfo()
277 if(m_currentSelection
.size() == 0) {
278 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
281 if (fileItem
.isDir()) {
282 addInfoLine(i18n("Type:"), i18n("Directory"));
284 if ( MetaDataWidget::metaDataAvailable() )
285 m_metadataWidget
->setFile( fileItem
.url() );
287 else if (m_currentSelection
.count() == 1) {
288 KFileItem
* fileItem
= m_currentSelection
.at(0);
289 addInfoLine(i18n("Type:"), fileItem
->mimeComment());
291 QString
sizeText(KIO::convertSize(fileItem
->size()));
292 addInfoLine(i18n("Size:"), sizeText
);
293 addInfoLine(i18n("Modified:"), fileItem
->timeString());
295 const KFileMetaInfo
& metaInfo
= fileItem
->metaInfo();
296 if (metaInfo
.isValid()) {
297 QStringList keys
= metaInfo
.supportedKeys();
298 for (QStringList::Iterator it
= keys
.begin(); it
!= keys
.end(); ++it
) {
299 if (showMetaInfo(*it
)) {
300 KFileMetaInfoItem metaInfoItem
= metaInfo
.item(*it
);
301 addInfoLine(*it
, metaInfoItem
.value().toString());
305 if ( MetaDataWidget::metaDataAvailable() )
306 m_metadataWidget
->setFile( fileItem
->url() );
309 if ( MetaDataWidget::metaDataAvailable() )
310 m_metadataWidget
->setFiles( m_currentSelection
.urlList() );
311 unsigned long int totSize
= 0;
312 foreach(KFileItem
* item
, m_currentSelection
) {
313 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
315 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize
));
320 void InfoSidebarPage::beginInfoLines()
322 m_infoLines
= QString("");
325 void InfoSidebarPage::endInfoLines()
327 m_infos
->setText(m_infoLines
);
330 bool InfoSidebarPage::showMetaInfo(const QString
& key
) const
332 // sorted list of keys, where it's data should be shown
333 static const char* keys
[] = {
348 // do a binary search for the key...
350 int bottom
= sizeof(keys
) / sizeof(char*) - 1;
351 while (top
< bottom
) {
352 const int middle
= (top
+ bottom
) / 2;
353 const int result
= key
.compare(keys
[middle
]);
357 else if (result
> 0) {
368 void InfoSidebarPage::addInfoLine(const QString
& labelText
, const QString
& infoText
)
370 if (!m_infoLines
.isEmpty())
371 m_infoLines
+= "<br/>";
372 m_infoLines
+= QString("<b>%1</b> %2").arg(labelText
).arg(infoText
);
375 void InfoSidebarPage::insertActions()
377 QListIterator
<QPushButton
*> deleteIter(m_actionBox
->findChildren
<QPushButton
*>());
379 while (deleteIter
.hasNext()) {
380 widget
= deleteIter
.next();
382 widget
->deleteLater();
385 m_actionsVector
.clear();
387 int actionsIndex
= 0;
389 // The algorithm for searching the available actions works on a list
390 // of KFileItems. If no selection is given, a temporary KFileItem
391 // by the given Url 'url' is created and added to the list.
392 KFileItem
fileItem(S_IFDIR
, KFileItem::Unknown
, m_shownUrl
);
393 KFileItemList itemList
= m_currentSelection
;
394 if (itemList
.isEmpty()) {
396 itemList
.append(&fileItem
);
399 // 'itemList' contains now all KFileItems, where an item information should be shown.
400 // TODO: the following algorithm is quite equal to DolphinContextMenu::insertActionItems().
401 // It's open yet whether they should be merged or whether they have to work slightly different.
402 QStringList dirs
= KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/");
403 for (QStringList::ConstIterator dirIt
= dirs
.begin(); dirIt
!= dirs
.end(); ++dirIt
) {
405 QStringList entries
= dir
.entryList(QStringList("*.desktop"), QDir::Files
);
407 for (QStringList::ConstIterator entryIt
= entries
.begin(); entryIt
!= entries
.end(); ++entryIt
) {
408 KConfigGroup
cfg(KSharedConfig::openConfig( *dirIt
+ *entryIt
, KConfig::OnlyLocal
), "Desktop Entry" );
409 if ((cfg
.hasKey("Actions") || cfg
.hasKey("X-KDE-GetActionMenu")) && cfg
.hasKey("ServiceTypes")) {
410 const QStringList types
= cfg
.readEntry("ServiceTypes", QStringList(), ',');
411 for (QStringList::ConstIterator it
= types
.begin(); it
!= types
.end(); ++it
) {
412 // check whether the mime type is equal or whether the
413 // mimegroup (e. g. image/*) is supported
416 if ((*it
) == "all/allfiles") {
417 // The service type is valid for all files, but not for directories.
418 // Check whether the selected items only consist of files...
419 QListIterator
<KFileItem
*> mimeIt(itemList
);
421 while (insert
&& mimeIt
.hasNext()) {
422 KFileItem
* item
= mimeIt
.next();
423 insert
= !item
->isDir();
428 // Check whether the MIME types of all selected files match
429 // to the mimetype of the service action. As soon as one MIME
430 // type does not match, no service menu is shown at all.
431 QListIterator
<KFileItem
*> mimeIt(itemList
);
433 while (insert
&& mimeIt
.hasNext()) {
434 KFileItem
* item
= mimeIt
.next();
435 const QString
mimeType(item
->mimetype());
436 const QString
mimeGroup(mimeType
.left(mimeType
.indexOf('/')));
438 insert
= (*it
== mimeType
) ||
439 ((*it
).right(1) == "*") &&
440 ((*it
).left((*it
).indexOf('/')) == mimeGroup
);
445 const QString submenuName
= cfg
.readEntry( "X-KDE-Submenu" );
447 if (!submenuName
.isEmpty()) {
448 // create a sub menu containing all actions
450 connect(popup
, SIGNAL(activated(int)),
451 this, SLOT(startService(int)));
453 QPushButton
* button
= new QPushButton(submenuName
, m_actionBox
);
454 button
->setFlat(true);
455 button
->setMenu(popup
);
459 QList
<KDEDesktopMimeType::Service
> userServices
=
460 KDEDesktopMimeType::userDefinedServices(*dirIt
+ *entryIt
, true);
462 // iterate through all actions and add them to a widget
463 QList
<KDEDesktopMimeType::Service
>::Iterator serviceIt
;
464 for (serviceIt
= userServices
.begin(); serviceIt
!= userServices
.end(); ++serviceIt
) {
465 KDEDesktopMimeType::Service service
= (*serviceIt
);
467 ServiceButton
* button
= new ServiceButton(KIcon(service
.m_strIcon
),
471 connect(button
, SIGNAL(requestServiceStart(int)),
472 this, SLOT(startService(int)));
476 popup
->insertItem(KIcon(service
.m_strIcon
), service
.m_strName
, actionsIndex
);
479 m_actionsVector
.append(service
);
491 ServiceButton::ServiceButton(const QIcon
& icon
,
495 QPushButton(icon
, text
, parent
),
499 setEraseColor(palette().brush(QPalette::Background
).color());
500 setFocusPolicy(Qt::NoFocus
);
501 connect(this, SIGNAL(released()),
502 this, SLOT(slotReleased()));
505 ServiceButton::~ServiceButton()
509 void ServiceButton::paintEvent(QPaintEvent
* event
)
512 QPainter
painter(this);
513 const int buttonWidth
= width();
514 const int buttonHeight
= height();
516 QColor backgroundColor
;
517 QColor foregroundColor
;
519 backgroundColor
= KGlobalSettings::highlightColor();
520 foregroundColor
= KGlobalSettings::highlightedTextColor();
523 backgroundColor
= palette().brush(QPalette::Background
).color();
524 foregroundColor
= KGlobalSettings::buttonTextColor();
527 // draw button background
528 painter
.setPen(Qt::NoPen
);
529 painter
.setBrush(backgroundColor
);
530 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
532 const int spacing
= KDialog::spacingHint();
536 const int y
= (buttonHeight
- K3Icon::SizeSmall
) / 2;
537 const QIcon
&set
= icon();
539 painter
.drawPixmap(x
, y
, set
.pixmap(QIcon::Small
, QIcon::Normal
));
541 x
+= K3Icon::SizeSmall
+ spacing
;
544 painter
.setPen(foregroundColor
);
546 const int textWidth
= buttonWidth
- x
;
547 QFontMetrics
fontMetrics(font());
548 const bool clipped
= fontMetrics
.width(text()) >= textWidth
;
549 //const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter;
550 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
553 // Blend the right area of the text with the background, as the
555 // TODO #1: use alpha blending in Qt4 instead of drawing the text that often
556 // TODO #2: same code as in UrlNavigatorButton::drawButton() -> provide helper class?
557 const int blendSteps
= 16;
559 QColor
blendColor(backgroundColor
);
560 const int redInc
= (foregroundColor
.red() - backgroundColor
.red()) / blendSteps
;
561 const int greenInc
= (foregroundColor
.green() - backgroundColor
.green()) / blendSteps
;
562 const int blueInc
= (foregroundColor
.blue() - backgroundColor
.blue()) / blendSteps
;
563 for (int i
= 0; i
< blendSteps
; ++i
) {
564 painter
.setClipRect(QRect(x
+ textWidth
- i
, 0, 1, buttonHeight
));
565 painter
.setPen(blendColor
);
566 painter
.drawText(QRect(x
, 0, textWidth
, buttonHeight
), Qt::AlignVCenter
, text());
568 blendColor
.setRgb(blendColor
.red() + redInc
,
569 blendColor
.green() + greenInc
,
570 blendColor
.blue() + blueInc
);
575 void ServiceButton::enterEvent(QEvent
* event
)
577 QPushButton::enterEvent(event
);
582 void ServiceButton::leaveEvent(QEvent
* event
)
584 QPushButton::leaveEvent(event
);
589 void ServiceButton::slotReleased()
591 emit
requestServiceStart(m_index
);
594 #include "infosidebarpage.moc"