#!/usr/bin/env python
###########################################################################
# commandrunner.py - description                                          #
# ------------------------------                                          #
# begin     : Sun Feb  3 2008                                             #
# copyright : (C) 2008 by Simon Edwards                                   #
# email     : simon@simonzone.com                                         #
#                                                                         #
###########################################################################
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 2 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
###########################################################################

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.kdecore import *
from PyKDE4.kdeui import *

import sys

class CommandRunner(KDialog):
    def __init__(self, parent):
        KDialog.__init__(self,parent)

        self.top = KVBox()
        hbox = KHBox(self.top)
        icon = QLabel(hbox)
        icon.setPixmap(KIconLoader.global_().loadIcon("dialog-warning",KIconLoader.Desktop,KIconLoader.SizeMedium))
        icon.setMinimumSize(16,16)
        icon.setSizePolicy(QSizePolicy(QSizePolicy.Minimum,QSizePolicy.Minimum))
        self.label = QLabel(hbox)
        hbox.setStretchFactor(self.label,1)
        
        self.text_widget = KTextEdit(self.top)
        self.text_widget.setReadOnly(True)
        self.setMainWidget(self.top)
        self.setButtons(KDialog.ButtonCode(KDialog.Close))
        self.process = KProcess()
        self.process.setOutputChannelMode(KProcess.MergedChannels)
        self.connect(self.process,SIGNAL("readyRead()"),self.slotReadyRead)
        self.connect(self.process,SIGNAL("finished(int,QProcess::ExitStatus)"),self.slotFinished)
        
    def setHeading(self,heading):
        self.label.setText(heading)
    
    def run(self,cmd):
        """Run the given shell command line.
        
        """
        self.process.setShellCommand(cmd)
        self.text_widget.setPlainText("")
        self.enableButton(KDialog.Close, False)
        self.boot_strap = self.startTimer(0)
        self.exec_()
        
    def slotReadyRead(self):
        line = self.process.readLine()
        while line!="":
            self.text_widget.insertPlainText(QString(line))
            line = self.process.readLine()
        self.text_widget.ensureCursorVisible()
        
    def slotFinished(self,exitCore,exitStatus):
        self.enableButton(KDialog.Close, True)
        
    def timerEvent(self,event):
        if event.timerId()==self.boot_strap:
            self.process.start()
            self.boot_strap = None
        else:
            KDialog.timerEvent(self,event)

# Test
def main():
    global app
    
    appName     = "commandrunner"
    catalog     = "command runner"
    programName = ki18n("Command runner")
    version     = "1.0"
    description = ki18n ("Advanced Networking utility")
    license     = KAboutData.License_GPL
    copyright   = ki18n ("(c) 2008 Simon Edwards")
    text        = ki18n ("Test for the Command runner class")
    homePage    = "http://www.simonzone.com/software/guidedog/"
    bugEmail    = "simon@simonzone.com"
    aboutData   = KAboutData (appName, catalog, programName, version, description,
                                license, copyright, text, homePage, bugEmail)
    
    KCmdLineArgs.init(sys.argv, aboutData)
    KCmdLineArgs.StdCmdLineArgs()
    app = KApplication()
    cr = CommandRunner(None)
    cr.setHeading("Testing the command runner")
    cr.run("/bin/ls /")

if __name__=="__main__":
    main()
