]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
Add clang-format and format code as in Frameworks
[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/Job>
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 m_infoTimer->start();
66 }
67 }
68
69 void InformationPanel::requestDelayedItemInfo(const KFileItem &item)
70 {
71 if (!isVisible()) {
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 *configureAction = popup.addAction(i18nc("@action:inmenu", "Configure..."));
164 configureAction->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
165 if (m_inConfigurationMode) {
166 configureAction->setEnabled(false);
167 }
168
169 QAction *dateformatAction = popup.addAction(i18nc("@action:inmenu", "Condensed Date"));
170 dateformatAction->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
171 dateformatAction->setCheckable(true);
172 dateformatAction->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat));
173
174 popup.addSeparator();
175 const auto actions = customContextMenuActions();
176 for (QAction *action : actions) {
177 popup.addAction(action);
178 }
179
180 // Open the popup and adjust the settings for the
181 // selected action.
182 QAction *action = popup.exec(pos);
183 if (!action) {
184 return;
185 }
186
187 const bool isChecked = action->isChecked();
188 if (action == previewAction) {
189 InformationPanelSettings::setPreviewsShown(isChecked);
190 m_content->refreshPreview();
191 } else if (action == configureAction) {
192 m_inConfigurationMode = true;
193 m_content->configureShownProperties();
194 }
195 if (action == dateformatAction) {
196 int dateFormat = static_cast<int>(isChecked ? Baloo::DateFormats::ShortFormat : Baloo::DateFormats::LongFormat);
197
198 InformationPanelSettings::setDateFormat(dateFormat);
199 m_content->refreshMetaData();
200 } else if (action == previewAutoPlayAction) {
201 InformationPanelSettings::setPreviewsAutoPlay(isChecked);
202 m_content->setPreviewAutoPlay(isChecked);
203 }
204 }
205
206 void InformationPanel::showItemInfo()
207 {
208 if (!isVisible()) {
209 return;
210 }
211
212 cancelRequest();
213 //qDebug() << "showItemInfo" << m_fileItem;
214
215 if (m_hoveredItem.isNull() && (m_selection.count() > 1)) {
216 // The information for a selection of items should be shown
217 m_content->showItems(m_selection);
218 } else {
219 // The information for exactly one item should be shown
220 KFileItem item;
221 if (!m_hoveredItem.isNull()) {
222 item = m_hoveredItem;
223 } else if (!m_selection.isEmpty()) {
224 Q_ASSERT(m_selection.count() == 1);
225 item = m_selection.first();
226 }
227
228 if (!item.isNull()) {
229 m_shownUrl = item.url();
230 m_content->showItem(item);
231 return;
232 }
233
234 // No item is hovered and no selection has been done: provide
235 // an item for the currently shown directory.
236 m_shownUrl = url();
237 m_folderStatJob = KIO::statDetails(m_shownUrl, KIO::StatJob::SourceSide, KIO::StatDefaultDetails | KIO::StatRecursiveSize, KIO::HideProgressInfo);
238 if (m_folderStatJob->uiDelegate()) {
239 KJobWidgets::setWindow(m_folderStatJob, this);
240 }
241 connect(m_folderStatJob, &KIO::Job::result, this, &InformationPanel::slotFolderStatFinished);
242 }
243 }
244
245 void InformationPanel::slotFolderStatFinished(KJob *job)
246 {
247 m_folderStatJob = nullptr;
248 const KIO::UDSEntry entry = static_cast<KIO::StatJob *>(job)->statResult();
249 m_content->showItem(KFileItem(entry, m_shownUrl));
250 }
251
252 void InformationPanel::slotInfoTimeout()
253 {
254 m_shownUrl = m_urlCandidate;
255 m_urlCandidate.clear();
256 showItemInfo();
257 }
258
259 void InformationPanel::reset()
260 {
261 if (m_invalidUrlCandidate == m_shownUrl) {
262 m_invalidUrlCandidate = QUrl();
263
264 // The current URL is still invalid. Reset
265 // the content to show the directory URL.
266 m_selection.clear();
267 m_shownUrl = url();
268 showItemInfo();
269 }
270 }
271
272 void InformationPanel::slotFileRenamed(const QString &source, const QString &dest)
273 {
274 auto sourceUrl = QUrl::fromUserInput(source);
275 if (m_shownUrl == sourceUrl) {
276 auto destUrl = QUrl::fromUserInput(dest);
277
278 if ((m_selection.count() == 1) && (m_selection[0].url() == sourceUrl)) {
279 m_selection[0] = KFileItem(destUrl);
280 // Implementation note: Updating the selection is only required if exactly one
281 // item is selected, as the name of the item is shown. If this should change
282 // in future: Before parsing the whole selection take care to test possible
283 // performance bottlenecks when renaming several hundreds of files.
284 } else {
285 m_hoveredItem = KFileItem(destUrl);
286 }
287
288 showItemInfo();
289 }
290 }
291
292 void InformationPanel::slotFilesAdded(const QString &directory)
293 {
294 if (m_shownUrl == QUrl::fromUserInput(directory)) {
295 // If the 'trash' icon changes because the trash has been emptied or got filled,
296 // the signal filesAdded("trash:/") will be emitted.
297 requestDelayedItemInfo(KFileItem());
298 }
299 }
300
301 void InformationPanel::slotFilesItemChanged(const KFileItemList &changedFileItems)
302 {
303 const auto item = changedFileItems.findByUrl(m_shownUrl);
304 if (!item.isNull()) {
305 showItemInfo();
306 }
307 }
308
309 void InformationPanel::slotFilesChanged(const QStringList &files)
310 {
311 for (const QString &fileName : files) {
312 if (m_shownUrl == QUrl::fromUserInput(fileName)) {
313 showItemInfo();
314 break;
315 }
316 }
317 }
318
319 void InformationPanel::slotFilesRemoved(const QStringList &files)
320 {
321 for (const QString &fileName : files) {
322 if (m_shownUrl == QUrl::fromUserInput(fileName)) {
323 // the currently shown item has been removed, show
324 // the parent directory as fallback
325 markUrlAsInvalid();
326 break;
327 }
328 }
329 }
330
331 void InformationPanel::slotEnteredDirectory(const QString &directory)
332 {
333 Q_UNUSED(directory)
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, this, &InformationPanel::slotInfoTimeout);
379
380 m_urlChangedTimer = new QTimer(this);
381 m_urlChangedTimer->setInterval(200);
382 m_urlChangedTimer->setSingleShot(true);
383 connect(m_urlChangedTimer, &QTimer::timeout, this, &InformationPanel::showItemInfo);
384
385 m_resetUrlTimer = new QTimer(this);
386 m_resetUrlTimer->setInterval(1000);
387 m_resetUrlTimer->setSingleShot(true);
388 connect(m_resetUrlTimer, &QTimer::timeout, this, &InformationPanel::reset);
389
390 Q_ASSERT(m_urlChangedTimer->interval() < m_infoTimer->interval());
391 Q_ASSERT(m_urlChangedTimer->interval() < m_resetUrlTimer->interval());
392
393 org::kde::KDirNotify *dirNotify = new org::kde::KDirNotify(QString(), QString(), QDBusConnection::sessionBus(), this);
394 connect(dirNotify, &OrgKdeKDirNotifyInterface::FileRenamed, this, &InformationPanel::slotFileRenamed);
395 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesAdded, this, &InformationPanel::slotFilesAdded);
396 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesChanged, this, &InformationPanel::slotFilesChanged);
397 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesRemoved, this, &InformationPanel::slotFilesRemoved);
398 connect(dirNotify, &OrgKdeKDirNotifyInterface::enteredDirectory, this, &InformationPanel::slotEnteredDirectory);
399 connect(dirNotify, &OrgKdeKDirNotifyInterface::leftDirectory, this, &InformationPanel::slotLeftDirectory);
400
401 m_content = new InformationPanelContent(this);
402 connect(m_content, &InformationPanelContent::urlActivated, this, &InformationPanel::urlActivated);
403 connect(m_content, &InformationPanelContent::configurationFinished, this, [this]() {
404 m_inConfigurationMode = false;
405 });
406 connect(m_content, &InformationPanelContent::contextMenuRequested, this, &InformationPanel::showContextMenu);
407
408 QVBoxLayout *layout = new QVBoxLayout(this);
409 layout->setContentsMargins(0, 0, 0, 0);
410 layout->addWidget(m_content);
411
412 m_initialized = true;
413 }