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

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

"""
Module implementing a dialog to enter options used to start a project in the VCS.
"""

import os

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from KdeQt import KQFileDialog

from E4Gui.E4Completers import E4DirCompleter

from Ui_SvnOptionsDialog import Ui_SvnOptionsDialog
from Config import ConfigSvnProtocols

import Utilities

class SvnOptionsDialog(QDialog, Ui_SvnOptionsDialog):
    """
    Class implementing a dialog to enter options used to start a project in the
    repository.
    """
    def __init__(self, project, parent = None):
        """
        Constructor
        
        @param project reference to the project object
        @param parent parent widget (QWidget)
        """
        QDialog.__init__(self, parent)
        self.setupUi(self)
        
        self.vcsDirectoryCompleter = E4DirCompleter(self.vcsDirectoryEdit)
        
        self.project = project
        
        self.protocolCombo.addItems(ConfigSvnProtocols)
        
        hd = Utilities.toNativeSeparators(QDir.homePath())
        hd = os.path.join(unicode(hd), 'subversionroot')
        self.vcsDirectoryEdit.setText(hd)
        
        self.localPath = unicode(hd)
        self.networkPath = "localhost/"
        self.localProtocol = True
        
    @pyqtSignature("")
    def on_vcsDirButton_clicked(self):
        """
        Private slot to display a directory selection dialog.
        """
        directory = KQFileDialog.getExistingDirectory(\
            self,
            self.trUtf8("Select Repository-Directory"),
            self.vcsDirectoryEdit.text(),
            QFileDialog.Options(QFileDialog.ShowDirsOnly))
        
        if not directory.isEmpty():
            self.vcsDirectoryEdit.setText(Utilities.toNativeSeparators(directory))
        
    @pyqtSignature("QString")
    def on_protocolCombo_activated(self, protocol):
        """
        Private slot to switch the status of the directory selection button.
        """
        if str(protocol) == "file://":
            self.vcsDirButton.setEnabled(True)
            self.networkPath = unicode(self.vcsDirectoryEdit.text())
            self.vcsDirectoryEdit.setText(self.localPath)
            self.localProtocol = True
        else:
            self.vcsDirButton.setEnabled(False)
            if self.localProtocol:
                self.localPath = unicode(self.vcsDirectoryEdit.text())
                self.vcsDirectoryEdit.setText(self.networkPath)
                self.localProtocol = False
        
    def getData(self):
        """
        Public slot to retrieve the data entered into the dialog.
        
        @return a dictionary containing the data entered
        """
        vcsdatadict = {
            "url" : '%s%s' % (str(self.protocolCombo.currentText()),
                              unicode(self.vcsDirectoryEdit.text())),
            "message" : unicode(self.vcsLogEdit.text()),
            "standardLayout" : self.layoutCheckBox.isChecked(),
        }
        return vcsdatadict
