File: Conversation.py

package info (click to toggle)
emesene 1.0-dist-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 4,596 kB
  • ctags: 3,006
  • sloc: python: 25,171; makefile: 14; sh: 1
file content (688 lines) | stat: -rw-r--r-- 24,679 bytes parent folder | download
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
# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene 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; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import gtk
import pango
import gobject
import time

from urllib import quote
from warnings import warn

import emesenelib
from emesenecommon import MAX_MESSAGE_LENGTH
import dialog
import abstract.stock as stock

import ConversationUI
import FileTransfer

class CustomEmoticons(object): # FIXME: wtf?
    def __init__(self):
        self.emoticons = {}
        
    def setNew(self, user, shortcut, id):
        if user != None: user = user.lower()
        if self.emoticons.has_key(user):
            self.emoticons[user].update({shortcut:id})
        else:
            self.emoticons.update({user:{shortcut:id}})
        
    def get(self, user=None):
        if user != None: user = user.lower()
        if self.emoticons.has_key(user):
            return self.emoticons[ user ]
        else:
            return []

class Conversation(gobject.GObject):
    '''This class is an abstraction of a conversation, it is used to separate
    the data from the GUI in a conversation to let us have single and tabbed
    windows with the same codebase (also MVC is good :P)'''

    def __init__(self, controller, switchboard):
        '''Constructor'''
        gobject.GObject.__init__(self)

        self.callbackIdList = []
        self.P2PSignals = {}
        
        self.ui = None
        self.switchboard = None
        self.setSwitchboard(switchboard)
        msn = switchboard.msn
        self.controller = controller
        self.parser = controller.unifiedParser
        self.config = controller.config
        self.title = ""
        
        self.lastMessageMail = '' # the mail of the user who sent the last message
        self.isCurrent = False # if True this is the tab that has the focus
        self.closed = False
        self.textBuffer = gtk.TextBuffer()
        self.autoreplySent = False # if true we allready sent the autoreply message
        self.inputText = ''
        self.theme = controller.theme
        self.parentConversationWindow = None
        
        msn.connect('user-attr-changed', self.onUserAttrChanged)
        msn.connect('custom-emoticon-transfered', self.onCustomEmoticonTransfered)
        
        #TODO: from now on this signals will be emitted by conversation window or conversationUI
        controller.connect('color-changed', self.onColorChanged)
        controller.connect('font-changed', self.onFontChanged)
        
        self.ui = ConversationUI.ConversationUI(self.controller, self)
        self.customEmoticons = CustomEmoticons()
        self.parser = self.controller.unifiedParser
       
        self.sendOffline = False
        self.lastSpeaker = ''
        self.user = switchboard.user
        
        self.transfers = []
    
    def onFtInvite(self, p2p, session, context, sender):
        ft = FileTransfer.FileTransfer(self.controller, p2p, self, session,
            context, sender)
        self.transfers.append(ft)
        self.ui.transfers.add(ft)
        self.ui.transfers.show_all()
    
    def sendFile(self, path):
        '''This sends a file'''
        # TODO: handle multichat
        p2p = self.controller.msn.p2p[self.switchboard.firstUser]
        sender = emesenelib.msn_p2p.FTSender(p2p, path)
        self.onFtInvite(p2p, sender.session_id, sender.context, 'Me')
    
    def getSwitchboard(self):
        return self.switchboard
        
    def close(self):
        '''close the tab'''
        self.parentConversationWindow.closeTab(self)
        self.setClosed(True)
        
    def onColorChanged(self, controller, colorStr):
        self.config.user['fontColor'] = colorStr
        self.ui.input.applyAttrsToInput()
    
    def onFontChanged(self, controller, font, bold, italic, size):
        self.setFont(font, italic, bold, size)
        
        self.ui.input.toolbar.setFontBold(bold)
        self.ui.input.toolbar.setFontItalic(italic)
        
        self.ui.input.applyAttrsToInput()
        
    def onUserAttrChanged(self, msnp, contact):
        if contact.email in self.getMembers() and \
           self.isCurrent and self.parentConversationWindow:
            
            win = self.parentConversationWindow
            win.update_title()
            
            if not self.config.user['avatarsInTaskbar']:
                win.set_icon(self.getWindowIcon())

    def getStatusIcon(self):
        '''returns the status icon for this conversation'''
        members = self.getMembers()
        theme = self.controller.theme
        user = None
        
        if len(members) > 1: 
            return theme.getImage('groupChat')
        if len(members) == 0:
            return theme.getImage('userPanel')
        
        user = self.controller.getContact(members[0])
        if not user:
            return theme.getImage('userPanel')
        return theme.statusToPixbuf(user.status)

    def getWindowIcon(self):
        '''returns the window icon for this conversation'''
        members = self.getMembers()
        user = None
        
        if len(members) == 1:
            user = self.controller.getContact(members[0])
            
        if user and self.config.user['avatarsInTaskbar'] and \
                self.controller.theme.hasUserDisplayPicture(user):
            return self.controller.theme.getUserDisplayPicture(user, 64, 64)
        return self.getStatusIcon()

    def getFontColor(self):
        '''return the user color'''
        return self.config.user['fontColor']

    def setFont(self, font, italic=False, bold=False, size=10):
        '''set the font of the user text'''
        self.config.user['fontFace'] = font
        self.config.user['fontItalic'] = italic
        self.config.user['fontBold'] = bold
        self.config.user['fontSize'] = size
    
    def getTitle(self):
        '''return a title according to the users in the conversation'''
        if not self.controller:
            return self.title

        members = self.getMembers()
        if len(members) > 1:
            self.title = _('Group chat')
        elif len(members) == 1:
            if self.config.user['useAliasIfAvailable']:
                title = self.controller.contacts.get_alias(members[0])
            if not title:
                title = self.controller.contacts.get_nick(members[0])

            if title:
                self.title = title

        return self.title

    def getUser(self):
        '''return the (local) user mail'''
        return self.switchboard.user
        
    def getRTL(self, message):
        '''check whether it's an right-to-left string'''
        try:
            if pango.find_base_dir(message, -1) == pango.DIRECTION_RTL:
                return '1'
        finally:
            return '0'

    def getStyle(self, message=''):
        '''return the style string to use in the sendMessage method'''

        effectValue = ''

        if self.config.user['fontBold']:
            effectValue += 'B'

        if self.config.user['fontItalic']:
            effectValue += 'I'

        if self.config.user['fontUnderline']:
            effectValue += 'U'
        
        if self.config.user['fontStrike']:
            effectValue += 'S'

        color = self.config.user['fontColor'].replace('#', '')
        color = color[ 4:6 ] + color[ 2:4 ] + color[ :2 ]
        
        face = self.config.user['fontFace'].replace(' ', '%20')

        return "X-MMS-IM-Format: FN=" + face + \
            "; EF=" + effectValue + "; CO=" + color + \
            "; PF=0;RL=" + self.getRTL(message)

    def getId(self):
        '''return the id of the switchboard'''
        return self.switchboard.getId()

    def getOnlineUsers(self):
        '''This method returns a list ol mails of the contacts who are not offline'''
        return self.switchboard.getOnlineUsers()

    def invite(self, mail):
        '''invite a user to the conversation'''
        self.switchboard.invite(mail)

    def getMembers(self):
        '''return a list of the members in the conversation'''
        members = self.switchboard.getMembers()
        if len(members) != 0: return members

        members = self.switchboard.getInvitedMembers()
        if len(members) != 0: return members
        
        return [self.switchboard.firstUser]

    def getWindow(self):
        '''return the window that hold this conversation'''
        return self.parentConversationWindow

    def setWindow(self, window):
        '''set the window that hold this conversation'''

        self.parentConversationWindow = window

    def setIsCurrent(self, current):
        '''set the isCurrent attribute, if true, this conversation
        is the tab that is shown'''
        self.isCurrent = current

    def getIsCurrent(self):
        '''return the value of isCurrent'''
        return self.isCurrent

    def receiveNudge(self, switchboard, mail):
        '''This method is called when a nudge is received in the
        switchboard'''
        nick = self.controller.msn.getUserDisplayName(mail)
        self.appendOutputText(None, _("%s just sent you a nudge!")% \
          self.parser.getParser(nick).get(False), 'information')
        self.doMessageWaiting(mail)

    def receiveOIM(self, nick, message, date):
        '''This method is called when a offline message is received'''
        self.appendOutputText(nick, message, 'offline_incoming', \
            timestamp=time.mktime(date))
        self.doMessageWaiting('')
        
    def receiveError(self, msnp, to, message, error):
        '''This method is called when a error message is received'''
        self.appendOutputText('Error', "Can\'t send message (%s)\n%s" % \
            (error, message), 'error')
        self.doMessageWaiting('')

    def onReceiveMessage(self, switchboard, mail, nick, message,
                         format, charset):
        '''This method is called when a message is received in the switchboard'''
        self.controller.conversationManager.emit('receive-message', self, \
            mail, nick, message, format, charset)

    def onInkMessage(self, switchboard, mail, filename):
        '''This method is called when an ink message is received
        in the switchboard'''

        self.doMessageWaiting(mail)
        self.appendOutputText(mail, quote(filename), 'ink_incoming')

    def doMessageWaiting(self, mail):
        win = self.parentConversationWindow
        if win and (not win.has_toplevel_focus() or not self.isCurrent):
            self.ui.setMessageWaiting(mail)
            self.parentConversationWindow.setUrgency()
            self.parentConversationWindow.show()
        else:
            self.ui.setDefault(mail)
        
    def do_receive_message(self, mail, nick, message, format, charset):
        '''This method is called when a message is received
        in the switchboard'''

        self.doMessageWaiting(mail)
        
        if self.config.user['autoReply'] and not self.autoreplySent:
            msg = self.config.user['autoReplyMessage']

            # no gettext here, it's a semi standard way
            # to identify automessages
            self.switchboard.sendMessage('AutoMessage: ' + msg)
            self.appendOutputText(None, 'AutoMessage: %s\n' % msg, \
                'information')
            self.autoreplySent = True
        
        if message is not None:
            self.appendOutputText(mail, message, 'incoming', \
                self.parseFormat(mail, format))

    def userJoin(self, switchboard, mail):
        '''This method is called when someone joins the conversation'''
        if switchboard.isGroupChat():
            nick = self.controller.msn.getUserDisplayName(mail)
            nick = self.parser.getParser(nick).get(False)
            self.appendOutputText('', \
                _("%s has joined the conversation") % nick, 'information')
       
        if self.isCurrent:
            self.parentConversationWindow.update_title()
            self.parentConversationWindow.set_icon(self.getWindowIcon())
        
        if self.ui:
            self.ui.update()

    def userLeave(self, switchboard, mail):
        '''method called when someone leaves the conversation'''
        nick = self.controller.msn.getUserDisplayName(mail)
        nick = self.parser.getParser(nick).get(False)
        self.appendOutputText('', _("%s has left the conversation")%nick, \
            'information')
        
        if self.isCurrent:
            self.parentConversationWindow.update_title()
            self.parentConversationWindow.set_icon(self.getWindowIcon())
        
        if self.ui:
            self.ui.update()
        
    def sbStatusChange(self, switchboard):
        if self.ui:
            self.ui.update()

    def inviteUser(self, mail):
        '''method called when the user selects a friend in the invite dialog'''
        if self.switchboard.status == 'closed':
            self.reconnect()

        self.invite(mail) 
        self.ui.messageWaiting[mail] = False
        self.ui.contactTyping[mail] = False
        self.ui.update()

    def doNudge(self):
        '''this method is called when the user clicks the nudge button'''
        if self.switchboard.status == 'closed':
            self.reconnect()

        try:
            self.switchboard.sendNudge()
        except Exception:
            self.reconnect()
            self.switchboard.sendNudge()

        self.appendOutputText(None, _("you have sent a nudge!"), 'information')

    def reconnect(self):
        '''reconnect the switchboard'''
        if not self.controller or not self.controller.msn:
            return

        user = self.getMembers()[0]
        self.setSwitchboard(self.controller.msn.getSwitchboard(user))
        self.autoreplySent = False

    def splitMessage(self, message):
        '''Split large messages'''
        messageChunks = []
        messageLen = len(message)
        msgStart = 0
        while msgStart < messageLen:
            chunk = message[msgStart:msgStart+MAX_MESSAGE_LENGTH]
            chunkLen = len(chunk)
            if chunkLen == MAX_MESSAGE_LENGTH:
                chunkEnd = chunk.rfind(' ')
                if chunkEnd!=-1 and chunkEnd>0:
                    messageChunks.append(chunk[0:chunkEnd])
                    msgStart += chunkEnd+1
                else:
                    msgStart += chunkLen
                    messageChunks.append(chunk)
            else:
                msgStart += chunkLen
                messageChunks.append(chunk)
        return messageChunks

    def do_send_message(self, message, retry=0):
        '''Send the message from the UI input. This chooses between OIM and
        switchboard to send the message.'''
        
        remoteMail = self.switchboard.firstUser
        remoteStatus = self.controller.contacts.get_status(remoteMail)

        def do_send_offline(response, mail, message):
            '''callback for the confirm dialog asking to send offline 
            message'''
            if response == stock.YES:
                self.sendOffline = True
                self.switchboard.msn.msnOIM.send(mail, message)
                self.appendOutputText(self.user, message, 'outgoing')
        
        if self.switchboard.status == 'error' and remoteStatus == 'FLN':
            
            if self.sendOffline == True:
                do_send_offline(stock.YES, remoteMail, message)
            else:
                dialog.yes_no(
                 _("Are you sure you want to send a offline message to %s") %\
                 remoteMail, do_send_offline, remoteMail, message)

            return
            
        if self.switchboard.status == 'closed':
            self.reconnect()

        messageChunks = self.splitMessage(message)
        for chunk in messageChunks:
            try:
                self.switchboard.sendCustomEmoticons(chunk)
                self.switchboard.sendMessage(chunk, self.getStyle(chunk))
                self.appendOutputText(self.user, chunk, 'outgoing')
            except Exception, e:
                raise
                print str(e)
                self.reconnect()
                if retry < 3:
                    self.do_send_message(
                        ''.join(messageChunks[messageChunks.index(chunk):]), \
                        retry + 1)
                else:
                    self.appendOutputText(None, _('Can\'t send message'), \
                        'information')
                return

    def sendMessage(self, message):
        '''send a message to the conversation'''
        self.controller.conversationManager.emit('send-message', self, message)
            
    def sendIsTyping(self):
        '''an easy method to send the is typing message'''
        if self.switchboard.status == 'closed':
            self.reconnect()

        try:
            self.switchboard.sendIsTyping()
        except Exception:
            self.reconnect()
            self.sendIsTyping()

    def parseFormat(self, mail, format):
        '''parse the format of a mail and return the style'''

        # if the useFriendsUnifiedFormat flag is set, then return that format

        if self.config.user['useFriendsUnifiedFormat']:
            font = self.config.user['friendsUnifiedFont']
            color = self.config.user['friendsUnifiedColor']
            return 'font-family: ' + emesenelib.common.escape(font) + \
                ';color: ' + emesenelib.common.escape(color) + ';'

        # FN=Sans; EF=; CO=000000; PF=0
     
        style = ''
     
        if format.find("FN=") != -1:
            font = format.split('FN=')[1].split(';')[0].replace('%20', ' ')
            style += 'font-family: ' + emesenelib.common.escape(font) + ';'

        if format.find("CO=") != -1:
            color = format.split('CO=')[1].split(';')[0]
                 
            if len(color) == 3:
                color = color[2] + color[1] + color[0]
                style += 'color: #' + emesenelib.common.escape(color) + ';'
            else:
                color = color.zfill(6)
                
            if len(color) == 6:
                color = color[4:6] + color[2:4] + color[:2]
                style += 'color: #' + emesenelib.common.escape(color) + ';'

        if format.find("EF=") != -1:
            effect = set(format.split('EF=')[1].split(';')[0])

            if "B" in effect: style += 'font-weight: bold;'
            if "I" in effect: style += 'font-style: italic;'
            if "U" in effect: style += 'text-decoration: underline;'
            if "S" in effect: style += 'text-decoration: line-through;'
        
        return style

    def setSwitchboard(self, switchboard):
        '''set a new switchboard for the conversation.
        useful if the conversation is closed
        and the other user starts a new one'''

        signalDict = {
            'nudge': self.receiveNudge,
            'message': self.onReceiveMessage,
            'ink-message': self.onInkMessage,
            'user-join': self.userJoin,
            'user-leave': self.userLeave,
            'typing': self.receiveTyping,
            'custom-emoticon-received': self.onCustomEmoticonReceived,
        }
        
        if self.switchboard:
            # disconnect old stuff
            
            while len(self.callbackIdList) > 0:
                self.switchboard.disconnect(self.callbackIdList.pop())
            
            if self.switchboard.msn:
                for mail in self.P2PSignals.keys():
                    self.switchboard.msn.p2p[mail].disconnect(\
                        self.P2PSignals.pop(mail))

        if switchboard is None:
            # the function was called just to disconnect everything
            return

        # connect new switchboard
        self.switchboard = switchboard

        for signalName in signalDict.keys():
            self.callbackIdList.append(self.switchboard.connect(signalName, \
                signalDict[signalName]))
        
        for mail in switchboard.getMembers():
            self.P2PSignals[mail] = switchboard.msn.p2p[mail].connect(\
                'file-transfer-invite', self.onFtInvite)
        
        self.autoreplySent = False

    def onCustomEmoticonReceived(self, switchboard, shortcut, msnobj):
        '''call when a smiley is received'''
        self.customEmoticons.setNew(msnobj.creator, shortcut, msnobj.sha1d)
        
    def onCustomEmoticonTransfered(self, switchboard, to, msnobj, path):
        '''call when a smiley is transfered'''
        if self.ui:
            self.ui.textview.setCustomObject(msnobj.sha1d, path, \
                type='application/x-emesene-emoticon')

    def appendOutputText(self, username, text, type, style = None, timestamp = None):
        '''append the given text to the outputBuffer'''
        
        if type.startswith('ink_'):
            type = type[4:]
            ink = True
        else:
            ink = False
        
        if type != 'incoming' and type != 'outgoing':
            self.lastSpeaker = ''
        elif username == self.lastSpeaker:
            type = 'consecutive_' + type
        
        self.lastSpeaker = username
        
        if username == self.switchboard.user:
            nick = emesenelib.common.escape(self.controller.msn.nick)
        elif username != None:
            nick = emesenelib.common.escape( \
                self.controller.msn.getUserDisplayName(username))
        else:
            nick = ''

        if timestamp is None:
            timestamp = time.time()

        displayedText = self.controller.conversationLayoutManager.layout(\
            username, text, style, self, type, timestamp, ink)

        try:
            self.ui.textview.display_html(displayedText.encode('ascii', \
                'xmlcharrefreplace'))
        except Exception, e:
            print 'error trying to display "' + displayedText + '"' 
            print e

        self.ui.scrollToBottom()              

    def getStatus(self):
        return self.switchboard.status

    def setStatus(self, value):
        self.switchboard.setStatus(value)

    def isClosed(self):
        return self.closed

    def setClosed(self, value):
        self.closed = value
        if value == True:
            self.switchboard.leaveChat()
            self.setSwitchboard(None)
    
    def receiveTyping(self, switchboard, mail):
        '''This method is called when a is typing message is received in the
        switchboard'''
        if self.ui:
            self.ui.setTyping(mail)

    def getMembersDict(self):
        '''return a dict with email as key and contact instance as value'''
        
        userDict = {}
        for mail in self.getMembers():
            contact = self.controller.getContact(mail)
            if contact:
                userDict[mail] = contact
        
        return userDict
        
    def getTextTag(self):
        '''return a text tag from the current style'''
        
        tag = gtk.TextTag()
        
        if self.config.user['disableFormat']:
            return tag
        
        tag.set_property('font', self.config.user['fontFace'])
        tag.set_property('size-points', self.config.user['fontSize'])
        tag.set_property("foreground" , self.config.user['fontColor'])
        
        if self.config.user['fontBold']:
            tag.set_property("weight" , pango.WEIGHT_BOLD)
        
        if self.config.user['fontItalic']:
            tag.set_property("style" , pango.STYLE_ITALIC)
        
        if self.config.user['fontUnderline']:
            tag.set_property("underline-set" , True)
            tag.set_property("underline" , pango.UNDERLINE_SINGLE)
        else:
            #XXX: wtf is that underline-set=True?
            tag.set_property("underline-set" , True)
            tag.set_property("underline" , pango.UNDERLINE_NONE)
            
        tag.set_property("strikethrough" , self.config.user['fontStrike'])
               
        return tag
        
gobject.type_register(Conversation)