eric6/Plugins/VcsPlugins/vcsMercurial/HgUserConfigHostFingerprintDialog.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2016 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to edit a host fingerprint.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import pyqtSlot
13 from PyQt5.QtWidgets import QDialog, QDialogButtonBox
14
15 from .Ui_HgUserConfigHostFingerprintDialog import \
16 Ui_HgUserConfigHostFingerprintDialog
17
18
19 class HgUserConfigHostFingerprintDialog(
20 QDialog, Ui_HgUserConfigHostFingerprintDialog):
21 """
22 Class implementing a dialog to edit a host fingerprint.
23 """
24 supportedHashes = ("sha1", "sha256", "sha512")
25 fingerprintLength = {
26 "sha1": 40,
27 "sha256": 64,
28 "sha512": 128,
29 }
30
31 def __init__(self, parent=None, host="", fingerprint="",
32 version=(0, 0, 0)):
33 """
34 Constructor
35
36 @param parent reference to the parent widget
37 @type QWidget
38 @param host host name
39 @type str
40 @param fingerprint fingerprint for the host
41 @type str
42 @param version Mercurial version info
43 @type tuple of three integers
44 """
45 super(HgUserConfigHostFingerprintDialog, self).__init__(parent)
46 self.setupUi(self)
47
48 self.__version = version
49
50 self.hashComboBox.addItem("")
51 self.hashComboBox.addItems(
52 HgUserConfigHostFingerprintDialog.supportedHashes)
53 if self.__version < (3, 9, 0):
54 self.hashLabel.setEnabled(False)
55 self.hashComboBox.setEnabled(False)
56 hashType = "sha1"
57 else:
58 if fingerprint and fingerprint.startswith("sha"):
59 hashType, fingerprint = fingerprint.split(":", 1)
60 else:
61 hashType = ""
62
63 index = self.hashComboBox.findText(hashType)
64 self.hashComboBox.setCurrentIndex(index)
65 self.hostEdit.setText(host)
66 self.fingerprintEdit.setText(fingerprint)
67
68 msh = self.minimumSizeHint()
69 self.resize(max(self.width(), msh.width()), msh.height())
70
71 self.__updateOkButton()
72
73 def __updateOkButton(self):
74 """
75 Private method to update the status of the Ok button.
76 """
77 enabled = (
78 bool(self.hostEdit.text()) and
79 bool(self.fingerprintEdit.text())
80 )
81 if self.__version >= (3, 9, 0):
82 hashType = self.hashComboBox.currentText()
83 enabled &= bool(hashType)
84 if hashType:
85 enabled &= (
86 len(self.fingerprintEdit.text().replace(":", "")) ==
87 HgUserConfigHostFingerprintDialog.fingerprintLength[
88 hashType])
89
90 self.buttonBox.button(QDialogButtonBox.Ok).setEnabled(enabled)
91
92 @pyqtSlot(str)
93 def on_hostEdit_textChanged(self, txt):
94 """
95 Private slot to handle changes of the host edit.
96
97 @param txt current text
98 @type str
99 """
100 self.__updateOkButton()
101
102 @pyqtSlot(str)
103 def on_hashComboBox_currentIndexChanged(self, txt):
104 """
105 Private slot to handle changes of the hash combo.
106
107 @param txt current text
108 @type str
109 """
110 self.__updateOkButton()
111
112 @pyqtSlot(str)
113 def on_fingerprintEdit_textChanged(self, txt):
114 """
115 Private slot to handle changes of the fingerprint edit.
116
117 @param txt current text
118 @type str
119 """
120 if txt != txt.strip():
121 # get rid of whitespace
122 txt = txt.strip()
123 self.fingerprintEdit.setText(txt)
124
125 if txt.startswith(tuple(
126 h + ":" for h in
127 HgUserConfigHostFingerprintDialog.supportedHashes)):
128 parts = txt.split(":", 1)
129 if len(parts) == 2:
130 self.fingerprintEdit.setText(parts[1])
131 hashIndex = self.hashComboBox.findText(parts[0].strip())
132 if hashIndex > -1:
133 self.hashComboBox.setCurrentIndex(hashIndex)
134
135 self.__updateOkButton()
136
137 def getData(self):
138 """
139 Public method to retrieve the data.
140
141 @return tuple containig the host name and the fingerprint
142 @rtype tuple of two str
143 """
144 if self.__version < (3, 9, 0):
145 fingerprint = self.fingerprintEdit.text()
146 else:
147 fingerprint = "{0}:{1}".format(
148 self.hashComboBox.currentText(),
149 self.fingerprintEdit.text().strip()
150 )
151
152 return self.hostEdit.text().strip(), fingerprint

eric ide

mercurial