Changeset 658

Show
Ignore:
Timestamp:
02/10/08 13:42:00 (10 months ago)
Author:
ma..@jesperkristensen.dk
Message:

Changes to Config.initFilename. Use nsIFile.createUnique instead of buggy code.

Files:

Legend:

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

    r657 r658  
     1// In this file protected properties (prefixed with an underscore) may be 
     2// used anywhere within this file and versioning.js 
     3 
    14function Config() { 
    25  this.onload = null; 
     
    2225  return -1; 
    2326}; 
    24  
    25 Config.prototype.initFilename = function(script) { 
    26   var index = {}; 
    27   var base = script.name.replace(/[^A-Z0-9_]/gi, "").toLowerCase(); 
    28  
    29   // If no Latin characters found - use default 
    30   if (!base) { 
    31     base = "gm_script"; 
    32   } 
    33  
    34   // 24 is a totally arbitrary max length 
    35   if (base.length > 24) { 
    36     base = base.substring(0, 24); 
    37   } 
    38  
    39   for (var i = 0; i < this.scripts.length; i++) { 
    40     index[this.scripts[i].basedir] = this.scripts[i]; 
    41   } 
    42  
    43   if (!index[base]) { 
    44     script.filename = base + ".user.js"; 
    45     script.basedir = base; 
    46     return; 
    47   } 
    48  
    49   for (var count = 1; count < Number.MAX_VALUE; count++) { 
    50     if (!index[base + count]) { 
    51       script.filename = base + ".user.js"; 
    52       script.basedir = base + "("+ count + ")"; 
    53       return; 
    54     } 
    55  
    56     if (!index[filename]) { 
    57       // Check to make sure there's no file already in that space. 
    58       var file = this.scriptDir.clone(); 
    59       file.append(filename); 
    60       if (!file.exists()) { 
    61         script.filename = filename; 
    62         return; 
    63       } 
    64     } 
    65   } 
    66   // NOTE: non localised string 
    67   throw new Error("doooooooode. get some different user script or something."); 
    68 }; 
    69  
    70 Config.prototype.initDependencyFilename = function(script, req) { 
    71   var remoteFilename = req.url.substr(req.url.lastIndexOf("/") + 1); 
    72  
    73   if(remoteFilename.indexOf("?")>0){ 
    74     remoteFilename = remoteFilename.substr(0, remoteFilename.indexOf("?")); 
    75   } 
    76  
    77   var dotIndex = remoteFilename.lastIndexOf("."); 
    78   if (dotIndex > 0) { 
    79     var base = remoteFilename.substring(0, dotIndex); 
    80     var ext = remoteFilename.substring(dotIndex+1); 
    81   } else { 
    82     var base = remoteFilename; 
    83     var ext = ""; 
    84   } 
    85  
    86   ext = ext.replace(/[^A-Z0-9_]/gi, ""); 
    87   base = base.replace(/[^A-Z0-9_]/gi, ""); 
    88  
    89   if (base.length > 24) { 
    90     base = base.substring(0, 24); 
    91   } 
    92  
    93   if (ext.length > 0){ 
    94     ext = "."+ext; 
    95   } 
    96  
    97   for (var count = 0; count < Number.MAX_VALUE; count++) { 
    98     var stamp = (count > 0) ? "(" + count + ")" : ""; 
    99     var filename = base + stamp + ext; 
    100     var file = script.basedirFile; 
    101     file.append(filename); 
    102  
    103     if (!file.exists()) { 
    104       return filename; 
    105     } 
    106   } 
    107   return undefined; 
    108 } 
    10927 
    11028Config.prototype.load = function() { 
     
    220138 
    221139  try { 
    222     // initialize a new script object 
    223     script.filename = script.tempFile.leafName; 
    224  
    225     var newDir = this.scriptDir; 
    226140    var existingIndex = this.find(script.namespace, script.name); 
    227     var existingFile = null; 
    228     var oldScripts = new Array(this.scripts); 
    229  
    230     if (existingIndex > -1) { 
    231         existingFile = this.scripts[existingIndex].basedirFile; 
    232         existingFile.normalize(); 
    233         if (existingFile.equals(this.scriptDir)) { 
    234           existingFile = this.scripts[existingIndex].file; 
    235         } 
    236         if (existingFile.exists()) { 
    237           existingFile.remove(true); 
    238         } 
    239         this.scripts.splice(existingIndex, 1); 
    240     } 
    241  
    242     this.initFilename(script); 
    243     newDir.append(script.basedir); 
    244     script.tempFile.copyTo(newDir, script.filename); 
    245  
    246     for (var i = 0; i < script.requires.length; i++) { 
    247       this.installDependency(script, script.requires[i]); 
    248     } 
    249  
    250     for (var i = 0; i < script.resources.length; i++) { 
    251       this.installDependency(script, script.resources[i]); 
    252     } 
     141    if (existingIndex > -1) 
     142      this.uninstall(this.scripts[existingIndex], false); 
     143 
     144    script._initFile(script.tempFile); 
     145    script.tempFile = null; 
     146 
     147    for (var i = 0; i < script.requires.length; i++) 
     148      script.requires[i]._initFile(); 
     149 
     150    for (var i = 0; i < script.resources.length; i++) 
     151      script.resources[i]._initFile(); 
    253152 
    254153    this.scripts.push(script); 
     
    259158    // NOTE: unlocalised string 
    260159    alert("Error installing user script:\n\n" + (e2 ? e2 : "")); 
     160    throw e2; 
    261161  } 
    262162}; 
     
    275175     GM_prefRoot.remove("scriptvals." + script.namespace + "/" + script.name + "."); 
    276176} 
    277  
    278 Config.prototype.installDependency = function(script, req){ 
    279   GM_log("Installing dependency: " + req.url  + " from " + req.tempFile.path); 
    280  
    281   var scriptDir = this.scriptDir; 
    282   GM_log("Installing to " + script.basedir); 
    283   scriptDir.append(script.basedir); 
    284  
    285   req.filename = this.initDependencyFilename(script, req); 
    286   GM_log("Installing as: " + req.filename); 
    287  
    288   try { 
    289     req.tempFile.copyTo(scriptDir, req.filename) 
    290   } catch(e) { 
    291     throw e; 
    292   } 
    293 }; 
    294177 
    295178Config.prototype.__defineGetter__("scriptDir", function() { 
     
    358241    file.append(this.basedir); 
    359242    return file; 
     243  }, 
     244 
     245  _initFileName: function(name, useExt) 
     246  { 
     247    var ext = ""; 
     248    name = name.toLowerCase(); 
     249 
     250    var dotIndex = name.lastIndexOf("."); 
     251    if (dotIndex > 0 && useExt) { 
     252      ext = name.substring(dotIndex + 1); 
     253      name = name.substring(0, dotIndex); 
     254    } 
     255 
     256    name = name.replace(/[^A-Z0-9_]/gi, ""); 
     257    ext = ext.replace(/[^A-Z0-9_]/gi, ""); 
     258 
     259    // If no Latin characters found - use default 
     260    if (!name) 
     261      name = "gm_script"; 
     262 
     263    // 24 is a totally arbitrary max length 
     264    if (name.length > 24) 
     265      name = name.substring(0, 24); 
     266 
     267    if (ext) 
     268      name += "." + ext; 
     269   
     270    return name; 
     271  }, 
     272 
     273  _initFile: function(tempFile) 
     274  { 
     275    var file = this._config.scriptDir; 
     276    var name = this._initFileName(this.name, false); 
     277 
     278    file.append(name); 
     279    file.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0755); 
     280    this.basedir = file.leafName; 
     281 
     282    file.append(name + ".user.js"); 
     283    file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0644); 
     284    this.filename = file.leafName; 
     285 
     286    GM_log("Moving script file from " + tempFile.path + " to " + file.path); 
     287 
     288    file.remove(true); 
     289    tempFile.moveTo(file.parent, file.leafName); 
    360290  } 
    361291} 
     
    375305    file.append(this.filename); 
    376306    return file; 
     307  }, 
     308 
     309  _initFile: function() 
     310  { 
     311    var name = this.url.substr(this.url.lastIndexOf("/") + 1); 
     312    if(name.indexOf("?") > 0) 
     313      name = name.substr(0, name.indexOf("?")); 
     314    name = this._script._initFileName(name, true); 
     315 
     316    var file = this._script.basedirFile; 
     317    file.append(name); 
     318    file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0644); 
     319    this.filename = file.leafName; 
     320 
     321    GM_log("Moving dependency file from " + this.tempFile.path + " to " + file.path); 
     322 
     323    file.remove(true); 
     324    this.tempFile.moveTo(file.parent, file.leafName); 
     325    this.tempFile = null; 
    377326  } 
    378327} 
     
    395344    file.append(this.filename); 
    396345    return file; 
    397   } 
    398 
     346  }, 
     347   
     348  _initFile: ScriptRequire.prototype._initFile 
     349
  • branches/config-service/src/chrome/chromeFiles/content/newscript.js

    r655 r658  
    4646  // finish making the script object ready to install 
    4747  script.tempFile = tempFile; 
    48   config.initFilename(script); 
    4948 
    5049  // install this script 
  • branches/config-service/src/chrome/chromeFiles/content/versioning.js

    r657 r658  
     1// In this file protected properties (prefixed with an underscore) may be 
     2// used anywhere within this file and config.js 
     3 
    14/** 
    25 * Checks whether the version has changed since the last run and performs 
     
    129132        var scriptFile = this.oldScriptDir; 
    130133        scriptFile.append(script.filename); 
    131         this.initFilename(script); 
    132         log("renaming script " + scriptFile.leafName + " to " + script.filename); 
    133         scriptFile.moveTo(scriptFile.parent, script.filename); 
     134        script._initFile(scriptFile); 
    134135      } 
    135136    }