]> cloud.milkyroute.net Git - dolphin.git/blob - src/infosidebarpage.cpp
initial step for cleaning up the information sidebar: get rid of service actions...
[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 <config-kmetadata.h>
21
22 #include "infosidebarpage.h"
23
24 #include <QLayout>
25 #include <QPixmap>
26 #include <QLabel>
27 #include <QTimer>
28 #include <QPushButton>
29 #include <QMenu>
30 #include <QPainter>
31 #include <QFontMetrics>
32 #include <QEvent>
33 #include <QInputDialog>
34 #include <QDir>
35
36 #include <kfileplacesmodel.h>
37 #include <klocale.h>
38 #include <kstandarddirs.h>
39 #include <kio/previewjob.h>
40 #include <kfileitem.h>
41 #include <kdialog.h>
42 #include <kglobalsettings.h>
43 #include <kfilemetainfo.h>
44 #include <kvbox.h>
45 #include <kseparator.h>
46 #include <kiconloader.h>
47
48 #include "pixmapviewer.h"
49 #include "dolphinsettings.h"
50 #include "metadatawidget.h"
51
52 InfoSidebarPage::InfoSidebarPage(QWidget* parent) :
53 SidebarPage(parent),
54 m_multipleSelection(false), //TODO: check if I'm needed
55 m_pendingPreview(false),
56 m_timer(0),
57 m_currentSelection(KFileItemList()),
58 m_preview(0),
59 m_name(0),
60 m_infos(0),
61 m_metadataWidget(0)
62 {
63 const int spacing = KDialog::spacingHint();
64
65 m_timer = new QTimer(this);
66 connect(m_timer, SIGNAL(timeout()),
67 this, SLOT(slotTimeout()));
68
69 QVBoxLayout* layout = new QVBoxLayout;
70 layout->setSpacing(spacing);
71
72 // preview
73 m_preview = new PixmapViewer(this);
74 m_preview->setMinimumWidth(K3Icon::SizeEnormous);
75 m_preview->setFixedHeight(K3Icon::SizeEnormous);
76
77 // name
78 m_name = new QLabel(this);
79 m_name->setTextFormat(Qt::RichText);
80 m_name->setAlignment(m_name->alignment() | Qt::AlignHCenter);
81 QFontMetrics fontMetrics(m_name->font());
82 m_name->setMinimumHeight(fontMetrics.height() * 3);
83 m_name->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Maximum);
84
85 KSeparator* sep1 = new KSeparator(this);
86
87 // general information
88 m_infos = new QLabel(this);
89 m_infos->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
90 m_infos->setTextFormat(Qt::RichText);
91
92 KSeparator* sep2 = new KSeparator(this);
93
94 if (MetaDataWidget::metaDataAvailable()) {
95 m_metadataWidget = new MetaDataWidget(this);
96 }
97
98 layout->addItem(new QSpacerItem(spacing, spacing, QSizePolicy::Preferred, QSizePolicy::Fixed));
99 layout->addWidget(m_preview);
100 layout->addWidget(m_name);
101 layout->addWidget(sep1);
102 layout->addWidget(m_infos);
103 layout->addWidget(sep2);
104 if (m_metadataWidget) {
105 layout->addWidget(m_metadataWidget);
106 layout->addWidget(new KSeparator(this));
107 }
108 // ensure that widgets in the information side bar are aligned towards the top
109 layout->addStretch(1);
110 setLayout(layout);
111 }
112
113 InfoSidebarPage::~InfoSidebarPage()
114 {}
115
116 void InfoSidebarPage::setUrl(const KUrl& url)
117 {
118 if (!m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
119 cancelRequest();
120 m_shownUrl = url;
121 showItemInfo();
122 }
123 }
124
125 void InfoSidebarPage::setSelection(const KFileItemList& selection)
126 {
127 // TODO: deactivated the following code, as it has side effects. To
128 // reproduce start Dolphin and open a folder -> the URL navigator gets
129 // reset. First guess: it seems that a setUrl signal is emitted
130 // by the following code
131
132 Q_UNUSED(selection);
133 /*cancelRequest();
134 m_currentSelection = selection;
135 m_multipleSelection = (m_currentSelection.size() > 1);
136 showItemInfo();*/
137 }
138
139 void InfoSidebarPage::requestDelayedItemInfo(const KUrl& url)
140 {
141 cancelRequest();
142
143 if (!url.isEmpty() && !m_multipleSelection) {
144 m_urlCandidate = url;
145 m_timer->setSingleShot(true);
146 m_timer->start(300);
147 }
148 }
149
150 void InfoSidebarPage::showItemInfo()
151 {
152 cancelRequest();
153
154 KFileItemList selectedItems = m_currentSelection;
155 KUrl file;
156 if (selectedItems.count() == 0) {
157 file = m_shownUrl;
158 } else {
159 file = selectedItems[0]->url();
160 }
161 if (m_multipleSelection) {
162 KIconLoader iconLoader;
163 QPixmap icon = iconLoader.loadIcon("exec",
164 K3Icon::NoGroup,
165 K3Icon::SizeEnormous);
166 m_preview->setPixmap(icon);
167 m_name->setText(i18n("%1 items selected", selectedItems.count()));
168 } else if (!applyBookmark(file)) {
169 // try to get a preview pixmap from the item...
170 KUrl::List list;
171 list.append(file);
172
173 m_pendingPreview = true;
174 m_preview->setPixmap(QPixmap());
175
176 KIO::PreviewJob* job = KIO::filePreview(list,
177 m_preview->width(),
178 K3Icon::SizeEnormous,
179 0,
180 0,
181 true,
182 false);
183 job->setIgnoreMaximumSize(true);
184
185 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
186 this, SLOT(gotPreview(const KFileItem&, const QPixmap&)));
187 connect(job, SIGNAL(failed(const KFileItem&)),
188 this, SLOT(slotPreviewFailed(const KFileItem&)));
189
190 QString text("<b>");
191 text.append(file.fileName());
192 text.append("</b>");
193 m_name->setText(text);
194 }
195
196 createMetaInfo();
197 }
198
199 void InfoSidebarPage::slotTimeout()
200 {
201 m_shownUrl = m_urlCandidate;
202 showItemInfo();
203 }
204
205 void InfoSidebarPage::slotPreviewFailed(const KFileItem& item)
206 {
207 m_pendingPreview = false;
208 if (!applyBookmark(item.url())) {
209 m_preview->setPixmap(item.pixmap(K3Icon::SizeEnormous));
210 }
211 }
212
213 void InfoSidebarPage::gotPreview(const KFileItem& item,
214 const QPixmap& pixmap)
215 {
216 Q_UNUSED(item);
217 if (m_pendingPreview) {
218 m_preview->setPixmap(pixmap);
219 m_pendingPreview = false;
220 }
221 }
222
223 bool InfoSidebarPage::applyBookmark(const KUrl& url)
224 {
225 KFilePlacesModel *placesModel = DolphinSettings::instance().placesModel();
226 int count = placesModel->rowCount();
227
228 for (int i = 0; i < count; ++i) {
229 QModelIndex index = placesModel->index(i, 0);
230
231 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
232 QString text("<b>");
233 text.append(placesModel->text(index));
234 text.append("</b>");
235 m_name->setText(text);
236
237 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
238 return true;
239 }
240 }
241
242 return false;
243 }
244
245 void InfoSidebarPage::cancelRequest()
246 {
247 m_timer->stop();
248 m_pendingPreview = false;
249 }
250
251 void InfoSidebarPage::createMetaInfo()
252 {
253 beginInfoLines();
254 if (m_currentSelection.size() == 0) {
255 KFileItem fileItem(S_IFDIR, KFileItem::Unknown, m_shownUrl);
256 fileItem.refresh();
257
258 if (fileItem.isDir()) {
259 addInfoLine(i18n("Type:"), i18n("Directory"));
260 }
261 if (MetaDataWidget::metaDataAvailable())
262 m_metadataWidget->setFile(fileItem.url());
263 } else if (m_currentSelection.count() == 1) {
264 KFileItem* fileItem = m_currentSelection.at(0);
265 addInfoLine(i18n("Type:"), fileItem->mimeComment());
266
267 QString sizeText(KIO::convertSize(fileItem->size()));
268 addInfoLine(i18n("Size:"), sizeText);
269 addInfoLine(i18n("Modified:"), fileItem->timeString());
270
271 const KFileMetaInfo& metaInfo = fileItem->metaInfo();
272 if (metaInfo.isValid()) {
273 QStringList keys = metaInfo.supportedKeys();
274 for (QStringList::Iterator it = keys.begin(); it != keys.end(); ++it) {
275 if (showMetaInfo(*it)) {
276 KFileMetaInfoItem metaInfoItem = metaInfo.item(*it);
277 addInfoLine(*it, metaInfoItem.value().toString());
278 }
279 }
280 }
281 if (MetaDataWidget::metaDataAvailable())
282 m_metadataWidget->setFile(fileItem->url());
283 } else {
284 if (MetaDataWidget::metaDataAvailable())
285 m_metadataWidget->setFiles(m_currentSelection.urlList());
286 unsigned long int totSize = 0;
287 foreach(KFileItem* item, m_currentSelection) {
288 totSize += item->size(); //FIXME what to do with directories ? (same with the one-item-selected-code), item->size() does not return the size of the content : not very instinctive for users
289 }
290 addInfoLine(i18n("Total size:"), KIO::convertSize(totSize));
291 }
292 endInfoLines();
293 }
294
295 void InfoSidebarPage::beginInfoLines()
296 {
297 m_infoLines = QString();
298 }
299
300 void InfoSidebarPage::endInfoLines()
301 {
302 m_infos->setText(m_infoLines);
303 }
304
305 bool InfoSidebarPage::showMetaInfo(const QString& key) const
306 {
307 // sorted list of keys, where it's data should be shown
308 static const char* keys[] = {
309 "Album",
310 "Artist",
311 "Author",
312 "Bitrate",
313 "Date",
314 "Dimensions",
315 "Genre",
316 "Length",
317 "Lines",
318 "Pages",
319 "Title",
320 "Words"
321 };
322
323 // do a binary search for the key...
324 int top = 0;
325 int bottom = sizeof(keys) / sizeof(char*) - 1;
326 while (top < bottom) {
327 const int middle = (top + bottom) / 2;
328 const int result = key.compare(keys[middle]);
329 if (result < 0) {
330 bottom = middle - 1;
331 } else if (result > 0) {
332 top = middle + 1;
333 } else {
334 return true;
335 }
336 }
337
338 return false;
339 }
340
341 void InfoSidebarPage::addInfoLine(const QString& labelText, const QString& infoText)
342 {
343 if (!m_infoLines.isEmpty())
344 m_infoLines += "<br/>";
345 m_infoLines += QString("<b>%1</b> %2").arg(labelText).arg(infoText);
346 }
347
348 #include "infosidebarpage.moc"