summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/import/modules/BeckyProfileImporter.jsm
blob: 7c81ca7b665db286619a2696fba4dd089b986fcf (plain)
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
/* 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/. */

const EXPORTED_SYMBOLS = ["BeckyProfileImporter"];

var { BaseProfileImporter } = ChromeUtils.import(
  "resource:///modules/BaseProfileImporter.jsm"
);
var { setTimeout } = ChromeUtils.importESModule(
  "resource://gre/modules/Timer.sys.mjs"
);

/**
 * A module to import things from an becky profile dir into the current
 * profile.
 */
class BeckyProfileImporter extends BaseProfileImporter {
  SUPPORTED_ITEMS = {
    accounts: false,
    addressBooks: true,
    calendars: false,
    mailMessages: true,
  };

  async getSourceProfiles() {
    this._importModule = Cc[
      "@mozilla.org/import/import-becky;1"
    ].createInstance(Ci.nsIImportModule);
    this._importMailGeneric = this._importModule
      .GetImportInterface("mail")
      .QueryInterface(Ci.nsIImportGeneric);
    let importMail = this._importMailGeneric
      .GetData("mailInterface")
      .QueryInterface(Ci.nsIImportMail);
    let outLocation = {};
    let outFound = {};
    let outUserVerify = {};
    importMail.GetDefaultLocation(outLocation, outFound, outUserVerify);
    if (outLocation.value) {
      return [{ dir: outLocation.value }];
    }
    return [];
  }

  async startImport(sourceProfileDir, items) {
    this._logger.debug(
      `Start importing from ${sourceProfileDir.path}, items=${JSON.stringify(
        items
      )}`
    );
    this._itemsTotalCount = Object.values(items).filter(Boolean).length;
    this._itemsImportedCount = 0;

    let successStr = Cc["@mozilla.org/supports-string;1"].createInstance(
      Ci.nsISupportsString
    );
    let errorStr = Cc["@mozilla.org/supports-string;1"].createInstance(
      Ci.nsISupportsString
    );

    if (items.mailMessages) {
      // @see nsIImportGeneric.
      this._importMailGeneric.SetData("mailLocation", sourceProfileDir);
      let wantsProgress = this._importMailGeneric.WantsProgress();
      this._importMailGeneric.BeginImport(successStr, errorStr);
      if (wantsProgress) {
        while (this._importMailGeneric.GetProgress() < 100) {
          this._logger.debug(
            "Import mail messages progress:",
            this._importMailGeneric.GetProgress()
          );
          await new Promise(resolve => setTimeout(resolve, 50));
          this._importMailGeneric.ContinueImport();
        }
      }
      if (successStr.data) {
        this._logger.debug(
          "Finished importing mail messages:",
          successStr.data
        );
      }
      if (errorStr.data) {
        this._logger.error("Failed to import mail messages:", errorStr.data);
        throw new Error(errorStr.data);
      }
      await this._updateProgress();
    }

    if (items.addressBooks) {
      successStr.data = "";
      errorStr.data = "";

      let importABGeneric = this._importModule
        .GetImportInterface("addressbook")
        .QueryInterface(Ci.nsIImportGeneric);
      importABGeneric.SetData("addressLocation", sourceProfileDir);

      // @see nsIImportGeneric.
      let wantsProgress = importABGeneric.WantsProgress();
      importABGeneric.BeginImport(successStr, errorStr);
      if (wantsProgress) {
        while (importABGeneric.GetProgress() < 100) {
          this._logger.debug(
            "Import address books progress:",
            importABGeneric.GetProgress()
          );
          await new Promise(resolve => setTimeout(resolve, 50));
          importABGeneric.ContinueImport();
        }
      }
      if (successStr.data) {
        this._logger.debug(
          "Finished importing address books:",
          successStr.data
        );
      }
      if (errorStr.data) {
        this._logger.error("Failed to import address books:", errorStr.data);
        throw new Error(errorStr.data);
      }
      await this._updateProgress();
    }
  }
}