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

source: content/vI_smartIdentity.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: 11.8 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
25Components.utils.import("resource://v_identity/vI_nameSpaceWrapper.js");
26virtualIdentityExtension.ns(function () {
27  with(virtualIdentityExtension.LIB) {
28
29    let Log = vI.setupLogging("virtualIdentity.smartIdentity");
30
31    Components.utils.import("resource://v_identity/vI_identityData.js", virtualIdentityExtension);
32    Components.utils.import("resource://v_identity/vI_smartIdentityCollection.js", virtualIdentityExtension);
33    Components.utils.import("resource://v_identity/vI_prefs.js", virtualIdentityExtension);
34
35    var smartIdentity = {
36      messenger: Components.classes["@mozilla.org/messenger;1"].createInstance()
37        .QueryInterface(Components.interfaces.nsIMessenger),
38
39      stringBundle: Components.classes["@mozilla.org/intl/stringbundle;1"]
40        .getService(Components.interfaces.nsIStringBundleService)
41        .createBundle("chrome://v_identity/locale/v_identity.properties"),
42
43      _smartIdentityCollection: null,
44
45      // After Loading the MessageComposeDialog, check if smartIdentity is needed
46      init: function (currentWindow) {
47        var msgHdr;
48        var msgComposeTypeReference = Components.interfaces.nsIMsgCompType;
49        var newsgroup = gMsgCompose.compFields.newsgroups;
50        var autocreate = false;
51        Log.debug("msgComposeTypeReference = " + gMsgCompose.type);
52        switch (gMsgCompose.type) {
53        case msgComposeTypeReference.Reply:
54        case msgComposeTypeReference.ReplyAll:
55        case msgComposeTypeReference.ReplyToGroup: // reply to a newsgroup, would possibly be stopped later
56        case msgComposeTypeReference.ReplyToSender:
57        case msgComposeTypeReference.ReplyToSenderAndGroup: // reply to a newsgroup, would possibly be stopped later
58        case msgComposeTypeReference.ReplyWithTemplate:
59        case msgComposeTypeReference.ReplyToList:
60          Log.debug("Reply");
61          msgHdr = smartIdentity.messenger.
62          messageServiceFromURI(gMsgCompose.originalMsgURI).messageURIToMsgHdr(gMsgCompose.originalMsgURI);
63          smartIdentity._smartIdentityCollection = new vI.smartIdentityCollection(msgHdr, getCurrentIdentity(), document.getElementById("virtualIdentityExtension_msgIdentityClone").vid, newsgroup, this._getRecipients(currentWindow));
64          smartIdentity._smartIdentityCollection.Reply();
65          autocreate = false;
66          break;
67        case msgComposeTypeReference.Draft:
68        case msgComposeTypeReference.Template:
69          Log.debug("Draft");
70          msgHdr = smartIdentity.messenger.
71          messageServiceFromURI(gMsgCompose.compFields.draftId).messageURIToMsgHdr(gMsgCompose.compFields.draftId);
72          smartIdentity._smartIdentityCollection = new vI.smartIdentityCollection(msgHdr, getCurrentIdentity(), document.getElementById("virtualIdentityExtension_msgIdentityClone").vid, newsgroup, this._getRecipients(currentWindow));
73          smartIdentity._smartIdentityCollection.Draft();
74          autocreate = false;
75          break;
76        case msgComposeTypeReference.ForwardAsAttachment:
77        case msgComposeTypeReference.ForwardInline:
78        case msgComposeTypeReference.New:
79        case msgComposeTypeReference.NewsPost:
80        case msgComposeTypeReference.MailToUrl:
81          Log.debug("New Mail");
82          smartIdentity._smartIdentityCollection = new vI.smartIdentityCollection(null, getCurrentIdentity(), document.getElementById("virtualIdentityExtension_msgIdentityClone").vid, newsgroup, this._getRecipients(currentWindow));
83          // to enable composing new email with new identity: identity is hidden in subject line
84          // used for instance from conversation addon
85          var subject = gMsgCompose.compFields.subject.split(/\n/);
86          if (subject.length > 1 && subject[1] == "virtualIdentityExtension") {
87            Log.debug("NewMail() found stored identity preset: " + subject[2]);
88            smartIdentity._smartIdentityCollection.__parseHeadersWithArray(subject[2], smartIdentity._smartIdentityCollection._allIdentities);
89            gMsgCompose.compFields.subject = subject[0];
90            document.getElementById("msgSubject").value = subject[0];
91          } else smartIdentity._smartIdentityCollection.NewMail();
92          autocreate = true;
93          break;
94        }
95        if (smartIdentity._smartIdentityCollection._allIdentities.number > 0) smartIdentity.__smartIdentitySelection(autocreate);
96      },
97
98      _getRecipients: function (currentWindow) {
99        var recipients = [];
100        for (var row = 1; row <= top.MAX_RECIPIENTS; row++) {
101          var recipientType = currentWindow.awGetPopupElement(row).selectedItem.getAttribute("value");
102          if (recipientType == "addr_reply" || recipientType == "addr_followup" ||
103            vI.storage.isDoBcc(row, currentWindow) || currentWindow.awGetInputElement(row).value.match(/^\s*$/)) continue;
104          recipients.push({
105            recipient: currentWindow.awGetInputElement(row).value,
106            recipientType: recipientType
107          });
108        }
109        return recipients;
110      },
111
112      __smartIdentitySelection: function (autocreate) {
113        Log.debug("__smartIdentitySelection autocreate=" + autocreate);
114
115        if (vI.vIprefs.get("idSelection_preferExisting")) {
116          var existingIDIndex = smartIdentity._smartIdentityCollection._foundExistingIdentity();
117          if (existingIDIndex) {
118            Log.debug("found existing Identity, use without interaction.");
119            // add all Indentities to Clone Menu before selecting and leaving the function
120            document.getElementById("virtualIdentityExtension_msgIdentityClone").addIdentitiesToCloneMenu(smartIdentity._smartIdentityCollection._allIdentities);
121            smartIdentity.changeIdentityToSmartIdentity(smartIdentity._smartIdentityCollection._allIdentities, existingIDIndex.key);
122            return;
123          }
124        }
125
126        document.getElementById("virtualIdentityExtension_msgIdentityClone").addIdentitiesToCloneMenu(smartIdentity._smartIdentityCollection._allIdentities);
127        Log.debug("__smartIdentitySelection _allIdentities.number=" +
128          smartIdentity._smartIdentityCollection._allIdentities.number +
129          " _ask_always=" + vI.vIprefs.get("idSelection_ask_always") +
130          " _ask=" + vI.vIprefs.get("idSelection_ask"));
131        if (!autocreate && vI.vIprefs.get("idSelection_ask") &&
132          ((smartIdentity._smartIdentityCollection._allIdentities.number == 1 && vI.vIprefs.get("idSelection_ask_always")) || smartIdentity._smartIdentityCollection._allIdentities.number > 1)) {
133          for (var index = 0; index < smartIdentity._smartIdentityCollection._allIdentities.number; index++) {
134            Log.debug("smartIdentityReplyDialog index=" + index + ": '" + smartIdentity._smartIdentityCollection._allIdentities.identityDataCollection[index].combinedName + "' " + "(" + smartIdentity._smartIdentityCollection._allIdentities.identityDataCollection[index].id.value + "," + smartIdentity._smartIdentityCollection._allIdentities.identityDataCollection[index].smtp.value + ")");
135          }
136          window.openDialog("chrome://v_identity/content/vI_smartReplyDialog.xul", 0,
137            "chrome, dialog, modal, alwaysRaised, resizable=yes",
138            smartIdentity._smartIdentityCollection._allIdentities,
139            /* callback: */
140            smartIdentity.changeIdentityToSmartIdentity).focus();
141        } else if (autocreate || vI.vIprefs.get("idSelection_autocreate")) {
142          smartIdentity.changeIdentityToSmartIdentity(smartIdentity._smartIdentityCollection._allIdentities, 0);
143        }
144      },
145
146      changeIdentityToSmartIdentity: function (allIdentities, selectedValue) {
147        Log.debug("changeIdentityToSmartIdentity selectedValue=" + selectedValue + " from " + allIdentities.number);
148        Log.debug("changeIdentityToSmartIdentity selectedValue=" + selectedValue + ": '" + allIdentities.identityDataCollection[selectedValue].combinedName + "' " + "(" + allIdentities.identityDataCollection[selectedValue].id.value + "," + allIdentities.identityDataCollection[selectedValue].smtp.value + ")");
149        document.getElementById("virtualIdentityExtension_msgIdentityClone").selectedMenuItem = allIdentities.menuItems[selectedValue];
150        if (document.getElementById("virtualIdentityExtension_msgIdentityClone").vid) {
151          var label = smartIdentity.stringBundle.GetStringFromName("vident.smartIdentity.vIUsage");
152          if (allIdentities.number > 1) label += " " + smartIdentity.stringBundle.GetStringFromName("vident.smartIdentity.moreThanOne");
153          vI.SmartReplyNotification.info(label + ".");
154        }
155        smartIdentity.__removeSmartIdentityFromRecipients(allIdentities, selectedValue);
156      },
157
158      __removeSmartIdentityFromRecipients: function (allIdentities, index) {
159        if (!vI.vIprefs.get("idSelection_removeSmartIdentityFromRecipients")) return;
160
161        // check if selected email is defined as doBcc address. If so, it should not be removed.
162        var skip_bcc = false;
163        if (getCurrentIdentity().doBcc) {
164          var bcc_addresses = new vI.identityCollection();
165          smartIdentity.__parseHeadersWithArray(getCurrentIdentity().doBccList, bcc_addresses);
166
167          for (var i = 0; i < bcc_addresses.number; i++) {
168            if (allIdentities.identityDataCollection[index].email == bcc_addresses.identityDataCollection[i].email) {
169              skip_bcc = true;
170              break;
171            }
172          }
173        }
174
175        // check if there is more than one recipient for this mail. If not, preserve the only one existing.
176        var recipientCount = 0;
177        for (var row = 1; row <= top.MAX_RECIPIENTS; row++) {
178          var recipientType = awGetPopupElement(row).selectedItem.getAttribute("value");
179          if (recipientType == "addr_to" || recipientType == "addr_cc") recipientCount++;
180        }
181        if (recipientCount < 2) return;
182
183
184        for (var row = 1; row <= top.MAX_RECIPIENTS; row++) {
185          var popup = awGetPopupElement(row);
186          var input = awGetInputElement(row);
187          var recipientType = popup.selectedItem.getAttribute("value");
188          // if the entry is not a recipient, just continue
189          if (recipientType == "addr_reply" || recipientType == "addr_followup") continue;
190          // check if the entry is used as a BCC selected in account settings
191          if (recipientType == "addr_bcc" && skip_bcc) continue;
192          // check if entry is matching senders address, if so, remove it
193          if (input.value == allIdentities.identityDataCollection[index].email ||
194            input.value == allIdentities.identityDataCollection[index].combinedName) {
195            awSetInputAndPopupValue(input, "", popup, "addr_to", -1);
196            awCleanupRows()
197            vI.SmartReplyNotification.info(" " + smartIdentity.stringBundle.GetStringFromName("vident.smartIdentity.remRecipient"));
198            break;
199          }
200        }
201      }
202    }
203    vI.smartIdentity = smartIdentity;
204  }
205});
Note: See TracBrowser for help on using the repository browser.