]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
internal cleanup; additional meta information can be shown now, although this code...
[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-kmetadata.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_name(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_name = new QLabel(this);
74 m_name->setTextFormat(Qt::RichText);
75 m_name->setAlignment(m_name->alignment() | Qt::AlignHCenter);
76 m_name->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_name);
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 item name cannot get wrapped, the maximum width of
142 // the label is increased, so that the width of the information sidebar
143 // gets increased. To prevent this, the maximum width is adjusted to
144 // the current width of the sidebar.
145 m_name->setMaximumWidth(event->size().width() - KDialog::spacingHint() * 4);
146 SidebarPage::resizeEvent(event);
147 }
148
149 void InfoSidebarPage::showItemInfo()
150 {
151 if (!isVisible()) {
152 return;
153 }
154
155 cancelRequest();
156
157 const KFileItemList& selectedItems = selection();
158
159 KUrl file;
160 const int itemCount = selectedItems.count();
161 if (selectedItems.count() == 0) {
162 file = m_shownUrl;
163 } else {
164 file = selectedItems[0]->url();
165 }
166 if (itemCount > 1) {
167 KIconLoader iconLoader;
168 QPixmap icon = iconLoader.loadIcon("exec",
169 K3Icon::NoGroup,
170 K3Icon::SizeEnormous);
171 m_preview->setPixmap(icon);
172 m_name->setText(i18n("%1 items selected", selectedItems.count()));
173 } else if (!applyBookmark(file)) {
174 // try to get a preview pixmap from the item...
175 KUrl::List list;
176 list.append(file);
177
178 m_pendingPreview = true;
179 m_preview->setPixmap(QPixmap());
180
181 KIO::PreviewJob* job = KIO::filePreview(list,
182 m_preview->width(),
183 K3Icon::SizeEnormous,
184 0,
185 0,
186 true,
187 false);
188 job->setIgnoreMaximumSize(true);
189
190 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
191 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
192 connect(job, SIGNAL(failed(const KFileItem&)),
193 this, SLOT(showIcon(const KFileItem&)));
194
195 QString text("<b>");
196 text.append(file.fileName());
197 text.append("</b>");
198 m_name->setText(text);
199 }
200
201 showMetaInfo();
202 }
203
204 void InfoSidebarPage::slotTimeout()
205 {
206 m_shownUrl = m_urlCandidate;
207 showItemInfo();
208 }
209
210 void InfoSidebarPage::showIcon(const KFileItem& item)
211 {
212 m_pendingPreview = false;
213 if (!applyBookmark(item.url())) {
214 m_preview->setPixmap(item.pixmap(K3Icon::SizeEnormous));
215 }
216 }
217
218 void InfoSidebarPage::showPreview(const KFileItem& item,
219 const QPixmap& pixmap)
220 {
221 Q_UNUSED(item);
222 if (m_pendingPreview) {
223 m_preview->setPixmap(pixmap);
224 m_pendingPreview = false;
225 }
226 }
227
228 bool InfoSidebarPage::applyBookmark(const KUrl& url)
229 {
230 KFilePlacesModel* placesModel = DolphinSettings::instance().placesModel();
231 int count = placesModel->rowCount();
232
233 for (int i = 0; i < count; ++i) {
234 QModelIndex index = placesModel->index(i, 0);
235
236 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
237 QString text("<b>");
238 text.append(placesModel->text(index));
239 text.append("</b>");
240 m_name->setText(text);
241
242 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
243 return true;
244 }
245 }
246
247 return false;
248 }
249
250 void InfoSidebarPage::cancelRequest()
251 {
252 m_timer->stop();
253 m_pendingPreview = false;
254 }
255
256 void InfoSidebarPage::showMetaInfo()
257 {
258 QString text;
259
260 const KFileItemList& selectedItems = selection();
261 if (selectedItems.size() <= 1) {
262 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
263 fileItem.refresh();
264
265 if (fileItem.isDir()) {
266 addInfoLine(text, i18n("Type:"), i18n("Folder"));
267 } else {
268 addInfoLine(text, i18n("Type:"), fileItem.mimeComment());
269
270 QString sizeText(KIO::convertSize(fileItem.size()));
271 addInfoLine(text, i18n("Size:"), sizeText);
272 addInfoLine(text, i18n("Modified:"), fileItem.timeString());
273
274 // TODO: deactivate showing additional meta information, as the system
275 // hangs when retrieving the meta information of a zipped file
276 /*const KFileMetaInfo metaInfo(fileItem.url());
277 if (metaInfo.isValid()) {
278 const QHash<QString, KFileMetaInfoItem>& items = metaInfo.items();
279 QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
280 const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
281 QString labelText;
282 while (it != end) {
283 const KFileMetaInfoItem& metaInfo = it.value();
284 const QVariant& value = metaInfo.value();
285 if (value.isValid() && convertMetaInfo(metaInfo.name(), labelText)) {
286 addInfoLine(text, labelText, value.toString());
287 }
288 ++it;
289 }
290 }*/
291 }
292
293 if (MetaDataWidget::metaDataAvailable()) {
294 m_metadataWidget->setFile(fileItem.url());
295 }
296 } else {
297 if (MetaDataWidget::metaDataAvailable()) {
298 m_metadataWidget->setFiles(selectedItems.urlList());
299 }
300
301 unsigned long int totalSize = 0;
302 foreach (KFileItem* item, selectedItems) {
303 // TODO: what to do with directories (same with the one-item-selected-code)?,
304 // item->size() does not return the size of the content : not very instinctive for users
305 totalSize += item->size();
306 }
307 addInfoLine(text, i18n("Total size:"), KIO::convertSize(totalSize));
308 }
309 m_infoLabel->setText(text);
310 }
311
312 void InfoSidebarPage::addInfoLine(QString& text,
313 const QString& labelText,
314 const QString& infoText)
315 {
316 if (!infoText.isEmpty()) {
317 text += "<br/>";
318 }
319 text += QString("<b>%1</b> %2").arg(labelText).arg(infoText);
320 }
321
322 bool InfoSidebarPage::convertMetaInfo(const QString& key, QString& text) const
323 {
324 // TODO: This code prevents that interesting meta information might be hidden
325 // and only bypasses the current problem that not all the meta information should
326 // be shown to the user. Check whether it's possible with Nepomuk to show
327 // all "user relevant" information in a readable way...
328
329 struct MetaKey {
330 const char* key;
331 const char* text;
332 };
333
334 // sorted list of keys, where its data should be shown
335 static const MetaKey keys[] = {
336 { "audio.album", "Album:" },
337 { "audio.artist", "Artist:" },
338 { "audio.title", "Title:" },
339 };
340
341 // do a binary search for the key...
342 int top = 0;
343 int bottom = sizeof(keys) / sizeof(MetaKey) - 1;
344 while (top <= bottom) {
345 const int middle = (top + bottom) / 2;
346 const int result = key.compare(keys[middle].key);
347 if (result < 0) {
348 bottom = middle - 1;
349 } else if (result > 0) {
350 top = middle + 1;
351 } else {
352 text = keys[middle].text;
353 return true;
354 }
355 }
356
357 return false;
358 }
359
360 #include "infosidebarpage.moc"