From 25d0aacaf5d30fabbce6b65eb97ef77cfeab3113 Mon Sep 17 00:00:00 2001 From: Daniel Richman Date: Tue, 4 Sep 2012 22:17:19 +0100 Subject: [PATCH] Add update check --- src/Makefile.am | 2 + src/dl_fldigi/dl_fldigi.cxx | 3 + src/dl_fldigi/update.cxx | 138 ++++++++++++++++++++++++++ src/include/dl_fldigi/update.h | 21 ++++ src/include/threads.h | 1 - update_server/{__main__.py => app.py} | 24 +++-- update_server/config.yml | 12 +-- 7 files changed, 185 insertions(+), 16 deletions(-) create mode 100644 src/dl_fldigi/update.cxx create mode 100644 src/include/dl_fldigi/update.h rename update_server/{__main__.py => app.py} (61%) diff --git a/src/Makefile.am b/src/Makefile.am index a3f68db9..6d2b1790 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 diff --git a/src/dl_fldigi/dl_fldigi.cxx b/src/dl_fldigi/dl_fldigi.cxx index 4091a509..b5a04f80 100644 --- a/src/dl_fldigi/dl_fldigi.cxx +++ b/src/dl_fldigi/dl_fldigi.cxx @@ -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) diff --git a/src/dl_fldigi/update.cxx b/src/dl_fldigi/update.cxx new file mode 100644 index 00000000..b748839a --- /dev/null +++ b/src/dl_fldigi/update.cxx @@ -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 +#include +#include + +#include +#include + +#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 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 */ diff --git a/src/include/dl_fldigi/update.h b/src/include/dl_fldigi/update.h new file mode 100644 index 00000000..5d596569 --- /dev/null +++ b/src/include/dl_fldigi/update.h @@ -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 */ diff --git a/src/include/threads.h b/src/include/threads.h index 0377646b..4fe9ac19 100644 --- a/src/include/threads.h +++ b/src/include/threads.h @@ -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 }; diff --git a/update_server/__main__.py b/update_server/app.py similarity index 61% rename from update_server/__main__.py rename to update_server/app.py index 51b05f4f..3b3f5377 100644 --- a/update_server/__main__.py +++ b/update_server/app.py @@ -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() diff --git a/update_server/config.yml b/update_server/config.yml index 20f81610..fa989845 100644 --- a/update_server/config.yml +++ b/update_server/config.yml @@ -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"