2020-09-24 09:57:54 +10:00
|
|
|
# Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
|
2019-12-19 22:46:16 +11:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
2019-12-18 23:41:02 +11:00
|
|
|
"""User data and configuration files definitions."""
|
|
|
|
|
2019-07-19 10:30:41 +10:00
|
|
|
import asyncio
|
|
|
|
import json
|
2020-03-28 23:01:26 +11:00
|
|
|
import os
|
2020-10-08 11:12:32 +11:00
|
|
|
import traceback
|
2020-10-05 18:06:07 +11:00
|
|
|
from collections.abc import MutableMapping
|
2019-09-11 07:28:16 +10:00
|
|
|
from dataclasses import dataclass, field
|
2019-07-19 10:30:41 +10:00
|
|
|
from pathlib import Path
|
2020-10-08 11:12:32 +11:00
|
|
|
from typing import (
|
|
|
|
TYPE_CHECKING, Any, ClassVar, Dict, Iterator, Optional, Tuple,
|
|
|
|
)
|
2019-07-19 10:30:41 +10:00
|
|
|
|
2020-03-15 08:33:13 +11:00
|
|
|
import pyotherside
|
2020-11-16 05:57:00 +11:00
|
|
|
from watchgod import Change, awatch
|
2020-03-15 08:33:13 +11:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
from .pcn.section import Section
|
2021-01-20 04:22:29 +11:00
|
|
|
from .pyotherside_events import (
|
|
|
|
LoopException, Pre070SettingsDetected, UserFileChanged,
|
|
|
|
)
|
2019-07-23 17:14:02 +10:00
|
|
|
from .theme_parser import convert_to_qml
|
2020-11-16 05:57:00 +11:00
|
|
|
from .utils import (
|
|
|
|
aiopen, atomic_write, deep_serialize_for_qml, dict_update_recursive,
|
2020-11-17 23:24:55 +11:00
|
|
|
flatten_dict_keys,
|
2020-11-16 05:57:00 +11:00
|
|
|
)
|
2019-07-19 10:30:41 +10:00
|
|
|
|
2020-02-12 07:22:05 +11:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .backend import Backend
|
|
|
|
|
2019-07-19 10:30:41 +10:00
|
|
|
|
|
|
|
@dataclass
|
2020-10-05 18:06:07 +11:00
|
|
|
class UserFile:
|
|
|
|
"""Base class representing a user config or data file."""
|
2019-12-18 23:41:02 +11:00
|
|
|
|
2020-02-15 03:21:24 +11:00
|
|
|
create_missing: ClassVar[bool] = True
|
2019-12-18 23:41:02 +11:00
|
|
|
|
2021-01-04 18:31:26 +11:00
|
|
|
backend: "Backend" = field(repr=False)
|
|
|
|
filename: str = field()
|
|
|
|
parent: Optional["UserFile"] = None
|
|
|
|
children: Dict[Path, "UserFile"] = field(default_factory=dict)
|
2019-07-19 10:30:41 +10:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
data: Any = field(init=False, default_factory=dict)
|
|
|
|
_need_write: bool = field(init=False, default=False)
|
|
|
|
_mtime: Optional[float] = field(init=False, default=None)
|
2019-12-11 08:59:04 +11:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
_reader: Optional[asyncio.Future] = field(init=False, default=None)
|
|
|
|
_writer: Optional[asyncio.Future] = field(init=False, default=None)
|
2019-12-11 08:59:04 +11:00
|
|
|
|
|
|
|
def __post_init__(self) -> None:
|
2021-01-04 23:29:59 +11:00
|
|
|
self.data = self.default_data
|
|
|
|
self._need_write = self.create_missing
|
|
|
|
|
2021-01-04 18:31:26 +11:00
|
|
|
if self.path.exists():
|
2021-01-04 23:29:59 +11:00
|
|
|
try:
|
|
|
|
text = self.path.read_text()
|
|
|
|
self.data, self._need_write = self.deserialized(text)
|
|
|
|
except Exception as err: # noqa
|
|
|
|
LoopException(str(err), err, traceback.format_exc().rstrip())
|
2019-12-11 08:59:04 +11:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
self._reader = asyncio.ensure_future(self._start_reader())
|
|
|
|
self._writer = asyncio.ensure_future(self._start_writer())
|
2019-12-11 08:59:04 +11:00
|
|
|
|
2019-07-19 10:30:41 +10:00
|
|
|
@property
|
|
|
|
def path(self) -> Path:
|
2020-10-08 11:12:32 +11:00
|
|
|
"""Full path of the file to read, can exist or not exist."""
|
2020-10-05 18:06:07 +11:00
|
|
|
raise NotImplementedError()
|
2020-03-28 23:01:26 +11:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
@property
|
|
|
|
def write_path(self) -> Path:
|
|
|
|
"""Full path of the file to write, can exist or not exist."""
|
|
|
|
return self.path
|
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
@property
|
|
|
|
def default_data(self) -> Any:
|
|
|
|
"""Default deserialized content to use if the file doesn't exist."""
|
|
|
|
raise NotImplementedError()
|
2019-07-23 17:14:02 +10:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
@property
|
|
|
|
def qml_data(self) -> Any:
|
|
|
|
"""Data converted for usage in QML."""
|
|
|
|
return self.data
|
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
def deserialized(self, data: str) -> Tuple[Any, bool]:
|
|
|
|
"""Return parsed data from file text and whether to call `save()`."""
|
|
|
|
return (data, False)
|
2019-07-23 17:14:02 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
def serialized(self) -> str:
|
|
|
|
"""Return text from `UserFile.data` that can be written to disk."""
|
|
|
|
raise NotImplementedError()
|
2019-12-18 23:41:02 +11:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
def save(self) -> None:
|
|
|
|
"""Inform the disk writer coroutine that the data has changed."""
|
|
|
|
self._need_write = True
|
2019-07-23 17:14:02 +10:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
def stop_watching(self) -> None:
|
|
|
|
"""Stop watching the on-disk file for changes."""
|
|
|
|
if self._reader:
|
|
|
|
self._reader.cancel()
|
|
|
|
|
|
|
|
if self._writer:
|
|
|
|
self._writer.cancel()
|
|
|
|
|
2021-01-04 18:31:26 +11:00
|
|
|
for child in self.children.values():
|
|
|
|
child.stop_watching()
|
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
async def set_data(self, data: Any) -> None:
|
|
|
|
"""Set `data` and call `save()`, conveniance method for QML."""
|
|
|
|
self.data = data
|
|
|
|
self.save()
|
2019-07-23 17:14:02 +10:00
|
|
|
|
2021-01-04 18:31:26 +11:00
|
|
|
async def update_from_file(self) -> None:
|
|
|
|
"""Read file at `path`, update `data` and call `save()` if needed."""
|
|
|
|
|
|
|
|
if not self.path.exists():
|
|
|
|
self.data = self.default_data
|
|
|
|
self._need_write = self.create_missing
|
|
|
|
return
|
|
|
|
|
|
|
|
async with aiopen(self.path) as file:
|
|
|
|
self.data, self._need_write = self.deserialized(await file.read())
|
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
async def _start_reader(self) -> None:
|
|
|
|
"""Disk reader coroutine, watches for file changes to update `data`."""
|
2020-07-24 15:01:37 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
while not self.path.exists():
|
|
|
|
await asyncio.sleep(1)
|
2019-12-18 23:41:02 +11:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
async for changes in awatch(self.path):
|
2020-10-08 11:12:32 +11:00
|
|
|
try:
|
|
|
|
ignored = 0
|
|
|
|
|
|
|
|
for change in changes:
|
|
|
|
if change[0] in (Change.added, Change.modified):
|
2020-11-16 11:11:30 +11:00
|
|
|
mtime = self.path.stat().st_mtime
|
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
if mtime == self._mtime:
|
|
|
|
ignored += 1
|
|
|
|
continue
|
2020-02-15 03:21:24 +11:00
|
|
|
|
2021-01-04 18:31:26 +11:00
|
|
|
await self.update_from_file()
|
2020-11-16 11:11:30 +11:00
|
|
|
self._mtime = mtime
|
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
elif change[0] == Change.deleted:
|
2020-11-16 11:11:30 +11:00
|
|
|
self._mtime = None
|
2020-10-08 11:12:32 +11:00
|
|
|
self.data = self.default_data
|
|
|
|
self._need_write = self.create_missing
|
2019-07-23 17:14:02 +10:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
if changes and ignored < len(changes):
|
|
|
|
UserFileChanged(type(self), self.qml_data)
|
|
|
|
|
2021-01-04 18:31:26 +11:00
|
|
|
parent = self.parent
|
|
|
|
while parent:
|
|
|
|
await parent.update_from_file()
|
|
|
|
UserFileChanged(type(parent), parent.qml_data)
|
|
|
|
parent = parent.parent
|
|
|
|
|
2020-11-16 11:11:30 +11:00
|
|
|
while not self.path.exists():
|
|
|
|
# Prevent error spam after file gets deleted
|
|
|
|
await asyncio.sleep(0.5)
|
|
|
|
|
2020-11-16 05:57:00 +11:00
|
|
|
except Exception as err: # noqa
|
2020-10-08 11:12:32 +11:00
|
|
|
LoopException(str(err), err, traceback.format_exc().rstrip())
|
2019-12-11 08:59:04 +11:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
async def _start_writer(self) -> None:
|
|
|
|
"""Disk writer coroutine, update the file with a 1 second cooldown."""
|
2019-12-18 23:41:02 +11:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
self.write_path.parent.mkdir(parents=True, exist_ok=True)
|
2019-07-23 17:14:02 +10:00
|
|
|
|
2019-12-11 08:59:04 +11:00
|
|
|
while True:
|
2020-02-15 03:21:24 +11:00
|
|
|
await asyncio.sleep(1)
|
2019-12-11 08:59:04 +11:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
try:
|
|
|
|
if self._need_write:
|
|
|
|
async with atomic_write(self.write_path) as (new, done):
|
|
|
|
await new.write(self.serialized())
|
|
|
|
done()
|
|
|
|
|
|
|
|
self._need_write = False
|
|
|
|
self._mtime = self.write_path.stat().st_mtime
|
2019-12-11 08:59:04 +11:00
|
|
|
|
2020-11-16 05:57:00 +11:00
|
|
|
except Exception as err: # noqa
|
2020-10-05 18:06:07 +11:00
|
|
|
self._need_write = False
|
2020-10-08 11:12:32 +11:00
|
|
|
LoopException(str(err), err, traceback.format_exc().rstrip())
|
2020-02-15 03:21:24 +11:00
|
|
|
|
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
@dataclass
|
|
|
|
class ConfigFile(UserFile):
|
|
|
|
"""A file that goes in the configuration directory, e.g. ~/.config/app."""
|
2019-07-19 10:30:41 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
@property
|
|
|
|
def path(self) -> Path:
|
|
|
|
return Path(
|
|
|
|
os.environ.get("MIRAGE_CONFIG_DIR") or
|
|
|
|
self.backend.appdirs.user_config_dir,
|
|
|
|
) / self.filename
|
2019-07-19 10:30:41 +10:00
|
|
|
|
2019-12-18 23:41:02 +11:00
|
|
|
|
2019-07-19 10:30:41 +10:00
|
|
|
@dataclass
|
2020-10-05 18:06:07 +11:00
|
|
|
class UserDataFile(UserFile):
|
|
|
|
"""A file that goes in the user data directory, e.g. ~/.local/share/app."""
|
2020-03-23 03:01:22 +11:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
@property
|
|
|
|
def path(self) -> Path:
|
|
|
|
return Path(
|
|
|
|
os.environ.get("MIRAGE_DATA_DIR") or
|
|
|
|
self.backend.appdirs.user_data_dir,
|
|
|
|
) / self.filename
|
2020-03-23 03:01:22 +11:00
|
|
|
|
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
@dataclass
|
|
|
|
class MappingFile(MutableMapping, UserFile):
|
|
|
|
"""A file manipulable like a dict. `data` must be a mutable mapping."""
|
2020-10-08 11:12:32 +11:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
def __getitem__(self, key: Any) -> Any:
|
|
|
|
return self.data[key]
|
2020-03-23 03:01:22 +11:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
def __setitem__(self, key: Any, value: Any) -> None:
|
|
|
|
self.data[key] = value
|
2020-03-23 03:01:22 +11:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
def __delitem__(self, key: Any) -> None:
|
|
|
|
del self.data[key]
|
2019-07-19 11:58:21 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
def __iter__(self) -> Iterator:
|
|
|
|
return iter(self.data)
|
2019-07-19 11:58:21 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
def __len__(self) -> int:
|
|
|
|
return len(self.data)
|
2019-12-18 23:41:02 +11:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
def __getattr__(self, key: Any) -> Any:
|
|
|
|
try:
|
|
|
|
return self.data[key]
|
|
|
|
except KeyError:
|
|
|
|
return super().__getattribute__(key)
|
|
|
|
|
|
|
|
def __setattr__(self, key: Any, value: Any) -> None:
|
|
|
|
if key in self.__dataclass_fields__:
|
|
|
|
super().__setattr__(key, value)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.data[key] = value
|
|
|
|
|
|
|
|
def __delattr__(self, key: Any) -> None:
|
|
|
|
del self.data[key]
|
|
|
|
|
2020-02-15 03:21:24 +11:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
@dataclass
|
|
|
|
class JSONFile(MappingFile):
|
|
|
|
"""A file stored on disk in the JSON format."""
|
2020-02-15 03:21:24 +11:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
@property
|
|
|
|
def default_data(self) -> dict:
|
|
|
|
return {}
|
2020-07-24 15:01:37 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
def deserialized(self, data: str) -> Tuple[dict, bool]:
|
|
|
|
"""Return parsed data from file text and whether to call `save()`.
|
2019-07-21 23:24:11 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
If the file has missing keys, the missing data will be merged to the
|
|
|
|
returned dict and the second tuple item will be `True`.
|
|
|
|
"""
|
2019-08-17 05:12:14 +10:00
|
|
|
|
2020-10-07 14:18:33 +11:00
|
|
|
loaded = json.loads(data)
|
2020-10-05 18:06:07 +11:00
|
|
|
all_data = self.default_data.copy()
|
|
|
|
dict_update_recursive(all_data, loaded)
|
|
|
|
return (all_data, loaded != all_data)
|
2019-07-19 10:30:41 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
def serialized(self) -> str:
|
|
|
|
data = self.data
|
|
|
|
return json.dumps(data, indent=4, ensure_ascii=False, sort_keys=True)
|
2019-07-19 10:30:41 +10:00
|
|
|
|
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
@dataclass
|
|
|
|
class PCNFile(MappingFile):
|
|
|
|
"""File stored in the PCN format, with machine edits in a separate JSON."""
|
|
|
|
|
|
|
|
create_missing = False
|
|
|
|
|
2021-01-04 18:31:26 +11:00
|
|
|
path_override: Optional[Path] = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self) -> Path:
|
|
|
|
return self.path_override or super().path
|
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
@property
|
|
|
|
def write_path(self) -> Path:
|
|
|
|
"""Full path of file where programatically-done edits are stored."""
|
|
|
|
return self.path.with_suffix(".gui.json")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def qml_data(self) -> Dict[str, Any]:
|
|
|
|
return deep_serialize_for_qml(self.data.as_dict()) # type: ignore
|
|
|
|
|
2020-11-17 23:24:55 +11:00
|
|
|
@property
|
|
|
|
def default_data(self) -> Section:
|
|
|
|
return Section()
|
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
def deserialized(self, data: str) -> Tuple[Section, bool]:
|
|
|
|
root = Section.from_source_code(data, self.path)
|
2020-11-16 10:05:04 +11:00
|
|
|
edits = "{}"
|
|
|
|
|
|
|
|
if self.write_path.exists():
|
|
|
|
edits = self.write_path.read_text()
|
|
|
|
|
2021-01-04 18:31:26 +11:00
|
|
|
includes_now = list(root.all_includes)
|
|
|
|
|
|
|
|
for path, pcn in self.children.copy().items():
|
|
|
|
if path not in includes_now:
|
|
|
|
pcn.stop_watching()
|
|
|
|
del self.children[path]
|
|
|
|
|
|
|
|
for path in includes_now:
|
|
|
|
if path not in self.children:
|
|
|
|
self.children[path] = PCNFile(
|
|
|
|
self.backend,
|
|
|
|
filename = path.name,
|
|
|
|
parent = self,
|
|
|
|
path_override = path,
|
|
|
|
)
|
|
|
|
|
2020-11-15 02:30:03 +11:00
|
|
|
return (root, root.deep_merge_edits(json.loads(edits)))
|
2020-10-08 11:12:32 +11:00
|
|
|
|
|
|
|
def serialized(self) -> str:
|
|
|
|
edits = self.data.edits_as_dict()
|
|
|
|
return json.dumps(edits, indent=4, ensure_ascii=False)
|
|
|
|
|
|
|
|
async def set_data(self, data: Dict[str, Any]) -> None:
|
|
|
|
self.data.deep_merge_edits({"set": data}, has_expressions=False)
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
|
2019-07-19 10:30:41 +10:00
|
|
|
@dataclass
|
2020-10-05 18:06:07 +11:00
|
|
|
class Accounts(ConfigFile, JSONFile):
|
|
|
|
"""Config file for saved matrix accounts: user ID, access tokens, etc"""
|
2019-12-18 23:41:02 +11:00
|
|
|
|
2019-07-19 10:30:41 +10:00
|
|
|
filename: str = "accounts.json"
|
|
|
|
|
|
|
|
async def any_saved(self) -> bool:
|
2020-10-05 18:06:07 +11:00
|
|
|
"""Return for QML whether there are any accounts saved on disk."""
|
|
|
|
return bool(self.data)
|
2019-07-19 10:30:41 +10:00
|
|
|
|
|
|
|
async def add(self, user_id: str) -> None:
|
2019-12-18 23:41:02 +11:00
|
|
|
"""Add an account to the config and write it on disk.
|
|
|
|
|
|
|
|
The account's details such as its access token are retrieved from
|
|
|
|
the corresponding `MatrixClient` in `backend.clients`.
|
|
|
|
"""
|
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
client = self.backend.clients[user_id]
|
|
|
|
account = self.backend.models["accounts"][user_id]
|
2019-07-19 10:30:41 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
self.update({
|
2019-07-19 10:30:41 +10:00
|
|
|
client.user_id: {
|
|
|
|
"homeserver": client.homeserver,
|
|
|
|
"token": client.access_token,
|
|
|
|
"device_id": client.device_id,
|
2020-04-06 05:04:40 +10:00
|
|
|
"enabled": True,
|
2020-07-10 06:06:14 +10:00
|
|
|
"presence": account.presence.value,
|
2020-07-17 06:09:14 +10:00
|
|
|
"status_msg": account.status_msg,
|
2020-07-11 00:59:26 +10:00
|
|
|
"order": account.order,
|
Big performance refactoring & various improvements
Instead of passing all sorts of events for the JS to handle and manually
add to different data models, we now handle everything we can in Python.
For any change, the python models send a sync event with their
contents (no more than 4 times per second) to JS, and the QSyncable
library's JsonListModel takes care of converting it to a QML ListModel
and sending the appropriate signals.
The SortFilterProxyModel library is not used anymore, the only case
where we need to filter/sort something now is when the user interacts
with the "Filter rooms" or "Filter members" fields. These cases are
handled by a simple JS function.
We now keep separated room and timeline models for different accounts,
the previous approach of sharing all the data we could between accounts
created a lot of complications (local echoes, decrypted messages
replacing others, etc).
The users's own account profile changes are now hidden in the timeline.
On startup, if all events for a room were only own profile changes, more
events will be loaded.
Any kind of image format supported by Qt is now handled by the
pyotherside image provider, instead of just PNG/JPG.
SVGs which previously caused errors are supported as well.
The typing members bar paddings/margins are fixed.
The behavior of the avatar/"upload a profile picture" overlay is fixed.
Config files read from disk are now cached (TODO: make them reloadable
again).
Pylint is not used anymore because of all its annoying false warnings
and lack of understanding for dataclasses, it is replaced by flake8 with
a custom config and various plugins.
Debug mode is now considered on if the program was compiled with
the right option, instead of taking an argument from CLI.
When on, C++ will set a flag in the Window QML component.
The loading screen is now unloaded after the UI is ready, where
previously it just stayed in the background invisible and wasted CPU.
The overall refactoring and improvements make us now able to handle
rooms with thousand of members and no lazy-loading, where previously
everything would freeze and simply scrolling up to load past events
in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
|
|
|
},
|
2019-07-19 10:30:41 +10:00
|
|
|
})
|
2020-10-05 18:06:07 +11:00
|
|
|
self.save()
|
2019-07-19 10:30:41 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
async def set(
|
2020-07-10 06:06:14 +10:00
|
|
|
self,
|
2020-07-17 06:09:14 +10:00
|
|
|
user_id: str,
|
|
|
|
enabled: Optional[str] = None,
|
|
|
|
presence: Optional[str] = None,
|
|
|
|
order: Optional[int] = None,
|
|
|
|
status_msg: Optional[str] = None,
|
2020-07-10 06:06:14 +10:00
|
|
|
) -> None:
|
2020-07-12 07:11:04 +10:00
|
|
|
"""Update an account if found in the config file and write to disk."""
|
2020-07-10 06:06:14 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
if user_id not in self:
|
2020-07-12 07:11:04 +10:00
|
|
|
return
|
|
|
|
|
2020-07-12 04:36:30 +10:00
|
|
|
if enabled is not None:
|
2020-10-05 18:06:07 +11:00
|
|
|
self[user_id]["enabled"] = enabled
|
2020-07-12 04:36:30 +10:00
|
|
|
|
|
|
|
if presence is not None:
|
2020-10-05 18:06:07 +11:00
|
|
|
self[user_id]["presence"] = presence
|
2020-07-12 04:36:30 +10:00
|
|
|
|
|
|
|
if order is not None:
|
2020-10-05 18:06:07 +11:00
|
|
|
self[user_id]["order"] = order
|
2020-07-10 06:06:14 +10:00
|
|
|
|
2020-07-17 06:09:14 +10:00
|
|
|
if status_msg is not None:
|
2020-10-05 18:06:07 +11:00
|
|
|
self[user_id]["status_msg"] = status_msg
|
2020-07-17 06:09:14 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
self.save()
|
2020-07-10 06:06:14 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
async def forget(self, user_id: str) -> None:
|
2019-12-18 23:41:02 +11:00
|
|
|
"""Delete an account from the config and write it on disk."""
|
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
self.pop(user_id, None)
|
|
|
|
self.save()
|
2019-07-19 10:30:41 +10:00
|
|
|
|
|
|
|
|
2021-01-20 04:22:29 +11:00
|
|
|
@dataclass
|
|
|
|
class Pre070Settings(ConfigFile):
|
|
|
|
"""Detect and warn about the presence of a pre-0.7.0 settings.json file."""
|
|
|
|
|
|
|
|
filename: str = "settings.json"
|
|
|
|
|
|
|
|
def __post_init__(self) -> None:
|
|
|
|
if self.path.exists():
|
|
|
|
Pre070SettingsDetected(self.path)
|
|
|
|
|
|
|
|
|
2019-07-19 10:30:41 +10:00
|
|
|
@dataclass
|
2020-10-08 11:12:32 +11:00
|
|
|
class Settings(ConfigFile, PCNFile):
|
2020-10-05 18:06:07 +11:00
|
|
|
"""General config file for UI and backend settings"""
|
2019-12-18 23:41:02 +11:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
filename: str = "settings.py"
|
2019-07-19 11:58:21 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
@property
|
2020-10-08 11:12:32 +11:00
|
|
|
def default_data(self) -> Section:
|
|
|
|
root = Section.from_file("src/config/settings.py")
|
|
|
|
edits = "{}"
|
2020-05-14 16:48:48 +10:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
if self.write_path.exists():
|
|
|
|
edits = self.write_path.read_text()
|
2020-04-01 20:08:08 +11:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
root.deep_merge_edits(json.loads(edits))
|
|
|
|
return root
|
2019-07-21 20:05:01 +10:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
def deserialized(self, data: str) -> Tuple[Section, bool]:
|
|
|
|
section, save = super().deserialized(data)
|
2020-10-05 18:06:07 +11:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
if self and self.General.theme != section.General.theme:
|
2021-01-04 23:29:59 +11:00
|
|
|
if hasattr(self.backend, "theme"):
|
|
|
|
self.backend.theme.stop_watching()
|
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
self.backend.theme = Theme(
|
|
|
|
self.backend, section.General.theme, # type: ignore
|
|
|
|
)
|
2020-11-17 23:24:55 +11:00
|
|
|
UserFileChanged(Theme, self.backend.theme.qml_data)
|
|
|
|
|
2021-01-04 15:45:11 +11:00
|
|
|
# if self and self.General.new_theme != section.General.new_theme:
|
|
|
|
# self.backend.new_theme.stop_watching()
|
|
|
|
# self.backend.new_theme = NewTheme(
|
|
|
|
# self.backend, section.General.new_theme, # type: ignore
|
|
|
|
# )
|
|
|
|
# UserFileChanged(Theme, self.backend.new_theme.qml_data)
|
2020-10-05 18:06:07 +11:00
|
|
|
|
2020-10-08 11:12:32 +11:00
|
|
|
return (section, save)
|
2020-10-05 18:06:07 +11:00
|
|
|
|
2019-07-21 20:05:01 +10:00
|
|
|
|
2020-11-17 23:24:55 +11:00
|
|
|
@dataclass
|
|
|
|
class NewTheme(UserDataFile, PCNFile):
|
|
|
|
"""A theme file defining the look of QML components."""
|
|
|
|
|
|
|
|
create_missing = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self) -> Path:
|
|
|
|
data_dir = Path(
|
|
|
|
os.environ.get("MIRAGE_DATA_DIR") or
|
|
|
|
self.backend.appdirs.user_data_dir,
|
|
|
|
)
|
|
|
|
return data_dir / "themes" / self.filename
|
|
|
|
|
|
|
|
@property
|
|
|
|
def qml_data(self) -> Dict[str, Any]:
|
|
|
|
return flatten_dict_keys(super().qml_data, last_level=False)
|
|
|
|
|
|
|
|
|
2019-07-21 20:05:01 +10:00
|
|
|
@dataclass
|
2020-10-05 18:06:07 +11:00
|
|
|
class UIState(UserDataFile, JSONFile):
|
|
|
|
"""File used to save and restore the state of QML components."""
|
2019-12-11 08:59:04 +11:00
|
|
|
|
2019-12-18 23:41:02 +11:00
|
|
|
filename: str = "state.json"
|
2019-07-23 17:14:02 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
@property
|
|
|
|
def default_data(self) -> dict:
|
2019-07-21 20:05:01 +10:00
|
|
|
return {
|
2019-12-11 05:46:05 +11:00
|
|
|
"collapseAccounts": {},
|
|
|
|
"page": "Pages/Default.qml",
|
|
|
|
"pageProperties": {},
|
2019-07-21 20:05:01 +10:00
|
|
|
}
|
2019-07-23 17:14:02 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
def deserialized(self, data: str) -> Tuple[dict, bool]:
|
|
|
|
dict_data, save = super().deserialized(data)
|
|
|
|
|
|
|
|
for user_id, do in dict_data["collapseAccounts"].items():
|
|
|
|
self.backend.models["all_rooms"].set_account_collapse(user_id, do)
|
|
|
|
|
|
|
|
return (dict_data, save)
|
|
|
|
|
2019-07-23 17:14:02 +10:00
|
|
|
|
2019-12-10 04:21:12 +11:00
|
|
|
@dataclass
|
2020-10-05 18:06:07 +11:00
|
|
|
class History(UserDataFile, JSONFile):
|
2019-12-18 23:41:02 +11:00
|
|
|
"""File to save and restore lines typed by the user in QML components."""
|
2019-12-11 08:59:04 +11:00
|
|
|
|
2019-12-18 23:41:02 +11:00
|
|
|
filename: str = "history.json"
|
2019-12-10 04:21:12 +11:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
@property
|
|
|
|
def default_data(self) -> dict:
|
2019-12-10 04:21:12 +11:00
|
|
|
return {"console": []}
|
|
|
|
|
|
|
|
|
2019-07-23 17:14:02 +10:00
|
|
|
@dataclass
|
2020-10-05 18:06:07 +11:00
|
|
|
class Theme(UserDataFile):
|
2019-12-18 23:41:02 +11:00
|
|
|
"""A theme file defining the look of QML components."""
|
|
|
|
|
2020-02-15 03:21:24 +11:00
|
|
|
# Since it currently breaks at every update and the file format will be
|
|
|
|
# changed later, don't copy the theme to user data dir if it doesn't exist.
|
|
|
|
create_missing = False
|
|
|
|
|
2019-07-23 17:14:02 +10:00
|
|
|
@property
|
|
|
|
def path(self) -> Path:
|
2020-09-02 04:05:19 +10:00
|
|
|
data_dir = Path(
|
|
|
|
os.environ.get("MIRAGE_DATA_DIR") or
|
|
|
|
self.backend.appdirs.user_data_dir,
|
|
|
|
)
|
2020-03-15 08:33:13 +11:00
|
|
|
return data_dir / "themes" / self.filename
|
2019-07-23 17:14:02 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
@property
|
|
|
|
def default_data(self) -> str:
|
2021-01-31 07:21:16 +11:00
|
|
|
if self.filename in ("Midnight.qpl", "Glass.qpl"):
|
|
|
|
path = f"src/themes/{self.filename}"
|
|
|
|
else:
|
|
|
|
path = "src/themes/Midnight.qpl"
|
2020-03-15 08:33:13 +11:00
|
|
|
|
|
|
|
try:
|
|
|
|
byte_content = pyotherside.qrc_get_file_contents(path)
|
|
|
|
except ValueError:
|
|
|
|
# App was compiled without QRC
|
2020-10-05 18:06:07 +11:00
|
|
|
return convert_to_qml(Path(path).read_text())
|
2020-03-15 08:33:13 +11:00
|
|
|
else:
|
2020-10-05 18:06:07 +11:00
|
|
|
return convert_to_qml(byte_content.decode())
|
2019-07-23 17:14:02 +10:00
|
|
|
|
2020-10-05 18:06:07 +11:00
|
|
|
def deserialized(self, data: str) -> Tuple[str, bool]:
|
|
|
|
return (convert_to_qml(data), False)
|