/*! @file

    @id $Id: unix.hxx 5 2009-03-11 15:37:56Z  $
*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890

#ifndef PROXY_QT
#define PROXY_QT

#include <QtNetwork/QNetworkProxy>
#include <QtNetwork/QNetworkProxyFactory>
#include <QtNetwork/QNetworkProxyQuery>

namespace proxy {

  class QtProxy: public Interface {

    public:

      QtProxy() {
        QNetworkProxyFactory::setUseSystemConfiguration(true);
      }

      virtual ~QtProxy() {}

      //! Implemented using Qt System Proxy
      virtual List proxies(const std::string& url) {

        qDebug()<<"************ QTPROXY ********************";
        
        List res;
        
        qDebug()<<"Query System Proxies for "<<QString::fromStdString(url)
                <<" ...";
        qDebug()<<" ... 1. setup query";
        QNetworkProxyQuery proxyQuery(QUrl(QString::fromStdString(url)));
        qDebug()<<" ... 2. query factory";
        QList<QNetworkProxy> proxies
          (QNetworkProxyFactory::systemProxyForQuery(proxyQuery));
        qDebug()<<"Found "<<proxies.size()<<" System Proxies.";

        for (QList<QNetworkProxy>::const_iterator proxy(proxies.begin());
             proxy!=proxies.end(); ++proxy) {
          Type type;
          switch (proxy->type()) {
            case QNetworkProxy::NoProxy: type=DIRECT; break;
            case QNetworkProxy::DefaultProxy: type=DEFAULT; break;
            case QNetworkProxy::Socks5Proxy: type=SOCKS; break;
            case QNetworkProxy::HttpProxy:
            case QNetworkProxy::HttpCachingProxy:
            case QNetworkProxy::FtpCachingProxy:
              type=HTTP;
          }
          qDebug()<<QString("************ QTPROXY: %1:%2")
            .arg(proxy->hostName()).arg(proxy->port());
          res.push_back
            (Proxy(type, proxy->hostName().toStdString(), proxy->port()));
        }

        return res;

      }

  };
  
}

#endif
