]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
Merge branch 'Applications/19.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* configureAction = popup.addAction(i18nc("@action:inmenu", "Configure..."));
180 configureAction->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
181 if (m_inConfigurationMode) {
182 configureAction->setEnabled(false);
183 }
184
185 QAction* dateformatAction = popup.addAction(i18nc("@action:inmenu", "Condensed Date"));
186 dateformatAction->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
187 dateformatAction->setCheckable(true);
188 dateformatAction->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat));
189
190 popup.addSeparator();
191 const auto actions = customContextMenuActions();
192 for (QAction *action : actions) {
193 popup.addAction(action);
194 }
195
196 // Open the popup and adjust the settings for the
197 // selected action.
198 QAction* action = popup.exec(pos);
199 if (!action) {
200 return;
201 }
202
203 const bool isChecked = action->isChecked();
204 if (action == previewAction) {
205 InformationPanelSettings::setPreviewsShown(isChecked);
206 m_content->refreshPreview();
207 } else if (action == configureAction) {
208 m_inConfigurationMode = true;
209 m_content->configureShownProperties();
210 }
211 if (action == dateformatAction) {
212 int dateFormat = static_cast<int>(isChecked ? Baloo::DateFormats::ShortFormat : Baloo::DateFormats::LongFormat);
213
214 InformationPanelSettings::setDateFormat(dateFormat);
215 m_content->refreshMetaData();
216 }
217 }
218
219 void InformationPanel::showItemInfo()
220 {
221 if (!isVisible()) {
222 return;
223 }
224
225 cancelRequest();
226
227 if (m_fileItem.isNull() && (m_selection.count() > 1)) {
228 // The information for a selection of items should be shown
229 m_content->showItems(m_selection);
230 } else {
231 // The information for exactly one item should be shown
232 KFileItem item;
233 if (!m_fileItem.isNull()) {
234 item = m_fileItem;
235 } else if (!m_selection.isEmpty()) {
236 Q_ASSERT(m_selection.count() == 1);
237 item = m_selection.first();
238 }
239
240 if (item.isNull()) {
241 // No item is hovered and no selection has been done: provide
242 // an item for the currently shown directory.
243 m_folderStatJob = KIO::stat(url(), KIO::HideProgressInfo);
244 if (m_folderStatJob->uiDelegate()) {
245 KJobWidgets::setWindow(m_folderStatJob, this);
246 }
247 connect(m_folderStatJob, &KIO::Job::result,
248 this, &InformationPanel::slotFolderStatFinished);
249 } else {
250 m_content->showItem(item);
251 }
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 m_fileItem = KFileItem();
279 showItemInfo();
280 }
281 }
282
283 void InformationPanel::slotFileRenamed(const QString& source, const QString& dest)
284 {
285 if (m_shownUrl == QUrl::fromLocalFile(source)) {
286 m_shownUrl = QUrl::fromLocalFile(dest);
287 m_fileItem = KFileItem(m_shownUrl);
288
289 if ((m_selection.count() == 1) && (m_selection[0].url() == QUrl::fromLocalFile(source))) {
290 m_selection[0] = m_fileItem;
291 // Implementation note: Updating the selection is only required if exactly one
292 // item is selected, as the name of the item is shown. If this should change
293 // in future: Before parsing the whole selection take care to test possible
294 // performance bottlenecks when renaming several hundreds of files.
295 }
296
297 showItemInfo();
298 }
299 }
300
301 void InformationPanel::slotFilesAdded(const QString& directory)
302 {
303 if (m_shownUrl == QUrl::fromLocalFile(directory)) {
304 // If the 'trash' icon changes because the trash has been emptied or got filled,
305 // the signal filesAdded("trash:/") will be emitted.
306 KFileItem item(QUrl::fromLocalFile(directory));
307 requestDelayedItemInfo(item);
308 }
309 }
310
311 void InformationPanel::slotFilesChanged(const QStringList& files)
312 {
313 for (const QString& fileName : files) {
314 if (m_shownUrl == QUrl::fromLocalFile(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::fromLocalFile(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 if (m_shownUrl == QUrl::fromLocalFile(directory)) {
336 KFileItem item(QUrl::fromLocalFile(directory));
337 requestDelayedItemInfo(item);
338 }
339 }
340
341 void InformationPanel::slotLeftDirectory(const QString& directory)
342 {
343 if (m_shownUrl == QUrl::fromLocalFile(directory)) {
344 // The signal 'leftDirectory' is also emitted when a media
345 // has been unmounted. In this case no directory change will be
346 // done in Dolphin, but the Information Panel must be updated to
347 // indicate an invalid directory.
348 markUrlAsInvalid();
349 }
350 }
351
352 void InformationPanel::cancelRequest()
353 {
354 delete m_folderStatJob;
355 m_folderStatJob = nullptr;
356
357 m_infoTimer->stop();
358 m_resetUrlTimer->stop();
359 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
360 // has the smallest interval (see init()), it is not possible that the exceeded timer
361 // would overwrite an information provided by a selection or hovering.
362
363 m_invalidUrlCandidate.clear();
364 m_urlCandidate.clear();
365 }
366
367 bool InformationPanel::isEqualToShownUrl(const QUrl& url) const
368 {
369 return m_shownUrl.matches(url, QUrl::StripTrailingSlash);
370 }
371
372 void InformationPanel::markUrlAsInvalid()
373 {
374 m_invalidUrlCandidate = m_shownUrl;
375 m_resetUrlTimer->start();
376 }
377
378 void InformationPanel::init()
379 {
380 m_infoTimer = new QTimer(this);
381 m_infoTimer->setInterval(300);
382 m_infoTimer->setSingleShot(true);
383 connect(m_infoTimer, &QTimer::timeout,
384 this, &InformationPanel::slotInfoTimeout);
385
386 m_urlChangedTimer = new QTimer(this);
387 m_urlChangedTimer->setInterval(200);
388 m_urlChangedTimer->setSingleShot(true);
389 connect(m_urlChangedTimer, &QTimer::timeout,
390 this, &InformationPanel::showItemInfo);
391
392 m_resetUrlTimer = new QTimer(this);
393 m_resetUrlTimer->setInterval(1000);
394 m_resetUrlTimer->setSingleShot(true);
395 connect(m_resetUrlTimer, &QTimer::timeout,
396 this, &InformationPanel::reset);
397
398 Q_ASSERT(m_urlChangedTimer->interval() < m_infoTimer->interval());
399 Q_ASSERT(m_urlChangedTimer->interval() < m_resetUrlTimer->interval());
400
401 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
402 QDBusConnection::sessionBus(), this);
403 connect(dirNotify, &OrgKdeKDirNotifyInterface::FileRenamed, this, &InformationPanel::slotFileRenamed);
404 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesAdded, this, &InformationPanel::slotFilesAdded);
405 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesChanged, this, &InformationPanel::slotFilesChanged);
406 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesRemoved, this, &InformationPanel::slotFilesRemoved);
407 connect(dirNotify, &OrgKdeKDirNotifyInterface::enteredDirectory, this, &InformationPanel::slotEnteredDirectory);
408 connect(dirNotify, &OrgKdeKDirNotifyInterface::leftDirectory, this, &InformationPanel::slotLeftDirectory);
409
410 m_content = new InformationPanelContent(this);
411 connect(m_content, &InformationPanelContent::urlActivated, this, &InformationPanel::urlActivated);
412 connect(m_content, &InformationPanelContent::configurationFinished, this, [this]() { m_inConfigurationMode = false; });
413
414 QVBoxLayout* layout = new QVBoxLayout(this);
415 layout->setContentsMargins(0, 0, 0, 0);
416 layout->addWidget(m_content);
417
418 m_initialized = true;
419 }
420