| 1 |
# COPYRIGHT (C) 2020-2023 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 |
from gi.repository import GObject |
| 20 |
from gi.repository import Gtk |
| 21 |
from gi.repository import Pango |
| 22 |
|
| 23 |
from pynicotine.config import config |
| 24 |
from pynicotine.core import core |
| 25 |
from pynicotine.events import events |
| 26 |
from pynicotine.gtkgui.application import GTK_API_VERSION |
| 27 |
from pynicotine.gtkgui.widgets import ui |
| 28 |
from pynicotine.gtkgui.widgets.accelerator import Accelerator |
| 29 |
from pynicotine.gtkgui.widgets.popover import Popover |
| 30 |
from pynicotine.gtkgui.widgets.popupmenu import PopupMenu |
| 31 |
from pynicotine.gtkgui.widgets.textentry import CompletionEntry |
| 32 |
from pynicotine.gtkgui.widgets.theme import add_css_class |
| 33 |
from pynicotine.gtkgui.widgets.treeview import TreeView |
| 34 |
from pynicotine.utils import humanize |
| 35 |
|
| 36 |
|
| 37 |
class RoomList(Popover): |
| 38 |
|
| 39 |
PRIVATE_USERS_OFFSET = 10000000 |
| 40 |
|
| 41 |
def __init__(self, window): |
| 42 |
|
| 43 |
( |
| 44 |
self.container, |
| 45 |
self.list_container, |
| 46 |
self.private_room_toggle, |
| 47 |
self.public_feed_toggle, |
| 48 |
self.refresh_button, |
| 49 |
self.search_entry |
| 50 |
) = ui.load(scope=self, path="popovers/roomlist.ui") |
| 51 |
|
| 52 |
super().__init__( |
| 53 |
window=window, |
| 54 |
content_box=self.container, |
| 55 |
width=450, |
| 56 |
height=500 |
| 57 |
) |
| 58 |
|
| 59 |
self.list_view = TreeView( |
| 60 |
window, parent=self.list_container, |
| 61 |
activate_row_callback=self.on_row_activated, search_entry=self.search_entry, |
| 62 |
columns={ |
| 63 |
# Visible columns |
| 64 |
"room": { |
| 65 |
"column_type": "text", |
| 66 |
"title": _("Room"), |
| 67 |
"width": 260, |
| 68 |
"expand_column": True, |
| 69 |
"text_underline_column": "room_underline_data", |
| 70 |
"text_weight_column": "room_weight_data" |
| 71 |
}, |
| 72 |
"users": { |
| 73 |
"column_type": "number", |
| 74 |
"title": _("Users"), |
| 75 |
"sort_column": "users_data", |
| 76 |
"default_sort_type": "descending" |
| 77 |
}, |
| 78 |
|
| 79 |
# Hidden data columns |
| 80 |
"users_data": {"data_type": GObject.TYPE_UINT}, |
| 81 |
"is_private_data": {"data_type": GObject.TYPE_BOOLEAN}, |
| 82 |
"room_weight_data": {"data_type": Pango.Weight}, |
| 83 |
"room_underline_data": {"data_type": Pango.Underline} |
| 84 |
} |
| 85 |
) |
| 86 |
|
| 87 |
self.popup_room = None |
| 88 |
self.popup_menu = PopupMenu(window.application, self.list_view.widget, self.on_popup_menu) |
| 89 |
self.popup_menu.add_items( |
| 90 |
("=" + _("Join Room"), self.on_popup_join), |
| 91 |
("=" + _("Leave Room"), self.on_popup_leave), |
| 92 |
("", None), |
| 93 |
("=" + _("Disown Private Room"), self.on_popup_private_room_disown), |
| 94 |
("=" + _("Cancel Room Membership"), self.on_popup_private_room_cancel_membership) |
| 95 |
) |
| 96 |
|
| 97 |
for toggle in (self.public_feed_toggle, self.private_room_toggle): |
| 98 |
parent = next(iter(toggle.get_parent())) |
| 99 |
|
| 100 |
if GTK_API_VERSION >= 4: |
| 101 |
parent.gesture_click = Gtk.GestureClick() |
| 102 |
parent.add_controller(parent.gesture_click) |
| 103 |
else: |
| 104 |
parent.set_has_window(True) |
| 105 |
parent.gesture_click = Gtk.GestureMultiPress(widget=parent) |
| 106 |
|
| 107 |
parent.gesture_click.connect("released", self.on_toggle_label_pressed, toggle) |
| 108 |
|
| 109 |
self.private_room_toggle.set_active(config.sections["server"]["private_chatrooms"]) |
| 110 |
self.private_room_toggle.connect("notify::active", self.on_toggle_accept_private_room) |
| 111 |
|
| 112 |
Accelerator("<Primary>f", self.widget, self.on_search_accelerator) |
| 113 |
self.completion_entry = CompletionEntry(window.chatrooms_entry, self.list_view.model, column=0) |
| 114 |
|
| 115 |
if GTK_API_VERSION >= 4: |
| 116 |
inner_button = next(iter(window.room_list_button)) |
| 117 |
add_css_class(widget=inner_button, css_class="arrow-button") |
| 118 |
|
| 119 |
self.set_menu_button(window.room_list_button) |
| 120 |
|
| 121 |
for event_name, callback in ( |
| 122 |
("join-room", self.join_room), |
| 123 |
("private-room-added", self.private_room_added), |
| 124 |
("remove-room", self.remove_room), |
| 125 |
("room-list", self.room_list), |
| 126 |
("server-disconnect", self.clear), |
| 127 |
("show-room", self.show_room), |
| 128 |
("user-joined-room", self.user_joined_room), |
| 129 |
("user-left-room", self.user_left_room) |
| 130 |
): |
| 131 |
events.connect(event_name, callback) |
| 132 |
|
| 133 |
def destroy(self): |
| 134 |
|
| 135 |
self.list_view.destroy() |
| 136 |
self.popup_menu.destroy() |
| 137 |
self.completion_entry.destroy() |
| 138 |
|
| 139 |
super().destroy() |
| 140 |
|
| 141 |
def get_selected_room(self): |
| 142 |
|
| 143 |
for iterator in self.list_view.get_selected_rows(): |
| 144 |
return self.list_view.get_row_value(iterator, "room") |
| 145 |
|
| 146 |
return None |
| 147 |
|
| 148 |
def toggle_accept_private_room(self, active): |
| 149 |
self.private_room_toggle.set_active(active) |
| 150 |
|
| 151 |
def add_room(self, room, user_count=0, is_private=False, is_owned=False): |
| 152 |
|
| 153 |
h_user_count = humanize(user_count) |
| 154 |
|
| 155 |
if is_private: |
| 156 |
# Large internal value to sort private rooms first |
| 157 |
user_count += self.PRIVATE_USERS_OFFSET |
| 158 |
|
| 159 |
text_weight = Pango.Weight.BOLD if is_private else Pango.Weight.NORMAL |
| 160 |
text_underline = Pango.Underline.SINGLE if is_owned else Pango.Underline.NONE |
| 161 |
|
| 162 |
self.list_view.add_row([ |
| 163 |
room, |
| 164 |
h_user_count, |
| 165 |
user_count, |
| 166 |
is_private, |
| 167 |
text_weight, |
| 168 |
text_underline |
| 169 |
], select_row=False) |
| 170 |
|
| 171 |
def update_room_user_count(self, room, user_count=None, decrement=False): |
| 172 |
|
| 173 |
iterator = self.list_view.iterators.get(room) |
| 174 |
|
| 175 |
if iterator is None: |
| 176 |
return |
| 177 |
|
| 178 |
is_private = self.list_view.get_row_value(iterator, "is_private_data") |
| 179 |
|
| 180 |
if user_count is None: |
| 181 |
user_count = self.list_view.get_row_value(iterator, "users_data") |
| 182 |
|
| 183 |
if decrement: |
| 184 |
if user_count > 0: |
| 185 |
user_count -= 1 |
| 186 |
else: |
| 187 |
user_count += 1 |
| 188 |
|
| 189 |
elif is_private: |
| 190 |
# Large internal value to sort private rooms first |
| 191 |
user_count += self.PRIVATE_USERS_OFFSET |
| 192 |
|
| 193 |
h_user_count = humanize(user_count - self.PRIVATE_USERS_OFFSET) if is_private else humanize(user_count) |
| 194 |
|
| 195 |
self.list_view.set_row_values( |
| 196 |
iterator, |
| 197 |
column_ids=["users", "users_data"], |
| 198 |
values=[h_user_count, user_count] |
| 199 |
) |
| 200 |
|
| 201 |
def clear(self, *_args): |
| 202 |
self.list_view.clear() |
| 203 |
|
| 204 |
def private_room_added(self, msg): |
| 205 |
self.add_room(msg.room, is_private=True) |
| 206 |
|
| 207 |
def join_room(self, msg): |
| 208 |
|
| 209 |
room = msg.room |
| 210 |
|
| 211 |
if room not in core.chatrooms.joined_rooms: |
| 212 |
return |
| 213 |
|
| 214 |
user_count = len(msg.users) |
| 215 |
|
| 216 |
if room not in self.list_view.iterators: |
| 217 |
self.add_room( |
| 218 |
room, user_count, is_private=msg.private, |
| 219 |
is_owned=(msg.owner == core.users.login_username) |
| 220 |
) |
| 221 |
|
| 222 |
self.update_room_user_count(room, user_count=user_count) |
| 223 |
|
| 224 |
def show_room(self, room, *_args): |
| 225 |
if room == core.chatrooms.GLOBAL_ROOM_NAME: |
| 226 |
self.public_feed_toggle.set_active(True) |
| 227 |
|
| 228 |
def remove_room(self, room): |
| 229 |
|
| 230 |
if room == core.chatrooms.GLOBAL_ROOM_NAME: |
| 231 |
self.public_feed_toggle.set_active(False) |
| 232 |
|
| 233 |
self.update_room_user_count(room, decrement=True) |
| 234 |
|
| 235 |
def user_joined_room(self, msg): |
| 236 |
if msg.userdata.username != core.users.login_username: |
| 237 |
self.update_room_user_count(msg.room) |
| 238 |
|
| 239 |
def user_left_room(self, msg): |
| 240 |
if msg.username != core.users.login_username: |
| 241 |
self.update_room_user_count(msg.room, decrement=True) |
| 242 |
|
| 243 |
def room_list(self, msg): |
| 244 |
|
| 245 |
self.list_view.freeze() |
| 246 |
self.clear() |
| 247 |
|
| 248 |
for room, user_count in msg.ownedprivaterooms: |
| 249 |
self.add_room(room, user_count, is_private=True, is_owned=True) |
| 250 |
|
| 251 |
for room, user_count in msg.otherprivaterooms: |
| 252 |
self.add_room(room, user_count, is_private=True) |
| 253 |
|
| 254 |
for room, user_count in msg.rooms: |
| 255 |
self.add_room(room, user_count) |
| 256 |
|
| 257 |
self.list_view.unfreeze() |
| 258 |
|
| 259 |
def on_row_activated(self, *_args): |
| 260 |
|
| 261 |
room = self.get_selected_room() |
| 262 |
|
| 263 |
if room is not None: |
| 264 |
self.popup_room = room |
| 265 |
self.on_popup_join() |
| 266 |
|
| 267 |
def on_popup_menu(self, menu, _widget): |
| 268 |
|
| 269 |
room = self.get_selected_room() |
| 270 |
self.popup_room = room |
| 271 |
|
| 272 |
is_private_room_owned = core.chatrooms.is_private_room_owned(room) |
| 273 |
is_private_room_member = core.chatrooms.is_private_room_member(room) |
| 274 |
|
| 275 |
menu.actions[_("Join Room")].set_enabled(room not in core.chatrooms.joined_rooms) |
| 276 |
menu.actions[_("Leave Room")].set_enabled(room in core.chatrooms.joined_rooms) |
| 277 |
|
| 278 |
menu.actions[_("Disown Private Room")].set_enabled(is_private_room_owned) |
| 279 |
menu.actions[_("Cancel Room Membership")].set_enabled(is_private_room_member and not is_private_room_owned) |
| 280 |
|
| 281 |
def on_popup_join(self, *_args): |
| 282 |
core.chatrooms.show_room(self.popup_room) |
| 283 |
self.close(use_transition=False) |
| 284 |
|
| 285 |
def on_toggle_label_pressed(self, _controller, _num_p, _pos_x, _pos_y, toggle): |
| 286 |
toggle.emit("activate") |
| 287 |
|
| 288 |
def on_toggle_public_feed(self, *_args): |
| 289 |
|
| 290 |
global_room_name = core.chatrooms.GLOBAL_ROOM_NAME |
| 291 |
|
| 292 |
if self.public_feed_toggle.get_active(): |
| 293 |
if global_room_name not in core.chatrooms.joined_rooms: |
| 294 |
core.chatrooms.show_room(global_room_name) |
| 295 |
|
| 296 |
self.close(use_transition=False) |
| 297 |
return |
| 298 |
|
| 299 |
core.chatrooms.remove_room(global_room_name) |
| 300 |
|
| 301 |
def on_popup_private_room_disown(self, *_args): |
| 302 |
core.chatrooms.request_private_room_disown(self.popup_room) |
| 303 |
|
| 304 |
def on_popup_private_room_cancel_membership(self, *_args): |
| 305 |
core.chatrooms.request_private_room_cancel_membership(self.popup_room) |
| 306 |
|
| 307 |
def on_popup_leave(self, *_args): |
| 308 |
core.chatrooms.remove_room(self.popup_room) |
| 309 |
|
| 310 |
def on_refresh(self, *_args): |
| 311 |
core.chatrooms.request_room_list() |
| 312 |
|
| 313 |
def on_toggle_accept_private_room(self, *_args): |
| 314 |
active = config.sections["server"]["private_chatrooms"] = self.private_room_toggle.get_active() |
| 315 |
core.chatrooms.request_private_room_toggle(active) |
| 316 |
|
| 317 |
def on_search_accelerator(self, *_args): |
| 318 |
"""Ctrl+F - Search rooms.""" |
| 319 |
|
| 320 |
self.search_entry.grab_focus() |
| 321 |
return True |