Skip to content

Commit

Permalink
ChatView: add_line() and prepend_old_messages() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
slook committed Jun 24, 2024
1 parent da33f0b commit 7e566f7
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 149 deletions.
6 changes: 3 additions & 3 deletions pynicotine/chatrooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,14 @@ def _say_chat_room(self, msg, is_global=False):
is_action_message = (msg.message_type == "action")

if is_action_message:
message = message.replace("/me ", "", 1)
msg.message = message = message.replace("/me ", "", 1)

if config.sections["words"]["censorwords"] and username != core.users.login_username:
message = censor_text(message, censored_patterns=config.sections["words"]["censored"])
msg.message = message = censor_text(message, censored_patterns=config.sections["words"]["censored"])

if config.sections["logging"]["chatrooms"] or room in config.sections["logging"]["rooms"]:
if is_action_message:
formatted_message = msg.message = f"* {username} {message}"
formatted_message = f"* {username} {message}"
else:
formatted_message = f"[{username}] {message}"

Expand Down
43 changes: 19 additions & 24 deletions pynicotine/gtkgui/chatrooms.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,12 @@ def __init__(self, chatrooms, room, is_private, is_global):
)

self.setup_public_feed()
self.read_room_logs()

# Old log lines need to be read from the file now, but they can be prepended later
self.old_messages = self.get_old_messages(num_lines=config.sections["logging"]["readroomlines"])

def load(self):
GLib.idle_add(self.read_room_logs_finished)
GLib.idle_add(self.on_loaded)
self.loaded = True

def clear(self):
Expand Down Expand Up @@ -648,25 +650,25 @@ def add_user_row(self, userdata):
underline
], select_row=False)

def read_room_logs_finished(self):
def on_loaded(self):

if not hasattr(self, "chat_view"):
# Tab was closed
return

self.put_old_messages()

self.activity_view.scroll_bottom()
self.chat_view.scroll_bottom()

self.activity_view.auto_scroll = self.chat_view.auto_scroll = True

def read_room_logs(self):
def get_old_messages(self, num_lines=None):
return log.read_log(log.room_folder_path, self.room, num_lines) or []

self.chat_view.append_log_lines(
folder_path=log.room_folder_path,
basename=self.room,
num_lines=config.sections["logging"]["readroomlines"],
timestamp_format=config.sections["logging"]["rooms_timestamp"]
)
def put_old_messages(self):
self.chat_view.prepend_log_lines(self.old_messages, login_username=config.sections["server"]["login"])
self.old_messages.clear()

def populate_room_users(self, users):

Expand Down Expand Up @@ -694,7 +696,7 @@ def populate_room_users(self, users):
if self.chatrooms.get_current_page() == self.container:
self.update_room_user_completions()

