vlmaddon / view / dialog.py

#!/usr/bin/env python

#    This file is part of the vectorlinux multimedia bonus disc.
#
#    This file 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.
#
#    This file 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 this file.  If not, see <http://www.gnu.org/licenses/>.

import gtk
import time
import threading
import sys
import os
import message

try:
    sys.path.append(os.path.join(os.getcwd(),'..'))
except:
    pass

try:
    from utils import vl
except:
    pass


__author__ = 'Moises Henriquez'
__author_email__ = 'moc.liamg@xnl.E0M'[::-1]


def _(str):
    return str

class InstallWindow(gtk.Dialog):
    def __init__(self, parent = None, pkglist = [], slaptconfig = None):
        gtk.Dialog.__init__(self, parent = parent)
        self.pkglist = pkglist
        self.set_title(_('Installing Packages'))
        if parent is None:
            self.set_position(gtk.WIN_POS_CENTER)

        content = self.get_content_area()
        action = self.get_action_area()

        banner = gtk.Label(
                _('Please wait while the following tasks complete')
                )
        self.banner = banner
        blank = gtk.Label()
        self.progress = gtk.ProgressBar()
        self.status = gtk.Label()

        for item in (banner, blank, self.status, self.progress):
            content.pack_start(item, False, False, 4)

        btnCancel = gtk.Button(stock = gtk.STOCK_CANCEL)
        btnCancel.connect('clicked', self.exit)
        self.btCancel = btnCancel

        action.pack_start(btnCancel, False, False, 4)
        self.stopbar = False
        self.monitor = threading.Thread(target = self.move)
        self.resize(400, 150)
        self.current = ""
        self.failed_installs = []

        self.show_all()
        self._icurrent = 0.0
        self.packagemanager = vl.PackageManager(
                configfile = slaptconfig.name)

    def change_cancel_button(self):
        """ Change the cancel button to read close, 
        and also change its icon.
        This is to be triggered when the install process
        is finished successful or not"""
        wid = self.btCancel
        wid.set_use_stock(False)
        img = gtk.Image()
        img.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_BUTTON)
        wid.set_property('label', _('Close'))
        wid.set_image(img)
        return

    def run(self):
        """ Append our start_process() method to the dialog's
        .run method"""
        self.start_process()
#        return gtk.Dialog.run(self)

    def start_process(self, widget = None):
        """ Start installation process"""
        self.stopbar = False
        self.monitor.start()
        ins = threading.Thread(target = self.installation, args=(self.pkglist,))
        ins.start()


    def installation(self, pkgs):
        """ Installation method. This method is not to be called
        directly, but rather inside a thread"""
        # First, update the cache:
        self._icurrent = -1.0
        gtk.gdk.threads_enter()
        self.status.set_property('label',
                _('Updating Package Cache')
                )
        gtk.gdk.threads_leave()
        i = self.packagemanager.update_cache()

        # FIXME: ^^ Error checking ??? ^^
       # ====== Brief error checking ======
        if i[1] > 0:
            # Something went wrong during cache update
            # FIXME: Error out at this point or proceed? 
            pass

        self._icurrent = 0.0
        for pkg in self.pkglist:
            self.current = pkg
            gtk.gdk.threads_enter()
            self.status.set_property('label', 'Installing %s'% pkg)
            gtk.gdk.threads_leave()
            # FIXME: Instead of this wait time, call the slapt-get
            # install method to install the package and process the
            # output.
            ret = self.packagemanager.install_package(pkg)

            # === Brief Error Checking ========
            if ret[1] > 0 or 'unmet dependencies' in ret[0][1]:
                self.failed_installs.append((pkg, ret[0][1]))
            self._icurrent += 1

        # When done, stop the monitor thread
        self.stopbar = True
        # Change the cancel button to 'Close'
        time.sleep(0.5)
        if len(self.failed_installs) > 0:
            dia = message.FailedPackagesDialog(
                    self.failed_installs)
            gtk.gdk.threads_enter()
            if dia.run():
                dia.destroy()
            gtk.gdk.threads_leave()
        else:
            gtk.gdk.threads_enter()
            self.change_cancel_button()
            gtk.gdk.threads_leave()

    def exit(self, widget=None):
        """ Do a clean exit when closing the dialog
        and returning to the main gui"""
        self.stopbar = True
        time.sleep(0.25)
        self.destroy()

    def move(self):
        """ Move the progressbar according to the progress made"""
        val = 0.0
        while not self.stopbar:
            time.sleep(0.1)
            gtk.gdk.threads_enter()
            self.progress.pulse()
            gtk.gdk.threads_leave()

#            gtk.gdk.threads_enter()
#            self.progress.pulse()
#            gtk.threads_leave()
#            time.sleep(0.25)
#            time.sleep(0.125)
#            while self._icurrent == -1.0:
                #                gtk.gdk.threads_enter()
#                self.progress.set_text(_('Updating Cache'))
#                self.progress.pulse()
#                gtk.gdk.threads_leave()
#                time.sleep(0.25)
#                val = float(float(self._icurrent) / float(len(self.pkglist)))
#                valstr = str(int(val) * 100)
#                gtk.gdk.threads_enter()
#                self.progress.pulse()
#                self.progress.set_fraction(val)
#                gtk.gdk.threads_leave()
#            self.progress.set_fraction(val)
#            gtk.gdk.threads_leave()
#            time.sleep(0.125)

        time.sleep(0.5)


        gtk.gdk.threads_enter()
        self.progress.set_text('%i'% (val * 100) + ' %')
        if len(self.failed_installs) > 0:
            self.banner.set_property('label',
                    _('Install process complete'))
            self.progress.set_text('')
            self.status.set_property('label',
                    _('WARNING: Some packages failed to install.'))
        else:
            self.banner.set_property('label',
                    _('Installation complete'))
            self.progress.set_text('100 %')
            self.status.set_property('label',
                    _('All packages installed successfully'))
        time.sleep(0.125)
        self.progress.set_fraction(1.0)
        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.