Changeset f8f8ea


Ignore:
Timestamp:
Dec 16, 2007, 9:14:57 PM (14 years ago)
Author:
rene <rene@…>
Branches:
master
Children:
730ee6
Parents:
63fe91
Message:

rewrite to support newsgroups and mailinglists in the future. (comminted as a backup, not to use yet)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • chrome/content/v_identity/vI_addressBook.js

    r63fe91 rf8f8ea  
    112112    },
    113113   
    114     removeVIdentityFromABook: function(remove) {
     114    //~ removeNGCardsFromABook: function(remove) {
     115        //~ // this function will be called exclusivly from vI_prefDialog. So it is used in different context than the rest of
     116        //~ // the functions, access of vI.* is not possible
     117        //~ // given the function paramter as false it might be used to count the fields which have a VirtualIdentity stored
     118       
     119        //~ counter = 0;
     120       
     121        //~ // enumerate all of the address books on this system
     122        //~ var parentDir = vI_addressBook.rdfService.GetResource("moz-abdirectory://").QueryInterface(Components.interfaces.nsIAbDirectory);
     123        //~ var enumerator = parentDir.childNodes;
     124           
     125        //~ vI_notificationBar.dump("## vI_addressBook: Search Virtual Identities in addressbooks.\n")
     126       
     127        //~ if (remove) {
     128            //~ var number = vI_addressBook.removeNGCardsFromABook(false)
     129            //~ var strings = document.getElementById("vIdentBundle");
     130            //~ var warning = strings.getString("vident.clearAddressBook.statusNG.prefix") + " " + number + " " +
     131                    //~ strings.getString("vident.clearAddressBook.statusNG.postfix") + " " +
     132                    //~ strings.getString("vident.clearAddressBook.warningNG")
     133            //~ if (!vI_addressBook.promptService.confirm(window,"Warning",warning))
     134                //~ return;
     135        //~ }
     136       
     137        //~ while (enumerator && enumerator.hasMoreElements()) {
     138            //~ var addrbook = enumerator.getNext();  // an addressbook directory
     139            //~ addrbook.QueryInterface(Components.interfaces.nsIAbDirectory);
     140            //~ var searchUri = (addrbook.directoryProperties?addrbook.directoryProperties.URI:addrbook.URI) + "?(or(Notes,c," + encodeURIComponent(vI_addressBook.addNote) + "))"; // search for the address in this book
     141            //~ var directory = vI_addressBook.rdfService.GetResource(searchUri).QueryInterface(Components.interfaces.nsIAbDirectory);
     142            //~ // directory will now be a subset of the addressbook containing only those cards that match the searchstring 'address'
     143            //~ if (!directory) break;
     144           
     145            //~ if (remove) addrbook.deleteDirectory(directory)
     146            //~ else {
     147                //~ var ChildCards = directory.childCards;
     148                //~ var keepGoing = 1;
     149                //~ try { ChildCards.first(); } catch (ex) { keepGoing = 0; }
     150               
     151                //~ while (keepGoing == 1) {
     152                    //~ var Card = ChildCards.currentItem();
     153                    //~ Card = Card.QueryInterface(Components.interfaces.nsIAbCard);
     154                    //~ counter += 1;
     155                    //~ try { ChildCards.next(); } catch (ex) { keepGoing = 0; }
     156                //~ }
     157            //~ }
     158        //~ }
     159        //~ return counter;
     160    //~ },
     161   
     162    countVIdentitiesInABook : function (){
     163        var queryString = "?(or"
     164        for each (var prop in vI_addressBook.CardFields)
     165            queryString += "(" + prop + ",c,vIdentity:)";
     166        queryString += ")"
     167        return vI_addressBook._walkTroughCards(queryString,vI_addressBook._countVIdentities, 0)
     168    },
     169   
     170    _countVIdentities : function(addrbook, Card, counter) {
     171        if (addrbook.description != "Virtual Identity") counter += 1;
     172        return counter;
     173    },
     174
     175    removeVIdentityFromABook : function() {
    115176        // this function will be called exclusivly from vI_prefDialog. So it is used in different context than the rest of
    116177        // the functions, access of vI.* is not possible
    117         // given the function paramter as false it might be used to count the fields which have a VirtualIdentity stored
    118        
    119         counter = 0;
    120        
    121         // enumerate all of the address books on this system
     178        var number = vI_addressBook.removeVIdentityFromABook(false)
     179        var strings = document.getElementById("vIdentBundle");
     180        var warning = strings.getString("vident.clearAddressBook.status.prefix") + " " + number + " " +
     181                strings.getString("vident.clearAddressBook.status.postfix") + " " +
     182                strings.getString("vident.clearAddressBook.warning")
     183        if (!vI_addressBook.promptService.confirm(window,"Warning",warning))
     184            return;
     185        for each (var prop in vI_addressBook.CardFields) {
     186            var queryString = "?(or(" + prop + ",c,vIdentity:))";
     187            vI_addressBook._walkTroughCards(queryString,vI_addressBook._removeVIdentity,prop)
     188        }
     189    },
     190   
     191    _removeVIdentity: function(addrbook, Card, prop) {
     192        Card[prop.toLowerCase()] = "";
     193        Card.editCardToDatabase("");
     194    },
     195
     196    _walkTroughCards : function (queryString, callFunction, returnVar) {
    122197        var parentDir = vI_addressBook.rdfService.GetResource("moz-abdirectory://").QueryInterface(Components.interfaces.nsIAbDirectory);
    123198        var enumerator = parentDir.childNodes;
    124            
    125         //~ vI_notificationBar.dump("## vI_addressBook: Search Virtual Identities in addressbooks.\n")
    126        
    127         if (remove) {
    128             var number = vI_addressBook.removeVIdentityFromABook(false)
    129             var strings = document.getElementById("vIdentBundle");
    130             var warning = strings.getString("vident.clearAddressBook.status.prefix") + " " + number + " " +
    131                     strings.getString("vident.clearAddressBook.status.postfix") + " " +
    132                     strings.getString("vident.clearAddressBook.warning")
    133             if (!vI_addressBook.promptService.confirm(window,"Warning",warning))
    134                 return;
    135         }
    136        
     199        if (!enumerator) {vI_notificationBar.dump("## vI_addressBook: no addressbooks?\n"); return null;} // uups, no addressbooks?
    137200        while (enumerator && enumerator.hasMoreElements()) {
    138201            var addrbook = enumerator.getNext();  // an addressbook directory
    139202            addrbook.QueryInterface(Components.interfaces.nsIAbDirectory);
    140             for each (var prop in vI_addressBook.CardFields) {
    141                 var searchUri = (addrbook.directoryProperties?addrbook.directoryProperties.URI:addrbook.URI) + "?(or(" + prop + ",c,vIdentity:))"; // search for the address in this book
    142                 var directory = vI_addressBook.rdfService.GetResource(searchUri).QueryInterface(Components.interfaces.nsIAbDirectory);
    143                 // directory will now be a subset of the addressbook containing only those cards that match the searchstring 'address'
    144                 if (!directory) break;
    145                 var ChildCards = directory.childCards;
    146                 var keepGoing = 1;
    147                 try { ChildCards.first(); } catch (ex) { keepGoing = 0; }
    148                
    149                 while (keepGoing == 1) {
    150                     var Card = ChildCards.currentItem();
    151                     Card = Card.QueryInterface(Components.interfaces.nsIAbCard);
    152                     counter += 1;
    153                     if (remove) {
    154                         Card[prop.toLowerCase()] = "";
    155                         Card.editCardToDatabase("");
    156                     }
    157                    
    158                     try { ChildCards.next(); } catch (ex) { keepGoing = 0; }
    159                 }
    160             }
    161         }
    162         return counter;
    163     },
    164 
    165     getCardForAddress: function(recipient) {
    166         // enumerate all of the address books on this system
    167         var parentDir = vI_addressBook.rdfService.GetResource("moz-abdirectory://").QueryInterface(Components.interfaces.nsIAbDirectory);
    168         var enumerator = parentDir.childNodes;
    169         if (!enumerator) {vI_notificationBar.dump("## vI_addressBook: no addressbooks?\n"); return null;} // uups, no addressbooks?
    170        
    171         var splittedRecipient = vI.helper.parseAddress(recipient);
    172         var queryString;
    173         if (splittedRecipient.email) {
    174             queryString = "?(or(PrimaryEmail,c," + encodeURIComponent(splittedRecipient.email) + ")(SecondEmail,c," +
    175                 encodeURIComponent(splittedRecipient.email) + "))";
    176             vI_notificationBar.dump("## vI_addressBook: Search '" + splittedRecipient.email + "' in addressbook emails.\n")
    177         }
    178         else {
    179             queryString = "?(or(DisplayName,c," +
    180                 encodeURIComponent(splittedRecipient.name) + "))";
    181             vI_notificationBar.dump("## vI_addressBook: Search '" + splittedRecipient.name + "' in addressbook displayed names.\n")
    182         }
    183         var matchingEmailCards = { number : 0, cards : {} }
    184         var matchingFullNameCards = { number : 0, cards : {} }
    185         while (enumerator && enumerator.hasMoreElements()) {
    186             var addrbook = enumerator.getNext();  // an addressbook directory
    187             addrbook.QueryInterface(Components.interfaces.nsIAbDirectory);
    188             var searchUri = (addrbook.directoryProperties?addrbook.directoryProperties.URI:addrbook.URI) + queryString;  // search for the address in this book
     203            var searchUri = (addrbook.directoryProperties?addrbook.directoryProperties.URI:addrbook.URI) + queryString;
    189204            vI_notificationBar.dump("## vI_addressBook: searchUri '" + searchUri + "'\n");
    190             var directory = vI_addressBook.rdfService.GetResource(searchUri).QueryInterface(Components.interfaces.nsIAbDirectory);
    191            
    192             //~ var AbView = Components.classes["@mozilla.org/addressbook/abview;1"].createInstance(Components.interfaces.nsIAbView);
    193             //~ AbView.init(searchUri, true, null, "GeneratedName", "ascending");
    194             //~ var directory = AbView.directory;
    195            
    196             // directory will now be a subset of the addressbook containing only those cards that match the searchstring 'address'
     205            //~ var directory = vI_addressBook.rdfService.GetResource(searchUri).QueryInterface(Components.interfaces.nsIAbDirectory);
     206           
     207            var AbView = Components.classes["@mozilla.org/addressbook/abview;1"].createInstance(Components.interfaces.nsIAbView);
     208            AbView.init(searchUri, true, null, "GeneratedName", "ascending");
     209            var directory = AbView.directory;
     210           
     211            // directory will now be a subset of the addressbook containing only those cards that match the searchstring
    197212            if (!directory) break;
    198213            var childCards = null; var keepGoing = 1;
     
    205220                currentCard.QueryInterface(Components.interfaces.nsIAbCard);
    206221                vI_notificationBar.dump("## vI_addressBook:             checking '" + currentCard.displayName + "'.\n")
    207                 if (splittedRecipient.email) {
    208                     if (currentCard.primaryEmail.toLowerCase() == splittedRecipient.email.toLowerCase() ||
    209                         currentCard.secondEmail.toLowerCase() == splittedRecipient.email.toLowerCase()) {
    210                         vI_notificationBar.dump("## vI_addressBook: card found, primaryEmail '" + currentCard.primaryEmail.toLowerCase() + "'.\n")
    211                         if (splittedRecipient.name != "" && currentCard.displayName == splittedRecipient.name) {
    212                             vI_notificationBar.dump("## vI_addressBook:             matching displayed Name '" + currentCard.displayName + "'.\n")
    213                             matchingFullNameCards.cards[matchingFullNameCards.number++] = currentCard;
    214                         }
    215                         else matchingEmailCards.cards[matchingEmailCards.number++] = currentCard;
    216                     }
     222                returnVar = callFunction(addrbook, currentCard, returnVar);
     223                try { childCards.next(); } catch (ex) { keepGoing = 0; }
     224            }
     225        }
     226        return returnVar;
     227    },
     228
     229    _findMatchingCard : function (addrbook, Card, returnVar) {
     230    //  var returnVar = { email : email, name : name, mailListName : mailListName,
     231    //      matchingEmailCards : { number : 0, cards : {} }, matchingFullNameCards : { number : 0, cards : {} } }
     232        if (addrbook.dirName == "Virtual Identity") return returnVar
     233        if (returnVar.email) {
     234            if (Card.primaryEmail.toLowerCase() == returnVar.email.toLowerCase() ||
     235                Card.secondEmail.toLowerCase() == returnVar.email.toLowerCase()) {
     236                vI_notificationBar.dump("## vI_addressBook: card found, primaryEmail '" + Card.primaryEmail.toLowerCase() + "'.\n")
     237                if (returnVar.name != "" && Card.displayName == returnVar.name) {
     238                    vI_notificationBar.dump("## vI_addressBook:             matching displayed Name '" + Card.displayName + "'.\n")
     239                    returnVar.matchingFullNameCards.cards[returnVar.matchingFullNameCards.number++] = Card;
    217240                }
    218                 else {
    219                     if (splittedRecipient.name != "" && currentCard.displayName == splittedRecipient.name) {
    220                         vI_notificationBar.dump("## vI_addressBook:             matching displayed Name '" + currentCard.displayName + "'.\n")
    221                         matchingFullNameCards.cards[matchingFullNameCards.number++] = currentCard;
    222                     }
    223                 }
    224                 try { childCards.next(); } catch (ex) { keepGoing = 0; }
    225             }
    226         }
    227        
    228         vI_notificationBar.dump("## vI_addressBook: found " + matchingEmailCards.number + " card(s) with matching email.\n")
     241                else returnVar.matchingEmailCards.cards[returnVar.matchingEmailCards.number++] = Card;
     242            }
     243        }
     244        else if (returnVar.name != "" && Card.displayName == returnVar.name) {
     245            vI_notificationBar.dump("## vI_addressBook:             matching displayed Name '" + Card.displayName + "'.\n")
     246            returnVar.matchingFullNameCards.cards[returnVar.matchingFullNameCards.number++] = Card;
     247        }
     248        return returnVar;
     249    },
     250
     251    _findMatchingCardInVIAbook : function(addrbook, Card, returnVar) {
     252        vI_notificationBar.dump("## vI_addressBook:             aBook '" + addrbook.dirName + "'.\n")
     253        if (!returnVar.Card && addrbook.dirName == "Virtual Identity" &&
     254            Card.displayName.toLowerCase() == returnVar.name.toLowerCase() ) {
     255            vI_notificationBar.dump("## vI_addressBook:             found CardInVIAbook.\n")
     256            return { name: returnVar.name, Card : Card }
     257        }
     258        else return returnVar
     259    },
     260
     261    // newsgroup and mailin-list entries are stored in a separate Virtual Identity AddressBook
     262    getCardForAddressFromVIAbook : function(name) {
     263        vI_notificationBar.dump("## vI_addressBook: getCardForAddressFromVIAbook '" + name + "' \n")
     264        queryString = "?(or(DisplayName,c," + encodeURIComponent(name) + "))";
     265        vI_notificationBar.dump("## vI_addressBook: Search '" + name + "' in VI addressbook displayed names.\n")
     266        var returnVar = vI_addressBook._walkTroughCards(queryString, vI_addressBook._findMatchingCardInVIAbook,
     267            { name : name, Card : null } )
     268        return returnVar.Card       
     269    },
     270   
     271    _isMailingListCard : function (addrbook, Card, returnVar) {
     272    // returnVar = { mailListName : mailListName, isMailList : false }
     273        return (returnVar.isMailList || addrbook.dirName != "Virtual Identity" &&
     274            Card.isMailList && Card.displayName.toLowerCase() == returnVar.mailListName.toLowerCase())
     275    },
     276   
     277    getMailListName : function(recipient) {
     278        if (recipient.match(/<[^>]*>/) || recipient.match(/$/)) {
     279            mailListName = RegExp.leftContext + RegExp.rightContext
     280            mailListName = mailListName.replace(/^\s+|\s+$/g,"")
     281        }
     282        return mailListName;
     283    },
     284   
     285    isMailingList: function(recipient) {
     286        vI_notificationBar.dump("## vI_addressBook: isMailingList '" + recipient + "' \n")
     287        queryString = "?(or(DisplayName,c," + encodeURIComponent(vI_addressBook.getMailListName(recipient)) + "))"
     288        var returnVar = vI_addressBook._walkTroughCards(queryString, vI_addressBook._isMailingListCard,
     289            { mailListName : mailListName, isMailList : false } )
     290        return returnVar.isMailList;
     291    },
     292   
     293    getCardForAddress: function(recipient, recipientType) {
     294        if (recipientType == "addr_newsgroups")
     295            return vI_addressBook.getCardForAddressFromVIAbook(recipient)
     296        else if (vI_addressBook.isMailingList(recipient))
     297            return vI_addressBook.getCardForAddressFromVIAbook(getMailListName(recipient))
     298        else
     299            return vI_addressBook.getCardForAddressFromNonVIAbook(recipient)
     300    },
     301   
     302    getCardForAddressFromNonVIAbook :function (recipient) {
     303        var queryString; var email = null; var name = null;
     304       
     305        var splittedRecipient = vI.helper.parseAddress(recipient)
     306        email = splittedRecipient.email; name = splittedRecipient.name
     307        queryString = "?(or(PrimaryEmail,c," + encodeURIComponent(email) + ")(SecondEmail,c," + encodeURIComponent(email) + "))"
     308
     309        vI_notificationBar.dump("## vI_addressBook: Search '" + email + "' in addressbook emails.\n")
     310
     311        var returnVar = { email : email, name : name, mailListName : mailListName,
     312            matchingEmailCards : { number : 0, cards : {} }, matchingFullNameCards : { number : 0, cards : {} } }
     313        returnVar = vI_addressBook._walkTroughCards(queryString, vI_addressBook._findMatchingCard, returnVar)
     314        var matchingEmailCards = returnVar.matchingEmailCards; var matchingFullNameCards = returnVar.matchingFullNameCards;
     315       
     316        vI_notificationBar.dump("## vI_addressBook: found " + matchingEmailCards.number + " card(s) with matching email (or lists).\n")
    229317        vI_notificationBar.dump("## vI_addressBook: found " + matchingFullNameCards.number + " card(s) with matching displayed Name.\n")
     318       
    230319        // prefer matchingFullNameCards over matchingEmailCards
    231         var matchingCards = matchingEmailCards
     320        matchingCards = matchingEmailCards
    232321        if (matchingFullNameCards.number > 0) matchingCards = matchingFullNameCards
    233322       
     
    242331       
    243332        // upps, more than one matching address found
    244         vI_notificationBar.dump("## vI_addressBook WARNING: " + matchingEmailCards.number + " matching entries found.\n")
     333        vI_notificationBar.dump("## vI_addressBook WARNING: " + matchingCards.number + " matching entries found.\n")
    245334               
    246335        for (index = 0; index < matchingCards.number; index++) {
     
    331420        }
    332421       
    333         var email = inputElement.value
    334         if (email == "") {
    335             vI_notificationBar.dump("## vI_addressBook: no email found, not checked.\n"); return;
     422        if (inputElement.value == "") {
     423            vI_notificationBar.dump("## vI_addressBook: no recipient found, not checked.\n"); return;
    336424        }
    337425       
    338426        var row = inputElement.id.replace(/^addressCol2#/,"")
    339         if (vI_addressBook.lastCheckedEmail[row] && vI_addressBook.lastCheckedEmail[row] == email) {
     427        if (vI_addressBook.lastCheckedEmail[row] && vI_addressBook.lastCheckedEmail[row] == inputElement.value) {
    340428            vI_notificationBar.dump("## vI_addressBook: same email than before, not checked again.\n"); return;
    341429        }
    342         vI_addressBook.lastCheckedEmail[row] = email;
    343        
    344         var Card = vI_addressBook.getCardForAddress(email); if (!Card) return;
     430        vI_addressBook.lastCheckedEmail[row] = inputElement.value;
     431       
     432        var Card = vI_addressBook.getCardForAddress(inputElement.value, recipientType); if (!Card) return;
    345433       
    346434        // found Card, so store InputElement
     
    394482    },
    395483   
    396     updateABookFromVIdentity : function(email) {
    397         var Card = vI_addressBook.getCardForAddress(email)
     484    updateABookFromVIdentity : function(recipient, recipientType) {
     485        var Card = vI_addressBook.getCardForAddress(recipient, recipientType)
    398486        if (!Card) return;
    399487       
     
    465553            if (recipientType == "addr_reply" || recipientType == "addr_followup" ||
    466554                awGetInputElement(row).value.match(/^\s*$/) ) continue;
    467             if (recipientType == "addr_newsgroups" && vI_addressBook.prefroot.getBoolPref("mail.collect_email_address_outgoing") &&
    468                 (!vI_addressBook.getCardForAddress(awGetInputElement(row).value))) {
    469                
    470                 newCard = Components.classes["@mozilla.org/addressbook/cardproperty;1"]
    471                     .createInstance(Components.interfaces.nsIAbCard);
    472                 newCard.displayName = awGetInputElement(row).value;
    473                 newCard.notes = vI_addressBook.addNote
    474                
    475                 var aBook = vI_addressBook.rdfService.GetResource(vI_addressBook.prefroot.getCharPref("mail.collect_addressbook"))
    476                     .QueryInterface(Components.interfaces.nsIAbDirectory);
    477                 aBook.addCard(newCard)
    478             }
    479             window.setTimeout(vI_addressBook.updateABookFromVIdentity, 50, awGetInputElement(row).value)
     555            if (recipientType == "addr_newsgroups") vI_addressBook.addNewCard(awGetInputElement(row).value, recipientType)
     556            window.setTimeout(vI_addressBook.updateABookFromVIdentity, 50, awGetInputElement(row).value, recipientType)
     557        }
     558    },
     559   
     560    addNewCard : function(recipient, recipientType) {
     561        if (recipientType == "addr_newsgroups" && vI_addressBook.prefroot.getBoolPref("mail.collect_email_address_outgoing") &&
     562            (!vI_addressBook.getCardForAddress(recipient,recipientType))) {
     563           
     564            newCard = Components.classes["@mozilla.org/addressbook/cardproperty;1"]
     565                .createInstance(Components.interfaces.nsIAbCard);
     566            newCard.displayName = recipient;
     567            newCard.notes = vI_addressBook.addNote
     568           
     569            var aBook = vI_addressBook.rdfService.GetResource(vI_addressBook.prefroot.getCharPref("mail.collect_addressbook"))
     570                .QueryInterface(Components.interfaces.nsIAbDirectory);
     571            aBook.addCard(newCard)
    480572        }
    481573    },
     
    492584            if (recipientType == "addr_reply" || recipientType == "addr_followup") continue;
    493585            vI_addressBook.lastCheckedEmail[row] = awGetInputElement(row).value;
    494             var Card = vI_addressBook.getCardForAddress(awGetInputElement(row).value);
     586            var Card = vI_addressBook.getCardForAddress(awGetInputElement(row).value,recipientType);
    495587            if (!Card) continue;
    496588            var addresses = vI_addressBook.readVIdentityFromCard(Card);
Note: See TracChangeset for help on using the changeset viewer.