self.activity_view.append_line(
self.activity_view.add_line(
_("%s joined the room") % core.users.login_username,
timestamp_format=config.sections["logging"]["rooms_timestamp"]
)
Expand Down Expand Up @@ -786,8 +788,6 @@ def say_chat_room(self, msg):
username = msg.user
message = msg.message
message_type = msg.message_type
roomtag = self.chat_view.get_room_tag(roomname)
usertag = self.chat_view.get_user_tag(username)

if message_type != "local":
if self.speech_toggle.get_active():
Expand All @@ -798,13 +798,8 @@ def say_chat_room(self, msg):
self._show_notification(
roomname, username, message, is_mentioned=(message_type == "hilite"))

self.chat_view.append_line(
message,
message_type=message_type,
username=username,
usertag=usertag,
roomname=roomname,
roomtag=roomtag,
self.chat_view.add_line(
message, message_type=message_type, username=username, roomname=roomname,
timestamp_format=config.sections["logging"]["rooms_timestamp"]
)

Expand All @@ -818,7 +813,7 @@ def echo_room_message(self, message, message_type):
else:
timestamp_format = None

self.chat_view.append_line(message, message_type=message_type, timestamp_format=timestamp_format)
self.chat_view.add_line(message, message_type=message_type, timestamp_format=timestamp_format)

def user_joined_room(self, msg):

Expand All @@ -833,7 +828,7 @@ def user_joined_room(self, msg):
self.chatrooms.completion.add_completion(username)

if not core.network_filter.is_user_ignored(username) and not core.network_filter.is_user_ip_ignored(username):
self.activity_view.append_line(
self.activity_view.add_line(
_("%s joined the room") % username, timestamp_format=config.sections["logging"]["rooms_timestamp"]
)

Expand All @@ -857,7 +852,7 @@ def user_left_room(self, msg):
if not core.network_filter.is_user_ignored(username) and \
not core.network_filter.is_user_ip_ignored(username):
timestamp_format = config.sections["logging"]["rooms_timestamp"]
self.activity_view.append_line(_("%s left the room") % username, timestamp_format=timestamp_format)
self.activity_view.add_line(_("%s left the room") % username, timestamp_format=timestamp_format)

self.users_list_view.remove_row(iterator)

Expand Down Expand Up @@ -916,7 +911,7 @@ def user_status(self, msg):
return

if not core.network_filter.is_user_ignored(user) and not core.network_filter.is_user_ip_ignored(user):
self.activity_view.append_line(
self.activity_view.add_line(
action % user, timestamp_format=config.sections["logging"]["rooms_timestamp"])

self.users_list_view.set_row_value(iterator, "status", status_icon_name)
Expand Down
2 changes: 1 addition & 1 deletion pynicotine/gtkgui/dialogs/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -2876,7 +2876,7 @@ def on_select_plugin(self, list_view, iterator):
self.plugin_authors_label.set_text(plugin_authors)

self.plugin_description_view.clear()
self.plugin_description_view.append_line(plugin_description)
self.plugin_description_view.add_line(plugin_description)
self.plugin_description_view.place_cursor_at_line(0)

self.check_plugin_settings_button(self.selected_plugin)
Expand Down
2 changes: 1 addition & 1 deletion pynicotine/gtkgui/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ def update_log(self, timestamp_format, msg, title, level):
if level not in {"transfer", "connection", "message", "miscellaneous"}:
self.set_status_text(msg)

self.log_view.append_line(msg, timestamp_format=timestamp_format)
self.log_view.add_line(msg, timestamp_format=timestamp_format)

def on_popup_menu_log(self, menu, _textview):
menu.actions[_("_Copy")].set_enabled(self.log_view.get_has_selection())
Expand Down
9 changes: 5 additions & 4 deletions pynicotine/gtkgui/popovers/roomwall.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ def _update_message_list(self):

tickers = core.chatrooms.joined_rooms[self.room].tickers
newline = "\n"
messages = [f"> [{user}] {msg.replace(newline, ' ')}" for user, msg in reversed(list(tickers.items()))]

self.message_view.append_line("\n".join(messages))
for user, message in list(tickers.items()):
self.message_view.add_line(f"> [{user}] {message.replace(newline, ' ')}", prepend=True)

self.message_view.place_cursor_at_line(0)

def _clear_room_wall_message(self, update_list=True):
Expand All @@ -79,8 +80,8 @@ def on_set_room_wall_message(self, *_args):
core.chatrooms.request_update_ticker(self.room, entry_text)

if entry_text:
user = core.users.login_username
self.message_view.append_line(f"> [{user}] {entry_text}")
username = core.users.login_username
self.message_view.add_line(f"> [{username}] {entry_text}", prepend=True)

self._update_message_list()

Expand Down
32 changes: 16 additions & 16 deletions pynicotine/gtkgui/privatechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,29 +379,30 @@ def __init__(self, chats, user):

self.popup_menus = (self.popup_menu, self.popup_menu_user_chat, self.popup_menu_user_tab)

self.read_private_log()
# Old log lines need to be read from the file now, but they can be prepended later
self.old_messages = self.get_old_messages(num_lines=config.sections["logging"]["readprivatelines"])

def load(self):
GLib.idle_add(self.read_private_log_finished)
GLib.idle_add(self.on_loaded)
self.loaded = True

def read_private_log_finished(self):
def on_loaded(self):

if not hasattr(self, "chat_view"):
# Tab was closed
return

self.put_old_messages()

self.chat_view.scroll_bottom()
self.chat_view.auto_scroll = True

def read_private_log(self):
def get_old_messages(self, num_lines=None):
return log.read_log(log.private_chat_folder_path, self.user, num_lines) or []

self.chat_view.append_log_lines(
folder_path=log.private_chat_folder_path,
basename=self.user,
num_lines=config.sections["logging"]["readprivatelines"],
timestamp_format=config.sections["logging"]["private_timestamp"]
)
def put_old_messages(self):
self.chat_view.prepend_log_lines(self.old_messages, login_username=config.sections["server"]["login"])
self.old_messages.clear()

def server_login(self):
self.chat_entry.set_sensitive(True)
Expand Down Expand Up @@ -504,7 +505,6 @@ def message_user(self, msg):

username = msg.user
tag_username = (core.users.login_username if is_outgoing_message else username)
usertag = self.chat_view.get_user_tag(tag_username)

timestamp = msg.timestamp if not is_new_message else None
timestamp_format = config.sections["logging"]["private_timestamp"]
Expand All @@ -520,7 +520,7 @@ def message_user(self, msg):

if not is_outgoing_message and not is_new_message:
if not self.offline_message:
self.chat_view.append_line(
self.chat_view.add_line(
_("* Messages sent while you were offline"), message_type="hilite",
timestamp_format=timestamp_format
)
Expand All @@ -529,11 +529,11 @@ def message_user(self, msg):
else:
self.offline_message = False

self.chat_view.append_line(
self.chat_view.add_line(
message, message_type=message_type, timestamp=timestamp, timestamp_format=timestamp_format,
username=tag_username, usertag=usertag
username=tag_username
)
self.chats.history.update_user(username, message, timestamp)
self.chats.history.update_user(username, message)

def echo_private_message(self, text, message_type):

Expand All @@ -542,7 +542,7 @@ def echo_private_message(self, text, message_type):
else:
timestamp_format = None

self.chat_view.append_line(text, message_type=message_type, timestamp_format=timestamp_format)
self.chat_view.add_line(text, message_type=message_type, timestamp_format=timestamp_format)

def username_event(self, pos_x, pos_y, user):

Expand Down
2 changes: 1 addition & 1 deletion pynicotine/gtkgui/userinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ def user_info_response(self, msg):

if msg.descr is not None:
self.description_view.clear()
self.description_view.append_line(msg.descr)
self.description_view.add_line(msg.descr)

self.upload_slots_label.set_text(humanize(msg.totalupl))
self.queued_uploads_label.set_text(humanize(msg.queuesize))
Expand Down
2 changes: 1 addition & 1 deletion pynicotine/gtkgui/widgets/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def _add_long_message(self, text):
self.container.add(frame) # pylint: disable=no-member

textview = self.default_focus_widget = TextView(scrolled_window, editable=False)
textview.append_line(text)
textview.add_line(text)

self.container.set_visible(True)

Expand Down
Loading

0 comments on commit 7e566f7

Please sign in to comment.