Changeset 662

Show
Ignore:
Timestamp:
02/16/08 04:52:29 (10 months ago)
Author:
ma..@jesperkristensen.dk
Message:

Use one code style across the whole config.js file

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/config-service/src/chrome/chromeFiles/content/config.js

    r661 r662  
    22// used anywhere within this file and versioning.js 
    33 
    4 function Config() { 
     4function Config() 
     5
    56  this._scripts = null; 
    67  this._configFile = this._scriptDir; 
     
    1314}; 
    1415 
     16Config.prototype = { 
     17  addObserver: function(observer, script) 
     18  { 
     19    var observers = script ? script._observers : this._observers; 
     20    observers.push(observer); 
     21  }, 
     22 
     23  removeObserver: function(observer, script) 
     24  { 
     25    var observers = script ? script._observers : this._observers; 
     26    var index = observers.indexOf(observer); 
     27    if (index == -1) throw new Error("Observer not found"); 
     28    observers.splice(index, 1); 
     29  }, 
     30 
     31  _notifyObservers: function(script, event, data) 
     32  { 
     33    var observers = this._observers.concat(script._observers); 
     34    for (var i = 0, observer; observer = observers[i]; i++) 
     35      observer.notifyEvent(script, event, data); 
     36  }, 
     37 
     38  _changed: function(script, event, data) 
     39  { 
     40    this._save(); 
     41    this._notifyObservers(script, event, data); 
     42  }, 
     43 
     44  installIsUpdate: function(script) 
     45  { 
     46    return this._find(script) > -1; 
     47  }, 
     48 
     49  _find: function(aScript) 
     50  { 
     51    namespace = aScript._namespace.toLowerCase(); 
     52    name = aScript._name.toLowerCase(); 
     53 
     54    for (var i = 0, script; script = this._scripts[i]; i++) 
     55      if (script._namespace.toLowerCase() == namespace && script._name.toLowerCase() == name) 
     56        return i; 
     57 
     58    return -1; 
     59  }, 
     60 
     61  _load: function() 
     62  { 
     63    var domParser = Components.classes["@mozilla.org/xmlextras/domparser;1"] 
     64                              .createInstance(Components.interfaces.nsIDOMParser); 
     65 
     66    var configContents = getContents(this._configFile); 
     67    var doc = domParser.parseFromString(configContents, "text/xml"); 
     68    var nodes = doc.evaluate("/UserScriptConfig/Script", doc, null, 0, null); 
     69 
     70    this._scripts = []; 
     71 
     72    for (var node = null; node = nodes.iterateNext(); ) { 
     73      var script = new Script(this); 
     74 
     75      for (var i = 0, childNode; childNode = node.childNodes[i]; i++) { 
     76        switch (childNode.nodeName) { 
     77        case "Include": 
     78          script._includes.push(childNode.firstChild.nodeValue); 
     79          break; 
     80        case "Exclude": 
     81          script._excludes.push(childNode.firstChild.nodeValue); 
     82          break; 
     83        case "Require": 
     84          var scriptRequire = new ScriptRequire(script); 
     85          scriptRequire._filename = childNode.getAttribute("filename"); 
     86          script._requires.push(scriptRequire); 
     87          break; 
     88        case "Resource": 
     89          var scriptResource = new ScriptResource(script); 
     90          scriptResource._name = childNode.getAttribute("name"); 
     91          scriptResource._filename = childNode.getAttribute("filename"); 
     92          scriptResource._mimetype = childNode.getAttribute("mimetype"); 
     93          scriptResource._charset  = childNode.getAttribute("charset"); 
     94          script._resources.push(scriptResource); 
     95          break; 
     96        } 
     97      } 
     98 
     99      script._filename = node.getAttribute("filename"); 
     100      script._name = node.getAttribute("name"); 
     101      script._namespace = node.getAttribute("namespace"); 
     102      script._description = node.getAttribute("description"); 
     103      script._enabled = node.getAttribute("enabled") == true.toString(); 
     104      script._basedir = node.getAttribute("basedir") || "."; 
     105 
     106      this._scripts.push(script); 
     107    } 
     108  }, 
     109 
     110  _save: function() 
     111  { 
     112    var doc = Components.classes["@mozilla.org/xmlextras/domparser;1"] 
     113      .createInstance(Components.interfaces.nsIDOMParser) 
     114      .parseFromString("<UserScriptConfig></UserScriptConfig>", "text/xml"); 
     115 
     116    for (var i = 0, scriptObj; scriptObj = this._scripts[i]; i++) { 
     117      var scriptNode = doc.createElement("Script"); 
     118 
     119      for (var j = 0; j < scriptObj._includes.length; j++) { 
     120        var includeNode = doc.createElement("Include"); 
     121        includeNode.appendChild(doc.createTextNode(scriptObj._includes[j])); 
     122        scriptNode.appendChild(doc.createTextNode("\n\t\t")); 
     123        scriptNode.appendChild(includeNode); 
     124      } 
     125 
     126      for (var j = 0; j < scriptObj._excludes.length; j++) { 
     127        var excludeNode = doc.createElement("Exclude"); 
     128        excludeNode.appendChild(doc.createTextNode(scriptObj._excludes[j])); 
     129        scriptNode.appendChild(doc.createTextNode("\n\t\t")); 
     130        scriptNode.appendChild(excludeNode); 
     131      } 
     132 
     133      for (var j = 0; j < scriptObj._requires.length; j++) { 
     134        var req = scriptObj._requires[j]; 
     135        var resourceNode = doc.createElement("Require"); 
     136 
     137        resourceNode.setAttribute("filename", req._filename); 
     138 
     139        scriptNode.appendChild(doc.createTextNode("\n\t\t")); 
     140        scriptNode.appendChild(resourceNode); 
     141      } 
     142 
     143      for (var j = 0; j< scriptObj._resources.length; j++) { 
     144        var imp = scriptObj._resources[j]; 
     145        var resourceNode = doc.createElement("Resource"); 
     146 
     147        resourceNode.setAttribute("name", imp._name); 
     148        resourceNode.setAttribute("filename", imp._filename); 
     149        resourceNode.setAttribute("mimetype", imp._mimetype); 
     150        if (imp._charset) { 
     151          resourceNode.setAttribute("charset", imp._charset); 
     152        } 
     153 
     154        scriptNode.appendChild(doc.createTextNode("\n\t\t")); 
     155        scriptNode.appendChild(resourceNode); 
     156      } 
     157 
     158      scriptNode.appendChild(doc.createTextNode("\n\t")); 
     159 
     160      scriptNode.setAttribute("filename", scriptObj._filename); 
     161      scriptNode.setAttribute("name", scriptObj._name); 
     162      scriptNode.setAttribute("namespace", scriptObj._namespace); 
     163      scriptNode.setAttribute("description", scriptObj._description); 
     164      scriptNode.setAttribute("enabled", scriptObj._enabled); 
     165      scriptNode.setAttribute("basedir", scriptObj._basedir); 
     166 
     167      doc.firstChild.appendChild(doc.createTextNode("\n\t")); 
     168      doc.firstChild.appendChild(scriptNode); 
     169    } 
     170 
     171    doc.firstChild.appendChild(doc.createTextNode("\n")); 
     172 
     173    var configStream = getWriteStream(this._configFile); 
     174    Components.classes["@mozilla.org/xmlextras/xmlserializer;1"] 
     175      .createInstance(Components.interfaces.nsIDOMSerializer) 
     176      .serializeToStream(doc, configStream, "utf-8"); 
     177    configStream.close(); 
     178  }, 
     179 
     180  parse: function(source, uri) 
     181  { 
     182    var ioservice = Components.classes["@mozilla.org/network/io-service;1"] 
     183                              .getService(Components.interfaces.nsIIOService); 
     184 
     185    var script = new Script(this); 
     186    script._downloadUrl = uri.spec; 
     187    script._enabled = true; 
     188 
     189    // read one line at a time looking for start meta delimiter or EOF 
     190    var lines = source.match(/.+/g); 
     191    var lnIdx = 0; 
     192    var result = {}; 
     193    var foundMeta = false; 
     194 
     195    while ((result = lines[lnIdx++])) { 
     196      if (result.indexOf("// ==UserScript==") == 0) { 
     197        foundMeta = true; 
     198        break; 
     199      } 
     200    } 
     201 
     202    // gather up meta lines 
     203    if (foundMeta) { 
     204      // used for duplicate resource name detection 
     205      var previousResourceNames = {}; 
     206 
     207      while ((result = lines[lnIdx++])) { 
     208        if (result.indexOf("// ==/UserScript==") == 0) { 
     209          break; 
     210        } 
     211 
     212        var match = result.match(/\/\/ \@(\S+)\s+([^\n]+)/); 
     213        if (match != null) { 
     214          switch (match[1]) { 
     215            case "name": 
     216            case "namespace": 
     217            case "description": 
     218              script["_" + match[1]] = match[2]; 
     219              break; 
     220            case "include": 
     221              script._includes.push(match[2]); 
     222              break; 
     223            case "exclude": 
     224              script._excludes.push(match[2]); 
     225              break; 
     226            case "require": 
     227              var reqUri = ioservice.newURI(match[2], null, uri); 
     228              var scriptRequire = new ScriptRequire(script); 
     229              scriptRequire._downloadUrl = reqUri.spec; 
     230              script._requires.push(scriptRequire); 
     231              break; 
     232            case "resource": 
     233              var res = match[2].match(/(\S+)\s+(.*)/); 
     234              if (res === null) { 
     235                // NOTE: Unlocalized strings 
     236                throw new Error("Invalid syntax for @resource declaration '" + 
     237                                match[2] + "'. Resources are declared like: " + 
     238                                "@resource <name> <url>."); 
     239              } 
     240 
     241              var resName = res[1]; 
     242              if (previousResourceNames[resName]) { 
     243                throw new Error("Duplicate resource name '" + resName + "' " + 
     244                                "detected. Each resource must have a unique " + 
     245                                "name."); 
     246              } else { 
     247                previousResourceNames[resName] = true; 
     248              } 
     249 
     250              var resUri = ioservice.newURI(res[2], null, uri); 
     251              var scriptResource = new ScriptResource(script); 
     252              scriptResource._name = resName; 
     253              scriptResource._downloadUrl = resUri.spec; 
     254              script._resources.push(scriptResource); 
     255              break; 
     256          } 
     257        } 
     258      } 
     259    } 
     260 
     261    // if no meta info, default to reasonable values 
     262    if (script._name == null) { 
     263      script._name = parseScriptName(uri); 
     264    } 
     265 
     266    if (script._namespace == null) { 
     267      script._namespace = uri.host; 
     268    } 
     269 
     270    if (!script._description) 
     271      script._description = ""; 
     272 
     273    if (script._includes.length == 0) { 
     274      script._includes.push("*"); 
     275    } 
     276 
     277    return script; 
     278  }, 
     279 
     280  install: function(script) 
     281  { 
     282    GM_log("> Config.install"); 
     283 
     284    var existingIndex = this._find(script); 
     285    if (existingIndex > -1) 
     286      this.uninstall(this._scripts[existingIndex], false); 
     287 
     288    script._initFile(script._tempFile); 
     289    script._tempFile = null; 
     290 
     291    for (var i = 0; i < script._requires.length; i++) 
     292      script._requires[i]._initFile(); 
     293 
     294    for (var i = 0; i < script._resources.length; i++) 
     295      script._resources[i]._initFile(); 
     296 
     297    this._scripts.push(script); 
     298    this._changed(script, "install", null); 
     299 
     300    GM_log("< Config.install"); 
     301  }, 
     302 
     303  uninstall: function(script, uninstallPrefs) 
     304  { 
     305    var idx = this._find(script); 
     306    this._scripts.splice(idx, 1); 
     307    this._changed(script, "uninstall", null); 
     308 
     309    if (script._basedir) // if script has its own dir, remove the dir + contents 
     310      script._basedirFile.remove(true); 
     311    else // if script is in the root, just remove the file 
     312      script._file.remove(false); 
     313 
     314    if (uninstallPrefs) // Remove saved preferences 
     315       GM_prefRoot.remove("scriptvals." + script._namespace + "/" + script._name + "."); 
     316  }, 
     317 
     318  /** 
     319   * Moves an installed user script to a new position in the array of installed scripts. 
     320   * 
     321   * @param script The script to be moved. 
     322   * @param destination Can be either (a) a numeric offset for the script to be 
     323   *                    moved or (b) another installet script to which position 
     324   *                    the script will be moved. 
     325   */ 
     326  move: function(script, destination) 
     327  { 
     328    var from = this._scripts.indexOf(script); 
     329    var to = -1; 
     330 
     331    // Make sure the user script is installed 
     332    if (from == -1) 
     333      return; 
     334 
     335    if (typeof destination == 'number') { // if destination is an offset 
     336      to = from + destination; 
     337      to = Math.max(0, to); 
     338      to = Math.min(this._scripts.length - 1, to); 
     339    } else { // if destination is a script object 
     340      to = this._scripts.indexOf(destination); 
     341    } 
     342 
     343    if (to == -1) 
     344      return; 
     345 
     346    var tmp = this._scripts.splice(from, 1)[0]; 
     347    this._scripts.splice(to, 0, tmp); 
     348    this._changed(script, 'move', to); 
     349  }, 
     350 
     351  get _scriptDir() 
     352  { 
     353    var newDir = this._newScriptDir; 
     354    if (newDir.exists()) 
     355      return newDir; 
     356 
     357    var oldDir = this._oldScriptDir; 
     358    if (oldDir.exists()) 
     359      return oldDir; 
     360 
     361    // if we called this function, we want a script dir. 
     362    // but, at this branch, neither the old nor new exists, so create one 
     363    newDir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755); 
     364 
     365    var defaultConfigFile = getContentDir(); 
     366    defaultConfigFile.append("default-config.xml"); 
     367 
     368    defaultConfigFile.copyTo(newDir, "config.xml"); 
     369    defaultConfigFile.permissions = 0644; 
     370 
     371    return newDir; 
     372  }, 
     373 
     374  get _newScriptDir() 
     375  { 
     376    var file = Components.classes["@mozilla.org/file/directory_service;1"] 
     377                         .getService(Components.interfaces.nsIProperties) 
     378                         .get("ProfD", Components.interfaces.nsILocalFile); 
     379    file.append("gm_scripts"); 
     380    return file; 
     381  }, 
     382 
     383  get _oldScriptDir() 
     384  { 
     385    var file = getContentDir(); 
     386    file.append("scripts"); 
     387    return file; 
     388  }, 
     389 
     390  get scripts() 
     391  { 
     392    return this._scripts.concat(); 
     393  }, 
     394 
     395  getScriptsForUrl: function(url, onlyEnabled) 
     396  { 
     397    var scripts = []; 
     398 
     399    scriptLoop: 
     400    for (var i = 0, script; script = this._scripts[i]; i++) { 
     401      if (script.enabled || !onlyEnabled) { 
     402        for (var j = 0, include; include = script._includes[j]; j++) { 
     403          if (convert2RegExp(include).test(url)) { 
     404            for (var k = 0, exclude; exclude = script._excludes[k]; k++) { 
     405              if (convert2RegExp(exclude).test(url)) { 
     406                continue scriptLoop; 
     407              } 
     408            } 
     409            scripts.push(script); 
     410            continue scriptLoop; 
     411          } 
     412        } 
     413      } 
     414    } 
     415 
     416    return scripts; 
     417  } 
     418}; 
     419 
    15420Components.classes["@mozilla.org/moz/jssubscript-loader;1"] 
    16421  .getService(Components.interfaces.mozIJSSubScriptLoader) 
    17422  .loadSubScript("chrome://greasemonkey/content/versioning.js"); 
    18  
    19 Config.prototype.addObserver = function(observer, script) { 
    20   var observers = script ? script._observers : this._observers; 
    21   observers.push(observer); 
    22 }; 
    23  
    24 Config.prototype.removeObserver = function(observer, script) { 
    25   var observers = script ? script._observers : this._observers; 
    26   var index = observers.indexOf(observer); 
    27   if (index == -1) throw new Error("Observer not found"); 
    28   observers.splice(index, 1); 
    29 }; 
    30  
    31 Config.prototype._notifyObservers = function(script, event, data) { 
    32   var observers = this._observers.concat(script._observers); 
    33   for (var i = 0, observer; observer = observers[i]; i++) 
    34     observer.notifyEvent(script, event, data); 
    35 }; 
    36  
    37 Config.prototype._changed = function(script, event, data) { 
    38   this._save(); 
    39   this._notifyObservers(script, event, data); 
    40 }; 
    41  
    42 Config.prototype.installIsUpdate = function(script) 
    43 { 
    44   return this._find(script) > -1; 
    45 }; 
    46  
    47 Config.prototype._find = function(aScript) { 
    48   namespace = aScript._namespace.toLowerCase(); 
    49   name = aScript._name.toLowerCase(); 
    50  
    51   for (var i = 0, script = null; (script = this._scripts[i]); i++) { 
    52     if (script._namespace.toLowerCase() == namespace && script._name.toLowerCase() == name) { 
    53       return i; 
    54     } 
    55   } 
    56  
    57   return -1; 
    58 }; 
    59  
    60 Config.prototype._load = function() { 
    61   var domParser = Components.classes["@mozilla.org/xmlextras/domparser;1"] 
    62                             .createInstance(Components.interfaces.nsIDOMParser); 
    63  
    64   var configContents = getContents(this._configFile); 
    65   var doc = domParser.parseFromString(configContents, "text/xml"); 
    66   var nodes = doc.evaluate("/UserScriptConfig/Script", doc, null, 0, null); 
    67  
    68   this._scripts = []; 
    69  
    70   for (var node = null; (node = nodes.iterateNext()); ) { 
    71     var script = new Script(this); 
    72  
    73     for (var i = 0, childNode = null; (childNode = node.childNodes[i]); i++) { 
    74       if (childNode.nodeName == "Include") { 
    75         script._includes.push(childNode.firstChild.nodeValue); 
    76       } else if (childNode.nodeName == "Exclude") { 
    77         script._excludes.push(childNode.firstChild.nodeValue); 
    78       } else if (childNode.nodeName == "Require") { 
    79         var scriptRequire = new ScriptRequire(script); 
    80         scriptRequire._filename = childNode.getAttribute("filename"); 
    81         script._requires.push(scriptRequire); 
    82       } else if (childNode.nodeName == "Resource") { 
    83         var scriptResource = new ScriptResource(script); 
    84         scriptResource._name = childNode.getAttribute("name"); 
    85         scriptResource._filename = childNode.getAttribute("filename"); 
    86         scriptResource._mimetype = childNode.getAttribute("mimetype"); 
    87         scriptResource._charset  = childNode.getAttribute("charset"); 
    88         script._resources.push(scriptResource); 
    89       } 
    90     } 
    91  
    92     script._filename = node.getAttribute("filename"); 
    93     script._name = node.getAttribute("name"); 
    94     script._namespace = node.getAttribute("namespace"); 
    95     script._description = node.getAttribute("description"); 
    96     script._enabled = node.getAttribute("enabled") == true.toString(); 
    97     script._basedir = node.getAttribute("basedir") || "."; 
    98  
    99     this._scripts.push(script); 
    100   } 
    101 }; 
    102  
    103 Config.prototype._save = function() { 
    104   var doc = Components.classes["@mozilla.org/xmlextras/domparser;1"] 
    105     .createInstance(Components.interfaces.nsIDOMParser) 
    106     .parseFromString("<UserScriptConfig></UserScriptConfig>", "text/xml"); 
    107  
    108   for (var i = 0, scriptObj = null; (scriptObj = this._scripts[i]); i++) { 
    109     var scriptNode = doc.createElement("Script"); 
    110  
    111     for (var j = 0; j < scriptObj._includes.length; j++) { 
    112       var includeNode = doc.createElement("Include"); 
    113       includeNode.appendChild(doc.createTextNode(scriptObj._includes[j])); 
    114       scriptNode.appendChild(doc.createTextNode("\n\t\t")); 
    115       scriptNode.appendChild(includeNode); 
    116     } 
    117  
    118     for (var j = 0; j < scriptObj._excludes.length; j++) { 
    119       var excludeNode = doc.createElement("Exclude"); 
    120       excludeNode.appendChild(doc.createTextNode(scriptObj._excludes[j])); 
    121       scriptNode.appendChild(doc.createTextNode("\n\t\t")); 
    122       scriptNode.appendChild(excludeNode); 
    123     } 
    124  
    125     for (var j = 0; j < scriptObj._requires.length; j++) { 
    126       var req = scriptObj._requires[j]; 
    127       var resourceNode = doc.createElement("Require"); 
    128  
    129       resourceNode.setAttribute("filename", req._filename); 
    130  
    131       scriptNode.appendChild(doc.createTextNode("\n\t\t")); 
    132       scriptNode.appendChild(resourceNode); 
    133     } 
    134  
    135     for (var j = 0; j< scriptObj._resources.length; j++) { 
    136       var imp = scriptObj._resources[j]; 
    137       var resourceNode = doc.createElement("Resource"); 
    138  
    139       resourceNode.setAttribute("name", imp._name); 
    140       resourceNode.setAttribute("filename", imp._filename); 
    141       resourceNode.setAttribute("mimetype", imp._mimetype); 
    142       if (imp._charset) { 
    143         resourceNode.setAttribute("charset", imp._charset); 
    144       } 
    145  
    146       scriptNode.appendChild(doc.createTextNode("\n\t\t")); 
    147       scriptNode.appendChild(resourceNode); 
    148     } 
    149  
    150     scriptNode.appendChild(doc.createTextNode("\n\t")); 
    151  
    152     scriptNode.setAttribute("filename", scriptObj._filename); 
    153     scriptNode.setAttribute("name", scriptObj._name); 
    154     scriptNode.setAttribute("namespace", scriptObj._namespace); 
    155     scriptNode.setAttribute("description", scriptObj._description); 
    156     scriptNode.setAttribute("enabled", scriptObj._enabled); 
    157     scriptNode.setAttribute("basedir", scriptObj._basedir); 
    158  
    159     doc.firstChild.appendChild(doc.createTextNode("\n\t")); 
    160     doc.firstChild.appendChild(scriptNode); 
    161   } 
    162  
    163   doc.firstChild.appendChild(doc.createTextNode("\n")); 
    164  
    165   var configStream = getWriteStream(this._configFile); 
    166   Components.classes["@mozilla.org/xmlextras/xmlserializer;1"] 
    167     .createInstance(Components.interfaces.nsIDOMSerializer) 
    168     .serializeToStream(doc, configStream, "utf-8"); 
    169   configStream.close(); 
    170 }; 
    171  
    172 Config.prototype.parse = function(source, uri) 
    173 { 
    174   var ioservice = Components.classes["@mozilla.org/network/io-service;1"] 
    175                             .getService(Components.interfaces.nsIIOService); 
    176  
    177   var script = new Script(this); 
    178   script._downloadUrl = uri.spec; 
    179   script._enabled = true; 
    180  
    181   // read one line at a time looking for start meta delimiter or EOF 
    182   var lines = source.match(/.+/g); 
    183   var lnIdx = 0; 
    184   var result = {}; 
    185   var foundMeta = false; 
    186  
    187   while ((result = lines[lnIdx++])) { 
    188     if (result.indexOf("// ==UserScript==") == 0) { 
    189       foundMeta = true; 
    190       break; 
    191     } 
    192   } 
    193  
    194   // gather up meta lines 
    195   if (foundMeta) { 
    196     // used for duplicate resource name detection 
    197     var previousResourceNames = {}; 
    198  
    199     while ((result = lines[lnIdx++])) { 
    200       if (result.indexOf("// ==/UserScript==") == 0) { 
    201         break; 
    202       } 
    203  
    204       var match = result.match(/\/\/ \@(\S+)\s+([^\n]+)/); 
    205       if (match != null) { 
    206         switch (match[1]) { 
    207           case "name": 
    208           case "namespace": 
    209           case "description": 
    210             script["_" + match[1]] = match[2]; 
    211             break; 
    212           case "include": 
    213             script._includes.push(match[2]); 
    214             break; 
    215           case "exclude": 
    216             script._excludes.push(match[2]); 
    217             break; 
    218           case "require": 
    219             var reqUri = ioservice.newURI(match[2], null, uri); 
    220             var scriptRequire = new ScriptRequire(script); 
    221             scriptRequire._downloadUrl = reqUri.spec; 
    222             script._requires.push(scriptRequire); 
    223             break; 
    224           case "resource": 
    225             var res = match[2].match(/(\S+)\s+(.*)/); 
    226             if (res === null) { 
    227               // NOTE: Unlocalized strings 
    228               throw new Error("Invalid syntax for @resource declaration '" + 
    229                               match[2] + "'. Resources are declared like: " + 
    230                               "@resource <name> <url>."); 
    231             } 
    232  
    233             var resName = res[1]; 
    234             if (previousResourceNames[resName]) { 
    235               throw new Error("Duplicate resource name '" + resName + "' " + 
    236                               "detected. Each resource must have a unique " + 
    237                               "name."); 
    238             } else { 
    239               previousResourceNames[resName] = true; 
    240             } 
    241  
    242             var resUri = ioservice.newURI(res[2], null, uri); 
    243             var scriptResource = new ScriptResource(script); 
    244             scriptResource._name = resName; 
    245             scriptResource._downloadUrl = resUri.spec; 
    246             script._resources.push(scriptResource); 
    247             break; 
    248         } 
    249       } 
    250     } 
    251   } 
    252  
    253   // if no meta info, default to reasonable values 
    254   if (script._name == null) { 
    255     script._name = parseScriptName(uri); 
    256   } 
    257  
    258   if (script._namespace == null) { 
    259     script._namespace = uri.host; 
    260   } 
    261  
    262   if (!script._description) 
    263     script._description = ""; 
    264  
    265   if (script._includes.length == 0) { 
    266     script._includes.push("*"); 
    267   } 
    268  
    269   return script; 
    270 } 
    271  
    272 Config.prototype.install = function(script) { 
    273   GM_log("> Config.install"); 
    274  
    275   try { 
    276     var existingIndex = this._find(script); 
    277     if (existingIndex > -1) 
    278       this.uninstall(this._scripts[existingIndex], false); 
    279  
    280     script._initFile(script._tempFile); 
    281     script._tempFile = null; 
    282  
    283     for (var i = 0; i < script._requires.length; i++) 
    284       script._requires[i]._initFile(); 
    285  
    286     for (var i = 0; i < script._resources.length; i++) 
    287       script._resources[i]._initFile(); 
    288  
    289     this._scripts.push(script); 
    290     this._changed(script, "install", null); 
    291  
    292     GM_log("< Config.install"); 
    293   } catch (e2) { 
    294     // NOTE: unlocalised string 
    295     alert("Error installing user script:\n\n" + (e2 ? e2 : "")); 
    296     throw e2; 
    297   } 
    298 }; 
    299  
    300 Config.prototype.uninstall = function(script, uninstallPrefs) 
    301 { 
    302   var idx = this._find(script); 
    303   this._scripts.splice(idx, 1); // TODO 
    304   this._changed(script, "uninstall", null); 
    305  
    306   if (script._basedir) // if script has its own dir, remove the dir + contents 
    307     script._basedirFile.remove(true); 
    308   else // if script is in the root, just remove the file 
    309     script._file.remove(false); 
    310  
    311   if (uninstallPrefs) // Remove saved preferences 
    312      GM_prefRoot.remove("scriptvals." + script._namespace + "/" + script._name + "."); 
    313 } 
    314  
    315 /** 
    316  * Moves an installed user script to a new position in the array of installed scripts. 
    317  * 
    318  * @param script The script to be moved. 
    319  * @param destination Can be either (a) a numeric offset for the script to be 
    320  *                    moved or (b) another installet script to which position 
    321  *                    the script will be moved. 
    322  */ 
    323 Config.prototype.move = function(script, destination) 
    324 { 
    325   var from = this._scripts.indexOf(script); 
    326   var to = -1; 
    327  
    328   // Make sure the user script is installed 
    329   if (from == -1) 
    330     return null; 
    331  
    332   if (typeof destination == 'number') { // if destination is an offset 
    333     to = from + destination; 
    334     to = Math.max(0, to); 
    335     to = Math.min(this._scripts.length - 1, to); 
    336   } else { // if destination is a script object 
    337     to = this._scripts.indexOf(destination); 
    338   } 
    339  
    340   if (to == -1) 
    341     return null; 
    342  
    343   var tmp = this._scripts.splice(from, 1)[0]; 
    344   this._scripts.splice(to, 0, tmp); 
    345   this._changed(script, 'move', to); 
    346 }; 
    347  
    348 Config.prototype.__defineGetter__("_scriptDir", function() { 
    349   var newDir = this._newScriptDir; 
    350   if (newDir.exists()) 
    351     return newDir; 
    352  
    353   var oldDir = this._oldScriptDir; 
    354   if (oldDir.exists()) 
    355     return oldDir; 
    356  
    357   // if we called this function, we want a script dir. 
    358   // but, at this branch, neither the old nor new exists, so create one 
    359   newDir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755); 
    360  
    361   var defaultConfigFile = getContentDir(); 
    362   defaultConfigFile.append("default-config.xml"); 
    363  
    364   defaultConfigFile.copyTo(newDir, "config.xml"); 
    365   defaultConfigFile.permissions = 0644; 
    366  
    367   return newDir; 
    368 }); 
    369  
    370 Config.prototype.__defineGetter__("_newScriptDir", function() { 
    371   var file = Components.classes["@mozilla.org/file/directory_service;1"] 
    372                        .getService(Components.interfaces.nsIProperties) 
    373                        .get("ProfD", Components.interfaces.nsILocalFile); 
    374   file.append("gm_scripts"); 
    375   return file; 
    376 }); 
    377  
    378 Config.prototype.__defineGetter__("_oldScriptDir", function() { 
    379   var file = getContentDir(); 
    380   file.append("scripts"); 
    381   return file; 
    382 }); 
    383  
    384 Config.prototype.__defineGetter__("scripts", function() { 
    385   return this._scripts.concat(); 
    386 }); 
    387  
    388 Config.prototype.getScriptsForUrl = function(url, onlyEnabled) 
    389 { 
    390   var scripts = []; 
    391  
    392   scriptLoop: 
    393   for (var i = 0, script; script = this._scripts[i]; i++) { 
    394     if (script.enabled || !onlyEnabled) { 
    395       for (var j = 0, include; include = script._includes[j]; j++) { 
    396         if (convert2RegExp(include).test(url)) { 
    397           for (var k = 0, exclude; exclude = script._excludes[k]; k++) { 
    398             if (convert2RegExp(exclude).test(url)) { 
    399               continue scriptLoop; 
    400             } 
    401           } 
    402           scripts.push(script); 
    403           continue scriptLoop; 
    404         } 
    405       } 
    406     } 
    407   } 
    408  
    409   return scripts; 
    410 }; 
    411423 
    412424function Script(config) 
     
    428440  this._requires = []; 
    429441  this._resources = []; 
    430 } 
     442}; 
    431443 
    432444Script.prototype = { 
     
    517529  get urlToDownload() { return this._downloadUrl; }, 
    518530  setDownloadedFile: function(file) { this._tempFile = file; } 
    519 } 
     531}; 
    520532 
    521533function ScriptRequire(script) 
     
    526538  this._tempFile = null; // Only for scripts not installed 
    527539  this._filename = null; 
    528 } 
     540}; 
    529541 
    530542ScriptRequire.prototype = { 
     
    560572  get urlToDownload() { return this._downloadUrl; }, 
    561573  setDownloadedFile: function(file) { this._tempFile = file; } 
    562 } 
     574}; 
    563575 
    564576function ScriptResource(script) 
     
    573585 
    574586  this._name = null; 
    575 } 
     587}; 
    576588 
    577589ScriptResource.prototype = { 
     
    613625    this._charset = charset; 
    614626  } 
    615 } 
     627}; 
  • branches/config-service/src/chrome/chromeFiles/content/versioning.js

    r661 r662  
    3333 
    3434  log("< GM_updateVersion"); 
    35 } 
     35}; 
    3636 
    3737/** 
     
    4848    scriptDir.copyTo(scriptDirBackup.parent, scriptDirBackup.leafName); 
    4949  } 
    50 } 
     50}; 
    5151 
    5252/** 
     
    6161 
    6262  log("< GM_pointFourMigrate"); 
    63 } 
     63}; 
    6464 
    6565/** 
     
    147147    log("< GM_pointThreeMigrate"); 
    148148  } 
    149 } 
     149};