2016-02-13 17:06:36 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-08-19 04:25:49 -05:00
|
|
|
import time
|
2016-02-13 17:06:36 -06:00
|
|
|
import widgetUtils
|
|
|
|
import output
|
|
|
|
from pubsub import pub
|
2016-04-11 11:48:35 -05:00
|
|
|
import attach
|
2016-08-19 04:25:49 -05:00
|
|
|
from wxUI.dialogs import message, selector
|
2016-02-13 17:06:36 -06:00
|
|
|
from extra import SpellChecker, translator
|
2016-08-19 04:25:49 -05:00
|
|
|
from logging import getLogger
|
|
|
|
|
|
|
|
log = getLogger("controller.message")
|
2016-02-13 17:06:36 -06:00
|
|
|
|
|
|
|
class post(object):
|
2016-08-19 04:25:49 -05:00
|
|
|
def __init__(self, session, title, caption, text, post_type="post"):
|
2016-02-13 17:06:36 -06:00
|
|
|
super(post, self).__init__()
|
2016-08-19 04:25:49 -05:00
|
|
|
self.session = session
|
2016-02-13 17:06:36 -06:00
|
|
|
self.title = title
|
|
|
|
self.message = getattr(message, post_type)(title, caption, text)
|
|
|
|
self.message.set_title(title)
|
|
|
|
widgetUtils.connect_event(self.message.spellcheck, widgetUtils.BUTTON_PRESSED, self.spellcheck)
|
|
|
|
widgetUtils.connect_event(self.message.translateButton, widgetUtils.BUTTON_PRESSED, self.translate)
|
2016-08-19 04:25:49 -05:00
|
|
|
widgetUtils.connect_event(self.message.mention, widgetUtils.BUTTON_PRESSED, self.mention)
|
2016-04-11 11:48:35 -05:00
|
|
|
self.images = []
|
|
|
|
if hasattr(self.message, "attach"):
|
|
|
|
widgetUtils.connect_event(self.message.attach, widgetUtils.BUTTON_PRESSED, self.show_attach_dialog)
|
2016-02-13 17:06:36 -06:00
|
|
|
|
|
|
|
def get_privacy_options(self):
|
|
|
|
p = self.message.get("privacy")
|
|
|
|
if p == _(u"Friends of friends"):
|
|
|
|
privacy = 0
|
|
|
|
elif p == _(u"All users"):
|
|
|
|
privacy = 1
|
|
|
|
return privacy
|
|
|
|
|
2016-08-19 04:25:49 -05:00
|
|
|
def mention(self, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
fields = "id, first_name, last_name"
|
|
|
|
friends = self.session.vk.client.friends.get(count=5000, fields=fields)
|
|
|
|
except AttributeError:
|
|
|
|
time.sleep(2)
|
|
|
|
log.exception("Error retrieving friends...")
|
|
|
|
return self.mention(*args, **kwargs)
|
|
|
|
users = []
|
|
|
|
for i in friends["items"]:
|
|
|
|
users.append(u"{0} {1}".format(i["first_name"], i["last_name"]))
|
|
|
|
select = selector.selectPeople(users)
|
|
|
|
if select.get_response() == widgetUtils.OK and select.users.GetCount() > 0:
|
|
|
|
self.tagged_people = []
|
|
|
|
tagged_users = select.get_all_users()
|
2016-09-05 13:13:45 -05:00
|
|
|
for i in tagged_users:
|
|
|
|
self.tagged_people.append(u"[id%s|%s]" % (str(friends["items"][i]["id"]), friends["items"][i]["first_name"]))
|
2016-08-19 04:25:49 -05:00
|
|
|
self.message.text.SetValue(self.message.text.GetValue()+ u", ".join(self.tagged_people))
|
|
|
|
|
2016-02-13 17:06:36 -06:00
|
|
|
def translate(self, *args, **kwargs):
|
|
|
|
dlg = translator.gui.translateDialog()
|
|
|
|
if dlg.get_response() == widgetUtils.OK:
|
2016-02-25 05:08:19 -06:00
|
|
|
text_to_translate = self.message.get_text()
|
2016-02-13 17:06:36 -06:00
|
|
|
dest = [x[0] for x in translator.translator.available_languages()][dlg.get("dest_lang")]
|
2018-09-03 10:24:59 -05:00
|
|
|
msg = translator.translator.translate(text_to_translate, dest)
|
2016-02-13 17:06:36 -06:00
|
|
|
self.message.set_text(msg)
|
|
|
|
self.message.text_focus()
|
|
|
|
output.speak(_(u"Translated"))
|
2016-02-25 05:08:19 -06:00
|
|
|
dlg.Destroy()
|
2016-02-13 17:06:36 -06:00
|
|
|
|
|
|
|
def spellcheck(self, event=None):
|
|
|
|
text = self.message.get_text()
|
|
|
|
checker = SpellChecker.spellchecker.spellChecker(text, "")
|
|
|
|
if hasattr(checker, "fixed_text"):
|
|
|
|
self.message.set_text(checker.fixed_text)
|
2016-02-25 05:08:19 -06:00
|
|
|
checker.clean()
|
2016-02-13 17:06:36 -06:00
|
|
|
|
2016-04-11 11:48:35 -05:00
|
|
|
def show_attach_dialog(self, *args, **kwargs):
|
2018-11-05 09:37:40 -06:00
|
|
|
a = attach.attachFromLocal()
|
2016-04-11 11:48:35 -05:00
|
|
|
if len(a.attachments) != 0:
|
|
|
|
self.attachments = a.attachments
|
2016-02-13 17:06:36 -06:00
|
|
|
|
|
|
|
class comment(post):
|
2016-08-19 04:25:49 -05:00
|
|
|
def __init__(self, session, title, caption, text):
|
|
|
|
super(comment, self).__init__(session, title, caption, text, "comment")
|
2016-02-13 17:06:36 -06:00
|
|
|
self.message.set_title(_(u"New comment"))
|