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