vpackager / src / vpackager / jobmanage.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 RMenu(gtk.Menu):
    """Menu that pops up when the right button is clicked """
    def __init__(self, selected=None, parent=None):
        gtk.Menu.__init__(self)
        self.selected = selected
        self._parent = parent
        item_delete = gtk.MenuItem('Remove')
        item_delete.connect('activate', self._item_delete_event)
        item_delete.show()
        item_details = gtk.MenuItem('Details')
        item_details.connect('activate',
                self._item_details_event)
        item_details.show()
        self.append(item_delete)
        self.append(item_details)

    def _item_delete_event(self, widget=None):
        """ Removes a job from que queue using the right-click menu"""
        return self._parent._btDeleteClick(widget)

    def _item_details_event(self, widget=None):
        """ Triggers display of job details from the right-click menu"""
        return self._parent._job_details_event(widget)


class Main(gtk.VBox):
    '''job manager main container'''
    def __init__(self, dbase=None):
        gtk.VBox.__init__(self)
        if dbase:
            self.dbase = dbase
        else:
            self.dbase = dbutils.dbase()

        hbtop = gtk.HBox()
        self.pack_start(hbtop, False, True, 4)
        lbtop = gtk.Label('Manage job queue')
        hbtop.pack_start(lbtop, True, True, 2)
        scrl = gtk.ScrolledWindow()
        scrl.set_policy(gtk.POLICY_AUTOMATIC,
                gtk.POLICY_AUTOMATIC)
        scrl.add(self._get_tree())
        self._fill_tree_with_queue()
        self.pack_start(scrl, True, True, 8)
        hbbot = gtk.HBox()
        self._selected_jobid = None
        self.pack_start(hbbot, False, True, 4)
        self.btAdd = gtk.Button(stock=gtk.STOCK_ADD)
        self.btRem = gtk.Button(stock=gtk.STOCK_REMOVE)
        self.btRem.connect('clicked', self._btDeleteClick)
        self.btAdd.connect('clicked', self._btAddClick)
        btJobDetails = guitools.vButton(stock=gtk.STOCK_INFO,
                label='Details')
        self.btJobDetails = btJobDetails
        btJobDetails.connect('clicked', self._job_details_event)
        frmJobCtrl = gtk.Frame('Job Control')
        jobsvbox = gtk.HBox()
        frmJobCtrl.add(jobsvbox)
        jobsvbox.pack_start(self.btAdd, False, False, 2)
        jobsvbox.pack_start(self.btRem, False, False, 2)
        jobsvbox.pack_start(btJobDetails, False, False, 2)
        hbbot.pack_start(frmJobCtrl, True, True , 4)
        self.btJobDetails.set_sensitive(
                self._selected_jobid != None)
        self.btRem.set_sensitive(
                self._selected_jobid != None)


    def _job_details_event(self, widget=None):
        ''' Pop-up the job info dialog'''
        dia = guitools.AboutJob(dbase = self.dbase,
                jobid = self._selected_jobid)
        if dia.run():
            dia.destroy()

    def _btAddClick(self, widget=None):
        ''' Launch a dialog to select the tarball for this job'''
        dia = guitools.JobAddDialog(parent=self)
        res = dia.run()
        dia.hide()
        if res == gtk.RESPONSE_OK:
            app = dia.app
            ver = dia.ver
            desc = dia.DescURI
            srcuri = dia.SrcURI
            rel = dia.release
            srctype = dia.srctype
            toinstall = dia.install
            patches = ','.join(dia.patches)
            dia.destroy()
            self.dbase.NewJob(app, ver, scruri, desc,
                    rel, srctype, toinstall, patches)
            return self._update_queue()

    def _btDeleteClick(self, widget=None):
        '''Delete the selected job from the job queue'''
        if self._selected_jobid:
            self.dbase.DeleteJob(self._selected_jobid)
            self._update_queue()

    def _update_queue(self):
        '''Refresh the GUI displaying the job queue'''
        self.tree.set_model(gtk.ListStore(int,str, str, str))
        lst = self.dbase.GetQueue()
        for entry in lst:
            self.tree.get_model().append(entry)

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

    def _get_tree(self):
        mod = gtk.ListStore(int, str, str, str)
        self.tree = gtk.TreeView(model = mod)
        self.tree.connect('cursor-changed', self._tree_click_event)
        self.tree.connect('button-press-event', self._tree_button_press)

        i = 0
        for field in ('ID', 'Application', 'Version',
                'Release'):
            col = gtk.TreeViewColumn(field)
            col.set_resizable(True)
            cr = gtk.CellRendererText()
            col.pack_start(cr)
            col.add_attribute(cr, 'text', i)
            self.tree.append_column(col)
            i += 1


        return self.tree

    def _tree_click_event(self, tree=None):
        sel = tree.get_selection()
        (model, iter) = sel.get_selected()
        selrow = tree.get_model()[iter][0]
        self._selected_jobid = selrow
        self.btJobDetails.set_property('sensitive',
                self._selected_jobid != None)
        self.btRem.set_property('sensitive',
                self._selected_jobid != None)


    def _fill_tree_with_queue(self):
        tm = self.tree.get_model()
        q = self.dbase.GetQueue()
        for item in q:
            tm.append(item)

    def _exit(self, widget=None):
        try:
            self.dbase.close()
        except:
            pass
        gtk.main_quit()

if __name__ == '__main__':
    w = gtk.Window(gtk.WINDOW_TOPLEVEL)
    content = Main()
    w.connect('destroy', content._exit)
    w.set_title('Job Queue Manager')
    w.set_size_request(600, 400)
    #content = Main()
    w.add(content)
    w.show_all()
    gtk.gdk.threads_init()
    gtk.gdk.threads_enter()
    gtk.main()
    gtk.gdk.threads_leave()
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.