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_infoTimer
->start();
106 } else if (item
.url().isValid() && !isEqualToShownUrl(item
.url())) {
107 // The cursor is above an item that is not shown currently
110 m_urlCandidate
= item
.url();
112 m_infoTimer
->start();
116 bool InformationPanel::urlChanged()
118 if (!url().isValid()) {
129 if (!isEqualToShownUrl(url())) {
131 m_fileItem
= KFileItem();
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
137 m_urlChangedTimer
->start();
143 void InformationPanel::showEvent(QShowEvent
* event
)
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
159 void InformationPanel::resizeEvent(QResizeEvent
* event
)
162 m_urlCandidate
= m_shownUrl
;
163 m_infoTimer
->start();
165 Panel::resizeEvent(event
);
168 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
170 m_content
->configureSettings();
171 Panel::contextMenuEvent(event
);
174 void InformationPanel::showItemInfo()
182 if (m_fileItem
.isNull() && (m_selection
.count() > 1)) {
183 // The information for a selection of items should be shown
184 m_content
->showItems(m_selection
);
186 // The information for exactly one item should be shown
188 if (!m_fileItem
.isNull()) {
190 } else if (!m_selection
.isEmpty()) {
191 Q_ASSERT(m_selection
.count() == 1);
192 item
= m_selection
.first();
196 // No item is hovered and no selection has been done: provide
197 // an item for the currently shown directory.
198 m_folderStatJob
= KIO::stat(url(), KIO::HideProgressInfo
);
199 connect(m_folderStatJob
, SIGNAL(result(KJob
*)),
200 this, SLOT(slotFolderStatFinished(KJob
*)));
202 m_content
->showItem(item
);
207 void InformationPanel::slotFolderStatFinished(KJob
* job
)
210 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
211 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
214 void InformationPanel::slotInfoTimeout()
216 m_shownUrl
= m_urlCandidate
;
217 m_urlCandidate
.clear();
221 void InformationPanel::reset()
223 if (m_invalidUrlCandidate
== m_shownUrl
) {
224 m_invalidUrlCandidate
= KUrl();
226 // The current URL is still invalid. Reset
227 // the content to show the directory URL.
230 m_fileItem
= KFileItem();
235 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
237 if (m_shownUrl
== KUrl(source
)) {
238 m_shownUrl
= KUrl(dest
);
239 m_fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
241 if ((m_selection
.count() == 1) && (m_selection
[0].url() == KUrl(source
))) {
242 m_selection
[0] = m_fileItem
;
243 // Implementation note: Updating the selection is only required if exactly one
244 // item is selected, as the name of the item is shown. If this should change
245 // in future: Before parsing the whole selection take care to test possible
246 // performance bottlenecks when renaming several hundreds of files.
253 void InformationPanel::slotFilesAdded(const QString
& directory
)
255 if (m_shownUrl
== KUrl(directory
)) {
256 // If the 'trash' icon changes because the trash has been emptied or got filled,
257 // the signal filesAdded("trash:/") will be emitted.
258 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
259 requestDelayedItemInfo(item
);
263 void InformationPanel::slotFilesChanged(const QStringList
& files
)
265 foreach (const QString
& fileName
, files
) {
266 if (m_shownUrl
== KUrl(fileName
)) {
273 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
275 foreach (const QString
& fileName
, files
) {
276 if (m_shownUrl
== KUrl(fileName
)) {
277 // the currently shown item has been removed, show
278 // the parent directory as fallback
285 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
287 if (m_shownUrl
== KUrl(directory
)) {
288 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
289 requestDelayedItemInfo(item
);
293 void InformationPanel::slotLeftDirectory(const QString
& directory
)
295 if (m_shownUrl
== KUrl(directory
)) {
296 // The signal 'leftDirectory' is also emitted when a media
297 // has been unmounted. In this case no directory change will be
298 // done in Dolphin, but the Information Panel must be updated to
299 // indicate an invalid directory.
304 void InformationPanel::cancelRequest()
306 delete m_folderStatJob
;
310 m_urlChangedTimer
->stop();
311 m_resetUrlTimer
->stop();
313 m_invalidUrlCandidate
.clear();
314 m_urlCandidate
.clear();
317 bool InformationPanel::isEqualToShownUrl(const KUrl
& url
) const
319 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
322 void InformationPanel::markUrlAsInvalid()
324 m_invalidUrlCandidate
= m_shownUrl
;
325 m_resetUrlTimer
->start();
328 void InformationPanel::init()
330 const int defaultDelay
= 300;
332 m_infoTimer
= new QTimer(this);
333 m_infoTimer
->setInterval(defaultDelay
);
334 m_infoTimer
->setSingleShot(true);
335 connect(m_infoTimer
, SIGNAL(timeout()),
336 this, SLOT(slotInfoTimeout()));
338 m_urlChangedTimer
= new QTimer(this);
339 m_urlChangedTimer
->setInterval(defaultDelay
);
340 m_urlChangedTimer
->setSingleShot(true);
341 connect(m_urlChangedTimer
, SIGNAL(timeout()),
342 this, SLOT(showItemInfo()));
344 m_resetUrlTimer
= new QTimer(this);
345 m_resetUrlTimer
->setInterval(defaultDelay
* 3);
346 m_resetUrlTimer
->setSingleShot(true);
347 connect(m_resetUrlTimer
, SIGNAL(timeout()),
348 this, SLOT(reset()));
350 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
351 QDBusConnection::sessionBus(), this);
352 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
353 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
354 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
355 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
356 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
357 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
359 m_content
= new InformationPanelContent(this);
360 connect(m_content
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
362 QVBoxLayout
* layout
= new QVBoxLayout(this);
363 layout
->addWidget(m_content
);
365 m_initialized
= true;
368 #include "informationpanel.moc"