BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
networkfactory.cpp
1 /*
2  Copyright (c) 2012, BuildmLearn Contributors listed at http://buildmlearn.org/people/
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 
11  * Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  * Neither the name of the BuildmLearn nor the names of its
16  contributors may be used to endorse or promote products derived from
17  this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 
32 #include "network-web/networkfactory.h"
33 
34 #include "definitions/definitions.h"
35 #include "miscellaneous/settings.h"
36 #include "network-web/silentnetworkaccessmanager.h"
37 
38 #include <QEventLoop>
39 #include <QTimer>
40 #include <QIcon>
41 #include <QPixmap>
42 #include <QTextDocument>
43 
44 
45 NetworkFactory::NetworkFactory() {
46 }
47 
48 QString NetworkFactory::networkErrorText(QNetworkReply::NetworkError error_code) {
49  switch (error_code) {
50  case QNetworkReply::ProtocolUnknownError:
51  case QNetworkReply::ProtocolFailure:
52  //: Network status.
53  return tr("protocol error");
54 
55  case QNetworkReply::HostNotFoundError:
56  //: Network status.
57  return tr("host not found");
58 
59  case QNetworkReply::RemoteHostClosedError:
60  case QNetworkReply::ConnectionRefusedError:
61  //: Network status.
62  return tr("connection refused");
63 
64  case QNetworkReply::TimeoutError:
65  case QNetworkReply::ProxyTimeoutError:
66  //: Network status.
67  return tr("connection timed out");
68 
69  case QNetworkReply::SslHandshakeFailedError:
70  //: Network status.
71  return tr("SSL handshake failed");
72 
73  case QNetworkReply::ProxyConnectionClosedError:
74  case QNetworkReply::ProxyConnectionRefusedError:
75  //: Network status.
76  return tr("proxy server connection refused");
77 
78  case QNetworkReply::TemporaryNetworkFailureError:
79  //: Network status.
80  return tr("temporary failure");
81 
82  case QNetworkReply::AuthenticationRequiredError:
83  //: Network status.
84  return tr("authentication failed");
85 
86  case QNetworkReply::ProxyAuthenticationRequiredError:
87  //: Network status.
88  return tr("proxy authentication required");
89 
90  case QNetworkReply::ProxyNotFoundError:
91  //: Network status.
92  return tr("proxy server not found");
93 
94  case QNetworkReply::NoError:
95  //: Network status.
96  return tr("success");
97 
98  case QNetworkReply::UnknownContentError:
99  //: Network status.
100  return tr("uknown content");
101 
102  case QNetworkReply::ContentNotFoundError:
103  //: Network status.
104  return tr("content not found");
105 
106  default:
107  //: Network status.
108  return tr("unknown error");
109  }
110 }
111 
112 QNetworkReply::NetworkError NetworkFactory::downloadFile(const QString &url,
113  int timeout,
114  QByteArray &output,
115  bool protected_contents,
116  const QString &username,
117  const QString &password) {
118  // Original asynchronous behavior of QNetworkAccessManager
119  // is replaced by synchronous behavior in order to make
120  // process of downloading of a file easier to understand.
121 
122  // Make necessary variables.
124  QEventLoop loop;
125  QTimer timer;
126  QNetworkRequest request;
127  QNetworkReply *reply;
128  QObject originatingObject;
129 
130  // Set credential information as originating object.
131  originatingObject.setProperty("protected", protected_contents);
132  originatingObject.setProperty("username", username);
133  originatingObject.setProperty("password", password);
134  request.setOriginatingObject(&originatingObject);
135 
136  // Set url for this reques.
137  request.setUrl(url);
138 
139  // Create necessary connections.
140  // TODO: Edited, maybe remove this line.
141  QObject::connect(qApp, SIGNAL(aboutToQuit()), &loop, SLOT(quit()));
142  QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
143  QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
144 
145  forever {
146  // This timer fires just ONCE.
147  timer.setSingleShot(true);
148 
149  // Try to open communication channel.
150  reply = manager.get(request);
151 
152  // Start the timeout timer.
153  timer.start(timeout);
154 
155  // Enter the event loop.
156  loop.exec();
157 
158  // At this point one of two things happened:
159  // a) file download was completed,
160  // b) communication timed-out.
161 
162  if (timer.isActive()) {
163  // Download is complete because timer is still running.
164  timer.stop();
165  }
166  else {
167  if (reply != NULL) {
168  delete reply;
169  reply = NULL;
170  }
171 
172  // Timer already fired. Download is NOT successful.
173  return QNetworkReply::TimeoutError;
174  }
175 
176  // In this phase, some part of downloading process is completed.
177  QUrl redirection_url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
178 
179  if (redirection_url.isValid()) {
180  // Communication indicates that HTTP redirection is needed.
181  // Setup redirection URL and download again.
182  request.setUrl(redirection_url);
183 
184  delete reply;
185  reply = NULL;
186  }
187  else {
188  // No redirection is indicated. Final file is obtained
189  // in our "reply" object.
190  break;
191  }
192  }
193 
194  // Read the data into output buffer.
195  output = reply->readAll();
196 
197  QNetworkReply::NetworkError reply_error = reply->error();
198 
199  qDebug("File '%s' fetched with status '%s' (code %d).",
200  qPrintable(url),
201  qPrintable(networkErrorText(reply_error)),
202  reply_error);
203 
204  // Delete needed stuff and exit.
205  if (reply != NULL) {
206  delete reply;
207  reply = NULL;
208  }
209 
210  return reply_error;
211 }
Network access manager with supressed authentication dialogs.
static QString networkErrorText(QNetworkReply::NetworkError error_code)
Gets human readable text for given network error.
static QNetworkReply::NetworkError downloadFile(const QString &url, int timeout, QByteArray &output, bool protected_contents=false, const QString &username=QString(), const QString &password=QString())
Performs SYNCHRONOUS download of file with given URL and given timeout.