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