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 "informationpanelcontent.h"
25 InformationPanel::InformationPanel(QWidget
* parent
) :
33 m_invalidUrlCandidate(),
40 InformationPanel::~InformationPanel()
44 QSize
InformationPanel::sizeHint() const
46 QSize size
= Panel::sizeHint();
47 size
.setWidth(minimumSizeHint().width());
51 void InformationPanel::setUrl(const KUrl
& url
)
54 if (url
.isValid() && !isEqualToShownUrl(url
)) {
58 // Update the content with a delay. This gives
59 // the directory lister the chance to show the content
60 // before expensive operations are done to show
62 m_urlChangedTimer
->start();
69 void InformationPanel::setSelection(const KFileItemList
& selection
)
75 if ((selection
.count() == 0) && (m_selection
.count() == 0)) {
76 // The selection has not really changed, only the current index.
77 // QItemSelectionModel emits a signal in this case and it is less
78 // expensive doing the check this way instead of patching
79 // DolphinView::emitSelectionChanged().
83 m_selection
= selection
;
84 m_fileItem
= KFileItem();
86 const int count
= selection
.count();
88 if (!isEqualToShownUrl(url())) {
93 if ((count
== 1) && !selection
.first().url().isEmpty()) {
94 m_urlCandidate
= selection
.first().url();
100 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
108 m_fileItem
= KFileItem();
110 // The cursor is above the viewport. If files are selected,
111 // show information regarding the selection.
112 if (m_selection
.size() > 0) {
113 m_infoTimer
->start();
116 const KUrl url
= item
.url();
117 if (url
.isValid() && !isEqualToShownUrl(url
)) {
118 m_urlCandidate
= item
.url();
120 m_infoTimer
->start();
125 void InformationPanel::showEvent(QShowEvent
* event
)
127 Panel::showEvent(event
);
128 if (!event
->spontaneous()) {
129 if (!m_initialized
) {
130 // do a delayed initialization so that no performance
131 // penalty is given when Dolphin is started with a closed
139 void InformationPanel::resizeEvent(QResizeEvent
* event
)
142 m_urlCandidate
= m_shownUrl
;
143 m_infoTimer
->start();
145 Panel::resizeEvent(event
);
148 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
150 m_content
->configureSettings();
151 Panel::contextMenuEvent(event
);
154 void InformationPanel::showItemInfo()
162 if (showMultipleSelectionInfo()) {
163 m_content
->showItems(m_selection
);
167 if (!m_fileItem
.isNull()) {
169 } else if (!m_selection
.isEmpty()) {
170 Q_ASSERT(m_selection
.count() == 1);
171 item
= m_selection
.first();
173 // no item is hovered and no selection has been done: provide
174 // an item for the directory represented by m_shownUrl
175 item
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
179 m_content
->showItem(item
);
183 void InformationPanel::slotInfoTimeout()
185 m_shownUrl
= m_urlCandidate
;
189 void InformationPanel::reset()
191 if (m_invalidUrlCandidate
== m_shownUrl
) {
192 m_invalidUrlCandidate
= KUrl();
194 // The current URL is still invalid. Reset
195 // the content to show the directory URL.
198 m_fileItem
= KFileItem();
203 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
205 if (m_shownUrl
== KUrl(source
)) {
206 m_shownUrl
= KUrl(dest
);
207 m_fileItem
= KFileItem(KFileItem::Unknown
, KFileItem::Unknown
, m_shownUrl
);
209 if ((m_selection
.count() == 1) && (m_selection
[0].url() == KUrl(source
))) {
210 m_selection
[0] = m_fileItem
;
211 // Implementation note: Updating the selection is only required if exactly one
212 // item is selected, as the name of the item is shown. If this should change
213 // in future: Before parsing the whole selection take care to test possible
214 // performance bottlenecks when renaming several hundreds of files.
221 void InformationPanel::slotFilesAdded(const QString
& directory
)
223 if (m_shownUrl
== KUrl(directory
)) {
224 // If the 'trash' icon changes because the trash has been emptied or got filled,
225 // the signal filesAdded("trash:/") will be emitted.
226 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
227 requestDelayedItemInfo(item
);
231 void InformationPanel::slotFilesChanged(const QStringList
& files
)
233 foreach (const QString
& fileName
, files
) {
234 if (m_shownUrl
== KUrl(fileName
)) {
241 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
243 foreach (const QString
& fileName
, files
) {
244 if (m_shownUrl
== KUrl(fileName
)) {
245 // the currently shown item has been removed, show
246 // the parent directory as fallback
253 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
255 if (m_shownUrl
== KUrl(directory
)) {
256 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, KUrl(directory
));
257 requestDelayedItemInfo(item
);
261 void InformationPanel::slotLeftDirectory(const QString
& directory
)
263 if (m_shownUrl
== KUrl(directory
)) {
264 // The signal 'leftDirectory' is also emitted when a media
265 // has been unmounted. In this case no directory change will be
266 // done in Dolphin, but the Information Panel must be updated to
267 // indicate an invalid directory.
272 void InformationPanel::cancelRequest()
277 bool InformationPanel::showMultipleSelectionInfo() const
279 return m_fileItem
.isNull() && (m_selection
.count() > 1);
282 bool InformationPanel::isEqualToShownUrl(const KUrl
& url
) const
284 return m_shownUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
);
287 void InformationPanel::markUrlAsInvalid()
289 m_invalidUrlCandidate
= m_shownUrl
;
290 m_resetUrlTimer
->start();
293 void InformationPanel::init()
295 const int defaultDelay
= 300;
297 m_infoTimer
= new QTimer(this);
298 m_infoTimer
->setInterval(defaultDelay
);
299 m_infoTimer
->setSingleShot(true);
300 connect(m_infoTimer
, SIGNAL(timeout()),
301 this, SLOT(slotInfoTimeout()));
303 m_urlChangedTimer
= new QTimer(this);
304 m_urlChangedTimer
->setInterval(defaultDelay
);
305 m_urlChangedTimer
->setSingleShot(true);
306 connect(m_urlChangedTimer
, SIGNAL(timeout()),
307 this, SLOT(showItemInfo()));
309 m_resetUrlTimer
= new QTimer(this);
310 m_resetUrlTimer
->setInterval(defaultDelay
* 3);
311 m_resetUrlTimer
->setSingleShot(true);
312 connect(m_resetUrlTimer
, SIGNAL(timeout()),
313 this, SLOT(reset()));
315 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
316 QDBusConnection::sessionBus(), this);
317 connect(dirNotify
, SIGNAL(FileRenamed(QString
, QString
)), SLOT(slotFileRenamed(QString
, QString
)));
318 connect(dirNotify
, SIGNAL(FilesAdded(QString
)), SLOT(slotFilesAdded(QString
)));
319 connect(dirNotify
, SIGNAL(FilesChanged(QStringList
)), SLOT(slotFilesChanged(QStringList
)));
320 connect(dirNotify
, SIGNAL(FilesRemoved(QStringList
)), SLOT(slotFilesRemoved(QStringList
)));
321 connect(dirNotify
, SIGNAL(enteredDirectory(QString
)), SLOT(slotEnteredDirectory(QString
)));
322 connect(dirNotify
, SIGNAL(leftDirectory(QString
)), SLOT(slotLeftDirectory(QString
)));
324 m_content
= new InformationPanelContent(this);
325 connect(m_content
, SIGNAL(urlActivated(KUrl
)), this, SIGNAL(urlActivated(KUrl
)));
327 m_initialized
= true;
330 #include "informationpanel.moc"