1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "informationpanel.h"
22 #include "informationpanelcontent.h"
24 #include <kdirnotify.h>
25 #include <QApplication>
27 #include <QVBoxLayout>
29 InformationPanel::InformationPanel(QWidget
* parent
) :
37 m_invalidUrlCandidate(),
45 InformationPanel::~InformationPanel()
49 void InformationPanel::setSelection(const KFileItemList
& selection
)
55 if ((selection
.count() == 0) && (m_selection
.count() == 0)) {
56 // The selection has not really changed, only the current index.
57 // QItemSelectionModel emits a signal in this case and it is less
58 // expensive doing the check this way instead of patching
59 // DolphinView::emitSelectionChanged().
63 m_selection
= selection
;
64 m_fileItem
= KFileItem();
66 const int count
= selection
.count();
68 if (!isEqualToShownUrl(url())) {
73 if ((count
== 1) && !selection
.first().url().isEmpty()) {
74 m_urlCandidate
= selection
.first().url();
80 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
82 if (!isVisible() || (item
.isNull() && m_fileItem
.isNull())) {
86 if (QApplication::mouseButtons() & Qt::LeftButton
) {
87 // Ignore the request of an item information when a rubberband
88 // selection is ongoing.
95 // The cursor is above the viewport. If files are selected,
96 // show information regarding the selection.
97 if (m_selection
.size() > 0) {
98 m_fileItem
= KFileItem();
101 } else if (item
.url().isValid() && !isEqualToShownUrl(item
.url())) {
102 // The cursor is above an item that is not shown currently
103 m_urlCandidate
= item
.url();
105 m_infoTimer
->start();
109 bool InformationPanel::urlChanged()
111 if (!url().isValid()) {
122 if (!isEqualToShownUrl(url())) {
124 m_fileItem
= KFileItem();
126 // Update the content with a delay. This gives
127 // the directory lister the chance to show the content
128 // before expensive operations are done to show
130 m_urlChangedTimer
->start();
136 void InformationPanel::showEvent(QShowEvent
* event
)
138 Panel::showEvent(event
);
139 if (!event
->spontaneous()) {
140 if (!m_initialized
) {
141 // do a delayed initialization so that no performance
142 // penalty is given when Dolphin is started with a closed
152 void InformationPanel::resizeEvent(QResizeEvent
* event
)
155 m_urlCandidate
= m_shownUrl
;
156 m_infoTimer
->start();
158 Panel::resizeEvent(event
);
161 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
163 // TODO: Move code from InformationPanelContent::configureSettings() here
164 m_content
->configureSettings(customContextMenuActions());
165 Panel::contextMenuEvent(event
);
168 void InformationPanel::showItemInfo()
176 if (m_fileItem
.isNull() && (m_selection
.count() > 1)) {
177 // The information for a selection of items should be shown
178 m_content
->showItems(m_selection
);
180 // The information for exactly one item should be shown
182 if (!m_fileItem
.isNull()) {
184 } else if (!m_selection
.isEmpty()) {
185 Q_ASSERT(m_selection
.count() == 1);
186 item
= m_selection
.first();
190 // No item is hovered and no selection has been done: provide
191 // an item for the currently shown directory.
192 m_folderStatJob
= KIO::stat(url(), KIO::HideProgressInfo
);
193 connect(m_folderStatJob
, SIGNAL(result(KJob
*)),
194 this, SLOT(slotFolderStatFinished(KJob
*)));
196 m_content
->showItem(item
);
201 void InformationPanel::slotFolderStatFinished(KJob
* job
)
204 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
205 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
208 void InformationPanel::slotInfoTimeout()
210 m_shownUrl
= m_urlCandidate
;
211 m_urlCandidate
.clear();
215 void InformationPanel::reset()
217 if (m_invalidUrlCandidate
== m_shownUrl
) {
218 m_invalidUrlCandidate
= KUrl();
220 // The current URL is still invalid. Reset
221 // the content to show the directory URL.
224 m_fileItem
= KFileItem();
229 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
231 if (m_shownUrl
== KUrl(source
)) {
232 m_shownUrl
= KUrl(dest
);
233 m_fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
235 if ((m_selection
.count() == 1) && (m_selection
[0].url() == KUrl(source
))) {
236 m_selection
[0] = m_fileItem
;
237 // Implementation note: Updating the selection is only required if exactly one
238 // item is selected, as the name of the item is shown. If this should change
239 // in future: Before parsing the whole selection take care to test possible
240 // performance bottlenecks when renaming several hundreds of files.
247 void InformationPanel::slotFilesAdded(const QString
& directory
)
249 if (m_shownUrl
== KUrl(directory
)) {
250 // If the 'trash' icon changes because the trash has been emptied or got filled,
251 // the signal filesAdded("trash:/") will be emitted.
252 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
253 requestDelayedItemInfo(item
);
257 void InformationPanel::slotFilesChanged(const QStringList
& files
)
259 foreach (const QString
& fileName
, files
) {
260 if (m_shownUrl
== KUrl(fileName
)) {
267 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
269 foreach (const QString
& fileName
, files
) {
270 if (m_shownUrl
== KUrl(fileName
)) {
271 // the currently shown item has been removed, show
272 // the parent directory as fallback
279 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
281 if (m_shownUrl
== KUrl(directory
)) {
282 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
283 requestDelayedItemInfo(item
);
287 void InformationPanel::slotLeftDirectory(const QString
& directory
)
289 if (m_shownUrl
== KUrl(directory
)) {
290 // The signal 'leftDirectory' is also emitted when a media
291 // has been unmounted. In this case no directory change will be
292 // done in Dolphin, but the Information Panel must be updated to
293 // indicate an invalid directory.
298 void InformationPanel::cancelRequest()
300 delete m_folderStatJob
;
304 m_resetUrlTimer
->stop();
305 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
306 // has the smallest interval (see init()), it is not possible that the exceeded timer
307 // would overwrite an information provided by a selection or hovering.
309 m_invalidUrlCandidate
.clear();
310 m_urlCandidate
.clear();
313 bool InformationPanel::isEqualToShownUrl(const KUrl
& url
) const
315 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
318 void InformationPanel::markUrlAsInvalid()
320 m_invalidUrlCandidate
= m_shownUrl
;
321 m_resetUrlTimer
->start();
324 void InformationPanel::init()
326 m_infoTimer
= new QTimer(this);
327 m_infoTimer
->setInterval(300);
328 m_infoTimer
->setSingleShot(true);
329 connect(m_infoTimer
, SIGNAL(timeout()),
330 this, SLOT(slotInfoTimeout()));
332 m_urlChangedTimer
= new QTimer(this);
333 m_urlChangedTimer
->setInterval(200);
334 m_urlChangedTimer
->setSingleShot(true);
335 connect(m_urlChangedTimer
, SIGNAL(timeout()),
336 this, SLOT(showItemInfo()));
338 m_resetUrlTimer
= new QTimer(this);
339 m_resetUrlTimer
->setInterval(1000);
340 m_resetUrlTimer
->setSingleShot(true);
341 connect(m_resetUrlTimer
, SIGNAL(timeout()),
342 this, SLOT(reset()));
344 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
345 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
347 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
348 QDBusConnection::sessionBus(), this);
349 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
350 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
351 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
352 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
353 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
354 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
356 m_content
= new InformationPanelContent(this);
357 connect(m_content
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
359 QVBoxLayout
* layout
= new QVBoxLayout(this);
360 layout
->addWidget(m_content
);
362 m_initialized
= true;
365 #include "informationpanel.moc"