Refactored some functions to call wx's threads properly.

This commit is contained in:
Manuel Cortez 2019-06-10 09:26:10 -05:00
parent 4e6126405f
commit 4e3c397ce5
4 changed files with 78 additions and 75 deletions

View File

@ -14,6 +14,8 @@
* Fixed an error with two factor authentication in the recent socializer version. Now it works reliably again.
* Fixed an error when trying to attach a photo to a wall post. The error was fixed in the [vk_api](https://github.com/python273/vk_api) module and the fix was sent to the developer of the library, so he will be able to merge it in the next version. In the meantime, socializer already includes the fix for this method, so you can upload photos to wall posts normally.
* Fixed an error retrieving some group information for the current session.
* When posting in a topic, links will be posted properly.
### Changes

View File

@ -80,7 +80,7 @@ class baseBuffer(object):
def insert(self, item, reversed=False):
""" Add a new item to the list. Uses renderers.composefunc for parsing the dictionary and create a valid result for putting it in the list."""
item_ = getattr(renderers, self.compose_function)(item, self.session)
self.tab.list.insert_item(reversed, *item_)
wx.CallAfter(self.tab.list.insert_item, reversed, *item_)
def get_items(self, show_nextpage=False):
""" Retrieve items from the VK API. This function is called repeatedly by the main controller and users could call it implicitly as well with the update buffer option.
@ -1143,9 +1143,9 @@ class chatBuffer(baseBuffer):
if show_nextpage == False:
if self.tab.history.GetValue() != "" and num > 0:
v = [i for i in self.session.db[self.name]["items"][:num]]
[self.insert(i, False) for i in v]
[wx.CallAfter(self.insert, i, False) for i in v]
else:
[self.insert(i) for i in self.session.db[self.name]["items"][:num]]
[wx.CallAfter(self.insert, i) for i in self.session.db[self.name]["items"][:num]]
else:
if num > 0:
# At this point we save more CPU and mathematical work if we just delete everything in the chat history and readd all messages.
@ -1155,7 +1155,7 @@ class chatBuffer(baseBuffer):
self.chats = dict()
self.tab.history.SetValue("")
v = [i for i in self.session.db[self.name]["items"]]
[self.insert(i) for i in v]
[wx.CallAfter(self.insert, i) for i in v]
# Now it's time to set back the focus in the post.
for i in self.chats.keys():
if self.chats[i] == focused_post["id"]:

View File

@ -411,7 +411,7 @@ class Controller(object):
if user == None:
log.exception("Getting user manually...")
user = self.session.vk.client.users.get(user_ids=event.user_id, fields="last_seen")[0]
online_buffer.add_person(user)
wx.CallAfter(online_buffer.add_person, user)
def user_offline(self, event):
""" Sends a notification of an user logging off in VK.
@ -424,7 +424,7 @@ class Controller(object):
sound = "friend_offline.ogg"
self.notify(msg, sound, self.session.settings["chat"]["notifications"])
online_friends = self.search("online_friends")
online_friends.remove_person(event.user_id)
wx.CallAfter(online_friends.remove_person, event.user_id)
def notify(self, message="", sound="", type="native"):
""" display a notification in Socializer.
@ -512,7 +512,7 @@ class Controller(object):
# Let's add this to the buffer.
# ToDo: Clean this code and test how is the database working with this set to True.
buffer.session.db[buffer.name]["items"].append(message)
buffer.insert(self.session.db[buffer.name]["items"][-1], False)
wx.CallAfter(buffer.insert, self.session.db[buffer.name]["items"][-1], False)
self.session.soundplayer.play("message_received.ogg")
wx.CallAfter(self.reorder_buffer, buffer)
# Check if we have to read the message aloud

View File

@ -137,89 +137,90 @@ class mainLoopObject(wx.App):
self.app.MainLoop()
class multiselectionBaseList(wx.ListCtrl, listmix.CheckListCtrlMixin):
def __init__(self, *args, **kwargs):
wx.ListCtrl.__init__(self, *args, **kwargs)
listmix.CheckListCtrlMixin.__init__(self)
self.Bind(wx.EVT_CHAR_HOOK, self.on_keydown)
self.Bind(wx.EVT_LIST_ITEM_FOCUSED, self.on_focus)
def __init__(self, *args, **kwargs):
wx.ListCtrl.__init__(self, *args, **kwargs)
listmix.CheckListCtrlMixin.__init__(self)
self.Bind(wx.EVT_CHAR_HOOK, self.on_keydown)
self.Bind(wx.EVT_LIST_ITEM_FOCUSED, self.on_focus)
def on_focus(self, event):
currentItem = self.GetFocusedItem()
if self.IsChecked(currentItem):
pub.sendMessage("play-sound", sound="selected.ogg")
event.Skip()
def on_focus(self, event):
currentItem = self.GetFocusedItem()
if self.IsChecked(currentItem):
pub.sendMessage("play-sound", sound="selected.ogg")
event.Skip()
def OnCheckItem(self, index, flag):
if flag == True:
pub.sendMessage("play-sound", sound="checked.ogg")
else:
pub.sendMessage("play-sound", sound="unchecked.ogg")
def OnCheckItem(self, index, flag):
if flag == True:
pub.sendMessage("play-sound", sound="checked.ogg")
else:
pub.sendMessage("play-sound", sound="unchecked.ogg")
def on_keydown(self, event):
if event.GetKeyCode() == wx.WXK_SPACE:
self.ToggleItem(self.GetFocusedItem())
event.Skip()
def on_keydown(self, event):
if event.GetKeyCode() == wx.WXK_SPACE:
self.ToggleItem(self.GetFocusedItem())
event.Skip()
class list(object):
def __init__(self, parent, *columns, **listArguments):
self.columns = columns
self.listArguments = listArguments
self.create_list(parent)
def __init__(self, parent, *columns, **listArguments):
self.columns = columns
self.listArguments = listArguments
self.create_list(parent)
def set_windows_size(self, column, characters_max):
self.list.SetColumnWidth(column, characters_max*2)
def set_windows_size(self, column, characters_max):
self.list.SetColumnWidth(column, characters_max*2)
def set_size(self):
self.list.SetSize((self.list.GetBestSize()[0], 1000))
def set_size(self):
self.list.SetSize((self.list.GetBestSize()[0], 1000))
def create_list(self, parent):
self.list = wx.ListCtrl(parent, -1, **self.listArguments)
for i in range(0, len(self.columns)):
self.list.InsertColumn(i, "%s" % (self.columns[i]))
def create_list(self, parent):
self.list = wx.ListCtrl(parent, -1, **self.listArguments)
for i in range(0, len(self.columns)):
self.list.InsertColumn(i, "%s" % (self.columns[i]))
def insert_item(self, reversed, *item):
""" Inserts an item on the list."""
if reversed == False: items = self.list.GetItemCount()
else: items = 0
self.list.InsertItem(items, item[0])
for i in range(1, len(self.columns)):
self.list.SetItem(items, i, item[i])
def insert_item(self, reversed, *item):
""" Inserts an item on the list."""
if reversed == False: items = self.list.GetItemCount()
else: items = 0
self.list.InsertItem(items, item[0])
for i in range(1, len(self.columns)):
self.list.SetItem(items, i, item[i])
def remove_item(self, pos):
""" Deletes an item from the list."""
if pos > 0: self.list.Focus(pos-1)
self.list.DeleteItem(pos)
def remove_item(self, pos):
""" Deletes an item from the list."""
if pos > 0: self.list.Focus(pos-1)
self.list.DeleteItem(pos)
def clear(self):
self.list.DeleteAllItems()
def clear(self):
self.list.DeleteAllItems()
def get_selected(self):
return self.list.GetFocusedItem()
def get_selected(self):
return self.list.GetFocusedItem()
def select_item(self, pos):
self.list.Focus(pos)
def select_item(self, pos):
self.list.Focus(pos)
def get_count(self):
selected = self.list.GetItemCount()
if selected == -1:
return 0
else:
return selected
def get_count(self):
selected = self.list.GetItemCount()
if selected == -1:
return 0
else:
return selected
def Enable(self, value):
return self.list.Enable(value)
def Enable(self, value):
return self.list.Enable(value)
class multiselectionList(list):
def create_list(self, parent):
self.list = multiselectionBaseList(parent, -1, **self.listArguments)
for i in range(0, len(self.columns)):
self.list.InsertColumn(i, "%s" % (self.columns[i]))
def create_list(self, parent):
self.list = multiselectionBaseList(parent, -1, **self.listArguments)
for i in range(0, len(self.columns)):
self.list.InsertColumn(i, "%s" % (self.columns[i]))
def get_multiple_selection(self):
selected = []
for item in range(0, self.list.GetItemCount()):
if self.list.IsChecked(item):
selected.append(item)
if len(selected) == 0 and self.list.GetFocusedItem() != -1:
selected.append(self.list.GetFocusedItem())
return selected
def get_multiple_selection(self):
selected = []
for item in range(0, self.list.GetItemCount()):
if self.list.IsChecked(item):
selected.append(item)
if len(selected) == 0 and self.list.GetFocusedItem() != -1:
selected.append(self.list.GetFocusedItem())
return selected