LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
downloadhandler.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 "downloadhandler.h"
31 #include <QFile>
32 #include <util/sll/util.h>
33 #include <util/sys/paths.h>
34 #include <util/sll/slotclosure.h>
35 #include <interfaces/idownload.h>
37 #include "util.h"
38 
39 namespace LeechCraft
40 {
41 namespace Util
42 {
44  const QVariantMap& additional,
45  IEntityManager *iem,
46  const DataHandler_t& cont,
47  QObject *parent)
49  {
50  [&]
51  {
52  auto e = MakeEntity (url,
58  e.Additional_ = additional;
59  return e;
60  } (),
61  iem,
62  {
63  [cont] (IDownload::Error error) { cont.Left (error); },
64  [this, cont]
65  {
66  QFile file { E_.Location_ };
67  const auto fileGuard = MakeScopeGuard ([&file] { file.remove (); });
68  if (!file.open (QIODevice::ReadOnly))
69  {
71  return;
72  }
73 
74  cont.Right (file.readAll ());
75  file.remove ();
76  }
77  },
78  parent
79  }
80  {
81  }
82 
84  IEntityManager *iem,
85  const DataHandler_t& cont,
86  QObject *parent)
87  : DownloadHandler { url, {}, iem, cont, parent }
88  {
89  }
90 
92  IEntityManager *iem,
93  const EntityHandler_t& cont,
94  QObject *parent)
95  : QObject { parent }
96  , E_ { e }
97  , Cont_ { cont }
98  {
99  const auto& res = iem->DelegateEntity (e);
100  if (!res.Handler_)
101  throw std::runtime_error ("Cannot delegate entity.");
102 
103  JobId_ = res.ID_;
104  connect (res.Handler_,
105  SIGNAL (jobFinished (int)),
106  this,
107  SLOT (handleFinished (int)));
108  connect (res.Handler_,
109  SIGNAL (jobError (int, IDownload::Error)),
110  this,
111  SLOT (handleError (int, IDownload::Error)));
112  }
113 
114  void DownloadHandler::handleFinished (int id)
115  {
116  if (JobId_ != id)
117  return;
118 
119  Cont_.Right ();
120  deleteLater ();
121  }
122 
123  void DownloadHandler::handleError (int id, IDownload::Error error)
124  {
125  if (JobId_ != id)
126  return;
127 
128  Cont_.Left (error);
129  deleteLater ();
130  }
131 }
132 }
Entity MakeEntity(const QVariant &entity, const QString &location, TaskParameters tp, const QString &mime)
Definition: util.cpp:105
Proxy to core entity manager.
auto Right(Args &&... args) const
Invoke the right function and return its result.
Definition: eithercont.h:124
detail::ScopeGuard< F > MakeScopeGuard(const F &f)
Returns an object performing passed function on scope exit.
Definition: util.h:157
A peir of two functions, typically a continuation and an error handler.
Definition: eithercont.h:51
DownloadHandler(const QUrl &url, const QVariantMap &additional, IEntityManager *iem, const DataHandler_t &, QObject *parent=nullptr)
QString GetTemporaryName(const QString &pattern)
Returns a temporary filename.
Definition: paths.cpp:166
auto Left(Args &&... args) const
Invoke the left function and return its result.
Definition: eithercont.h:105
A message used for inter-plugin communication.
Definition: structures.h:119
QString Location_
Source or destination.
Definition: structures.h:146