]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
Merge branch 'release/20.08' into master
[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_content->showItem(item);
246 }
247 }
248 }
249
250 void InformationPanel::slotFolderStatFinished(KJob* job)
251 {
252 m_folderStatJob = nullptr;
253 const KIO::UDSEntry entry = static_cast<KIO::StatJob*>(job)->statResult();
254 m_content->showItem(KFileItem(entry, m_shownUrl));
255 }
256
257 void InformationPanel::slotInfoTimeout()
258 {
259 m_shownUrl = m_urlCandidate;
260 m_urlCandidate.clear();
261 showItemInfo();
262 }
263
264 void InformationPanel::reset()
265 {
266 if (m_invalidUrlCandidate == m_shownUrl) {
267 m_invalidUrlCandidate = QUrl();
268
269 // The current URL is still invalid. Reset
270 // the content to show the directory URL.
271 m_selection.clear();
272 m_shownUrl = url();
273 m_fileItem = KFileItem();
274 showItemInfo();
275 }
276 }
277
278 void InformationPanel::slotFileRenamed(const QString& source, const QString& dest)
279 {
280 if (m_shownUrl == QUrl::fromUserInput(source)) {
281 m_shownUrl = QUrl::fromUserInput(dest);
282 m_fileItem = KFileItem(m_shownUrl);
283
284 if ((m_selection.count() == 1) && (m_selection[0].url() == QUrl::fromLocalFile(source))) {
285 m_selection[0] = m_fileItem;
286 // Implementation note: Updating the selection is only required if exactly one
287 // item is selected, as the name of the item is shown. If this should change
288 // in future: Before parsing the whole selection take care to test possible
289 // performance bottlenecks when renaming several hundreds of files.
290 }
291
292 showItemInfo();
293 }
294 }
295
296 void InformationPanel::slotFilesAdded(const QString& directory)
297 {
298 if (m_shownUrl == QUrl::fromUserInput(directory)) {
299 // If the 'trash' icon changes because the trash has been emptied or got filled,
300 // the signal filesAdded("trash:/") will be emitted.
301 KFileItem item(QUrl::fromUserInput(directory));
302 requestDelayedItemInfo(item);
303 }
304 }
305
306 void InformationPanel::slotFilesChanged(const QStringList& files)
307 {
308 for (const QString& fileName : files) {
309 if (m_shownUrl == QUrl::fromUserInput(fileName)) {
310 showItemInfo();
311 break;
312 }
313 }
314 }
315
316 void InformationPanel::slotFilesRemoved(const QStringList& files)
317 {
318 for (const QString& fileName : files) {
319 if (m_shownUrl == QUrl::fromUserInput(fileName)) {
320 // the currently shown item has been removed, show
321 // the parent directory as fallback
322 markUrlAsInvalid();
323 break;
324 }
325 }
326 }
327
328 void InformationPanel::slotEnteredDirectory(const QString& directory)
329 {
330 if (m_shownUrl == QUrl::fromUserInput(directory)) {
331 KFileItem item(QUrl::fromUserInput(directory));
332 requestDelayedItemInfo(item);
333 }
334 }
335
336 void InformationPanel::slotLeftDirectory(const QString& directory)
337 {
338 if (m_shownUrl == QUrl::fromUserInput(directory)) {
339 // The signal 'leftDirectory' is also emitted when a media
340 // has been unmounted. In this case no directory change will be
341 // done in Dolphin, but the Information Panel must be updated to
342 // indicate an invalid directory.
343 markUrlAsInvalid();
344 }
345 }
346
347 void InformationPanel::cancelRequest()
348 {
349 delete m_folderStatJob;
350 m_folderStatJob = nullptr;
351
352 m_infoTimer->stop();
353 m_resetUrlTimer->stop();
354 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
355 // has the smallest interval (see init()), it is not possible that the exceeded timer
356 // would overwrite an information provided by a selection or hovering.
357
358 m_invalidUrlCandidate.clear();
359 m_urlCandidate.clear();
360 }
361
362 bool InformationPanel::isEqualToShownUrl(const QUrl& url) const
363 {
364 return m_shownUrl.matches(url, QUrl::StripTrailingSlash);
365 }
366
367 void InformationPanel::markUrlAsInvalid()
368 {
369 m_invalidUrlCandidate = m_shownUrl;
370 m_resetUrlTimer->start();
371 }
372
373 void InformationPanel::init()
374 {
375 m_infoTimer = new QTimer(this);
376 m_infoTimer->setInterval(300);
377 m_infoTimer->setSingleShot(true);
378 connect(m_infoTimer, &QTimer::timeout,
379 this, &InformationPanel::slotInfoTimeout);
380
381 m_urlChangedTimer = new QTimer(this);
382 m_urlChangedTimer->setInterval(200);
383 m_urlChangedTimer->setSingleShot(true);
384 connect(m_urlChangedTimer, &QTimer::timeout,
385 this, &InformationPanel::showItemInfo);
386
387 m_resetUrlTimer = new QTimer(this);
388 m_resetUrlTimer->setInterval(1000);
389 m_resetUrlTimer->setSingleShot(true);
390 connect(m_resetUrlTimer, &QTimer::timeout,
391 this, &InformationPanel::reset);
392
393 Q_ASSERT(m_urlChangedTimer->interval() < m_infoTimer->interval());
394 Q_ASSERT(m_urlChangedTimer->interval() < m_resetUrlTimer->interval());
395
396 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
397 QDBusConnection::sessionBus(), this);
398 connect(dirNotify, &OrgKdeKDirNotifyInterface::FileRenamed, this, &InformationPanel::slotFileRenamed);
399 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesAdded, this, &InformationPanel::slotFilesAdded);
400 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesChanged, this, &InformationPanel::slotFilesChanged);
401 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesRemoved, this, &InformationPanel::slotFilesRemoved);
402 connect(dirNotify, &OrgKdeKDirNotifyInterface::enteredDirectory, this, &InformationPanel::slotEnteredDirectory);
403 connect(dirNotify, &OrgKdeKDirNotifyInterface::leftDirectory, this, &InformationPanel::slotLeftDirectory);
404
405 m_content = new InformationPanelContent(this);
406 connect(m_content, &InformationPanelContent::urlActivated, this, &InformationPanel::urlActivated);
407 connect(m_content, &InformationPanelContent::configurationFinished, this, [this]() { m_inConfigurationMode = false; });
408
409 QVBoxLayout* layout = new QVBoxLayout(this);
410 layout->setContentsMargins(0, 0, 0, 0);
411 layout->addWidget(m_content);
412
413 m_initialized = true;
414 }
415