1 /* coded by Ketmar // Invisible Vector (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
10 ////////////////////////////////////////////////////////////////////////////////
11 Components
.utils
.import("chrome://guerilla-script-jscode/content/modules/signals.jsm");
12 let asvc
= Components
.classes
["@mozilla.org/alerts-service;1"].getService(Ci
.nsIAlertsService
);
15 addSignalListener("package-downloader", function (signame
, state
) {
16 conlog("package-downloader: phase=", state
.phase
, "; url=", state
.url
);
18 if (state
.phase
.substr(0, 6) === "error-") {
20 asvc
.showAlertNotification(
21 "chrome://guerilla-script-misc/content/icon.png",
22 "GuerillaScript package manager",
23 "DOWNLOAD FAILED! ("+state
.phase
+")\n"+state
.url
,
27 //error-main-content-type
30 } else if (state
.phase
=== "complete") {
32 asvc
.showAlertNotification(
33 "chrome://guerilla-script-misc/content/icon.png",
34 "GuerillaScript package manager",
35 "DOWNLOAD COMPLETE!\n"+state
.url
,
42 ////////////////////////////////////////////////////////////////////////////////
44 let lastUpdateCount
= 0;
45 let pkgList
= {lmod
:-1};
48 ////////////////////////////////////////////////////////////////////////////////
49 exports
.getPackageList = function () {
50 if (lastUpdateCount
!= pkgList
.lmod
) {
51 pkgList
.lmod
= lastUpdateCount
;
52 pkgList
.list
= pkgDB
.getActivePackages();
53 //for (let pi of pkgList.list) conlog("id=", pi.id, "; name=", pi.name);
59 exports
.getLastUpdateTimeForId = function (pkgid
) pkgDB
.getPackageUpdateTimeById(pkgid
);
62 ////////////////////////////////////////////////////////////////////////////////
66 let queue
= new Array();
69 ////////////////////////////////////////////////////////////////////////////////
70 exports
.__defineGetter__("inProgress", function () (dldr
&& dldr
.running
));
73 ////////////////////////////////////////////////////////////////////////////////
74 exports
.cancel = function () {
76 if (dldr
.running
) dldr
.cancel();
83 ////////////////////////////////////////////////////////////////////////////////
84 function pingQueue () {
85 if (dldr
) return; // in progress
86 let qo
; // queue object
88 if (queue
.length
== 0) return; // nothing to do
91 queue
.splice(0, 1); // remove first element
93 //conlog("NO URL for '", qo.name, "'");
94 // no url: update request; find package
95 let pi
= pkgDB
.getActivePackageByName(qo
.name
);
99 "pi.id=", pi.id, "\n",
100 "pi.name=", pi.name, "\n",
101 "pi.dirname=", pi.dirname, "\n",
102 "pi.version=", pi.version, "\n",
103 "pi.url=", pi.url, "\n"
109 logError("can't update unknown package '"+qo
.name
+"'");
114 //conlog("GS: qo.name=", qo.name, "; qo.url=", qo.url);
116 dldr
= new PkgDownloader(qo
.url
);
117 let skipCancel
= false;
119 dldr
.onError = function () {
120 /*++lastUpdateCount;*/
121 emitSignal("package-manager", {name
:qo
.name
, phase
:"error"});
122 logError("package '", qo
.name
, "': download failed!");
126 dldr
.onCancel = function () {
127 /*++lastUpdateCount;*/
129 emitSignal("package-manager", {name
:qo
.name
, phase
:"cancelled"});
130 logError("package '", qo
.name
, "': download cancelled!");
135 dldr
.onComplete = function () {
137 let upd
= pkgDB
.checkAndUpdate(qo
.name
, this.pkg
);
138 conlog((upd
? "" : "NOT "), "UPDATED: '", qo
.name
, "'");
143 emitSignal("package-manager", {name
:qo
.name
, phase
:"complete"});
146 dldr
.onMainReceived = function () {
147 conlog("package '", qo
.name
, "': received main file");
148 let res
= pkgDB
.checkAndUpdate(qo
.name
, this.pkg
, {checkOnly
:true});
150 conlog("package '", qo
.name
, "' is up-to-date");
152 emitSignal("package-manager", {name
:qo
.name
, phase
:"complete"});
156 emitSignal("package-manager", {name
:qo
.name
, phase
:"started"});
161 ////////////////////////////////////////////////////////////////////////////////
162 exports
.install = function (name
, url
) {
163 if (typeof(name
) !== "string" || !name
) throw new Error("invalid package name");
164 if (typeof(url
) !== "string" || !(/https?:/.test(url
))) throw new Error("invalid package url");
167 for (let qo
of queue
) {
168 if (qo
.name
== name
) {
169 if (qo
.url
!= url
) throw new Error("conflicting URLs for package '"+name
+"'");
174 if (!found
) queue
.push({name
:name
, url
:url
});
179 ////////////////////////////////////////////////////////////////////////////////
180 exports
.update = function (name
) {
181 if (typeof(name
) !== "string" || !name
) throw new Error("invalid package name");
184 for (let qo
of queue
) {
185 if (qo
.name
== name
) {
190 if (!found
) queue
.push({name
:name
, url
:null});
195 ////////////////////////////////////////////////////////////////////////////////
196 exports
.remove = function (name
) {
197 if (typeof(name
) !== "string" || !name
) throw new Error("invalid package name");
198 if (dldr
&& dldr
.running
) throw new Error("package manager is busy");
200 pkgDB
.removePackage(name
);
201 conlog("package '", name
, "' removed");
202 } catch (e
) { logException("WTF", e
); }