diff --git a/.gitignore b/.gitignore index cecc9e8..257dfd6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ npm-debug.log* node_modules/ releases/ -creds.js \ No newline at end of file +creds.js +dev-app-update.yml \ No newline at end of file diff --git a/main.js b/main.js index 513793e..5cbc7e9 100644 --- a/main.js +++ b/main.js @@ -3,6 +3,7 @@ const path = require('path'); const request = require('request'); const moment = require('moment'); const Store = require('electron-store'); +const {autoUpdater} = require("electron-updater"); const store = new Store(); let tray = null; @@ -10,78 +11,74 @@ let tray = null; const WINDOW_WIDTH = 350; const WINDOW_HEIGHT = 335; const HORIZ_PADDING = 65; -const VERT_PADDING = 15; +const VERT_PADDING = 20; app.on('ready', () => { var platform = require('os').platform(); + autoUpdater.checkForUpdatesAndNotify(); + // Determine appropriate icon for platform + let iconPath = null + if (platform == 'win32') { + iconPath = path.join(__dirname, 'aussie_icon.ico'); + } + else { + iconPath = path.join(__dirname, 'aussie_icon.png'); + } + tray = new Tray(iconPath); + + if (platform != 'linux') { + tray.on('click', function (event) { + toggleWindow(); + }); + } const loggediNMenu = Menu.buildFromTemplate([ { label: 'Update', click: () => { updateData(); } }, { label: 'Logout', click: () => { logOut(); }}, { label: 'Quit', click: () => { app.quit(); } }, ]); - const contextMenu = Menu.buildFromTemplate([ { label: 'Login', click: () => { toggleWindow(); } }, { label: 'Quit', click: () => { app.quit(); } }, ]); - // Determine appropriate icon for platform - if (platform == 'darwin') { - const iconPath = path.join(__dirname, 'aussie_icon.png'); - tray = new Tray(iconPath); - } - else if (platform == 'win32') { - const iconPath = path.join(__dirname, 'aussie_icon.ico'); - tray = new Tray(iconPath); - } createWindow(); - if( platform == 'linux'){ - - const iconPath = path.join(__dirname, 'aussie_icon.png'); - tray = new Tray(iconPath); - if(!!store.get('username') && !!store.get('password')){ - tray.setContextMenu(loggediNMenu); - } - else{ - tray.setContextMenu(contextMenu); - } - - } - else{ - // tray.popUpContextMenu(contextMenu); - tray.on('click', function (event) { - toggleWindow(); - }); - tray.on('right-click', function (event) { - if(!!store.get('username') && !!store.get('password')){ - tray.popUpContextMenu(loggediNMenu); - } - else{ - tray.popUpContextMenu(contextMenu); - } - - }); - } - - - // test if we have stored creds - if (!!store.get('username')) { + if (!!store.get('username') && !!store.get('password')) { + + let res = { + username: store.get('username'), + password: store.get('password') + }; + + tray.setContextMenu(loggediNMenu); tray.setToolTip('Getting data from AussieBB...'); + + sendMessage('asynchronous-message','appLoaded',res); updateData(); } else{ + tray.setContextMenu(contextMenu); tray.setToolTip('Login to check your usage....'); toggleWindow(); } +}); +// when the update has been downloaded and is ready to be installed, notify the BrowserWindow +autoUpdater.on('update-downloaded', (info) => { + win.webContents.send('updateReady') }); - // Quit when all windows are closed. - app.on('window-all-closed', () => { +// when receiving a quitAndInstall signal, quit and install the new version ;) +ipcMain.on("quitAndInstall", (event, arg) => { + autoUpdater.quitAndInstall(); +}) + + +// Quit when all windows are closed. +app.on('window-all-closed', () => { // On macOS it is common for applications and their menu bar // to stay active until the user quits explicitly with Cmd + Q if (process.platform !== 'darwin') { @@ -89,7 +86,7 @@ app.on('ready', () => { } }) -const updateData = () => { +const updateData = () => { let aussie = request.jar(); request.post({ url: 'https://my.aussiebroadband.com.au/usage.php?xml=yes', @@ -103,30 +100,42 @@ const updateData = () => { function (error, response, body) { if (!error) { var parseString = require('xml2js').parseString; - console.log('raw XML', body); if (response.headers['content-type'] === 'text/xml;charset=UTF-8') { - - ipcMain.on('asynchronous-message', (event, arg) => { - let res = 'Login success' - event.sender.send('success', res); - }); - + console.log('raw XML', body); + sendMessage('asynchronous-message','success','Login success'); parseString(body, function (err, result) { console.dir(result); - + sendMessage('asynchronous-message','fullData',result); //Update tray tool tip + const timestamp = moment(result.usage.lastUpdated).fromNow(); + const date = new Date(); + const today = moment(date); + + let daysToRoll = null + let rolldate = null + + if(result.usage.rollover < 10){ + rolldate = 0+''+result.usage.rollover; + rolldate = moment(new Date(date.getFullYear(), date.getMonth()+1, rolldate)); + daysToRoll = rolldate.diff(today,'days'); + console.log(daysToRoll); + }else{ + rolldate = result.usage.rollover; + rolldate = moment(new Date(date.getFullYear(), date.getMonth()+1, rolldate)); + daysToRoll = rolldate.diff(today,'days'); + console.log(daysToRoll); + } + if (result.usage.allowance1_mb == 100000000) { // unlimited test console.log('unlimited account'); - const timestamp = moment(result.usage.lastUpdated).fromNow(); - tray.setToolTip(`You have used D:${formatFileSize(result.usage.down1, 2)} U:${formatFileSize(result.usage.up1, 2)} as of ${timestamp}, ${result.usage.rollover} Day/s till rollover`); + tray.setToolTip(`You have used D:${formatFileSize(result.usage.down1, 2)} U:${formatFileSize(result.usage.up1, 2)} as of ${timestamp}, ${daysToRoll} Day/s till rollover`); } - if (result.usage.left1 == '') { // corp test + else if (result.usage.left1 == '') { // corp test console.log('corp account'); - tray.setToolTip(`You have used D:${formatFileSize(result.usage.down1, 2)} U:${formatFileSize(result.usage.up1, 2)}, ${result.usage.rollover} Day/s till rollover`); + tray.setToolTip(`You have used D:${formatFileSize(result.usage.down1, 2)} U:${formatFileSize(result.usage.up1, 2)}, ${daysToRoll} Day/s till rollover`); } else { console.log('normal account'); - const timestamp = moment(result.usage.lastUpdated).fromNow(); const dataLeft_mb = (result.usage.left1 / 1000000) / JSON.parse(result.usage.allowance1_mb) ; const percent = dataLeft_mb * 100; console.log('data left', dataLeft_mb); @@ -137,21 +146,16 @@ const updateData = () => { }); } else { - tray.setToolTip(`An issue has occured retrieving your usage data`); - console.log('no xml in response payload, assuming an login error') - - ipcMain.on('asynchronous-message', (event, arg) => { - let res = 'no xml in response payload, assuming an login error' - event.sender.send('error', res); - toggleWindow(); - }); - + let message = `An issue has occured retrieving your usage data` + tray.setToolTip(message); + sendMessage('asynchronous-message','error',message) + console.log(message) } } else { tray.setToolTip(`An issue has occured retrieving your usage data`); console.log(error) } - }); + }); }; function formatFileSize(bytes,decimalPoint) { @@ -203,11 +207,13 @@ const createWindow = () => { window.loadURL(`file://${path.join(__dirname, 'views/settings.html')}`) // Hide the window when it loses focus - window.on('blur', () => { + window.on('blur', () => { + if (!window.webContents.isDevToolsOpened()) { window.hide() } - }) + }); + } const toggleWindow = () => { @@ -219,8 +225,14 @@ const toggleWindow = () => { const trayPositionVert = cursorPosition.y >= primarySize.height/2 ? 'bottom' : 'top'; const trayPositionHoriz = cursorPosition.x >= primarySize.width/2 ? 'right' : 'left'; window.setPosition(getTrayPosX(), getTrayPosY()); + + + + window.show(); window.focus(); + + function getTrayPosX(){ // Find the horizontal bounds if the window were positioned normally @@ -246,21 +258,16 @@ const showWindow = () => { const position = getWindowPosition(); window.setPosition(position.x, position.y, false); window.show(); - } -ipcMain.on('show-window', (event, arg) => { - - // send user settings to settings.html - let userSettings = { - un: store.get('username'), - pw: store.get('password'), - } +const sendMessage = (channel, eventName, message) => { + ipcMain.on(channel, (event, arg) => { + event.sender.send(eventName, message); + toggleWindow(); + }); +} - event.sender.send('asynchronous-reply', userSettings) - showWindow() -}) // receive message from index.html ipcMain.on('asynchronous-message', (event, arg) => { @@ -269,14 +276,13 @@ ipcMain.on('asynchronous-message', (event, arg) => { store.set('username', arg.un), store.set('password', arg.pw), - updateData(), - window.hide(); + updateData(); // send message to index.html - let userSettings = { - un: store.get('username'), - pw: store.get('password'), - } + // let userSettings = { + // un: store.get('username'), + // pw: store.get('password'), + // } - event.sender.send('asynchronous-reply', userSettings); + //event.sender.send('asynchronous-reply', userSettings); }); diff --git a/package-lock.json b/package-lock.json index 19fd1ff..222b893 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "aussiebb_tray", - "version": "1.0.0", + "version": "0.0.2", "lockfileVersion": 1, "dependencies": { "@types/node": { @@ -24,6 +24,11 @@ "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" }, + "argparse": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", + "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==" + }, "array-find-index": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/array-find-index/-/array-find-index-1.0.2.tgz", @@ -100,8 +105,12 @@ "bluebird": { "version": "3.5.1", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz", - "integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA==", - "dev": true + "integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA==" + }, + "bluebird-lst": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/bluebird-lst/-/bluebird-lst-1.0.5.tgz", + "integrity": "sha512-Ey0bDNys5qpYPhZ/oQ9vOEvD0TYQDTILMXWP2iGfvMg7rSDde+oV4aQQgqRH+CvBFNz2BSDQnPGMUl6LKBUUQA==" }, "boom": { "version": "4.3.1", @@ -113,12 +122,29 @@ "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==" }, + "buffer-from": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.0.0.tgz", + "integrity": "sha512-83apNb8KK0Se60UE1+4Ukbe3HbfELJ6UlI4ldtOGs7So4KD26orJM8hIY9lxdzP+UpItH1Yh/Y8GUvNFWFFRxA==" + }, "buffers": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/buffers/-/buffers-0.1.1.tgz", "integrity": "sha1-skV5w77U1tOWru5tmorn9Ugqt7s=", "dev": true }, + "builder-util-runtime": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/builder-util-runtime/-/builder-util-runtime-4.2.0.tgz", + "integrity": "sha512-cROCExnJOJvRD58HHcnrrgyRAoDHGZT0hKox0op7vTuuuRC/1JKMXvSR+Hxy7KWy/aEmKu0HfSqMd4znDEqQsA==", + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==" + } + } + }, "builtin-modules": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz", @@ -290,6 +316,11 @@ "resolved": "https://registry.npmjs.org/electron-download/-/electron-download-3.3.0.tgz", "integrity": "sha1-LP1U1pZsAZxNSa1l++Zcyc3vaMg=" }, + "electron-is-dev": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/electron-is-dev/-/electron-is-dev-0.3.0.tgz", + "integrity": "sha1-FOb9pcaOnk7L7/nM8DfL18BcWv4=" + }, "electron-osx-sign": { "version": "0.4.10", "resolved": "https://registry.npmjs.org/electron-osx-sign/-/electron-osx-sign-0.4.10.tgz", @@ -375,6 +406,11 @@ "resolved": "https://registry.npmjs.org/electron-store/-/electron-store-1.3.0.tgz", "integrity": "sha512-r1Pdl5MwpiCxgbsl0qnwv/GABO5+J/JTO16+KyqL+bOITIk9o3cq3Sw69uO9NgPkpfcKeEwxtJFbtbiBlGTiDA==" }, + "electron-updater": { + "version": "2.21.8", + "resolved": "https://registry.npmjs.org/electron-updater/-/electron-updater-2.21.8.tgz", + "integrity": "sha512-mYZu3m0i5okXtxh6uz1WbEiz3c1vVsITFPfrhctqaGFSaQbI/7YqzoY12QJX6KVO4Iu1hUMijr+wJlmVTlLhZw==" + }, "env-paths": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/env-paths/-/env-paths-1.0.0.tgz", @@ -390,6 +426,11 @@ "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.4.tgz", "integrity": "sha512-/NdNZVJg+uZgtm9eS3O6lrOLYmQag2DjdEXuPaHlZ6RuVqgqaVZfgYCepEIKsLqwdQArOPtC3XzRLqGGfT8KQQ==" }, + "esprima": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.0.tgz", + "integrity": "sha512-oftTcaMu/EGrEIu904mWteKIv8vMuOgGYo7EhVJJN00R/EED9DCua/xxHRdYnKtcECzVg7xOWhflvJMnqcFZjw==" + }, "extend": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.1.tgz", @@ -466,6 +507,23 @@ "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-0.30.0.tgz", "integrity": "sha1-8jP/zAjU2n1DLapEl3aYnbHfk/A=" }, + "fs-extra-p": { + "version": "4.5.2", + "resolved": "https://registry.npmjs.org/fs-extra-p/-/fs-extra-p-4.5.2.tgz", + "integrity": "sha512-ZYqFpBdy9w7PsK+vB30j+TnHOyWHm/CJbUq1qqoE8tb71m6qgk5Wa7gp3MYQdlGFxb9vfznF+yD4jcl8l+y91A==", + "dependencies": { + "fs-extra": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-5.0.0.tgz", + "integrity": "sha512-66Pm4RYbjzdyeuqudYqhFiNBbCIuI9kgRqLPSHIlXHidW8NIQtVdkM1yeZ4lXwuhbTETv3EUGMNHAAw6hiundQ==" + }, + "jsonfile": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", + "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=" + } + } + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -678,6 +736,11 @@ "resolved": "https://registry.npmjs.org/isstream/-/isstream-0.1.2.tgz", "integrity": "sha1-R+Y/evVa+m+S4VAOaQ64uFKcCZo=" }, + "js-yaml": { + "version": "3.11.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.11.0.tgz", + "integrity": "sha512-saJstZWv7oNeOyBh3+Dx1qWzhW0+e6/8eDzo7p5rDFqxntSztloLtuKu+Ejhtq82jsilwOIZYsCz+lIjthg1Hw==" + }, "jsbn": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz", @@ -714,6 +777,11 @@ "resolved": "https://registry.npmjs.org/klaw/-/klaw-1.3.1.tgz", "integrity": "sha1-QIhDO0azsbolnXh4XY6W9zugJDk=" }, + "lazy-val": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/lazy-val/-/lazy-val-1.0.3.tgz", + "integrity": "sha512-pjCf3BYk+uv3ZcPzEVM0BFvO9Uw58TmlrU0oG5tTrr9Kcid3+kdKxapH8CjdYmVa2nO5wOoZn2rdvZx2PKj/xg==" + }, "load-json-file": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz", @@ -737,6 +805,11 @@ "integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=", "dev": true }, + "lodash.isequal": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/lodash.isequal/-/lodash.isequal-4.5.0.tgz", + "integrity": "sha1-QVxEePK8wwEgwizhDtMib30+GOA=" + }, "loud-rejection": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/loud-rejection/-/loud-rejection-1.6.0.tgz", @@ -1102,6 +1175,16 @@ "resolved": "https://registry.npmjs.org/sntp/-/sntp-2.1.0.tgz", "integrity": "sha512-FL1b58BDrqS3A11lJ0zEdnJ3UOKqVxawAkF3k7F0CVN7VQ34aZrV+G8BZ1WC9ZL7NyrwsW0oviwsWDgRuVYtJg==" }, + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" + }, + "source-map-support": { + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.5.tgz", + "integrity": "sha512-mR7/Nd5l1z6g99010shcXJiNEaf3fEtmLhRB/sBcQVJGodcHCULPp2y4Sfa43Kv2zq7T+Izmfp/WHCR6dYkQCA==" + }, "spdx-correct": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.0.0.tgz", @@ -1127,6 +1210,11 @@ "resolved": "https://registry.npmjs.org/speedometer/-/speedometer-0.1.4.tgz", "integrity": "sha1-mHbb0qFp0xFUAtSObqYynIgWpQ0=" }, + "sprintf-js": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" + }, "sshpk": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/sshpk/-/sshpk-1.14.1.tgz", @@ -1243,8 +1331,7 @@ "universalify": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.1.tgz", - "integrity": "sha1-+nG63UQ3r0wUiEHjs7Fl+enlkLc=", - "dev": true + "integrity": "sha1-+nG63UQ3r0wUiEHjs7Fl+enlkLc=" }, "utf8-byte-length": { "version": "1.0.4", diff --git a/package.json b/package.json index 2968155..ffea179 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { "name": "aussiebb_tray", - "version": "0.0.3", + "version": "0.5.0-beta", "description": "Display your monthly useage and data left.", "main": "main.js", "scripts": { + "publish": "build --mac --win -p always", "start": "electron .", "package-mac": "electron-packager . --overwrite --platform=darwin --arch=x64 --icon=aussie_icon.icns --prune=true --out=releases", "package-win": "electron-packager . --overwrite --asar=true --platform=win32 --icon=aussie_icon.ico --prune=true --out=releases --version-string.CompanyName=CE --version-string.FileDescription=CE --version-string.ProductName=\"AussieBB Tray App\"", @@ -11,13 +12,14 @@ }, "repository": { "type": "git", - "url": "" + "url": "https://github.com/lukealford/aussiebb_tray" }, "author": "Luke Alford", "license": "MIT", "dependencies": { "electron": "~1.6.2", "electron-store": "^1.3.0", + "electron-updater": "^2.21.8", "moment": "^2.18.1", "request": "^2.81.0", "xml2js": "^0.4.19" diff --git a/views/settings.html b/views/settings.html index 47ef50a..1e3d37c 100644 --- a/views/settings.html +++ b/views/settings.html @@ -1,139 +1,268 @@ - -
+
-

Tray App

+

AUNT

+
-
-

Username

- -
-
-

Password

- -
+
+

Username

+ +
+
+

Password

+ +
+ - - -
+ + +
\ No newline at end of file