#!/usr/bin/env python
from qt import *
from loginBA import LoginBA
from workerThread import WorkerThread
import logging
from constants import EVENT_LOGIN
from messageQueue import mainMessageQueue
from loginSettings import LoginSettings
import os
from help import Help
try:
from base64 import b32encode,b32decode
except ImportError:
from python24.base64 import b32encode,b32decode
debug = logging.getLogger("login").debug
error = logging.getLogger("login").error
class Login(LoginBA):
def __init__(self, parent, data_path, sf, *args):
apply(LoginBA.__init__,(self,) + args)
self.parent = parent
self.data_path = data_path
self.settings = LoginSettings(data_path)
username = self.settings['sf_username']
password = self.settings['sf_password']
try:
if password: password = b32decode(password)
except:
QMessageBox.warning(self, "Password Reset",
"Could not decode your saved password."
"You will need to re-enter it.")
password = ""
self.usernameLineEdit.setText(username)
self.passwordLineEdit.setText(password)
rememberMe = self.settings['sf_remember_password']
if rememberMe.lower() in ('1', 'true'):
self.rememberCheckBox.setChecked(True)
self.sf = sf
self.thread = None
self.show()
if username and password: self.buttonOk.setFocus()
elif username: self.passwordLineEdit.setFocus()
def customEvent(self, event):
if event.type() != EVENT_LOGIN: return
projects = event.data()
debug("customEvent: %s", projects)
self.setCursor(Qt.arrowCursor)
self.parent.statusBar().clear()
self.setEnabled(True)
#self.parent.setCursor(Qt.arrowCursor)
if not projects:
QMessageBox.critical(self, "Login Failed",
"Unable to login to SourceForge.\n"
"Please verify your username and password.")
self.enableElements()
return
settings = self.settings
settings['sf_username'] = self.username
settings['sf_remember_password'] = self.remember
if self.remember:
settings['sf_password'] = b32encode(self.password)
else:
settings['sf_password'] = ""
settings.saveSettings()
self.parent.emit(PYSIGNAL('projects_fetched()'), (projects,) )
LoginBA.accept(self)
def __del__(self):
if self.thread:
t = self.thread
running = t.running()
t.stop()
if not t.finished():
t.wait()
def help(self):
self._help = Help(self, "login.html")
def disableElements(self):
self.enableElements(0)
def enableElements(self, enable=1):
self.usernameLineEdit.setEnabled(enable)
self.passwordLineEdit.setEnabled(enable)
self.rememberCheckBox.setEnabled(enable)
self.buttonOk.setEnabled(enable)
def reject(self):
if self.thread: self.thread.stop()
mainMessageQueue.put("Sourceforge login cancelled")
LoginBA.reject(self)
def accept(self):
self.username = str(self.usernameLineEdit.text())
self.password = str(self.passwordLineEdit.text())
self.remember = str(self.rememberCheckBox.isChecked()) # True/False
if not self.username or not self.password:
QMessageBox.critical(self, "Login Failed",
"You must supply a valid username and password.\n")
return
self.setCursor(Qt.waitCursor)
mainMessageQueue.put("Authenticating user")
self.disableElements()
self.thread = WorkerThread(self,
EVENT_LOGIN,
self.sf.initialize,
self.username,
self.password)
self.thread.start()
|