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