2006-05-17 02:26:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Datasource initialization
|
|
|
|
**/
|
|
|
|
|
2006-05-17 02:28:05 +00:00
|
|
|
var gRDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIRDFService);
|
2006-05-17 02:26:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler Override class
|
|
|
|
**/
|
|
|
|
function HandlerOverride(aURI)
|
|
|
|
{
|
|
|
|
this.URI = aURI;
|
2006-05-17 02:26:47 +00:00
|
|
|
this.mUpdateMode = false;
|
2006-05-17 02:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HandlerOverride.prototype = {
|
|
|
|
// general information
|
|
|
|
get mimeType()
|
|
|
|
{
|
|
|
|
return getLiteralForContentType(this.URI, "value");
|
|
|
|
},
|
|
|
|
|
|
|
|
set mimeType(aMIMETypeString)
|
|
|
|
{
|
2006-05-17 02:36:29 +00:00
|
|
|
changeMIMEStuff(MIME_URI(aMIMETypeString), "value", aMIMETypeString.toLowerCase());
|
2006-05-17 02:26:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get description()
|
|
|
|
{
|
|
|
|
return getLiteralForContentType(this.URI, "description");
|
|
|
|
},
|
|
|
|
|
|
|
|
set description(aDescriptionString)
|
|
|
|
{
|
2006-05-17 02:36:29 +00:00
|
|
|
changeMIMEStuff(MIME_URI(this.mimeType), "description", aDescriptionString);
|
2006-05-17 02:26:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get isEditable()
|
|
|
|
{
|
|
|
|
return getLiteralForContentType(this.URI, "editable");
|
|
|
|
},
|
|
|
|
|
|
|
|
set isEditable(aIsEditableString)
|
|
|
|
{
|
2006-05-17 02:36:29 +00:00
|
|
|
changeMIMEStuff(MIME_URI(this.mimeType), "editable", aIsEditableString);
|
2006-05-17 02:26:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get extensions()
|
|
|
|
{
|
2006-05-17 02:36:31 +00:00
|
|
|
var extensionResource = gRDF.GetUnicodeResource(NC_RDF("fileExtensions"));
|
|
|
|
var contentTypeResource = gRDF.GetUnicodeResource(MIME_URI(this.mimeType));
|
2006-05-17 02:36:29 +00:00
|
|
|
var extensionTargets = gDS.GetTargets(contentTypeResource, extensionResource, true);
|
2006-05-17 02:26:44 +00:00
|
|
|
var extString = "";
|
|
|
|
if (extensionTargets) {
|
|
|
|
while (extensionTargets.hasMoreElements()) {
|
|
|
|
var currentExtension = extensionTargets.getNext();
|
|
|
|
if (currentExtension) {
|
|
|
|
currentExtension = currentExtension.QueryInterface(Components.interfaces.nsIRDFLiteral);
|
2006-05-17 02:36:05 +00:00
|
|
|
if (extString != "") {
|
|
|
|
extString += " ";
|
|
|
|
}
|
|
|
|
extString += currentExtension.Value.toLowerCase();
|
2006-05-17 02:26:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return extString;
|
|
|
|
},
|
|
|
|
|
|
|
|
addExtension: function (aExtensionString)
|
|
|
|
{
|
2006-05-17 02:33:02 +00:00
|
|
|
assertMIMEStuff(MIME_URI(this.mimeType), "fileExtensions", aExtensionString.toLowerCase());
|
2006-05-17 02:26:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
removeExtension: function (aExtensionString)
|
|
|
|
{
|
2006-05-17 02:33:02 +00:00
|
|
|
unassertMIMEStuff(MIME_URI(this.mimeType), "fileExtensions", aExtensionString.toLowerCase());
|
2006-05-17 02:26:44 +00:00
|
|
|
},
|
2006-05-17 02:36:29 +00:00
|
|
|
|
|
|
|
clearExtensions: function ()
|
|
|
|
{
|
|
|
|
var extArray = this.extensions.split(" ");
|
|
|
|
for (i = extArray.length - 1; i >= 0; --i) {
|
|
|
|
this.removeExtension(extArray[i]);
|
|
|
|
}
|
|
|
|
},
|
2006-05-17 02:26:44 +00:00
|
|
|
|
|
|
|
// content handling
|
|
|
|
get saveToDisk()
|
|
|
|
{
|
|
|
|
return getHandlerInfoForType(this.URI, "saveToDisk");
|
|
|
|
},
|
|
|
|
|
|
|
|
set saveToDisk(aSavedToDisk)
|
|
|
|
{
|
2006-05-17 02:36:29 +00:00
|
|
|
changeMIMEStuff(HANDLER_URI(this.mimeType), "saveToDisk", aSavedToDisk);
|
2006-05-17 02:26:47 +00:00
|
|
|
this.setHandlerProcedure("handleInternal", "false");
|
2006-05-17 02:35:59 +00:00
|
|
|
this.setHandlerProcedure("useSystemDefault", "false");
|
2006-05-17 02:26:47 +00:00
|
|
|
},
|
2006-05-17 02:35:59 +00:00
|
|
|
|
|
|
|
get useSystemDefault()
|
|
|
|
{
|
|
|
|
return getHandlerInfoForType(this.URI, "useSystemDefault");
|
|
|
|
},
|
|
|
|
|
|
|
|
set useSystemDefault(aUseSystemDefault)
|
|
|
|
{
|
2006-05-17 02:36:29 +00:00
|
|
|
changeMIMEStuff(HANDLER_URI(this.mimeType), "useSystemDefault", aUseSystemDefault);
|
2006-05-17 02:35:59 +00:00
|
|
|
this.setHandlerProcedure("handleInternal", "false");
|
|
|
|
this.setHandlerProcedure("saveToDisk", "false");
|
|
|
|
},
|
2006-05-17 02:26:44 +00:00
|
|
|
|
|
|
|
get handleInternal()
|
|
|
|
{
|
|
|
|
return getHandlerInfoForType(this.URI, "handleInternal");
|
|
|
|
},
|
|
|
|
|
|
|
|
set handleInternal(aHandledInternally)
|
|
|
|
{
|
2006-05-17 02:36:29 +00:00
|
|
|
changeMIMEStuff(HANDLER_URI(this.mimeType), "handleInternal", aHandledInternally);
|
2006-05-17 02:26:47 +00:00
|
|
|
this.setHandlerProcedure("saveToDisk", "false");
|
2006-05-17 02:35:59 +00:00
|
|
|
this.setHandlerProcedure("useSystemDefault", "false");
|
2006-05-17 02:26:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setHandlerProcedure: function (aHandlerProcedure, aValue)
|
|
|
|
{
|
2006-05-17 02:36:31 +00:00
|
|
|
var handlerSource = gRDF.GetUnicodeResource(HANDLER_URI(this.mimeType));
|
|
|
|
var handlerProperty = gRDF.GetUnicodeResource(NC_RDF(aHandlerProcedure));
|
2006-05-17 02:26:47 +00:00
|
|
|
var oppositeValue = aValue == "false" ? "true" : "false";
|
|
|
|
var trueLiteral = gRDF.GetLiteral(oppositeValue);
|
|
|
|
var hasCounterpart = gDS.HasAssertion(handlerSource, handlerProperty, trueLiteral, true);
|
|
|
|
if (hasCounterpart) {
|
|
|
|
var falseLiteral = gRDF.GetLiteral(aValue);
|
|
|
|
gDS.Change(handlerSource, handlerProperty, trueLiteral, falseLiteral);
|
|
|
|
}
|
2006-05-17 02:26:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get alwaysAsk()
|
|
|
|
{
|
|
|
|
return getHandlerInfoForType(this.URI, "alwaysAsk");
|
|
|
|
},
|
|
|
|
|
|
|
|
set alwaysAsk(aAlwaysAsk)
|
|
|
|
{
|
2006-05-17 02:36:29 +00:00
|
|
|
changeMIMEStuff(HANDLER_URI(this.mimeType), "alwaysAsk", aAlwaysAsk);
|
2006-05-17 02:26:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// helper application
|
|
|
|
get appDisplayName()
|
|
|
|
{
|
|
|
|
return getHelperAppInfoForType(this.URI, "prettyName");
|
|
|
|
},
|
|
|
|
|
|
|
|
set appDisplayName(aDisplayName)
|
|
|
|
{
|
2006-05-17 02:36:29 +00:00
|
|
|
changeMIMEStuff(APP_URI(this.mimeType), "prettyName", aDisplayName);
|
2006-05-17 02:26:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get appPath()
|
|
|
|
{
|
|
|
|
return getHelperAppInfoForType(this.URI, "path");
|
|
|
|
},
|
|
|
|
|
|
|
|
set appPath(aAppPath)
|
|
|
|
{
|
2006-05-17 02:36:29 +00:00
|
|
|
changeMIMEStuff(APP_URI(this.mimeType), "path", aAppPath);
|
2006-05-17 02:26:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* After setting the various properties on this override, we need to
|
|
|
|
* build the links between the mime type resource, the handler for that
|
|
|
|
* resource, and the helper app (if any) associated with the resource.
|
|
|
|
* We also need to add this mime type to the RDF seq (list) of types.
|
|
|
|
**/
|
|
|
|
buildLinks: function()
|
|
|
|
{
|
|
|
|
// assert the handler resource
|
2006-05-17 02:36:31 +00:00
|
|
|
var mimeSource = gRDF.GetUnicodeResource(MIME_URI(this.mimeType));
|
|
|
|
var handlerProperty = gRDF.GetUnicodeResource(NC_RDF("handlerProp"));
|
|
|
|
var handlerResource = gRDF.GetUnicodeResource(HANDLER_URI(this.mimeType));
|
2006-05-17 02:26:44 +00:00
|
|
|
gDS.Assert(mimeSource, handlerProperty, handlerResource, true);
|
|
|
|
// assert the helper app resource
|
2006-05-17 02:36:31 +00:00
|
|
|
var helperAppProperty = gRDF.GetUnicodeResource(NC_RDF("externalApplication"));
|
|
|
|
var helperAppResource = gRDF.GetUnicodeResource(APP_URI(this.mimeType));
|
2006-05-17 02:26:44 +00:00
|
|
|
gDS.Assert(handlerResource, helperAppProperty, helperAppResource, true);
|
|
|
|
// add the mime type to the MIME types seq
|
2006-05-17 02:27:37 +00:00
|
|
|
var container = Components.classes["@mozilla.org/rdf/container;1"].createInstance();
|
2006-05-17 02:26:44 +00:00
|
|
|
if (container) {
|
|
|
|
container = container.QueryInterface(Components.interfaces.nsIRDFContainer);
|
|
|
|
if (container) {
|
2006-05-17 02:36:31 +00:00
|
|
|
var containerRes = gRDF.GetUnicodeResource("urn:mimetypes:root");
|
2006-05-17 02:26:44 +00:00
|
|
|
container.Init(gDS, containerRes);
|
2006-05-17 02:36:31 +00:00
|
|
|
var element = gRDF.GetUnicodeResource(MIME_URI(this.mimeType));
|
2006-05-17 02:26:44 +00:00
|
|
|
if (container.IndexOf(element) == -1)
|
|
|
|
container.AppendElement(element);
|
|
|
|
}
|
|
|
|
}
|
2006-05-17 02:28:05 +00:00
|
|
|
}
|
2006-05-17 02:26:44 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility functions for building URIs easily
|
|
|
|
**/
|
|
|
|
function NC_RDF(aProperty)
|
|
|
|
{
|
|
|
|
return "http://home.netscape.com/NC-rdf#" + aProperty;
|
|
|
|
}
|
|
|
|
|
|
|
|
function HANDLER_URI(aHandler)
|
|
|
|
{
|
|
|
|
return "urn:mimetype:handler:" + aHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
function APP_URI(aType)
|
|
|
|
{
|
|
|
|
return "urn:mimetype:externalApplication:" + aType;
|
|
|
|
}
|
|
|
|
|
|
|
|
function MIME_URI(aType)
|
|
|
|
{
|
|
|
|
return "urn:mimetype:" + aType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility functions for reading data from the RDF datasource
|
|
|
|
**/
|
|
|
|
function getLiteralForContentType(aURI, aProperty)
|
|
|
|
{
|
2006-05-17 02:36:31 +00:00
|
|
|
var contentTypeResource = gRDF.GetUnicodeResource(aURI);
|
|
|
|
var propertyResource = gRDF.GetUnicodeResource(NC_RDF(aProperty));
|
2006-05-17 02:26:44 +00:00
|
|
|
return getLiteral(contentTypeResource, propertyResource);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLiteral(aSource, aProperty)
|
|
|
|
{
|
|
|
|
var node = gDS.GetTarget(aSource, aProperty, true);
|
|
|
|
if (node) {
|
|
|
|
node = node.QueryInterface(Components.interfaces.nsIRDFLiteral);
|
|
|
|
return node.Value;
|
|
|
|
}
|
2006-05-17 02:28:05 +00:00
|
|
|
return "";
|
2006-05-17 02:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getHandlerInfoForType(aURI, aPropertyString)
|
|
|
|
{
|
|
|
|
// get current selected type
|
|
|
|
var handler = HANDLER_URI(getLiteralForContentType(aURI, "value"));
|
2006-05-17 02:36:31 +00:00
|
|
|
var source = gRDF.GetUnicodeResource(handler);
|
|
|
|
var property = gRDF.GetUnicodeResource(NC_RDF(aPropertyString));
|
2006-05-17 02:26:44 +00:00
|
|
|
var target = gDS.GetTarget(source, property, true);
|
|
|
|
if (target) {
|
|
|
|
target = target.QueryInterface(Components.interfaces.nsIRDFLiteral);
|
|
|
|
return target.Value;
|
|
|
|
}
|
2006-05-17 02:28:05 +00:00
|
|
|
return "";
|
2006-05-17 02:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getHelperAppInfoForType(aURI, aPropertyString)
|
|
|
|
{
|
|
|
|
var appURI = APP_URI(getLiteralForContentType(aURI, "value"));
|
2006-05-17 02:36:31 +00:00
|
|
|
var appRes = gRDF.GetUnicodeResource(appURI);
|
|
|
|
var appProperty = gRDF.GetUnicodeResource(NC_RDF(aPropertyString));
|
2006-05-17 02:26:44 +00:00
|
|
|
return getLiteral(appRes, appProperty);
|
|
|
|
}
|
|
|
|
|
|
|
|
function mimeHandlerExists(aMIMEType)
|
|
|
|
{
|
2006-05-17 02:36:31 +00:00
|
|
|
var valueProperty = gRDF.GetUnicodeResource(NC_RDF("value"));
|
|
|
|
var mimeSource = gRDF.GetUnicodeResource(MIME_URI(aMIMEType));
|
2006-05-17 02:35:59 +00:00
|
|
|
var mimeLiteral = gRDF.GetLiteral(aMIMEType);
|
2006-05-17 02:26:44 +00:00
|
|
|
return gDS.HasAssertion(mimeSource, valueProperty, mimeLiteral, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write to the ds
|
|
|
|
function assertMIMEStuff(aMIMEString, aPropertyString, aValueString)
|
|
|
|
{
|
2006-05-17 02:36:31 +00:00
|
|
|
var mimeSource = gRDF.GetUnicodeResource(aMIMEString);
|
|
|
|
var valueProperty = gRDF.GetUnicodeResource(NC_RDF(aPropertyString));
|
2006-05-17 02:26:44 +00:00
|
|
|
var mimeLiteral = gRDF.GetLiteral(aValueString);
|
|
|
|
gDS.Assert(mimeSource, valueProperty, mimeLiteral, true);
|
|
|
|
}
|
|
|
|
|
2006-05-17 02:26:47 +00:00
|
|
|
function changeMIMEStuff(aMIMEString, aPropertyString, aValueString)
|
|
|
|
{
|
2006-05-17 02:36:31 +00:00
|
|
|
var mimeSource = gRDF.GetUnicodeResource(aMIMEString);
|
|
|
|
var valueProperty = gRDF.GetUnicodeResource(NC_RDF(aPropertyString));
|
2006-05-17 02:26:47 +00:00
|
|
|
var mimeLiteral = gRDF.GetLiteral(aValueString);
|
|
|
|
var currentValue = gDS.GetTarget(mimeSource, valueProperty, true);
|
2006-05-17 02:36:29 +00:00
|
|
|
if (currentValue) {
|
|
|
|
gDS.Change(mimeSource, valueProperty, currentValue, mimeLiteral);
|
|
|
|
} else {
|
|
|
|
gDS.Assert(mimeSource, valueProperty, mimeLiteral, true);
|
|
|
|
}
|
2006-05-17 02:26:47 +00:00
|
|
|
}
|
|
|
|
|
2006-05-17 02:26:44 +00:00
|
|
|
function unassertMIMEStuff(aMIMEString, aPropertyString, aValueString)
|
|
|
|
{
|
2006-05-17 02:36:31 +00:00
|
|
|
var mimeSource = gRDF.GetUnicodeResource(aMIMEString);
|
|
|
|
var valueProperty = gRDF.GetUnicodeResource(NC_RDF(aPropertyString));
|
2006-05-17 02:26:44 +00:00
|
|
|
var mimeLiteral = gRDF.GetLiteral(aValueString);
|
|
|
|
gDS.Unassert(mimeSource, valueProperty, mimeLiteral, true);
|
|
|
|
}
|
|
|
|
|
2006-05-17 02:26:54 +00:00
|
|
|
function removeOverride(aMIMEType)
|
|
|
|
{
|
|
|
|
// remove entry from seq
|
2006-05-17 02:27:37 +00:00
|
|
|
var rdfc = Components.classes["@mozilla.org/rdf/container;1"].createInstance();
|
2006-05-17 02:26:54 +00:00
|
|
|
if (rdfc) {
|
|
|
|
rdfc = rdfc.QueryInterface(Components.interfaces.nsIRDFContainer);
|
|
|
|
if (rdfc) {
|
2006-05-17 02:36:31 +00:00
|
|
|
var containerRes = gRDF.GetUnicodeResource("urn:mimetypes:root");
|
2006-05-17 02:26:54 +00:00
|
|
|
rdfc.Init(gDS, containerRes);
|
2006-05-17 02:36:31 +00:00
|
|
|
var element = gRDF.GetUnicodeResource(MIME_URI(aMIMEType));
|
2006-05-17 02:27:03 +00:00
|
|
|
if (rdfc.IndexOf(element) != -1) {
|
|
|
|
try {
|
|
|
|
rdfc.RemoveElement(element, true);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
// suppress (benign?) errors
|
|
|
|
}
|
|
|
|
}
|
2006-05-17 02:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-17 02:27:03 +00:00
|
|
|
|
2006-05-17 02:26:54 +00:00
|
|
|
// remove items from the graph
|
2006-05-17 02:27:03 +00:00
|
|
|
var urns = [ [MIME_URI, ["description", "editable", "value", "fileExtensions", "smallIcon", "largeIcon"],
|
|
|
|
[HANDLER_URI, "handlerProp"]],
|
2006-05-17 02:35:59 +00:00
|
|
|
[HANDLER_URI, ["handleInternal", "saveToDisk", "alwaysAsk", "useSystemDefault"],
|
2006-05-17 02:27:03 +00:00
|
|
|
[APP_URI, "externalApplication"]],
|
2006-05-17 02:26:54 +00:00
|
|
|
[APP_URI, ["path", "prettyName"]] ];
|
|
|
|
for (var i = 0; i < urns.length; i++) {
|
2006-05-17 02:36:31 +00:00
|
|
|
var mimeRes = gRDF.GetUnicodeResource(urns[i][0](aMIMEType));
|
2006-05-17 02:26:54 +00:00
|
|
|
// unassert the toplevel properties
|
|
|
|
var properties = urns[i][1];
|
|
|
|
for (var j = 0; j < properties.length; j++) {
|
2006-05-17 02:36:31 +00:00
|
|
|
var propertyRes = gRDF.GetUnicodeResource(NC_RDF(properties[j]), true);
|
2006-05-17 02:27:03 +00:00
|
|
|
if (properties[j] == "fileExtensions") { // hacky. do it better next time.
|
|
|
|
var mimeValues = gDS.GetTargets(mimeRes, propertyRes, true);
|
|
|
|
mimeValues = mimeValues.QueryInterface(Components.interfaces.nsISimpleEnumerator);
|
|
|
|
while (mimeValues.hasMoreElements()) {
|
|
|
|
var currItem = mimeValues.getNext();
|
|
|
|
if (mimeRes && propertyRes && currItem)
|
|
|
|
gDS.Unassert(mimeRes, propertyRes, currItem, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var mimeValue = gDS.GetTarget(mimeRes, propertyRes, true);
|
|
|
|
if (mimeRes && propertyRes && mimeValue)
|
|
|
|
gDS.Unassert(mimeRes, propertyRes, mimeValue, true);
|
|
|
|
}
|
2006-05-17 02:26:54 +00:00
|
|
|
}
|
2006-05-17 02:35:59 +00:00
|
|
|
if ("2" in urns[i] && urns[i][2]) {
|
2006-05-17 02:36:31 +00:00
|
|
|
var linkRes = gRDF.GetUnicodeResource(NC_RDF(urns[i][2][1]), true);
|
|
|
|
var linkTarget = gRDF.GetUnicodeResource(urns[i][2][0](aMIMEType), true);
|
2006-05-17 02:26:54 +00:00
|
|
|
gDS.Unassert(mimeRes, linkRes, linkTarget);
|
|
|
|
}
|
|
|
|
}
|
2006-05-17 02:32:59 +00:00
|
|
|
try {
|
|
|
|
gDS.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource).Flush();
|
|
|
|
} catch(e) {
|
|
|
|
}
|
2006-05-17 02:27:37 +00:00
|
|
|
}
|
2006-05-17 02:32:57 +00:00
|
|
|
|
|
|
|
function checkInput() {
|
|
|
|
var result = true;
|
|
|
|
// Check for empty MIME type field.
|
2006-05-17 02:33:01 +00:00
|
|
|
if ( gMIMEField.value.search(/\S/) == -1 ) {
|
2006-05-17 02:32:57 +00:00
|
|
|
// Input is not OK.
|
|
|
|
result = false;
|
|
|
|
|
|
|
|
// Focus the mime type field.
|
|
|
|
gMIMEField.focus();
|
|
|
|
|
|
|
|
// Put up alert. Title is same as parent dialog's.
|
|
|
|
var title = window.document.documentElement.getAttribute( "title" );
|
|
|
|
var text = gPrefApplicationsBundle.getString("emptyMIMEType");
|
|
|
|
var prompter = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
|
|
|
|
prompter.alert(window, title, text);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|