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