LeechCraft 0.6.70-19214-g121e5318a7
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
downloadhelpers.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "downloadhelpers.h"
10#include <QFile>
11#include <QtDebug>
12#include <util/sll/either.h>
13#include <util/sll/visitor.h>
14#include <util/sys/paths.h>
15#include <util/xpc/util.h>
16#include <util/threads/coro.h>
18
19namespace LC::Util
20{
22 {
23 const auto& path = GetTemporaryName ();
24 auto e = MakeEntity (url,
25 path,
27 Internal |
30 e.Mime_ = std::move (params.Mime_);
31 e.Additional_ = std::move (params.Additional_);
32
33 const auto res = iem.DelegateEntity (e);
34 if (!res)
35 {
36 qWarning () << "delegation failed for" << url;
37 co_return IDownload::Error
38 {
40 QObject::tr ("Unable to find a downloader plugin.")
41 };
42 }
43
44 const auto result = co_await res.DownloadResult_;
45 const auto success [[maybe_unused]] = co_await WithHandler (result,
46 [&url] (const IDownload::Error& error)
47 {
48 qWarning () << "failed downloading" << url << static_cast<int> (error.Type_) << error.Message_;
49 });
50
51 QFile file { path };
52 auto removeGuard = MakeScopeGuard ([&file] { file.remove (); });
53 if (!file.open (QIODevice::ReadOnly))
54 {
55 qWarning () << "unable to open downloaded file" << file.errorString ();
56 co_return IDownload::Error
57 {
59 QObject::tr ("Unable to open local file: %1.").arg (file.errorString ())
60 };
61 }
62
63 co_return file.readAll ();
64 }
65}
Proxy to core entity manager.
virtual DelegationResult DelegateEntity(LC::Entity entity, QObject *desired=nullptr)=0
Delegates the given entity and returns the delegation result.
QString GetTemporaryName(const QString &pattern)
Returns a temporary filename.
Definition paths.cpp:143
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
Definition util.h:155
detail::EitherAwaiter< L, R, F > WithHandler(const Either< L, R > &either, F &&errorHandler)
Definition either.h:83
Task< TempDownload_t > DownloadAsTemporary(IEntityManager &iem, QUrl url, DownloadParams params)
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition util.cpp:82
@ DoNotSaveInHistory
Definition structures.h:36
@ Internal
Definition structures.h:54
@ DoNotNotifyUser
Definition structures.h:49
@ NotPersistent
Definition structures.h:59
enum IDownload::Error::Type Type_
QString Message_
Definition idownload.h:64
QFuture< IDownload::Result > DownloadResult_