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