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");
37 const LOG = DebugUtils.generateLogFunction(
"ngMainEqualizerPresetProvider", 2);
49 if(this._createStorageIfNotExisting())
50 this._presets = this._getPresets();
54 XPCOMUtils.defineLazyServiceGetter(
this,
"_observerService",
55 "@mozilla.org/observer-service;1",
"nsIObserverService");
60 classID: Components.ID(
"{5e503ed9-1c8c-4135-8d25-61b0835475b4}"),
61 contractID:
"@getnightingale.com/equalizer-presets/main-provider;1",
63 Ci.ngIEqualizerPresetCollection]),
65 _observerService:
null,
72 return ArrayConverter.nsIArray(this._presets);
74 throw Cr.NS_ERROR_UNEXPECTED;
76 getPresetByName:
function(
aName) {
77 var index = this._getPresetIndex(
aName);
79 return this._presets[index];
82 hasPresetNamed:
function(
aName) {
83 return this._presetExists(
aName);
86 deletePreset:
function(
aName) {
87 if(
aName.length == 0) {
88 throw Cr.NS_ERROR_ILLEGAL_VALUE;
91 if(this._presetExists(
aName)) {
92 this._presets.splice(this._getPresetIndex(
aName), 1);
94 this._xml.getElementsByTagName(
"presets")[0].removeChild(this._getXMLPresetByName(
aName, this._xml));
98 this._observerService.notifyObservers(
this,
"equalizer-preset-deleted",
aName);
101 savePreset:
function(
aName, aValues) {
102 if(
aName.length == 0 || aValues.length != 10)
103 throw Cr.NS_ERROR_ILLEGAL_VALUE;
105 if(this._presetExists(
aName))
106 this._overwritePreset(
aName, aValues);
108 this._createPreset(
aName, aValues);
111 this._observerService.notifyObservers(
this,
"equalizer-preset-saved",
aName);
114 _getPresets:
function() {
115 return this._getPresetsFromXML(this._xml);
117 _savePresets:
function() {
118 var
file =
Cc[
"@mozilla.org/file/local;1"].createInstance(
Ci.nsILocalFile);
120 this._saveXMLDocument(this._xml, path);
122 _presetExists:
function(
aName) {
123 return this._presets.some(
function(item) {
124 return item.name ==
aName;
127 _getPresetIndex:
function(
aName) {
129 this._presets.some(
function(item,
i) {
130 if(item.name ==
aName) {
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);
144 this._presets.push(mutablePreset.QueryInterface(
Ci.ngIEqualizerPreset));
146 this._xml.getElementsByTagName(
"presets")[0]
147 .appendChild(this._createXMLPreset(
aName, ArrayConverter.JSArray(aValues)));
150 _overwritePreset:
function(
aName, aValues) {
151 var preset = this._presets[this._getPresetIndex(
aName)]
152 .QueryInterface(
Ci.ngIMutableEqualizerPreset);
153 preset.setValues(aValues);
155 var oldNode = this._getXMLPresetByName(
aName, this._xml);
156 this._xml.getElementsByTagName(
"presets")[0]
157 .replaceChild(this._createXMLPreset(
aName, ArrayConverter.JSArray(aValues)), oldNode);
160 _createStorageIfNotExisting:
function() {
162 var
file =
Cc[
"@mozilla.org/file/local;1"].createInstance(
Ci.nsILocalFile);
164 file.initWithPath(path);
168 LOG(
"XML isn't existing yet");
169 this._createPresetStorage(file);
172 this._xml = this._readXMLDocument(path);
173 return file.exists();
177 _createPresetStorage:
function(
file) {
178 file.create(
"text/XML",0777);
181 var parser =
Cc[
"@mozilla.org/xmlextras/domparser;1"]
182 .createInstance(
Ci.nsIDOMParser);
185 var
data = this._createXMLString([]);
188 var DOMDoc = parser.parseFromString(data,
"text/xml");
191 LOG(
"Saving the newly created XML file to the profile");
192 this._saveXMLDocument(DOMDoc, file.path);
194 _createXMLString:
function(aPresets) {
195 var data =
"<presets>";
196 for(var preset in aPresets){
197 data += this._presetToXML(preset, aPreset[preset]);
199 data +=
"</presets>";
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";
207 data +=
"</preset>\n";
210 _createXMLPreset:
function(
aName, aBands) {
211 var presetNode = this._xml.createElement(
"preset");
212 presetNode.setAttribute(
"name",
aName);
214 aBands.forEach(
function(band) {
215 var bandNode = this._xml.createElement(
"band"),
216 bandValue = this._xml.createTextNode(band
218 bandNode.appendChild(bandValue);
219 presetNode.appendChild(bandNode);
223 _readXMLDocument:
function(
aPath) {
224 return DOMUtils.loadDocument(
"file://"+
aPath);
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) {
234 _getPresetsFromXML:
function(
aDocument) {
236 var presetNodes =
aDocument.getElementsByTagName(
"preset");
238 for(var
i = 0;
i < presetNodes.length; ++
i) {
239 presets.push(this._getPresetFromXML(presetNodes[
i]));
244 _getPresetFromXML:
function(aPreset) {
245 var mutablePreset =
Cc[
"@getnightingale.com/equalizer-presets/mutable;1"]
246 .createInstance(
Ci.ngIMutableEqualizerPreset);
248 mutablePreset.setName(aPreset.getAttribute(
"name"));
250 var bandNodes = aPreset.getElementsByTagName(
"band");
251 for(var
i = 0;
i < bandNodes.length; ++
i) {
252 bands.push(bandNodes[
i].firstChild.nodeValue);
254 mutablePreset.setValues(ArrayConverter.nsIArray(bands));
256 return mutablePreset.QueryInterface(
Ci.ngIEqualizerPreset);
258 _getFilePathInProfile:
function(aRelativePath) {
260 var file =
Cc[
"@mozilla.org/file/directory_service;1"]
261 .getService(
Ci.nsIProperties)
262 .get(
"ProfD",
Ci.nsIFile);
264 var path = aRelativePath.split(
"/");
265 for (var
i = 0, sz = path.length;
i < sz;
i++) {
267 file.append(path[
i]);
271 _saveXMLDocument:
function(aDomDoc,
aPath) {
273 var file =
Cc[
"@mozilla.org/file/local;1"]
274 .createInstance(
Ci.nsILocalFile);
275 file.initWithPath(
aPath);
277 var stream =
Cc[
"@mozilla.org/network/file-output-stream;1"]
278 .createInstance(
Ci.nsIFileOutputStream);
279 stream.init(file, -1, -1, 0);
282 var serializer =
Cc[
"@mozilla.org/xmlextras/xmlserializer;1"]
283 .createInstance(
Ci.nsIDOMSerializer);
284 serializer.serializeToStream(aDomDoc, stream,
"UTF-8");
const PRESETS_STORAGE_FILE_PATH
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
sbOSDControlService prototype QueryInterface
sbDeviceFirmwareAutoCheckForUpdate prototype classDescription
function ngMainEqualizerPresetProvider()
_collectFormDataForFrame aDocument
sbDeviceFirmwareAutoCheckForUpdate prototype classID
_getSelectedPageStyle s i