ngMainEqualizerPresetProvider.js
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN NIGHTINGALE GPL
4 //
5 // This file is part of the Nightingale web player.
6 //
7 // http://getnightingale.com
8 //
9 // This file may be licensed under the terms of of the
10 // GNU General Public License Version 2 (the "GPL").
11 //
12 // Software distributed under the License is distributed
13 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
14 // express or implied. See the GPL for the specific language
15 // governing rights and limitations.
16 //
17 // You should have received a copy of the GPL along with this
18 // program. If not, go to http://www.gnu.org/licenses/gpl.html
19 // or write to the Free Software Foundation, Inc.,
20 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 //
22 // END NIGHTINGALE GPL
23 //
24 */
25 
26 "use strict";
27 
28 const Cc = Components.classes;
29 const Cu = Components.utils;
30 const Ci = Components.interfaces;
31 const Cr = Components.results;
32 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
33 Cu.import("resource://app/jsmodules/ArrayConverter.jsm");
34 Cu.import("resource://app/jsmodules/DebugUtils.jsm");
35 Cu.import("resource://app/jsmodules/DOMUtils.jsm");
36 
37 const LOG = DebugUtils.generateLogFunction("ngMainEqualizerPresetProvider", 2);
38 
39 const PRESETS_STORAGE_FILE_PATH = "equalizer_presets.xml";
40 
49  if(this._createStorageIfNotExisting())
50  this._presets = this._getPresets();
51  else
52  this._presets = [];
53 
54  XPCOMUtils.defineLazyServiceGetter(this, "_observerService",
55  "@mozilla.org/observer-service;1", "nsIObserverService");
56 }
57 
59  classDescription: "Main provider responsible for saving user set presets",
60  classID: Components.ID("{5e503ed9-1c8c-4135-8d25-61b0835475b4}"),
61  contractID: "@getnightingale.com/equalizer-presets/main-provider;1",
62  QueryInterface: XPCOMUtils.generateQI([Ci.ngIMainEqualizerPresetProvider,
63  Ci.ngIEqualizerPresetCollection]),
64 
65  _observerService: null,
66 
67  _presets: null,
68  _xml: null,
69 
70  get presets() {
71  if(this._presets)
72  return ArrayConverter.nsIArray(this._presets);
73  else
74  throw Cr.NS_ERROR_UNEXPECTED;
75  },
76  getPresetByName: function(aName) {
77  var index = this._getPresetIndex(aName);
78  if(index >= 0)
79  return this._presets[index];
80  return null;
81  },
82  hasPresetNamed: function(aName) {
83  return this._presetExists(aName);
84  },
85 
86  deletePreset: function(aName) {
87  if(aName.length == 0) {
88  throw Cr.NS_ERROR_ILLEGAL_VALUE;
89  }
90 
91  if(this._presetExists(aName)) {
92  this._presets.splice(this._getPresetIndex(aName), 1);
93 
94  this._xml.getElementsByTagName("presets")[0].removeChild(this._getXMLPresetByName(aName, this._xml));
95  this._savePresets();
96 
97  // notify everybody who's interested about the deletion of the preset
98  this._observerService.notifyObservers(this, "equalizer-preset-deleted", aName);
99  }
100  },
101  savePreset: function(aName, aValues) {
102  if(aName.length == 0 || aValues.length != 10)
103  throw Cr.NS_ERROR_ILLEGAL_VALUE;
104 
105  if(this._presetExists(aName))
106  this._overwritePreset(aName, aValues);
107  else
108  this._createPreset(aName, aValues);
109 
110  // notify everybody who's interested about the preset that's been saved
111  this._observerService.notifyObservers(this, "equalizer-preset-saved", aName);
112  },
113 
114  _getPresets: function() {
115  return this._getPresetsFromXML(this._xml);
116  },
117  _savePresets: function() {
118  var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
119  var path = this._getFilePathInProfile(PRESETS_STORAGE_FILE_PATH);
120  this._saveXMLDocument(this._xml, path);
121  },
122  _presetExists: function(aName) {
123  return this._presets.some(function(item) {
124  return item.name == aName;
125  });
126  },
127  _getPresetIndex: function(aName) {
128  var index = -1;
129  this._presets.some(function(item, i) {
130  if(item.name == aName) {
131  index = i;
132  return true;
133  }
134  return false;
135  });
136  return index;
137  },
138  _createPreset: function(aName, aValues) {
139  var mutablePreset = Cc["@getnightingale.com/equalizer-presets/mutable;1"]
140  .createInstance(Ci.ngIMutableEqualizerPreset);
141  mutablePreset.setName(aName);
142  mutablePreset.setValues(aValues);
143 
144  this._presets.push(mutablePreset.QueryInterface(Ci.ngIEqualizerPreset));
145 
146  this._xml.getElementsByTagName("presets")[0]
147  .appendChild(this._createXMLPreset(aName, ArrayConverter.JSArray(aValues)));
148  this._savePresets();
149  },
150  _overwritePreset: function(aName, aValues) {
151  var preset = this._presets[this._getPresetIndex(aName)]
152  .QueryInterface(Ci.ngIMutableEqualizerPreset);
153  preset.setValues(aValues);
154 
155  var oldNode = this._getXMLPresetByName(aName, this._xml);
156  this._xml.getElementsByTagName("presets")[0]
157  .replaceChild(this._createXMLPreset(aName, ArrayConverter.JSArray(aValues)), oldNode);
158  this._savePresets();
159  },
160  _createStorageIfNotExisting: function() {
161  // Get the file object
162  var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
163  var path = this._getFilePathInProfile(PRESETS_STORAGE_FILE_PATH);
164  file.initWithPath(path);
165 
166  // Let's verify that the file actually exists, and if it doesn't create it
167  if(!file.exists()) {
168  LOG("XML isn't existing yet");
169  this._createPresetStorage(file);
170  }
171 
172  this._xml = this._readXMLDocument(path);
173  return file.exists();
174  },
175 
176  // From hereon be XML & file madness
177  _createPresetStorage: function(file) {
178  file.create("text/XML",0777);
179 
180  // get a DOM parser
181  var parser = Cc["@mozilla.org/xmlextras/domparser;1"]
182  .createInstance(Ci.nsIDOMParser);
183 
184  // create the file content
185  var data = this._createXMLString([]);
186 
187  // Create the document
188  var DOMDoc = parser.parseFromString(data,"text/xml");
189 
190  // safe the file in the profile
191  LOG("Saving the newly created XML file to the profile");
192  this._saveXMLDocument(DOMDoc, file.path);
193  },
194  _createXMLString: function(aPresets) {
195  var data = "<presets>";
196  for(var preset in aPresets){
197  data += this._presetToXML(preset, aPreset[preset]);
198  }
199  data += "</presets>";
200  return data;
201  },
202  _presetToXML: function(aPresetName, aBands) {
203  var data = "<preset name='" + DOMUtils.encodeTextForXML(aPresetName) + "'>\n";
204  aBands.forEach(function(band) {
205  data +="<band>" + DOMUtils.encodeTextForXML(band) + "</band>\n";
206  });
207  data += "</preset>\n";
208  return data;
209  },
210  _createXMLPreset: function(aName, aBands) {
211  var presetNode = this._xml.createElement("preset");
212  presetNode.setAttribute("name", aName);
213 
214  aBands.forEach(function(band) {
215  var bandNode = this._xml.createElement("band"),
216  bandValue = this._xml.createTextNode(band
217  .QueryInterface(Ci.nsISupportsDouble).data);
218  bandNode.appendChild(bandValue);
219  presetNode.appendChild(bandNode);
220  }, this)
221  return presetNode;
222  },
223  _readXMLDocument: function(aPath) {
224  return DOMUtils.loadDocument("file://"+aPath);
225  },
226  _getXMLPresetByName: function(aName, aDocument) {
227  LOG("Getting the XML preset node for the preset named " + aName);
228  var presetArr = DOMUtils.getElementsByAttribute(aDocument, "name", aName);
229  if(presetArr.length > 0) {
230  return presetArr[0];
231  }
232  return null;
233  },
234  _getPresetsFromXML: function(aDocument) {
235  var presets = [];
236  var presetNodes = aDocument.getElementsByTagName("preset");
237 
238  for(var i = 0; i < presetNodes.length; ++i) {
239  presets.push(this._getPresetFromXML(presetNodes[i]));
240  }
241 
242  return presets;
243  },
244  _getPresetFromXML: function(aPreset) {
245  var mutablePreset = Cc["@getnightingale.com/equalizer-presets/mutable;1"]
246  .createInstance(Ci.ngIMutableEqualizerPreset);
247 
248  mutablePreset.setName(aPreset.getAttribute("name"));
249  var bands = [];
250  var bandNodes = aPreset.getElementsByTagName("band");
251  for(var i = 0; i < bandNodes.length; ++i) {
252  bands.push(bandNodes[i].firstChild.nodeValue);
253  }
254  mutablePreset.setValues(ArrayConverter.nsIArray(bands));
255 
256  return mutablePreset.QueryInterface(Ci.ngIEqualizerPreset);
257  },
258  _getFilePathInProfile: function(aRelativePath) {
259  // get a file to represent the profile directory
260  var file = Cc["@mozilla.org/file/directory_service;1"]
261  .getService(Ci.nsIProperties)
262  .get("ProfD", Ci.nsIFile);
263  // add the relative path to the profiledir
264  var path = aRelativePath.split("/");
265  for (var i = 0, sz = path.length; i < sz; i++) {
266  if (path[i] != "")
267  file.append(path[i]);
268  }
269  return file.path;
270  },
271  _saveXMLDocument: function(aDomDoc, aPath) {
272  // get folder to write to
273  var file = Cc["@mozilla.org/file/local;1"]
274  .createInstance(Ci.nsILocalFile);
275  file.initWithPath(aPath);
276  // get a filestream
277  var stream = Cc["@mozilla.org/network/file-output-stream;1"]
278  .createInstance(Ci.nsIFileOutputStream);
279  stream.init(file, -1, -1, 0);
280 
281  // write the xml document into the stream
282  var serializer = Cc["@mozilla.org/xmlextras/xmlserializer;1"]
283  .createInstance(Ci.nsIDOMSerializer);
284  serializer.serializeToStream(aDomDoc, stream, "UTF-8");
285  }
286 };
287 
288 var NSGetModule = XPCOMUtils.generateNSGetModule([ngMainEqualizerPresetProvider]);
_updateCookies aPath
const PRESETS_STORAGE_FILE_PATH
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
sbOSDControlService prototype QueryInterface
sbDeviceFirmwareAutoCheckForUpdate prototype classDescription
function ngMainEqualizerPresetProvider()
_collectFormDataForFrame aDocument
return null
Definition: FeedWriter.js:1143
_updateCookies aName
sbDeviceFirmwareAutoCheckForUpdate prototype classID
observe data
Definition: FeedWriter.js:1329
_getSelectedPageStyle s i
var file