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