]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
Reset the file-item when the viewport is entered and files are selected. Otherwise...
[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_fileItem = KFileItem();
105 m_infoTimer->start();
106 }
107 } else if (item.url().isValid() && !isEqualToShownUrl(item.url())) {
108 // The cursor is above an item that is not shown currently
109 cancelRequest();
110
111 m_urlCandidate = item.url();
112 m_fileItem = item;
113 m_infoTimer->start();
114 }
115 }
116
117 bool InformationPanel::urlChanged()
118 {
119 if (!url().isValid()) {
120 return false;
121 }
122
123 if (!isVisible()) {
124 return true;
125 }
126
127 cancelRequest();
128 m_selection.clear();
129
130 if (!isEqualToShownUrl(url())) {
131 m_shownUrl = url();
132 m_fileItem = KFileItem();
133
134 // Update the content with a delay. This gives
135 // the directory lister the chance to show the content
136 // before expensive operations are done to show
137 // meta information.
138 m_urlChangedTimer->start();
139 }
140
141 return true;
142 }
143
144 void InformationPanel::showEvent(QShowEvent* event)
145 {
146 Panel::showEvent(event);
147 if (!event->spontaneous()) {
148 if (!m_initialized) {
149 // do a delayed initialization so that no performance
150 // penalty is given when Dolphin is started with a closed
151 // Information Panel
152 init();
153 }
154
155 m_shownUrl = url();
156 showItemInfo();
157 }
158 }
159
160 void InformationPanel::resizeEvent(QResizeEvent* event)
161 {
162 if (isVisible()) {
163 m_urlCandidate = m_shownUrl;
164 m_infoTimer->start();
165 }
166 Panel::resizeEvent(event);
167 }
168
169 void InformationPanel::contextMenuEvent(QContextMenuEvent* event)
170 {
171 m_content->configureSettings();
172 Panel::contextMenuEvent(event);
173 }
174
175 void InformationPanel::showItemInfo()
176 {
177 if (!isVisible()) {
178 return;
179 }
180
181 cancelRequest();
182
183 if (m_fileItem.isNull() && (m_selection.count() > 1)) {
184 // The information for a selection of items should be shown
185 m_content->showItems(m_selection);
186 } else {
187 // The information for exactly one item should be shown
188 KFileItem item;
189 if (!m_fileItem.isNull()) {
190 item = m_fileItem;
191 } else if (!m_selection.isEmpty()) {
192 Q_ASSERT(m_selection.count() == 1);
193 item = m_selection.first();
194 }
195
196 if (item.isNull()) {
197 // No item is hovered and no selection has been done: provide
198 // an item for the currently shown directory.
199 m_folderStatJob = KIO::stat(url(), KIO::HideProgressInfo);
200 connect(m_folderStatJob, SIGNAL(result(KJob*)),
201 this, SLOT(slotFolderStatFinished(KJob*)));
202 } else {
203 m_content->showItem(item);
204 }
205 }
206 }
207
208 void InformationPanel::slotFolderStatFinished(KJob* job)
209 {
210 m_folderStatJob = 0;
211 const KIO::UDSEntry entry = static_cast<KIO::StatJob*>(job)->statResult();
212 m_content->showItem(KFileItem(entry, m_shownUrl));
213 }
214
215 void InformationPanel::slotInfoTimeout()
216 {
217 m_shownUrl = m_urlCandidate;
218 m_urlCandidate.clear();
219 showItemInfo();
220 }
221
222 void InformationPanel::reset()
223 {
224 if (m_invalidUrlCandidate == m_shownUrl) {
225 m_invalidUrlCandidate = KUrl();
226
227 // The current URL is still invalid. Reset
228 // the content to show the directory URL.
229 m_selection.clear();
230 m_shownUrl = url();
231 m_fileItem = KFileItem();
232 showItemInfo();
233 }
234 }
235
236 void InformationPanel::slotFileRenamed(const QString& source, const QString& dest)
237 {
238 if (m_shownUrl == KUrl(source)) {
239 m_shownUrl = KUrl(dest);
240 m_fileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
241
242 if ((m_selection.count() == 1) && (m_selection[0].url() == KUrl(source))) {
243 m_selection[0] = m_fileItem;
244 // Implementation note: Updating the selection is only required if exactly one
245 // item is selected, as the name of the item is shown. If this should change
246 // in future: Before parsing the whole selection take care to test possible
247 // performance bottlenecks when renaming several hundreds of files.
248 }
249
250 showItemInfo();
251 }
252 }
253
254 void InformationPanel::slotFilesAdded(const QString& directory)
255 {
256 if (m_shownUrl == KUrl(directory)) {
257 // If the 'trash' icon changes because the trash has been emptied or got filled,
258 // the signal filesAdded("trash:/") will be emitted.
259 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
260 requestDelayedItemInfo(item);
261 }
262 }
263
264 void InformationPanel::slotFilesChanged(const QStringList& files)
265 {
266 foreach (const QString& fileName, files) {
267 if (m_shownUrl == KUrl(fileName)) {
268 showItemInfo();
269 break;
270 }
271 }
272 }
273
274 void InformationPanel::slotFilesRemoved(const QStringList& files)
275 {
276 foreach (const QString& fileName, files) {
277 if (m_shownUrl == KUrl(fileName)) {
278 // the currently shown item has been removed, show
279 // the parent directory as fallback
280 markUrlAsInvalid();
281 break;
282 }
283 }
284 }
285
286 void InformationPanel::slotEnteredDirectory(const QString& directory)
287 {
288 if (m_shownUrl == KUrl(directory)) {
289 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
290 requestDelayedItemInfo(item);
291 }
292 }
293
294 void InformationPanel::slotLeftDirectory(const QString& directory)
295 {
296 if (m_shownUrl == KUrl(directory)) {
297 // The signal 'leftDirectory' is also emitted when a media
298 // has been unmounted. In this case no directory change will be
299 // done in Dolphin, but the Information Panel must be updated to
300 // indicate an invalid directory.
301 markUrlAsInvalid();
302 }
303 }
304
305 void InformationPanel::cancelRequest()
306 {
307 delete m_folderStatJob;
308 m_folderStatJob = 0;
309
310 m_infoTimer->stop();
311 m_urlChangedTimer->stop();
312 m_resetUrlTimer->stop();
313
314 m_invalidUrlCandidate.clear();
315 m_urlCandidate.clear();
316 }
317
318 bool InformationPanel::isEqualToShownUrl(const KUrl& url) const
319 {
320 return m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash);
321 }
322
323 void InformationPanel::markUrlAsInvalid()
324 {
325 m_invalidUrlCandidate = m_shownUrl;
326 m_resetUrlTimer->start();
327 }
328
329 void InformationPanel::init()
330 {
331 const int defaultDelay = 300;
332
333 m_infoTimer = new QTimer(this);
334 m_infoTimer->setInterval(defaultDelay);
335 m_infoTimer->setSingleShot(true);
336 connect(m_infoTimer, SIGNAL(timeout()),
337 this, SLOT(slotInfoTimeout()));
338
339 m_urlChangedTimer = new QTimer(this);
340 m_urlChangedTimer->setInterval(defaultDelay);
341 m_urlChangedTimer->setSingleShot(true);
342 connect(m_urlChangedTimer, SIGNAL(timeout()),
343 this, SLOT(showItemInfo()));
344
345 m_resetUrlTimer = new QTimer(this);
346 m_resetUrlTimer->setInterval(defaultDelay * 3);
347 m_resetUrlTimer->setSingleShot(true);
348 connect(m_resetUrlTimer, SIGNAL(timeout()),
349 this, SLOT(reset()));
350
351 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
352 QDBusConnection::sessionBus(), this);
353 connect(dirNotify, SIGNAL(FileRenamed(QString, QString)), SLOT(slotFileRenamed(QString, QString)));
354 connect(dirNotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
355 connect(dirNotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
356 connect(dirNotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
357 connect(dirNotify, SIGNAL(enteredDirectory(QString)), SLOT(slotEnteredDirectory(QString)));
358 connect(dirNotify, SIGNAL(leftDirectory(QString)), SLOT(slotLeftDirectory(QString)));
359
360 m_content = new InformationPanelContent(this);
361 connect(m_content, SIGNAL(urlActivated(KUrl)), this, SIGNAL(urlActivated(KUrl)));
362
363 QVBoxLayout* layout = new QVBoxLayout(this);
364 layout->addWidget(m_content);
365
366 m_initialized = true;
367 }
368
369 #include "informationpanel.moc"