toolkit/mozapps/downloads/content/helperApps.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 var Cc = Components.classes;
michael@0 8 var Ci = Components.interfaces;
michael@0 9
michael@0 10 var gRDF;
michael@0 11
michael@0 12 const CLASS_MIMEINFO = "mimetype";
michael@0 13 const CLASS_PROTOCOLINFO = "scheme";
michael@0 14
michael@0 15 // namespace prefix
michael@0 16 const NC_NS = "http://home.netscape.com/NC-rdf#";
michael@0 17
michael@0 18 // type list properties
michael@0 19
michael@0 20 const NC_MIME_TYPES = NC_NS + "MIME-types";
michael@0 21 const NC_PROTOCOL_SCHEMES = NC_NS + "Protocol-Schemes";
michael@0 22
michael@0 23 ///////////////////////////////////////////////////////////////////////////////
michael@0 24 // MIME Types DataSource Wrapper
michael@0 25
michael@0 26 function NC_URI(aProperty)
michael@0 27 {
michael@0 28 return "http://home.netscape.com/NC-rdf#" + aProperty;
michael@0 29 }
michael@0 30
michael@0 31 function MIME_URI(aType)
michael@0 32 {
michael@0 33 return "urn:mimetype:" + aType;
michael@0 34 }
michael@0 35
michael@0 36 function HANDLER_URI(aHandler)
michael@0 37 {
michael@0 38 return "urn:mimetype:handler:" + aHandler;
michael@0 39 }
michael@0 40
michael@0 41 function APP_URI(aType)
michael@0 42 {
michael@0 43 return "urn:mimetype:externalApplication:" + aType;
michael@0 44 }
michael@0 45
michael@0 46 function ArrayEnumerator(aItems) {
michael@0 47 if (aItems) {
michael@0 48 for (var i = 0; i < aItems.length; ++i) {
michael@0 49 if (!aItems[i])
michael@0 50 aItems.splice(i--, 1);
michael@0 51 }
michael@0 52 this._contents = aItems;
michael@0 53 } else {
michael@0 54 this._contents = [];
michael@0 55 }
michael@0 56 }
michael@0 57
michael@0 58 ArrayEnumerator.prototype = {
michael@0 59 _index: 0,
michael@0 60
michael@0 61 hasMoreElements: function () {
michael@0 62 return this._index < this._contents.length;
michael@0 63 },
michael@0 64
michael@0 65 getNext: function () {
michael@0 66 return this._contents[this._index++];
michael@0 67 },
michael@0 68
michael@0 69 push: function (aElement) {
michael@0 70 if (aElement)
michael@0 71 this._contents.push(aElement);
michael@0 72 }
michael@0 73 };
michael@0 74
michael@0 75 function HelperApps()
michael@0 76 {
michael@0 77 if (!gRDF)
michael@0 78 gRDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
michael@0 79
michael@0 80 const mimeTypes = "UMimTyp";
michael@0 81 var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
michael@0 82
michael@0 83 var file = fileLocator.get(mimeTypes, Components.interfaces.nsIFile);
michael@0 84
michael@0 85 var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
michael@0 86 var fileHandler = ioService.getProtocolHandler("file").QueryInterface(Components.interfaces.nsIFileProtocolHandler);
michael@0 87 this._inner = gRDF.GetDataSourceBlocking(fileHandler.getURLSpecFromFile(file));
michael@0 88 this._inner.AddObserver(this);
michael@0 89
michael@0 90 this._fileTypeArc = gRDF.GetResource(NC_URI("FileType"));
michael@0 91 this._fileHandlerArc = gRDF.GetResource(NC_URI("FileHandler"));
michael@0 92 this._fileIconArc = gRDF.GetResource(NC_URI("FileIcon"));
michael@0 93 this._fileExtensionArc = gRDF.GetResource(NC_URI("FileExtension"));
michael@0 94 this._fileExtensionsArc = gRDF.GetResource(NC_URI("FileExtensions"));
michael@0 95 this._handleAutoArc = gRDF.GetResource(NC_URI("FileHandleAuto"));
michael@0 96 this._valueArc = gRDF.GetResource(NC_URI("value"));
michael@0 97 this._handlerPropArc = gRDF.GetResource(NC_URI("handlerProp"));
michael@0 98 this._externalAppArc = gRDF.GetResource(NC_URI("externalApplication"));
michael@0 99 }
michael@0 100
michael@0 101 HelperApps.prototype = {
michael@0 102 mimeHandlerExists: function (aMIMEType)
michael@0 103 {
michael@0 104 var valueProperty = gRDF.GetUnicodeResource(NC_URI("value"));
michael@0 105 var mimeSource = gRDF.GetUnicodeResource(MIME_URI(aMIMEType));
michael@0 106 var mimeLiteral = gRDF.GetLiteral(aMIMEType);
michael@0 107 return this._inner.HasAssertion(mimeSource, valueProperty, mimeLiteral, true);
michael@0 108 },
michael@0 109
michael@0 110 updateTypeInfo: function (aMIMEInfo)
michael@0 111 {
michael@0 112 var mimeType = aMIMEInfo.MIMEType;
michael@0 113 var isNewMIMEType = this.mimeHandlerExists(mimeType);
michael@0 114 var entry = new HandlerOverride(MIME_URI(mimeType), this._inner);
michael@0 115 entry.mimeType = mimeType;
michael@0 116 entry.isEditable = true;
michael@0 117 entry.alwaysAsk = aMIMEInfo.alwaysAskBeforeHandling;
michael@0 118
michael@0 119 // If not updating (i.e., a newly encountered mime type),
michael@0 120 // then update extension list and description.
michael@0 121 if (!isNewMIMEType) {
michael@0 122 var extEnumerator = aMIMEInfo.getFileExtensions();
michael@0 123 while (extEnumerator.hasMore()) {
michael@0 124 entry.addExtension(extEnumerator.getNext());
michael@0 125 }
michael@0 126 entry.description = aMIMEInfo.description;
michael@0 127 entry.appDisplayName = "";
michael@0 128 }
michael@0 129
michael@0 130 const nsIMIMEInfo = Components.interfaces.nsIMIMEInfo;
michael@0 131 if (aMIMEInfo.preferredAction == nsIMIMEInfo.saveToDisk) {
michael@0 132 entry.saveToDisk = true;
michael@0 133 if (!isNewMIMEType) {
michael@0 134 // Creating a new entry, set path.
michael@0 135 entry.appPath = "";
michael@0 136 }
michael@0 137 }
michael@0 138 else if (aMIMEInfo.preferredAction == nsIMIMEInfo.useSystemDefault ||
michael@0 139 aMIMEInfo.preferredApplicationHandler == null) {
michael@0 140 entry.useSystemDefault = true;
michael@0 141 if (!isNewMIMEType) {
michael@0 142 // Creating a new entry, set path.
michael@0 143 entry.appPath = "";
michael@0 144 }
michael@0 145 }
michael@0 146 else if (aMIMEInfo.preferredApplicationHandler instanceof Components.interfaces.nsILocalHandlerApp) {
michael@0 147 entry.saveToDisk = false;
michael@0 148 entry.useSystemDefault = false;
michael@0 149 entry.handleInternal = false;
michael@0 150 entry.appPath = aMIMEInfo.preferredApplicationHandler.executable.path;
michael@0 151 entry.appDisplayName = aMIMEInfo.preferredApplicationHandler.name;
michael@0 152 }
michael@0 153
michael@0 154 // Do RDF magic.
michael@0 155 entry.buildLinks();
michael@0 156 this.flush();
michael@0 157 },
michael@0 158
michael@0 159 getLiteralValue: function (aResource, aProperty)
michael@0 160 {
michael@0 161 var res = gRDF.GetResource(aResource);
michael@0 162 var prop = gRDF.GetResource(NC_URI(aProperty));
michael@0 163 var val = this.GetTarget(res, prop, true);
michael@0 164 if (val) {
michael@0 165 val = val.QueryInterface(Components.interfaces.nsIRDFLiteral);
michael@0 166 return val.Value;
michael@0 167 }
michael@0 168 return "";
michael@0 169 },
michael@0 170
michael@0 171 /* nsIRDFDataSource */
michael@0 172 get URI() {
michael@0 173 return this._inner.URI;
michael@0 174 },
michael@0 175
michael@0 176 GetSource: function (aProperty, aTarget, aTruthValue) {
michael@0 177 return this._inner.GetSource(aProperty, aTarget, aTruthValue);
michael@0 178 },
michael@0 179 GetSources: function (aProperty, aTarget, aTruthValue) {
michael@0 180 return this._inner.GetSources(aProperty, aTarget, aTruthValue);
michael@0 181 },
michael@0 182
michael@0 183 _isRootTypeResource: function (aResource) {
michael@0 184 aResource = aResource.QueryInterface(Components.interfaces.nsIRDFResource);
michael@0 185 const kRootTypePrefix = "urn:mimetype:";
michael@0 186 return (aResource.Value.substr(0, kRootTypePrefix.length) == kRootTypePrefix);
michael@0 187 },
michael@0 188
michael@0 189 getMIMEInfo: function (aResource) {
michael@0 190 var types = this._inner.GetTarget(aResource, this._valueArc, true);
michael@0 191 if (types) {
michael@0 192 types = types.QueryInterface(Components.interfaces.nsIRDFLiteral);
michael@0 193 types = types.Value.split(", ");
michael@0 194
michael@0 195 mimeSvc = Components.classes["@mozilla.org/mime;1"].getService(Components.interfaces.nsIMIMEService);
michael@0 196 return mimeSvc.getFromTypeAndExtension(types[0], null);
michael@0 197 }
michael@0 198
michael@0 199 return null;
michael@0 200 },
michael@0 201
michael@0 202 GetTarget: function (aSource, aProperty, aTruthValue) {
michael@0 203 if (this._isRootTypeResource(aSource)) {
michael@0 204 var typeInfo = this.getMIMEInfo(aSource);
michael@0 205 if (typeInfo) {
michael@0 206 var bundle = document.getElementById("strings");
michael@0 207 if (aProperty.EqualsNode(this._handleAutoArc)) {
michael@0 208 var handler = this.GetTarget(aSource, this._handlerPropArc, true);
michael@0 209 if (handler) {
michael@0 210 handler = handler.QueryInterface(Components.interfaces.nsIRDFResource);
michael@0 211 return gRDF.GetLiteral(!(this.getLiteralValue(handler.Value, "alwaysAsk") == "true"));
michael@0 212 }
michael@0 213 }
michael@0 214 else if (aProperty.EqualsNode(this._fileTypeArc)) {
michael@0 215 if (typeInfo.description == "") {
michael@0 216 try {
michael@0 217 var literal = bundle.getFormattedString("fileEnding", [typeInfo.primaryExtension.toUpperCase()]);
michael@0 218 return gRDF.GetLiteral(literal);
michael@0 219 }
michael@0 220 catch (e) {
michael@0 221 // Wow, this sucks, just show the MIME type as a last ditch effort to display
michael@0 222 // the type of file that this is.
michael@0 223 return gRDF.GetLiteral(typeInfo.MIMEType);
michael@0 224 }
michael@0 225 }
michael@0 226 return gRDF.GetLiteral(typeInfo.description);
michael@0 227 }
michael@0 228 else if (aProperty.EqualsNode(this._fileHandlerArc)) {
michael@0 229 var handler = this.GetTarget(aSource, this._handlerPropArc, true);
michael@0 230 if (handler) {
michael@0 231 handler = handler.QueryInterface(Components.interfaces.nsIRDFResource);
michael@0 232 if (this.getLiteralValue(handler.Value, "saveToDisk") == "true") {
michael@0 233 var saveToDisk = bundle.getString("saveToDisk");
michael@0 234 return gRDF.GetLiteral(saveToDisk);
michael@0 235 }
michael@0 236 else if (this.getLiteralValue(handler.Value, "useSystemDefault") == "false") {
michael@0 237 var extApp = this.GetTarget(handler, this._externalAppArc, true);
michael@0 238 if (extApp) {
michael@0 239 extApp = extApp.QueryInterface(Components.interfaces.nsIRDFResource);
michael@0 240 var openWith = bundle.getFormattedString("openWith", [this.getLiteralValue(extApp.Value, "prettyName")]);
michael@0 241 return gRDF.GetLiteral(openWith);
michael@0 242 }
michael@0 243 }
michael@0 244 }
michael@0 245
michael@0 246 var openWith2 = bundle.getFormattedString("openWith", [typeInfo.defaultDescription]);
michael@0 247 return gRDF.GetLiteral(openWith2);
michael@0 248 }
michael@0 249 else if (aProperty.EqualsNode(this._fileIconArc)) {
michael@0 250 try {
michael@0 251 return gRDF.GetLiteral("moz-icon://goat." + typeInfo.primaryExtension + "?size=16");
michael@0 252 }
michael@0 253 catch (e) {
michael@0 254 return gRDF.GetLiteral("moz-icon://goat?size=16&contentType=" + typeInfo.MIMEType);
michael@0 255 }
michael@0 256 }
michael@0 257 else if (aProperty.EqualsNode(this._fileExtensionArc)) {
michael@0 258 try {
michael@0 259 return gRDF.GetLiteral(typeInfo.primaryExtension.toUpperCase());
michael@0 260 }
michael@0 261 catch (e) { }
michael@0 262 return gRDF.GetLiteral("");
michael@0 263 }
michael@0 264 else if (aProperty.EqualsNode(this._fileExtensionsArc)) {
michael@0 265 var extns = typeInfo.getFileExtensions();
michael@0 266
michael@0 267 // Prevent duplicates.
michael@0 268 var hash = { };
michael@0 269 while (extns.hasMore())
michael@0 270 hash[extns.getNext().toUpperCase()] = 0;
michael@0 271
michael@0 272 var str = "";
michael@0 273 for (var extn in hash)
michael@0 274 str += extn + ",";
michael@0 275 str = str.substring(0, str.length - 1);
michael@0 276
michael@0 277 return gRDF.GetLiteral(str);
michael@0 278 }
michael@0 279 }
michael@0 280 }
michael@0 281
michael@0 282 return this._inner.GetTarget(aSource, aProperty, aTruthValue);
michael@0 283 },
michael@0 284
michael@0 285 GetTargets: function (aSource, aProperty, aTruthValue) {
michael@0 286 if (this._isRootTypeResource(aSource)) {
michael@0 287 return new ArrayEnumerator([this.GetTarget(aSource, aProperty, aTruthValue)]);
michael@0 288 }
michael@0 289
michael@0 290 return this._inner.GetTargets(aSource, aProperty, aTruthValue);
michael@0 291 },
michael@0 292 Assert: function (aSource, aProperty, aTarget, aTruthValue) {
michael@0 293 return this._inner.Assert(aSource, aProperty, aTarget, aTruthValue);
michael@0 294 },
michael@0 295 Unassert: function (aSource, aProperty, aTarget) {
michael@0 296 return this._inner.Unassert(aSource, aProperty, aTarget);
michael@0 297 },
michael@0 298 Change: function (aSource, aProperty, aOldTarget, aNewTarget) {
michael@0 299 if (aOldTarget)
michael@0 300 var ot = aOldTarget.QueryInterface(Components.interfaces.nsIRDFLiteral);
michael@0 301 if (aNewTarget)
michael@0 302 var nt = aNewTarget.QueryInterface(Components.interfaces.nsIRDFLiteral);
michael@0 303
michael@0 304 return this._inner.Change(aSource, aProperty, aOldTarget, aNewTarget);
michael@0 305 },
michael@0 306 Move: function (aOldSource, aNewSource, aProperty, aTarget) {
michael@0 307 return this._inner.Assert(aOldSource, aNewSource, aProperty, aTarget);
michael@0 308 },
michael@0 309 HasAssertion: function (aSource, aProperty, aTarget, aTruthValue) {
michael@0 310 if (this._isRootTypeResource(aSource)) {
michael@0 311 // Don't show entries in the list for types that we DO NOT handle
michael@0 312 // automatically. i.e. this list is a means of editing and removing
michael@0 313 // automatic overrides only.
michael@0 314 if (aProperty.EqualsNode(this._handleAutoArc)) {
michael@0 315 var handler = this.GetTarget(aSource, this._handlerPropArc, true);
michael@0 316 if (handler) {
michael@0 317 handler = handler.QueryInterface(Components.interfaces.nsIRDFResource);
michael@0 318 return !(this.getLiteralValue(handler.Value, "alwaysAsk") == "true");
michael@0 319 }
michael@0 320 }
michael@0 321 }
michael@0 322 return this._inner.HasAssertion(aSource, aProperty, aTarget, aTruthValue);
michael@0 323 },
michael@0 324 ArcLabelsIn: function (aNode) {
michael@0 325 return this._inner.ArcLabelsIn(aNode);
michael@0 326 },
michael@0 327 ArcLabelsOut: function (aNode) {
michael@0 328 return this._inner.ArcLabelsOut(aNode);
michael@0 329 },
michael@0 330 GetAllResources: function () {
michael@0 331 return this._inner.GetAllResources();
michael@0 332 },
michael@0 333 hasArcIn: function (aNode, aArc) {
michael@0 334 return this._inner.hasArcIn(aNode, aArc);
michael@0 335 },
michael@0 336 hasArcOut: function (aNode, aArc) {
michael@0 337 return this._inner.hasArcOut(aNode, aArc);
michael@0 338 },
michael@0 339
michael@0 340 _observers: [],
michael@0 341 AddObserver: function (aObserver) {
michael@0 342 this._observers.push(aObserver);
michael@0 343 },
michael@0 344
michael@0 345 RemoveObserver: function (aObserver) {
michael@0 346 for (var i = 0; i < this._observers.length; ++i) {
michael@0 347 if (this._observers[i] == aObserver) {
michael@0 348 this._observers.splice(i, 1);
michael@0 349 break;
michael@0 350 }
michael@0 351 }
michael@0 352 },
michael@0 353
michael@0 354 onAssert: function (aDataSource, aSource, aProperty, aTarget) {
michael@0 355 for (var i = 0; i < this._observers.length; ++i) {
michael@0 356 this._observers[i].onAssert(aDataSource, aSource, aProperty, aTarget);
michael@0 357 }
michael@0 358 },
michael@0 359
michael@0 360 onUnassert: function (aDataSource, aSource, aProperty, aTarget) {
michael@0 361 for (var i = 0; i < this._observers.length; ++i) {
michael@0 362 this._observers[i].onUnassert(aDataSource, aSource, aProperty, aTarget);
michael@0 363 }
michael@0 364 },
michael@0 365
michael@0 366 onChange: function (aDataSource, aSource, aProperty, aOldTarget, aNewTarget) {
michael@0 367 for (var i = 0; i < this._observers.length; ++i) {
michael@0 368 this._observers[i].onChange(aDataSource, aSource, aProperty, aOldTarget, aNewTarget);
michael@0 369 }
michael@0 370 },
michael@0 371
michael@0 372 onMove: function (aDataSource, aOldSource, aNewSource, aProperty, aTarget) {
michael@0 373 for (var i = 0; i < this._observers.length; ++i) {
michael@0 374 this._observers[i].onMove(aDataSource, aOldSource, aNewSource, aProperty, aTarget);
michael@0 375 }
michael@0 376 },
michael@0 377
michael@0 378 onBeginUpdateBatch: function (aDataSource) {
michael@0 379 for (var i = 0; i < this._observers.length; ++i) {
michael@0 380 this._observers[i].onBeginUpdateBatch(aDataSource);
michael@0 381 }
michael@0 382 },
michael@0 383
michael@0 384 onEndUpdateBatch: function (aDataSource) {
michael@0 385 for (var i = 0; i < this._observers.length; ++i) {
michael@0 386 this._observers[i].onEndUpdateBatch(aDataSource);
michael@0 387 }
michael@0 388 },
michael@0 389
michael@0 390 beginUpdateBatch: function (aDataSource) {
michael@0 391 for (var i = 0; i < this._observers.length; ++i) {
michael@0 392 this._observers[i].beginUpdateBatch(aDataSource);
michael@0 393 }
michael@0 394 },
michael@0 395
michael@0 396 endUpdateBatch: function (aDataSource) {
michael@0 397 for (var i = 0; i < this._observers.length; ++i) {
michael@0 398 this._observers[i].endUpdateBatch(aDataSource);
michael@0 399 }
michael@0 400 },
michael@0 401
michael@0 402 flush: function () {
michael@0 403 var rds = this._inner.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
michael@0 404 if (rds)
michael@0 405 rds.Flush();
michael@0 406 },
michael@0 407
michael@0 408 destroy: function () {
michael@0 409 this._inner.RemoveObserver(this);
michael@0 410 }
michael@0 411 };
michael@0 412
michael@0 413 /**
michael@0 414 * Handler Override class
michael@0 415 **/
michael@0 416 function HandlerOverride(aURI, aDatasource)
michael@0 417 {
michael@0 418 this.URI = aURI;
michael@0 419 this._DS = aDatasource;
michael@0 420 }
michael@0 421
michael@0 422 HandlerOverride.prototype = {
michael@0 423 // general information
michael@0 424 get mimeType()
michael@0 425 {
michael@0 426 return this.getLiteralForContentType(this.URI, "value");
michael@0 427 },
michael@0 428
michael@0 429 set mimeType(aMIMETypeString)
michael@0 430 {
michael@0 431 this.changeMIMEStuff(MIME_URI(aMIMETypeString), "value", aMIMETypeString.toLowerCase());
michael@0 432 return aMIMETypeString;
michael@0 433 },
michael@0 434
michael@0 435 get description()
michael@0 436 {
michael@0 437 return this.getLiteralForContentType(this.URI, "description");
michael@0 438 },
michael@0 439
michael@0 440 set description(aDescriptionString)
michael@0 441 {
michael@0 442 this.changeMIMEStuff(MIME_URI(this.mimeType), "description", aDescriptionString);
michael@0 443 return aDescriptionString;
michael@0 444 },
michael@0 445
michael@0 446 get isEditable()
michael@0 447 {
michael@0 448 return this.getLiteralForContentType(this.URI, "editable");
michael@0 449 },
michael@0 450
michael@0 451 set isEditable(aIsEditableString)
michael@0 452 {
michael@0 453 this.changeMIMEStuff(MIME_URI(this.mimeType), "editable", aIsEditableString);
michael@0 454 return aIsEditableString;
michael@0 455 },
michael@0 456
michael@0 457 get extensions()
michael@0 458 {
michael@0 459 var extensionResource = gRDF.GetUnicodeResource(NC_URI("fileExtensions"));
michael@0 460 var contentTypeResource = gRDF.GetUnicodeResource(MIME_URI(this.mimeType));
michael@0 461 var extensionTargets = this._DS.GetTargets(contentTypeResource, extensionResource, true);
michael@0 462 var extString = "";
michael@0 463 if (extensionTargets) {
michael@0 464 while (extensionTargets.hasMoreElements()) {
michael@0 465 var currentExtension = extensionTargets.getNext();
michael@0 466 if (currentExtension) {
michael@0 467 currentExtension = currentExtension.QueryInterface(Components.interfaces.nsIRDFLiteral);
michael@0 468 if (extString != "") {
michael@0 469 extString += " ";
michael@0 470 }
michael@0 471 extString += currentExtension.Value.toLowerCase();
michael@0 472 }
michael@0 473 }
michael@0 474 }
michael@0 475 return extString;
michael@0 476 },
michael@0 477
michael@0 478 addExtension: function (aExtensionString)
michael@0 479 {
michael@0 480 this.assertMIMEStuff(MIME_URI(this.mimeType), "fileExtensions", aExtensionString.toLowerCase());
michael@0 481 },
michael@0 482
michael@0 483 removeExtension: function (aExtensionString)
michael@0 484 {
michael@0 485 this.unassertMIMEStuff(MIME_URI(this.mimeType), "fileExtensions", aExtensionString.toLowerCase());
michael@0 486 },
michael@0 487
michael@0 488 clearExtensions: function ()
michael@0 489 {
michael@0 490 var extArray = this.extensions.split(" ");
michael@0 491 for (i = extArray.length - 1; i >= 0; --i) {
michael@0 492 this.removeExtension(extArray[i]);
michael@0 493 }
michael@0 494 },
michael@0 495
michael@0 496 // content handling
michael@0 497 get saveToDisk()
michael@0 498 {
michael@0 499 return this.getHandlerInfoForType(this.URI, "saveToDisk");
michael@0 500 },
michael@0 501
michael@0 502 set saveToDisk(aSavedToDisk)
michael@0 503 {
michael@0 504 this.changeMIMEStuff(HANDLER_URI(this.mimeType), "saveToDisk", aSavedToDisk);
michael@0 505 this.setHandlerProcedure("handleInternal", "false");
michael@0 506 this.setHandlerProcedure("useSystemDefault", "false");
michael@0 507 return aSavedToDisk;
michael@0 508 },
michael@0 509
michael@0 510 get useSystemDefault()
michael@0 511 {
michael@0 512 return this.getHandlerInfoForType(this.URI, "useSystemDefault");
michael@0 513 },
michael@0 514
michael@0 515 set useSystemDefault(aUseSystemDefault)
michael@0 516 {
michael@0 517 this.changeMIMEStuff(HANDLER_URI(this.mimeType), "useSystemDefault", aUseSystemDefault);
michael@0 518 this.setHandlerProcedure("handleInternal", "false");
michael@0 519 this.setHandlerProcedure("saveToDisk", "false");
michael@0 520 return aUseSystemDefault;
michael@0 521 },
michael@0 522
michael@0 523 get handleInternal()
michael@0 524 {
michael@0 525 return this.getHandlerInfoForType(this.URI, "handleInternal");
michael@0 526 },
michael@0 527
michael@0 528 set handleInternal(aHandledInternally)
michael@0 529 {
michael@0 530 this.changeMIMEStuff(HANDLER_URI(this.mimeType), "handleInternal", aHandledInternally);
michael@0 531 this.setHandlerProcedure("saveToDisk", "false");
michael@0 532 this.setHandlerProcedure("useSystemDefault", "false");
michael@0 533 return aHandledInternally;
michael@0 534 },
michael@0 535
michael@0 536 setHandlerProcedure: function (aHandlerProcedure, aValue)
michael@0 537 {
michael@0 538 var handlerSource = gRDF.GetUnicodeResource(HANDLER_URI(this.mimeType));
michael@0 539 var handlerProperty = gRDF.GetUnicodeResource(NC_URI(aHandlerProcedure));
michael@0 540 var oppositeValue = aValue == "false" ? "true" : "false";
michael@0 541 var trueLiteral = gRDF.GetLiteral(oppositeValue);
michael@0 542 var hasCounterpart = this._DS.HasAssertion(handlerSource, handlerProperty, trueLiteral, true);
michael@0 543 if (hasCounterpart) {
michael@0 544 var falseLiteral = gRDF.GetLiteral(aValue);
michael@0 545 this._DS.Change(handlerSource, handlerProperty, trueLiteral, falseLiteral);
michael@0 546 }
michael@0 547 },
michael@0 548
michael@0 549 get alwaysAsk()
michael@0 550 {
michael@0 551 return this.getHandlerInfoForType(this.URI, "alwaysAsk");
michael@0 552 },
michael@0 553
michael@0 554 set alwaysAsk(aAlwaysAsk)
michael@0 555 {
michael@0 556 this.changeMIMEStuff(HANDLER_URI(this.mimeType), "alwaysAsk", aAlwaysAsk);
michael@0 557 return aAlwaysAsk;
michael@0 558 },
michael@0 559
michael@0 560 // helper application
michael@0 561 get appDisplayName()
michael@0 562 {
michael@0 563 return getHelperAppInfoForType(this.URI, "prettyName");
michael@0 564 },
michael@0 565
michael@0 566 set appDisplayName(aDisplayName)
michael@0 567 {
michael@0 568 if (aDisplayName)
michael@0 569 this.changeMIMEStuff(APP_URI(this.mimeType), "prettyName", aDisplayName);
michael@0 570 else {
michael@0 571 var currentValue = this.getLiteralForContentType(APP_URI(this.mimeType), "prettyName");
michael@0 572 this.unassertMIMEStuff(APP_URI(this.mimeType), "prettyName", currentValue);
michael@0 573 }
michael@0 574
michael@0 575 return aDisplayName;
michael@0 576 },
michael@0 577
michael@0 578 get appPath()
michael@0 579 {
michael@0 580 return this.getHelperAppInfoForType(this.URI, "path");
michael@0 581 },
michael@0 582
michael@0 583 set appPath(aAppPath)
michael@0 584 {
michael@0 585 if (aAppPath)
michael@0 586 this.changeMIMEStuff(APP_URI(this.mimeType), "path", aAppPath);
michael@0 587 else {
michael@0 588 var currentValue = this.getLiteralForContentType(APP_URI(this.mimeType), "path");
michael@0 589 this.unassertMIMEStuff(APP_URI(this.mimeType), "path", currentValue);
michael@0 590 }
michael@0 591
michael@0 592 return aAppPath;
michael@0 593 },
michael@0 594
michael@0 595 /**
michael@0 596 * After setting the various properties on this override, we need to
michael@0 597 * build the links between the mime type resource, the handler for that
michael@0 598 * resource, and the helper app (if any) associated with the resource.
michael@0 599 * We also need to add this mime type to the RDF seq (list) of types.
michael@0 600 **/
michael@0 601 buildLinks: function()
michael@0 602 {
michael@0 603 // assert the handler resource
michael@0 604 var mimeSource = gRDF.GetUnicodeResource(MIME_URI(this.mimeType));
michael@0 605 var handlerProperty = gRDF.GetUnicodeResource(NC_URI("handlerProp"));
michael@0 606 var handlerResource = gRDF.GetUnicodeResource(HANDLER_URI(this.mimeType));
michael@0 607 this._DS.Assert(mimeSource, handlerProperty, handlerResource, true);
michael@0 608 // assert the helper app resource
michael@0 609 var helperAppProperty = gRDF.GetUnicodeResource(NC_URI("externalApplication"));
michael@0 610 var helperAppResource = gRDF.GetUnicodeResource(APP_URI(this.mimeType));
michael@0 611 this._DS.Assert(handlerResource, helperAppProperty, helperAppResource, true);
michael@0 612 // add the mime type to the MIME types seq
michael@0 613 var container = this.ensureAndGetTypeList("mimetype");
michael@0 614 var element = gRDF.GetUnicodeResource(MIME_URI(this.mimeType));
michael@0 615 if (container.IndexOf(element) == -1)
michael@0 616 container.AppendElement(element);
michael@0 617 },
michael@0 618
michael@0 619 // Implementation helper methods
michael@0 620
michael@0 621 getLiteralForContentType: function (aURI, aProperty)
michael@0 622 {
michael@0 623 var contentTypeResource = gRDF.GetUnicodeResource(aURI);
michael@0 624 var propertyResource = gRDF.GetUnicodeResource(NC_URI(aProperty));
michael@0 625 return this.getLiteral(contentTypeResource, propertyResource);
michael@0 626 },
michael@0 627
michael@0 628 getLiteral: function (aSource, aProperty)
michael@0 629 {
michael@0 630 var node = this._DS.GetTarget(aSource, aProperty, true);
michael@0 631 if (node) {
michael@0 632 node = node.QueryInterface(Components.interfaces.nsIRDFLiteral);
michael@0 633 return node.Value;
michael@0 634 }
michael@0 635 return "";
michael@0 636 },
michael@0 637
michael@0 638 getHandlerInfoForType: function (aURI, aPropertyString)
michael@0 639 {
michael@0 640 // get current selected type
michael@0 641 var handler = HANDLER_URI(this.getLiteralForContentType(aURI, "value"));
michael@0 642 var source = gRDF.GetUnicodeResource(handler);
michael@0 643 var property = gRDF.GetUnicodeResource(NC_URI(aPropertyString));
michael@0 644 var target = this._DS.GetTarget(source, property, true);
michael@0 645 if (target) {
michael@0 646 target = target.QueryInterface(Components.interfaces.nsIRDFLiteral);
michael@0 647 return target.Value;
michael@0 648 }
michael@0 649 return "";
michael@0 650 },
michael@0 651
michael@0 652 getHelperAppInfoForType: function (aURI, aPropertyString)
michael@0 653 {
michael@0 654 var appURI = APP_URI(this.getLiteralForContentType(aURI, "value"));
michael@0 655 var appRes = gRDF.GetUnicodeResource(appURI);
michael@0 656 var appProperty = gRDF.GetUnicodeResource(NC_URI(aPropertyString));
michael@0 657 return getLiteral(appRes, appProperty);
michael@0 658 },
michael@0 659
michael@0 660 // write to the ds
michael@0 661 assertMIMEStuff: function (aMIMEString, aPropertyString, aValueString)
michael@0 662 {
michael@0 663 var mimeSource = gRDF.GetUnicodeResource(aMIMEString);
michael@0 664 var valueProperty = gRDF.GetUnicodeResource(NC_URI(aPropertyString));
michael@0 665 var mimeLiteral = gRDF.GetLiteral(aValueString);
michael@0 666 this._DS.Assert(mimeSource, valueProperty, mimeLiteral, true);
michael@0 667 },
michael@0 668
michael@0 669 changeMIMEStuff: function(aMIMEString, aPropertyString, aValueString)
michael@0 670 {
michael@0 671 var mimeSource = gRDF.GetUnicodeResource(aMIMEString);
michael@0 672 var valueProperty = gRDF.GetUnicodeResource(NC_URI(aPropertyString));
michael@0 673 var mimeLiteral = gRDF.GetLiteral(aValueString);
michael@0 674 var currentValue = this._DS.GetTarget(mimeSource, valueProperty, true);
michael@0 675 if (currentValue) {
michael@0 676 this._DS.Change(mimeSource, valueProperty, currentValue, mimeLiteral);
michael@0 677 } else {
michael@0 678 this._DS.Assert(mimeSource, valueProperty, mimeLiteral, true);
michael@0 679 }
michael@0 680 },
michael@0 681
michael@0 682 unassertMIMEStuff: function(aMIMEString, aPropertyString, aValueString)
michael@0 683 {
michael@0 684 var mimeSource = gRDF.GetUnicodeResource(aMIMEString);
michael@0 685 var valueProperty = gRDF.GetUnicodeResource(NC_URI(aPropertyString));
michael@0 686 var mimeLiteral = gRDF.GetLiteral(aValueString);
michael@0 687 this._DS.Unassert(mimeSource, valueProperty, mimeLiteral, true);
michael@0 688 },
michael@0 689
michael@0 690 /**
michael@0 691 * Get the list of types for the given class, creating the list if it doesn't
michael@0 692 * already exist. The class can be either CLASS_MIMEINFO or CLASS_PROTOCOLINFO
michael@0 693 * (i.e. the result of a call to _getClass).
michael@0 694 *
michael@0 695 * |urn:<class>s|
michael@0 696 * |urn:<class>s:root|
michael@0 697 *
michael@0 698 * @param aClass {string} the class for which to retrieve a list of types
michael@0 699 *
michael@0 700 * @returns {nsIRDFContainer} the list of types
michael@0 701 */
michael@0 702 ensureAndGetTypeList: function (aClass) {
michael@0 703 var source = gRDF.GetResource("urn:" + aClass + "s");
michael@0 704 var property =
michael@0 705 gRDF.GetResource(aClass == CLASS_MIMEINFO ? NC_MIME_TYPES
michael@0 706 : NC_PROTOCOL_SCHEMES);
michael@0 707 var target = gRDF.GetResource("urn:" + aClass + "s:root");
michael@0 708
michael@0 709 // Make sure we have an arc from the source to the target.
michael@0 710 if (!this._DS.HasAssertion(source, property, target, true))
michael@0 711 this._DS.Assert(source, property, target, true);
michael@0 712
michael@0 713 // Make sure the target is a container.
michael@0 714 var containerUtils = Cc["@mozilla.org/rdf/container-utils;1"]
michael@0 715 .getService(Ci.nsIRDFContainerUtils);
michael@0 716 if (!containerUtils.IsContainer(this._DS, target))
michael@0 717 containerUtils.MakeSeq(this._DS, target);
michael@0 718
michael@0 719 // Get the type list as an RDF container.
michael@0 720 var typeList =
michael@0 721 Cc["@mozilla.org/rdf/container;1"].createInstance(Ci.nsIRDFContainer);
michael@0 722 typeList.Init(this._DS, target);
michael@0 723
michael@0 724 return typeList;
michael@0 725 }
michael@0 726 };
michael@0 727

mercurial