Changeset 655

Show
Ignore:
Timestamp:
02/09/08 07:04:57 (10 months ago)
Author:
ma..@jesperkristensen.dk
Message:

Move file handling from utils.js to config.js and make it OO

Files:

Legend:

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

    r652 r655  
    235235  var ioSvc = Components.classes["@mozilla.org/network/io-service;1"] 
    236236                        .getService(Components.interfaces.nsIIOService); 
    237   var uri = ioSvc.newFileURI(scriptDownloader.script.file); 
     237  var uri = ioSvc.newFileURI(scriptDownloader.script.tempFile); 
    238238 
    239239  var tab = this.tabBrowser.addTab(uri.spec); 
  • branches/config-service/src/chrome/chromeFiles/content/config.js

    r548 r655  
    22  this.onload = null; 
    33  this.scripts = null; 
    4   this.configFile = getConfigFile(); 
     4  this.configFile = this.scriptDir; 
     5  this.configFile.append("config.xml"); 
    56}; 
    67 
     
    5152    if (!index[filename]) { 
    5253      // Check to make sure there's no file already in that space. 
    53       var file = getScriptDir().clone(); 
     54      var file = this.scriptDir.clone(); 
    5455      file.append(filename); 
    5556      if (!file.exists()) { 
     
    9394    var stamp = (count > 0) ? "(" + count + ")" : ""; 
    9495    var filename = base + stamp + ext; 
    95     var file = getScriptBasedir(script) 
     96    var file = script.basedirFile; 
    9697    file.append(filename); 
    9798 
     
    107108                            .createInstance(Components.interfaces.nsIDOMParser); 
    108109 
    109   var configContents = getContents(getConfigFileURI()); 
     110  var configContents = getContents(this.configFile); 
    110111  var doc = domParser.parseFromString(configContents, "text/xml"); 
    111112  var nodes = doc.evaluate("/UserScriptConfig/Script", doc, null, 0, null); 
     
    114115 
    115116  for (var node = null; (node = nodes.iterateNext()); ) { 
    116     var script = new Script(); 
     117    var script = new Script(this); 
    117118 
    118119    for (var i = 0, childNode = null; (childNode = node.childNodes[i]); i++) { 
     
    122123        script.excludes.push(childNode.firstChild.nodeValue); 
    123124      } else if (childNode.nodeName == "Require") { 
    124         script.requires.push({ filename : childNode.getAttribute("filename")}); 
     125        var scriptRequire = new ScriptRequire(script); 
     126        scriptRequire.filename = childNode.getAttribute("filename"); 
     127        script.requires.push(scriptRequire); 
    125128      } else if (childNode.nodeName == "Resource") { 
    126         script.resources.push({ name : childNode.getAttribute("name"), 
    127                                 filename : childNode.getAttribute("filename"), 
    128                                 mimetype : childNode.getAttribute("mimetype"), 
    129                                 charset  : childNode.getAttribute("charset")}); 
     129        var scriptResource = new ScriptResource(script); 
     130        scriptResource.name = childNode.getAttribute("name"); 
     131        scriptResource.filename = childNode.getAttribute("filename"); 
     132        scriptResource.mimetype = childNode.getAttribute("mimetype"); 
     133        scriptResource.charset  = childNode.getAttribute("charset"); 
     134        script.resources.push(scriptResource); 
    130135      } 
    131136    } 
     
    212217  try { 
    213218    // initialize a new script object 
    214     script.filename = script.file.leafName; 
    215  
    216     var newDir = getScriptDir()
     219    script.filename = script.tempFile.leafName; 
     220 
     221    var newDir = this.scriptDir
    217222    var existingIndex = this.find(script.namespace, script.name); 
    218223    var existingFile = null; 
     
    220225 
    221226    if (existingIndex > -1) { 
    222         existingFile = getScriptBasedir(this.scripts[existingIndex])
     227        existingFile = this.scripts[existingIndex].basedirFile
    223228        existingFile.normalize(); 
    224         if (existingFile.equals(getScriptDir())) { 
    225           existingFile = getScriptFile(this.scripts[existingIndex])
     229        if (existingFile.equals(this.scriptDir)) { 
     230          existingFile = this.scripts[existingIndex].file
    226231        } 
    227232        if (existingFile.exists()) { 
     
    233238    this.initFilename(script); 
    234239    newDir.append(script.basedir); 
    235     script.file.copyTo(newDir, script.filename); 
     240    script.tempFile.copyTo(newDir, script.filename); 
    236241 
    237242    for (var i = 0; i < script.requires.length; i++) { 
     
    254259 
    255260Config.prototype.installDependency = function(script, req){ 
    256   GM_log("Installing dependency: " + req.url  + " from " + req.file.path); 
    257  
    258   var scriptDir = getScriptDir()
     261  GM_log("Installing dependency: " + req.url  + " from " + req.tempFile.path); 
     262 
     263  var scriptDir = this.scriptDir
    259264  GM_log("Installing to " + script.basedir); 
    260265  scriptDir.append(script.basedir); 
     
    264269 
    265270  try { 
    266     req.file.copyTo(scriptDir, req.filename) 
     271    req.tempFile.copyTo(scriptDir, req.filename) 
    267272  } catch(e) { 
    268273    throw e; 
     
    270275}; 
    271276 
    272 function Script() { 
     277Config.prototype.__defineGetter__("scriptDir", function() { 
     278  var dir = this.newScriptDir; 
     279 
     280  if (dir.exists()) { 
     281    return dir; 
     282  } else { 
     283    var oldDir = this.oldScriptDir; 
     284    if (oldDir.exists()) { 
     285      return oldDir; 
     286    } else { 
     287      // if we called this function, we want a script dir. 
     288      // but, at this branch, neither the old nor new exists, so create one 
     289      return GM_createScriptsDir(dir); 
     290    } 
     291  } 
     292}); 
     293 
     294Config.prototype.__defineGetter__("newScriptDir", function() { 
     295  var file = Components.classes["@mozilla.org/file/directory_service;1"] 
     296                       .getService(Components.interfaces.nsIProperties) 
     297                       .get("ProfD", Components.interfaces.nsILocalFile); 
     298  file.append("gm_scripts"); 
     299  return file; 
     300}); 
     301 
     302Config.prototype.__defineGetter__("oldScriptDir", function() { 
     303  var file = getContentDir(); 
     304  file.append("scripts"); 
     305  return file; 
     306}); 
     307 
     308function Script(config) 
     309
     310  this._config = config; 
     311  this.tempFile = null; // Only for scripts not installed 
    273312  this.filename = null; 
    274313  this.name = null; 
     
    281320  this.requires = []; 
    282321  this.resources = []; 
    283 }; 
    284  
    285 function ScriptDependency(){ 
    286   this.url = null 
    287   this.file = null; 
     322
     323 
     324Script.prototype = { 
     325  get file() 
     326  { 
     327    var file = this.basedirFile; 
     328    file.append(this.filename); 
     329    return file; 
     330  }, 
     331 
     332  get basedirFile() 
     333  { 
     334    var file = this._config.scriptDir; 
     335    file.append(this.basedir); 
     336    return file; 
     337  } 
     338
     339 
     340function ScriptRequire(script) 
     341
     342  this._script = script; 
     343  this.url = null; // Only for scripts not installed 
     344  this.tempFile = null; // Only for scripts not installed 
    288345  this.filename = null; 
    289 }; 
    290  
    291 function ScriptResource(){ 
    292   this.url = null; 
     346
     347 
     348ScriptRequire.prototype = { 
     349  get file() 
     350  { 
     351    var file = this._script.basedirFile; 
     352    file.append(this.filename); 
     353    return file; 
     354  } 
     355
     356 
     357function ScriptResource(script) 
     358
     359  this._script = script; 
     360  this.url = null; // Only for scripts not installed 
     361  this.tempFile = null; // Only for scripts not installed 
    293362  this.name = null; 
    294   this.file = null; 
    295363  this.filename = null; 
    296364  this.mimetype = null; 
    297 }; 
     365  this.charset = null; 
     366
     367 
     368ScriptResource.prototype = { 
     369  get file() 
     370  { 
     371    var file = this._script.basedirFile; 
     372    file.append(this.filename); 
     373    return file; 
     374  } 
     375
  • branches/config-service/src/chrome/chromeFiles/content/manage.js

    r652 r655  
    2121  var chkUninstallPrefs = document.getElementById('chkUninstallPrefs'); 
    2222  for (var i = 0, script = null; (script = uninstallList[i]); i++) { 
    23     file = getScriptBasedir(script)
     23    file = script.basedirFile
    2424    file.normalize(); 
    25     if (!file.equals(getScriptDir())) { 
     25    if (!file.equals(config.scriptDir)) { 
    2626      if (file.exists()) { 
    2727        file.remove(true); // file==base directory recursive delete 
    2828      } 
    2929    } else { 
    30       file = getScriptFile(script)
     30      file = script.file
    3131      if (file.exists()) { 
    3232        file.remove(false); 
  • branches/config-service/src/chrome/chromeFiles/content/miscapis.js

    r645 r655  
    4040 
    4141  var window = appSvc.hiddenDOMWindow; 
    42   var binaryContents = getBinaryContents(getDependencyFileURI(this.script, dep)); 
     42  var binaryContents = getBinaryContents(dep.file); 
    4343 
    4444  var mimetype = dep.mimetype; 
     
    5757 
    5858  var dep = this.getDep_(name); 
    59   return getContents(getDependencyFileURI(this.script, dep)); 
     59  return getContents(dep.file); 
    6060}; 
    6161 
  • branches/config-service/src/chrome/chromeFiles/content/newscript.js

    r652 r655  
    4545 
    4646  // finish making the script object ready to install 
    47   script.file = tempFile; 
     47  script.tempFile = tempFile; 
    4848  config.initFilename(script); 
    4949 
  • branches/config-service/src/chrome/chromeFiles/content/scriptdownloader.js

    r609 r655  
    7272    ws.close(); 
    7373 
    74     this.script.file = file; 
     74    this.script.tempFile = file; 
    7575 
    7676    window.setTimeout(GM_hitch(this, "fetchDependencies"), 0); 
     
    116116      var ioservice = 
    117117        Components.classes["@mozilla.org/network/io-service;1"] 
    118         .getService(); 
     118        .getService(Components.interfaces.nsIIOService); 
    119119      var sourceUri = ioservice.newURI(dep.url, null, null); 
    120120      var sourceChannel = ioservice.newChannelFromURI(sourceUri); 
     
    151151  if (httpChannel) { 
    152152    if (httpChannel.requestSucceeded) { 
    153       dep.file = file; 
     153      dep.tempFile = file; 
    154154      dep.mimetype= channel.contentType; 
    155155      if (channel.contentCharset) { 
     
    163163    } 
    164164  } else { 
    165     dep.file = file; 
     165    dep.tempFile = file; 
    166166    this.downloadNextDependency(); 
    167167  } 
     
    228228ScriptDownloader.prototype.parseScript = function(source, uri) { 
    229229  var ioservice = Components.classes["@mozilla.org/network/io-service;1"] 
    230                             .getService(); 
    231  
    232   var script = new Script(); 
    233   script.uri = uri
     230                            .getService(Components.interfaces.nsIIOService); 
     231 
     232  var config = new Config(); // This is fragile, but temporary until config becomes a service 
     233  var script = new Script(config)
    234234  script.enabled = true; 
    235235  script.includes = []; 
     
    273273          case "require": 
    274274            var reqUri = ioservice.newURI(match[2], null, uri); 
    275             var scriptDependency = new ScriptDependency(); 
    276             scriptDependency.url = reqUri.spec; 
    277             script.requires.push(scriptDependency); 
     275            var scriptRequire = new ScriptRequire(script); 
     276            scriptRequire.url = reqUri.spec; 
     277            script.requires.push(scriptRequire); 
    278278            break; 
    279279          case "resource": 
     
    296296 
    297297            var resUri = ioservice.newURI(res[2], null, uri); 
    298             var scriptResource = new ScriptResource(); 
     298            var scriptResource = new ScriptResource(script); 
    299299            scriptResource.name = resName; 
    300300            scriptResource.url = resUri.spec; 
  • branches/config-service/src/chrome/chromeFiles/content/utils.js

    r652 r655  
    7474 
    7575function openInEditor(script) { 
    76   var file = getScriptFile(script)
     76  var file = script.file
    7777  var stringBundle = Components 
    7878    .classes["@mozilla.org/intl/stringbundle;1"] 
     
    191191}; 
    192192 
    193 function getBinaryContents(url){ 
     193function getBinaryContents(file){ 
    194194    var ioService = Components.classes["@mozilla.org/network/io-service;1"] 
    195195                              .getService(Components.interfaces.nsIIOService); 
    196196 
    197     var channel = ioService.newChannelFromURI(url); 
     197    var channel = ioService.newChannelFromURI(GM_getUriFromFile(file)); 
    198198    var input = channel.open(); 
    199199 
     
    207207}; 
    208208 
    209 function getContents(aURL, charset){ 
     209function getContents(file, charset){ 
    210210  if( !charset ) { 
    211211    charset = "UTF-8" 
     
    222222  unicodeConverter.charset = charset; 
    223223 
    224   var channel=ioService.newChannelFromURI(aURL); 
     224  var channel=ioService.newChannelFromURI(GM_getUriFromFile(file)); 
    225225  var input=channel.open(); 
    226226  scriptableStream.init(input); 
     
    245245}; 
    246246 
    247 function getConfigFile(){ 
    248   var file = getScriptDir(); 
    249   file.append("config.xml"); 
    250   return file; 
    251 }; 
    252  
    253 function getConfigFileURI(){ 
     247function GM_getUriFromFile(file) 
     248
    254249  return Components.classes["@mozilla.org/network/io-service;1"] 
    255250                   .getService(Components.interfaces.nsIIOService) 
    256                    .newFileURI(getConfigFile()); 
    257 }; 
    258  
    259 function getDependencyFileURI(script, dep){ 
    260   return Components.classes["@mozilla.org/network/io-service;1"] 
    261                    .getService(Components.interfaces.nsIIOService) 
    262                    .newFileURI(getDependencyFile(script, dep)); 
    263 }; 
    264  
    265 function getDependencyFile(script, dep){ 
    266   var file = getScriptDir(); 
    267   file.append(script.basedir); 
    268   file.append(dep.filename); 
    269   return file; 
    270 }; 
    271  
    272 function getScriptFileURI(script) { 
    273   return Components.classes["@mozilla.org/network/io-service;1"] 
    274                    .getService(Components.interfaces.nsIIOService) 
    275                    .newFileURI(getScriptFile(script)); 
    276 }; 
    277  
    278 function getScriptBasedir(script) { 
    279   var file = getScriptDir(); 
    280   file.append(script.basedir); 
    281   return file; 
    282 }; 
    283  
    284 function getScriptFile(script) { 
    285   var file = getScriptDir(); 
    286   file.append(script.basedir); 
    287   file.append(script.filename); 
    288   return file; 
    289 }; 
    290  
    291 function getScriptDir() { 
    292   var dir = getNewScriptDir(); 
    293  
    294   if (dir.exists()) { 
    295     return dir; 
    296   } else { 
    297     var oldDir = getOldScriptDir(); 
    298     if (oldDir.exists()) { 
    299       return oldDir; 
    300     } else { 
    301       // if we called this function, we want a script dir. 
    302       // but, at this branch, neither the old nor new exists, so create one 
    303       return GM_createScriptsDir(dir); 
    304     } 
    305   } 
    306 }; 
    307  
    308 function getNewScriptDir() { 
    309   var file = Components.classes["@mozilla.org/file/directory_service;1"] 
    310                        .getService(Components.interfaces.nsIProperties) 
    311                        .get("ProfD", Components.interfaces.nsILocalFile); 
    312   file.append("gm_scripts"); 
    313   return file; 
    314 }; 
    315  
    316 function getOldScriptDir() { 
    317   var file = getContentDir(); 
    318   file.append("scripts"); 
    319   return file; 
    320 }; 
     251                   .newFileURI(file); 
     252
    321253 
    322254function getContentDir() { 
  • branches/config-service/src/chrome/chromeFiles/content/versioning.js

    r627 r655  
    3737 */ 
    3838function GM_pointEightBackup() { 
    39   var scriptDir = getNewScriptDir(); 
     39  var config = new Config(); 
     40  var scriptDir = config.newScriptDir; 
    4041  var scriptDirBackup = scriptDir.clone(); 
    4142  scriptDirBackup.leafName += "_08bak"; 
     
    5152  log("> GM_pointFourMigrate"); 
    5253 
    53   var oldDir = getOldScriptDir(); 
    54   var newDir = getNewScriptDir(); 
     54  var config = new Config(); 
     55  var oldDir = config.oldScriptDir; 
     56  var newDir = config.newScriptDir; 
    5557 
    5658  if (!oldDir.exists() && !newDir.exists()) { 
     
    136138 
    137139    // now, load config normally and reinitialize all scripts's filenames 
    138     var config = new Config(GM_getPointThreeScriptFile("config.xml")); 
     140    var config = new Config(); 
    139141    config.load(); 
    140142 
  • branches/config-service/src/components/greasemonkey.js

    r651 r655  
    312312      sandbox.__proto__ = safeWin; 
    313313 
    314       var contents = getContents(getScriptFileURI(script)) 
     314      var contents = getContents(script.file); 
    315315 
    316316      var requires = []; 
     
    319319 
    320320      script.requires.forEach(function(req){ 
    321         var uri = getDependencyFileURI(script, req); 
    322         var contents = getContents(uri); 
     321        var contents = getContents(req.file); 
    323322        var lineCount = contents.split("\n").length; 
    324         requires.push(getContents(uri)); 
     323        requires.push(contents); 
    325324        offset += lineCount; 
    326325        offsets.push(offset); 
     
    398397            e, // error obj 
    399398            0, // 0 = error (1 = warning) 
    400             getScriptFileURI(script).spec, 
     399            GM_getUriFromFile(script.file).spec, 
    401400            0 
    402401          ); 
     
    414413      if (lineNumber < end) { 
    415414        return { 
    416           uri: getDependencyFileURI(script, script.requires[i]).spec, 
     415          uri: GM_getUriFromFile(script.requires[i].file).spec, 
    417416          lineNumber: (lineNumber - start) 
    418417        }; 
     
    422421 
    423422    return { 
    424       uri: getScriptFileURI(script).spec, 
     423      uri: GM_getUriFromFile(script.file).spec, 
    425424      lineNumber: (lineNumber - end) 
    426425    };