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.chat_function.get_full_chat(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)): chat_message = chat[i][0].split( "#split:#" ) # a chat-message is like: username#split:#message, so we need to split this two partner_username = chat_message[0] # from who is the message message = chat_message[1] # the real content / message additional_msg = chat_message[2] if len(chat_message) == 4: if chat_type == "private" and self.partner[0] == self.partner[ 1] and additional_msg == "member": for i in range(len(self.person_list)): if self.partner[1] == self.person_list[i][1]: self.person_list[i][ 0] = partner_username # the creator of the group gets the name of his partner for the first time self.partner[0] = partner_username self.username_label.config( text=TextWrapper.shorten_name( self.partner[0], 34)) else: # length of message == 3 if partner_username != self.username: # message from the partner(s) # print the message with white background: if chat_type == "group": self.listBox1.insert('end', partner_username + ":") try: self.listBox1.itemconfig( 'end', bg='white', foreground=Colorize.name_to_color( partner_username)) except: self.listBox1.itemconfig( 'end', bg='white', foreground=Colorize.name_to_color( "randomName")) self.listBox2.insert('end', "") if additional_msg[0:3] == "pdf": self.listBox1.insert( 'end', "Click to open pdf. (" + str(i) + ")") self.listBox1.itemconfig('end', bg='white') self.listBox2.insert('end', "") elif additional_msg[0:3] == "img": self.listBox1.insert( 'end', "Click to open image. (" + str(i) + ")") self.listBox1.itemconfig('end', bg='white') self.listBox2.insert('end', "") else: messages = TextWrapper.textWrap(message, 0) for i in range(len(messages)): self.listBox1.insert('end', messages[i]) self.listBox1.itemconfig('end', bg='white') self.listBox2.insert('end', '') self.listBox1.insert( 'end', "{:<22}{:>16}".format( "", time.strftime("%H:%M %d.%m.%Y", time.gmtime(chat[i][1] + timezone)))) self.listBox1.itemconfig('end', bg='white', foreground="lightgrey") self.listBox2.insert('end', "") 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) + ")") self.listBox2.itemconfig('end', bg='#dafac9') self.listBox1.insert('end', '') elif additional_msg[0:3] == "img": self.listBox2.insert( 'end', "Click to open image. (" + str(i) + ")") self.listBox2.itemconfig('end', bg='#dafac9') self.listBox1.insert('end', '') else: # == msg messages = TextWrapper.textWrap(message, 0) for i in range(len(messages)): self.listBox2.insert('end', messages[i]) self.listBox2.itemconfig('end', bg='#dafac9') self.listBox1.insert('end', '') self.listBox2.insert( 'end', "{:<22}{:>16}".format( "", time.strftime("%H:%M %d.%m.%Y", time.gmtime(chat[i][1] + timezone)))) self.listBox2.itemconfig('end', bg='#dafac9', foreground="lightgrey") self.listBox1.insert('end', '') self.listBox2.yview(END) self.listBox1.yview(END) self.listBox2.insert('end', '') # some space to enhance appeal self.listBox1.insert('end', '')
def add(self, username, content): # self.updateContent(self.partner) # instead of an update button, it checks for new incoming messages when/before when you send a new message: currently results in errors (Aborted (core dumped)): seems like an infinite loop self.time = datetime.datetime.now() # self.lastMessage[chatNumber] = self.time #Update last message sent from this person if username != self.username: # user updated try: wrappedContent = TextWrapper.textWrap(content, 0) print("getting message from " + self.partner + ":") self.listBox1.insert( 'end', '[' + self.time.strftime("%H:%M:%S") + ']') self.listBox1.itemconfig('end', bg='white', foreground="lightgrey") self.listBox1.insert('end', wrappedContent[0]) for i in range(2): self.listBox2.insert( 'end', "") # add 2 empty lines to balance both sides self.listBox1.itemconfig('end', bg='white') self.listBox1.yview(END) self.listBox2.yview(END) self.listBox1.insert('end', '') # some space to enhance appeal self.listBox2.insert('end', '') except: print("no new messages available from " + self.partner) else: # user typed something lastEntry = '' index = 0 ContentArray = pcap.dumpIt(username + '.pcap') while True: # while not at the end of the list (we want to get the last entry) try: lastEntry = ContentArray[index + 1] lastEntry = lastEntry[2:len(ContentArray[index + 1]) - 2] # removing the [""] index += 1 except: print("Arrived at last message: \"" + lastEntry + "\"") if content != '': self.listBox2.insert( 'end', '[' + self.time.strftime("%H:%M:%S") + ']') self.listBox2.itemconfig( 'end', bg='#dafac9', foreground="lightgrey" ) # dafac9 = light green (coloring for sender messaages) self.listBox1.insert( 'end', '' ) # adjust the other listbox for them to by synced on the same height wrappedContent = TextWrapper.textWrap(content, 0) print(".", wrappedContent) for i in range(len(wrappedContent)): self.listBox2.insert('end', wrappedContent[i]) self.listBox2.itemconfig('end', bg='#dafac9') self.listBox1.insert( 'end', '' ) # adjust the other listbox for them to by synced on the same height self.listBox1.yview(END) self.listBox2.yview(END) self.listBox1.yview(END) self.listBox2.yview(END) self.listBox1.insert( 'end', '') # some space to enhance appeal self.listBox2.insert('end', '') self.text_field.delete(0, 'end') self.save(content) break