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
|
/* -*- 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 "prprf.h"
#include "plstr.h"
#include "nsCOMPtr.h"
#include "nsMsgUtils.h"
#include "nsIImportService.h"
#include "nsIImportAddressBooks.h"
#include "nsIImportGeneric.h"
#include "nsISupportsPrimitives.h"
#include "nsIImportABDescriptor.h"
#include "nsIImportFieldMap.h"
#include "nsStringGlue.h"
#include "nsIFile.h"
#include "nsIAddrDatabase.h"
#include "nsIAbManager.h"
#include "nsIAbLDIFService.h"
#include "nsAbBaseCID.h"
#include "nsIStringBundle.h"
#include "nsImportStringBundle.h"
#include "nsTextFormatter.h"
#include "nsServiceManagerUtils.h"
#include "msgCore.h"
#include "ImportDebug.h"
#include "nsIAbMDBDirectory.h"
#include "nsComponentManagerUtils.h"
#include "nsIArray.h"
#include "nsCOMArray.h"
#include "nsArrayUtils.h"
static void ImportAddressThread(void *stuff);
class AddressThreadData;
class nsImportGenericAddressBooks : public nsIImportGeneric
{
public:
nsImportGenericAddressBooks();
NS_DECL_THREADSAFE_ISUPPORTS
/* nsISupports GetData (in string dataId); */
NS_IMETHOD GetData(const char *dataId, nsISupports **_retval) override;
NS_IMETHOD SetData(const char *dataId, nsISupports *pData) override;
NS_IMETHOD GetStatus(const char *statusKind, int32_t *_retval) override;
NS_IMETHOD WantsProgress(bool *_retval) override;
NS_IMETHOD BeginImport(nsISupportsString *successLog, nsISupportsString *errorLog, bool *_retval) override;
NS_IMETHOD ContinueImport(bool *_retval) override;
NS_IMETHOD GetProgress(int32_t *_retval) override;
NS_IMETHOD CancelImport(void) override;
private:
virtual ~nsImportGenericAddressBooks();
void GetDefaultLocation(void);
void GetDefaultBooks(void);
void GetDefaultFieldMap(void);
public:
static void SetLogs(nsString& success, nsString& error, nsISupportsString *pSuccess, nsISupportsString *pError);
static void ReportError(const char16_t *pName, nsString *pStream,
nsIStringBundle *aBundle);
private:
nsIImportAddressBooks * m_pInterface;
nsCOMPtr<nsIArray> m_Books;
nsCOMArray<nsIAddrDatabase> m_DBs;
nsCOMPtr <nsIFile> m_pLocation;
nsIImportFieldMap * m_pFieldMap;
bool m_autoFind;
char16_t * m_description;
bool m_gotLocation;
bool m_found;
bool m_userVerify;
nsISupportsString * m_pSuccessLog;
nsISupportsString * m_pErrorLog;
uint32_t m_totalSize;
bool m_doImport;
AddressThreadData * m_pThreadData;
char * m_pDestinationUri;
nsCOMPtr<nsIStringBundle> m_stringBundle;
};
class AddressThreadData {
public:
bool driverAlive;
bool threadAlive;
bool abort;
bool fatalError;
uint32_t currentTotal;
uint32_t currentSize;
nsIArray *books;
nsCOMArray<nsIAddrDatabase>* dBs;
nsCOMPtr<nsIAbLDIFService> ldifService;
nsIImportAddressBooks * addressImport;
nsIImportFieldMap * fieldMap;
nsISupportsString * successLog;
nsISupportsString * errorLog;
char * pDestinationUri;
nsIStringBundle* stringBundle;
AddressThreadData();
~AddressThreadData();
};
nsresult NS_NewGenericAddressBooks(nsIImportGeneric** aImportGeneric)
{
NS_PRECONDITION(aImportGeneric != nullptr, "null ptr");
if (! aImportGeneric)
return NS_ERROR_NULL_POINTER;
nsImportGenericAddressBooks *pGen = new nsImportGenericAddressBooks();
if (pGen == nullptr)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(pGen);
nsresult rv = pGen->QueryInterface(NS_GET_IID(nsIImportGeneric), (void **)aImportGeneric);
NS_RELEASE(pGen);
return rv;
}
nsImportGenericAddressBooks::nsImportGenericAddressBooks()
{
m_pInterface = nullptr;
m_pSuccessLog = nullptr;
m_pErrorLog = nullptr;
m_totalSize = 0;
m_doImport = false;
m_pThreadData = nullptr;
m_pDestinationUri = nullptr;
m_pFieldMap = nullptr;
m_autoFind = false;
m_description = nullptr;
m_gotLocation = false;
m_found = false;
m_userVerify = false;
nsImportStringBundle::GetStringBundle(IMPORT_MSGS_URL, getter_AddRefs(m_stringBundle));
}
nsImportGenericAddressBooks::~nsImportGenericAddressBooks()
{
if (m_pDestinationUri)
NS_Free(m_pDestinationUri);
if (m_description)
NS_Free(m_description);
NS_IF_RELEASE(m_pFieldMap);
NS_IF_RELEASE(m_pInterface);
NS_IF_RELEASE(m_pSuccessLog);
NS_IF_RELEASE(m_pErrorLog);
}
NS_IMPL_ISUPPORTS(nsImportGenericAddressBooks, nsIImportGeneric)
NS_IMETHODIMP nsImportGenericAddressBooks::GetData(const char *dataId, nsISupports **_retval)
{
NS_PRECONDITION(_retval != nullptr, "null ptr");
if (!_retval)
return NS_ERROR_NULL_POINTER;
nsresult rv;
*_retval = nullptr;
if (!PL_strcasecmp(dataId, "addressInterface")) {
*_retval = m_pInterface;
NS_IF_ADDREF(m_pInterface);
}
if (!PL_strcasecmp(dataId, "addressLocation")) {
if (!m_pLocation)
GetDefaultLocation();
NS_IF_ADDREF(*_retval = m_pLocation);
}
if (!PL_strcasecmp(dataId, "addressBooks")) {
if (!m_pLocation)
GetDefaultLocation();
if (!m_Books)
GetDefaultBooks();
*_retval = m_Books;
}
if (!PL_strcasecmp(dataId, "addressDestination")) {
if (m_pDestinationUri) {
nsCOMPtr<nsISupportsCString> abString = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
abString->SetData(nsDependentCString(m_pDestinationUri));
NS_IF_ADDREF(*_retval = abString);
}
}
if (!PL_strcasecmp(dataId, "fieldMap")) {
if (m_pFieldMap) {
*_retval = m_pFieldMap;
m_pFieldMap->AddRef();
}
else {
if (m_pInterface && m_pLocation) {
bool needsIt = false;
m_pInterface->GetNeedsFieldMap(m_pLocation, &needsIt);
if (needsIt) {
GetDefaultFieldMap();
if (m_pFieldMap) {
*_retval = m_pFieldMap;
m_pFieldMap->AddRef();
}
}
}
}
}
if (!PL_strncasecmp(dataId, "sampleData-", 11)) {
// extra the record number
const char *pNum = dataId + 11;
int32_t rNum = 0;
while (*pNum) {
rNum *= 10;
rNum += (*pNum - '0');
pNum++;
}
IMPORT_LOG1("Requesting sample data #: %ld\n", (long)rNum);
if (m_pInterface) {
nsCOMPtr<nsISupportsString> data = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
if (NS_FAILED(rv))
return rv;
char16_t * pData = nullptr;
bool found = false;
rv = m_pInterface->GetSampleData(rNum, &found, &pData);
if (NS_FAILED(rv))
return rv;
if (found) {
data->SetData(nsDependentString(pData));
*_retval = data;
NS_ADDREF(*_retval);
}
NS_Free(pData);
}
}
return NS_OK;
}
NS_IMETHODIMP nsImportGenericAddressBooks::SetData(const char *dataId, nsISupports *item)
{
NS_PRECONDITION(dataId != nullptr, "null ptr");
if (!dataId)
return NS_ERROR_NULL_POINTER;
if (!PL_strcasecmp(dataId, "addressInterface")) {
NS_IF_RELEASE(m_pInterface);
if (item)
item->QueryInterface(NS_GET_IID(nsIImportAddressBooks), (void **) &m_pInterface);
}
if (!PL_strcasecmp(dataId, "addressBooks")) {
if (item)
item->QueryInterface(NS_GET_IID(nsIArray), (void **) &m_Books);
}
if (!PL_strcasecmp(dataId, "addressLocation")) {
m_pLocation = nullptr;
if (item) {
nsresult rv;
m_pLocation = do_QueryInterface(item, &rv);
NS_ENSURE_SUCCESS(rv,rv);
}
if (m_pInterface)
m_pInterface->SetSampleLocation(m_pLocation);
}
if (!PL_strcasecmp(dataId, "addressDestination")) {
if (item) {
nsCOMPtr<nsISupportsCString> abString;
item->QueryInterface(NS_GET_IID(nsISupportsCString), getter_AddRefs(abString));
if (abString) {
if (m_pDestinationUri)
NS_Free(m_pDestinationUri);
m_pDestinationUri = nullptr;
nsAutoCString tempUri;
abString->GetData(tempUri);
m_pDestinationUri = ToNewCString(tempUri);
}
}
}
if (!PL_strcasecmp(dataId, "fieldMap")) {
NS_IF_RELEASE(m_pFieldMap);
if (item)
item->QueryInterface(NS_GET_IID(nsIImportFieldMap), (void **) &m_pFieldMap);
}
return NS_OK;
}
NS_IMETHODIMP nsImportGenericAddressBooks::GetStatus(const char *statusKind, int32_t *_retval)
{
NS_PRECONDITION(statusKind != nullptr, "null ptr");
NS_PRECONDITION(_retval != nullptr, "null ptr");
if (!statusKind || !_retval)
return NS_ERROR_NULL_POINTER;
*_retval = 0;
if (!PL_strcasecmp(statusKind, "isInstalled")) {
GetDefaultLocation();
*_retval = (int32_t) m_found;
}
if (!PL_strcasecmp(statusKind, "canUserSetLocation")) {
GetDefaultLocation();
*_retval = (int32_t) m_userVerify;
}
if (!PL_strcasecmp(statusKind, "autoFind")) {
GetDefaultLocation();
*_retval = (int32_t) m_autoFind;
}
if (!PL_strcasecmp(statusKind, "supportsMultiple")) {
bool multi = false;
if (m_pInterface)
m_pInterface->GetSupportsMultiple(&multi);
*_retval = (int32_t) multi;
}
if (!PL_strcasecmp(statusKind, "needsFieldMap")) {
bool needs = false;
if (m_pInterface && m_pLocation)
m_pInterface->GetNeedsFieldMap(m_pLocation, &needs);
*_retval = (int32_t) needs;
}
return NS_OK;
}
void nsImportGenericAddressBooks::GetDefaultLocation(void)
{
if (!m_pInterface)
return;
if ((m_pLocation && m_gotLocation) || m_autoFind)
return;
if (m_description)
NS_Free(m_description);
m_description = nullptr;
m_pInterface->GetAutoFind(&m_description, &m_autoFind);
m_gotLocation = true;
if (m_autoFind) {
m_found = true;
m_userVerify = false;
return;
}
nsCOMPtr <nsIFile> pLoc;
m_pInterface->GetDefaultLocation(getter_AddRefs(pLoc), &m_found, &m_userVerify);
if (!m_pLocation)
m_pLocation = pLoc;
}
void nsImportGenericAddressBooks::GetDefaultBooks(void)
{
if (!m_pInterface || m_Books)
return;
if (!m_pLocation && !m_autoFind)
return;
nsresult rv = m_pInterface->FindAddressBooks(m_pLocation, getter_AddRefs(m_Books));
if (NS_FAILED(rv)) {
IMPORT_LOG0("*** Error: FindAddressBooks failed\n");
}
}
void nsImportGenericAddressBooks::GetDefaultFieldMap(void)
{
if (!m_pInterface || !m_pLocation)
return;
NS_IF_RELEASE(m_pFieldMap);
nsresult rv;
nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
if (NS_FAILED(rv)) {
IMPORT_LOG0("*** Unable to get nsIImportService.\n");
return;
}
rv = impSvc->CreateNewFieldMap(&m_pFieldMap);
if (NS_FAILED(rv))
return;
int32_t sz = 0;
rv = m_pFieldMap->GetNumMozFields(&sz);
if (NS_SUCCEEDED(rv))
rv = m_pFieldMap->DefaultFieldMap(sz);
if (NS_SUCCEEDED(rv))
rv = m_pInterface->InitFieldMap(m_pFieldMap);
if (NS_FAILED(rv)) {
IMPORT_LOG0("*** Error: Unable to initialize field map\n");
NS_IF_RELEASE(m_pFieldMap);
}
}
NS_IMETHODIMP nsImportGenericAddressBooks::WantsProgress(bool *_retval)
{
NS_PRECONDITION(_retval != nullptr, "null ptr");
NS_ENSURE_ARG_POINTER(_retval);
GetDefaultLocation();
GetDefaultBooks();
bool result = false;
if (m_Books) {
uint32_t count = 0;
uint32_t i;
bool import;
uint32_t size;
uint32_t totalSize = 0;
m_Books->GetLength(&count);
for (i = 0; i < count; i++) {
nsCOMPtr<nsIImportABDescriptor> book = do_QueryElementAt(m_Books, i);
if (book) {
import = false;
size = 0;
nsresult rv = book->GetImport(&import);
if (NS_SUCCEEDED(rv) && import) {
(void) book->GetSize(&size);
result = true;
}
totalSize += size;
}
}
m_totalSize = totalSize;
}
m_doImport = result;
*_retval = result;
return NS_OK;
}
void nsImportGenericAddressBooks::SetLogs(nsString& success, nsString& error, nsISupportsString *pSuccess, nsISupportsString *pError)
{
nsAutoString str;
if (pSuccess) {
pSuccess->GetData(str);
str.Append(success);
pSuccess->SetData(success);
}
if (pError) {
pError->GetData(str);
str.Append(error);
pError->SetData(error);
}
}
already_AddRefed<nsIAddrDatabase> GetAddressBookFromUri(const char *pUri)
{
if (!pUri)
return nullptr;
nsCOMPtr<nsIAbManager> abManager = do_GetService(NS_ABMANAGER_CONTRACTID);
if (!abManager)
return nullptr;
nsCOMPtr<nsIAbDirectory> directory;
abManager->GetDirectory(nsDependentCString(pUri),
getter_AddRefs(directory));
if (!directory)
return nullptr;
nsCOMPtr<nsIAbMDBDirectory> mdbDirectory = do_QueryInterface(directory);
if (!mdbDirectory)
return nullptr;
nsCOMPtr<nsIAddrDatabase> pDatabase;
mdbDirectory->GetDatabase(getter_AddRefs(pDatabase));
return pDatabase.forget();
}
already_AddRefed<nsIAddrDatabase> GetAddressBook(const char16_t *name,
bool makeNew)
{
if (!makeNew) {
// FIXME: How do I get the list of address books and look for a
// specific name. Major bogosity!
// For now, assume we didn't find anything with that name
}
IMPORT_LOG0("In GetAddressBook\n");
nsresult rv;
nsCOMPtr<nsIAddrDatabase> pDatabase;
nsCOMPtr<nsIFile> dbPath;
nsCOMPtr<nsIAbManager> abManager = do_GetService(NS_ABMANAGER_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv))
{
/* Get the profile directory */
rv = abManager->GetUserProfileDirectory(getter_AddRefs(dbPath));
if (NS_SUCCEEDED(rv))
{
// Create a new address book file - we don't care what the file
// name is, as long as it's unique
rv = dbPath->Append(NS_LITERAL_STRING("impab.mab"));
if (NS_SUCCEEDED(rv))
{
rv = dbPath->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
if (NS_SUCCEEDED(rv))
{
IMPORT_LOG0("Getting the address database factory\n");
nsCOMPtr<nsIAddrDatabase> addrDBFactory =
do_GetService(NS_ADDRDATABASE_CONTRACTID, &rv);
if (NS_FAILED(rv))
return nullptr;
IMPORT_LOG0("Opening the new address book\n");
rv = addrDBFactory->Open(dbPath, true, true,
getter_AddRefs(pDatabase));
}
}
}
}
if (NS_FAILED(rv))
{
IMPORT_LOG0("Failed to get the user profile directory from the address book session\n");
}
if (pDatabase && dbPath)
{
// We made a database, add it to the UI?!?!?!?!?!?!
// This is major bogosity again! Why doesn't the address book
// just handle this properly for me? Uggggg...
nsCOMPtr<nsIAbDirectory> parentDir;
abManager->GetDirectory(NS_LITERAL_CSTRING(kAllDirectoryRoot),
getter_AddRefs(parentDir));
if (parentDir)
{
nsAutoCString URI("moz-abmdbdirectory://");
nsAutoCString leafName;
rv = dbPath->GetNativeLeafName(leafName);
if (NS_FAILED(rv))
IMPORT_LOG0("*** Error: Unable to get name of database file\n");
else
{
URI.Append(leafName);
rv = parentDir->CreateDirectoryByURI(nsDependentString(name), URI);
if (NS_FAILED(rv))
IMPORT_LOG0("*** Error: Unable to create address book directory\n");
}
}
if (NS_SUCCEEDED(rv))
IMPORT_LOG0("Added new address book to the UI\n");
else
IMPORT_LOG0("*** Error: An error occurred while adding the address book to the UI\n");
}
return pDatabase.forget();
}
NS_IMETHODIMP nsImportGenericAddressBooks::BeginImport(nsISupportsString *successLog, nsISupportsString *errorLog, bool *_retval)
{
NS_PRECONDITION(_retval != nullptr, "null ptr");
if (!_retval)
return NS_ERROR_NULL_POINTER;
nsString success;
nsString error;
if (!m_doImport) {
*_retval = true;
nsImportStringBundle::GetStringByID(IMPORT_NO_ADDRBOOKS, m_stringBundle,
success);
SetLogs(success, error, successLog, errorLog);
return NS_OK;
}
if (!m_pInterface || !m_Books) {
nsImportStringBundle::GetStringByID(IMPORT_ERROR_AB_NOTINITIALIZED,
m_stringBundle, error);
SetLogs(success, error, successLog, errorLog);
*_retval = false;
return NS_OK;
}
bool needsFieldMap = false;
if (NS_FAILED(m_pInterface->GetNeedsFieldMap(m_pLocation, &needsFieldMap)) ||
(needsFieldMap && !m_pFieldMap)) {
nsImportStringBundle::GetStringByID(IMPORT_ERROR_AB_NOTINITIALIZED,
m_stringBundle, error);
SetLogs(success, error, successLog, errorLog);
*_retval = false;
return NS_OK;
}
NS_IF_RELEASE(m_pSuccessLog);
NS_IF_RELEASE(m_pErrorLog);
m_pSuccessLog = successLog;
m_pErrorLog = errorLog;
NS_IF_ADDREF(m_pSuccessLog);
NS_IF_ADDREF(m_pErrorLog);
// create the info need to drive address book import. We're
// not going to create a new thread for this since address books
// don't tend to be large, and import is rare.
m_pThreadData = new AddressThreadData();
m_pThreadData->books = m_Books;
NS_ADDREF(m_Books);
m_pThreadData->addressImport = m_pInterface;
NS_ADDREF(m_pInterface);
m_pThreadData->fieldMap = m_pFieldMap;
NS_IF_ADDREF(m_pFieldMap);
m_pThreadData->errorLog = m_pErrorLog;
NS_IF_ADDREF(m_pErrorLog);
m_pThreadData->successLog = m_pSuccessLog;
NS_IF_ADDREF(m_pSuccessLog);
if (m_pDestinationUri)
m_pThreadData->pDestinationUri = strdup(m_pDestinationUri);
uint32_t count = 0;
m_Books->GetLength(&count);
// Create/obtain any address books that we need here, so that we don't need
// to do so inside the import thread which would just proxy the create
// operations back to the main thread anyway.
nsCOMPtr<nsIAddrDatabase> db = GetAddressBookFromUri(m_pDestinationUri);
for (uint32_t i = 0; i < count; ++i)
{
nsCOMPtr<nsIImportABDescriptor> book = do_QueryElementAt(m_Books, i);
if (book)
{
if (!db)
{
nsString name;
book->GetPreferredName(name);
db = GetAddressBook(name.get(), true);
}
m_DBs.AppendObject(db);
}
}
m_pThreadData->dBs = &m_DBs;
NS_IF_ADDREF(m_pThreadData->stringBundle = m_stringBundle);
nsresult rv;
m_pThreadData->ldifService = do_GetService(NS_ABLDIFSERVICE_CONTRACTID, &rv);
ImportAddressThread(m_pThreadData);
delete m_pThreadData;
m_pThreadData = nullptr;
*_retval = true;
return NS_OK;
}
NS_IMETHODIMP nsImportGenericAddressBooks::ContinueImport(bool *_retval)
{
NS_PRECONDITION(_retval != nullptr, "null ptr");
if (!_retval)
return NS_ERROR_NULL_POINTER;
*_retval = true;
if (m_pThreadData) {
if (m_pThreadData->fatalError)
*_retval = false;
}
return NS_OK;
}
NS_IMETHODIMP nsImportGenericAddressBooks::GetProgress(int32_t *_retval)
{
// This returns the progress from the the currently
// running import mail or import address book thread.
NS_PRECONDITION(_retval != nullptr, "null ptr");
if (!_retval)
return NS_ERROR_NULL_POINTER;
if (!m_pThreadData || !(m_pThreadData->threadAlive)) {
*_retval = 100;
return NS_OK;
}
uint32_t sz = 0;
if (m_pThreadData->currentSize && m_pInterface) {
if (NS_FAILED(m_pInterface->GetImportProgress(&sz)))
sz = 0;
}
if (m_totalSize)
*_retval = ((m_pThreadData->currentTotal + sz) * 100) / m_totalSize;
else
*_retval = 0;
// never return less than 5 so it looks like we are doing something!
if (*_retval < 5)
*_retval = 5;
// as long as the thread is alive don't return completely
// done.
if (*_retval > 99)
*_retval = 99;
return NS_OK;
}
NS_IMETHODIMP nsImportGenericAddressBooks::CancelImport(void)
{
if (m_pThreadData) {
m_pThreadData->abort = true;
m_pThreadData = nullptr;
}
return NS_OK;
}
AddressThreadData::AddressThreadData()
{
fatalError = false;
driverAlive = true;
threadAlive = true;
abort = false;
currentTotal = 0;
currentSize = 0;
books = nullptr;
addressImport = nullptr;
successLog = nullptr;
errorLog = nullptr;
pDestinationUri = nullptr;
fieldMap = nullptr;
stringBundle = nullptr;
ldifService = nullptr;
}
AddressThreadData::~AddressThreadData()
{
if (pDestinationUri)
NS_Free(pDestinationUri);
NS_IF_RELEASE(books);
NS_IF_RELEASE(addressImport);
NS_IF_RELEASE(errorLog);
NS_IF_RELEASE(successLog);
NS_IF_RELEASE(fieldMap);
NS_IF_RELEASE(stringBundle);
}
void nsImportGenericAddressBooks::ReportError(const char16_t *pName,
nsString *pStream,
nsIStringBundle* aBundle)
{
if (!pStream)
return;
// load the error string
char16_t *pFmt = nsImportStringBundle::GetStringByID(IMPORT_ERROR_GETABOOK, aBundle);
char16_t *pText = nsTextFormatter::smprintf(pFmt, pName);
pStream->Append(pText);
nsTextFormatter::smprintf_free(pText);
NS_Free(pFmt);
pStream->AppendLiteral(MSG_LINEBREAK);
}
static void ImportAddressThread(void *stuff)
{
IMPORT_LOG0("In Begin ImportAddressThread\n");
AddressThreadData *pData = (AddressThreadData *)stuff;
uint32_t count = 0;
uint32_t i;
bool import;
uint32_t size;
nsString success;
nsString error;
(void) pData->books->GetLength(&count);
for (i = 0; (i < count) && !(pData->abort); i++) {
nsCOMPtr<nsIImportABDescriptor> book =
do_QueryElementAt(pData->books, i);
if (book) {
import = false;
size = 0;
nsresult rv = book->GetImport(&import);
if (NS_SUCCEEDED(rv) && import)
rv = book->GetSize(&size);
if (NS_SUCCEEDED(rv) && size && import) {
nsString name;
book->GetPreferredName(name);
nsCOMPtr<nsIAddrDatabase> db = pData->dBs->ObjectAt(i);
bool fatalError = false;
pData->currentSize = size;
if (db) {
char16_t *pSuccess = nullptr;
char16_t *pError = nullptr;
/*
if (pData->fieldMap) {
int32_t sz = 0;
int32_t mapIndex;
bool active;
pData->fieldMap->GetMapSize(&sz);
IMPORT_LOG1("**** Field Map Size: %d\n", (int) sz);
for (int32_t i = 0; i < sz; i++) {
pData->fieldMap->GetFieldMap(i, &mapIndex);
pData->fieldMap->GetFieldActive(i, &active);
IMPORT_LOG3("Field map #%d: index=%d, active=%d\n", (int) i, (int) mapIndex, (int) active);
}
}
*/
rv = pData->addressImport->ImportAddressBook(book,
db,
pData->fieldMap,
pData->ldifService,
&pError,
&pSuccess,
&fatalError);
if (NS_SUCCEEDED(rv) && pSuccess) {
success.Append(pSuccess);
NS_Free(pSuccess);
}
if (pError) {
error.Append(pError);
NS_Free(pError);
}
}
else {
nsImportGenericAddressBooks::ReportError(name.get(), &error, pData->stringBundle);
}
pData->currentSize = 0;
pData->currentTotal += size;
if (db)
db->Close(true);
if (fatalError) {
pData->fatalError = true;
break;
}
}
}
}
nsImportGenericAddressBooks::SetLogs(success, error, pData->successLog, pData->errorLog);
if (pData->abort || pData->fatalError) {
// FIXME: do what is necessary to get rid of what has been imported so far.
// Nothing if we went into an existing address book! Otherwise, delete
// the ones we created?
}
}
|