1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2002 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing the Subversion Options Dialog for a new project from the
8repository.
9"""
10
11import os
12
13from PyQt5.QtCore import QDir, pyqtSlot
14from PyQt5.QtWidgets import QDialog, QDialogButtonBox
15
16from E5Gui.E5PathPicker import E5PathPickerModes
17
18from .Ui_SvnNewProjectOptionsDialog import Ui_SvnNewProjectOptionsDialog
19from .Config import ConfigSvnProtocols
20
21import Utilities
22import Preferences
23
24
25class SvnNewProjectOptionsDialog(QDialog, Ui_SvnNewProjectOptionsDialog):
26    """
27    Class implementing the Options Dialog for a new project from the
28    repository.
29    """
30    def __init__(self, vcs, parent=None):
31        """
32        Constructor
33
34        @param vcs reference to the version control object
35        @param parent parent widget (QWidget)
36        """
37        super().__init__(parent)
38        self.setupUi(self)
39
40        self.vcsProjectDirPicker.setMode(E5PathPickerModes.DirectoryMode)
41        self.vcsUrlPicker.setMode(E5PathPickerModes.DirectoryMode)
42
43        self.protocolCombo.addItems(ConfigSvnProtocols)
44
45        hd = Utilities.toNativeSeparators(QDir.homePath())
46        hd = os.path.join(hd, 'subversionroot')
47        self.vcsUrlPicker.setText(hd)
48
49        self.vcs = vcs
50
51        self.localPath = hd
52        self.networkPath = "localhost/"
53        self.localProtocol = True
54
55        ipath = (
56            Preferences.getMultiProject("Workspace") or
57            Utilities.getHomeDir()
58        )
59        self.__initPaths = [
60            Utilities.fromNativeSeparators(ipath),
61            Utilities.fromNativeSeparators(ipath) + "/",
62        ]
63        self.vcsProjectDirPicker.setText(self.__initPaths[0])
64
65        self.resize(self.width(), self.minimumSizeHint().height())
66
67        self.buttonBox.button(
68            QDialogButtonBox.StandardButton.Ok).setEnabled(False)
69
70        msh = self.minimumSizeHint()
71        self.resize(max(self.width(), msh.width()), msh.height())
72
73    @pyqtSlot(str)
74    def on_vcsProjectDirPicker_textChanged(self, txt):
75        """
76        Private slot to handle a change of the project directory.
77
78        @param txt name of the project directory (string)
79        """
80        self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(
81            bool(txt) and
82            Utilities.fromNativeSeparators(txt) not in self.__initPaths)
83
84    @pyqtSlot()
85    def on_vcsUrlPicker_pickerButtonClicked(self):
86        """
87        Private slot to display a repository browser dialog.
88        """
89        from .SvnRepoBrowserDialog import SvnRepoBrowserDialog
90        dlg = SvnRepoBrowserDialog(self.vcs, mode="select", parent=self)
91        dlg.start(
92            self.protocolCombo.currentText() + self.vcsUrlPicker.text())
93        if dlg.exec() == QDialog.DialogCode.Accepted:
94            url = dlg.getSelectedUrl()
95            if url:
96                protocol = url.split("://")[0]
97                path = url.split("://")[1]
98                self.protocolCombo.setCurrentIndex(
99                    self.protocolCombo.findText(protocol + "://"))
100                self.vcsUrlPicker.setText(path)
101
102    def on_layoutCheckBox_toggled(self, checked):
103        """
104        Private slot to handle the change of the layout checkbox.
105
106        @param checked flag indicating the state of the checkbox (boolean)
107        """
108        self.vcsTagLabel.setEnabled(checked)
109        self.vcsTagEdit.setEnabled(checked)
110        if not checked:
111            self.vcsTagEdit.clear()
112
113    @pyqtSlot(int)
114    def on_protocolCombo_activated(self, index):
115        """
116        Private slot to switch the status of the directory selection button.
117
118        @param index index of the selected entry
119        @type int
120        """
121        protocol = self.protocolCombo.itemText(index)
122        if protocol == "file://":
123            self.networkPath = self.vcsUrlPicker.text()
124            self.vcsUrlPicker.setText(self.localPath)
125            self.vcsUrlLabel.setText(self.tr("Pat&h:"))
126            self.localProtocol = True
127            self.vcsUrlPicker.setMode(E5PathPickerModes.DirectoryMode)
128        else:
129            if self.localProtocol:
130                self.localPath = self.vcsUrlPicker.text()
131                self.vcsUrlPicker.setText(self.networkPath)
132                self.vcsUrlLabel.setText(self.tr("&URL:"))
133                self.localProtocol = False
134                self.vcsUrlPicker.setMode(E5PathPickerModes.CustomMode)
135
136    @pyqtSlot(str)
137    def on_vcsUrlPicker_textChanged(self, txt):
138        """
139        Private slot to handle changes of the URL.
140
141        @param txt current text of the line edit (string)
142        """
143        enable = "://" not in txt
144        self.buttonBox.button(
145            QDialogButtonBox.StandardButton.Ok).setEnabled(enable)
146
147    def getData(self):
148        """
149        Public slot to retrieve the data entered into the dialog.
150
151        @return a tuple of a string (project directory) and a dictionary
152            containing the data entered.
153        """
154        scheme = self.protocolCombo.currentText()
155        url = self.vcsUrlPicker.text()
156        vcsdatadict = {
157            "url": '{0}{1}'.format(scheme, url),
158            "tag": self.vcsTagEdit.text(),
159            "standardLayout": self.layoutCheckBox.isChecked(),
160        }
161        return (self.vcsProjectDirPicker.text(), vcsdatadict)
162