/*! @file

    @id $Id: main.cxx 15 2010-06-03 14:17:27Z marc $

    Build für Windoof:
    <code>QMAKESPEC=$(pwd)/mkspecs/mingw-g++ qmake-qt4 && make release</code>
*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890

#include <QtGui/QApplication>
#include <QtCore/QTranslator>
#include <QtCore/QTextCodec>
#include <QtCore/QLocale>
#include <QtCore/QProcess>
#include <QtCore/QFileInfo>
#include <QtNetwork/QNetworkProxy>
#include <QtNetwork/QSslConfiguration>
#include <QtNetwork/QSslCertificate>
#include <QtNetwork/QSslKey>
#include <QtWebKit/QWebSettings>

#include <browser.hxx>
#include <iostream>
#include <sstream>

#include <memory>


//! @defgroup browser The SwissBrowser Executable and Main Window
//! @defgroup pkcs11 The PKCS#11 Client Authentication
//! @defgroup qbrowserlib The Swiss Browser Library
//! @defgroup designer Qt Designer Plugin for Browser Related Widgets

/*! @mainpage The Secure Swiss PKCS#11 WebBrowser

    The browser is divided into a library, @ref qbrowserlib that implements
    generic browsing utilities, the main @ref browser page that collects
    those features into a main window, a @ref pkcs11 Post-SuisseID
    hardware token client authentication and a @ref designer plugin for
    flexible GUI creation.
    */

//! @addtogroup browser
//! @{

QString TMP;

const QByteArray SWISSSIGN_EV_GOLD_CA_2009_G2
    ("-----BEGIN CERTIFICATE-----\n"
     "MIIGvzCCBaegAwIBAgIQAPI39KUS4aGJo+mkR4+YuTANBgkqhkiG9w0BAQUFADBN\n"
     "MQswCQYDVQQGEwJDSDEVMBMGA1UEChMMU3dpc3NTaWduIEFHMScwJQYDVQQDEx5T\n"
     "d2lzc1NpZ24gRVYgR29sZCBDQSAyMDA5IC0gRzIwHhcNMTAwNDI5MDgyMzA2WhcN\n"
     "MTIwNDI5MDgyMzA2WjCCASQxCzAJBgNVBAYTAkNIMQ0wCwYDVQQIEwRCZXJuMQ0w\n"
     "CwYDVQQHEwRCZXJuMSAwHgYDVQQKExdEaWUgU2Nod2VpemVyaXNjaGUgUG9zd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RkE3\n"
     "MjcxMTlERUJGMjc1RTBENjhFMEY3MjcwgaiggaWggaKGgZ9sZGFwOi8vZGlyZWN0\n"
     "b3J5LnN3aXNzc2lnbi5uZXQvQ049ODg3NDQ2NkRDNzdDQjVGQTcyNzExOURFQkYy\n"
     "NzVFMEQ2OEUwRjcyNyUyQ089U3dpc3NTaWduJTJDQz1DSD9jZXJ0aWZpY2F0ZVJl\n"
     "dm9jYXRpb25MaXN0P2Jhc2U/b2JqZWN0Q2xhc3M9Y1JMRGlzdHJpYnV0aW9uUG9p\n"
     "bnQwYgYDVR0gBFswWTBXBglghXQBWQECAQEwSjBIBggrBgEFBQcCARY8aHR0cDov\n"
     "L3JlcG9zaXRvcnkuc3dpc3NzaWduLmNvbS9Td2lzc1NpZ24tR29sZC1DUC1DUFMt\n"
     "UjQucGRmMIHGBggrBgEFBQcBAQSBuTCBtjBkBggrBgEFBQcwAoZYaHR0cDovL3N3\n"
     "aXNzc2lnbi5uZXQvY2dpLWJpbi9hdXRob3JpdHkvZG93bmxvYWQvODg3NDQ2NkRD\n"
     "NzdDQjVGQTcyNzExOURFQkYyNzVFMEQ2OEUwRjcyNzBOBggrBgEFBQcwAYZCaHR0\n"
     "cDovL29jc3Auc3dpc3NzaWduLm5ldC84ODc0NDY2REM3N0NCNUZBNzI3MTE5REVC\n"
     "RjI3NUUwRDY4RTBGNzI3MA0GCSqGSIb3DQEBBQUAA4IBAQCNpT2u/s/IKqXrWmHX\n"
     "TTVbW2Kh8xul9jLole+hWXtx8Ln4E8oW+bV25HGUjt8QrC1wXGABRCijgIjQ8PtR\n"
     "CQrcwJCmC9ShcckP7KXiaETaT67TUB8qIQ/pf3akNeWKcdFGcD6CDYM7umuu1Q1X\n"
     "326mufNviiqlU0uoK1LJQHhme3HMHarYdzkuIeShvIYHUYP3JnorXaizcCqjBhdF\n"
     "rj4QcCZx8hKE6gmNL+dJkBM/5CSqByy15tMs2qa0iMdwkPUiNxlL0VuZkwzooVQx\n"
     "1eKQwsSx3807MkQMKs+4v1LexkY+5/xU9dvtyRysjuSEWS8yCx3+E3fOOVAIX5jD\n"
     "0uSX\n"
     "-----END CERTIFICATE-----");
