1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz@gmx.at> *
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 QSize
InformationPanel::sizeHint() const
51 QSize size
= Panel::sizeHint();
52 size
.setWidth(minimumSizeHint().width());
56 void InformationPanel::setSelection(const KFileItemList
& selection
)
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().
70 m_selection
= selection
;
71 m_fileItem
= KFileItem();
73 const int count
= selection
.count();
75 if (!isEqualToShownUrl(url())) {
80 if ((count
== 1) && !selection
.first().url().isEmpty()) {
81 m_urlCandidate
= selection
.first().url();
87 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
89 if (!isVisible() || (item
.isNull() && m_fileItem
.isNull())) {
93 if (QApplication::mouseButtons() & Qt::LeftButton
) {
94 // Ignore the request of an item information when a rubberband
95 // selection is ongoing.
100 // The cursor is above the viewport. If files are selected,
101 // show information regarding the selection.
102 if (m_selection
.size() > 0) {
104 m_fileItem
= KFileItem();
105 m_infoTimer
->start();
107 } else if (item
.url().isValid() && !isEqualToShownUrl(item
.url())) {
108 // The cursor is above an item that is not shown currently
111 m_urlCandidate
= item
.url();
113 m_infoTimer
->start();
117 bool InformationPanel::urlChanged()
119 if (!url().isValid()) {
130 if (!isEqualToShownUrl(url())) {
132 m_fileItem
= KFileItem();
134 // Update the content with a delay. This gives
135 // the directory lister the chance to show the content
136 // before expensive operations are done to show
138 m_urlChangedTimer
->start();
144 void InformationPanel::showEvent(QShowEvent
* event
)
146 Panel::showEvent(event
);
147 if (!event
->spontaneous()) {
148 if (!m_initialized
) {
149 // do a delayed initialization so that no performance
150 // penalty is given when Dolphin is started with a closed
160 void InformationPanel::resizeEvent(QResizeEvent
* event
)
163 m_urlCandidate
= m_shownUrl
;
164 m_infoTimer
->start();
166 Panel::resizeEvent(event
);
169 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
171 m_content
->configureSettings();
172 Panel::contextMenuEvent(event
);
175 void InformationPanel::showItemInfo()
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
);
187 // The information for exactly one item should be shown
189 if (!m_fileItem
.isNull()) {
191 } else if (!m_selection
.isEmpty()) {
192 Q_ASSERT(m_selection
.count() == 1);
193 item
= m_selection
.first();
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
*)));
203 m_content
->showItem(item
);
208 void InformationPanel::slotFolderStatFinished(KJob
* job
)
211 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
212 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
215 void InformationPanel::slotInfoTimeout()
217 m_shownUrl
= m_urlCandidate
;
218 m_urlCandidate
.clear();
222 void InformationPanel::reset()
224 if (m_invalidUrlCandidate
== m_shownUrl
) {
225 m_invalidUrlCandidate
= KUrl();
227 // The current URL is still invalid. Reset
228 // the content to show the directory URL.
231 m_fileItem
= KFileItem();
236 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
238 if (m_shownUrl
== KUrl(source
)) {
239 m_shownUrl
= KUrl(dest
);
240 m_fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
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.
254 void InformationPanel::slotFilesAdded(const QString
& directory
)
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
);
264 void InformationPanel::slotFilesChanged(const QStringList
& files
)
266 foreach (const QString
& fileName
, files
) {
267 if (m_shownUrl
== KUrl(fileName
)) {
274 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
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
286 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
288 if (m_shownUrl
== KUrl(directory
)) {
289 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
290 requestDelayedItemInfo(item
);
294 void InformationPanel::slotLeftDirectory(const QString
& directory
)
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.
305 void InformationPanel::cancelRequest()
307 delete m_folderStatJob
;
311 m_urlChangedTimer
->stop();
312 m_resetUrlTimer
->stop();
314 m_invalidUrlCandidate
.clear();
315 m_urlCandidate
.clear();
318 bool InformationPanel::isEqualToShownUrl(const KUrl
& url
) const
320 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
323 void InformationPanel::markUrlAsInvalid()
325 m_invalidUrlCandidate
= m_shownUrl
;
326 m_resetUrlTimer
->start();
329 void InformationPanel::init()
331 const int defaultDelay
= 300;
333 m_infoTimer
= new QTimer(this);
334 m_infoTimer
->setInterval(defaultDelay
);
335 m_infoTimer
->setSingleShot(true);
336 connect(m_infoTimer
, SIGNAL(timeout()),
337 this, SLOT(slotInfoTimeout()));
339 m_urlChangedTimer
= new QTimer(this);
340 m_urlChangedTimer
->setInterval(defaultDelay
);
341 m_urlChangedTimer
->setSingleShot(true);
342 connect(m_urlChangedTimer
, SIGNAL(timeout()),
343 this, SLOT(showItemInfo()));
345 m_resetUrlTimer
= new QTimer(this);
346 m_resetUrlTimer
->setInterval(defaultDelay
* 3);
347 m_resetUrlTimer
->setSingleShot(true);
348 connect(m_resetUrlTimer
, SIGNAL(timeout()),
349 this, SLOT(reset()));
351 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
352 QDBusConnection::sessionBus(), this);
353 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
354 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
355 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
356 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
357 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
358 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
360 m_content
= new InformationPanelContent(this);
361 connect(m_content
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
363 QVBoxLayout
* layout
= new QVBoxLayout(this);
364 layout
->addWidget(m_content
);
366 m_initialized
= true;
369 #include "informationpanel.moc"