/
roster_window.py
2788 lines (2506 loc) · 100 KB
/
roster_window.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## roster_window.py
##
## Contributors for this file:
## - Yann Le Boulanger <asterix@lagaule.org>
## - Nikos Kouremenos <kourem@gmail.com>
## - Dimitur Kirov <dkirov@gmail.com>
##
## Copyright (C) 2003-2004 Yann Le Boulanger <asterix@lagaule.org>
## Vincent Hanquez <tab@snarc.org>
## Copyright (C) 2005 Yann Le Boulanger <asterix@lagaule.org>
## Vincent Hanquez <tab@snarc.org>
## Nikos Kouremenos <nkour@jabber.org>
## Dimitur Kirov <dkirov@gmail.com>
## Travis Shirk <travis@pobox.com>
## Norman Rasmussen <norman@rasmussen.co.za>
##
## This program 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 2 only.
##
## This program 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.
##
import gtk
import gtk.glade
import gobject
import os
import time
import common.sleepy
import history_window
import dialogs
import vcard
import config
import disco
import gtkgui_helpers
import cell_renderer_image
import tooltips
import message_control
from common import gajim
from common import helpers
from common import i18n
from message_window import MessageWindowMgr
from chat_control import ChatControl
from groupchat_control import GroupchatControl
from groupchat_control import PrivateChatControl
_ = i18n._
APP = i18n.APP
gtk.glade.bindtextdomain(APP, i18n.DIR)
gtk.glade.textdomain(APP)
#(icon, name, type, jid, account, editable, second pixbuf)
(
C_IMG, # image to show state (online, new message etc)
C_NAME, # cellrenderer text that holds contact nickame
C_TYPE, # account, group or contact?
C_JID, # the jid of the row
C_ACCOUNT, # cellrenderer text that holds account name
C_EDITABLE, # cellrenderer text that holds name editable or not?
C_SECPIXBUF, # secondary_pixbuf (holds avatar or padlock)
) = range(7)
GTKGUI_GLADE = 'gtkgui.glade'
DEFAULT_ICONSET = 'dcraven'
class RosterWindow:
'''Class for main window of gtkgui interface'''
def get_account_iter(self, name):
model = self.tree.get_model()
if model is None:
return
account_iter = model.get_iter_root()
if self.regroup:
return account_iter
while account_iter:
account_name = model[account_iter][C_NAME].decode('utf-8')
if name == account_name:
break
account_iter = model.iter_next(account_iter)
return account_iter
def get_group_iter(self, name, account):
model = self.tree.get_model()
root = self.get_account_iter(account)
group_iter = model.iter_children(root)
# C_NAME column contacts the pango escaped group name
name = gtkgui_helpers.escape_for_pango_markup(name)
while group_iter:
group_name = model[group_iter][C_NAME].decode('utf-8')
if name == group_name:
break
group_iter = model.iter_next(group_iter)
return group_iter
def get_contact_iter(self, jid, account):
model = self.tree.get_model()
acct = self.get_account_iter(account)
found = []
if model is None: # when closing Gajim model can be none (async pbs?)
return found
group_iter = model.iter_children(acct)
while group_iter:
contact_iter = model.iter_children(group_iter)
while contact_iter:
if jid == model[contact_iter][C_JID].decode('utf-8') and \
account == model[contact_iter][C_ACCOUNT].decode('utf-8'):
found.append(contact_iter)
contact_iter = model.iter_next(contact_iter)
group_iter = model.iter_next(group_iter)
return found
def add_account_to_roster(self, account):
model = self.tree.get_model()
if self.get_account_iter(account):
return
if self.regroup:
show = helpers.get_global_show()
model.append(None, [self.jabber_state_images['16'][show],
_('Merged accounts'), 'account', '', 'all', False, None])
return
show = gajim.SHOW_LIST[gajim.connections[account].connected]
tls_pixbuf = None
if gajim.con_types.has_key(account) and \
gajim.con_types[account] in ('tls', 'ssl'):
tls_pixbuf = self.window.render_icon(gtk.STOCK_DIALOG_AUTHENTICATION,
gtk.ICON_SIZE_MENU) # the only way to create a pixbuf from stock
our_jid = gajim.get_jid_from_account(account)
model.append(None, [self.jabber_state_images['16'][show],
gtkgui_helpers.escape_for_pango_markup(account),
'account', our_jid, account, False, tls_pixbuf])
def remove_newly_added(self, jid, account):
if jid in gajim.newly_added[account]:
gajim.newly_added[account].remove(jid)
self.draw_contact(jid, account)
def add_contact_to_roster(self, jid, account):
'''Add a contact to the roster and add groups if they aren't in roster'''
showOffline = gajim.config.get('showoffline')
contact = gajim.contacts.get_first_contact_from_jid(account, jid)
if not contact:
return
if contact.jid.find('@') <= 0:
# if not '@' or '@' starts the jid ==> agent
contact.groups = [_('Transports')]
# JEP-0162
hide = True
if contact.sub in ('both', 'to'):
hide = False
elif contact.ask == 'subscribe':
hide = False
elif contact.name or len(contact.groups):
hide = False
observer = False
if hide and contact.sub == 'from':
observer = True
if (contact.show in ('offline', 'error') or hide) and \
not showOffline and (not _('Transports') in contact.groups or \
gajim.connections[account].connected < 2) and \
not gajim.awaiting_events[account].has_key(jid):
return
model = self.tree.get_model()
groups = contact.groups
if observer:
groups = [_('Observers')]
if not groups:
groups = [_('General')]
for g in groups:
iterG = self.get_group_iter(g, account)
if not iterG:
IterAcct = self.get_account_iter(account)
iterG = model.append(IterAcct, [
self.jabber_state_images['16']['closed'],
gtkgui_helpers.escape_for_pango_markup(g), 'group', g, account,
False, None])
if not gajim.groups[account].has_key(g): #It can probably never append
if account + g in self.collapsed_rows:
ishidden = False
else:
ishidden = True
gajim.groups[account][g] = { 'expand': ishidden }
if not account in self.collapsed_rows:
self.tree.expand_row((model.get_path(iterG)[0]), False)
typestr = 'contact'
if g == _('Transports'):
typestr = 'agent'
name = contact.get_shown_name()
# we add some values here. see draw_contact for more
model.append(iterG, (None, name, typestr, contact.jid, account,
False, None))
if gajim.groups[account][g]['expand']:
self.tree.expand_row(model.get_path(iterG), False)
self.draw_contact(jid, account)
self.draw_avatar(jid, account)
def add_transport_to_roster(self, account, transport):
c = gajim.contacts.create_contact(jid = transport, name = transport,
groups = [_('Transports')], show = 'offline', status = 'offline',
sub = 'from')
gajim.contacts.add_contact(account, c)
gajim.interface.roster.add_contact_to_roster(transport, account)
def really_remove_contact(self, contact, account):
if contact.jid in gajim.newly_added[account]:
return
if contact.jid.find('@') < 1 and gajim.connections[account].connected > 1: # It's an agent
return
if contact.jid in gajim.to_be_removed[account]:
gajim.to_be_removed[account].remove(contact.jid)
if gajim.config.get('showoffline'):
self.draw_contact(contact.jid, account)
return
self.remove_contact(contact, account)
def remove_contact(self, contact, account):
'''Remove a contact from the roster'''
if contact.jid in gajim.to_be_removed[account]:
return
model = self.tree.get_model()
for i in self.get_contact_iter(contact.jid, account):
parent_i = model.iter_parent(i)
group = model.get_value(parent_i, 3).decode('utf-8')
model.remove(i)
if model.iter_n_children(parent_i) == 0:
model.remove(parent_i)
# We need to check all contacts, even offline contacts
for jid in gajim.contacts.get_jid_list(account):
if group in gajim.contacts.get_contact_with_highest_priority(
account, jid).groups:
break
else:
if gajim.groups[account].has_key(group):
del gajim.groups[account][group]
def get_appropriate_state_images(self, jid, size = '16'):
'''check jid and return the appropriate state images dict for
the demanded size'''
transport = gajim.get_transport_name_from_jid(jid)
if transport:
return self.transports_state_images[size][transport]
return self.jabber_state_images[size]
def draw_contact(self, jid, account, selected = False, focus = False):
'''draw the correct state image, name BUT not avatar'''
# focus is about if the roster window has toplevel-focus or not
model = self.tree.get_model()
iters = self.get_contact_iter(jid, account)
if len(iters) == 0:
return
contact_instances = gajim.contacts.get_contact(account, jid)
contact = gajim.contacts.get_highest_prio_contact_from_contacts(
contact_instances)
if not contact:
return
name = gtkgui_helpers.escape_for_pango_markup(contact.get_shown_name())
if len(contact_instances) > 1:
name += ' (' + unicode(len(contact_instances)) + ')'
# FIXME: remove when we use metacontacts
# shoz (account_name) if there are 2 contact with same jid in merged mode
if self.regroup:
add_acct = False
# look through all contacts of all accounts
for a in gajim.connections:
for j in gajim.contacts.get_jid_list(a):
# [0] cause it'fster than highest_prio
c = gajim.contacts.get_first_contact_from_jid(a, j)
if c.name == contact.name and (j, a) != (jid, account):
add_acct = True
break
if add_acct:
# No need to continue in other account if we already found one
break
if add_acct:
name += ' (' + account + ')'
# add status msg, if not empty, under contact name in the treeview
if contact.status and gajim.config.get('show_status_msgs_in_roster'):
status = contact.status.strip()
if status != '':
status = gtkgui_helpers.reduce_chars_newlines(status, max_lines = 1)
# escape markup entities and make them small italic and fg color
color = gtkgui_helpers._get_fade_color(self.tree, selected, focus)
colorstring = "#%04x%04x%04x" % (color.red, color.green, color.blue)
name += '\n<span size="small" style="italic" foreground="%s">%s</span>'\
% (colorstring, gtkgui_helpers.escape_for_pango_markup(status))
icon_name = helpers.get_icon_name_to_show(contact, account)
state_images = self.get_appropriate_state_images(jid, size = '16')
img = state_images[icon_name]
for iter in iters:
model[iter][C_IMG] = img
model[iter][C_NAME] = name
def draw_avatar(self, jid, account):
'''draw the avatar'''
model = self.tree.get_model()
iters = self.get_contact_iter(jid, account)
if gajim.config.get('show_avatars_in_roster'):
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(jid)
if pixbuf in ('ask', None):
scaled_pixbuf = None
else:
scaled_pixbuf = gtkgui_helpers.get_scaled_pixbuf(pixbuf, 'roster')
else:
scaled_pixbuf = None
for iter in iters:
model[iter][C_SECPIXBUF] = scaled_pixbuf
def join_gc_room(self, account, room_jid, nick, password):
'''joins the room immediatelly'''
if gajim.interface.msg_win_mgr.has_window(room_jid) and \
gajim.gc_connected[account][room_jid]:
dialogs.ErrorDialog(_('You are already in room %s') % room_jid
).get_response()
return
invisible_show = gajim.SHOW_LIST.index('invisible')
if gajim.connections[account].connected == invisible_show:
dialogs.ErrorDialog(_('You cannot join a room while you are invisible')
).get_response()
return
room, server = room_jid.split('@')
if not gajim.interface.msg_win_mgr.has_window(room_jid):
self.new_room(room_jid, nick, account)
gc_win = gajim.interface.msg_win_mgr.get_window(room_jid)
gc_win.set_active_tab(room_jid)
gc_win.window.present()
gajim.connections[account].join_gc(nick, room, server, password)
if password:
gajim.gc_passwords[room_jid] = password
def on_bookmark_menuitem_activate(self, widget, account, bookmark):
self.join_gc_room(account, bookmark['jid'], bookmark['nick'],
bookmark['password'])
def on_bm_header_changed_state(self, widget, event):
widget.set_state(gtk.STATE_NORMAL) #do not allow selected_state
def on_send_server_message_menuitem_activate(self, widget, account):
server = gajim.config.get_per('accounts', account, 'hostname')
server += '/announce/online'
dialogs.SingleMessageWindow(account, server, 'send')
def on_xml_console_menuitem_activate(self, widget, account):
if gajim.interface.instances[account].has_key('xml_console'):
gajim.interface.instances[account]['xml_console'].window.present()
else:
gajim.interface.instances[account]['xml_console'].window.show_all()
def on_set_motd_menuitem_activate(self, widget, account):
server = gajim.config.get_per('accounts', account, 'hostname')
server += '/announce/motd'
dialogs.SingleMessageWindow(account, server, 'send')
def on_update_motd_menuitem_activate(self, widget, account):
server = gajim.config.get_per('accounts', account, 'hostname')
server += '/announce/motd/update'
dialogs.SingleMessageWindow(account, server, 'send')
def on_delete_motd_menuitem_activate(self, widget, account):
server = gajim.config.get_per('accounts', account, 'hostname')
server += '/announce/motd/delete'
gajim.connections[account].send_motd(server)
def on_online_users_menuitem_activate(self, widget, account):
pass #FIXME: impement disco in users for 0.9
def get_and_connect_advanced_menuitem_menu(self, account):
xml = gtk.glade.XML(GTKGUI_GLADE, 'advanced_menuitem_menu', APP)
advanced_menuitem_menu = xml.get_widget('advanced_menuitem_menu')
send_single_message_menuitem = xml.get_widget(
'send_single_message_menuitem')
xml_console_menuitem = xml.get_widget('xml_console_menuitem')
administrator_menuitem = xml.get_widget('administrator_menuitem')
online_users_menuitem = xml.get_widget('online_users_menuitem')
send_server_message_menuitem = xml.get_widget(
'send_server_message_menuitem')
set_motd_menuitem = xml.get_widget('set_motd_menuitem')
update_motd_menuitem = xml.get_widget('update_motd_menuitem')
delete_motd_menuitem = xml.get_widget('delete_motd_menuitem')
send_single_message_menuitem.connect('activate',
self.on_send_single_message_menuitem_activate, account)
xml_console_menuitem.connect('activate',
self.on_xml_console_menuitem_activate, account)
#FIXME: 0.9 should have this: it does disco in the place where users are
online_users_menuitem.set_no_show_all(True)
online_users_menuitem.hide()
online_users_menuitem.connect('activate',
self.on_online_users_menuitem_activate, account)
send_server_message_menuitem.connect('activate',
self.on_send_server_message_menuitem_activate, account)
set_motd_menuitem.connect('activate',
self.on_set_motd_menuitem_activate, account)
update_motd_menuitem.connect('activate',
self.on_update_motd_menuitem_activate, account)
delete_motd_menuitem.connect('activate',
self.on_delete_motd_menuitem_activate, account)
advanced_menuitem_menu.show_all()
return advanced_menuitem_menu
def make_menu(self):
'''create the main window's menus'''
new_message_menuitem = self.xml.get_widget('new_message_menuitem')
join_gc_menuitem = self.xml.get_widget('join_gc_menuitem')
add_new_contact_menuitem = self.xml.get_widget('add_new_contact_menuitem')
service_disco_menuitem = self.xml.get_widget('service_disco_menuitem')
advanced_menuitem = self.xml.get_widget('advanced_menuitem')
show_offline_contacts_menuitem = self.xml.get_widget(
'show_offline_contacts_menuitem')
profile_avatar_menuitem = self.xml.get_widget('profile_avatar_menuitem')
# make it sensitive. it is insensitive only if no accounts are *available*
advanced_menuitem.set_sensitive(True)
if self.add_new_contact_handler_id:
add_new_contact_menuitem.handler_disconnect(
self.add_new_contact_handler_id)
self.add_new_contact_handler_id = None
if self.service_disco_handler_id:
service_disco_menuitem.handler_disconnect(
self.service_disco_handler_id)
self.service_disco_handler_id = None
if self.new_message_menuitem_handler_id:
new_message_menuitem.handler_disconnect(
self.new_message_menuitem_handler_id)
self.new_message_menuitem_handler_id = None
# remove the existing submenus
add_new_contact_menuitem.remove_submenu()
service_disco_menuitem.remove_submenu()
join_gc_menuitem.remove_submenu()
new_message_menuitem.remove_submenu()
advanced_menuitem.remove_submenu()
# remove the existing accelerator
if self.have_new_message_accel:
ag = gtk.accel_groups_from_object(self.window)[0]
new_message_menuitem.remove_accelerator(ag, gtk.keysyms.n,
gtk.gdk.CONTROL_MASK)
self.have_new_message_accel = False
# join gc
sub_menu = gtk.Menu()
join_gc_menuitem.set_submenu(sub_menu)
at_least_one_account_connected = False
multiple_accounts = len(gajim.connections) >= 2 #FIXME: stop using bool var here
for account in gajim.connections:
if gajim.connections[account].connected <= 1: # if offline or connecting
continue
if not at_least_one_account_connected:
at_least_one_account_connected = True
if multiple_accounts:
label = gtk.Label()
label.set_markup('<u>' + account.upper() +'</u>')
label.set_use_underline(False)
item = gtk.MenuItem()
item.add(label)
item.connect('state-changed', self.on_bm_header_changed_state)
sub_menu.append(item)
item = gtk.MenuItem(_('_Join New Room'))
item.connect('activate', self.on_join_gc_activate, account)
sub_menu.append(item)
for bookmark in gajim.connections[account].bookmarks:
item = gtk.MenuItem(bookmark['name'], False) # Do not use underline
item.connect('activate', self.on_bookmark_menuitem_activate,
account, bookmark)
sub_menu.append(item)
if at_least_one_account_connected: #FIXME: move this below where we do this check
#and make sure it works
newitem = gtk.SeparatorMenuItem() # seperator
sub_menu.append(newitem)
newitem = gtk.ImageMenuItem(_('Manage Bookmarks...'))
img = gtk.image_new_from_stock(gtk.STOCK_PREFERENCES,
gtk.ICON_SIZE_MENU)
newitem.set_image(img)
newitem.connect('activate', self.on_manage_bookmarks_menuitem_activate)
sub_menu.append(newitem)
sub_menu.show_all()
if multiple_accounts: # 2 or more accounts? make submenus
#add
sub_menu = gtk.Menu()
for account in gajim.connections:
if gajim.connections[account].connected <= 1:
#if offline or connecting
continue
item = gtk.MenuItem(_('to %s account') % account, False)
sub_menu.append(item)
item.connect('activate', self.on_add_new_contact, account)
add_new_contact_menuitem.set_submenu(sub_menu)
sub_menu.show_all()
#disco
sub_menu = gtk.Menu()
for account in gajim.connections:
if gajim.connections[account].connected <= 1:
#if offline or connecting
continue
item = gtk.MenuItem(_('using %s account') % account, False)
sub_menu.append(item)
item.connect('activate', self.on_service_disco_menuitem_activate,
account)
service_disco_menuitem.set_submenu(sub_menu)
sub_menu.show_all()
#new message
sub_menu = gtk.Menu()
for account in gajim.connections:
if gajim.connections[account].connected <= 1:
#if offline or connecting
continue
item = gtk.MenuItem(_('using account %s') % account, False)
sub_menu.append(item)
item.connect('activate', self.on_new_message_menuitem_activate,
account)
new_message_menuitem.set_submenu(sub_menu)
sub_menu.show_all()
#Advanced Actions
sub_menu = gtk.Menu()
for account in gajim.connections:
item = gtk.MenuItem(_('for account %s') % account, False)
sub_menu.append(item)
advanced_menuitem_menu = self.get_and_connect_advanced_menuitem_menu(
account)
item.set_submenu(advanced_menuitem_menu)
advanced_menuitem.set_submenu(sub_menu)
sub_menu.show_all()
else:
if len(gajim.connections) == 1: # user has only one account
#add
if not self.add_new_contact_handler_id:
self.add_new_contact_handler_id = add_new_contact_menuitem.connect(
'activate', self.on_add_new_contact, gajim.connections.keys()[0])
#disco
if not self.service_disco_handler_id:
self.service_disco_handler_id = service_disco_menuitem.connect(
'activate', self.on_service_disco_menuitem_activate,
gajim.connections.keys()[0])
#new msg
if not self.new_message_menuitem_handler_id:
self.new_message_menuitem_handler_id = new_message_menuitem.\
connect('activate', self.on_new_message_menuitem_activate,
gajim.connections.keys()[0])
#new msg accel
if not self.have_new_message_accel:
ag = gtk.accel_groups_from_object(self.window)[0]
new_message_menuitem.add_accelerator('activate', ag,
gtk.keysyms.n, gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE)
self.have_new_message_accel = True
account = gajim.connections.keys()[0]
advanced_menuitem_menu = self.get_and_connect_advanced_menuitem_menu(
account)
advanced_menuitem.set_submenu(advanced_menuitem_menu)
elif len(gajim.connections) == 0: # user has no accounts
advanced_menuitem.set_sensitive(False)
#FIXME: Gajim 0.9 should have this visible
profile_avatar_menuitem.set_no_show_all(True)
profile_avatar_menuitem.hide()
if at_least_one_account_connected:
new_message_menuitem.set_sensitive(True)
join_gc_menuitem.set_sensitive(True)
add_new_contact_menuitem.set_sensitive(True)
service_disco_menuitem.set_sensitive(True)
show_offline_contacts_menuitem.set_sensitive(True)
else:
# make the menuitems insensitive
new_message_menuitem.set_sensitive(False)
join_gc_menuitem.set_sensitive(False)
add_new_contact_menuitem.set_sensitive(False)
service_disco_menuitem.set_sensitive(False)
show_offline_contacts_menuitem.set_sensitive(False)
profile_avatar_menuitem.set_sensitive(False)
def _change_style(self, model, path, iter, option):
if option is None:
model[iter][C_NAME] = model[iter][C_NAME]
elif model[iter][C_TYPE] == 'account':
if option == 'account':
model[iter][C_NAME] = model[iter][C_NAME]
elif model[iter][C_TYPE] == 'group':
if option == 'group':
model[iter][C_NAME] = model[iter][C_NAME]
elif model[iter][C_TYPE] == 'contact':
if option == 'contact':
model[iter][C_NAME] = model[iter][C_NAME]
def change_roster_style(self, option):
model = self.tree.get_model()
model.foreach(self._change_style, option)
for win in gajim.interface.msg_win_mgr.windows():
win.repaint_themed_widgets()
# update gc's roster
for ctl in gajim.interface.msg_win_mgr.controls():
if ctl.type_id == message_control.TYPE_GC:
ctl.update_ui()
def draw_roster(self):
'''Clear and draw roster'''
self.tree.get_model().clear()
for acct in gajim.connections:
self.add_account_to_roster(acct)
for jid in gajim.contacts.get_jid_list(acct):
self.add_contact_to_roster(jid, acct)
self.make_menu() # re-make menu in case an account was removed
#FIXME: maybe move thie make_menu() in where we remove the account?
def fill_contacts_and_groups_dicts(self, array, account):
'''fill gajim.contacts and gajim.groups'''
if account not in gajim.contacts.get_accounts():
gajim.contacts.add_account(account)
if not gajim.groups.has_key(account):
gajim.groups[account] = {}
for jid in array.keys():
jids = jid.split('/')
#get jid
ji = jids[0]
#get resource
resource = ''
if len(jids) > 1:
resource = '/'.join(jids[1:])
#get name
name = array[jid]['name']
if not name:
name = ''
show = 'offline' # show is offline by default
status = '' #no status message by default
keyID = ''
attached_keys = gajim.config.get_per('accounts', account,
'attached_gpg_keys').split()
if jid in attached_keys:
keyID = attached_keys[attached_keys.index(jid) + 1]
contact1 = gajim.contacts.create_contact(jid = ji, name = name,
groups = array[jid]['groups'], show = show, status = status,
sub = array[jid]['subscription'], ask = array[jid]['ask'],
resource = resource, keyID = keyID)
gajim.contacts.add_contact(account, contact1)
# when we draw the roster, we avoid having the same contact
# more than once (f.e. we avoid showing it twice when 2 resources)
for g in array[jid]['groups']:
if g in gajim.groups[account].keys():
continue
if account + g in self.collapsed_rows:
ishidden = False
else:
ishidden = True
gajim.groups[account][g] = { 'expand': ishidden }
if gajim.config.get('ask_avatars_on_startup'):
pixbuf = gtkgui_helpers.get_avatar_pixbuf_from_cache(ji)
if pixbuf == 'ask':
jid_with_resource = contact1.jid
if contact1.resource:
jid_with_resource += '/' + contact1.resource
gajim.connections[account].request_vcard(jid_with_resource)
def chg_contact_status(self, contact, show, status, account):
'''When a contact changes his or her status'''
showOffline = gajim.config.get('showoffline')
contact_instances = gajim.contacts.get_contact(account, contact.jid)
contact.show = show
contact.status = status
if show in ('offline', 'error') and \
not gajim.awaiting_events[account].has_key(contact.jid):
if len(contact_instances) > 1:
# if multiple resources
gajim.contacts.remove_contact(account, contact)
self.draw_contact(contact.jid, account)
elif not showOffline:
# we don't show offline contacts
self.remove_contact(contact, account)
else:
self.draw_contact(contact.jid, account)
else:
if not self.get_contact_iter(contact.jid, account):
self.add_contact_to_roster(contact.jid, account)
self.draw_contact(contact.jid, account)
# print status in chat window and update status/GPG image
if gajim.interface.msg_win_mgr.has_window(contact.jid):
jid = contact.jid
win = gajim.interface.msg_win_mgr.get_window(contact.jid)
ctl = win.get_control(jid)
ctl.update_ui()
win.redraw_tab(contact)
name = contact.get_shown_name()
if contact.resource != '':
name += '/' + contact.resource
uf_show = helpers.get_uf_show(show)
ctl.print_conversation(_('%s is now %s (%s)') % (name, uf_show, status),
'status')
if contact == gajim.contacts.get_contact_with_highest_priority(account,
contact.jid):
ctl.draw_banner()
def on_info(self, widget, contact, account):
'''Call vcard_information_window class to display contact's information'''
info = gajim.interface.instances[account]['infos']
if info.has_key(contact.jid):
info[contact.jid].window.present()
else:
info[contact.jid] = vcard.VcardWindow(contact, account)
def show_tooltip(self, contact):
pointer = self.tree.get_pointer()
props = self.tree.get_path_at_pos(pointer[0], pointer[1])
if props and self.tooltip.id == props[0]:
# check if the current pointer is at the same path
# as it was before setting the timeout
rect = self.tree.get_cell_area(props[0], props[1])
position = self.tree.window.get_origin()
pointer = self.window.get_pointer()
self.tooltip.show_tooltip(contact, (pointer[0], rect.height),
(position[0], position[1] + rect.y))
else:
self.tooltip.hide_tooltip()
def on_roster_treeview_leave_notify_event(self, widget, event):
model = widget.get_model()
props = widget.get_path_at_pos(int(event.x), int(event.y))
if self.tooltip.timeout > 0:
if not props or self.tooltip.id == props[0]:
self.tooltip.hide_tooltip()
def on_roster_treeview_motion_notify_event(self, widget, event):
model = widget.get_model()
props = widget.get_path_at_pos(int(event.x), int(event.y))
if self.tooltip.timeout > 0:
if not props or self.tooltip.id != props[0]:
self.tooltip.hide_tooltip()
if props:
[row, col, x, y] = props
iter = None
try:
iter = model.get_iter(row)
except:
self.tooltip.hide_tooltip()
return
if model[iter][C_TYPE] == 'contact':
# we're on a contact entry in the roster
account = model[iter][C_ACCOUNT].decode('utf-8')
jid = model[iter][C_JID].decode('utf-8')
if self.tooltip.timeout == 0 or self.tooltip.id != props[0]:
self.tooltip.id = row
contacts = gajim.contacts.get_contact(account, jid)
self.tooltip.timeout = gobject.timeout_add(500,
self.show_tooltip, contacts)
elif model[iter][C_TYPE] == 'account':
# we're on an account entry in the roster
account = model[iter][C_ACCOUNT].decode('utf-8')
if account == 'all':
if self.tooltip.timeout == 0 or self.tooltip.id != props[0]:
self.tooltip.id = row
self.tooltip.timeout = gobject.timeout_add(500,
self.show_tooltip, [])
return
jid = gajim.get_jid_from_account(account)
contacts = []
connection = gajim.connections[account]
# get our current contact info
contact = gajim.contacts.create_contact(jid = jid, name = account,
show = connection.get_status(), sub = 'both',
status = connection.status,
resource = gajim.config.get_per('accounts', connection.name,
'resource'),
priority = gajim.config.get_per('accounts', connection.name,
'priority'),
keyID = gajim.config.get_per('accounts', connection.name,
'keyid'))
contacts.append(contact)
# if we're online ...
if connection.connection:
roster = connection.connection.getRoster()
if roster.getItem(jid):
resources = roster.getResources(jid)
# ...get the contact info for our other online resources
for resource in resources:
show = roster.getShow(jid+'/'+resource)
if not show:
show = 'online'
contact = gajim.contacts.create_contact(jid = jid,
name = account, show = show,
status = roster.getStatus(jid+'/'+resource),
resource = resource,
priority = roster.getPriority(jid+'/'+resource))
contacts.append(contact)
if self.tooltip.timeout == 0 or self.tooltip.id != props[0]:
self.tooltip.id = row
self.tooltip.timeout = gobject.timeout_add(500,
self.show_tooltip, contacts)
def on_agent_logging(self, widget, jid, state, account):
'''When an agent is requested to log in or off'''
gajim.connections[account].send_agent_status(jid, state)
def on_edit_agent(self, widget, contact, account):
'''When we want to modify the agent registration'''
gajim.connections[account].request_register_agent_info(contact.jid)
def on_remove_agent(self, widget, contact, account):
'''When an agent is requested to log in or off'''
if gajim.config.get_per('accounts', account, 'hostname') == contact.jid:
# We remove the server contact
# remove it from treeview
self.remove_contact(contact, account)
gajim.contacts.remove_contact(account, contact)
return
window = dialogs.ConfirmationDialog(_('Transport "%s" will be removed') % contact.jid, _('You will no longer be able to send and receive messages to contacts from this transport.'))
if window.get_response() == gtk.RESPONSE_OK:
gajim.connections[account].unsubscribe_agent(contact.jid + '/' \
+ contact.resource)
# remove transport from treeview
self.remove_contact(contact, account)
# remove transport's contacts from treeview
jid_list = gajim.contacts.get_jid_list(account)
for jid in jid_list:
if jid.endswith('@' + contact.jid):
c = gajim.contacts.get_first_contact_from_jid(account, jid)
gajim.log.debug(
'Removing contact %s due to unregistered transport %s'\
% (jid, contact.jid))
# Transport contacts can't have 2 resources
self.remove_contact(c, account)
gajim.contacts.remove_contact(account, contact)
def on_rename(self, widget, iter, path):
# this function is called either by F2 or by Rename menuitem
# to display that menuitem we show a menu, that does focus-out
# we then select Rename and focus-in
# focus-in callback checks on this var and if is NOT None
# it redraws the selected contact resulting in stopping our rename
# procedure. So set this to None to stop that
self._last_selected_contact = None
model = self.tree.get_model()
row_type = model[iter][C_TYPE]
jid = model[iter][C_JID].decode('utf-8')
account = model[iter][C_ACCOUNT].decode('utf-8')
if row_type == 'contact':
# it's jid
# Remove resource indicator (Name (2))
contact = gajim.contacts.get_first_contact_from_jid(account, jid)
name = contact.name
model[iter][C_NAME] = gtkgui_helpers.escape_for_pango_markup(name)
model[iter][C_EDITABLE] = True # set 'editable' to True
self.tree.set_cursor(path, self.tree.get_column(0), True)
def on_assign_pgp_key(self, widget, contact, account):
attached_keys = gajim.config.get_per('accounts', account,
'attached_gpg_keys').split()
keys = {}
keyID = 'None'
for i in xrange(0, len(attached_keys)/2):
keys[attached_keys[2*i]] = attached_keys[2*i+1]
if attached_keys[2*i] == contact.jid:
keyID = attached_keys[2*i+1]
public_keys = gajim.connections[account].ask_gpg_keys()
public_keys['None'] = 'None'
instance = dialogs.ChooseGPGKeyDialog(_('Assign OpenPGP Key'),
_('Select a key to apply to the contact'), public_keys, keyID)
keyID = instance.run()
if keyID is None:
return
if keyID[0] == 'None':
if contact.jid in keys:
del keys[contact.jid]
else:
keys[contact.jid] = keyID[0]
for u in gajim.contacts.get_contact(account, contact.jid):
u.keyID = keyID[0]
if gajim.interface.msg_win_mgr.has_window(contact.jid):
ctl = gajim.interface.msg_win_mgr.get_control(contact.jid)
ctl.update_ui()
keys_str = ''
for jid in keys:
keys_str += jid + ' ' + keys[jid] + ' '
gajim.config.set_per('accounts', account, 'attached_gpg_keys', keys_str)
def on_edit_groups(self, widget, contact, account):
dlg = dialogs.EditGroupsDialog(contact, account)
dlg.run()
def on_history(self, widget, contact, account):
'''When history menuitem is activated: call log window'''
if gajim.interface.instances['logs'].has_key(contact.jid):
gajim.interface.instances['logs'][contact.jid].window.present()
else:
gajim.interface.instances['logs'][contact.jid] = history_window.\
HistoryWindow(contact.jid, account)
def on_send_single_message_menuitem_activate(self, wiget, account,
contact = None):
if contact is None:
dialogs.SingleMessageWindow(account, action = 'send')
else:
dialogs.SingleMessageWindow(account, contact.jid, 'send')
def on_send_file_menuitem_activate(self, widget, account, contact):
gajim.interface.instances['file_transfers'].show_file_send_request(
account, contact)
def mk_menu_user(self, event, iter):
'''Make contact's popup menu'''
model = self.tree.get_model()
jid = model[iter][C_JID].decode('utf-8')
path = model.get_path(iter)
account = model[iter][C_ACCOUNT].decode('utf-8')
contact = gajim.contacts.get_contact_with_highest_priority(account, jid)
xml = gtk.glade.XML(GTKGUI_GLADE, 'roster_contact_context_menu',
APP)
roster_contact_context_menu = xml.get_widget(
'roster_contact_context_menu')
childs = roster_contact_context_menu.get_children()
start_chat_menuitem = childs[0]
send_single_message_menuitem = childs[1]
rename_menuitem = childs[2]
edit_groups_menuitem = childs[3]
# separator4 goes with assign_openpgp_key_menuitem
assign_openpgp_separator = childs[4]
send_file_menuitem = childs[5]
assign_openpgp_key_menuitem = childs[6]
#skip a seperator
send_auth_menuitem, ask_auth_menuitem, revoke_auth_menuitem =\
childs[8].get_submenu().get_children()
add_to_roster_menuitem = childs[9]
remove_from_roster_menuitem = childs[10]
#skip a seperator
information_menuitem = childs[12]
history_menuitem = childs[13]
if contact.resource:
send_file_menuitem.connect('activate',
self.on_send_file_menuitem_activate, account, contact)
else: # if we do not have resource we cannot send file
send_file_menuitem.hide()
send_file_menuitem.set_no_show_all(True)
start_chat_menuitem.connect('activate',
self.on_roster_treeview_row_activated, path)
send_single_message_menuitem.connect('activate',
self.on_send_single_message_menuitem_activate, account, contact)
rename_menuitem.connect('activate', self.on_rename, iter, path)
remove_from_roster_menuitem.connect('activate', self.on_req_usub,
contact, account)