2008-06-09 13:46:29 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 3 only.
|
|
|
|
##
|
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
|
|
|
|
|
|
|
'''
|
|
|
|
Acronyms expander plugin.
|
|
|
|
|
|
|
|
:author: Mateusz Biliński <mateusz@bilinski.it>
|
2008-06-18 22:45:22 +02:00
|
|
|
:since: 9th June 2008
|
2008-06-09 13:46:29 +02:00
|
|
|
:copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it>
|
|
|
|
:license: GPL
|
|
|
|
'''
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
2013-01-01 11:26:25 +01:00
|
|
|
from gi.repository import Gtk
|
|
|
|
from gi.repository import GObject
|
2008-06-09 13:46:29 +02:00
|
|
|
|
|
|
|
from plugins import GajimPlugin
|
|
|
|
from plugins.helpers import log, log_calls
|
|
|
|
|
|
|
|
class AcronymsExpanderPlugin(GajimPlugin):
|
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
@log_calls('AcronymsExpanderPlugin')
|
|
|
|
def init(self):
|
2011-09-02 21:50:36 +02:00
|
|
|
self.description = _('Replaces acronyms (or other strings) '
|
|
|
|
'with given expansions/substitutes.')
|
2010-04-08 01:20:17 +02:00
|
|
|
self.config_dialog = None
|
2008-06-09 13:46:29 +02:00
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
self.gui_extension_points = {
|
|
|
|
'chat_control_base': (self.connect_with_chat_control_base,
|
|
|
|
self.disconnect_from_chat_control_base)
|
|
|
|
}
|
2008-06-09 13:46:29 +02:00
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
self.config_default_values = {
|
2011-08-30 14:23:16 +02:00
|
|
|
'INVOKER': (' ', ''),
|
2010-04-08 01:20:17 +02:00
|
|
|
'ACRONYMS': ({'RTFM': 'Read The Friendly Manual',
|
|
|
|
'/slap': '/me slaps',
|
|
|
|
'PS-': 'plug-in system',
|
|
|
|
'G-': 'Gajim',
|
|
|
|
'GNT-': 'http://trac.gajim.org/newticket',
|
|
|
|
'GW-': 'http://trac.gajim.org/',
|
|
|
|
'GTS-': 'http://trac.gajim.org/report',
|
|
|
|
},
|
2011-08-30 14:23:16 +02:00
|
|
|
''),
|
2010-04-08 01:20:17 +02:00
|
|
|
}
|
2008-06-09 13:46:29 +02:00
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
@log_calls('AcronymsExpanderPlugin')
|
|
|
|
def textbuffer_live_acronym_expander(self, tb):
|
|
|
|
"""
|
|
|
|
@param tb gtk.TextBuffer
|
|
|
|
"""
|
|
|
|
#assert isinstance(tb,gtk.TextBuffer)
|
|
|
|
ACRONYMS = self.config['ACRONYMS']
|
|
|
|
INVOKER = self.config['INVOKER']
|
2013-01-01 11:26:25 +01:00
|
|
|
t = tb.get_text(tb.get_start_iter(), tb.get_end_iter(), True)
|
2010-04-08 01:20:17 +02:00
|
|
|
#log.debug('%s %d'%(t, len(t)))
|
|
|
|
if t and t[-1] == INVOKER:
|
|
|
|
#log.debug('changing msg text')
|
|
|
|
base, sep, head=t[:-1].rpartition(INVOKER)
|
|
|
|
log.debug('%s | %s | %s'%(base, sep, head))
|
|
|
|
if head in ACRONYMS:
|
|
|
|
head = ACRONYMS[head]
|
|
|
|
#log.debug('head: %s'%(head))
|
|
|
|
t = ''.join((base, sep, head, INVOKER))
|
|
|
|
#log.debug("setting text: '%s'"%(t))
|
2013-01-01 11:26:25 +01:00
|
|
|
GObject.idle_add(tb.set_text, t)
|
2008-06-09 13:46:29 +02:00
|
|
|
|
2010-04-08 01:20:17 +02:00
|
|
|
@log_calls('AcronymsExpanderPlugin')
|
|
|
|
def connect_with_chat_control_base(self, chat_control):
|
|
|
|
d = {}
|
|
|
|
tv = chat_control.msg_textview
|
|
|
|
tb = tv.get_buffer()
|
|
|
|
h_id = tb.connect('changed', self.textbuffer_live_acronym_expander)
|
|
|
|
d['h_id'] = h_id
|
|
|
|
|
|
|
|
chat_control.acronyms_expander_plugin_data = d
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
@log_calls('AcronymsExpanderPlugin')
|
|
|
|
def disconnect_from_chat_control_base(self, chat_control):
|
|
|
|
d = chat_control.acronyms_expander_plugin_data
|
|
|
|
tv = chat_control.msg_textview
|
|
|
|
tv.get_buffer().disconnect(d['h_id'])
|