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