profile load and saving update
This commit is contained in:
parent
b0e1aeb0f0
commit
1734ea02f9
5 changed files with 49 additions and 51 deletions
12
src/main.py
12
src/main.py
|
@ -32,7 +32,7 @@ class Toxygen(object):
|
||||||
if self.path is not None:
|
if self.path is not None:
|
||||||
path = os.path.dirname(self.path.encode(locale.getpreferredencoding())) + '/'
|
path = os.path.dirname(self.path.encode(locale.getpreferredencoding())) + '/'
|
||||||
name = os.path.basename(self.path.encode(locale.getpreferredencoding()))[:-4]
|
name = os.path.basename(self.path.encode(locale.getpreferredencoding()))[:-4]
|
||||||
data = ProfileHelper.open_profile(path, name)
|
data = ProfileHelper(path, name).open_profile()
|
||||||
settings = Settings(name)
|
settings = Settings(name)
|
||||||
self.tox = tox_factory(data, settings)
|
self.tox = tox_factory(data, settings)
|
||||||
else:
|
else:
|
||||||
|
@ -64,20 +64,20 @@ class Toxygen(object):
|
||||||
self.tox = tox_factory()
|
self.tox = tox_factory()
|
||||||
self.tox.self_set_name(_login.name if _login.name else 'Toxygen User')
|
self.tox.self_set_name(_login.name if _login.name else 'Toxygen User')
|
||||||
self.tox.self_set_status_message('Toxing on Toxygen')
|
self.tox.self_set_status_message('Toxing on Toxygen')
|
||||||
ProfileHelper.save_profile(self.tox.get_savedata(), name)
|
ProfileHelper(Settings.get_default_path(), name).save_profile(self.tox.get_savedata())
|
||||||
path = Settings.get_default_path()
|
path = Settings.get_default_path()
|
||||||
settings = Settings(name)
|
settings = Settings(name)
|
||||||
else: # load existing profile
|
else: # load existing profile
|
||||||
path, name = _login.get_data()
|
path, name = _login.get_data()
|
||||||
if _login.default:
|
if _login.default:
|
||||||
Settings.set_auto_profile(path, name)
|
Settings.set_auto_profile(path, name)
|
||||||
data = ProfileHelper.open_profile(path, name)
|
data = ProfileHelper(path, name).open_profile()
|
||||||
settings = Settings(name)
|
settings = Settings(name)
|
||||||
self.tox = tox_factory(data, settings)
|
self.tox = tox_factory(data, settings)
|
||||||
else:
|
else:
|
||||||
path, name = auto_profile
|
path, name = auto_profile
|
||||||
path = path.encode(locale.getpreferredencoding())
|
path = path.encode(locale.getpreferredencoding())
|
||||||
data = ProfileHelper.open_profile(path, name)
|
data = ProfileHelper(path, name).open_profile()
|
||||||
settings = Settings(name)
|
settings = Settings(name)
|
||||||
self.tox = tox_factory(data, settings)
|
self.tox = tox_factory(data, settings)
|
||||||
|
|
||||||
|
@ -146,7 +146,7 @@ class Toxygen(object):
|
||||||
self.init.wait()
|
self.init.wait()
|
||||||
self.avloop.wait()
|
self.avloop.wait()
|
||||||
data = self.tox.get_savedata()
|
data = self.tox.get_savedata()
|
||||||
ProfileHelper.save_profile(data)
|
ProfileHelper.get_instance().save_profile(data)
|
||||||
settings.close()
|
settings.close()
|
||||||
del self.tox
|
del self.tox
|
||||||
|
|
||||||
|
@ -162,7 +162,7 @@ class Toxygen(object):
|
||||||
self.init.wait()
|
self.init.wait()
|
||||||
self.avloop.wait()
|
self.avloop.wait()
|
||||||
data = self.tox.get_savedata()
|
data = self.tox.get_savedata()
|
||||||
ProfileHelper.save_profile(data)
|
ProfileHelper.get_instance().save_profile(data)
|
||||||
del self.tox
|
del self.tox
|
||||||
# create new tox instance
|
# create new tox instance
|
||||||
self.tox = tox_factory(data, Settings.get_instance())
|
self.tox = tox_factory(data, Settings.get_instance())
|
||||||
|
|
|
@ -165,7 +165,7 @@ class ProfileSettings(CenteredWidget):
|
||||||
def export_profile(self):
|
def export_profile(self):
|
||||||
directory = QtGui.QFileDialog.getExistingDirectory() + '/'
|
directory = QtGui.QFileDialog.getExistingDirectory() + '/'
|
||||||
if directory != '/':
|
if directory != '/':
|
||||||
ProfileHelper.export_profile(directory)
|
ProfileHelper.get_instance().export_profile(directory)
|
||||||
settings = Settings.get_instance()
|
settings = Settings.get_instance()
|
||||||
settings.export(directory)
|
settings.export(directory)
|
||||||
profile = Profile.get_instance()
|
profile = Profile.get_instance()
|
||||||
|
|
|
@ -742,7 +742,7 @@ class Profile(Contact, Singleton):
|
||||||
else:
|
else:
|
||||||
self.set_active(0)
|
self.set_active(0)
|
||||||
data = self._tox.get_savedata()
|
data = self._tox.get_savedata()
|
||||||
ProfileHelper.save_profile(data)
|
ProfileHelper.get_instance().save_profile(data)
|
||||||
|
|
||||||
def add_friend(self, tox_id):
|
def add_friend(self, tox_id):
|
||||||
num = self._tox.friend_add_norequest(tox_id) # num - friend number
|
num = self._tox.friend_add_norequest(tox_id) # num - friend number
|
||||||
|
@ -772,7 +772,7 @@ class Profile(Contact, Singleton):
|
||||||
num = self._tox.friend_by_public_key(tox_id)
|
num = self._tox.friend_by_public_key(tox_id)
|
||||||
self.delete_friend(num)
|
self.delete_friend(num)
|
||||||
data = self._tox.get_savedata()
|
data = self._tox.get_savedata()
|
||||||
ProfileHelper.save_profile(data)
|
ProfileHelper.get_instance().save_profile(data)
|
||||||
except: # not in friend list
|
except: # not in friend list
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -788,7 +788,7 @@ class Profile(Contact, Singleton):
|
||||||
if add_to_friend_list:
|
if add_to_friend_list:
|
||||||
self.add_friend(tox_id)
|
self.add_friend(tox_id)
|
||||||
data = self._tox.get_savedata()
|
data = self._tox.get_savedata()
|
||||||
ProfileHelper.save_profile(data)
|
ProfileHelper.get_instance().save_profile(data)
|
||||||
|
|
||||||
# -----------------------------------------------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------------------------------------------
|
||||||
# Friend requests
|
# Friend requests
|
||||||
|
@ -824,7 +824,7 @@ class Profile(Contact, Singleton):
|
||||||
friend = Friend(message_getter, result, tox_id, '', item, tox_id)
|
friend = Friend(message_getter, result, tox_id, '', item, tox_id)
|
||||||
self._friends.append(friend)
|
self._friends.append(friend)
|
||||||
data = self._tox.get_savedata()
|
data = self._tox.get_savedata()
|
||||||
ProfileHelper.save_profile(data)
|
ProfileHelper.get_instance().save_profile(data)
|
||||||
return True
|
return True
|
||||||
except Exception as ex: # wrong data
|
except Exception as ex: # wrong data
|
||||||
log('Friend request failed with ' + str(ex))
|
log('Friend request failed with ' + str(ex))
|
||||||
|
@ -844,7 +844,7 @@ class Profile(Contact, Singleton):
|
||||||
if reply == QtGui.QMessageBox.Yes: # accepted
|
if reply == QtGui.QMessageBox.Yes: # accepted
|
||||||
self.add_friend(tox_id)
|
self.add_friend(tox_id)
|
||||||
data = self._tox.get_savedata()
|
data = self._tox.get_savedata()
|
||||||
ProfileHelper.save_profile(data)
|
ProfileHelper.get_instance().save_profile(data)
|
||||||
except Exception as ex: # something is wrong
|
except Exception as ex: # something is wrong
|
||||||
log('Accept friend request failed! ' + str(ex))
|
log('Accept friend request failed! ' + str(ex))
|
||||||
|
|
||||||
|
|
|
@ -136,11 +136,44 @@ class Settings(Singleton, dict):
|
||||||
return os.getenv('APPDATA') + '/Tox/'
|
return os.getenv('APPDATA') + '/Tox/'
|
||||||
|
|
||||||
|
|
||||||
# TODO: singleton (with encryption)
|
# TODO: encrypted profiles support
|
||||||
class ProfileHelper(object):
|
class ProfileHelper(Singleton):
|
||||||
"""
|
"""
|
||||||
Class with static methods for search, load and save profiles
|
Class with methods for search, load and save profiles
|
||||||
"""
|
"""
|
||||||
|
def __init__(self, path, name):
|
||||||
|
path = path.decode(locale.getpreferredencoding())
|
||||||
|
self._path = path + name + '.tox'
|
||||||
|
self._directory = path
|
||||||
|
# create /avatars if not exists:
|
||||||
|
directory = path + 'avatars'
|
||||||
|
if not os.path.exists(directory):
|
||||||
|
os.makedirs(directory)
|
||||||
|
|
||||||
|
def open_profile(self):
|
||||||
|
with open(self._path, 'rb') as fl:
|
||||||
|
data = fl.read()
|
||||||
|
if data:
|
||||||
|
return data
|
||||||
|
else:
|
||||||
|
raise IOError('Save file has zero size!')
|
||||||
|
|
||||||
|
def get_dir(self):
|
||||||
|
return self._directory
|
||||||
|
|
||||||
|
def save_profile(self, data):
|
||||||
|
with open(self._path, 'wb') as fl:
|
||||||
|
fl.write(data)
|
||||||
|
print 'Profile saved successfully'
|
||||||
|
|
||||||
|
def export_profile(self, new_path):
|
||||||
|
new_path += os.path.basename(self._path)
|
||||||
|
with open(self._path, 'rb') as fin:
|
||||||
|
data = fin.read()
|
||||||
|
with open(new_path, 'wb') as fout:
|
||||||
|
fout.write(data)
|
||||||
|
print 'Profile exported successfully'
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def find_profiles():
|
def find_profiles():
|
||||||
path = Settings.get_default_path()
|
path = Settings.get_default_path()
|
||||||
|
@ -173,39 +206,6 @@ class ProfileHelper(object):
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def open_profile(path, name):
|
|
||||||
path = path.decode(locale.getpreferredencoding())
|
|
||||||
ProfileHelper._path = path + name + '.tox'
|
|
||||||
ProfileHelper._directory = path
|
|
||||||
# create /avatars if not exists:
|
|
||||||
directory = path + 'avatars'
|
|
||||||
if not os.path.exists(directory):
|
|
||||||
os.makedirs(directory)
|
|
||||||
with open(ProfileHelper._path, 'rb') as fl:
|
|
||||||
data = fl.read()
|
|
||||||
if data:
|
|
||||||
return data
|
|
||||||
else:
|
|
||||||
raise IOError('Save file has zero size!')
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def save_profile(data, name=None):
|
|
||||||
if name is not None:
|
|
||||||
ProfileHelper._path = Settings.get_default_path() + name + '.tox'
|
|
||||||
ProfileHelper._directory = Settings.get_default_path()
|
|
||||||
with open(ProfileHelper._path, 'wb') as fl:
|
|
||||||
fl.write(data)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def export_profile(new_path):
|
|
||||||
new_path += os.path.basename(ProfileHelper._path)
|
|
||||||
with open(ProfileHelper._path, 'rb') as fin:
|
|
||||||
data = fin.read()
|
|
||||||
with open(new_path, 'wb') as fout:
|
|
||||||
fout.write(data)
|
|
||||||
print 'Data exported to: {}'.format(new_path)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_path():
|
def get_path():
|
||||||
return ProfileHelper._directory
|
return ProfileHelper.get_instance().get_dir()
|
||||||
|
|
|
@ -13,5 +13,3 @@ class LibToxEncryptSave(util.Singleton):
|
||||||
|
|
||||||
def set_password(self, value):
|
def set_password(self, value):
|
||||||
self._password = value
|
self._password = value
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue