LeechCraft
0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
Toggle main menu visibility
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/future.h
>
17
#include <
util/threads/coro.h
>
18
#include <
interfaces/core/ientitymanager.h
>
19
20
namespace
LC::Util
21
{
22
Task<TempDownload_t>
DownloadAsTemporary
(
IEntityManager
& iem, QUrl url,
DownloadParams
params)
23
{
24
const
auto
& path =
GetTemporaryName
();
25
auto
e =
MakeEntity
(url,
26
path,
27
DoNotSaveInHistory
|
28
Internal
|
29
DoNotNotifyUser
|
30
NotPersistent
);
31
e.Mime_ = std::move (params.
Mime_
);
32
e.Additional_ = std::move (params.
Additional_
);
33
34
const
auto
res = iem.
DelegateEntity
(e);
35
if
(!res)
36
{
37
qWarning () <<
"delegation failed for"
<< url;
38
co_return
IDownload::Error
39
{
40
IDownload::Error::Type::LocalError
,
41
QObject::tr (
"Unable to find a downloader plugin."
)
42
};
43
}
44
45
const
auto
result =
co_await
res.
DownloadResult_
;
46
const
auto
success [[maybe_unused]] =
co_await
WithHandler
(result,
47
[&url] (
const
IDownload::Error
& error)
48
{
49
qWarning () <<
"failed downloading"
<< url << static_cast<int> (error.
Type_
) << error.
Message_
;
50
});
51
52
QFile file { path };
53
auto
removeGuard =
MakeScopeGuard
([&file] { file.remove (); });
54
if
(!file.open (QIODevice::ReadOnly))
55
{
56
qWarning () <<
"unable to open downloaded file"
<< file.errorString ();
57
co_return
IDownload::Error
58
{
59
IDownload::Error::Type::LocalError
,
60
QObject::tr (
"Unable to open local file: %1."
).arg (file.errorString ())
61
};
62
}
63
64
co_return
file.readAll ();
65
}
66
}
IEntityManager
Proxy to core entity manager.
Definition
ientitymanager.h:32
IEntityManager::DelegateEntity
virtual DelegationResult DelegateEntity(LC::Entity entity, QObject *desired=nullptr)=0
Delegates the given entity and returns the delegation result.
LC::Util::Task
Definition
task.h:155
coro.h
downloadhelpers.h
future.h
ientitymanager.h
LC::Util
Definition
icoreproxy.h:34
LC::Util::GetTemporaryName
QString GetTemporaryName(const QString &pattern)
Returns a temporary filename.
Definition
paths.cpp:143
LC::Util::MakeScopeGuard
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
Definition
util.h:155
LC::Util::WithHandler
detail::EitherAwaiter< L, R, F > WithHandler(const Either< L, R > &either, F &&errorHandler)
Definition
either.h:82
LC::Util::DownloadAsTemporary
Task< TempDownload_t > DownloadAsTemporary(IEntityManager &iem, QUrl url, DownloadParams params)
Definition
downloadhelpers.cpp:22
LC::Util::MakeEntity
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition
util.cpp:82
LC::DoNotSaveInHistory
@ DoNotSaveInHistory
Definition
structures.h:36
LC::Internal
@ Internal
Definition
structures.h:54
LC::DoNotNotifyUser
@ DoNotNotifyUser
Definition
structures.h:49
LC::NotPersistent
@ NotPersistent
Definition
structures.h:59
paths.h
either.h
IDownload::Error
Definition
idownload.h:46
IDownload::Error::Type::LocalError
@ LocalError
Definition
idownload.h:60
IDownload::Error::Type_
enum IDownload::Error::Type Type_
IDownload::Error::Message_
QString Message_
Definition
idownload.h:64
IEntityManager::DelegationResult::DownloadResult_
QFuture< IDownload::Result > DownloadResult_
Definition
ientitymanager.h:46
LC::Util::DownloadParams
Definition
downloadhelpers.h:25
LC::Util::DownloadParams::Mime_
QString Mime_
Definition
downloadhelpers.h:26
LC::Util::DownloadParams::Additional_
QVariantMap Additional_
Definition
downloadhelpers.h:27
visitor.h
util.h
src
util
xpc
downloadhelpers.cpp
Generated by
1.17.0