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