]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
Internal cleanup for panels: Let the panel-implementations decide whether they accept...
[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::setSelection(const KFileItemList& selection)
56 {
57 if (!isVisible()) {
58 return;
59 }
60
61 if ((selection.count() == 0) && (m_selection.count() == 0)) {
62 // The selection has not really changed, only the current index.
63 // QItemSelectionModel emits a signal in this case and it is less
64 // expensive doing the check this way instead of patching
65 // DolphinView::emitSelectionChanged().
66 return;
67 }
68
69 m_selection = selection;
70 m_fileItem = KFileItem();
71
72 const int count = selection.count();
73 if (count == 0) {
74 if (!isEqualToShownUrl(url())) {
75 m_shownUrl = url();
76 showItemInfo();
77 }
78 } else {
79 if ((count == 1) && !selection.first().url().isEmpty()) {
80 m_urlCandidate = selection.first().url();
81 }
82 m_infoTimer->start();
83 }
84 }
85
86 void InformationPanel::requestDelayedItemInfo(const KFileItem& item)
87 {
88 if (!isVisible() || (item.isNull() && m_fileItem.isNull())) {
89 return;
90 }
91
92 cancelRequest();
93
94 m_fileItem = KFileItem();
95 if (item.isNull()) {
96 // The cursor is above the viewport. If files are selected,
97 // show information regarding the selection.
98 if (m_selection.size() > 0) {
99 m_infoTimer->start();
100 }
101 } else {
102 const KUrl url = item.url();
103 if (url.isValid() && !isEqualToShownUrl(url)) {
104 m_urlCandidate = item.url();
105 m_fileItem = item;
106 m_infoTimer->start();
107 }
108 }
109 }
110
111 bool InformationPanel::urlChanged()
112 {
113 if (!url().isValid() || isEqualToShownUrl(url())) {
114 return false;
115 }
116
117 m_shownUrl = url();
118 if (isVisible()) {
119 cancelRequest();
120 // Update the content with a delay. This gives
121 // the directory lister the chance to show the content
122 // before expensive operations are done to show
123 // meta information.
124 m_urlChangedTimer->start();
125 }
126
127 return true;
128 }
129
130 void InformationPanel::showEvent(QShowEvent* event)
131 {
132 Panel::showEvent(event);
133 if (!event->spontaneous()) {
134 if (!m_initialized) {
135 // do a delayed initialization so that no performance
136 // penalty is given when Dolphin is started with a closed
137 // Information Panel
138 init();
139 }
140 showItemInfo();
141 }
142 }
143
144 void InformationPanel::resizeEvent(QResizeEvent* event)
145 {
146 if (isVisible()) {
147 m_urlCandidate = m_shownUrl;
148 m_infoTimer->start();
149 }
150 Panel::resizeEvent(event);
151 }
152
153 void InformationPanel::contextMenuEvent(QContextMenuEvent* event)
154 {
155 m_content->configureSettings();
156 Panel::contextMenuEvent(event);
157 }
158
159 void InformationPanel::showItemInfo()
160 {
161 if (!isVisible()) {
162 return;
163 }
164
165 cancelRequest();
166
167 if (showMultipleSelectionInfo()) {
168 m_content->showItems(m_selection);
169 m_shownUrl = KUrl();
170 } else {
171 KFileItem item;
172 if (!m_fileItem.isNull()) {
173 item = m_fileItem;
174 } else if (!m_selection.isEmpty()) {
175 Q_ASSERT(m_selection.count() == 1);
176 item = m_selection.first();
177 }
178
179 if (item.isNull()) {
180 // no item is hovered and no selection has been done: provide
181 // an item for the directory represented by m_shownUrl
182 m_folderStatJob = KIO::stat(m_shownUrl, KIO::HideProgressInfo);
183 connect(m_folderStatJob, SIGNAL(result(KJob*)),
184 this, SLOT(slotFolderStatFinished(KJob*)));
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"