From a6f47c6248fd3188a204a3a97c3fe7aa1fd31840 Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Sat, 2 Jul 2016 15:40:06 +0300 Subject: [PATCH] profile lock --- src/callbacks.py | 12 +++++----- src/main.py | 22 ++++++++++++++----- src/mainscreen.py | 17 +++++++++++++++ src/passwordscreen.py | 51 ++++++++++++++++++++++++++++++++----------- src/settings.py | 1 + src/toxencryptsave.py | 3 +++ 6 files changed, 81 insertions(+), 25 deletions(-) diff --git a/src/callbacks.py b/src/callbacks.py index 484af3c..fc13b99 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -62,7 +62,7 @@ def friend_status(tox, friend_num, new_status, user_data): """ Check friend's status (none, busy, away) """ - print("Friend's #{} status changed! New status: {}".format(friend_num, new_status)) + print("Friend's #{} status changed!".format(friend_num)) profile = Profile.get_instance() friend = profile.get_friend_by_number(friend_num) if friend.status is None and Settings.get_instance()['sound_notifications'] and profile.status != TOX_USER_STATUS['BUSY']: @@ -94,7 +94,7 @@ def friend_name(tox, friend_num, name, size, user_data): Friend changed his name """ profile = Profile.get_instance() - print('New name: ', friend_num, name) + print('New name friend #' + str(friend_num)) invoke_in_main_thread(profile.new_name, friend_num, name) @@ -106,7 +106,7 @@ def friend_status_message(tox, friend_num, status_message, size, user_data): profile = Profile.get_instance() friend = profile.get_friend_by_number(friend_num) invoke_in_main_thread(friend.set_status_message, status_message) - print('User #{} has new status: {}'.format(friend_num, status_message)) + print('User #{} has new status'.format(friend_num)) invoke_in_main_thread(profile.send_messages, friend_num) if profile.get_active_number() == friend_num: invoke_in_main_thread(profile.set_active) @@ -123,7 +123,7 @@ def friend_message(window, tray): invoke_in_main_thread(profile.new_message, friend_number, message_type, message) if not window.isActiveWindow(): friend = profile.get_friend_by_number(friend_number) - if settings['notifications'] and profile.status != TOX_USER_STATUS['BUSY']: + if settings['notifications'] and profile.status != TOX_USER_STATUS['BUSY'] and not settings.locked: invoke_in_main_thread(tray_notification, friend.name, message, tray, window) if settings['sound_notifications'] and profile.status != TOX_USER_STATUS['BUSY']: sound_notification(SOUND_NOTIFICATION['MESSAGE']) @@ -178,14 +178,14 @@ def tox_file_recv(window, tray): file_name) if not window.isActiveWindow(): friend = profile.get_friend_by_number(friend_number) - if settings['notifications'] and profile.status != TOX_USER_STATUS['BUSY']: + if settings['notifications'] and profile.status != TOX_USER_STATUS['BUSY'] and not settings.locked: file_from = QtGui.QApplication.translate("Callback", "File from", None, QtGui.QApplication.UnicodeUTF8) invoke_in_main_thread(tray_notification, file_from + ' ' + friend.name, file_name, tray, window) if settings['sound_notifications'] and profile.status != TOX_USER_STATUS['BUSY']: sound_notification(SOUND_NOTIFICATION['FILE_TRANSFER']) invoke_in_main_thread(tray.setIcon, QtGui.QIcon(curr_directory() + '/images/icon_new_messages.png')) else: # AVATAR - print ('Avatar') + print('Avatar') invoke_in_main_thread(profile.incoming_avatar, friend_number, file_number, diff --git a/src/main.py b/src/main.py index 54e4891..d4936b7 100644 --- a/src/main.py +++ b/src/main.py @@ -12,7 +12,7 @@ from callbacks import init_callbacks from util import curr_directory import styles.style import toxencryptsave -from passwordscreen import PasswordScreen +from passwordscreen import PasswordScreen, UnlockAppScreen import profile from plugin_support import PluginLoader @@ -151,7 +151,7 @@ class Toxygen: def aboutToShow(self): status = profile.Profile.get_instance().status act = self.act - if status is None: + if status is None or Settings.get_instance().locked: self.actions()[1].setVisible(False) else: self.actions()[1].setVisible(True) @@ -159,6 +159,7 @@ class Toxygen: act.actions()[1].setChecked(False) act.actions()[2].setChecked(False) act.actions()[status].setChecked(True) + self.actions()[2].setVisible(not Settings.get_instance().locked) def languageChange(self, *args, **kwargs): self.actions()[0].setText(QtGui.QApplication.translate('tray', 'Open Toxygen', None, QtGui.QApplication.UnicodeUTF8)) @@ -181,10 +182,19 @@ class Toxygen: exit = m.addAction(QtGui.QApplication.translate('tray', 'Exit', None, QtGui.QApplication.UnicodeUTF8)) def show_window(): - if not self.ms.isActiveWindow(): - self.ms.setWindowState(self.ms.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive) - self.ms.activateWindow() - self.ms.show() + def show(): + if not self.ms.isActiveWindow(): + self.ms.setWindowState(self.ms.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive) + self.ms.activateWindow() + self.ms.show() + if not Settings.get_instance().locked: + show() + else: + def correct_pass(): + show() + Settings.get_instance().locked = False + self.p = UnlockAppScreen(toxencryptsave.ToxEncryptSave.get_instance(), correct_pass) + self.p.show() m.connect(show, QtCore.SIGNAL("triggered()"), show_window) m.connect(exit, QtCore.SIGNAL("triggered()"), lambda: app.exit()) diff --git a/src/mainscreen.py b/src/mainscreen.py index 4c3dbed..cd727b3 100644 --- a/src/mainscreen.py +++ b/src/mainscreen.py @@ -52,8 +52,10 @@ class MainWindow(QtGui.QMainWindow): self.actionSettings.setObjectName("actionSettings") self.audioSettings = QtGui.QAction(MainWindow) self.pluginData = QtGui.QAction(MainWindow) + self.lockApp = QtGui.QAction(MainWindow) self.menuProfile.addAction(self.actionAdd_friend) self.menuProfile.addAction(self.actionSettings) + self.menuProfile.addAction(self.lockApp) self.menuSettings.addAction(self.actionPrivacy_settings) self.menuSettings.addAction(self.actionInterface_settings) self.menuSettings.addAction(self.actionNotifications) @@ -75,6 +77,7 @@ class MainWindow(QtGui.QMainWindow): self.actionNotifications.triggered.connect(self.notification_settings) self.audioSettings.triggered.connect(self.audio_settings) self.pluginData.triggered.connect(self.plugins_menu) + self.lockApp.triggered.connect(self.lock_app) QtCore.QMetaObject.connectSlotsByName(MainWindow) def languageChange(self, *args, **kwargs): @@ -86,6 +89,7 @@ class MainWindow(QtGui.QMainWindow): return super(MainWindow, self).event(event) def retranslateUi(self): + self.lockApp.setText(QtGui.QApplication.translate("MainWindow", "Lock", None, QtGui.QApplication.UnicodeUTF8)) self.menuPlugins.setTitle(QtGui.QApplication.translate("MainWindow", "Plugins", None, QtGui.QApplication.UnicodeUTF8)) self.pluginData.setText(QtGui.QApplication.translate("MainWindow", "List of plugins", None, QtGui.QApplication.UnicodeUTF8)) self.menuProfile.setTitle(QtGui.QApplication.translate("MainWindow", "Profile", None, QtGui.QApplication.UnicodeUTF8)) @@ -387,6 +391,19 @@ class MainWindow(QtGui.QMainWindow): self.audio_s = AudioSettings() self.audio_s.show() + def lock_app(self): + if toxencryptsave.ToxEncryptSave.get_instance().has_password(): + Settings.get_instance().locked = True + self.hide() + else: + msgBox = QtGui.QMessageBox() + msgBox.setWindowTitle( + QtGui.QApplication.translate("MainWindow", "Cannot lock app", None, QtGui.QApplication.UnicodeUTF8)) + msgBox.setText( + QtGui.QApplication.translate("MainWindow", 'Error. Profile password is not set.', None, + QtGui.QApplication.UnicodeUTF8)) + msgBox.exec_() + def show_menu(self): if not hasattr(self, 'menu'): self.menu = DropdownMenu(self) diff --git a/src/passwordscreen.py b/src/passwordscreen.py index 5f7c870..a9400e6 100644 --- a/src/passwordscreen.py +++ b/src/passwordscreen.py @@ -19,12 +19,11 @@ class PasswordArea(LineEdit): super(PasswordArea, self).keyPressEvent(event) -class PasswordScreen(CenteredWidget): +class PasswordScreenBase(CenteredWidget): - def __init__(self, encrypt, data): - super(PasswordScreen, self).__init__() + def __init__(self, encrypt): + super(PasswordScreenBase, self).__init__() self._encrypt = encrypt - self._data = data self.initUI() def initUI(self): @@ -52,6 +51,27 @@ class PasswordScreen(CenteredWidget): self.center() QtCore.QMetaObject.connectSlotsByName(self) + def button_click(self): + pass + + def keyPressEvent(self, event): + if event.key() == QtCore.Qt.Key_Enter: + self.button_click() + else: + super(PasswordScreenBase, self).keyPressEvent(event) + + def retranslateUi(self): + self.setWindowTitle(QtGui.QApplication.translate("pass", "Enter password", None, QtGui.QApplication.UnicodeUTF8)) + self.enter_pass.setText(QtGui.QApplication.translate("pass", "Password:", None, QtGui.QApplication.UnicodeUTF8)) + self.warning.setText(QtGui.QApplication.translate("pass", "Incorrect password", None, QtGui.QApplication.UnicodeUTF8)) + + +class PasswordScreen(PasswordScreenBase): + + def __init__(self, encrypt, data): + super(PasswordScreen, self).__init__(encrypt) + self._data = data + def button_click(self): if self.password.text(): try: @@ -64,14 +84,19 @@ class PasswordScreen(CenteredWidget): self._data[0] = new_data self.close() - def keyPressEvent(self, event): - if event.key() == QtCore.Qt.Key_Enter: - self.button_click() - else: - super(PasswordScreen, self).keyPressEvent(event) - def retranslateUi(self): - self.setWindowTitle(QtGui.QApplication.translate("pass", "Enter password", None, QtGui.QApplication.UnicodeUTF8)) - self.enter_pass.setText(QtGui.QApplication.translate("pass", "Password:", None, QtGui.QApplication.UnicodeUTF8)) - self.warning.setText(QtGui.QApplication.translate("pass", "Incorrect password", None, QtGui.QApplication.UnicodeUTF8)) +class UnlockAppScreen(PasswordScreenBase): + def __init__(self, encrypt, callback): + super(UnlockAppScreen, self).__init__(encrypt) + self._callback = callback + self.setWindowFlags(QtCore.Qt.FramelessWindowHint) + + def button_click(self): + if self.password.text(): + if self._encrypt.is_password(self.password.text()): + self._callback() + self.close() + else: + self.warning.setVisible(True) + print('Wrong password!') diff --git a/src/settings.py b/src/settings.py index 7250397..974fbf4 100644 --- a/src/settings.py +++ b/src/settings.py @@ -35,6 +35,7 @@ class Settings(dict, Singleton): self.save() smileys.SmileyLoader(self) p = pyaudio.PyAudio() + self.locked = False self.audio = {'input': p.get_default_input_device_info()['index'], 'output': p.get_default_output_device_info()['index']} diff --git a/src/toxencryptsave.py b/src/toxencryptsave.py index 191dcb2..14d3aee 100644 --- a/src/toxencryptsave.py +++ b/src/toxencryptsave.py @@ -48,6 +48,9 @@ class ToxEncryptSave(util.Singleton): def has_password(self): return bool(self._passphrase) + def is_password(self, password): + return self._passphrase == password + def is_data_encrypted(self, data): func = self.libtoxencryptsave.tox_is_data_encrypted func.restype = c_bool