Skip to content

Commit

Permalink
Help.Update... and an autoupdater
Browse files Browse the repository at this point in the history
Found out that the RELEASES file can have url's in it, so theoretically
I can centralize the RELEASES file, but leave each release at a
different url (ie, github.com releases)
  • Loading branch information
markwal committed Aug 18, 2015
1 parent 992e0e1 commit 67db181
Show file tree
Hide file tree
Showing 9 changed files with 395 additions and 11 deletions.
2 changes: 1 addition & 1 deletion GPX
9 changes: 6 additions & 3 deletions GpxUi.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ SOURCES += main.cpp\
rungpx.cpp \
inih/ini.c \
iniedit.cpp \
machineeditor.cpp
machineeditor.cpp \
updates.cpp

HEADERS += mainwindow.h \
rungpx.h \
build/version.h \
inih/ini.h \
iniedit.h \
machineeditor.h \
main.h
main.h \
updates.h

FORMS += mainwindow.ui \
rungpx.ui \
machineeditor.ui
machineeditor.ui \
updates.ui

DISTFILES += \
gpxui.rc
Expand Down
9 changes: 9 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "rungpx.h"
#include "updates.h"
#include "machineeditor.h"
#include "build/version.h"

Expand All @@ -26,6 +27,8 @@ MainWindow::MainWindow(QWidget *parent) :
connect(ui->actionAbout_Qt, SIGNAL(triggered()), QApplication::instance(), SLOT(aboutQt()));

on_btnReload_clicked();

Updates::autoUpdate();
}

MainWindow::~MainWindow()
Expand Down Expand Up @@ -299,3 +302,9 @@ void MainWindow::on_about()
"Please see additional license information in the documentation distributed with this program.\n"
);
}

void MainWindow::on_action_Updates_triggered()
{
Updates updates(this);
updates.exec();
}
2 changes: 2 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ private slots:

void on_btnSave_clicked();

void on_action_Updates_triggered();

private:
Ui::MainWindow *ui;
int heightCollapsed;
Expand Down
17 changes: 11 additions & 6 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<rect>
<x>0</x>
<y>0</y>
<width>448</width>
<width>462</width>
<height>581</height>
</rect>
</property>
<property name="windowTitle">
<string>GPX - Gcode to X3g Converter</string>
</property>
<property name="windowIcon">
<iconset resource="gpxui.qrc">
<iconset>
<normaloff>:/icons/gpx.ico</normaloff>:/icons/gpx.ico</iconset>
</property>
<widget class="QWidget" name="centralWidget">
Expand Down Expand Up @@ -564,7 +564,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>448</width>
<width>462</width>
<height>21</height>
</rect>
</property>
Expand All @@ -580,6 +580,8 @@
</property>
<addaction name="actionAbout"/>
<addaction name="actionAbout_Qt"/>
<addaction name="separator"/>
<addaction name="action_Updates"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuHelp"/>
Expand All @@ -600,6 +602,11 @@
<string>About &amp;Qt</string>
</property>
</action>
<action name="action_Updates">
<property name="text">
<string>&amp;Updates...</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<tabstops>
Expand Down Expand Up @@ -627,9 +634,7 @@
<tabstop>sbRightTemp</tabstop>
<tabstop>sbRightStandby</tabstop>
</tabstops>
<resources>
<include location="gpxui.qrc"/>
</resources>
<resources/>
<connections>
<connection>
<sender>actionExit</sender>
Expand Down
4 changes: 3 additions & 1 deletion rungpx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ RunGpx::~RunGpx()
pprocess->terminate();
pprocess->waitForFinished(3000);
}
delete pprocess;
pprocess = NULL;
delete ui;
}

Expand All @@ -45,7 +47,7 @@ void RunGpx::DelayReadStdOut()
ptimer->start(10);
}
else if (pprocess->state() != QProcess::Running) {
tc.insertText(tr("GPX finished with code = %1.\n").arg(pprocess->exitCode()));
tc.insertText(tr("GPX finished with code = 0x%1.\n").arg(pprocess->exitCode(), 0, 16));
}
ui->teLogOutput->setTextCursor(tc);
}
Expand Down
133 changes: 133 additions & 0 deletions updates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "updates.h"
#include "ui_updates.h"

#include <QSettings>
#include <QDir>
#include <QProcess>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDateTime>

