cleidigh / ThunderKdB

Thunderbird Addon Code Knowledge Base
11 stars 7 forks source link

How do you get the account name for a given folder? #33

Closed eyalroz closed 3 years ago

eyalroz commented 3 years ago

Suppose I have a folder (i.e. an nsIMsgFolder instance). How do I get its account? I suppose I could parse its URI to get some string ID, but is there a more civilized and idiomatic way to do this?

PS - Asking about regular Thunderbird extension/core code, not WebExtensions.

eyalroz commented 3 years ago

Given a nsIMsgFolder f, you can get the root folder as f.server.rootFolder. Now, for many things, it's actually the root folder you want rather than the account. If you really need the nsIMsgAccount, you can get it with this function:

function rootFolderToAccount(rootFolder) {
  for (let account of MailServices.accounts.accounts) {
    if (rootFolder == account.incomingServer.rootMsgFolder)
      return account;
  }
  return null;
}

yes, it's an iteration :-(

jobisoft commented 3 years ago
let server = folder.server;
let account = MailServices.accounts.FindAccountForServer(server);
jobisoft commented 3 years ago

But I guess it is doing the iteration internally as well.