From 47bab9d2c4cfa19ba4b0c92f891786ef35541ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Cort=C3=A9z?= Date: Wed, 8 Jun 2016 05:45:03 -0500 Subject: [PATCH] Added a context menu for most kind of buffers --- changelog.md | 2 + src/controller/buffers.py | 130 ++++++++++++++++++++++++++++++- src/controller/mainController.py | 2 +- src/wxUI/menus.py | 48 +++++++----- 4 files changed, 157 insertions(+), 25 deletions(-) diff --git a/changelog.md b/changelog.md index 0f898a4..934c9da 100644 --- a/changelog.md +++ b/changelog.md @@ -11,6 +11,8 @@ * When opened, socializer will try to create chat buffers for all unread messages. * Update some information on certain posts when an item is selected. For example, update the date of a post. * Read messages will be marked as read in the social network, so it'll cause that your friends could see that you have read the message and socializer will not load chat buffers with read messages at startup. +* Included a brief manual in the help menu. Currently available only in English. +* Included a context menu in list items. Currently there are functions not available. Menu for chat buffers is not implemented yet. ## Changes on build 2016.05.25 diff --git a/src/controller/buffers.py b/src/controller/buffers.py index f04c3ad..99a0be8 100644 --- a/src/controller/buffers.py +++ b/src/controller/buffers.py @@ -13,7 +13,7 @@ from wxUI.tabs import home from pubsub import pub from sessionmanager import session from mysc.thread_utils import call_threaded -from wxUI import commonMessages +from wxUI import commonMessages, menus from vk import upload from vk.exceptions import VkAPIMethodError @@ -21,6 +21,10 @@ log = logging.getLogger("controller.buffers") class baseBuffer(object): """ a basic representation of a buffer. Other buffers should be derived from this class""" + + def get_post(self): + return self.session.db[self.name]["items"][self.tab.list.get_selected()] + def __init__(self, parent=None, name="", session=None, composefunc=None, *args, **kwargs): """ parent wx.Treebook: parent for the buffer panel, name str: Name for saving this buffer's data in the local storage variable, @@ -38,6 +42,8 @@ class baseBuffer(object): self.update_function = "get_page" self.name = name self.connect_events() + self.user_key = "source_id" + self.post_key = "post_id" def create_tab(self, parent): """ Creates the Wx panel.""" @@ -113,8 +119,79 @@ class baseBuffer(object): def connect_events(self): widgetUtils.connect_event(self.tab.post, widgetUtils.BUTTON_PRESSED, self.post) widgetUtils.connect_event(self.tab.list.list, widgetUtils.KEYPRESS, self.get_event) + widgetUtils.connect_event(self.tab.list.list, wx.EVT_LIST_ITEM_RIGHT_CLICK, self.show_menu) + widgetUtils.connect_event(self.tab.list.list, wx.EVT_LIST_KEY_DOWN, self.show_menu_by_key) self.tab.set_focus_function(self.onFocus) + def show_menu(self, ev, pos=0, *args, **kwargs): + if self.tab.list.get_count() == 0: return + menu = self.get_menu() + if pos != 0: + self.tab.PopupMenu(menu, pos) + else: + self.tab.PopupMenu(menu, ev.GetPosition()) + + def show_menu_by_key(self, ev): + if self.tab.list.get_count() == 0: + return + if ev.GetKeyCode() == wx.WXK_WINDOWS_MENU: + self.show_menu(widgetUtils.MENU, pos=self.tab.list.list.GetPosition()) + + def get_menu(self): + m = menus.postMenu() + p = self.get_post() + if p.has_key("likes") == False: + m.like.Enable(False) + elif p["likes"]["user_likes"] == 1: + m.like.Enable(False) + m.dislike.Enable(True) + if p.has_key("comments") == False: + m.comment.Enable(False) + widgetUtils.connect_event(m, widgetUtils.MENU, self.open_post, menuitem=m.open) + widgetUtils.connect_event(m, widgetUtils.MENU, self.do_like, menuitem=m.like) + widgetUtils.connect_event(m, widgetUtils.MENU, self.do_dislike, menuitem=m.dislike) + widgetUtils.connect_event(m, widgetUtils.MENU, self.do_comment, menuitem=m.comment) + return m + + def do_like(self, *args, **kwargs): + post = self.get_post() + user = post[self.user_key] + id = post[self.post_key] + if post.has_key("type"): + type_ = post["type"] + else: + type_ = "post" + l = self.session.vk.client.likes.add(owner_id=user, item_id=id, type=type_) + self.session.db[self.name]["items"][self.tab.list.get_selected()]["likes"]["count"] = l["likes"] + self.session.db[self.name]["items"][self.tab.list.get_selected()]["likes"]["user_likes"] = 1 + output.speak(_(u"You liked this")) + + def do_dislike(self, *args, **kwargs): + post = self.get_post() + user = post[self.user_key] + id = post[self.post_key] + if post.has_key("type"): + type_ = post["type"] + else: + type_ = "post" + l = self.session.vk.client.likes.delete(owner_id=user, item_id=id, type=type_) + self.session.db[self.name]["items"][self.tab.list.get_selected()]["likes"]["count"] = l["likes"] + self.session.db[self.name]["items"][self.tab.list.get_selected()]["likes"]["user_likes"] = 2 + output.speak(_(u"You don't like this")) + + def do_comment(self, *args, **kwargs): + comment = messages.comment(title=_(u"Add a comment"), caption="", text="") + if comment.message.get_response() == widgetUtils.OK: + msg = comment.message.get_text().encode("utf-8") + post = self.get_post() + try: + user = post[self.user_key] + id = post[self.post_key] + self.session.vk.client.wall.addComment(owner_id=user, post_id=id, text=msg) + output.speak(_(u"You've posted a comment")) + except Exception as msg: + print msg + def get_event(self, ev): if ev.GetKeyCode() == wx.WXK_RETURN and ev.ControlDown() and ev.ShiftDown(): event = "pause_audio" elif ev.GetKeyCode() == wx.WXK_RETURN and ev.ControlDown(): event = "play_audio" @@ -141,7 +218,7 @@ class baseBuffer(object): if post.has_key("type") and post["type"] == "audio": pub.sendMessage("play-audio", audio_object=post["audio"]["items"][0]) - def open_post(self): + def open_post(self, *args, **kwargs): post = self.session.db[self.name]["items"][self.tab.list.get_selected()] if post.has_key("type") and post["type"] == "audio": a = posts.audio(self.session, post["audio"]["items"]) @@ -209,6 +286,11 @@ class feedBuffer(baseBuffer): else: return False + def __init__(self, *args, **kwargs): + super(feedBuffer, self).__init__(*args, **kwargs) + self.user_key = "from_id" + self.post_key = "id" + class audioBuffer(feedBuffer): def create_tab(self, parent): self.tab = home.audioTab(parent) @@ -222,7 +304,7 @@ class audioBuffer(feedBuffer): selected = self.tab.list.get_selected() pub.sendMessage("play-audio", audio_object=self.session.db[self.name]["items"][selected]) - def open_post(self): + def open_post(self, *args, **kwargs): selected = self.tab.list.get_selected() audios = [self.session.db[self.name]["items"][selected]] a = posts.audio(self.session, audios) @@ -257,6 +339,39 @@ class audioBuffer(feedBuffer): def onFocus(self, *args, **kwargs): pass + def add_to_library(self, *args, **kwargs): + post = self.get_post() + args = {} + args["audio_id"] = post["id"] + if post.has_key("album_id"): + args["album_id"] = post["album_id"] + args["owner_id"] = post["owner_id"] + audio = self.session.vk.client.audio.add(**args) + if audio != None and int(audio) > 21: + output.speak(_(u"Audio added to your library")) + + def remove_from_library(self, *args, **kwargs): + post = self.get_post() + args = {} + args["audio_id"] = post["id"] + args["owner_id"] = self.session.user_id + result = self.session.vk.client.audio.delete(**args) + if int(result) == 1: + output.speak(_(u"Removed audio from library")) + + def get_menu(self): + p = self.get_post() + m = menus.audioMenu() + widgetUtils.connect_event(m, widgetUtils.MENU, self.open_post, menuitem=m.open) + widgetUtils.connect_event(m, widgetUtils.MENU, self.play_audio, menuitem=m.play) + # if owner_id is the current user, the audio is added to the user's audios. + if p["owner_id"] == self.session.user_id: + m.library.SetItemLabel(_(u"&Remove from library")) + widgetUtils.connect_event(m, widgetUtils.MENU, self.remove_from_library, menuitem=m.library) + else: + widgetUtils.connect_event(m, widgetUtils.MENU, self.add_to_library, menuitem=m.library) + return m + class empty(object): def __init__(self, name=None, parent=None, *args, **kwargs): @@ -336,3 +451,12 @@ class peopleBuffer(feedBuffer): original_date = arrow.get(post["last_seen"]["time"]) created_at = original_date.humanize(locale=languageHandler.getLanguage()) self.tab.list.list.SetStringItem(self.tab.list.get_selected(), 1, created_at) + + def open_timeline(self, *args, **kwargs): + pass + + def get_menu(self, *args, **kwargs): + m = menus.peopleMenu() + widgetUtils.connect_event(m, widgetUtils.MENU, self.new_chat, menuitem=m.message) + widgetUtils.connect_event(m, widgetUtils.MENU, self.open_timeline, menuitem=m.timeline) + return m \ No newline at end of file diff --git a/src/controller/mainController.py b/src/controller/mainController.py index 22db09c..d12a19a 100644 --- a/src/controller/mainController.py +++ b/src/controller/mainController.py @@ -92,6 +92,7 @@ class Controller(object): timelines = buffers.empty(parent=self.window.tb, name="timelines") self.buffers.append(timelines) self.window.add_buffer(timelines.tab, _(u"Timelines")) + self.window.realize() def connect_events(self): log.debug("Connecting events to responses...") @@ -143,7 +144,6 @@ class Controller(object): self.status_setter.start() self.set_online() self.create_unread_messages() - self.window.realize() def in_post(self, buffer): buffer = self.search(buffer) buffer.get_items() diff --git a/src/wxUI/menus.py b/src/wxUI/menus.py index 00a65ea..bab740a 100644 --- a/src/wxUI/menus.py +++ b/src/wxUI/menus.py @@ -8,13 +8,14 @@ class postMenu(wx.Menu): self.AppendItem(self.open) self.like = wx.MenuItem(self, wx.NewId(), _(u"Like")) self.AppendItem(self.like) - self.unlike = wx.MenuItem(self, wx.NewId(), _(u"Unlike")) - self.AppendItem(self.unlike) + self.dislike = wx.MenuItem(self, wx.NewId(), _(u"Dislike")) + self.dislike.Enable(False) + self.AppendItem(self.dislike) self.comment = wx.MenuItem(self, wx.NewId(), _(u"Add comment")) self.AppendItem(self.comment) - self.post_in_wall = self.Append(wx.NewId(), _(u"Post to this profile")) - self.AppendItem(self.post_in_wall) + self.post_in_wall = wx.MenuItem(self, wx.NewId(), _(u"Post to this profile")) self.post_in_wall.Enable(False) + self.AppendItem(self.post_in_wall) def create_specific_post_options(self): self.update = wx.MenuItem(self, wx.NewId(), _(u"Update")) @@ -22,6 +23,28 @@ class postMenu(wx.Menu): self.delete = wx.MenuItem(self, wx.NewId(), _(u"Delete")) self.AppendItem(self.delete) +class audioMenu(wx.Menu): + + def __init__(self, *args, **kwargs): + super(audioMenu, self).__init__(*args, **kwargs) + self.open = wx.MenuItem(self, wx.NewId(), _(u"&Open")) + self.AppendItem(self.open) + self.play = wx.MenuItem(self, wx.NewId(), _(u"&Play")) + self.AppendItem(self.play) + self.library = wx.MenuItem(self, wx.NewId(), _(u"&Add to library")) + self.AppendItem(self.library) + +class peopleMenu(wx.Menu): + def __init__(self, *args, **kwargs): + super(peopleMenu, self).__init__(*args, **kwargs) + self.view_profile = wx.MenuItem(self, wx.NewId(), _(u"View profile")) + self.AppendItem(self.view_profile) + self.message = wx.MenuItem(self, wx.NewId(), _(u"Send a message")) + self.AppendItem(self.message) + self.timeline = wx.MenuItem(self, wx.NewId(), _(u"Open timeline")) + self.AppendItem(self.timeline) + self.view_profile.Enable(False) + class commentMenu(wx.Menu): def __init__(self, *args, **kwargs): super(commentMenu, self).__init__(*args, **kwargs) @@ -41,20 +64,3 @@ class notificationsMenu(wx.Menu): super(notificationsMenu, self).__init__() self.mark_as_read = wx.MenuItem(self, wx.NewId(), _(u"Mark as read")) self.AppendItem(self.mark_as_read) - -class toolsMenu(wx.Menu): - def __init__(self, *args, **kwargs): - super(toolsMenu, self).__init__(*args, **kwargs) - self.url = wx.MenuItem(self, -1, _(u"Open URL")) - self.AppendItem(self.url) -# self.url.Enable(False) - self.translate = wx.MenuItem(self, -1, _(u"Translate")) - self.AppendItem(self.translate) - self.CheckSpelling = wx.MenuItem(self, -1, _(u"Check Spelling")) - self.AppendItem(self.CheckSpelling) - -class attachMenu(wx.Menu): - def __init__(self): - super(attachMenu, self).__init__() - self.photo = wx.MenuItem(self, wx.NewId(), _(u"Picture")) - self.AppendItem(self.photo)