]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
Convert foreach to for loops
[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 const auto actions = customContextMenuActions();
189 for (QAction *action : actions) {
190 popup.addAction(action);
191 }
192
193 // Open the popup and adjust the settings for the
194 // selected action.
195 QAction* action = popup.exec(pos);
196 if (!action) {
197 return;
198 }
199
200 const bool isChecked = action->isChecked();
201 if (action == previewAction) {
202 m_content->setPreviewVisible(isChecked);
203 InformationPanelSettings::setPreviewsShown(isChecked);
204 } else if (action == configureAction) {
205 FileMetaDataConfigurationDialog* dialog = new FileMetaDataConfigurationDialog(this);
206 dialog->setDescription(i18nc("@label::textbox",
207 "Select which data should be shown in the information panel:"));
208 dialog->setItems(m_content->items());
209 dialog->setAttribute(Qt::WA_DeleteOnClose);
210 dialog->show();
211 connect(dialog, &FileMetaDataConfigurationDialog::destroyed, m_content, &InformationPanelContent::refreshMetaData);
212 }
213 if (action == dateformatAction) {
214 int dateFormat = static_cast<int>(isChecked ? Baloo::DateFormats::ShortFormat : Baloo::DateFormats::LongFormat);
215
216 InformationPanelSettings::setDateFormat(dateFormat);
217 m_content->refreshMetaData();
218 }
219 }
220
221 void InformationPanel::showItemInfo()
222 {
223 if (!isVisible()) {
224 return;
225 }
226
227 cancelRequest();
228
229 if (m_fileItem.isNull() && (m_selection.count() > 1)) {
230 // The information for a selection of items should be shown
231 m_content->showItems(m_selection);
232 } else {
233 // The information for exactly one item should be shown
234 KFileItem item;
235 if (!m_fileItem.isNull()) {
236 item = m_fileItem;
237 } else if (!m_selection.isEmpty()) {
238 Q_ASSERT(m_selection.count() == 1);
239 item = m_selection.first();
240 }
241
242 if (item.isNull()) {
243 // No item is hovered and no selection has been done: provide
244 // an item for the currently shown directory.
245 m_folderStatJob = KIO::stat(url(), KIO::HideProgressInfo);
246 if (m_folderStatJob->uiDelegate()) {
247 KJobWidgets::setWindow(m_folderStatJob, this);
248 }
249 connect(m_folderStatJob, &KIO::Job::result,
250 this, &InformationPanel::slotFolderStatFinished);
251 } else {
252 m_content->showItem(item);
253 }
254 }
255 }
256
257 void InformationPanel::slotFolderStatFinished(KJob* job)
258 {
259 m_folderStatJob = nullptr;
260 const KIO::UDSEntry entry = static_cast<KIO::StatJob*>(job)->statResult();
261 m_content->showItem(KFileItem(entry, m_shownUrl));
262 }
263
264 void InformationPanel::slotInfoTimeout()
265 {
266 m_shownUrl = m_urlCandidate;
267 m_urlCandidate.clear();
268 showItemInfo();
269 }
270
271 void InformationPanel::reset()
272 {
273 if (m_invalidUrlCandidate == m_shownUrl) {
274 m_invalidUrlCandidate = QUrl();
275
276 // The current URL is still invalid. Reset
277 // the content to show the directory URL.
278 m_selection.clear();
279 m_shownUrl = url();
280 m_fileItem = KFileItem();
281 showItemInfo();
282 }
283 }
284
285 void InformationPanel::slotFileRenamed(const QString& source, const QString& dest)
286 {
287 if (m_shownUrl == QUrl::fromLocalFile(source)) {
288 m_shownUrl = QUrl::fromLocalFile(dest);
289 m_fileItem = KFileItem(m_shownUrl);
290
291 if ((m_selection.count() == 1) && (m_selection[0].url() == QUrl::fromLocalFile(source))) {
292 m_selection[0] = m_fileItem;
293 // Implementation note: Updating the selection is only required if exactly one
294 // item is selected, as the name of the item is shown. If this should change
295 // in future: Before parsing the whole selection take care to test possible
296 // performance bottlenecks when renaming several hundreds of files.
297 }
298
299 showItemInfo();
300 }
301 }
302
303 void InformationPanel::slotFilesAdded(const QString& directory)
304 {
305 if (m_shownUrl == QUrl::fromLocalFile(directory)) {
306 // If the 'trash' icon changes because the trash has been emptied or got filled,
307 // the signal filesAdded("trash:/") will be emitted.
308 KFileItem item(QUrl::fromLocalFile(directory));
309 requestDelayedItemInfo(item);
310 }
311 }
312
313 void InformationPanel::slotFilesChanged(const QStringList& files)
314 {
315 for (const QString& fileName : files) {
316 if (m_shownUrl == QUrl::fromLocalFile(fileName)) {
317 showItemInfo();
318 break;
319 }
320 }
321 }
322
323 void InformationPanel::slotFilesRemoved(const QStringList& files)
324 {
325 for (const QString& fileName : files) {
326 if (m_shownUrl == QUrl::fromLocalFile(fileName)) {
327 // the currently shown item has been removed, show
328 // the parent directory as fallback
329 markUrlAsInvalid();
330 break;
331 }
332 }
333 }
334
335 void InformationPanel::slotEnteredDirectory(const QString& directory)
336 {
337 if (m_shownUrl == QUrl::fromLocalFile(directory)) {
338 KFileItem item(QUrl::fromLocalFile(directory));
339 requestDelayedItemInfo(item);
340 }
341 }
342
343 void InformationPanel::slotLeftDirectory(const QString& directory)
344 {
345 if (m_shownUrl == QUrl::fromLocalFile(directory)) {
346 // The signal 'leftDirectory' is also emitted when a media
347 // has been unmounted. In this case no directory change will be
348 // done in Dolphin, but the Information Panel must be updated to
349 // indicate an invalid directory.
350 markUrlAsInvalid();
351 }
352 }
353
354 void InformationPanel::cancelRequest()
355 {
356 delete m_folderStatJob;
357 m_folderStatJob = nullptr;
358
359 m_infoTimer->stop();
360 m_resetUrlTimer->stop();
361 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
362 // has the smallest interval (see init()), it is not possible that the exceeded timer
363 // would overwrite an information provided by a selection or hovering.
364
365 m_invalidUrlCandidate.clear();
366 m_urlCandidate.clear();
367 }
368
369 bool InformationPanel::isEqualToShownUrl(const QUrl& url) const
370 {
371 return m_shownUrl.matches(url, QUrl::StripTrailingSlash);
372 }
373
374 void InformationPanel::markUrlAsInvalid()
375 {
376 m_invalidUrlCandidate = m_shownUrl;
377 m_resetUrlTimer->start();
378 }
379
380 void InformationPanel::init()
381 {
382 m_infoTimer = new QTimer(this);
383 m_infoTimer->setInterval(300);
384 m_infoTimer->setSingleShot(true);
385 connect(m_infoTimer, &QTimer::timeout,
386 this, &InformationPanel::slotInfoTimeout);
387
388 m_urlChangedTimer = new QTimer(this);
389 m_urlChangedTimer->setInterval(200);
390 m_urlChangedTimer->setSingleShot(true);
391 connect(m_urlChangedTimer, &QTimer::timeout,
392 this, &InformationPanel::showItemInfo);
393
394 m_resetUrlTimer = new QTimer(this);
395 m_resetUrlTimer->setInterval(1000);
396 m_resetUrlTimer->setSingleShot(true);
397 connect(m_resetUrlTimer, &QTimer::timeout,
398 this, &InformationPanel::reset);
399
400 Q_ASSERT(m_urlChangedTimer->interval() < m_infoTimer->interval());
401 Q_ASSERT(m_urlChangedTimer->interval() < m_resetUrlTimer->interval());
402
403 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
404 QDBusConnection::sessionBus(), this);
405 connect(dirNotify, &OrgKdeKDirNotifyInterface::FileRenamed, this, &InformationPanel::slotFileRenamed);
406 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesAdded, this, &InformationPanel::slotFilesAdded);
407 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesChanged, this, &InformationPanel::slotFilesChanged);
408 connect(dirNotify, &OrgKdeKDirNotifyInterface::FilesRemoved, this, &InformationPanel::slotFilesRemoved);
409 connect(dirNotify, &OrgKdeKDirNotifyInterface::enteredDirectory, this, &InformationPanel::slotEnteredDirectory);
410 connect(dirNotify, &OrgKdeKDirNotifyInterface::leftDirectory, this, &InformationPanel::slotLeftDirectory);
411
412 m_content = new InformationPanelContent(this);
413 connect(m_content, &InformationPanelContent::urlActivated, this, &InformationPanel::urlActivated);
414
415 QVBoxLayout* layout = new QVBoxLayout(this);
416 layout->setContentsMargins(0, 0, 0, 0);
417 layout->addWidget(m_content);
418
419 m_initialized = true;
420 }
421