|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to edit a host fingerprint. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSlot |
|
11 from PyQt5.QtWidgets import QDialog, QDialogButtonBox |
|
12 |
|
13 from .Ui_HgUserConfigHostFingerprintDialog import ( |
|
14 Ui_HgUserConfigHostFingerprintDialog |
|
15 ) |
|
16 |
|
17 |
|
18 class HgUserConfigHostFingerprintDialog( |
|
19 QDialog, Ui_HgUserConfigHostFingerprintDialog): |
|
20 """ |
|
21 Class implementing a dialog to edit a host fingerprint. |
|
22 """ |
|
23 supportedHashes = ("sha1", "sha256", "sha512") |
|
24 fingerprintLength = { |
|
25 "sha1": 40, |
|
26 "sha256": 64, |
|
27 "sha512": 128, |
|
28 } |
|
29 |
|
30 def __init__(self, parent=None, host="", fingerprint="", |
|
31 version=(0, 0, 0)): |
|
32 """ |
|
33 Constructor |
|
34 |
|
35 @param parent reference to the parent widget |
|
36 @type QWidget |
|
37 @param host host name |
|
38 @type str |
|
39 @param fingerprint fingerprint for the host |
|
40 @type str |
|
41 @param version Mercurial version info |
|
42 @type tuple of three integers |
|
43 """ |
|
44 super().__init__(parent) |
|
45 self.setupUi(self) |
|
46 |
|
47 self.__version = version |
|
48 |
|
49 self.hashComboBox.addItem("") |
|
50 self.hashComboBox.addItems( |
|
51 HgUserConfigHostFingerprintDialog.supportedHashes) |
|
52 if self.__version < (3, 9, 0): |
|
53 self.hashLabel.setEnabled(False) |
|
54 self.hashComboBox.setEnabled(False) |
|
55 hashType = "sha1" |
|
56 else: |
|
57 if fingerprint and fingerprint.startswith("sha"): |
|
58 hashType, fingerprint = fingerprint.split(":", 1) |
|
59 else: |
|
60 hashType = "" |
|
61 |
|
62 index = self.hashComboBox.findText(hashType) |
|
63 self.hashComboBox.setCurrentIndex(index) |
|
64 self.hostEdit.setText(host) |
|
65 self.fingerprintEdit.setText(fingerprint) |
|
66 |
|
67 msh = self.minimumSizeHint() |
|
68 self.resize(max(self.width(), msh.width()), msh.height()) |
|
69 |
|
70 self.__updateOkButton() |
|
71 |
|
72 def __updateOkButton(self): |
|
73 """ |
|
74 Private method to update the status of the Ok button. |
|
75 """ |
|
76 enabled = ( |
|
77 bool(self.hostEdit.text()) and |
|
78 bool(self.fingerprintEdit.text()) |
|
79 ) |
|
80 if self.__version >= (3, 9, 0): |
|
81 hashType = self.hashComboBox.currentText() |
|
82 enabled &= bool(hashType) |
|
83 if hashType: |
|
84 enabled &= ( |
|
85 len(self.fingerprintEdit.text().replace(":", "")) == |
|
86 HgUserConfigHostFingerprintDialog.fingerprintLength[ |
|
87 hashType]) |
|
88 |
|
89 self.buttonBox.button( |
|
90 QDialogButtonBox.StandardButton.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(int) |
|
103 def on_hashComboBox_currentIndexChanged(self, index): |
|
104 """ |
|
105 Private slot to handle changes of the hash combo. |
|
106 |
|
107 @param index current index |
|
108 @type int |
|
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 fingerprint = ( |
|
145 self.fingerprintEdit.text() |
|
146 if self.__version < (3, 9, 0) else |
|
147 "{0}:{1}".format(self.hashComboBox.currentText(), |
|
148 self.fingerprintEdit.text().strip()) |
|
149 ) |
|
150 |
|
151 return self.hostEdit.text().strip(), fingerprint |