summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/cloudfile/cloudFileAccounts.jsm
blob: 3cb478f60f83c7d6f8543a612aabb892ecc10c2d (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
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
/* 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 = ["cloudFileAccounts"];

var ACCOUNT_ROOT = "mail.cloud_files.accounts.";

var { EventEmitter } = ChromeUtils.importESModule(
  "resource://gre/modules/EventEmitter.sys.mjs"
);

var cloudFileAccounts = new (class extends EventEmitter {
  get constants() {
    return {
      offlineErr: 0x80550014, // NS_MSG_ERROR_OFFLINE
      authErr: 0x8055001e, // NS_MSG_USER_NOT_AUTHENTICATED
      uploadErr: 0x8055311a, // NS_MSG_ERROR_ATTACHING_FILE
      uploadWouldExceedQuota: 0x8055311b,
      uploadExceedsFileLimit: 0x8055311c,
      uploadCancelled: 0x8055311d,
      uploadErrWithCustomMessage: 0x8055311f,
      renameErr: 0x80553120,
      renameErrWithCustomMessage: 0x80553121,
      renameNotSupported: 0x80553122,
      deleteErr: 0x80553123,
      attachmentErr: 0x80553124,
      accountErr: 0x80553125,
    };
  }

  constructor() {
    super();
    this._providers = new Map();
    this._accounts = new Map();
    this._highestOrdinal = 0;
  }

  get _accountKeys() {
    let accountKeySet = new Set();
    let branch = Services.prefs.getBranch(ACCOUNT_ROOT);
    let children = branch.getChildList("");
    for (let child of children) {
      let subbranch = child.substr(0, child.indexOf("."));
      accountKeySet.add(subbranch);

      let match = /^account(\d+)$/.exec(subbranch);
      if (match) {
        let ordinal = parseInt(match[1], 10);
        this._highestOrdinal = Math.max(this._highestOrdinal, ordinal);
      }
    }

    // TODO: sort by ordinal
    return accountKeySet.keys();
  }

  /**
   * Ensure that we have the account key for an account. If we already have the
   * key, just return it. If we have the account, get the key from it.
   *
   * @param aKeyOrAccount the key or the account object
   * @returns the account key
   */
  _ensureKey(aKeyOrAccount) {
    if (typeof aKeyOrAccount == "string") {
      return aKeyOrAccount;
    }
    if ("accountKey" in aKeyOrAccount) {
      return aKeyOrAccount.accountKey;
    }
    throw new Error("String or cloud file account expected");
  }

  /**
   * Register a cloudfile provider, e.g. from an extension.
   *
   * @param {object} The implementation to register
   */
  registerProvider(aType, aProvider) {
    if (this._providers.has(aType)) {
      throw new Error(`Cloudfile provider ${aType} is already registered`);
    }
    this._providers.set(aType, aProvider);
    this.emit("providerRegistered", aProvider);
  }

  /**
   * Unregister a cloudfile provider.
   *
   * @param {string} aType - The provider type to unregister
   */
  unregisterProvider(aType) {
    if (!this._providers.has(aType)) {
      throw new Error(`Cloudfile provider ${aType} is not registered`);
    }

    for (let account of this.getAccountsForType(aType)) {
      this._accounts.delete(account.accountKey);
    }

    this._providers.delete(aType);
    this.emit("providerUnregistered", aType);
  }

  get providers() {
    return [...this._providers.values()];
  }

  getProviderForType(aType) {
    return this._providers.get(aType);
  }

  createAccount(aType) {
    this._highestOrdinal++;
    let key = "account" + this._highestOrdinal;

    try {
      let provider = this.getProviderForType(aType);
      let account = provider.initAccount(key);

      Services.prefs.setCharPref(ACCOUNT_ROOT + key + ".type", aType);
      Services.prefs.setCharPref(
        ACCOUNT_ROOT + key + ".displayName",
        account.displayName
      );

      this._accounts.set(key, account);
      this.emit("accountAdded", account);
      return account;
    } catch (e) {
      for (let prefName of Services.prefs.getChildList(
        `${ACCOUNT_ROOT}${key}.`
      )) {
        Services.prefs.clearUserPref(prefName);
      }
      throw e;
    }
  }

  removeAccount(aKeyOrAccount) {
    let key = this._ensureKey(aKeyOrAccount);
    let type = Services.prefs.getCharPref(ACCOUNT_ROOT + key + ".type");

    this._accounts.delete(key);
    for (let prefName of Services.prefs.getChildList(
      `${ACCOUNT_ROOT}${key}.`
    )) {
      Services.prefs.clearUserPref(prefName);
    }

    this.emit("accountDeleted", key, type);
  }

  get accounts() {
    let arr = [];
    for (let key of this._accountKeys) {
      let account = this.getAccount(key);
      if (account) {
        arr.push(account);
      }
    }
    return arr;
  }

  get configuredAccounts() {
    return this.accounts.filter(account => account.configured);
  }

  getAccount(aKey) {
    if (this._accounts.has(aKey)) {
      return this._accounts.get(aKey);
    }

    let type = Services.prefs.getCharPref(ACCOUNT_ROOT + aKey + ".type", "");
    if (type) {
      let provider = this.getProviderForType(type);
      if (provider) {
        let account = provider.initAccount(aKey);
        this._accounts.set(aKey, account);
        return account;
      }
    }
    return null;
  }

  getAccountsForType(aType) {
    let result = [];

    for (let accountKey of this._accountKeys) {
      let type = Services.prefs.getCharPref(
        ACCOUNT_ROOT + accountKey + ".type"
      );
      if (type === aType) {
        result.push(this.getAccount(accountKey));
      }
    }

    return result;
  }

  getDisplayName(aKeyOrAccount) {
    // If no display name has been set, we return the empty string.
    let key = this._ensureKey(aKeyOrAccount);
    return Services.prefs.getCharPref(ACCOUNT_ROOT + key + ".displayName", "");
  }

  setDisplayName(aKeyOrAccount, aDisplayName) {
    let key = this._ensureKey(aKeyOrAccount);
    Services.prefs.setCharPref(
      ACCOUNT_ROOT + key + ".displayName",
      aDisplayName
    );
  }
})();