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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
|
/* -*- Mode: C++; tab-width: 2; 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 "nsAbDirProperty.h"
#include "nsAbBaseCID.h"
#include "nsIAbCard.h"
#include "nsDirPrefs.h"
#include "nsIPrefService.h"
#include "nsIPrefLocalizedString.h"
#include "nsServiceManagerUtils.h"
#include "nsComponentManagerUtils.h"
#include "prmem.h"
#include "nsIAbManager.h"
#include "nsArrayUtils.h"
// From nsDirPrefs
#define kDefaultPosition 1
nsAbDirProperty::nsAbDirProperty(void)
: m_LastModifiedDate(0),
mIsValidURI(false),
mIsQueryURI(false)
{
m_IsMailList = false;
}
nsAbDirProperty::~nsAbDirProperty(void)
{
#if 0
// this code causes a regression #138647
// don't turn it on until you figure it out
if (m_AddressList) {
uint32_t count;
nsresult rv;
rv = m_AddressList->GetLength(&count);
NS_ASSERTION(NS_SUCCEEDED(rv), "Count failed");
int32_t i;
for (i = count - 1; i >= 0; i--)
m_AddressList->RemoveElementAt(i);
}
#endif
}
NS_IMPL_ISUPPORTS(nsAbDirProperty, nsIAbDirectory, nsISupportsWeakReference,
nsIAbCollection, nsIAbItem)
NS_IMETHODIMP nsAbDirProperty::GetUuid(nsACString &uuid)
{
// XXX: not all directories have a dirPrefId...
nsresult rv = GetDirPrefId(uuid);
NS_ENSURE_SUCCESS(rv, rv);
uuid.Append('&');
nsString dirName;
GetDirName(dirName);
uuid.Append(NS_ConvertUTF16toUTF8(dirName));
return rv;
}
NS_IMETHODIMP nsAbDirProperty::GenerateName(int32_t aGenerateFormat,
nsIStringBundle *aBundle,
nsAString &name)
{
return GetDirName(name);
}
NS_IMETHODIMP nsAbDirProperty::GetPropertiesChromeURI(nsACString &aResult)
{
aResult.AssignLiteral("chrome://messenger/content/addressbook/abAddressBookNameDialog.xul");
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::GetDirName(nsAString &aDirName)
{
if (m_DirPrefId.IsEmpty())
{
aDirName = m_ListDirName;
return NS_OK;
}
nsCString dirName;
nsresult rv = GetLocalizedStringValue("description", EmptyCString(), dirName);
NS_ENSURE_SUCCESS(rv, rv);
// In TB 2 only some prefs had chrome:// URIs. We had code in place that would
// only get the localized string pref for the particular address books that
// were built-in.
// Additionally, nsIPrefBranch::getComplexValue will only get a non-user-set,
// non-locked pref value if it is a chrome:// URI and will get the string
// value at that chrome URI. This breaks extensions/autoconfig that want to
// set default pref values and allow users to change directory names.
//
// Now we have to support this, and so if for whatever reason we fail to get
// the localized version, then we try and get the non-localized version
// instead. If the string value is empty, then we'll just get the empty value
// back here.
if (dirName.IsEmpty())
{
rv = GetStringValue("description", EmptyCString(), dirName);
NS_ENSURE_SUCCESS(rv, rv);
}
CopyUTF8toUTF16(dirName, aDirName);
return NS_OK;
}
// XXX Although mailing lists could use the NotifyItemPropertyChanged
// mechanism here, it requires some rework on how we write/save data
// relating to mailing lists, so we're just using the old method of a
// local variable to store the mailing list name.
NS_IMETHODIMP nsAbDirProperty::SetDirName(const nsAString &aDirName)
{
if (m_DirPrefId.IsEmpty())
{
m_ListDirName = aDirName;
return NS_OK;
}
// Store the old value.
nsString oldDirName;
nsresult rv = GetDirName(oldDirName);
NS_ENSURE_SUCCESS(rv, rv);
// Save the new value
rv = SetLocalizedStringValue("description", NS_ConvertUTF16toUTF8(aDirName));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbManager> abManager = do_GetService(NS_ABMANAGER_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv))
// We inherit from nsIAbDirectory, so this static cast should be safe.
abManager->NotifyItemPropertyChanged(static_cast<nsIAbDirectory*>(this),
"DirName", oldDirName.get(),
nsString(aDirName).get());
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::GetDirType(int32_t *aDirType)
{
return GetIntValue("dirType", LDAPDirectory, aDirType);
}
NS_IMETHODIMP nsAbDirProperty::GetFileName(nsACString &aFileName)
{
return GetStringValue("filename", EmptyCString(), aFileName);
}
NS_IMETHODIMP nsAbDirProperty::GetURI(nsACString &aURI)
{
// XXX Should we complete this for Mailing Lists?
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP nsAbDirProperty::GetPosition(int32_t *aPosition)
{
return GetIntValue("position", kDefaultPosition, aPosition);
}
NS_IMETHODIMP nsAbDirProperty::GetLastModifiedDate(uint32_t *aLastModifiedDate)
{
NS_ENSURE_ARG_POINTER(aLastModifiedDate);
*aLastModifiedDate = m_LastModifiedDate;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::SetLastModifiedDate(uint32_t aLastModifiedDate)
{
if (aLastModifiedDate)
{
m_LastModifiedDate = aLastModifiedDate;
}
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::GetListNickName(nsAString &aListNickName)
{
aListNickName = m_ListNickName;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::SetListNickName(const nsAString &aListNickName)
{
m_ListNickName = aListNickName;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::GetDescription(nsAString &aDescription)
{
aDescription = m_Description;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::SetDescription(const nsAString &aDescription)
{
m_Description = aDescription;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::GetIsMailList(bool *aIsMailList)
{
*aIsMailList = m_IsMailList;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::SetIsMailList(bool aIsMailList)
{
m_IsMailList = aIsMailList;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::GetAddressLists(nsIMutableArray * *aAddressLists)
{
if (!m_AddressList)
{
nsresult rv;
m_AddressList = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
}
*aAddressLists = m_AddressList;
NS_ADDREF(*aAddressLists);
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::SetAddressLists(nsIMutableArray * aAddressLists)
{
m_AddressList = aAddressLists;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::CopyMailList(nsIAbDirectory* srcList)
{
SetIsMailList(true);
nsString str;
srcList->GetDirName(str);
SetDirName(str);
srcList->GetListNickName(str);
SetListNickName(str);
srcList->GetDescription(str);
SetDescription(str);
nsCOMPtr<nsIMutableArray> pAddressLists;
srcList->GetAddressLists(getter_AddRefs(pAddressLists));
SetAddressLists(pAddressLists);
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::GetIsQuery(bool *aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
// Mailing lists are not queries by default, individual directory types
// will override this.
*aResult = false;
return NS_OK;
}
NS_IMETHODIMP
nsAbDirProperty::Init(const char *aURI)
{
mURINoQuery = aURI;
mURI = aURI;
mIsValidURI = true;
int32_t searchCharLocation = mURINoQuery.FindChar('?');
if (searchCharLocation >= 0)
{
mQueryString = Substring(mURINoQuery, searchCharLocation + 1);
mURINoQuery.SetLength(searchCharLocation);
mIsQueryURI = true;
}
return NS_OK;
}
// nsIAbDirectory NOT IMPLEMENTED methods
NS_IMETHODIMP
nsAbDirProperty::GetChildNodes(nsISimpleEnumerator **childList)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP
nsAbDirProperty::GetChildCards(nsISimpleEnumerator **childCards)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP
nsAbDirProperty::DeleteDirectory(nsIAbDirectory *directory)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP
nsAbDirProperty::HasCard(nsIAbCard *cards, bool *hasCard)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP
nsAbDirProperty::HasDirectory(nsIAbDirectory *dir, bool *hasDir)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP
nsAbDirProperty::HasMailListWithName(const char16_t *aName, bool *aHasList)
{
NS_ENSURE_ARG_POINTER(aName);
NS_ENSURE_ARG_POINTER(aHasList);
*aHasList = false;
bool supportsLists = false;
nsresult rv = GetSupportsMailingLists(&supportsLists);
if (NS_FAILED(rv) || !supportsLists)
return NS_OK;
if (m_IsMailList)
return NS_OK;
nsCOMPtr<nsIMutableArray> addressLists;
rv = GetAddressLists(getter_AddRefs(addressLists));
NS_ENSURE_SUCCESS(rv, rv);
uint32_t listCount = 0;
rv = addressLists->GetLength(&listCount);
NS_ENSURE_SUCCESS(rv, rv);
for (uint32_t i = 0; i < listCount; i++)
{
nsCOMPtr<nsIAbDirectory> listDir(do_QueryElementAt(addressLists, i, &rv));
if (NS_SUCCEEDED(rv) && listDir)
{
nsAutoString listName;
rv = listDir->GetDirName(listName);
if (NS_SUCCEEDED(rv) && listName.Equals(aName))
{
*aHasList = true;
return NS_OK;
}
}
}
return NS_OK;
}
NS_IMETHODIMP
nsAbDirProperty::CreateNewDirectory(const nsAString &aDirName,
const nsACString &aURI,
uint32_t aType,
const nsACString &aPrefName,
nsACString &aResult)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP
nsAbDirProperty::CreateDirectoryByURI(const nsAString &aDisplayName,
const nsACString &aURI)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP nsAbDirProperty::AddMailList(nsIAbDirectory *list, nsIAbDirectory **addedList)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP nsAbDirProperty::EditMailListToDatabase(nsIAbCard *listCard)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP nsAbDirProperty::AddCard(nsIAbCard *childCard, nsIAbCard **addedCard)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP nsAbDirProperty::ModifyCard(nsIAbCard *aModifiedCard)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP nsAbDirProperty::DeleteCards(nsIArray *cards)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP nsAbDirProperty::DropCard(nsIAbCard *childCard, bool needToCopyCard)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP nsAbDirProperty::CardForEmailAddress(const nsACString &aEmailAddress,
nsIAbCard ** aAbCard)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP nsAbDirProperty::GetCardFromProperty(const char *aProperty,
const nsACString &aValue,
bool caseSensitive,
nsIAbCard **result)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP nsAbDirProperty::GetCardsFromProperty(const char *aProperty,
const nsACString &aValue,
bool caseSensitive,
nsISimpleEnumerator **result)
{ return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP nsAbDirProperty::GetSupportsMailingLists(bool *aSupportsMailingsLists)
{
NS_ENSURE_ARG_POINTER(aSupportsMailingsLists);
// We don't currently support nested mailing lists, so only return true if
// we're not a mailing list.
*aSupportsMailingsLists = !m_IsMailList;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::GetReadOnly(bool *aReadOnly)
{
NS_ENSURE_ARG_POINTER(aReadOnly);
// Default is that we are writable. Any implementation that is read-only must
// override this method.
*aReadOnly = false;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::GetIsRemote(bool *aIsRemote)
{
NS_ENSURE_ARG_POINTER(aIsRemote);
*aIsRemote = false;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::GetIsSecure(bool *aIsSecure)
{
NS_ENSURE_ARG_POINTER(aIsSecure);
*aIsSecure = false;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::UseForAutocomplete(const nsACString &aIdentityKey,
bool *aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
// Is local autocomplete enabled?
nsresult rv;
nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID,
&rv));
NS_ENSURE_SUCCESS(rv, rv);
return prefBranch->GetBoolPref("mail.enable_autocomplete", aResult);
}
NS_IMETHODIMP nsAbDirProperty::GetDirPrefId(nsACString &aDirPrefId)
{
aDirPrefId = m_DirPrefId;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::SetDirPrefId(const nsACString &aDirPrefId)
{
if (!m_DirPrefId.Equals(aDirPrefId))
{
m_DirPrefId.Assign(aDirPrefId);
// Clear the directory pref branch so that it is re-initialized next
// time its required.
m_DirectoryPrefs = nullptr;
}
return NS_OK;
}
nsresult nsAbDirProperty::InitDirectoryPrefs()
{
if (m_DirPrefId.IsEmpty())
return NS_ERROR_NOT_INITIALIZED;
nsresult rv;
nsCOMPtr<nsIPrefService> prefService(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
nsCString realPrefId(m_DirPrefId);
realPrefId.Append('.');
return prefService->GetBranch(realPrefId.get(), getter_AddRefs(m_DirectoryPrefs));
}
NS_IMETHODIMP nsAbDirProperty::GetIntValue(const char *aName,
int32_t aDefaultValue,
int32_t *aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
if (!m_DirectoryPrefs && NS_FAILED(InitDirectoryPrefs()))
return NS_ERROR_NOT_INITIALIZED;
if (NS_FAILED(m_DirectoryPrefs->GetIntPref(aName, aResult)))
*aResult = aDefaultValue;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::GetBoolValue(const char *aName,
bool aDefaultValue,
bool *aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
if (!m_DirectoryPrefs && NS_FAILED(InitDirectoryPrefs()))
return NS_ERROR_NOT_INITIALIZED;
if (NS_FAILED(m_DirectoryPrefs->GetBoolPref(aName, aResult)))
*aResult = aDefaultValue;
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::GetStringValue(const char *aName,
const nsACString &aDefaultValue,
nsACString &aResult)
{
if (!m_DirectoryPrefs && NS_FAILED(InitDirectoryPrefs()))
return NS_ERROR_NOT_INITIALIZED;
nsCString value;
/* unfortunately, there may be some prefs out there which look like (null) */
if (NS_SUCCEEDED(m_DirectoryPrefs->GetCharPref(aName, getter_Copies(value))) &&
!value.EqualsLiteral("(null"))
aResult = value;
else
aResult = aDefaultValue;
return NS_OK;
}
/*
* Get localized unicode string pref from properties file, convert into an
* UTF8 string since address book prefs store as UTF8 strings. So far there
* are 2 default prefs stored in addressbook.properties.
* "ldap_2.servers.pab.description"
* "ldap_2.servers.history.description"
*/
NS_IMETHODIMP nsAbDirProperty::GetLocalizedStringValue(const char *aName,
const nsACString &aDefaultValue,
nsACString &aResult)
{
if (!m_DirectoryPrefs && NS_FAILED(InitDirectoryPrefs()))
return NS_ERROR_NOT_INITIALIZED;
nsString wvalue;
nsCOMPtr<nsIPrefLocalizedString> locStr;
nsresult rv = m_DirectoryPrefs->GetComplexValue(aName,
NS_GET_IID(nsIPrefLocalizedString),
getter_AddRefs(locStr));
if (NS_SUCCEEDED(rv))
{
rv = locStr->ToString(getter_Copies(wvalue));
NS_ENSURE_SUCCESS(rv, rv);
}
if (wvalue.IsEmpty())
aResult = aDefaultValue;
else
CopyUTF16toUTF8(wvalue, aResult);
return NS_OK;
}
NS_IMETHODIMP nsAbDirProperty::SetIntValue(const char *aName,
int32_t aValue)
{
if (!m_DirectoryPrefs && NS_FAILED(InitDirectoryPrefs()))
return NS_ERROR_NOT_INITIALIZED;
return m_DirectoryPrefs->SetIntPref(aName, aValue);
}
NS_IMETHODIMP nsAbDirProperty::SetBoolValue(const char *aName,
bool aValue)
{
if (!m_DirectoryPrefs && NS_FAILED(InitDirectoryPrefs()))
return NS_ERROR_NOT_INITIALIZED;
return m_DirectoryPrefs->SetBoolPref(aName, aValue);
}
NS_IMETHODIMP nsAbDirProperty::SetStringValue(const char *aName,
const nsACString &aValue)
{
if (!m_DirectoryPrefs && NS_FAILED(InitDirectoryPrefs()))
return NS_ERROR_NOT_INITIALIZED;
return m_DirectoryPrefs->SetCharPref(aName, nsCString(aValue).get());
}
NS_IMETHODIMP nsAbDirProperty::SetLocalizedStringValue(const char *aName,
const nsACString &aValue)
{
if (!m_DirectoryPrefs && NS_FAILED(InitDirectoryPrefs()))
return NS_ERROR_NOT_INITIALIZED;
nsresult rv;
nsCOMPtr<nsIPrefLocalizedString> locStr(
do_CreateInstance(NS_PREFLOCALIZEDSTRING_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
rv = locStr->SetData(NS_ConvertUTF8toUTF16(aValue).get());
NS_ENSURE_SUCCESS(rv, rv);
return m_DirectoryPrefs->SetComplexValue(aName,
NS_GET_IID(nsIPrefLocalizedString),
locStr);
}
|