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