| 1 |
# COPYRIGHT (C) 2020-2025 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 os |
| 20 |
import sys |
| 21 |
|
| 22 |
from gi.repository import GdkPixbuf |
| 23 |
from gi.repository import Gio |
| 24 |
from gi.repository import GLib |
| 25 |
from gi.repository import Gtk |
| 26 |
|
| 27 |
import pynicotine |
| 28 |
from pynicotine.config import config |
| 29 |
from pynicotine.core import core |
| 30 |
from pynicotine.gtkgui.application import GTK_API_VERSION |
| 31 |
from pynicotine.gtkgui.application import GTK_GUI_FOLDER_PATH |
| 32 |
from pynicotine.gtkgui.widgets.theme import ICON_THEME |
| 33 |
from pynicotine.gtkgui.widgets.window import Window |
| 34 |
from pynicotine.logfacility import log |
| 35 |
from pynicotine.slskmessages import UserStatus |
| 36 |
from pynicotine.utils import encode_path |
| 37 |
from pynicotine.utils import truncate_string_byte |
| 38 |
|
| 39 |
|
| 40 |
class ImplementationUnavailable(Exception): |
| 41 |
pass |
| 42 |
|
| 43 |
|
| 44 |
class BaseImplementation: |
| 45 |
|
| 46 |
def __init__(self, application): |
| 47 |
|
| 48 |
self.application = application |
| 49 |
self.menu_items = {} |
| 50 |
self.menu_item_id = 1 |
| 51 |
self.activate_callback = application.on_window_hide_unhide |
| 52 |
self.is_visible = True |
| 53 |
|
| 54 |
self._create_menu() |
| 55 |
|
| 56 |
def _create_item(self, text=None, callback=None, check=False): |
| 57 |
|
| 58 |
item = {"id": self.menu_item_id, "visible": True} |
| 59 |
|
| 60 |
if text is not None: |
| 61 |
item["text"] = text |
| 62 |
|
| 63 |
if callback is not None: |
| 64 |
item["callback"] = callback |
| 65 |
|
| 66 |
if check: |
| 67 |
item["toggled"] = False |
| 68 |
|
| 69 |
self.menu_items[self.menu_item_id] = item |
| 70 |
self.menu_item_id += 1 |
| 71 |
|
| 72 |
return item |
| 73 |
|
| 74 |
def _set_item_text(self, item, text): |
| 75 |
item["text"] = text |
| 76 |
|
| 77 |
def _set_item_visible(self, item, visible): |
| 78 |
item["visible"] = visible |
| 79 |
|
| 80 |
def _set_item_toggled(self, item, toggled): |
| 81 |
item["toggled"] = toggled |
| 82 |
|
| 83 |
def _create_menu(self): |
| 84 |
|
| 85 |
self.show_hide_item = self._create_item("placeholder", self.application.on_window_hide_unhide) |
| 86 |
|
| 87 |
self._create_item() |
| 88 |
|
| 89 |
self.downloads_item = self._create_item(_("Downloads"), self.application.on_downloads) |
| 90 |
self.uploads_item = self._create_item(_("Uploads"), self.application.on_uploads) |
| 91 |
|
| 92 |
self._create_item() |
| 93 |
|
| 94 |
self._create_item(_("Private Chat"), self.application.on_private_chat) |
| 95 |
self._create_item(_("Chat Rooms"), self.application.on_chat_rooms) |
| 96 |
self._create_item(_("Searches"), self.application.on_searches) |
| 97 |
|
| 98 |
self._create_item() |
| 99 |
|
| 100 |
self.away_item = self._create_item(_("Away"), self.application.on_away, check=True) |
| 101 |
self.connect_item = self._create_item(_("_Connect"), self.application.on_connect) |
| 102 |
self.disconnect_item = self._create_item(_("_Disconnect"), self.application.on_disconnect) |
| 103 |
|
| 104 |
self._create_item() |
| 105 |
|
| 106 |
self._create_item(_("Preferences"), self.application.on_preferences) |
| 107 |
self._create_item(_("_Quit"), self.application.on_quit_request) |
| 108 |
|
| 109 |
def _update_window_visibility(self): |
| 110 |
|
| 111 |
if self.application.window is None: |
| 112 |
return |
| 113 |
|
| 114 |
label = _("Hide Nicotine+") if self.application.window.is_visible() else _("Show Nicotine+") |
| 115 |
|
| 116 |
if self.show_hide_item.get("text") != label: |
| 117 |
self._set_item_text(self.show_hide_item, label) |
| 118 |
|
| 119 |
def _update_user_status(self): |
| 120 |
|
| 121 |
should_update = False |
| 122 |
connect_visible = core.users.login_status == UserStatus.OFFLINE |
| 123 |
away_visible = not connect_visible |
| 124 |
away_toggled = core.users.login_status == UserStatus.AWAY |
| 125 |
|
| 126 |
if self.connect_item.get("visible") != connect_visible: |
| 127 |
self._set_item_visible(self.connect_item, connect_visible) |
| 128 |
should_update = True |
| 129 |
|
| 130 |
if self.disconnect_item.get("visible") == connect_visible: |
| 131 |
self._set_item_visible(self.disconnect_item, not connect_visible) |
| 132 |
should_update = True |
| 133 |
|
| 134 |
if self.away_item.get("visible") != away_visible: |
| 135 |
self._set_item_visible(self.away_item, away_visible) |
| 136 |
should_update = True |
| 137 |
|
| 138 |
if self.away_item.get("toggled") != away_toggled: |
| 139 |
self._set_item_toggled(self.away_item, away_toggled) |
| 140 |
should_update = True |
| 141 |
|
| 142 |
if should_update: |
| 143 |
self._update_icon() |
| 144 |
|
| 145 |
def _update_icon(self): |
| 146 |
|
| 147 |
# Check for highlights, and display highlight icon if there is a highlighted room or private chat |
| 148 |
if (self.application.window |
| 149 |
and (self.application.window.chatrooms.highlighted_rooms |
| 150 |
or self.application.window.privatechat.highlighted_users)): |
| 151 |
icon_name = "msg" |
| 152 |
|
| 153 |
elif core.users.login_status == UserStatus.ONLINE: |
| 154 |
icon_name = "connect" |
| 155 |
|
| 156 |
elif core.users.login_status == UserStatus.AWAY: |
| 157 |
icon_name = "away" |
| 158 |
|
| 159 |
else: |
| 160 |
icon_name = "disconnect" |
| 161 |
|
| 162 |
icon_name = f"{pynicotine.__application_id__}-{icon_name}" |
| 163 |
self._set_icon_name(icon_name) |
| 164 |
|
| 165 |
def _set_icon_name(self, icon_name): |
| 166 |
# Implemented in subclasses |
| 167 |
pass |
| 168 |
|
| 169 |
def _update_icon_theme(self): |
| 170 |
# Implemented in subclasses |
| 171 |
pass |
| 172 |
|
| 173 |
def update(self): |
| 174 |
|
| 175 |
self.is_visible = config.sections["ui"]["trayicon"] |
| 176 |
|
| 177 |
self._update_icon_theme() |
| 178 |
self._update_icon() |
| 179 |
self._update_window_visibility() |
| 180 |
self._update_user_status() |
| 181 |
|
| 182 |
def set_download_status(self, status): |
| 183 |
self._set_item_text(self.downloads_item, status) |
| 184 |
|
| 185 |
def set_upload_status(self, status): |
| 186 |
self._set_item_text(self.uploads_item, status) |
| 187 |
|
| 188 |
def show_notification(self, title, message, action=None, action_target=None, high_priority=False): |
| 189 |
# Implemented in subclasses |
| 190 |
pass |
| 191 |
|
| 192 |
def unload(self, is_shutdown=True): |
| 193 |
# Implemented in subclasses |
| 194 |
pass |
| 195 |
|
| 196 |
|
| 197 |
class StatusNotifierImplementation(BaseImplementation): |
| 198 |
|
| 199 |
class DBusProperty: |
| 200 |
|
| 201 |
def __init__(self, name, signature, value): |
| 202 |
|
| 203 |
self.name = name |
| 204 |
self.signature = signature |
| 205 |
self.value = value |
| 206 |
|
| 207 |
class DBusSignal: |
| 208 |
|
| 209 |
def __init__(self, name, args): |
| 210 |
self.name = name |
| 211 |
self.args = args |
| 212 |
|
| 213 |
class DBusMethod: |
| 214 |
|
| 215 |
def __init__(self, name, in_args, out_args, callback): |
| 216 |
|
| 217 |
self.name = name |
| 218 |
self.in_args = in_args |
| 219 |
self.out_args = out_args |
| 220 |
self.callback = callback |
| 221 |
|
| 222 |
class DBusService: |
| 223 |
|
| 224 |
def __init__(self, interface_name, object_path, bus_type): |
| 225 |
|
| 226 |
self._interface_name = interface_name |
| 227 |
self._object_path = object_path |
| 228 |
|
| 229 |
self._bus = Gio.bus_get_sync(bus_type) |
| 230 |
self._registration_id = None |
| 231 |
self.properties = {} |
| 232 |
self.signals = {} |
| 233 |
self.methods = {} |
| 234 |
|
| 235 |
def register(self): |
| 236 |
|
| 237 |
xml_output = f"<node name='/'><interface name='{self._interface_name}'>" |
| 238 |
|
| 239 |
for property_name, prop in self.properties.items(): |
| 240 |
xml_output += f"<property name='{property_name}' type='{prop.signature}' access='read'/>" |
| 241 |
|
| 242 |
for method_name, method in self.methods.items(): |
| 243 |
xml_output += f"<method name='{method_name}'>" |
| 244 |
|
| 245 |
for in_signature in method.in_args: |
| 246 |
xml_output += f"<arg type='{in_signature}' direction='in'/>" |
| 247 |
for out_signature in method.out_args: |
| 248 |
xml_output += f"<arg type='{out_signature}' direction='out'/>" |
| 249 |
|
| 250 |
xml_output += "</method>" |
| 251 |
|
| 252 |
for signal_name, signal in self.signals.items(): |
| 253 |
xml_output += f"<signal name='{signal_name}'>" |
| 254 |
|
| 255 |
for signature in signal.args: |
| 256 |
xml_output += f"<arg type='{signature}'/>" |
| 257 |
|
| 258 |
xml_output += "</signal>" |
| 259 |
|
| 260 |
xml_output += "</interface></node>" |
| 261 |
|
| 262 |
registration_id = self._bus.register_object( |
| 263 |
object_path=self._object_path, |
| 264 |
interface_info=Gio.DBusNodeInfo.new_for_xml(xml_output).interfaces[0], |
| 265 |
method_call_closure=self.on_method_call, |
| 266 |
get_property_closure=self.on_get_property, |
| 267 |
set_property_closure=None |
| 268 |
) |
| 269 |
|
| 270 |
if not registration_id: |
| 271 |
raise GLib.Error(f"Failed to register object with path {self._object_path}") |
| 272 |
|
| 273 |
self._registration_id = registration_id |
| 274 |
|
| 275 |
def unregister(self): |
| 276 |
|
| 277 |
if self._registration_id is None: |
| 278 |
return |
| 279 |
|
| 280 |
self._bus.unregister_object(self._registration_id) |
| 281 |
self._registration_id = None |
| 282 |
|
| 283 |
def add_property(self, name, signature, value): |
| 284 |
self.properties[name] = StatusNotifierImplementation.DBusProperty(name, signature, value) |
| 285 |
|
| 286 |
def add_signal(self, name, args): |
| 287 |
self.signals[name] = StatusNotifierImplementation.DBusSignal(name, args) |
| 288 |
|
| 289 |
def add_method(self, name, in_args, out_args, callback): |
| 290 |
self.methods[name] = StatusNotifierImplementation.DBusMethod(name, in_args, out_args, callback) |
| 291 |
|
| 292 |
def _emit_signal(self, name, parameters=None): |
| 293 |
|
| 294 |
self._bus.emit_signal( |
| 295 |
destination_bus_name=None, |
| 296 |
object_path=self._object_path, |
| 297 |
interface_name=self._interface_name, |
| 298 |
signal_name=name, |
| 299 |
parameters=parameters |
| 300 |
) |
| 301 |
|
| 302 |
def on_method_call(self, _connection, _sender, _path, _interface_name, method_name, parameters, invocation): |
| 303 |
|
| 304 |
method = self.methods[method_name] |
| 305 |
out_parameters = method.callback(*parameters.unpack()) |
| 306 |
|
| 307 |
invocation.return_value(out_parameters) |
| 308 |
|
| 309 |
def on_get_property(self, _connection, _sender, _path, _interface_name, property_name): |
| 310 |
prop = self.properties[property_name] |
| 311 |
return GLib.Variant(prop.signature, prop.value) |
| 312 |
|
| 313 |
class DBusMenuService(DBusService): |
| 314 |
|
| 315 |
def __init__(self, menu_items): |
| 316 |
|
| 317 |
super().__init__( |
| 318 |
interface_name="com.canonical.dbusmenu", |
| 319 |
object_path="/StatusNotifierItem/menu", |
| 320 |
bus_type=Gio.BusType.SESSION |
| 321 |
) |
| 322 |
|
| 323 |
self._items = menu_items |
| 324 |
|
| 325 |
for method_name, in_args, out_args, callback in ( |
| 326 |
("GetGroupProperties", ("ai", "as"), ("a(ia{sv})",), self.on_get_group_properties), |
| 327 |
("GetLayout", ("i", "i", "as"), ("u", "(ia{sv}av)"), self.on_get_layout), |
| 328 |
("Event", ("i", "s", "v", "u"), (), self.on_event), |
| 329 |
): |
| 330 |
self.add_method(method_name, in_args, out_args, callback) |
| 331 |
|
| 332 |
for signal_name, value in ( |
| 333 |
("LayoutUpdated", ("u", "i")), |
| 334 |
): |
| 335 |
self.add_signal(signal_name, value) |
| 336 |
|
| 337 |
def update_item(self, item): |
| 338 |
|
| 339 |
item_id = item["id"] |
| 340 |
|
| 341 |
if item_id not in self._items: |
| 342 |
return |
| 343 |
|
| 344 |
self._items[item_id] = item |
| 345 |
|
| 346 |
self._emit_signal( |
| 347 |
name="ItemsPropertiesUpdated", |
| 348 |
parameters=GLib.Variant.new_tuple( |
| 349 |
GLib.Variant.new_array( |
| 350 |
children=[ |
| 351 |
GLib.Variant.new_tuple( |
| 352 |
GLib.Variant.new_int32(item_id), |
| 353 |
GLib.Variant.new_array(children=self._serialize_item(item)) |
| 354 |
) |
| 355 |
], |
| 356 |
), |
| 357 |
GLib.Variant.new_array(child_type=GLib.VariantType("(ias)")) |
| 358 |
) |
| 359 |
) |
| 360 |
|
| 361 |
@staticmethod |
| 362 |
def _serialize_item(item): |
| 363 |
|
| 364 |
def dict_entry(key, value): |
| 365 |
return GLib.Variant.new_dict_entry( |
| 366 |
GLib.Variant.new_string(key), |
| 367 |
GLib.Variant.new_variant(value) |
| 368 |
) |
| 369 |
|
| 370 |
props = [] |
| 371 |
|
| 372 |
if "text" in item: |
| 373 |
props.append(dict_entry("label", GLib.Variant.new_string(item["text"]))) |
| 374 |
|
| 375 |
if item.get("toggled") is not None: |
| 376 |
props.append(dict_entry("toggle-type", GLib.Variant.new_string("checkmark"))) |
| 377 |
props.append(dict_entry("toggle-state", GLib.Variant.new_int32(int(item["toggled"])))) |
| 378 |
else: |
| 379 |
props.append(dict_entry("type", GLib.Variant.new_string("separator"))) |
| 380 |
|
| 381 |
props.append(dict_entry("visible", GLib.Variant.new_boolean(item["visible"]))) |
| 382 |
return props |
| 383 |
|
| 384 |
def on_get_group_properties(self, ids, _properties): |
| 385 |
|
| 386 |
item_properties = [] |
| 387 |
requested_ids = set(ids) |
| 388 |
|
| 389 |
for idx, item in self._items.items(): |
| 390 |
# According to the spec, if no IDs are requested, we should send the entire menu |
| 391 |
if requested_ids and idx not in requested_ids: |
| 392 |
continue |
| 393 |
|
| 394 |
item_properties.append( |
| 395 |
GLib.Variant.new_tuple( |
| 396 |
GLib.Variant.new_int32(idx), |
| 397 |
GLib.Variant.new_array(children=self._serialize_item(item)) |
| 398 |
) |
| 399 |
) |
| 400 |
|
| 401 |
return GLib.Variant.new_tuple( |
| 402 |
GLib.Variant.new_array(children=item_properties) |
| 403 |
) |
| 404 |
|
| 405 |
def on_get_layout(self, _parent_id, _recursion_depth, _property_names): |
| 406 |
|
| 407 |
revision = 0 |
| 408 |
serialized_items = [] |
| 409 |
|
| 410 |
for idx, item in self._items.items(): |
| 411 |
serialized_item = GLib.Variant.new_variant( |
| 412 |
GLib.Variant.new_tuple( |
| 413 |
GLib.Variant.new_int32(idx), |
| 414 |
GLib.Variant.new_array(children=self._serialize_item(item)), |
| 415 |
GLib.Variant.new_array(child_type=GLib.VariantType("v")) |
| 416 |
) |
| 417 |
) |
| 418 |
serialized_items.append(serialized_item) |
| 419 |
|
| 420 |
return GLib.Variant.new_tuple( |
| 421 |
GLib.Variant.new_uint32(revision), |
| 422 |
GLib.Variant.new_tuple( |
| 423 |
GLib.Variant.new_int32(0), |
| 424 |
GLib.Variant.new_array(child_type=GLib.VariantType("{sv}")), |
| 425 |
GLib.Variant.new_array(children=serialized_items) |
| 426 |
) |
| 427 |
) |
| 428 |
|
| 429 |
def on_event(self, idx, event_id, _data, _timestamp): |
| 430 |
if event_id == "clicked": |
| 431 |
self._items[idx]["callback"]() |
| 432 |
|
| 433 |
class StatusNotifierItemService(DBusService): |
| 434 |
|
| 435 |
def __init__(self, activate_callback, menu_items): |
| 436 |
|
| 437 |
super().__init__( |
| 438 |
interface_name="org.kde.StatusNotifierItem", |
| 439 |
object_path="/StatusNotifierItem", |
| 440 |
bus_type=Gio.BusType.SESSION |
| 441 |
) |
| 442 |
|
| 443 |
self.menu = StatusNotifierImplementation.DBusMenuService(menu_items) |
| 444 |
|
| 445 |
for property_name, signature, value in ( |
| 446 |
("Category", "s", "Communications"), |
| 447 |
("Id", "s", pynicotine.__application_id__), |
| 448 |
("Title", "s", pynicotine.__application_name__), |
| 449 |
("ToolTip", "(sa(iiay)ss)", ("", [], pynicotine.__application_name__, "")), |
| 450 |
("Menu", "o", "/StatusNotifierItem/menu"), |
| 451 |
("ItemIsMenu", "b", False), |
| 452 |
("IconName", "s", ""), |
| 453 |
("IconThemePath", "s", ""), |
| 454 |
("Status", "s", "Active") |
| 455 |
): |
| 456 |
self.add_property(property_name, signature, value) |
| 457 |
|
| 458 |
for method_name, in_args, out_args, callback in ( |
| 459 |
("Activate", ("i", "i"), (), activate_callback), |
| 460 |
("ProvideXdgActivationToken", ("s",), (), self.on_provide_activation_token) |
| 461 |
): |
| 462 |
self.add_method(method_name, in_args, out_args, callback) |
| 463 |
|
| 464 |
for signal_name, value in ( |
| 465 |
("NewIcon", ()), |
| 466 |
("NewIconThemePath", ("s",)), |
| 467 |
("NewStatus", ("s",)) |
| 468 |
): |
| 469 |
self.add_signal(signal_name, value) |
| 470 |
|
| 471 |
def register(self): |
| 472 |
self.menu.register() |
| 473 |
super().register() |
| 474 |
|
| 475 |
def unregister(self): |
| 476 |
super().unregister() |
| 477 |
self.menu.unregister() |
| 478 |
|
| 479 |
def update_icon(self): |
| 480 |
self._emit_signal("NewIcon") |
| 481 |
|
| 482 |
def update_icon_theme(self, icon_path): |
| 483 |
|
| 484 |
self._emit_signal( |
| 485 |
name="NewIconThemePath", |
| 486 |
parameters=GLib.Variant.new_tuple( |
| 487 |
GLib.Variant.new_string(icon_path) |
| 488 |
) |
| 489 |
) |
| 490 |
|
| 491 |
def update_status(self, status): |
| 492 |
|
| 493 |
self._emit_signal( |
| 494 |
name="NewStatus", |
| 495 |
parameters=GLib.Variant.new_tuple( |
| 496 |
GLib.Variant.new_string(status) |
| 497 |
) |
| 498 |
) |
| 499 |
|
| 500 |
def on_provide_activation_token(self, token): |
| 501 |
Window.activation_token = token |
| 502 |
|
| 503 |
def __init__(self, application): |
| 504 |
|
| 505 |
super().__init__(application) |
| 506 |
|
| 507 |
self.tray_icon = None |
| 508 |
self.custom_icons = False |
| 509 |
|
| 510 |
try: |
| 511 |
self.bus = Gio.bus_get_sync(bus_type=Gio.BusType.SESSION) |
| 512 |
self.tray_icon = self.StatusNotifierItemService( |
| 513 |
activate_callback=self.activate_callback, |
| 514 |
menu_items=self.menu_items |
| 515 |
) |
| 516 |
self.tray_icon.register() |
| 517 |
|
| 518 |
self.bus.call_sync( |
| 519 |
bus_name="org.kde.StatusNotifierWatcher", |
| 520 |
object_path="/StatusNotifierWatcher", |
| 521 |
interface_name="org.kde.StatusNotifierWatcher", |
| 522 |
method_name="RegisterStatusNotifierItem", |
| 523 |
parameters=GLib.Variant.new_tuple( |
| 524 |
GLib.Variant.new_string(self.bus.get_unique_name()) |
| 525 |
), |
| 526 |
reply_type=None, |
| 527 |
flags=Gio.DBusCallFlags.NONE, |
| 528 |
timeout_msec=1000, |
| 529 |
cancellable=None |
| 530 |
) |
| 531 |
|
| 532 |
except GLib.Error as error: |
| 533 |
self.unload() |
| 534 |
raise ImplementationUnavailable(f"StatusNotifier implementation not available: {error}") from error |
| 535 |
|
| 536 |
@staticmethod |
| 537 |
def _check_icon_path(icon_name, icon_path): |
| 538 |
"""Check if tray icons exist in the specified icon path.""" |
| 539 |
|
| 540 |
if not icon_path: |
| 541 |
return False |
| 542 |
|
| 543 |
icon_path_encoded = encode_path(icon_path) |
| 544 |
|
| 545 |
if not os.path.isdir(icon_path_encoded): |
| 546 |
return False |
| 547 |
|
| 548 |
icon_scheme = f"{pynicotine.__application_id__}-{icon_name}.".encode() |
| 549 |
|
| 550 |
try: |
| 551 |
with os.scandir(icon_path_encoded) as entries: |
| 552 |
for entry in entries: |
| 553 |
if entry.is_file() and entry.name.startswith(icon_scheme): |
| 554 |
return True |
| 555 |
|
| 556 |
except OSError as error: |
| 557 |
log.add_debug("Error accessing tray icon path %s: %s", (icon_path, error)) |
| 558 |
|
| 559 |
return False |
| 560 |
|
| 561 |
def _get_icon_path(self): |
| 562 |
"""Returns an icon path to use for tray icons, or None to fall back to |
| 563 |
system-wide icons.""" |
| 564 |
|
| 565 |
self.custom_icons = False |
| 566 |
custom_icon_path = os.path.join(config.data_folder_path, ".nicotine-icon-theme") |
| 567 |
|
| 568 |
if hasattr(sys, "real_prefix") or sys.base_prefix != sys.prefix: |
| 569 |
# Virtual environment |
| 570 |
local_icon_path = os.path.join(sys.prefix, "share", "icons", "hicolor", "scalable", "apps") |
| 571 |
else: |
| 572 |
# Git folder |
| 573 |
local_icon_path = os.path.join(GTK_GUI_FOLDER_PATH, "icons", "hicolor", "scalable", "apps") |
| 574 |
|
| 575 |
for icon_name in ("away", "connect", "disconnect", "msg"): |
| 576 |
# Check if custom icons exist |
| 577 |
if config.sections["ui"]["icontheme"] and self._check_icon_path(icon_name, custom_icon_path): |
| 578 |
self.custom_icons = True |
| 579 |
return custom_icon_path |
| 580 |
|
| 581 |
# Check if local icons exist |
| 582 |
if self._check_icon_path(icon_name, local_icon_path): |
| 583 |
return local_icon_path |
| 584 |
|
| 585 |
return "" |
| 586 |
|
| 587 |
def _set_icon_name(self, icon_name): |
| 588 |
|
| 589 |
icon_name_property = self.tray_icon.properties["IconName"] |
| 590 |
|
| 591 |
if self.custom_icons: |
| 592 |
# Use alternative icon names to enforce custom icons, since system-wide icons take precedence |
| 593 |
icon_name = icon_name.replace(pynicotine.__application_id__, "nplus-tray") |
| 594 |
|
| 595 |
if icon_name_property.value != icon_name: |
| 596 |
icon_name_property.value = icon_name |
| 597 |
self.tray_icon.update_icon() |
| 598 |
|
| 599 |
if not self.is_visible: |
| 600 |
return |
| 601 |
|
| 602 |
status_property = self.tray_icon.properties["Status"] |
| 603 |
status = "Active" |
| 604 |
|
| 605 |
if status_property.value != status: |
| 606 |
status_property.value = status |
| 607 |
self.tray_icon.update_status(status) |
| 608 |
|
| 609 |
def _update_icon_theme(self): |
| 610 |
|
| 611 |
# If custom icon path was found, use it, otherwise we fall back to system icons |
| 612 |
icon_path = self._get_icon_path() |
| 613 |
icon_path_property = self.tray_icon.properties["IconThemePath"] |
| 614 |
|
| 615 |
if icon_path_property.value == icon_path: |
| 616 |
return |
| 617 |
|
| 618 |
icon_path_property.value = icon_path |
| 619 |
self.tray_icon.update_icon_theme(icon_path) |
| 620 |
|
| 621 |
if icon_path: |
| 622 |
log.add_debug("Using tray icon path %s", icon_path) |
| 623 |
|
| 624 |
def _set_item_text(self, item, text): |
| 625 |
super()._set_item_text(item, text) |
| 626 |
self.tray_icon.menu.update_item(item) |
| 627 |
|
| 628 |
def _set_item_visible(self, item, visible): |
| 629 |
super()._set_item_visible(item, visible) |
| 630 |
self.tray_icon.menu.update_item(item) |
| 631 |
|
| 632 |
def _set_item_toggled(self, item, toggled): |
| 633 |
super()._set_item_toggled(item, toggled) |
| 634 |
self.tray_icon.menu.update_item(item) |
| 635 |
|
| 636 |
def unload(self, is_shutdown=True): |
| 637 |
|
| 638 |
if self.tray_icon is None: |
| 639 |
return |
| 640 |
|
| 641 |
status_property = self.tray_icon.properties["Status"] |
| 642 |
status = "Passive" |
| 643 |
|
| 644 |
if status_property.value != status: |
| 645 |
status_property.value = status |
| 646 |
self.tray_icon.update_status(status) |
| 647 |
|
| 648 |
if is_shutdown: |
| 649 |
self.tray_icon.unregister() |
| 650 |
self.tray_icon = None |
| 651 |
|
| 652 |
|
| 653 |
class Win32Implementation(BaseImplementation): |
| 654 |
"""Windows NotifyIcon implementation. |
| 655 |
|
| 656 |
https://learn.microsoft.com/en-us/windows/win32/shell/notification-area |
| 657 |
https://learn.microsoft.com/en-us/windows/win32/shell/taskbar |
| 658 |
""" |
| 659 |
|
| 660 |
WINDOW_CLASS_NAME = "NicotineTrayIcon" |
| 661 |
|
| 662 |
NIM_ADD = 0 |
| 663 |
NIM_MODIFY = 1 |
| 664 |
NIM_DELETE = 2 |
| 665 |
|
| 666 |
NIF_MESSAGE = 1 |
| 667 |
NIF_ICON = 2 |
| 668 |
NIF_TIP = 4 |
| 669 |
NIF_INFO = 16 |
| 670 |
NIF_REALTIME = 64 |
| 671 |
NIIF_NOSOUND = 16 |
| 672 |
|
| 673 |
MIIM_STATE = 1 |
| 674 |
MIIM_ID = 2 |
| 675 |
MIIM_STRING = 64 |
| 676 |
|
| 677 |
MFS_ENABLED = 0 |
| 678 |
MFS_UNCHECKED = 0 |
| 679 |
MFS_DISABLED = 3 |
| 680 |
MFS_CHECKED = 8 |
| 681 |
|
| 682 |
MFT_SEPARATOR = 2048 |
| 683 |
|
| 684 |
WM_NULL = 0 |
| 685 |
WM_DESTROY = 2 |
| 686 |
WM_CLOSE = 16 |
| 687 |
WM_COMMAND = 273 |
| 688 |
WM_LBUTTONUP = 514 |
| 689 |
WM_RBUTTONUP = 517 |
| 690 |
WM_USER = 1024 |
| 691 |
WM_TRAYICON = (WM_USER + 1) |
| 692 |
NIN_BALLOONHIDE = (WM_USER + 3) |
| 693 |
NIN_BALLOONTIMEOUT = (WM_USER + 4) |
| 694 |
NIN_BALLOONUSERCLICK = (WM_USER + 5) |
| 695 |
|
| 696 |
CS_VREDRAW = 1 |
| 697 |
CS_HREDRAW = 2 |
| 698 |
COLOR_WINDOW = 5 |
| 699 |
IDC_ARROW = 32512 |
| 700 |
|
| 701 |
WS_OVERLAPPED = 0 |
| 702 |
WS_SYSMENU = 524288 |
| 703 |
CW_USEDEFAULT = -2147483648 |
| 704 |
|
| 705 |
IMAGE_ICON = 1 |
| 706 |
LR_LOADFROMFILE = 16 |
| 707 |
SM_CXSMICON = 49 |
| 708 |
|
| 709 |
if sys.platform == "win32": |
| 710 |
from ctypes import Structure |
| 711 |
|
| 712 |
class WNDCLASSW(Structure): |
| 713 |
from ctypes import CFUNCTYPE, wintypes |
| 714 |
|
| 715 |
LPFN_WND_PROC = CFUNCTYPE( |
| 716 |
wintypes.INT, |
| 717 |
wintypes.HWND, |
| 718 |
wintypes.UINT, |
| 719 |
wintypes.WPARAM, |
| 720 |
wintypes.LPARAM |
| 721 |
) |
| 722 |
_fields_ = [ |
| 723 |
("style", wintypes.UINT), |
| 724 |
("lpfn_wnd_proc", LPFN_WND_PROC), |
| 725 |
("cb_cls_extra", wintypes.INT), |
| 726 |
("cb_wnd_extra", wintypes.INT), |
| 727 |
("h_instance", wintypes.HINSTANCE), |
| 728 |
("h_icon", wintypes.HICON), |
| 729 |
("h_cursor", wintypes.HANDLE), |
| 730 |
("hbr_background", wintypes.HBRUSH), |
| 731 |
("lpsz_menu_name", wintypes.LPCWSTR), |
| 732 |
("lpsz_class_name", wintypes.LPCWSTR) |
| 733 |
] |
| 734 |
|
| 735 |
class MENUITEMINFOW(Structure): |
| 736 |
from ctypes import wintypes |
| 737 |
|
| 738 |
_fields_ = [ |
| 739 |
("cb_size", wintypes.UINT), |
| 740 |
("f_mask", wintypes.UINT), |
| 741 |
("f_type", wintypes.UINT), |
| 742 |
("f_state", wintypes.UINT), |
| 743 |
("w_id", wintypes.UINT), |
| 744 |
("h_sub_menu", wintypes.HMENU), |
| 745 |
("hbmp_checked", wintypes.HBITMAP), |
| 746 |
("hbmp_unchecked", wintypes.HBITMAP), |
| 747 |
("dw_item_data", wintypes.LPVOID), |
| 748 |
("dw_type_data", wintypes.LPWSTR), |
| 749 |
("cch", wintypes.UINT), |
| 750 |
("hbmp_item", wintypes.HBITMAP) |
| 751 |
] |
| 752 |
|
| 753 |
class NOTIFYICONDATAW(Structure): |
| 754 |
from ctypes import wintypes |
| 755 |
|
| 756 |
_fields_ = [ |
| 757 |
("cb_size", wintypes.DWORD), |
| 758 |
("h_wnd", wintypes.HWND), |
| 759 |
("u_id", wintypes.UINT), |
| 760 |
("u_flags", wintypes.UINT), |
| 761 |
("u_callback_message", wintypes.UINT), |
| 762 |
("h_icon", wintypes.HICON), |
| 763 |
("sz_tip", wintypes.WCHAR * 128), |
| 764 |
("dw_state", wintypes.DWORD), |
| 765 |
("dw_state_mask", wintypes.DWORD), |
| 766 |
("sz_info", wintypes.WCHAR * 256), |
| 767 |
("u_version", wintypes.UINT), |
| 768 |
("sz_info_title", wintypes.WCHAR * 64), |
| 769 |
("dw_info_flags", wintypes.DWORD), |
| 770 |
("guid_item", wintypes.CHAR * 16), |
| 771 |
("h_balloon_icon", wintypes.HICON) |
| 772 |
] |
| 773 |
|
| 774 |
def __init__(self, application): |
| 775 |
|
| 776 |
from ctypes import windll |
| 777 |
|
| 778 |
super().__init__(application) |
| 779 |
|
| 780 |
self._window_class = None |
| 781 |
self._h_wnd = None |
| 782 |
self._notify_id = None |
| 783 |
self._h_icon = None |
| 784 |
self._menu = None |
| 785 |
self._wm_taskbarcreated = windll.user32.RegisterWindowMessageW("TaskbarCreated") |
| 786 |
self._click_action = None |
| 787 |
self._click_action_target = None |
| 788 |
|
| 789 |
self._register_class() |
| 790 |
self._create_window() |
| 791 |
|
| 792 |
def _register_class(self): |
| 793 |
|
| 794 |
from ctypes import byref, windll |
| 795 |
|
| 796 |
self._window_class = self.WNDCLASSW( |
| 797 |
style=(self.CS_VREDRAW | self.CS_HREDRAW), |
| 798 |
lpfn_wnd_proc=self.WNDCLASSW.LPFN_WND_PROC(self.on_process_window_message), |
| 799 |
h_cursor=windll.user32.LoadCursorW(0, self.IDC_ARROW), |
| 800 |
hbr_background=self.COLOR_WINDOW, |
| 801 |
lpsz_class_name=self.WINDOW_CLASS_NAME |
| 802 |
) |
| 803 |
|
| 804 |
windll.user32.RegisterClassW(byref(self._window_class)) |
| 805 |
|
| 806 |
def _unregister_class(self): |
| 807 |
|
| 808 |
if self._window_class is None: |
| 809 |
return |
| 810 |
|
| 811 |
from ctypes import windll |
| 812 |
|
| 813 |
windll.user32.UnregisterClassW(self.WINDOW_CLASS_NAME, self._window_class.h_instance) |
| 814 |
self._window_class = None |
| 815 |
|
| 816 |
def _create_window(self): |
| 817 |
|
| 818 |
from ctypes import windll |
| 819 |
|
| 820 |
style = self.WS_OVERLAPPED | self.WS_SYSMENU |
| 821 |
self._h_wnd = windll.user32.CreateWindowExW( |
| 822 |
0, |
| 823 |
self.WINDOW_CLASS_NAME, |
| 824 |
self.WINDOW_CLASS_NAME, |
| 825 |
style, |
| 826 |
0, |
| 827 |
0, |
| 828 |
self.CW_USEDEFAULT, |
| 829 |
self.CW_USEDEFAULT, |
| 830 |
0, |
| 831 |
0, |
| 832 |
0, |
| 833 |
None |
| 834 |
) |
| 835 |
|
| 836 |
windll.user32.UpdateWindow(self._h_wnd) |
| 837 |
|
| 838 |
def _destroy_window(self): |
| 839 |
|
| 840 |
if self._h_wnd is None: |
| 841 |
return |
| 842 |
|
| 843 |
from ctypes import windll |
| 844 |
|
| 845 |
windll.user32.DestroyWindow(self._h_wnd) |
| 846 |
self._h_wnd = None |
| 847 |
|
| 848 |
def _load_ico_buffer(self, icon_name, icon_size): |
| 849 |
|
| 850 |
ico_buffer = b"" |
| 851 |
|
| 852 |
if GTK_API_VERSION >= 4: |
| 853 |
icon = ICON_THEME.lookup_icon(icon_name, fallbacks=None, size=icon_size, scale=1, direction=0, flags=0) |
| 854 |
icon_path = icon.get_file().get_path() |
| 855 |
|
| 856 |
if not icon_path: |
| 857 |
return ico_buffer |
| 858 |
|
| 859 |
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icon_path, icon_size, icon_size) |
| 860 |
else: |
| 861 |
icon = ICON_THEME.lookup_icon(icon_name, size=icon_size, flags=0) |
| 862 |
|
| 863 |
if not icon: |
| 864 |
return ico_buffer |
| 865 |
|
| 866 |
pixbuf = icon.load_icon() |
| 867 |
|
| 868 |
_success, ico_buffer = pixbuf.save_to_bufferv("ico") |
| 869 |
return ico_buffer |
| 870 |
|
| 871 |
def _load_h_icon(self, icon_name): |
| 872 |
|
| 873 |
from ctypes import windll |
| 874 |
|
| 875 |
# Attempt to load custom icons first |
| 876 |
icon_size = windll.user32.GetSystemMetrics(self.SM_CXSMICON) |
| 877 |
ico_buffer = self._load_ico_buffer( |
| 878 |
icon_name.replace(f"{pynicotine.__application_id__}-", "nplus-tray-"), icon_size) |
| 879 |
|
| 880 |
if not ico_buffer: |
| 881 |
# No custom icons present, fall back to default icons |
| 882 |
ico_buffer = self._load_ico_buffer(icon_name, icon_size) |
| 883 |
|
| 884 |
try: |
| 885 |
import tempfile |
| 886 |
file_handle = tempfile.NamedTemporaryFile(delete=False) |
| 887 |
|
| 888 |
with file_handle: |
| 889 |
file_handle.write(ico_buffer) |
| 890 |
|
| 891 |
return windll.user32.LoadImageA( |
| 892 |
0, |
| 893 |
encode_path(file_handle.name), |
| 894 |
self.IMAGE_ICON, |
| 895 |
icon_size, |
| 896 |
icon_size, |
| 897 |
self.LR_LOADFROMFILE |
| 898 |
) |
| 899 |
|
| 900 |
finally: |
| 901 |
os.remove(file_handle.name) |
| 902 |
|
| 903 |
def _destroy_h_icon(self): |
| 904 |
|
| 905 |
from ctypes import windll |
| 906 |
|
| 907 |
if self._h_icon: |
| 908 |
windll.user32.DestroyIcon(self._h_icon) |
| 909 |
self._h_icon = None |
| 910 |
|
| 911 |
def _update_notify_icon(self, title="", message="", icon_name=None, click_action=None, |
| 912 |
click_action_target=None, high_priority=False): |
| 913 |
# pylint: disable=attribute-defined-outside-init,no-member |
| 914 |
|
| 915 |
if self._h_wnd is None: |
| 916 |
return |
| 917 |
|
| 918 |
if icon_name: |
| 919 |
self._destroy_h_icon() |
| 920 |
self._h_icon = self._load_h_icon(icon_name) |
| 921 |
|
| 922 |
if not self.is_visible and not (title or message): |
| 923 |
# When disabled by user, temporarily show tray icon when displaying a notification |
| 924 |
return |
| 925 |
|
| 926 |
from ctypes import byref, sizeof, windll |
| 927 |
|
| 928 |
notify_action = self.NIM_MODIFY |
| 929 |
|
| 930 |
if self._notify_id is None: |
| 931 |
self._notify_id = self.NOTIFYICONDATAW( |
| 932 |
cb_size=sizeof(self.NOTIFYICONDATAW), |
| 933 |
h_wnd=self._h_wnd, |
| 934 |
u_id=0, |
| 935 |
u_flags=(self.NIF_ICON | self.NIF_MESSAGE | self.NIF_TIP | self.NIF_INFO), |
| 936 |
u_callback_message=self.WM_TRAYICON, |
| 937 |
sz_tip=truncate_string_byte(pynicotine.__application_name__, byte_limit=127) |
| 938 |
) |
| 939 |
notify_action = self.NIM_ADD |
| 940 |
|
| 941 |
if config.sections["notifications"]["notification_popup_sound"]: |
| 942 |
self._notify_id.dw_info_flags &= ~self.NIIF_NOSOUND |
| 943 |
else: |
| 944 |
self._notify_id.dw_info_flags |= self.NIIF_NOSOUND |
| 945 |
|
| 946 |
if high_priority: |
| 947 |
self._notify_id.u_flags &= ~self.NIF_REALTIME |
| 948 |
else: |
| 949 |
self._notify_id.u_flags |= self.NIF_REALTIME |
| 950 |
|
| 951 |
self._notify_id.h_icon = self._h_icon |
| 952 |
self._notify_id.sz_info_title = truncate_string_byte(title, byte_limit=63, ellipsize=True) |
| 953 |
self._notify_id.sz_info = truncate_string_byte(message, byte_limit=255, ellipsize=True) |
| 954 |
|
| 955 |
self._click_action = click_action.replace("app.", "") if click_action else None |
| 956 |
self._click_action_target = GLib.Variant.new_string(click_action_target) if click_action_target else None |
| 957 |
|
| 958 |
windll.shell32.Shell_NotifyIconW(notify_action, byref(self._notify_id)) |
| 959 |
|
| 960 |
def _remove_notify_icon(self): |
| 961 |
|
| 962 |
from ctypes import byref, windll |
| 963 |
|
| 964 |
if self._notify_id: |
| 965 |
windll.shell32.Shell_NotifyIconW(self.NIM_DELETE, byref(self._notify_id)) |
| 966 |
self._notify_id = None |
| 967 |
|
| 968 |
self._destroy_menu() |
| 969 |
|
| 970 |
def _serialize_menu_item(self, item): |
| 971 |
# pylint: disable=attribute-defined-outside-init,no-member |
| 972 |
|
| 973 |
from ctypes import sizeof |
| 974 |
|
| 975 |
item_info = self.MENUITEMINFOW(cb_size=sizeof(self.MENUITEMINFOW)) |
| 976 |
w_id = item["id"] |
| 977 |
text = item.get("text") |
| 978 |
is_checked = item.get("toggled") |
| 979 |
|
| 980 |
item_info.f_mask |= self.MIIM_ID |
| 981 |
item_info.w_id = w_id |
| 982 |
|
| 983 |
if text is not None: |
| 984 |
item_info.f_mask |= self.MIIM_STRING |
| 985 |
item_info.dw_type_data = text.replace("_", "&") # Mnemonics use & |
| 986 |
else: |
| 987 |
item_info.f_type |= self.MFT_SEPARATOR |
| 988 |
|
| 989 |
if is_checked is not None: |
| 990 |
item_info.f_mask |= self.MIIM_STATE |
| 991 |
item_info.f_state |= self.MFS_CHECKED if is_checked else self.MFS_UNCHECKED |
| 992 |
|
| 993 |
return item_info |
| 994 |
|
| 995 |
def _show_menu(self): |
| 996 |
|
| 997 |
from ctypes import byref, windll, wintypes |
| 998 |
|
| 999 |
self._populate_menu() |
| 1000 |
|
| 1001 |
pos = wintypes.POINT() |
| 1002 |
windll.user32.GetCursorPos(byref(pos)) |
| 1003 |
|
| 1004 |
# PRB: Menus for Notification Icons Do Not Work Correctly |
| 1005 |
# https://web.archive.org/web/20121015064650/http://support.microsoft.com/kb/135788 |
| 1006 |
|
| 1007 |
windll.user32.SetForegroundWindow(self._h_wnd) |
| 1008 |
windll.user32.TrackPopupMenu( |
| 1009 |
self._menu, |
| 1010 |
0, |
| 1011 |
pos.x, |
| 1012 |
pos.y, |
| 1013 |
0, |
| 1014 |
self._h_wnd, |
| 1015 |
None |
| 1016 |
) |
| 1017 |
windll.user32.PostMessageW(self._h_wnd, self.WM_NULL, 0, 0) |
| 1018 |
|
| 1019 |
def _populate_menu(self): |
| 1020 |
|
| 1021 |
from ctypes import byref, windll |
| 1022 |
|
| 1023 |
self._destroy_menu() |
| 1024 |
|
| 1025 |
if self._menu is None: |
| 1026 |
self._menu = windll.user32.CreatePopupMenu() |
| 1027 |
|
| 1028 |
for item in self.menu_items.values(): |
| 1029 |
if not item["visible"]: |
| 1030 |
continue |
| 1031 |
|
| 1032 |
item_id = item["id"] |
| 1033 |
item_info = self._serialize_menu_item(item) |
| 1034 |
|
| 1035 |
windll.user32.InsertMenuItemW(self._menu, item_id, False, byref(item_info)) |
| 1036 |
|
| 1037 |
def _destroy_menu(self): |
| 1038 |
|
| 1039 |
if self._menu is None: |
| 1040 |
return |
| 1041 |
|
| 1042 |
from ctypes import windll |
| 1043 |
|
| 1044 |
windll.user32.DestroyMenu(self._menu) |
| 1045 |
self._menu = None |
| 1046 |
|
| 1047 |
def _set_icon_name(self, icon_name): |
| 1048 |
self._update_notify_icon(icon_name=icon_name) |
| 1049 |
|
| 1050 |
def on_process_window_message(self, h_wnd, msg, w_param, l_param): |
| 1051 |
|
| 1052 |
from ctypes import windll, wintypes |
| 1053 |
|
| 1054 |
if msg == self.WM_TRAYICON: |
| 1055 |
if l_param == self.WM_RBUTTONUP: |
| 1056 |
# Icon pressed |
| 1057 |
self._show_menu() |
| 1058 |
|
| 1059 |
elif l_param == self.WM_LBUTTONUP: |
| 1060 |
# Icon pressed |
| 1061 |
self.activate_callback() |
| 1062 |
|
| 1063 |
elif l_param in {self.NIN_BALLOONHIDE, self.NIN_BALLOONTIMEOUT, self.NIN_BALLOONUSERCLICK}: |
| 1064 |
if l_param == self.NIN_BALLOONUSERCLICK and self._click_action is not None: |
| 1065 |
# Notification was clicked, perform action |
| 1066 |
self.application.lookup_action(self._click_action).activate(self._click_action_target) |
| 1067 |
self._click_action = self._click_action_target = None |
| 1068 |
|
| 1069 |
if not config.sections["ui"]["trayicon"]: |
| 1070 |
# Notification dismissed, but user has disabled tray icon |
| 1071 |
self._remove_notify_icon() |
| 1072 |
|
| 1073 |
elif msg == self.WM_COMMAND: |
| 1074 |
# Menu item pressed |
| 1075 |
menu_item_id = w_param & 0xFFFF |
| 1076 |
menu_item_callback = self.menu_items[menu_item_id]["callback"] |
| 1077 |
menu_item_callback() |
| 1078 |
|
| 1079 |
elif msg == self._wm_taskbarcreated: |
| 1080 |
# Taskbar process restarted, create new icon |
| 1081 |
self._remove_notify_icon() |
| 1082 |
self._update_notify_icon() |
| 1083 |
|
| 1084 |
return windll.user32.DefWindowProcW( |
| 1085 |
wintypes.HWND(h_wnd), |
| 1086 |
msg, |
| 1087 |
wintypes.WPARAM(w_param), |
| 1088 |
wintypes.LPARAM(l_param) |
| 1089 |
) |
| 1090 |
|
| 1091 |
def show_notification(self, title, message, action=None, action_target=None, high_priority=False): |
| 1092 |
|
| 1093 |
self._update_notify_icon( |
| 1094 |
title=title, message=message, click_action=action, click_action_target=action_target, |
| 1095 |
high_priority=high_priority) |
| 1096 |
|
| 1097 |
def unload(self, is_shutdown=True): |
| 1098 |
|
| 1099 |
self._remove_notify_icon() |
| 1100 |
|
| 1101 |
if not is_shutdown: |
| 1102 |
# Keep notification support as long as we're running |
| 1103 |
return |
| 1104 |
|
| 1105 |
self._destroy_h_icon() |
| 1106 |
self._destroy_window() |
| 1107 |
self._unregister_class() |
| 1108 |
|
| 1109 |
|
| 1110 |
class StatusIconImplementation(BaseImplementation): |
| 1111 |
|
| 1112 |
def __init__(self, application): |
| 1113 |
|
| 1114 |
super().__init__(application) |
| 1115 |
|
| 1116 |
if not hasattr(Gtk, "StatusIcon") or os.environ.get("WAYLAND_DISPLAY"): |
| 1117 |
# GtkStatusIcon does not work on Wayland |
| 1118 |
raise ImplementationUnavailable("StatusIcon implementation not available") |
| 1119 |
|
| 1120 |
self.tray_icon = Gtk.StatusIcon(tooltip_text=pynicotine.__application_name__) |
| 1121 |
self.tray_icon.connect("activate", self.activate_callback) |
| 1122 |
self.tray_icon.connect("popup-menu", self.on_status_icon_popup) |
| 1123 |
|
| 1124 |
self.gtk_menu = self._build_gtk_menu() |
| 1125 |
GLib.idle_add(self._update_icon, priority=GLib.PRIORITY_HIGH_IDLE) |
| 1126 |
|
| 1127 |
def on_status_icon_popup(self, _status_icon, button, _activate_time): |
| 1128 |
|
| 1129 |
if button == 3: |
| 1130 |
time = Gtk.get_current_event_time() |
| 1131 |
self.gtk_menu.popup(None, None, None, None, button, time) |
| 1132 |
|
| 1133 |
def _set_item_text(self, item, text): |
| 1134 |
super()._set_item_text(item, text) |
| 1135 |
item["gtk_menu_item"].set_label(text) |
| 1136 |
|
| 1137 |
def _set_item_visible(self, item, visible): |
| 1138 |
super()._set_item_visible(item, visible) |
| 1139 |
item["gtk_menu_item"].set_visible(visible) |
| 1140 |
|
| 1141 |
def _set_item_toggled(self, item, toggled): |
| 1142 |
|
| 1143 |
super()._set_item_toggled(item, toggled) |
| 1144 |
gtk_menu_item = item["gtk_menu_item"] |
| 1145 |
|
| 1146 |
with gtk_menu_item.handler_block(item["gtk_handler"]): |
| 1147 |
gtk_menu_item.set_active(toggled) |
| 1148 |
|
| 1149 |
def _build_gtk_menu(self): |
| 1150 |
|
| 1151 |
gtk_menu = Gtk.Menu() |
| 1152 |
|
| 1153 |
for item in self.menu_items.values(): |
| 1154 |
text = item.get("text") |
| 1155 |
|
| 1156 |
if text is None: |
| 1157 |
item["gtk_menu_item"] = gtk_menu_item = Gtk.SeparatorMenuItem(visible=True) |
| 1158 |
else: |
| 1159 |
gtk_menu_item_class = Gtk.CheckMenuItem if "toggled" in item else Gtk.MenuItem |
| 1160 |
item["gtk_menu_item"] = gtk_menu_item = gtk_menu_item_class( |
| 1161 |
label=text, use_underline=True, visible=True |
| 1162 |
) |
| 1163 |
item["gtk_handler"] = gtk_menu_item.connect("activate", item["callback"]) |
| 1164 |
|
| 1165 |
gtk_menu.append(gtk_menu_item) |
| 1166 |
|
| 1167 |
return gtk_menu |
| 1168 |
|
| 1169 |
def _set_icon_name(self, icon_name): |
| 1170 |
|
| 1171 |
if not self.tray_icon.get_visible(): |
| 1172 |
self.tray_icon.set_visible(True) |
| 1173 |
|
| 1174 |
if self.tray_icon.is_embedded() and self.tray_icon.get_icon_name() != icon_name: |
| 1175 |
self.tray_icon.set_from_icon_name(icon_name) |
| 1176 |
|
| 1177 |
def unload(self, is_shutdown=True): |
| 1178 |
if self.tray_icon.get_visible(): |
| 1179 |
self.tray_icon.set_visible(False) |
| 1180 |
|
| 1181 |
|
| 1182 |
class TrayIcon: |
| 1183 |
|
| 1184 |
def __init__(self, application): |
| 1185 |
|
| 1186 |
self.application = application |
| 1187 |
self.available = True |
| 1188 |
self.implementation = None |
| 1189 |
self.watch_id = None |
| 1190 |
|
| 1191 |
self._watch_availability() |
| 1192 |
self.load() |
| 1193 |
|
| 1194 |
def _watch_availability(self): |
| 1195 |
|
| 1196 |
if sys.platform in {"win32", "darwin"}: |
| 1197 |
return |
| 1198 |
|
| 1199 |
if self.watch_id is not None: |
| 1200 |
return |
| 1201 |
|
| 1202 |
self.watch_id = Gio.bus_watch_name( |
| 1203 |
bus_type=Gio.BusType.SESSION, |
| 1204 |
name="org.kde.StatusNotifierWatcher", |
| 1205 |
flags=Gio.BusNameWatcherFlags.NONE, |
| 1206 |
name_appeared_closure=self.load, |
| 1207 |
name_vanished_closure=self.unload |
| 1208 |
) |
| 1209 |
|
| 1210 |
def _unwatch_availability(self): |
| 1211 |
|
| 1212 |
if self.watch_id is not None: |
| 1213 |
Gio.bus_unwatch_name(self.watch_id) |
| 1214 |
self.watch_id = None |
| 1215 |
|
| 1216 |
def load(self, *_args): |
| 1217 |
|
| 1218 |
self.available = True |
| 1219 |
|
| 1220 |
if sys.platform == "win32": |
| 1221 |
# Always keep tray icon loaded for Windows notification support |
| 1222 |
pass |
| 1223 |
|
| 1224 |
elif not config.sections["ui"]["trayicon"]: |
| 1225 |
# No need to have tray icon loaded now (unless this is Windows) |
| 1226 |
return |
| 1227 |
|
| 1228 |
if self.implementation is None: |
| 1229 |
if sys.platform == "win32": |
| 1230 |
self.implementation = Win32Implementation(self.application) |
| 1231 |
|
| 1232 |
elif sys.platform == "darwin": |
| 1233 |
self.available = False |
| 1234 |
|
| 1235 |
else: |
| 1236 |
try: |
| 1237 |
self.implementation = StatusNotifierImplementation(self.application) |
| 1238 |
|
| 1239 |
except ImplementationUnavailable: |
| 1240 |
try: |
| 1241 |
self.implementation = StatusIconImplementation(self.application) |
| 1242 |
self._unwatch_availability() |
| 1243 |
|
| 1244 |
except ImplementationUnavailable: |
| 1245 |
self.available = False |
| 1246 |
return |
| 1247 |
|
| 1248 |
self.update() |
| 1249 |
|
| 1250 |
def set_download_status(self, status): |
| 1251 |
if self.implementation: |
| 1252 |
self.implementation.set_download_status(status) |
| 1253 |
|
| 1254 |
def set_upload_status(self, status): |
| 1255 |
if self.implementation: |
| 1256 |
self.implementation.set_upload_status(status) |
| 1257 |
|
| 1258 |
def show_notification(self, title, message, action=None, action_target=None, high_priority=False): |
| 1259 |
|
| 1260 |
if self.implementation: |
| 1261 |
self.implementation.show_notification( |
| 1262 |
title=title, message=message, action=action, action_target=action_target, |
| 1263 |
high_priority=high_priority) |
| 1264 |
|
| 1265 |
def update(self): |
| 1266 |
if self.implementation: |
| 1267 |
self.implementation.update() |
| 1268 |
|
| 1269 |
def unload(self, *_args, is_shutdown=True): |
| 1270 |
|
| 1271 |
if self.implementation: |
| 1272 |
self.implementation.unload(is_shutdown=is_shutdown) |
| 1273 |
self.implementation.is_visible = False |
| 1274 |
|
| 1275 |
if is_shutdown: |
| 1276 |
self.implementation = None |
| 1277 |
self.available = False |
| 1278 |
|
| 1279 |
def destroy(self): |
| 1280 |
self.unload() |
| 1281 |
self.__dict__.clear() |