]>
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"
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"
40 InformationPanel::InformationPanel(QWidget
* parent
) :
44 m_urlChangedTimer(nullptr),
45 m_resetUrlTimer(nullptr),
48 m_invalidUrlCandidate(),
51 m_folderStatJob(nullptr),
56 InformationPanel::~InformationPanel()
60 void InformationPanel::setSelection(const KFileItemList
& selection
)
62 m_selection
= selection
;
63 m_fileItem
= KFileItem();
69 const int count
= selection
.count();
71 if (!isEqualToShownUrl(url())) {
76 if ((count
== 1) && !selection
.first().url().isEmpty()) {
77 m_urlCandidate
= selection
.first().url();
83 void InformationPanel::requestDelayedItemInfo(const KFileItem
& item
)
85 if (!isVisible() || (item
.isNull() && m_fileItem
.isNull())) {
89 if (QApplication::mouseButtons() & Qt::LeftButton
) {
90 // Ignore the request of an item information when a rubberband
91 // selection is ongoing.
98 // The cursor is above the viewport. If files are selected,
99 // show information regarding the selection.
100 if (!m_selection
.isEmpty()) {
101 m_fileItem
= KFileItem();
102 m_infoTimer
->start();
104 } else if (item
.url().isValid() && !isEqualToShownUrl(item
.url())) {
105 // The cursor is above an item that is not shown currently
106 m_urlCandidate
= item
.url();
108 m_infoTimer
->start();
112 bool InformationPanel::urlChanged()
114 if (!url().isValid()) {
125 if (!isEqualToShownUrl(url())) {
127 m_fileItem
= KFileItem();
129 // Update the content with a delay. This gives
130 // the directory lister the chance to show the content
131 // before expensive operations are done to show
133 m_urlChangedTimer
->start();
139 void InformationPanel::showEvent(QShowEvent
* event
)
141 Panel::showEvent(event
);
142 if (!event
->spontaneous()) {
143 if (!m_initialized
) {
144 // do a delayed initialization so that no performance
145 // penalty is given when Dolphin is started with a closed
155 void InformationPanel::resizeEvent(QResizeEvent
* event
)
158 m_urlCandidate
= m_shownUrl
;
159 m_infoTimer
->start();
161 Panel::resizeEvent(event
);
164 void InformationPanel::contextMenuEvent(QContextMenuEvent
* event
)
166 showContextMenu(event
->globalPos());
167 Panel::contextMenuEvent(event
);
170 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")));
181 if (m_inConfigurationMode
) {
182 configureAction
->setEnabled(false);
185 QAction
* dateformatAction
= popup
.addAction(i18nc("@action:inmenu", "Condensed Date"));
186 dateformatAction
->setIcon(QIcon::fromTheme(QStringLiteral("change-date-symbolic")));
187 dateformatAction
->setCheckable(true);
188 dateformatAction
->setChecked(InformationPanelSettings::dateFormat() == static_cast<int>(Baloo::DateFormats::ShortFormat
));
190 popup
.addSeparator();
191 const auto actions
= customContextMenuActions();
192 for (QAction
*action
: actions
) {
193 popup
.addAction(action
);
196 // Open the popup and adjust the settings for the
198 QAction
* action
= popup
.exec(pos
);
203 const bool isChecked
= action
->isChecked();
204 if (action
== previewAction
) {
205 InformationPanelSettings::setPreviewsShown(isChecked
);
206 m_content
->refreshPreview();
207 } else if (action
== configureAction
) {
208 m_inConfigurationMode
= true;
209 m_content
->configureShownProperties();
211 if (action
== dateformatAction
) {
212 int dateFormat
= static_cast<int>(isChecked
? Baloo::DateFormats::ShortFormat
: Baloo::DateFormats::LongFormat
);
214 InformationPanelSettings::setDateFormat(dateFormat
);
215 m_content
->refreshMetaData();
219 void InformationPanel::showItemInfo()
227 if (m_fileItem
.isNull() && (m_selection
.count() > 1)) {
228 // The information for a selection of items should be shown
229 m_content
->showItems(m_selection
);
231 // The information for exactly one item should be shown
233 if (!m_fileItem
.isNull()) {
235 } else if (!m_selection
.isEmpty()) {
236 Q_ASSERT(m_selection
.count() == 1);
237 item
= m_selection
.first();
241 // No item is hovered and no selection has been done: provide
242 // an item for the currently shown directory.
243 m_folderStatJob
= KIO::stat(url(), KIO::HideProgressInfo
);
244 if (m_folderStatJob
->uiDelegate()) {
245 KJobWidgets::setWindow(m_folderStatJob
, this);
247 connect(m_folderStatJob
, &KIO::Job::result
,
248 this, &InformationPanel::slotFolderStatFinished
);
250 m_content
->showItem(item
);
255 void InformationPanel::slotFolderStatFinished(KJob
* job
)
257 m_folderStatJob
= nullptr;
258 const KIO::UDSEntry entry
= static_cast<KIO::StatJob
*>(job
)->statResult();
259 m_content
->showItem(KFileItem(entry
, m_shownUrl
));
262 void InformationPanel::slotInfoTimeout()
264 m_shownUrl
= m_urlCandidate
;
265 m_urlCandidate
.clear();
269 void InformationPanel::reset()
271 if (m_invalidUrlCandidate
== m_shownUrl
) {
272 m_invalidUrlCandidate
= QUrl();
274 // The current URL is still invalid. Reset
275 // the content to show the directory URL.
278 m_fileItem
= KFileItem();
283 void InformationPanel::slotFileRenamed(const QString
& source
, const QString
& dest
)
285 if (m_shownUrl
== QUrl::fromLocalFile(source
)) {
286 m_shownUrl
= QUrl::fromLocalFile(dest
);
287 m_fileItem
= KFileItem(m_shownUrl
);
289 if ((m_selection
.count() == 1) && (m_selection
[0].url() == QUrl::fromLocalFile(source
))) {
290 m_selection
[0] = m_fileItem
;
291 // Implementation note: Updating the selection is only required if exactly one
292 // item is selected, as the name of the item is shown. If this should change
293 // in future: Before parsing the whole selection take care to test possible
294 // performance bottlenecks when renaming several hundreds of files.
301 void InformationPanel::slotFilesAdded(const QString
& directory
)
303 if (m_shownUrl
== QUrl::fromLocalFile(directory
)) {
304 // If the 'trash' icon changes because the trash has been emptied or got filled,
305 // the signal filesAdded("trash:/") will be emitted.
306 KFileItem
item(QUrl::fromLocalFile(directory
));
307 requestDelayedItemInfo(item
);
311 void InformationPanel::slotFilesChanged(const QStringList
& files
)
313 for (const QString
& fileName
: files
) {
314 if (m_shownUrl
== QUrl::fromLocalFile(fileName
)) {
321 void InformationPanel::slotFilesRemoved(const QStringList
& files
)
323 for (const QString
& fileName
: files
) {
324 if (m_shownUrl
== QUrl::fromLocalFile(fileName
)) {
325 // the currently shown item has been removed, show
326 // the parent directory as fallback
333 void InformationPanel::slotEnteredDirectory(const QString
& directory
)
335 if (m_shownUrl
== QUrl::fromLocalFile(directory
)) {
336 KFileItem
item(QUrl::fromLocalFile(directory
));
337 requestDelayedItemInfo(item
);
341 void InformationPanel::slotLeftDirectory(const QString
& directory
)
343 if (m_shownUrl
== QUrl::fromLocalFile(directory
)) {
344 // The signal 'leftDirectory' is also emitted when a media
345 // has been unmounted. In this case no directory change will be
346 // done in Dolphin, but the Information Panel must be updated to
347 // indicate an invalid directory.
352 void InformationPanel::cancelRequest()
354 delete m_folderStatJob
;
355 m_folderStatJob
= nullptr;
358 m_resetUrlTimer
->stop();
359 // Don't reset m_urlChangedTimer. As it is assured that the timeout of m_urlChangedTimer
360 // has the smallest interval (see init()), it is not possible that the exceeded timer
361 // would overwrite an information provided by a selection or hovering.
363 m_invalidUrlCandidate
.clear();
364 m_urlCandidate
.clear();
367 bool InformationPanel::isEqualToShownUrl(const QUrl
& url
) const
369 return m_shownUrl
.matches(url
, QUrl::StripTrailingSlash
);
372 void InformationPanel::markUrlAsInvalid()
374 m_invalidUrlCandidate
= m_shownUrl
;
375 m_resetUrlTimer
->start();
378 void InformationPanel::init()
380 m_infoTimer
= new QTimer(this);
381 m_infoTimer
->setInterval(300);
382 m_infoTimer
->setSingleShot(true);
383 connect(m_infoTimer
, &QTimer::timeout
,
384 this, &InformationPanel::slotInfoTimeout
);
386 m_urlChangedTimer
= new QTimer(this);
387 m_urlChangedTimer
->setInterval(200);
388 m_urlChangedTimer
->setSingleShot(true);
389 connect(m_urlChangedTimer
, &QTimer::timeout
,
390 this, &InformationPanel::showItemInfo
);
392 m_resetUrlTimer
= new QTimer(this);
393 m_resetUrlTimer
->setInterval(1000);
394 m_resetUrlTimer
->setSingleShot(true);
395 connect(m_resetUrlTimer
, &QTimer::timeout
,
396 this, &InformationPanel::reset
);
398 Q_ASSERT(m_urlChangedTimer
->interval() < m_infoTimer
->interval());
399 Q_ASSERT(m_urlChangedTimer
->interval() < m_resetUrlTimer
->interval());
401 org::kde::KDirNotify
* dirNotify
= new org::kde::KDirNotify(QString(), QString(),
402 QDBusConnection::sessionBus(), this);
403 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FileRenamed
, this, &InformationPanel::slotFileRenamed
);
404 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesAdded
, this, &InformationPanel::slotFilesAdded
);
405 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesChanged
, this, &InformationPanel::slotFilesChanged
);
406 connect(dirNotify
, &OrgKdeKDirNotifyInterface::FilesRemoved
, this, &InformationPanel::slotFilesRemoved
);
407 connect(dirNotify
, &OrgKdeKDirNotifyInterface::enteredDirectory
, this, &InformationPanel::slotEnteredDirectory
);
408 connect(dirNotify
, &OrgKdeKDirNotifyInterface::leftDirectory
, this, &InformationPanel::slotLeftDirectory
);
410 m_content
= new InformationPanelContent(this);
411 connect(m_content
, &InformationPanelContent::urlActivated
, this, &InformationPanel::urlActivated
);
412 connect(m_content
, &InformationPanelContent::configurationFinished
, this, [this]() { m_inConfigurationMode
= false; });
414 QVBoxLayout
* layout
= new QVBoxLayout(this);
415 layout
->setContentsMargins(0, 0, 0, 0);
416 layout
->addWidget(m_content
);
418 m_initialized
= true;