asterator / Asterator / conftree.py

import gobject
import pygtk
pygtk.require('2.0')
import gtk

(
    CHANNEL_COLUMN,
    CALLERID_COLUMN,
    NUMBER_COLUMN,
    TIME_COLUMN,
    TALKING_COLUMN
) = range(5)

class conf_tree(gtk.TreeView):
    def gen_model(self):
        model = gtk.TreeStore(
            gobject.TYPE_STRING,
            gobject.TYPE_STRING,
            gobject.TYPE_STRING,
            gobject.TYPE_STRING,
            gobject.TYPE_STRING)

        self.confs = {}

        return model
    
    def __init__(self):
        model = self.gen_model()
        
        gtk.TreeView.__init__(self, model)
        self.set_rules_hint(True)
        self.connect('realize', lambda tv: tv.expand_all())
        self.connect('row-activated', self.on_rowactivated)
        
        renderer = gtk.CellRendererText()
        renderer.set_property("xalign", 0.0)
        column = gtk.TreeViewColumn("Channel", renderer, text=CHANNEL_COLUMN)
        column.set_resizable(True)
        self.append_column(column)
        
        renderer = gtk.CellRendererText()
        renderer.set_property("xalign", 0.0)
        column = gtk.TreeViewColumn("CallerID", renderer, text=CALLERID_COLUMN)
        column.set_resizable(True)
        self.append_column(column)
        
        renderer = gtk.CellRendererText()
        renderer.set_property("xalign", 0.0)
        column = gtk.TreeViewColumn("Member No.", renderer, text=NUMBER_COLUMN)
        column.set_resizable(True)
        self.append_column(column)
        
        renderer = gtk.CellRendererText()
        renderer.set_property("xalign", 0.0)
        column = gtk.TreeViewColumn("Joined", renderer, text=TIME_COLUMN)
        column.set_resizable(True)
        self.append_column(column)
        
        renderer = gtk.CellRendererText()
        renderer.set_property("xalign", 0.0)
        column = gtk.TreeViewColumn("Talking", renderer, text=TALKING_COLUMN)
        column.set_resizable(True)
        self.append_column(column)
        
    def on_rowactivated(self, tv, path, view_col):
        if tv.row_expanded(path):
            tv.collapse_row(path)
        else:
            tv.expand_row(path, False)

    def clear(self):
        self.set_model(self.gen_model())

    def reconcile(self, listener):
        """A quickly written refresh method

        Don't call this from within a listener callback or you'll get
        a deadlock lolz because this locks the listener object.

        ToDo: Make this better
        
        """
        self.clear()
        listener.lock()
        for conf in listener.confs.values():
            self.add_conf(conf)
            for memb in conf.members.values():
                self.add_conf_member(conf, memb)
        listener.unlock()

    def add_conf(self, conf):
        model = self.get_model()
        iter = model.append(None)
        conf.iter = iter
        model.set(conf.iter,
                  CHANNEL_COLUMN, 'Conf: ' + conf.number,
                  CALLERID_COLUMN, '',
                  NUMBER_COLUMN, '',
                  TIME_COLUMN, '',
                  TALKING_COLUMN, '')

        self.confs[conf.number] = 0

    def del_conf(self, conf):
        model = self.get_model()
        model.remove(conf.iter)
        del self.confs[conf.number]

    def add_conf_member(self, conf, memb):
        model = self.get_model()
        memb.iter = model.append(conf.iter)
        if memb.talking:
            talking = True
        else:
            talking = False
        model.set(memb.iter,
                  CHANNEL_COLUMN, memb.chan.name,
                  CALLERID_COLUMN, memb.chan.get_callerid(),
                  NUMBER_COLUMN, memb.usernum,
                  TIME_COLUMN, memb.get_timealive(),
                  TALKING_COLUMN, talking)

        self.confs[conf.number] += 1

        if self.confs[conf.number] == 1:
            self.expand_row(model.get_string_from_iter(conf.iter), True)

    def del_conf_member(self, conf, memb):
        model = self.get_model()
        model.remove(memb.iter)
        self.confs[conf.number] -= 1

    def upd_conf_member(self, conf, memb):
        model = self.get_model()
        if memb.talking:
            talking = True
        else:
            talking = False
        model.set(memb.iter,
                  CHANNEL_COLUMN, memb.chan.name,
                  CALLERID_COLUMN, memb.chan.get_callerid(),
                  NUMBER_COLUMN, memb.usernum,
                  TIME_COLUMN, memb.get_timealive(),
                  TALKING_COLUMN, talking)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.