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"
25 #include <KIO/JobUiDelegate>
26 #include <KJobWidgets>
28 #include <KLocalizedString>
30 #include <Baloo/FileMetaDataWidget>
32 #include <QApplication>
34 #include <QVBoxLayout>
38 #include "dolphin_informationpanelsettings.h"
39 #include "filemetadataconfigurationdialog.h"
41 InformationPanel::InformationPanel(QWidget
* parent
) :
45 m_urlChangedTimer(nullptr),
46 m_resetUrlTimer(nullptr),
49 m_invalidUrlCandidate(),
52 m_folderStatJob(nullptr),
57 InformationPanel::~InformationPanel()
61 void InformationPanel::setSelection(const KFileItemList
& selection
)
63 m_selection
= selection
;
64 m_fileItem
= KFileItem();
70 const int count
= selection
.count();
72 if (!isEqualToShownUrl(url())) {
77 if ((count
== 1) && !selection
.first().url().isEmpty()) {
78 m_urlCandidate
= selection
.first().url();
84 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
86 if (!isVisible() || (item
.isNull() && m_fileItem
.isNull())) {
90 if (QApplication::mouseButtons() & Qt::LeftButton
) {
91 // Ignore the request of an item information when a rubberband
92 // selection is ongoing.
99 // The cursor is above the viewport. If files are selected,
100 // show information regarding the selection.
101 if (!m_selection
.isEmpty()) {
102 m_fileItem
= KFileItem();
103 m_infoTimer
->start();
105 } else if (item
.url().isValid() && !isEqualToShownUrl(item
.url())) {
106 // The cursor is above an item that is not shown currently
107 m_urlCandidate
= item
.url();
109 m_infoTimer
->start();
113 bool InformationPanel::urlChanged()
115 if (!url().isValid()) {
126 if (!isEqualToShownUrl(url())) {
128 m_fileItem
= KFileItem();
130 // Update the content with a delay. This gives
131 // the directory lister the chance to show the content
132 // before expensive operations are done to show
134 m_urlChangedTimer
->start();
140 void InformationPanel::showEvent(QShowEvent
* event
)
142 Panel::showEvent(event
);
143 if (!event
->spontaneous()) {
144 if (!m_initialized
) {
145 // do a delayed initialization so that no performance
146 // penalty is given when Dolphin is started with a closed
156 void InformationPanel::resizeEvent(QResizeEvent
* event
)
159 m_urlCandidate
= m_shownUrl
;
160 m_infoTimer
->start();
162 Panel::resizeEvent(event
);
165 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
167 showContextMenu(event
->globalPos());
168 Panel::contextMenuEvent(event
);
171 void InformationPanel::showContextMenu(const QPoint
&pos
) {
174 QAction
* previewAction
= popup
.addAction(i18nc("@action:inmenu", "Preview"));
175 previewAction
->setIcon(QIcon::fromTheme(QStringLiteral("view-preview")));
176 previewAction
->setCheckable(true);
177 previewAction
->setChecked(InformationPanelSettings::previewsShown());
179 QAction
* configureAction
= popup
.addAction(i18nc("@action:inmenu", "Configure..."));
180 configureAction
->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
182 QAction
* dateformatAction
= popup
.addAction(i18nc("@action:inmenu", "Condensed Date"));
183 dateformatAction
->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
184 dateformatAction
->setCheckable(true);
185 dateformatAction
->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat
));
187 popup
.addSeparator();
188 const auto actions
= customContextMenuActions();
189 for (QAction
*action
: actions
) {
190 popup
.addAction(action
);
193 // Open the popup and adjust the settings for the
195 QAction
* action
= popup
.exec(pos
);
200 const bool isChecked
= action
->isChecked();
201 if (action
== previewAction
) {
202 InformationPanelSettings::setPreviewsShown(isChecked
);
203 m_content
->refreshPreview();
204 } else if (action
== configureAction
) {
205 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog(this);
206 dialog
->setDescription(i18nc("@label::textbox",
207 "Select which data should be shown in the information panel:"));
208 dialog
->setItems(m_content
->items());
209 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
211 connect(dialog
, &FileMetaDataConfigurationDialog::destroyed
, m_content
, &InformationPanelContent::refreshMetaData
);
213 if (action
== dateformatAction
) {
214 int dateFormat
= static_cast<int>(isChecked
? Baloo::DateFormats::ShortFormat
: Baloo::DateFormats::LongFormat
);
216 InformationPanelSettings::setDateFormat(dateFormat
);
217 m_content
->refreshMetaData();
221 void InformationPanel::showItemInfo()
229 if (m_fileItem
.isNull() && (m_selection
.count() > 1)) {
230 // The information for a selection of items should be shown
231 m_content
->showItems(m_selection
);
233 // The information for exactly one item should be shown
235 if (!m_fileItem
.isNull()) {
237 } else if (!m_selection
.isEmpty()) {
238 Q_ASSERT(m_selection
.count() == 1);
239 item
= m_selection
.first();
243 // No item is hovered and no selection has been done: provide
244 // an item for the currently shown directory.
245 m_folderStatJob
= KIO::stat(url(), KIO::HideProgressInfo
);
246 if (m_folderStatJob
->uiDelegate()) {
247 KJobWidgets::setWindow(m_folderStatJob
, this);
249 connect(m_folderStatJob
, &KIO::Job::result
,
250 this, &InformationPanel::slotFolderStatFinished
);
252 m_content
->showItem(item
);
257 void InformationPanel::slotFolderStatFinished(KJob
* job
)
259 m_folderStatJob
= nullptr;
260 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
261 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
264 void InformationPanel::slotInfoTimeout()
266 m_shownUrl
= m_urlCandidate
;
267 m_urlCandidate
.clear();
271 void InformationPanel::reset()
273 if (m_invalidUrlCandidate
== m_shownUrl
) {
274 m_invalidUrlCandidate
= QUrl();
276 // The current URL is still invalid. Reset
277 // the content to show the directory URL.
280 m_fileItem
= KFileItem();
285 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
287 if (m_shownUrl
== QUrl::fromLocalFile(source
)) {
288 m_shownUrl
= QUrl::fromLocalFile(dest
);
289 m_fileItem
= KFileItem(m_shownUrl
);
291 if ((m_selection
.count() == 1) && (m_selection
[0].url() == QUrl::fromLocalFile(source
))) {
292 m_selection
[0] = m_fileItem
;
293 // Implementation note: Updating the selection is only required if exactly one
294 // item is selected, as the name of the item is shown. If this should change
295 // in future: Before parsing the whole selection take care to test possible
296 // performance bottlenecks when renaming several hundreds of files.
303 void InformationPanel::slotFilesAdded(const QString
& directory
)
305 if (m_shownUrl
== QUrl::fromLocalFile(directory
)) {
306 // If the 'trash' icon changes because the trash has been emptied or got filled,
307 // the signal filesAdded("trash:/") will be emitted.
308 KFileItem
item(QUrl::fromLocalFile(directory
));
309 requestDelayedItemInfo(item
);
313 void InformationPanel::slotFilesChanged(const QStringList
& files
)
315 for (const QString
& fileName
: files
) {
316 if (m_shownUrl
== QUrl::fromLocalFile(fileName
)) {
323 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
325 for (const QString
& fileName
: files
) {
326 if (m_shownUrl
== QUrl::fromLocalFile(fileName
)) {
327 // the currently shown item has been removed, show
328 // the parent directory as fallback
335 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
337 if (m_shownUrl
== QUrl::fromLocalFile(directory
)) {
338 KFileItem
item(QUrl::fromLocalFile(directory
));
339 requestDelayedItemInfo(item
);
343 void InformationPanel::slotLeftDirectory(const QString
& directory
)
345 if (m_shownUrl
== QUrl::fromLocalFile(directory
)) {
346 // The signal 'leftDirectory' is also emitted when a media
347 // has been unmounted. In this case no directory change will be
348 // done in Dolphin, but the Information Panel must be updated to
349 // indicate an invalid directory.
354 void InformationPanel::cancelRequest()
356 delete m_folderStatJob
;
357 m_folderStatJob
= nullptr;
360 m_resetUrlTimer
->stop();
361 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
362 // has the smallest interval (see init()), it is not possible that the exceeded timer
363 // would overwrite an information provided by a selection or hovering.
365 m_invalidUrlCandidate
.clear();
366 m_urlCandidate
.clear();
369 bool InformationPanel::isEqualToShownUrl(const QUrl
& url
) const
371 return m_shownUrl
.matches(url
, QUrl::StripTrailingSlash
);
374 void InformationPanel::markUrlAsInvalid()
376 m_invalidUrlCandidate
= m_shownUrl
;
377 m_resetUrlTimer
->start();
380 void InformationPanel::init()
382 m_infoTimer
= new QTimer(this);
383 m_infoTimer
->setInterval(300);
384 m_infoTimer
->setSingleShot(true);
385 connect(m_infoTimer
, &QTimer::timeout
,
386 this, &InformationPanel::slotInfoTimeout
);
388 m_urlChangedTimer
= new QTimer(this);
389 m_urlChangedTimer
->setInterval(200);
390 m_urlChangedTimer
->setSingleShot(true);
391 connect(m_urlChangedTimer
, &QTimer::timeout
,
392 this, &InformationPanel::showItemInfo
);
394 m_resetUrlTimer
= new QTimer(this);
395 m_resetUrlTimer
->setInterval(1000);
396 m_resetUrlTimer
->setSingleShot(true);
397 connect(m_resetUrlTimer
, &QTimer::timeout
,
398 this, &InformationPanel::reset
);
400 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
401 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
403 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
404 QDBusConnection::sessionBus(), this);
405 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FileRenamed
, this, &InformationPanel::slotFileRenamed
);
406 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesAdded
, this, &InformationPanel::slotFilesAdded
);
407 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesChanged
, this, &InformationPanel::slotFilesChanged
);
408 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesRemoved
, this, &InformationPanel::slotFilesRemoved
);
409 connect(dirNotify
, &OrgKdeKDirNotifyInterface::enteredDirectory
, this, &InformationPanel::slotEnteredDirectory
);
410 connect(dirNotify
, &OrgKdeKDirNotifyInterface::leftDirectory
, this, &InformationPanel::slotLeftDirectory
);
412 m_content
= new InformationPanelContent(this);
413 connect(m_content
, &InformationPanelContent::urlActivated
, this, &InformationPanel::urlActivated
);
415 QVBoxLayout
* layout
= new QVBoxLayout(this);
416 layout
->setContentsMargins(0, 0, 0, 0);
417 layout
->addWidget(m_content
);
419 m_initialized
= true;