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 Class Reference

Network-related functionality. More...

#include <networkfactory.h>

Collaboration diagram for NetworkFactory:
Collaboration graph

Static Public Member Functions

static QString networkErrorText (QNetworkReply::NetworkError error_code)
 Gets human readable text for given network error. More...
 
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. More...
 

Detailed Description

Network-related functionality.

Definition at line 40 of file networkfactory.h.

Member Function Documentation

QNetworkReply::NetworkError NetworkFactory::downloadFile ( const QString &  url,
int  timeout,
QByteArray &  output,
bool  protected_contents = false,
const QString &  username = QString(),
const QString &  password = QString() 
)
static

Performs SYNCHRONOUS download of file with given URL and given timeout.

Parameters
urlUrl.
timeoutDownload timeout.
outputOutput to store data to.
protected_contentsIs destination URL protected?
usernameUsername.
passwordPassword.
Returns
Returns indication of network status after the donwload finished.

Definition at line 112 of file networkfactory.cpp.

117  {
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.

Here is the call graph for this function:

Here is the caller graph for this function:

QString NetworkFactory::networkErrorText ( QNetworkReply::NetworkError  error_code)
static

Gets human readable text for given network error.

Parameters
error_code
Returns
Returns human readable text for given network error.

Definition at line 48 of file networkfactory.cpp.

48  {
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 }

Here is the caller graph for this function:


The documentation for this class was generated from the following files: