Changeset db5435


Ignore:
Timestamp:
Nov 12, 2011, 3:28:11 PM (8 years ago)
Author:
rene <rene@…>
Branches:
ng_0.9
Children:
d9af4a
Parents:
184c6c
Message:

cleanup and usage of stdlib-module (still incomplete)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • content/vI_getHeader.js

    r184c6c rdb5435  
    2626 * ***** END LICENSE BLOCK ***** */
    2727
    28 /**
    29 * some code copied and adapted from 'display Mail User Agent (MUA)'
    30 * thanks to Christian Weiske <cweiske@cweiske.de>
    31 */
    32 /**
    33 * some code copied and adapted from 'enigmail'
    34 * thanks to Patrick Brunschwig <patrick.brunschwig@gmx.net>
    35 */
    36 /**
    37 * some code copied and adapted from 'http://xulsolutions.blogspot.com/2006/07/creating-uninstall-script-for.html'
    38 * thanks to the unknown programmer
    39 */
    40 
     28Components.utils.import("resource://v_identity/stdlib/msgHdrUtils.js");
     29// XXX still missing implementation to select specific header by number and to display notification.
     30 
    4131virtualIdentityExtension.ns(function() { with (virtualIdentityExtension.LIB) {
    4232 
    4333// var storedHeaders = { };
    4434var getHeader = {
    45     messenger: null,
    4635    preferences : Components.classes["@mozilla.org/preferences-service;1"]
    4736            .getService(Components.interfaces.nsIPrefService)
     
    5140            .createInstance(Components.interfaces.nsIScriptableUnicodeConverter),
    5241
    53     strings : document.getElementById("vIdentBundle"),
     42// strings : document.getElementById("vIdentBundle"),
    5443   
    5544    headerToSearch : null,
    5645   
    57     prepareHeaderToSearchArray : function() {
     46    prefObserverToSearchArray : function() {
    5847        var headerList = getHeader.unicodeConverter.ConvertToUnicode(getHeader.preferences.getCharPref("smart_reply_headers")).split(/\n/)
    5948       
     
    6655            var headerNameToSearch = headerToSearch_splitted[0].toLowerCase()
    6756            // check second or third part for any number
    68             var headerNumberToSearch = parseInt(headerToSearch_splitted[1])
    69             if (isNaN(headerNumberToSearch)) headerNumberToSearch = parseInt(headerToSearch_splitted[2])
     57//          var headerNumberToSearch = parseInt(headerToSearch_splitted[1])
     58//          if (isNaN(headerNumberToSearch)) headerNumberToSearch = parseInt(headerToSearch_splitted[2])
    7059           
    7160            // create header name to store the value
    72             var headerNameToStore = "vI_" + headerNameToSearch
    73             if (!isNaN(headerNumberToSearch)) headerNameToStore += ":" + headerNumberToSearch
     61//          var headerNameToStore = "vI_" + headerNameToSearch
     62//          if (!isNaN(headerNumberToSearch)) headerNameToStore += ":" + headerNumberToSearch
    7463           
    75             getHeader.headerToSearch.push({ headerNameToSearch : headerNameToSearch, headerNumberToSearch : headerNumberToSearch,
    76                     headerNameToStore : headerNameToStore });
     64//          getHeader.headerToSearch.push({ headerNameToSearch : headerNameToSearch, headerNumberToSearch : headerNumberToSearch,
     65//                  headerNameToStore : headerNameToStore });
     66            getHeader.headerToSearch.push({ headerNameToSearch : headerNameToSearch });
    7767        }
    7868    },
    7969
    80     getHeaderDummy: function() {
    81        
     70    getHeader: function() {
     71      vI.notificationBar.clear_dump();
     72      vI.notificationBar.dump("\n");
     73     
     74      if (!getHeader.headerToSearch) getHeader.prefObserverToSearchArray()
     75     
     76      msgHdrGetHeaders(getHeader.hdr, function (aHeaders) {
     77        if (aHeaders.has("list-id")) {
     78          getHeader.hdr.setStringProperty("vI_list-id","found");
     79          vI.notificationBar.dump("## getHeader: found header: list-id  ...stored to recognize mailing-list\n");
     80        }
     81        if (aHeaders.has("received")) {
     82          getHeader.hdr.setStringProperty("vI_received","found");
     83          vI.notificationBar.dump("## getHeader: found header: received  ...stored to recognize received mail\n");
     84        }
     85        if (aHeaders.has("content-base")) {
     86          getHeader.hdr.setStringProperty("vI_content_base","found");
     87          vI.notificationBar.dump("## getHeader: found header: content-base  ...stored to recognize blog/news-feed\n");
     88        }
     89        for (let index = 0; index < getHeader.headerToSearch.length; index++) {
     90          let headerNameToSearch = getHeader.headerToSearch[index].headerNameToSearch;
     91          if (aHeaders.has(headerNameToSearch)) {
     92            let value = aHeaders.get(headerNameToSearch);
     93            getHeader.hdr.setStringProperty("vI_" + headerNameToSearch,
     94                                  getHeader.unicodeConverter.ConvertFromUnicode(value) + getHeader.unicodeConverter.Finish());
     95            let storedValue = getHeader.hdr.getProperty("vI_" + headerNameToSearch);
     96            let storedConvValue = getHeader.unicodeConverter.ConvertToUnicode(storedValue);
     97            vI.notificationBar.dump("## getHeader: found header: " + headerNameToSearch +
     98                " - stored as '" + storedConvValue + "'\n");
     99          }
     100        }
     101      });
    82102    },
    83103   
    84     getHeader: function(hdr) {
    85      
    86      
    87      
    88         vI.notificationBar.clear_dump()
    89         var index;
    90 
    91         if (!getHeader.headerToSearch) getHeader.prepareHeaderToSearchArray()
    92 
    93         var found = false; var label = "";
    94         var subtitle = getHeader.strings.getString("vident.getHeader.noHeader");
    95         // create array to count the header
    96         var currentHeadersCounter = [];
    97 //      storedHeaders = { };
     104    setupEventListener: function() {
    98105       
    99         var listId = false; var received = false; var content_base = false;
    100         for (var header in currentHeaderData) {
    101             var headerName = currentHeaderData[header].headerName.toLowerCase();
    102 //          vI.notificationBar.dump("## getHeader: found header: " + currentHeaderData[header].headerName + "\n");
    103 
    104             // remember list-id header to prevent using Mailing-List addresses as sender
    105             if (!listId && headerName == "list-id") {
    106                 hdr.setStringProperty("vI_list-id","found"); listId = true;
    107 //              storedHeaders["vI_list-id"] = "found";
    108                 vI.notificationBar.dump("## getHeader: found header: list-id  ...stored to recognize mailing-list\n");
    109 //              continue;
    110             }
    111 
    112             // remember received header to prevent using Mailing-List addresses as sender
    113             if (!received && headerName == "received") {
    114                 hdr.setStringProperty("vI_received","found"); received = true;
    115 //              storedHeaders["vI_received"] = "found";
    116                 vI.notificationBar.dump("## getHeader: found header: received  ...stored to recognize received mail\n");
    117 //              continue;
    118             }
    119            
    120             // remember content-base header to prevent using Blog/News-Feed addresses as sender
    121             if (!content_base && headerName == "content-base") {
    122                 hdr.setStringProperty("vI_content_base","found"); content_base = true;
    123 //              storedHeaders["vI_content_base"] = "found";
    124                 vI.notificationBar.dump("## getHeader: found header: content-base  ...stored to recognize blog/news-feed\n");
    125 //              continue;
    126             }
    127 
    128             if (currentHeadersCounter[headerName]) currentHeadersCounter[headerName]++
    129             else currentHeadersCounter[headerName] = 1
    130            
    131             for (var index = 0; index < getHeader.headerToSearch.length; index++) {
    132                 if (headerName == getHeader.headerToSearch[index].headerNameToSearch &&
    133                     (isNaN(getHeader.headerToSearch[index].headerNumberToSearch) ||
    134                         getHeader.headerToSearch[index].headerNumberToSearch == currentHeadersCounter[headerName])) {
    135                    
    136                     var value = currentHeaderData[header].headerValue;
    137                     if (currentHeadersCounter[headerName] != 1)
    138                         value = hdr.getStringProperty(getHeader.headerToSearch[index].headerNameToStore) +
    139                         ", " + value;
    140                     hdr.setStringProperty(getHeader.headerToSearch[index].headerNameToStore,getHeader.unicodeConverter.ConvertFromUnicode(value) + getHeader.unicodeConverter.Finish());
    141                    
    142 //                  storedHeaders[getHeader.headerToSearch[index].headerNameToStore] = getHeader.unicodeConverter.ConvertFromUnicode(value) + getHeader.unicodeConverter.Finish();
    143 
    144                     var storedValue = hdr.getProperty(getHeader.headerToSearch[index].headerNameToStore)
    145                     var storedConvValue = getHeader.unicodeConverter.ConvertToUnicode(storedValue)
    146                     vI.notificationBar.dump("## getHeader: found header: " + headerName +
    147                         "[:" + currentHeadersCounter[headerName] + "] - stored as '" +
    148                         storedConvValue + "'\n");
    149 /*                  vI.notificationBar.dump("## getHeader: additional stored header: " + getHeader.headerToSearch[index].headerNameToStore +
    150                         " '" + storedHeaders[getHeader.headerToSearch[index].headerNameToStore] + "'\n");*/
    151                     if (!found) {
    152                         subtitle = getHeader.strings.getString("vident.getHeader.headerFound");
    153                         found = true;
    154                     }
    155                     label += (label)?"\n":""
    156                     label += currentHeaderData[header].headerName +
    157                     "[:" + currentHeadersCounter[headerName] + "]:\t" + currentHeaderData[header].headerValue
    158                     break;
    159                 }
    160             }
    161         }
    162         vI.notificationBar.setNote(label, "get_header_notification", subtitle);
    163     },
    164    
    165     hideExtraHeader: function() {
    166         var addedHdrs = prepareHeader.prefroot.getCharPref("extensions.virtualIdentity.smart_reply_added_extraHeaders").split(/ /);
    167         for (var index = 0; index < addedHdrs.length; index++) {
    168             var header_to_search_splitted=addedHdrs[index].split(/:/)
    169             var header_to_search=header_to_search_splitted[0].toLowerCase()
    170             if (typeof(gExpandedHeaderView[header_to_search]) == "object") {
    171                 if (! gViewAllHeaders) {
    172                     gExpandedHeaderView[header_to_search].enclosingBox.setAttribute("hidden", true);
    173                 }
    174                 else {
    175                     gExpandedHeaderView[header_to_search].enclosingBox.removeAttribute("hidden");
    176                 }
    177             }
    178         }
    179     },
    180 
    181     setupEventListener: function() {
    182         var listener = {};
    183         listener.onStartHeaders = getHeader.hideExtraHeader;
    184         listener.onEndHeaders   = getHeader.getHeaderDummy;
    185         gMessageListeners.push(listener);
    186 
    187         getHeader.messenger = Components.classes["@mozilla.org/messenger;1"].createInstance();
    188         getHeader.messenger = getHeader.messenger.QueryInterface(Components.interfaces.nsIMessenger);
    189106        getHeader.strings = document.getElementById("vIdentBundle");
    190107       
    191108        getHeader.unicodeConverter.charset = "UTF-8";
    192109       
    193         // read headers later if msg is loaded completely - this ensures compatibility to Thunderbird Conversation
    194         getHeader.orig_OnMsgLoaded = OnMsgLoaded;
    195         OnMsgLoaded = getHeader.OnMsgLoaded;
     110        // read headers later if msg is loaded completely - this ensures compatibility to Thunderbird Conversation
     111        getHeader.orig_OnMsgLoaded = OnMsgLoaded;
     112        OnMsgLoaded = getHeader.OnMsgLoaded;
     113
    196114    },
    197115   
     
    204122        neckoURL.QueryInterface(Ci.nsIMsgMessageUrl);
    205123       
    206         var msgHdr = neckoURL.messageHeader;
    207         if (msgHdr) getHeader.getHeader(msgHdr);
    208         getHeader.orig_OnMsgLoaded(url)
     124        getHeader.hdr = neckoURL.messageHeader;
     125        if (getHeader.hdr) getHeader.getHeader();
     126        getHeader.orig_OnMsgLoaded(url);
    209127    }
    210128}
    211129
    212130
    213 var prepareHeader = {
     131var prefObserver = {
    214132    prefroot : Components.classes["@mozilla.org/preferences-service;1"]
    215133            .getService(Components.interfaces.nsIPrefService)
    216             .getBranch(null),
     134            .getBranch(null).QueryInterface(Components.interfaces.nsIPrefBranch2),
    217135
    218136    unicodeConverter : Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
     
    222140   
    223141    init : function() {
    224         prepareHeader.orig_initializeHeaderViewTables = initializeHeaderViewTables;
    225         initializeHeaderViewTables = prepareHeader.replacement_initializeHeaderViewTables;
    226         if (prepareHeader.addExtraHeader()) prepareHeader.addObserver();
    227     },
    228    
    229     replacement_initializeHeaderViewTables : function() {
    230         prepareHeader.cleanup();
    231         vI.notificationBar.dump("## getHeader: initializeHeaderViewTables\n");
    232         prepareHeader.orig_initializeHeaderViewTables();
    233         if (prepareHeader.addExtraHeader()) prepareHeader.addObserver();
     142      prefObserver.prefroot.addObserver("extensions.virtualIdentity.smart_reply_headers", this, false);
    234143    },
    235144   
    236145    cleanup : function() {
    237         prepareHeader.removeObserver();
    238         prepareHeader.removeExtraHeader();
    239     },
    240    
    241     addObserver : function() {
    242         if (prepareHeader.observer_added) return;
    243         prepareHeader.prefroot.QueryInterface(Components.interfaces.nsIPrefBranch2);
    244         prepareHeader.prefroot.addObserver("extensions.virtualIdentity.smart_reply_headers", this, false);
    245         prepareHeader.uninstallObserver.register();
    246         prepareHeader.observer_added = true;
    247     },
    248    
    249     removeObserver : function() {
    250         if (!prepareHeader.observer_added) return;
    251         prepareHeader.prefroot.removeObserver("extensions.virtualIdentity.smart_reply_headers", this);
    252         prepareHeader.uninstallObserver.unregister();
    253         prepareHeader.observer_added = false;
    254     },
    255    
    256     // this is a adapted copy of enigEnsureExtraHeaders() from enigmail, thanks
    257     addExtraHeader : function() {
    258         prepareHeader.unicodeConverter.charset = "UTF-8";
    259         var header_list = prepareHeader.unicodeConverter.ConvertToUnicode(prepareHeader.prefroot.getCharPref("extensions.virtualIdentity.smart_reply_headers")).split(/\n/)
    260        
    261         // add List-Id to recognizable headers to prevent using Mailing-List addresses as sender
    262         header_list.push("List-Id")
    263 
    264         // add Received to recognizable headers to detect if mail was sent or received
    265         header_list.push("Received")
    266 
    267         // add Website to recognizable headers to detect Blog/News-Posts
    268         header_list.push("content-base")
    269 
    270 //      try {
    271             var extraHdrs = " " +
    272                 prepareHeader.prefroot.getCharPref("mailnews.headers.extraExpandedHeaders").toLowerCase();
    273 
    274             var addedHeaders = prepareHeader.prefroot.getCharPref("extensions.virtualIdentity.smart_reply_added_extraHeaders");
    275 
    276             for (var index = 0; index < header_list.length; index++) {
    277                 var headerToSearch_splitted = header_list[index].split(/:/)
    278                 var headerToSearch = headerToSearch_splitted[0].toLowerCase()
    279                
    280                 var j; var found = false;
    281                
    282                 // collapsedHeaderView is removed in
    283                 // https://bugzilla.mozilla.org/show_bug.cgi?id=480623
    284                 // http://build.mozillamessaging.com/mercurial/comm-central/rev/1fbbd90413d9
    285                 if (typeof(gCollapsedHeaderList) != "undefined") {
    286                     // check if Header is included in collapsed HeaderView
    287                     for (var j = 0; j < gCollapsedHeaderList.length; j++) {
    288                         if (gCollapsedHeaderList[j].name == headerToSearch) {
    289     //                      vI.notificationBar.dump("## prepareHeader: Header '" + headerToSearch + "' in gCollapsedHeaderList\n");
    290                             found = true; break;
    291                         }
    292                     }
    293                     if (found) continue;
    294                 }
    295 
    296                 // check if Header is included in expanded HeaderView
    297                 for (var j = 0; j < gExpandedHeaderList.length; j++) {
    298                     if (gExpandedHeaderList[j].name == headerToSearch) {
    299 //                      vI.notificationBar.dump("## prepareHeader: Header '" + headerToSearch + "' in gExpandedHeaderList\n");
    300                         found = true; break;
    301                     }
    302                 }
    303                 if (found) continue;
    304 
    305                 if ((extraHdrs.indexOf(" " + headerToSearch + " ") < 0) &&
    306                     (addedHeaders.indexOf(" " + headerToSearch + " ") < 0))
    307                         addedHeaders += " " + headerToSearch;
    308 //              else vI.notificationBar.dump("## prepareHeader: Header '" + headerToSearch + "' already in extraExpandedHeaders\n");
    309             }
    310            
    311             addedHeaders = addedHeaders.replace(/^\s+|\s+$/g,"")
    312             if (addedHeaders.length > 0) {
    313                 extraHdrs += " " + addedHeaders;
    314                 extraHdrs = extraHdrs.replace(/^\s+|\s+$/g,"")
    315                 prepareHeader.prefroot.setCharPref("mailnews.headers.extraExpandedHeaders", extraHdrs)
    316                 prepareHeader.prefroot.setCharPref("extensions.virtualIdentity.smart_reply_added_extraHeaders", addedHeaders)
    317                 vI.notificationBar.dump("## prepareHeader: extraExpandedHeaders '" + addedHeaders + "' added\n");
    318             }       
    319 
    320             return true;
    321 //      }
    322 //      catch (e) {
    323 //          vI.notificationBar.dump("## prepareHeader: your application is too old, please update. Otherwise try to install mnenhy or enigmail to use additional headers.")
    324 //          return false;
    325 //      }
    326     },
    327 
    328     removeExtraHeader: function() {
    329         vI.notificationBar.dump("## prepareHeader: cleanupExtraHeader\n");
    330 
    331         var addedHdrs = prepareHeader.prefroot.getCharPref("extensions.virtualIdentity.smart_reply_added_extraHeaders").split(/ /);
    332 
    333         if (addedHdrs.length > 0) {
    334             var extraHdrs = prepareHeader.prefroot.getCharPref("mailnews.headers.extraExpandedHeaders").toLowerCase().split(/ /);
    335        
    336             for (var i = 0; i < addedHdrs.length; i++) {
    337                 for (var j = 0; j < extraHdrs.length; j++) {
    338                     if (extraHdrs[j] == addedHdrs[i]) {
    339                         extraHdrs.splice(j,1);
    340                         break;
    341                     }
    342                 }
    343             }
    344             vI.notificationBar.dump("## prepareHeader: extraExpandedHeaders '" + addedHdrs.join(" ") + "' removed\n");
    345             prepareHeader.prefroot.setCharPref("mailnews.headers.extraExpandedHeaders", extraHdrs.join(" "))
    346             prepareHeader.prefroot.setCharPref("extensions.virtualIdentity.smart_reply_added_extraHeaders", "")
    347         }
     146      prefObserver.prefroot.removeObserver("extensions.virtualIdentity.smart_reply_headers", this);
    348147    },
    349148   
    350149    observe: function(subject, topic, data) {
    351150        if (topic == "nsPref:changed") {
    352             prepareHeader.removeExtraHeader();
    353             prepareHeader.addExtraHeader();
    354             vI.notificationBar.dump("## prepareHeader: changed preference '" + subject + " " + topic + " " + data + "'\n");
    355            
    356151            // remove (old) prepared headerArray
    357152            getHeader.headerToSearch = null;
    358153           
    359             vI.notificationBar.dump("## prepareHeader: reload Message\n");
     154            vI.notificationBar.dump("## prefObserver: reload Message\n");
    360155            MsgReload();
    361156        }
    362157    },
    363158
    364 //  code adapted from http://xulsolutions.blogspot.com/2006/07/creating-uninstall-script-for.html
    365     uninstallObserver : {
    366         _uninstall : false,
    367         observe : function(subject, topic, data) {
    368             if (topic == "quit-application-granted") {
    369                 /* uninstall stuff. */
    370                 vI.notificationBar.dump("## vI.uninstall: uninstall/disabledment \n");
    371                 prepareHeader.removeExtraHeader();
    372                 vI.notificationBar.dump("## vI.uninstall: uninstall/disablement done\n");
    373                 this.unregister();
    374             }
    375         },
    376         register : function() {
    377             Components.classes["@mozilla.org/observer-service;1"].
    378                 getService(Components.interfaces.nsIObserverService).
    379                     addObserver(this, "quit-application-granted", false);
    380         },
    381         unregister : function() {
    382             Components.classes["@mozilla.org/observer-service;1"].
    383                 getService(Components.interfaces.nsIObserverService).
    384                     removeObserver(this,"quit-application-granted");
    385         }
    386     }
    387159}
    388160addEventListener('messagepane-loaded', getHeader.setupEventListener, true);
    389 window.addEventListener("load", function(e) { prepareHeader.init(); }, false);
    390 window.addEventListener("unload", function(e) { prepareHeader.cleanup(); }, false);
     161window.addEventListener("load", function(e) { prefObserver.init(); }, false);
     162window.addEventListener("unload", function(e) { prefObserver.cleanup(); }, false);
    391163// vI.storedHeaders = storedHeaders;
    392164}});
Note: See TracChangeset for help on using the changeset viewer.