]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
GIT_SILENT Sync po/docbooks with svn
[dolphin.git] / src / panels / information / informationpanel.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006-2009 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "informationpanel.h"
8
9 #include "informationpanelcontent.h"
10
11 #define HAVE_KDIRNOTIFY __has_include(<KDirNotify>)
12 #if HAVE_KDIRNOTIFY
13 #include <KDirNotify>
14 #endif
15 #include <KIO/StatJob>
16 #include <KJobWidgets>
17 #include <KLocalizedString>
18
19 #include <Baloo/FileMetaDataWidget>
20
21 #include <QApplication>
22 #include <QMenu>
23 #include <QShowEvent>
24 #include <QTimer>
25 #include <QVBoxLayout>
26
27 #include "dolphin_informationpanelsettings.h"
28
29 InformationPanel::InformationPanel(QWidget *parent)
30 : Panel(parent)
31 , m_initialized(false)
32 , m_infoTimer(nullptr)
33 , m_urlChangedTimer(nullptr)
34 , m_resetUrlTimer(nullptr)
35 , m_shownUrl()
36 , m_urlCandidate()
37 , m_invalidUrlCandidate()
38 , m_hoveredItem()
39 , m_selection()
40 , m_folderStatJob(nullptr)
41 , m_content(nullptr)
42 {
43 }
44
45 InformationPanel::~InformationPanel()
46 {
47 }
48
49 void InformationPanel::setSelection(const KFileItemList &selection)
50 {
51 m_selection = selection;
52
53 if (!isVisible()) {
54 return;
55 }
56
57 const int count = selection.count();
58 if (count == 0) {
59 if (!isEqualToShownUrl(url())) {
60 m_shownUrl = url();
61 showItemInfo();
62 }
63 m_infoTimer->stop();
64 } else {
65 if ((count == 1) && !selection.first().url().isEmpty()) {
66 m_urlCandidate = selection.first().url();
67 }
68 showItemInfo();
69 }
70 }
71
72 void InformationPanel::requestDelayedItemInfo(const KFileItem &item)
73 {
74 if (!isVisible() || !InformationPanelSettings::showHovered()) {
75 return;
76 }
77
78 if (QApplication::mouseButtons() & Qt::LeftButton) {
79 // Ignore the request of an item information when a rubberband
80 // selection is ongoing.
81 return;
82 }
83
84 if (item.isNull()) {
85 m_hoveredItem = KFileItem();
86 return;
87 }
88
89 cancelRequest();
90
91 m_hoveredItem = item;
92 m_infoTimer->start();
93 }
94
95 bool InformationPanel::urlChanged()
96 {
97 if (!url().isValid()) {
98 return false;
99 }
100
101 if (!isVisible()) {
102 return true;
103 }
104
105 cancelRequest();
106 m_selection.clear();
107
108 if (!isEqualToShownUrl(url())) {
109 m_shownUrl = url();
110
111 // Update the content with a delay. This gives
112 // the directory lister the chance to show the content
113 // before expensive operations are done to show
114 // meta information.
115 m_urlChangedTimer->start();
116 }
117
118 return true;
119 }
120
121 void InformationPanel::showEvent(QShowEvent *event)
122 {
123 Panel::showEvent(event);
124 if (!event->spontaneous()) {
125 if (!m_initialized) {
126 // do a delayed initialization so that no performance
127 // penalty is given when Dolphin is started with a closed
128 // Information Panel
129 init();
130 }
131
132 m_shownUrl = url();
133 showItemInfo();
134 }
135 }
136
137 void InformationPanel::resizeEvent(QResizeEvent *event)
138 {
139 if (isVisible()) {
140 m_urlCandidate = m_shownUrl;
141 m_infoTimer->start();
142 }
143 Panel::resizeEvent(event);
144 }
145
146 void InformationPanel::contextMenuEvent(QContextMenuEvent *event)
147 {
148 showContextMenu(event->globalPos());
149 Panel::contextMenuEvent(event);
150 }
151
152 void InformationPanel::showContextMenu(const QPoint &pos)
153 {
154 QMenu popup(this);
155
156 QAction *previewAction = popup.addAction(i18nc("@action:inmenu", "Preview"));
157 previewAction->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
158 previewAction->setCheckable(true);
159 previewAction->setChecked(InformationPanelSettings::previewsShown());
160
161 QAction *previewAutoPlayAction = popup.addAction(i18nc("@action:inmenu", "Auto-Play media files"));
162 previewAutoPlayAction->setIcon(QIcon::fromTheme(QStringLiteral("media-playback-start")));
163 previewAutoPlayAction->setCheckable(true);
164 previewAutoPlayAction->setChecked(InformationPanelSettings::previewsAutoPlay());
165
166 QAction *showHoveredAction = popup.addAction(i18nc("@action:inmenu", "Show item on hover"));
167 showHoveredAction->setIcon(QIcon::fromTheme(QStringLiteral("followmouse")));
168 showHoveredAction->setCheckable(true);
169 showHoveredAction->setChecked(InformationPanelSettings::showHovered());
170
171 QAction *configureAction = popup.addAction(i18nc("@action:inmenu", "Configure…"));
172 configureAction->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
173 if (m_inConfigurationMode) {
174 configureAction->setEnabled(false);
175 }
176
177 QAction *dateformatAction = popup.addAction(i18nc("@action:inmenu", "Condensed Date"));
178 dateformatAction->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
179 dateformatAction->setCheckable(true);
180 dateformatAction->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat));
181
182 popup.addSeparator();
183 const auto actions = customContextMenuActions();
184 for (QAction *action : actions) {
185 popup.addAction(action);
186 }
187
188 // Open the popup and adjust the settings for the
189 // selected action.
190 QAction *action = popup.exec(pos);
191 if (!action) {
192 return;
193 }
194
195 const bool isChecked = action->isChecked();
196 if (action == previewAction) {
197 InformationPanelSettings::setPreviewsShown(isChecked);
198 m_content->refreshPreview();
199 } else if (action == previewAutoPlayAction) {
200 InformationPanelSettings::setPreviewsAutoPlay(isChecked);
201 m_content->setPreviewAutoPlay(isChecked);
202 } else if (action == showHoveredAction) {
203 InformationPanelSettings::setShowHovered(isChecked);
204 if (!isChecked) {
205 m_hoveredItem = KFileItem();
206 showItemInfo();
207 }
208 } else if (action == configureAction) {
209 m_inConfigurationMode = true;
210 m_content->configureShownProperties();
211 } else if (action == dateformatAction) {
212 int dateFormat = static_cast<int>(isChecked ? Baloo::DateFormats::ShortFormat : Baloo::DateFormats::LongFormat);
213
214 InformationPanelSettings::setDateFormat(dateFormat);
215 m_content->refreshMetaData();
216 }
217 }
218
219 void InformationPanel::showItemInfo()
220 {
221 if (!isVisible()) {
222 return;
223 }
224
225 cancelRequest();
226 //qDebug() << "showItemInfo" << m_fileItem;
227
228 if (m_hoveredItem.isNull() && (m_selection.count() > 1)) {
229 // The information for a selection of items should be shown
230 m_content->showItems(m_selection);
231 } else {
232 // The information for exactly one item should be shown
233 KFileItem item;
234 if (!m_hoveredItem.isNull() && InformationPanelSettings::showHovered()) {
235 item = m_hoveredItem;
236 } else if (!m_selection.isEmpty()) {
237 Q_ASSERT(m_selection.count() == 1);
238 item = m_selection.first();
239 }
240
241 if (!item.isNull()) {
242 m_shownUrl = item.url();
243 m_content->showItem(item);
244 return;
245 }
246
247 // No item is hovered and no selection has been done: provide
248 // an item for the currently shown directory.
249 m_shownUrl = url();
250 m_folderStatJob = KIO::stat(m_shownUrl, KIO::StatJob::SourceSide, KIO::StatDefaultDetails | KIO::StatRecursiveSize, KIO::HideProgressInfo);
251 if (m_folderStatJob->uiDelegate()) {
252 KJobWidgets::setWindow(m_folderStatJob, this);
253 }
254 connect(m_folderStatJob, &KIO::Job::result, this, &InformationPanel::slotFolderStatFinished);
255 }
256 }
257
258 void InformationPanel::slotFolderStatFinished(KJob *job)
259 {
260 m_folderStatJob = nullptr;
261 const KIO::UDSEntry entry = static_cast<KIO::StatJob *>(job)->statResult();
262 m_content->showItem(KFileItem(entry, m_shownUrl));
263 }
264
265 void InformationPanel::slotInfoTimeout()
266 {
267 m_shownUrl = m_urlCandidate;
268 m_urlCandidate.clear();
269 showItemInfo();
270 }
271
272 void InformationPanel::reset()
273 {
274 if (m_invalidUrlCandidate == m_shownUrl) {
275 m_invalidUrlCandidate = QUrl();
276
277 // The current URL is still invalid. Reset
278 // the content to show the directory URL.
279 m_selection.clear();
280 m_shownUrl = url();
281 showItemInfo();
282 }
283 }
284
285 void InformationPanel::slotFileRenamed(const QString &source, const QString &dest)
286 {
287 auto sourceUrl = QUrl::fromUserInput(source);
288 if (m_shownUrl == sourceUrl) {
289 auto destUrl = QUrl::fromUserInput(dest);
290
291 if ((m_selection.count() == 1) && (m_selection[0].url() == sourceUrl)) {
292 m_selection[0] = KFileItem(destUrl);
293 // Implementation note: Updating the selection is only required if exactly one
294 // item is selected, as the name of the item is shown. If this should change
295 // in future: Before parsing the whole selection take care to test possible
296 // performance bottlenecks when renaming several hundreds of files.
297 }
298
299 showItemInfo();
300 }
301 }
302
303 void InformationPanel::slotFilesAdded(const QString &directory)
304 {
305 if (m_shownUrl == QUrl::fromUserInput(directory)) {
306 // If the 'trash' icon changes because the trash has been emptied or got filled,
307 // the signal filesAdded("trash:/") will be emitted.
308 requestDelayedItemInfo(KFileItem());
309 }
310 }
311
312 void InformationPanel::slotFilesItemChanged(const KFileItemList &changedFileItems)
313 {
314 const auto item = changedFileItems.findByUrl(m_shownUrl);
315 if (!item.isNull()) {
316 showItemInfo();
317 }
318 }
319
320 void InformationPanel::slotFilesChanged(const QStringList &files)
321 {
322 for (const QString &fileName : files) {
323 if (m_shownUrl == QUrl::fromUserInput(fileName)) {
324 showItemInfo();
325 break;
326 }
327 }
328 }
329
330 void InformationPanel::slotFilesRemoved(const QStringList &files)
331 {
332 for (const QString &fileName : files) {
333 if (m_shownUrl == QUrl::fromUserInput(fileName)) {
334 // the currently shown item has been removed, show
335 // the parent directory as fallback
336 markUrlAsInvalid();
337 break;
338 }
339 }
340 }
341
342 void InformationPanel::slotEnteredDirectory(const QString &directory)
343 {
344 Q_UNUSED(directory)
345 }
346
347 void InformationPanel::slotLeftDirectory(const QString &directory)
348 {
349 if (m_shownUrl == QUrl::fromUserInput(directory)) {
350 // The signal 'leftDirectory' is also emitted when a media
351 // has been unmounted. In this case no directory change will be
352 // done in Dolphin, but the Information Panel must be updated to
353 // indicate an invalid directory.
354 markUrlAsInvalid();
355 }
356 }
357
358 void InformationPanel::cancelRequest()
359 {
360 delete m_folderStatJob;
361 m_folderStatJob = nullptr;
362
363 m_infoTimer->stop();
364 m_resetUrlTimer->stop();
365 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
366 // has the smallest interval (see init()), it is not possible that the exceeded timer
367 // would overwrite an information provided by a selection or hovering.
368
369 m_invalidUrlCandidate.clear();
370 m_urlCandidate.clear();
371 }
372
373 bool InformationPanel::isEqualToShownUrl(const QUrl &url) const
374 {
375 return m_shownUrl.matches(url, QUrl::StripTrailingSlash);
376 }
377
378 void InformationPanel::markUrlAsInvalid()
379 {
380 m_invalidUrlCandidate = m_shownUrl;
381 m_resetUrlTimer->start();
382 }
383
384 void InformationPanel::readSettings()
385 {
386 if (m_initialized) {
387 m_content->refreshPreview();
388 m_content->refreshMetaData();
389 }
390 }
391
392 void InformationPanel::init()
393 {
394 m_infoTimer = new QTimer(this);
395 m_infoTimer->setInterval(300);
396 m_infoTimer->setSingleShot(true);
397 connect(m_infoTimer, &QTimer::timeout, this, &InformationPanel::slotInfoTimeout);
398
399 m_urlChangedTimer = new QTimer(this);
400 m_urlChangedTimer->setInterval(200);
401 m_urlChangedTimer->setSingleShot(true);
402 connect(m_urlChangedTimer, &QTimer::timeout, this, &InformationPanel::showItemInfo);
403
404 m_resetUrlTimer = new QTimer(this);
405 m_resetUrlTimer->setInterval(1000);
406 m_resetUrlTimer->setSingleShot(true);
407 connect(m_resetUrlTimer, &QTimer::timeout, this, &InformationPanel::reset);
408
409 Q_ASSERT(m_urlChangedTimer->interval() < m_infoTimer->interval());
410 Q_ASSERT(m_urlChangedTimer->interval() < m_resetUrlTimer->interval());
411
412 #if HAVE_KDIRNOTIFY
413 org::kde::KDirNotify *dirNotify = new org::kde::KDirNotify(QString(), QString(), QDBusConnection::sessionBus(), this);
414 connect(dirNotify, &OrgKdeKDirNotifyInterface::FileRenamed, this, &InformationPanel::slotFileRenamed);
415 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesAdded, this, &InformationPanel::slotFilesAdded);
416 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesChanged, this, &InformationPanel::slotFilesChanged);
417 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesRemoved, this, &InformationPanel::slotFilesRemoved);
418 connect(dirNotify, &OrgKdeKDirNotifyInterface::enteredDirectory, this, &InformationPanel::slotEnteredDirectory);
419 connect(dirNotify, &OrgKdeKDirNotifyInterface::leftDirectory, this, &InformationPanel::slotLeftDirectory);
420 #endif
421
422 m_content = new InformationPanelContent(this);
423 connect(m_content, &InformationPanelContent::urlActivated, this, &InformationPanel::urlActivated);
424 connect(m_content, &InformationPanelContent::configurationFinished, this, [this]() {
425 m_inConfigurationMode = false;
426 });
427 connect(m_content, &InformationPanelContent::contextMenuRequested, this, &InformationPanel::showContextMenu);
428
429 QVBoxLayout *layout = new QVBoxLayout(this);
430 layout->setContentsMargins(0, 0, 0, 0);
431 layout->addWidget(m_content);
432
433 m_initialized = true;
434 }
435
436 #include "moc_informationpanel.cpp"