From 1a996522f194cc1d391f0a5151fd543cfc00df3a Mon Sep 17 00:00:00 2001 From: ingvar1995 Date: Wed, 16 Mar 2016 22:59:15 +0300 Subject: [PATCH] profile.py updates - avatars and file transfers --- src/callbacks.py | 3 ++- src/mainscreen.py | 9 ++++++++- src/menu.py | 2 +- src/profile.py | 30 +++++++++++++++++++++++++++--- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/callbacks.py b/src/callbacks.py index fb8768f..a80fb5d 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -152,7 +152,8 @@ def tox_file_recv(window, tray): print 'Avatar' invoke_in_main_thread(profile.incoming_avatar, friend_number, - file_number) + file_number, + size) return wrapped # ----------------------------------------------------------------------------------------------------------------- diff --git a/src/mainscreen.py b/src/mainscreen.py index 7c56f2f..93a1c43 100644 --- a/src/mainscreen.py +++ b/src/mainscreen.py @@ -102,6 +102,7 @@ class MainWindow(QtGui.QMainWindow): self.sendMessageButton.setGeometry(QtCore.QRect(550, 10, 60, 110)) self.sendMessageButton.setObjectName("sendMessageButton") self.sendMessageButton.clicked.connect(self.send_message) + self.fileTransferButton.clicked.connect(self.send_file) self.screenshotButton.setText(QtGui.QApplication.translate("Form", "Screenshot", None, QtGui.QApplication.UnicodeUTF8)) self.fileTransferButton.setText(QtGui.QApplication.translate("Form", "File", None, QtGui.QApplication.UnicodeUTF8)) self.sendMessageButton.setText(QtGui.QApplication.translate("Form", "Send", None, QtGui.QApplication.UnicodeUTF8)) @@ -268,13 +269,19 @@ class MainWindow(QtGui.QMainWindow): self.int_s.show() # ----------------------------------------------------------------------------------------------------------------- - # Messages + # Messages and file transfers # ----------------------------------------------------------------------------------------------------------------- def send_message(self): text = self.messageEdit.toPlainText() self.profile.send_message(text) + def send_file(self): + if self.profile.is_active_online(): # active friend exists and online + name = QtGui.QFileDialog.getOpenFileName(self, 'Choose file') + if name: + self.profile.send_file(name) + # ----------------------------------------------------------------------------------------------------------------- # Functions which called when user open context menu in friends list # ----------------------------------------------------------------------------------------------------------------- diff --git a/src/menu.py b/src/menu.py index aa54276..60c92e0 100644 --- a/src/menu.py +++ b/src/menu.py @@ -165,7 +165,7 @@ class ProfileSettings(CenteredWidget): Profile.get_instance().reset_avatar() def set_avatar(self): - name = QtGui.QFileDialog.getOpenFileName(self, 'Open file') + name = QtGui.QFileDialog.getOpenFileName(self, 'Open file', None, 'Image Files (*.png *.jpg *.bmp)') print name if name[0]: with open(name[0], 'rb') as f: diff --git a/src/profile.py b/src/profile.py index a8fcd70..26157fd 100644 --- a/src/profile.py +++ b/src/profile.py @@ -56,7 +56,7 @@ class ProfileHelper(object): @staticmethod def export_profile(new_path): - new_path += ProfileHelper._path.split('/')[-1] + new_path += os.path.basename(ProfileHelper._path) with open(ProfileHelper._path, 'rb') as fin: data = fin.read() with open(new_path, 'wb') as fout: @@ -167,6 +167,14 @@ class Contact(object): f.write(avatar) self.load_avatar() + def get_avatar_hash(self): + avatar_path = (Settings.get_default_path() + 'avatars/{}.png').format(self._tox_id[:TOX_PUBLIC_KEY_SIZE * 2]) + if not os.path.isfile(avatar_path): # load default image + avatar_path = curr_directory() + '/images/avatar.png' + with open(avatar_path, 'rb') as fl: + data = fl.read() + return Tox.hash(data) + class Friend(Contact): """ @@ -304,7 +312,7 @@ class Profile(Contact, Singleton): self._screen = screen self._messages = screen.messages self._tox = tox - self._file_transfers = [] # list of file transfers + self._file_transfers = [] # list of file transfers settings = Settings.get_instance() self._show_online = settings['show_online_friends'] screen.online_contacts.setChecked(self._show_online) @@ -696,7 +704,23 @@ class Profile(Contact, Singleton): def incoming_file_transfer(self, friend_number, file_number, size, file_name): pass - def incoming_avatar(self, friend_number, file_number): + def incoming_avatar(self, friend_number, file_number, size): + friend = self.get_friend_by_number(friend_number) + if not size: + friend.reset_avatar() + self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['CANCEL']) + else: + hash = friend.get_avatar_hash() + new_avatar_hash = self._tox.file_get_file_id(friend_number, file_number) + if hash == new_avatar_hash: # avatar is the same + self._tox.file_control(friend_number, file_number, TOX_FILE_CONTROL['CANCEL']) # ignore file + else: + pass + + def send_avatar(self, friend_number): + pass + + def send_file(self, path): pass