2019-04-12 03:22:43 +10:00
|
|
|
# Copyright 2018 miruka
|
|
|
|
# This file is part of harmonyqt, licensed under GPLv3.
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import threading
|
|
|
|
from typing import Dict, Optional
|
|
|
|
|
|
|
|
from atomicfile import AtomicFile
|
2019-04-12 18:33:09 +10:00
|
|
|
from PyQt5.QtCore import (
|
|
|
|
QObject, QStandardPaths, pyqtProperty, pyqtSignal, pyqtSlot
|
|
|
|
)
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
from harmonyqml import __about__
|
|
|
|
|
2019-04-18 13:28:25 +10:00
|
|
|
from .backend import Backend
|
2019-04-12 03:22:43 +10:00
|
|
|
from .client import Client
|
|
|
|
|
|
|
|
AccountConfig = Dict[str, Dict[str, str]]
|
|
|
|
|
2019-04-23 00:05:08 +10:00
|
|
|
_CONFIG_LOCK = threading.Lock()
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
|
|
|
|
class ClientManager(QObject):
|
2019-04-23 01:37:41 +10:00
|
|
|
clientAdded = pyqtSignal(Client)
|
|
|
|
clientDeleted = pyqtSignal(str)
|
|
|
|
_clientsUpdate = pyqtSignal()
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
|
2019-04-18 13:28:25 +10:00
|
|
|
def __init__(self, backend: Backend) -> None:
|
|
|
|
super().__init__(backend)
|
|
|
|
self.backend = backend
|
2019-04-12 03:22:43 +10:00
|
|
|
self._clients: Dict[str, Client] = {}
|
|
|
|
|
2019-04-23 01:37:41 +10:00
|
|
|
self.clientAdded.connect(self._clientsUpdate.emit)
|
|
|
|
self.clientDeleted.connect(self._clientsUpdate.emit)
|
|
|
|
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"{type(self).__name__}(clients={self.clients!r})"
|
|
|
|
|
|
|
|
|
2019-04-23 01:37:41 +10:00
|
|
|
@pyqtProperty("QVariantMap", notify=_clientsUpdate)
|
2019-04-12 03:22:43 +10:00
|
|
|
def clients(self):
|
|
|
|
return self._clients
|
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def configLoad(self) -> None:
|
|
|
|
for user_id, info in self.configAccounts().items():
|
2019-04-18 13:28:25 +10:00
|
|
|
client = Client(self, info["hostname"], user_id)
|
2019-04-12 18:33:09 +10:00
|
|
|
client.resumeSession(user_id, info["token"], info["device_id"])\
|
|
|
|
.add_done_callback(lambda _, c=client: self._on_connected(c))
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(str, str, str)
|
|
|
|
@pyqtSlot(str, str, str)
|
|
|
|
def new(self, hostname: str, username: str, password: str,
|
|
|
|
device_id: str = "") -> None:
|
|
|
|
|
2019-04-18 13:28:25 +10:00
|
|
|
client = Client(self, hostname, username, device_id)
|
2019-04-12 18:33:09 +10:00
|
|
|
client.login(password, self.defaultDeviceName)\
|
|
|
|
.add_done_callback(lambda _: self._on_connected(client))
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
|
2019-04-12 18:33:09 +10:00
|
|
|
def _on_connected(self, client: Client) -> None:
|
2019-04-21 07:36:21 +10:00
|
|
|
self.clients[client.userId] = client
|
2019-04-12 18:33:09 +10:00
|
|
|
self.clientAdded.emit(client)
|
2019-04-19 16:07:01 +10:00
|
|
|
client.startSyncing()
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(str)
|
|
|
|
def delete(self, user_id: str) -> None:
|
2019-04-12 18:33:09 +10:00
|
|
|
client = self.clients.pop(user_id, None)
|
2019-04-12 03:22:43 +10:00
|
|
|
if client:
|
2019-04-12 18:33:09 +10:00
|
|
|
self.clientDeleted.emit(user_id)
|
2019-04-12 03:22:43 +10:00
|
|
|
client.logout()
|
|
|
|
|
|
|
|
|
2019-04-12 18:33:09 +10:00
|
|
|
@pyqtProperty(str, constant=True)
|
2019-04-12 03:22:43 +10:00
|
|
|
def defaultDeviceName(self) -> str: # pylint: disable=no-self-use
|
|
|
|
os_ = f" on {platform.system()}".rstrip()
|
|
|
|
os_ = f"{os_} {platform.release()}".rstrip() if os_ != " on" else ""
|
|
|
|
return f"{__about__.__pretty_name__}{os}"
|
|
|
|
|
|
|
|
|
|
|
|
# Standard file paths
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _get_standard_path(kind: QStandardPaths.StandardLocation,
|
|
|
|
file: str,
|
|
|
|
initial_content: Optional[str] = None) -> str:
|
|
|
|
relative_path = file.replace("/", os.sep)
|
|
|
|
|
|
|
|
path = QStandardPaths.locate(kind, relative_path)
|
|
|
|
if path:
|
|
|
|
return path
|
|
|
|
|
|
|
|
base_dir = QStandardPaths.writableLocation(kind)
|
|
|
|
path = f"{base_dir}{os.sep}{relative_path}"
|
|
|
|
os.makedirs(os.path.split(path)[0], exist_ok=True)
|
|
|
|
|
|
|
|
if initial_content is not None:
|
|
|
|
with AtomicFile(path, "w") as new:
|
|
|
|
new.write(initial_content)
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
def getAccountConfigPath(self) -> str:
|
|
|
|
return self._get_standard_path(
|
|
|
|
QStandardPaths.AppConfigLocation, "accounts.json", "[]"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Config file operations
|
|
|
|
|
|
|
|
def configAccounts(self) -> AccountConfig:
|
|
|
|
with open(self.getAccountConfigPath(), "r") as file:
|
|
|
|
return json.loads(file.read().strip()) or {}
|
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(Client)
|
|
|
|
def configAdd(self, client: Client) -> None:
|
|
|
|
self._write_config({
|
|
|
|
**self.configAccounts(),
|
2019-04-21 07:36:21 +10:00
|
|
|
**{client.userId: {
|
2019-04-12 03:22:43 +10:00
|
|
|
"hostname": client.nio.host,
|
|
|
|
"token": client.nio.access_token,
|
|
|
|
"device_id": client.nio.device_id,
|
|
|
|
}}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(str)
|
|
|
|
def configDelete(self, user_id: str) -> None:
|
|
|
|
self._write_config({
|
|
|
|
uid: info
|
|
|
|
for uid, info in self.configAccounts().items() if uid != user_id
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def _write_config(self, accounts: AccountConfig) -> None:
|
|
|
|
js = json.dumps(accounts, indent=4, ensure_ascii=False, sort_keys=True)
|
|
|
|
|
|
|
|
with _CONFIG_LOCK:
|
|
|
|
with AtomicFile(self.getAccountConfigPath(), "w") as new:
|
|
|
|
new.write(js)
|