2024-02-02 05:04:30 +00:00
|
|
|
import logging
|
2024-02-05 07:30:47 +00:00
|
|
|
import threading
|
2024-03-03 08:44:16 +00:00
|
|
|
from collections.abc import Callable
|
2024-03-09 16:15:32 +00:00
|
|
|
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
|
|
|
|
2024-02-02 05:04:30 +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 = {
|
2024-03-06 08:05:10 +00:00
|
|
|
"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
|
|
|
|
2024-03-03 09:01:08 +00:00
|
|
|
def _join_finished_task(self) -> bool:
|
2024-03-06 08:05:10 +00:00
|
|
|
self.emit("join_finished")
|
2024-03-03 04:21:12 +00:00
|
|
|
return GLib.SOURCE_REMOVE
|
|
|
|
|
2024-03-03 05:47:18 +00:00
|
|
|
def __init__(self, url: ClanURI) -> None:
|
2024-01-20 12:15:25 +00:00
|
|
|
super().__init__()
|
2024-03-09 16:15:32 +00:00
|
|
|
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
|
2024-03-03 09:01:08 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2024-03-03 05:47:18 +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()
|
|
|
|
"""
|
|
|
|
|
2024-03-03 05:47:18 +00:00
|
|
|
_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
|
2024-03-03 05:47:18 +00:00
|
|
|
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)
|
|
|
|
|
2024-03-06 08:05:10 +00:00
|
|
|
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
|
|
|
|
|
2024-03-06 08:05:10 +00:00
|
|
|
def _rerender_join_list(
|
|
|
|
self, source: GKVStore, position: int, removed: int, added: int
|
2024-03-05 16:10:30 +00:00
|
|
|
) -> None:
|
|
|
|
self.list_store.items_changed(
|
|
|
|
0, self.list_store.get_n_items(), self.list_store.get_n_items()
|
|
|
|
)
|
|
|
|
|
2024-03-03 05:47:18 +00:00
|
|
|
def is_empty(self) -> bool:
|
|
|
|
return self.list_store.get_n_items() == 0
|
|
|
|
|
2024-03-06 08:05:10 +00:00
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2024-03-05 16:10:30 +00:00
|
|
|
value = JoinValue(uri)
|
2024-03-07 12:04:48 +00:00
|
|
|
if value.url.machine.get_id() in [
|
2024-03-09 16:15:32 +00:00
|
|
|
cast(JoinValue, item).url.machine.get_id() for item in self.list_store
|
2024-03-07 12:04:48 +00:00
|
|
|
]:
|
2024-03-03 08:44:16 +00:00
|
|
|
log.info(f"Join request already exists: {value.url}. Ignoring.")
|
2024-02-07 10:16:20 +00:00
|
|
|
return
|
|
|
|
|
2024-03-03 05:47:18 +00:00
|
|
|
value.connect("join_finished", self._on_join_finished)
|
2024-03-03 08:44:16 +00:00
|
|
|
value.connect("join_finished", after_join)
|
2024-02-05 07:30:47 +00:00
|
|
|
|
2024-03-03 05:47:18 +00:00
|
|
|
self.list_store.append(value)
|
2024-01-20 12:15:25 +00:00
|
|
|
|
2024-03-06 08:05:10 +00:00
|
|
|
def _on_join_finished(self, source: JoinValue) -> None:
|
|
|
|
log.info(f"Join finished: {source.url}")
|
|
|
|
self.discard(source)
|
2024-03-12 16:19:20 +00:00
|
|
|
assert source.entry is not None
|
2024-03-06 08:05:10 +00:00
|
|
|
ClanStore.use().push_history_entry(source.entry)
|
2024-01-20 12:15:25 +00:00
|
|
|
|
2024-03-03 05:47:18 +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)
|