home *** CD-ROM | disk | FTP | other *** search
- function Update(application) {
- this._application = application;
-
- (this._timerOne = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer))
- .initWithCallback(this, 24 * 3600 * 1000, Ci.nsITimer.TYPE_REPEATING_SLACK);
-
- let Preferences = this._application.core.Preferences;
- let now = parseInt(Date.now() / 1000, 10);
- let PREF_LAST_UPDATE_TIME = this._application.name + ".updates.widgets.lastUpdateTime";
-
- if (Math.abs((Preferences.get(PREF_LAST_UPDATE_TIME) || 0) - now) > 24 * 3600) {
- Preferences.set(PREF_LAST_UPDATE_TIME, now);
-
- (this._timerTwo = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer))
- .initWithCallback(this, 30 * 1000, Ci.nsITimer.TYPE_ONE_SHOT);
- }
- };
-
- Update.prototype = {
- notify: function() {
- this._update();
- },
-
- _activePresets: function() {
- var presets = [];
- var entries = this._application.directories.XBPresets.directoryEntries;
-
- while (entries.hasMoreElements()) {
- let file = entries.getNext().QueryInterface(Ci.nsIFile);
- let address = decodeURIComponent(file.leafName);
- let preset = new this._application.core.Lib.XB.Preset(file, address);
-
- let used = false;
- for each (let widgetInfo in preset.widgetsInfo)
- if (this._widgetLibrary.isKnownWidget(widgetInfo.protoID)) {
- used = true;
- break;
- }
-
- if (used)
- presets.push(preset);
- else
- file.remove(false);
- }
-
- return presets;
- },
-
- _compareVersion: function (a, b) {
- if (!this._comparator)
- this._comparator = Cc["@mozilla.org/xpcom/version-comparator;1"].getService(Ci.nsIVersionComparator);
- return this._comparator.compare(a.version, b.version);
- },
-
- _update: function() {
- this._presets = [];
- this._manifests = [];
-
- this._packageManager = this._application.packageManager;
- this._widgetLibrary = this._application.widgetLibrary;
- this._misc = this._application.core.Lib.misc;
-
- this._updateStepOne();
- },
-
- _updateStepOne: function() {
- var knownPresets = this._activePresets();
-
- var items = [];
-
- for each (let preset in knownPresets) {
- items.push({
- url: preset.address,
- preset: preset
- });
- }
-
- var context = this;
- this.queue = new this._misc.DownloadQueue(
- items,
- function (items) { context._updateStepTwo(items); },
- null
- );
- },
-
- _updateStepTwo: function(items) {
- var unknownPackageIds = [],
- knownPackageIds = this._packageManager.packagesIDs;
-
- knownPackageIds = knownPackageIds.filter(function(id) {
- return !this._application.isPreinstalledPackage(id);
- }, this);
-
- for each (let item in items) {
- if (item.status === Cr.NS_OK) {
- try {
- let preset = new this._application.core.Lib.XB.Preset(item.file, item.url);
- if (this._compareVersion(preset, item.preset) > 0) {
- this._presets.push(preset);
-
- for each (let info in preset.widgetsInfo)
- if (!this._packageManager.isPackageInstalled(info.packageID) && (unknownPackageIds.indexOf(info.packageID) == -1))
- unknownPackageIds.push(info.packageID);
- }
- }
- catch(e) {
- }
- }
- }
-
- this.queue.destroy();
- items = [];
-
- for each (let id in unknownPackageIds) {
- let url = id;
- items.push({
- url: url
- });
- }
-
- for each (let id in knownPackageIds) {
- if (this._widgetLibrary.getWidgetProtoIDs(id).length == 0)
- continue;
- let info = this._packageManager.getPackageInfo(id);
- items.push({
- url: info.uri,
- info: info
- });
- }
-
- var context = this;
- this.queue = new this._misc.DownloadQueue(
- items,
- function (items) { context._updateStepThree(items); },
- null
- );
- },
-
- _updateStepThree: function(items) {
- for each (let item in items) {
- if (item.status === Cr.NS_OK) {
- try {
- let manifest = new this._application.core.Lib.XB.PackageManifest(item.url, item.file);
- if (item.info) {
- let [info, ] = this._application.selectBestPackage(manifest);
- if (info && this._compareVersion(info, item.info) > 0)
- this._manifests.push(manifest);
- }
- else
- this._manifests.push(manifest);
- }
- catch (e) {
- }
- }
- }
-
- this.queue.destroy();
-
- if (this._presets.length == 0 && this._manifests == 0)
- return;
-
- var context = this;
- var window = this._application.core.Lib.misc.openWindow({
- url: "chrome://" + this._application.name +
- "/content/custombar/dialogs/package-management/notification-update/notification-update.xul",
- features: "__popup__",
- name: "notification-update",
- presets: this._presets,
- manifests: this._manifests,
- application: this._application,
- install: function(simple) { context._openDialog(simple); }
- });
- },
-
- _openDialog: function(simple) {
- var window = this._application.core.Lib.misc.openWindow({
- url: "chrome://" + this._application.name +
- "/content/custombar/dialogs/package-management/update/update.xul",
- features: "__popup__",
- name: "package-management-update",
- mode: "update",
- presets: this._presets,
- manifests: this._manifests,
- application: this._application,
- simple: !!simple
- });
- }
- };