示例#1
0
    def loadChat(
        self,
        chat=None
    ):  # when the user clicks on Person in listBox3 this function is called and loads the correct chat and sets up everything needed to start the communication
        if not chat:
            if self.listBox3.curselection()[0] or self.listBox3.curselection(
            )[0] == 0:
                selection = self.listBox3.curselection()[
                    0]  # this gives an int value: first element = 0

            if selection or selection == 0:  # if something is selected
                self.partner[0] = self.person_list[int(selection)][0]
                self.partner[1] = self.person_list[int(selection)][1]
        else:
            if self.partner[0] != chat[0]:
                self.partner[0] = chat[0]
                self.partner[1] = chat[1]

        for i in range(len(self.person_list)):
            if self.person_list[i][1] == self.partner[1]:
                self.person_list[i][2] = time.time()

        chat_type = self.chat_function.get_full_chat(
            'chat', self.feed_id, self.partner[1])[0][0].split("#split:#")[
                3]  # get the type of the chat (private or group)

        if chat_type == "private":
            self.username_label.config(
                text=TextWrapper.shorten_name(self.partner[0], 34))
        else:  # chat_type == "group"
            self.username_label.config(
                text=TextWrapper.shorten_name(self.partner[0], 27) + " (" +
                self.partner[1] + ")")

        self.updateContent(self.partner[1])
    def add(self, chatID):

        self.listBox1.delete(0, 'end')
        self.listBox2.delete(0, 'end')

        self.time = datetime.datetime.now()
        timezone = 7200  # +2h, so we have the european time in the most simplistic way

        chat = self.chatfunction.get_full_chat('chat', self.feed_id, chatID)

        chat_type = chat[0][0].split("#split:#")[
            3]  #get the type of the chat (private or group)

        for i in range(1, len(chat)):

            chatMessage = chat[i][0].split(
                "#split:#"
            )  # a chat-message is like: username#split:#message, so we need to split this two
            username = chatMessage[0]  #from who is the message
            message = chatMessage[1]  #the real content / message
            additional_msg = chatMessage[2]

            if len(chatMessage) == 4:

                if chat_type == "private" and self.partner[0] == self.partner[
                        1]:
                    for i in range(len(self.personList)):
                        if self.partner[1] == self.personList[i][1]:
                            self.personList[i][
                                0] = username  #the creator of the group gets the name of his partner for the first time
                            self.partner[0] = username
                            self.username_label.config(
                                text=TextWrapper.shorten_name(
                                    self.partner[0], 34))

            else:  # length of message == 3 #TODO: special clause for BILD and PDF_

                if username != self.username:  #message from the partner(s)
                    #print the message with white background:

                    if chat_type == "group":
                        self.listBox1.insert('end', username + ":")
                        try:
                            self.listBox1.itemconfig(
                                'end',
                                bg='white',
                                foreground=HexMex.name_to_color(username))
                        except:
                            self.listBox1.itemconfig(
                                'end',
                                bg='white',
                                foreground=HexMex.name_to_color("randomName"))
                        self.listBox2.insert('end', "")

                    if additional_msg[0:3] == "pdf":
                        self.listBox1.insert(
                            'end', "Click to open pdf. (" + str(i) + ")")
                    elif additional_msg[0:3] == "img":
                        self.listBox1.insert(
                            'end', "Click to open image. (" + str(i) + ")")
                    else:
                        self.listBox1.insert('end', message)

                    self.listBox1.itemconfig('end', bg='white')
                    self.listBox1.insert(
                        'end', "{:<16}{:>16}".format(
                            "",
                            time.strftime("%H:%M %d.%m.%Y",
                                          time.gmtime(chat[i][1] + timezone))))
                    self.listBox1.itemconfig('end',
                                             bg='white',
                                             foreground="lightgrey")

                    for i in range(2):
                        self.listBox2.insert(
                            'end',
                            "")  # add 2 empty lines to balance both sides
                    self.listBox1.yview(END)
                    self.listBox2.yview(END)
                    self.listBox1.insert('end',
                                         '')  # some space to enhance appeal
                    self.listBox2.insert('end', '')

                else:  #username = self.username: message from the user
                    #print the message with green background:

                    if additional_msg[0:3] == "pdf":
                        self.listBox2.insert(
                            'end', "Click to open PDF. (" + str(i) + ")")
                    elif additional_msg[0:3] == "img":
                        self.listBox2.insert(
                            'end', "Click to open image. (" + str(i) + ")")
                    else:  # == msg
                        self.listBox2.insert('end', message)

                    self.listBox2.itemconfig('end', bg='#dafac9')
                    self.listBox2.insert(
                        'end', "{:<16}{:>16}".format(
                            "",
                            time.strftime("%H:%M %d.%m.%Y",
                                          time.gmtime(chat[i][1] + timezone))))
                    self.listBox2.itemconfig('end',
                                             bg='#dafac9',
                                             foreground="lightgrey")

                    for i in range(2):
                        self.listBox1.insert(
                            'end',
                            "")  # add 2 empty lines to balance both sides
                    self.listBox2.yview(END)
                    self.listBox1.yview(END)
                    self.listBox2.insert('end',
                                         '')  # some space to enhance appeal
                    self.listBox1.insert('end', '')