Ignore:
Timestamp:
2010-02-01T17:13:40+01:00 (14 years ago)
Author:
stoecker
Message:

improved proxy handling (patches by Robert Schedel) and added caching for QT >= 4.5

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/wmsplugin/webkit-image.cpp

    r13262 r19704  
    1111#include <QtCore/QFile>
    1212#include <QtCore/QString>
     13#include <QtCore/QUrl>
    1314#include <QtWebKit/QWebPage>
    1415#include <QtWebKit/QWebFrame>
    1516#include <QtNetwork/QNetworkProxy>
    1617#include <QtCore/QProcess>
     18#if QT_VERSION >= 0x040500
     19#include <QDesktopServices>
     20#include <QNetworkDiskCache>
     21#endif
    1722
    1823/* using mingw to set binary mode */
     
    7681  if(idx != -1)
    7782  {
    78      QString proxyHost;
    79      int proxyPort = 8080;
    80      QStringList tmpList = environment.at(idx).split("=");
    81      QStringList host_port = tmpList.at(1).split(":");
    82      proxyHost = host_port.at(0);
    83      if(host_port.size() == 2)
    84      {
    85        bool ok;
    86        int port = host_port.at(1).toInt(&ok);
    87        if(ok)
    88          proxyPort = port;
    89      }
     83    QString scheme = "http";   // default
     84    QString proxyHost;
    9085
    91      QNetworkProxy proxy;
    92      proxy.setType(QNetworkProxy::HttpCachingProxy);
    93      proxy.setHostName(proxyHost);
    94      proxy.setPort(proxyPort);
    95      page->networkAccessManager()->setProxy(proxy);
     86    int proxyPort = 8080;
     87    QStringList tmpList = environment.at(idx).split("=");
     88
     89#if QT_VERSION >= 0x040600  // Qt4.6: Use QUrl::fromUserInput
     90    // set URL (and guess if proto scheme missing)
     91    QUrl url (QUrl::fromUserInput(tmpList.at(1)));
     92#else
     93    // set URL
     94    QUrl url (tmpList.at(1));
     95#endif
     96    if (url.isValid() && !url.host().isEmpty())
     97    {
     98      proxyHost = url.host();
     99
     100      if (url.port() != -1)
     101        proxyPort = url.port();
     102
     103      if (!url.scheme().isEmpty())
     104        scheme = url.scheme();
     105
     106      if (scheme == "http")   // we support only http
     107      {
     108        QNetworkProxy proxy;
     109        proxy.setType(QNetworkProxy::HttpCachingProxy);
     110        proxy.setHostName(proxyHost);
     111        proxy.setPort(proxyPort);
     112        if (!url.userName().isEmpty())
     113          proxy.setUser(url.userName());
     114        if (!url.password().isEmpty())
     115          proxy.setPassword(url.password());
     116        page->networkAccessManager()->setProxy(proxy);
     117      }
     118    }
     119    else /* manual mode */
     120    {
     121      QStringList proto_host_port = tmpList.at(1).split("://");
     122      QStringList host_port;
     123      if (proto_host_port.size() == 2)  // string has proto
     124      {
     125        scheme = proto_host_port.at(0);
     126        host_port = proto_host_port.at(1).split(":");
     127      }
     128      else  // no proto (or invalid format with several delimiters)
     129      {
     130        host_port = tmpList.at(1).split(":");
     131      }
     132      if (scheme == "http")   // we support only http
     133      {
     134        proxyHost = host_port.at(0);
     135        if(host_port.size() == 2)
     136        {
     137          bool ok;
     138          int port = host_port.at(1).toInt(&ok);
     139          if(ok)
     140            proxyPort = port;
     141        }
     142
     143        QNetworkProxy proxy;
     144        proxy.setType(QNetworkProxy::HttpCachingProxy);
     145        proxy.setHostName(proxyHost);
     146        proxy.setPort(proxyPort);
     147        page->networkAccessManager()->setProxy(proxy);
     148      }
     149    }
    96150  }
     151
     152#if QT_VERSION >= 0x040500
     153  QNetworkDiskCache *diskCache = new QNetworkDiskCache(page);
     154  QString location = QDesktopServices::storageLocation(QDesktopServices::CacheLocation);
     155  diskCache->setCacheDirectory(location);
     156  page->networkAccessManager()->setCache(diskCache);
     157#endif
    97158
    98159  QObject::connect(page, SIGNAL(loadFinished(bool)), s, SLOT(loaded(bool)));
Note: See TracChangeset for help on using the changeset viewer.