#include "updaterdialog.h"
#include "cfg.h"
#include "ui_updaterdialog.h"

UpdaterDialog::UpdaterDialog(QWidget *parent) : BaseDlg(parent), ui(new Ui::UpdaterDialog) {
    setAttribute(Qt::WA_DeleteOnClose);
    ui->setupUi(this);
    ui->pushButton_2->setVisible(false);
    ui->label_2->setText(tr("CurVersion")+":"+APP_VERSION);
    QSimpleUpdater *m_updater = QSimpleUpdater::getInstance();
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(OnCheckForUpdates()));
    connect(m_updater, SIGNAL(checkingFinished(QString)), this, SLOT(updateChangelog(QString)));
    m_updater->setModuleVersion(UpdVerUrl, APP_VERSION);
    m_updater->setNotifyOnUpdate(UpdVerUrl, false);
    m_updater->setNotifyOnFinish(UpdVerUrl, false);
    m_updater->checkForUpdates(UpdVerUrl);
}

UpdaterDialog::~UpdaterDialog(){
    delete ui;
}
void UpdaterDialog::OnCheckForUpdates(){
    QSimpleUpdater::getInstance()->setNotifyOnUpdate(UpdVerUrl, true);
    QSimpleUpdater::getInstance()->setNotifyOnFinish(UpdVerUrl, false);
    QSimpleUpdater::getInstance()->setMandatoryUpdate(UpdVerUrl, true);
    QSimpleUpdater::getInstance()->checkForUpdates(UpdVerUrl);
}
void UpdaterDialog::updateChangelog(QString){
    if(QSimpleUpdater::getInstance()->getUpdateAvailable(UpdVerUrl)){
        QString strtip = tr("LatestVersion:") + QSimpleUpdater::getInstance()->getLatestVersion(UpdVerUrl) + "\r\n"
            + tr("Update log:") + "\r\n" + QSimpleUpdater::getInstance()->getChangelog(UpdVerUrl);
        ui->textEdit->setText(strtip);
        ui->pushButton_2->setVisible(true);
    } else {
        ui->textEdit->setText(tr("The current version is already the latest version") + "\r\n\r\n"
           + tr("Update log:") + "\r\n" + QSimpleUpdater::getInstance()->getChangelog(UpdVerUrl));
    }
}
