]> git.decadent.org.uk Git - emusic-remote.git/blob - components/eCmdLineHandler.js
Renamed launcher script to 'emusicremote' to match upstream binaries.
[emusic-remote.git] / components / eCmdLineHandler.js
1 //http://developer.mozilla.org/en/docs/Chrome:_Command_Line
2
3 //const nsIAppShellService    = Components.interfaces.nsIAppShellService;
4 const nsISupports           = Components.interfaces.nsISupports;
5 const nsICategoryManager    = Components.interfaces.nsICategoryManager;
6 const nsIComponentRegistrar = Components.interfaces.nsIComponentRegistrar;
7 const nsICommandLine        = Components.interfaces.nsICommandLine;
8 const nsICommandLineHandler = Components.interfaces.nsICommandLineHandler;
9 const nsIFactory            = Components.interfaces.nsIFactory;
10 const nsIModule             = Components.interfaces.nsIModule;
11 //const nsIWindowWatcher      = Components.interfaces.nsIWindowWatcher;
12
13 /**
14  * The XPCOM component that implements nsICommandLineHandler.
15  * It also implements nsIFactory to serve as its own singleton factory.
16  */
17 const myEMHandler = {
18
19   /* nsISupports */
20   QueryInterface : function clh_QI(iid) {
21     if (iid.equals(nsICommandLineHandler) ||
22         iid.equals(nsIFactory) ||
23         iid.equals(nsISupports))
24       return this;
25
26     throw Components.results.NS_ERROR_NO_INTERFACE;
27   },
28
29   /* nsICommandLineHandler */
30
31   handle : function clh_handle(cmdLine) {
32
33     try {
34       var currentArg, prefs
35       var argCount = cmdLine.length;
36
37 //      Components.utils.reportError(cmdLine.state);
38       for (var i = 0; i < argCount; ++i) {
39         currentArg = cmdLine.getArgument(i);
40
41         /*var mConsoleService = Components.classes["@mozilla.org/consoleservice;1"]
42                .getService(Components.interfaces.nsIConsoleService)
43         mConsoleService.logStringMessage("eMusic_protocol: " + currentArg + "\n");*/
44
45         if (currentArg == "emusic://quit/") {
46           var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1'].
47             getService(Components.interfaces.nsIAppStartup);
48         
49           var quitSeverity = false ? Components.interfaces.nsIAppStartup.eForceQuit :
50                                           Components.interfaces.nsIAppStartup.eAttemptQuit;
51           appStartup.quit(quitSeverity);
52           return false;
53         }
54         //Components.utils.reportError("eMusic: " + currentArg + " - " + i);
55         if (currentArg && (/\.emp$/i.test(currentArg) || /\.emx$/i.test(currentArg))) {
56 //          Components.utils.reportError(currentArg);
57           currentArg = cmdLine.resolveFile(currentArg).path
58 //          Components.utils.reportError(currentArg);
59
60           prefs = Components.classes["@mozilla.org/preferences-service;1"]
61                     .getService(Components.interfaces.nsIPrefService)
62                     .getBranch("eMusic.");
63
64           prefs.setCharPref("cmdLineValue", currentArg);
65
66         }
67       }
68     } catch (e) {
69       Components.utils.reportError(e);
70       return true;
71     }
72     return true;
73   },
74
75   helpInfo : "  just the emp path please\n",
76
77   /* nsIFactory */
78
79   createInstance : function clh_CI(outer, iid) {
80     if (outer != null)
81       throw Components.results.NS_ERROR_NO_AGGREGATION;
82
83     return this.QueryInterface(iid);
84   },
85
86   lockFactory : function clh_lock(lock) {
87     /* no-op */
88   }
89 };
90
91 // CHANGEME: change the contract id, CID, and category to be unique
92 // to your application.
93 //const clh_contractID = "@mozilla.org/commandlinehandler/general-startup;1?type=emhandler";
94 const clh_contractID = "@mozilla.org/toolkit/default-clh;1?type=emhandler";
95 const clh_CID = Components.ID("{EA8CD2F1-920F-48b1-8C5E-84BB23139C73}");
96
97 // category names are sorted alphabetically. Typical command-line handlers use a
98 // category that begins with the letter "m".
99 const clh_category = "m-emhandler";
100
101 /**
102  * The XPCOM glue that implements nsIModule
103  */
104 const myEMHandlerModule = {
105   /* nsISupports */
106   QueryInterface : function mod_QI(iid) {
107     if (iid.equals(nsIModule) ||
108         iid.equals(nsISupports))
109       return this;
110
111     throw Components.results.NS_ERROR_NO_INTERFACE;
112   },
113
114   /* nsIModule */
115   getClassObject : function mod_gch(compMgr, cid, iid) {
116     if (cid.equals(clh_CID))
117       return myEMHandler.QueryInterface(iid);
118
119     throw Components.results.NS_ERROR_NOT_REGISTERED;
120   },
121
122   registerSelf : function mod_regself(compMgr, fileSpec, location, type) {
123     var compReg = compMgr.QueryInterface(nsIComponentRegistrar);
124     compReg.registerFactoryLocation(clh_CID,
125                                     "myEMHandler",
126                                     clh_contractID,
127                                     fileSpec,
128                                     location,
129                                     type);
130
131     var catMan = Components.classes["@mozilla.org/categorymanager;1"].
132       getService(nsICategoryManager);
133     catMan.addCategoryEntry("command-line-handler",
134                             clh_category,
135                             clh_contractID, true, true);
136   },
137
138   unregisterSelf : function mod_unreg(compMgr, location, type) {
139     var compReg = compMgr.QueryInterface(nsIComponentRegistrar);
140     compReg.unregisterFactoryLocation(clh_CID, location);
141
142     var catMan = Components.classes["@mozilla.org/categorymanager;1"].
143       getService(nsICategoryManager);
144     catMan.deleteCategoryEntry("command-line-handler", clh_category);
145   },
146
147   canUnload : function (compMgr) {
148     return true;
149   }
150 };
151
152 /* The NSGetModule function is the magic entry point that XPCOM uses to find what XPCOM objects
153  * this component provides
154  */
155 function NSGetModule(comMgr, fileSpec) {
156   return myEMHandlerModule;
157 }