vpackager / src / vpackager / dbviews.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 v3 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 buildutils
import time

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

class RCMenu(gtk.Menu):
    """ Menu that pops-up when the treeview is right-clicked"""
    def __init__(self, items):
        gtk.Menu.__init__(self)
        self.menu_actions = {}
        for item in items:
            self.menu_actions[
                item.replace(' ','_').lower()] = gtk.MenuItem(item)
            mitem = self.menu_actions[item.replace(' ', '_').lower()]
            mitem.show()
            self.append(mitem)

    def connect_item(self, item, callback):
        return self.menu_actions[item].connect('activate', callback)

class QueueView(gtk.VBox):
    """ Job manager container"""
    def __init__(self):
        gtk.VBox.__init__(self)
        self.rc_menu = RCMenu(items = [
            'Job Details', 'Delete Job', 'Edit Job'])
        self.exposed_widgets = {}
        self.banner_area = gtk.HBox()
        #self.pack_start(self.banner_area, False, True, 4)
        self.banner_label = gtk.Label('Manage job queue')
        self.banner_area.pack_start(self.banner_label, True, True, 2)
        scrl = gtk.ScrolledWindow()
        scrl.set_policy(gtk.POLICY_AUTOMATIC,
                        gtk.POLICY_AUTOMATIC)
        _model = gtk.ListStore(
            int, str, str, str)
        self.treeview = self._get_treeview()
        self.treeview.connect('button-press-event', self.tree_click_event)
        self.treeview.connect('cursor-changed', self.tree_selection_change_event)
        scrl.add(self.treeview)
        self.pack_start(scrl, True, True, 2)
        bottombar = self._get_bottom_toolbar()
        self.pack_start(bottombar, False, True, 2)
        self._selectedjob = None

    def connect_item(self, item, callback, *kwargs):
        if type(item) is gtk.RadioButton:
            return self.exposed_widgets[item].connect(
                'toggled', callback, *kwargs)
        else:
            return self.exposed_widgets[item].connect('clicked', callback, *kwargs)

    def _get_treeview(self):
        _model = gtk.ListStore(int, str, str, str)
        return guitools.TableTree(
            model = _model,
            columns = [
                'ID',
                'Application',
                'Version',
                'Release'
            ]
        )

    def _get_bottom_toolbar(self):
        bar = gtk.HBox()
        self.exposed_widgets['add_job'] = guitools.vButton(
            stock=gtk.STOCK_ADD, label='New Job')
        self.exposed_widgets['remove_job'] = guitools.vButton(
            stock=gtk.STOCK_REMOVE, label='Delete Job')
        self.exposed_widgets['about_job'] = guitools.vButton(
            stock=gtk.STOCK_INFO, label='About Job')

        for button in self.exposed_widgets.values():
            bar.pack_start(button, False, False, 2)
        return bar

    def tree_selection_change_event(self, tree=None):
        """ Every time the user clicks on the treeview, update the selected
        job id, so that we can remove it or see details about it"""

        model, iter = tree.get_selection().get_selected()
        selrow = model[iter][0]
        self._selectedjob = selrow
        self.Refresh_Toolbar_Status()

    def tree_click_event(self, tree, event, data=None):
        """ Use the tree click event to redirect the calls depending on type of click
        the tree received"""
        self.Refresh_Toolbar_Status
        if event.button == 1 :
            return
        elif event.button == 3:
            if not self._selectedjob:
                return
            # Show the right-click menu
            self.rc_menu.popup(None, None, None, event.button, event.time)

    def Update_TableView(self, data=None):
        """ Triggered by the model to refresh the contents of the GUI
        representing the job queue"""
        self.Refresh_Toolbar_Status()
        model = self.treeview.get_model()
        model.clear()
        for item in data:
            model.append([item[0], item[1], item[2], item[3]])


    def Refresh_Toolbar_Status(self):
        for button in (
            self.exposed_widgets['remove_job'],
            self.exposed_widgets['about_job']):
            button.set_property('sensitive', self._selectedjob is not None)

class HistoryView(QueueView):
    """ History manager tree view """
    def __init__(self):
        QueueView.__init__(self)
        self.banner_label.set_label('Job History')
        self.rc_menu = RCMenu(items=['About Job', 'Rebuild Job',
                                     'View Build Log'])

    def tree_click_event(self, tree, event, data=None):
        """ Events triggered with the treeview is clicked """
        if event.button == 1:
            return
        elif event.button == 3:
            if not self._selectedjob:
                return
            self.rc_menu.popup(None, None, None, event.button, event.time)

    def Refresh_Toolbar_Status(self):
        self.exposed_widgets['about_task'].set_property(
            'sensitive', self._selectedjob is not None)
        self.exposed_widgets['wipe_history'].set_property(
            'sensitive', self.treeview.GetRowCount() > 0)

    def _get_treeview(self):
        _model = gtk.ListStore(int, str, str, str, str, str, str)
        return guitools.TableTree(
            model = _model,
            columns = ['ID',
                       'Application',
                       'Version',
                       'Release',
                       'Build Start',
                       'Build End',
                       'Result']
        )

    def _get_bottom_toolbar(self):
        box = gtk.HBox()
        self.exposed_widgets['about_task'] = guitools.vButton(
            stock = gtk.STOCK_INFO, label = 'About Task')
        self.exposed_widgets['wipe_history'] = guitools.vButton(
            stock = gtk.STOCK_DELETE, label='Delete History')
        filters = []
        self.exposed_widgets['filter_all'] = gtk.RadioButton(
            label='All')
        self.exposed_widgets['filter_successful'] = gtk.RadioButton(
            group = self.exposed_widgets['filter_all'], label='Successful')
        self.exposed_widgets['filter_failed'] = gtk.RadioButton(
            group = self.exposed_widgets['filter_all'], label='Failed')
        lb = gtk.Label('Search history: ')
        self.exposed_widgets['searchbox'] = guitools.SearchBox()

        box.pack_start(self.exposed_widgets['wipe_history'], False, False, 2)
        #box.pack_end(self.exposed_widgets['filter_failed'], False, False, 2)
        #box.pack_end(self.exposed_widgets['filter_successful'], False, False, 2)
        #box.pack_end(self.exposed_widgets['filter_all'], False, False, 2)
        #box.pack_start(lb, False, False, 8)
        #searchbox = guitools.SearchBox()
        #box.pack_start(self.exposed_widgets['searchbox'], True, True, 2)
        box.pack_end(self.exposed_widgets['searchbox'], False, False, 2)
        box.pack_end(lb, False, False, 8)

        return box

    def Update_TableView(self, data=None):
        """ Update the GUI representing the history"""
        model = self.treeview.get_model()
        model.clear()
        for item in data:
            model.append([
                item[0], item[1], item[2], item[3], item[4], item[5], item[6]
            ])
        self.exposed_widgets['wipe_history'].set_property(
            'sensitive', self.treeview.GetRowCount() > 0)
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.