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

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

"""
Module implementing the user specific project properties dialog.
"""

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

import Preferences

from Ui_UserPropertiesDialog import Ui_UserPropertiesDialog

class UserPropertiesDialog(QDialog, Ui_UserPropertiesDialog):
    """
    Class implementing the user specific project properties dialog.
    """
    def __init__(self, project, parent = None, name = None):
        """
        Constructor
        
        @param project reference to the project object
        @param parent parent widget of this dialog (QWidget)
        @param name name of this dialog (string or QString)
        """
        QDialog.__init__(self, parent)
        if name:
            self.setObjectName(name)
        self.setupUi(self)
        
        self.project = project
        
        if self.project.pudata["VCSSTATUSMONITORINTERVAL"]:
            self.vcsStatusMonitorIntervalSpinBox.setValue(
                self.project.pudata["VCSSTATUSMONITORINTERVAL"][0])
        else:
            self.vcsStatusMonitorIntervalSpinBox.setValue(
                Preferences.getVCS("StatusMonitorInterval"))
        if self.project.pdata["VCS"]:
            if self.project.pdata["VCS"][0] in ["PySvn", "Subversion"]:
                self.svnGroup.setEnabled(True)
            else:
                self.svnGroup.setEnabled(False)
        else:
            self.svnGroup.setEnabled(False)
        if self.project.pudata["VCSOVERRIDE"]:
            if self.project.pudata["VCSOVERRIDE"][0] in ["PySvn", "Subversion"]:
                self.svnInterfaceGroup.setEnabled(True)
                if self.project.pudata["VCSOVERRIDE"][0] == "PySvn":
                    try:
                        import pysvn
                        self.pysvnButton.setChecked(True)
                    except ImportError:
                        self.svnButton.setChecked(True)
                else:
                    self.svnButton.setChecked(True)
            else:
                self.svnInterfaceGroup.setEnabled(False)
        else:
            if self.project.pdata["VCS"]:
                if self.project.pdata["VCS"][0] in ["PySvn", "Subversion"]:
                    self.svnInterfaceGroup.setEnabled(True)
                    if self.project.pdata["VCS"][0] == "PySvn":
                        try:
                            import pysvn
                            self.pysvnButton.setChecked(True)
                        except ImportError:
                            self.svnButton.setChecked(True)
                    else:
                        self.svnButton.setChecked(True)
                else:
                    self.svnInterfaceGroup.setEnabled(False)

    def storeData(self):
        """
        Public method to store the entered/modified data.
        """
        vcsStatusMonitorInterval = self.vcsStatusMonitorIntervalSpinBox.value()
        if vcsStatusMonitorInterval != Preferences.getVCS("StatusMonitorInterval"):
            self.project.pudata["VCSSTATUSMONITORINTERVAL"] = [vcsStatusMonitorInterval]
        else:
            self.project.pudata["VCSSTATUSMONITORINTERVAL"] = []
        
        if self.svnGroup.isEnabled() and self.svnInterfaceGroup.isEnabled():
            if self.pysvnButton.isChecked():
                vcsSystem = "PySvn"
            else:
                vcsSystem = "Subversion"
            if self.svnInterfaceDefaultCheckBox.isChecked():
                if vcsSystem != self.project.pdata["VCS"][0]:
                    self.project.pdata["VCS"] = [vcsSystem]
                    self.project.pudata["VCSOVERRIDE"] = []
                    self.project.setDirty(True)
            else:
                if vcsSystem != self.project.pdata["VCS"][0]:
                    self.project.pudata["VCSOVERRIDE"] = [vcsSystem]
                else:
                    self.project.pudata["VCSOVERRIDE"] = []
