~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/toolkit/mozapps/downloads/content/helperApps.js

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

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