]> git.donarmstrong.com Git - dactyl.git/blob - melodactyl/content/config.js
Import 1.0b7.1 supporting Firefox up to 8.*
[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     hasTabbrowser: true,
186
187     removeTab: function (tab) {
188         if (config.tabbrowser.mTabs.length > 1)
189             config.tabbrowser.removeTab(tab);
190         else {
191             if (buffer.URL != "about:blank" || window.getWebNavigation().sessionHistory.count > 0) {
192                 dactyl.open("about:blank", dactyl.NEW_BACKGROUND_TAB);
193                 config.tabbrowser.removeTab(tab);
194             }
195             else
196                 dactyl.beep();
197         }
198     },
199
200     scripts: [
201         "browser",
202         "bookmarks",
203         "history",
204         "quickmarks",
205         "tabs",
206         "player",
207         "library"
208     ],
209
210     sidebars: {
211         viewAddons:      ["Add-ons",     "A", "chrome://mozapps/content/extensions/extensions.xul"],
212         viewConsole:     ["Console",     "C", "chrome://global/content/console.xul"],
213         viewDownloads:   ["Downloads",   "D", "chrome://mozapps/content/downloads/downloads.xul"],
214         viewPreferences: ["Preferences", "P", "about:config"]
215     },
216
217     // FIXME: tab arg and media tab exception?
218     stop: function (tab) {
219         window.SBGetBrowser().mCurrentBrowser.stop();
220     }
221 }, {
222     /**
223      * @property {object} A map of display pane command argument strings to
224      *     panel element IDs.
225      */
226     displayPanes: {
227         "leftservice"  : "servicepane",
228         "bottomcontent": "displaypane_contentpane_bottom",
229         "bottomservice": "displaypane_servicepane_bottom",
230         "rightsidebar" : "displaypane_right_sidebar"
231     }
232 }, {
233     commands: function initCommands(dactyl, modules, window) {
234         const { commands, completion, options } = modules;
235
236         commands.add(["dpcl[ose]"],
237             "Close a display pane",
238             function (args) {
239                 let arg = args.literalArg;
240                 dactyl.assert(arg in Config.displayPanes, _("error.invalidArgument", arg));
241                 config.closeDisplayPane(Config.displayPanes[arg]);
242             },
243             {
244                 argCount: "1",
245                 completer: function (context) completion.displayPane(context),
246                 literal: 0
247             });
248
249         // TODO: this should accept a second arg to specify content
250         commands.add(["displayp[ane]", "dp[ane]", "dpope[n]"],
251             "Open a display pane",
252             function (args) {
253                 let arg = args.literalArg;
254                 dactyl.assert(arg in Config.displayPanes, _("error.invalidArgument", arg));
255                 // TODO: focus when we have better key handling of these extended modes
256                 config.openDisplayPane(Config.displayPanes[arg]);
257             },
258             {
259                 argCount: "1",
260                 completer: function (context) completion.displayPane(context),
261                 literal: 0
262             });
263
264         commands.add(["pref[erences]", "prefs"],
265             "Show " + config.host + " preferences",
266             function (args) {
267                 if (args.bang) { // open Songbird settings GUI dialog
268                     dactyl.open("about:config",
269                         (options["newtab"] && options.get("newtab").has("all", "prefs"))
270                                 ? dactyl.NEW_TAB : dactyl.CURRENT_TAB);
271                 }
272                 else
273                     window.openPreferences();
274             },
275             {
276                 argCount: "0",
277                 bang: true
278             });
279     },
280     completion: function initCompletion(dactyl, modules, window) {
281         const completion = require("completion");
282
283         completion.displayPane = function (context) {
284             context.title = ["Display Pane"];
285             context.completions = Config.displayPanes; // FIXME: useful description etc
286         };
287     },
288     modes: function initModes(dactyl, modules, window) {
289         const { modes } = modules;
290
291         this.ignoreKeys = {
292             "<Return>": modes.NORMAL | modes.INSERT,
293             "<Space>": modes.NORMAL | modes.INSERT,
294             "<Up>": modes.NORMAL | modes.INSERT,
295             "<Down>": modes.NORMAL | modes.INSERT
296         };
297
298         modes.addMode("PLAYER", {
299             char: "p"
300         });
301     },
302     options: function initOptions(dactyl, modules, window) {
303         const { options } = modules;
304
305         // TODO: SB doesn't explicitly support an offline mode. Should we? --djk
306         options.add(["online"],
307             "Set the 'work offline' option",
308             "boolean", true,
309             {
310                 setter: function (value) {
311                     const ioService = services.io;
312                     ioService.offline = !value;
313                     prefs.set("browser.offline", ioService.offline);
314                     return value;
315                 },
316                 getter: function () !services.io.offline
317             });
318     },
319     services: function initServices(dactyl, modules, window) {
320         services.add("displayPaneManager",      "@songbirdnest.com/Songbird/DisplayPane/Manager;1",         Ci.sbIDisplayPaneManager);
321         services.add("mediaPageManager",        "@songbirdnest.com/Songbird/MediaPageManager;1",            Ci.sbIMediaPageManager);
322         services.add("propertyManager",         "@songbirdnest.com/Songbird/Properties/PropertyManager;1",  Ci.sbIPropertyManager);
323
324         services.addClass("mutablePropertyArray", "@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1",
325                           Ci.sbIMutablePropertyArray);
326     }
327 });
328
329 // vim: set fdm=marker sw=4 ts=4 et: