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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
|
/* -*- 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 "nsAbOSXDirectory.h"
#include "nsAbOSXCard.h"
#include "nsAbOSXUtils.h"
#include "nsAbQueryStringToExpression.h"
#include "nsArrayEnumerator.h"
#include "nsAutoPtr.h"
#include "nsCOMArray.h"
#include "nsEnumeratorUtils.h"
#include "nsIAbDirectoryQueryProxy.h"
#include "nsIAbManager.h"
#include "nsObjCExceptions.h"
#include "nsServiceManagerUtils.h"
#include "nsIMutableArray.h"
#include "nsArrayUtils.h"
#include "nsIAbBooleanExpression.h"
#include "nsComponentManagerUtils.h"
#include "nsISimpleEnumerator.h"
#include <AddressBook/AddressBook.h>
#define kABDeletedRecords (kABDeletedRecords? kABDeletedRecords : @"ABDeletedRecords")
#define kABUpdatedRecords (kABUpdatedRecords ? kABUpdatedRecords : @"ABUpdatedRecords")
#define kABInsertedRecords (kABInsertedRecords ? kABInsertedRecords : @"ABInsertedRecords")
static nsresult
GetOrCreateGroup(NSString *aUid, nsIAbDirectory **aResult)
{
NS_ASSERTION(aUid, "No UID for group!.");
nsAutoCString uri(NS_ABOSXDIRECTORY_URI_PREFIX);
AppendToCString(aUid, uri);
nsresult rv;
nsCOMPtr<nsIAbManager> abManager = do_GetService(NS_ABMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbDirectory> directory;
rv = abManager->GetDirectory(uri, getter_AddRefs(directory));
NS_ENSURE_SUCCESS(rv, rv);
NS_IF_ADDREF(*aResult = directory);
return NS_OK;
}
static nsresult
GetCard(ABRecord *aRecord, nsIAbCard **aResult, nsIAbOSXDirectory *osxDirectory)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NSString *uid = [aRecord uniqueId];
NS_ASSERTION(uid, "No UID for card!.");
if (!uid)
return NS_ERROR_FAILURE;
nsAutoCString uri(NS_ABOSXCARD_URI_PREFIX);
AppendToCString(uid, uri);
nsCOMPtr<nsIAbOSXCard> osxCard;
nsresult rv = osxDirectory->GetCardByUri(uri, getter_AddRefs(osxCard));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbCard> card = do_QueryInterface(osxCard, &rv);
NS_ENSURE_SUCCESS(rv, rv);
NS_IF_ADDREF(*aResult = card);
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
static nsresult
CreateCard(ABRecord *aRecord, nsIAbCard **aResult)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NSString *uid = [aRecord uniqueId];
NS_ASSERTION(uid, "No UID for card!.");
if (!uid)
return NS_ERROR_FAILURE;
nsresult rv;
nsCOMPtr<nsIAbOSXCard> osxCard = do_CreateInstance(NS_ABOSXCARD_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString uri(NS_ABOSXCARD_URI_PREFIX);
AppendToCString(uid, uri);
rv = osxCard->Init(uri.get());
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbCard> card = do_QueryInterface(osxCard, &rv);
NS_ENSURE_SUCCESS(rv, rv);
NS_IF_ADDREF(*aResult = card);
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
static nsresult
Sync(NSString *aUid)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
ABAddressBook *addressBook = [ABAddressBook sharedAddressBook];
ABRecord *card = [addressBook recordForUniqueId:aUid];
if ([card isKindOfClass:[ABGroup class]])
{
nsCOMPtr<nsIAbDirectory> directory;
GetOrCreateGroup(aUid, getter_AddRefs(directory));
nsCOMPtr<nsIAbOSXDirectory> osxDirectory =
do_QueryInterface(directory);
if (osxDirectory) {
osxDirectory->Update();
}
}
else {
nsCOMPtr<nsIAbCard> abCard;
nsresult rv;
nsCOMPtr<nsIAbManager> abManager = do_GetService(NS_ABMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbDirectory> directory;
rv = abManager->GetDirectory(NS_LITERAL_CSTRING(NS_ABOSXDIRECTORY_URI_PREFIX"/"),
getter_AddRefs(directory));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbOSXDirectory> osxDirectory =
do_QueryInterface(directory, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = GetCard(card, getter_AddRefs(abCard), osxDirectory);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbOSXCard> osxCard = do_QueryInterface(abCard);
osxCard->Update(true);
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
@interface ABChangedMonitor : NSObject
-(void)ABChanged:(NSNotification *)aNotification;
@end
@implementation ABChangedMonitor
-(void)ABChanged:(NSNotification *)aNotification
{
NSDictionary *changes = [aNotification userInfo];
nsresult rv;
NSArray *inserted = [changes objectForKey:kABInsertedRecords];
if (inserted) {
nsCOMPtr<nsIAbManager> abManager = do_GetService(NS_ABMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS_VOID(rv);
nsCOMPtr<nsIAbDirectory> directory;
rv = abManager->GetDirectory(NS_LITERAL_CSTRING(NS_ABOSXDIRECTORY_URI_PREFIX"/"),
getter_AddRefs(directory));
NS_ENSURE_SUCCESS_VOID(rv);
nsCOMPtr<nsIAbOSXDirectory> osxDirectory =
do_QueryInterface(directory, &rv);
NS_ENSURE_SUCCESS_VOID(rv);
unsigned int i, count = [inserted count];
for (i = 0; i < count; ++i) {
ABAddressBook *addressBook =
[ABAddressBook sharedAddressBook];
ABRecord *card =
[addressBook recordForUniqueId:[inserted objectAtIndex:i]];
if ([card isKindOfClass:[ABGroup class]]) {
nsCOMPtr<nsIAbDirectory> directory;
GetOrCreateGroup([inserted objectAtIndex:i],
getter_AddRefs(directory));
rv = osxDirectory->AssertDirectory(abManager, directory);
NS_ENSURE_SUCCESS_VOID(rv);
}
else {
nsCOMPtr<nsIAbCard> abCard;
// Construct a card
nsresult rv = CreateCard(card, getter_AddRefs(abCard));
NS_ENSURE_SUCCESS_VOID(rv);
rv = osxDirectory->AssertCard(abManager, abCard);
NS_ENSURE_SUCCESS_VOID(rv);
}
}
}
NSArray *updated = [changes objectForKey:kABUpdatedRecords];
if (updated) {
unsigned int i, count = [updated count];
for (i = 0; i < count; ++i) {
NSString *uid = [updated objectAtIndex:i];
Sync(uid);
}
}
NSArray *deleted = [changes objectForKey:kABDeletedRecords];
if (deleted) {
nsCOMPtr<nsIAbManager> abManager = do_GetService(NS_ABMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS_VOID(rv);
nsCOMPtr<nsIAbDirectory> directory;
rv = abManager->GetDirectory(NS_LITERAL_CSTRING(NS_ABOSXDIRECTORY_URI_PREFIX"/"),
getter_AddRefs(directory));
NS_ENSURE_SUCCESS_VOID(rv);
nsCOMPtr<nsIAbOSXDirectory> osxDirectory =
do_QueryInterface(directory, &rv);
NS_ENSURE_SUCCESS_VOID(rv);
unsigned int i, count = [deleted count];
for (i = 0; i < count; ++i) {
NSString *deletedUid = [deleted objectAtIndex:i];
nsAutoCString uid;
AppendToCString(deletedUid, uid);
rv = osxDirectory->DeleteUid(uid);
NS_ENSURE_SUCCESS_VOID(rv);
}
}
if (!inserted && !updated && !deleted) {
// XXX This is supposed to mean "everything was updated", but we get
// this whenever something has changed, so not sure what to do.
}
}
@end
static nsresult
MapConditionString(nsIAbBooleanConditionString *aCondition, bool aNegate,
bool &aCanHandle, ABSearchElement **aResult)
{
aCanHandle = false;
nsAbBooleanConditionType conditionType = 0;
nsresult rv = aCondition->GetCondition(&conditionType);
NS_ENSURE_SUCCESS(rv, rv);
ABSearchComparison comparison;
switch (conditionType) {
case nsIAbBooleanConditionTypes::Contains:
{
if (!aNegate) {
comparison = kABContainsSubString;
aCanHandle = true;
}
break;
}
case nsIAbBooleanConditionTypes::DoesNotContain:
{
if (aNegate) {
comparison = kABContainsSubString;
aCanHandle = true;
}
break;
}
case nsIAbBooleanConditionTypes::Is:
{
comparison = aNegate ? kABNotEqual : kABEqual;
aCanHandle = true;
break;
}
case nsIAbBooleanConditionTypes::IsNot:
{
comparison = aNegate ? kABEqual : kABNotEqual;
aCanHandle = true;
break;
}
case nsIAbBooleanConditionTypes::BeginsWith:
{
if (!aNegate) {
comparison = kABPrefixMatch;
aCanHandle = true;
}
break;
}
case nsIAbBooleanConditionTypes::EndsWith:
{
//comparison = kABSuffixMatch;
break;
}
case nsIAbBooleanConditionTypes::LessThan:
{
comparison = aNegate ? kABGreaterThanOrEqual : kABLessThan;
aCanHandle = true;
break;
}
case nsIAbBooleanConditionTypes::GreaterThan:
{
comparison = aNegate ? kABLessThanOrEqual : kABGreaterThan;
aCanHandle = true;
break;
}
}
if (!aCanHandle)
return NS_OK;
nsCString name;
rv = aCondition->GetName(getter_Copies(name));
NS_ENSURE_SUCCESS(rv, rv);
nsString value;
rv = aCondition->GetValue(getter_Copies(value));
NS_ENSURE_SUCCESS(rv, rv);
uint32_t length = value.Length();
uint32_t i;
for (i = 0; i < nsAbOSXUtils::kPropertyMapSize; ++i) {
if (name.Equals(nsAbOSXUtils::kPropertyMap[i].mPropertyName)) {
*aResult =
[ABPerson searchElementForProperty:nsAbOSXUtils::kPropertyMap[i].mOSXProperty
label:nsAbOSXUtils::kPropertyMap[i].mOSXLabel
key:nsAbOSXUtils::kPropertyMap[i].mOSXKey
value:[NSString stringWithCharacters:reinterpret_cast<const unichar*>(value.get()) length:length]
comparison:comparison];
return NS_OK;
}
}
if (name.EqualsLiteral("DisplayName") && comparison == kABContainsSubString) {
ABSearchElement *first =
[ABPerson searchElementForProperty:kABFirstNameProperty
label:nil
key:nil
value:[NSString stringWithCharacters:reinterpret_cast<const unichar*>(value.get()) length:length]
comparison:comparison];
ABSearchElement *second =
[ABPerson searchElementForProperty:kABLastNameProperty
label:nil
key:nil
value:[NSString stringWithCharacters:reinterpret_cast<const unichar*>(value.get()) length:length]
comparison:comparison];
ABSearchElement *third =
[ABGroup searchElementForProperty:kABGroupNameProperty
label:nil
key:nil
value:[NSString stringWithCharacters:reinterpret_cast<const unichar*>(value.get()) length:length]
comparison:comparison];
*aResult = [ABSearchElement searchElementForConjunction:kABSearchOr children:[NSArray arrayWithObjects:first, second, third, nil]];
return NS_OK;
}
aCanHandle = false;
return NS_OK;
}
static nsresult
BuildSearchElements(nsIAbBooleanExpression *aExpression,
bool &aCanHandle,
ABSearchElement **aResult)
{
aCanHandle = true;
nsCOMPtr<nsIArray> expressions;
nsresult rv = aExpression->GetExpressions(getter_AddRefs(expressions));
NS_ENSURE_SUCCESS(rv, rv);
nsAbBooleanOperationType operation;
rv = aExpression->GetOperation(&operation);
NS_ENSURE_SUCCESS(rv, rv);
uint32_t count;
rv = expressions->GetLength(&count);
NS_ENSURE_SUCCESS(rv, rv);
NS_ASSERTION(count > 1 && operation != nsIAbBooleanOperationTypes::NOT,
"This doesn't make sense!");
NSMutableArray *array = nullptr;
if (count > 1)
array = [[NSMutableArray alloc] init];
uint32_t i;
nsCOMPtr<nsIAbBooleanConditionString> condition;
nsCOMPtr<nsIAbBooleanExpression> subExpression;
for (i = 0; i < count; ++i) {
ABSearchElement *element = nullptr;
condition = do_QueryElementAt(expressions, i);
if (condition) {
rv = MapConditionString(condition, operation == nsIAbBooleanOperationTypes::NOT, aCanHandle, &element);
if (NS_FAILED(rv))
break;
}
else {
subExpression = do_QueryElementAt(expressions, i);
if (subExpression) {
rv = BuildSearchElements(subExpression, aCanHandle, &element);
if (NS_FAILED(rv))
break;
}
}
if (!aCanHandle) {
// remember to free the array when returning early
[array release];
return NS_OK;
}
if (element) {
if (array)
[array addObject:element];
else
*aResult = element;
}
}
if (array) {
if (NS_SUCCEEDED(rv)) {
ABSearchConjunction conjunction = operation == nsIAbBooleanOperationTypes::AND ? kABSearchAnd : kABSearchOr;
*aResult = [ABSearchElement searchElementForConjunction:conjunction children:array];
}
[array release];
}
return rv;
}
static bool
Search(nsIAbBooleanExpression *aExpression, NSArray **aResult)
{
bool canHandle = false;
ABSearchElement *searchElement;
nsresult rv = BuildSearchElements(aExpression, canHandle, &searchElement);
NS_ENSURE_SUCCESS(rv, false);
if (canHandle)
*aResult = [[ABAddressBook sharedAddressBook] recordsMatchingSearchElement:searchElement];
return canHandle;
}
static uint32_t sObserverCount = 0;
static ABChangedMonitor *sObserver = nullptr;
nsAbOSXDirectory::nsAbOSXDirectory()
{
}
nsAbOSXDirectory::~nsAbOSXDirectory()
{
if (--sObserverCount == 0) {
[[NSNotificationCenter defaultCenter] removeObserver:sObserver];
[sObserver release];
}
}
NS_IMPL_ISUPPORTS_INHERITED(nsAbOSXDirectory,
nsAbDirProperty,
nsIAbOSXDirectory,
nsIAbDirSearchListener)
NS_IMETHODIMP
nsAbOSXDirectory::Init(const char *aUri)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
nsresult rv;
rv = nsAbDirProperty::Init(aUri);
NS_ENSURE_SUCCESS(rv, rv);
ABAddressBook *addressBook = [ABAddressBook sharedAddressBook];
if (sObserverCount == 0) {
sObserver = [[ABChangedMonitor alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:(ABChangedMonitor*)sObserver
selector:@selector(ABChanged:)
name:kABDatabaseChangedExternallyNotification
object:nil];
}
++sObserverCount;
NSArray *cards;
nsCOMPtr<nsIMutableArray> cardList;
bool isRootOSXDirectory = false;
if (!mIsQueryURI && mURINoQuery.Length() <= sizeof(NS_ABOSXDIRECTORY_URI_PREFIX))
isRootOSXDirectory = true;
if (mIsQueryURI || isRootOSXDirectory)
{
m_DirPrefId.AssignLiteral("ldap_2.servers.osx");
cards = [[addressBook people] arrayByAddingObjectsFromArray:[addressBook groups]];
if (!mCardList)
mCardList = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
else
rv = mCardList->Clear();
NS_ENSURE_SUCCESS(rv, rv);
cardList = mCardList;
}
else
{
nsAutoCString uid(Substring(mURINoQuery, sizeof(NS_ABOSXDIRECTORY_URI_PREFIX) - 1));
ABRecord *card = [addressBook recordForUniqueId:[NSString stringWithUTF8String:uid.get()]];
NS_ASSERTION([card isKindOfClass:[ABGroup class]], "Huh.");
m_IsMailList = true;
AppendToString([card valueForProperty:kABGroupNameProperty], m_ListDirName);
ABGroup *group = (ABGroup*)[addressBook recordForUniqueId:[NSString stringWithUTF8String:nsAutoCString(Substring(mURINoQuery, 21)).get()]];
cards = [[group members] arrayByAddingObjectsFromArray:[group subgroups]];
if (!m_AddressList)
m_AddressList = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
else
rv = m_AddressList->Clear();
NS_ENSURE_SUCCESS(rv, rv);
cardList = m_AddressList;
}
nsAutoCString ourUuid;
GetUuid(ourUuid);
unsigned int nbCards = [cards count];
nsCOMPtr<nsIAbCard> card;
nsCOMPtr<nsIAbManager> abManager = do_GetService(NS_ABMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbOSXDirectory> rootOSXDirectory;
if (!isRootOSXDirectory)
{
rv = GetRootOSXDirectory(getter_AddRefs(rootOSXDirectory));
NS_ENSURE_SUCCESS(rv, rv);
}
for (unsigned int i = 0; i < nbCards; ++i)
{
// If we're a Group, it's likely that the cards we're going
// to create were already created in the root nsAbOSXDirectory,
if (!isRootOSXDirectory)
rv = GetCard([cards objectAtIndex:i], getter_AddRefs(card),
rootOSXDirectory);
else
{
// If we're not a Group, that means we're the root nsAbOSXDirectory,
// which means we have to create the cards from scratch.
rv = CreateCard([cards objectAtIndex:i], getter_AddRefs(card));
}
NS_ENSURE_SUCCESS(rv, rv);
// If we're not a query directory, we're going to want to
// tell the AB Manager that we've added some cards so that they
// show up in the address book views.
if (!mIsQueryURI)
AssertCard(abManager, card);
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP
nsAbOSXDirectory::GetURI(nsACString &aURI)
{
if (mURI.IsEmpty())
return NS_ERROR_NOT_INITIALIZED;
aURI = mURI;
return NS_OK;
}
NS_IMETHODIMP
nsAbOSXDirectory::GetReadOnly(bool *aReadOnly)
{
NS_ENSURE_ARG_POINTER(aReadOnly);
*aReadOnly = true;
return NS_OK;
}
static bool
CheckRedundantCards(nsIAbManager *aManager, nsIAbDirectory *aDirectory,
nsIAbCard *aCard, NSMutableArray *aCardList)
{
nsresult rv;
nsCOMPtr<nsIAbOSXCard> osxCard = do_QueryInterface(aCard, &rv);
NS_ENSURE_SUCCESS(rv, false);
nsAutoCString uri;
rv = osxCard->GetURI(uri);
NS_ENSURE_SUCCESS(rv, false);
NSString *uid = [NSString stringWithUTF8String:(uri.get() + 21)];
unsigned int i, count = [aCardList count];
for (i = 0; i < count; ++i) {
if ([[[aCardList objectAtIndex:i] uniqueId] isEqualToString:uid]) {
[aCardList removeObjectAtIndex:i];
break;
}
}
if (i == count) {
aManager->NotifyDirectoryItemDeleted(aDirectory, aCard);
return true;
}
return false;
}
nsresult
nsAbOSXDirectory::GetRootOSXDirectory(nsIAbOSXDirectory **aResult)
{
if (!mCacheTopLevelOSXAb)
{
// Attempt to get card from the toplevel directories
nsresult rv;
nsCOMPtr<nsIAbManager> abManager = do_GetService(NS_ABMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbDirectory> directory;
rv = abManager->GetDirectory(NS_LITERAL_CSTRING(NS_ABOSXDIRECTORY_URI_PREFIX"/"),
getter_AddRefs(directory));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbOSXDirectory> osxDirectory =
do_QueryInterface(directory, &rv);
NS_ENSURE_SUCCESS(rv, rv);
mCacheTopLevelOSXAb = osxDirectory;
}
NS_IF_ADDREF(*aResult = mCacheTopLevelOSXAb);
return NS_OK;
}
nsresult
nsAbOSXDirectory::Update()
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
nsresult rv;
nsCOMPtr<nsIAbManager> abManager = do_GetService(NS_ABMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
if (mIsQueryURI) {
return NS_OK;
}
ABAddressBook *addressBook = [ABAddressBook sharedAddressBook];
// Due to the horrible way the address book code works wrt mailing lists
// we have to use a different list depending on what we are. This pointer
// holds a reference to that list.
nsIMutableArray* cardList;
NSArray *groups, *cards;
if (m_IsMailList) {
ABGroup *group = (ABGroup*)[addressBook recordForUniqueId:[NSString stringWithUTF8String:nsAutoCString(Substring(mURINoQuery, 21)).get()]];
groups = nil;
cards = [[group members] arrayByAddingObjectsFromArray:[group subgroups]];
if (!m_AddressList)
{
m_AddressList = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
}
// For mailing lists, store the cards in m_AddressList
cardList = m_AddressList;
}
else {
groups = [addressBook groups];
cards = [[addressBook people] arrayByAddingObjectsFromArray:groups];
if (!mCardList)
{
mCardList = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
}
// For directories, store the cards in mCardList
cardList = mCardList;
}
NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:cards];
uint32_t addressCount;
rv = cardList->GetLength(&addressCount);
NS_ENSURE_SUCCESS(rv, rv);
while (addressCount--)
{
nsCOMPtr<nsIAbCard> card(do_QueryElementAt(cardList, addressCount, &rv));
if (NS_FAILED(rv))
break;
if (CheckRedundantCards(abManager, this, card, mutableArray))
cardList->RemoveElementAt(addressCount);
}
NSEnumerator *enumerator = [mutableArray objectEnumerator];
ABRecord *card;
nsCOMPtr<nsIAbCard> abCard;
nsCOMPtr<nsIAbOSXDirectory> rootOSXDirectory;
rv = GetRootOSXDirectory(getter_AddRefs(rootOSXDirectory));
NS_ENSURE_SUCCESS(rv, rv);
while ((card = [enumerator nextObject]))
{
rv = GetCard(card, getter_AddRefs(abCard), rootOSXDirectory);
if (NS_FAILED(rv))
rv = CreateCard(card, getter_AddRefs(abCard));
NS_ENSURE_SUCCESS(rv, rv);
AssertCard(abManager, abCard);
}
card = (ABRecord*)[addressBook recordForUniqueId:[NSString stringWithUTF8String:nsAutoCString(Substring(mURINoQuery, 21)).get()]];
NSString * stringValue = [card valueForProperty:kABGroupNameProperty];
if (![stringValue isEqualToString:WrapString(m_ListDirName)])
{
nsAutoString oldValue(m_ListDirName);
AssignToString(stringValue, m_ListDirName);
nsCOMPtr<nsISupports> supports = do_QueryInterface(static_cast<nsIAbDirectory *>(this), &rv);
NS_ENSURE_SUCCESS(rv, rv);
abManager->NotifyItemPropertyChanged(supports, "DirName",
oldValue.get(), m_ListDirName.get());
}
if (groups)
{
mutableArray = [NSMutableArray arrayWithArray:groups];
nsCOMPtr<nsIAbDirectory> directory;
// It is ok to use m_AddressList here as only top-level directories have
// groups, and they will be in m_AddressList
if (m_AddressList)
{
rv = m_AddressList->GetLength(&addressCount);
NS_ENSURE_SUCCESS(rv, rv);
while (addressCount--)
{
directory = do_QueryElementAt(m_AddressList, addressCount, &rv);
if (NS_FAILED(rv))
continue;
nsAutoCString uri;
directory->GetURI(uri);
uri.Cut(0, 21);
NSString *uid = [NSString stringWithUTF8String:uri.get()];
unsigned int j, arrayCount = [mutableArray count];
for (j = 0; j < arrayCount; ++j) {
if ([[[mutableArray objectAtIndex:j] uniqueId] isEqualToString:uid]) {
[mutableArray removeObjectAtIndex:j];
break;
}
}
if (j == arrayCount) {
UnassertDirectory(abManager, directory);
}
}
}
enumerator = [mutableArray objectEnumerator];
while ((card = [enumerator nextObject])) {
rv = GetOrCreateGroup([card uniqueId], getter_AddRefs(directory));
NS_ENSURE_SUCCESS(rv, rv);
AssertDirectory(abManager, directory);
}
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
nsresult
nsAbOSXDirectory::AssertChildNodes()
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
// Queries and mailing lists can't have childnodes.
if (mIsQueryURI || m_IsMailList) {
return NS_OK;
}
nsresult rv;
nsCOMPtr<nsIAbManager> abManager =
do_GetService(NS_ABMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
NSArray *groups = [[ABAddressBook sharedAddressBook] groups];
unsigned int i, count = [groups count];
if (count > 0 && !m_AddressList) {
m_AddressList = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
}
nsCOMPtr<nsIAbDirectory> directory;
for (i = 0; i < count; ++i) {
rv = GetOrCreateGroup([[groups objectAtIndex:i] uniqueId],
getter_AddRefs(directory));
NS_ENSURE_SUCCESS(rv, rv);
rv = AssertDirectory(abManager, directory);
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
nsresult
nsAbOSXDirectory::AssertDirectory(nsIAbManager *aManager,
nsIAbDirectory *aDirectory)
{
uint32_t pos;
if (m_AddressList &&
NS_SUCCEEDED(m_AddressList->IndexOf(0, aDirectory, &pos)))
// We already have this directory, so no point in adding it again.
return NS_OK;
nsresult rv;
if (!m_AddressList) {
m_AddressList = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
}
rv = m_AddressList->AppendElement(aDirectory, false);
NS_ENSURE_SUCCESS(rv, rv);
return aManager->NotifyDirectoryItemAdded(this, aDirectory);
}
nsresult
nsAbOSXDirectory::AssertCard(nsIAbManager *aManager,
nsIAbCard *aCard)
{
nsAutoCString ourUuid;
GetUuid(ourUuid);
aCard->SetDirectoryId(ourUuid);
nsresult rv = m_IsMailList ? m_AddressList->AppendElement(aCard, false) :
mCardList->AppendElement(aCard, false);
NS_ENSURE_SUCCESS(rv, rv);
// Get the card's URI and add it to our card store
nsCOMPtr<nsIAbOSXCard> osxCard = do_QueryInterface(aCard, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCString uri;
rv = osxCard->GetURI(uri);
nsCOMPtr<nsIAbOSXCard> retrievedCard;
if (!mCardStore.Get(uri, getter_AddRefs(retrievedCard)))
mCardStore.Put(uri, osxCard);
return aManager->NotifyDirectoryItemAdded(this, aCard);
}
nsresult
nsAbOSXDirectory::UnassertCard(nsIAbManager *aManager,
nsIAbCard *aCard,
nsIMutableArray *aCardList)
{
nsresult rv;
uint32_t pos;
if (NS_SUCCEEDED(aCardList->IndexOf(0, aCard, &pos)))
rv = aCardList->RemoveElementAt(pos);
return aManager->NotifyDirectoryItemDeleted(this, aCard);
}
nsresult
nsAbOSXDirectory::UnassertDirectory(nsIAbManager *aManager,
nsIAbDirectory *aDirectory)
{
NS_ENSURE_TRUE(m_AddressList, NS_ERROR_NULL_POINTER);
uint32_t pos;
if (NS_SUCCEEDED(m_AddressList->IndexOf(0, aDirectory, &pos)))
{
nsresult rv = m_AddressList->RemoveElementAt(pos);
NS_ENSURE_SUCCESS(rv, rv);
}
return aManager->NotifyDirectoryItemDeleted(this, aDirectory);
}
NS_IMETHODIMP
nsAbOSXDirectory::GetChildNodes(nsISimpleEnumerator **aNodes)
{
NS_ENSURE_ARG_POINTER(aNodes);
// Queries don't have childnodes.
if (mIsQueryURI || m_IsMailList || !m_AddressList)
return NS_NewEmptyEnumerator(aNodes);
return NS_NewArrayEnumerator(aNodes, m_AddressList);
}
NS_IMETHODIMP
nsAbOSXDirectory::GetChildCards(nsISimpleEnumerator **aCards)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NS_ENSURE_ARG_POINTER(aCards);
nsresult rv;
NSArray *cards;
if (mIsQueryURI)
{
nsCOMPtr<nsIAbBooleanExpression> expression;
rv = nsAbQueryStringToExpression::Convert(mQueryString,
getter_AddRefs(expression));
NS_ENSURE_SUCCESS(rv, rv);
bool canHandle = !m_IsMailList && Search(expression, &cards);
if (!canHandle)
return FallbackSearch(expression, aCards);
if (!mCardList)
mCardList = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
else
mCardList->Clear();
NS_ENSURE_SUCCESS(rv, rv);
// The uuid for initializing cards
nsAutoCString ourUuid;
GetUuid(ourUuid);
// Fill the results array and update the card list
unsigned int nbCards = [cards count];
unsigned int i;
nsCOMPtr<nsIAbCard> card;
nsCOMPtr<nsIAbOSXDirectory> rootOSXDirectory;
rv = GetRootOSXDirectory(getter_AddRefs(rootOSXDirectory));
NS_ENSURE_SUCCESS(rv, rv);
for (i = 0; i < nbCards; ++i)
{
rv = GetCard([cards objectAtIndex:i], getter_AddRefs(card),
rootOSXDirectory);
if (NS_FAILED(rv))
rv = CreateCard([cards objectAtIndex:i],
getter_AddRefs(card));
NS_ENSURE_SUCCESS(rv, rv);
card->SetDirectoryId(ourUuid);
mCardList->AppendElement(card, false);
}
return NS_NewArrayEnumerator(aCards, mCardList);
}
// Not a search, so just return the appropriate list of items.
return m_IsMailList ? NS_NewArrayEnumerator(aCards, m_AddressList) :
NS_NewArrayEnumerator(aCards, mCardList);
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP
nsAbOSXDirectory::GetIsQuery(bool *aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
*aResult = mIsQueryURI;
return NS_OK;
}
/* Recursive method that searches for a child card by URI. If it cannot find
* it within this directory, it checks all subfolders.
*/
NS_IMETHODIMP
nsAbOSXDirectory::GetCardByUri(const nsACString &aUri, nsIAbOSXCard **aResult)
{
nsCOMPtr<nsIAbOSXCard> osxCard;
// Base Case
if (mCardStore.Get(aUri, getter_AddRefs(osxCard)))
{
NS_IF_ADDREF(*aResult = osxCard);
return NS_OK;
}
// Search children
nsCOMPtr<nsISimpleEnumerator> enumerator;
nsresult rv = this->GetChildNodes(getter_AddRefs(enumerator));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsISupports> item;
bool hasMore = false;
while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore)
{
rv = enumerator->GetNext(getter_AddRefs(item));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbOSXDirectory> childDirectory;
childDirectory = do_QueryInterface(item, &rv);
if (NS_SUCCEEDED(rv))
{
rv = childDirectory->GetCardByUri(aUri, getter_AddRefs(osxCard));
if (NS_SUCCEEDED(rv))
{
NS_IF_ADDREF(*aResult = osxCard);
return NS_OK;
}
}
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsAbOSXDirectory::GetCardFromProperty(const char *aProperty,
const nsACString &aValue,
bool aCaseSensitive,
nsIAbCard **aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
*aResult = nullptr;
if (aValue.IsEmpty())
return NS_OK;
nsIMutableArray *list = m_IsMailList ? m_AddressList : mCardList;
if (!list)
return NS_OK;
uint32_t length;
nsresult rv = list->GetLength(&length);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbCard> card;
nsAutoCString cardValue;
for (uint32_t i = 0; i < length && !*aResult; ++i)
{
card = do_QueryElementAt(list, i, &rv);
if (NS_SUCCEEDED(rv))
{
rv = card->GetPropertyAsAUTF8String(aProperty, cardValue);
if (NS_SUCCEEDED(rv))
{
#ifdef MOZILLA_INTERNAL_API
bool equal = aCaseSensitive ? cardValue.Equals(aValue) :
cardValue.Equals(aValue, nsCaseInsensitiveCStringComparator());
#else
bool equal = aCaseSensitive ? cardValue.Equals(aValue) :
cardValue.Equals(aValue, CaseInsensitiveCompare);
#endif
if (equal)
NS_IF_ADDREF(*aResult = card);
}
}
}
return NS_OK;
}
NS_IMETHODIMP
nsAbOSXDirectory::GetCardsFromProperty(const char *aProperty,
const nsACString &aValue,
bool aCaseSensitive,
nsISimpleEnumerator **aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
*aResult = nullptr;
if (aValue.IsEmpty())
return NS_NewEmptyEnumerator(aResult);
nsIMutableArray *list = m_IsMailList ? m_AddressList : mCardList;
if (!list)
return NS_NewEmptyEnumerator(aResult);
uint32_t length;
nsresult rv = list->GetLength(&length);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMArray<nsIAbCard> resultArray;
nsCOMPtr<nsIAbCard> card;
nsAutoCString cardValue;
for (uint32_t i = 0; i < length; ++i)
{
card = do_QueryElementAt(list, i, &rv);
if (NS_SUCCEEDED(rv))
{
rv = card->GetPropertyAsAUTF8String(aProperty, cardValue);
if (NS_SUCCEEDED(rv))
{
#ifdef MOZILLA_INTERNAL_API
bool equal = aCaseSensitive ? cardValue.Equals(aValue) :
cardValue.Equals(aValue, nsCaseInsensitiveCStringComparator());
#else
bool equal = aCaseSensitive ? cardValue.Equals(aValue) :
cardValue.Equals(aValue, CaseInsensitiveCompare);
#endif
if (equal)
resultArray.AppendObject(card);
}
}
}
return NS_NewArrayEnumerator(aResult, resultArray);
}
NS_IMETHODIMP
nsAbOSXDirectory::CardForEmailAddress(const nsACString &aEmailAddress,
nsIAbCard **aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
*aResult = nullptr;
if (aEmailAddress.IsEmpty())
return NS_OK;
nsIMutableArray *list = m_IsMailList ? m_AddressList : mCardList;
if (!list)
return NS_OK;
uint32_t length;
nsresult rv = list->GetLength(&length);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbCard> card;
for (uint32_t i = 0; i < length && !*aResult; ++i)
{
card = do_QueryElementAt(list, i, &rv);
if (NS_SUCCEEDED(rv))
{
bool hasEmailAddress = false;
rv = card->HasEmailAddress(aEmailAddress, &hasEmailAddress);
if (NS_SUCCEEDED(rv) && hasEmailAddress)
NS_IF_ADDREF(*aResult = card);
}
}
return NS_OK;
}
NS_IMETHODIMP
nsAbOSXDirectory::HasCard(nsIAbCard *aCard, bool *aHasCard)
{
NS_ENSURE_ARG_POINTER(aCard);
NS_ENSURE_ARG_POINTER(aHasCard);
nsresult rv = NS_OK;
uint32_t index;
if (m_IsMailList)
{
if (m_AddressList)
rv = m_AddressList->IndexOf(0, aCard, &index);
}
else if (mCardList)
rv = mCardList->IndexOf(0, aCard, &index);
*aHasCard = NS_SUCCEEDED(rv);
return NS_OK;
}
NS_IMETHODIMP
nsAbOSXDirectory::HasDirectory(nsIAbDirectory *aDirectory,
bool *aHasDirectory)
{
NS_ENSURE_ARG_POINTER(aDirectory);
NS_ENSURE_ARG_POINTER(aHasDirectory);
*aHasDirectory = false;
uint32_t pos;
if (m_AddressList && NS_SUCCEEDED(m_AddressList->IndexOf(0, aDirectory, &pos)))
*aHasDirectory = true;
return NS_OK;
}
NS_IMETHODIMP
nsAbOSXDirectory::OnSearchFinished(int32_t aResult, const nsAString &aErrorMsg)
{
return NS_OK;
}
NS_IMETHODIMP
nsAbOSXDirectory::OnSearchFoundCard(nsIAbCard *aCard)
{
nsresult rv;
if (!m_AddressList) {
m_AddressList = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
}
if (!mCardList) {
mCardList = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
}
rv = m_AddressList->AppendElement(aCard, false);
NS_ENSURE_SUCCESS(rv, rv);
rv = mCardList->AppendElement(aCard, false);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString ourUuid;
GetUuid(ourUuid);
aCard->SetDirectoryId(ourUuid);
return NS_OK;
}
nsresult
nsAbOSXDirectory::FallbackSearch(nsIAbBooleanExpression *aExpression,
nsISimpleEnumerator **aCards)
{
nsresult rv;
if (mCardList)
rv = mCardList->Clear();
else
mCardList = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
if (m_AddressList) {
m_AddressList->Clear();
}
else {
m_AddressList = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
}
nsCOMPtr<nsIAbDirectoryQueryArguments> arguments =
do_CreateInstance(NS_ABDIRECTORYQUERYARGUMENTS_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = arguments->SetExpression(aExpression);
NS_ENSURE_SUCCESS(rv, rv);
// Don't search the subdirectories. If the current directory is a mailing
// list, it won't have any subdirectories. If the current directory is an
// addressbook, searching both it and the subdirectories (the mailing
// lists), will yield duplicate results because every entry in a mailing
// list will be an entry in the parent addressbook.
rv = arguments->SetQuerySubDirectories(false);
NS_ENSURE_SUCCESS(rv, rv);
// Get the directory without the query
nsCOMPtr<nsIAbManager> abManager = do_GetService(NS_ABMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbDirectory> directory;
rv = abManager->GetDirectory(mURINoQuery, getter_AddRefs(directory));
NS_ENSURE_SUCCESS(rv, rv);
// Initiate the proxy query with the no query directory
nsCOMPtr<nsIAbDirectoryQueryProxy> queryProxy =
do_CreateInstance(NS_ABDIRECTORYQUERYPROXY_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = queryProxy->Initiate();
NS_ENSURE_SUCCESS(rv, rv);
int32_t context = 0;
rv = queryProxy->DoQuery(directory, arguments, this, -1, 0, &context);
NS_ENSURE_SUCCESS(rv, rv);
return NS_NewArrayEnumerator(aCards, m_AddressList);
}
nsresult nsAbOSXDirectory::DeleteUid(const nsACString &aUid)
{
if (!m_AddressList)
return NS_ERROR_NULL_POINTER;
nsresult rv;
nsCOMPtr<nsIAbManager> abManager =
do_GetService(NS_ABMANAGER_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
// At this stage we don't know if aUid represents a card or group. The OS X
// interfaces don't give us chance to find out, so we have to go through
// our lists to find it.
// First, we'll see if its in the group list as it is likely to be shorter.
// See if this item is in our address list
uint32_t addressCount;
rv = m_AddressList->GetLength(&addressCount);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString uri(NS_ABOSXDIRECTORY_URI_PREFIX);
uri.Append(aUid);
// Iterate backwards in case we remove something
while (addressCount--)
{
nsCOMPtr<nsIAbItem> abItem(do_QueryElementAt(m_AddressList,
addressCount, &rv));
if (NS_FAILED(rv))
continue;
nsCOMPtr<nsIAbDirectory> directory(do_QueryInterface(abItem, &rv));
if (NS_SUCCEEDED(rv))
{
nsAutoCString dirUri;
directory->GetURI(dirUri);
if (uri.Equals(dirUri))
return UnassertDirectory(abManager, directory);
} else {
nsCOMPtr<nsIAbOSXCard> osxCard(do_QueryInterface(abItem, &rv));
if (NS_SUCCEEDED(rv))
{
nsAutoCString cardUri;
osxCard->GetURI(cardUri);
if (uri.Equals(cardUri))
{
nsCOMPtr<nsIAbCard> card(do_QueryInterface(osxCard, &rv));
if (NS_SUCCEEDED(rv))
return UnassertCard(abManager, card, m_AddressList);
}
}
}
}
// Second, see if it is one of the cards.
if (!mCardList)
return NS_ERROR_FAILURE;
uri = NS_ABOSXCARD_URI_PREFIX;
uri.Append(aUid);
rv = mCardList->GetLength(&addressCount);
NS_ENSURE_SUCCESS(rv, rv);
while (addressCount--)
{
nsCOMPtr<nsIAbOSXCard> osxCard(do_QueryElementAt(mCardList, addressCount, &rv));
if (NS_FAILED(rv))
continue;
nsAutoCString cardUri;
osxCard->GetURI(cardUri);
if (uri.Equals(cardUri)) {
nsCOMPtr<nsIAbCard> card(do_QueryInterface(osxCard, &rv));
if (NS_SUCCEEDED(rv))
return UnassertCard(abManager, card, mCardList);
}
}
return NS_OK;
}
|