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) 2011 |
---|
20 | the Initial Developer. All Rights Reserved. |
---|
21 | |
---|
22 | Contributor(s): |
---|
23 | * ***** END LICENSE BLOCK ***** */ |
---|
24 | |
---|
25 | var EXPORTED_SYMBOLS = ["getAccountsArray", "getIdentitiesArray"] |
---|
26 | |
---|
27 | const { |
---|
28 | classes: Cc, |
---|
29 | interfaces: Ci, |
---|
30 | utils: Cu, |
---|
31 | results: Cr |
---|
32 | } = Components; |
---|
33 | |
---|
34 | Cu.import("resource://v_identity/vI_log.js"); |
---|
35 | let Log = setupLogging("virtualIdentity.accountUtils"); |
---|
36 | |
---|
37 | let accountManager = Components.classes["@mozilla.org/messenger/account-manager;1"] |
---|
38 | .getService(Components.interfaces.nsIMsgAccountManager); |
---|
39 | |
---|
40 | // copy/pasted from MsgComposeCommands.js |
---|
41 | function queryISupportsArray(supportsArray, iid) { |
---|
42 | let count = supportsArray.Count(); |
---|
43 | var result = new Array |
---|
44 | for (let i = 0; i < count; i++) |
---|
45 | result[i] = supportsArray.QueryElementAt(i, iid); |
---|
46 | return result; |
---|
47 | }; |
---|
48 | |
---|
49 | function getIdentitiesArray(account) { |
---|
50 | var identities; |
---|
51 | if (Components.utils.import("resource:///modules/folderUtils.jsm") && Components.utils.import("resource:///modules/iteratorUtils.jsm") && typeof (toArray) == 'function' && typeof (fixIterator) == 'function') { |
---|
52 | identities = toArray(fixIterator(account.identities, Components.interfaces.nsIMsgIdentity)); |
---|
53 | } else { |
---|
54 | identities = queryISupportsArray(accounts[i].identities, Components.interfaces.nsIMsgIdentity); |
---|
55 | } |
---|
56 | return identities; |
---|
57 | } |
---|
58 | |
---|
59 | function getAccountsArray() { |
---|
60 | var accounts; |
---|
61 | |
---|
62 | function sortAccounts(a, b) { |
---|
63 | if (a.key == accountManager.defaultAccount.key) |
---|
64 | return -1; |
---|
65 | if (b.key == accountManager.defaultAccount.key) |
---|
66 | return 1; |
---|
67 | var aIsNews = a.incomingServer.type == "nntp"; |
---|
68 | var bIsNews = b.incomingServer.type == "nntp"; |
---|
69 | if (aIsNews && !bIsNews) |
---|
70 | return 1; |
---|
71 | if (bIsNews && !aIsNews) |
---|
72 | return -1; |
---|
73 | |
---|
74 | var aIsLocal = a.incomingServer.type == "none"; |
---|
75 | var bIsLocal = b.incomingServer.type == "none"; |
---|
76 | if (aIsLocal && !bIsLocal) |
---|
77 | return 1; |
---|
78 | if (bIsLocal && !aIsLocal) |
---|
79 | return -1; |
---|
80 | return 0; |
---|
81 | } |
---|
82 | if (Components.utils.import("resource:///modules/folderUtils.jsm") && Components.utils.import("resource:///modules/iteratorUtils.jsm") && typeof (allAccountsSorted) == 'function') { |
---|
83 | // if this worked we are having at least seamonkey 1.17 |
---|
84 | // Log.debug("getAccounts - new schema"); |
---|
85 | accounts = allAccountsSorted(true); |
---|
86 | } else { |
---|
87 | // still some older version |
---|
88 | // Log.debug("getAccounts - old schema"); |
---|
89 | var accounts = queryISupportsArray(accountManager.accounts, |
---|
90 | Components.interfaces.nsIMsgAccount); |
---|
91 | |
---|
92 | // Ugly hack to work around bug 41133. :-( |
---|
93 | accounts = accounts.filter(function isNonSuckyAccount(a) { |
---|
94 | return !!a.incomingServer; |
---|
95 | }); |
---|
96 | accounts.sort(sortAccounts); |
---|
97 | } |
---|
98 | return accounts |
---|
99 | }; |
---|