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

source: content/vI_getHeader.js

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

code formatting (no code changes)

  • Property mode set to 100644
File size: 7.3 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): Christian Weiske
23    Contributor(s): Patrick Brunschwig
24    Contributor(s): http://xulsolutions.blogspot.com/2006/07/creating-uninstall-script-for.html
25
26 * ***** END LICENSE BLOCK ***** */
27
28Components.utils.import("resource://v_identity/vI_nameSpaceWrapper.js");
29virtualIdentityExtension.ns(function () {
30  with(virtualIdentityExtension.LIB) {
31
32    Components.utils.import("resource://v_identity/stdlib/msgHdrUtils.js", virtualIdentityExtension);
33    Components.utils.import("resource://v_identity/vI_prefs.js", virtualIdentityExtension);
34    let Log = vI.setupLogging("virtualIdentity.getHeader");
35
36    // var storedHeaders = { };
37    var getHeader = {
38      unicodeConverter: Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
39        .createInstance(Components.interfaces.nsIScriptableUnicodeConverter),
40
41      headerToSearch: null,
42
43      prefObserverToSearchArray: function () {
44        var headerList = getHeader.unicodeConverter.ConvertToUnicode(vI.vIprefs.get("smart_reply_headers")).split(/\n/)
45
46        getHeader.headerToSearch = [];
47
48        // prepare headerToSearch for speedup.
49        for (var index = 0; index < headerList.length; index++) {
50          var headerToSearch_splitted = headerList[index].split(/:/)
51            // use first part (all before ':') as the header name
52          var headerNameToSearch = headerToSearch_splitted[0].toLowerCase()
53            // check second or third part for any number
54          var headerNumberToSearch = null;
55          if (headerToSearch_splitted.length > 1) parseInt(headerToSearch_splitted[1]);
56          if ((!headerNumberToSearch || isNaN(headerNumberToSearch)) && headerToSearch_splitted.length > 2) headerNumberToSearch = parseInt(headerToSearch_splitted[2]);
57
58          // create header name to store the value
59          var headerNameToStore = headerNameToSearch;
60          if (headerNumberToSearch && !isNaN(headerNumberToSearch)) headerNameToStore += ":" + headerNumberToSearch
61
62          getHeader.headerToSearch.push({
63            headerNameToSearch: headerNameToSearch,
64            headerNumberToSearch: headerNumberToSearch,
65            headerNameToStore: headerNameToStore
66          });
67        }
68      },
69
70      getHeader: function () {
71        vI.clearDebugOutput();
72        if (!getHeader.headerToSearch) getHeader.prefObserverToSearchArray()
73
74        vI.msgHdrGetHeaders(getHeader.hdr, function (aHeaders) {
75          let label = "";
76          if (aHeaders.has("list-id")) {
77            getHeader.hdr.setStringProperty("vI_list-id", "found");
78            Log.debug("found header: list-id  ...stored to recognize mailing-list");
79          }
80          if (aHeaders.has("received")) {
81            getHeader.hdr.setStringProperty("vI_received", "found");
82            Log.debug("found header: received  ...stored to recognize received mail");
83          }
84          if (aHeaders.has("content-base")) {
85            getHeader.hdr.setStringProperty("vI_content_base", "found");
86            Log.debug("found header: content-base  ...stored to recognize blog/news-feed");
87          }
88          for (let index = 0; index < getHeader.headerToSearch.length; index++) {
89            let {
90              headerNameToSearch: headerNameToSearch,
91              headerNumberToSearch: headerNumberToSearch,
92              headerNameToStore: headerNameToStore
93            } = getHeader.headerToSearch[index];
94            if (aHeaders.has(headerNameToSearch)) {
95              let value = "";
96              let values = aHeaders.getAll(headerNameToSearch);
97              if (!headerNumberToSearch || isNaN(headerNumberToSearch))
98                for (let i = 0; i < values.length;)
99                  value += ((value) ? (", ") : ("")) + values[i++];
100              else value = values[headerNumberToSearch - 1];
101              if (value) {
102                getHeader.hdr.setStringProperty("vI_" + headerNameToStore,
103                  getHeader.unicodeConverter.ConvertFromUnicode(value) + getHeader.unicodeConverter.Finish());
104
105                let storedValue = getHeader.hdr.getProperty("vI_" + headerNameToStore);
106                let storedConvValue = getHeader.unicodeConverter.ConvertToUnicode(storedValue);
107
108                Log.debug("found header: " + headerNameToStore +
109                  " - stored as '" + storedConvValue + "'");
110                label += (label) ? "\n" : ""
111                label += headerNameToStore + ":\t" + storedConvValue
112              }
113            }
114          }
115          vI.GetHeaderNotification.info(label);
116        });
117      },
118
119      setupEventListener: function () {
120        getHeader.unicodeConverter.charset = "UTF-8";
121
122        // read headers later if msg is loaded completely - this ensures compatibility to Thunderbird Conversation
123        getHeader.orig_OnMsgLoaded = OnMsgLoaded;
124        OnMsgLoaded = getHeader.OnMsgLoaded;
125
126      },
127
128      OnMsgLoaded: function (url) {
129        const Cc = Components.classes;
130        const Ci = Components.interfaces;
131        // Necko URL, so convert it into a message header
132        let ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
133        var neckoURL = ioService.newURI(null, null, url.baseURI);
134        neckoURL.QueryInterface(Ci.nsIMsgMessageUrl);
135
136        getHeader.hdr = neckoURL.messageHeader;
137        if (getHeader.hdr) getHeader.getHeader();
138        getHeader.orig_OnMsgLoaded(url);
139      }
140    }
141
142
143    var prefObserver = {
144      unicodeConverter: Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
145        .createInstance(Components.interfaces.nsIScriptableUnicodeConverter),
146
147      observer_added: false,
148
149      init: function () {
150        vI.vIprefs.addObserver("smart_reply_headers", this.observe, this);
151      },
152
153      cleanup: function () {
154        let self = this;
155        vI.vIprefs.removeObserver("smart_reply_headers", self.observe);
156      },
157
158      observe: function (self, subject, topic, data) {
159        if (topic == "nsPref:changed") {
160          // remove (old) prepared headerArray
161          getHeader.headerToSearch = null;
162          ReloadMessage();
163        }
164      },
165
166    }
167    addEventListener('messagepane-loaded', getHeader.setupEventListener, true);
168    window.addEventListener("load", function (e) {
169      prefObserver.init();
170    }, false);
171    window.addEventListener("unload", function (e) {
172      prefObserver.cleanup();
173    }, false);
174    // vI.storedHeaders = storedHeaders;
175  }
176});
Note: See TracBrowser for help on using the repository browser.