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