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 foreach (QAction
* action
, customContextMenuActions()) {
189 popup
.addAction(action
);
192 // Open the popup and adjust the settings for the
194 QAction
* action
= popup
.exec(pos
);
199 const bool isChecked
= action
->isChecked();
200 if (action
== previewAction
) {
201 InformationPanelSettings::setPreviewsShown(isChecked
);
202 m_content
->refreshPreview();
203 } else if (action
== configureAction
) {
204 FileMetaDataConfigurationDialog
* dialog
= new FileMetaDataConfigurationDialog(this);
205 dialog
->setDescription(i18nc("@label::textbox",
206 "Select which data should be shown in the information panel:"));
207 dialog
->setItems(m_content
->items());
208 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
210 connect(dialog
, &FileMetaDataConfigurationDialog::destroyed
, m_content
, &InformationPanelContent::refreshMetaData
);
212 if (action
== dateformatAction
) {
213 int dateFormat
= static_cast<int>(isChecked
? Baloo::DateFormats::ShortFormat
: Baloo::DateFormats::LongFormat
);
215 InformationPanelSettings::setDateFormat(dateFormat
);
216 m_content
->refreshMetaData();
220 void InformationPanel::showItemInfo()
228 if (m_fileItem
.isNull() && (m_selection
.count() > 1)) {
229 // The information for a selection of items should be shown
230 m_content
->showItems(m_selection
);
232 // The information for exactly one item should be shown
234 if (!m_fileItem
.isNull()) {
236 } else if (!m_selection
.isEmpty()) {
237 Q_ASSERT(m_selection
.count() == 1);
238 item
= m_selection
.first();
242 // No item is hovered and no selection has been done: provide
243 // an item for the currently shown directory.
244 m_folderStatJob
= KIO::stat(url(), KIO::HideProgressInfo
);
245 if (m_folderStatJob
->uiDelegate()) {
246 KJobWidgets::setWindow(m_folderStatJob
, this);
248 connect(m_folderStatJob
, &KIO::Job::result
,
249 this, &InformationPanel::slotFolderStatFinished
);
251 m_content
->showItem(item
);
256 void InformationPanel::slotFolderStatFinished(KJob
* job
)
258 m_folderStatJob
= nullptr;
259 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
260 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
263 void InformationPanel::slotInfoTimeout()
265 m_shownUrl
= m_urlCandidate
;
266 m_urlCandidate
.clear();
270 void InformationPanel::reset()
272 if (m_invalidUrlCandidate
== m_shownUrl
) {
273 m_invalidUrlCandidate
= QUrl();
275 // The current URL is still invalid. Reset
276 // the content to show the directory URL.
279 m_fileItem
= KFileItem();
284 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
286 if (m_shownUrl
== QUrl::fromLocalFile(source
)) {
287 m_shownUrl
= QUrl::fromLocalFile(dest
);
288 m_fileItem
= KFileItem(m_shownUrl
);
290 if ((m_selection
.count() == 1) && (m_selection
[0].url() == QUrl::fromLocalFile(source
))) {
291 m_selection
[0] = m_fileItem
;
292 // Implementation note: Updating the selection is only required if exactly one
293 // item is selected, as the name of the item is shown. If this should change
294 // in future: Before parsing the whole selection take care to test possible
295 // performance bottlenecks when renaming several hundreds of files.
302 void InformationPanel::slotFilesAdded(const QString
& directory
)
304 if (m_shownUrl
== QUrl::fromLocalFile(directory
)) {
305 // If the 'trash' icon changes because the trash has been emptied or got filled,
306 // the signal filesAdded("trash:/") will be emitted.
307 KFileItem
item(QUrl::fromLocalFile(directory
));
308 requestDelayedItemInfo(item
);
312 void InformationPanel::slotFilesChanged(const QStringList
& files
)
314 foreach (const QString
& fileName
, files
) {
315 if (m_shownUrl
== QUrl::fromLocalFile(fileName
)) {
322 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
324 foreach (const QString
& fileName
, files
) {
325 if (m_shownUrl
== QUrl::fromLocalFile(fileName
)) {
326 // the currently shown item has been removed, show
327 // the parent directory as fallback
334 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
336 if (m_shownUrl
== QUrl::fromLocalFile(directory
)) {
337 KFileItem
item(QUrl::fromLocalFile(directory
));
338 requestDelayedItemInfo(item
);
342 void InformationPanel::slotLeftDirectory(const QString
& directory
)
344 if (m_shownUrl
== QUrl::fromLocalFile(directory
)) {
345 // The signal 'leftDirectory' is also emitted when a media
346 // has been unmounted. In this case no directory change will be
347 // done in Dolphin, but the Information Panel must be updated to
348 // indicate an invalid directory.
353 void InformationPanel::cancelRequest()
355 delete m_folderStatJob
;
356 m_folderStatJob
= nullptr;
359 m_resetUrlTimer
->stop();
360 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
361 // has the smallest interval (see init()), it is not possible that the exceeded timer
362 // would overwrite an information provided by a selection or hovering.
364 m_invalidUrlCandidate
.clear();
365 m_urlCandidate
.clear();
368 bool InformationPanel::isEqualToShownUrl(const QUrl
& url
) const
370 return m_shownUrl
.matches(url
, QUrl::StripTrailingSlash
);
373 void InformationPanel::markUrlAsInvalid()
375 m_invalidUrlCandidate
= m_shownUrl
;
376 m_resetUrlTimer
->start();
379 void InformationPanel::init()
381 m_infoTimer
= new QTimer(this);
382 m_infoTimer
->setInterval(300);
383 m_infoTimer
->setSingleShot(true);
384 connect(m_infoTimer
, &QTimer::timeout
,
385 this, &InformationPanel::slotInfoTimeout
);
387 m_urlChangedTimer
= new QTimer(this);
388 m_urlChangedTimer
->setInterval(200);
389 m_urlChangedTimer
->setSingleShot(true);
390 connect(m_urlChangedTimer
, &QTimer::timeout
,
391 this, &InformationPanel::showItemInfo
);
393 m_resetUrlTimer
= new QTimer(this);
394 m_resetUrlTimer
->setInterval(1000);
395 m_resetUrlTimer
->setSingleShot(true);
396 connect(m_resetUrlTimer
, &QTimer::timeout
,
397 this, &InformationPanel::reset
);
399 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
400 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
402 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
403 QDBusConnection::sessionBus(), this);
404 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FileRenamed
, this, &InformationPanel::slotFileRenamed
);
405 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesAdded
, this, &InformationPanel::slotFilesAdded
);
406 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesChanged
, this, &InformationPanel::slotFilesChanged
);
407 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesRemoved
, this, &InformationPanel::slotFilesRemoved
);
408 connect(dirNotify
, &OrgKdeKDirNotifyInterface::enteredDirectory
, this, &InformationPanel::slotEnteredDirectory
);
409 connect(dirNotify
, &OrgKdeKDirNotifyInterface::leftDirectory
, this, &InformationPanel::slotLeftDirectory
);
411 m_content
= new InformationPanelContent(this);
412 connect(m_content
, &InformationPanelContent::urlActivated
, this, &InformationPanel::urlActivated
);
414 QVBoxLayout
* layout
= new QVBoxLayout(this);
415 layout
->setContentsMargins(0, 0, 0, 0);
416 layout
->addWidget(m_content
);
418 m_initialized
= true;