Source

asterator / Asterator / mainwin.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553

import time
import thread
import socket
import timeoutsocket
import gobject
import pygtk
pygtk.require('2.0')
import gtk

from channeltree import channel_tree
from zaptree import zap_tree
from conftree import conf_tree
from about import ast_about
from info import channel_info
from Asterisk import listener, manager

timeoutsocket.setDefaultSocketTimeout(5.0)

class asterator(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
        self.connect('destroy', self.action_quit)
        self.set_title("asterator")
        self.set_default_size(700, 500)
        self.set_position(gtk.WIN_POS_MOUSE)
        
        vbox = gtk.VBox(homogeneous=False, spacing=0);
        self.add(vbox)
        vbox.show()
        
        self.ag = gtk.ActionGroup('asterator')
        self.ag.add_actions([
            ('FileMenu', None, '_File'),
            ('HelpMenu', None, '_Help'),
            ('DebugMenu', None, '_Debug'),
            ('Connect', gtk.STOCK_NETWORK, '_Connect', '<control>c', None, self.action_connect),
            ('Disconnect', gtk.STOCK_NO, '_Disconnect', '<control>d', None, self.action_disconnect),
            ('Expand', gtk.STOCK_ZOOM_IN, '_Expand All', '<control>e', None, self.action_expand),
            ('Collapse', gtk.STOCK_ZOOM_OUT, 'C_ollapse All', None, None, self.action_collapse),
            ('Refresh', None, '_Refresh', '<control>r', None, self.action_refresh),
            ('Exit', gtk.STOCK_QUIT, None, None, None, self.action_quit),
            ('About', gtk.STOCK_ABOUT, 'A_bout', None, None, self.action_about),
            ('Dump', gtk.STOCK_QUIT, 'Dump', None, None, self.action_quit),
            ('Hangup', gtk.STOCK_CANCEL, 'Hangup', None, None, self.action_chan_hangup),
            ('HangupAfter', None, 'Hangup After...', None, None, self.action_chan_hangupafter),
            ('Redirect', gtk.STOCK_REDO, 'Redirect...', None, None, self.action_chan_redirect),
            ])
        self.ag.add_toggle_actions([
            ('AutoUpdate', None, '_Auto Update', None, None, self.action_autoupdate, True),
            ])
        
        self.ui = gtk.UIManager()
        self.ui.set_add_tearoffs(True)
        self.ui.insert_action_group(self.ag, 0)
        self.ui.add_ui_from_string("""<ui>
  <!-- The UI Manager Thingy is SOOOO COOL!!! -->
  <menubar name='mainmenu'>
    <menu name='connect' action='FileMenu'>
      <menuitem name='connect' action='Connect'/>
      <menuitem name='disconnect' action='Disconnect'/>
      <separator/>
      <menuitem name='expand' action='Expand'/>
      <menuitem name='collapse' action='Collapse'/>
      <separator/>
      <menuitem name='refresh' action='Refresh'/>
      <menuitem name='autoupdate' action='AutoUpdate'/>
      <separator/>
      <menuitem name='exit' action='Exit'/>
    </menu>
    <menu name='help' action='HelpMenu' position='bot'>
      <menuitem name='about' action='About'/>
    </menu>
  </menubar>
  <popup name='popchan'>
    <menuitem action='Hangup'/>
    <menuitem action='HangupAfter'/>
    <menuitem action='Redirect'/>
  </popup>
</ui>
""")
        self.add_accel_group(self.ui.get_accel_group())
        vbox.pack_start(self.ui.get_widget('/ui/mainmenu'), False, False, 0)
        self.ui.get_widget('/ui/mainmenu/connect/disconnect').set_sensitive(False)

        hbox = gtk.HBox(homogeneous=False, spacing=5)
        hbox.set_border_width(10)
        vbox.pack_start(hbox, False)
        hbox.show()
        sep = gtk.HSeparator()
        vbox.pack_start(sep, False)
        sep.show()

        self.lbl_host = gtk.Label('Host/IP:')
        self.lbl_host.show()
        hbox.pack_start(self.lbl_host, False)
        self.txt_host = gtk.Entry(32)
        self.txt_host.set_width_chars(15)
        self.txt_host.set_text('localhost')
        self.txt_host.set_activates_default(True)
        self.txt_host.connect('activate', self.action_connect)
        hbox.pack_start(self.txt_host, False, False)
        self.txt_host.show()

        self.lbl_port = gtk.Label('Port:')
        self.lbl_port.show()
        hbox.pack_start(self.lbl_port, False)
        self.txt_port = gtk.Entry(5)
        self.txt_port.set_width_chars(5)
        self.txt_port.set_text('5038')
        self.txt_port.set_activates_default(True)
        self.txt_port.connect('activate', self.action_connect)
        hbox.pack_start(self.txt_port, False, False)
        self.txt_port.show()
        
        sep = gtk.VSeparator()
        hbox.pack_start(sep, False, False)
        sep.show()
        
        self.lbl_user = gtk.Label('User:')
        self.lbl_user.show()
        hbox.pack_start(self.lbl_user, False)
        self.txt_user = gtk.Entry(32)
        self.txt_user.set_width_chars(10)
        self.txt_user.set_activates_default(True)
        self.txt_user.connect('activate', self.action_connect)
        hbox.pack_start(self.txt_user, False, False)
        self.txt_user.show()

        self.lbl_pass = gtk.Label('Pass:')
        self.lbl_pass.show()
        hbox.pack_start(self.lbl_pass, False)
        self.txt_pass = gtk.Entry(32)
        self.txt_pass.set_visibility(False)
        self.txt_pass.set_width_chars(10)
        self.txt_pass.set_activates_default(True)
        self.txt_pass.connect('activate', self.action_connect)
        hbox.pack_start(self.txt_pass, False, False)
        self.txt_pass.show()
        
        self.btn_disconnect = gtk.Button('Disconnect')
        self.btn_disconnect.connect('clicked', self.action_disconnect)
        self.btn_disconnect.show()
        self.btn_disconnect.set_sensitive(False)
        hbox.pack_end(self.btn_disconnect, False, False)
        
        self.btn_connect = gtk.Button('Connect')
        self.btn_connect.connect('clicked', self.action_connect)
        self.btn_connect.show()
        hbox.pack_end(self.btn_connect, False, False)

        notebook = gtk.Notebook()
        notebook.set_tab_pos(gtk.POS_TOP)
        vbox.pack_start(notebook)
        notebook.show()

        scrollbox = gtk.ScrolledWindow()
        scrollbox.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        scrollbox.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        scrollbox.show()
        self.chantree = channel_tree()
        self.chantree.connect('row-activated', self.on_chan_rowactivated)
        self.chantree.popup = self.ui.get_widget('/ui/popchan')
        scrollbox.add(self.chantree)
        self.chantree.show()

        notebook.append_page(scrollbox, gtk.Label('Active Channels'))

        scrollbox = gtk.ScrolledWindow()
        scrollbox.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        scrollbox.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        scrollbox.show()
        self.zaptree = zap_tree()
        scrollbox.add(self.zaptree)
        self.zaptree.show()

        notebook.append_page(scrollbox, gtk.Label('Zaptel Channels'))

        scrollbox = gtk.ScrolledWindow()
        scrollbox.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        scrollbox.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        scrollbox.show()
        self.conftree = conf_tree()
        scrollbox.add(self.conftree)
        self.conftree.show()

        notebook.append_page(scrollbox, gtk.Label('Conferences'))

        self.statusbar = gtk.Statusbar()
        vbox.pack_start(self.statusbar, False, False, 0)
        self.statusbar.show()

        self.context_id = self.statusbar.get_context_id('General Messages')
        self.statusbar.push(self.context_id, 'Ready')

        self.in_session = False
        self.mansock = None
        self.man = None
        self.listen = None
        self.thread = None
        self.auto_update = True

    def do_connect(self):
        if self.man:
            return

        self.chantree.clear()
        self.zaptree.clear()
        self.conftree.clear()

        self.statusbar.pop(self.context_id)
        self.statusbar.push(self.context_id, 'Attempting to connect...')
        
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.settimeout(5)
        try:
            sock.connect((self.txt_host.get_text(), int(self.txt_port.get_text())))
        except timeoutsocket.Timeout:
            dlg = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT,
                                    gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
                                    "Connection attempt timed out")
            dlg.run()
            dlg.hide()
            self.statusbar.pop(self.context_id)
            self.statusbar.push(self.context_id, 'Connect Timeout!')
            return
        except socket.error, e:
            dlg = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT,
                                    gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
                                    "Connect Failed: " + e[1])
            dlg.run()
            dlg.hide()
            self.statusbar.pop(self.context_id)
            self.statusbar.push(self.context_id, 'Connect Failed!')
            return
        
        sock.settimeout(None)
        
        self.statusbar.pop(self.context_id)
        self.statusbar.push(self.context_id, 'Waiting for server...')

        self.mansock = manager.mansock(sock=sock, debug=True)
        self.mansock.handler_session = self.mansock_session
        
        self.in_session = False
        gobject.timeout_add(5000, self.session_timeout)

        self.man = manager.manager(sock=self.mansock, debug=True)

        self.listen = listener.manager_listener(self.man)
        self.listen.on_newchan = lambda l, c: self.invoke(self.on_newchan, l, c)
        self.listen.on_delchan = lambda l, c: self.invoke(self.on_delchan, l, c)
        self.listen.on_updchan = lambda l, c: self.invoke(self.on_updchan, l, c)
        self.listen.on_newlink = lambda l, c: self.invoke(self.on_newlink, l, c)
        self.listen.on_dellink = lambda l, c: self.invoke(self.on_dellink, l, c)
        self.listen.on_chan_park = lambda l, c: self.invoke(self.on_chan_park, l, c)
        self.listen.on_chan_unpark = lambda l, c: self.invoke(self.on_chan_unpark, l, c)
        self.listen.on_chan_parktimeout = lambda l, c: self.invoke(self.on_chan_unpark, l, c)
        self.listen.on_chan_parkgiveup = lambda l, c: self.invoke(self.on_chan_unpark, l, c)
        self.listen.on_newzap = lambda l, c: self.invoke(self.on_newzap, l, c)
        self.listen.on_updzap = lambda l, c: self.invoke(self.on_updzap, l, c)
        self.listen.on_newconf = lambda l, c: self.invoke(self.on_newconf, l, c)
        self.listen.on_delconf = lambda l, c: self.invoke(self.on_delconf, l, c)
        self.listen.on_joinconf = lambda l, c, m: self.invoke2(self.on_joinconf, l, c, m)
        self.listen.on_leaveconf = lambda l, c, m: self.invoke2(self.on_leaveconf, l, c, m)
        self.listen.on_updmemberconf = lambda l, c, m: self.invoke2(self.on_updmemberconf, l, c, m)

        self.thread = thread.start_new_thread(manager.run_manager, (self.man, 'ignore'))

        self.ui.get_widget('/ui/mainmenu/connect/connect').set_sensitive(False)
        self.ui.get_widget('/ui/mainmenu/connect/disconnect').set_sensitive(True)
        self.btn_connect.set_sensitive(False)
        self.btn_disconnect.set_sensitive(True)
        self.lbl_host.set_sensitive(False)
        self.txt_host.set_sensitive(False)
        self.lbl_port.set_sensitive(False)
        self.txt_port.set_sensitive(False)
        self.lbl_user.set_sensitive(False)
        self.txt_user.set_sensitive(False)
        self.lbl_pass.set_sensitive(False)
        self.txt_pass.set_sensitive(False)

    def do_disconnect(self):
        if not self.man:
            return

        self.man.terminate()
        self.mansock = None
        self.man = None
        self.listen = None
        self.thread = None

        self.statusbar.pop(self.context_id)
        self.statusbar.push(self.context_id, 'Disconnected')
        self.ui.get_widget('/ui/mainmenu/connect/connect').set_sensitive(True)
        self.ui.get_widget('/ui/mainmenu/connect/disconnect').set_sensitive(False)
        self.btn_connect.set_sensitive(True)
        self.btn_disconnect.set_sensitive(False)
        self.lbl_host.set_sensitive(True)
        self.txt_host.set_sensitive(True)
        self.lbl_port.set_sensitive(True)
        self.txt_port.set_sensitive(True)
        self.lbl_user.set_sensitive(True)
        self.txt_user.set_sensitive(True)
        self.lbl_pass.set_sensitive(True)
        self.txt_pass.set_sensitive(True)
        
    def action_connect(self, widget, data=None):
        self.do_connect()

    def action_disconnect(self, widget, data=None):
        self.do_disconnect()

    def action_expand(self, widget, data=None):
        self.chantree.expand_all()

    def action_collapse(self, widget, data=None):
        self.chantree.collapse_all()

    def action_refresh(self, widget, data=None):
        self.chantree.reconcile(self.listen)
        self.zaptree.reconcile(self.listen)
        self.conftree.reconcile(self.listen)

    def action_autoupdate(self, widget, data=None):
        self.auto_update = widget.get_active()

    def action_about(self, widget, data=None):
        abt = ast_about()
        abt.run()
        abt.hide()

    def action_quit(self, widget, data=None):
        self.do_disconnect()
        gtk.main_quit()

    def generic_callback(self, frame):
        gtk.threads_enter()
        if not 'Message' in frame:
            frame['Message'] = 'None'
        if frame['Response'] == 'Success':
            self.statusbar.pop(self.context_id)
            self.statusbar.push(self.context_id, 'Success: ' + frame['Message'])
        else:
            dlg = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT,
                                    gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
                                    "Error: " + frame['Message'])
            dlg.run()
            dlg.hide()
        gtk.threads_leave()

    def on_chan_rowactivated(self, tv, path, view_col):
        if not self.man:
            return False
        
        m = self.chantree.get_model()
        chan = None
        self.listen.lock()
        for c in self.listen.channels.values():
            if m.get_path(c.iter) == path:
                chan = c
        self.listen.unlock()
        if chan == None:
            return
        cw = channel_info(chan)
        cw.show()

    def action_chan_hangup(self, widget, data=None):
        if not self.man:
            return False
        
        sel = self.chantree.get_selection()
        self.listen.lock()
        found = False
        for chan in self.listen.channels.values():
            if sel.iter_is_selected(chan.iter):
                chan.hangup(self.generic_callback)
                found = True
        self.listen.unlock()
        if not found:
            dlg = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT,
                                    gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
                                    "No channels selected!")
            dlg.run()
            dlg.hide()

    def action_chan_hangupafter(self, widget, data=None):
        pass

    def action_chan_redirect(self, widget, data=None):
        pass

    def session_timeout(self):
        if not self.man:
            return False
        
        if not self.in_session:
            gtk.threads_enter()
            dlg = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT,
                                    gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
                                    "Server did not send valid protocol header")
            dlg.run()
            dlg.hide()
            gtk.threads_leave()
            self.do_disconnect()
        return False

    def mansock_session(self):
        """Invoked by mansock after header is received

        This callback is implemented so we know when to login.  We
        want to wait until after we receive the protocol header from
        the server.
        
        """
        gtk.threads_enter()
        self.statusbar.pop(self.context_id)
        self.statusbar.push(self.context_id, 'Sending Login...')
        gtk.threads_leave()
        
        self.in_session = True
        self.man.login(self.man_login, username='user', secret='pass')

    def man_login(self, frame):
        """Invoked by manager when response to login is sent
        """
        if frame['Response'] == 'Success':
            gtk.threads_enter()
            self.statusbar.pop(self.context_id)
            self.statusbar.push(self.context_id, 'Connected')
            gtk.threads_leave()
            self.listen.fetch_info()
            
            gobject.timeout_add(1000, lambda l, c: self.invoke(self.on_heartbeat, l, c), None, None)
        else:
            gtk.threads_enter()
            dlg = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT,
                                    gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
                                    "Login Failed: " + frame['Message'])
            dlg.run()
            dlg.hide()
            self.statusbar.pop(self.context_id)
            self.statusbar.push(self.context_id, 'Login Failed')
            gtk.threads_leave()
            self.do_disconnect()

    def invoke(self, callback, listener, chan):
        gtk.threads_enter()
        callback(listener, chan)
        gtk.threads_leave()

    def invoke2(self, callback, listener, c, m):
        gtk.threads_enter()
        callback(listener, c, m)
        gtk.threads_leave()

    def on_heartbeat(self, ignore1, ignore2):
        """Called every second by GTK

        This method is intended to update the 'Alive' column of all
        the channels.

        """
        if not self.auto_update:
            return
        if not self.man:
            return False

        self.listen.lock()
        for chan in self.listen.channels.values():
            self.on_updchan(self.listen, chan)
        for conf in self.listen.confs.values():
            for memb in conf.members.values():
                self.on_updmemberconf(self.listen, chan, memb)
        self.listen.unlock()
        
        gobject.timeout_add(1000, lambda l, c: self.invoke(self.on_heartbeat, l, c), None, None)

    def on_newchan(self, listener, chan):
        if not self.auto_update:
            return
        self.chantree.add_channel(chan)

    def on_delchan(self, listener, chan):
        if not self.auto_update:
            return
        self.chantree.del_channel(chan)

    def on_updchan(self, listener, chan):
        if not self.auto_update:
            return
        self.chantree.upd_channel(chan)

    def on_chan_park(self, listener, chan):
        if not self.auto_update:
            return
        self.chantree.prk_channel(chan)
        
    def on_chan_unpark(self, listener, chan):
        if not self.auto_update:
            return
        self.chantree.unprk_channel(chan)
    
    def on_newlink(self, listener, link):
        if not self.auto_update:
            return
        self.chantree.add_link(link)
    
    def on_dellink(self, listener, link):
        if not self.auto_update:
            return
        self.chantree.del_link(link)

    def on_newzap(self, listener, zap):
        if not self.auto_update:
            return
        self.zaptree.add_zap(zap)

    def on_updzap(self, listener, zap):
        if not self.auto_update:
            return
        self.zaptree.upd_zap(zap)

    def on_newconf(self, listener, conf):
        if not self.auto_update:
            return
        self.conftree.add_conf(conf)

    def on_delconf(self, listener, conf):
        if not self.auto_update:
            return
        self.conftree.del_conf(conf)

    def on_joinconf(self, listener, conf, memb):
        if not self.auto_update:
            return
        self.conftree.add_conf_member(conf, memb)

    def on_leaveconf(self, listener, conf, memb):
        if not self.auto_update:
            return
        self.conftree.del_conf_member(conf, memb)

    def on_updmemberconf(self, listener, conf, memb):
        if not self.auto_update:
            return
        self.conftree.upd_conf_member(conf, memb)

def main():
    gtk.threads_init()
    win = asterator()
    win.show()
    gtk.main()