]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanelcontent.cpp
Ensure authentication data is cached properly
[dolphin.git] / src / panels / information / informationpanelcontent.cpp
1 /***************************************************************************
2 * Copyright (C) 2009 by Peter Penz <peter.penz19@gmail.com> *
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 "informationpanelcontent.h"
21
22 #include <KDialog>
23 #include <KFileItem>
24 #include <kfilemetadatawidget.h>
25 #include <KFilePlacesModel>
26 #include <KGlobalSettings>
27 #include <KIO/JobUiDelegate>
28 #include <KIO/PreviewJob>
29 #include <KIconEffect>
30 #include <KIconLoader>
31 #include <KLocale>
32 #include <KMenu>
33 #include <kseparator.h>
34 #include <KStringHandler>
35
36 #include <Phonon/BackendCapabilities>
37 #include <Phonon/MediaObject>
38 #include <Phonon/SeekSlider>
39
40 #include <QEvent>
41 #include <QLabel>
42 #include <QPixmap>
43 #include <QPointer>
44 #include <QResizeEvent>
45 #include <QScrollArea>
46 #include <QTextDocument>
47 #include <QTextLayout>
48 #include <QTextLine>
49 #include <QTimer>
50 #include <QVBoxLayout>
51
52 #include "dolphin_informationpanelsettings.h"
53 #include "filemetadataconfigurationdialog.h"
54 #include "phononwidget.h"
55 #include "pixmapviewer.h"
56 #include "views/dolphinplacesmodel.h"
57
58 InformationPanelContent::InformationPanelContent(QWidget* parent) :
59 QWidget(parent),
60 m_item(),
61 m_pendingPreview(false),
62 m_outdatedPreviewTimer(0),
63 m_preview(0),
64 m_phononWidget(0),
65 m_nameLabel(0),
66 m_metaDataWidget(0),
67 m_metaDataArea(0)
68 {
69 parent->installEventFilter(this);
70
71 // Initialize timer for disabling an outdated preview with a small
72 // delay. This prevents flickering if the new preview can be generated
73 // within a very small timeframe.
74 m_outdatedPreviewTimer = new QTimer(this);
75 m_outdatedPreviewTimer->setInterval(300);
76 m_outdatedPreviewTimer->setSingleShot(true);
77 connect(m_outdatedPreviewTimer, SIGNAL(timeout()),
78 this, SLOT(markOutdatedPreview()));
79
80 QVBoxLayout* layout = new QVBoxLayout(this);
81 layout->setSpacing(KDialog::spacingHint());
82
83 // preview
84 const int minPreviewWidth = KIconLoader::SizeEnormous + KIconLoader::SizeMedium;
85
86 m_preview = new PixmapViewer(parent);
87 m_preview->setMinimumWidth(minPreviewWidth);
88 m_preview->setMinimumHeight(KIconLoader::SizeEnormous);
89
90 m_phononWidget = new PhononWidget(parent);
91 m_phononWidget->hide();
92 m_phononWidget->setMinimumWidth(minPreviewWidth);
93 connect(m_phononWidget, SIGNAL(playingStarted()),
94 this, SLOT(slotPlayingStarted()));
95 connect(m_phononWidget, SIGNAL(playingStopped()),
96 this, SLOT(slotPlayingStopped()));
97
98 // name
99 m_nameLabel = new QLabel(parent);
100 QFont font = m_nameLabel->font();
101 font.setBold(true);
102 m_nameLabel->setFont(font);
103 m_nameLabel->setAlignment(Qt::AlignHCenter);
104 m_nameLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
105
106 const bool previewsShown = InformationPanelSettings::previewsShown();
107 m_preview->setVisible(previewsShown);
108
109 m_metaDataWidget = new KFileMetaDataWidget(parent);
110 m_metaDataWidget->setFont(KGlobalSettings::smallestReadableFont());
111 m_metaDataWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
112 connect(m_metaDataWidget, SIGNAL(urlActivated(KUrl)), this, SIGNAL(urlActivated(KUrl)));
113
114 // Encapsulate the MetaDataWidget inside a container that has a dummy widget
115 // at the bottom. This prevents that the meta data widget gets vertically stretched
116 // in the case where the height of m_metaDataArea > m_metaDataWidget.
117 QWidget* metaDataWidgetContainer = new QWidget(parent);
118 QVBoxLayout* containerLayout = new QVBoxLayout(metaDataWidgetContainer);
119 containerLayout->setContentsMargins(0, 0, 0, 0);
120 containerLayout->setSpacing(0);
121 containerLayout->addWidget(m_metaDataWidget);
122 containerLayout->addStretch();
123
124 m_metaDataArea = new QScrollArea(parent);
125 m_metaDataArea->setWidget(metaDataWidgetContainer);
126 m_metaDataArea->setWidgetResizable(true);
127 m_metaDataArea->setFrameShape(QFrame::NoFrame);
128
129 QWidget* viewport = m_metaDataArea->viewport();
130 viewport->installEventFilter(this);
131
132 QPalette palette = viewport->palette();
133 palette.setColor(viewport->backgroundRole(), QColor(Qt::transparent));
134 viewport->setPalette(palette);
135
136 layout->addWidget(m_preview);
137 layout->addWidget(m_phononWidget);
138 layout->addWidget(m_nameLabel);
139 layout->addWidget(new KSeparator());
140 layout->addWidget(m_metaDataArea);
141 }
142
143 InformationPanelContent::~InformationPanelContent()
144 {
145 InformationPanelSettings::self()->writeConfig();
146 }
147
148 void InformationPanelContent::showItem(const KFileItem& item)
149 {
150 m_pendingPreview = false;
151
152 const KUrl itemUrl = item.url();
153 const bool isSearchUrl = itemUrl.protocol().contains("search") && item.nepomukUri().isEmpty();
154 if (!applyPlace(itemUrl)) {
155 setNameLabelText(item.text());
156 if (isSearchUrl) {
157 // in the case of a search-URL the URL is not readable for humans
158 // (at least not useful to show in the Information Panel)
159 KIconLoader iconLoader;
160 QPixmap icon = iconLoader.loadIcon("nepomuk",
161 KIconLoader::NoGroup,
162 KIconLoader::SizeEnormous);
163 m_preview->setPixmap(icon);
164 } else {
165 // try to get a preview pixmap from the item...
166 m_pendingPreview = true;
167
168 // Mark the currently shown preview as outdated. This is done
169 // with a small delay to prevent a flickering when the next preview
170 // can be shown within a short timeframe. This timer is not started
171 // for directories, as directory previews might fail and return the
172 // same icon.
173 if (!item.isDir()) {
174 m_outdatedPreviewTimer->start();
175 }
176
177 KIO::PreviewJob* job = new KIO::PreviewJob(KFileItemList() << item, QSize(m_preview->width(), m_preview->height()));
178 job->setScaleType(KIO::PreviewJob::Unscaled);
179 job->setIgnoreMaximumSize(item.isLocalFile());
180 if (job->ui()) {
181 job->ui()->setWindow(this);
182 }
183
184 connect(job, SIGNAL(gotPreview(KFileItem,QPixmap)),
185 this, SLOT(showPreview(KFileItem,QPixmap)));
186 connect(job, SIGNAL(failed(KFileItem)),
187 this, SLOT(showIcon(KFileItem)));
188 }
189 }
190
191 if (m_metaDataWidget) {
192 m_metaDataWidget->show();
193 m_metaDataWidget->setItems(KFileItemList() << item);
194 }
195
196 if (InformationPanelSettings::previewsShown()) {
197 const QString mimeType = item.mimetype();
198 const bool usePhonon = Phonon::BackendCapabilities::isMimeTypeAvailable(mimeType) &&
199 (mimeType != "image/png"); // TODO: workaround, as Phonon
200 // thinks it supports PNG images
201 if (usePhonon) {
202 m_phononWidget->show();
203 PhononWidget::Mode mode = mimeType.startsWith(QLatin1String("video"))
204 ? PhononWidget::Video
205 : PhononWidget::Audio;
206 m_phononWidget->setMode(mode);
207 m_phononWidget->setUrl(item.targetUrl());
208 if ((mode == PhononWidget::Video) && m_preview->isVisible()) {
209 m_phononWidget->setVideoSize(m_preview->size());
210 }
211 } else {
212 m_phononWidget->hide();
213 m_preview->setVisible(true);
214 }
215 } else {
216 m_phononWidget->hide();
217 }
218
219 m_item = item;
220 }
221
222 void InformationPanelContent::showItems(const KFileItemList& items)
223 {
224 m_pendingPreview = false;
225
226 KIconLoader iconLoader;
227 QPixmap icon = iconLoader.loadIcon("dialog-information",
228 KIconLoader::NoGroup,
229 KIconLoader::SizeEnormous);
230 m_preview->setPixmap(icon);
231 setNameLabelText(i18ncp("@info", "%1 item selected", "%1 items selected", items.count()));
232
233 if (m_metaDataWidget) {
234 m_metaDataWidget->setItems(items);
235 }
236
237 m_phononWidget->hide();
238
239 m_item = KFileItem();
240 }
241
242 bool InformationPanelContent::eventFilter(QObject* obj, QEvent* event)
243 {
244 switch (event->type()) {
245 case QEvent::Resize: {
246 QResizeEvent* resizeEvent = static_cast<QResizeEvent*>(event);
247 if (obj == m_metaDataArea->viewport()) {
248 // The size of the meta text area has changed. Adjust the fixed
249 // width in a way that no horizontal scrollbar needs to be shown.
250 m_metaDataWidget->setFixedWidth(resizeEvent->size().width());
251 } else if (obj == parent()) {
252 adjustWidgetSizes(resizeEvent->size().width());
253 }
254 break;
255 }
256
257 case QEvent::Polish:
258 adjustWidgetSizes(parentWidget()->width());
259 break;
260
261 default:
262 break;
263 }
264
265 return QWidget::eventFilter(obj, event);
266 }
267
268 void InformationPanelContent::configureSettings(const QList<QAction*>& customContextMenuActions)
269 {
270 KMenu popup(this);
271
272 QAction* previewAction = popup.addAction(i18nc("@action:inmenu", "Preview"));
273 previewAction->setIcon(KIcon("view-preview"));
274 previewAction->setCheckable(true);
275 previewAction->setChecked(InformationPanelSettings::previewsShown());
276
277 QAction* configureAction = popup.addAction(i18nc("@action:inmenu", "Configure..."));
278 configureAction->setIcon(KIcon("configure"));
279
280 popup.addSeparator();
281 foreach (QAction* action, customContextMenuActions) {
282 popup.addAction(action);
283 }
284
285 // Open the popup and adjust the settings for the
286 // selected action.
287 QAction* action = popup.exec(QCursor::pos());
288 if (!action) {
289 return;
290 }
291
292 const bool isChecked = action->isChecked();
293 if (action == previewAction) {
294 m_preview->setVisible(isChecked);
295 InformationPanelSettings::setPreviewsShown(isChecked);
296 } else if (action == configureAction) {
297 FileMetaDataConfigurationDialog* dialog = new FileMetaDataConfigurationDialog();
298 dialog->setDescription(i18nc("@label::textbox",
299 "Select which data should be shown in the information panel:"));
300 dialog->setItems(m_metaDataWidget->items());
301 dialog->setAttribute(Qt::WA_DeleteOnClose);
302 dialog->show();
303 dialog->raise();
304 dialog->activateWindow();
305 connect(dialog, SIGNAL(destroyed()), this, SLOT(refreshMetaData()));
306 }
307 }
308
309 void InformationPanelContent::showIcon(const KFileItem& item)
310 {
311 m_outdatedPreviewTimer->stop();
312 m_pendingPreview = false;
313 if (!applyPlace(item.targetUrl())) {
314 KIcon icon(item.iconName(), KIconLoader::global(), item.overlays());
315 m_preview->setPixmap(icon.pixmap(KIconLoader::SizeEnormous));
316 }
317 }
318
319 void InformationPanelContent::showPreview(const KFileItem& item,
320 const QPixmap& pixmap)
321 {
322 m_outdatedPreviewTimer->stop();
323 Q_UNUSED(item);
324 if (m_pendingPreview) {
325 QPixmap p = pixmap;
326 KIconLoader::global()->drawOverlays(item.overlays(), p, KIconLoader::Desktop);
327 m_preview->setPixmap(p);
328 m_pendingPreview = false;
329 }
330 }
331
332 void InformationPanelContent::markOutdatedPreview()
333 {
334 KIconEffect *iconEffect = KIconLoader::global()->iconEffect();
335 QPixmap disabledPixmap = iconEffect->apply(m_preview->pixmap(),
336 KIconLoader::Desktop,
337 KIconLoader::DisabledState);
338 m_preview->setPixmap(disabledPixmap);
339 }
340
341 void InformationPanelContent::slotPlayingStarted()
342 {
343 m_preview->setVisible(m_phononWidget->mode() != PhononWidget::Video);
344 }
345
346 void InformationPanelContent::slotPlayingStopped()
347 {
348 m_preview->setVisible(true);
349 }
350
351 void InformationPanelContent::refreshMetaData()
352 {
353 if (!m_item.isNull() && m_item.nepomukUri().isValid()) {
354 showItem(m_item);
355 }
356 }
357
358 bool InformationPanelContent::applyPlace(const KUrl& url)
359 {
360 KFilePlacesModel* placesModel = DolphinPlacesModel::instance();
361 const int count = placesModel->rowCount();
362
363 for (int i = 0; i < count; ++i) {
364 QModelIndex index = placesModel->index(i, 0);
365
366 if (url.equals(placesModel->url(index), KUrl::CompareWithoutTrailingSlash)) {
367 setNameLabelText(placesModel->text(index));
368 m_preview->setPixmap(placesModel->icon(index).pixmap(128, 128));
369 return true;
370 }
371 }
372
373 return false;
374 }
375
376 void InformationPanelContent::setNameLabelText(const QString& text)
377 {
378 QTextOption textOption;
379 textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
380
381 const QString processedText = Qt::mightBeRichText(text) ? text : KStringHandler::preProcessWrap(text);
382
383 QTextLayout textLayout(processedText);
384 textLayout.setFont(m_nameLabel->font());
385 textLayout.setTextOption(textOption);
386
387 QString wrappedText;
388 wrappedText.reserve(processedText.length());
389
390 // wrap the text to fit into the width of m_nameLabel
391 textLayout.beginLayout();
392 QTextLine line = textLayout.createLine();
393 while (line.isValid()) {
394 line.setLineWidth(m_nameLabel->width());
395 wrappedText += processedText.mid(line.textStart(), line.textLength());
396
397 line = textLayout.createLine();
398 if (line.isValid()) {
399 wrappedText += QChar::LineSeparator;
400 }
401 }
402 textLayout.endLayout();
403
404 m_nameLabel->setText(wrappedText);
405 }
406
407 void InformationPanelContent::adjustWidgetSizes(int width)
408 {
409 // If the text inside the name label or the info label cannot
410 // get wrapped, then the maximum width of the label is increased
411 // so that the width of the information panel gets increased.
412 // To prevent this, the maximum width is adjusted to
413 // the current width of the panel.
414 const int maxWidth = width - KDialog::spacingHint() * 4;
415 m_nameLabel->setMaximumWidth(maxWidth);
416
417 // The metadata widget also contains a text widget which may return
418 // a large preferred width.
419 if (m_metaDataWidget) {
420 m_metaDataWidget->setMaximumWidth(maxWidth);
421 }
422
423 // try to increase the preview as large as possible
424 m_preview->setSizeHint(QSize(maxWidth, maxWidth));
425
426 if (m_phononWidget->isVisible() && (m_phononWidget->mode() == PhononWidget::Video)) {
427 // assure that the size of the video player is the same as the preview size
428 m_phononWidget->setVideoSize(QSize(maxWidth, maxWidth));
429 }
430 }
431
432 #include "informationpanelcontent.moc"