/*! @file

    @id $Id$
*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890

#ifndef CARDGUI_HXX
#define CARDGUI_HXX

#include <cardgui-model.hxx>
#include <ui_cardgui.hxx>
#include <password.hxx>

#include <QtCore/QProcess>
#if  QT_VERSION >= 0x050000
#include <QtWidgets/QMessageBox>
#else
#include <QtGui/QMessageBox>
#endif

class CardGui: public QMainWindow, protected Ui_CardGui {

    Q_OBJECT;
            
  public:
    
    CardGui(QWidget* p=0):
        QMainWindow(p), _pwd(this) {
      setupUi(this);
      on__rescan_clicked();
    }
    
    void resizeEvent(QResizeEvent* event) {
      QMainWindow::resizeEvent(event);
      if (_tree->model())
        for (int i = 0; i < _tree->model()->columnCount(); ++i)
          _tree->resizeColumnToContents(i);
    }

  public Q_SLOTS:

    void on__rescan_clicked(bool=true) {
      static bool inRescan(false);
      if (inRescan) return;
      inRescan = true;
      try {
        delete _tree->model();
        _tree->setModel(0);
        _card->clear();
        pcsc::Connection::Strings readers(pcsc::Connection::scan());
        for (pcsc::Connection::Strings::iterator r(readers.begin());
             r!=readers.end(); ++r)
          _card->addItem(QString::fromStdString(*r));
      } catch (...) {
      }
      inRescan = false;
    }

    void on__card_currentIndexChanged(int) {
      on__reload_clicked();
    }

    void on__reload_clicked(bool=true) {
      try {
        delete _tree->model();
        _tree->setModel(0);
        cardos::Commands cmd(pcsc::Connection::reader
                             (_card->currentText().toStdString()));
        _tree->setModel(new CardGuiModel(cmd, "3f00"));
      } catch (...) {
        on__rescan_clicked();
      }
    }

    void on__actionRestartPCSCD_triggered() {
      delete _tree->model();
      _tree->setModel(0);
      if (_pwd.exec()) {
        _p.start("sudo -S service pcscd restart");
        _p.write((_pwd.password()+"\n").toUtf8());
        _p.closeWriteChannel();
        if (!_p.waitForFinished())
          QMessageBox::warning(this, tr("Restarting PCSC Daemon failed"),
                               tr("<html><p>Restarting PCSC daemon"
                                  " failed with message:</p>"
                                  "\n<pre>%1</pre></html>")
                               .arg(QString::fromUtf8
                                    (_p.readAll()+"\n"+
                                     _p.readAllStandardOutput()+"\n"+
                                     _p.readAllStandardError())));
        else
          QMessageBox::information(this, tr("Restarted PCSC Daemon"),
                                   tr("<html><p>Restarted PCSC daemon"
                                      " with message:</p>"
                                      "\n<pre>%1</pre></html>")
                                   .arg(QString::fromUtf8
                                        (_p.readAll()+"\n"+
                                         _p.readAllStandardOutput()+"\n"+
                                         _p.readAllStandardError())));
      }
      on__reload_clicked();
    }

  protected:

    Password _pwd;
    QProcess _p;
    
};

#endif
