var qaNotifications = { storageService : Components.classes["@mozilla.org/storage/service;1"] .getService(Ci.mozIStorageService), dbHandle : null, openDatabase : function() { var file = Cc["@mozilla.org/file/directory_service;1"] .getService(Ci.nsIProperties) .get("ProfD", Ci.nsIFile); file.append("mozqa.sqlite"); this.dbHandle = this.storageService.openDatabase(file); this.createNotificationTable(); }, closeDatabase : function() { // there's no way to actually close a database, but setting // the db reference to null will cause the db handle to become // elegible for GC which is about as good as it gets dbHandle = null; }, createNotificationTable : function() { this.db.executeSimpleSQL("CREATE TABLE if not exists notifications ( \ id text, \ datetime integer, \ firstNotification integer, \ secondNotification integer, \ serializedJSData string)"); }, checkNotificationStatus : function() { var req = new XMLHttpRequest(); var url = qaPref.getPref('qa.extension.hermes.url', 'char'); req.open('GET', url, true); req.onreadystatechange = function(evt) { if (req.readyState == 4 && req.status == 200) { var notif = new Notification('xml', req.responseXML); notif.serializeToDbIfNotExists(); } }; req.send(null); // see if we are elegible for notification: var time = qaPref.getPref(qaPref.prefBase+'.lastNotificationCheckTime', 'int'); var interval = qaPref.getPref(qaPref.prefBase+'.minNotificationCheckInterval', 'int'); if (time + interval > new Date().valueOf()) return; // nothing to see here, try again later var sth = this.db.createStatement("SELECT * FROM notifications"); var notifications = []; try { while (sth.executeStep()) { notifications.push(new Notification('json', MochiKit.Base.evalJSON( sth.getString(4)))); } } finally { sth.reset(); } if (notifications[0] && notifications[0].okToShow() == true) { notifications[0].displayToBox(); $('qa-notify').removeAttribute('hidden'); } // reset the interval timer qaPref.setPref(qaPref.prefBase+'.lastNotificationCheckTime', new Date().valueOf(), 'int'); }, showHideNotify: function(bool) { var notify = $('qa-notify'); if (bool == true) { notify.removeAttribute("hidden"); } else { notify.setAttribute("hidden", "true"); } }, getNotificationSettings : function(bool) { var prefs = qaPref.getPref(qaPref.prefBase+'.notificationSettings', 'char'); return prefs.split(","); }, }; qaNotifications.__defineGetter__('db', function() { if (this.dbHandle != null) return this.dbHandle; else { this.openDatabase(); return this.dbHandle; } }); function Notification() { } function Notification(type, data) { if (type == 'json') { this.id = data.id; this.notificationClass = data.notificationClass; this.type = data.type; this.headline = data.headline; this.datetime = data.datetime; this.place = data.place; this.infotext = data.infotext; this.infolinktext = data.infolinktext; this.infolinkhref = data.infolinkhref; this.golinktext = data.golinktext; this.golinkhref = data.golinkhref; } else if (type == 'xml') { if (data.getElementsByTagName('notifications') == null) return; var notifs = data.getElementsByTagName('notification'); for (var i=0; i