root/branches/gajim_0.8/src/chat.py

Revision 3105, 44.8 kB (checked in by nk, 3 years ago)

prevent a tb but add a fixme because I am not sure why it happens

Line 
1##      plugins/tabbed_chat_window.py
2##
3## Gajim Team:
4##      - Yann Le Boulanger <asterix@lagaule.org>
5##      - Vincent Hanquez <tab@snarc.org>
6##      - Nikos Kouremenos <kourem@gmail.com>
7##
8##      Copyright (C) 2003-2005 Gajim Team
9##
10## This program is free software; you can redistribute it and/or modify
11## it under the terms of the GNU General Public License as published
12## by the Free Software Foundation; version 2 only.
13##
14## This program is distributed in the hope that it will be useful,
15## but WITHOUT ANY WARRANTY; without even the implied warranty of
16## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17## GNU General Public License for more details.
18##
19
20import gtk
21import gtk.glade
22import pango
23import gobject
24import time
25import dialogs
26import history_window
27
28try:
29        import gtkspell
30except:
31        pass
32
33from common import gajim
34from common import helpers
35from common import i18n
36
37_ = i18n._
38APP = i18n.APP
39gtk.glade.bindtextdomain(APP, i18n.DIR)
40gtk.glade.textdomain(APP)
41
42GTKGUI_GLADE = 'gtkgui.glade'
43
44class Chat:
45        """Class for chat/groupchat windows"""
46        def __init__(self, plugin, account, widget_name):
47                self.xml = gtk.glade.XML(GTKGUI_GLADE, widget_name, APP)
48                self.window = self.xml.get_widget(widget_name)
49
50                self.widget_name = widget_name
51                self.notebook = self.xml.get_widget('chat_notebook')
52                self.notebook.remove_page(0)
53                self.plugin = plugin
54                self.account = account
55                self.change_cursor = None
56                self.xmls = {}
57                self.tagIn = {} # holds tag for nick that talks to us
58                self.tagOut = {} # holds tag for our nick
59                self.tagStatus = {} # holds status messages
60                self.nb_unread = {}
61                self.last_time_printout = {}
62                self.print_time_timeout_id = {}
63                self.names = {} # what is printed in the tab (eg. user.name)
64                self.childs = {} # holds the contents for every tab (VBox)
65                self.popup_is_shown = False # is a context menu shown or not?
66
67                # the following vars are used to keep history of user's messages
68                self.sent_history = {}
69                self.sent_history_pos = {}
70                self.typing_new = {}
71                self.orig_msg = {}
72               
73                # we check that on opening new windows
74                self.always_compact_view = gajim.config.get('always_compact_view')
75
76        def update_font(self):
77                font = pango.FontDescription(gajim.config.get('conversation_font'))
78                for jid in self.tagIn:
79                        conversation_textview = self.xmls[jid].get_widget(
80                                'conversation_textview')
81                        conversation_textview.modify_font(font)
82                        message_textview = self.xmls[jid].get_widget('message_textview')
83                        message_textview.modify_font(font)
84
85        def update_tags(self):
86                for jid in self.tagIn:
87                        self.tagIn[jid].set_property('foreground',
88                                        gajim.config.get('inmsgcolor'))
89                        self.tagOut[jid].set_property('foreground',
90                                        gajim.config.get('outmsgcolor'))
91                        self.tagStatus[jid].set_property('foreground',
92                                        gajim.config.get('statusmsgcolor'))
93
94        def update_print_time(self):
95                if gajim.config.get('print_time') != 'sometimes':
96                        list_jid = self.print_time_timeout_id.keys()
97                        for jid in list_jid:
98                                gobject.source_remove(self.print_time_timeout_id[jid])
99                                del self.print_time_timeout_id[jid]
100                else:
101                        for jid in self.xmls:
102                                if self.print_time_timeout_id.has_key(jid):
103                                        continue
104                                self.print_time_timeout(jid)
105                                self.print_time_timeout_id[jid] = \
106                                                gobject.timeout_add(300000,
107                                                        self.print_time_timeout,
108                                                        jid)
109
110        def show_title(self):
111                """redraw the window's title"""
112                unread = 0
113                for jid in self.nb_unread:
114                        unread += self.nb_unread[jid]
115                start = ""
116                if unread > 1:
117                        start = '[' + str(unread) + '] '
118                elif unread == 1:
119                        start = '* '
120                chat = self.names[jid]
121                if len(self.xmls) > 1: # if more than one tab in the same window
122                        if self.widget_name == 'tabbed_chat_window':
123                                add = _('Chat')
124                        elif self.widget_name == 'groupchat_window':
125                                add = _('Group Chat')
126                elif len(self.xmls) == 1: # just one tab
127                        if self.widget_name == 'tabbed_chat_window':
128                                c = gajim.get_first_contact_instance_from_jid(self.account, jid)
129                                if c is None: # FIXME: I don't know why but c can be None!
130                                        add = ''
131                                else:
132                                        add = c.name
133                        elif self.widget_name == 'groupchat_window':
134                                name = gajim.get_nick_from_jid(jid)
135                                add = name
136
137                title = start + add
138                if len(gajim.connections) >= 2: # if we have 2 or more accounts
139                        title += ' (' + _('account: ') + self.account + ')'
140
141                self.window.set_title(title)
142
143        def redraw_tab(self, jid):
144                """redraw the label of the tab"""
145                start = ''
146                if self.nb_unread[jid] > 1:
147                        start = '[' + str(self.nb_unread[jid]) + '] '
148                elif self.nb_unread[jid] == 1:
149                        start = '* '
150                       
151                child = self.childs[jid]
152                hb = self.notebook.get_tab_label(child).get_children()[0]
153                if self.widget_name == 'tabbed_chat_window':
154                        nickname = hb.get_children()[1]
155                elif self.widget_name == 'groupchat_window':
156                        nickname = hb.get_children()[0]
157
158                #FIXME: when gtk2.4 is OOOOLD do it via glade2.10+
159                if gtk.pygtk_version >= (2, 6, 0) and gtk.gtk_version >= (2, 6, 0):
160                        nickname.set_max_width_chars(10)
161
162                nickname.set_text(start + self.names[jid])
163
164
165        def on_window_destroy(self, widget, kind): #kind is 'chats' or 'gc'
166                '''clean self.plugin.windows[self.account][kind]'''
167                for jid in self.xmls:
168                        windows = self.plugin.windows[self.account][kind]
169                        if kind == 'chats':
170                                # send 'gone' chatstate to every tabbed chat tab
171                                windows[jid].send_chatstate('gone', jid)
172                                gobject.source_remove(self.possible_paused_timeout_id[jid])
173                                gobject.source_remove(self.possible_inactive_timeout_id[jid])
174                        if self.plugin.systray_enabled and self.nb_unread[jid] > 0:
175                                self.plugin.systray.remove_jid(jid, self.account)
176                        del windows[jid]
177                        if self.print_time_timeout_id.has_key(jid):
178                                gobject.source_remove(self.print_time_timeout_id[jid])
179                if windows.has_key('tabbed'):
180                        del windows['tabbed']
181
182        def get_active_jid(self):
183                notebook = self.notebook
184                active_child = notebook.get_nth_page(notebook.get_current_page())
185                active_jid = ''
186                for jid in self.xmls:
187                        if self.childs[jid] == active_child:
188                                active_jid = jid
189                                break
190                return active_jid
191
192        def on_close_button_clicked(self, button, jid):
193                """When close button is pressed: close a tab"""
194                self.remove_tab(jid)
195
196        def on_history_menuitem_clicked(self, widget = None, jid = None):
197                """When history menuitem is pressed: call history window"""
198                if jid is None:
199                        jid = self.get_active_jid()
200                if self.plugin.windows['logs'].has_key(jid):
201                        self.plugin.windows['logs'][jid].window.present()
202                else:
203                        self.plugin.windows['logs'][jid] = history_window.HistoryWindow(
204                                self.plugin, jid, self.account)
205
206        def on_chat_window_focus_in_event(self, widget, event):
207                """When window gets focus"""
208                jid = self.get_active_jid()
209                textview = self.xmls[jid].get_widget('conversation_textview')
210                buffer = textview.get_buffer()
211                end_iter = buffer.get_end_iter()
212                end_rect = textview.get_iter_location(end_iter)
213                visible_rect = textview.get_visible_rect()
214                if end_rect.y <= (visible_rect.y + visible_rect.height):
215                        #we are at the end
216                        if self.nb_unread[jid] > 0:
217                                self.nb_unread[jid] = 0
218                                self.redraw_tab(jid)
219                                self.show_title()
220                                if self.plugin.systray_enabled:
221                                        self.plugin.systray.remove_jid(jid, self.account)
222       
223        def on_compact_view_menuitem_activate(self, widget):
224                isactive = widget.get_active()
225                self.set_compact_view(isactive)
226
227        def on_actions_button_clicked(self, widget):
228                '''popup action menu'''
229                menu = self.prepare_context_menu()
230                self.popup_is_shown = True
231                menu.connect('deactivate', self.on_popup_deactivate)
232                menu.popup(None, None, None, 1, 0)
233                menu.show_all()
234
235        def remove_possible_switch_to_menuitems(self, menu):
236                ''' remove duplicate 'Switch to' if they exist and return clean menu'''
237                childs = menu.get_children()
238
239                if self.widget_name == 'tabbed_chat_window':
240                        jid = self.get_active_jid()
241                        c = gajim.get_first_contact_instance_from_jid(self.account, jid)
242                        if _('not in the roster') in c.groups: # for add_to_roster_menuitem
243                                childs[5].show()
244                                childs[5].set_no_show_all(False)
245                        else:
246                                childs[5].hide()
247                                childs[5].set_no_show_all(True)
248                       
249                        start_removing_from = 6 # this is from the seperator and after
250                       
251                else:
252                        start_removing_from = 7 # # this is from the seperator and after
253                               
254                for child in childs[start_removing_from:]:
255                        menu.remove(child)
256
257                return menu
258       
259        def prepare_context_menu(self):
260                '''sets compact view menuitem active state
261                sets active and sensitivity state for toggle_gpg_menuitem
262                and remove possible 'Switch to' menuitems'''
263                if self.widget_name == 'groupchat_window':
264                        menu = self.gc_popup_menu
265                        childs = menu.get_children()
266                        # compact_view_menuitem
267                        childs[5].set_active(self.compact_view_current_state)
268                elif self.widget_name == 'tabbed_chat_window':
269                        menu = self.tabbed_chat_popup_menu
270                        childs = menu.get_children()
271                        # check if gpg capabitlies or else make gpg toggle insensitive
272                        jid = self.get_active_jid()
273                        gpg_btn = self.xmls[jid].get_widget('gpg_togglebutton')
274                        isactive = gpg_btn.get_active()
275                        issensitive = gpg_btn.get_property('sensitive')
276                        childs[3].set_active(isactive)
277                        childs[3].set_property('sensitive', issensitive)
278                        # compact_view_menuitem
279                        childs[4].set_active(self.compact_view_current_state)
280                menu = self.remove_possible_switch_to_menuitems(menu)
281               
282                return menu
283
284        def popup_menu(self, event):
285                self.popup_is_shown = True
286                menu = self.prepare_context_menu()
287                menu.connect('deactivate', self.on_popup_deactivate)
288                # common menuitems (tab switches)
289                if len(self.xmls) > 1: # if there is more than one tab
290                        menu.append(gtk.SeparatorMenuItem()) # seperator
291                        for jid in self.xmls:
292                                if jid != self.get_active_jid():
293                                        item = gtk.ImageMenuItem(_('Switch to %s') % self.names[jid])
294                                        img = gtk.image_new_from_stock(gtk.STOCK_JUMP_TO,
295                                                gtk.ICON_SIZE_MENU)
296                                        item.set_image(img)
297                                        item.connect('activate', lambda obj, jid:self.set_active_tab(
298                                                jid), jid)
299                                        menu.append(item)
300
301                # show the menu
302                menu.popup(None, None, None, event.button, event.time)
303                menu.show_all()
304
305        def on_banner_eventbox_button_press_event(self, widget, event):
306                '''If right-clicked, show popup'''
307                if event.button == 3: # right click
308                        self.popup_menu(event)
309
310        def on_popup_deactivate(self, widget):
311                self.popup_is_shown = False
312
313        def on_chat_notebook_switch_page(self, notebook, page, page_num):
314                # get the index of the page and then the page that we're leaving
315                old_no = notebook.get_current_page()
316                old_child = notebook.get_nth_page(old_no)
317               
318                new_child = notebook.get_nth_page(page_num)
319               
320                old_jid = ''
321                new_jid = ''
322                for jid in self.xmls:
323                        if self.childs[jid] == new_child:
324                                new_jid = jid
325                        elif self.childs[jid] == old_child:
326                                old_jid = jid
327                       
328                        if old_jid != '' and new_jid != '': # we found both jids
329                                break # so stop looping
330               
331                if self.widget_name == 'tabbed_chat_window':
332                        # send chatstate inactive to the one we're leaving
333                        # and active to the one we visit
334                        if old_jid != '':
335                                self.send_chatstate('inactive', old_jid)
336                        self.send_chatstate('active', new_jid)
337
338                conversation_textview = self.xmls[new_jid].get_widget(
339                        'conversation_textview')
340                conversation_buffer = conversation_textview.get_buffer()
341                end_iter = conversation_buffer.get_end_iter()
342                end_rect = conversation_textview.get_iter_location(end_iter)
343                visible_rect = conversation_textview.get_visible_rect()
344                if end_rect.y <= (visible_rect.y + visible_rect.height):
345                        #we are at the end
346                        if self.nb_unread[new_jid] > 0:
347                                self.nb_unread[new_jid] = 0
348                                self.redraw_tab(new_jid)
349                                self.show_title()
350                                if self.plugin.systray_enabled:
351                                        self.plugin.systray.remove_jid(new_jid, self.account)
352               
353                conversation_textview.grab_focus()
354
355        def set_active_tab(self, jid):
356                self.notebook.set_current_page(self.notebook.page_num(self.childs[jid]))
357
358        def remove_tab(self, jid, kind): #kind is 'chats' or 'gc'
359                if len(self.xmls) == 1: # only one tab when we asked to remove
360                        # so destroy window
361               
362                        # we check and possibly save positions here, because Ctrl+W, Escape
363                        # etc.. call remove_tab so similar code in delete_event callbacks
364                        # is not enough
365                        if gajim.config.get('saveposition'):
366                                if kind == 'chats':
367                                        x, y = self.window.get_position()
368                                        gajim.config.set('chat-x-position', x)
369                                        gajim.config.set('chat-y-position', y)
370                                        width, height = self.window.get_size()
371                                        gajim.config.set('chat-width', width)
372                                        gajim.config.set('chat-height', height)
373                                elif kind == 'gc':
374                                        gajim.config.set('gc-hpaned-position', self.hpaned_position)
375                                        x, y = self.window.get_position()
376                                        gajim.config.set('gc-x-position', x)
377                                        gajim.config.set('gc-y-position', y)
378                                        width, height = self.window.get_size()
379                                        gajim.config.set('gc-width', width)
380                                        gajim.config.set('gc-height', height)
381
382                        self.window.destroy()
383                else:
384                        if self.nb_unread[jid] > 0:
385                                self.nb_unread[jid] = 0
386                                if self.plugin.systray_enabled:
387                                        self.plugin.systray.remove_jid(jid, self.account)
388                        if self.print_time_timeout_id.has_key(jid):
389                                gobject.source_remove(self.print_time_timeout_id[jid])
390                                del self.print_time_timeout_id[jid]
391
392                        self.notebook.remove_page(self.notebook.page_num(self.childs[jid]))
393                               
394                       
395
396                if self.plugin.windows[self.account][kind].has_key(jid):
397                        del self.plugin.windows[self.account][kind][jid]
398                del self.nb_unread[jid]
399                del gajim.last_message_time[self.account][jid]
400                del self.last_time_printout[jid]
401                del self.xmls[jid]
402                del self.childs[jid]
403                del self.tagIn[jid]
404                del self.tagOut[jid]
405                del self.tagStatus[jid]
406               
407                if len(self.xmls) == 1: # we now have only one tab
408                        self.notebook.set_show_tabs(False)
409                        self.show_title()
410       
411        def bring_scroll_to_end(self, textview, diff_y = 0):
412                ''' scrolls to the end of textview if end is not visible '''
413                buffer = textview.get_buffer()
414                buffer.begin_user_action()
415                at_the_end = False
416                end_iter = buffer.get_end_iter()
417                end_rect = textview.get_iter_location(end_iter)
418                visible_rect = textview.get_visible_rect()
419                # scroll only if expected end is not visible
420                if end_rect.y >= (visible_rect.y + visible_rect.height + diff_y):
421                        gobject.idle_add(self.scroll_to_end_iter, textview)
422                       
423        def scroll_to_end_iter(self, textview):
424                buffer = textview.get_buffer()
425                end_iter = buffer.get_end_iter()
426                textview.scroll_to_iter(end_iter, 0, False, 1, 1)
427                return False
428               
429        def size_request(self, message_textview , requisition, xml_top):
430                ''' When message_textview changes its size. If the new height
431                will enlarge the window, enable the scrollbar automatic policy'''
432                if message_textview.window is None:
433                        return
434                message_scrolledwindow = xml_top.get_widget('message_scrolledwindow')
435                conversation_scrolledwindow = \
436                        xml_top.get_widget('conversation_scrolledwindow')
437                conversation_textview = \
438                        xml_top.get_widget('conversation_textview')
439
440                min_height = conversation_scrolledwindow.get_property('height-request')
441                conversation_height = conversation_textview.window.get_size()[1]
442                message_height = message_textview.window.get_size()[1]
443                # new tab is not exposed yet
444                if conversation_height < 2:
445                        return
446               
447                if conversation_height < min_height:
448                        min_height = conversation_height
449                       
450                diff_y =  message_height - requisition.height
451                if diff_y is not 0:
452                        if  conversation_height + diff_y < min_height:
453                                if message_height + conversation_height - min_height > min_height:
454                                        message_scrolledwindow.set_property('vscrollbar-policy', 
455                                                gtk.POLICY_AUTOMATIC)
456                                        message_scrolledwindow.set_property('hscrollbar-policy', 
457                                                gtk.POLICY_AUTOMATIC)
458                                        message_scrolledwindow.set_property('height-request', 
459                                                message_height + conversation_height - min_height)
460                                        self.bring_scroll_to_end(message_textview)
461                        else:
462                                message_scrolledwindow.set_property('vscrollbar-policy', 
463                                        gtk.POLICY_NEVER)
464                                message_scrolledwindow.set_property('hscrollbar-policy', 
465                                        gtk.POLICY_NEVER)
466                                message_scrolledwindow.set_property('height-request', -1)
467                self.bring_scroll_to_end(conversation_textview, diff_y - 18)
468                return True
469
470        def on_tab_eventbox_button_press_event(self, widget, event, child):
471                if event.button == 3:
472                        n = self.notebook.page_num(child)
473                        self.notebook.set_current_page(n)
474                        self.popup_menu(event)
475
476        def new_tab(self, jid):
477                #FIXME: text formating buttons will be hidden in 0.8 release
478                for w in ['bold_togglebutton', 'italic_togglebutton', 'underline_togglebutton']:
479                        self.xmls[jid].get_widget(w).set_no_show_all(True)
480
481                self.set_compact_view(self.always_compact_view)
482                self.nb_unread[jid] = 0
483                gajim.last_message_time[self.account][jid] = 0
484                self.last_time_printout[jid] = 0.
485                font = pango.FontDescription(gajim.config.get('conversation_font'))
486               
487                if gajim.config.get('use_speller') and 'gtkspell' in globals():
488                        message_textview = self.xmls[jid].get_widget('message_textview')
489                        try:
490                                gtkspell.Spell(message_textview)
491                        except gobject.GError, msg:
492                                #FIXME: add a ui for this use spell.set_language()
493                                dialogs.ErrorDialog(str(msg), _('If that is not your language for which you want to highlight misspelled words, then please set your $LANG as appropriate. Eg. for French do export LANG=fr_FR or export LANG=fr_FR.UTF-8 in ~/.bash_profile or to make it global in /etc/profile.\n\nHighlighting misspelled words feature will not be used')).get_response()
494                                gajim.config.set('use_speller', False)
495               
496                conversation_textview = self.xmls[jid].get_widget(
497                        'conversation_textview')
498                conversation_textview.modify_font(font)
499                conversation_buffer = conversation_textview.get_buffer()
500                end_iter = conversation_buffer.get_end_iter()
501               
502                conversation_buffer.create_mark('end', end_iter, False)
503               
504                self.tagIn[jid] = conversation_buffer.create_tag('incoming')
505                color = gajim.config.get('inmsgcolor')
506                self.tagIn[jid].set_property('foreground', color)
507                self.tagOut[jid] = conversation_buffer.create_tag('outgoing')
508                color = gajim.config.get('outmsgcolor')
509                self.tagOut[jid].set_property('foreground', color)
510                self.tagStatus[jid] = conversation_buffer.create_tag('status')
511                color = gajim.config.get('statusmsgcolor')
512                self.tagStatus[jid].set_property('foreground', color)
513
514                tag = conversation_buffer.create_tag('marked')
515                color = gajim.config.get('markedmsgcolor')
516                tag.set_property('foreground', color)
517                tag.set_property('weight', pango.WEIGHT_BOLD)
518
519                tag = conversation_buffer.create_tag('time_sometimes')
520                tag.set_property('foreground', '#9e9e9e')
521                tag.set_property('scale', pango.SCALE_SMALL)
522                tag.set_property('justification', gtk.JUSTIFY_CENTER)
523               
524                tag = conversation_buffer.create_tag('small')
525                tag.set_property('scale', pango.SCALE_SMALL)
526               
527                tag = conversation_buffer.create_tag('grey')
528                tag.set_property('foreground', '#9e9e9e')
529               
530                tag = conversation_buffer.create_tag('url')
531                tag.set_property('foreground', '#0000ff')
532                tag.set_property('underline', pango.UNDERLINE_SINGLE)
533                tag.connect('event', self.hyperlink_handler, 'url')
534
535                tag = conversation_buffer.create_tag('mail')
536                tag.set_property('foreground', '#0000ff')
537                tag.set_property('underline', pango.UNDERLINE_SINGLE)
538                tag.connect('event', self.hyperlink_handler, 'mail')
539               
540                tag = conversation_buffer.create_tag('bold')
541                tag.set_property('weight', pango.WEIGHT_BOLD)
542               
543                tag = conversation_buffer.create_tag('italic')
544                tag.set_property('style', pango.STYLE_ITALIC)
545               
546                tag = conversation_buffer.create_tag('underline')
547                tag.set_property('underline', pango.UNDERLINE_SINGLE)
548               
549                self.xmls[jid].signal_autoconnect(self)
550                conversation_scrolledwindow = self.xmls[jid].get_widget(
551                        'conversation_scrolledwindow')
552                conversation_scrolledwindow.get_vadjustment().connect('value-changed',
553                        self.on_conversation_vadjustment_value_changed)
554               
555
556                if len(self.xmls) > 1:
557                        self.notebook.set_show_tabs(True)
558
559                if self.widget_name == 'tabbed_chat_window':
560                        xm = gtk.glade.XML(GTKGUI_GLADE, 'chats_eventbox', APP)
561                        tab_hbox = xm.get_widget('chats_eventbox')
562                elif self.widget_name == 'groupchat_window':
563                        xm = gtk.glade.XML(GTKGUI_GLADE, 'gc_eventbox', APP)
564                        tab_hbox = xm.get_widget('gc_eventbox')
565
566                child = self.childs[jid]
567
568                xm.signal_connect('on_close_button_clicked',
569                        self.on_close_button_clicked, jid)
570                xm.signal_connect('on_tab_eventbox_button_press_event',
571                        self.on_tab_eventbox_button_press_event, child)
572