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 | |
---|
25 | var EXPORTED_SYMBOLS = ["vIaccount_cleanupSystem"] |
---|
26 | |
---|
27 | const { |
---|
28 | classes: Cc, |
---|
29 | interfaces: Ci, |
---|
30 | utils: Cu, |
---|
31 | results: Cr |
---|
32 | } = Components; |
---|
33 | |
---|
34 | Cu.import("resource://gre/modules/Services.jsm"); |
---|
35 | Cu.import("resource://v_identity/vI_log.js"); |
---|
36 | Cu.import("resource://v_identity/vI_identityData.js"); |
---|
37 | Cu.import("resource://v_identity/vI_rdfDatasource.js"); |
---|
38 | Cu.import("resource://v_identity/vI_prefs.js"); |
---|
39 | Cu.import("resource://v_identity/vI_accountUtils.js"); |
---|
40 | |
---|
41 | let Log = setupLogging("virtualIdentity.account"); |
---|
42 | |
---|
43 | var account = { |
---|
44 | // checks if directory is empty, not really used |
---|
45 | // ignores files ending with *.msf, else reports if a non-zero file is found. |
---|
46 | __dirEmpty: function (directory) { |
---|
47 | var dirEnumerator = directory.directoryEntries; |
---|
48 | while (dirEnumerator.hasMoreElements()) { |
---|
49 | var maildir = dirEnumerator.getNext(); |
---|
50 | maildir.QueryInterface(Ci.nsIFile); |
---|
51 | // recurse into all subdirectories |
---|
52 | if (maildir.isDirectory() && |
---|
53 | !account.__dirEmpty(maildir)) return false; |
---|
54 | // ignore files with ending "*.msf" |
---|
55 | if (!maildir.path.match(new RegExp(".*\.msf$", "i")) && |
---|
56 | maildir.fileSize != 0) return false; |
---|
57 | } |
---|
58 | return true; |
---|
59 | }, |
---|
60 | |
---|
61 | __cleanupDirectories: function () { |
---|
62 | Log.debug("checking for leftover VirtualIdentity directories ...") |
---|
63 | |
---|
64 | var file = Cc["@mozilla.org/file/directory_service;1"] |
---|
65 | .getService(Ci.nsIProperties) |
---|
66 | .get("ProfD", Ci.nsIFile); |
---|
67 | |
---|
68 | var fileEnumerator = file.directoryEntries |
---|
69 | while (fileEnumerator.hasMoreElements()) { |
---|
70 | var dir = fileEnumerator.getNext() |
---|
71 | dir.QueryInterface(Ci.nsIFile); |
---|
72 | if (dir.path.match(new RegExp("[/\\\\]Mail$", "i"))) { // match Windows and Linux/Mac separators |
---|
73 | var dirEnumerator = dir.directoryEntries |
---|
74 | while (dirEnumerator.hasMoreElements()) { |
---|
75 | var maildir = dirEnumerator.getNext() |
---|
76 | maildir.QueryInterface(Ci.nsIFile); |
---|
77 | // match Windows and Linux/Mac separators |
---|
78 | if (maildir.path.match(new RegExp("[/\\\\]virtualIdentity.*$", "i"))) { |
---|
79 | // should be empty, VirtualIdentity never uses those directories |
---|
80 | if (account.__dirEmpty(maildir)) { |
---|
81 | try { |
---|
82 | maildir.remove(true) |
---|
83 | } catch (e) {} |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | Log.debug("done.") |
---|
90 | }, |
---|
91 | |
---|
92 | cleanupSystem: function () { |
---|
93 | Log.debug("checking for leftover VirtualIdentity accounts ...") |
---|
94 | var accounts = getAccountsArray(); |
---|
95 | |
---|
96 | for (let acc = 0; acc < accounts.length; acc++) { |
---|
97 | let checkAccount = accounts[acc]; |
---|
98 | if (account.__isVIdentityAccount(checkAccount)) { |
---|
99 | account.__removeAccount(checkAccount); |
---|
100 | } |
---|
101 | // replace account with key, required for next check |
---|
102 | accounts[acc] = accounts[acc].key; |
---|
103 | } |
---|
104 | |
---|
105 | // account-prefs are not removed, grrrr --> https://bugzilla.mozilla.org/show_bug.cgi?id=875675 |
---|
106 | // compare against all accounts, getAccountsArray() does not include 'smart mailboxes' == 'unified folders' |
---|
107 | var all_accounts = prefroot.getCharPref("mail.accountmanager.accounts").split(","); |
---|
108 | try { |
---|
109 | var lastAccountKey = prefroot.getIntPref("mail.account.lastKey"); |
---|
110 | for (let key = 0; key <= lastAccountKey; key++) { |
---|
111 | if (all_accounts.indexOf("account" + key) > -1) continue; |
---|
112 | account.__removeAccountPrefs("account" + key); |
---|
113 | } |
---|
114 | } catch (e) {}; |
---|
115 | Log.debug("done.") |
---|
116 | account.__cleanupDirectories(); |
---|
117 | }, |
---|
118 | |
---|
119 | __isVIdentityAccount: function (checkAccount) { |
---|
120 | // check for new (post0.5.0) accounts, |
---|
121 | try { |
---|
122 | prefroot.getBoolPref("mail.account." + checkAccount.key + ".vIdentity"); |
---|
123 | return true; |
---|
124 | } catch (e) {}; |
---|
125 | // check for old (pre 0.5.0) accounts |
---|
126 | if (checkAccount.incomingServer && checkAccount.incomingServer.hostName == "virtualIdentity") return true; |
---|
127 | return false; |
---|
128 | }, |
---|
129 | |
---|
130 | __removeAccountPrefs: function (key) { |
---|
131 | // remove the additional tagging-pref |
---|
132 | try { |
---|
133 | prefroot.clearUserPref("mail.account." + key + ".vIdentity"); |
---|
134 | } catch (e) {}; |
---|
135 | try { |
---|
136 | // account-prefs are not removed, grrrr --> https://bugzilla.mozilla.org/show_bug.cgi?id=875675 |
---|
137 | prefroot.clearUserPref("mail.account." + key + ".server"); |
---|
138 | } catch (e) {}; |
---|
139 | try { |
---|
140 | // account-prefs are not removed, grrrr --> https://bugzilla.mozilla.org/show_bug.cgi?id=875675 |
---|
141 | prefroot.clearUserPref("mail.account." + key + ".identities"); |
---|
142 | } catch (e) {}; |
---|
143 | }, |
---|
144 | |
---|
145 | __removeAccount: function (checkAccount) { |
---|
146 | Log.debug("__removeAccount") |
---|
147 | // in new (post 0.5.0) Virtual Identity accounts the incomingServer of the account |
---|
148 | // points to an incoming server of a different account. Cause the internal |
---|
149 | // removeAccount function tries to removes the incomingServer ether, create |
---|
150 | // a real one before calling this function. |
---|
151 | if (!checkAccount.incomingServer || checkAccount.incomingServer.hostName != "virtualIdentity") { |
---|
152 | // if not some of the 'old' accounts |
---|
153 | checkAccount.incomingServer = account._AccountManager. |
---|
154 | createIncomingServer("toRemove", "virtualIdentity", "pop3"); |
---|
155 | } |
---|
156 | |
---|
157 | // remove the rootFolder of the account |
---|
158 | try { |
---|
159 | checkAccount.incomingServer.rootFolder.Delete(); |
---|
160 | } catch (e) {}; |
---|
161 | |
---|
162 | var key = checkAccount.key; |
---|
163 | Log.debug("removing account " + key) |
---|
164 | // remove the account |
---|
165 | account._AccountManager.removeAccount(checkAccount); |
---|
166 | |
---|
167 | // prevent useless increasing of lastKey https://bugzilla.mozilla.org/show_bug.cgi?id=485839 |
---|
168 | try { |
---|
169 | var lastAccountKey = prefroot.getIntPref("mail.account.lastKey"); |
---|
170 | if ("account" + lastAccountKey == key) |
---|
171 | prefroot.setIntPref("mail.account.lastKey", lastAccountKey - 1); |
---|
172 | } catch (e) {}; |
---|
173 | |
---|
174 | account.__removeAccountPrefs(key); |
---|
175 | }, |
---|
176 | } |
---|
177 | var vIaccount_cleanupSystem = account.cleanupSystem; |
---|