2016-03-15 17:05:19 +00:00
|
|
|
import sys
|
2016-02-18 16:15:38 +00:00
|
|
|
from loginscreen import LoginScreen
|
2016-04-03 20:51:46 +00:00
|
|
|
from settings import *
|
2016-03-15 17:05:19 +00:00
|
|
|
from PySide import QtCore, QtGui
|
|
|
|
from bootstrap import node_generator
|
2016-02-19 10:41:54 +00:00
|
|
|
from mainscreen import MainWindow
|
2016-04-03 20:51:46 +00:00
|
|
|
from profile import tox_factory
|
2016-02-22 21:18:58 +00:00
|
|
|
from callbacks import init_callbacks
|
2016-03-03 16:44:01 +00:00
|
|
|
from util import curr_directory, get_style
|
2016-03-21 10:55:50 +00:00
|
|
|
import styles.style
|
2016-04-05 16:39:05 +00:00
|
|
|
import locale
|
2016-05-15 14:39:49 +00:00
|
|
|
import toxencryptsave
|
2016-05-15 16:54:44 +00:00
|
|
|
from passwordscreen import PasswordScreen
|
2016-02-18 16:15:38 +00:00
|
|
|
|
|
|
|
|
2016-03-15 17:05:19 +00:00
|
|
|
class Toxygen(object):
|
2016-02-19 15:04:53 +00:00
|
|
|
|
2016-05-06 10:31:18 +00:00
|
|
|
def __init__(self, path=None):
|
2016-03-15 17:05:19 +00:00
|
|
|
super(Toxygen, self).__init__()
|
2016-04-24 10:45:11 +00:00
|
|
|
self.tox = self.ms = self.init = self.mainloop = self.avloop = None
|
2016-05-06 10:31:18 +00:00
|
|
|
self.path = path
|
2016-02-19 10:41:54 +00:00
|
|
|
|
2016-05-15 16:54:44 +00:00
|
|
|
def enter_pass(self, data):
|
2016-05-15 14:39:49 +00:00
|
|
|
"""
|
|
|
|
Show password screen
|
|
|
|
"""
|
2016-05-15 16:54:44 +00:00
|
|
|
tmp = [data]
|
|
|
|
p = PasswordScreen(toxencryptsave.LibToxEncryptSave.get_instance(), tmp)
|
|
|
|
p.show()
|
|
|
|
self.app.connect(self.app, QtCore.SIGNAL("lastWindowClosed()"), self.app, QtCore.SLOT("quit()"))
|
|
|
|
self.app.exec_()
|
|
|
|
if tmp[0] == data:
|
|
|
|
raise SystemExit()
|
|
|
|
else:
|
|
|
|
return tmp[0]
|
2016-05-15 14:39:49 +00:00
|
|
|
|
2016-03-15 17:05:19 +00:00
|
|
|
def main(self):
|
2016-02-19 15:04:53 +00:00
|
|
|
"""
|
2016-04-03 20:51:46 +00:00
|
|
|
Main function of app. loads login screen if needed and starts main screen
|
2016-03-15 17:05:19 +00:00
|
|
|
"""
|
|
|
|
app = QtGui.QApplication(sys.argv)
|
|
|
|
app.setWindowIcon(QtGui.QIcon(curr_directory() + '/images/icon.png'))
|
2016-05-15 16:54:44 +00:00
|
|
|
self.app = app
|
2016-03-21 14:23:34 +00:00
|
|
|
|
|
|
|
# application color scheme
|
|
|
|
with open(curr_directory() + '/styles/style.qss') as fl:
|
|
|
|
dark_style = fl.read()
|
|
|
|
app.setStyleSheet(dark_style)
|
2016-05-15 14:39:49 +00:00
|
|
|
|
|
|
|
encrypt_save = toxencryptsave.LibToxEncryptSave()
|
|
|
|
|
2016-05-06 10:31:18 +00:00
|
|
|
if self.path is not None:
|
|
|
|
path = os.path.dirname(self.path.encode(locale.getpreferredencoding())) + '/'
|
|
|
|
name = os.path.basename(self.path.encode(locale.getpreferredencoding()))[:-4]
|
2016-05-14 10:18:17 +00:00
|
|
|
data = ProfileHelper(path, name).open_profile()
|
2016-05-15 14:39:49 +00:00
|
|
|
if encrypt_save.is_data_encrypted(data):
|
|
|
|
data = self.enter_pass(data)
|
2016-04-03 20:51:46 +00:00
|
|
|
settings = Settings(name)
|
2016-03-15 17:05:19 +00:00
|
|
|
self.tox = tox_factory(data, settings)
|
2016-05-06 10:31:18 +00:00
|
|
|
else:
|
|
|
|
auto_profile = Settings.get_auto_profile()
|
|
|
|
if not auto_profile:
|
|
|
|
# show login screen if default profile not found
|
|
|
|
current_locale = QtCore.QLocale()
|
|
|
|
curr_lang = current_locale.languageToString(current_locale.language())
|
|
|
|
langs = Settings.supported_languages()
|
|
|
|
if curr_lang in map(lambda x: x[0], langs):
|
|
|
|
lang_path = filter(lambda x: x[0] == curr_lang, langs)[0][1]
|
|
|
|
translator = QtCore.QTranslator()
|
|
|
|
translator.load(curr_directory() + '/translations/' + lang_path)
|
|
|
|
app.installTranslator(translator)
|
|
|
|
app.translator = translator
|
|
|
|
ls = LoginScreen()
|
|
|
|
ls.setWindowIconText("Toxygen")
|
|
|
|
profiles = ProfileHelper.find_profiles()
|
|
|
|
ls.update_select(map(lambda x: x[1], profiles))
|
|
|
|
_login = self.Login(profiles)
|
|
|
|
ls.update_on_close(_login.login_screen_close)
|
|
|
|
ls.show()
|
|
|
|
app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app, QtCore.SLOT("quit()"))
|
|
|
|
app.exec_()
|
|
|
|
if not _login.t:
|
|
|
|
return
|
|
|
|
elif _login.t == 1: # create new profile
|
|
|
|
name = _login.name if _login.name else 'toxygen_user'
|
|
|
|
self.tox = tox_factory()
|
|
|
|
self.tox.self_set_name(_login.name if _login.name else 'Toxygen User')
|
|
|
|
self.tox.self_set_status_message('Toxing on Toxygen')
|
2016-05-14 10:18:17 +00:00
|
|
|
ProfileHelper(Settings.get_default_path(), name).save_profile(self.tox.get_savedata())
|
2016-05-06 10:31:18 +00:00
|
|
|
path = Settings.get_default_path()
|
|
|
|
settings = Settings(name)
|
|
|
|
else: # load existing profile
|
|
|
|
path, name = _login.get_data()
|
|
|
|
if _login.default:
|
|
|
|
Settings.set_auto_profile(path, name)
|
2016-05-14 10:18:17 +00:00
|
|
|
data = ProfileHelper(path, name).open_profile()
|
2016-05-15 14:39:49 +00:00
|
|
|
if encrypt_save.is_data_encrypted(data):
|
|
|
|
data = self.enter_pass(data)
|
2016-05-06 10:31:18 +00:00
|
|
|
settings = Settings(name)
|
|
|
|
self.tox = tox_factory(data, settings)
|
|
|
|
else:
|
|
|
|
path, name = auto_profile
|
|
|
|
path = path.encode(locale.getpreferredencoding())
|
2016-05-14 10:18:17 +00:00
|
|
|
data = ProfileHelper(path, name).open_profile()
|
2016-05-15 14:39:49 +00:00
|
|
|
if encrypt_save.is_data_encrypted(data):
|
|
|
|
data = self.enter_pass(data)
|
2016-05-06 10:31:18 +00:00
|
|
|
settings = Settings(name)
|
|
|
|
self.tox = tox_factory(data, settings)
|
2016-03-10 20:04:43 +00:00
|
|
|
|
2016-04-03 20:51:46 +00:00
|
|
|
if ProfileHelper.is_active_profile(path, name): # profile is in use
|
|
|
|
reply = QtGui.QMessageBox.question(None,
|
|
|
|
'Profile {}'.format(name),
|
2016-04-04 09:20:32 +00:00
|
|
|
QtGui.QApplication.translate("login", 'Looks like other instance of Toxygen uses this profile! Continue?', None, QtGui.QApplication.UnicodeUTF8),
|
2016-04-03 20:51:46 +00:00
|
|
|
QtGui.QMessageBox.Yes,
|
|
|
|
QtGui.QMessageBox.No)
|
|
|
|
if reply != QtGui.QMessageBox.Yes:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
settings.set_active_profile()
|
|
|
|
|
2016-04-04 09:20:32 +00:00
|
|
|
lang = filter(lambda x: x[0] == settings['language'], Settings.supported_languages())[0]
|
|
|
|
translator = QtCore.QTranslator()
|
2016-04-14 12:01:59 +00:00
|
|
|
translator.load(curr_directory() + '/translations/' + lang[1])
|
2016-04-04 09:20:32 +00:00
|
|
|
app.installTranslator(translator)
|
|
|
|
app.translator = translator
|
|
|
|
|
2016-04-01 14:09:45 +00:00
|
|
|
self.ms = MainWindow(self.tox, self.reset)
|
|
|
|
|
2016-03-15 19:12:37 +00:00
|
|
|
# tray icon
|
|
|
|
self.tray = QtGui.QSystemTrayIcon(QtGui.QIcon(curr_directory() + '/images/icon.png'))
|
2016-04-04 09:20:32 +00:00
|
|
|
self.tray.setObjectName('tray')
|
2016-04-04 11:00:50 +00:00
|
|
|
|
|
|
|
class Menu(QtGui.QMenu):
|
|
|
|
def languageChange(self, *args, **kwargs):
|
|
|
|
self.actions()[0].setText(QtGui.QApplication.translate('tray', 'Open Toxygen', None, QtGui.QApplication.UnicodeUTF8))
|
|
|
|
self.actions()[1].setText(QtGui.QApplication.translate('tray', 'Exit', None, QtGui.QApplication.UnicodeUTF8))
|
|
|
|
|
|
|
|
m = Menu()
|
2016-04-04 09:20:32 +00:00
|
|
|
show = m.addAction(QtGui.QApplication.translate('tray', 'Open Toxygen', None, QtGui.QApplication.UnicodeUTF8))
|
|
|
|
exit = m.addAction(QtGui.QApplication.translate('tray', 'Exit', None, QtGui.QApplication.UnicodeUTF8))
|
2016-04-01 14:38:24 +00:00
|
|
|
|
|
|
|
def show_window():
|
2016-04-02 19:11:56 +00:00
|
|
|
if not self.ms.isActiveWindow():
|
|
|
|
self.ms.setWindowState(self.ms.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive)
|
|
|
|
self.ms.activateWindow()
|
2016-05-07 21:47:10 +00:00
|
|
|
if self.ms.isHidden():
|
|
|
|
self.ms.show()
|
2016-04-01 14:38:24 +00:00
|
|
|
|
|
|
|
m.connect(show, QtCore.SIGNAL("triggered()"), show_window)
|
2016-04-01 14:09:45 +00:00
|
|
|
m.connect(exit, QtCore.SIGNAL("triggered()"), lambda: app.exit())
|
|
|
|
self.tray.setContextMenu(m)
|
2016-03-15 19:12:37 +00:00
|
|
|
self.tray.show()
|
|
|
|
|
2016-03-15 17:05:19 +00:00
|
|
|
self.ms.show()
|
|
|
|
QtGui.QApplication.setStyle(get_style(settings['theme'])) # set application style
|
2016-03-10 20:04:43 +00:00
|
|
|
|
2016-03-15 17:05:19 +00:00
|
|
|
# init thread
|
2016-03-15 19:12:37 +00:00
|
|
|
self.init = self.InitThread(self.tox, self.ms, self.tray)
|
2016-03-15 17:05:19 +00:00
|
|
|
self.init.start()
|
2016-02-19 21:10:24 +00:00
|
|
|
|
2016-04-24 10:45:11 +00:00
|
|
|
# starting threads for tox iterate and toxav iterate
|
2016-03-15 17:05:19 +00:00
|
|
|
self.mainloop = self.ToxIterateThread(self.tox)
|
|
|
|
self.mainloop.start()
|
2016-04-24 10:45:11 +00:00
|
|
|
self.avloop = self.ToxAVIterateThread(self.tox.AV)
|
|
|
|
self.avloop.start()
|
2016-03-15 17:05:19 +00:00
|
|
|
app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app, QtCore.SLOT("quit()"))
|
|
|
|
app.exec_()
|
2016-03-15 20:35:15 +00:00
|
|
|
self.init.stop = True
|
2016-03-15 17:05:19 +00:00
|
|
|
self.mainloop.stop = True
|
2016-04-24 10:45:11 +00:00
|
|
|
self.avloop.stop = True
|
2016-03-15 17:05:19 +00:00
|
|
|
self.mainloop.wait()
|
2016-03-15 20:35:15 +00:00
|
|
|
self.init.wait()
|
2016-04-24 10:45:11 +00:00
|
|
|
self.avloop.wait()
|
2016-03-15 17:05:19 +00:00
|
|
|
data = self.tox.get_savedata()
|
2016-05-14 10:18:17 +00:00
|
|
|
ProfileHelper.get_instance().save_profile(data)
|
2016-04-12 13:11:10 +00:00
|
|
|
settings.close()
|
2016-03-15 17:05:19 +00:00
|
|
|
del self.tox
|
2016-02-19 15:04:53 +00:00
|
|
|
|
2016-03-15 17:05:19 +00:00
|
|
|
def reset(self):
|
|
|
|
"""
|
|
|
|
Create new tox instance (new network settings)
|
|
|
|
:return: tox instance
|
|
|
|
"""
|
|
|
|
self.mainloop.stop = True
|
2016-03-15 19:42:24 +00:00
|
|
|
self.init.stop = True
|
2016-04-24 10:45:11 +00:00
|
|
|
self.avloop.stop = True
|
2016-03-15 17:05:19 +00:00
|
|
|
self.mainloop.wait()
|
2016-03-15 19:42:24 +00:00
|
|
|
self.init.wait()
|
2016-04-24 10:45:11 +00:00
|
|
|
self.avloop.wait()
|
2016-03-15 17:05:19 +00:00
|
|
|
data = self.tox.get_savedata()
|
2016-05-14 10:18:17 +00:00
|
|
|
ProfileHelper.get_instance().save_profile(data)
|
2016-03-15 17:05:19 +00:00
|
|
|
del self.tox
|
|
|
|
# create new tox instance
|
2016-03-15 19:12:37 +00:00
|
|
|
self.tox = tox_factory(data, Settings.get_instance())
|
2016-03-15 17:05:19 +00:00
|
|
|
# init thread
|
2016-03-15 19:12:37 +00:00
|
|
|
self.init = self.InitThread(self.tox, self.ms, self.tray)
|
2016-03-15 17:05:19 +00:00
|
|
|
self.init.start()
|
|
|
|
|
2016-04-24 10:45:11 +00:00
|
|
|
# starting threads for tox iterate and toxav iterate
|
2016-03-15 17:05:19 +00:00
|
|
|
self.mainloop = self.ToxIterateThread(self.tox)
|
|
|
|
self.mainloop.start()
|
2016-04-24 10:45:11 +00:00
|
|
|
|
|
|
|
self.avloop = self.ToxAVIterateThread(self.tox.AV)
|
|
|
|
self.avloop.start()
|
2016-03-15 17:05:19 +00:00
|
|
|
return self.tox
|
|
|
|
|
2016-03-15 20:54:01 +00:00
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
|
|
|
# Inner classes
|
|
|
|
# -----------------------------------------------------------------------------------------------------------------
|
2016-03-15 17:05:19 +00:00
|
|
|
|
|
|
|
class InitThread(QtCore.QThread):
|
|
|
|
|
2016-03-15 19:12:37 +00:00
|
|
|
def __init__(self, tox, ms, tray):
|
2016-03-15 17:05:19 +00:00
|
|
|
QtCore.QThread.__init__(self)
|
2016-03-15 19:12:37 +00:00
|
|
|
self.tox, self.ms, self.tray = tox, ms, tray
|
2016-03-15 19:42:24 +00:00
|
|
|
self.stop = False
|
2016-03-15 17:05:19 +00:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
# initializing callbacks
|
2016-03-15 19:12:37 +00:00
|
|
|
init_callbacks(self.tox, self.ms, self.tray)
|
2016-03-15 17:05:19 +00:00
|
|
|
# bootstrap
|
2016-03-16 15:15:55 +00:00
|
|
|
try:
|
2016-03-15 17:05:19 +00:00
|
|
|
for data in node_generator():
|
2016-04-03 20:51:46 +00:00
|
|
|
if self.stop:
|
|
|
|
return
|
2016-03-15 17:05:19 +00:00
|
|
|
self.tox.bootstrap(*data)
|
2016-03-16 15:15:55 +00:00
|
|
|
except:
|
|
|
|
pass
|
2016-04-03 20:51:46 +00:00
|
|
|
for _ in xrange(10):
|
|
|
|
if self.stop:
|
|
|
|
return
|
|
|
|
self.msleep(1000)
|
|
|
|
while not self.tox.self_get_connection_status():
|
2016-03-16 15:15:55 +00:00
|
|
|
try:
|
|
|
|
for data in node_generator():
|
2016-04-03 20:51:46 +00:00
|
|
|
if self.stop:
|
|
|
|
return
|
2016-03-16 15:15:55 +00:00
|
|
|
self.tox.bootstrap(*data)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
self.msleep(5000)
|
2016-03-15 17:05:19 +00:00
|
|
|
|
|
|
|
class ToxIterateThread(QtCore.QThread):
|
|
|
|
|
|
|
|
def __init__(self, tox):
|
|
|
|
QtCore.QThread.__init__(self)
|
|
|
|
self.tox = tox
|
|
|
|
self.stop = False
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while not self.stop:
|
|
|
|
self.tox.iterate()
|
|
|
|
self.msleep(self.tox.iteration_interval())
|
|
|
|
|
2016-04-24 10:45:11 +00:00
|
|
|
class ToxAVIterateThread(QtCore.QThread):
|
|
|
|
|
|
|
|
def __init__(self, toxav):
|
|
|
|
QtCore.QThread.__init__(self)
|
|
|
|
self.toxav = toxav
|
|
|
|
self.stop = False
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while not self.stop:
|
|
|
|
self.toxav.iterate()
|
|
|
|
self.msleep(self.toxav.iteration_interval())
|
|
|
|
|
2016-03-15 17:05:19 +00:00
|
|
|
class Login(object):
|
|
|
|
|
|
|
|
def __init__(self, arr):
|
|
|
|
self.arr = arr
|
|
|
|
|
|
|
|
def login_screen_close(self, t, number=-1, default=False, name=None):
|
|
|
|
""" Function which processes data from login screen
|
|
|
|
:param t: 0 - window was closed, 1 - new profile was created, 2 - profile loaded
|
2016-04-14 12:01:59 +00:00
|
|
|
:param number: num of chosen profile in list (-1 by default)
|
|
|
|
:param default: was or not chosen profile marked as default
|
2016-03-15 17:05:19 +00:00
|
|
|
:param name: name of new profile
|
|
|
|
"""
|
|
|
|
self.t = t
|
|
|
|
self.num = number
|
|
|
|
self.default = default
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return self.arr[self.num]
|
2016-02-19 10:41:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-05-06 10:31:18 +00:00
|
|
|
if len(sys.argv) == 1:
|
|
|
|
toxygen = Toxygen()
|
|
|
|
else: # path to profile
|
|
|
|
toxygen = Toxygen(sys.argv[1])
|
2016-03-15 17:05:19 +00:00
|
|
|
toxygen.main()
|