Exemple #1
0
class ConversationUI(gtk.VBox):
    '''this class represent all the widgets that are inside a tab
    also hold the tab widget because there is no better place than
    this to hold it...'''
    def __init__(self, controller, parentConversation):
        gtk.VBox.__init__(self, spacing=3)
        self.set_border_width(0)
        self.parentConversation = parentConversation
        self.controller = controller
        self.config = self.controller.config
        self.parser = controller.unifiedParser
        self.header = Header(self, controller)
        self.tabWidget = TabWidget(self, controller)
        self.tabWidget.show()
        self.input = InputWidget(self, controller, \
            parentConversation.isCurrent)
        self.input.show()
        self.status = gtk.Statusbar()
        self.toolbarinput = gtk.HBox()
        self.toolbarinput.show()
        self.listOfUsers = UserList.UserList(self.controller, \
            self.controller.theme, self.config, False)
        self.scrollList = gtk.ScrolledWindow()
        self.scrollList.set_shadow_type(gtk.SHADOW_IN)
        self.scrollList.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.scrollList.set_size_request(111, 0)
        self.scrollList.add(self.listOfUsers)
        self.scroll = gtk.ScrolledWindow()
        self.scroll.set_shadow_type(gtk.SHADOW_IN)
        self.scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.textview = HtmlTextView(controller, \
            self.parentConversation.textBuffer, self.scroll)
        self.textview.set_wrap_mode(gtk.WRAP_WORD_CHAR)
        self.textview.set_left_margin(6)
        self.textview.set_right_margin(6)
        self.textview.set_editable(False)
        self.textview.set_cursor_visible(False)
        self.textview.connect('key-press-event', self.onTextviewKeyPress)
        self.scroll.add(self.textview)
        self.scroll.show_all()
        self.remoteAvatar = AvatarHBox(self, controller,
            self.parentConversation.switchboard.firstUser)
        self.controller.msn.connect('user-attr-changed', self.onUserAttrChanged)
        self.vboxside = gtk.VBox()
        self.vboxside.pack_start(self.remoteAvatar, False, False)
        self.vboxside.pack_start(self.scrollList, True, True)
        self.vboxside.show()
        self.hbox = gtk.HBox(spacing=2)
        self.hbox.set_border_width(2)
        self.hbox.pack_start(self.scroll, True, True)
        self.hbox.pack_start(self.vboxside, False, False)
        self.hbox.show()
        self.toolbarinput.pack_start(self.input, True, True)
        self.toolbarinput.connect('size-allocate', self.onToolbarinputResize)
        vpaned = gtk.VPaned()
        vpaned.pack1(self.hbox, True, True)
        vpaned.pack2(self.toolbarinput, False)
        vpaned.show()
        self.transfers = FileTransferUI.FtBarWidget(self.controller, self,
                                        parentConversation)
        self.transfers.set_no_show_all(True)
        self.pack_start(self.header, False, False)
        self.pack_start(vpaned, True, True)
        self.pack_start(self.transfers, False, False)
        self.pack_start(self.status, False, False)
        self.messageWaiting = {}
        self.contactTyping = {}
        self.typingTimeoutID = 0
        self.closed = False
        self.last_mark = None
        self.show()
        self.update()
    def onToolbarinputResize(self, *args):
        alloc = self.toolbarinput.get_allocation()
        self.config.user['convInputHeight'] = alloc.height
        self.scrollToBottom()
    def onTextviewKeyPress(self, widget, event):
        if event.keyval == gtk.keysyms.Control_L or \
                event.keyval == gtk.keysyms.Control_R or \
                event.state & gtk.gdk.CONTROL_MASK:
            return
        self.input.input.emit('key-press-event', event)
        self.input.grabFocus()
    def close(self):
        self.closed = True
    def setInputEnabled(self, enable):
        self.input.input.set_sensitive(enable)
    def onUserAttrChanged(self, msn, contact):
        if contact.email in self.parentConversation.getMembers():
            self.update()
    def noneIsTyping(self):
        try:
            for contact in self.email_list:
                if self.contactTyping[contact] == True:
                    return False
            return True
        except:
            return False
    def update(self):
        self.header.update()
        self.tabWidget.update()
        self.input.update()
        self.email_list = [mail.email for mail in self.parentConversation.getMembersDict().values()]
        for email in self.email_list:
            if email not in self.contactTyping.keys():
                self.contactTyping[email] = False
            if email not in self.messageWaiting.keys():
                self.messageWaiting[email] = False
        for email in self.contactTyping.keys():
            if self.contactTyping[email]:
                self.setTyping(email)
            else:
                self.setDefault(email)
        isGroupChat = len(self.parentConversation.getMembers()) > 1
        if isGroupChat:
            members = self.parentConversation.getMembersDict()
            d = {'members' : emesenelib.ContactData.Group('members')}
            for member in members.values():
                d['members'].setUser(member.email, member)
            self.listOfUsers.fill(d)
        if isGroupChat:
            self.showUserList()
            self.remoteAvatar.hide()
        elif self.config.user['showAvatars']:
            self.hideUserList()
            self.remoteAvatar.show_all()
            self.remoteAvatar.update()
        else:
            self.hideUserList()
            self.remoteAvatar.hide()
        if not self.config.user['showHeader']:
            self.header.hide()
        else:
            self.header.show()
        if not self.config.user['showTabCloseButton']:
            self.tabWidget.closeButton.hide()
        else:
            self.tabWidget.closeButton.show()
        if not self.config.user['showStatusBar']:
            self.status.hide()
        else:
            self.status.show()
        inputHeight = self.config.user['convInputHeight']
        self.toolbarinput.set_size_request(0, inputHeight)
    def scrollToBottom(self):
        '''scroll to the end of the conversation'''
        self.textview.scrollToBottom()
    def showUserList(self):
        self.scrollList.show_all()
        self.listOfUsers.show()
    def hideUserList(self):
        self.scrollList.hide()
        self.listOfUsers.hide()
    def rebuildStatusText(self):
        '''Builds the text displayed in the statusbar, based in
        self.contactTyping. The "output" is a comma separated list
        of (full) mails and "is/are typing a message..." '''
        mails = [x for x in self.contactTyping.keys() \
                  if self.contactTyping[x] == True]
        string = ''
        if len(mails) > 0:
            comma = ', ' # TODO: gettext?
            for mail in mails:
                if self.config.user['showMailTyping']:
                    string += str(mail)
                else:
                    contact = self.controller.getContact(mail)
                    if contact:
                        parts = self.parser.getParser(contact.nick).get()
                        for part in parts:
                            string += str(part)
                    else:
                        string += str(mail)
                string += comma
            string = str(unicode(string)[:-len(comma)])
            if len(mails) == 1:
                string += ' ' + _('is typing a message...')
            else:
                string += ' ' + _('are typing a message...')
        self.status.get_children()[0].get_children()[0].set_text(string)
    def setMessageWaiting(self, mail):
        if self.parentConversation.isCurrent:
            return self.setDefault(mail)
        self.tabWidget.setMessageWaiting()
        self.header.setDefault()
        if mail:
            self.contactTyping[mail] = False
            self.messageWaiting[mail] = True
            self.rebuildStatusText()
    def setTyping(self, mail):
        if self.messageWaiting.has_key(mail) and self.messageWaiting[mail]: return
        self.header.setTyping()
        self.tabWidget.setTyping()
        self.contactTyping[mail] = True
        self.rebuildStatusText()
        if self.typingTimeoutID > 0:
            gobject.source_remove(self.typingTimeoutID)
        self.typingTimeoutID = gobject.timeout_add(8000, \
            self.clearTyping, mail)
    def clearTyping(self, mail):
        if mail in self.messageWaiting and self.messageWaiting[mail]:
            self.setMessageWaiting(mail)
        else:
            self.setDefault(mail)
        self.contactTyping[mail] = False
        return False
    def setDefault(self, mail):
        self.tabWidget.setDefault()
        self.header.setDefault()
        if mail:
            self.contactTyping[mail] = False
            self.messageWaiting[mail] = False
            self.rebuildStatusText()