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