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 <KIO/JobUiDelegate>
26 #include <QApplication>
28 #include <QVBoxLayout>
30 InformationPanel::InformationPanel(QWidget
* parent
) :
38 m_invalidUrlCandidate(),
46 InformationPanel::~InformationPanel()
50 void InformationPanel::setSelection(const KFileItemList
& selection
)
52 m_selection
= selection
;
53 m_fileItem
= KFileItem();
59 const int count
= selection
.count();
61 if (!isEqualToShownUrl(url())) {
66 if ((count
== 1) && !selection
.first().url().isEmpty()) {
67 m_urlCandidate
= selection
.first().url();
73 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
75 if (!isVisible() || (item
.isNull() && m_fileItem
.isNull())) {
79 if (QApplication::mouseButtons() & Qt::LeftButton
) {
80 // Ignore the request of an item information when a rubberband
81 // selection is ongoing.
88 // The cursor is above the viewport. If files are selected,
89 // show information regarding the selection.
90 if (m_selection
.size() > 0) {
91 m_fileItem
= KFileItem();
94 } else if (item
.url().isValid() && !isEqualToShownUrl(item
.url())) {
95 // The cursor is above an item that is not shown currently
96 m_urlCandidate
= item
.url();
102 bool InformationPanel::urlChanged()
104 if (!url().isValid()) {
115 if (!isEqualToShownUrl(url())) {
117 m_fileItem
= KFileItem();
119 // Update the content with a delay. This gives
120 // the directory lister the chance to show the content
121 // before expensive operations are done to show
123 m_urlChangedTimer
->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
145 void InformationPanel::resizeEvent(QResizeEvent
* event
)
148 m_urlCandidate
= m_shownUrl
;
149 m_infoTimer
->start();
151 Panel::resizeEvent(event
);
154 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
156 // TODO: Move code from InformationPanelContent::configureSettings() here
157 m_content
->configureSettings(customContextMenuActions());
158 Panel::contextMenuEvent(event
);
161 void InformationPanel::showItemInfo()
169 if (m_fileItem
.isNull() && (m_selection
.count() > 1)) {
170 // The information for a selection of items should be shown
171 m_content
->showItems(m_selection
);
173 // The information for exactly one item should be shown
175 if (!m_fileItem
.isNull()) {
177 } else if (!m_selection
.isEmpty()) {
178 Q_ASSERT(m_selection
.count() == 1);
179 item
= m_selection
.first();
183 // No item is hovered and no selection has been done: provide
184 // an item for the currently shown directory.
185 m_folderStatJob
= KIO::stat(url(), KIO::HideProgressInfo
);
186 if (m_folderStatJob
->ui()) {
187 m_folderStatJob
->ui()->setWindow(this);
189 connect(m_folderStatJob
, SIGNAL(result(KJob
*)),
190 this, SLOT(slotFolderStatFinished(KJob
*)));
192 m_content
->showItem(item
);
197 void InformationPanel::slotFolderStatFinished(KJob
* job
)
200 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
201 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
204 void InformationPanel::slotInfoTimeout()
206 m_shownUrl
= m_urlCandidate
;
207 m_urlCandidate
.clear();
211 void InformationPanel::reset()
213 if (m_invalidUrlCandidate
== m_shownUrl
) {
214 m_invalidUrlCandidate
= KUrl();
216 // The current URL is still invalid. Reset
217 // the content to show the directory URL.
220 m_fileItem
= KFileItem();
225 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
227 if (m_shownUrl
== KUrl(source
)) {
228 m_shownUrl
= KUrl(dest
);
229 m_fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
231 if ((m_selection
.count() == 1) && (m_selection
[0].url() == KUrl(source
))) {
232 m_selection
[0] = m_fileItem
;
233 // Implementation note: Updating the selection is only required if exactly one
234 // item is selected, as the name of the item is shown. If this should change
235 // in future: Before parsing the whole selection take care to test possible
236 // performance bottlenecks when renaming several hundreds of files.
243 void InformationPanel::slotFilesAdded(const QString
& directory
)
245 if (m_shownUrl
== KUrl(directory
)) {
246 // If the 'trash' icon changes because the trash has been emptied or got filled,
247 // the signal filesAdded("trash:/") will be emitted.
248 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
249 requestDelayedItemInfo(item
);
253 void InformationPanel::slotFilesChanged(const QStringList
& files
)
255 foreach (const QString
& fileName
, files
) {
256 if (m_shownUrl
== KUrl(fileName
)) {
263 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
265 foreach (const QString
& fileName
, files
) {
266 if (m_shownUrl
== KUrl(fileName
)) {
267 // the currently shown item has been removed, show
268 // the parent directory as fallback
275 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
277 if (m_shownUrl
== KUrl(directory
)) {
278 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
279 requestDelayedItemInfo(item
);
283 void InformationPanel::slotLeftDirectory(const QString
& directory
)
285 if (m_shownUrl
== KUrl(directory
)) {
286 // The signal 'leftDirectory' is also emitted when a media
287 // has been unmounted. In this case no directory change will be
288 // done in Dolphin, but the Information Panel must be updated to
289 // indicate an invalid directory.
294 void InformationPanel::cancelRequest()
296 delete m_folderStatJob
;
300 m_resetUrlTimer
->stop();
301 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
302 // has the smallest interval (see init()), it is not possible that the exceeded timer
303 // would overwrite an information provided by a selection or hovering.
305 m_invalidUrlCandidate
.clear();
306 m_urlCandidate
.clear();
309 bool InformationPanel::isEqualToShownUrl(const KUrl
& url
) const
311 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
314 void InformationPanel::markUrlAsInvalid()
316 m_invalidUrlCandidate
= m_shownUrl
;
317 m_resetUrlTimer
->start();
320 void InformationPanel::init()
322 m_infoTimer
= new QTimer(this);
323 m_infoTimer
->setInterval(300);
324 m_infoTimer
->setSingleShot(true);
325 connect(m_infoTimer
, SIGNAL(timeout()),
326 this, SLOT(slotInfoTimeout()));
328 m_urlChangedTimer
= new QTimer(this);
329 m_urlChangedTimer
->setInterval(200);
330 m_urlChangedTimer
->setSingleShot(true);
331 connect(m_urlChangedTimer
, SIGNAL(timeout()),
332 this, SLOT(showItemInfo()));
334 m_resetUrlTimer
= new QTimer(this);
335 m_resetUrlTimer
->setInterval(1000);
336 m_resetUrlTimer
->setSingleShot(true);
337 connect(m_resetUrlTimer
, SIGNAL(timeout()),
338 this, SLOT(reset()));
340 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
341 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
343 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
344 QDBusConnection::sessionBus(), this);
345 connect(dirNotify
, SIGNAL(FileRenamed(QString
,QString
)), SLOT(slotFileRenamed(QString
,QString
)));
346 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
347 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
348 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
349 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
350 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
352 m_content
= new InformationPanelContent(this);
353 connect(m_content
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
355 QVBoxLayout
* layout
= new QVBoxLayout(this);
356 layout
->setContentsMargins(0, 0, 0, 0);
357 layout
->addWidget(m_content
);
359 m_initialized
= true;
362 #include "informationpanel.moc"