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