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