summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/content/systemIntegrationDialog.js
blob: 41455b3db5ce5ce8f5ec9d770a8163c541740d48 (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
/* -*- Mode: Javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * 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/. */

// This dialog can only be opened if we have a shell service.

var { SearchIntegration } = ChromeUtils.import(
  "resource:///modules/SearchIntegration.jsm"
);

var gSystemIntegrationDialog = {
  _shellSvc: Cc["@mozilla.org/mail/shell-service;1"].getService(
    Ci.nsIShellService
  ),

  _mailCheckbox: null,

  _newsCheckbox: null,

  _rssCheckbox: null,

  _startupCheckbox: null,

  _searchCheckbox: null,

  onLoad() {
    // initialize elements
    this._mailCheckbox = document.getElementById("checkMail");
    this._newsCheckbox = document.getElementById("checkNews");
    this._rssCheckbox = document.getElementById("checkRSS");
    this._calendarCheckbox = document.getElementById("checkCalendar");
    this._startupCheckbox = document.getElementById("checkOnStartup");
    this._searchCheckbox = document.getElementById("searchIntegration");

    // Initialize the check boxes based on the default app states.
    this._mailCheckbox.disabled = this._shellSvc.isDefaultClient(
      false,
      this._shellSvc.MAIL
    );

    let calledFromPrefs =
      "arguments" in window && window.arguments[0] == "calledFromPrefs";

    if (!calledFromPrefs) {
      // As an optimization, if we aren't already the default mail client,
      // then pre-check that option for the user. We'll leave News and RSS alone.
      // Do this only if we are not called from the Preferences (Options) dialog.
      // In that case, the user may want to just check what the current state is.
      this._mailCheckbox.checked = true;
    } else {
      this._mailCheckbox.checked = this._mailCheckbox.disabled;

      // If called from preferences, use only a simpler "Cancel" label on the
      // cancel button.
      document.querySelector("dialog").getButton("cancel").label = document
        .querySelector("dialog")
        .getAttribute("buttonlabelcancel2");
    }

    if (!this._mailCheckbox.disabled) {
      this._mailCheckbox.removeAttribute("tooltiptext");
    }

    this._newsCheckbox.checked = this._newsCheckbox.disabled =
      this._shellSvc.isDefaultClient(false, this._shellSvc.NEWS);
    if (!this._newsCheckbox.disabled) {
      this._newsCheckbox.removeAttribute("tooltiptext");
    }

    this._rssCheckbox.checked = this._rssCheckbox.disabled =
      this._shellSvc.isDefaultClient(false, this._shellSvc.RSS);
    if (!this._rssCheckbox.disabled) {
      this._rssCheckbox.removeAttribute("tooltiptext");
    }

    this._calendarCheckbox.checked = this._calendarCheckbox.disabled =
      this._shellSvc.isDefaultClient(false, this._shellSvc.CALENDAR);

    // read the raw pref value and not shellSvc.shouldCheckDefaultMail
    this._startupCheckbox.checked = Services.prefs.getBoolPref(
      "mail.shell.checkDefaultClient"
    );

    // Search integration - check whether we should show/disable integration options
    if (SearchIntegration) {
      this._searchCheckbox.checked = SearchIntegration.prefEnabled;
      // On Windows, do not offer the option on startup as it does not perform well.
      if (
        Services.appinfo.OS == "WINNT" &&
        !calledFromPrefs &&
        !this._searchCheckbox.checked
      ) {
        this._searchCheckbox.hidden = true;
        // Even if the user wasn't presented the choice,
        // we do not want to ask again automatically.
        SearchIntegration.firstRunDone = true;
      } else if (!SearchIntegration.osVersionTooLow) {
        // Hide/disable the options if the OS does not support them.
        this._searchCheckbox.hidden = false;
        if (SearchIntegration.osComponentsNotRunning) {
          this._searchCheckbox.checked = false;
          this._searchCheckbox.disabled = true;
        }
      }
    }
  },

  /**
   * Called when the dialog is closed by any button.
   *
   * @param aSetAsDefault  If true, set TB as the default application for the
   *                       checked actions (mail/news/rss). Otherwise do nothing.
   */
  onDialogClose(aSetAsDefault) {
    // In all cases, save the user's decision for "always check at startup".
    this._shellSvc.shouldCheckDefaultClient = this._startupCheckbox.checked;

    // If the search checkbox is exposed, the user had the chance to make his choice.
    // So do not ask next time.
    let searchIntegPossible = !this._searchCheckbox.hidden;
    if (searchIntegPossible) {
      SearchIntegration.firstRunDone = true;
    }

    // If the "skip integration" button was used do not set any defaults
    // and close the dialog.
    if (!aSetAsDefault) {
      // Disable search integration in this case.
      if (searchIntegPossible) {
        SearchIntegration.prefEnabled = false;
      }

      return true;
    }

    // For each checked item, if we aren't already the default client,
    // make us the default.
    let appTypes = 0;

    if (
      this._mailCheckbox.checked &&
      !this._shellSvc.isDefaultClient(false, this._shellSvc.MAIL)
    ) {
      appTypes |= this._shellSvc.MAIL;
    }

    if (
      this._newsCheckbox.checked &&
      !this._shellSvc.isDefaultClient(false, this._shellSvc.NEWS)
    ) {
      appTypes |= this._shellSvc.NEWS;
    }

    if (
      this._rssCheckbox.checked &&
      !this._shellSvc.isDefaultClient(false, this._shellSvc.RSS)
    ) {
      appTypes |= this._shellSvc.RSS;
    }

    if (
      this._calendarCheckbox.checked &&
      !this._shellSvc.isDefaultClient(false, this._shellSvc.CALENDAR)
    ) {
      appTypes |= this._shellSvc.CALENDAR;
    }

    if (appTypes) {
      this._shellSvc.setDefaultClient(false, appTypes);
    }

    // Set the search integration pref if it is changed.
    // The integration will handle the rest.
    if (searchIntegPossible) {
      SearchIntegration.prefEnabled = this._searchCheckbox.checked;
    }

    return true;
  },
};

document.addEventListener("dialogaccept", () =>
  gSystemIntegrationDialog.onDialogClose(true)
);
document.addEventListener("dialogcancel", () =>
  gSystemIntegrationDialog.onDialogClose(false)
);