profile load and saving update

This commit is contained in:
ingvar1995 2016-05-14 13:18:17 +03:00
parent b0e1aeb0f0
commit 1734ea02f9
5 changed files with 49 additions and 51 deletions

View file

@ -32,7 +32,7 @@ class Toxygen(object):
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]
data = ProfileHelper.open_profile(path, name)
data = ProfileHelper(path, name).open_profile()
settings = Settings(name)
self.tox = tox_factory(data, settings)
else:
@ -64,20 +64,20 @@ class Toxygen(object):
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')
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()
settings = Settings(name)
else: # load existing profile
path, name = _login.get_data()
if _login.default:
Settings.set_auto_profile(path, name)
data = ProfileHelper.open_profile(path, name)
data = ProfileHelper(path, name).open_profile()
settings = Settings(name)
self.tox = tox_factory(data, settings)
else:
path, name = auto_profile
path = path.encode(locale.getpreferredencoding())
data = ProfileHelper.open_profile(path, name)
data = ProfileHelper(path, name).open_profile()
settings = Settings(name)
self.tox = tox_factory(data, settings)
@ -146,7 +146,7 @@ class Toxygen(object):
self.init.wait()
self.avloop.wait()
data = self.tox.get_savedata()
ProfileHelper.save_profile(data)
ProfileHelper.get_instance().save_profile(data)
settings.close()
del self.tox
@ -162,7 +162,7 @@ class Toxygen(object):
self.init.wait()
self.avloop.wait()
data = self.tox.get_savedata()
ProfileHelper.save_profile(data)
ProfileHelper.get_instance().save_profile(data)
del self.tox
# create new tox instance
self.tox = tox_factory(data, Settings.get_instance())

View file

@ -165,7 +165,7 @@ class ProfileSettings(CenteredWidget):
def export_profile(self):
directory = QtGui.QFileDialog.getExistingDirectory() + '/'
if directory != '/':
ProfileHelper.export_profile(directory)
ProfileHelper.get_instance().export_profile(directory)
settings = Settings.get_instance()
settings.export(directory)
profile = Profile.get_instance()

View file

@ -742,7 +742,7 @@ class Profile(Contact, Singleton):
else:
self.set_active(0)
data = self._tox.get_savedata()
ProfileHelper.save_profile(data)
ProfileHelper.get_instance().save_profile(data)
def add_friend(self, tox_id):
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)
self.delete_friend(num)
data = self._tox.get_savedata()
ProfileHelper.save_profile(data)
ProfileHelper.get_instance().save_profile(data)
except: # not in friend list
pass
@ -788,7 +788,7 @@ class Profile(Contact, Singleton):
if add_to_friend_list:
self.add_friend(tox_id)
data = self._tox.get_savedata()
ProfileHelper.save_profile(data)
ProfileHelper.get_instance().save_profile(data)
# -----------------------------------------------------------------------------------------------------------------
# Friend requests
@ -824,7 +824,7 @@ class Profile(Contact, Singleton):
friend = Friend(message_getter, result, tox_id, '', item, tox_id)
self._friends.append(friend)
data = self._tox.get_savedata()
ProfileHelper.save_profile(data)
ProfileHelper.get_instance().save_profile(data)
return True
except Exception as ex: # wrong data
log('Friend request failed with ' + str(ex))
@ -844,7 +844,7 @@ class Profile(Contact, Singleton):
if reply == QtGui.QMessageBox.Yes: # accepted
self.add_friend(tox_id)
data = self._tox.get_savedata()
ProfileHelper.save_profile(data)
ProfileHelper.get_instance().save_profile(data)
except Exception as ex: # something is wrong
log('Accept friend request failed! ' + str(ex))

View file

@ -136,11 +136,44 @@ class Settings(Singleton, dict):
return os.getenv('APPDATA') + '/Tox/'
# TODO: singleton (with encryption)
class ProfileHelper(object):
# TODO: encrypted profiles support
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
def find_profiles():
path = Settings.get_default_path()
@ -173,39 +206,6 @@ class ProfileHelper(object):
else:
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
def get_path():
return ProfileHelper._directory
return ProfileHelper.get_instance().get_dir()

View file

@ -13,5 +13,3 @@ class LibToxEncryptSave(util.Singleton):
def set_password(self, value):
self._password = value