| 1 |
# COPYRIGHT (C) 2020-2024 Nicotine+ Contributors |
| 2 |
# |
| 3 |
# GNU GENERAL PUBLIC LICENSE |
| 4 |
# Version 3, 29 June 2007 |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
import re |
| 20 |
import time |
| 21 |
|
| 22 |
from gi.repository import Gdk |
| 23 |
from gi.repository import Gtk |
| 24 |
|
| 25 |
from pynicotine.core import core |
| 26 |
from pynicotine.gtkgui.application import GTK_API_VERSION |
| 27 |
from pynicotine.gtkgui.widgets import clipboard |
| 28 |
from pynicotine.gtkgui.widgets.accelerator import Accelerator |
| 29 |
from pynicotine.gtkgui.widgets.theme import update_tag_visuals |
| 30 |
from pynicotine.gtkgui.widgets.theme import USER_STATUS_COLORS |
| 31 |
from pynicotine.slskmessages import UserStatus |
| 32 |
from pynicotine.utils import find_whole_word |
| 33 |
from pynicotine.utils import open_uri |
| 34 |
|
| 35 |
|
| 36 |
class TextView: |
| 37 |
|
| 38 |
try: |
| 39 |
if GTK_API_VERSION >= 4: |
| 40 |
DEFAULT_CURSOR = Gdk.Cursor(name="default") |
| 41 |
POINTER_CURSOR = Gdk.Cursor(name="pointer") |
| 42 |
TEXT_CURSOR = Gdk.Cursor(name="text") |
| 43 |
else: |
| 44 |
DEFAULT_CURSOR = Gdk.Cursor.new_from_name(Gdk.Display.get_default(), "default") |
| 45 |
POINTER_CURSOR = Gdk.Cursor.new_from_name(Gdk.Display.get_default(), "pointer") |
| 46 |
TEXT_CURSOR = Gdk.Cursor.new_from_name(Gdk.Display.get_default(), "text") |
| 47 |
|
| 48 |
except TypeError: |
| 49 |
# Broken cursor theme, but what can we do... |
| 50 |
DEFAULT_CURSOR = POINTER_CURSOR = TEXT_CURSOR = None |
| 51 |
|
| 52 |
MAX_NUM_LINES = 50000 |
| 53 |
URL_REGEX = re.compile("(\\w+\\://[^\\s]+)|(www\\.\\w+\\.[^\\s]+)|(mailto\\:[^\\s]+)") |
| 54 |
|
| 55 |
def __init__(self, parent, auto_scroll=False, parse_urls=True, editable=True, |
| 56 |
horizontal_margin=12, vertical_margin=8, pixels_above_lines=1, pixels_below_lines=1): |
| 57 |
|
| 58 |
self.widget = Gtk.TextView( |
| 59 |
accepts_tab=False, editable=editable, |
| 60 |
left_margin=horizontal_margin, right_margin=horizontal_margin, |
| 61 |
top_margin=vertical_margin, bottom_margin=vertical_margin, |
| 62 |
pixels_above_lines=pixels_above_lines, pixels_below_lines=pixels_below_lines, |
| 63 |
wrap_mode=Gtk.WrapMode.WORD_CHAR, visible=True |
| 64 |
) |
| 65 |
|
| 66 |
if GTK_API_VERSION >= 4: |
| 67 |
parent.set_child(self.widget) # pylint: disable=no-member |
| 68 |
else: |
| 69 |
parent.add(self.widget) # pylint: disable=no-member |
| 70 |
|
| 71 |
self.textbuffer = self.widget.get_buffer() |
| 72 |
self.end_iter = self.textbuffer.get_end_iter() |
| 73 |
self.scrollable = self.widget.get_ancestor(Gtk.ScrolledWindow) |
| 74 |
scrollable_container = self.scrollable.get_ancestor(Gtk.Box) |
| 75 |
|
| 76 |
self.adjustment = self.scrollable.get_vadjustment() |
| 77 |
self.auto_scroll = auto_scroll |
| 78 |
self.adjustment_bottom = self.adjustment_value = 0 |
| 79 |
self.notify_upper_handler = self.adjustment.connect("notify::upper", self.on_adjustment_upper_changed) |
| 80 |
self.notify_value_handler = self.adjustment.connect("notify::value", self.on_adjustment_value_changed) |
| 81 |
|
| 82 |
self.pressed_x = self.pressed_y = 0 |
| 83 |
self.type_tags = {} |
| 84 |
self.parse_urls = parse_urls |
| 85 |
|
| 86 |
if GTK_API_VERSION >= 4: |
| 87 |
self.textbuffer.set_enable_undo(editable) |
| 88 |
|
| 89 |
self.gesture_click_primary = Gtk.GestureClick() |
| 90 |
scrollable_container.add_controller(self.gesture_click_primary) |
| 91 |
|
| 92 |
self.gesture_click_secondary = Gtk.GestureClick() |
| 93 |
scrollable_container.add_controller(self.gesture_click_secondary) |
| 94 |
|
| 95 |
self.cursor_window = self.widget |
| 96 |
|
| 97 |
self.motion_controller = Gtk.EventControllerMotion() |
| 98 |
self.motion_controller.connect("motion", self.on_move_cursor) |
| 99 |
self.widget.add_controller(self.motion_controller) # pylint: disable=no-member |
| 100 |
else: |
| 101 |
self.gesture_click_primary = Gtk.GestureMultiPress(widget=scrollable_container) |
| 102 |
self.gesture_click_secondary = Gtk.GestureMultiPress(widget=scrollable_container) |
| 103 |
|
| 104 |
self.cursor_window = None |
| 105 |
|
| 106 |
self.widget.connect("motion-notify-event", self.on_move_cursor_event) |
| 107 |
|
| 108 |
self.gesture_click_primary.set_propagation_phase(Gtk.PropagationPhase.CAPTURE) |
| 109 |
self.gesture_click_primary.connect("released", self.on_released_primary) |
| 110 |
|
| 111 |
self.gesture_click_secondary.set_propagation_phase(Gtk.PropagationPhase.CAPTURE) |
| 112 |
self.gesture_click_secondary.set_button(Gdk.BUTTON_SECONDARY) |
| 113 |
self.gesture_click_secondary.connect("pressed", self.on_pressed_secondary) |
| 114 |
|
| 115 |
def destroy(self): |
| 116 |
|
| 117 |
# Prevent updates while destroying widget |
| 118 |
self.adjustment.disconnect(self.notify_upper_handler) |
| 119 |
self.adjustment.disconnect(self.notify_value_handler) |
| 120 |
|
| 121 |
self.__dict__.clear() |
| 122 |
|
| 123 |
def scroll_bottom(self): |
| 124 |
self.adjustment_value = (self.adjustment.get_upper() - self.adjustment.get_page_size()) |
| 125 |
self.adjustment.set_value(self.adjustment_value) |
| 126 |
|
| 127 |
def _insert_text(self, text, tag=None): |
| 128 |
|
| 129 |
if not text: |
| 130 |
return |
| 131 |
|
| 132 |
if tag is not None: |
| 133 |
start_offset = self.end_iter.get_offset() |
| 134 |
|
| 135 |
self.textbuffer.insert(self.end_iter, text) |
| 136 |
|
| 137 |
if tag is not None: |
| 138 |
start_iter = self.textbuffer.get_iter_at_offset(start_offset) |
| 139 |
self.textbuffer.apply_tag(tag, start_iter, self.end_iter) |
| 140 |
|
| 141 |
def _remove_old_lines(self, num_lines): |
| 142 |
|
| 143 |
if num_lines < self.MAX_NUM_LINES: |
| 144 |
return |
| 145 |
|
| 146 |
# Optimization: remove lines in batches |
| 147 |
start_iter = self.textbuffer.get_start_iter() |
| 148 |
end_line = (num_lines - (self.MAX_NUM_LINES - 1000)) |
| 149 |
end_iter = self.get_iter_at_line(end_line) |
| 150 |
|
| 151 |
self.textbuffer.delete(start_iter, end_iter) |
| 152 |
self.end_iter = self.textbuffer.get_end_iter() |
| 153 |
|
| 154 |
def append_line(self, line, message_type=None, timestamp=None, timestamp_format=None, |
| 155 |
username=None, usertag=None): |
| 156 |
|
| 157 |
tag = self.type_tags.get(message_type) |
| 158 |
num_lines = self.textbuffer.get_line_count() |
| 159 |
line = str(line).strip("\n") |
| 160 |
|
| 161 |
if timestamp_format: |
| 162 |
line = time.strftime(timestamp_format, time.localtime(timestamp)) + " " + line |
| 163 |
|
| 164 |
if self.textbuffer.get_char_count() > 0: |
| 165 |
# No tag applied on line breaks to prevent visual glitch where text on the |
| 166 |
# next line has the wrong color |
| 167 |
self._insert_text("\n") |
| 168 |
|
| 169 |
# Tag usernames with popup menu creating tag, and away/online/offline colors |
| 170 |
if username and username in line: |
| 171 |
start = line.find(username) |
| 172 |
|
| 173 |
self._insert_text(line[:start], tag) |
| 174 |
self._insert_text(username, usertag) |
| 175 |
|
| 176 |
line = line[start + len(username):] |
| 177 |
|
| 178 |
# Highlight urls, if found and tag them |
| 179 |
if self.parse_urls and ("://" in line or "www." in line or "mailto:" in line): |
| 180 |
# Match first url |
| 181 |
match = self.URL_REGEX.search(line) |
| 182 |
|
| 183 |
while match: |
| 184 |
self._insert_text(line[:match.start()], tag) |
| 185 |
|
| 186 |
url = match.group() |
| 187 |
urltag = self.create_tag("urlcolor", url=url) |
| 188 |
self._insert_text(url, urltag) |
| 189 |
|
| 190 |
# Match remaining url |
| 191 |
line = line[match.end():] |
| 192 |
match = self.URL_REGEX.search(line) |
| 193 |
|
| 194 |
self._insert_text(line, tag) |
| 195 |
self._remove_old_lines(num_lines) |
| 196 |
|
| 197 |
def get_iter_at_line(self, line_number): |
| 198 |
|
| 199 |
iterator = self.textbuffer.get_iter_at_line(line_number) |
| 200 |
|
| 201 |
if GTK_API_VERSION >= 4: |
| 202 |
_position_found, iterator = iterator |
| 203 |
|
| 204 |
return iterator |
| 205 |
|
| 206 |
def get_has_selection(self): |
| 207 |
return self.textbuffer.get_has_selection() |
| 208 |
|
| 209 |
def get_text(self): |
| 210 |
|
| 211 |
start_iter = self.textbuffer.get_start_iter() |
| 212 |
self.end_iter = self.textbuffer.get_end_iter() |
| 213 |
|
| 214 |
return self.textbuffer.get_text(start_iter, self.end_iter, include_hidden_chars=True) |
| 215 |
|
| 216 |
def get_tags_for_pos(self, pos_x, pos_y): |
| 217 |
|
| 218 |
buf_x, buf_y = self.widget.window_to_buffer_coords(Gtk.TextWindowType.WIDGET, pos_x, pos_y) |
| 219 |
over_text, iterator, _trailing = self.widget.get_iter_at_position(buf_x, buf_y) |
| 220 |
|
| 221 |
if not over_text: |
| 222 |
# Iterators are returned for whitespace after the last character, avoid accidental URL clicks |
| 223 |
return [] |
| 224 |
|
| 225 |
return iterator.get_tags() |
| 226 |
|
| 227 |
def get_url_for_current_pos(self): |
| 228 |
|
| 229 |
for tag in self.get_tags_for_pos(self.pressed_x, self.pressed_y): |
| 230 |
if hasattr(tag, "url"): |
| 231 |
return tag.url |
| 232 |
|
| 233 |
return "" |
| 234 |
|
| 235 |
def grab_focus(self): |
| 236 |
self.widget.grab_focus() |
| 237 |
|
| 238 |
def place_cursor_at_line(self, line_number): |
| 239 |
iterator = self.get_iter_at_line(line_number) |
| 240 |
self.textbuffer.place_cursor(iterator) |
| 241 |
|
| 242 |
def set_text(self, text): |
| 243 |
"""Sets text without any additional processing, and clears the undo stack.""" |
| 244 |
|
| 245 |
self.textbuffer.set_text(text) |
| 246 |
self.end_iter = self.textbuffer.get_end_iter() |
| 247 |
|
| 248 |
def update_cursor(self, pos_x, pos_y): |
| 249 |
|
| 250 |
cursor = self.TEXT_CURSOR |
| 251 |
|
| 252 |
if self.cursor_window is None: |
| 253 |
self.cursor_window = self.widget.get_window(Gtk.TextWindowType.TEXT) # pylint: disable=no-member |
| 254 |
|
| 255 |
for tag in self.get_tags_for_pos(pos_x, pos_y): |
| 256 |
if hasattr(tag, "username"): |
| 257 |
cursor = self.DEFAULT_CURSOR |
| 258 |
break |
| 259 |
|
| 260 |
if hasattr(tag, "url"): |
| 261 |
cursor = self.POINTER_CURSOR |
| 262 |
break |
| 263 |
|
| 264 |
if cursor != self.cursor_window.get_cursor(): |
| 265 |
self.cursor_window.set_cursor(cursor) |
| 266 |
|
| 267 |
def clear(self): |
| 268 |
self.set_text("") |
| 269 |
|
| 270 |
# Text Tags (Usernames, URLs) # |
| 271 |
|
| 272 |
def create_tag(self, color_id=None, callback=None, username=None, url=None): |
| 273 |
|
| 274 |
tag = self.textbuffer.create_tag() |
| 275 |
|
| 276 |
if color_id: |
| 277 |
update_tag_visuals(tag, color_id=color_id) |
| 278 |
tag.color_id = color_id |
| 279 |
|
| 280 |
if url: |
| 281 |
if url.startswith("www."): |
| 282 |
url = "http://" + url |
| 283 |
|
| 284 |
tag.url = url |
| 285 |
|
| 286 |
if username: |
| 287 |
tag.callback = callback |
| 288 |
tag.username = username |
| 289 |
|
| 290 |
return tag |
| 291 |
|
| 292 |
def update_tag(self, tag, color_id=None): |
| 293 |
|
| 294 |
if color_id is not None: |
| 295 |
tag.color_id = color_id |
| 296 |
|
| 297 |
update_tag_visuals(tag, color_id=tag.color_id) |
| 298 |
|
| 299 |
def update_tags(self): |
| 300 |
self.textbuffer.get_tag_table().foreach(self.update_tag) |
| 301 |
|
| 302 |
# Events # |
| 303 |
|
| 304 |
def on_released_primary(self, _controller, _num_p, pressed_x, pressed_y): |
| 305 |
|
| 306 |
self.pressed_x = pressed_x |
| 307 |
self.pressed_y = pressed_y |
| 308 |
|
| 309 |
if self.textbuffer.get_has_selection(): |
| 310 |
return False |
| 311 |
|
| 312 |
for tag in self.get_tags_for_pos(pressed_x, pressed_y): |
| 313 |
if hasattr(tag, "url"): |
| 314 |
open_uri(tag.url) |
| 315 |
return True |
| 316 |
|
| 317 |
if hasattr(tag, "username"): |
| 318 |
tag.callback(pressed_x, pressed_y, tag.username) |
| 319 |
return True |
| 320 |
|
| 321 |
return False |
| 322 |
|
| 323 |
def on_pressed_secondary(self, _controller, _num_p, pressed_x, pressed_y): |
| 324 |
|
| 325 |
self.pressed_x = pressed_x |
| 326 |
self.pressed_y = pressed_y |
| 327 |
|
| 328 |
if self.textbuffer.get_has_selection(): |
| 329 |
return False |
| 330 |
|
| 331 |
for tag in self.get_tags_for_pos(pressed_x, pressed_y): |
| 332 |
if hasattr(tag, "username"): |
| 333 |
tag.callback(pressed_x, pressed_y, tag.username) |
| 334 |
return True |
| 335 |
|
| 336 |
return False |
| 337 |
|
| 338 |
def on_move_cursor(self, _controller, pos_x, pos_y): |
| 339 |
self.update_cursor(pos_x, pos_y) |
| 340 |
|
| 341 |
def on_move_cursor_event(self, _widget, event): |
| 342 |
self.update_cursor(event.x, event.y) |
| 343 |
|
| 344 |
def on_copy_text(self, *_args): |
| 345 |
self.widget.emit("copy-clipboard") |
| 346 |
|
| 347 |
def on_copy_link(self, *_args): |
| 348 |
clipboard.copy_text(self.get_url_for_current_pos()) |
| 349 |
|
| 350 |
def on_copy_all_text(self, *_args): |
| 351 |
clipboard.copy_text(self.get_text()) |
| 352 |
|
| 353 |
def on_clear_all_text(self, *_args): |
| 354 |
self.clear() |
| 355 |
|
| 356 |
def on_adjustment_upper_changed(self, *_args): |
| 357 |
|
| 358 |
new_adjustment_bottom = (self.adjustment.get_upper() - self.adjustment.get_page_size()) |
| 359 |
|
| 360 |
if self.auto_scroll and self.adjustment_value >= self.adjustment_bottom: |
| 361 |
# Scroll to bottom if we were at the bottom previously |
| 362 |
self.scroll_bottom() |
| 363 |
|
| 364 |
self.adjustment_bottom = new_adjustment_bottom |
| 365 |
|
| 366 |
def on_adjustment_value_changed(self, *_args): |
| 367 |
|
| 368 |
new_value = self.adjustment.get_value() |
| 369 |
|
| 370 |
if new_value.is_integer() and (0 < new_value < self.adjustment_bottom): |
| 371 |
# The textview scrolls up on its own sometimes. Ignore these garbage values. |
| 372 |
return |
| 373 |
|
| 374 |
self.adjustment_value = new_value |
| 375 |
|
| 376 |
|
| 377 |
class ChatView(TextView): |
| 378 |
|
| 379 |
def __init__(self, *args, chat_entry=None, status_users=None, username_event=None, **kwargs): |
| 380 |
|
| 381 |
super().__init__(*args, **kwargs) |
| 382 |
|
| 383 |
self.user_tags = self.status_users = {} |
| 384 |
self.chat_entry = chat_entry |
| 385 |
self.username_event = username_event |
| 386 |
|
| 387 |
if status_users is not None: |
| 388 |
# In chatrooms, we only want to set the online status for users that are |
| 389 |
# currently in the room, even though we might know their global status |
| 390 |
self.status_users = status_users |
| 391 |
|
| 392 |
self.type_tags = { |
| 393 |
"remote": self.create_tag("chatremote"), |
| 394 |
"local": self.create_tag("chatlocal"), |
| 395 |
"command": self.create_tag("chatcommand"), |
| 396 |
"action": self.create_tag("chatme"), |
| 397 |
"hilite": self.create_tag("chathilite") |
| 398 |
} |
| 399 |
|
| 400 |
Accelerator("Down", self.widget, self.on_page_down_accelerator) |
| 401 |
Accelerator("Page_Down", self.widget, self.on_page_down_accelerator) |
| 402 |
|
| 403 |
def append_log_lines(self, log_lines, login_username=None): |
| 404 |
|
| 405 |
if not log_lines: |
| 406 |
return |
| 407 |
|
| 408 |
login_username_lower = login_username.lower() if login_username else None |
| 409 |
|
| 410 |
for log_line in log_lines: |
| 411 |
try: |
| 412 |
line = log_line.decode("utf-8") |
| 413 |
|
| 414 |
except UnicodeDecodeError: |
| 415 |
line = log_line.decode("latin-1") |
| 416 |
|
| 417 |
user = message_type = usertag = None |
| 418 |
|
| 419 |
if " [" in line and "] " in line: |
| 420 |
start = line.find(" [") + 2 |
| 421 |
end = line.find("] ", start) |
| 422 |
|
| 423 |
if end > start: |
| 424 |
user = line[start:end] |
| 425 |
usertag = self.get_user_tag(user) |
| 426 |
|
| 427 |
text = line[end + 2:-1] |
| 428 |
|
| 429 |
if user == login_username: |
| 430 |
message_type = "local" |
| 431 |
|
| 432 |
elif login_username_lower and find_whole_word(login_username_lower, text.lower()) > -1: |
| 433 |
message_type = "hilite" |
| 434 |
|
| 435 |
else: |
| 436 |
message_type = "remote" |
| 437 |
|
| 438 |
elif "* " in line: |
| 439 |
message_type = "action" |
| 440 |
|
| 441 |
self.append_line(line, message_type=message_type, username=user, usertag=usertag) |
| 442 |
|
| 443 |
self.append_line(_("--- old messages above ---"), message_type="hilite") |
| 444 |
|
| 445 |
def clear(self): |
| 446 |
super().clear() |
| 447 |
self.user_tags.clear() |
| 448 |
|
| 449 |
def get_user_tag(self, username): |
| 450 |
|
| 451 |
if username not in self.user_tags: |
| 452 |
self.user_tags[username] = self.create_tag(callback=self.username_event, username=username) |
| 453 |
self.update_user_tag(username) |
| 454 |
|
| 455 |
return self.user_tags[username] |
| 456 |
|
| 457 |
def update_user_tag(self, username): |
| 458 |
|
| 459 |
if username not in self.user_tags: |
| 460 |
return |
| 461 |
|
| 462 |
status = UserStatus.OFFLINE |
| 463 |
|
| 464 |
if username in self.status_users: |
| 465 |
status = core.users.statuses.get(username, UserStatus.OFFLINE) |
| 466 |
|
| 467 |
color = USER_STATUS_COLORS.get(status) |
| 468 |
self.update_tag(self.user_tags[username], color) |
| 469 |
|
| 470 |
def update_user_tags(self): |
| 471 |
for username in self.user_tags: |
| 472 |
self.update_user_tag(username) |
| 473 |
|
| 474 |
def on_page_down_accelerator(self, *_args): |
| 475 |
"""Page_Down, Down: Give focus to text entry if already scrolled at the |
| 476 |
bottom.""" |
| 477 |
|
| 478 |
if self.textbuffer.props.cursor_position >= self.textbuffer.get_char_count(): |
| 479 |
# Give focus to text entry upon scrolling down to the bottom |
| 480 |
self.chat_entry.grab_focus_without_selecting() |