const QByteArray SWISSSIGN_SERVER_GOLD_CA_2008_G2
    ("-----BEGIN CERTIFICATE-----\n"
     "MIIGXjCCBEagAwIBAgIIXsz6acAzJ+8wDQYJKoZIhvcNAQEFBQAwRTELMAkGA1UE\n"
     "BhMCQ0gxFTATBgNVBAoTDFN3aXNzU2lnbiBBRzEfMB0GA1UEAxMWU3dpc3NTaWdu\n"
     "IEdvbGQgQ0EgLSBHMjAeFw0wODA3MDcxNzA2MDNaFw0yMzA3MDcxNzA2MDNaMFEx\n"
     "CzAJBgNVBAYTAkNIMRUwEwYDVQQKEwxTd2lzc1NpZ24gQUcxKzApBgNVBAMTIlN3\n"
     "aXNzU2lnbiBTZXJ2ZXIgR29sZCBDQSAyMDA4IC0gRzIwggEiMA0GCSqGSIb3DQEB\n"
     "AQUAA4IBDwAwggEKAoIBAQDNMradteMVxVvR6t+pOooZejP8r1lY85el0PB7C118\n"
     "iKYv1Apfq3mZ0wt7ZdpCyd2DzKE338Ki04Qn9IidLCkKDLOSNtlQlw4Yvg12W2bU\n"
     "/WVaVAeUN0Tn3VeCkdWbSRIbvTGU63X9dp5lbgEm8XyjpLAK3SKABn7dWivfWuBo\n"
     "oUCy64hJ+fSWljJmqG772Qmc9p3GxfN7/L227JH4SMaOwVcJd0rkvMTi1bWa1XFp\n"
     "Q3lIlxW91JBEKqBKsru1h7wOq81VwIa3a2pdFgWY8F45G7M2Om9bLL5TiJ3Emkbl\n"
     "gKIFRX+GztmnQdKdY8c8rpknh/jWr6YiqYm+jV6+crGhAgMBAAGjggJEMIICQDAO\n"
     "BgNVHQ8BAf8EBAMCAQYwEgYDVR0TAQH/BAgwBgEB/wIBADAdBgNVHQ4EFgQUl3be\n"
     "CjTlEJpAxOvYnVpTeyHMRz4wHwYDVR0jBBgwFoAUWyV7lqRlUX64OfPAeGZe6Drn\n"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Ppi\n"
     "e/VCCMJagmH8yel0WhuyDUJE35bpNEL34w+pJKr7ApQ1sT0yh607jGEbl53Y+vFC\n"
     "Uuwo39MMqzx26notCbj0izccf+nDuW05jFQWmRc6OCI2L9Yw34r+1PQTmQe1TKx6\n"
     "FqnW3WP66ZwbXfGXwwfqmjcK+42695kQYSLsDfNaxxkCBTkt+MauYgXWiOfzAnYz\n"
     "R2nMQ/LHTDsTpTMkJ+eA/bHcrUJVkBHNWDiolD5C03oSFY0fPRlSnfXHRpgcOkq8\n"
     "yI/OYArKo4tvQpAYPnG7ELI/j2grgRx6xutuVMYVmzhr/JnqFto5Eg32hKjA5H5+\n"
     "c4Ssw1ZaGGMMb9aMFlnJi4SYoXB8MfL+SU/u1K54dM1mV/oFK6e2dtGgaYDdV86v\n"
     "0qo=\n"
     "-----END CERTIFICATE-----");
