]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
Lock panels per default and allow to unlock them like in Amarok.
[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 cancelRequest();
100
101 if (item.isNull()) {
102 // The cursor is above the viewport. If files are selected,
103 // show information regarding the selection.
104 if (m_selection.size() > 0) {
105 m_fileItem = KFileItem();
106 m_infoTimer->start();
107 }
108 } else if (item.url().isValid() && !isEqualToShownUrl(item.url())) {
109 // The cursor is above an item that is not shown currently
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 // TODO: Move code from InformationPanelContent::configureSettings() here
171 m_content->configureSettings(customContextMenuActions());
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_resetUrlTimer->stop();
312 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
313 // has the smallest interval (see init()), it is not possible that the exceeded timer
314 // would overwrite an information provided by a selection or hovering.
315
316 m_invalidUrlCandidate.clear();
317 m_urlCandidate.clear();
318 }
319
320 bool InformationPanel::isEqualToShownUrl(const KUrl& url) const
321 {
322 return m_shownUrl.equals(url, KUrl::CompareWithoutTrailingSlash);
323 }
324
325 void InformationPanel::markUrlAsInvalid()
326 {
327 m_invalidUrlCandidate = m_shownUrl;
328 m_resetUrlTimer->start();
329 }
330
331 void InformationPanel::init()
332 {
333 m_infoTimer = new QTimer(this);
334 m_infoTimer->setInterval(300);
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(200);
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(1000);
347 m_resetUrlTimer->setSingleShot(true);
348 connect(m_resetUrlTimer, SIGNAL(timeout()),
349 this, SLOT(reset()));
350
351 Q_ASSERT(m_urlChangedTimer->interval() < m_infoTimer->interval());
352 Q_ASSERT(m_urlChangedTimer->interval() < m_resetUrlTimer->interval());
353
354 org::kde::KDirNotify* dirNotify = new org::kde::KDirNotify(QString(), QString(),
355 QDBusConnection::sessionBus(), this);
356 connect(dirNotify, SIGNAL(FileRenamed(QString, QString)), SLOT(slotFileRenamed(QString, QString)));
357 connect(dirNotify, SIGNAL(FilesAdded(QString)), SLOT(slotFilesAdded(QString)));
358 connect(dirNotify, SIGNAL(FilesChanged(QStringList)), SLOT(slotFilesChanged(QStringList)));
359 connect(dirNotify, SIGNAL(FilesRemoved(QStringList)), SLOT(slotFilesRemoved(QStringList)));
360 connect(dirNotify, SIGNAL(enteredDirectory(QString)), SLOT(slotEnteredDirectory(QString)));
361 connect(dirNotify, SIGNAL(leftDirectory(QString)), SLOT(slotLeftDirectory(QString)));
362
363 m_content = new InformationPanelContent(this);
364 connect(m_content, SIGNAL(urlActivated(KUrl)), this, SIGNAL(urlActivated(KUrl)));
365
366 QVBoxLayout* layout = new QVBoxLayout(this);
367 layout->addWidget(m_content);
368
369 m_initialized = true;
370 }
371
372 #include "informationpanel.moc"