// ../Update.exe --download=https://markwal.github.io/GpxUi
// ../Update.exe --update=https://markwal.github.io/GpxUi
Updates::Updates(QWidget *parent) :
QDialog(parent),
ui(new Ui::Updates)
{
ui->setupUi(this);

QSettings settings;
ui->cboxAutoUpdate->setChecked(settings.value("auto_update", true).toBool());

pprocess = new QProcess(this);
pprocess->setProcessChannelMode(QProcess::MergedChannels);
connect(pprocess, SIGNAL(readyRead()), SLOT(readStdout()));
connect(pprocess, SIGNAL(finished(int)), SLOT(finished(int)));

checkForUpdates();
}

Updates::~Updates()
{
if (pprocess->state() == QProcess::Running) {
pprocess->terminate();
pprocess->waitForFinished(3000);
}
delete pprocess;
pprocess = NULL;
delete ui;
}

bool Updates::runUpdateExe(QProcess *pprocess, UpdateAction ua)
{
QDir dir(QApplication::instance()->applicationDirPath());
dir.cdUp();
QString sApp = dir.absoluteFilePath("Update.exe");
QStringList slArgs;
if (ua == Check)
slArgs << QLatin1String("--download=https://markwal.github.io/GpxUi");
else
slArgs << QLatin1String("--update=https://markwal.github.io/GpxUi");
if (ua == UpdateDetached) {
return pprocess->startDetached(sApp, slArgs);
}
pprocess->start(sApp, slArgs);
return pprocess->waitForStarted();
}

void Updates::doAction(UpdateAction ua)
{
if (pprocess->state() == QProcess::Running) {
return; // one at a time
}

fUpdating = (ua == Update);
if (!runUpdateExe(pprocess, ua))
ui->textStatus->setText("Unable to check for updates.");
else if (fUpdating)
ui->textStatus->setText("Installing latest version.");
else
ui->textStatus->setText("Checking for available updates.");
}

void Updates::checkForUpdates()
{
doAction(Check);
}

void Updates::readStdout()
{
QByteArray rgb = pprocess->readLine();
if (rgb.isEmpty())
return;
if (QChar(rgb[0]).isDigit()) {
int percent = QString(rgb).toInt();
if (percent >= 0 || percent <= 100)
ui->progressBar->setValue(percent);
}
else if (rgb[0] == '{') {
ui->progressBar->setValue(0); // back to 0 to prepare for "Update Now"
QJsonDocument jd(QJsonDocument::fromJson(rgb));
const QJsonObject &json = jd.object();
ui->textCurrent->setText(json["currentVersion"].toString());
ui->textAvailable->setText(json["futureVersion"].toString());
ui->textStatus->setText("");
}
}

void Updates::autoUpdate()
{
QSettings settings;
if (!settings.value("auto_update", true).toBool())
return;
QDateTime dt = settings.value("last_update_check", QDateTime()).toDateTime();
QDateTime dtNow = QDateTime::currentDateTimeUtc();
qint64 cdays = dt.daysTo(dtNow);
if (dt.isNull() || cdays > 0) {
settings.setValue("last_update_check", dtNow);
QProcess process;
runUpdateExe(&process, UpdateDetached);
}
}

#pragma GCC diagnostic ignored "-Wunused-parameter"
void Updates::finished(int ec)
{
if (fUpdating) {
ui->progressBar->setValue(100);
ui->textStatus->setText("New version installed for the next start of GpxUi.");
}
}
#pragma GCC diagnostic pop

void Updates::on_buttonBox_accepted()
{
QSettings settings;

settings.setValue("auto_update", ui->cboxAutoUpdate->isChecked());
}

void Updates::on_pbUpdateNow_clicked()
{
doAction(Update);
}
38 changes: 38 additions & 0 deletions updates.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef UPDATES_H
#define UPDATES_H

#include <QDialog>

namespace Ui {
class Updates;
}

class QProcess;

class Updates : public QDialog
{
Q_OBJECT

public:
explicit Updates(QWidget *parent = 0);
~Updates();
static void autoUpdate();

private slots:
void on_buttonBox_accepted();
void on_pbUpdateNow_clicked();
void readStdout();
void finished(int ec);

private:
Ui::Updates *ui;
QProcess *pprocess;
bool fUpdating;

void checkForUpdates();
typedef enum {Check, Update, UpdateDetached} UpdateAction;
static bool runUpdateExe(QProcess *, UpdateAction ua);
void doAction(UpdateAction);
};

#endif // UPDATES_H
Loading

0 comments on commit 67db181

Please sign in to comment.