clan-core/pkgs/clan-app/clan_app/singletons/use_join.py

107 lines
3.1 KiB
Python
Raw Normal View History

import logging
2024-02-05 07:30:47 +00:00
import threading
from collections.abc import Callable
from typing import Any, ClassVar, cast
2024-01-20 12:15:25 +00:00
import gi
from clan_cli.clan_uri import ClanURI
2024-03-03 06:50:49 +00:00
from clan_cli.history.add import HistoryEntry, add_history
2024-06-05 09:23:12 +00:00
from clan_app.components.gkvstore import GKVStore
from clan_app.singletons.use_vms import ClanStore
2024-01-20 12:15:25 +00:00
gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
2024-02-05 07:30:47 +00:00
from gi.repository import Gio, GLib, GObject
2024-01-20 12:15:25 +00:00
log = logging.getLogger(__name__)
2024-01-20 12:15:25 +00:00
class JoinValue(GObject.Object):
2024-02-05 07:30:47 +00:00
__gsignals__: ClassVar = {
"join_finished": (GObject.SignalFlags.RUN_FIRST, None, []),
2024-02-05 07:30:47 +00:00
}
2024-01-20 12:15:25 +00:00
url: ClanURI
2024-03-03 06:50:49 +00:00
entry: HistoryEntry | None
2024-01-20 12:15:25 +00:00
def _join_finished_task(self) -> bool:
self.emit("join_finished")
return GLib.SOURCE_REMOVE
def __init__(self, url: ClanURI) -> None:
2024-01-20 12:15:25 +00:00
super().__init__()
self.url: ClanURI = url
self.entry: HistoryEntry | None = None
2024-02-05 07:30:47 +00:00
def __join(self) -> None:
2024-03-03 06:50:49 +00:00
new_entry = add_history(self.url)
self.entry = new_entry
GLib.idle_add(self._join_finished_task)
2024-02-05 07:30:47 +00:00
def join(self) -> None:
threading.Thread(target=self.__join).start()
2024-01-20 12:15:25 +00:00
class JoinList:
2024-01-20 12:15:25 +00:00
"""
This is a singleton.
It is initialized with the first call of use()
"""
_instance: "None | JoinList" = None
2024-01-20 12:15:25 +00:00
list_store: Gio.ListStore
# Make sure the VMS class is used as a singleton
def __init__(self) -> None:
raise RuntimeError("Call use() instead")
@classmethod
def use(cls: Any) -> "JoinList":
2024-01-20 12:15:25 +00:00
if cls._instance is None:
cls._instance = cls.__new__(cls)
cls.list_store = Gio.ListStore.new(JoinValue)
ClanStore.use().register_on_deep_change(cls._instance._rerender_join_list)
2024-03-07 12:04:48 +00:00
2024-01-20 12:15:25 +00:00
return cls._instance
def _rerender_join_list(
self, source: GKVStore, position: int, removed: int, added: int
) -> None:
self.list_store.items_changed(
0, self.list_store.get_n_items(), self.list_store.get_n_items()
)
def is_empty(self) -> bool:
return self.list_store.get_n_items() == 0
def push(self, uri: ClanURI, after_join: Callable[[JoinValue], None]) -> None:
2024-01-20 12:15:25 +00:00
"""
Add a join request.
This method can add multiple join requests if called subsequently for each request.
"""
value = JoinValue(uri)
2024-03-07 12:04:48 +00:00
if value.url.machine.get_id() in [
cast(JoinValue, item).url.machine.get_id() for item in self.list_store
2024-03-07 12:04:48 +00:00
]:
log.info(f"Join request already exists: {value.url}. Ignoring.")
return
value.connect("join_finished", self._on_join_finished)
value.connect("join_finished", after_join)
2024-02-05 07:30:47 +00:00
self.list_store.append(value)
2024-01-20 12:15:25 +00:00
def _on_join_finished(self, source: JoinValue) -> None:
log.info(f"Join finished: {source.url}")
self.discard(source)
assert source.entry is not None
ClanStore.use().push_history_entry(source.entry)
2024-01-20 12:15:25 +00:00
def discard(self, value: JoinValue) -> None:
(has, idx) = self.list_store.find(value)
2024-01-20 12:15:25 +00:00
if has:
self.list_store.remove(idx)