2019-07-19 10:30:41 +10:00
|
|
|
import asyncio
|
|
|
|
import json
|
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
|
|
|
import logging as log
|
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
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
import aiofiles
|
|
|
|
|
2019-09-11 07:28:16 +10:00
|
|
|
import pyotherside
|
|
|
|
|
2019-07-19 10:30:41 +10:00
|
|
|
from .backend import Backend
|
2019-07-23 17:14:02 +10:00
|
|
|
from .theme_parser import convert_to_qml
|
2019-07-25 07:20:21 +10:00
|
|
|
from .utils import dict_update_recursive
|
2019-07-19 10:30:41 +10:00
|
|
|
|
2019-07-19 11:58:21 +10:00
|
|
|
JsonData = Dict[str, Any]
|
|
|
|
|
2019-07-19 10:30:41 +10:00
|
|
|
WRITE_LOCK = asyncio.Lock()
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ConfigFile:
|
2019-07-23 17:14:02 +10:00
|
|
|
backend: Backend = field(repr=False)
|
|
|
|
filename: str = field()
|
2019-07-19 10:30:41 +10:00
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self) -> Path:
|
2019-07-23 17:14:02 +10:00
|
|
|
return Path(self.backend.app.appdirs.user_config_dir) / self.filename
|
|
|
|
|
|
|
|
|
|
|
|
async def default_data(self):
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
async def read(self):
|
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
|
|
|
log.debug("Reading config %s at %s", type(self).__name__, self.path)
|
2019-08-28 10:06:58 +10:00
|
|
|
return self.path.read_text()
|
2019-07-23 17:14:02 +10:00
|
|
|
|
|
|
|
|
|
|
|
async def write(self, data) -> None:
|
|
|
|
async with WRITE_LOCK:
|
|
|
|
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
async with aiofiles.open(self.path, "w") as new:
|
|
|
|
await new.write(data)
|
2019-07-19 10:30:41 +10:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class JSONConfigFile(ConfigFile):
|
2019-07-19 11:58:21 +10:00
|
|
|
async def default_data(self) -> JsonData:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
async def read(self) -> JsonData:
|
2019-07-19 10:30:41 +10:00
|
|
|
try:
|
2019-07-23 17:14:02 +10:00
|
|
|
data = json.loads(await super().read())
|
2019-07-25 09:56:23 +10:00
|
|
|
except (FileNotFoundError, json.JSONDecodeError):
|
2019-07-21 23:24:11 +10:00
|
|
|
data = {}
|
|
|
|
|
2019-07-25 07:20:21 +10:00
|
|
|
all_data = await self.default_data()
|
|
|
|
dict_update_recursive(all_data, data)
|
2019-08-17 05:12:14 +10:00
|
|
|
|
2019-08-31 09:43:58 +10:00
|
|
|
if data != all_data:
|
2019-08-17 05:12:14 +10:00
|
|
|
await self.write(all_data)
|
|
|
|
|
2019-07-25 07:20:21 +10:00
|
|
|
return all_data
|
2019-07-19 10:30:41 +10:00
|
|
|
|
|
|
|
|
2019-07-19 11:58:21 +10:00
|
|
|
async def write(self, data: JsonData) -> None:
|
2019-07-19 10:30:41 +10:00
|
|
|
js = json.dumps(data, indent=4, ensure_ascii=False, sort_keys=True)
|
2019-07-23 17:14:02 +10:00
|
|
|
await super().write(js)
|
2019-07-19 10:30:41 +10:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Accounts(JSONConfigFile):
|
|
|
|
filename: str = "accounts.json"
|
|
|
|
|
|
|
|
async def any_saved(self) -> bool:
|
|
|
|
return bool(await self.read())
|
|
|
|
|
|
|
|
|
|
|
|
async def add(self, user_id: str) -> None:
|
|
|
|
client = self.backend.clients[user_id]
|
|
|
|
|
|
|
|
await self.write({
|
|
|
|
**await self.read(),
|
|
|
|
client.user_id: {
|
|
|
|
"homeserver": client.homeserver,
|
|
|
|
"token": client.access_token,
|
|
|
|
"device_id": client.device_id,
|
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
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
async def delete(self, user_id: str) -> None:
|
|
|
|
await self.write({
|
|
|
|
uid: info
|
|
|
|
for uid, info in (await self.read()).items() if uid != user_id
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class UISettings(JSONConfigFile):
|
2019-07-25 10:02:31 +10:00
|
|
|
filename: str = "settings.json"
|
2019-07-19 11:58:21 +10:00
|
|
|
|
|
|
|
async def default_data(self) -> JsonData:
|
|
|
|
return {
|
2019-08-17 05:07:30 +10:00
|
|
|
"alertOnMessageForMsec": 4000,
|
2019-09-13 07:32:48 +10:00
|
|
|
"autoPlayGIF": True,
|
2019-09-08 06:39:14 +10:00
|
|
|
"clearRoomFilterOnEnter": True,
|
2019-09-08 06:46:30 +10:00
|
|
|
"clearRoomFilterOnEscape": True,
|
2019-08-17 05:07:30 +10:00
|
|
|
"theme": "Default.qpl",
|
2019-07-23 17:14:02 +10:00
|
|
|
"writeAliases": {},
|
2019-07-25 07:05:27 +10:00
|
|
|
"keys": {
|
2019-09-06 05:05:57 +10:00
|
|
|
"startPythonDebugger": "Alt+Shift+D",
|
|
|
|
"toggleDebugConsole": "Alt+Shift+C",
|
|
|
|
"reloadConfig": "Alt+Shift+R",
|
2019-08-24 01:02:22 +10:00
|
|
|
|
2019-08-31 03:06:54 +10:00
|
|
|
"scrollUp": ["Alt+Up", "Alt+K"],
|
|
|
|
"scrollDown": ["Alt+Down", "Alt+J"],
|
|
|
|
"scrollPageUp": ["Alt+Ctrl+Up", "Alt+Ctrl+K", "PageUp"],
|
|
|
|
"scrollPageDown": ["Alt+Ctrl+Down", "Alt+Ctrl+J", "PageDown"],
|
2019-08-31 03:40:56 +10:00
|
|
|
"scrollToTop":
|
|
|
|
["Alt+Ctrl+Shift+Up", "Alt+Ctrl+Shift+K", "Home"],
|
|
|
|
"scrollToBottom":
|
|
|
|
["Alt+Ctrl+Shift+Down", "Alt+Ctrl+Shift+J", "End"],
|
2019-08-24 01:02:22 +10:00
|
|
|
|
2019-08-20 03:55:21 +10:00
|
|
|
"focusSidePane": ["Alt+S", "Ctrl+S"],
|
2019-08-20 04:05:59 +10:00
|
|
|
"clearRoomFilter": ["Alt+Shift+S", "Ctrl+Shift+S"],
|
2019-08-24 01:02:22 +10:00
|
|
|
"addNewAccount": ["Alt+N"],
|
2019-08-20 04:28:12 +10:00
|
|
|
|
2019-08-24 01:02:22 +10:00
|
|
|
"goToPreviousRoom": ["Alt+Shift+Up", "Alt+Shift+K"],
|
|
|
|
"goToNextRoom": ["Alt+Shift+Down", "Alt+Shift+J"],
|
2019-08-20 04:28:12 +10:00
|
|
|
"toggleCollapseAccount": [
|
|
|
|
"Alt+Shift+Left", "Alt+Shift+Right",
|
2019-08-21 03:01:22 +10:00
|
|
|
"Alt+Shift+H", "Alt+Shift+L",
|
2019-08-20 04:28:12 +10:00
|
|
|
],
|
2019-09-09 01:49:47 +10:00
|
|
|
|
|
|
|
"clearRoomMessages": ["Ctrl+L"],
|
2019-07-25 07:05:27 +10:00
|
|
|
},
|
2019-07-19 11:58:21 +10:00
|
|
|
}
|
2019-07-21 20:05:01 +10:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class UIState(JSONConfigFile):
|
2019-07-25 10:02:31 +10:00
|
|
|
filename: str = "state.json"
|
2019-07-23 17:14:02 +10:00
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self) -> Path:
|
|
|
|
return Path(self.backend.app.appdirs.user_data_dir) / self.filename
|
|
|
|
|
2019-07-21 20:05:01 +10:00
|
|
|
|
|
|
|
async def default_data(self) -> JsonData:
|
|
|
|
return {
|
2019-07-21 23:24:11 +10:00
|
|
|
"collapseAccounts": {},
|
2019-07-21 23:08:22 +10:00
|
|
|
"page": "Pages/Default.qml",
|
|
|
|
"pageProperties": {},
|
2019-08-16 01:29:22 +10:00
|
|
|
"sidePaneFilter": "",
|
2019-07-21 23:08:22 +10:00
|
|
|
"sidePaneManualWidth": None,
|
2019-07-21 20:05:01 +10:00
|
|
|
}
|
2019-07-23 17:14:02 +10:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Theme(ConfigFile):
|
|
|
|
@property
|
|
|
|
def path(self) -> Path:
|
|
|
|
data_dir = Path(self.backend.app.appdirs.user_data_dir)
|
2019-07-25 08:09:28 +10:00
|
|
|
return data_dir / "themes" / self.filename
|
2019-07-23 17:14:02 +10:00
|
|
|
|
|
|
|
|
|
|
|
async def default_data(self) -> str:
|
2019-09-01 19:42:51 +10:00
|
|
|
async with aiofiles.open("src/themes/Default.qpl") as file:
|
2019-07-25 08:09:28 +10:00
|
|
|
return await file.read()
|
2019-07-23 17:14:02 +10:00
|
|
|
|
|
|
|
|
|
|
|
async def read(self) -> str:
|
2019-07-25 08:09:28 +10:00
|
|
|
if not self.path.exists():
|
|
|
|
await self.write(await self.default_data())
|
2019-07-23 17:14:02 +10:00
|
|
|
|
2019-07-25 08:09:28 +10:00
|
|
|
return convert_to_qml(await super().read())
|