const QByteArray SWISSSIGN_GOLD_CA_G2
    ("-----BEGIN CERTIFICATE-----\n"
     "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\n"
     "-----END CERTIFICATE-----\n");
const QByteArray SWISSSIGN_PLATINUM_CA_G2
    ("-----BEGIN CERTIFICATE-----\n"
     "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\n"
     "-----END CERTIFICATE-----\n");
const QByteArray SWISSSIGN_SILVER_CA_G2
    ("-----BEGIN CERTIFICATE-----\n"
     "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\n"
     "-----END CERTIFICATE-----\n");

void notrace(QtMsgType, const char*) {
  return;
}

extern QWEBKIT_EXPORT void qt_drt_overwritePluginDirectories();

int main(int argv, char** argc) try {
//   qInstallMsgHandler(notrace);
//   std::cout.rdbuf((new std::stringstream)->rdbuf());
//   std::cerr.rdbuf((new std::stringstream)->rdbuf());
//   std::clog.rdbuf((new std::stringstream)->rdbuf());
  //............................................................................
  QSslConfiguration sslConfig(QSslConfiguration::defaultConfiguration());
  sslConfig.setProtocol(QSsl::AnyProtocol);
  sslConfig.setPeerVerifyMode(QSslSocket::AutoVerifyPeer);
  QList<QSslCertificate> certs(sslConfig.caCertificates());
  for (QList<QSslCertificate>::iterator cert(certs.begin());
       cert!=certs.end(); ++cert) {
    qDebug()<<"CERTIFICATE: "
            <<cert->issuerInfo(QSslCertificate::Organization)
            <<cert->issuerInfo(QSslCertificate::CommonName)
            <<cert->subjectInfo(QSslCertificate::Organization)
            <<cert->subjectInfo(QSslCertificate::CommonName);
  }
  // certs.push_back(QSslCertificate(SWISSSIGN_EV_GOLD_CA_2009_G2));
  // certs.push_back(QSslCertificate(SWISSSIGN_GOLD_CA_G2));
  // //certs.push_back(QSslCertificate(SWISSSIGN_SERVER_GOLD_CA_2008_G2));
  // certs.push_back(QSslCertificate(SWISSSIGN_SILVER_CA_G2));
  // certs.push_back(QSslCertificate(SWISSSIGN_PLATINUM_CA_G2));
  // sslConfig.setCaCertificates(certs);
  //............................................................................
  QStringList l(QProcess::systemEnvironment());
  QMap<QString, QString> env;
  for (QStringList::iterator it(l.begin()); it!=l.end(); ++it) {
    QStringList v(it->split('='));
    QString key(*v.begin());
    QString value((v.pop_front(), v.join("=")));
    env.insert(key, value);
    qDebug()<<"env:"<<key<<"="<<value;
  }
  //............................................................................
  QTextCodec* utf8(QTextCodec::codecForName("UTF-8"));
  QTextCodec::setCodecForCStrings(utf8);
  QTextCodec::setCodecForLocale(utf8);
  QTextCodec::setCodecForTr(utf8);
  QApplication app(argv, argc);
  app.setWindowIcon(QIcon(":/icons/32x32/swissbrowser.png"));
  app.setApplicationName(QObject::trUtf8("SwissBrowser", "application name"));
  app.setApplicationVersion(VERSION);
  QTranslator qtTranslator;
  if (env.contains("LANGUAGE")) QLocale::setDefault(env["LANGUAGE"]);
  qtTranslator.load(":/language/qt_" + QLocale().name());
  app.installTranslator(&qtTranslator);
  QTranslator appTranslator;
  appTranslator.load(":/language/swissbrowser_"+ QLocale().name());
  app.installTranslator(&appTranslator);
  //............................................................................
  if (env["PROXY_TYPE"]=="http")
    QNetworkProxy::setApplicationProxy
      (QNetworkProxy
       (QNetworkProxy::HttpProxy,
        env["PROXY_HOST"], env["PROXY_PORT"].toInt()));
  if (env["PROXY_TYPE"]=="socks")
    QNetworkProxy::setApplicationProxy
      (QNetworkProxy
       (QNetworkProxy::Socks5Proxy,
        env["PROXY_HOST"], env["PROXY_PORT"].toInt()));
  else
    QNetworkProxyFactory::setUseSystemConfiguration(true);
  qDebug()<<"***************************************************************";
  qDebug()<<"Start - Proxy:"
          <<(QNetworkProxy::applicationProxy().type()==QNetworkProxy::NoProxy
             ? "No Proxy"
             : (QNetworkProxy::applicationProxy().type()
                ==QNetworkProxy::Socks5Proxy ? "socks" : "http"))
          <<"Hostname"<<QNetworkProxy::applicationProxy().hostName()
          <<"Port"<<QNetworkProxy::applicationProxy().port();
  qDebug()<<"***************************************************************";
  //----------------------------------------------------------------------------
  TMP=QDir::toNativeSeparators(QDir::tempPath());
  QStringList urls;
  QString actlib
#ifdef Q_OS_LINUX
    ("libcvP11.so")
#else
#ifdef Q_OS_MAC
    ("libcvP11.dylib")
#else
#ifdef Q_OS_WIN
    ("cvP11.dll")
#endif
#endif
#endif
    ;
  QWebSettings::globalSettings()->setAttribute
    (QWebSettings::PluginsEnabled, true);
  bool silent(false);
  bool quirks(false);
  bool login(false);
  Settings::MimeTypes mimetypes;
  QString bookmarks;
  bool editbookmarks(false);
  QStringList args(app.arguments());
  std::auto_ptr<QSettings> settings
    (std::auto_ptr<QSettings>(new QSettings("SwissSign", "SwissBrowser")));
  for (QStringList::iterator it(args.begin()); ++it!=args.end();)
    if (*it=="-h" || *it=="--help" || *it=="-help" || *it=="/?") {
      std::cout<<QObject::trUtf8
        ("Usage: %1 [OPTIONS...] [<url> ...]\n"
         "Options:\n"
         " -h, --help        show this help text\n"
         " -t  --tmp path    temporary folder for data caching\n"
         " -k, --kiosk       no url bar\n"
         "                   if you sepcify -k and -s, -k must be first\n"
         " -q, --quirks      alternate user interface\n"
         " -n, --no-settings don't load or store any settings\n"
         " --login           ask for smartcard password at startup\n"
         " -l, --lib <file>  path to file libengine_act.so\n"
         " -s, --settings <file>\n"
         "                   load settings from <file>\n"
         "                   if you sepcify -k and -s, -k must be first\n"
         " -c, --cert <file> load local client certificate from <file>\n"
         " -y, --key  <file> load local certificate key from <file>\n"
         " -m, --mime  <mime> <ext> <tool>\n"
         "                   start <tool> for mimetype <mime>\n"
         " -b, --bookmarks <file>\n"
         "                   load and save bookmarks from and to <file>\n"
         " -e, --edit-bookmarks\n"
         "                   start in bookmark-edition mode\n"
         " <url>             optional full URL\n"
         "Environment:\n"
         "  LANGUAGE              \"de\", \"en\", ...          (actual: %5)\n"
         "  PROXY_TYPE            \"http\" or \"socks\" or \"\"  (actual: %2)\n"
         "  PROXY_PORT            proxy port number             (actual: %3)\n"
         "  PROXY_HOST            proxy host name               (actual: %4)\n"
         "  SWISS_USERAGENT       fake user agent               (actual: %6)\n")
        .arg(QFileInfo(argc[0]).fileName())
        .arg(env["PROXY_TYPE"]).arg(env["PROXY_PORT"]).arg(env["PROXY_HOST"])
           .arg(env["LANGUAGE"]).arg(env["SWISS_USERAGENT"])
        .toStdString()
               <<std::endl;
      return 0;
    } else if ((*it=="-t" || *it=="--tmp") && ++it!=args.end()) {
      TMP=*it;
    } else if ((*it=="-k" || *it=="--kiosk")) {
      silent=true;
      settings.reset();
    } else if ((*it=="-q" || *it=="--quirks")) {
      quirks=true;
    } else if ((*it=="-n" || *it=="--no-settings")) {
      settings.reset();
    } else if (*it=="--login") {
      login = true;
    } else if ((*it=="-l" || *it=="--lib") && ++it!=args.end()) {
      actlib = *it;
    } else if ((*it=="-b" || *it=="--bookmarks") && ++it!=args.end()) {
      bookmarks = *it;
    } else if ((*it=="-e" || *it=="--edit-bookmarks")) {
      editbookmarks = true;
    } else if ((*it=="-s" || *it=="--settings") && ++it!=args.end()) {
      settings = std::auto_ptr<QSettings>
        (new QSettings(*it, QSettings::IniFormat));
    } else if ((*it=="-c" || *it=="--cert") && ++it!=args.end()) {
      QFile file(*it);
      file.open(QIODevice::ReadOnly);
      QSslCertificate c(&file);
      if (c.isNull()) {
        std::cerr<<QObject::trUtf8("Cannot read PEM certificate from file: %1")
          .arg(*it).toStdString()<<std::endl;
        return 1;
      }
      sslConfig.setLocalCertificate(c);
      file.close();
      std::cout<<QObject::trUtf8("Read PEM certificates from file: %1")
        .arg(*it).toStdString()<<std::endl;
    } else if ((*it=="-y" || *it=="--key") && ++it!=args.end()) {
      QFile file(*it);
      file.open(QIODevice::ReadOnly);
      QSslKey k(&file, QSsl::Rsa);
      if (k.isNull()) {
        std::cerr<<QObject::trUtf8("Cannot read PEM RSA key from file: %1")
          .arg(*it).toStdString()<<std::endl;
        return 1;
      }
      sslConfig.setPrivateKey(k);
      std::cout<<QObject::trUtf8("Read private key from file: %1")
        .arg(*it).toStdString()<<std::endl;
    } else if ((*it=="-m" || *it=="--mime")
               && ++it!=args.end() && ++it!=args.end() && ++it!=args.end()) {
      QString mt(*----it);
      QString ext(*++it);
      QString app(*++it);
      mimetypes[mt] = QStringList()<<ext<<app;
    } else if (it!=args.end()) {
      urls<<*it;
    } else {
      std::cout<<QObject::trUtf8("Too few arguments.\nTry: %1 --help")
        .arg(QFileInfo(argc[0]).fileName()).toStdString()<<std::endl;
      return 1;
    }
  QSslConfiguration::setDefaultConfiguration(sslConfig);
  //............................................................................
  if (env.contains("QTWEBKIT_PLUGIN_PATH")) qt_drt_overwritePluginDirectories();
  Browser browser(actlib, urls, settings.get(), mimetypes, silent,
                  login, quirks, bookmarks);
  if (editbookmarks) browser.on_actionEditBookmarks_triggered();
  return app.exec();
 } catch (std::exception& x) {
  std::cerr<<"**** Error: "<<x.what()<<std::endl;
  return 1;
 } catch (...) {
  // unexpected exception - just terminate
  std::cerr<<"**** Error"<<std::endl;
  return 1;
 }

//! @}
