1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include "nsMailProfileMigratorUtils.h"
7 #include "nsISupportsPrimitives.h"
8 #include "nsProfileMigratorBase.h"
9 #include "nsIMailProfileMigrator.h"
10
11 #include "nsIImportSettings.h"
12 #include "nsIImportFilters.h"
13 #include "nsComponentManagerUtils.h"
14 #include "nsServiceManagerUtils.h"
15
16 #define kPersonalAddressbookUri "jsaddrbook://abook.sqlite"
17
nsProfileMigratorBase()18 nsProfileMigratorBase::nsProfileMigratorBase() {
19 mObserverService = do_GetService("@mozilla.org/observer-service;1");
20 mProcessingMailFolders = false;
21 }
22
~nsProfileMigratorBase()23 nsProfileMigratorBase::~nsProfileMigratorBase() {
24 if (mFileIOTimer) mFileIOTimer->Cancel();
25 }
26
ImportSettings(nsIImportModule * aImportModule)27 nsresult nsProfileMigratorBase::ImportSettings(nsIImportModule* aImportModule) {
28 nsresult rv;
29
30 nsAutoString index;
31 index.AppendInt(nsIMailProfileMigrator::ACCOUNT_SETTINGS);
32 NOTIFY_OBSERVERS(MIGRATION_ITEMBEFOREMIGRATE, index.get());
33
34 nsCOMPtr<nsISupports> supports;
35 rv = aImportModule->GetImportInterface(NS_IMPORT_SETTINGS_STR,
36 getter_AddRefs(supports));
37 NS_ENSURE_SUCCESS(rv, rv);
38
39 nsCOMPtr<nsIImportSettings> importSettings = do_QueryInterface(supports);
40 NS_ENSURE_SUCCESS(rv, rv);
41
42 bool importedSettings = false;
43
44 rv = importSettings->Import(getter_AddRefs(mLocalFolderAccount),
45 &importedSettings);
46
47 NOTIFY_OBSERVERS(MIGRATION_ITEMAFTERMIGRATE, index.get());
48
49 return rv;
50 }
51
ImportAddressBook(nsIImportModule * aImportModule)52 nsresult nsProfileMigratorBase::ImportAddressBook(
53 nsIImportModule* aImportModule) {
54 nsresult rv;
55
56 nsAutoString index;
57 index.AppendInt(nsIMailProfileMigrator::ADDRESSBOOK_DATA);
58 NOTIFY_OBSERVERS(MIGRATION_ITEMBEFOREMIGRATE, index.get());
59
60 nsCOMPtr<nsISupports> supports;
61 rv = aImportModule->GetImportInterface(NS_IMPORT_ADDRESS_STR,
62 getter_AddRefs(supports));
63 NS_ENSURE_SUCCESS(rv, rv);
64
65 mGenericImporter = do_QueryInterface(supports);
66 NS_ENSURE_SUCCESS(rv, rv);
67
68 nsCOMPtr<nsISupportsCString> pabString =
69 do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID, &rv);
70 NS_ENSURE_SUCCESS(rv, rv);
71
72 // We want to migrate the Outlook addressbook into our personal address book.
73 pabString->SetData(nsDependentCString(kPersonalAddressbookUri));
74 mGenericImporter->SetData("addressDestination", pabString);
75
76 bool importResult;
77 bool wantsProgress;
78 mGenericImporter->WantsProgress(&wantsProgress);
79 rv = mGenericImporter->BeginImport(nullptr, nullptr, &importResult);
80
81 if (wantsProgress)
82 ContinueImport();
83 else
84 FinishCopyingAddressBookData();
85
86 return rv;
87 }
88
FinishCopyingAddressBookData()89 nsresult nsProfileMigratorBase::FinishCopyingAddressBookData() {
90 nsAutoString index;
91 index.AppendInt(nsIMailProfileMigrator::ADDRESSBOOK_DATA);
92 NOTIFY_OBSERVERS(MIGRATION_ITEMAFTERMIGRATE, index.get());
93
94 // now kick off the mail migration code
95 ImportMailData(mImportModule);
96
97 return NS_OK;
98 }
99
ImportMailData(nsIImportModule * aImportModule)100 nsresult nsProfileMigratorBase::ImportMailData(nsIImportModule* aImportModule) {
101 nsresult rv;
102
103 nsAutoString index;
104 index.AppendInt(nsIMailProfileMigrator::MAILDATA);
105 NOTIFY_OBSERVERS(MIGRATION_ITEMBEFOREMIGRATE, index.get());
106
107 nsCOMPtr<nsISupports> supports;
108 rv = aImportModule->GetImportInterface(NS_IMPORT_MAIL_STR,
109 getter_AddRefs(supports));
110 NS_ENSURE_SUCCESS(rv, rv);
111
112 mGenericImporter = do_QueryInterface(supports);
113 NS_ENSURE_SUCCESS(rv, rv);
114
115 nsCOMPtr<nsISupportsPRBool> migrating =
116 do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID, &rv);
117 NS_ENSURE_SUCCESS(rv, rv);
118
119 // by setting the migration flag, we force the import utility to install local
120 // folders from OE directly into Local Folders and not as a subfolder
121 migrating->SetData(true);
122 mGenericImporter->SetData("migration", migrating);
123
124 bool importResult;
125 bool wantsProgress;
126 mGenericImporter->WantsProgress(&wantsProgress);
127 rv = mGenericImporter->BeginImport(nullptr, nullptr, &importResult);
128
129 mProcessingMailFolders = true;
130
131 if (wantsProgress)
132 ContinueImport();
133 else
134 FinishCopyingMailFolders();
135
136 return rv;
137 }
138
FinishCopyingMailFolders()139 nsresult nsProfileMigratorBase::FinishCopyingMailFolders() {
140 nsAutoString index;
141 index.AppendInt(nsIMailProfileMigrator::MAILDATA);
142 NOTIFY_OBSERVERS(MIGRATION_ITEMAFTERMIGRATE, index.get());
143
144 // now kick off the filters migration code
145 return ImportFilters(mImportModule);
146 }
147
ImportFilters(nsIImportModule * aImportModule)148 nsresult nsProfileMigratorBase::ImportFilters(nsIImportModule* aImportModule) {
149 nsresult rv = NS_OK;
150
151 nsCOMPtr<nsISupports> supports;
152 nsresult rv2 = aImportModule->GetImportInterface(NS_IMPORT_FILTERS_STR,
153 getter_AddRefs(supports));
154 nsCOMPtr<nsIImportFilters> importFilters = do_QueryInterface(supports);
155
156 if (NS_SUCCEEDED(rv2) && importFilters) {
157 nsAutoString index;
158 index.AppendInt(nsIMailProfileMigrator::FILTERS);
159 NOTIFY_OBSERVERS(MIGRATION_ITEMBEFOREMIGRATE, index.get());
160
161 bool importedFilters = false;
162 char16_t* error;
163
164 rv = importFilters->Import(&error, &importedFilters);
165
166 NOTIFY_OBSERVERS(MIGRATION_ITEMAFTERMIGRATE, index.get());
167 }
168
169 // migration is now done...notify the UI.
170 NOTIFY_OBSERVERS(MIGRATION_ENDED, nullptr);
171
172 return rv;
173 }
174