vpackager / src / vpackager / guitools.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 os
import buildutils
import message

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

class About(gtk.AboutDialog):
    """ About dialog for vpackager"""
    def __init__(self):
        gtk.AboutDialog.__init__(self)
        self.set_name('vpackager')
        self.set_version('2.0')
        self.set_authors(['Moises Henriquez (M0E-Lnx)',
            'Rodrigo Bistolfi (rbistolfi)'])
        f = open('data/LICENSE', 'r')
        self.set_license(f.read())
        f.close()
        self.set_website('https://bitbucket.org/m0e_lnx/vpackager')
        self.set_copyright('(c) 2008-2011 Moises Henriquez')
        logo = gtk.gdk.pixbuf_new_from_file('data/vpackager_logo.png')
        self.set_logo(logo)
        self.set_comments("Compile software from source code.")

class vButton(gtk.Button):
    '''Button class that can use a stock image with a custon label'''
    def __init__(self, stock=None, label=''):
        gtk.Button.__init__(self)
        if stock is not None:
            img = gtk.Image()
            img.set_from_stock(stock, gtk.ICON_SIZE_BUTTON)
            self.set_image(img)

        self.set_label(label)

class JobAddDialog(gtk.Dialog):
    '''Dialog window used to add a job to the queue'''
    def __init__(self, title='Add Job', parent=None):
        gtk.Dialog.__init__(self)
        self.SrcURI = None
        self.DescURI = None
        self.app = None
        self.ver = None
        self.release = 1
        self.install = False
        self.add_button(gtk.STOCK_OK, gtk.RESPONSE_OK)
        self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
        self.set_has_separator(True)
        self.set_default_response(gtk.RESPONSE_OK)
        body = self.get_content_area()
        self.set_title('Add New Job')
        self.set_size_request(500,300)
        frmSrc = gtk.Frame('Source Archive')
        body.pack_start(frmSrc, False, True, 8)
        hsrc = gtk.HBox()
        frmSrc.add(hsrc)
        self.srcentry = gtk.Entry()
        self.srcentry.set_tooltip_text('Enter path or URL')
        self.srcentry.connect('changed', self._src_entry_event)
        hsrc.pack_start(self.srcentry, True, True, 2)
        self.btBrowse = vButton(stock=gtk.STOCK_OPEN,
                label='Browse')
        self.btBrowse.connect('clicked', self._browse)
        hsrc.pack_start(self.btBrowse, False, False, 2)
        self.okButton = self.get_action_area().get_children()[1]
        self.okButton.set_property('sensitive', False)

        frmDesc = gtk.Frame('Package Description')
        body.pack_start(frmDesc, False, True, 4)
        hbdesc = gtk.HBox()
        frmDesc.add(hbdesc)
        self.descEntry = gtk.Entry()
        self.descEntry.set_tooltip_text('Enter path or URL')
        self.descEntry.connect('changed', self._descURI_change)
        btDescBrowse = vButton(stock=gtk.STOCK_OPEN, label='Browse')
        btDescBrowse.connect('clicked', self._desc_browse)
        hbdesc.pack_start(self.descEntry, True, True, 2)
        hbdesc.pack_start(btDescBrowse, False, True, 2)

        lb = gtk.Label('Application')
        self.appEntry = gtk.Entry()
        bapp = gtk.HBox()
        body.pack_start(bapp, False, True, 8)
        bapp.pack_start(lb, False, False, 2)
        bapp.pack_start(self.appEntry, False, False, 2)

        lbv = gtk.Label('Version')
        self.verEntry = gtk.Entry()
        bapp.pack_start(lbv, False, False, 2)
        bapp.pack_start(self.verEntry, False, False, 2)

        self.appEntry.connect('changed', self._appname_changed)
        self.verEntry.connect('changed', self._appver_changed)

        bbox = gtk.HBox()
        lbuild = gtk.Label('Release')
        rels = gtk.ListStore(int)
        for x in xrange(1, 5):
            rels.append([x])
        cbRel = gtk.ComboBox(rels)
        cr = gtk.CellRendererText()
        cbRel.pack_start(cr)
        cbRel.add_attribute(cr, 'text', 0)
        cbRel.set_active(0)

        lSrcType = gtk.Label('Source Type')
        types = gtk.ListStore(str)
        for _type in ('Auto','Autotools','Python','CMake','Perl','Waf'):
            types.append([_type])
        cbsrcsel = gtk.ComboBox(types)
        cbsrcsel.connect('changed', self._srctype_changed)
        crt = gtk.CellRendererText()
        cbsrcsel.pack_start(crt)
        cbsrcsel.add_attribute(crt, 'text', 0)
        cbsrcsel.set_active(0)

        bbox.pack_start(lbuild, False, False, 2)
        bbox.pack_start(cbRel, False, False, 2)
        bbox.pack_start(lSrcType, False, False, 2)
        bbox.pack_start(cbsrcsel, False, False, 2)
        body.pack_start(bbox, False, True, 2)
        cbInstallPkg = gtk.CheckButton(
                label='Install this package after it is compiled')
        cbInstallPkg.connect('toggled', self._cbInstallpkg_changed)
        body.pack_start(cbInstallPkg, False, True, 2)


        cbRel.connect('changed', self._release_changed)

        self.body = body
        body.show_all()

    def _cbInstallpkg_changed(self, widget=None):
        self.install = widget.get_active()

    def _srctype_changed(self, widget=None):
        selected = widget.get_model()[widget.get_active()][0].lower()
        if selected in ('auto', 'autotools'):
            self.srctype = 'default'
        else:
            self.srctype = selected

    def _release_changed(self, widget=None):
        self.release = widget.get_model()[widget.get_active()][0]

    def _appname_changed(self, widget=None):
        self.app = widget.get_text()
        self.okButton.set_property('sensitive',
                self.verEntry.get_text() != '')
        return

    def _appver_changed(self, widget=None):
        self.ver = widget.get_text()
        self.okButton.set_property('sensitive',
                self.appEntry.get_text() != '')
        return

    def _descURI_change(self, widget=None):
        self.DescURI = widget.get_text()

    def _desc_browse(self, widget=None):
        dia = gtk.FileChooserDialog(buttons=(gtk.STOCK_OK,
            gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
        dia.set_title('Select description file')
        flt = gtk.FileFilter()
        flt.add_mime_type('text/plain')
        flt.set_name('Text files')
        dia.add_filter(flt)
        res = dia.run()
        dia.hide()
        if res == gtk.RESPONSE_OK:
            self.descEntry.set_text(dia.get_filename())
        dia.destroy()


    def _browse(self, widget=None):
        dia = gtk.FileChooserDialog(buttons=(gtk.STOCK_OK,
            gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
        dia.set_title('Choose source archive')
        flt = gtk.FileFilter()
        flt.add_pattern('*.[bgx]z*')
        flt.set_name('Compressed Files')
        dia.add_filter(flt)
        res = dia.run()
        dia.hide()
        if res == gtk.RESPONSE_OK:
            self.SrcURI = dia.get_filename()
        dia.destroy()
        if self.SrcURI.startswith('/'):
            src = buildutils.Source(self.SrcURI)
            if src.builder is not None:
                self.appEntry.set_text(src.app)
                self.verEntry.set_text(src.ver)
                del src
            else:
                dia = message.Error(parent = self,
                        text='Invalid source type')
                if dia.run():
                    dia.destroy()


        self.srcentry.set_text(self.SrcURI)
        return


    def _src_entry_event(self, widget=None):
        starts = ('http:', 'https:','ftp:')
        ends = ('.tar.gz', '.xz', '.tar.bz2', '.tgz')
        uri = widget.get_text()
        enable = False
        for i in starts:
            if uri.startswith(i):
                #enable = True
                #self.okButton.set_property('sensitive', enable)
                self.SrcURI = widget.get_text()
                return
        for x in ends:
            if uri.endswith(x):
                if os.path.exists(uri):
                    enable = True
                    self.okButton.set_property('sensitive', enable)
                    self.SrcURI = widget.get_text()
                    return
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.