home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / jsmodules / LocalStore.jsm < prev    next >
Text File  |  2012-06-08  |  2KB  |  69 lines

  1. /**
  2.  * LocalStore.jsm
  3.  */
  4.  
  5. EXPORTED_SYMBOLS = [ "LocalStore" ];
  6.  
  7. Ci = Components.interfaces;
  8. Cc = Components.classes;
  9.  
  10. var LocalStore = {
  11.   _dirty: false,
  12.   _rdf: null,
  13.   _localStore: null,
  14.  
  15.   _initialize: function() {
  16.     this._rdf = Cc["@mozilla.org/rdf/rdf-service;1"]
  17.                   .getService(Ci.nsIRDFService);
  18.     this._localStore = this._rdf.GetDataSource("rdf:local-store");
  19.   },
  20.  
  21.   // Get an attribute value for an element id in a given file
  22.   getPersistedAttribute: function(file, id, attribute) {
  23.     if (!this._localStore)
  24.       this._initialize();
  25.  
  26.     var source = this._rdf.GetResource(file + "#" + id);
  27.     var property = this._rdf.GetResource(attribute);
  28.     var target = this._localStore.GetTarget(source, property, true);
  29.     if (target instanceof Ci.nsIRDFLiteral)
  30.       return target.Value;
  31.     return null;
  32.   },
  33.  
  34.   // Set an attribute on an element id in a given file
  35.   setPersistedAttribute: function(file, id, attribute, value) {
  36.     if (!this._localStore)
  37.       this._initialize();
  38.  
  39.     var source = this._rdf.GetResource(file + "#" +  id);
  40.     var property = this._rdf.GetResource(attribute);
  41.     try {
  42.       var oldTarget = this._localStore.GetTarget(source, property, true);
  43.       if (oldTarget) {
  44.         if (value)
  45.           this._localStore.Change(source, property, oldTarget,
  46.                                   this._rdf.GetLiteral(value));
  47.         else
  48.           this._localStore.Unassert(source, property, oldTarget);
  49.       }
  50.       else {
  51.         this._localStore.Assert(source, property,
  52.                                 this._rdf.GetLiteral(value), true);
  53.       }
  54.       this._dirty = true;
  55.     }
  56.     catch(ex) {
  57.       Components.utils.reportError(ex);
  58.     }
  59.   },
  60.  
  61.   // Save changes if needed
  62.   flush: function flush() {
  63.     if (this._localStore && this._dirty) {
  64.       this._localStore.QueryInterface(Ci.nsIRDFRemoteDataSource).Flush();
  65.       this._dirty = false;
  66.     }
  67.   }
  68. }
  69.