1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2010 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing the Mercurial Options Dialog for a new project from the
8repository.
9"""
10
11from PyQt5.QtCore import pyqtSlot, QUrl
12from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QComboBox
13
14from E5Gui.E5PathPicker import E5PathPickerModes
15
16from .Ui_HgNewProjectOptionsDialog import Ui_HgNewProjectOptionsDialog
17from .Config import ConfigHgSchemes
18
19import Utilities
20import Preferences
21import UI.PixmapCache
22
23
24class HgNewProjectOptionsDialog(QDialog, Ui_HgNewProjectOptionsDialog):
25    """
26    Class implementing the Options Dialog for a new project from the
27    repository.
28    """
29    def __init__(self, vcs, parent=None):
30        """
31        Constructor
32
33        @param vcs reference to the version control object
34        @param parent parent widget (QWidget)
35        """
36        super().__init__(parent)
37        self.setupUi(self)
38
39        self.vcsProjectDirPicker.setMode(E5PathPickerModes.DirectoryMode)
40
41        self.__vcs = vcs
42
43        vcsUrlHistory = self.__vcs.getPlugin().getPreferences(
44            "RepositoryUrlHistory")
45        self.vcsUrlPicker.setMode(E5PathPickerModes.DirectoryMode)
46        self.vcsUrlPicker.setInsertPolicy(QComboBox.InsertPolicy.InsertAtTop)
47        self.vcsUrlPicker.setSizeAdjustPolicy(
48            QComboBox.SizeAdjustPolicy.AdjustToMinimumContentsLengthWithIcon)
49        self.vcsUrlPicker.setPathsList(vcsUrlHistory)
50        self.vcsUrlClearHistoryButton.setIcon(
51            UI.PixmapCache.getIcon("editDelete"))
52        self.vcsUrlPicker.setText("")
53
54        ipath = (
55            Preferences.getMultiProject("Workspace") or
56            Utilities.getHomeDir()
57        )
58        self.__initPaths = [
59            Utilities.fromNativeSeparators(ipath),
60            Utilities.fromNativeSeparators(ipath) + "/",
61        ]
62        self.vcsProjectDirPicker.setText(self.__initPaths[0])
63
64        self.lfNoteLabel.setVisible(
65            self.__vcs.isExtensionActive("largefiles"))
66        self.largeCheckBox.setVisible(
67            self.__vcs.isExtensionActive("largefiles"))
68
69        self.buttonBox.button(
70            QDialogButtonBox.StandardButton.Ok).setEnabled(False)
71
72        msh = self.minimumSizeHint()
73        self.resize(max(self.width(), msh.width()), msh.height())
74
75    @pyqtSlot(str)
76    def on_vcsProjectDirPicker_textChanged(self, txt):
77        """
78        Private slot to handle a change of the project directory.
79
80        @param txt name of the project directory (string)
81        """
82        self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(
83            bool(txt) and
84            Utilities.fromNativeSeparators(txt) not in self.__initPaths)
85
86    @pyqtSlot(str)
87    def on_vcsUrlPicker_textChanged(self, txt):
88        """
89        Private slot to handle changes of the URL.
90
91        @param txt current text of the line edit (string)
92        """
93        url = QUrl.fromUserInput(txt)
94        enable = url.isValid() and url.scheme() in ConfigHgSchemes
95        self.buttonBox.button(
96            QDialogButtonBox.StandardButton.Ok).setEnabled(enable)
97
98        self.vcsUrlPicker.setPickerEnabled(url.scheme() == "file" or
99                                           len(txt) == 0)
100
101    @pyqtSlot()
102    def on_vcsUrlClearHistoryButton_clicked(self):
103        """
104        Private slot to clear the history of entered repository URLs.
105        """
106        currentVcsUrl = self.vcsUrlPicker.text()
107        self.vcsUrlPicker.clear()
108        self.vcsUrlPicker.setText(currentVcsUrl)
109
110        self.__saveHistory()
111
112    def getData(self):
113        """
114        Public slot to retrieve the data entered into the dialog and to
115        save the history of entered repository URLs.
116
117        @return a tuple of a string (project directory) and a dictionary
118            containing the data entered.
119        """
120        self.__saveHistory()
121
122        url = QUrl.fromUserInput(self.vcsUrlPicker.text().replace("\\", "/"))
123        vcsdatadict = {
124            "url": url.toString(QUrl.UrlFormattingOption.None_),
125            "revision": self.vcsRevisionEdit.text(),
126            "largefiles": self.largeCheckBox.isChecked(),
127        }
128        return (self.vcsProjectDirPicker.text(), vcsdatadict)
129
130    def __saveHistory(self):
131        """
132        Private method to save the repository URL history.
133        """
134        url = self.vcsUrlPicker.text()
135        vcsUrlHistory = self.vcsUrlPicker.getPathItems()
136        if url not in vcsUrlHistory:
137            vcsUrlHistory.insert(0, url)
138
139        # max. list sizes is hard coded to 20 entries
140        newVcsUrlHistory = [url for url in vcsUrlHistory if url]
141        if len(newVcsUrlHistory) > 20:
142            newVcsUrlHistory = newVcsUrlHistory[:20]
143
144        self.__vcs.getPlugin().setPreferences(
145            "RepositoryUrlHistory", newVcsUrlHistory)
146