|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter some user data. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSlot |
|
13 from PyQt5.QtWidgets import QDialog |
|
14 |
|
15 from Globals.E5ConfigParser import E5ConfigParser |
|
16 |
|
17 from .HgUtilities import getConfigPath |
|
18 |
|
19 from .Ui_HgUserConfigDialog import Ui_HgUserConfigDialog |
|
20 |
|
21 |
|
22 class HgUserConfigDialog(QDialog, Ui_HgUserConfigDialog): |
|
23 """ |
|
24 Class implementing a dialog to enter some user data. |
|
25 """ |
|
26 def __init__(self, version=(0, 0), parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param version Mercurial version info (tuple of two integers) |
|
31 @param parent reference to the parent widget (QWidget) |
|
32 """ |
|
33 super(HgUserConfigDialog, self).__init__(parent) |
|
34 self.setupUi(self) |
|
35 |
|
36 msh = self.minimumSizeHint() |
|
37 self.resize(max(self.width(), msh.width()), msh.height()) |
|
38 |
|
39 self.__config = None |
|
40 self.readUserConfig() |
|
41 |
|
42 def writeUserConfig(self): |
|
43 """ |
|
44 Public method to write the user configuration file. |
|
45 """ |
|
46 if self.__config is None: |
|
47 self.__config = E5ConfigParser() |
|
48 |
|
49 self.__config["ui"] = { |
|
50 "username": "{0} <{1}>".format( |
|
51 self.userNameEdit.text(), |
|
52 self.emailEdit.text(), |
|
53 ) |
|
54 } |
|
55 self.__config["extensions"] = {} |
|
56 if self.fetchCheckBox.isChecked(): |
|
57 self.__config["extensions"]["fetch"] = "" |
|
58 if self.gpgCheckBox.isChecked(): |
|
59 self.__config["extensions"]["gpg"] = "" |
|
60 if self.purgeCheckBox.isChecked(): |
|
61 self.__config["extensions"]["purge"] = "" |
|
62 if self.queuesCheckBox.isChecked(): |
|
63 self.__config["extensions"]["mq"] = "" |
|
64 if self.rebaseCheckBox.isChecked(): |
|
65 self.__config["extensions"]["rebase"] = "" |
|
66 if self.shelveCheckBox.isChecked(): |
|
67 self.__config["extensions"]["shelve"] = "" |
|
68 if self.largefilesCheckBox.isChecked(): |
|
69 self.__config["extensions"]["largefiles"] = "" |
|
70 self.__config["largefiles"] = { |
|
71 "minsize": self.lfFileSizeSpinBox.value(), |
|
72 } |
|
73 lfFilePatterns = self.lfFilePatternsEdit.text() |
|
74 if lfFilePatterns: |
|
75 self.__config["largefiles"]["patterns"] = lfFilePatterns |
|
76 |
|
77 cfgFile = getConfigPath() |
|
78 with open(cfgFile, "w") as configFile: |
|
79 self.__config.write(configFile) |
|
80 |
|
81 def readUserConfig(self): |
|
82 """ |
|
83 Public method to read the user configuration file. |
|
84 """ |
|
85 cfgFile = getConfigPath() |
|
86 |
|
87 self.__config = E5ConfigParser() |
|
88 if self.__config.read(cfgFile): |
|
89 # step 1: extract user name and email |
|
90 try: |
|
91 username = self.__config["ui"]["username"].strip() |
|
92 if "<" in username and username.endswith(">"): |
|
93 name, email = username[:-1].rsplit("<", 1) |
|
94 else: |
|
95 name = username |
|
96 email = "" |
|
97 self.userNameEdit.setText(name.strip()), |
|
98 self.emailEdit.setText(email.strip()), |
|
99 except KeyError: |
|
100 pass |
|
101 |
|
102 # step 2: extract extensions information |
|
103 if "extensions" in self.__config: |
|
104 self.fetchCheckBox.setChecked( |
|
105 "fetch" in self.__config["extensions"]) |
|
106 self.gpgCheckBox.setChecked( |
|
107 "gpg" in self.__config["extensions"]) |
|
108 self.purgeCheckBox.setChecked( |
|
109 "purge" in self.__config["extensions"]) |
|
110 self.queuesCheckBox.setChecked( |
|
111 "mq" in self.__config["extensions"]) |
|
112 self.rebaseCheckBox.setChecked( |
|
113 "rebase" in self.__config["extensions"]) |
|
114 self.shelveCheckBox.setChecked( |
|
115 "shelve" in self.__config["extensions"]) |
|
116 self.largefilesCheckBox.setChecked( |
|
117 "largefiles" in self.__config["extensions"]) |
|
118 |
|
119 # step 3: extract large files information |
|
120 if "largefiles" in self.__config: |
|
121 if "minsize" in self.__config["largefiles"]: |
|
122 self.lfFileSizeSpinBox.setValue( |
|
123 self.__config.getint("largefiles", "minsize")) |
|
124 if "patterns" in self.__config["largefiles"]: |
|
125 self.lfFilePatternsEdit.setText( |
|
126 self.__config["largefiles"]["patterns"]) |
|
127 |
|
128 @pyqtSlot() |
|
129 def accept(self): |
|
130 """ |
|
131 Public slot to accept the dialog. |
|
132 """ |
|
133 self.writeUserConfig() |
|
134 |
|
135 super(HgUserConfigDialog, self).accept() |