LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
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 <QFuture>
11 #include <QFile>
12 #include <QtDebug>
13 #include <util/sll/either.h>
14 #include <util/sll/visitor.h>
15 #include <util/sys/paths.h>
16 #include <util/xpc/util.h>
17 #include <util/threads/futures.h>
19 
20 namespace LC::Util
21 {
22  std::optional<QFuture<TempResultType_t>> DownloadAsTemporary (IEntityManager *iem,
23  const QUrl& url, DownloadParams params)
24  {
25  const auto& path = Util::GetTemporaryName ();
26  auto e = Util::MakeEntity (url,
27  path,
29  Internal |
32  e.Mime_ = std::move (params.Mime_);
33  e.Additional_ = std::move (params.Additional_);
34 
35  auto res = iem->DelegateEntity (e);
36  if (!res)
37  {
38  qWarning () << Q_FUNC_INFO
39  << "delegation failed for"
40  << url;
41  return {};
42  }
43 
44  return Util::Sequence (params.Context_, res.DownloadResult_) >>
46  {
47  [] (const IDownload::Error& err) { return Util::MakeReadyFuture (TempResultType_t::Left (err)); },
48  [path] (IDownload::Success)
49  {
50  QFile file { path };
51  auto removeGuard = Util::MakeScopeGuard ([&file] { file.remove (); });
52  if (!file.open (QIODevice::ReadOnly))
53  {
54  qWarning () << Q_FUNC_INFO
55  << "unable to open downloaded file"
56  << file.errorString ();
57  return Util::MakeReadyFuture (TempResultType_t::Left ({
59  "unable to open file"
60  }));
61  }
62 
63  return Util::MakeReadyFuture (TempResultType_t::Right (file.readAll ()));
64  }
65  };
66  }
67 
68 }
visitor.h
LC::DoNotNotifyUser
@ DoNotNotifyUser
Definition: structures.h:48
LC::Util::DownloadAsTemporary
std::optional< QFuture< TempResultType_t > > DownloadAsTemporary(IEntityManager *iem, const QUrl &url, DownloadParams params)
Definition: downloadhelpers.cpp:28
LC::Util::Either::Right
static Either Right(const R &r)
Definition: either.h:132
LC::DoNotSaveInHistory
@ DoNotSaveInHistory
Definition: structures.h:35
downloadhelpers.h
LC::Util
Definition: icoreproxy.h:33
LC::NotPersistent
@ NotPersistent
Definition: structures.h:58
futures.h
either.h
LC::Util::MakeEntity
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:82
LC::Util::Either::Left
static Either Left(const L &l)
Definition: either.h:127
paths.h
util.h
LC::Util::MakeScopeGuard
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
Definition: util.h:148
IEntityManager::DelegateEntity
virtual DelegationResult DelegateEntity(LC::Entity entity, QObject *desired=nullptr)=0
Delegates the given entity and returns the delegation result.
IEntityManager
Proxy to core entity manager.
Definition: ientitymanager.h:31
IDownload::Success
Definition: idownload.h:72
IDownload::Error
Definition: idownload.h:45
ientitymanager.h
IDownload::Error::Type::LocalError
@ LocalError
LC::Util::GetTemporaryName
QString GetTemporaryName(const QString &pattern)
Returns a temporary filename.
Definition: paths.cpp:149
LC::Util::Visitor
Visitor(Args &&...) -> Visitor< Void, Args... >
LC::Internal
@ Internal
Definition: structures.h:53