File indexing completed on 2024-05-12 05:12:55

0001 /*
0002   SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "downloadarticlejob.h"
0008 #include "akregator_debug.h"
0009 #include <KIO/FileCopyJob>
0010 #include <KIO/JobUiDelegate>
0011 #include <MimeTreeParser/AttachmentTemporaryFilesDirs>
0012 #include <QDesktopServices>
0013 #include <QTemporaryFile>
0014 #include <QUrlQuery>
0015 
0016 using namespace Akregator;
0017 
0018 DownloadArticleJob::DownloadArticleJob(QObject *parent)
0019     : QObject(parent)
0020 {
0021 }
0022 
0023 DownloadArticleJob::~DownloadArticleJob()
0024 {
0025     if (mAttachmentTemporaryFile) {
0026         mAttachmentTemporaryFile->removeTempFiles();
0027         mAttachmentTemporaryFile = nullptr;
0028     }
0029 }
0030 
0031 void DownloadArticleJob::forceCleanupTemporaryFile()
0032 {
0033     if (mAttachmentTemporaryFile) {
0034         mAttachmentTemporaryFile->forceCleanTempFiles();
0035         delete mAttachmentTemporaryFile;
0036         mAttachmentTemporaryFile = nullptr;
0037     }
0038 }
0039 
0040 void DownloadArticleJob::start()
0041 {
0042     if (mArticleUrl.isEmpty()) {
0043         deleteLater();
0044         return;
0045     }
0046     if (mTemporaryFile) {
0047         qCDebug(AKREGATOR_LOG) << " There is a problem as we call start twice";
0048         return;
0049     }
0050     mTemporaryFile = new QTemporaryFile(this);
0051     mTemporaryFile->open();
0052     mTemporaryFile->setAutoRemove(false);
0053     mAttachmentTemporaryFile = new MimeTreeParser::AttachmentTemporaryFilesDirs;
0054 
0055     KIO::Job *job = KIO::file_copy(mArticleUrl, QUrl::fromLocalFile(mTemporaryFile->fileName()), -1, KIO::Overwrite);
0056     mAttachmentTemporaryFile->addTempFile(mTemporaryFile->fileName());
0057     connect(job, &KIO::Job::result, this, &DownloadArticleJob::slotUrlSaveResult);
0058 }
0059 
0060 void DownloadArticleJob::setArticleUrl(const QUrl &articleUrl)
0061 {
0062     mArticleUrl = articleUrl;
0063 }
0064 
0065 void DownloadArticleJob::slotUrlSaveResult(KJob *job)
0066 {
0067     if (job->error()) {
0068         auto kiojob = dynamic_cast<KIO::Job *>(job);
0069         if (kiojob && kiojob->uiDelegate()) {
0070             kiojob->uiDelegate()->showErrorMessage();
0071         } else {
0072             qCWarning(AKREGATOR_LOG) << "There is no GUI delegate set for a kjob, and it failed with error:" << job->errorString();
0073         }
0074     } else {
0075         sendAttachment();
0076         deleteLater();
0077     }
0078 }
0079 
0080 void DownloadArticleJob::setTitle(const QString &title)
0081 {
0082     mTitle = title;
0083 }
0084 
0085 void DownloadArticleJob::sendAttachment()
0086 {
0087     QUrlQuery query;
0088     query.addQueryItem(QStringLiteral("subject"), mTitle);
0089     query.addQueryItem(QStringLiteral("body"), mText);
0090     query.addQueryItem(QStringLiteral("attach"), mTemporaryFile->fileName());
0091     QUrl url;
0092     url.setScheme(QStringLiteral("mailto"));
0093     url.setQuery(query);
0094     QDesktopServices::openUrl(url);
0095     deleteLater();
0096 }
0097 
0098 void DownloadArticleJob::setText(const QString &text)
0099 {
0100     mText = text;
0101 }
0102 
0103 #include "moc_downloadarticlejob.cpp"