2019-07-19 10:30:41 +10:00
|
|
|
# Copyright 2019 miruka
|
|
|
|
# This file is part of harmonyqml, licensed under LGPLv3.
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import json
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
|
|
import aiofiles
|
|
|
|
from dataclasses import dataclass, field
|
|
|
|
|
|
|
|
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:
|
|
|
|
# pylint: disable=no-member
|
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):
|
|
|
|
try:
|
|
|
|
return self.path.read_text()
|
|
|
|
except FileNotFoundError:
|
|
|
|
return await self.default_data()
|
|
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
except 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)
|
|
|
|
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:
|
|
|
|
# pylint: disable=no-member
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
filename: str = "ui-settings.json"
|
2019-07-19 11:58:21 +10:00
|
|
|
|
|
|
|
async def default_data(self) -> JsonData:
|
|
|
|
return {
|
2019-07-23 17:14:02 +10:00
|
|
|
"theme": "Default.qpl",
|
|
|
|
"writeAliases": {},
|
2019-07-25 07:05:27 +10:00
|
|
|
"keys": {
|
2019-07-25 07:26:40 +10:00
|
|
|
"reloadConfig": ["Alt+R"],
|
2019-07-25 07:05:27 +10:00
|
|
|
"scrollUp": ["Alt+Up", "Alt+K"],
|
|
|
|
"scrollDown": ["Alt+Down", "Alt+J"],
|
|
|
|
"startDebugger": ["Alt+Shift+D"],
|
|
|
|
},
|
2019-07-19 11:58:21 +10:00
|
|
|
}
|
2019-07-21 20:05:01 +10:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class UIState(JSONConfigFile):
|
2019-07-23 17:14:02 +10:00
|
|
|
filename: str = "ui-state.json"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def path(self) -> Path:
|
|
|
|
# pylint: disable=no-member
|
|
|
|
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": {},
|
|
|
|
"collapseCategories": {},
|
2019-07-21 23:08:22 +10:00
|
|
|
"page": "Pages/Default.qml",
|
|
|
|
"pageProperties": {},
|
|
|
|
"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:
|
|
|
|
# pylint: disable=no-member
|
|
|
|
data_dir = Path(self.backend.app.appdirs.user_data_dir)
|
|
|
|
user_file = data_dir / "themes" / self.filename
|
|
|
|
|
|
|
|
if user_file.exists():
|
|
|
|
return user_file
|
|
|
|
|
|
|
|
return Path("src") / "themes" / self.filename
|
|
|
|
|
|
|
|
|
|
|
|
async def default_data(self) -> str:
|
|
|
|
async with aiofiles.open("src/themes/Default.qpl", "r") as file:
|
|
|
|
return file.read()
|
|
|
|
|
|
|
|
|
|
|
|
async def read(self) -> str:
|
|
|
|
return convert_to_qml(await super().read())
|
|
|
|
|
|
|
|
|
|
|
|
async def write(self, data: str) -> None:
|
|
|
|
raise NotImplementedError()
|