Add update check

pull/2/head
Daniel Richman 2012-09-04 22:17:19 +01:00
rodzic 6e372c5978
commit 25d0aacaf5
7 zmienionych plików z 185 dodań i 16 usunięć

Wyświetl plik

@ -422,6 +422,7 @@ dl_fldigi_SOURCES += \
include/dl_fldigi/location.h \
include/dl_fldigi/gps.h \
include/dl_fldigi/hbtint.h \
include/dl_fldigi/update.h \
include/dl_fldigi/version.h \
include/habitat/CouchDB.h \
include/habitat/UploaderThread.h \
@ -578,6 +579,7 @@ dl_fldigi_SOURCES += \
dl_fldigi/location.cxx \
dl_fldigi/gps.cxx \
dl_fldigi/hbtint.cxx \
dl_fldigi/update.cxx \
dl_fldigi/version.cxx
# Sources that are part of the distribution but are not compiled directly

Wyświetl plik

@ -22,6 +22,7 @@
#include "dl_fldigi/hbtint.h"
#include "dl_fldigi/location.h"
#include "dl_fldigi/gps.h"
#include "dl_fldigi/update.h"
using namespace std;
@ -139,6 +140,8 @@ void online(bool val)
if (changed && dl_online)
{
update::check(); // N.B. only checks once
if (!flights::downloaded_flights_once)
hbtint::uthr->flights();
if (!flights::downloaded_payloads_once)

Wyświetl plik

@ -0,0 +1,138 @@
/*
* Copyright (C) 2012 Daniel Richman
* License: GNU GPL 3
*
* update.cxx: Automatically check for updates
*/
#include "dl_fldigi/update.h"
#include <stdexcept>
#include <map>
#include <string>
#include <FL/Fl.H>
#include <FL/fl_ask.H>
#include "config.h"
#include "debug.h"
#include "fl_digi.h"
#include "icons.h"
#include "jsoncpp.h"
#include "dl_fldigi/dl_fldigi.h"
#include "dl_fldigi/version.h"
#include "habitat/EZ.h"
using namespace std;
namespace dl_fldigi {
namespace update {
// Designed to be run once only
static UpdateThread thr;
static string update_text, update_url;
static void got_update(void *);
void check()
{
thr.start(); // EZ::SimpleThread won't start more than once
}
void cleanup()
{
try
{
thr.join();
}
catch (runtime_error &e)
{
// throws error if joined before started. Ignore.
}
}
#ifdef __MINGW32__
#define PLATFORM "mingw32"
#endif
#ifdef __APPLE__
#define PLATFORM "macosx"
#endif
#ifdef __linux__
#define PLATFORM "linux"
#endif
#ifndef PLATFORM
#error "Couldn't work out what the platform should be for update checking :-("
#endif
void *UpdateThread::run()
{
map<string,string> args;
args["platform"] = PLATFORM;
args["commit"] = dl_fldigi::git_commit;
string url = "http://habhub.org/dl-fldigi-check";
url.append(EZ::cURL::query_string(args, true));
EZ::cURL curl;
string response;
try
{
response = curl.get(url);
}
catch (runtime_error &e)
{
Fl_AutoLock lock;
LOG_WARN("Error in update check: %s", e.what());
return NULL;
}
/* blocking download done, now get the lock: */
Fl_AutoLock lock;
if (response == "")
{
LOG_INFO("dl-fldigi is up to date");
return NULL;
}
Json::Reader reader;
Json::Value val;
if (!reader.parse(response, val, false) ||
!val.isObject() || !val.size() ||
!val["text"].isString() || !val["url"].isString())
{
LOG_WARN("Error in update check: Bad JSON");
return NULL;
}
update_text = val["text"].asString();
update_url = val["url"].asString();
// Strange bug causing empty dialog boxes and unresponse process
// requires running got_update in the main thread, but whatever:
Fl::awake(got_update, NULL);
return NULL;
}
static void got_update(void *)
{
cleanup();
int c = fl_choice2("Test %s", "Close", "Open in browser", NULL,
update_text.c_str());
if (c)
{
LOG_INFO("Opening %s in browser", update_url.c_str());
// from fl_digi.h
cb_mnuVisitURL(0, (void *) update_url.c_str());
}
}
} /* namespace update */
} /* namespace dl_fldigi */

Wyświetl plik

@ -0,0 +1,21 @@
#ifndef DL_FLDIGI_UPDATE_H
#define DL_FLDIGI_UPDATE_H
#include "habitat/EZ.h"
namespace dl_fldigi {
namespace update {
void check();
void cleanup();
class UpdateThread : public EZ::SimpleThread
{
public:
void *run();
};
} /* namespace gps */
} /* namespace dl_fldigi */
#endif /* DL_FLDIGI_GPS_H */

Wyświetl plik

@ -44,7 +44,6 @@ enum {
XMLRPC_TID,
#endif
ARQ_TID, ARQSOCKET_TID,
DL_FLDIGI_TID, DL_FLDIGI_GPS_TID,
FLMAIN_TID,
NUM_THREADS, NUM_QRUNNER_THREADS = NUM_THREADS - 1
};

Wyświetl plik

@ -1,3 +1,5 @@
#!/usr/bin/env python
# Copyright 2012 Daniel Richman
# DL-Fldigi update check server
@ -25,15 +27,19 @@ def load_config():
def check():
load_config()
# KeyError becomes 400 Bad Request; good.
platform = request.args["platform"]
commit = request.args["commit"]
expect = config["expect"][platform]
try:
platform = request.args["platform"]
commit = request.args["commit"]
expect = config["expect"][platform]
if expect == commit:
return ""
else:
return config["update_text"]
if expect == commit:
return ""
else:
return json.dumps(config["update"])
except KeyError:
# bad platform or missing arg
abort(400)
if __name__ == "__main__":
app.run(debug=True)
app.run()

Wyświetl plik

@ -1,7 +1,7 @@
update_text: |
There is a new version of dl-fldigi available!
Go to http://ukhas.org.uk/projects:dl-fldigi to upgrade.
update:
url: "http://ukhas.org.uk/projects:dl-fldigi"
text: "There is a new version of dl-fldigi available!"
expect:
win32: "dcf3804ec3c006ab99867e053ec6996fb17ec50e"
linux: "dcf3804ec3c006ab99867e053ec6996fb17ec50e"
macosx: "dcf3804ec3c006ab99867e053ec6996fb17ec50e"
win32: "that would be self description"
linux: "which is unfortunately not possible"
macosx: "well, technically it is, but I don't have a super computer"