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
)) {
62 // Update the content with a delay. This gives
63 // the directory lister the chance to show the content
64 // before expensive operations are done to show
66 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
)
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();
178 if ( item
.isNull() ) {
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
*)));
186 m_content
->showItem(item
);
191 void InformationPanel::slotFolderStatFinished(KJob
* job
)
194 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
195 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
198 void InformationPanel::slotInfoTimeout()
200 m_shownUrl
= m_urlCandidate
;
204 void InformationPanel::reset()
206 if (m_invalidUrlCandidate
== m_shownUrl
) {
207 m_invalidUrlCandidate
= KUrl();
209 // The current URL is still invalid. Reset
210 // the content to show the directory URL.
213 m_fileItem
= KFileItem();
218 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
220 if (m_shownUrl
== KUrl(source
)) {
221 m_shownUrl
= KUrl(dest
);
222 m_fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
224 if ((m_selection
.count() == 1) && (m_selection
[0].url() == KUrl(source
))) {
225 m_selection
[0] = m_fileItem
;
226 // Implementation note: Updating the selection is only required if exactly one
227 // item is selected, as the name of the item is shown. If this should change
228 // in future: Before parsing the whole selection take care to test possible
229 // performance bottlenecks when renaming several hundreds of files.
236 void InformationPanel::slotFilesAdded(const QString
& directory
)
238 if (m_shownUrl
== KUrl(directory
)) {
239 // If the 'trash' icon changes because the trash has been emptied or got filled,
240 // the signal filesAdded("trash:/") will be emitted.
241 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
242 requestDelayedItemInfo(item
);
246 void InformationPanel::slotFilesChanged(const QStringList
& files
)
248 foreach (const QString
& fileName
, files
) {
249 if (m_shownUrl
== KUrl(fileName
)) {
256 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
258 foreach (const QString
& fileName
, files
) {
259 if (m_shownUrl
== KUrl(fileName
)) {
260 // the currently shown item has been removed, show
261 // the parent directory as fallback
268 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
270 if (m_shownUrl
== KUrl(directory
)) {
271 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
272 requestDelayedItemInfo(item
);
276 void InformationPanel::slotLeftDirectory(const QString
& directory
)
278 if (m_shownUrl
== KUrl(directory
)) {
279 // The signal 'leftDirectory' is also emitted when a media
280 // has been unmounted. In this case no directory change will be
281 // done in Dolphin, but the Information Panel must be updated to
282 // indicate an invalid directory.
287 void InformationPanel::cancelRequest()
289 delete m_folderStatJob
;
294 bool InformationPanel::showMultipleSelectionInfo() const
296 return m_fileItem
.isNull() && (m_selection
.count() > 1);
299 bool InformationPanel::isEqualToShownUrl(const KUrl
& url
) const
301 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
304 void InformationPanel::markUrlAsInvalid()
306 m_invalidUrlCandidate
= m_shownUrl
;
307 m_resetUrlTimer
->start();
310 void InformationPanel::init()
312 const int defaultDelay
= 300;
314 m_infoTimer
= new QTimer(this);
315 m_infoTimer
->setInterval(defaultDelay
);
316 m_infoTimer
->setSingleShot(true);
317 connect(m_infoTimer
, SIGNAL(timeout()),
318 this, SLOT(slotInfoTimeout()));
320 m_urlChangedTimer
= new QTimer(this);
321 m_urlChangedTimer
->setInterval(defaultDelay
);
322 m_urlChangedTimer
->setSingleShot(true);
323 connect(m_urlChangedTimer
, SIGNAL(timeout()),
324 this, SLOT(showItemInfo()));
326 m_resetUrlTimer
= new QTimer(this);
327 m_resetUrlTimer
->setInterval(defaultDelay
* 3);
328 m_resetUrlTimer
->setSingleShot(true);
329 connect(m_resetUrlTimer
, SIGNAL(timeout()),
330 this, SLOT(reset()));
332 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
333 QDBusConnection::sessionBus(), this);
334 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
335 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
336 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
337 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
338 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
339 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
341 m_content
= new InformationPanelContent(this);
342 connect(m_content
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
344 QVBoxLayout
* layout
= new QVBoxLayout(this);
345 layout
->addWidget(m_content
);
347 m_initialized
= true;
350 #include "informationpanel.moc"