Commit 004786ca authored by Philipp Hörist's avatar Philipp Hörist
Browse files

Bookmarks: Add annotations

parent 56c26983
......@@ -21,16 +21,19 @@
from typing import Tuple
from typing import Union
from typing import Optional
from typing import cast
import functools
from nbxmpp.namespaces import Namespace
from nbxmpp.protocol import JID
from nbxmpp.structs import BookmarkData
from nbxmpp.task import Task
from gi.repository import GLib
from gajim.common import app
from import NetworkEvent
from gajim.common.types import BookmarksDict
from gajim.common.modules.base import BaseModule
from gajim.common.modules.util import event_node
......@@ -47,8 +50,8 @@ def __init__(self, con):
self._compat = False
self._compat_pep = False
self._node_max = False
self._bookmarks = {}
self._join_timeouts = []
self._bookmarks: BookmarksDict = {}
self._join_timeouts: List[int] = []
self._request_in_progress = True
......@@ -65,7 +68,7 @@ def compat_pep(self) -> bool:
def bookmarks(self) -> List[BookmarkData]:
return self._bookmarks.values()
return list(self._bookmarks.values())
def pep_bookmarks_used(self) -> bool:
......@@ -125,11 +128,11 @@ def _bookmark_1_event_received(self, _con, _stanza, properties):'Retract: %s', jid)
bookmark = self._bookmarks.get(jid)
if bookmark is not None:
self._bookmarks.pop(bookmark, None)
self._bookmarks.pop(jid, None)
new_bookmark =
self._bookmarks[new_bookmark.jid] =
new_bookmark: BookmarkData =
self._bookmarks[new_bookmark.jid] = new_bookmark
......@@ -156,20 +159,18 @@ def _bookmark_module(self):
return 'PEPBookmarks'
return 'PrivateBookmarks'
def _act_on_changed_bookmarks(
self, old_bookmarks: Dict[str, BookmarkData]) -> None:
def _act_on_changed_bookmarks(self, current_bookmarks: BookmarksDict) -> None:
new_bookmarks = self._convert_to_set(self._bookmarks)
old_bookmarks = self._convert_to_set(old_bookmarks)
old_bookmarks = self._convert_to_set(current_bookmarks)
changed = new_bookmarks - old_bookmarks
if not changed:
join = [jid for jid, autojoin in changed if autojoin]
bookmarks = []
bookmarks: List[BookmarkData] = []
for jid in join:'Schedule autojoin in 10s for: %s', jid)
bookmarks.append(cast(BookmarkData, self._bookmarks.get(jid)))
# If another client creates a MUC, the MUC is locked until the
# configuration is finished. Give the user some time to finish
# the configuration.
......@@ -181,26 +182,24 @@ def _act_on_changed_bookmarks(
# leave = [jid for jid, autojoin in changed if not autojoin]
def _convert_to_set(
bookmarks: Dict[str, BookmarkData]) -> Set[Tuple[str, bool]]:
set_ = set()
def _convert_to_set(bookmarks: BookmarksDict) -> Set[Tuple[JID, bool]]:
set_: Set[Tuple[JID, bool]] = set()
for jid, bookmark in bookmarks.items():
set_.add((jid, bookmark.autojoin))
return set_
def _convert_to_dict(bookmarks: List) -> Dict[str, BookmarkData]:
_dict = {} # type: Dict[str, BookmarkData]
if bookmarks is None:
def _convert_to_dict(bookmarks: Optional[List[BookmarkData]]) -> BookmarksDict:
_dict: BookmarksDict = {}
if not bookmarks:
return _dict
for bookmark in bookmarks:
_dict[bookmark.jid] = bookmark
return _dict
def get_bookmark(self, jid: Union[str, JID]) -> BookmarkData:
return self._bookmarks.get(jid)
def get_bookmark(self, jid: Union[str, JID]) -> Optional[BookmarkData]:
return self._bookmarks.get(cast(JID, jid))
def request_bookmarks(self) -> None:
if not app.account_is_available(self._account):
......@@ -210,40 +209,36 @@ def request_bookmarks(self) -> None:
def _bookmarks_received(self, task: Any) -> None:
def _bookmarks_received(self, task: Task) -> None:
bookmarks = task.finish()
bookmarks: List[BookmarkData] = task.finish()
except Exception as error:
bookmarks = None
bookmarks = []
self._request_in_progress = False
self._bookmarks = self._convert_to_dict(bookmarks)
NetworkEvent('bookmarks-received', account=self._account))
def store_bookmarks(self, bookmarks: list = None) -> None:
def store_bookmarks(self, bookmarks: List[BookmarkData]) -> None:
if not app.account_is_available(self._account):
if bookmarks is None or not self.nativ_bookmarks_used:
bookmarks = self._bookmarks.values()
if not self.nativ_bookmarks_used:
bookmarks = self.bookmarks
NetworkEvent('bookmarks-received', account=self._account))
def _join_with_timeout(self, bookmarks: List[Any]) -> None:
def _join_with_timeout(self, bookmarks: List[BookmarkData]) -> None:
def auto_join_bookmarks(self,
bookmarks: Optional[List[Any]] = None) -> None:
if bookmarks is None:
bookmarks = self._bookmarks.values()
def auto_join_bookmarks(self, bookmarks: List[BookmarkData]) -> None:
for bookmark in bookmarks:
if bookmark.autojoin:
# Only join non-opened groupchats. Opened one are already
......@@ -259,7 +254,7 @@ def auto_join_bookmarks(self,
def modify(self, jid: str, **kwargs: Dict[str, str]) -> None:
def modify(self, jid: JID, **kwargs: Any) -> None:
bookmark = self._bookmarks.get(jid)
if bookmark is None:
......@@ -273,7 +268,7 @@ def modify(self, jid: str, **kwargs: Dict[str, str]) -> None:
def add_or_modify(self, jid: str, **kwargs: Dict[str, str]) -> None:
def add_or_modify(self, jid: JID, **kwargs: Any) -> None:
bookmark = self._bookmarks.get(jid)
if bookmark is not None:
self.modify(jid, **kwargs)
......@@ -291,17 +286,17 @@ def remove(self, jid: JID, publish: bool = True) -> None:
if publish:
if self.nativ_bookmarks_used:
def get_name_from_bookmark(self, jid: str) -> str:
bookmark = self._bookmarks.get(jid)
def get_name_from_bookmark(self, jid: Union[str, JID]) -> Optional[str]:
bookmark = self._bookmarks.get(cast(JID, jid))
if bookmark is None:
return ''
return bookmark
def is_bookmark(self, jid: str) -> bool:
def is_bookmark(self, jid: Union[str, JID]) -> bool:
return jid in self._bookmarks
def _remove_timeouts(self):
......@@ -28,6 +28,7 @@
from collections import defaultdict
from gi.repository import GLib
from nbxmpp.protocol import JID
from gajim import IS_PORTABLE
from gajim.common import app
......@@ -600,7 +601,7 @@ def set_account_setting(self,
def get_group_chat_setting(self,
account: str,
jid: str,
jid: Union[str, JID],
setting: str) -> SETTING_TYPE:
if account not in self._account_settings:
......@@ -24,6 +24,8 @@
from pathlib import Path
import nbxmpp
from nbxmpp.protocol import JID
from nbxmpp.structs import BookmarkData
from gajim.common.const import PathType, PathLocation
......@@ -63,3 +65,5 @@
PluginEvents = List['NetworkEvent']
SettingsT = Union['Settings']
BookmarksDict = Dict[JID, BookmarkData]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment