mainwin.js
Go to the documentation of this file.
1 // vim: fileencoding=utf-8 shiftwidth=2
2 
3 var systray = {
5  function systray_onHideButtonClick() {
6  var trayIcon = document.getElementById("trayicon-mainwin");
7  trayIcon.minimizeWindow();
8  },
9 
10  restore:
11  function systray_restore() {
12  var trayIcon = document.getElementById("trayicon-mainwin");
13  trayIcon.restoreWindow();
14  window.focus();
15  },
16 
17  observe:
18  function systray_observe(aSubject, aTopic, aData) {
19  if (aSubject instanceof Components.interfaces.nsIPrefBranch) {
20  return systray.observePref(aSubject, aTopic, aData);
21  }
22  return systray.observeRemote(aSubject, aTopic, aData);
23  },
24 
26  function systray_observePref(aSubject, aTopic, aData) {
27  switch(aData) {
28  case "sb.notify":
29  this._doAlertNotification =
30  (aSubject.PREF_BOOL == aSubject.getPrefType(aData) &&
31  aSubject.getBoolPref(aData));
32  break;
33  }
34  },
35 
36  _doAlertNotification: false,
37  _dataremote_timer: null,
38  observeRemote:
39  function systray_observeRemote(aSubject, aTopic, aData) {
40  // songbird likes to send *lots* of notifications at once
41  // so we do a workaround by delaying the alert by half a second, and abort
42  // if a new one shows up within that time
43  function showAlert(aSystray) {
44  const alsertsSvc =
45  Components.classes["@mozilla.org/alerts-service;1"]
46  .getService(Components.interfaces.nsIAlertsService);
47  var title = SBDataGetStringValue("metadata.title");
48  var artist = SBDataGetStringValue("metadata.artist");
49  var album = SBDataGetStringValue("metadata.album");
50  var position = SBDataGetStringValue("metadata.position.str");
51  var length = SBDataGetStringValue("metadata.length.str");
52  if (title + artist + album + position + length == "") {
53  // bogus alert, nothing to show
54  return;
55  }
56  trayIcon.title = title + '\n' + artist + '\n' + album + '\n' + position + '/' + length;
57  if (aSystray._doAlertNotification) {
58  alsertsSvc.showAlertNotification("chrome://branding/content/nightingale-64.png",
59  title, artist + " : " + album + " - " + position + '/' + length,
60  false, "", null);
61  }
62  }
63  clearTimeout(systray._dataremote_timer);
64  systray._dataremote_timer = setTimeout(showAlert, 500, this);
65  var trayIcon = document.getElementById("trayicon-mainwin");
66  trayIcon.title = SBDataGetStringValue("metadata.title") +
67  '\n' +
68  SBDataGetStringValue("metadata.artist") +
69  '\n' +
70  SBDataGetStringValue("metadata.album") +
71  '\n' +
72  SBDataGetStringValue("metadata.position.str") +
73  '/' +
74  SBDataGetStringValue("metadata.length.str");
75  },
76 
77  _urlRemote: null,
78  _titleRemote: null,
79  _artistRemote: null,
80  _positionRemote: null,
81  _lengthRemote: null,
82 
84  function systray_attachRemotes() {
85  // get notified about track changes
86  // (watch url primarily, but also title for streams)
87  systray._urlRemote = SB_NewDataRemote("metadata.url", null);
88  systray._urlRemote.bindObserver(systray, true);
89  systray._titleRemote = SB_NewDataRemote("metadata.title", null);
90  systray._titleRemote.bindObserver(systray, true);
91  systray._artistRemote = SB_NewDataRemote("metadata.artist", null);
92  systray._artistRemote.bindObserver(systray, true);
93  systray._positionRemote = SB_NewDataRemote("metadata.position.str", null);
94  systray._positionRemote.bindObserver(systray, true);
95  systray._lengthRemote = SB_NewDataRemote("metadata.length.str", null);
96  systray._lengthRemote.bindObserver(systray, true);
97  },
98 
99  detachRemotes:
100  function systray_detachRemotes() {
101  removeEventListener("unload", arguments.callee, false);
102  systray._urlRemote.unbind();
103  systray._titleRemote.unbind();
104  systray._artistRemote.unbind();
105  systray._positionRemote.unbind();
106  systray._lengthRemote.unbind();
107  },
108 
109  destroy:
110  function systray_destroy() {
111  systray._prefBranch.removeObserver("sb.", systray);
112  systray._prefBranch = null;
113  systray.detachRemotes();
114  }
115 };
116 
117 (function() {
118  if ("undefined" != typeof(onHideButtonClick)) {
119  // WTF, there already is a hidden button handler.
120  // we're probably overlaying the wrong window or something
121  Components.utils.reportError("systray: already have " + onHideButtonClick);
122  return;
123  }
124  // yes, it's silly how much work it is to get the "hide" button
125  const NS_XUL =
126  "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
127  var titlebar = document.getElementsByTagName("sb-sys-titlebar")[0];
128  if (!titlebar) {
129  Components.utils.reportError("systray: cannot find the title bar");
130  return;
131  }
132  var sysButtons = document.getAnonymousNodes(titlebar)[0]
133  .getElementsByTagNameNS(NS_XUL,"sb-sys-buttons")[0];
134  if (!sysButtons) {
135  Components.utils.reportError("systray: cannot find the system buttons");
136  return;
137  }
139  hideButton = document.getAnonymousElementByAttribute(sysButtons, "sbid", "hide");
140  if (!hideButton) {
141  Components.utils.reportError("systray: cannot find hide button");
142  return;
143  }
144 
145  window.onHideButtonClick = systray.onHideButtonClick;
146  hideButton.setAttribute("hidden", "false");
147 
148  addEventListener("unload", systray.destroy, false);
149 
150  systray._prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
151  .getService(Components.interfaces.nsIPrefService)
152  .getBranch("extensions.minimizetotray.")
153  .QueryInterface(Components.interfaces.nsIPrefBranch2);
154  systray._prefBranch.addObserver("sb.", systray, false);
155 
156  systray.observePref(systray._prefBranch, "nsPref:changed", "sb.notify");
157 
158  systray.attachRemotes();
159 })();
systray attachRemotes()
systray observePref(systray._prefBranch,"nsPref:changed","sb.notify")
addEventListener("unload", systray.destroy, false)
var hideButton
Definition: mainwin.js:138
var sysButtons
Definition: mainwin.js:132
var titlebar
Definition: mainwin.js:127
function SBDataGetStringValue(aKey)
Get the value of the data in string format.
let window
const SB_NewDataRemote
window onHideButtonClick
Definition: mainwin.js:145
var getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow('Songbird SBProperties artist
Definition: tuner2.js:40
aWindow setTimeout(function(){_this.restoreHistory(aWindow, aTabs, aTabData, aIdMap);}, 0)
return null
Definition: FeedWriter.js:1143
return!aWindow arguments!aWindow arguments[0]
var systray
Definition: mainwin.js:3
this removeEventListener("load", this.__SS_restore, true)
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe