toxygen/src/main.py

119 lines
3.4 KiB
Python
Raw Normal View History

2016-02-18 16:15:38 +00:00
from loginscreen import LoginScreen
from settings import Settings
from mainscreen import MainWindow
2016-02-20 18:55:58 +00:00
from profile import Profile, tox_factory
2016-02-18 16:15:38 +00:00
import sys
from PySide import QtCore, QtGui
2016-02-20 18:55:58 +00:00
from tox import Tox
2016-02-20 11:50:49 +00:00
from bootstrap import node_generator
2016-02-18 16:15:38 +00:00
class login(object):
2016-02-19 15:04:53 +00:00
def __init__(self, arr):
self.arr = arr
def login_screen_close(self, t, number=-1, default=False, name=None):
2016-02-19 15:04:53 +00:00
""" Function which processes data from login screen
:param t: 0 - window was closed, 1 - new profile was created, 2 - profile loaded
:param number: num of choosen profile in list (-1 by default)
:param default: was or not choosen profile marked as default
:param name: name of new profile
"""
print str(t), str(number), str(default), str(name)
self.t = t
self.num = number
self.default = default
self.name = name
def get_data(self):
return self.arr[self.num]
2016-02-19 21:10:24 +00:00
def status(a, b, c):
print 'WOW, it works!'
print str(b)
2016-02-20 20:50:10 +00:00
def friend_status(a, b, c, d, e):
print 'Friend connected! Friend number: ' + str(c)
def message(a, b, c, d, e, f):
print 'Message: ', str(d)
2016-02-20 11:50:49 +00:00
def main():
2016-02-19 15:04:53 +00:00
"""
2016-02-20 20:50:10 +00:00
main function of app. loads loginscreen if needed and starts mainscreen
2016-02-19 15:04:53 +00:00
"""
2016-02-18 16:15:38 +00:00
app = QtGui.QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('images/icon.png'))
2016-02-18 16:15:38 +00:00
settings = Settings()
if not settings['auto_profile']:
# show login screen if default profile not found
ls = LoginScreen()
ls.setWindowIconText("Toxygen")
profiles = Profile.find_profiles()
ls.update_select(map(lambda x: x[1], profiles))
_login = login(profiles)
ls.update_on_close(_login.login_screen_close)
ls.show()
2016-02-18 16:15:38 +00:00
app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app, QtCore.SLOT("quit()"))
app.exec_()
if not _login.t:
return
elif _login.t == 1: # create new profile
# TODO: add creation of new profile
path = Settings.get_default_path()
name = _login.name if _login.name else 'Toxygen User'
return
else: # load existing profile
path, name = _login.get_data()
if _login.default:
settings['auto_profile'] = (path, name)
settings.save()
else:
path, name = settings['auto_profile']
# loading profile
print str(path), str(name)
data = Profile.open_profile(path, name)
ms = MainWindow()
2016-02-19 21:10:24 +00:00
# creating tox instance
tox = tox_factory(data, settings)
2016-02-20 11:50:49 +00:00
# bootstrap
for data in node_generator():
tox.bootstrap(*data)
2016-02-20 20:50:10 +00:00
# TODO: set all callbacks
tox.callback_friend_message(message, 0)
2016-02-20 11:50:49 +00:00
tox.callback_self_connection_status(status, 0)
2016-02-20 20:50:10 +00:00
#tox.callback_friend_connection_status(friend_status, 0)
2016-02-19 21:10:24 +00:00
# starting thread for tox iterate
mainloop = ToxIterateThread(tox)
mainloop.start()
ms.show()
app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app, QtCore.SLOT("quit()"))
app.exec_()
2016-02-19 21:10:24 +00:00
mainloop.stop = True
mainloop.wait()
2016-02-19 15:04:53 +00:00
del tox
class ToxIterateThread(QtCore.QThread):
2016-02-19 21:10:24 +00:00
def __init__(self, tox):
2016-02-19 15:04:53 +00:00
QtCore.QThread.__init__(self)
2016-02-19 21:10:24 +00:00
self.tox = tox
self.stop = False
2016-02-19 15:04:53 +00:00
def run(self):
2016-02-19 21:10:24 +00:00
while not self.stop:
self.tox.iterate()
self.msleep(self.tox.iteration_interval())
if __name__ == '__main__':
main()