File indexing completed on 2024-05-12 16:27:21

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "messageattachmentdownloadandsavejob.h"
0008 #include "common/delegateutil.h"
0009 #include "rocketchataccount.h"
0010 #include "ruqolawidgets_debug.h"
0011 #include <KLocalizedString>
0012 #include <QProgressDialog>
0013 
0014 MessageAttachmentDownloadAndSaveJob::MessageAttachmentDownloadAndSaveJob(QObject *parent)
0015     : QObject{parent}
0016 {
0017     connect(this, &MessageAttachmentDownloadAndSaveJob::downloadDone, this, &MessageAttachmentDownloadAndSaveJob::slotDownloadDone);
0018 }
0019 
0020 MessageAttachmentDownloadAndSaveJob::~MessageAttachmentDownloadAndSaveJob() = default;
0021 
0022 void MessageAttachmentDownloadAndSaveJob::slotDownloadDone(const QString &path)
0023 {
0024     switch (mInfo.actionType) {
0025     case MessageAttachmentDownloadAndSaveJob::ActionType::DownloadAndSave:
0026         DelegateUtil::saveFile(mInfo.parentWidget, path, saveFileString());
0027         break;
0028     case MessageAttachmentDownloadAndSaveJob::ActionType::DownloadOnly:
0029         Q_EMIT attachmentFileDownloadDone(path);
0030         break;
0031     case MessageAttachmentDownloadAndSaveJob::ActionType::Unknown:
0032         qCWarning(RUQOLAWIDGETS_LOG) << "ActionType is unknown. It's a bug";
0033         break;
0034     }
0035     slotDownloadCancel();
0036 }
0037 
0038 bool MessageAttachmentDownloadAndSaveJob::canStart() const
0039 {
0040     return mInfo.isValid();
0041 }
0042 
0043 void MessageAttachmentDownloadAndSaveJob::slotFileDownloaded(const QString &filePath, const QUrl &cacheAttachmentUrl)
0044 {
0045     qCDebug(RUQOLAWIDGETS_LOG) << "File Downloaded : " << filePath << " cacheImageUrl " << cacheAttachmentUrl;
0046     if (filePath == QUrl(mInfo.attachmentPath).toString()) {
0047         const QString cacheAttachmentUrlPath{cacheAttachmentUrl.toLocalFile()};
0048         Q_EMIT downloadDone(cacheAttachmentUrlPath);
0049     }
0050 }
0051 
0052 QString MessageAttachmentDownloadAndSaveJob::saveFileString() const
0053 {
0054     QString str;
0055     switch (mInfo.attachmentType) {
0056     case MessageAttachmentDownloadAndSaveJob::AttachmentType::Unknown:
0057         break;
0058     case MessageAttachmentDownloadAndSaveJob::AttachmentType::Image:
0059         str = i18n("Save Image");
0060         break;
0061     case MessageAttachmentDownloadAndSaveJob::AttachmentType::Video:
0062         str = i18n("Save Video");
0063         break;
0064     case MessageAttachmentDownloadAndSaveJob::AttachmentType::Sound:
0065         str = i18n("Save Sound");
0066         break;
0067     }
0068     return str;
0069 }
0070 
0071 void MessageAttachmentDownloadAndSaveJob::assignProgressDialogStr(QProgressDialog *progressDialog)
0072 {
0073     switch (mInfo.attachmentType) {
0074     case MessageAttachmentDownloadAndSaveJob::AttachmentType::Unknown:
0075         break;
0076     case MessageAttachmentDownloadAndSaveJob::AttachmentType::Image:
0077         progressDialog->setWindowTitle(i18nc("@title:window", "Download Image"));
0078         progressDialog->setLabelText(i18n("Download Image..."));
0079         break;
0080     case MessageAttachmentDownloadAndSaveJob::AttachmentType::Video:
0081         progressDialog->setWindowTitle(i18nc("@title:window", "Download Video"));
0082         progressDialog->setLabelText(i18n("Download Video..."));
0083         break;
0084     case MessageAttachmentDownloadAndSaveJob::AttachmentType::Sound:
0085         progressDialog->setWindowTitle(i18nc("@title:window", "Download Sound"));
0086         progressDialog->setLabelText(i18n("Download Sound..."));
0087         break;
0088     }
0089 }
0090 
0091 void MessageAttachmentDownloadAndSaveJob::slotDownloadCancel()
0092 {
0093     if (mProgressDialogBox) {
0094         mProgressDialogBox->hide();
0095         mProgressDialogBox->deleteLater();
0096     }
0097     deleteLater();
0098 }
0099 
0100 void MessageAttachmentDownloadAndSaveJob::start()
0101 {
0102     if (!canStart()) {
0103         qCWarning(RUQOLAWIDGETS_LOG) << "Attachment url empty";
0104         deleteLater();
0105         return;
0106     }
0107     if (!mRocketChatAccount) {
0108         qCWarning(RUQOLAWIDGETS_LOG) << "mRocketChatAccount is empty. It's a bug";
0109         deleteLater();
0110         return;
0111     }
0112 
0113     if (mInfo.needToDownloadAttachment) {
0114         if (mRocketChatAccount) {
0115             mProgressDialogBox = new QProgressDialog(mInfo.parentWidget);
0116             assignProgressDialogStr(mProgressDialogBox);
0117             mProgressDialogBox->reset();
0118             mProgressDialogBox->setRange(0, 0);
0119             mProgressDialogBox->setValue(0);
0120             mProgressDialogBox->setModal(true);
0121             mProgressDialogBox->setAutoClose(false);
0122             mProgressDialogBox->setAutoReset(false);
0123             mProgressDialogBox->setMinimumDuration(0);
0124             connect(mProgressDialogBox, &QProgressDialog::canceled, this, &MessageAttachmentDownloadAndSaveJob::slotDownloadCancel);
0125             connect(mRocketChatAccount, &RocketChatAccount::fileDownloaded, this, &MessageAttachmentDownloadAndSaveJob::slotFileDownloaded);
0126             (void)mRocketChatAccount->attachmentUrlFromLocalCache(mInfo.attachmentPath);
0127         }
0128     } else {
0129         Q_EMIT downloadDone(mRocketChatAccount->attachmentUrlFromLocalCache(mInfo.attachmentPath).toLocalFile());
0130     }
0131 }
0132 
0133 RocketChatAccount *MessageAttachmentDownloadAndSaveJob::rocketChatAccount() const
0134 {
0135     return mRocketChatAccount;
0136 }
0137 
0138 void MessageAttachmentDownloadAndSaveJob::setRocketChatAccount(RocketChatAccount *newRocketChatAccount)
0139 {
0140     mRocketChatAccount = newRocketChatAccount;
0141 }
0142 
0143 MessageAttachmentDownloadAndSaveJob::MessageAttachmentDownloadJobInfo MessageAttachmentDownloadAndSaveJob::info() const
0144 {
0145     return mInfo;
0146 }
0147 
0148 void MessageAttachmentDownloadAndSaveJob::setInfo(const MessageAttachmentDownloadJobInfo &newInfo)
0149 {
0150     mInfo = newInfo;
0151 }
0152 
0153 bool MessageAttachmentDownloadAndSaveJob::MessageAttachmentDownloadJobInfo::isValid() const
0154 {
0155     if (attachmentType == MessageAttachmentDownloadAndSaveJob::AttachmentType::Unknown) {
0156         qCWarning(RUQOLAWIDGETS_LOG) << "Attachment type not defined";
0157         return false;
0158     }
0159     return !attachmentPath.isEmpty();
0160 }
0161 
0162 #include "moc_messageattachmentdownloadandsavejob.cpp"