2018-04-28 21:52:42 +00:00
|
|
|
from history.database import MESSAGE_AUTHOR
|
2018-05-19 18:04:40 +00:00
|
|
|
import os.path
|
2018-05-15 14:00:12 +00:00
|
|
|
from ui.messages_widgets import *
|
2016-03-29 12:54:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
MESSAGE_TYPE = {
|
|
|
|
'TEXT': 0,
|
|
|
|
'ACTION': 1,
|
|
|
|
'FILE_TRANSFER': 2,
|
2016-06-13 16:28:17 +00:00
|
|
|
'INLINE': 3,
|
2018-03-11 21:32:46 +00:00
|
|
|
'INFO_MESSAGE': 4
|
2016-03-29 12:54:58 +00:00
|
|
|
}
|
|
|
|
|
2018-04-28 21:52:42 +00:00
|
|
|
PAGE_SIZE = 42
|
|
|
|
|
2016-03-29 12:54:58 +00:00
|
|
|
|
2018-04-26 20:54:39 +00:00
|
|
|
class MessageAuthor:
|
|
|
|
|
|
|
|
def __init__(self, author_name, author_type):
|
2018-05-19 18:04:40 +00:00
|
|
|
self._name = author_name
|
|
|
|
self._type = author_type
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
name = property(get_name)
|
|
|
|
|
|
|
|
def get_type(self):
|
|
|
|
return self._type
|
|
|
|
|
2018-08-10 21:30:33 +00:00
|
|
|
def set_type(self, value):
|
|
|
|
self._type = value
|
|
|
|
|
|
|
|
type = property(get_type, set_type)
|
2018-04-26 20:54:39 +00:00
|
|
|
|
|
|
|
|
2016-06-22 11:35:22 +00:00
|
|
|
class Message:
|
2016-03-29 12:54:58 +00:00
|
|
|
|
2018-05-16 11:10:24 +00:00
|
|
|
MESSAGE_ID = 0
|
|
|
|
|
2018-05-15 14:00:12 +00:00
|
|
|
def __init__(self, message_type, author, time):
|
2016-03-29 12:54:58 +00:00
|
|
|
self._time = time
|
|
|
|
self._type = message_type
|
2018-04-26 20:54:39 +00:00
|
|
|
self._author = author
|
|
|
|
self._widget = None
|
2018-05-16 11:10:24 +00:00
|
|
|
self._message_id = self._get_id()
|
2016-03-29 12:54:58 +00:00
|
|
|
|
|
|
|
def get_type(self):
|
|
|
|
return self._type
|
|
|
|
|
2018-04-26 20:54:39 +00:00
|
|
|
type = property(get_type)
|
2016-04-01 17:44:02 +00:00
|
|
|
|
2018-04-26 20:54:39 +00:00
|
|
|
def get_author(self):
|
|
|
|
return self._author
|
|
|
|
|
|
|
|
author = property(get_author)
|
2016-06-04 19:17:32 +00:00
|
|
|
|
2018-05-01 18:40:29 +00:00
|
|
|
def get_time(self):
|
|
|
|
return self._time
|
|
|
|
|
|
|
|
time = property(get_time)
|
|
|
|
|
2018-05-16 11:10:24 +00:00
|
|
|
def get_message_id(self):
|
|
|
|
return self._message_id
|
|
|
|
|
|
|
|
message_id = property(get_message_id)
|
|
|
|
|
2018-05-15 14:00:12 +00:00
|
|
|
def get_widget(self, *args):
|
2018-08-29 21:36:07 +00:00
|
|
|
self._widget = self._create_widget(*args)
|
2018-04-26 20:54:39 +00:00
|
|
|
|
|
|
|
return self._widget
|
|
|
|
|
|
|
|
widget = property(get_widget)
|
|
|
|
|
|
|
|
def remove_widget(self):
|
|
|
|
self._widget = None
|
|
|
|
|
|
|
|
def mark_as_sent(self):
|
2018-08-10 21:30:33 +00:00
|
|
|
self._author.type = MESSAGE_AUTHOR['ME']
|
2018-05-18 15:40:41 +00:00
|
|
|
if self._widget is not None:
|
|
|
|
self._widget.mark_as_sent()
|
2018-04-26 20:54:39 +00:00
|
|
|
|
2018-05-15 14:00:12 +00:00
|
|
|
def _create_widget(self, *args):
|
2018-04-26 20:54:39 +00:00
|
|
|
pass
|
|
|
|
|
2018-05-16 11:10:24 +00:00
|
|
|
@staticmethod
|
|
|
|
def _get_id():
|
|
|
|
Message.MESSAGE_ID += 1
|
|
|
|
|
2018-05-17 12:20:47 +00:00
|
|
|
return int(Message.MESSAGE_ID)
|
2018-05-16 11:10:24 +00:00
|
|
|
|
2016-03-29 12:54:58 +00:00
|
|
|
|
|
|
|
class TextMessage(Message):
|
2016-03-30 18:05:21 +00:00
|
|
|
"""
|
|
|
|
Plain text or action message
|
|
|
|
"""
|
2016-03-29 12:54:58 +00:00
|
|
|
|
2018-05-16 11:10:24 +00:00
|
|
|
def __init__(self, message, owner, time, message_type, message_id=0):
|
2018-05-15 14:00:12 +00:00
|
|
|
super().__init__(message_type, owner, time)
|
2016-03-29 12:54:58 +00:00
|
|
|
self._message = message
|
2018-05-16 11:10:24 +00:00
|
|
|
self._id = message_id
|
2016-03-29 12:54:58 +00:00
|
|
|
|
2018-05-01 18:40:29 +00:00
|
|
|
def get_text(self):
|
|
|
|
return self._message
|
|
|
|
|
|
|
|
text = property(get_text)
|
|
|
|
|
2018-05-16 11:10:24 +00:00
|
|
|
def get_id(self):
|
|
|
|
return self._id
|
|
|
|
|
|
|
|
id = property(get_id)
|
|
|
|
|
|
|
|
def is_saved(self):
|
|
|
|
return self._id > 0
|
2016-03-29 12:54:58 +00:00
|
|
|
|
2018-05-15 14:00:12 +00:00
|
|
|
def _create_widget(self, *args):
|
|
|
|
return MessageItem(self, *args)
|
|
|
|
|
|
|
|
|
|
|
|
class OutgoingTextMessage(TextMessage):
|
|
|
|
|
2018-05-19 13:00:28 +00:00
|
|
|
def __init__(self, message, owner, time, message_type, tox_message_id=0):
|
2018-05-15 14:00:12 +00:00
|
|
|
super().__init__(message, owner, time, message_type)
|
|
|
|
self._tox_message_id = tox_message_id
|
|
|
|
|
|
|
|
def get_tox_message_id(self):
|
|
|
|
return self._tox_message_id
|
|
|
|
|
2018-05-18 15:40:41 +00:00
|
|
|
def set_tox_message_id(self, tox_message_id):
|
|
|
|
self._tox_message_id = tox_message_id
|
|
|
|
|
|
|
|
tox_message_id = property(get_tox_message_id, set_tox_message_id)
|
2018-04-26 20:54:39 +00:00
|
|
|
|
2016-03-29 12:54:58 +00:00
|
|
|
|
2017-07-16 22:11:09 +00:00
|
|
|
class GroupChatMessage(TextMessage):
|
|
|
|
|
2018-03-11 21:32:46 +00:00
|
|
|
def __init__(self, id, message, owner, time, message_type, name):
|
|
|
|
super().__init__(id, message, owner, time, message_type)
|
2017-07-16 22:11:09 +00:00
|
|
|
self._user_name = name
|
|
|
|
|
|
|
|
|
2016-03-29 12:54:58 +00:00
|
|
|
class TransferMessage(Message):
|
2016-03-30 18:05:21 +00:00
|
|
|
"""
|
|
|
|
Message with info about file transfer
|
|
|
|
"""
|
2016-03-29 12:54:58 +00:00
|
|
|
|
2018-05-16 16:04:02 +00:00
|
|
|
def __init__(self, author, time, state, size, file_name, friend_number, file_number):
|
|
|
|
super().__init__(MESSAGE_TYPE['FILE_TRANSFER'], author, time)
|
|
|
|
self._state = state
|
2016-03-29 12:54:58 +00:00
|
|
|
self._size = size
|
2018-05-16 16:04:02 +00:00
|
|
|
self._file_name = file_name
|
2016-03-29 12:54:58 +00:00
|
|
|
self._friend_number, self._file_number = friend_number, file_number
|
|
|
|
|
|
|
|
def is_active(self, file_number):
|
2018-05-19 18:04:40 +00:00
|
|
|
if self._file_number != file_number:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return self._state not in (FILE_TRANSFER_STATE['FINISHED'], FILE_TRANSFER_STATE['CANCELLED'])
|
2016-03-29 12:54:58 +00:00
|
|
|
|
|
|
|
def get_friend_number(self):
|
|
|
|
return self._friend_number
|
|
|
|
|
2018-05-16 16:04:02 +00:00
|
|
|
friend_number = property(get_friend_number)
|
|
|
|
|
2016-03-29 12:54:58 +00:00
|
|
|
def get_file_number(self):
|
|
|
|
return self._file_number
|
|
|
|
|
2018-05-16 16:04:02 +00:00
|
|
|
file_number = property(get_file_number)
|
|
|
|
|
|
|
|
def get_state(self):
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
def set_state(self, value):
|
|
|
|
self._state = value
|
|
|
|
|
|
|
|
state = property(get_state, set_state)
|
|
|
|
|
|
|
|
def get_size(self):
|
|
|
|
return self._size
|
|
|
|
|
|
|
|
size = property(get_size)
|
|
|
|
|
|
|
|
def get_file_name(self):
|
|
|
|
return self._file_name
|
2016-03-29 12:54:58 +00:00
|
|
|
|
2018-05-16 16:04:02 +00:00
|
|
|
file_name = property(get_file_name)
|
2016-03-29 12:54:58 +00:00
|
|
|
|
2018-05-16 21:02:22 +00:00
|
|
|
def transfer_updated(self, state, percentage, time):
|
|
|
|
self._state = state
|
|
|
|
if self._widget is not None:
|
|
|
|
self._widget.update_transfer_state(state, percentage, time)
|
|
|
|
|
2018-05-16 16:31:08 +00:00
|
|
|
def _create_widget(self, *args):
|
|
|
|
return FileTransferItem(self, *args)
|
|
|
|
|
2016-03-29 12:54:58 +00:00
|
|
|
|
2018-05-19 18:04:40 +00:00
|
|
|
class UnsentFileMessage(TransferMessage):
|
2018-05-16 11:10:24 +00:00
|
|
|
|
2018-05-19 18:04:40 +00:00
|
|
|
def __init__(self, path, data, time, author, size, friend_number):
|
|
|
|
file_name = os.path.basename(path)
|
|
|
|
super().__init__(author, time, FILE_TRANSFER_STATE['UNSENT'], size, file_name, friend_number, -1)
|
2016-06-15 12:38:42 +00:00
|
|
|
self._data, self._path = data, path
|
|
|
|
|
2018-05-18 09:54:00 +00:00
|
|
|
def get_data(self):
|
|
|
|
return self._data
|
|
|
|
|
|
|
|
data = property(get_data)
|
|
|
|
|
|
|
|
def get_path(self):
|
|
|
|
return self._path
|
|
|
|
|
|
|
|
path = property(get_path)
|
|
|
|
|
|
|
|
def _create_widget(self, *args):
|
|
|
|
return UnsentFileItem(self, *args)
|
|
|
|
|
2016-06-15 12:38:42 +00:00
|
|
|
|
2018-05-18 09:54:00 +00:00
|
|
|
class InlineImageMessage(Message):
|
2016-03-30 18:05:21 +00:00
|
|
|
"""
|
|
|
|
Inline image
|
|
|
|
"""
|
2016-03-29 12:54:58 +00:00
|
|
|
|
2018-05-16 11:47:14 +00:00
|
|
|
def __init__(self, data):
|
|
|
|
super().__init__(MESSAGE_TYPE['INLINE'], None, None)
|
2016-03-29 12:54:58 +00:00
|
|
|
self._data = data
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return self._data
|
2016-06-13 16:28:17 +00:00
|
|
|
|
2018-05-16 11:10:24 +00:00
|
|
|
data = property(get_data)
|
|
|
|
|
2018-05-16 16:31:08 +00:00
|
|
|
def _create_widget(self, *args):
|
|
|
|
return InlineImageItem(self, *args)
|
|
|
|
|
2016-06-13 16:28:17 +00:00
|
|
|
|
|
|
|
class InfoMessage(TextMessage):
|
|
|
|
|
2018-05-18 10:23:48 +00:00
|
|
|
def __init__(self, message, time):
|
|
|
|
super().__init__(message, None, time, MESSAGE_TYPE['INFO_MESSAGE'])
|