Welcome to mirror list, hosted at ThFree Co, Russian Federation.

acronyms_expander.py « plugins - dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6dec956143fa22c066767803dd56da950ca96ec3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# -*- 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>
:since: 9th June 2008
:copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it>
:license: GPL
'''

import sys

import gtk

from plugins import GajimPlugin
from plugins.helpers import log, log_calls

class AcronymsExpanderPlugin(GajimPlugin):
	name = u'Acronyms Expander'
	short_name = u'acronyms_expander'
	version = u'0.1'
	description = u'''Replaces acronyms (or other strings) with given expansions/substitutes.'''
	authors = [u'Mateusz Biliński <mateusz@bilinski.it>']
	homepage = u'http://blog.bilinski.it'
		
	@log_calls('AcronymsExpanderPlugin')
	def init(self):
		self.config_dialog = None
		
		self.gui_extension_points = {
			'chat_control_base' : (self.connect_with_chat_control_base,
								   self.disconnect_from_chat_control_base)
		}		

		self.config_default_values = {'INVOKER' : (' ', _('')),
						  			  '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'
													}, _('')),
									 }

	@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']
		t = tb.get_text(tb.get_start_iter(), tb.get_end_iter())
		#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("turning off notify")
				tb.freeze_notify()
				#log.debug("setting text: '%s'"%(t))
				tb.set_text(t)
				#log.debug("turning on notify")
				tb.thaw_notify()
		
	@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'])