You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.7 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. *
  3. * This file is part of QMapControl,
  4. * an open-source cross-platform map widget
  5. *
  6. * Copyright (C) 2007 - 2008 Kai Winter
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with QMapControl. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * Contact e-mail: kaiwinter@gmx.de
  22. * Program URL : http://qmapcontrol.sourceforge.net/
  23. *
  24. */
  25. #include "mapnetwork.h"
  26. #include <QtNetwork/QNetworkRequest>
  27. #include <QtCore/QUrl>
  28. #include <QtCore/QMapIterator>
  29. #include <QtCore/QWaitCondition>
  30. #include <QtCore/QMutexLocker>
  31. MapNetwork::MapNetwork(ImageManager* parent){
  32. loaded = 0;
  33. m_pImageManager = parent;
  34. m_bNetworkActive = false;
  35. m_bCacheEnabled = false;
  36. m_pHttpAccessManager = curlRequestsManager::getInstance();
  37. }
  38. MapNetwork::~MapNetwork(){
  39. //curlRequest *tRequest;
  40. //foreach(tRequest, m_lRequestsList){
  41. //if (tReply->isRunning()){
  42. // tReply->abort();
  43. //}
  44. //tReply->deleteLater();
  45. // delete tRequest;
  46. // tRequest = NULL;
  47. //};
  48. //m_pHttpAccessManager->deleteLater();
  49. delete m_pHttpAccessManager;
  50. m_pHttpAccessManager = NULL;
  51. }
  52. void MapNetwork::loadImage(const QString& host, const QString& url){
  53. QString hostName = host;
  54. QString portNumber = QString("80");
  55. QRegExp r(".:.");
  56. if(r.indexIn(host) >= 0){
  57. QStringList s = host.split(":");
  58. hostName = s.at(0);
  59. portNumber = s.at(1);
  60. };
  61. QString finalUrl = QString("http://%1:%2%3").arg(hostName).arg(portNumber).arg(url);
  62. char HostPart[64];
  63. char PathPart[256];
  64. sprintf(HostPart, "http://%s:%s", hostName.toStdString().c_str(), portNumber.toStdString().c_str());
  65. sprintf(PathPart, "%s", url.toStdString().c_str());
  66. if (m_bCacheEnabled){
  67. QIODevice *QIO = m_pDiskCache->data(QUrl(finalUrl));
  68. if (QIO != NULL){
  69. qint64 size;
  70. size = QIO->bytesAvailable();
  71. if (size){
  72. QPixmap pm;
  73. QByteArray ax;
  74. QIO->open(QIODevice::ReadOnly);
  75. ax = QIO->readAll();
  76. QIO->close();
  77. if (pm.loadFromData(ax) && pm.size().width() > 1 && pm.size().height() > 1){
  78. loaded += pm.size().width()*pm.size().height()*pm.depth() / 8 / 1024;
  79. //qDebug() << "Network loaded: " << me->loaded << " width:" << pm.size().width() << " height:" << pm.size().height();
  80. m_pImageManager->receivedImage(pm, QString(PathPart));
  81. }
  82. else {
  83. m_pImageManager->fetchFailed(QString(PathPart));
  84. };
  85. if (loadQueueSize() == 0){
  86. //qDebug () << "all loaded";
  87. m_pImageManager->loadingQueueEmpty();
  88. }
  89. return;
  90. };
  91. };
  92. };
  93. curlRequest *tRequest = new curlRequest(HostPart, PathPart);
  94. if (tRequest != NULL){
  95. tRequest->setHandler(this, &MapNetwork::requestFinished, tRequest);
  96. if (m_pHttpAccessManager->get(tRequest)){
  97. QMutexLocker lock(&vectorMutex);
  98. //m_lRequestsList.append(tRequest);
  99. m_mLoadingMap.insert(url,finalUrl);
  100. } else {
  101. qDebug() << "Failed request: " << finalUrl;
  102. };
  103. };
  104. };
  105. //void MapNetwork::requestFinished(QNetworkReply *reply){
  106. void *MapNetwork::requestFinished( void *ptr1, void *ptr2 ){
  107. if (!ptr1) return NULL;
  108. if (!ptr2) return NULL;
  109. MapNetwork *me;
  110. curlRequest *rr;
  111. me = (MapNetwork*)ptr1;
  112. rr = (curlRequest *)ptr2;
  113. //qDebug() << "MapNetwork::requestFinished" << rr->getPath();
  114. QString id = rr->getPath();
  115. // check if id is in map?
  116. bool idInMap = false;
  117. QString url;
  118. if( rr->isFinished() ){
  119. QMutexLocker lock(&me->vectorMutex);
  120. idInMap = me->m_mLoadingMap.contains(id);
  121. if (idInMap){
  122. url = me->m_mLoadingMap[id];
  123. me->m_mLoadingMap.remove(id);
  124. };
  125. };
  126. if( idInMap ){
  127. if (rr->getSize()){
  128. //qDebug() << "Network loaded: " << rr->getSize() << " bytes";
  129. QPixmap pm;
  130. QByteArray ax;
  131. ax = QByteArray::fromRawData((const char *)rr->getBuffer(), (int)rr->getSize());
  132. if (pm.loadFromData(ax) && pm.size().width() > 1 && pm.size().height() > 1){
  133. me->loaded += pm.size().width()*pm.size().height()*pm.depth() / 8 / 1024;
  134. //qDebug() << "Network loaded: " << me->loaded << " width:" << pm.size().width() << " height:" << pm.size().height();
  135. me->m_pImageManager->receivedImage(pm, QString(rr->getPath()));
  136. if( me->m_bCacheEnabled){
  137. QNetworkCacheMetaData QMeta;
  138. QMeta.setSaveToDisk(true);
  139. QMeta.setUrl(QUrl(rr->getURL()));
  140. //QIODevice *QIO = me->m_pDiskCache->data(QUrl(rr->getURL()));
  141. QIODevice *QIO = me->m_pDiskCache->prepare(QMeta);
  142. QIO->open(QIODevice::WriteOnly);
  143. QIO->write((const char *)rr->getBuffer(), (int)rr->getSize());
  144. QIO->close();
  145. };
  146. } else {
  147. me->m_pImageManager->fetchFailed(QString(rr->getPath()));
  148. };
  149. } else {
  150. me->m_pImageManager->fetchFailed(QString(rr->getPath()));
  151. };
  152. };
  153. if (me->loadQueueSize() == 0){
  154. //qDebug () << "all loaded";
  155. me->m_pImageManager->loadingQueueEmpty();
  156. }
  157. //QMutexLocker lock( &me->vectorMutex );
  158. //me->m_lRequestsList.removeAll(rr);
  159. return NULL;
  160. };
  161. int MapNetwork::loadQueueSize() const
  162. {
  163. QMutexLocker lock(&vectorMutex);
  164. return m_mLoadingMap.size();
  165. }
  166. void MapNetwork::setDiskCache(QNetworkDiskCache *qCache){
  167. m_bCacheEnabled = (qCache != 0);
  168. if (m_bCacheEnabled){//m_pHttpAccessManager){
  169. m_pDiskCache = qCache; //m_pHttpAccessManager->setCache(qCache);
  170. };
  171. };
  172. void MapNetwork::abortLoading(){
  173. qDebug() << "MapNetwork::abortLoading";
  174. // be sure that replyList is copied in case it's modified in another thread
  175. //QListIterator<curlRequest *> iter(m_lRequestsList);
  176. //while(iter.hasNext())
  177. //{
  178. // curlRequest *tRequest = iter.next();
  179. // if (tRequest){
  180. //if(reply->isRunning())
  181. //{
  182. // reply->abort();
  183. //}
  184. //reply->deleteLater();
  185. // delete tRequest;
  186. // tRequest = 0;
  187. // };
  188. //};
  189. QMutexLocker lock(&vectorMutex);
  190. //m_lRequestsList.clear();
  191. m_mLoadingMap.clear();
  192. };
  193. bool MapNetwork::imageIsLoading(QString url)
  194. {
  195. QMutexLocker lock(&vectorMutex);
  196. return m_mLoadingMap.contains(url);
  197. }
  198. void MapNetwork::setProxy(const QString host, const int port, const QString username, const QString password)
  199. {
  200. // do not set proxy on qt/extended
  201. #ifndef Q_WS_QWS
  202. if (m_pHttpAccessManager){
  203. QNetworkProxy proxy = QNetworkProxy( QNetworkProxy::HttpProxy, host, port );
  204. proxy.setUser( username );
  205. proxy.setPassword( password );
  206. //m_pHttpAccessManager->setProxy(proxy);
  207. }
  208. #endif
  209. }