/*! @file

    @id $Id$
*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
#ifndef SSLCLIENTNETWORKMANAGER_HXX
#define SSLCLIENTNETWORKMANAGER_HXX

#include <qbrowserlib/log.hxx>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkAccessManager>
#include <smartcardauth.hxx>

//! @addtogroup pkcs11
//! @{
class SslClientAuthNetworkAccessManager: public QNetworkAccessManager {
    Q_OBJECT;
  public:
    
    SslClientAuthNetworkAccessManager(QObject* parent = 0):
        QNetworkAccessManager(parent) {
      TRC;
    }
    
    virtual ~SslClientAuthNetworkAccessManager() {
      TRC;
    }

    void scAuth(SmartCardAuth* auth) {
      _scAuth = std::auto_ptr<SmartCardAuth>(auth);
    }

  Q_SIGNALS:

    void created(QNetworkReply*);
    
  protected:
    
    virtual QNetworkReply* createRequest(Operation op,
                                         const QNetworkRequest& req,
                                         QIODevice* outgoingData = 0 ) {
      TRC; LOG<<req.url();
      if (req.url().scheme()=="https") {
	LOG<<"Need to login";
	if (_scAuth.get()) _scAuth->login(false);
      }
      QNetworkReply* rep
        (QNetworkAccessManager::createRequest(op, req, outgoingData));
      created(rep);
      LOG<<"Reply to URL: "<<rep->url().toString();
      return rep;
    }

  private:

    std::auto_ptr<SmartCardAuth> _scAuth;

};

//! @}
#endif
