From 19f67ef9b0b3cb67ea0fa7a4e15772537c68d5f1 Mon Sep 17 00:00:00 2001 From: Nikolay Golub Date: Fri, 4 Jul 2014 11:49:16 +0400 Subject: [PATCH] Fix saving credentials from relogin menu window --- pyjtt/app.py | 31 +++++++------------------------ pyjtt/gui.py | 31 +++++++++++++++++++++++++++---- pyjtt/utils.py | 26 ++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 28 deletions(-) diff --git a/pyjtt/app.py b/pyjtt/app.py index 7c4726d..d840381 100644 --- a/pyjtt/app.py +++ b/pyjtt/app.py @@ -25,7 +25,7 @@ from os import path, mkdir import sys -import configparser + import logging import logging.handlers logger = logging.getLogger(__name__) @@ -36,24 +36,6 @@ import utils LOGGING_FORMAT = '%(asctime)s %(levelname)s - %(name)s@%(thread)d - %(message)s' -CONFIG_FILENAME = 'pyjtt.cfg' - - -def init_config(workdir): - """ - Prepares config class for usage - """ - defaults = {'log_level': 'INFO'} - for item in ('jirahost', 'login', 'password', 'save_password'): - defaults[item] = '' - - config_filename = path.join(workdir, CONFIG_FILENAME) - config = configparser.ConfigParser(defaults=defaults) - config.read(config_filename) - - if not config.has_section('main'): - config.add_section('main') - return config def main(): @@ -64,7 +46,9 @@ def main(): if not path.isdir(workdir): mkdir(workdir) - config = init_config(workdir) + config = utils.init_config() + + # Initialize logging log_filename = path.join(workdir, 'application.log') log_rotater = logging.handlers.RotatingFileHandler(log_filename, mode='a', @@ -80,8 +64,6 @@ def app_quit(): """ Standard procedures before close application """ - with open(path.join(workdir, CONFIG_FILENAME), 'w') as configfile: - config.write(configfile) app.quit() sys.exit(app.exec_()) jira_host = config.get('main', 'jirahost') @@ -104,11 +86,12 @@ def app_quit(): if login_window.ui.checkBoxSaveCredentials.isChecked(): config.set('main', 'password', password) + utils.write_config(config) else: app_quit() - main_window = gui.MainWindow(jira_host, login, password) - main_window.show() + #TODO: add check that login and password from config are correct + main_window = gui.MainWindow(jira_host, login, password) app_quit() diff --git a/pyjtt/gui.py b/pyjtt/gui.py index bbfe8c8..88fa2e7 100644 --- a/pyjtt/gui.py +++ b/pyjtt/gui.py @@ -113,6 +113,7 @@ def __init__(self, icon, parent=None): super(SystemTrayIcon, self).__init__(icon, parent) menu = QtWidgets.QMenu(parent) exitAction = menu.addAction("Exit") + exitAction.triggered.connect(parent.close) self.setContextMenu(menu) @@ -196,6 +197,7 @@ def __init__(self, jirahost, login, password, parent=None): # Initialize app self.app = core.TimeTrackerApp(jirahost, login, password) + # Initialize workers self.worker_threads = [] for i in range(self.number_of_workers): @@ -224,6 +226,9 @@ def __init__(self, jirahost, login, password, parent=None): self.ui.actionLogout.triggered.connect(self.logout) self.ui.tray_icon.activated.connect(self.tray_click) + # Appear + self.show() + # Request assigned issues get_assigned_issues_job = partial(self.app.get_user_assigned_issues) self.tasks_queue.put(get_assigned_issues_job) @@ -233,10 +238,19 @@ def __init__(self, jirahost, login, password, parent=None): # Core stuff def closeEvent(self, event): - for thread in self.worker_threads: - thread.quit() - # TODO: Add checking of online tracking - event.accept() + buttons = QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No + message = 'Are you sure you want to close this application?' + confirmation = QtWidgets.QMessageBox.question(self, + 'Exit', + message, + buttons=buttons) + if confirmation == QtWidgets.QMessageBox.Yes: + for thread in self.worker_threads: + thread.quit() + # TODO: Add checking of online tracking + super(MainWindow, self).closeEvent(event) + else: + event.ignore() def logout(self): relogin_window = LoginWindow(self.app.jira_accessor.jirahost, @@ -248,7 +262,16 @@ def logout(self): jira_host = relogin_window.ui.lineEditHostAddress.text() login = relogin_window.ui.lineEditLogin.text() password = relogin_window.ui.lineEditPassword.text() + config = utils.init_config() + config.set('main', 'jirahost', jira_host) + config.set('main', 'login', login) + if relogin_window.ui.checkBoxSaveCredentials.isChecked(): + config.set('main', 'password', password) + else: + config.remove_option('main', 'password') + utils.write_config(config) self.app = core.TimeTrackerApp(jira_host, login, password) + else: self.close() diff --git a/pyjtt/utils.py b/pyjtt/utils.py index 96577ac..1fed030 100644 --- a/pyjtt/utils.py +++ b/pyjtt/utils.py @@ -23,12 +23,14 @@ __license__ = "GPL" import os +import configparser import platform from datetime import datetime, timedelta import logging logger = logging.getLogger(__name__) +CONFIG_FILENAME = 'pyjtt.cfg' def get_app_working_dir(): """Returns path to application operational folder. @@ -45,6 +47,30 @@ def get_app_working_dir(): return os.path.abspath('.' + app_name) +def init_config(workdir=None): + """ + Prepares config class for usage + """ + if workdir is None: + workdir = get_app_working_dir() + defaults = {'log_level': 'INFO'} + for item in ('jirahost', 'login', 'password', 'save_password'): + defaults[item] = '' + + config_filename = os.path.join(workdir, CONFIG_FILENAME) + config = configparser.ConfigParser(defaults=defaults) + config.read(config_filename) + + if not config.has_section('main'): + config.add_section('main') + return config + + +def write_config(config): + workdir = get_app_working_dir() + with open(os.path.join(workdir, CONFIG_FILENAME), 'w') as configfile: + config.write(configfile) + def get_local_utc_offset(now, utcnow): """Calculates local UTC offset. Returns string""" logger.debug('Getting local UTC Offset')