various fixes - peers list, resize event, tox instance recreation

This commit is contained in:
ingvar1995 2018-05-20 15:57:08 +03:00
parent 02af0f7671
commit b8fa8df41a
18 changed files with 97 additions and 51 deletions

View file

@ -291,7 +291,11 @@ class App:
self._tox = self._create_tox(data) self._tox = self._create_tox(data)
self._start_threads() self._start_threads()
# TODO: foreach in list of tox savers set_tox tox_savers = [self._friend_factory, self._group_factory, self._plugin_loader, self._contacts_manager,
self._contacts_provider, self._messenger, self._file_transfer_handler, self._groups_service]
for tox_saver in tox_savers:
tox_saver.set_tox(self._tox)
self._calls_manager.set_toxav(self._tox.AV)
return self._tox return self._tox
@ -327,7 +331,7 @@ class App:
self._groups_service = GroupsService(self._tox, self._contacts_manager, self._contacts_provider, self._ms) self._groups_service = GroupsService(self._tox, self._contacts_manager, self._contacts_provider, self._ms)
widgets_factory = WidgetsFactory(self._settings, profile, self._profile_manager, self._contacts_manager, widgets_factory = WidgetsFactory(self._settings, profile, self._profile_manager, self._contacts_manager,
self._file_transfer_handler, self._smiley_loader, self._plugin_loader, self._file_transfer_handler, self._smiley_loader, self._plugin_loader,
self._toxes, self._version, self._groups_service) self._toxes, self._version, self._groups_service, history)
self._tray = tray.init_tray(profile, self._settings, self._ms, self._toxes) self._tray = tray.init_tray(profile, self._settings, self._ms, self._toxes)
self._ms.set_dependencies(widgets_factory, self._tray, self._contacts_manager, self._messenger, profile, self._ms.set_dependencies(widgets_factory, self._tray, self._contacts_manager, self._messenger, profile,
self._plugin_loader, self._file_transfer_handler, history, self._calls_manager, self._plugin_loader, self._file_transfer_handler, history, self._calls_manager,

View file

@ -36,6 +36,9 @@ class AV:
self._video_width = 640 self._video_width = 640
self._video_height = 480 self._video_height = 480
def set_toxav(self, toxav):
self._toxav = toxav
def stop(self): def stop(self):
self._running = False self._running = False
self.stop_audio_thread() self.stop_audio_thread()

View file

@ -18,6 +18,9 @@ class CallsManager:
self._call_started_event = event.Event() # friend_number, audio, video, is_outgoing self._call_started_event = event.Event() # friend_number, audio, video, is_outgoing
self._call_finished_event = event.Event() # friend_number, is_declined self._call_finished_event = event.Event() # friend_number, is_declined
def set_toxav(self, toxav):
self._call.set_toxav(toxav)
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
# Events # Events
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------

View file

@ -1,16 +1,17 @@
from contacts.friend import Friend from contacts.friend import Friend
from contacts.group_chat import GroupChat from contacts.group_chat import GroupChat
from messenger.messages import * from messenger.messages import *
from common.tox_save import ToxSave
class ContactsManager: class ContactsManager(ToxSave):
""" """
Represents contacts list. Represents contacts list.
""" """
def __init__(self, tox, settings, screen, profile_manager, contact_provider, history, tox_dns, def __init__(self, tox, settings, screen, profile_manager, contact_provider, history, tox_dns,
messages_items_factory): messages_items_factory):
self._tox = tox super().__init__(tox)
self._settings = settings self._settings = settings
self._screen = screen self._screen = screen
self._profile_manager = profile_manager self._profile_manager = profile_manager
@ -19,6 +20,7 @@ class ContactsManager:
self._messages_items_factory = messages_items_factory self._messages_items_factory = messages_items_factory
self._messages = screen.messages self._messages = screen.messages
self._contacts, self._active_contact = [], -1 self._contacts, self._active_contact = [], -1
self._active_contact_changed = Event()
self._sorting = settings['sorting'] self._sorting = settings['sorting']
self._filter_string = '' self._filter_string = ''
self._friend_item_height = 40 if settings['compact_mode'] else 70 self._friend_item_height = 40 if settings['compact_mode'] else 70
@ -115,13 +117,18 @@ class ContactsManager:
# else: # else:
# self._screen.call_finished() # self._screen.call_finished()
self._set_current_contact_data(contact) self._set_current_contact_data(contact)
self._active_contact_changed(contact)
except Exception as ex: # no friend found. ignore except Exception as ex: # no friend found. ignore
util.log('Friend value: ' + str(value)) util.log('Friend value: ' + str(value))
util.log('Error in set active: ' + str(ex)) util.log('Error in set active: ' + str(ex))
raise raise
active_friend = property(get_active, set_active) active_contact = property(get_active, set_active)
def get_active_contact_changed(self):
return self._active_contact_changed
active_contact_changed = property(get_active_contact_changed)
def set_active_by_number_and_type(self, number, is_friend): # TODO: by id def set_active_by_number_and_type(self, number, is_friend): # TODO: by id
for i in range(len(self._contacts)): for i in range(len(self._contacts)):

View file

@ -1,11 +1,13 @@
from contacts.friend import Friend from contacts.friend import Friend
from common.tox_save import ToxSave
class FriendFactory: class FriendFactory(ToxSave):
def __init__(self, profile_manager, settings, tox, db, items_factory): def __init__(self, profile_manager, settings, tox, db, items_factory):
super().__init__(tox)
self._profile_manager = profile_manager self._profile_manager = profile_manager
self._settings, self._tox = settings, tox self._settings = settings
self._db = db self._db = db
self._items_factory = items_factory self._items_factory = items_factory

View file

@ -3,13 +3,14 @@ from contacts.contact_menu import GroupMenuGenerator
import utils.util as util import utils.util as util
from groups.group_peer import GroupChatPeer from groups.group_peer import GroupChatPeer
from wrapper import toxcore_enums_and_consts as constants from wrapper import toxcore_enums_and_consts as constants
from common.tox_save import ToxSave
class GroupChat(contact.Contact): class GroupChat(contact.Contact, ToxSave):
def __init__(self, tox, profile_manager, message_getter, number, name, status_message, widget, tox_id): def __init__(self, tox, profile_manager, message_getter, number, name, status_message, widget, tox_id):
super().__init__(profile_manager, message_getter, number, name, status_message, widget, tox_id) super().__init__(profile_manager, message_getter, number, name, status_message, widget, tox_id)
self._tox = tox ToxSave.__init__(self, tox)
self.set_status(constants.TOX_USER_STATUS['NONE']) self.set_status(constants.TOX_USER_STATUS['NONE'])
self._peers = [] self._peers = []
self._add_self_to_gc() self._add_self_to_gc()

View file

@ -1,11 +1,13 @@
from contacts.group_chat import GroupChat from contacts.group_chat import GroupChat
from common.tox_save import ToxSave
class GroupFactory: class GroupFactory(ToxSave):
def __init__(self, profile_manager, settings, tox, db, items_factory): def __init__(self, profile_manager, settings, tox, db, items_factory):
super().__init__(tox)
self._profile_manager = profile_manager self._profile_manager = profile_manager
self._settings, self._tox = settings, tox self._settings = settings
self._db = db self._db = db
self._items_factory = items_factory self._items_factory = items_factory

View file

@ -67,7 +67,7 @@ class Profile(basecontact.BaseContact):
# Reset # Reset
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
def _restart(self): def restart(self):
""" """
Recreate tox instance Recreate tox instance
""" """
@ -80,6 +80,6 @@ class Profile(basecontact.BaseContact):
contacts = self._contacts_provider.get_all() contacts = self._contacts_provider.get_all()
if self.status is None or all(list(map(lambda x: x.status is None, contacts))) and len(contacts): if self.status is None or all(list(map(lambda x: x.status is None, contacts))) and len(contacts):
self._waiting_for_reconnection = True self._waiting_for_reconnection = True
self._restart() self.restart()
self._timer = threading.Timer(50, self._reconnect) self._timer = threading.Timer(50, self._reconnect)
self._timer.start() self._timer.start()

View file

@ -1,12 +1,13 @@
from messenger.messages import * from messenger.messages import *
from ui.contact_items import * from ui.contact_items import *
import utils.util as util import utils.util as util
from common.tox_save import ToxSave
class FileTransfersHandler: class FileTransfersHandler(ToxSave):
def __init__(self, tox, settings, contact_provider, file_transfers_message_service, profile): def __init__(self, tox, settings, contact_provider, file_transfers_message_service, profile):
self._tox = tox super().__init__(tox)
self._settings = settings self._settings = settings
self._contact_provider = contact_provider self._contact_provider = contact_provider
self._file_transfers_message_service = file_transfers_message_service self._file_transfers_message_service = file_transfers_message_service

View file

@ -11,6 +11,11 @@ class GroupsService(tox_save.ToxSave):
self._contacts_provider = contacts_provider self._contacts_provider = contacts_provider
self._peers_list_widget = main_screen.peers_list self._peers_list_widget = main_screen.peers_list
def set_tox(self, tox):
super().set_tox(tox)
for group in self._get_all_groups():
group.set_tox(tox)
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
# Groups creation # Groups creation
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
@ -81,3 +86,6 @@ class GroupsService(tox_save.ToxSave):
def _get_friend(self, friend_number): def _get_friend(self, friend_number):
return self._contacts_provider.get_friend_by_number(friend_number) return self._contacts_provider.get_friend_by_number(friend_number)
def _get_all_groups(self):
return self._contacts_provider.get_all_groups()

View file

@ -2,6 +2,8 @@ from PyQt5 import QtWidgets, QtCore
from ui.group_peers_list import PeerItem, PeerTypeItem from ui.group_peers_list import PeerItem, PeerTypeItem
import utils.ui as util_ui import utils.ui as util_ui
from wrapper.toxcore_enums_and_consts import * from wrapper.toxcore_enums_and_consts import *
from ui.widgets import *
# ----------------------------------------------------------------------------------------------------------------- # -----------------------------------------------------------------------------------------------------------------
# Builder # Builder
@ -47,16 +49,16 @@ class PeerListBuilder:
return parent return parent
def _add_peer_item(self, peer, parent): def _add_peer_item(self, peer, parent):
item = PeerItem(peer, self._handler, parent.width()) item = PeerItem(peer, self._handler, parent.width(), parent)
self._add_item(parent, item) self._add_item(parent, item)
def _add_peer_type_item(self, text, parent): def _add_peer_type_item(self, text, parent):
item = PeerTypeItem(text, parent.width()) item = PeerTypeItem(text, parent.width(), parent)
self._add_item(parent, item) self._add_item(parent, item)
@staticmethod @staticmethod
def _add_item(parent, item): def _add_item(parent, item):
elem = QtWidgets.QListWidgetItem() elem = QtWidgets.QListWidgetItem(parent)
elem.setSizeHint(QtCore.QSize(parent.width(), item.height())) elem.setSizeHint(QtCore.QSize(parent.width(), item.height()))
parent.addItem(elem) parent.addItem(elem)
parent.setItemWidget(elem, item) parent.setItemWidget(elem, item)

View file

@ -1,28 +1,26 @@
import utils.util as util import utils.util as util
from contacts import profile
import os import os
import importlib import importlib
import inspect import inspect
import plugins.plugin_super_class as pl import plugins.plugin_super_class as pl
from user_data import toxes
import sys import sys
from common.tox_save import ToxSave
class PluginLoader(): class PluginLoader(ToxSave):
def __init__(self, tox, toxes, profile, settings): def __init__(self, tox, toxes, profile, settings):
super().__init__() super().__init__(tox)
self._profile = profile self._profile = profile
self._settings = settings self._settings = settings
self._plugins = {} # dict. key - plugin unique short name, value - tuple (plugin instance, is active) self._plugins = {} # dict. key - plugin unique short name, value - tuple (plugin instance, is active)
self._tox = tox
self._toxes = toxes self._toxes = toxes
def set_tox(self, tox): def set_tox(self, tox):
""" """
New tox instance New tox instance
""" """
self._tox = tox super().set_tox(tox)
for value in self._plugins.values(): for value in self._plugins.values():
value[0].set_tox(tox) value[0].set_tox(tox)

View file

@ -8,7 +8,7 @@ class PeerItem(QtWidgets.QWidget):
super().__init__(parent) super().__init__(parent)
self.resize(QtCore.QSize(width, 34)) self.resize(QtCore.QSize(width, 34))
self.nameLabel = DataLabel(self) self.nameLabel = DataLabel(self)
self.nameLabel.setGeometry(0, 0, width, 34) self.nameLabel.setGeometry(5, 0, width - 5, 34)
name = peer.name name = peer.name
if peer.is_current_user: if peer.is_current_user:
name += util_ui.tr(' (You)') name += util_ui.tr(' (You)')
@ -29,5 +29,5 @@ class PeerTypeItem(QtWidgets.QWidget):
super().__init__(parent) super().__init__(parent)
self.resize(QtCore.QSize(width, 34)) self.resize(QtCore.QSize(width, 34))
self.nameLabel = DataLabel(self) self.nameLabel = DataLabel(self)
self.nameLabel.setGeometry(0, 0, width, 34) self.nameLabel.setGeometry(5, 0, width - 5, 34)
self.nameLabel.setText(text) self.nameLabel.setText(text)

View file

@ -33,6 +33,7 @@ class MainWindow(QtWidgets.QMainWindow):
self._history_loader = history_loader self._history_loader = history_loader
self._calls_manager = calls_manager self._calls_manager = calls_manager
self._groups_service = groups_service self._groups_service = groups_service
self._contacts_manager.active_contact_changed.add_callback(self._new_contact_selected)
self.messageEdit.set_messenger(messenger) self.messageEdit.set_messenger(messenger)
def show(self): def show(self):
@ -284,6 +285,7 @@ class MainWindow(QtWidgets.QMainWindow):
pixmap = QtGui.QPixmap(util.join_path(util.get_images_directory(), 'menu.png')) pixmap = QtGui.QPixmap(util.join_path(util.get_images_directory(), 'menu.png'))
icon = QtGui.QIcon(pixmap) icon = QtGui.QIcon(pixmap)
self.groupMenuButton.setIcon(icon) self.groupMenuButton.setIcon(icon)
self.groupMenuButton.setIconSize(QtCore.QSize(45, 60))
self.update_call_state('call') self.update_call_state('call')
self.typing = QtWidgets.QLabel(Form) self.typing = QtWidgets.QLabel(Form)
self.typing.setGeometry(QtCore.QRect(500, 25, 50, 30)) self.typing.setGeometry(QtCore.QRect(500, 25, 50, 30))
@ -331,6 +333,8 @@ class MainWindow(QtWidgets.QMainWindow):
self.peers_list.setSpacing(1) self.peers_list.setSpacing(1)
self.peers_list.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded) self.peers_list.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.peers_list.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.peers_list.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.peers_list.verticalScrollBar().setContextMenuPolicy(QtCore.Qt.NoContextMenu)
self.peers_list.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
def initUI(self): def initUI(self):
self.setMinimumSize(920, 500) self.setMinimumSize(920, 500)
@ -431,6 +435,7 @@ class MainWindow(QtWidgets.QMainWindow):
self.account_name.setGeometry(QtCore.QRect(100, 15, self.width() - 560, 25)) self.account_name.setGeometry(QtCore.QRect(100, 15, self.width() - 560, 25))
self.account_status.setGeometry(QtCore.QRect(100, 35, self.width() - 560, 25)) self.account_status.setGeometry(QtCore.QRect(100, 35, self.width() - 560, 25))
self.messageEdit.setFocus() self.messageEdit.setFocus()
self._contacts_manager.update()
def keyPressEvent(self, event): def keyPressEvent(self, event):
key, modifiers = event.key(), event.modifiers() key, modifiers = event.key(), event.modifiers()
@ -564,7 +569,7 @@ class MainWindow(QtWidgets.QMainWindow):
def send_smiley(self): def send_smiley(self):
self.menu.hide() self.menu.hide()
if self._contacts_manager.active_friend + 1: if self._contacts_manager.get_curr_contact() is not None:
self.smiley = self._widget_factory.create_smiley_window(self) self.smiley = self._widget_factory.create_smiley_window(self)
self.smiley.setGeometry(QtCore.QRect(self.x() if self._settings['mirror_mode'] else 270 + self.x(), self.smiley.setGeometry(QtCore.QRect(self.x() if self._settings['mirror_mode'] else 270 + self.x(),
self.y() + self.height() - 200, self.y() + self.height() - 200,
@ -687,11 +692,8 @@ class MainWindow(QtWidgets.QMainWindow):
def friend_click(self, index): def friend_click(self, index):
num = index.row() num = index.row()
self._contacts_manager.set_active(num) self._contacts_manager.active_contact = num
self.groupMenuButton.setVisible(not self._contacts_manager.is_active_a_friend()) self.groupMenuButton.setVisible(not self._contacts_manager.is_active_a_friend())
if self._should_show_group_peers_list:
self._toggle_gc_peers_list()
self.resizeEvent()
def mouseReleaseEvent(self, event): def mouseReleaseEvent(self, event):
pos = self.connection_status.pos() pos = self.connection_status.pos()
@ -711,14 +713,21 @@ class MainWindow(QtWidgets.QMainWindow):
return return
if self._contacts_manager.get_curr_friend() is None: if self._contacts_manager.get_curr_friend() is None:
return return
self.search_field = SearchScreen(self.messages, self.messages.width(), self.messages.parent()) self.search_field = self._widget_factory.create_search_screen(self.messages)
x, y = self.messages.x(), self.messages.y() + self.messages.height() - 40 x, y = self.messages.x(), self.messages.y() + self.messages.height() - 40
self.search_field.setGeometry(x, y, self.messages.width(), 40) self.search_field.setGeometry(x, y, self.messages.width(), 40)
self.messages.setGeometry(x, self.messages.y(), self.messages.width(), self.messages.height() - 40) self.messages.setGeometry(x, self.messages.y(), self.messages.width(), self.messages.height() - 40)
if self._should_show_group_peers_list:
self.peers_list.setFixedHeight(self.peers_list.height() - 40)
self.search_field.show() self.search_field.show()
def _toggle_gc_peers_list(self): def _toggle_gc_peers_list(self):
self._should_show_group_peers_list = not self._should_show_group_peers_list self._should_show_group_peers_list = not self._should_show_group_peers_list
self.resizeEvent()
if self._should_show_group_peers_list: if self._should_show_group_peers_list:
self._groups_service.generate_peers_list() self._groups_service.generate_peers_list()
def _new_contact_selected(self, contact):
if self._should_show_group_peers_list:
self._toggle_gc_peers_list()
self.resizeEvent() self.resizeEvent()

View file

@ -374,8 +374,10 @@ class ClickableLabel(QtWidgets.QLabel):
class SearchScreen(QtWidgets.QWidget): class SearchScreen(QtWidgets.QWidget):
def __init__(self, messages, width, *args): def __init__(self, contacts_manager, history_loader, messages, width, *args):
super().__init__(*args) super().__init__(*args)
self._contacts_manager = contacts_manager
self._history_loader = history_loader
self.setMaximumSize(width, 40) self.setMaximumSize(width, 40)
self.setMinimumSize(width, 40) self.setMinimumSize(width, 40)
self._messages = messages self._messages = messages
@ -426,24 +428,24 @@ class SearchScreen(QtWidgets.QWidget):
self.search_text.setFocus() self.search_text.setFocus()
def search(self): def search(self):
Profile.get_instance().update() self._contacts_manager.update()
text = self.search_text.text() text = self.search_text.text()
friend = Profile.get_instance().get_curr_friend() contact = self._contacts_manager.get_curr_contact()
if text and friend and util.is_re_valid(text): if text and contact and util.is_re_valid(text):
index = friend.search_string(text) index = contact.search_string(text)
self.load_messages(index) self.load_messages(index)
def prev(self): def prev(self):
friend = Profile.get_instance().get_curr_friend() contact = self._contacts_manager.get_curr_contact()
if friend is not None: if contact is not None:
index = friend.search_prev() index = contact.search_prev()
self.load_messages(index) self.load_messages(index)
def next(self): def next(self):
friend = Profile.get_instance().get_curr_friend() contact = self._contacts_manager.get_curr_contact()
text = self.search_text.text() text = self.search_text.text()
if friend is not None: if contact is not None:
index = friend.search_next() index = contact.search_next()
if index is not None: if index is not None:
count = self._messages.count() count = self._messages.count()
index += count index += count
@ -456,10 +458,9 @@ class SearchScreen(QtWidgets.QWidget):
def load_messages(self, index): def load_messages(self, index):
text = self.search_text.text() text = self.search_text.text()
if index is not None: if index is not None:
profile = Profile.get_instance()
count = self._messages.count() count = self._messages.count()
while count + index < 0: while count + index < 0:
profile.load_history() self._history_loader.load_history()
count = self._messages.count() count = self._messages.count()
index += count index += count
item = self._messages.item(index) item = self._messages.item(index)

View file

@ -281,7 +281,7 @@ class NetworkSettings(CenteredWidget):
def __init__(self, settings, reset): def __init__(self, settings, reset):
super().__init__() super().__init__()
self._settings = settings self._settings = settings
self.reset = reset self._reset = reset
self.initUI() self.initUI()
self.center() self.center()
@ -360,7 +360,7 @@ class NetworkSettings(CenteredWidget):
self._settings['download_nodes_list'] = self.nodes.isChecked() self._settings['download_nodes_list'] = self.nodes.isChecked()
self._settings.save() self._settings.save()
# recreate tox instance # recreate tox instance
self._profile.reset() self._reset()
self.close() self.close()
except Exception as ex: except Exception as ex:
log('Exception in restart: ' + str(ex)) log('Exception in restart: ' + str(ex))

View file

@ -6,7 +6,7 @@ from ui.groups_widgets import *
class WidgetsFactory: class WidgetsFactory:
def __init__(self, settings, profile, profile_manager, contacts_manager, file_transfer_handler, smiley_loader, def __init__(self, settings, profile, profile_manager, contacts_manager, file_transfer_handler, smiley_loader,
plugin_loader, toxes, version, groups_service): plugin_loader, toxes, version, groups_service, history):
self._settings = settings self._settings = settings
self._profile = profile self._profile = profile
self._profile_manager = profile_manager self._profile_manager = profile_manager
@ -17,6 +17,7 @@ class WidgetsFactory:
self._toxes = toxes self._toxes = toxes
self._version = version self._version = version
self._groups_service = groups_service self._groups_service = groups_service
self._history = history
def create_screenshot_window(self, *args): def create_screenshot_window(self, *args):
return ScreenShotWindow(self._file_transfer_handler, self._contacts_manager, *args) return ScreenShotWindow(self._file_transfer_handler, self._contacts_manager, *args)
@ -31,7 +32,7 @@ class WidgetsFactory:
return ProfileSettings(self._profile, self._profile_manager, self._settings, self._toxes) return ProfileSettings(self._profile, self._profile_manager, self._settings, self._toxes)
def create_network_settings_window(self): def create_network_settings_window(self):
return NetworkSettings(self._settings, self._profile.reset) return NetworkSettings(self._settings, self._profile.restart)
def create_audio_settings_window(self): def create_audio_settings_window(self):
return AudioSettings(self._settings) return AudioSettings(self._settings)
@ -71,3 +72,6 @@ class WidgetsFactory:
def create_join_group_screen_window(self): def create_join_group_screen_window(self):
return JoinGroupScreen(self._groups_service) return JoinGroupScreen(self._groups_service)
def create_search_screen(self, messages):
return SearchScreen(self._contacts_manager, self._history, messages, messages.parent())

View file

@ -146,7 +146,8 @@ class Settings(dict):
'font': 'Times New Roman', 'font': 'Times New Roman',
'update': 1, 'update': 1,
'group_notifications': True, 'group_notifications': True,
'download_nodes_list': False 'download_nodes_list': False,
'notify_all_gc': False
} }
@staticmethod @staticmethod