correctly set transient window for muc error dialogs. Fixes #6943
[gajim.git] / plugins / google_translation / plugin.py
blob9be29a8ef1e186d912af13be39e9992cf4f23237
1 # -*- coding: utf-8 -*-
2 ##
3 ## This file is part of Gajim.
4 ##
5 ## Gajim is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## Gajim is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
14 ## You should have received a copy of the GNU General Public License
15 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
17 '''
18 Google Translation plugin.
20 Translates (currently only incoming) messages using Google Translate.
22 :note: consider this as proof-of-concept
23 :author: Mateusz Biliński <mateusz@bilinski.it>
24 :since: 25th August 2008
25 :copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it>
26 :license: GPL
27 '''
29 import re
30 import urllib2
31 import new
32 from pprint import pformat
33 from sys import getfilesystemencoding
35 from common import helpers
36 from common import gajim
38 from plugins import GajimPlugin
39 from plugins.helpers import log_calls, log
40 from common import ged
41 from common import nec
43 class GoogleTranslationPlugin(GajimPlugin):
45 @log_calls('GoogleTranslationPlugin')
46 def init(self):
47 self.config_dialog = None
48 #self.gui_extension_points = {}
49 self.config_default_values = {
50 'from_lang' :
51 (u'en', _(u'Language of text to be translated')),
52 'to_lang' :
53 (u'fr', _(u'Language to which translation will be made')),
54 'user_agent' :
55 (u'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.12) '
56 'Gecko/20080213 Firefox/2.0.0.11',
57 _(u'User Agent data to be used with urllib2 '
58 'when connecting to Google Translate service'))}
60 #self.events_handlers = {}
62 self.events = [GoogleTranslateMessageReceivedEvent]
64 self.translated_text_re = re.compile(
65 r'google.language.callbacks.id100\(\'22\','
66 '{"translatedText":"(?P<text>[^"]*)"}, 200, null, 200\)')
68 @log_calls('GoogleTranslationPlugin')
69 def translate_text(self, text, from_lang, to_lang):
70 # Converts text so it can be used within URL as query to Google
71 # Translate.
72 quoted_text = urllib2.quote(text.encode(getfilesystemencoding()))
73 # prepare url
74 headers = { 'User-Agent' : self.config['user_agent'] }
75 translation_url = u'http://www.google.com/uds/Gtranslate?callback='\
76 'google.language.callbacks.id100&context=22&q=%(quoted_text)s&'\
77 'langpair=%(from_lang)s%%7C%(to_lang)s&key=notsupplied&v=1.0' % \
78 locals()
79 request = urllib2.Request(translation_url, headers=headers)
81 try:
82 response = urllib2.urlopen(request)
83 except urllib2.URLError, e:
84 # print e
85 return text
87 results = response.read()
88 translated_text = self.translated_text_re.search(results).group('text')
90 if translated_text:
91 return translated_text
92 return text
94 @log_calls('GoogleTranslationPlugin')
95 def activate(self):
96 pass
98 @log_calls('GoogleTranslationPlugin')
99 def deactivate(self):
100 pass
102 class GoogleTranslateMessageReceivedEvent(nec.NetworkIncomingEvent):
103 name = 'google-translate-message-received'
104 base_network_events = ['raw-message-received']
106 def generate(self):
107 msg_type = self.base_event.stanza.attrs.get('type', None)
108 if msg_type == u'chat':
109 msg_text = "".join(self.base_event.stanza.kids[0].data)
110 if msg_text:
111 from_lang = self.plugin.config['from_lang']
112 to_lang = self.plugin.config['to_lang']
113 self.base_event.stanza.kids[0].setData(
114 self.plugin.translate_text(msg_text, from_lang, to_lang))
116 # We only want to modify old event, not emit another, so we return False
117 # here.
118 return False