# -*- coding: utf-8 -*-

# Copyright (c) 2003 - 2005 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a dialog to show the output of the svn status command process.
"""

import types
import os

from qt import *

from KdeQt import KQMessageBox

from StatusForm import StatusForm

import Preferences

class SvnStatusDialog(StatusForm):
    """
    Module implementing a dialog to show the output of the svn status command process.
    """
    def __init__(self, vcs, parent = None):
        """
        Constructor
        
        @param vcs -- reference to the vcs object
        @param parent -- parent widget (QWidget)
        """
        StatusForm.__init__(self, parent)
        
        self.setWFlags(self.getWFlags() | Qt.WDestructiveClose)
        self.process = QProcess()
        self.vcs = vcs
        
        self.statusList.setSorting(9)
        
        self.menu = QPopupMenu()
        self.commitItem = self.menu.insertItem(self.trUtf8("Commit changes to repository..."),
            self.handleCommit)
        self.menu.setItemEnabled(self.commitItem, 0)
        
        self.connect(self.statusList,SIGNAL('contextMenuRequested(QListViewItem *, const QPoint &, int)'),
                     self.handleContextMenu)
        self.connect(self.process, SIGNAL('readyReadStdout()'),
            self.handleReadStdout)
        self.connect(self.process, SIGNAL('readyReadStderr()'),
            self.handleReadStderr)
        self.connect(self.process, SIGNAL('processExited()'),
            self.handleProcessExited)
            
        self.modifiedIndicators = QStringList()
        self.modifiedIndicators.append(self.trUtf8('added'))
        self.modifiedIndicators.append(self.trUtf8('deleted'))
        self.modifiedIndicators.append(self.trUtf8('modified'))
        
        self.status = {
            ' ' : self.trUtf8('normal'),
            'A' : self.trUtf8('added'),
            'D' : self.trUtf8('deleted'),
            'M' : self.trUtf8('modified'),
            'C' : self.trUtf8('conflict'),
            'X' : self.trUtf8('external'),
            'I' : self.trUtf8('ignored'),
            '?' : self.trUtf8('unversioned'),
            '!' : self.trUtf8('missing'),
            '~' : self.trUtf8('type error')
        }
        self.propStatus = {
            ' ' : self.trUtf8('normal'),
            'M' : self.trUtf8('modified'),
            'C' : self.trUtf8('conflict')
        }
        self.locked = {
            ' ' : self.trUtf8('no'),
            'L' : self.trUtf8('yes')
        }
        self.history = {
            ' ' : self.trUtf8('no'),
            '+' : self.trUtf8('yes')
        }
        self.switched = {
            ' ' : self.trUtf8('no'),
            'S' : self.trUtf8('yes')
        }
        self.uptodate = {
            ' ' : self.trUtf8('yes'),
            '*' : self.trUtf8('no')
        }
        
        self.rx_status = QRegExp('(.{8})\\s+([0-9]+)\\s+([0-9?]+)\\s+([\\w?]+)\\s+(.+)')
        
    def handleContextMenu(self,itm,coord,col):
        """
        Private slot to show the context menu of the listview.
        
        @param itm the selected listview item (QListViewItem)
        @param coord the position of the mouse pointer (QPoint)
        @param col the column of the mouse pointer (int)
        """
        self.menu.popup(coord)
        
    def closeEvent(self, e):
        """
        Private slot implementing a close event handler.
        
        @param e -- close event (QCloseEvent)
        """
        if self.process is not None:
            self.process.tryTerminate()
            QTimer.singleShot(2000, self.process, SLOT('kill()'))
            
        e.accept()
        
    def start(self, fn):
        """
        Public slot to start the svn status command.
        
        @param fn filename(s) to show the status of (string or list of strings)
        """
        self.intercept = 0
        self.args = fn
        
        if self.process:
            self.process.kill()
        else:
            self.process = QProcess()
            self.connect(self.process, SIGNAL('readyReadStdout()'),
                self.handleReadStdout)
            self.connect(self.process, SIGNAL('readyReadStderr()'),
                self.handleReadStderr)
            self.connect(self.process, SIGNAL('processExited()'),
                self.handleProcessExited)
        
        self.process.clearArguments()
        self.process.addArgument('svn')
        self.process.addArgument('status')
        self.vcs.addArguments(self.process, self.vcs.options['global'])
        self.vcs.addArguments(self.process, self.vcs.options['status'])
        self.process.addArgument('--verbose')
        if '--show-updates' in self.vcs.options['status'] or \
           '-u' in self.vcs.options['status']:
            try:
                self.setActiveWindow()
                self.raiseW()
            except:
                pass
        if type(fn) is types.ListType:
            self.dname, fnames = self.vcs.splitPathList(fn)
            self.vcs.addArguments(self.process, fnames)
        else:
            self.dname, fname = self.vcs.splitPath(fn)
            self.process.addArgument(fname)
        self.process.setWorkingDirectory(QDir(self.dname))
        
        self.process.start()
        self.setCaption(self.trUtf8('Subversion Status'))
        
    def finish(self):
        """
        Private slot called when the process finished or the user pressed the button.
        """
        if self.process is not None:
            self.process.tryTerminate()
            QTimer.singleShot(2000, self.process, SLOT('kill()'))
            
        self.cancelButton.setText(self.trUtf8('&OK'))
        self.cancelButton.setDefault(1)
        self.cancelButton.setFocus()
        
        self.inputGroup.setEnabled(0)
        self.refreshButton.setEnabled(1)
        
        self.menu.setItemEnabled(self.commitItem, 1)
        
        self.process = None
        
    def buttonPressed(self):
        """
        Private slot connected to the button clicked signal.
        """
        if self.process is None:
            self.close()
        else:
            self.finish()
            
    def handleProcessExited(self):
        """
        Private slot to handle the processExited signal.
        
        Just call finish().
        """
        self.finish()
        
    def handleReadStdout(self):
        """
        Private slot to handle the readyReadStdout signal.
        
        It reads the output of the process, formats it and inserts it into
        the contents pane.<br />
        <b>Note</b>: If the order gets changed, the handleCommit and
        getModifiedItems method need to be changed accordingly.
        """
        while self.process.canReadLineStdout():
            s = self.process.readLineStdout()
            if self.rx_status.exactMatch(s):
                flags = str(self.rx_status.cap(1))
                rev = self.rx_status.cap(2)
                change = self.rx_status.cap(3)
                author = self.rx_status.cap(4)
                path = self.rx_status.cap(5).stripWhiteSpace()
                itm = QListViewItem(self.statusList,
                    self.status[flags[0]], self.propStatus[flags[1]],
                    self.locked[flags[2]], self.history[flags[3]],
                    self.switched[flags[4]], self.uptodate[flags[5]],
                    "%7s" % str(rev), "%7s" % str(change))
                itm.setText(8, author)
                itm.setText(9, path)
                
    def handleReadStderr(self):
        """
        Private slot to handle the readyReadStderr signal.
        
        It reads the error output of the process and inserts it into the
        error pane.
        """
        if self.process is not None:
            s = unicode(self.process.readStderr())
            self.errors.moveCursor(QTextEdit.MoveEnd, 0)
            self.errors.insert(s)
        
    def passwordMode(self, isOn):
        """
        Private slot to handle the password checkbox toggled.
        
        @param isOn flag indicating the status of the check box (boolean)
        """
        if isOn:
            self.input.setEchoMode(QLineEdit.Password)
        else:
            self.input.setEchoMode(QLineEdit.Normal)
        
    def sendInput(self):
        """
        Private slot to send the input to the subversion process.
        """
        input = self.input.text()
        input.append(os.linesep)
        
        self.errors.moveCursor(QTextEdit.MoveEnd, 0)
        if self.passwordCheckBox.isChecked():
            self.errors.insert(os.linesep)
        else:
            self.errors.insert(input)
        
        self.proc.writeToStdin(input)
        
        self.passwordCheckBox.setChecked(0)
        self.input.clear()
        
    def returnPressed(self):
        """
        Private slot to handle the press of the return key in the input field.
        """
        self.intercept = 1
        self.sendInput()
        
    def keyPressEvent(self, evt):
        """
        Protected slot to handle a key press event.
        
        @param evt the key press event (QKeyEvent)
        """
        if self.intercept:
            self.intercept = 0
            evt.accept()
            return
        StatusForm.keyPressEvent(self, evt)
        
    def handleCommit(self):
        """
        Private slot to handle the Commit context menu entry.
        """
        names = [os.path.join(self.dname, unicode(itm.text(9))) \
                 for itm in self.getModifiedItems()]
        if not names:
            KQMessageBox.information(self,
                self.trUtf8("Commit"),
                self.trUtf8("""There are no uncommitted changes available/selected."""),
                self.trUtf8("&OK"),
                QString.null,
                QString.null,
                0, -1)
            return
        
        if Preferences.getVCS("AutoSaveFiles"):
            vm = qApp.mainWidget().getViewManager()
            for name in names:
                vm.saveEditor(name)
        self.vcs.vcsCommit(names, '', parent=self)
        self.refreshStatus()
        
    def getModifiedItems(self):
        """
        Private method to retrieve all emtries, that have a modified status.
        
        @return list of all items with a modified status
        """
        modifiedItems = []
        itm = self.statusList.firstChild()
        while itm:
            if itm.isSelected() and \
                    (self.modifiedIndicators.contains(itm.text(0)) or \
                     self.modifiedIndicators.contains(itm.text(1))):
                modifiedItems.append(itm)
            itm = itm.itemBelow()
        return modifiedItems

    def refreshStatus(self):
        """
        Private slot to refresh the status display.
        """
        self.cancelButton.setText(self.trUtf8('&Cancel'))
        self.cancelButton.setDefault(1)
        self.cancelButton.setFocus()
        
        self.inputGroup.setEnabled(1)
        self.refreshButton.setEnabled(0)
        
        self.menu.setItemEnabled(self.commitItem, 0)
        
        self.statusList.clear()
        
        self.start(self.args)
