diff --git a/clientapp/app.js b/clientapp/app.js index 84fd96b..08ce27d 100644 --- a/clientapp/app.js +++ b/clientapp/app.js @@ -13,6 +13,7 @@ var Router = require('./router'); var Storage = require('./storage'); var xmppEventHandlers = require('./helpers/xmppEventHandlers'); var Notify = require('notify.js'); +var Desktop = require('./helpers/desktop'); module.exports = { @@ -33,6 +34,7 @@ module.exports = { async.series([ function (cb) { app.notifications = new Notify(); + app.desktop = new Desktop(); app.storage = new Storage(); app.storage.open(cb); }, diff --git a/clientapp/helpers/desktop.js b/clientapp/helpers/desktop.js new file mode 100644 index 0000000..cc95f35 --- /dev/null +++ b/clientapp/helpers/desktop.js @@ -0,0 +1,77 @@ +var WildEmitter = require('wildemitter'); + + +function DesktopApp(opts) { + WildEmitter.call(this); + + var self = this; + opts = opts || {}; + + this.mozAppManifest = opts.manifest || window.location.origin + '/manifest.webapp'; + + this.installed = !!window.macgap || !!window.fluid; + this.installable = !!window.macgap || !!window.fluid || !!navigator.mozApps; + this.uninstallable = false; + + if (window.macgap || this.fluid) { + this.installed = true; + } else if (navigator.mozApps) { + var req = navigator.mozApps.getSelf(); + req.onsuccess = function (e) { + self.mozApp = e.result; + if (e.result) { + self.installed = true; + self.uninstallable = true; + } + }; + } + + if (window.macgap) { + document.addEventListener('sleep', function () { + self.emit('sleep'); + }, true); + + document.addEventListener('wake', function () { + self.emit('wake'); + }, true); + } +} + +DesktopApp.prototype = Object.create(WildEmitter.prototype, { + constructor: { + value: DesktopApp + } +}); + +DesktopApp.prototype.isRunning = function () { + return !!window.macgap || !!window.fluid || !!this.mozApp; +}; + +DesktopApp.prototype.install = function (cb) { + if (navigator.mozApps) { + var req = navigator.mozApps.install(this.mozAppManifest); + req.onsuccess = function (e) { + cb(null, e); + }; + req.onerror = function (e) { + cb(e); + }; + } +}; + +DesktopApp.prototype.uninstall = function () { + if (this.mozApp) { + return this.mozApp.uninstall(); + } +}; + +DesktopApp.prototype.updateBadge = function (badge) { + if (window.macgap) { + window.macgap.dock.badge = badge || ''; + } else if (window.fluid) { + window.fluid.dockBadge = badge || ''; + } +}; + + +module.exports = DesktopApp; diff --git a/clientapp/models/state.js b/clientapp/models/state.js index 72ea61b..5b423e4 100644 --- a/clientapp/models/state.js +++ b/clientapp/models/state.js @@ -16,25 +16,12 @@ module.exports = HumanModel.define({ } self.markActive(); }); - if (window.macgap) { - document.addEventListener('sleep', function () { - clearTimeout(this.idleTimer); - console.log('went to sleep'); - self.markInactive(); - }, true); - } - if (window.navigator.mozApps) { - this.installable = true; - var req = navigator.mozApps.checkInstalled(window.location.origin + '/manifest.webapp'); - req.onsuccess = function (e) { - if (req.result) { - self.installedFirefox = true; - } - }; - } - - //this.allowAlerts = app.notifications.allowed(); + app.desktop.on('sleep', function () { + clearTimeout(this.idleTimer); + console.log('went to sleep'); + self.markInactive(); + }); this.markActive(); }, @@ -47,9 +34,7 @@ module.exports = HumanModel.define({ idleSince: 'date', allowAlerts: ['bool', true, false], badge: ['string', true, ''], - pageTitle: ['string', true, ''], - installable: ['bool', true, false], - installedFirefox: ['bool', true, false] + pageTitle: ['string', true, ''] }, derived: { title: { diff --git a/clientapp/pages/main.js b/clientapp/pages/main.js index 1f94dd8..c6b621f 100644 --- a/clientapp/pages/main.js +++ b/clientapp/pages/main.js @@ -40,7 +40,11 @@ module.exports = BasePage.extend({ } }, installFirefox: function () { - navigator.mozApps.install(window.location.origin + '/manifest.webapp'); + if (!app.desktop.installed) { + app.desktop.install(); + } else { + app.desktop.uninstall(); + } }, handleAvatarChangeDragOver: function (e) { e.preventDefault(); diff --git a/package.json b/package.json index c45867d..29b3963 100644 --- a/package.json +++ b/package.json @@ -18,12 +18,12 @@ "node-uuid": "1.4.1", "semi-static": "0.0.4", "sound-effect-manager": "0.0.5", - "human-model": "1.1.3", + "human-model": "1.4.0", "human-view": "1.1.2", "templatizer": "0.1.2", "underscore": "1.5.1", "raf-component": "1.1.1", - "stanza.io": "2.5.2", + "stanza.io": "2.5.4", "notify.js": "0.0.1" }, "devDependencies": { diff --git a/public/x-manifest.cache b/public/x-manifest.cache index fdaba23..1f5d39e 100644 --- a/public/x-manifest.cache +++ b/public/x-manifest.cache @@ -1,5 +1,5 @@ CACHE MANIFEST -# 0.0.1 1381556294101 +# 0.0.1 1381729479369 CACHE: /app.js diff --git a/public/x-manifest.webapp b/public/x-manifest.webapp index 1d03313..fc07da5 100644 --- a/public/x-manifest.webapp +++ b/public/x-manifest.webapp @@ -3,5 +3,13 @@ "description": "Modern XMPP client", "icons": { "128": "/images/icon_128x128.png" + }, + "permissions": { + "storage": { + "description": "Required for caching contact information and message history." + }, + "desktop-notification": { + "description": "Required to show alerts for new messages." + } } }