This is just some static backup of the original site, don't expect every link to work!

source: modules/vI_identityDataExtras.js @ 509348

ng_0.9
Last change on this file since 509348 was 509348, checked in by rene <rene@…>, 8 years ago

code formatting (no code changes)

  • Property mode set to 100644
File size: 10.6 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2    This program is free software; you can redistribute it and/or modify
3    it under the terms of the GNU General Public License as published by
4    the Free Software Foundation; either version 2 of the License, or
5    (at your option) any later version.
6
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
15
16    The Original Code is the Virtual Identity Extension.
17
18    The Initial Developer of the Original Code is Rene Ejury.
19    Portions created by the Initial Developer are Copyright (C) 2007
20    the Initial Developer. All Rights Reserved.
21
22    Contributor(s):
23 * ***** END LICENSE BLOCK ***** */
24
25var EXPORTED_SYMBOLS = ["identityDataExtras", "registerIdExtrasObject", "identityDataExtrasObject", "identityDataExtrasCheckboxObject"];
26
27const {
28  classes: Cc,
29  interfaces: Ci,
30  utils: Cu,
31  results: Cr
32} = Components;
33
34Cu.import("resource://v_identity/vI_log.js");
35Cu.import("resource://v_identity/vI_prefs.js");
36
37let Log = setupLogging("virtualIdentity.identityDataExtras");
38
39let stringBundle = Cc["@mozilla.org/intl/stringbundle;1"]
40  .getService(Ci.nsIStringBundleService)
41  .createBundle("chrome://v_identity/locale/vI_storageExtras.properties");
42
43let idExtrasObjects = [];
44
45function registerIdExtrasObject(h) {
46  idExtrasObjects.push(h);
47}
48
49function identityDataExtras(rdfDatasource, resource) {
50  this.extras = [];
51  let currentWindow = Cc["@mozilla.org/appshell/window-mediator;1"]
52    .getService(Ci.nsIWindowMediator)
53    .getMostRecentWindow(null);
54  for each(let [, identityDataExtrasObject] in Iterator(idExtrasObjects)) {
55    this.extras.push(new identityDataExtrasObject(currentWindow));
56  }
57  if (rdfDatasource)
58    this.loopThroughExtras(
59      function (extra) {
60        extra.value = rdfDatasource._getRDFValue(resource, extra.field);
61      });
62}
63identityDataExtras.prototype = {
64  loopThroughExtras: function (k, returnVal) {
65    for (var i = 0; i < this.extras.length; i++) {
66      try {
67        returnVal = k(this.extras[i], i, returnVal)
68      } catch (e) {
69        Log.warn("identityDataExtras '" + this.extras[i].field + "' returned an error:", e);
70        dumpCallStack(e);
71      }
72    }
73    return returnVal;
74  },
75
76  // just give a duplicate of the current identityDataExtras, else we will work with pointers
77  getDuplicate: function () {
78    var newExtras = new identityDataExtras();
79    this.loopThroughExtras(function (extra, i) {
80      newExtras.extras[i].value = extra.value;
81    });
82    return newExtras;
83  },
84
85  // copys all values of an identity. This way we can create a new object with a different document-context
86  copy: function (extras) {
87    this.loopThroughExtras(function (extra, i) {
88      extra.value = extras.extras[i].value;
89    });
90  },
91
92  equal: function (identityDataExtras) {
93    var returnVal = true;
94    return this.loopThroughExtras(function (extra, i, returnVal) {
95      return extra.active ? (extra.equal(identityDataExtras.extras[i]) && returnVal) : returnVal;
96    }, returnVal);
97  },
98
99  getMatrix: function () {
100    var returnVal = "";
101    return this.loopThroughExtras(function (extra, i, returnVal) {
102      if (extra.active && extra.valueHtml)
103        returnVal += "<tr>" +
104        "<td class='col1 extras '>" + stringBundle.GetStringFromName("vident.identityData.extras." + extra.field) + "</td>" +
105        "<td class='col2 extras '>" + extra.valueHtml + "</td>" +
106        "</tr>"
107      return returnVal;
108    }, returnVal);
109  },
110
111  getCompareMatrix: function () {
112    var returnVal = "";
113    return this.loopThroughExtras(function (extra, i, returnVal) {
114      if (extra.active) {
115        var classEqual = (extra.lastCompareResult) ? "equal" : "unequal";
116        returnVal += "<tr>" +
117          "<td class='col1 extras " + classEqual + "'>" + stringBundle.GetStringFromName("vident.identityData.extras." + extra.field) + "</td>" +
118          "<td class='col2 extras " + classEqual + "'>" + extra.lastCompareValue + "</td>" +
119          "<td class='col3 extras " + classEqual + "'>" + extra.valueHtml + "</td>" +
120          "</tr>"
121      }
122      return returnVal
123    }, returnVal);
124  },
125
126  status: function () {
127    var returnVal = "";
128    return this.loopThroughExtras(function (extra) {
129      return returnVal += returnVal ? " " : "" + extra.status;
130    }, returnVal);
131  },
132
133  readIdentityValues: function (identity) {
134    this.loopThroughExtras(function (extra) {
135      extra.readIdentityValue(identity)
136    });
137  },
138
139  setValuesToEnvironment: function () {
140    this.loopThroughExtras(function (extra) {
141      extra.setValueToEnvironment()
142    });
143  },
144
145  getValuesFromEnvironment: function () {
146    this.loopThroughExtras(function (extra) {
147      extra.getValueFromEnvironment()
148    });
149  },
150
151  // add value's to the pref object, required for rdfDataTreeCollection
152  addPrefs: function (pref) {
153    this.loopThroughExtras(function (extra) {
154      pref[extra.field + "Col"] = extra.valueNice;
155    });
156  }
157}
158
159function identityDataExtrasObject(currentWindow) {
160  this.currentWindow = currentWindow;
161}
162identityDataExtrasObject.prototype = {
163  value: null, // will contain the current value of the object and can be accessed from outside
164  field: null, // short description of the field
165  option: null, // option from preferences, boolean
166  window: null, // the current Window the object was created for
167
168  lastCompareValue: "",
169  lastCompareResult: false,
170
171  get valueHtml() {
172    if (!this.value)
173      return "";
174    return "<div class='bool" + ((this.value == "true") ? " checked" : "") + "'>" +
175      "<label class='screen'>&nbsp;</label>" +
176      "<label class='braille'>" + this.valueNice + "</label>" +
177      "</div>"
178  },
179  get valueNice() {
180    if (!this.value)
181      return "";
182    return (this.value == "true") ? "yes" : "no";
183  },
184  get status() {
185    if (this.active && this.value)
186      return this.field + "='" + this.value + "'";
187    return "";
188  },
189  get active() {
190    return vIprefs.get("storage") && vIprefs.get(this.option)
191  },
192  equal: function (compareIdentityDataExtrasObject) {
193    this.lastCompareValue = compareIdentityDataExtrasObject.valueHtml;
194    this.lastCompareResult = (!this.value || !compareIdentityDataExtrasObject.value || this.value == compareIdentityDataExtrasObject.value);
195    if (!this.lastCompareResult) {
196      Log.debug("extras not equal " + this.field + " ('" + this.value + "' != '" + compareIdentityDataExtrasObject.value + "')");
197    }
198    return this.lastCompareResult;
199  },
200  // function to read the value from a given identity (probably not part of identity)
201  readIdentityValue: function (identity) {},
202  // function to set or read the value from/to the environment
203  setValueToEnvironment: function () {
204    let id = this.currentWindow.document.documentElement.id;
205    switch (id) {
206    case "msgcomposeWindow":
207      this.setValueToEnvironment_msgCompose();
208      break;
209    case "messengerWindow":
210      this.setValueToEnvironment_messenger();
211      break;
212    case "vI_rdfDataTreeWindow":
213    case "vI_rdfDataEditor":
214      this.setValueToEnvironment_dataEditor();
215      break;
216    default:
217      Log.error("getValueFromEnvironment unknown window: " + id)
218    }
219  },
220  getValueFromEnvironment: function () {
221    let id = this.currentWindow.document.documentElement.id;
222    switch (id) {
223    case "msgcomposeWindow":
224      this.getValueFromEnvironment_msgCompose();
225      break;
226    case "messengerWindow":
227      this.getValueFromEnvironment_messenger();
228      break;
229    case "vI_rdfDataTreeWindow":
230    case "vI_rdfDataEditor":
231      this.getValueFromEnvironment_dataEditor();
232      break;
233    default:
234      Log.error("getValueFromEnvironment unknown window: " + id)
235    }
236  },
237  setValueToEnvironment_msgCompose: function () {
238    Log.error("setValueToEnvironment not implemented for msgCompose and " + this.field)
239  },
240  setValueToEnvironment_messenger: function () {
241    Log.error("setValueToEnvironment not implemented for Messenger and " + this.field)
242  },
243  setValueToEnvironment_dataEditor: function () {
244    Log.error("setValueToEnvironment not implemented for dataEditor and " + this.field)
245  },
246  getValueFromEnvironment_msgCompose: function () {
247    Log.error("setValueToEnvironment not implemented for msgCompose and " + this.field)
248  },
249  getValueFromEnvironment_messenger: function () {
250    Log.error("setValueToEnvironment not implemented for Messenger and " + this.field)
251  },
252  getValueFromEnvironment_dataEditor: function () {
253    Log.error("setValueToEnvironment not implemented for dataEditor and " + this.field)
254  }
255}
256
257
258function identityDataExtrasCheckboxObject(currentWindow) {
259  this.currentWindow = currentWindow;
260}
261identityDataExtrasCheckboxObject.prototype = {
262  __proto__: identityDataExtrasObject.prototype,
263
264  updateFunction_msgCompose: function () {},
265  elementID_msgCompose: null,
266
267  readIdentityValue: function (identity) {
268    if (this.active)
269      this.value = (this.func_valueFromIdentity(identity)) ? "true" : "false";
270  },
271
272  setValueToEnvironment_msgCompose: function () {
273    var element = this.currentWindow.document.getElementById(this.elementID_msgCompose);
274    if (!this.active || (this.value == null) || !element)
275      return;
276
277    this.updateFunction_msgCompose();
278    if ((element.getAttribute("checked") == "true") != (this.value == "true")) {
279      Log.debug("change " + this.field + " to " + this.value + " with doCommand");
280      element.doCommand();
281    }
282  },
283
284  setValueToEnvironment_dataEditor: function () {
285    if (this.value != null) {
286      this.currentWindow.document.getElementById("vI_" + this.option).setAttribute("checked", this.value);
287      this.currentWindow.document.getElementById("vI_" + this.option + "_store").setAttribute("checked", "true");
288    }
289    this.currentWindow.document.getElementById("vI_" + this.option + "_store").doCommand();
290  },
291
292  getValueFromEnvironment_msgCompose: function () {
293    var element = this.currentWindow.document.getElementById(this.elementID_msgCompose)
294    if (this.active && element) {
295      this.updateFunction_msgCompose();
296      this.value = ((element.getAttribute("checked") == "true") ? "true" : "false");
297    }
298  },
299
300  getValueFromEnvironment_dataEditor: function () {
301    if (this.currentWindow.document.getElementById("vI_" + this.option + "_store").getAttribute("checked") == "true") {
302      var elementValue = this.currentWindow.document.getElementById("vI_" + this.option).getAttribute("checked");
303      this.value = (elementValue == "true") ? "true" : "false"
304    } else
305      this.value = null;
306  }
307}
Note: See TracBrowser for help on using the repository browser.