vpackager / src / vpackager / historymanager.py

#!/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
import os

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

class TaskMenu(gtk.Menu):
    """Menu that pops up when right-clicking a task"""
    def __init__(self, selected, parent=None):
        gtk.Menu.__init__(self)
        self.selected = selected
        self._parent = parent
        item_rebuild = gtk.MenuItem('Build again')
        item_rebuild.connect('activate',
                self._rebuild_event)
        item_details = gtk.MenuItem('Build Details')
        item_details.connect('activate',
                self._task_details_event)
        item_rebuild.show()
        item_details.show()
        self.append(item_rebuild)
        self.append(item_details)

    def _rebuild_event(self, widget=None):
        _id = self.selected
        _srcuri = dbutils.GetTaskProp(_id, 'srcURI')
        if _srcuri.startswith('/'):
            if not os.path.exists(_srcuri):
                # FIXME: Source package can no longer be found
                err = message.Error(parent=self,
                        text="The source package for this job can no longer "+\
                                "be found on the system. Please specify " +\
                                "the new location of the source package")
                if err.run():
                    err.destroy()
        _descuri = dbutils.GetTaskProp(_id, 'descURI')
        _patches = dbutils.GetTaskProp(_id, 'patches')
        _type = dbutils.GetTaskProp(_id, 'type')
        _app = dbutils.GetTaskProp(_id, 'app')
        _ver = dbutils.GetTaskProp(_id, 'ver')
        dia = guitools.JobAddDialog()
        dia.SrcURI = _srcuri
        dia.DescURI = _descuri
        dia.appEntry.set_text(_app)
        dia.verEntry.set_text(_ver)
        dia.srcentry.set_text(_srcuri)
        dia.descEntry.set_text(_descuri)
        if _type == 'autotools':
            typeid = 1
        elif _type == 'python':
            typeid = 2
        elif _type == 'cmake':
            typeid = 3
        elif _type == 'perl':
            typeid = 4
        elif _type == 'waf':
            typeid = 5
        else:
            typeid = 0

        dia.srcTypeSel.set_active(typeid)
        #dia.cbsrcTypeSel.set_active(
        #        dia.dbsrcTypeSel.get_model())
        if _patches:
            mod = dia.patchestree.get_model()
            for patch in _patches:
                mod.append([patch])

        res = dia.run()
        dia.hide()
        if res == gtk.RESPONSE_OK:
            app = dia.app
            ver = dia.ver
            desc = dia.DescURI
            src = dia.SrcURI
            rel = dia.release
            srctype = dia.srctype
            toinstall = dia.install
            patches = ','.join(dia.patches)
            dbutils.NewJob(app, ver, src, desc,
                    rel, srctype, toinstall, patches)
        dia.destroy()

    def _task_details_event(self, widget=None):
        return self._parent._task_details_event(widget)


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):
        dia = guitools.AboutTask(self._selected_jobid)
        if dia.run():
            dia.destroy()

    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)
        box.pack_start(btWipe, False, False, 2)
        box.pack_start(self.btTaskDetails, False, False, 2)

        return box

    def _tree_button_press(self, widget, event):
        if event.button == 3:
            m = TaskMenu(selected = self._selected_jobid,
                    parent=self)
            m.popup(None, None, None, event.button, event.time)

    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)
        self.tree.connect('button-press-event', self._tree_button_press)
        return self.tree

    def _do_wipe_history(self, widget=None):
        """ Wipe the entire history """
        dia = message.Question(
                text='This will erase you entire build history. \n' + \
                        'Are you sure you want to continue?')
        res = dia.run()
        dia.destroy()
        if res == gtk.RESPONSE_YES:
            dbutils.WipeHistory()
        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()
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.