]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/informationpanel.cpp
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>
25 #include <KJobWidgets>
27 #include <QApplication>
29 #include <QVBoxLayout>
32 InformationPanel::InformationPanel(QWidget
* parent
) :
40 m_invalidUrlCandidate(),
48 InformationPanel::~InformationPanel()
52 void InformationPanel::setSelection(const KFileItemList
& selection
)
54 m_selection
= selection
;
55 m_fileItem
= KFileItem();
61 const int count
= selection
.count();
63 if (!isEqualToShownUrl(url())) {
68 if ((count
== 1) && !selection
.first().url().isEmpty()) {
69 m_urlCandidate
= selection
.first().url();
75 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
77 if (!isVisible() || (item
.isNull() && m_fileItem
.isNull())) {
81 if (QApplication::mouseButtons() & Qt::LeftButton
) {
82 // Ignore the request of an item information when a rubberband
83 // selection is ongoing.
90 // The cursor is above the viewport. If files are selected,
91 // show information regarding the selection.
92 if (m_selection
.size() > 0) {
93 m_fileItem
= KFileItem();
96 } else if (item
.url().isValid() && !isEqualToShownUrl(item
.url())) {
97 // The cursor is above an item that is not shown currently
98 m_urlCandidate
= item
.url();
100 m_infoTimer
->start();
104 bool InformationPanel::urlChanged()
106 if (!url().isValid()) {
117 if (!isEqualToShownUrl(url())) {
119 m_fileItem
= KFileItem();
121 // Update the content with a delay. This gives
122 // the directory lister the chance to show the content
123 // before expensive operations are done to show
125 m_urlChangedTimer
->start();
131 void InformationPanel::showEvent(QShowEvent
* event
)
133 Panel::showEvent(event
);
134 if (!event
->spontaneous()) {
135 if (!m_initialized
) {
136 // do a delayed initialization so that no performance
137 // penalty is given when Dolphin is started with a closed
147 void InformationPanel::resizeEvent(QResizeEvent
* event
)
150 m_urlCandidate
= m_shownUrl
;
151 m_infoTimer
->start();
153 Panel::resizeEvent(event
);
156 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
158 // TODO: Move code from InformationPanelContent::configureSettings() here
159 m_content
->configureSettings(customContextMenuActions());
160 Panel::contextMenuEvent(event
);
163 void InformationPanel::showItemInfo()
171 if (m_fileItem
.isNull() && (m_selection
.count() > 1)) {
172 // The information for a selection of items should be shown
173 m_content
->showItems(m_selection
);
175 // The information for exactly one item should be shown
177 if (!m_fileItem
.isNull()) {
179 } else if (!m_selection
.isEmpty()) {
180 Q_ASSERT(m_selection
.count() == 1);
181 item
= m_selection
.first();
185 // No item is hovered and no selection has been done: provide
186 // an item for the currently shown directory.
187 m_folderStatJob
= KIO::stat(url(), KIO::HideProgressInfo
);
188 if (m_folderStatJob
->uiDelegate()) {
189 KJobWidgets::setWindow(m_folderStatJob
, this);
191 connect(m_folderStatJob
, &KIO::Job::result
,
192 this, &InformationPanel::slotFolderStatFinished
);
194 m_content
->showItem(item
);
199 void InformationPanel::slotFolderStatFinished(KJob
* job
)
202 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
203 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
206 void InformationPanel::slotInfoTimeout()
208 m_shownUrl
= m_urlCandidate
;
209 m_urlCandidate
.clear();
213 void InformationPanel::reset()
215 if (m_invalidUrlCandidate
== m_shownUrl
) {
216 m_invalidUrlCandidate
= QUrl();
218 // The current URL is still invalid. Reset
219 // the content to show the directory URL.
222 m_fileItem
= KFileItem();
227 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
229 if (m_shownUrl
== QUrl::fromLocalFile(source
)) {
230 m_shownUrl
= QUrl::fromLocalFile(dest
);
231 m_fileItem
= KFileItem(m_shownUrl
);
233 if ((m_selection
.count() == 1) && (m_selection
[0].url() == QUrl::fromLocalFile(source
))) {
234 m_selection
[0] = m_fileItem
;
235 // Implementation note: Updating the selection is only required if exactly one
236 // item is selected, as the name of the item is shown. If this should change
237 // in future: Before parsing the whole selection take care to test possible
238 // performance bottlenecks when renaming several hundreds of files.
245 void InformationPanel::slotFilesAdded(const QString
& directory
)
247 if (m_shownUrl
== QUrl::fromLocalFile(directory
)) {
248 // If the 'trash' icon changes because the trash has been emptied or got filled,
249 // the signal filesAdded("trash:/") will be emitted.
250 KFileItem
item(QUrl::fromLocalFile(directory
));
251 requestDelayedItemInfo(item
);
255 void InformationPanel::slotFilesChanged(const QStringList
& files
)
257 foreach (const QString
& fileName
, files
) {
258 if (m_shownUrl
== QUrl::fromLocalFile(fileName
)) {
265 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
267 foreach (const QString
& fileName
, files
) {
268 if (m_shownUrl
== QUrl::fromLocalFile(fileName
)) {
269 // the currently shown item has been removed, show
270 // the parent directory as fallback
277 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
279 if (m_shownUrl
== QUrl::fromLocalFile(directory
)) {
280 KFileItem
item(QUrl::fromLocalFile(directory
));
281 requestDelayedItemInfo(item
);
285 void InformationPanel::slotLeftDirectory(const QString
& directory
)
287 if (m_shownUrl
== QUrl::fromLocalFile(directory
)) {
288 // The signal 'leftDirectory' is also emitted when a media
289 // has been unmounted. In this case no directory change will be
290 // done in Dolphin, but the Information Panel must be updated to
291 // indicate an invalid directory.
296 void InformationPanel::cancelRequest()
298 delete m_folderStatJob
;
302 m_resetUrlTimer
->stop();
303 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
304 // has the smallest interval (see init()), it is not possible that the exceeded timer
305 // would overwrite an information provided by a selection or hovering.
307 m_invalidUrlCandidate
.clear();
308 m_urlCandidate
.clear();
311 bool InformationPanel::isEqualToShownUrl(const QUrl
& url
) const
313 return m_shownUrl
.matches(url
, QUrl::StripTrailingSlash
);
316 void InformationPanel::markUrlAsInvalid()
318 m_invalidUrlCandidate
= m_shownUrl
;
319 m_resetUrlTimer
->start();
322 void InformationPanel::init()
324 m_infoTimer
= new QTimer(this);
325 m_infoTimer
->setInterval(300);
326 m_infoTimer
->setSingleShot(true);
327 connect(m_infoTimer
, &QTimer::timeout
,
328 this, &InformationPanel::slotInfoTimeout
);
330 m_urlChangedTimer
= new QTimer(this);
331 m_urlChangedTimer
->setInterval(200);
332 m_urlChangedTimer
->setSingleShot(true);
333 connect(m_urlChangedTimer
, &QTimer::timeout
,
334 this, &InformationPanel::showItemInfo
);
336 m_resetUrlTimer
= new QTimer(this);
337 m_resetUrlTimer
->setInterval(1000);
338 m_resetUrlTimer
->setSingleShot(true);
339 connect(m_resetUrlTimer
, &QTimer::timeout
,
340 this, &InformationPanel::reset
);
342 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
343 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
345 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
346 QDBusConnection::sessionBus(), this);
347 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FileRenamed
, this, &InformationPanel::slotFileRenamed
);
348 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesAdded
, this, &InformationPanel::slotFilesAdded
);
349 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesChanged
, this, &InformationPanel::slotFilesChanged
);
350 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesRemoved
, this, &InformationPanel::slotFilesRemoved
);
351 connect(dirNotify
, &OrgKdeKDirNotifyInterface::enteredDirectory
, this, &InformationPanel::slotEnteredDirectory
);
352 connect(dirNotify
, &OrgKdeKDirNotifyInterface::leftDirectory
, this, &InformationPanel::slotLeftDirectory
);
354 m_content
= new InformationPanelContent(this);
355 connect(m_content
, &InformationPanelContent::urlActivated
, this, &InformationPanel::urlActivated
);
357 QVBoxLayout
* layout
= new QVBoxLayout(this);
358 layout
->setContentsMargins(0, 0, 0, 0);
359 layout
->addWidget(m_content
);
361 m_initialized
= true;