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