updatedialog.cpp 4.37 KB
Newer Older
Ashot's avatar
Ashot committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "updatedialog.h"
#include "ui_updatedialog.h"
#include <QPushButton>
#include <QFileDialog>
#include <QMessageBox>

struct myprogress {
    double lastruntime;
    CURL *curl;
};

UpdateDialog::UpdateDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UpdateDialog),
    finished(false)
{
    ui->setupUi(this);
    ui->progressBar->setVisible(false);
    ui->buttonBox->button(QDialogButtonBox::Ok)->setText("Download Crown");
    ui->buttonBox->button(QDialogButtonBox::Cancel)->setText("Don't Download");

    connect(ui->osComboBox , SIGNAL(currentIndexChanged(int)),this,SLOT(selectionChanged(int)));

    setPossibleOS();
    setProgressValue(0);
}

UpdateDialog* UpdateDialog::instance = 0;

UpdateDialog::~UpdateDialog()
{
    delete ui;
}

void UpdateDialog::setUpdateVersion(QString text)
{
    ui->topLabel->setText(ui->topLabel->text().arg(text));
}

void UpdateDialog::setCurrentVersion(QString text)
{
    ui->currentVersionLabel->setText(ui->currentVersionLabel->text().arg(text));
}

QString UpdateDialog::getOSString(Updater::OS os)
{
    QString osString = "Unknown";
    switch(os) {
        case Updater::LINUX_32:
            osString = "Linux 32";
            break;
        case Updater::LINUX_64:
            osString = "Linux 64";
            break;
        case Updater::WINDOWS_32:
            osString = "Windows 32";
            break;
        case Updater::WINDOWS_64:
            osString = "Windows 64";
            break;
        case Updater::MAC_OS:
            osString = "Mac OS";
            break;
        default:
            osString = "Unknown";
    }
    return osString;
}

void UpdateDialog::setOS(Updater::OS os)
{
    ui->osComboBox->setCurrentIndex(os);
}

Updater::OS UpdateDialog::getOS()
{
    return static_cast<Updater::OS>(ui->osComboBox->currentIndex());
}

void UpdateDialog::setPossibleOS()
{
    ui->osComboBox->addItem(getOSString(Updater::UNKNOWN));
    ui->osComboBox->addItem(getOSString(Updater::LINUX_32));
    ui->osComboBox->addItem(getOSString(Updater::LINUX_64));
    ui->osComboBox->addItem(getOSString(Updater::WINDOWS_32));
    ui->osComboBox->addItem(getOSString(Updater::WINDOWS_64));
    ui->osComboBox->addItem(getOSString(Updater::MAC_OS));
}

void UpdateDialog::selectionChanged(int index)
{
    (void)index;
    if (getOS() == Updater::UNKNOWN)
    {
        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    } else {
        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
    }
};

101
void UpdateProgressBar(curl_off_t now, curl_off_t total)
Ashot's avatar
Ashot committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{
    QCoreApplication::processEvents();
    if (now != 0 && total != 0)
    {
        UpdateDialog::GetInstance()->setProgressMaximum(total);
        UpdateDialog::GetInstance()->setProgressValue(now);
        if (now == total)
        {
            UpdateDialog::GetInstance()->downloadFinished();
        }
    }
}

void UpdateDialog::downloadFinished()
{
    if (!finished)
    {
        finished = true;
        QMessageBox::information(this, tr("Download Complete"),
            tr("Package has been successfully downloaded!\nPath - %1").arg(fileName));
        QDialog::done(true);
    }
}

void UpdateDialog::setProgressMaximum(int max)
{
    ui->progressBar->setMaximum(max);
}

void UpdateDialog::setProgressValue(int value)
{
    ui->progressBar->setValue(value);
}

void UpdateDialog::downloadVersion()
{
    finished = false;
    std::string url = updater.GetDownloadUrl(getOS());
    QFileInfo fileInfo(QString::fromStdString(url));

    fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
            QDir::homePath() + QDir::separator() + fileInfo.fileName(),
            tr("Archives (*.zip *.tar.gz)"));
    if (!fileName.trimmed().isEmpty())
    {
        ui->progressBar->setVisible(true);
        this->resize(this->width(), this->height() + 45);
        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
        ui->buttonBox->button(QDialogButtonBox::Cancel)->setText("Cancel");
        updater.DownloadFile(url, fileName.toStdString(), &UpdateProgressBar);
    }
}

UpdateDialog* UpdateDialog::GetInstance()
{
    if (NULL == instance)
    {
        instance = new UpdateDialog();
    }
    return instance;
}

void UpdateDialog::done(int r)
{
    if (QDialog::Accepted == r)
    {
        downloadVersion();
        //QDialog::done(r);
        return;
    }
    else
    {
        updater.StopDownload();
        QDialog::done(r);
        return;
    }
}