Messagelib

opensavedfilefolderwidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2014-2022 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "opensavedfilefolderwidget.h"
8 
9 #include <KIO/JobUiDelegate>
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 
20 using namespace std::chrono_literals;
21 using namespace MessageViewer;
22 
23 OpenSavedFileFolderWidget::OpenSavedFileFolderWidget(QWidget *parent)
24  : KMessageWidget(parent)
25  , mTimer(new QTimer(this))
26  , mShowFolderAction(new QAction(i18nc("@action", "Open folder where attachment was saved"), this))
27  , mOpenFileAction(new QAction(i18nc("@action", "Open File"), this))
28 {
29  mTimer->setSingleShot(true);
30  mTimer->setInterval(5s); // 5 seconds
31  connect(mTimer, &QTimer::timeout, this, &OpenSavedFileFolderWidget::slotTimeOut);
32  setVisible(false);
33  setCloseButtonVisible(true);
34  setMessageType(Positive);
35  setWordWrap(true);
36  auto action = this->findChild<QAction *>(); // should give us the close action...
37  if (action) {
38  connect(action, &QAction::triggered, this, &OpenSavedFileFolderWidget::slotExplicitlyClosed);
39  }
40 
41  connect(mShowFolderAction, &QAction::triggered, this, &OpenSavedFileFolderWidget::slotOpenSavedFileFolder);
42  addAction(mShowFolderAction);
43  connect(mOpenFileAction, &QAction::triggered, this, &OpenSavedFileFolderWidget::slotOpenFile);
44  addAction(mOpenFileAction);
45 }
46 
47 OpenSavedFileFolderWidget::~OpenSavedFileFolderWidget() = default;
48 
49 void OpenSavedFileFolderWidget::slotExplicitlyClosed()
50 {
51  if (mTimer->isActive()) {
52  mTimer->stop();
53  }
54 }
55 
56 void OpenSavedFileFolderWidget::setUrls(const QList<QUrl> &urls, FileType fileType)
57 {
58  mUrls = urls;
59  switch (fileType) {
60  case FileType::Attachment:
61  mShowFolderAction->setText(i18np("Open folder where attachment was saved", "Open folder where attachments were saved", mUrls.count()));
62  break;
63  case FileType::Pdf:
64  mShowFolderAction->setText(i18n("Open folder where PDF file was saved"));
65  break;
66  }
67 }
68 
69 void OpenSavedFileFolderWidget::slotOpenFile()
70 {
71  for (const auto &url : std::as_const(mUrls)) {
72  auto job = new KIO::OpenUrlJob(url);
73  job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this));
74  job->setDeleteTemporaryFile(true);
75  connect(job, &KIO::OpenUrlJob::result, this, [this](KJob *job) {
76  if (job->error() == KIO::ERR_USER_CANCELED) {
77  KMessageBox::sorry(this, i18n("KMail was unable to open the attachment."), job->errorString());
78  }
79  });
80  job->start();
81  }
82 }
83 
84 void OpenSavedFileFolderWidget::slotOpenSavedFileFolder()
85 {
86  if (!mUrls.isEmpty()) {
88  slotHideWarning();
89  }
90 }
91 
92 void OpenSavedFileFolderWidget::slotHideWarning()
93 {
94  if (mTimer->isActive()) {
95  mTimer->stop();
96  }
97  animatedHide();
98 }
99 
100 void OpenSavedFileFolderWidget::slotShowWarning()
101 {
102  if (mTimer->isActive()) {
103  mTimer->stop();
104  }
105  mTimer->start();
106  animatedShow();
107 }
108 
109 void OpenSavedFileFolderWidget::slotTimeOut()
110 {
111  animatedHide();
112 }
virtual Q_SCRIPTABLE void start()=0
OpenFileManagerWindowJob * highlightInFileManager(const QList< QUrl > &urls, const QByteArray &asn=QByteArray())
void result(KJob *job)
QString i18n(const char *text, const TYPE &arg...)
void timeout()
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
void triggered(bool checked)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu May 26 2022 03:52:54 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.