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