Check for a new version

This commit is contained in:
Alexander Kojevnikov 2012-08-21 10:32:16 -07:00
parent 47fc4b95d6
commit 0d2514e018
3 changed files with 63 additions and 43 deletions

View File

@ -16,23 +16,33 @@
* along with Spek. If not, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>
#include <wx/aboutdlg.h>
#include <wx/artprov.h>
#include <wx/dnd.h>
#include <wx/filename.h>
#include "spek-preferences.hh"
#include "spek-spectrogram.hh"
#include "spek-window.hh"
DECLARE_EVENT_TYPE(SPEK_NOTIFY_EVENT, -1)
DEFINE_EVENT_TYPE(SPEK_NOTIFY_EVENT)
BEGIN_EVENT_TABLE(SpekWindow, wxFrame)
EVT_MENU(wxID_OPEN, SpekWindow::on_open)
EVT_MENU(wxID_SAVE, SpekWindow::on_save)
EVT_MENU(wxID_EXIT, SpekWindow::on_exit)
EVT_MENU(wxID_PREFERENCES, SpekWindow::on_preferences)
EVT_MENU(wxID_ABOUT, SpekWindow::on_about)
EVT_COMMAND(-1, SPEK_NOTIFY_EVENT, SpekWindow::on_notify)
END_EVENT_TABLE()
// Forward declarations.
static void * check_version(void *);
class SpekDropTarget : public wxFileDropTarget
{
public:
@ -104,6 +114,7 @@ SpekWindow::SpekWindow(const wxString& path) :
// wxInfoBar is too limited, construct a custom one.
wxPanel *info_bar = new wxPanel(this);
info_bar->Hide();
info_bar->SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOTEXT));
info_bar->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_INFOBK));
wxSizer *info_sizer = new wxBoxSizer(wxHORIZONTAL);
@ -135,6 +146,9 @@ SpekWindow::SpekWindow(const wxString& path) :
SetDropTarget(new SpekDropTarget(this));
SetSizer(sizer);
pthread_t thread;
pthread_create(&thread, NULL, &check_version, this);
}
void SpekWindow::open(const wxString& path)
@ -294,6 +308,12 @@ void SpekWindow::on_about(wxCommandEvent& event)
wxAboutBox(info);
}
void SpekWindow::on_notify(wxCommandEvent& event)
{
this->GetSizer()->Show((size_t)0);
this->Layout();
}
void SpekWindow::on_visit(wxCommandEvent& event)
{
wxLaunchDefaultBrowser(wxT("http://spek-project.org"));
@ -305,3 +325,45 @@ void SpekWindow::on_close(wxCommandEvent& event)
self->GetSizer()->Hide((size_t)0);
self->Layout();
}
static void * check_version(void *p)
{
// Does the user want to check for updates?
SpekPreferences& prefs = SpekPreferences::get();
if (!prefs.get_check_update()) {
return NULL;
}
// Calculate the number of days since 0001-01-01, borrowed from GLib.
wxDateTime now = wxDateTime::Now();
int year = now.GetYear() - 1;
int days = year * 365;
days += (year >>= 2); // divide by 4 and add
days -= (year /= 25); // divides original # years by 100
days += year >> 2; // divides by 4, which divides original by 400
days += now.GetDayOfYear();
// When was the last update?
int diff = days - prefs.get_last_update();
if (diff < 7) {
return NULL;
}
// Get the version number.
wxString version = wxT("0.8.1"); //Platform.read_line ("http://www.spek-project.org/version");
if (version.IsEmpty()) {
return NULL;
}
if (version > wxT(PACKAGE_VERSION)) {
SpekWindow *self = (SpekWindow *)p;
wxCommandEvent event(SPEK_NOTIFY_EVENT, -1);
event.SetEventObject(self);
wxPostEvent(self, event);
}
// Update the preferences.
prefs.set_check_update(true);
prefs.set_last_update(days);
return NULL;
}

View File

@ -35,6 +35,7 @@ private:
void on_exit(wxCommandEvent& event);
void on_preferences(wxCommandEvent& event);
void on_about(wxCommandEvent& event);
void on_notify(wxCommandEvent& event);
void on_visit(wxCommandEvent& event);
void on_close(wxCommandEvent& event);

View File

@ -22,53 +22,10 @@ using Gtk;
namespace Spek {
public class Window : Gtk.Window {
public Window (string? file_name) {
try {
Thread.create<void*> (check_version, false);
} catch (ThreadError e) {
}
}
private void on_edit_preferences () {
var dlg = new PreferencesDialog ();
dlg.transient_for = this;
dlg.run ();
}
private void * check_version () {
// Does the user want to check for updates?
var prefs = Preferences.instance;
var check = prefs.check_update;
if (!check) {
return null;
}
// When was the last update?
var time_val = TimeVal ();
time_val.get_current_time ();
Date today = Date ();
today.set_time_val (time_val);
int day = prefs.last_update;
int diff = (int) today.get_julian () - day;
if (diff < 7) {
return null;
}
// Get the version number.
var version = Platform.read_line ("http://www.spek-project.org/version");
if (version == null) {
return null;
}
if (version != null && version > Config.PACKAGE_VERSION) {
Idle.add (() => { info_bar.show_all (); return false; });
}
// Update the preferences.
prefs.check_update = check;
prefs.last_update = (int) today.get_julian ();
prefs.save ();
return null;
}
}
}