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