summaryrefslogtreecommitdiffstats
path: root/comm/mail/modules/WindowsJumpLists.jsm
blob: 8bce51a3a38d1cd5b0b9c40731f06c6d4cfa200d (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
/* -*- 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/. */

var EXPORTED_SYMBOLS = ["WinTaskbarJumpList"];

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

// Prefs
var PREF_TASKBAR_BRANCH = "mail.taskbar.lists.";
var PREF_TASKBAR_ENABLED = "enabled";
var PREF_TASKBAR_TASKS = "tasks.enabled";

const lazy = {};

XPCOMUtils.defineLazyGetter(lazy, "_stringBundle", function () {
  return Services.strings.createBundle(
    "chrome://messenger/locale/taskbar.properties"
  );
});

XPCOMUtils.defineLazyServiceGetter(
  lazy,
  "_taskbarService",
  "@mozilla.org/windows-taskbar;1",
  "nsIWinTaskbar"
);

XPCOMUtils.defineLazyGetter(lazy, "_prefs", function () {
  return Services.prefs.getBranch(PREF_TASKBAR_BRANCH);
});

function _getString(aName) {
  return lazy._stringBundle.GetStringFromName(aName);
}

/**
 * Task list
 */
var gTasks = [
  // Write new message
  {
    get title() {
      return _getString("taskbar.tasks.composeMessage.label");
    },
    get description() {
      return _getString("taskbar.tasks.composeMessage.description");
    },
    args: "-compose",
    iconIndex: 2, // Write message icon
  },

  // Open address book
  {
    get title() {
      return _getString("taskbar.tasks.openAddressBook.label");
    },
    get description() {
      return _getString("taskbar.tasks.openAddressBook.description");
    },
    args: "-addressbook",
    iconIndex: 3, // Open address book icon
  },
];

var WinTaskbarJumpList = {
  /**
   * Startup, shutdown, and update
   */

  startup() {
    // exit if this isn't win7 or higher.
    if (!this._initTaskbar()) {
      return;
    }

    // Store our task list config data
    this._tasks = gTasks;

    // retrieve taskbar related prefs.
    this._refreshPrefs();

    // observer for our prefs branch
    this._initObs();

    this.update();
  },

  update() {
    // are we disabled via prefs? don't do anything!
    if (!this._enabled) {
      return;
    }

    // do what we came here to do, update the taskbar jumplist
    this._buildList();
  },

  _shutdown() {
    this._shuttingDown = true;

    this._free();
  },

  /**
   * List building
   */

  _buildList() {
    // anything to build?
    if (!this._showTasks) {
      // don't leave the last list hanging on the taskbar.
      this._deleteActiveJumpList();
      return;
    }

    if (!this._startBuild()) {
      return;
    }

    if (this._showTasks) {
      this._buildTasks();
    }

    this._commitBuild();
  },

  /**
   * Taskbar api wrappers
   */

  _startBuild() {
    // This is useful if there are any async tasks pending. Since we don't right
    // now, it's just harmless.
    this._builder.abortListBuild();
    // Since our list is static right now, we won't actually get back any
    // removed items.
    let removedItems = Cc["@mozilla.org/array;1"].createInstance(
      Ci.nsIMutableArray
    );
    return this._builder.initListBuild(removedItems);
  },

  _commitBuild() {
    this._builder.commitListBuild(succeed => {
      if (!succeed) {
        this._builder.abortListBuild();
      }
    });
  },

  _buildTasks() {
    if (this._tasks.length > 0) {
      var items = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
      for (let item of this._tasks.map(task =>
        this._createHandlerAppItem(task)
      )) {
        items.appendElement(item);
      }
      this._builder.addListToBuild(
        this._builder.JUMPLIST_CATEGORY_TASKS,
        items
      );
    }
  },

  _deleteActiveJumpList() {
    this._builder.deleteActiveList();
  },

  /**
   * Jump list item creation helpers
   */

  _createHandlerAppItem(aTask) {
    let file = Services.dirsvc.get("XCurProcD", Ci.nsIFile);

    // XXX where can we grab this from in the build? Do we need to?
    file.append("thunderbird.exe");

    let handlerApp = Cc[
      "@mozilla.org/uriloader/local-handler-app;1"
    ].createInstance(Ci.nsILocalHandlerApp);
    handlerApp.executable = file;
    // handlers default to the leaf name if a name is not specified
    let title = aTask.title;
    if (title && title.length != 0) {
      handlerApp.name = title;
    }
    handlerApp.detailedDescription = aTask.description;
    handlerApp.appendParameter(aTask.args);

    let item = Cc["@mozilla.org/windows-jumplistshortcut;1"].createInstance(
      Ci.nsIJumpListShortcut
    );
    item.app = handlerApp;
    item.iconIndex = aTask.iconIndex;
    return item;
  },

  _createSeparatorItem() {
    return Cc["@mozilla.org/windows-jumplistseparator;1"].createInstance(
      Ci.nsIJumpListSeparator
    );
  },

  /**
   * Prefs utilities
   */

  _refreshPrefs() {
    this._enabled = lazy._prefs.getBoolPref(PREF_TASKBAR_ENABLED);
    this._showTasks = lazy._prefs.getBoolPref(PREF_TASKBAR_TASKS);
  },

  /**
   * Init and shutdown utilities
   */

  _initTaskbar() {
    this._builder = lazy._taskbarService.createJumpListBuilder(false);
    if (!this._builder || !this._builder.available) {
      return false;
    }

    return true;
  },

  _initObs() {
    Services.obs.addObserver(this, "profile-before-change");
    lazy._prefs.addObserver("", this);
  },

  _freeObs() {
    Services.obs.removeObserver(this, "profile-before-change");
    lazy._prefs.removeObserver("", this);
  },

  observe(aSubject, aTopic, aData) {
    switch (aTopic) {
      case "nsPref:changed":
        if (this._enabled && !lazy._prefs.getBoolPref(PREF_TASKBAR_ENABLED)) {
          this._deleteActiveJumpList();
        }
        this._refreshPrefs();
        this.update();
        break;

      case "profile-before-change":
        this._shutdown();
        break;
    }
  },

  _free() {
    this._freeObs();
    delete this._builder;
  },
};