Messagelib

opensavedfilefolderwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "opensavedfilefolderwidget.h"
8
9#include <KIO/JobUiDelegateFactory>
10#include <KIO/OpenFileManagerWindowJob>
11#include <KIO/OpenUrlJob>
12#include <KLocalizedString>
13#include <KMessageBox>
14
15#include <QTimer>
16
17#include <QAction>
18#include <chrono>
19
20using namespace std::chrono_literals;
21using namespace MessageViewer;
22
23OpenSavedFileFolderWidget::OpenSavedFileFolderWidget(QWidget *parent)
24 : KMessageWidget(parent)
25 , mTimer(new QTimer(this))
26 , mShowFolderAction(new QAction(i18nc("@action - For we don't know if it's one or many attachments", "Open folder where attachment was saved"), this))
27 , mOpenFileAction(new QAction(i18nc("@action", "Open File"), this))
28{
29 setPosition(KMessageWidget::Header);
30 mTimer->setSingleShot(true);
31 mTimer->setInterval(5s); // 5 seconds
32 connect(mTimer, &QTimer::timeout, this, &OpenSavedFileFolderWidget::slotTimeOut);
33 setVisible(false);
34 setCloseButtonVisible(true);
35 setMessageType(Positive);
36 setWordWrap(true);
37 auto action = this->findChild<QAction *>(); // should give us the close action...
38 if (action) {
39 connect(action, &QAction::triggered, this, &OpenSavedFileFolderWidget::slotExplicitlyClosed);
40 }
41
42 connect(mShowFolderAction, &QAction::triggered, this, &OpenSavedFileFolderWidget::slotOpenSavedFileFolder);
43 addAction(mShowFolderAction);
44 connect(mOpenFileAction, &QAction::triggered, this, &OpenSavedFileFolderWidget::slotOpenFile);
45 addAction(mOpenFileAction);
46}
47
48OpenSavedFileFolderWidget::~OpenSavedFileFolderWidget() = default;
49
50void OpenSavedFileFolderWidget::slotExplicitlyClosed()
51{
52 if (mTimer->isActive()) {
53 mTimer->stop();
54 }
55}
56
57void OpenSavedFileFolderWidget::setUrls(const QList<QUrl> &urls, FileType fileType)
58{
59 mUrls = urls;
60 switch (fileType) {
61 case FileType::Attachment:
62 mShowFolderAction->setText(i18ncp("@action", "Open folder where attachment was saved", "Open folder where attachments were saved", mUrls.count()));
63 break;
64 case FileType::Pdf:
65 mShowFolderAction->setText(i18nc("@action", "Open folder where PDF file was saved"));
66 break;
67 }
68}
69
70void OpenSavedFileFolderWidget::slotOpenFile()
71{
72 for (const auto &url : std::as_const(mUrls)) {
73 auto job = new KIO::OpenUrlJob(url);
75 job->setDeleteTemporaryFile(true);
76 connect(job, &KIO::OpenUrlJob::result, this, [this](KJob *job) {
77 if (job->error() == KIO::ERR_USER_CANCELED) {
78 KMessageBox::error(this, i18n("KMail was unable to open the attachment."), job->errorString());
79 }
80 });
81 job->start();
82 }
83}
84
85void OpenSavedFileFolderWidget::slotOpenSavedFileFolder()
86{
87 if (!mUrls.isEmpty()) {
89 slotHideWarning();
90 }
91}
92
93void OpenSavedFileFolderWidget::slotHideWarning()
94{
95 if (mTimer->isActive()) {
96 mTimer->stop();
97 }
99}
100
101void OpenSavedFileFolderWidget::slotShowWarning()
102{
103 if (mTimer->isActive()) {
104 mTimer->stop();
105 }
106 mTimer->start();
107 animatedShow();
108}
109
110void OpenSavedFileFolderWidget::slotTimeOut()
111{
112 animatedHide();
113}
114
115#include "moc_opensavedfilefolderwidget.cpp"
virtual QString errorString() const
int error() const
void result(KJob *job)
virtual Q_SCRIPTABLE void start()=0
void animatedHide()
void animatedShow()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QString i18ncp(const char *context, const char *singular, const char *plural, const TYPE &arg...)
OpenFileManagerWindowJob * highlightInFileManager(const QList< QUrl > &urls, const QByteArray &asn=QByteArray())
KIOCORE_EXPORT KJobUiDelegate * createDefaultJobUiDelegate()
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
void setText(const QString &text)
void triggered(bool checked)
qsizetype count() const const
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool isActive() const const
void start()
void stop()
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.