1# _control.py
2#
3# openipmi GUI handling for controls
4#
5# Author: MontaVista Software, Inc.
6#         Corey Minyard <minyard@mvista.com>
7#         source@mvista.com
8#
9# Copyright 2005 MontaVista Software Inc.
10#
11#  This program is free software; you can redistribute it and/or
12#  modify it under the terms of the GNU Lesser General Public License
13#  as published by the Free Software Foundation; either version 2 of
14#  the License, or (at your option) any later version.
15#
16#
17#  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
18#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22#  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
23#  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24#  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25#  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
26#  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28#  You should have received a copy of the GNU Lesser General Public
29#  License along with this program; if not, write to the Free
30#  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31#
32
33import OpenIPMI
34from openipmigui import gui_popup
35from openipmigui import gui_setdialog
36from openipmigui import gui_lightset
37
38class ControlRefreshData:
39    def __init__(self, c):
40        self.c = c
41        return
42
43    def control_cb(self, control):
44        if (self.c.control_type == OpenIPMI.CONTROL_IDENTIFIER):
45            control.identifier_get_val(self.c)
46        elif (self.c.setting_light):
47            control.get_light(self.c)
48        else:
49            control.get_val(self.c)
50            pass
51        return
52
53    pass
54
55class ControlSet:
56    def __init__(self, c):
57        self.c = c;
58        return
59
60    def HandleMenu(self, event):
61        if (self.c.impt_data == None):
62            ml = [ [ "Add to watch values", self.c.add_impt ] ]
63            pass
64        else:
65            ml = [ [ "Remove from watch values", self.c.remove_impt ] ]
66            pass
67        ml.append([ "Modify Value", self.modval ])
68        ml.append([ "Set to 0",     self.SetTo0 ])
69        ml.append([ "Set to 1",     self.SetTo1 ])
70        gui_popup.popup(self.c.ui, event, ml)
71        return
72
73    def modval(self, event):
74        vals = self.c.vals
75        while (len(vals) < self.c.num_vals):
76            vals.append(0)
77            pass
78        gui_setdialog.SetDialog("Set Control Values for " + self.c.name,
79                                vals,
80                                self.c.num_vals,
81                                self)
82        return
83
84    def SetTo0(self, event):
85        self.ival = [ 0 ]
86        self.c.control_id.to_control(self)
87        return
88
89    def SetTo1(self, event):
90        self.ival = [ 1 ]
91        self.c.control_id.to_control(self)
92        return
93
94    def ok(self, vals):
95        self.ival = [ ]
96        for val in vals:
97            self.ival.append(int(val))
98            pass
99        self.c.control_id.to_control(self)
100        return
101
102    def control_cb(self, control):
103        if (self.c.control_type == OpenIPMI.CONTROL_IDENTIFIER):
104            control.identifier_set_val(self.ival)
105        else:
106            control.set_val(self.ival)
107            pass
108        return
109
110    def do_on_close(self):
111        self.c = None
112
113    pass
114
115class LightSet:
116    def __init__(self, c):
117        self.c = c;
118        return
119
120    def HandleMenu(self, event):
121        if (self.c.impt_data == None):
122            ml = [ [ "Add to watch values", self.c.add_impt ] ]
123            pass
124        else:
125            ml = [ [ "Remove from watch values", self.c.remove_impt ] ]
126            pass
127        ml.append([ "Modify Value", self.modval ])
128        gui_popup.popup(self.c.ui, event, ml)
129        return
130
131    def modval(self, event):
132        gui_lightset.LightSet("Set Light Values for " + self.c.name,
133                              self.c.num_vals, self.c.lights, self.c.vals,
134                              self);
135        return
136
137    def ok(self, val):
138        self.ival = ';'.join(val)
139        self.c.control_id.to_control(self)
140        return
141
142    def control_cb(self, control):
143        rv = control.set_light(self.ival)
144        if (rv != 0):
145            raise ValueError("set_light failed: " + str(rv))
146        return
147
148class Control:
149    def __init__(self, e, control):
150        self.e = e
151        self.name = control.get_name()
152        e.controls[self.name] = self
153        self.control_type_str = control.get_type_string()
154        self.control_id = control.get_id()
155        self.ui = e.ui;
156        self.updater = ControlRefreshData(self)
157        self.vals = [ ]
158        self.ui.add_control(self.e, self)
159        self.control_type = control.get_type()
160        self.destroyed = False
161        if (self.control_type == OpenIPMI.CONTROL_IDENTIFIER):
162            self.num_vals = control.identifier_get_max_length();
163        else:
164            self.num_vals = control.get_num_vals();
165            pass
166
167        if ((self.control_type == OpenIPMI.CONTROL_LIGHT)
168            and (control.light_set_with_setting())):
169            self.setting_light = True
170            self.lights = [ ]
171            for  i in range (0, self.num_vals):
172                lc = control.light_has_local_control(i)
173                colors = [ ]
174                for j in range (0, OpenIPMI.CONTROL_NUM_COLORS):
175                    if control.light_is_color_supported(i, j):
176                        colors.append(OpenIPMI.color_string(j))
177                        pass
178                    pass
179                self.lights.append((lc, colors))
180                pass
181            pass
182        else:
183            self.setting_light = False
184            pass
185
186        self.is_settable = control.is_settable()
187        self.is_readable = control.is_readable()
188        if (self.is_settable):
189            if (self.setting_light):
190                self.setter = LightSet(self)
191            else:
192                self.setter = ControlSet(self)
193                pass
194            pass
195        else:
196            self.setter = None
197            pass
198
199        self.ui.prepend_item(self, "Control Type", self.control_type_str)
200        cs = [ ]
201        if (control.has_events()):
202            cs.append("generates_events")
203            pass
204        if (self.is_settable):
205            cs.append("settable")
206            pass
207        if (self.is_readable):
208            cs.append("readable")
209            pass
210        self.ui.append_item(self, "Control Capabilities", ' '.join(cs))
211        if (self.control_type == OpenIPMI.CONTROL_LIGHT):
212            self.ui.append_item(self, "Num Lights", str(self.num_vals))
213            if (self.setting_light):
214                self.ui.append_item(self, "Light Type", "setting")
215                for i in range(0, self.num_vals):
216                    cap = [ ]
217                    if control.light_has_local_control(i):
218                        cap.append("local_control")
219                        pass
220                    for j in range (0, OpenIPMI.CONTROL_NUM_COLORS):
221                        if control.light_is_color_supported(i, j):
222                            cap.append(OpenIPMI.color_string(j))
223                            pass
224                        pass
225                    self.ui.append_item(self, "Light" + str(i), ' '.join(cap))
226                    pass
227                pass
228            else:
229                self.ui.append_item(self, "Light Type", "transition")
230                for i in range(0, self.num_vals):
231                    cap = [ ]
232                    for j in range (0, control.get_num_light_values(i)):
233                        cap2 = [ ]
234                        for k in range (0,control.get_num_light_transitions(i, j)):
235                            cap3 = [ ]
236                            val = control.get_light_color(i, j, k)
237                            cap3.append(OpenIPMI.color_string(val))
238                            val = control.get_light_color_time(i, j, k)
239                            cap3.append(OpenIPMI.color_string(val))
240                            cap2.append(cap3)
241                            pass
242                        cap.append(cap2)
243                        pass
244                    self.ui.append_item(self, "Light" + str(i), str(cap))
245                    pass
246                pass
247            pass
248        elif (self.control_type == OpenIPMI.CONTROL_IDENTIFIER):
249            self.ui.append_item(self, "Max Length", str(self.num_vals))
250        else:
251            self.ui.append_item(self, "Num Vals", str(self.num_vals))
252            pass
253        return
254
255    def __str__(self):
256        return self.name
257
258    def DoUpdate(self):
259        if (self.is_readable):
260            self.control_id.to_control(self.updater)
261            pass
262        return
263
264    def HandleMenu(self, event):
265        if (self.setter != None):
266            self.setter.HandleMenu(event)
267            pass
268        else:
269            if (self.impt_data == None):
270                ml = [ [ "Add to watch values", self.add_impt ] ]
271                pass
272            else:
273                ml = [ [ "Remove from watch values", self.remove_impt ] ]
274                pass
275            gui_popup.popup(self.ui, event, ml)
276            pass
277        return
278
279    def add_impt(self, event):
280        self.ui.add_impt_data("control", str(self), self)
281        return
282
283    def remove_impt(self, event):
284        self.ui.remove_impt_data(self.impt_data)
285        return
286
287    def control_get_val_cb(self, control, err, vals):
288        if (self.destroyed):
289            return
290        if (err != 0):
291            self.ui.set_item_text(self.treeroot, None)
292            return
293        self.num_vals = control.get_num_vals();
294        self.vals = vals
295        self.ui.set_item_text(self.treeroot, str(vals))
296        return
297
298    def control_get_id_cb(self, control, err, val):
299        if (self.destroyed):
300            return
301        if (err != 0):
302            self.ui.set_item_text(self.treeroot, None)
303            return
304        self.num_vals = control.identifier_get_max_length();
305        self.val = val
306        self.ui.set_item_text(self.treeroot, str(val))
307        return
308
309    def control_get_light_cb(self, control, err, vals):
310        if (self.destroyed):
311            return
312        if (err != 0):
313            self.ui.set_item_text(self.treeroot, None)
314            return
315        self.num_vals = control.get_num_vals();
316        v1 = vals.split(":")
317        self.vals = [ ]
318        for s1 in v1:
319            v1 = s1.split()
320            if (v1[0] != "lc"):
321                v1.insert(0, "")
322                pass
323            self.vals.append(v1)
324            pass
325        self.ui.set_item_text(self.treeroot, str(self.vals))
326        return
327
328    def remove(self):
329        self.e.controls.pop(self.name)
330        self.ui.remove_control(self)
331        self.destroyed = True
332        self.e = None
333        self.updater = None
334        self.ui = None
335        return
336
337    pass
338