forked from ThinkTank/TorChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tc_gui.py
1369 lines (1062 loc) · 43.7 KB
/
tc_gui.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
##############################################################################
# #
# Copyright (c) 2007-2008 Bernd Kreuss <prof7bit@gmail.com> #
# #
# This program is licensed under the GNU General Public License V3, #
# the full source code is included in the binary distribution. #
# #
# Included in the distribution are files from other open source projects: #
# - Tor (c) The Tor Project, 3-clause-BSD #
# - SocksiPy (c) Dan Haim, BSD Style License #
# - Gajim buddy status icons (c) The Gajim Team, GNU GPL #
# #
##############################################################################
# this is a graphical User interface for the TorChat client library.
import config
import logging as log
import wx
import tc_client
import sys
import os
import time
import subprocess
import textwrap
import threading
import version
import dlg_settings
import translations
lang = translations.lang_en
ICON_NAMES = {
tc_client.STATUS_OFFLINE: 'offline.png',
tc_client.STATUS_ONLINE: 'online.png',
tc_client.STATUS_HANDSHAKE: 'connecting.png',
tc_client.STATUS_AWAY: 'away.png',
tc_client.STATUS_XA: 'xa.png',
}
_icon_images = {} # this is a cache for getStatusBitmap()
def getStatusBitmap(status):
global _icon_images
if not status in _icon_images:
image = wx.Image(os.path.join(config.get('internal', 'icon_dir'
), ICON_NAMES[status]), wx.BITMAP_TYPE_PNG)
image.ConvertAlphaToMask()
_icon_images[status] = image
bitmap = _icon_images[status].ConvertToBitmap()
return bitmap
class TaskbarIcon(wx.TaskBarIcon):
def __init__(self, main_window):
wx.TaskBarIcon.__init__(self)
self.mw = main_window
self.showStatus(self.mw.buddy_list.own_status)
self.timer = wx.Timer(self, -1)
self.blink_phase = False
self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.onLeftClick)
self.Bind(wx.EVT_TIMER, self.onTimer)
def showEvent(self):
try:
icon = wx.IconFromBitmap(self.img_event.ConvertToBitmap())
except:
img = wx.Image(os.path.join(config.get('internal',
'icon_dir'), 'event.png'))
img.ConvertAlphaToMask()
self.img_event = img
icon = wx.IconFromBitmap(self.img_event.ConvertToBitmap())
self.SetIcon(icon, self.getToolTipText())
def showStatus(self, status):
icon = wx.IconFromBitmap(getStatusBitmap(status))
self.SetIcon(icon, self.getToolTipText())
def onLeftClick(self, evt):
self.mw.Show(not self.mw.IsShown())
def CreatePopupMenu(self):
return TaskbarMenu(self.mw)
def getToolTipText(self):
text = 'TorChat: %s' % config.getProfileLongName()
for window in self.mw.chat_windows:
if not window.IsShown():
text += '\n' + window.getTitleShort()
return text
def blink(self, start=True):
if start:
self.timer.Start(500, False)
else:
self.timer.Stop()
self.showStatus(self.mw.buddy_list.own_status)
def onTimer(self, evt):
self.blink_phase = not self.blink_phase
if self.blink_phase:
self.showStatus(self.mw.buddy_list.own_status)
else:
self.showEvent()
# stop blinking, if there are no more hidden windows
found = False
for window in self.mw.chat_windows:
if not window.IsShown():
found = True
break
if not found:
self.blink(False)
class TaskbarMenu(wx.Menu):
def __init__(self, main_window):
wx.Menu.__init__(self)
self.mw = main_window
self.mw.taskbar_icon.blink(False)
# show/hide
item = wx.MenuItem(self, wx.NewId(), lang.MTB_SHOW_HIDE_TORCHAT)
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onShowHide, item)
self.AppendSeparator()
# (hidden) chat windows
cnt = 0
self.wnd = {}
for window in self.mw.chat_windows:
if not window.IsShown():
id = wx.NewId()
self.wnd[id] = window
item = wx.MenuItem(self, id, window.getTitleShort())
item.SetBitmap(getStatusBitmap(window.buddy.status))
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onChatWindow, item)
cnt += 1
if cnt:
self.AppendSeparator()
# status
item = wx.MenuItem(self, wx.NewId(), lang.ST_AVAILABLE)
item.SetBitmap(getStatusBitmap(tc_client.STATUS_ONLINE))
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onAvailable, item)
item = wx.MenuItem(self, wx.NewId(), lang.ST_AWAY)
item.SetBitmap(getStatusBitmap(tc_client.STATUS_AWAY))
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onAway, item)
item = wx.MenuItem(self, wx.NewId(), lang.ST_EXTENDED_AWAY)
item.SetBitmap(getStatusBitmap(tc_client.STATUS_XA))
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onXA, item)
self.AppendSeparator()
# quit
item = wx.MenuItem(self, wx.NewId(), lang.MTB_QUIT)
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onExit, item)
def onShowHide(self, evt):
self.mw.Show(not self.mw.IsShown())
def onChatWindow(self, evt):
self.wnd[evt.GetId()].Show()
# force update of the tooltip text (window list)
self.mw.taskbar_icon.showStatus(self.mw.buddy_list.own_status)
def onExit(self, evt):
self.mw.exitProgram()
def onAvailable(self, evt):
self.mw.status_switch.setStatus(tc_client.STATUS_ONLINE)
def onAway(self, evt):
self.mw.status_switch.setStatus(tc_client.STATUS_AWAY)
def onXA(self, evt):
self.mw.status_switch.setStatus(tc_client.STATUS_XA)
class NotificationWindowAnimation(threading.Thread):
def __init__(self, win):
threading.Thread.__init__(self)
self.win = win
self.start()
def run(self):
(cx, cy, maxx, maxy) = wx.ClientDisplayRect()
(w, h) = self.win.GetSize()
self.x_end = maxx - w - 20
self.y_end = maxy - h - 20
self.win.SetPosition((-w, self.y_end))
wx.CallAfter(self.win.Show)
for x in range(-w, self.x_end, 20):
wx.CallAfter(self.win.SetPosition, (x, self.y_end))
time.sleep(0.01)
wx.CallAfter(self.win.SetPosition, (self.x_end, self.y_end))
time.sleep(5)
(w, h) = self.win.GetSize()
for y in reversed(range(-h, self.y_end, 20)):
wx.CallAfter(self.win.SetPosition, (self.x_end, y))
time.sleep(0.01)
wx.CallAfter(self.win.Hide)
wx.CallAfter(self.win.Close)
class NotificationWindow(wx.PopupWindow):
def __init__(self, mw, text):
wx.PopupWindow.__init__(self, mw)
self.panel = wx.Panel(self)
sizer = wx.BoxSizer()
self.panel.SetSizer(sizer)
bitmap = wx.Bitmap(config.get('internal', 'icon_dir')
+ '/torchat.png', wx.BITMAP_TYPE_PNG)
static_image = wx.StaticBitmap(self.panel, -1, bitmap)
sizer.Add(static_image, 0, wx.ALL, 5)
self.label = wx.StaticText(self.panel)
font = self.label.GetFont()
font.SetPointSize(12)
self.label.SetFont(font)
self.label.SetLabel(text)
sizer.Add(self.label, 0, wx.ALL, 5)
wsizer = wx.BoxSizer()
wsizer.Add(self.panel, 0, wx.ALL, 0)
self.SetSizerAndFit(wsizer)
self.Layout()
self.a = NotificationWindowAnimation(self)
class PopupMenu(wx.Menu):
def __init__(self, main_window, type):
wx.Menu.__init__(self)
self.mw = main_window
# options for buddy
if type == 'contact':
item = wx.MenuItem(self, wx.NewId(), lang.MPOP_CHAT)
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.mw.gui_bl.onDClick, item)
item = wx.MenuItem(self, wx.NewId(), lang.MPOP_SEND_FILE)
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onSendFile, item)
item = wx.MenuItem(self, wx.NewId(),
lang.MPOP_SHOW_OFFLINE_MESSAGES)
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onShowOffline, item)
item = wx.MenuItem(self, wx.NewId(),
lang.MPOP_CLEAR_OFFLINE_MESSAGES)
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onClearOffline, item)
self.AppendSeparator()
item = wx.MenuItem(self, wx.NewId(), lang.MPOP_EDIT_CONTACT)
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onEdit, item)
item = wx.MenuItem(self, wx.NewId(),
lang.MPOP_DELETE_CONTACT)
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onDelete, item)
self.AppendSeparator()
item = wx.MenuItem(self, wx.NewId(), lang.MPOP_ADD_CONTACT)
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onAdd, item)
# ask bernd
item = wx.MenuItem(self, wx.NewId(), lang.MPOP_ASK_AUTHOR
% config.get('internal', 'authors_name'))
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onAskAuthor, item)
self.AppendSeparator()
# settings
item = wx.MenuItem(self, wx.NewId(), lang.MPOP_SETTINGS)
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onSettings, item)
# about
item = wx.MenuItem(self, wx.NewId(), lang.MPOP_ABOUT)
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onAbout, item)
# exit program
self.AppendSeparator()
item = wx.MenuItem(self, wx.NewId(), lang.MTB_QUIT)
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.onQuit, item)
def onSendFile(self, evt):
buddy = self.mw.gui_bl.getSelectedBuddy()
title = lang.DFT_FILE_OPEN_TITLE \
% buddy.getAddressAndDisplayName()
dialog = wx.FileDialog(self.mw, title, style=wx.OPEN
| wx.FD_PREVIEW)
if dialog.ShowModal() == wx.ID_OK:
file_name = dialog.GetPath()
transfer_window = FileTransferWindow(self.mw, buddy,
file_name)
def onEdit(self, evt):
buddy = self.mw.gui_bl.getSelectedBuddy()
dialog = DlgEditContact(self.mw, self.mw, buddy)
dialog.ShowModal()
def onDelete(self, evt):
buddy = self.mw.gui_bl.getSelectedBuddy()
answer = wx.MessageBox(lang.D_CONFIRM_DELETE_MESSAGE
% (buddy.address, buddy.name),
lang.D_CONFIRM_DELETE_TITLE, wx.YES_NO
| wx.NO_DEFAULT)
if answer == wx.YES:
# remove from list without disconnecting
# this will send a remove_me message
# the other buddy will then disconnect,
# because there is not much it can do with the
# connections anymore.
self.mw.buddy_list.removeBuddy(buddy, disconnect=False)
def onShowOffline(self, event):
buddy = self.mw.gui_bl.getSelectedBuddy()
om = buddy.getOfflineMessages()
if om:
om = lang.MSG_OFFLINE_QUEUED % (buddy.address, om)
else:
om = lang.MSG_OFFLINE_EMPTY % buddy.address
wx.MessageBox(om, lang.MSG_OFFLINE_TITLE, wx.ICON_INFORMATION)
def onClearOffline(self, evt):
buddy = self.mw.gui_bl.getSelectedBuddy()
try:
os.unlink(buddy.getOfflineFileName())
except:
pass
def onAdd(self, evt):
dialog = DlgEditContact(self.mw, self.mw)
dialog.ShowModal()
def onSettings(self, evt):
dialog = dlg_settings.Dialog(self.mw)
dialog.ShowModal()
def onAbout(self, evt):
wx.MessageBox(lang.ABOUT_TEXT % {
'version': version.VERSION,
'svn': version.VERSION_SVN,
'copyright': config.get('internal', 'copyright'),
'python': '.'.join(str(x) for x in sys.version_info),
'wx': wx.version(),
'translators': config.getTranslators(),
}, lang.ABOUT_TITLE)
def onAskAuthor(self, evt):
if self.mw.buddy_list.getBuddyFromAddress(config.get('internal'
, 'authors_id')):
wx.MessageBox(lang.DEC_MSG_ALREADY_ON_LIST
% config.get('internal', 'authors_name'))
else:
dialog = DlgEditContact(self.mw, self.mw, add_author=True)
dialog.ShowModal()
def onQuit(self, evt):
self.mw.exitProgram()
class DlgEditContact(wx.Dialog):
def __init__( # no buddy -> Add new
self,
parent,
main_window,
buddy=None,
add_author=False,
):
wx.Dialog.__init__(self, parent, -1)
self.mw = main_window
self.bl = self.mw.buddy_list
self.buddy = buddy
if buddy == None:
self.SetTitle(lang.DEC_TITLE_ADD)
address = ''
name = ''
else:
self.SetTitle(lang.DEC_TITLE_EDIT)
address = buddy.address
name = buddy.name
self.panel = wx.Panel(self)
# setup the sizers
sizer = wx.GridBagSizer(vgap=5, hgap=5)
box_sizer = wx.BoxSizer()
box_sizer.Add(sizer, 1, wx.EXPAND | wx.ALL, 5)
# address
row = 0
lbl = wx.StaticText(self.panel, -1, lang.DEC_TORCHAT_ID)
sizer.Add(lbl, (row, 0))
self.txt_address = wx.TextCtrl(self.panel, -1, address)
self.txt_address.SetMinSize((250, -1))
sizer.Add(self.txt_address, (row, 1), (1, 2))
# name
row += 1
lbl = wx.StaticText(self.panel, -1, lang.DEC_DISPLAY_NAME)
sizer.Add(lbl, (row, 0))
self.txt_name = wx.TextCtrl(self.panel, -1, name)
self.txt_name.SetMinSize((250, -1))
sizer.Add(self.txt_name, (row, 1), (1, 2))
# add-me message (new buddies)
if not self.buddy:
row += 1
lbl = wx.StaticText(self.panel, -1, lang.DEC_INTRODUCTION)
sizer.Add(lbl, (row, 0))
self.txt_intro = wx.TextCtrl(self.panel, -1,
'hello, my friend...')
self.txt_intro.SetMinSize((250, -1))
sizer.Add(self.txt_intro, (row, 1), (1, 2))
if add_author:
self.txt_address.SetValue(config.get('internal',
'authors_id'))
self.txt_name.SetValue(config.get('internal', 'authors_name'
))
# buttons
row += 1
self.btn_cancel = wx.Button(self.panel, -1, lang.BTN_CANCEL)
sizer.Add(self.btn_cancel, (row, 1), flag=wx.EXPAND)
self.btn_ok = wx.Button(self.panel, -1, lang.BTN_OK)
self.btn_ok.SetDefault()
sizer.Add(self.btn_ok, (row, 2), flag=wx.EXPAND)
# fit the sizers
self.panel.SetSizer(box_sizer)
box_sizer.Fit(self)
# bind the events
self.btn_cancel.Bind(wx.EVT_BUTTON, self.onCancel)
self.btn_ok.Bind(wx.EVT_BUTTON, self.onOk)
def onOk(self, evt):
address = self.txt_address.GetValue().rstrip().lstrip()
if len(address) != 16:
l = len(address)
wx.MessageBox(lang.DEC_MSG_16_CHARACTERS % l)
return
for c in address:
if c not in '0123456789abcdefghijklmnopqrstuvwxyz':
wx.MessageBox(lang.DEC_MSG_ONLY_ALPANUM)
return
if self.buddy == None:
buddy = tc_client.Buddy(address, self.bl,
self.txt_name.GetValue())
res = self.bl.addBuddy(buddy)
if res == False:
wx.MessageBox(lang.DEC_MSG_ALREADY_ON_LIST % address)
else:
buddy.storeOfflineChatMessage(self.txt_intro.GetValue())
else:
address_old = self.buddy.address
offline_file_name_old = self.buddy.getOfflineFileName()
self.buddy.address = address
offline_file_name_new = self.buddy.getOfflineFileName()
self.buddy.name = self.txt_name.GetValue()
self.bl.save()
if address != address_old:
self.buddy.disconnect()
try:
os.rename(offline_file_name_old,
offline_file_name_new)
except:
pass
self.Close()
def onCancel(self, evt):
self.Close()
class BuddyList(wx.ListCtrl):
def __init__(self, parent, main_window):
wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT
| wx.LC_NO_HEADER)
self.mw = main_window
self.bl = self.mw.buddy_list
self.InsertColumn(0, 'buddy')
self.r_down = False
self.il = wx.ImageList(16, 16)
self.il_idx = {}
for status in [tc_client.STATUS_OFFLINE,
tc_client.STATUS_HANDSHAKE,
tc_client.STATUS_ONLINE, tc_client.STATUS_AWAY,
tc_client.STATUS_XA]:
self.il_idx[status] = self.il.Add(getStatusBitmap(status))
img_event = wx.Image(os.path.join(config.get('internal',
'icon_dir'), 'event.png'))
img_event.ConvertAlphaToMask()
self.il_idx[100] = self.il.Add(img_event.ConvertToBitmap())
self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
self.blink_phase = False
self.timer = wx.Timer(self, -1)
self.Bind(wx.EVT_TIMER, self.onTimer, self.timer)
self.old_sum = ''
self.timer.Start(milliseconds=500, oneShot=False)
self.Bind(wx.EVT_LEFT_DCLICK, self.onDClick)
self.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.onRClick)
self.Bind(wx.EVT_RIGHT_DOWN, self.onRDown)
def blinkBuddy(self, buddy, blink=True):
name = buddy.getDisplayName()
for index in xrange(0, self.GetItemCount()):
if name == self.GetItemText(index):
if blink:
if self.blink_phase:
self.SetItemImage(index, self.il_idx[100])
else:
self.SetItemImage(index,
self.il_idx[buddy.status])
else:
self.SetItemImage(index, self.il_idx[buddy.status])
def onTimer(self, evt):
# first check if there have been any changes to the buddy list
sum = ''
for buddy in self.bl.list:
sum += buddy.address + buddy.name + str(buddy.status)
if sum != self.old_sum:
# FIXME: This whole method is more than ugly
# remove items which are not in list anymore
for index in xrange(0, self.GetItemCount()):
found = False
for buddy in self.bl.list:
if buddy.getDisplayName() \
== self.GetItemText(index):
found = True
break
if not found:
self.DeleteItem(index)
break
# add new items to the list or change status icons
for buddy in self.bl.list:
line = buddy.getDisplayName()
index = self.FindItem(0, line)
if index == -1:
index = self.InsertImageStringItem(sys.maxint,
line, self.il_idx[tc_client.STATUS_OFFLINE])
self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
self.SetItemImage(index, self.il_idx[buddy.status])
self.old_sum = sum
# always: show unread messages
self.blink_phase = not self.blink_phase
for buddy in self.bl.list:
found = False
for window in self.mw.chat_windows:
if not window.IsShown() and window.buddy == buddy:
found = True
break
if found:
self.blinkBuddy(buddy, True)
else:
self.blinkBuddy(buddy, False)
def onDClick(self, evt):
i = self.GetFirstSelected()
address = self.GetItemText(i)[0:16]
for buddy in self.bl.list:
if buddy.address == address:
found_window = False
for window in self.mw.chat_windows:
if window.buddy == buddy:
found_window = True
break
if not found_window:
window = ChatWindow(self.mw, buddy)
if not window.IsShown():
window.Show()
window.txt_out.SetFocus()
break
evt.Skip()
def onRClick(self, evt):
(index, flags) = self.HitTest(evt.GetPosition())
if index != -1:
self.mw.PopupMenu(PopupMenu(self.mw, 'contact'))
def onRDown(self, evt):
(index, flags) = self.HitTest(evt.GetPosition())
if index == -1:
self.mw.PopupMenu(PopupMenu(self.mw, 'empty'))
else:
evt.Skip()
def getSelectedBuddy(self):
index = self.GetFirstSelected()
addr = self.GetItemText(index)[0:16]
return self.bl.getBuddyFromAddress(addr)
class StatusSwitchList(wx.Menu):
def __init__(self, status_switch):
wx.Menu.__init__(self)
self.status_switch = status_switch
item = wx.MenuItem(self, wx.NewId(), lang.ST_AVAILABLE)
item.SetBitmap(getStatusBitmap(tc_client.STATUS_ONLINE))
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.status_switch.onAvailable, item)
item = wx.MenuItem(self, wx.NewId(), lang.ST_AWAY)
item.SetBitmap(getStatusBitmap(tc_client.STATUS_AWAY))
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.status_switch.onAway, item)
item = wx.MenuItem(self, wx.NewId(), lang.ST_EXTENDED_AWAY)
item.SetBitmap(getStatusBitmap(tc_client.STATUS_XA))
self.AppendItem(item)
self.Bind(wx.EVT_MENU, self.status_switch.onXA, item)
class StatusSwitch(wx.Button):
def __init__(self, parent, main_window):
wx.Button.__init__(self, parent)
self.parent = parent
self.main_window = main_window
self.status = self.main_window.buddy_list.own_status
self.Bind(wx.EVT_BUTTON, self.onClick)
self.setStatus(self.main_window.buddy_list.own_status)
def onClick(self, evt):
self.PopupMenu(StatusSwitchList(self))
def onAvailable(self, evt):
self.setStatus(tc_client.STATUS_ONLINE)
def onAway(self, evt):
self.setStatus(tc_client.STATUS_AWAY)
def onXA(self, evt):
self.setStatus(tc_client.STATUS_XA)
def setStatus(self, status):
self.status = status
self.main_window.setStatus(status)
if status == tc_client.STATUS_AWAY:
status_text = lang.ST_AWAY
if status == tc_client.STATUS_XA:
status_text = lang.ST_EXTENDED_AWAY
if status == tc_client.STATUS_ONLINE:
status_text = lang.ST_AVAILABLE
if status == tc_client.STATUS_OFFLINE:
status_text = lang.ST_OFFLINE
self.SetLabel(status_text)
class ChatWindow(wx.Frame):
def __init__(
self,
main_window,
buddy,
message='',
hidden=False,
notify_offline_sent=False,
):
wx.Frame.__init__(self, main_window, -1, size=(400, 400))
self.mw = main_window
self.buddy = buddy
self.unread = 0
self.updateTitle()
self.panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
self.txt_in = wx.TextCtrl(self.panel, -1, style=wx.TE_READONLY
| wx.TE_MULTILINE | wx.TE_AUTO_URL
| wx.TE_AUTO_SCROLL | wx.TE_RICH2
| wx.BORDER_SUNKEN)
sizer.Add(self.txt_in, 1, wx.EXPAND | wx.ALL, 0)
self.txt_out = wx.TextCtrl(self.panel, -1,
style=wx.TE_MULTILINE | wx.TE_RICH2
| wx.BORDER_SUNKEN)
sizer.Add(self.txt_out, 0, wx.EXPAND | wx.ALL, 0)
sizer.SetItemMinSize(self.txt_out, (-1, 50))
self.panel.SetSizer(sizer)
sizer.FitInside(self)
om = self.buddy.getOfflineMessages()
if om:
om = '[%s]\n' % lang.NOTICE_DELAYED_MSG_WAITING + om
self.writeColored((0, 0, 192), 'myself', om)
if message != '':
self.process(message)
if notify_offline_sent:
self.notifyOfflineSent()
self.timer = wx.Timer(self, -1)
self.timer.Start(1000, False)
self.onTimer(None)
self.Bind(wx.EVT_TIMER, self.onTimer)
self.Bind(wx.EVT_CLOSE, self.onClose)
self.txt_out.Bind(wx.EVT_KEY_DOWN, self.onKey)
self.txt_in.Bind(wx.EVT_TEXT_URL, self.onURL)
self.Bind(wx.EVT_ACTIVATE, self.onActivate)
self.txt_in.Bind(wx.EVT_CONTEXT_MENU, self.OnContextMenu)
self.drop_target_in = FileDropTarget(self)
self.drop_target_out = FileDropTarget(self)
self.txt_in.SetDropTarget(self.drop_target_in)
self.txt_out.SetDropTarget(self.drop_target_out)
if not hidden:
self.Show()
self.mw.chat_windows.append(self)
def updateTitle(self):
if self.unread == 1:
title = '* '
elif self.unread > 1:
title = '*[%i] ' % self.unread
else:
title = ''
title += self.buddy.address
if self.buddy.name != '':
title += ' (%s)' % self.buddy.name
self.SetTitle(title + ' %s' % config.getProfileLongName())
def getTitleShort(self):
t = self.GetTitle()
return t[:-19]
def writeColored(
self,
color,
name,
text,
):
self.txt_in.SetDefaultStyle(wx.TextAttr(wx.Color(128, 128,
128)))
self.txt_in.write('%s ' % time.strftime(config.get('gui',
'time_stamp_format')))
(red, green, blue) = color
self.txt_in.SetDefaultStyle(wx.TextAttr(wx.Color(red, green,
blue)))
self.txt_in.write('%s: ' % name)
self.txt_in.SetDefaultStyle(wx.TextAttr(wx.Color(0, 0, 0)))
self.txt_in.write('%s\n' % text)
# workaround scroll bug on windows
# https://sourceforge.net/tracker/?func=detail&atid=109863&aid=665381&group_id=9863
self.txt_in.ScrollLines(-1)
self.txt_in.ShowPosition(self.txt_in.GetLastPosition())
def notify(self, name, message):
# needs unicode
if not self.IsActive():
if config.getint('gui', 'notification_flash_window'):
self.RequestUserAttention(wx.USER_ATTENTION_INFO)
self.unread += 1
self.updateTitle()
if config.getint('gui', 'notification_popup'):
nt = textwrap.fill('%s:\n%s' % (name, message), 40)
try:
NotificationWindow(self.mw, nt)
except:
# Some platforms (Mac) dont have wx.PopupWindow
# FIXME: need alternative solution
pass
if not self.IsShown():
self.mw.taskbar_icon.blink()
def notifyOfflineSent(self):
# all should be unicode here
message = '[%s]' % lang.NOTICE_DELAYED_MSG_SENT
self.writeColored((0, 0, 192), 'myself', message)
self.notify('to %s' % self.buddy.address, message)
def process(self, message):
# message must be unicode
if self.buddy.name != '':
name = self.buddy.name
else:
name = self.buddy.address
self.writeColored((192, 0, 0), name, message)
self.notify(name, message)
def onActivate(self, evt):
self.unread = 0
self.updateTitle()
evt.Skip()
def onClose(self, evt):
self.mw.chat_windows.remove(self)
self.Destroy()
def onKey(self, evt):
if evt.GetKeyCode() == 13 and not evt.ShiftDown():
self.onSend(evt)
else:
evt.Skip()
def onSend(self, evt):
evt.Skip()
text = self.txt_out.GetValue().rstrip().lstrip()
wx.CallAfter(self.txt_out.SetValue, '')
if self.buddy.status not in [tc_client.STATUS_OFFLINE,
tc_client.STATUS_HANDSHAKE]:
self.buddy.sendChatMessage(text)
self.writeColored((0, 0, 192), 'myself', text)
else:
self.buddy.storeOfflineChatMessage(text)
self.writeColored((0, 0, 192), 'myself', '[%s] %s'
% (lang.NOTICE_DELAYED, text))
def onTimer(self, evt):
bmp = getStatusBitmap(self.buddy.status)
icon = wx.IconFromBitmap(bmp)
self.SetIcon(icon)
def onURL(self, evt):
# all URL mouse events trigger this
if evt.GetMouseEvent().GetEventType() == wx.wxEVT_LEFT_DOWN:
# left button down, now we need the URL
start = evt.GetURLStart()
end = evt.GetURLEnd()
url = self.txt_in.GetRange(start, end)
if config.isWindows():
# this works very reliable
subprocess.Popen(('cmd /c start %s' % url).split(),
creationflags=0x08000000)
else:
# FIXME: is this the way to go? better make it a config option.
subprocess.Popen(('/etc/alternatives/x-www-browser %s'
% url).split())
else:
evt.Skip()
def OnContextMenu(self, evt):
menu = wx.Menu()
id = wx.NewId()
item = wx.MenuItem(menu, id, lang.CPOP_COPY)
self.Bind(wx.EVT_MENU, self.onCopy, id=id)
menu.AppendItem(item)
(sel_from, sel_to) = self.txt_in.GetSelection()
empty = sel_from == sel_to
if empty:
item.Enable(False)
id = wx.NewId()
item = wx.MenuItem(menu, id, lang.MPOP_SEND_FILE)
self.Bind(wx.EVT_MENU, self.onSendFile, id=id)
menu.AppendItem(item)
id = wx.NewId()
item = wx.MenuItem(menu, id, lang.MPOP_EDIT_CONTACT)
self.Bind(wx.EVT_MENU, self.onEditBuddy, id=id)
menu.AppendItem(item)
self.PopupMenu(menu)
menu.Destroy()
def onCopy(self, evt):
(sel_from, sel_to) = self.txt_in.GetSelection()
if sel_from == sel_to:
return
text = self.txt_in.GetRange(sel_from, sel_to)
clipdata = wx.TextDataObject()
clipdata.SetText(text)
wx.TheClipboard.Open()
wx.TheClipboard.SetData(clipdata)
wx.TheClipboard.Close()
def onSendFile(self, evt):
title = lang.DFT_FILE_OPEN_TITLE \
% self.buddy.getAddressAndDisplayName()
dialog = wx.FileDialog(self, title, style=wx.OPEN
| wx.FD_PREVIEW)
if dialog.ShowModal() == wx.ID_OK:
file_name = dialog.GetPath()
transfer_window = FileTransferWindow(self.mw, self.buddy,
file_name)
def onEditBuddy(self, evt):
dialog = DlgEditContact(self, self.mw, self.buddy)
dialog.ShowModal()