]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
fix crash when e. g. right clicking on a file using SMB inside Konqueror
[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 "infosidebarpage.h"
21
22 #include <config-nepomuk.h>
23
24 #include <kfileplacesmodel.h>
25 #include <klocale.h>
26 #include <kstandarddirs.h>
27 #include <kio/previewjob.h>
28 #include <kfileitem.h>
29 #include <kdialog.h>
30 #include <kglobalsettings.h>
31 #include <kfilemetainfo.h>
32 #include <kiconeffect.h>
33 #include <kseparator.h>
34 #include <kiconloader.h>
35
36 #include <QEvent>
37 #include <QInputDialog>
38 #include <QLabel>
39 #include <QPainter>
40 #include <QPixmap>
41 #include <QResizeEvent>
42 #include <QTimer>
43 #include <QVBoxLayout>
44
45 #include "dolphinsettings.h"
46 #include "metadatawidget.h"
47 #include "metatextlabel.h"
48 #include "pixmapviewer.h"
49
50 #include <kdebug.h>
51
52 InfoSidebarPage::InfoSidebarPage(QWidget* parent) :
53 SidebarPage(parent),
54 m_pendingPreview(false),
55 m_shownUrl(),
56 m_urlCandidate(),
57 m_fileItem(),
58 m_selection(),
59 m_nameLabel(0),
60 m_preview(0),
61 m_metaDataWidget(0),
62 m_metaTextLabel(0)
63 {
64 const int spacing = KDialog::spacingHint();
65
66 m_timer = new QTimer(this);
67 m_timer->setSingleShot(true);
68 connect(m_timer, SIGNAL(timeout()),
69 this, SLOT(slotTimeout()));
70
71 QVBoxLayout* layout = new QVBoxLayout;
72 layout->setSpacing(spacing);
73
74 // name
75 m_nameLabel = new QLabel(this);
76 QFont font = m_nameLabel->font();
77 font.setBold(true);
78 m_nameLabel->setFont(font);
79 m_nameLabel->setAlignment(Qt::AlignHCenter);
80 m_nameLabel->setWordWrap(true);
81 m_nameLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
82
83 // preview
84 m_preview = new PixmapViewer(this);
85 m_preview->setMinimumWidth(KIconLoader::SizeEnormous + KIconLoader::SizeMedium);
86 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
87
88 if (MetaDataWidget::metaDataAvailable()) {
89 // rating, comment and tags
90 m_metaDataWidget = new MetaDataWidget(this);
91 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
92 }
93
94 // general meta text information
95 m_metaTextLabel = new MetaTextLabel(this);
96 m_metaTextLabel->setMinimumWidth(spacing);
97 m_metaTextLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
98
99 layout->addWidget(m_nameLabel);
100 layout->addWidget(new KSeparator(this));
101 layout->addWidget(m_preview);
102 layout->addWidget(new KSeparator(this));
103 if (m_metaDataWidget != 0) {
104 layout->addWidget(m_metaDataWidget);
105 layout->addWidget(new KSeparator(this));
106 }
107 layout->addWidget(m_metaTextLabel);
108
109 // ensure that widgets in the information side bar are aligned towards the top
110 layout->addStretch(1);
111 setLayout(layout);
112 }
113
114 InfoSidebarPage::~InfoSidebarPage()
115 {
116 }
117
118 QSize InfoSidebarPage::sizeHint() const
119 {
120 QSize size = SidebarPage::sizeHint();
121 size.setWidth(minimumSizeHint().width());
122 return size;
123 }
124
125 void InfoSidebarPage::setUrl(const KUrl& url)
126 {
127 SidebarPage::setUrl(url);
128 if (url.isValid() && !m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
129 cancelRequest();
130 m_shownUrl = url;
131 showItemInfo();
132 }
133 }
134
135 void InfoSidebarPage::setSelection(const KFileItemList& selection)
136 {
137 m_selection = selection;
138
139 const int count = selection.count();
140 if (count == 0) {
141 m_shownUrl = url();
142 showItemInfo();
143 } else {
144 if ((count == 1) && !selection.first().url().isEmpty()) {
145 m_urlCandidate = selection.first().url();
146 }
147 m_timer->start(TimerDelay);
148 }
149 }
150
151 void InfoSidebarPage::requestDelayedItemInfo(const KFileItem& item)
152 {
153 cancelRequest();
154
155 m_fileItem = KFileItem();
156 if (item.isNull()) {
157 // The cursor is above the viewport. If files are selected,
158 // show information regarding the selection.
159 if (m_selection.size() > 0) {
160 m_pendingPreview = false;
161 m_timer->start(TimerDelay);
162 }
163 } else if (!item.url().isEmpty()) {
164 m_urlCandidate = item.url();
165 m_fileItem = item;
166 m_timer->start(TimerDelay);
167 }
168 }
169
170 void InfoSidebarPage::showEvent(QShowEvent* event)
171 {
172 SidebarPage::showEvent(event);
173 if (!event->spontaneous()) {
174 showItemInfo();
175 }
176 }
177
178 void InfoSidebarPage::resizeEvent(QResizeEvent* event)
179 {
180 // If the text inside the name label or the info label cannot
181 // get wrapped, then the maximum width of the label is increased
182 // so that the width of the information sidebar gets increased.
183 // To prevent this, the maximum width is adjusted to
184 // the current width of the sidebar.
185 const int maxWidth = event->size().width() - KDialog::spacingHint() * 4;
186 m_nameLabel->setMaximumWidth(maxWidth);
187
188 // try to increase the preview as large as possible
189 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
190 m_urlCandidate = m_shownUrl; // reset the URL candidate if a resizing is done
191 m_timer->start(TimerDelay);
192
193 SidebarPage::resizeEvent(event);
194 }
195
196 void InfoSidebarPage::showItemInfo()
197 {
198 if (!isVisible()) {
199 return;
200 }
201
202 cancelRequest();
203
204 const KUrl file = (!m_fileItem.isNull() || m_selection.isEmpty()) ? m_shownUrl : m_selection[0].url();
205 if (!file.isValid()) {
206 return;
207 }
208
209 const int selectionCount = m_selection.count();
210 if (m_fileItem.isNull() && (selectionCount > 1)) {
211 KIconLoader iconLoader;
212 QPixmap icon = iconLoader.loadIcon("dialog-information",
213 KIconLoader::NoGroup,
214 KIconLoader::SizeEnormous);
215 m_preview->setPixmap(icon);
216 m_nameLabel->setText(i18ncp("@info", "%1 item selected", "%1 items selected", selectionCount));
217 } else if (!applyPlace(file)) {
218 // try to get a preview pixmap from the item...
219 KUrl::List list;
220 list.append(file);
221
222 m_pendingPreview = true;
223
224 KIconEffect iconEffect;
225 QPixmap disabledPixmap = iconEffect.apply(m_preview->pixmap(), KIconLoader::Desktop, KIconLoader::DisabledState);
226 m_preview->setPixmap(disabledPixmap);
227
228 KIO::PreviewJob* job = KIO::filePreview(list,
229 m_preview->width(),
230 m_preview->height(),
231 0,
232 0,
233 true,
234 false);
235 job->setIgnoreMaximumSize(true);
236
237 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
238 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
239 connect(job, SIGNAL(failed(const KFileItem&)),
240 this, SLOT(showIcon(const KFileItem&)));
241
242 m_nameLabel->setText(file.fileName());
243 }
244
245 showMetaInfo();
246 }
247
248 void InfoSidebarPage::slotTimeout()
249 {
250 m_shownUrl = m_urlCandidate;
251 showItemInfo();
252 }
253
254 void InfoSidebarPage::showIcon(const KFileItem& item)
255 {
256 m_pendingPreview = false;
257 if (!applyPlace(item.url())) {
258 m_preview->setPixmap(item.pixmap(KIconLoader::SizeEnormous));
259 }
260 }
261
262 void InfoSidebarPage::showPreview(const KFileItem& item,
263 const QPixmap& pixmap)
264 {
265 Q_UNUSED(item);
266 if (m_pendingPreview) {
267 m_preview->setPixmap(pixmap);
268 m_pendingPreview = false;
269 }
270 }
271
272 bool InfoSidebarPage::applyPlace(const KUrl& url)
273 {
274 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
275 int count = placesModel->rowCount();
276
277 for (int i = 0; i < count; ++i) {
278 QModelIndex index = placesModel->index(i, 0);
279
280 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
281 m_nameLabel->setText(placesModel->text(index));
282 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
283 return true;
284 }
285 }
286
287 return false;
288 }
289
290 void InfoSidebarPage::cancelRequest()
291 {
292 m_timer->stop();
293 }
294
295 void InfoSidebarPage::showMetaInfo()
296 {
297 m_metaTextLabel->clear();
298
299 if ((m_selection.size() <= 1) || !m_fileItem.isNull()) {
300 KFileItem fileItem;
301 if (m_fileItem.isNull()) {
302 // no pending request is ongoing
303 const KUrl url = (m_selection.size() == 1) ? m_selection.first().url() : m_shownUrl;
304 fileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, url);
305 fileItem.refresh();
306 } else {
307 fileItem = m_fileItem;
308 }
309
310 if (fileItem.isDir()) {
311 m_metaTextLabel->add(i18nc("@label", "Type:"), i18nc("@label", "Folder"));
312 m_metaTextLabel->add(i18nc("@label", "Modified:"), fileItem.timeString());
313 } else {
314 m_metaTextLabel->add(i18nc("@label", "Type:"), fileItem.mimeComment());
315
316 m_metaTextLabel->add(i18nc("@label", "Size:"), KIO::convertSize(fileItem.size()));
317 m_metaTextLabel->add(i18nc("@label", "Modified:"), fileItem.timeString());
318
319 if (fileItem.isLocalFile()) {
320 // TODO: See convertMetaInfo below, find a way to display only interesting information
321 // in a readable way
322 const KFileMetaInfo::WhatFlags flags = KFileMetaInfo::Fastest |
323 KFileMetaInfo::TechnicalInfo |
324 KFileMetaInfo::ContentInfo;
325 const QString path = fileItem.url().path();
326 const KFileMetaInfo fileMetaInfo(path, QString(), flags);
327 if (fileMetaInfo.isValid()) {
328 const QHash<QString, KFileMetaInfoItem>& items = fileMetaInfo.items();
329 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
330 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
331 QString labelText;
332 while (it != end) {
333 const KFileMetaInfoItem& metaInfoItem = it.value();
334 const QVariant& value = metaInfoItem.value();
335 if (value.isValid() && convertMetaInfo(metaInfoItem.name(), labelText)) {
336 m_metaTextLabel->add(labelText, value.toString());
337 }
338 ++it;
339 }
340 }
341 }
342 }
343
344 if (m_metaDataWidget != 0) {
345 m_metaDataWidget->setFile(fileItem.targetUrl());
346 }
347 } else {
348 if (m_metaDataWidget != 0) {
349 KUrl::List urls;
350 foreach (const KFileItem& item, m_selection) {
351 urls.append(item.targetUrl());
352 }
353 m_metaDataWidget->setFiles(urls);
354 }
355
356 quint64 totalSize = 0;
357 foreach (const KFileItem& item, m_selection) {
358 // Only count the size of files, not dirs to match what
359 // DolphinViewContainer::selectionStatusBarText() does.
360 if (!item.isDir() && !item.isLink()) {
361 totalSize += item.size();
362 }
363 }
364 m_metaTextLabel->add(i18nc("@label", "Total size:"), KIO::convertSize(totalSize));
365 }
366 }
367
368 bool InfoSidebarPage::convertMetaInfo(const QString& key, QString& text) const
369 {
370 // TODO: This code prevents that interesting meta information might be hidden
371 // and only bypasses the current problem that not all the meta information should
372 // be shown to the user. Check whether it's possible with Nepomuk to show
373 // all "user relevant" information in a readable way...
374
375 struct MetaKey {
376 const char* key;
377 const char* text;
378 };
379
380 // sorted list of keys, where its data should be shown
381 static const MetaKey keys[] = {
382 { "audio.album", "Album:" },
383 { "audio.artist", "Artist:" },
384 { "audio.title", "Title:" },
385 };
386
387 // do a binary search for the key...
388 int top = 0;
389 int bottom = sizeof(keys) / sizeof(MetaKey) - 1;
390 while (top <= bottom) {
391 const int middle = (top + bottom) / 2;
392 const int result = key.compare(keys[middle].key);
393 if (result < 0) {
394 bottom = middle - 1;
395 } else if (result > 0) {
396 top = middle + 1;
397 } else {
398 text = keys[middle].text;
399 return true;
400 }
401 }
402
403 return false;
404 }
405
406 #include "infosidebarpage.moc"