toolkit/mozapps/downloads/content/helperApps.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/downloads/content/helperApps.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,727 @@
     1.4 +// -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 +
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +var Cc = Components.classes;
    1.11 +var Ci = Components.interfaces;
    1.12 +
    1.13 +var gRDF;    
    1.14 +
    1.15 +const CLASS_MIMEINFO        = "mimetype";
    1.16 +const CLASS_PROTOCOLINFO    = "scheme";
    1.17 +
    1.18 +// namespace prefix
    1.19 +const NC_NS                 = "http://home.netscape.com/NC-rdf#";
    1.20 +
    1.21 +// type list properties
    1.22 +
    1.23 +const NC_MIME_TYPES         = NC_NS + "MIME-types";
    1.24 +const NC_PROTOCOL_SCHEMES   = NC_NS + "Protocol-Schemes";
    1.25 +
    1.26 +///////////////////////////////////////////////////////////////////////////////
    1.27 +// MIME Types DataSource Wrapper
    1.28 +
    1.29 +function NC_URI(aProperty)
    1.30 +{
    1.31 +  return "http://home.netscape.com/NC-rdf#" + aProperty;
    1.32 +}
    1.33 +
    1.34 +function MIME_URI(aType)
    1.35 +{
    1.36 +  return "urn:mimetype:" + aType;
    1.37 +}
    1.38 +
    1.39 +function HANDLER_URI(aHandler)
    1.40 +{
    1.41 +  return "urn:mimetype:handler:" + aHandler;
    1.42 +}
    1.43 +
    1.44 +function APP_URI(aType)
    1.45 +{
    1.46 +  return "urn:mimetype:externalApplication:" + aType;
    1.47 +}
    1.48 +
    1.49 +function ArrayEnumerator(aItems) {
    1.50 +  if (aItems) {
    1.51 +    for (var i = 0; i < aItems.length; ++i) {
    1.52 +      if (!aItems[i])
    1.53 +        aItems.splice(i--, 1);
    1.54 +    }
    1.55 +    this._contents = aItems;
    1.56 +  } else {
    1.57 +    this._contents = [];
    1.58 +  }
    1.59 +}
    1.60 +
    1.61 +ArrayEnumerator.prototype = {
    1.62 +  _index: 0,
    1.63 +
    1.64 +  hasMoreElements: function () {
    1.65 +    return this._index < this._contents.length;
    1.66 +  },
    1.67 +
    1.68 +  getNext: function () {
    1.69 +    return this._contents[this._index++];
    1.70 +  },
    1.71 +
    1.72 +  push: function (aElement) {
    1.73 +    if (aElement)
    1.74 +      this._contents.push(aElement);
    1.75 +  }
    1.76 +};
    1.77 +
    1.78 +function HelperApps()
    1.79 +{
    1.80 +  if (!gRDF) 
    1.81 +    gRDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
    1.82 +  
    1.83 +  const mimeTypes = "UMimTyp";
    1.84 +  var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
    1.85 +  
    1.86 +  var file = fileLocator.get(mimeTypes, Components.interfaces.nsIFile);
    1.87 +
    1.88 +  var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
    1.89 +  var fileHandler = ioService.getProtocolHandler("file").QueryInterface(Components.interfaces.nsIFileProtocolHandler);
    1.90 +  this._inner = gRDF.GetDataSourceBlocking(fileHandler.getURLSpecFromFile(file));
    1.91 +  this._inner.AddObserver(this);
    1.92 +
    1.93 +  this._fileTypeArc       = gRDF.GetResource(NC_URI("FileType"));
    1.94 +  this._fileHandlerArc    = gRDF.GetResource(NC_URI("FileHandler"));
    1.95 +  this._fileIconArc       = gRDF.GetResource(NC_URI("FileIcon"));
    1.96 +  this._fileExtensionArc  = gRDF.GetResource(NC_URI("FileExtension"));
    1.97 +  this._fileExtensionsArc = gRDF.GetResource(NC_URI("FileExtensions"));
    1.98 +  this._handleAutoArc     = gRDF.GetResource(NC_URI("FileHandleAuto"));
    1.99 +  this._valueArc          = gRDF.GetResource(NC_URI("value"));
   1.100 +  this._handlerPropArc    = gRDF.GetResource(NC_URI("handlerProp"));
   1.101 +  this._externalAppArc    = gRDF.GetResource(NC_URI("externalApplication"));
   1.102 +}
   1.103 +
   1.104 +HelperApps.prototype = {
   1.105 +  mimeHandlerExists: function (aMIMEType)
   1.106 +  {
   1.107 +    var valueProperty = gRDF.GetUnicodeResource(NC_URI("value"));
   1.108 +    var mimeSource = gRDF.GetUnicodeResource(MIME_URI(aMIMEType));
   1.109 +    var mimeLiteral = gRDF.GetLiteral(aMIMEType);
   1.110 +    return this._inner.HasAssertion(mimeSource, valueProperty, mimeLiteral, true);
   1.111 +  },
   1.112 +
   1.113 +  updateTypeInfo: function (aMIMEInfo) 
   1.114 +  {
   1.115 +    var mimeType = aMIMEInfo.MIMEType;
   1.116 +    var isNewMIMEType = this.mimeHandlerExists(mimeType);
   1.117 +    var entry = new HandlerOverride(MIME_URI(mimeType), this._inner);
   1.118 +    entry.mimeType    = mimeType;
   1.119 +    entry.isEditable  = true;
   1.120 +    entry.alwaysAsk = aMIMEInfo.alwaysAskBeforeHandling;
   1.121 +    
   1.122 +    // If not updating (i.e., a newly encountered mime type),
   1.123 +    // then update extension list and description.
   1.124 +    if (!isNewMIMEType) {
   1.125 +      var extEnumerator = aMIMEInfo.getFileExtensions();
   1.126 +      while (extEnumerator.hasMore()) {
   1.127 +          entry.addExtension(extEnumerator.getNext());
   1.128 +      }
   1.129 +      entry.description = aMIMEInfo.description;
   1.130 +      entry.appDisplayName = "";
   1.131 +    }
   1.132 +    
   1.133 +    const nsIMIMEInfo = Components.interfaces.nsIMIMEInfo;
   1.134 +    if (aMIMEInfo.preferredAction == nsIMIMEInfo.saveToDisk) {
   1.135 +      entry.saveToDisk = true;
   1.136 +      if (!isNewMIMEType) {
   1.137 +        // Creating a new entry, set path.
   1.138 +        entry.appPath = "";
   1.139 +      }
   1.140 +    } 
   1.141 +    else if (aMIMEInfo.preferredAction == nsIMIMEInfo.useSystemDefault ||
   1.142 +             aMIMEInfo.preferredApplicationHandler == null) {
   1.143 +      entry.useSystemDefault = true;
   1.144 +      if (!isNewMIMEType) {
   1.145 +        // Creating a new entry, set path.
   1.146 +        entry.appPath = "";
   1.147 +      }
   1.148 +    } 
   1.149 +    else if (aMIMEInfo.preferredApplicationHandler instanceof Components.interfaces.nsILocalHandlerApp) {
   1.150 +      entry.saveToDisk       = false;
   1.151 +      entry.useSystemDefault = false;
   1.152 +      entry.handleInternal   = false;
   1.153 +      entry.appPath = aMIMEInfo.preferredApplicationHandler.executable.path;
   1.154 +      entry.appDisplayName = aMIMEInfo.preferredApplicationHandler.name;
   1.155 +    }
   1.156 +    
   1.157 +    // Do RDF magic.
   1.158 +    entry.buildLinks();
   1.159 +    this.flush();
   1.160 +  },
   1.161 +
   1.162 +  getLiteralValue: function (aResource, aProperty)
   1.163 +  {
   1.164 +    var res = gRDF.GetResource(aResource);
   1.165 +    var prop = gRDF.GetResource(NC_URI(aProperty));
   1.166 +    var val = this.GetTarget(res, prop, true);
   1.167 +    if (val) {
   1.168 +      val = val.QueryInterface(Components.interfaces.nsIRDFLiteral);
   1.169 +      return val.Value;
   1.170 +    }
   1.171 +    return "";
   1.172 +  },
   1.173 +
   1.174 +  /* nsIRDFDataSource */
   1.175 +  get URI() {
   1.176 +    return this._inner.URI;
   1.177 +  },
   1.178 +  
   1.179 +  GetSource: function (aProperty, aTarget, aTruthValue) {
   1.180 +    return this._inner.GetSource(aProperty, aTarget, aTruthValue);
   1.181 +  },
   1.182 +  GetSources: function (aProperty, aTarget, aTruthValue) {
   1.183 +    return this._inner.GetSources(aProperty, aTarget, aTruthValue);
   1.184 +  },         
   1.185 +  
   1.186 +  _isRootTypeResource: function (aResource) {
   1.187 +    aResource = aResource.QueryInterface(Components.interfaces.nsIRDFResource);  
   1.188 +    const kRootTypePrefix = "urn:mimetype:";
   1.189 +    return (aResource.Value.substr(0, kRootTypePrefix.length) == kRootTypePrefix);
   1.190 +  },
   1.191 +  
   1.192 +  getMIMEInfo: function (aResource) {
   1.193 +    var types = this._inner.GetTarget(aResource, this._valueArc, true);
   1.194 +    if (types) {
   1.195 +      types = types.QueryInterface(Components.interfaces.nsIRDFLiteral);
   1.196 +      types = types.Value.split(", ");
   1.197 +
   1.198 +      mimeSvc = Components.classes["@mozilla.org/mime;1"].getService(Components.interfaces.nsIMIMEService);
   1.199 +      return mimeSvc.getFromTypeAndExtension(types[0], null);
   1.200 +    }
   1.201 +    
   1.202 +    return null;
   1.203 +  },
   1.204 +   
   1.205 +  GetTarget: function (aSource, aProperty, aTruthValue) {
   1.206 +    if (this._isRootTypeResource(aSource)) {
   1.207 +      var typeInfo = this.getMIMEInfo(aSource);
   1.208 +      if (typeInfo) {
   1.209 +        var bundle = document.getElementById("strings");
   1.210 +        if (aProperty.EqualsNode(this._handleAutoArc)) {
   1.211 +          var handler = this.GetTarget(aSource, this._handlerPropArc, true);
   1.212 +          if (handler) {
   1.213 +            handler = handler.QueryInterface(Components.interfaces.nsIRDFResource);
   1.214 +            return gRDF.GetLiteral(!(this.getLiteralValue(handler.Value, "alwaysAsk") == "true"));
   1.215 +          }
   1.216 +        }
   1.217 +        else if (aProperty.EqualsNode(this._fileTypeArc)) {
   1.218 +          if (typeInfo.description == "") {
   1.219 +            try {
   1.220 +              var literal = bundle.getFormattedString("fileEnding", [typeInfo.primaryExtension.toUpperCase()]);
   1.221 +              return gRDF.GetLiteral(literal);
   1.222 +            }
   1.223 +            catch (e) { 
   1.224 +              // Wow, this sucks, just show the MIME type as a last ditch effort to display
   1.225 +              // the type of file that this is. 
   1.226 +              return gRDF.GetLiteral(typeInfo.MIMEType);
   1.227 +            }
   1.228 +          }
   1.229 +          return gRDF.GetLiteral(typeInfo.description);
   1.230 +        }
   1.231 +        else if (aProperty.EqualsNode(this._fileHandlerArc)) {
   1.232 +          var handler = this.GetTarget(aSource, this._handlerPropArc, true);
   1.233 +          if (handler) {
   1.234 +            handler = handler.QueryInterface(Components.interfaces.nsIRDFResource);
   1.235 +            if (this.getLiteralValue(handler.Value, "saveToDisk") == "true") {
   1.236 +              var saveToDisk = bundle.getString("saveToDisk");
   1.237 +              return gRDF.GetLiteral(saveToDisk);
   1.238 +            }
   1.239 +            else if (this.getLiteralValue(handler.Value, "useSystemDefault") == "false") {
   1.240 +              var extApp = this.GetTarget(handler, this._externalAppArc, true);
   1.241 +              if (extApp) {
   1.242 +                extApp = extApp.QueryInterface(Components.interfaces.nsIRDFResource);
   1.243 +                var openWith = bundle.getFormattedString("openWith", [this.getLiteralValue(extApp.Value, "prettyName")]);
   1.244 +                return gRDF.GetLiteral(openWith);
   1.245 +              }
   1.246 +            }
   1.247 +          }     
   1.248 +          
   1.249 +          var openWith2 = bundle.getFormattedString("openWith", [typeInfo.defaultDescription]);
   1.250 +          return gRDF.GetLiteral(openWith2);
   1.251 +        }
   1.252 +        else if (aProperty.EqualsNode(this._fileIconArc)) {
   1.253 +          try {
   1.254 +            return gRDF.GetLiteral("moz-icon://goat." + typeInfo.primaryExtension + "?size=16");
   1.255 +          }
   1.256 +          catch (e) {
   1.257 +            return gRDF.GetLiteral("moz-icon://goat?size=16&contentType=" + typeInfo.MIMEType);
   1.258 +          }
   1.259 +        }
   1.260 +        else if (aProperty.EqualsNode(this._fileExtensionArc)) {
   1.261 +          try {
   1.262 +            return gRDF.GetLiteral(typeInfo.primaryExtension.toUpperCase());
   1.263 +          }
   1.264 +          catch (e) { }
   1.265 +          return gRDF.GetLiteral("");
   1.266 +        }
   1.267 +        else if (aProperty.EqualsNode(this._fileExtensionsArc)) {
   1.268 +          var extns = typeInfo.getFileExtensions();
   1.269 +          
   1.270 +          // Prevent duplicates.
   1.271 +          var hash = { };
   1.272 +          while (extns.hasMore())
   1.273 +            hash[extns.getNext().toUpperCase()] = 0;
   1.274 +          
   1.275 +          var str = "";
   1.276 +          for (var extn in hash)
   1.277 +            str += extn + ",";
   1.278 +          str = str.substring(0, str.length - 1);
   1.279 +
   1.280 +          return gRDF.GetLiteral(str);
   1.281 +        }
   1.282 +      }
   1.283 +    }
   1.284 +
   1.285 +    return this._inner.GetTarget(aSource, aProperty, aTruthValue);
   1.286 +  },      
   1.287 +  
   1.288 +  GetTargets: function (aSource, aProperty, aTruthValue) {
   1.289 +    if (this._isRootTypeResource(aSource)) { 
   1.290 +      return new ArrayEnumerator([this.GetTarget(aSource, aProperty, aTruthValue)]);
   1.291 +    }
   1.292 +    
   1.293 +    return this._inner.GetTargets(aSource, aProperty, aTruthValue);
   1.294 +  }, 
   1.295 +  Assert: function (aSource, aProperty, aTarget, aTruthValue) {
   1.296 +    return this._inner.Assert(aSource, aProperty, aTarget, aTruthValue);
   1.297 +  },
   1.298 +  Unassert: function (aSource, aProperty, aTarget) {
   1.299 +    return this._inner.Unassert(aSource, aProperty, aTarget);
   1.300 +  },
   1.301 +  Change: function (aSource, aProperty, aOldTarget, aNewTarget) {
   1.302 +    if (aOldTarget)
   1.303 +      var ot = aOldTarget.QueryInterface(Components.interfaces.nsIRDFLiteral);
   1.304 +    if (aNewTarget)
   1.305 +      var nt = aNewTarget.QueryInterface(Components.interfaces.nsIRDFLiteral);
   1.306 +
   1.307 +    return this._inner.Change(aSource, aProperty, aOldTarget, aNewTarget);
   1.308 +  },
   1.309 +  Move: function (aOldSource, aNewSource, aProperty, aTarget) {
   1.310 +    return this._inner.Assert(aOldSource, aNewSource, aProperty, aTarget);
   1.311 +  },
   1.312 +  HasAssertion: function (aSource, aProperty, aTarget, aTruthValue) {
   1.313 +    if (this._isRootTypeResource(aSource)) {
   1.314 +      // Don't show entries in the list for types that we DO NOT handle
   1.315 +      // automatically. i.e. this list is a means of editing and removing
   1.316 +      // automatic overrides only. 
   1.317 +      if (aProperty.EqualsNode(this._handleAutoArc)) {
   1.318 +        var handler = this.GetTarget(aSource, this._handlerPropArc, true);
   1.319 +        if (handler) {
   1.320 +          handler = handler.QueryInterface(Components.interfaces.nsIRDFResource);
   1.321 +          return !(this.getLiteralValue(handler.Value, "alwaysAsk") == "true");
   1.322 +        }
   1.323 +      }
   1.324 +    }
   1.325 +    return this._inner.HasAssertion(aSource, aProperty, aTarget, aTruthValue);
   1.326 +  },
   1.327 +  ArcLabelsIn: function (aNode) {
   1.328 +    return this._inner.ArcLabelsIn(aNode);
   1.329 +  },
   1.330 +  ArcLabelsOut: function (aNode) {
   1.331 +    return this._inner.ArcLabelsOut(aNode);
   1.332 +  },
   1.333 +  GetAllResources: function () {
   1.334 +    return this._inner.GetAllResources();
   1.335 +  },
   1.336 +  hasArcIn: function (aNode, aArc) {
   1.337 +    return this._inner.hasArcIn(aNode, aArc);
   1.338 +  },
   1.339 +  hasArcOut: function (aNode, aArc) {
   1.340 +    return this._inner.hasArcOut(aNode, aArc);
   1.341 +  },
   1.342 +  
   1.343 +  _observers: [],
   1.344 +  AddObserver: function (aObserver) {
   1.345 +    this._observers.push(aObserver);
   1.346 +  },
   1.347 +  
   1.348 +  RemoveObserver: function (aObserver) {
   1.349 +    for (var i = 0; i < this._observers.length; ++i) {
   1.350 +      if (this._observers[i] == aObserver) {
   1.351 +        this._observers.splice(i, 1);
   1.352 +        break;
   1.353 +      }
   1.354 +    }
   1.355 +  },
   1.356 +  
   1.357 +  onAssert: function (aDataSource, aSource, aProperty, aTarget) {
   1.358 +    for (var i = 0; i < this._observers.length; ++i) {
   1.359 +      this._observers[i].onAssert(aDataSource, aSource, aProperty, aTarget);
   1.360 +    }
   1.361 +  },
   1.362 +
   1.363 +  onUnassert: function (aDataSource, aSource, aProperty, aTarget) {
   1.364 +    for (var i = 0; i < this._observers.length; ++i) {
   1.365 +      this._observers[i].onUnassert(aDataSource, aSource, aProperty, aTarget);
   1.366 +    }
   1.367 +  },
   1.368 +  
   1.369 +  onChange: function (aDataSource, aSource, aProperty, aOldTarget, aNewTarget) {
   1.370 +    for (var i = 0; i < this._observers.length; ++i) {
   1.371 +      this._observers[i].onChange(aDataSource, aSource, aProperty, aOldTarget, aNewTarget);
   1.372 +    }
   1.373 +  },
   1.374 +  
   1.375 +  onMove: function (aDataSource, aOldSource, aNewSource, aProperty, aTarget) {
   1.376 +    for (var i = 0; i < this._observers.length; ++i) {
   1.377 +      this._observers[i].onMove(aDataSource, aOldSource, aNewSource, aProperty, aTarget);
   1.378 +    }
   1.379 +  },
   1.380 +  
   1.381 +  onBeginUpdateBatch: function (aDataSource) {
   1.382 +    for (var i = 0; i < this._observers.length; ++i) {
   1.383 +      this._observers[i].onBeginUpdateBatch(aDataSource);
   1.384 +    }
   1.385 +  },
   1.386 +  
   1.387 +  onEndUpdateBatch: function (aDataSource) {
   1.388 +    for (var i = 0; i < this._observers.length; ++i) {
   1.389 +      this._observers[i].onEndUpdateBatch(aDataSource);
   1.390 +    }
   1.391 +  },
   1.392 +  
   1.393 +  beginUpdateBatch: function (aDataSource) {
   1.394 +    for (var i = 0; i < this._observers.length; ++i) {
   1.395 +      this._observers[i].beginUpdateBatch(aDataSource);
   1.396 +    }
   1.397 +  },
   1.398 +  
   1.399 +  endUpdateBatch: function (aDataSource) {
   1.400 +    for (var i = 0; i < this._observers.length; ++i) {
   1.401 +      this._observers[i].endUpdateBatch(aDataSource);
   1.402 +    }
   1.403 +  },
   1.404 +
   1.405 +  flush: function () {
   1.406 +    var rds = this._inner.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
   1.407 +    if (rds) 
   1.408 +      rds.Flush();
   1.409 +  },
   1.410 +  
   1.411 +  destroy: function () {
   1.412 +    this._inner.RemoveObserver(this);
   1.413 +  }
   1.414 +};
   1.415 +
   1.416 +/**
   1.417 + * Handler Override class
   1.418 + **/
   1.419 +function HandlerOverride(aURI, aDatasource)
   1.420 +{
   1.421 +  this.URI = aURI;
   1.422 +  this._DS = aDatasource;
   1.423 +}
   1.424 +
   1.425 +HandlerOverride.prototype = {
   1.426 +  // general information
   1.427 +  get mimeType()
   1.428 +  {
   1.429 +    return this.getLiteralForContentType(this.URI, "value");
   1.430 +  },
   1.431 +  
   1.432 +  set mimeType(aMIMETypeString)
   1.433 +  {
   1.434 +    this.changeMIMEStuff(MIME_URI(aMIMETypeString), "value", aMIMETypeString.toLowerCase());
   1.435 +    return aMIMETypeString;
   1.436 +  },
   1.437 +  
   1.438 +  get description()
   1.439 +  {
   1.440 +    return this.getLiteralForContentType(this.URI, "description");
   1.441 +  },  
   1.442 +  
   1.443 +  set description(aDescriptionString)
   1.444 +  {
   1.445 +    this.changeMIMEStuff(MIME_URI(this.mimeType), "description", aDescriptionString);
   1.446 +    return aDescriptionString;
   1.447 +  },
   1.448 +  
   1.449 +  get isEditable()
   1.450 +  {
   1.451 +    return this.getLiteralForContentType(this.URI, "editable");
   1.452 +  },
   1.453 +  
   1.454 +  set isEditable(aIsEditableString)
   1.455 +  {
   1.456 +    this.changeMIMEStuff(MIME_URI(this.mimeType), "editable", aIsEditableString);
   1.457 +    return aIsEditableString;
   1.458 +  },
   1.459 +
   1.460 +  get extensions()
   1.461 +  {
   1.462 +    var extensionResource = gRDF.GetUnicodeResource(NC_URI("fileExtensions"));
   1.463 +    var contentTypeResource = gRDF.GetUnicodeResource(MIME_URI(this.mimeType));
   1.464 +    var extensionTargets = this._DS.GetTargets(contentTypeResource, extensionResource, true);
   1.465 +    var extString = "";
   1.466 +    if (extensionTargets) {
   1.467 +      while (extensionTargets.hasMoreElements()) {
   1.468 +        var currentExtension = extensionTargets.getNext();
   1.469 +        if (currentExtension) {
   1.470 +          currentExtension = currentExtension.QueryInterface(Components.interfaces.nsIRDFLiteral);
   1.471 +          if (extString != "") {
   1.472 +            extString += " ";
   1.473 +          }
   1.474 +          extString += currentExtension.Value.toLowerCase();
   1.475 +        }
   1.476 +      }
   1.477 +    }
   1.478 +    return extString;
   1.479 +  },
   1.480 +  
   1.481 +  addExtension: function (aExtensionString)
   1.482 +  {
   1.483 +    this.assertMIMEStuff(MIME_URI(this.mimeType), "fileExtensions", aExtensionString.toLowerCase());
   1.484 +  },
   1.485 +  
   1.486 +  removeExtension: function (aExtensionString)
   1.487 +  {
   1.488 +    this.unassertMIMEStuff(MIME_URI(this.mimeType), "fileExtensions", aExtensionString.toLowerCase());
   1.489 +  },
   1.490 +
   1.491 +  clearExtensions: function ()
   1.492 +  {
   1.493 +    var extArray = this.extensions.split(" ");
   1.494 +    for (i = extArray.length - 1; i >= 0; --i) {
   1.495 +      this.removeExtension(extArray[i]);
   1.496 +    }
   1.497 +  },
   1.498 +  
   1.499 +  // content handling
   1.500 +  get saveToDisk()
   1.501 +  {
   1.502 +    return this.getHandlerInfoForType(this.URI, "saveToDisk");
   1.503 +  },
   1.504 +  
   1.505 +  set saveToDisk(aSavedToDisk)
   1.506 +  {
   1.507 +    this.changeMIMEStuff(HANDLER_URI(this.mimeType), "saveToDisk", aSavedToDisk);
   1.508 +    this.setHandlerProcedure("handleInternal", "false");
   1.509 +    this.setHandlerProcedure("useSystemDefault", "false");
   1.510 +    return aSavedToDisk;
   1.511 + },
   1.512 +
   1.513 +  get useSystemDefault()
   1.514 +  {
   1.515 +    return this.getHandlerInfoForType(this.URI, "useSystemDefault");
   1.516 +  },
   1.517 +
   1.518 +  set useSystemDefault(aUseSystemDefault)
   1.519 +  {
   1.520 +    this.changeMIMEStuff(HANDLER_URI(this.mimeType), "useSystemDefault", aUseSystemDefault);
   1.521 +    this.setHandlerProcedure("handleInternal", "false");
   1.522 +    this.setHandlerProcedure("saveToDisk", "false");
   1.523 +    return aUseSystemDefault;
   1.524 +  },
   1.525 +  
   1.526 +  get handleInternal()
   1.527 +  {
   1.528 +    return this.getHandlerInfoForType(this.URI, "handleInternal");
   1.529 +  },
   1.530 +  
   1.531 +  set handleInternal(aHandledInternally)
   1.532 +  {
   1.533 +    this.changeMIMEStuff(HANDLER_URI(this.mimeType), "handleInternal", aHandledInternally);
   1.534 +    this.setHandlerProcedure("saveToDisk", "false");
   1.535 +    this.setHandlerProcedure("useSystemDefault", "false");
   1.536 +    return aHandledInternally;
   1.537 +  },
   1.538 +
   1.539 +  setHandlerProcedure: function (aHandlerProcedure, aValue)
   1.540 +  {
   1.541 +    var handlerSource = gRDF.GetUnicodeResource(HANDLER_URI(this.mimeType));
   1.542 +    var handlerProperty = gRDF.GetUnicodeResource(NC_URI(aHandlerProcedure));
   1.543 +    var oppositeValue = aValue == "false" ? "true" : "false";
   1.544 +    var trueLiteral = gRDF.GetLiteral(oppositeValue);
   1.545 +    var hasCounterpart = this._DS.HasAssertion(handlerSource, handlerProperty, trueLiteral, true);
   1.546 +    if (hasCounterpart) {
   1.547 +      var falseLiteral = gRDF.GetLiteral(aValue);
   1.548 +      this._DS.Change(handlerSource, handlerProperty, trueLiteral, falseLiteral);
   1.549 +    }
   1.550 +  },
   1.551 +  
   1.552 +  get alwaysAsk()
   1.553 +  {
   1.554 +    return this.getHandlerInfoForType(this.URI, "alwaysAsk");
   1.555 +  },
   1.556 +  
   1.557 +  set alwaysAsk(aAlwaysAsk)
   1.558 +  {
   1.559 +    this.changeMIMEStuff(HANDLER_URI(this.mimeType), "alwaysAsk", aAlwaysAsk);
   1.560 +    return aAlwaysAsk;
   1.561 +  },
   1.562 +  
   1.563 +  // helper application
   1.564 +  get appDisplayName()
   1.565 +  {
   1.566 +    return getHelperAppInfoForType(this.URI, "prettyName");
   1.567 +  },
   1.568 +  
   1.569 +  set appDisplayName(aDisplayName)
   1.570 +  {
   1.571 +    if (aDisplayName)
   1.572 +      this.changeMIMEStuff(APP_URI(this.mimeType), "prettyName", aDisplayName);
   1.573 +    else {
   1.574 +      var currentValue = this.getLiteralForContentType(APP_URI(this.mimeType), "prettyName");
   1.575 +      this.unassertMIMEStuff(APP_URI(this.mimeType), "prettyName", currentValue);
   1.576 +    }
   1.577 +
   1.578 +    return aDisplayName;
   1.579 +  },
   1.580 +  
   1.581 +  get appPath()
   1.582 +  {
   1.583 +    return this.getHelperAppInfoForType(this.URI, "path");
   1.584 +  },
   1.585 +  
   1.586 +  set appPath(aAppPath)
   1.587 +  {
   1.588 +    if (aAppPath)
   1.589 +      this.changeMIMEStuff(APP_URI(this.mimeType), "path", aAppPath);
   1.590 +    else {
   1.591 +      var currentValue = this.getLiteralForContentType(APP_URI(this.mimeType), "path");
   1.592 +      this.unassertMIMEStuff(APP_URI(this.mimeType), "path", currentValue);
   1.593 +    }
   1.594 +
   1.595 +    return aAppPath;
   1.596 +  },
   1.597 +
   1.598 +  /**
   1.599 +   * After setting the various properties on this override, we need to
   1.600 +   * build the links between the mime type resource, the handler for that
   1.601 +   * resource, and the helper app (if any) associated with the resource.
   1.602 +   * We also need to add this mime type to the RDF seq (list) of types.
   1.603 +   **/
   1.604 +  buildLinks: function()
   1.605 +  {
   1.606 +    // assert the handler resource
   1.607 +    var mimeSource = gRDF.GetUnicodeResource(MIME_URI(this.mimeType));
   1.608 +    var handlerProperty = gRDF.GetUnicodeResource(NC_URI("handlerProp"));
   1.609 +    var handlerResource = gRDF.GetUnicodeResource(HANDLER_URI(this.mimeType));
   1.610 +    this._DS.Assert(mimeSource, handlerProperty, handlerResource, true);
   1.611 +    // assert the helper app resource
   1.612 +    var helperAppProperty = gRDF.GetUnicodeResource(NC_URI("externalApplication"));
   1.613 +    var helperAppResource = gRDF.GetUnicodeResource(APP_URI(this.mimeType));
   1.614 +    this._DS.Assert(handlerResource, helperAppProperty, helperAppResource, true);
   1.615 +    // add the mime type to the MIME types seq
   1.616 +    var container = this.ensureAndGetTypeList("mimetype");
   1.617 +    var element = gRDF.GetUnicodeResource(MIME_URI(this.mimeType));
   1.618 +    if (container.IndexOf(element) == -1)
   1.619 +      container.AppendElement(element);
   1.620 +  }, 
   1.621 +  
   1.622 +  // Implementation helper methods
   1.623 +  
   1.624 +  getLiteralForContentType: function (aURI, aProperty)
   1.625 +  {
   1.626 +    var contentTypeResource = gRDF.GetUnicodeResource(aURI);
   1.627 +    var propertyResource = gRDF.GetUnicodeResource(NC_URI(aProperty));
   1.628 +    return this.getLiteral(contentTypeResource, propertyResource);
   1.629 +  },
   1.630 +
   1.631 +  getLiteral: function (aSource, aProperty)
   1.632 +  {
   1.633 +    var node = this._DS.GetTarget(aSource, aProperty, true);
   1.634 +    if (node) {
   1.635 +      node = node.QueryInterface(Components.interfaces.nsIRDFLiteral);
   1.636 +      return node.Value;
   1.637 +    }
   1.638 +    return "";
   1.639 +  },
   1.640 +
   1.641 +  getHandlerInfoForType: function (aURI, aPropertyString)
   1.642 +  {
   1.643 +    // get current selected type
   1.644 +    var handler = HANDLER_URI(this.getLiteralForContentType(aURI, "value"));
   1.645 +    var source = gRDF.GetUnicodeResource(handler);
   1.646 +    var property = gRDF.GetUnicodeResource(NC_URI(aPropertyString));
   1.647 +    var target = this._DS.GetTarget(source, property, true);
   1.648 +    if (target) {
   1.649 +      target = target.QueryInterface(Components.interfaces.nsIRDFLiteral);
   1.650 +      return target.Value;
   1.651 +    }
   1.652 +    return "";
   1.653 +  },
   1.654 +
   1.655 +  getHelperAppInfoForType: function (aURI, aPropertyString)
   1.656 +  {
   1.657 +    var appURI      = APP_URI(this.getLiteralForContentType(aURI, "value"));
   1.658 +    var appRes      = gRDF.GetUnicodeResource(appURI);
   1.659 +    var appProperty = gRDF.GetUnicodeResource(NC_URI(aPropertyString));
   1.660 +    return getLiteral(appRes, appProperty);
   1.661 +  },
   1.662 +
   1.663 +  // write to the ds
   1.664 +  assertMIMEStuff: function (aMIMEString, aPropertyString, aValueString)
   1.665 +  {
   1.666 +    var mimeSource = gRDF.GetUnicodeResource(aMIMEString);
   1.667 +    var valueProperty = gRDF.GetUnicodeResource(NC_URI(aPropertyString));
   1.668 +    var mimeLiteral = gRDF.GetLiteral(aValueString);
   1.669 +    this._DS.Assert(mimeSource, valueProperty, mimeLiteral, true);
   1.670 +  },
   1.671 +
   1.672 +  changeMIMEStuff: function(aMIMEString, aPropertyString, aValueString)
   1.673 +  {
   1.674 +    var mimeSource = gRDF.GetUnicodeResource(aMIMEString);
   1.675 +    var valueProperty = gRDF.GetUnicodeResource(NC_URI(aPropertyString));
   1.676 +    var mimeLiteral = gRDF.GetLiteral(aValueString);
   1.677 +    var currentValue = this._DS.GetTarget(mimeSource, valueProperty, true);
   1.678 +    if (currentValue) {
   1.679 +      this._DS.Change(mimeSource, valueProperty, currentValue, mimeLiteral);
   1.680 +    } else {
   1.681 +      this._DS.Assert(mimeSource, valueProperty, mimeLiteral, true);
   1.682 +    } 
   1.683 +  },
   1.684 +
   1.685 +  unassertMIMEStuff: function(aMIMEString, aPropertyString, aValueString)
   1.686 +  {
   1.687 +    var mimeSource = gRDF.GetUnicodeResource(aMIMEString);
   1.688 +    var valueProperty = gRDF.GetUnicodeResource(NC_URI(aPropertyString));
   1.689 +    var mimeLiteral = gRDF.GetLiteral(aValueString);
   1.690 +    this._DS.Unassert(mimeSource, valueProperty, mimeLiteral, true);
   1.691 +  },
   1.692 +
   1.693 +  /**
   1.694 +   * Get the list of types for the given class, creating the list if it doesn't
   1.695 +   * already exist. The class can be either CLASS_MIMEINFO or CLASS_PROTOCOLINFO
   1.696 +   * (i.e. the result of a call to _getClass).
   1.697 +   * 
   1.698 +   * |urn:<class>s|
   1.699 +   * |urn:<class>s:root|
   1.700 +   * 
   1.701 +   * @param aClass {string} the class for which to retrieve a list of types
   1.702 +   *
   1.703 +   * @returns {nsIRDFContainer} the list of types
   1.704 +   */
   1.705 +  ensureAndGetTypeList: function (aClass) {
   1.706 +    var source = gRDF.GetResource("urn:" + aClass + "s");
   1.707 +    var property =
   1.708 +      gRDF.GetResource(aClass == CLASS_MIMEINFO ? NC_MIME_TYPES
   1.709 +                                                : NC_PROTOCOL_SCHEMES);
   1.710 +    var target = gRDF.GetResource("urn:" + aClass + "s:root");
   1.711 +
   1.712 +    // Make sure we have an arc from the source to the target.
   1.713 +    if (!this._DS.HasAssertion(source, property, target, true))
   1.714 +      this._DS.Assert(source, property, target, true);
   1.715 +
   1.716 +    // Make sure the target is a container.
   1.717 +    var containerUtils = Cc["@mozilla.org/rdf/container-utils;1"]
   1.718 +                            .getService(Ci.nsIRDFContainerUtils);
   1.719 +    if (!containerUtils.IsContainer(this._DS, target))
   1.720 +      containerUtils.MakeSeq(this._DS, target);
   1.721 +
   1.722 +    // Get the type list as an RDF container.
   1.723 +    var typeList =
   1.724 +          Cc["@mozilla.org/rdf/container;1"].createInstance(Ci.nsIRDFContainer);
   1.725 +    typeList.Init(this._DS, target);
   1.726 +
   1.727 +    return typeList;
   1.728 +  }
   1.729 +};
   1.730 +

mercurial