]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/components/clh.js
9ef48f3cca1a7882eb82c5539183f29af4437eb4
[working/Evergreen.git] / Open-ILS / xul / staff_client / components / clh.js
1 const nsISupports           = Components.interfaces.nsISupports;\r
2 const nsICategoryManager    = Components.interfaces.nsICategoryManager;\r
3 const nsIComponentRegistrar = Components.interfaces.nsIComponentRegistrar;\r
4 const nsICommandLine        = Components.interfaces.nsICommandLine;\r
5 const nsICommandLineHandler = Components.interfaces.nsICommandLineHandler;\r
6 const nsIFactory            = Components.interfaces.nsIFactory;\r
7 const nsIModule             = Components.interfaces.nsIModule;\r
8 const nsIWindowWatcher      = Components.interfaces.nsIWindowWatcher;\r
9 \r
10 \r
11 const XUL_STANDALONE = "chrome://open_ils_staff_client/content/circ/offline.xul";\r
12 const XUL_MAIN = "chrome://open_ils_staff_client/content/main/main.xul";\r
13 const WINDOW_STANDALONE = "eg_offline"\r
14 const WINDOW_MAIN = "eg_main"\r
15 \r
16 const clh_contractID = "@mozilla.org/commandlinehandler/general-startup;1?type=egcli";\r
17 const clh_CID = Components.ID("{7e608198-7355-483a-a85a-20322e4ef91a}");\r
18 // category names are sorted alphabetically. Typical command-line handlers use a\r
19 // category that begins with the letter "m".\r
20 const clh_category = "m-egcli";\r
21 \r
22 /**\r
23  * Utility functions\r
24  */\r
25 \r
26 /**\r
27  * Opens a chrome window.\r
28  * @param aChromeURISpec a string specifying the URI of the window to open.\r
29  * @param aArgument an argument to pass to the window (may be null)\r
30  */\r
31 function findOrOpenWindow(aWindowType, aChromeURISpec, aName, aArgument, aLoginInfo)\r
32 {\r
33   var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].\r
34     getService(Components.interfaces.nsIWindowMediator);\r
35   var targetWindow = wm.getMostRecentWindow(aWindowType);\r
36   if (targetWindow != null) {\r
37       var noFocus = false;\r
38       if(typeof targetWindow.new_tabs == 'function' && aArgument != null) {\r
39           targetWindow.new_tabs(aArgument);\r
40           noFocus = true;\r
41       }\r
42       if(typeof targetWindow.auto_login == 'function' && aLoginInfo != null) {\r
43           targetWindow.auto_login(aLoginInfo);\r
44           noFocus = true;\r
45       }\r
46       if(!noFocus) {\r
47           targetwindow.focus;\r
48       }\r
49   }\r
50   else {\r
51     var params = null;\r
52     if (aArgument != null && aArgument.length != 0 || aLoginInfo != null)\r
53     {\r
54         params = { "openTabs" : aArgument, "loginInfo" : aLoginInfo };\r
55         params.wrappedJSObject = params;\r
56     }\r
57     var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].\r
58         getService(Components.interfaces.nsIWindowWatcher);\r
59     ww.openWindow(null, aChromeURISpec, aName,\r
60         "chrome,resizable,dialog=no", params);\r
61   }\r
62 }\r
63  \r
64 /**\r
65  * The XPCOM component that implements nsICommandLineHandler.\r
66  * It also implements nsIFactory to serve as its own singleton factory.\r
67  */\r
68 const myAppHandler = {\r
69   /* nsISupports */\r
70   QueryInterface : function clh_QI(iid)\r
71   {\r
72     if (iid.equals(nsICommandLineHandler) ||\r
73         iid.equals(nsIFactory) ||\r
74         iid.equals(nsISupports))\r
75       return this;\r
76 \r
77     throw Components.results.NS_ERROR_NO_INTERFACE;\r
78   },\r
79 \r
80   /* nsICommandLineHandler */\r
81 \r
82   handle : function clh_handle(cmdLine)\r
83   {\r
84     // Each of these options is used for opening a new tab, either on login or remote send.\r
85     // XULRunner does some sanitize to turn /ilsblah into -ilsblah, for example.\r
86     // In addition to the ones here, -ilslogin, -ilsoffline, and -ilsstandalone\r
87     // are defined below.\r
88 \r
89     // With the exception of 'new', 'tab', and 'init', the value is checked for in urls in main.js.\r
90 \r
91     // NOTE: The option itself should be all lowercase (we .toLowerCase below)\r
92     var options = {\r
93     '-ilscheckin' : 'XUL_CHECKIN',\r
94     '-ilscheckout' : 'XUL_PATRON_BARCODE_ENTRY',\r
95     '-ilsnew' : 'new', // 'new' is a special keyword for opening a new window\r
96     '-ilstab' : 'tab', // 'tab' is a special keyword for opening a new tab with the default content\r
97     '-ilsnew_default' : 'init', // 'init' is a special keyword for opening a new window with an initial default tab\r
98     };\r
99 \r
100     var inParams = new Array();\r
101     var loginInfo = {};\r
102     var loginInfoProvided = false;\r
103         var position = 0;\r
104         while (position < cmdLine.length) {\r
105                 var arg = cmdLine.getArgument(position).toLowerCase();\r
106                 if (options[arg] != undefined) {\r
107                         inParams.push(options[arg]);\r
108                         cmdLine.removeArguments(position,position);\r
109                         continue;\r
110                 }\r
111         if (arg == '-ilsurl' && cmdLine.length > position) {\r
112                   inParams.push(cmdLine.getArgument(position + 1));\r
113                   cmdLine.removeArguments(position, position + 1);\r
114                   continue;\r
115                 }\r
116         if (arg == '-ilshost' && cmdLine.length > position) {\r
117           loginInfo.host = cmdLine.getArgument(position + 1);\r
118           cmdLine.removeArguments(position, position + 1);\r
119           loginInfoProvided = true;\r
120           continue;\r
121         }\r
122         if (arg == '-ilsuser' && cmdLine.length > position) {\r
123           loginInfo.user = cmdLine.getArgument(position + 1);\r
124           cmdLine.removeArguments(position, position + 1);\r
125           loginInfoProvided = true;\r
126           continue;\r
127         }\r
128         if (arg == '-ilspassword' && cmdLine.length > position) {\r
129           loginInfo.passwd = cmdLine.getArgument(position + 1);\r
130           cmdLine.removeArguments(position, position + 1);\r
131           loginInfoProvided = true;\r
132           continue;\r
133         }\r
134                 position=position + 1;\r
135         }\r
136 \r
137         if (cmdLine.handleFlag("ILSlogin", false) || inParams.length > 0 || loginInfoProvided) {\r
138           findOrOpenWindow(WINDOW_MAIN, XUL_MAIN, '_blank', inParams, loginInfoProvided ? loginInfo : null);\r
139           cmdLine.preventDefault = true;\r
140         }\r
141 \r
142     if (cmdLine.handleFlag("ILSoffline", false) || cmdLine.handleFlag("ILSstandalone", false)) {\r
143           findOrOpenWindow(WINDOW_STANDALONE, XUL_STANDALONE, 'Offline', null, null);\r
144       cmdLine.preventDefault = true;\r
145         }\r
146   },\r
147 \r
148   // CHANGEME: change the help info as appropriate, but\r
149   // follow the guidelines in nsICommandLineHandler.idl\r
150   // specifically, flag descriptions should start at\r
151   // character 24, and lines should be wrapped at\r
152   // 72 characters with embedded newlines,\r
153   // and finally, the string should end with a newline\r
154   helpInfo : "  -ILScheckin          Open an Evergreen checkin tab\n" +\r
155              "  -ILScheckout         Open an Evergreen checkout tab\n" +\r
156              "  -ILSnew              Open a new Evergreen 'menu' window\n" +\r
157              "  -ILSnew_default      Open a new Evergreen 'menu' window,\n" +\r
158              "                       with a 'default' tab\n" +\r
159              "  -ILStab              Open a 'default' tab alone\n" +\r
160              "  -ILSurl <url>        Open the specified url in an Evergreen tab\n" +\r
161              "  -ILShost             Default hostname for login\n" +\r
162              "  -ILSuser             Default username for login\n" +\r
163              "  -ILSpassword         Default password for login\n" +\r
164              "  The above three, if all specified, trigger an automatic login attempt\n" +\r
165              "  The above nine imply -ILSlogin\n" +\r
166              "  -ILSlogin            Open the Evergreen Login window\n" +\r
167              "  -ILSstandalone       Open the Evergreen Standalone interface\n" +\r
168              "  -ILSoffline          Alias for -ILSstandalone\n",\r
169 \r
170   /* nsIFactory */\r
171 \r
172   createInstance : function clh_CI(outer, iid)\r
173   {\r
174     if (outer != null)\r
175       throw Components.results.NS_ERROR_NO_AGGREGATION;\r
176 \r
177     return this.QueryInterface(iid);\r
178   },\r
179 \r
180   lockFactory : function clh_lock(lock)\r
181   {\r
182     /* no-op */\r
183   }\r
184 };\r
185 \r
186 /**\r
187  * The XPCOM glue that implements nsIModule\r
188  */\r
189 const myAppHandlerModule = {\r
190   /* nsISupports */\r
191   QueryInterface : function mod_QI(iid)\r
192   {\r
193     if (iid.equals(nsIModule) ||\r
194         iid.equals(nsISupports))\r
195       return this;\r
196 \r
197     throw Components.results.NS_ERROR_NO_INTERFACE;\r
198   },\r
199 \r
200   /* nsIModule */\r
201   getClassObject : function mod_gch(compMgr, cid, iid)\r
202   {\r
203     if (cid.equals(clh_CID))\r
204       return myAppHandler.QueryInterface(iid);\r
205 \r
206     throw Components.results.NS_ERROR_NOT_REGISTERED;\r
207   },\r
208 \r
209   registerSelf : function mod_regself(compMgr, fileSpec, location, type)\r
210   {\r
211     compMgr.QueryInterface(nsIComponentRegistrar);\r
212 \r
213     compMgr.registerFactoryLocation(clh_CID,\r
214                                     "myAppHandler",\r
215                                     clh_contractID,\r
216                                     fileSpec,\r
217                                     location,\r
218                                     type);\r
219 \r
220     var catMan = Components.classes["@mozilla.org/categorymanager;1"].\r
221       getService(nsICategoryManager);\r
222     catMan.addCategoryEntry("command-line-handler",\r
223                             clh_category,\r
224                             clh_contractID, true, true);\r
225   },\r
226 \r
227   unregisterSelf : function mod_unreg(compMgr, location, type)\r
228   {\r
229     compMgr.QueryInterface(nsIComponentRegistrar);\r
230     compMgr.unregisterFactoryLocation(clh_CID, location);\r
231 \r
232     var catMan = Components.classes["@mozilla.org/categorymanager;1"].\r
233       getService(nsICategoryManager);\r
234     catMan.deleteCategoryEntry("command-line-handler", clh_category);\r
235   },\r
236 \r
237   canUnload : function (compMgr)\r
238   {\r
239     return true;\r
240   }\r
241 };\r
242 \r
243 /* The NSGetModule function is the magic entry point that XPCOM uses to find what XPCOM objects\r
244  * this component provides\r
245  */\r
246 function NSGetModule(comMgr, fileSpec)\r
247 {\r
248   return myAppHandlerModule;\r
249 }\r
250 \r