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

source: modules/vI_identityDataExtras.js @ 8aa4c2

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

only test extra-equality if feature is active

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