Helpviewer/Passwords/PasswordManager.py

changeset 945
8cd4d08fa9f6
parent 791
9ec2ac20e54e
child 1112
8a7d1b9d18db
equal deleted inserted replaced
944:1b59c4ba121e 945:8cd4d08fa9f6
18 from Helpviewer.JavaScriptResources import parseForms_js 18 from Helpviewer.JavaScriptResources import parseForms_js
19 19
20 from Utilities.AutoSaver import AutoSaver 20 from Utilities.AutoSaver import AutoSaver
21 import Utilities 21 import Utilities
22 import Preferences 22 import Preferences
23
23 24
24 class LoginForm(object): 25 class LoginForm(object):
25 """ 26 """
26 Class implementing a data structure for login forms. 27 Class implementing a data structure for login forms.
27 """ 28 """
30 Constructor 31 Constructor
31 """ 32 """
32 self.url = QUrl() 33 self.url = QUrl()
33 self.name = "" 34 self.name = ""
34 self.hasAPassword = False 35 self.hasAPassword = False
35 self.elements = [] # list of tuples of element name and value 36 self.elements = [] # list of tuples of element name and value
36 # (string, string) 37 # (string, string)
37 self.elementTypes = {} # dict of element name as key and type as value 38 self.elementTypes = {} # dict of element name as key and type as value
38 39
39 def isValid(self): 40 def isValid(self):
40 """ 41 """
69 f.write("{0}\n".format(self.name)) 70 f.write("{0}\n".format(self.name))
70 f.write("{0}\n".format(self.hasAPassword)) 71 f.write("{0}\n".format(self.hasAPassword))
71 for element in self.elements: 72 for element in self.elements:
72 f.write("{0} = {1}\n".format(element[0], element[1])) 73 f.write("{0} = {1}\n".format(element[0], element[1]))
73 74
75
74 class PasswordManager(QObject): 76 class PasswordManager(QObject):
75 """ 77 """
76 Class implementing the password manager. 78 Class implementing the password manager.
77 79
78 @signal changed() emitted to indicate a change 80 @signal changed() emitted to indicate a change
81 83
82 SEPARATOR = "====================" 84 SEPARATOR = "===================="
83 FORMS = "=====FORMS=====" 85 FORMS = "=====FORMS====="
84 NEVER = "=====NEVER=====" 86 NEVER = "=====NEVER====="
85 87
86 def __init__(self, parent = None): 88 def __init__(self, parent=None):
87 """ 89 """
88 Constructor 90 Constructor
89 91
90 @param parent reference to the parent object (QObject) 92 @param parent reference to the parent object (QObject)
91 """ 93 """
168 if not self.__loaded: 170 if not self.__loaded:
169 return 171 return
170 172
171 loginFile = os.path.join(Utilities.getConfigDir(), "browser", "logins") 173 loginFile = os.path.join(Utilities.getConfigDir(), "browser", "logins")
172 try: 174 try:
173 f = open(loginFile, "w", encoding = "utf-8") 175 f = open(loginFile, "w", encoding="utf-8")
174 for key, login in list(self.__logins.items()): 176 for key, login in list(self.__logins.items()):
175 f.write("{0}\n".format(key)) 177 f.write("{0}\n".format(key))
176 f.write("{0}\n".format(login[0])) 178 f.write("{0}\n".format(login[0]))
177 f.write("{0}\n".format(login[1])) 179 f.write("{0}\n".format(login[1]))
178 f.write("{0}\n".format(self.SEPARATOR)) 180 f.write("{0}\n".format(self.SEPARATOR))
199 Private method to load the saved login credentials. 201 Private method to load the saved login credentials.
200 """ 202 """
201 loginFile = os.path.join(Utilities.getConfigDir(), "browser", "logins") 203 loginFile = os.path.join(Utilities.getConfigDir(), "browser", "logins")
202 if os.path.exists(loginFile): 204 if os.path.exists(loginFile):
203 try: 205 try:
204 f = open(loginFile, "r", encoding = "utf-8") 206 f = open(loginFile, "r", encoding="utf-8")
205 lines = f.read() 207 lines = f.read()
206 f.close() 208 f.close()
207 except IOError as err: 209 except IOError as err:
208 E5MessageBox.critical(None, 210 E5MessageBox.critical(None,
209 self.trUtf8("Loading login data"), 211 self.trUtf8("Loading login data"),
359 boundary = contentTypeHeader.split(" ")[1].split("=")[1] 361 boundary = contentTypeHeader.split(" ")[1].split("=")[1]
360 else: 362 else:
361 boundary = None 363 boundary = None
362 364
363 # find the matching form on the web page 365 # find the matching form on the web page
364 form = self.__findForm(webPage, data, boundary = boundary) 366 form = self.__findForm(webPage, data, boundary=boundary)
365 if not form.isValid(): 367 if not form.isValid():
366 return 368 return
367 form.url = QUrl(url) 369 form.url = QUrl(url)
368 370
369 # check, if the form has a password 371 # check, if the form has a password
372 374
373 # prompt, if the form has never be seen 375 # prompt, if the form has never be seen
374 key = self.__createKey(url, "") 376 key = self.__createKey(url, "")
375 if key not in self.__loginForms: 377 if key not in self.__loginForms:
376 mb = E5MessageBox.E5MessageBox(E5MessageBox.Question, 378 mb = E5MessageBox.E5MessageBox(E5MessageBox.Question,
377 self.trUtf8("Save password"), 379 self.trUtf8("Save password"),
378 self.trUtf8( 380 self.trUtf8(
379 """<b>Would you like to save this password?</b><br/>""" 381 """<b>Would you like to save this password?</b><br/>"""
380 """To review passwords you have saved and remove them, """ 382 """To review passwords you have saved and remove them, """
381 """use the password management dialog of the Settings menu."""), 383 """use the password management dialog of the Settings menu."""),
382 modal = True) 384 modal=True)
383 neverButton = mb.addButton( 385 neverButton = mb.addButton(
384 self.trUtf8("Never for this site"), E5MessageBox.DestructiveRole) 386 self.trUtf8("Never for this site"), E5MessageBox.DestructiveRole)
385 noButton = mb.addButton(self.trUtf8("Not now"), E5MessageBox.RejectRole) 387 noButton = mb.addButton(self.trUtf8("Not now"), E5MessageBox.RejectRole)
386 mb.addButton(E5MessageBox.Yes) 388 mb.addButton(E5MessageBox.Yes)
387 mb.exec_() 389 mb.exec_()
420 cleanUrl.setQueryItems([]) 422 cleanUrl.setQueryItems([])
421 cleanUrl.setFragment("") 423 cleanUrl.setFragment("")
422 cleanUrl.setUserInfo("") 424 cleanUrl.setUserInfo("")
423 return cleanUrl 425 return cleanUrl
424 426
425 def __findForm(self, webPage, data, boundary = None): 427 def __findForm(self, webPage, data, boundary=None):
426 """ 428 """
427 Private method to find the form used for logging in. 429 Private method to find the form used for logging in.
428 430
429 @param webPage reference to the web page (QWebPage) 431 @param webPage reference to the web page (QWebPage)
430 @param data data to be sent (QByteArray) 432 @param data data to be sent (QByteArray)

eric ide

mercurial