From a2c509180a7653403e6143747492fa59da600763 Mon Sep 17 00:00:00 2001 From: twanvl Date: Wed, 22 Nov 2006 21:05:26 +0000 Subject: [PATCH] Added update checker git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@88 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/gui/set/window.cpp | 3 +- src/gui/update_checker.cpp | 135 +++++++++++++++++++++++++++++++++++++ src/gui/update_checker.hpp | 30 +++++++++ src/main.cpp | 5 ++ src/mse.vcproj | 8 ++- src/util/version.hpp | 5 +- 6 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 src/gui/update_checker.cpp create mode 100644 src/gui/update_checker.hpp diff --git a/src/gui/set/window.cpp b/src/gui/set/window.cpp index fae3e0a2..23549894 100644 --- a/src/gui/set/window.cpp +++ b/src/gui/set/window.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -529,7 +530,7 @@ void SetWindow::onChildMenu(wxCommandEvent& ev) { void SetWindow::onIdle(wxIdleEvent& ev) { // Stuff that must be done in the main thread handle_pending_errors(); -// showUpdateDialog(this); + show_update_dialog(this); } // ----------------------------------------------------------------------------- : Event table diff --git a/src/gui/update_checker.cpp b/src/gui/update_checker.cpp new file mode 100644 index 00000000..72eea045 --- /dev/null +++ b/src/gui/update_checker.cpp @@ -0,0 +1,135 @@ +//+----------------------------------------------------------------------------+ +//| Description: Magic Set Editor - Program to make Magic (tm) cards | +//| Copyright: (C) 2001 - 2006 Twan van Laarhoven | +//| License: GNU General Public License 2 or later (see file COPYING) | +//+----------------------------------------------------------------------------+ + +// ----------------------------------------------------------------------------- : Includes + +#include +#include +#include +#include +#include +#include + +DECLARE_POINTER_TYPE(VersionData); + +// ----------------------------------------------------------------------------- : Update data + +/// Information on the latest availible version +class VersionData { + public: + Version version; ///< Latest version number + String description; ///< html description + String new_updates_url; ///< updates url has moved? + + DECLARE_REFLECTION(); +}; + +IMPLEMENT_REFLECTION(VersionData) { + REFLECT(version); + REFLECT(description); + REFLECT(new_updates_url); +} + +// The information for the latest version +VersionDataP update_version_data; +// Is update checking in progress? +volatile bool checking_updates = false; + +// ----------------------------------------------------------------------------- : Update checking + +// Thread to retrieve update information +// Checks if the current version is the latest version +// If not, displays a message +class CheckUpdateThread : public wxThread { + public: + virtual void* Entry() { + Work(); + return 0;; + } + + static void Work() { + if (checking_updates) return; // don't check multiple times simultaniously + checking_updates = true; + try { + wxURL url(settings.updates_url); + wxInputStream* isP = url.GetInputStream(); + if (!isP) return; // failed to get data + InputStreamP is(isP); + // Read version data + VersionDataP version_data; + Reader reader(is, _("updates")); + reader.handle(version_data); + // has the updates url changed? + if (!version_data->new_updates_url.empty()) { + settings.updates_url = version_data->new_updates_url; + } + // Make available + update_version_data = version_data; + } catch (...) { + // ignore all errors, we don't want problems if update checking fails + } + checking_updates = false; + } +}; + +void check_updates() { + if (settings.check_updates == CHECK_ALWAYS) { + check_updates_now(); + } else if (settings.check_updates == CHECK_IF_CONNECTED) { + // only if internet connection exists + wxDialUpManager* dum = wxDialUpManager::Create(); + if (dum->IsOk() && dum->IsOnline()) { + check_updates_now(); + } + delete dum; + } +} + +void check_updates_now(bool async) { + wxSocketBase::Initialize(); + if (async) { + CheckUpdateThread* thread = new CheckUpdateThread; + thread->Create(); + thread->Run(); + } else { + CheckUpdateThread::Work(); + } +} + + +// ----------------------------------------------------------------------------- : Dialog + +// A HTML control that opens all pages in an actual browser +struct HtmlWindowToBrowser : public wxHtmlWindow { + HtmlWindowToBrowser(Window* parent, int id, const wxPoint& pos, const wxSize& size, long flags) + : wxHtmlWindow(parent, id, pos, size, flags) + {} + + virtual void OnLinkClicked(const wxHtmlLinkInfo& info) { + wxLaunchDefaultBrowser( info.GetHref() ); + } +}; + +void show_update_dialog(Window* parent) { + if (!update_version_data) return; + if (update_version_data->version <= app_version) return; // we already have the latest version + // Show update dialog + wxDialog* dlg = new wxDialog(parent, wxID_ANY, _("Updates availible"), wxDefaultPosition); + // controls + wxHtmlWindow* html = new HtmlWindowToBrowser(dlg, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxHW_SCROLLBAR_AUTO | wxSUNKEN_BORDER); + html->SetPage(update_version_data->description); + wxButton* close = new wxButton(dlg, wxID_OK, _("&Close")); + close->SetDefault(); + // layout + wxSizer* s = new wxBoxSizer(wxVERTICAL); + s->Add(html, 1, wxEXPAND | wxALL, 8); + s->Add(close, 0, wxALIGN_RIGHT | wxALL & ~wxTOP, 8); + dlg->SetSizer(s); + dlg->SetSize(400,400); + dlg->Show(); + // And never show it again this run + update_version_data = VersionDataP(); +} diff --git a/src/gui/update_checker.hpp b/src/gui/update_checker.hpp new file mode 100644 index 00000000..18c7831f --- /dev/null +++ b/src/gui/update_checker.hpp @@ -0,0 +1,30 @@ +//+----------------------------------------------------------------------------+ +//| Description: Magic Set Editor - Program to make Magic (tm) cards | +//| Copyright: (C) 2001 - 2006 Twan van Laarhoven | +//| License: GNU General Public License 2 or later (see file COPYING) | +//+----------------------------------------------------------------------------+ + +#ifndef HEADER_UTIL_UPDATE_CHECKER +#define HEADER_UTIL_UPDATE_CHECKER + +// ----------------------------------------------------------------------------- : Includes + +#include + +// ----------------------------------------------------------------------------- : Update checking + +// Checks for updates if the settings say so +void check_updates(); + +/// Checks if the current version is the latest version +/** If async==true then checking is done in another thread + */ +void check_updates_now(bool async = true); + +/// Show a dialog to inform the user that updates are availible (if there are any) +/** Call check_updates first. + * Call this function from an onIdle loop */ +void show_update_dialog(Window* parent); + +// ----------------------------------------------------------------------------- : EOF +#endif diff --git a/src/main.cpp b/src/main.cpp index 6b5db8ee..995bc2bb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,8 +13,10 @@ #include #include #include +#include #include #include +#include // ----------------------------------------------------------------------------- : Main function/class @@ -39,9 +41,12 @@ bool MSE::OnInit() { try { SetAppName(_("Magic Set Editor")); wxInitAllImageHandlers(); + wxFileSystem::AddHandler(new wxInternetFSHandler); // needed for update checker init_file_formats(); packages.init(); settings.read(); + // check for updates + check_updates(); //Window* wnd = new SymbolWindow(nullptr); //GameP g = Game::byName(_("magic")) SetP s = new_shared(); diff --git a/src/mse.vcproj b/src/mse.vcproj index 9ca61f0f..dcac9726 100644 --- a/src/mse.vcproj +++ b/src/mse.vcproj @@ -178,7 +178,7 @@ Name="VCCustomBuildTool"/> + + + + diff --git a/src/util/version.hpp b/src/util/version.hpp index 6be61244..2452cc33 100644 --- a/src/util/version.hpp +++ b/src/util/version.hpp @@ -25,7 +25,10 @@ struct Version { Version() : version(0) {} Version(UInt version) : version(version) {} - inline bool operator < (Version v) const { return version < v.version; } + inline bool operator < (Version v) const { return version < v.version; } + inline bool operator <= (Version v) const { return version <= v.version; } + inline bool operator > (Version v) const { return version > v.version; } + inline bool operator >= (Version v) const { return version >= v.version; } /// Convert a version number to a string String toString() const;