Source

vpackager / src / vpackager / historymanager.py

Full commit
#!/usr/bin/env python

#    This file is part of vpackager.
#
#    vpackager is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v2 as published by
#    the Free Software Foundation.
#
#    vpackager is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with vpackager.  If not, see <http://www.gnu.org/licenses/>.

import gtk
import dbutils
import guitools
import message

__author__ = 'M0E-lnx'
__author_email__ = 'moc.liamg@xnl.E0M'[::-1]
__version__ = '0.1'

class Main(gtk.VBox):
    ''' Container holding a treeview with convinient methods to work with the
    job history'''
    def __init__(self):
        gtk.VBox.__init__(self)
        self._selected_jobid = None
        self.model = gtk.ListStore(
                int, str, str, str, str, str)
        self.pack_start(gtk.Label('Job History'), False, True, 2)
        scroll = gtk.ScrolledWindow()
        scroll.set_policy(gtk.POLICY_AUTOMATIC,
                gtk.POLICY_AUTOMATIC)
        self.pack_start(scroll, True, True, 2)
        scroll.add(self._get_tree())

        self.show_all()
        self.pack_start(self._get_bottom_toolbar(), False, True, 2)
        self._refresh_history_display()
        self.btWipeHistory.set_property('sensitive',
                len(dbutils.GetHistory()) > 0)

    def _task_details_event(self, widget=None):
        pass

    def _get_bottom_toolbar(self):
        '''toolbar at the bottom of the job history'''
        box = gtk.HBox()
        btWipe = guitools.vButton(stock=gtk.STOCK_DELETE,
                label='Erase History')
        self.btWipeHistory = btWipe
        self.btTaskDetails = guitools.vButton(stock=gtk.STOCK_INFO,
                label='Build Details')
        self.btTaskDetails.connect('clicked', self._task_details_event)
        self.btWipeHistory.connect('clicked', self._do_wipe_history)
        btWipe.connect('clicked', self._do_wipe_history)
        box.pack_start(btWipe, False, False, 2)
        box.pack_start(self.btTaskDetails, False, False, 2)

        return box

    def _tree_click_event(self, tree=None):
        """ Events triggered by clicking on the treeview"""
        sel = tree.get_selection()
        (model, _iter) = sel.get_selected()
        selrow = tree.get_model()[_iter][0]
        self._selected_jobid = selrow
        self.btTaskDetails.set_property('sensitive',
                self._selected_jobid != None)

    def _get_tree(self):
        """ Create the treeview that displays the history"""
        self.tree = gtk.TreeView(model = self.model)
        i = 0
        for field in ('ID','Application', 'Version',
                'Build Start', 'Build End', 'Result'):
            col = gtk.TreeViewColumn(field)
            col.set_resizable(True)
            crt = gtk.CellRendererText()
            col.pack_start(crt)
            col.add_attribute(crt, 'text', i)
            self.tree.append_column(col)
            i += 1
        self.tree.connect('cursor-changed', self._tree_click_event)
        return self.tree

    def _do_wipe_history(self, widget=None):
        """ Wipe the entire history """
        dia = message.Question(parent = self.parent,
                text='This will erase you entire build history. \n' + \
                        'Are you sure you want to continue?')
        res = dia.run()
        dia.hide()
        if dia.response == gtk.RESPONSE_YES:
            dbutils.WipeHistory()
        dia.destroy()
        return self._refresh_history_display()

    def _refresh_history_display(self):
        self.tree.set_model(
                gtk.ListStore(int,str, str, str, str, str))
        lst = dbutils.GetHistory()
        for entry in lst:
            self.tree.get_model().append(entry)
        self.btWipeHistory.set_property('sensitive', (len(lst) > 0))
        self.btTaskDetails.set_property('sensitive',
                self._selected_jobid != None)

if __name__ == '__main__':
    w = gtk.Window(gtk.WINDOW_TOPLEVEL)
    w.add(Main())
    w.set_size_request(500,300)
    w.connect('destroy', gtk.main_quit)
    w.show_all()
    gtk.gdk.threads_init()
    gtk.gdk.threads_enter()
    gtk.main()