1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "prprf.h"
#include "plstr.h"
#include "prmem.h"
#include "nsIComponentManager.h"
#include "nsIServiceManager.h"
#include "nsCRTGlue.h"
#include "nsCOMPtr.h"
#include "nsIMsgFolderNotificationService.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#include "nsMsgBaseCID.h"
#include "nsMsgAccount.h"
#include "nsIMsgAccount.h"
#include "nsIMsgAccountManager.h"
#include "nsServiceManagerUtils.h"
#include "nsMemory.h"
#include "nsComponentManagerUtils.h"
#include "nsMsgUtils.h"
#include "nsArrayUtils.h"
NS_IMPL_ISUPPORTS(nsMsgAccount, nsIMsgAccount)
nsMsgAccount::nsMsgAccount()
: mTriedToGetServer(false)
{
}
nsMsgAccount::~nsMsgAccount()
{
}
nsresult
nsMsgAccount::getPrefService()
{
if (m_prefs)
return NS_OK;
nsresult rv;
NS_ENSURE_FALSE(m_accountKey.IsEmpty(), NS_ERROR_NOT_INITIALIZED);
nsCOMPtr<nsIPrefService> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString accountRoot("mail.account.");
accountRoot.Append(m_accountKey);
accountRoot.Append('.');
return prefs->GetBranch(accountRoot.get(), getter_AddRefs(m_prefs));
}
NS_IMETHODIMP
nsMsgAccount::GetIncomingServer(nsIMsgIncomingServer **aIncomingServer)
{
NS_ENSURE_ARG_POINTER(aIncomingServer);
// create the incoming server lazily
if (!mTriedToGetServer && !m_incomingServer) {
mTriedToGetServer = true;
// ignore the error (and return null), but it's still bad so warn
mozilla::DebugOnly<nsresult> rv = createIncomingServer();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "couldn't lazily create the server\n");
}
NS_IF_ADDREF(*aIncomingServer = m_incomingServer);
return NS_OK;
}
NS_IMETHODIMP
nsMsgAccount::CreateServer()
{
if (m_incomingServer)
return NS_ERROR_ALREADY_INITIALIZED;
return createIncomingServer();
}
nsresult
nsMsgAccount::createIncomingServer()
{
// from here, load mail.account.myaccount.server
// Load the incoming server
//
// ex) mail.account.myaccount.server = "myserver"
nsresult rv = getPrefService();
NS_ENSURE_SUCCESS(rv, rv);
// get the "server" pref
nsCString serverKey;
rv = m_prefs->GetCharPref("server", getter_Copies(serverKey));
NS_ENSURE_SUCCESS(rv, rv);
// get the server from the account manager
nsCOMPtr<nsIMsgAccountManager> accountManager =
do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIMsgIncomingServer> server;
rv = accountManager->GetIncomingServer(serverKey, getter_AddRefs(server));
NS_ENSURE_SUCCESS(rv, rv);
// store the server in this structure
m_incomingServer = server;
accountManager->NotifyServerLoaded(server);
return NS_OK;
}
NS_IMETHODIMP
nsMsgAccount::SetIncomingServer(nsIMsgIncomingServer *aIncomingServer)
{
NS_ENSURE_ARG_POINTER(aIncomingServer);
nsCString key;
nsresult rv = aIncomingServer->GetKey(key);
if (NS_SUCCEEDED(rv)) {
rv = getPrefService();
NS_ENSURE_SUCCESS(rv, rv);
m_prefs->SetCharPref("server", key.get());
}
m_incomingServer = aIncomingServer;
bool serverValid;
(void) aIncomingServer->GetValid(&serverValid);
// only notify server loaded if server is valid so
// account manager only gets told about finished accounts.
if (serverValid)
{
// this is the point at which we can notify listeners about the
// creation of the root folder, which implies creation of the new server.
nsCOMPtr<nsIMsgFolder> rootFolder;
rv = aIncomingServer->GetRootFolder(getter_AddRefs(rootFolder));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIFolderListener> mailSession =
do_GetService(NS_MSGMAILSESSION_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
mailSession->OnItemAdded(nullptr, rootFolder);
nsCOMPtr<nsIMsgFolderNotificationService> notifier(do_GetService(NS_MSGNOTIFICATIONSERVICE_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
notifier->NotifyFolderAdded(rootFolder);
nsCOMPtr<nsIMsgAccountManager> accountManager = do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv))
accountManager->NotifyServerLoaded(aIncomingServer);
// Force built-in folders to be created and discovered. Then, notify listeners
// about them.
nsCOMPtr<nsISimpleEnumerator> enumerator;
rv = rootFolder->GetSubFolders(getter_AddRefs(enumerator));
NS_ENSURE_SUCCESS(rv, rv);
bool hasMore;
while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore)
{
nsCOMPtr<nsISupports> item;
enumerator->GetNext(getter_AddRefs(item));
nsCOMPtr<nsIMsgFolder> msgFolder(do_QueryInterface(item));
if (!msgFolder)
continue;
mailSession->OnItemAdded(rootFolder, msgFolder);
notifier->NotifyFolderAdded(msgFolder);
}
}
return NS_OK;
}
NS_IMETHODIMP
nsMsgAccount::GetIdentities(nsIArray **_retval)
{
NS_ENSURE_ARG_POINTER(_retval);
NS_ENSURE_TRUE(m_identities, NS_ERROR_FAILURE);
NS_IF_ADDREF(*_retval = m_identities);
return NS_OK;
}
/*
* set up the m_identities array
* do not call this more than once or we'll leak.
*/
nsresult
nsMsgAccount::createIdentities()
{
NS_ENSURE_FALSE(m_identities, NS_ERROR_FAILURE);
nsresult rv;
m_identities = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCString identityKey;
rv = getPrefService();
NS_ENSURE_SUCCESS(rv, rv);
m_prefs->GetCharPref("identities", getter_Copies(identityKey));
if (identityKey.IsEmpty()) // not an error if no identities, but
return NS_OK; // strtok will be unhappy
// get the server from the account manager
nsCOMPtr<nsIMsgAccountManager> accountManager =
do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
char* newStr = identityKey.BeginWriting();
char* token = NS_strtok(",", &newStr);
// temporaries used inside the loop
nsCOMPtr<nsIMsgIdentity> identity;
nsAutoCString key;
// iterate through id1,id2, etc
while (token) {
key = token;
key.StripWhitespace();
// create the account
rv = accountManager->GetIdentity(key, getter_AddRefs(identity));
if (NS_SUCCEEDED(rv)) {
// ignore error from addIdentityInternal() - if it fails, it fails.
rv = addIdentityInternal(identity);
NS_ASSERTION(NS_SUCCEEDED(rv), "Couldn't create identity");
}
// advance to next key, if any
token = NS_strtok(",", &newStr);
}
return rv;
}
/* attribute nsIMsgIdentity defaultIdentity; */
NS_IMETHODIMP
nsMsgAccount::GetDefaultIdentity(nsIMsgIdentity **aDefaultIdentity)
{
NS_ENSURE_ARG_POINTER(aDefaultIdentity);
NS_ENSURE_TRUE(m_identities, NS_ERROR_NOT_INITIALIZED);
*aDefaultIdentity = nullptr;
uint32_t count;
nsresult rv = m_identities->GetLength(&count);
NS_ENSURE_SUCCESS(rv, rv);
if (count == 0)
return NS_OK;
nsCOMPtr<nsIMsgIdentity> identity = do_QueryElementAt(m_identities, 0, &rv);
identity.swap(*aDefaultIdentity);
return rv;
}
NS_IMETHODIMP
nsMsgAccount::SetDefaultIdentity(nsIMsgIdentity *aDefaultIdentity)
{
NS_ENSURE_TRUE(m_identities, NS_ERROR_FAILURE);
uint32_t position = 0;
nsresult rv = m_identities->IndexOf(0, aDefaultIdentity, &position);
NS_ENSURE_SUCCESS(rv, rv);
rv = m_identities->RemoveElementAt(position);
NS_ENSURE_SUCCESS(rv, rv);
// The passed in identity is in the list, so we have at least one element.
rv = m_identities->InsertElementAt(aDefaultIdentity, 0, false);
NS_ENSURE_SUCCESS(rv, rv);
return saveIdentitiesPref();
}
// add the identity to m_identities, but don't fiddle with the
// prefs. The assumption here is that the pref for this identity is
// already set.
nsresult
nsMsgAccount::addIdentityInternal(nsIMsgIdentity *identity)
{
NS_ENSURE_TRUE(m_identities, NS_ERROR_FAILURE);
return m_identities->AppendElement(identity, false);
}
/* void addIdentity (in nsIMsgIdentity identity); */
NS_IMETHODIMP
nsMsgAccount::AddIdentity(nsIMsgIdentity *identity)
{
NS_ENSURE_ARG_POINTER(identity);
// hack hack - need to add this to the list of identities.
// for now just treat this as a Setxxx accessor
// when this is actually implemented, don't refcount the default identity
nsCString key;
nsresult rv = identity->GetKey(key);
if (NS_SUCCEEDED(rv)) {
nsCString identityList;
m_prefs->GetCharPref("identities", getter_Copies(identityList));
nsAutoCString newIdentityList(identityList);
nsAutoCString testKey; // temporary to strip whitespace
bool foundIdentity = false; // if the input identity is found
if (!identityList.IsEmpty()) {
char *newStr = identityList.BeginWriting();
char *token = NS_strtok(",", &newStr);
// look for the identity key that we're adding
while (token) {
testKey = token;
testKey.StripWhitespace();
if (testKey.Equals(key))
foundIdentity = true;
token = NS_strtok(",", &newStr);
}
}
// if it didn't already exist, append it
if (!foundIdentity) {
if (newIdentityList.IsEmpty())
newIdentityList = key;
else {
newIdentityList.Append(',');
newIdentityList.Append(key);
}
}
m_prefs->SetCharPref("identities", newIdentityList.get());
}
// now add it to the in-memory list
return addIdentityInternal(identity);
}
/* void removeIdentity (in nsIMsgIdentity identity); */
NS_IMETHODIMP
nsMsgAccount::RemoveIdentity(nsIMsgIdentity *aIdentity)
{
NS_ENSURE_ARG_POINTER(aIdentity);
NS_ENSURE_TRUE(m_identities, NS_ERROR_FAILURE);
uint32_t count = 0;
m_identities->GetLength(&count);
// At least one identity must stay after the delete.
NS_ENSURE_TRUE(count > 1, NS_ERROR_FAILURE);
uint32_t pos = 0;
nsresult rv = m_identities->IndexOf(0, aIdentity, &pos);
NS_ENSURE_SUCCESS(rv, rv);
// remove our identity
m_identities->RemoveElementAt(pos);
// clear out the actual pref values associated with the identity
aIdentity->ClearAllValues();
return saveIdentitiesPref();
}
nsresult
nsMsgAccount::saveIdentitiesPref()
{
nsAutoCString newIdentityList;
// Iterate over the existing identities and build the pref value,
// a string of identity keys: id1, id2, idX...
uint32_t count;
nsresult rv = m_identities->GetLength(&count);
NS_ENSURE_SUCCESS(rv, rv);
nsCString key;
for (uint32_t index = 0; index < count; index++)
{
nsCOMPtr<nsIMsgIdentity> identity = do_QueryElementAt(m_identities, index, &rv);
if (identity)
{
identity->GetKey(key);
if (!index) {
newIdentityList = key;
}
else
{
newIdentityList.Append(',');
newIdentityList.Append(key);
}
}
}
// Save the pref.
m_prefs->SetCharPref("identities", newIdentityList.get());
return NS_OK;
}
NS_IMETHODIMP nsMsgAccount::GetKey(nsACString& accountKey)
{
accountKey = m_accountKey;
return NS_OK;
}
NS_IMETHODIMP
nsMsgAccount::SetKey(const nsACString& accountKey)
{
m_accountKey = accountKey;
m_prefs = nullptr;
m_identities = nullptr;
return createIdentities();
}
NS_IMETHODIMP
nsMsgAccount::ToString(nsAString& aResult)
{
nsAutoString val;
aResult.AssignLiteral("[nsIMsgAccount: ");
aResult.Append(NS_ConvertASCIItoUTF16(m_accountKey));
aResult.AppendLiteral("]");
return NS_OK;
}
NS_IMETHODIMP
nsMsgAccount::ClearAllValues()
{
nsresult rv = getPrefService();
NS_ENSURE_SUCCESS(rv, rv);
return m_prefs->DeleteBranch("");
}
|