summaryrefslogtreecommitdiffstats
path: root/mailnews/base/content/msgAccountCentral.js
blob: de5543dffe3dc6be8409ca13d26579090052a8bb (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
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * 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/. */

Components.utils.import("resource://gre/modules/Services.jsm");

var selectedServer = null;

function OnInit()
{
    // Set the header for the page.
    // Title containts the brand name of the application and the account
    // type (mail/news) and the name of the account
    try {
        let msgFolder    = null;
        let title;

        // Get the brand name
        let brandName = document.getElementById("bundle_brand")
                                .getString("brandShortName");
        let messengerBundle = document.getElementById("bundle_messenger");

        selectedServer = GetSelectedServer();
        if (selectedServer) {
            // Get the account type
            let serverType = selectedServer.type;
            let acctType;
            if (serverType == "nntp")
                acctType = messengerBundle.getString("newsAcctType");
            else if (serverType == "rss")
                acctType = messengerBundle.getString("feedsAcctType");
            else
                acctType = messengerBundle.getString("mailAcctType");

            // Get the account name
            msgFolder = GetSelectedMsgFolder();
            ArrangeAccountCentralItems(selectedServer, msgFolder);

            let acctName = msgFolder.prettyName;
            // Display and collapse items presented to the user based on account type
            title = messengerBundle.getFormattedString("acctCentralTitleFormat",
                                                       [brandName, acctType, acctName]);
        } else {
          // If there is no selectedServer, we are in a brand new profile with
          // no accounts - show the create account rows.
          title = brandName;
          SetItemDisplay("AccountsHeader", true);
          SetItemDisplay("CreateAccount", true);
          SetItemDisplay("CreateAccounts", true);
        }

        // Set the title for the document
        document.getElementById("AccountCentralTitle").setAttribute("value", title);
    }
    catch(ex) {
        Components.utils.reportError("Error getting selected account: " + ex + "\n");
    }
}

// Show items in the AccountCentral page depending on the capabilities
// of the given account
function ArrangeAccountCentralItems(server, msgFolder)
{
    let exceptions = [];
    let protocolInfo = null;
    try {
      protocolInfo = server.protocolInfo;
    } catch (e) {
      exceptions.push(e);
    }

    // Is this a RSS account?
    let displayRssHeader = server && server.type == 'rss';

    /***** Email header and items : Begin *****/

    // Read Messages
    let canGetMessages = false;
    try {
        canGetMessages = protocolInfo && protocolInfo.canGetMessages;
        SetItemDisplay("ReadMessages", canGetMessages && !displayRssHeader);
    } catch (e) { exceptions.push(e); }

    // Compose Messages link
    let showComposeMsgLink = false;
    try {
        showComposeMsgLink = protocolInfo && protocolInfo.showComposeMsgLink;
        SetItemDisplay("ComposeMessage", showComposeMsgLink);
    } catch (e) { exceptions.push(e); }

    // Junk mail settings (false, until ready for prime time)
    let canControlJunkEmail = false
    try {
        canControlJunkEmail = false && protocolInfo &&
                              protocolInfo.canGetIncomingMessages &&
                              protocolInfo.canGetMessages;
        SetItemDisplay("JunkSettingsMail", canControlJunkEmail);
    } catch (e) { exceptions.push(e); }

    // Display Email header, only if any of the items are displayed
    let displayEmailHeader = !displayRssHeader &&
                             (canGetMessages || showComposeMsgLink ||
                              canControlJunkEmail);
    SetItemDisplay("EmailHeader", displayEmailHeader);

    /***** Email header and items : End *****/

    /***** News header and items : Begin *****/

    // Subscribe to Newsgroups
    let canSubscribe = false;
    try {
        canSubscribe = msgFolder && msgFolder.canSubscribe &&
                       protocolInfo && !protocolInfo.canGetMessages;
        SetItemDisplay("SubscribeNewsgroups", canSubscribe);
    } catch (e) { exceptions.push(e); }

    // Junk news settings (false, until ready for prime time)
    let canControlJunkNews = false;
    try {
        canControlJunkNews = false && protocolInfo &&
                             protocolInfo.canGetIncomingMessages &&
                             !protocolInfo.canGetMessages;
        SetItemDisplay("JunkSettingsNews", canControlJunkNews);
    } catch (e) { exceptions.push(e); }

    // Display News header, only if any of the items are displayed
    let displayNewsHeader = canSubscribe || canControlJunkNews;
    SetItemDisplay("NewsHeader", displayNewsHeader);

    /***** News header and items : End *****/

    /***** RSS header and items : Begin *****/

    // Display RSS header, only if this is RSS account
    SetItemDisplay("rssHeader", displayRssHeader);

    // Subscribe to RSS Feeds
    SetItemDisplay("SubscribeRSS", displayRssHeader);

    /***** RSS header and items : End *****/

    // If either of above sections exists, show section separators
    SetItemDisplay("MessagesSection",
                   displayNewsHeader || displayEmailHeader || displayRssHeader);

    /***** Accounts : Begin *****/

    // Account Settings if a server is found
    let canShowAccountSettings = server != null;
    SetItemDisplay("AccountSettings", canShowAccountSettings);

    // Show New Mail Account Wizard if not prohibited by pref
    let canShowCreateAccount = false;
    try {
        canShowCreateAccount = !Services.prefs
          .prefIsLocked("mail.disable_new_account_addition");
        SetItemDisplay("CreateAccount", canShowCreateAccount);
        SetItemDisplay("CreateAccounts", canShowCreateAccount);
    } catch (e) { exceptions.push(e); }

    // Display Accounts header, only if any of the items are displayed
    let displayAccountsHeader = canShowAccountSettings || canShowCreateAccount;
    SetItemDisplay("AccountsHeader", canShowCreateAccount);

    /***** Accounts : End *****/

    /***** Advanced Features header and items : Begin *****/

    // Search Messages
    let canSearchMessages = false;
    try {
        canSearchMessages = server && server.canSearchMessages;
        SetItemDisplay("SearchMessages", canSearchMessages);
    } catch (e) { exceptions.push(e); }

    // Create Filters
    let canHaveFilters = false;
    try {
        canHaveFilters = server && server.canHaveFilters;
        SetItemDisplay("CreateFilters", canHaveFilters);
    } catch (e) { exceptions.push(e); }

    // Subscribe to IMAP Folders
    let canSubscribeImapFolders = false;
    try {
        canSubscribeImapFolders = msgFolder && msgFolder.canSubscribe &&
                                  protocolInfo && protocolInfo.canGetMessages;
        SetItemDisplay("SubscribeImapFolders", canSubscribeImapFolders);
    } catch (e) { exceptions.push(e); }

    // Offline Settings
    let supportsOffline = false;
    try {
        supportsOffline = server && server.offlineSupportLevel != 0;
        SetItemDisplay("OfflineSettings", supportsOffline);
    } catch (e) { exceptions.push(e); }

    // Display Adv Features header, only if any of the items are displayed
    let displayAdvFeatures = canSearchMessages || canHaveFilters ||
                             canSubscribeImapFolders|| supportsOffline;
    SetItemDisplay("AdvancedFeaturesHeader", displayAdvFeatures);

    /***** Advanced Featuers header and items : End *****/

    // If either of above features exist, show section separators
    SetItemDisplay("AccountsSection", displayAdvFeatures);

    while (exceptions.length) {
        Components.utils.reportError("Error in setting AccountCentral Items: "
            + exceptions.pop() + "\n");
    }
}

// Show the item if the item feature is supported
function SetItemDisplay(elemId, displayThisItem)
{
    if (displayThisItem) {
        let elem = document.getElementById(elemId);
        if (elem)
            elem.setAttribute("collapsed", false);

        let elemSpacer = document.getElementById(elemId + ".spacer");
        if (elemSpacer)
            elemSpacer.setAttribute("collapsed", false);
    }
}

// From the current folder tree, return the selected server or null
function GetSelectedServer()
{
    let currentFolder = GetSelectedMsgFolder();
    return currentFolder ? currentFolder.server : null;
}

// From the current folder tree, return the selected folder,
// the root folder of default account or null
function GetSelectedMsgFolder()
{
    return window.parent.GetSelectedMsgFolders()[0] ||
           window.parent.GetDefaultAccountRootFolder();
}

/**
 * Open Inbox for selected server.
 * If needed, open the twisty and select Inbox.
 */
function ReadMessages()
{
    if (!selectedServer)
        return;
    try {
        window.parent.OpenInboxForServer(selectedServer);
    }
    catch(ex) {
        Components.utils
                  .reportError("Error opening Inbox for server: " + ex + "\n");
    }
}

// Trigger composer for a new message
function ComposeAMessage(event)
{
    // Pass event to allow holding Shift key for toggling HTML vs. plaintext format
    window.parent.MsgNewMessage(event);
}

/**
 * Open AccountManager to view settings for a given account
 * @param selectPage  the xul file name for the viewing page,
 *                    null for the account main page, other pages are
 *                    'am-server.xul', 'am-copies.xul', 'am-offline.xul',
 *                    'am-addressing.xul', 'am-smtp.xul'
 */
function ViewSettings(selectPage)
{
    window.parent.MsgAccountManager(selectPage);
}

// Open AccountWizard to create an account
function CreateNewAccount()
{
    window.parent.msgOpenAccountWizard();
}

function CreateNewAccountTB(type)
{
  if (type == "mail") {
    if (Services.prefs.getBoolPref("mail.provider.enabled"))
        NewMailAccountProvisioner(null);
    else
        AddMailAccount();
    return;
  }

  if (type == "feeds") {
    AddFeedAccount();
    return;
  }

  window.parent.msgOpenAccountWizard(
      function(state) {
        let win = getMostRecentMailWindow();
        if (state && win && win.gFolderTreeView && this.gCurrentAccount) {
          win.gFolderTreeView.selectFolder(
              this.gCurrentAccount.incomingServer.rootMsgFolder);
        }
      },
      type);
}

// Bring up search interface for selected account
function SearchMessages()
{
    window.parent.MsgSearchMessages();
}

// Open filters window
function CreateMsgFilters()
{
    window.parent.MsgFilters(null, null);
}

// Open Subscribe dialog
function Subscribe()
{
    if (!selectedServer)
        return;
    if (selectedServer.type == 'rss')
        window.parent.openSubscriptionsDialog(selectedServer.rootFolder);
    else
        window.parent.MsgSubscribe();
}

// Open junk mail settings dialog
function JunkSettings()
{
    // TODO: function does not exist yet, will throw an exception if exposed
    window.parent.MsgJunkMail();
}