]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
Fix issue that no content is shown in the Information Panel on startup
[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 return;
60 }
61
62 m_shownUrl = url;
63 if (isVisible()) {
64 cancelRequest();
65 // Update the content with a delay. This gives
66 // the directory lister the chance to show the content
67 // before expensive operations are done to show
68 // meta information.
69 m_urlChangedTimer->start();
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() || (item.isNull() && m_fileItem.isNull())) {
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 } else {
185 m_content->showItem(item);
186 }
187 }
188 }
189
190 void InformationPanel::slotFolderStatFinished(KJob* job)
191 {
192 m_folderStatJob = 0;
193 const KIO::UDSEntry entry = static_cast<KIO::StatJob*>(job)->statResult();
194 m_content->showItem(KFileItem(entry, m_shownUrl));
195 }
196
197 void InformationPanel::slotInfoTimeout()
198 {
199 m_shownUrl = m_urlCandidate;
200 showItemInfo();
201 }
202
203 void InformationPanel::reset()
204 {
205 if (m_invalidUrlCandidate == m_shownUrl) {
206 m_invalidUrlCandidate = KUrl();
207
208 // The current URL is still invalid. Reset
209 // the content to show the directory URL.
210 m_selection.clear();
211 m_shownUrl = url();
212 m_fileItem = KFileItem();
213 showItemInfo();
214 }
215 }
216
217 void InformationPanel::slotFileRenamed(const QString& source, const QString& dest)
218 {
219 if (m_shownUrl == KUrl(source)) {
220 m_shownUrl = KUrl(dest);
221 m_fileItem = KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_shownUrl);
222
223 if ((m_selection.count() == 1) && (m_selection[0].url() == KUrl(source))) {
224 m_selection[0] = m_fileItem;
225 // Implementation note: Updating the selection is only required if exactly one
226 // item is selected, as the name of the item is shown. If this should change
227 // in future: Before parsing the whole selection take care to test possible
228 // performance bottlenecks when renaming several hundreds of files.
229 }
230
231 showItemInfo();
232 }
233 }
234
235 void InformationPanel::slotFilesAdded(const QString& directory)
236 {
237 if (m_shownUrl == KUrl(directory)) {
238 // If the 'trash' icon changes because the trash has been emptied or got filled,
239 // the signal filesAdded("trash:/") will be emitted.
240 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
241 requestDelayedItemInfo(item);
242 }
243 }
244
245 void InformationPanel::slotFilesChanged(const QStringList& files)
246 {
247 foreach (const QString& fileName, files) {
248 if (m_shownUrl == KUrl(fileName)) {
249 showItemInfo();
250 break;
251 }
252 }
253 }
254
255 void InformationPanel::slotFilesRemoved(const QStringList& files)
256 {
257 foreach (const QString& fileName, files) {
258 if (m_shownUrl == KUrl(fileName)) {
259 // the currently shown item has been removed, show
260 // the parent directory as fallback
261 markUrlAsInvalid();
262 break;
263 }
264 }
265 }
266
267 void InformationPanel::slotEnteredDirectory(const QString& directory)
268 {
269 if (m_shownUrl == KUrl(directory)) {
270 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, KUrl(directory));
271 requestDelayedItemInfo(item);
272 }
273 }
274
275 void InformationPanel::slotLeftDirectory(const QString& directory)
276 {
277 if (m_shownUrl == KUrl(directory)) {
278 // The signal 'leftDirectory' is also emitted when a media
279 // has been unmounted. In this case no directory change will be
280 // done in Dolphin, but the Information Panel must be updated to
281 // indicate an invalid directory.
282 markUrlAsInvalid();
283 }
284 }
285
286 void InformationPanel::cancelRequest()
287 {
288 delete m_folderStatJob;
289 m_folderStatJob = 0;
290 m_infoTimer->stop();
291 }
292
293 bool InformationPanel::showMultipleSelectionInfo() const
294 {
295 return m_fileItem.isNull() && (m_selection.count() > 1);
296 }
297
298 bool InformationPanel::isEqualToShownUrl(const KUrl& url) const
299 {
300 return m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash);
301 }
302
303 void InformationPanel::markUrlAsInvalid()
304 {
305 m_invalidUrlCandidate = m_shownUrl;
306 m_resetUrlTimer->start();
307 }
308
309 void InformationPanel::init()
310 {
311 const int defaultDelay = 300;
312
313 m_infoTimer = new QTimer(this);
314 m_infoTimer->setInterval(defaultDelay);
315 m_infoTimer->setSingleShot(true);
316 connect(m_infoTimer, SIGNAL(timeout()),
317 this, SLOT(slotInfoTimeout()));
318
319 m_urlChangedTimer = new QTimer(this);
320 m_urlChangedTimer->setInterval(defaultDelay);
321 m_urlChangedTimer->setSingleShot(true);
322 connect(m_urlChangedTimer, SIGNAL(timeout()),
323 this, SLOT(showItemInfo()));
324
325 m_resetUrlTimer = new QTimer(this);
326 m_resetUrlTimer->setInterval(defaultDelay * 3);
327 m_resetUrlTimer->setSingleShot(true);
328 connect(m_resetUrlTimer, SIGNAL(timeout()),
329 this, SLOT(reset()));
330
331 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
332 QDBusConnection::sessionBus(), this);
333 connect(dirNotify, SIGNAL(FileRenamed(QString, QString)), SLOT(slotFileRenamed(QString, QString)));
334 connect(dirNotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
335 connect(dirNotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
336 connect(dirNotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
337 connect(dirNotify, SIGNAL(enteredDirectory(QString)), SLOT(slotEnteredDirectory(QString)));
338 connect(dirNotify, SIGNAL(leftDirectory(QString)), SLOT(slotLeftDirectory(QString)));
339
340 m_content = new InformationPanelContent(this);
341 connect(m_content, SIGNAL(urlActivated(KUrl)), this, SIGNAL(urlActivated(KUrl)));
342
343 QVBoxLayout* layout = new QVBoxLayout(this);
344 layout->addWidget(m_content);
345
346 m_initialized = true;
347 }
348
349 #include "informationpanel.moc"