r22324: Another step closer to nice listing of user accounts.
[samba.git] / webapps / swat / source / class / swat / module / netmgr / Gui.js
1 /*
2  * Copyright (C)  Rafal Szczesniak 2007
3  */
4
5 /**
6  * Swat Net Manager class graphical user interface
7  */
8 qx.OO.defineClass("swat.module.netmgr.Gui", qx.core.Target,
9 function()
10 {
11   qx.core.Target.call(this);
12 });
13
14
15 //qx.OO.addProperty({ name : "_tree", type : "object" });
16 //qx.OO.addProperty({ name : "_panel", type : "object" });
17 //qx.OO.addProperty({ name : "_view", type : "object" });
18 //qx.OO.addProperty({ name : "_txtDomain", type : "object" });
19 //qx.OO.addProperty({ name : "_txtUsername", type : "object" });
20
21 /* NetContex resource number assigned on the server side.
22    Necessary for every ejsnet call */
23 qx.OO.addProperty({ name : "netCtx", type : "number" });
24
25
26 qx.Proto.buildGui = function(module)
27 {
28   var fsm = module.fsm;
29   
30   // Main layout composing the whole form
31   var vlayout = new qx.ui.layout.VerticalBoxLayout();
32   vlayout.set({
33                 top: 20,
34                 left: 20,
35                 width: "100%",
36                 bottom: 20
37               });
38
39   // Horizontal layout holding TreeView and a "panel" for ListView
40   var hlayout = new qx.ui.layout.HorizontalBoxLayout();
41   hlayout.set({
42                 top: 0,
43                 left: 0,
44                 right: 0,
45                 height: "80%"
46               });
47
48   // Create a hosts tree
49   this._tree = new qx.ui.treevirtual.TreeVirtual(["Hosts"]);
50   var tree = this._tree;
51
52   // Set the tree's properties
53   tree.set({
54              backgroundColor: 255,
55              border: qx.renderer.border.BorderPresets.getInstance().thinInset,
56              overflow: "hidden",
57              width: "20%",
58              height: "100%",
59              alwaysShowOpenCloseSymbol: true
60            });
61
62   tree.setCellFocusAttributes({ backgroundColor : "transparent" });
63
64   // Create event listener
65   tree.addEventListener("appear", fsm.eventListener, fsm);
66
67   // Give a tree widget nicer name to handle
68   fsm.addObject("tree", tree, "swat.main.fsmUtils.disable_during_rpc");
69
70   // "Panel" for list view
71   this._panel = new qx.ui.layout.VerticalBoxLayout();
72   var panel = this._panel;
73   
74   panel.set({
75               top: 0,
76               left: 10,
77               width: "80%",
78               height: "100%"
79             });
80
81   // Setup some initial columns and (empty) item list - to be replaced soon
82   // with default view loading
83   var columns = { name : { label: "Name", width: 120, type: "text" }};
84   var items = [];
85
86   // Setup the list view
87   this._view = new qx.ui.listview.ListView(items, columns);
88   var view = this._view;
89   view.setBorder(qx.renderer.border.BorderPresets.getInstance().shadow);
90   view.setBackgroundColor("white");
91   view.set({
92              top: 0,
93              left: 0,
94              width: "80%",
95              height: "100%"
96            });
97
98   // Give a list view name to handle
99   fsm.addObject("view", view);
100
101   // and the list view to the panel
102   panel.add(view);
103   
104   // Add the tree view and panel for list view to the layout
105   hlayout.add(tree);
106   hlayout.add(panel);
107
108   // Status layout containing informative labels and status information
109   var statusLayout = new qx.ui.layout.HorizontalBoxLayout();
110   statusLayout.set({
111                      top: 10,
112                      left: 0,
113                      right: 0,
114                      height: "100%"
115                    });
116
117   // First "column" of status fields
118   var colALayout = new qx.ui.layout.VerticalBoxLayout();
119   colALayout.set({
120                   top: 0,
121                   left: 0,
122                   width: 150,
123                   height: "100%"
124                 });
125
126   // Domain name (credentials) - label and text box
127   var statusDomain = new qx.ui.layout.HorizontalBoxLayout();
128   statusDomain.set({ top: 0, left: 0, width: "100%", height: 20,
129                        verticalChildrenAlign: "middle" });
130   
131   var lblDomain = new qx.ui.basic.Atom();
132   lblDomain.setLabel("Domain:");
133   lblDomain.set({ width: 70, right: 5, horizontalChildrenAlign: "right" });
134
135   var txtDomain = new qx.ui.form.TextField();
136   txtDomain.set({ width: 80, readOnly: true });
137   this._txtDomain = txtDomain;
138
139   statusDomain.add(lblDomain);
140   statusDomain.add(txtDomain);
141   
142   // Username (credentials) - label and text box
143   var statusUsername = new qx.ui.layout.HorizontalBoxLayout();
144   statusUsername.set({ top: 0, left: 0, width: "100%", height: 20,
145                        verticalChildrenAlign: "middle" });
146
147   var lblUsername = new qx.ui.basic.Atom();
148   lblUsername.setLabel("Username:");
149   lblUsername.set({ width: 70, right: 5, horizontalChildrenAlign: "right" });
150   
151   var txtUsername = new qx.ui.form.TextField();
152   txtUsername.set({ width: 80, readOnly: true });
153   this._txtUsername = txtUsername;
154   
155   statusUsername.add(lblUsername);
156   statusUsername.add(txtUsername);
157   
158   colALayout.add(statusDomain);
159   colALayout.add(statusUsername);
160
161   statusLayout.add(colALayout);
162   
163   vlayout.add(hlayout);
164   vlayout.add(statusLayout);
165
166   vlayout.addEventListener("appear", fsm.eventListener, fsm);
167   fsm.addObject("vlayout", vlayout);
168
169   // place everything on canvas
170   module.canvas.add(vlayout);
171
172   // Add event handler to netCtx property change
173   this.addEventListener("changeNetCtx", fsm.eventListener, fsm);
174   fsm.addObject("swat.module.netmgr.Gui", this);
175 };
176
177
178 qx.Proto.displayData = function(module, rpcRequest)
179 {
180   var gui = module.gui;
181   var fsm = module.fsm;
182   var result = rpcRequest.getUserData("result");
183   var requestType = rpcRequest.getUserData("requestType");
184
185   // Something went wrong
186   if (result.type == "failed")
187   {
188     alert("Async(" + result.id + ") exception: " + result.data);
189     return;
190   }
191
192   switch (requestType)
193   {
194     case "hostname":
195     // Add local host node
196     this._addHostNode(module, rpcRequest, true);
197     break;
198
199     case "NetContext":
200     this._initNetContext(module, rpcRequest);
201     break;
202
203     case "NetContextCreds":
204     this._updateNetContextCreds(module, rpcRequest);
205     break;
206
207     case "UserMgr":
208     this._initUserManager(module, rpcRequest);
209     break;
210   }
211
212   qx.ui.core.Widget.flushGlobalQueues();
213 };
214
215
216 qx.Proto.getParentNode = function(module, node)
217 {
218   var tree = this._tree;
219   var nodes = tree.getTableModel().getData();
220   if (nodes == undefined)
221   {
222     return undefined;
223   }
224
225   if (node.parentNodeId == 0)
226   {
227     // there is no parent node
228     return node;
229   }
230   
231   var parentNode = nodes[node.parentNodeId];
232   return parentNode;
233 };
234
235
236 qx.Proto._addHostNode = function(module, rpcRequest, local)
237 {
238   var fsm = module.fsm;
239   var hostname = rpcRequest.getUserData("result").data;
240
241   // Get the tree widget
242   var tree = this._tree;
243   var dataModel = tree.getDataModel();
244   
245   // Add new host and its service leaves
246   var hostNodeId = dataModel.addBranch(null, hostname, false);
247   
248   var domainNodeId = dataModel.addLeaf(hostNodeId, "Domain", false);
249   var usersNodeId = dataModel.addLeaf(hostNodeId, "Users", false);
250   var groupsNodeId = dataModel.addLeaf(hostNodeId, "Groups", false);
251   var srvcsNodeId = dataModel.addLeaf(hostNodeId, "Services", false);
252   
253   dataModel.setData();
254   tree.addEventListener("changeSelection", fsm.eventListener, fsm);
255
256   var hostNode = dataModel.getData()[hostNodeId];
257
258   // Set host-specific properties
259   hostNode.netCtx = undefined;
260   hostNode.local = local;
261 };
262
263
264 qx.Proto._initNetContext = function(module, rpcRequest)
265 {
266   // Gather obtained NetContext handle
267   var result = rpcRequest.getUserData("result").data;
268   this.setNetCtx(result);
269 };
270
271
272 qx.Proto._updateNetContextCreds = function(module, rpcRequest)
273 {
274   // Get requested credentials from the current NetContext
275   var result = rpcRequest.getUserData("result").data;
276   this._txtUsername.setValue(result.username);
277   this._txtDomain.setValue(result.domain);
278 };
279
280
281 qx.Proto._initUserManager = function(module, rpcRequest)
282 {
283   // Get obtained UsrCtx handle
284   var result = rpcRequest.getUserData("result").data;
285 };
286
287
288 /**
289  * Singleton Instance Getter
290  */
291 qx.Class.getInstance = qx.lang.Function.returnInstance;