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"
21 #include <kdirnotify.h>
23 #include <QVBoxLayout>
24 #include "informationpanelcontent.h"
28 InformationPanel::InformationPanel(QWidget
* parent
) :
36 m_invalidUrlCandidate(),
44 InformationPanel::~InformationPanel()
48 QSize
InformationPanel::sizeHint() const
50 QSize size
= Panel::sizeHint();
51 size
.setWidth(minimumSizeHint().width());
55 void InformationPanel::setUrl(const KUrl
& url
)
58 if (!url
.isValid() || isEqualToShownUrl(url
)) {
65 // Update the content with a delay. This gives
66 // the directory lister the chance to show the content
67 // before expensive operations are done to show
69 m_urlChangedTimer
->start();
73 void InformationPanel::setSelection(const KFileItemList
& selection
)
79 if ((selection
.count() == 0) && (m_selection
.count() == 0)) {
80 // The selection has not really changed, only the current index.
81 // QItemSelectionModel emits a signal in this case and it is less
82 // expensive doing the check this way instead of patching
83 // DolphinView::emitSelectionChanged().
87 m_selection
= selection
;
88 m_fileItem
= KFileItem();
90 const int count
= selection
.count();
92 if (!isEqualToShownUrl(url())) {
97 if ((count
== 1) && !selection
.first().url().isEmpty()) {
98 m_urlCandidate
= selection
.first().url();
100 m_infoTimer
->start();
104 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
106 if (!isVisible() || (item
.isNull() && m_fileItem
.isNull())) {
112 m_fileItem
= KFileItem();
114 // The cursor is above the viewport. If files are selected,
115 // show information regarding the selection.
116 if (m_selection
.size() > 0) {
117 m_infoTimer
->start();
120 const KUrl url
= item
.url();
121 if (url
.isValid() && !isEqualToShownUrl(url
)) {
122 m_urlCandidate
= item
.url();
124 m_infoTimer
->start();
129 void InformationPanel::showEvent(QShowEvent
* event
)
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
143 void InformationPanel::resizeEvent(QResizeEvent
* event
)
146 m_urlCandidate
= m_shownUrl
;
147 m_infoTimer
->start();
149 Panel::resizeEvent(event
);
152 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
154 m_content
->configureSettings();
155 Panel::contextMenuEvent(event
);
158 void InformationPanel::showItemInfo()
166 if (showMultipleSelectionInfo()) {
167 m_content
->showItems(m_selection
);
171 if (!m_fileItem
.isNull()) {
173 } else if (!m_selection
.isEmpty()) {
174 Q_ASSERT(m_selection
.count() == 1);
175 item
= m_selection
.first();
179 // no item is hovered and no selection has been done: provide
180 // an item for the directory represented by m_shownUrl
181 m_folderStatJob
= KIO::stat(m_shownUrl
, KIO::HideProgressInfo
);
182 connect(m_folderStatJob
, SIGNAL(result(KJob
*)),
183 this, SLOT(slotFolderStatFinished(KJob
*)));
185 m_content
->showItem(item
);
190 void InformationPanel::slotFolderStatFinished(KJob
* job
)
193 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
194 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
197 void InformationPanel::slotInfoTimeout()
199 m_shownUrl
= m_urlCandidate
;
203 void InformationPanel::reset()
205 if (m_invalidUrlCandidate
== m_shownUrl
) {
206 m_invalidUrlCandidate
= KUrl();
208 // The current URL is still invalid. Reset
209 // the content to show the directory URL.
212 m_fileItem
= KFileItem();
217 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
219 if (m_shownUrl
== KUrl(source
)) {
220 m_shownUrl
= KUrl(dest
);
221 m_fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
223 if ((m_selection
.count() == 1) && (m_selection
[0].url() == KUrl(source
))) {
224 m_selection
[0] = m_fileItem
;
225 // Implementation note: Updating the selection is only required if exactly one
226 // item is selected, as the name of the item is shown. If this should change
227 // in future: Before parsing the whole selection take care to test possible
228 // performance bottlenecks when renaming several hundreds of files.
235 void InformationPanel::slotFilesAdded(const QString
& directory
)
237 if (m_shownUrl
== KUrl(directory
)) {
238 // If the 'trash' icon changes because the trash has been emptied or got filled,
239 // the signal filesAdded("trash:/") will be emitted.
240 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
241 requestDelayedItemInfo(item
);
245 void InformationPanel::slotFilesChanged(const QStringList
& files
)
247 foreach (const QString
& fileName
, files
) {
248 if (m_shownUrl
== KUrl(fileName
)) {
255 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
257 foreach (const QString
& fileName
, files
) {
258 if (m_shownUrl
== KUrl(fileName
)) {
259 // the currently shown item has been removed, show
260 // the parent directory as fallback
267 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
269 if (m_shownUrl
== KUrl(directory
)) {
270 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
271 requestDelayedItemInfo(item
);
275 void InformationPanel::slotLeftDirectory(const QString
& directory
)
277 if (m_shownUrl
== KUrl(directory
)) {
278 // The signal 'leftDirectory' is also emitted when a media
279 // has been unmounted. In this case no directory change will be
280 // done in Dolphin, but the Information Panel must be updated to
281 // indicate an invalid directory.
286 void InformationPanel::cancelRequest()
288 delete m_folderStatJob
;
293 bool InformationPanel::showMultipleSelectionInfo() const
295 return m_fileItem
.isNull() && (m_selection
.count() > 1);
298 bool InformationPanel::isEqualToShownUrl(const KUrl
& url
) const
300 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
303 void InformationPanel::markUrlAsInvalid()
305 m_invalidUrlCandidate
= m_shownUrl
;
306 m_resetUrlTimer
->start();
309 void InformationPanel::init()
311 const int defaultDelay
= 300;
313 m_infoTimer
= new QTimer(this);
314 m_infoTimer
->setInterval(defaultDelay
);
315 m_infoTimer
->setSingleShot(true);
316 connect(m_infoTimer
, SIGNAL(timeout()),
317 this, SLOT(slotInfoTimeout()));
319 m_urlChangedTimer
= new QTimer(this);
320 m_urlChangedTimer
->setInterval(defaultDelay
);
321 m_urlChangedTimer
->setSingleShot(true);
322 connect(m_urlChangedTimer
, SIGNAL(timeout()),
323 this, SLOT(showItemInfo()));
325 m_resetUrlTimer
= new QTimer(this);
326 m_resetUrlTimer
->setInterval(defaultDelay
* 3);
327 m_resetUrlTimer
->setSingleShot(true);
328 connect(m_resetUrlTimer
, SIGNAL(timeout()),
329 this, SLOT(reset()));
331 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
332 QDBusConnection::sessionBus(), this);
333 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
334 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
335 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
336 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
337 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
338 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
340 m_content
= new InformationPanelContent(this);
341 connect(m_content
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
343 QVBoxLayout
* layout
= new QVBoxLayout(this);
344 layout
->addWidget(m_content
);
346 m_initialized
= true;
349 #include "informationpanel.moc"