LeechCraft  0.6.70-13729-g7046a9d2a7
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "downloadhelpers.h"
31 #include <QFuture>
32 #include <QFile>
33 #include <QtDebug>
34 #include <util/sll/either.h>
35 #include <util/sll/visitor.h>
36 #include <util/sys/paths.h>
37 #include <util/xpc/util.h>
38 #include <util/threads/futures.h>
40 
41 namespace LC::Util
42 {
43  std::optional<QFuture<TempResultType_t>> DownloadAsTemporary (IEntityManager *iem,
44  const QUrl& url, DownloadParams params)
45  {
46  const auto& path = Util::GetTemporaryName ();
47  auto e = Util::MakeEntity (url,
48  path,
50  Internal |
53  e.Mime_ = std::move (params.Mime_);
54  e.Additional_ = std::move (params.Additional_);
55 
56  auto res = iem->DelegateEntity (e);
57  if (!res)
58  {
59  qWarning () << Q_FUNC_INFO
60  << "delegation failed for"
61  << url;
62  return {};
63  }
64 
65  return Util::Sequence (params.Context_, res.DownloadResult_) >>
67  {
68  [] (const IDownload::Error& err) { return Util::MakeReadyFuture (TempResultType_t::Left (err)); },
69  [path] (IDownload::Success)
70  {
71  QFile file { path };
72  auto removeGuard = Util::MakeScopeGuard ([&file] { file.remove (); });
73  if (!file.open (QIODevice::ReadOnly))
74  {
75  qWarning () << Q_FUNC_INFO
76  << "unable to open downloaded file"
77  << file.errorString ();
78  return Util::MakeReadyFuture (TempResultType_t::Left ({
80  "unable to open file"
81  }));
82  }
83 
84  return Util::MakeReadyFuture (TempResultType_t::Right (file.readAll ()));
85  }
86  };
87  }
88 
89 }
Proxy to core entity manager.
std::optional< QFuture< TempResultType_t > > DownloadAsTemporary(IEntityManager *iem, const QUrl &url, DownloadParams params)
static Either Left(const L &l)
Definition: either.h:133
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:105
virtual DelegationResult DelegateEntity(LC::Entity entity, QObject *desired=nullptr)=0
Delegates the given entity and returns the delegation result.
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
Definition: util.h:157
static Either Right(const R &r)
Definition: either.h:138
QString GetTemporaryName(const QString &pattern)
Returns a temporary filename.
Definition: paths.cpp:166