Browse Source

check updates.

oldj 10 years ago
parent
commit
a885b52c9d
6 changed files with 165 additions and 69 deletions
  1. 12 0
      app/config.js
  2. 78 0
      app/js/chk.js
  3. 4 0
      app/js/lang.js
  4. 2 2
      app/js/sh.js
  5. 2 1
      app/js/top_menu.js
  6. 67 66
      app/main.js

+ 12 - 0
app/config.js

@@ -0,0 +1,12 @@
+/**
+ * @author oldj
+ * @blog http://oldj.net
+ */
+
+"use strict";
+
+module.exports = {
+    VERSION: '3.0.0'
+    , url_chk_version: 'http://oldj.github.io/SwitchHosts/v.txt'
+    , url_homepage: 'http://oldj.github.io/SwitchHosts/'
+};

+ 78 - 0
app/js/chk.js

@@ -0,0 +1,78 @@
+/**
+ * @author oldj
+ * @blog http://oldj.net
+ */
+
+"use strict";
+
+const http = require('http');
+const config = require('../config');
+const lang = require('./lang').getLang('en');
+
+function compareVersion(v1, v2) {
+    if (v1 == v2) return 0;
+
+    let a1 = v1.split('.');
+    let a2 = v2.split('.');
+    let i;
+    let l = Math.min(a1.length, a2.length);
+    let c1;
+    let c2;
+
+    for (i = 0; i < l; i ++) {
+        c1 = parseInt(a1[i]);
+        c2 = parseInt(a2[i]);
+
+        if (isNaN(c2) || c1 > c2) {
+            return 1;
+        } else if (c1 < c2) {
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
+function chkUpdate(current_version, win) {
+    const dialog = require('electron').dialog;
+
+    http.get(config.url_chk_version, function (res) {
+        let s = '';
+        res.on('data', (c) => {
+            s += c;
+        });
+        res.on('end', () => {
+            //console.log(s);
+            let new_version = s.replace(/^\s+|\s+$/g, '');
+            if (compareVersion(current_version, new_version) < 0) {
+                // new version available
+                dialog.showMessageBox(win, {
+                    type: 'info'
+                    , buttons: ['cancel', 'YES']
+                    , title: 'New version found!'
+                    , message: lang.new_version_available + '\n\nv: ' + new_version
+                }, function (c) {
+                    if (c == 1) {
+                        require('electron').shell.openExternal(config.url_homepage);
+                    }
+                });
+            } else {
+                dialog.showMessageBox(win, {
+                    type: 'info'
+                    , buttons: ['OK']
+                    , title: 'You are up to date!'
+                    , message: lang.is_updated
+                });
+            }
+        });
+    }).on('error', function (e) {
+        dialog.showMessageBox(win, {
+            type: 'error'
+            , buttons: ['OK']
+            , title: 'Error'
+            , message: e.message
+        });
+    });
+}
+
+exports.chkUpdate = chkUpdate;

+ 4 - 0
app/js/lang.js

@@ -21,6 +21,8 @@ const languages = {
         , confirm_del: 'Are you sure you want to delete this host?'
         , tmp_clean: 'Temporarily turn off all rules.'
         , tmp_recover: 'Recover rules.'
+        , new_version_available: 'New version available, download now?'
+        , is_updated: 'You already have the latest version of SwitchHosts! installed.'
     },
     'cn': {
         add: '添加'
@@ -37,6 +39,8 @@ const languages = {
         , confirm_del: '确定要删除此 host 吗?'
         , tmp_clean: '临时去掉所有绑定'
         , tmp_recover: '恢复绑定'
+        , new_version_available: '检测到新版本,是否要下载?'
+        , is_updated: '当前版本是最新版本。'
     }
 };
 

+ 2 - 2
app/js/sh.js

@@ -5,7 +5,7 @@
 
 "use strict";
 
-const VERSION = '0.3.0';
+const config = require('../config');
 const $ = require('jquery');
 const Vue = require('vue');
 //Vue.config.debug = true;
@@ -22,7 +22,7 @@ const app = new Vue({
     data: {
         lang: lang,
         hosts: cf.getData({
-            VERSION: VERSION
+            VERSION: config.VERSION
         }),
         is_prompt_show: false,
         is_edit_show: false,

+ 2 - 1
app/js/top_menu.js

@@ -1,9 +1,10 @@
 /**
  * @author oldj
+ * @blog http://oldj.net
  */
 
 
-var template = [
+let template = [
     {
         label: "SwitchHosts!",
         submenu: [

+ 67 - 66
app/main.js

@@ -1,41 +1,47 @@
 /**
- * author: oldj
- * blog: http://oldj.net
+ * @author oldj
+ * @blog http://oldj.net
  */
 
-var app = require("app");  // Module to control application life.
-var BrowserWindow = require("browser-window");  // Module to create native browser window.
-var Menu = require("menu");
-var Tray = require("tray");
+'use strict';
+
+const config = require('./config');
+
+const app = require('app');  // Module to control application life.
+const BrowserWindow = require('browser-window');  // Module to create native browser window.
+const http = require('http');
+const Menu = require('menu');
+//const Tray = require('tray');
+
+//let is_debug = true;
+let is_debug = false;
 
-//var is_debug = true;
-var is_debug = false;
 
 // Report crashes to our server.
-require("crash-reporter").start();
+require('crash-reporter').start();
 
 // Keep a global reference of the window object, if you don't, the window will
 // be closed automatically when the javascript object is GCed.
-var mainWindow = null;
-var force_quit = false;
+let mainWindow = null;
+let force_quit = false;
 
 // Quit when all windows are closed.
-app.on("window-all-closed", function () {
-    if (process.platform != "darwin") {
+app.on('window-all-closed', function () {
+    if (process.platform != 'darwin') {
         app.quit();
     }
 });
 
-var appIcon = null;
+let appIcon = null;
 
 // This method will be called when atom-shell has done everything
 // initialization and ready for creating browser windows.
-app.on("ready", function () {
+app.on('ready', function () {
     // Create the browser window.
     mainWindow = new BrowserWindow({width: 800, height: 600});
 
     // and load the index.html of the app.
-    mainWindow.loadURL("file://" + __dirname + "/index.html");
+    mainWindow.loadURL('file://' + __dirname + '/index.html');
 
     if (is_debug) {
         mainWindow.toggleDevTools();
@@ -60,7 +66,7 @@ app.on("ready", function () {
     // Remove mainWindow.on('closed'), as it is redundant
     /*
      // Emitted when the window is closed.
-     mainWindow.on("closed", function () {
+     mainWindow.on('closed', function () {
      // Dereference the window object, usually you would store windows
      // in an array if your app supports multi windows, this is the time
      // when you should delete the corresponding element.
@@ -73,11 +79,11 @@ app.on("ready", function () {
     });
 
     /*
-     //console.log("file://" + __dirname + "/images/t.png");
+     //console.log('file://' + __dirname + '/images/t.png');
      // @see https://github.com/atom/electron/blob/master/docs/api/tray.md
-     appIcon = new Tray(__dirname + "/images/t.png");
-     //appIcon = new Tray("/Users/wu/studio/owl/sh3/app/images/t.png");
-     var contextMenu = Menu.buildFromTemplate([
+     appIcon = new Tray(__dirname + '/images/t.png');
+     //appIcon = new Tray('/Users/wu/studio/owl/sh3/app/images/t.png');
+     let contextMenu = Menu.buildFromTemplate([
      {label: 'Item1', type: 'radio'},
      {label: 'Item2', type: 'radio'},
      {label: 'Item3', type: 'radio', checked: true},
@@ -87,7 +93,7 @@ app.on("ready", function () {
      appIcon.setContextMenu(contextMenu);
      */
 
-    var template = [{
+    let template = [{
         label: 'Edit',
         submenu: [{
             label: 'Undo',
@@ -166,7 +172,7 @@ app.on("ready", function () {
         submenu: [{
             label: 'Homepage',
             click: function () {
-                require('electron').shell.openExternal('http://oldj.github.io/SwitchHosts/')
+                require('electron').shell.openExternal(config.url_homepage);
             }
         }]
     }];
@@ -183,51 +189,46 @@ app.on("ready", function () {
     }
 
     if (process.platform == 'darwin') {
-        var name = require('electron').app.getName();
+        let name = require('electron').app.getName();
         template.unshift({
             label: name,
-            submenu: [
-                {
-                    label: 'About ' + name,
-                    role: 'about'
-                },
-                {
-                    type: 'separator'
-                },
-                //{
-                //    label: 'Services',
-                //    role: 'services',
-                //    submenu: []
-                //},
-                //{
-                //    type: 'separator'
-                //},
-                {
-                    label: 'Hide ' + name,
-                    accelerator: 'Command+H',
-                    role: 'hide'
-                },
-                {
-                    label: 'Hide Others',
-                    accelerator: 'Command+Shift+H',
-                    role: 'hideothers'
-                },
-                {
-                    label: 'Show All',
-                    role: 'unhide'
-                },
-                {
-                    type: 'separator'
-                },
-                {
-                    label: 'Quit',
-                    accelerator: 'Command+Q',
-                    click: function () {
-                        force_quit = true;
-                        app.quit();
-                    }
+            submenu: [{
+                label: 'About ' + name,
+                role: 'about'
+            }, {
+                label: 'Check for Updates...',
+                click: function () {
+                    require('./js/chk').chkUpdate(config.VERSION, mainWindow);
+                }
+            }, {
+                type: 'separator'
+            }, {
+                label: 'Services',
+                role: 'services',
+                submenu: []
+            }, {
+                type: 'separator'
+            }, {
+                label: 'Hide ' + name,
+                accelerator: 'Command+H',
+                role: 'hide'
+            }, {
+                label: 'Hide Others',
+                accelerator: 'Command+Shift+H',
+                role: 'hideothers'
+            }, {
+                label: 'Show All',
+                role: 'unhide'
+            }, {
+                type: 'separator'
+            }, {
+                label: 'Quit',
+                accelerator: 'Command+Q',
+                click: function () {
+                    force_quit = true;
+                    app.quit();
                 }
-            ]
+            }]
         });
         // Window menu.
         template[3].submenu.push(
@@ -241,6 +242,6 @@ app.on("ready", function () {
         );
     }
 
-    var menu = Menu.buildFromTemplate(template);
+    let menu = Menu.buildFromTemplate(template);
     Menu.setApplicationMenu(menu);
 });