/** @id $Id$

   This file has been added:
     - by bootstrap.sh
     - on Thu, 08 October 2015 11:56:48 +0200

*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890

#ifndef CERTMAN_HXX
#define CERTMAN_HXX

#include <certificate.hxx>
#include <openfromurl.hxx>
#include <ui_certman.hxx>
#include <QMainWindow>
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>
#include <QRegExp>

#include <QDebug>

/// Main Window
/** Main window for certman */
class CertMan: public QMainWindow, protected Ui::CertMan {
    Q_OBJECT;
  public:
    explicit CertMan(QWidget *parent = 0):
        QMainWindow(parent),
        _openFromURL(this) {
      setupUi(this);
      connect(&_openFromURL, SIGNAL(certificate(QUrl, QSslCertificate)),
              SLOT(certificate(QUrl, QSslCertificate)));
    }
    bool open(const QString& fileName) {
      bool status(false);
      QFile file(fileName);
      file.open(QIODevice::ReadOnly);
      if (file.error()!=QFileDevice::NoError) {
        qDebug()<<"Error open file"<<fileName<<file.errorString();
        return false;
      }
      for (const QSslCertificate& c: QSslCertificate::fromDevice(&file)) {
        Certificate* win(new Certificate(_mdi));
        _mdi->addSubWindow(win)->setWindowTitle
          (c.subjectInfo(QSslCertificate::CommonName).join(", ")
           +" ("+QFileInfo(fileName).fileName()+")");
        win->show();
        win->certificate(c);
        status = true;
      }
      file.close();
      return status;
    }
    virtual ~CertMan() {}
  protected slots:
    void on__actionOpenCertificate_triggered() {
      QString fileName = QFileDialog::getOpenFileName(this);
      if (fileName.isEmpty()) return;
      if (!open(fileName))
        QMessageBox::warning(this, "Error Loading File",
                             QString("Error loading file:\n%1").arg(fileName));
    }
    void on__actionOpenFromURL_triggered() {
      if (_openFromURL.exec()==QDialog::Accepted) {
        _openFromURL.read();
      }
    }
    void certificate(QUrl url, QSslCertificate cert) {
      qDebug()<<"got certificate: "<<cert.subjectInfo(QSslCertificate::CommonName).join("/");
      Certificate* win(new Certificate(_mdi));
      _mdi->addSubWindow(win)->setWindowTitle
        (url.host()+" - "+
         cert.subjectInfo(QSslCertificate::CommonName).join("/"));
      win->show();
      win->certificate(cert);
    }
    void on__actionSaveAs_triggered() {
      QMdiSubWindow* win(_mdi->currentSubWindow());
      if (!win) return;
      Certificate* cert(dynamic_cast<Certificate*>(win->widget()));
      QString fileName(QFileDialog::getSaveFileName
                       (this,
                        win->windowTitle(),
                        win->windowTitle()
                        .replace(" ", "_").replace(QRegExp("\\.(pem|der)$"), "")
                        +".pem",
                        tr("Certificates (*.der *.pem *.txt)")));
      if (fileName.isEmpty()) return;
      QFile file(fileName);
      file.open(QIODevice::WriteOnly);
      QFileInfo fileInfo(fileName);
      if (fileInfo.suffix()=="der") {
        QDataStream out(&file);
        out<<cert->certificate().toDer();
      } else if (fileInfo.suffix()=="pem") {
        QDataStream out(&file);
        out<<cert->certificate().toPem();
      } else if (fileInfo.suffix()=="txt") {
        QTextStream out(&file);
        out<<cert->certificate().toText();
      } else { // defaults to pem
        QDataStream out(&file);
        out<<cert->certificate().toPem();
      }
      file.close();
    }
  private:
    OpenFromURL _openFromURL;
};

#endif
