]> git.donarmstrong.com Git - dactyl.git/blob - melodactyl/content/config.js
Import 1.0rc1 supporting Firefox up to 11.*
[dactyl.git] / melodactyl / content / config.js
1 // Copyright (c) 2009 by Martin Stubenschrott <stubenschrott@vimperator.org>
2 // Copyright (c) 2009 by Prathyush Thota <prathyushthota@gmail.com>
3 // Copyright (c) 2009-2011 by Doug Kearns <dougkearns@gmail.com>
4 // Copyright (c) 2009-2011 by Kris Maglione <maglione.k@gmail.com>
5 //
6 // This work is licensed for reuse under an MIT license. Details are
7 // given in the LICENSE.txt file included with this file.
8 "use strict";
9
10 Components.utils.import("resource://gre/modules/utils.js"); // XXX: PlacesUtils
11
12 const Config = Module("config", ConfigBase, {
13     name: "melodactyl",
14     appName: "Melodactyl",
15     idName: "MELODACTYL",
16     host: "Songbird",
17     hostbin: "songbird",
18
19     commandContainer: "mainplayer",
20
21     Local: function Local(dactyl, modules, window) let ({ config } = modules, { document } = window) {
22         init: function init() {
23             init.supercall(this);
24
25             // TODO: mention this to SB devs, they seem keen to provide these
26             // functions to make porting from FF as simple as possible.
27             window.toJavaScriptConsole = function () {
28                 toOpenWindowByType("global:console", "chrome://global/content/console.xul");
29             };
30         },
31
32         // FIXME: unless I'm seeing double in in the wee small hours gBrowser is
33         // first set from getBrowser which they've deprecated in FF.
34         get browser() window.getBrowser(),
35         get tabbrowser() window.getBrowser(),
36
37         dialogs: {
38             about: ["About Songbird",
39                 function () { window.openDialog("chrome://songbird/content/xul/about.xul", "_blank", "chrome,dialog,modal,centerscreen"); }],
40             addons: ["Manage Add-ons",
41                 function () { window.SBOpenPreferences("paneAddons"); }],
42             checkupdates: ["Check for updates",
43                 function () { window.checkForUpdates(); }],
44             cookies: ["List your cookies",
45                 function () { window.toOpenWindowByType("Browser:Cookies", "chrome://browser/content/preferences/cookies.xul", "chrome,dialog=no,resizable"); }],
46             console: ["JavaScript console",
47                 function () { window.toJavaScriptConsole(); }],
48             dominspector: ["DOM Inspector",
49                 function () { window.inspectDOMDocument(window.content.document); },
50                 function () "inspectDOMDocument" in window],
51             downloads: ["Manage Downloads",
52                 function () { window.toOpenWindowByType("Download:Manager", "chrome://mozapps/content/downloads/downloads.xul", "chrome,dialog=no,resizable"); }],
53             newsmartplaylist: ["Open the file selector dialog",
54                 function () { window.SBNewSmartPlaylist(); }],
55             openfile: ["Open the file selector dialog",
56                 function () { window.SBFileOpen(); }],
57             pagesource: ["View page source",
58                 function () { window.BrowserViewSourceOfDocument(window.content.document); }],
59             preferences: ["Show Songbird preferences dialog",
60                 function () { window.openPreferences(); }],
61             printsetup: ["Setup the page size and orientation before printing",
62                 function () { window.PrintUtils.showPageSetup(); }],
63             print: ["Show print dialog",
64                 function () { window.PrintUtils.print(); }],
65             saveframe: ["Save frame to disk",
66                 function () { window.saveFrameDocument(); }],
67             savepage: ["Save page to disk",
68                 function () { window.saveDocument(window.content.document); }],
69             searchengines: ["Manage installed search engines",
70                 function () { window.openDialog("chrome://browser/content/search/engineManager.xul", "_blank", "chrome,dialog,modal,centerscreen"); }],
71             selectionsource: ["View selection source",
72                 function () { modules.buffer.viewSelectionSource(); }],
73             subscribe: ["Add a new subscription",
74                 function () { window.SBSubscribe(); }]
75         },
76
77         // TODO: clean this up
78         focusChange: function (win) {
79             const { modes } = modules;
80
81             // Switch to -- PLAYER -- mode for Songbird Media Player.
82             if (this.isPlayerWindow)
83                 modes.set(modes.PLAYER);
84             else
85                 if (modes.main == modes.PLAYER)
86                     modes.pop();
87         },
88
89         get isPlayerWindow() window.SBGetBrowser().mCurrentTab == window.SBGetBrowser().mediaTab,
90
91         /**
92          * Shows or hides the main service pane.
93          *
94          * @param {boolean} value Show the service pane if true, hide it if false.
95          */
96         showServicePane: function (value) {
97             const key = "splitter.servicepane_splitter.was_collapsed";
98             window.gServicePane.open = value;
99             window.SBDataSetBoolValue(key, window.gServicePane.open);
100         },
101
102         /**
103          * Opens the display panel with the specified *id*.
104          *
105          * @param {string} id The ID of the display pane.
106          */
107         openDisplayPane: function (id) {
108             if (id == "servicepane")
109                 this.showServicePane(true);
110             else {
111                 let pane = document.getElementById(id);
112                 let manager = services.displayPaneManager;
113                 let paneinfo = manager.getPaneInfo(pane._lastURL.stringValue);
114
115                 if (!paneinfo)
116                     paneinfo = manager.defaultPaneInfo;
117
118                 pane.loadContent(paneinfo);
119             }
120         },
121
122         /**
123          * Closes the display panel with the specified *id*
124          *
125          * @param {string} id The ID of the display pane.
126          */
127         closeDisplayPane: function (id) {
128             if (id == "servicepane")
129                 this.showServicePane(false);
130             else
131                 document.getElementById(id).hide();
132         }
133     },
134
135     /*** optional options, there are checked for existence and a fallback provided  ***/
136     features: Set(["bookmarks", "hints", "marks", "history", "quickmarks", "session", "tabs", "player"]),
137
138     defaults: {
139         guioptions: "bCmprs",
140         showtabline: 2,
141         get titlestring() config.name
142     },
143
144     guioptions: {
145         m: ["Menubar",         ["main-menubar"]],
146         T: ["Toolbar",         ["nav-bar"]],
147         p: ["Player controls", ["player_wrapper"]]
148     },
149
150     overlayChrome: ["chrome://purplerain/content/xul/mainplayer.xul"],
151
152     styleableChrome: ["chrome://purplerain/content/xul/mainplayer.xul"],
153
154     autocommands: {
155         BookmarkAdd: "Triggered after a page is bookmarked",
156         ColorScheme: "Triggered after a color scheme has been loaded",
157         DOMLoad: "Triggered when a page's DOM content has fully loaded",
158         DownloadPost: "Triggered when a download has completed",
159         Fullscreen: "Triggered when the browser's fullscreen state changes",
160         LocationChange: "Triggered when changing tabs or when navigation to a new location",
161         PageLoadPre: "Triggered after a page load is initiated",
162         PageLoad: "Triggered when a page gets (re)loaded/opened",
163         ShellCmdPost: "Triggered after executing a shell command with :!cmd",
164         TrackChangePre: "Triggered before a playing track is changed",
165         TrackChange: "Triggered after a playing track has changed",
166         ViewChangePre: "Triggered before a sequencer view is changed",
167         ViewChange: "Triggered after a sequencer view is changed",
168         StreamStart: "Triggered after a stream has started",
169         StreamPause: "Triggered after a stream has paused",
170         StreamEnd: "Triggered after a stream has ended",
171         StreamStop: "Triggered after a stream has stopped",
172         Enter: "Triggered after Songbird starts",
173         LeavePre: "Triggered before exiting Songbird, just before destroying each module",
174         Leave: "Triggered before exiting Songbird"
175     },
176
177     completers: Class.memoize(function () update({
178         displaypane: "displayPane",
179         playlist: "playlist",
180         mediaview: "mediaView",
181         mediasort: "mediaListSort",
182         song: "song"
183     }, this.__proto__.completers)),
184
185     removeTab: function (tab) {
186         if (config.tabbrowser.mTabs.length > 1)
187             config.tabbrowser.removeTab(tab);
188         else {
189             if (buffer.URL != "about:blank" || window.getWebNavigation().sessionHistory.count > 0) {
190                 dactyl.open("about:blank", dactyl.NEW_BACKGROUND_TAB);
191                 config.tabbrowser.removeTab(tab);
192             }
193             else
194                 dactyl.beep();
195         }
196     },
197
198     scripts: [
199         "browser",
200         "bookmarks",
201         "history",
202         "quickmarks",
203         "tabs",
204         "player",
205         "library"
206     ],
207
208     sidebars: {
209         viewAddons:      ["Add-ons",     "A", "chrome://mozapps/content/extensions/extensions.xul"],
210         viewConsole:     ["Console",     "C", "chrome://global/content/console.xul"],
211         viewDownloads:   ["Downloads",   "D", "chrome://mozapps/content/downloads/downloads.xul"],
212         viewPreferences: ["Preferences", "P", "about:config"]
213     },
214
215     // FIXME: tab arg and media tab exception?
216     stop: function (tab) {
217         window.SBGetBrowser().mCurrentBrowser.stop();
218     }
219 }, {
220     /**
221      * @property {object} A map of display pane command argument strings to
222      *     panel element IDs.
223      */
224     displayPanes: {
225         "leftservice"  : "servicepane",
226         "bottomcontent": "displaypane_contentpane_bottom",
227         "bottomservice": "displaypane_servicepane_bottom",
228         "rightsidebar" : "displaypane_right_sidebar"
229     }
230 }, {
231     commands: function initCommands(dactyl, modules, window) {
232         const { commands, completion, options } = modules;
233
234         commands.add(["dpcl[ose]"],
235             "Close a display pane",
236             function (args) {
237                 let arg = args.literalArg;
238                 dactyl.assert(arg in Config.displayPanes, _("error.invalidArgument", arg));
239                 config.closeDisplayPane(Config.displayPanes[arg]);
240             },
241             {
242                 argCount: "1",
243                 completer: function (context) completion.displayPane(context),
244                 literal: 0
245             });
246
247         // TODO: this should accept a second arg to specify content
248         commands.add(["displayp[ane]", "dp[ane]", "dpope[n]"],
249             "Open a display pane",
250             function (args) {
251                 let arg = args.literalArg;
252                 dactyl.assert(arg in Config.displayPanes, _("error.invalidArgument", arg));
253                 // TODO: focus when we have better key handling of these extended modes
254                 config.openDisplayPane(Config.displayPanes[arg]);
255             },
256             {
257                 argCount: "1",
258                 completer: function (context) completion.displayPane(context),
259                 literal: 0
260             });
261
262         commands.add(["pref[erences]", "prefs"],
263             "Show " + config.host + " preferences",
264             function (args) {
265                 if (args.bang) { // open Songbird settings GUI dialog
266                     dactyl.open("about:config",
267                         (options["newtab"] && options.get("newtab").has("all", "prefs"))
268                                 ? dactyl.NEW_TAB : dactyl.CURRENT_TAB);
269                 }
270                 else
271                     window.openPreferences();
272             },
273             {
274                 argCount: "0",
275                 bang: true
276             });
277     },
278     completion: function initCompletion(dactyl, modules, window) {
279         const completion = require("completion");
280
281         completion.displayPane = function (context) {
282             context.title = ["Display Pane"];
283             context.completions = Config.displayPanes; // FIXME: useful description etc
284         };
285     },
286     modes: function initModes(dactyl, modules, window) {
287         const { modes } = modules;
288
289         this.ignoreKeys = {
290             "<Return>": modes.NORMAL | modes.INSERT,
291             "<Space>": modes.NORMAL | modes.INSERT,
292             "<Up>": modes.NORMAL | modes.INSERT,
293             "<Down>": modes.NORMAL | modes.INSERT
294         };
295
296         modes.addMode("PLAYER", {
297             char: "p"
298         });
299     },
300     options: function initOptions(dactyl, modules, window) {
301         const { options } = modules;
302
303         // TODO: SB doesn't explicitly support an offline mode. Should we? --djk
304         options.add(["online"],
305             "Set the 'work offline' option",
306             "boolean", true,
307             {
308                 setter: function (value) {
309                     const ioService = services.io;
310                     ioService.offline = !value;
311                     prefs.set("browser.offline", ioService.offline);
312                     return value;
313                 },
314                 getter: function () !services.io.offline
315             });
316     },
317     services: function initServices(dactyl, modules, window) {
318         services.add("displayPaneManager",      "@songbirdnest.com/Songbird/DisplayPane/Manager;1",         Ci.sbIDisplayPaneManager);
319         services.add("mediaPageManager",        "@songbirdnest.com/Songbird/MediaPageManager;1",            Ci.sbIMediaPageManager);
320         services.add("propertyManager",         "@songbirdnest.com/Songbird/Properties/PropertyManager;1",  Ci.sbIPropertyManager);
321
322         services.addClass("mutablePropertyArray", "@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1",
323                           Ci.sbIMutablePropertyArray);
324     }
325 });
326
327 // vim: set fdm=marker sw=4 ts=4 et: