michael@0: // -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: var Cc = Components.classes; michael@0: var Ci = Components.interfaces; michael@0: michael@0: var gRDF; michael@0: michael@0: const CLASS_MIMEINFO = "mimetype"; michael@0: const CLASS_PROTOCOLINFO = "scheme"; michael@0: michael@0: // namespace prefix michael@0: const NC_NS = "http://home.netscape.com/NC-rdf#"; michael@0: michael@0: // type list properties michael@0: michael@0: const NC_MIME_TYPES = NC_NS + "MIME-types"; michael@0: const NC_PROTOCOL_SCHEMES = NC_NS + "Protocol-Schemes"; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // MIME Types DataSource Wrapper michael@0: michael@0: function NC_URI(aProperty) michael@0: { michael@0: return "http://home.netscape.com/NC-rdf#" + aProperty; michael@0: } michael@0: michael@0: function MIME_URI(aType) michael@0: { michael@0: return "urn:mimetype:" + aType; michael@0: } michael@0: michael@0: function HANDLER_URI(aHandler) michael@0: { michael@0: return "urn:mimetype:handler:" + aHandler; michael@0: } michael@0: michael@0: function APP_URI(aType) michael@0: { michael@0: return "urn:mimetype:externalApplication:" + aType; michael@0: } michael@0: michael@0: function ArrayEnumerator(aItems) { michael@0: if (aItems) { michael@0: for (var i = 0; i < aItems.length; ++i) { michael@0: if (!aItems[i]) michael@0: aItems.splice(i--, 1); michael@0: } michael@0: this._contents = aItems; michael@0: } else { michael@0: this._contents = []; michael@0: } michael@0: } michael@0: michael@0: ArrayEnumerator.prototype = { michael@0: _index: 0, michael@0: michael@0: hasMoreElements: function () { michael@0: return this._index < this._contents.length; michael@0: }, michael@0: michael@0: getNext: function () { michael@0: return this._contents[this._index++]; michael@0: }, michael@0: michael@0: push: function (aElement) { michael@0: if (aElement) michael@0: this._contents.push(aElement); michael@0: } michael@0: }; michael@0: michael@0: function HelperApps() michael@0: { michael@0: if (!gRDF) michael@0: gRDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService); michael@0: michael@0: const mimeTypes = "UMimTyp"; michael@0: var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties); michael@0: michael@0: var file = fileLocator.get(mimeTypes, Components.interfaces.nsIFile); michael@0: michael@0: var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService); michael@0: var fileHandler = ioService.getProtocolHandler("file").QueryInterface(Components.interfaces.nsIFileProtocolHandler); michael@0: this._inner = gRDF.GetDataSourceBlocking(fileHandler.getURLSpecFromFile(file)); michael@0: this._inner.AddObserver(this); michael@0: michael@0: this._fileTypeArc = gRDF.GetResource(NC_URI("FileType")); michael@0: this._fileHandlerArc = gRDF.GetResource(NC_URI("FileHandler")); michael@0: this._fileIconArc = gRDF.GetResource(NC_URI("FileIcon")); michael@0: this._fileExtensionArc = gRDF.GetResource(NC_URI("FileExtension")); michael@0: this._fileExtensionsArc = gRDF.GetResource(NC_URI("FileExtensions")); michael@0: this._handleAutoArc = gRDF.GetResource(NC_URI("FileHandleAuto")); michael@0: this._valueArc = gRDF.GetResource(NC_URI("value")); michael@0: this._handlerPropArc = gRDF.GetResource(NC_URI("handlerProp")); michael@0: this._externalAppArc = gRDF.GetResource(NC_URI("externalApplication")); michael@0: } michael@0: michael@0: HelperApps.prototype = { michael@0: mimeHandlerExists: function (aMIMEType) michael@0: { michael@0: var valueProperty = gRDF.GetUnicodeResource(NC_URI("value")); michael@0: var mimeSource = gRDF.GetUnicodeResource(MIME_URI(aMIMEType)); michael@0: var mimeLiteral = gRDF.GetLiteral(aMIMEType); michael@0: return this._inner.HasAssertion(mimeSource, valueProperty, mimeLiteral, true); michael@0: }, michael@0: michael@0: updateTypeInfo: function (aMIMEInfo) michael@0: { michael@0: var mimeType = aMIMEInfo.MIMEType; michael@0: var isNewMIMEType = this.mimeHandlerExists(mimeType); michael@0: var entry = new HandlerOverride(MIME_URI(mimeType), this._inner); michael@0: entry.mimeType = mimeType; michael@0: entry.isEditable = true; michael@0: entry.alwaysAsk = aMIMEInfo.alwaysAskBeforeHandling; michael@0: michael@0: // If not updating (i.e., a newly encountered mime type), michael@0: // then update extension list and description. michael@0: if (!isNewMIMEType) { michael@0: var extEnumerator = aMIMEInfo.getFileExtensions(); michael@0: while (extEnumerator.hasMore()) { michael@0: entry.addExtension(extEnumerator.getNext()); michael@0: } michael@0: entry.description = aMIMEInfo.description; michael@0: entry.appDisplayName = ""; michael@0: } michael@0: michael@0: const nsIMIMEInfo = Components.interfaces.nsIMIMEInfo; michael@0: if (aMIMEInfo.preferredAction == nsIMIMEInfo.saveToDisk) { michael@0: entry.saveToDisk = true; michael@0: if (!isNewMIMEType) { michael@0: // Creating a new entry, set path. michael@0: entry.appPath = ""; michael@0: } michael@0: } michael@0: else if (aMIMEInfo.preferredAction == nsIMIMEInfo.useSystemDefault || michael@0: aMIMEInfo.preferredApplicationHandler == null) { michael@0: entry.useSystemDefault = true; michael@0: if (!isNewMIMEType) { michael@0: // Creating a new entry, set path. michael@0: entry.appPath = ""; michael@0: } michael@0: } michael@0: else if (aMIMEInfo.preferredApplicationHandler instanceof Components.interfaces.nsILocalHandlerApp) { michael@0: entry.saveToDisk = false; michael@0: entry.useSystemDefault = false; michael@0: entry.handleInternal = false; michael@0: entry.appPath = aMIMEInfo.preferredApplicationHandler.executable.path; michael@0: entry.appDisplayName = aMIMEInfo.preferredApplicationHandler.name; michael@0: } michael@0: michael@0: // Do RDF magic. michael@0: entry.buildLinks(); michael@0: this.flush(); michael@0: }, michael@0: michael@0: getLiteralValue: function (aResource, aProperty) michael@0: { michael@0: var res = gRDF.GetResource(aResource); michael@0: var prop = gRDF.GetResource(NC_URI(aProperty)); michael@0: var val = this.GetTarget(res, prop, true); michael@0: if (val) { michael@0: val = val.QueryInterface(Components.interfaces.nsIRDFLiteral); michael@0: return val.Value; michael@0: } michael@0: return ""; michael@0: }, michael@0: michael@0: /* nsIRDFDataSource */ michael@0: get URI() { michael@0: return this._inner.URI; michael@0: }, michael@0: michael@0: GetSource: function (aProperty, aTarget, aTruthValue) { michael@0: return this._inner.GetSource(aProperty, aTarget, aTruthValue); michael@0: }, michael@0: GetSources: function (aProperty, aTarget, aTruthValue) { michael@0: return this._inner.GetSources(aProperty, aTarget, aTruthValue); michael@0: }, michael@0: michael@0: _isRootTypeResource: function (aResource) { michael@0: aResource = aResource.QueryInterface(Components.interfaces.nsIRDFResource); michael@0: const kRootTypePrefix = "urn:mimetype:"; michael@0: return (aResource.Value.substr(0, kRootTypePrefix.length) == kRootTypePrefix); michael@0: }, michael@0: michael@0: getMIMEInfo: function (aResource) { michael@0: var types = this._inner.GetTarget(aResource, this._valueArc, true); michael@0: if (types) { michael@0: types = types.QueryInterface(Components.interfaces.nsIRDFLiteral); michael@0: types = types.Value.split(", "); michael@0: michael@0: mimeSvc = Components.classes["@mozilla.org/mime;1"].getService(Components.interfaces.nsIMIMEService); michael@0: return mimeSvc.getFromTypeAndExtension(types[0], null); michael@0: } michael@0: michael@0: return null; michael@0: }, michael@0: michael@0: GetTarget: function (aSource, aProperty, aTruthValue) { michael@0: if (this._isRootTypeResource(aSource)) { michael@0: var typeInfo = this.getMIMEInfo(aSource); michael@0: if (typeInfo) { michael@0: var bundle = document.getElementById("strings"); michael@0: if (aProperty.EqualsNode(this._handleAutoArc)) { michael@0: var handler = this.GetTarget(aSource, this._handlerPropArc, true); michael@0: if (handler) { michael@0: handler = handler.QueryInterface(Components.interfaces.nsIRDFResource); michael@0: return gRDF.GetLiteral(!(this.getLiteralValue(handler.Value, "alwaysAsk") == "true")); michael@0: } michael@0: } michael@0: else if (aProperty.EqualsNode(this._fileTypeArc)) { michael@0: if (typeInfo.description == "") { michael@0: try { michael@0: var literal = bundle.getFormattedString("fileEnding", [typeInfo.primaryExtension.toUpperCase()]); michael@0: return gRDF.GetLiteral(literal); michael@0: } michael@0: catch (e) { michael@0: // Wow, this sucks, just show the MIME type as a last ditch effort to display michael@0: // the type of file that this is. michael@0: return gRDF.GetLiteral(typeInfo.MIMEType); michael@0: } michael@0: } michael@0: return gRDF.GetLiteral(typeInfo.description); michael@0: } michael@0: else if (aProperty.EqualsNode(this._fileHandlerArc)) { michael@0: var handler = this.GetTarget(aSource, this._handlerPropArc, true); michael@0: if (handler) { michael@0: handler = handler.QueryInterface(Components.interfaces.nsIRDFResource); michael@0: if (this.getLiteralValue(handler.Value, "saveToDisk") == "true") { michael@0: var saveToDisk = bundle.getString("saveToDisk"); michael@0: return gRDF.GetLiteral(saveToDisk); michael@0: } michael@0: else if (this.getLiteralValue(handler.Value, "useSystemDefault") == "false") { michael@0: var extApp = this.GetTarget(handler, this._externalAppArc, true); michael@0: if (extApp) { michael@0: extApp = extApp.QueryInterface(Components.interfaces.nsIRDFResource); michael@0: var openWith = bundle.getFormattedString("openWith", [this.getLiteralValue(extApp.Value, "prettyName")]); michael@0: return gRDF.GetLiteral(openWith); michael@0: } michael@0: } michael@0: } michael@0: michael@0: var openWith2 = bundle.getFormattedString("openWith", [typeInfo.defaultDescription]); michael@0: return gRDF.GetLiteral(openWith2); michael@0: } michael@0: else if (aProperty.EqualsNode(this._fileIconArc)) { michael@0: try { michael@0: return gRDF.GetLiteral("moz-icon://goat." + typeInfo.primaryExtension + "?size=16"); michael@0: } michael@0: catch (e) { michael@0: return gRDF.GetLiteral("moz-icon://goat?size=16&contentType=" + typeInfo.MIMEType); michael@0: } michael@0: } michael@0: else if (aProperty.EqualsNode(this._fileExtensionArc)) { michael@0: try { michael@0: return gRDF.GetLiteral(typeInfo.primaryExtension.toUpperCase()); michael@0: } michael@0: catch (e) { } michael@0: return gRDF.GetLiteral(""); michael@0: } michael@0: else if (aProperty.EqualsNode(this._fileExtensionsArc)) { michael@0: var extns = typeInfo.getFileExtensions(); michael@0: michael@0: // Prevent duplicates. michael@0: var hash = { }; michael@0: while (extns.hasMore()) michael@0: hash[extns.getNext().toUpperCase()] = 0; michael@0: michael@0: var str = ""; michael@0: for (var extn in hash) michael@0: str += extn + ","; michael@0: str = str.substring(0, str.length - 1); michael@0: michael@0: return gRDF.GetLiteral(str); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return this._inner.GetTarget(aSource, aProperty, aTruthValue); michael@0: }, michael@0: michael@0: GetTargets: function (aSource, aProperty, aTruthValue) { michael@0: if (this._isRootTypeResource(aSource)) { michael@0: return new ArrayEnumerator([this.GetTarget(aSource, aProperty, aTruthValue)]); michael@0: } michael@0: michael@0: return this._inner.GetTargets(aSource, aProperty, aTruthValue); michael@0: }, michael@0: Assert: function (aSource, aProperty, aTarget, aTruthValue) { michael@0: return this._inner.Assert(aSource, aProperty, aTarget, aTruthValue); michael@0: }, michael@0: Unassert: function (aSource, aProperty, aTarget) { michael@0: return this._inner.Unassert(aSource, aProperty, aTarget); michael@0: }, michael@0: Change: function (aSource, aProperty, aOldTarget, aNewTarget) { michael@0: if (aOldTarget) michael@0: var ot = aOldTarget.QueryInterface(Components.interfaces.nsIRDFLiteral); michael@0: if (aNewTarget) michael@0: var nt = aNewTarget.QueryInterface(Components.interfaces.nsIRDFLiteral); michael@0: michael@0: return this._inner.Change(aSource, aProperty, aOldTarget, aNewTarget); michael@0: }, michael@0: Move: function (aOldSource, aNewSource, aProperty, aTarget) { michael@0: return this._inner.Assert(aOldSource, aNewSource, aProperty, aTarget); michael@0: }, michael@0: HasAssertion: function (aSource, aProperty, aTarget, aTruthValue) { michael@0: if (this._isRootTypeResource(aSource)) { michael@0: // Don't show entries in the list for types that we DO NOT handle michael@0: // automatically. i.e. this list is a means of editing and removing michael@0: // automatic overrides only. michael@0: if (aProperty.EqualsNode(this._handleAutoArc)) { michael@0: var handler = this.GetTarget(aSource, this._handlerPropArc, true); michael@0: if (handler) { michael@0: handler = handler.QueryInterface(Components.interfaces.nsIRDFResource); michael@0: return !(this.getLiteralValue(handler.Value, "alwaysAsk") == "true"); michael@0: } michael@0: } michael@0: } michael@0: return this._inner.HasAssertion(aSource, aProperty, aTarget, aTruthValue); michael@0: }, michael@0: ArcLabelsIn: function (aNode) { michael@0: return this._inner.ArcLabelsIn(aNode); michael@0: }, michael@0: ArcLabelsOut: function (aNode) { michael@0: return this._inner.ArcLabelsOut(aNode); michael@0: }, michael@0: GetAllResources: function () { michael@0: return this._inner.GetAllResources(); michael@0: }, michael@0: hasArcIn: function (aNode, aArc) { michael@0: return this._inner.hasArcIn(aNode, aArc); michael@0: }, michael@0: hasArcOut: function (aNode, aArc) { michael@0: return this._inner.hasArcOut(aNode, aArc); michael@0: }, michael@0: michael@0: _observers: [], michael@0: AddObserver: function (aObserver) { michael@0: this._observers.push(aObserver); michael@0: }, michael@0: michael@0: RemoveObserver: function (aObserver) { michael@0: for (var i = 0; i < this._observers.length; ++i) { michael@0: if (this._observers[i] == aObserver) { michael@0: this._observers.splice(i, 1); michael@0: break; michael@0: } michael@0: } michael@0: }, michael@0: michael@0: onAssert: function (aDataSource, aSource, aProperty, aTarget) { michael@0: for (var i = 0; i < this._observers.length; ++i) { michael@0: this._observers[i].onAssert(aDataSource, aSource, aProperty, aTarget); michael@0: } michael@0: }, michael@0: michael@0: onUnassert: function (aDataSource, aSource, aProperty, aTarget) { michael@0: for (var i = 0; i < this._observers.length; ++i) { michael@0: this._observers[i].onUnassert(aDataSource, aSource, aProperty, aTarget); michael@0: } michael@0: }, michael@0: michael@0: onChange: function (aDataSource, aSource, aProperty, aOldTarget, aNewTarget) { michael@0: for (var i = 0; i < this._observers.length; ++i) { michael@0: this._observers[i].onChange(aDataSource, aSource, aProperty, aOldTarget, aNewTarget); michael@0: } michael@0: }, michael@0: michael@0: onMove: function (aDataSource, aOldSource, aNewSource, aProperty, aTarget) { michael@0: for (var i = 0; i < this._observers.length; ++i) { michael@0: this._observers[i].onMove(aDataSource, aOldSource, aNewSource, aProperty, aTarget); michael@0: } michael@0: }, michael@0: michael@0: onBeginUpdateBatch: function (aDataSource) { michael@0: for (var i = 0; i < this._observers.length; ++i) { michael@0: this._observers[i].onBeginUpdateBatch(aDataSource); michael@0: } michael@0: }, michael@0: michael@0: onEndUpdateBatch: function (aDataSource) { michael@0: for (var i = 0; i < this._observers.length; ++i) { michael@0: this._observers[i].onEndUpdateBatch(aDataSource); michael@0: } michael@0: }, michael@0: michael@0: beginUpdateBatch: function (aDataSource) { michael@0: for (var i = 0; i < this._observers.length; ++i) { michael@0: this._observers[i].beginUpdateBatch(aDataSource); michael@0: } michael@0: }, michael@0: michael@0: endUpdateBatch: function (aDataSource) { michael@0: for (var i = 0; i < this._observers.length; ++i) { michael@0: this._observers[i].endUpdateBatch(aDataSource); michael@0: } michael@0: }, michael@0: michael@0: flush: function () { michael@0: var rds = this._inner.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource); michael@0: if (rds) michael@0: rds.Flush(); michael@0: }, michael@0: michael@0: destroy: function () { michael@0: this._inner.RemoveObserver(this); michael@0: } michael@0: }; michael@0: michael@0: /** michael@0: * Handler Override class michael@0: **/ michael@0: function HandlerOverride(aURI, aDatasource) michael@0: { michael@0: this.URI = aURI; michael@0: this._DS = aDatasource; michael@0: } michael@0: michael@0: HandlerOverride.prototype = { michael@0: // general information michael@0: get mimeType() michael@0: { michael@0: return this.getLiteralForContentType(this.URI, "value"); michael@0: }, michael@0: michael@0: set mimeType(aMIMETypeString) michael@0: { michael@0: this.changeMIMEStuff(MIME_URI(aMIMETypeString), "value", aMIMETypeString.toLowerCase()); michael@0: return aMIMETypeString; michael@0: }, michael@0: michael@0: get description() michael@0: { michael@0: return this.getLiteralForContentType(this.URI, "description"); michael@0: }, michael@0: michael@0: set description(aDescriptionString) michael@0: { michael@0: this.changeMIMEStuff(MIME_URI(this.mimeType), "description", aDescriptionString); michael@0: return aDescriptionString; michael@0: }, michael@0: michael@0: get isEditable() michael@0: { michael@0: return this.getLiteralForContentType(this.URI, "editable"); michael@0: }, michael@0: michael@0: set isEditable(aIsEditableString) michael@0: { michael@0: this.changeMIMEStuff(MIME_URI(this.mimeType), "editable", aIsEditableString); michael@0: return aIsEditableString; michael@0: }, michael@0: michael@0: get extensions() michael@0: { michael@0: var extensionResource = gRDF.GetUnicodeResource(NC_URI("fileExtensions")); michael@0: var contentTypeResource = gRDF.GetUnicodeResource(MIME_URI(this.mimeType)); michael@0: var extensionTargets = this._DS.GetTargets(contentTypeResource, extensionResource, true); michael@0: var extString = ""; michael@0: if (extensionTargets) { michael@0: while (extensionTargets.hasMoreElements()) { michael@0: var currentExtension = extensionTargets.getNext(); michael@0: if (currentExtension) { michael@0: currentExtension = currentExtension.QueryInterface(Components.interfaces.nsIRDFLiteral); michael@0: if (extString != "") { michael@0: extString += " "; michael@0: } michael@0: extString += currentExtension.Value.toLowerCase(); michael@0: } michael@0: } michael@0: } michael@0: return extString; michael@0: }, michael@0: michael@0: addExtension: function (aExtensionString) michael@0: { michael@0: this.assertMIMEStuff(MIME_URI(this.mimeType), "fileExtensions", aExtensionString.toLowerCase()); michael@0: }, michael@0: michael@0: removeExtension: function (aExtensionString) michael@0: { michael@0: this.unassertMIMEStuff(MIME_URI(this.mimeType), "fileExtensions", aExtensionString.toLowerCase()); michael@0: }, michael@0: michael@0: clearExtensions: function () michael@0: { michael@0: var extArray = this.extensions.split(" "); michael@0: for (i = extArray.length - 1; i >= 0; --i) { michael@0: this.removeExtension(extArray[i]); michael@0: } michael@0: }, michael@0: michael@0: // content handling michael@0: get saveToDisk() michael@0: { michael@0: return this.getHandlerInfoForType(this.URI, "saveToDisk"); michael@0: }, michael@0: michael@0: set saveToDisk(aSavedToDisk) michael@0: { michael@0: this.changeMIMEStuff(HANDLER_URI(this.mimeType), "saveToDisk", aSavedToDisk); michael@0: this.setHandlerProcedure("handleInternal", "false"); michael@0: this.setHandlerProcedure("useSystemDefault", "false"); michael@0: return aSavedToDisk; michael@0: }, michael@0: michael@0: get useSystemDefault() michael@0: { michael@0: return this.getHandlerInfoForType(this.URI, "useSystemDefault"); michael@0: }, michael@0: michael@0: set useSystemDefault(aUseSystemDefault) michael@0: { michael@0: this.changeMIMEStuff(HANDLER_URI(this.mimeType), "useSystemDefault", aUseSystemDefault); michael@0: this.setHandlerProcedure("handleInternal", "false"); michael@0: this.setHandlerProcedure("saveToDisk", "false"); michael@0: return aUseSystemDefault; michael@0: }, michael@0: michael@0: get handleInternal() michael@0: { michael@0: return this.getHandlerInfoForType(this.URI, "handleInternal"); michael@0: }, michael@0: michael@0: set handleInternal(aHandledInternally) michael@0: { michael@0: this.changeMIMEStuff(HANDLER_URI(this.mimeType), "handleInternal", aHandledInternally); michael@0: this.setHandlerProcedure("saveToDisk", "false"); michael@0: this.setHandlerProcedure("useSystemDefault", "false"); michael@0: return aHandledInternally; michael@0: }, michael@0: michael@0: setHandlerProcedure: function (aHandlerProcedure, aValue) michael@0: { michael@0: var handlerSource = gRDF.GetUnicodeResource(HANDLER_URI(this.mimeType)); michael@0: var handlerProperty = gRDF.GetUnicodeResource(NC_URI(aHandlerProcedure)); michael@0: var oppositeValue = aValue == "false" ? "true" : "false"; michael@0: var trueLiteral = gRDF.GetLiteral(oppositeValue); michael@0: var hasCounterpart = this._DS.HasAssertion(handlerSource, handlerProperty, trueLiteral, true); michael@0: if (hasCounterpart) { michael@0: var falseLiteral = gRDF.GetLiteral(aValue); michael@0: this._DS.Change(handlerSource, handlerProperty, trueLiteral, falseLiteral); michael@0: } michael@0: }, michael@0: michael@0: get alwaysAsk() michael@0: { michael@0: return this.getHandlerInfoForType(this.URI, "alwaysAsk"); michael@0: }, michael@0: michael@0: set alwaysAsk(aAlwaysAsk) michael@0: { michael@0: this.changeMIMEStuff(HANDLER_URI(this.mimeType), "alwaysAsk", aAlwaysAsk); michael@0: return aAlwaysAsk; michael@0: }, michael@0: michael@0: // helper application michael@0: get appDisplayName() michael@0: { michael@0: return getHelperAppInfoForType(this.URI, "prettyName"); michael@0: }, michael@0: michael@0: set appDisplayName(aDisplayName) michael@0: { michael@0: if (aDisplayName) michael@0: this.changeMIMEStuff(APP_URI(this.mimeType), "prettyName", aDisplayName); michael@0: else { michael@0: var currentValue = this.getLiteralForContentType(APP_URI(this.mimeType), "prettyName"); michael@0: this.unassertMIMEStuff(APP_URI(this.mimeType), "prettyName", currentValue); michael@0: } michael@0: michael@0: return aDisplayName; michael@0: }, michael@0: michael@0: get appPath() michael@0: { michael@0: return this.getHelperAppInfoForType(this.URI, "path"); michael@0: }, michael@0: michael@0: set appPath(aAppPath) michael@0: { michael@0: if (aAppPath) michael@0: this.changeMIMEStuff(APP_URI(this.mimeType), "path", aAppPath); michael@0: else { michael@0: var currentValue = this.getLiteralForContentType(APP_URI(this.mimeType), "path"); michael@0: this.unassertMIMEStuff(APP_URI(this.mimeType), "path", currentValue); michael@0: } michael@0: michael@0: return aAppPath; michael@0: }, michael@0: michael@0: /** michael@0: * After setting the various properties on this override, we need to michael@0: * build the links between the mime type resource, the handler for that michael@0: * resource, and the helper app (if any) associated with the resource. michael@0: * We also need to add this mime type to the RDF seq (list) of types. michael@0: **/ michael@0: buildLinks: function() michael@0: { michael@0: // assert the handler resource michael@0: var mimeSource = gRDF.GetUnicodeResource(MIME_URI(this.mimeType)); michael@0: var handlerProperty = gRDF.GetUnicodeResource(NC_URI("handlerProp")); michael@0: var handlerResource = gRDF.GetUnicodeResource(HANDLER_URI(this.mimeType)); michael@0: this._DS.Assert(mimeSource, handlerProperty, handlerResource, true); michael@0: // assert the helper app resource michael@0: var helperAppProperty = gRDF.GetUnicodeResource(NC_URI("externalApplication")); michael@0: var helperAppResource = gRDF.GetUnicodeResource(APP_URI(this.mimeType)); michael@0: this._DS.Assert(handlerResource, helperAppProperty, helperAppResource, true); michael@0: // add the mime type to the MIME types seq michael@0: var container = this.ensureAndGetTypeList("mimetype"); michael@0: var element = gRDF.GetUnicodeResource(MIME_URI(this.mimeType)); michael@0: if (container.IndexOf(element) == -1) michael@0: container.AppendElement(element); michael@0: }, michael@0: michael@0: // Implementation helper methods michael@0: michael@0: getLiteralForContentType: function (aURI, aProperty) michael@0: { michael@0: var contentTypeResource = gRDF.GetUnicodeResource(aURI); michael@0: var propertyResource = gRDF.GetUnicodeResource(NC_URI(aProperty)); michael@0: return this.getLiteral(contentTypeResource, propertyResource); michael@0: }, michael@0: michael@0: getLiteral: function (aSource, aProperty) michael@0: { michael@0: var node = this._DS.GetTarget(aSource, aProperty, true); michael@0: if (node) { michael@0: node = node.QueryInterface(Components.interfaces.nsIRDFLiteral); michael@0: return node.Value; michael@0: } michael@0: return ""; michael@0: }, michael@0: michael@0: getHandlerInfoForType: function (aURI, aPropertyString) michael@0: { michael@0: // get current selected type michael@0: var handler = HANDLER_URI(this.getLiteralForContentType(aURI, "value")); michael@0: var source = gRDF.GetUnicodeResource(handler); michael@0: var property = gRDF.GetUnicodeResource(NC_URI(aPropertyString)); michael@0: var target = this._DS.GetTarget(source, property, true); michael@0: if (target) { michael@0: target = target.QueryInterface(Components.interfaces.nsIRDFLiteral); michael@0: return target.Value; michael@0: } michael@0: return ""; michael@0: }, michael@0: michael@0: getHelperAppInfoForType: function (aURI, aPropertyString) michael@0: { michael@0: var appURI = APP_URI(this.getLiteralForContentType(aURI, "value")); michael@0: var appRes = gRDF.GetUnicodeResource(appURI); michael@0: var appProperty = gRDF.GetUnicodeResource(NC_URI(aPropertyString)); michael@0: return getLiteral(appRes, appProperty); michael@0: }, michael@0: michael@0: // write to the ds michael@0: assertMIMEStuff: function (aMIMEString, aPropertyString, aValueString) michael@0: { michael@0: var mimeSource = gRDF.GetUnicodeResource(aMIMEString); michael@0: var valueProperty = gRDF.GetUnicodeResource(NC_URI(aPropertyString)); michael@0: var mimeLiteral = gRDF.GetLiteral(aValueString); michael@0: this._DS.Assert(mimeSource, valueProperty, mimeLiteral, true); michael@0: }, michael@0: michael@0: changeMIMEStuff: function(aMIMEString, aPropertyString, aValueString) michael@0: { michael@0: var mimeSource = gRDF.GetUnicodeResource(aMIMEString); michael@0: var valueProperty = gRDF.GetUnicodeResource(NC_URI(aPropertyString)); michael@0: var mimeLiteral = gRDF.GetLiteral(aValueString); michael@0: var currentValue = this._DS.GetTarget(mimeSource, valueProperty, true); michael@0: if (currentValue) { michael@0: this._DS.Change(mimeSource, valueProperty, currentValue, mimeLiteral); michael@0: } else { michael@0: this._DS.Assert(mimeSource, valueProperty, mimeLiteral, true); michael@0: } michael@0: }, michael@0: michael@0: unassertMIMEStuff: function(aMIMEString, aPropertyString, aValueString) michael@0: { michael@0: var mimeSource = gRDF.GetUnicodeResource(aMIMEString); michael@0: var valueProperty = gRDF.GetUnicodeResource(NC_URI(aPropertyString)); michael@0: var mimeLiteral = gRDF.GetLiteral(aValueString); michael@0: this._DS.Unassert(mimeSource, valueProperty, mimeLiteral, true); michael@0: }, michael@0: michael@0: /** michael@0: * Get the list of types for the given class, creating the list if it doesn't michael@0: * already exist. The class can be either CLASS_MIMEINFO or CLASS_PROTOCOLINFO michael@0: * (i.e. the result of a call to _getClass). michael@0: * michael@0: * |urn:s| michael@0: * |urn:s:root| michael@0: * michael@0: * @param aClass {string} the class for which to retrieve a list of types michael@0: * michael@0: * @returns {nsIRDFContainer} the list of types michael@0: */ michael@0: ensureAndGetTypeList: function (aClass) { michael@0: var source = gRDF.GetResource("urn:" + aClass + "s"); michael@0: var property = michael@0: gRDF.GetResource(aClass == CLASS_MIMEINFO ? NC_MIME_TYPES michael@0: : NC_PROTOCOL_SCHEMES); michael@0: var target = gRDF.GetResource("urn:" + aClass + "s:root"); michael@0: michael@0: // Make sure we have an arc from the source to the target. michael@0: if (!this._DS.HasAssertion(source, property, target, true)) michael@0: this._DS.Assert(source, property, target, true); michael@0: michael@0: // Make sure the target is a container. michael@0: var containerUtils = Cc["@mozilla.org/rdf/container-utils;1"] michael@0: .getService(Ci.nsIRDFContainerUtils); michael@0: if (!containerUtils.IsContainer(this._DS, target)) michael@0: containerUtils.MakeSeq(this._DS, target); michael@0: michael@0: // Get the type list as an RDF container. michael@0: var typeList = michael@0: Cc["@mozilla.org/rdf/container;1"].createInstance(Ci.nsIRDFContainer); michael@0: typeList.Init(this._DS, target); michael@0: michael@0: return typeList; michael@0: } michael@0: }; michael@0: