2019-04-12 03:22:43 +10:00
|
|
|
# Copyright 2018 miruka
|
|
|
|
# This file is part of harmonyqt, licensed under GPLv3.
|
|
|
|
|
|
|
|
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)
|
2019-04-27 06:02:20 +10:00
|
|
|
clientCountChanged = pyqtSignal(int)
|
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-27 06:02:20 +10:00
|
|
|
func = lambda: self.clientCountChanged.emit(len(self.clients))
|
|
|
|
self.clientAdded.connect(func)
|
|
|
|
self.clientDeleted.connect(func)
|
2019-04-23 01:37:41 +10:00
|
|
|
|
2019-04-12 03:22:43 +10:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"{type(self).__name__}(clients={self.clients!r})"
|
|
|
|
|
|
|
|
|
2019-04-27 06:02:20 +10:00
|
|
|
@pyqtProperty("QVariantMap", notify=clientCountChanged)
|
2019-04-12 03:22:43 +10:00
|
|
|
def clients(self):
|
|
|
|
return self._clients
|
|
|
|
|
|
|
|
|
2019-04-27 06:02:20 +10:00
|
|
|
@pyqtProperty(int, notify=clientCountChanged)
|
|
|
|
def clientCount(self):
|
|
|
|
return len(self.clients)
|
|
|
|
|
|
|
|
|
2019-04-12 03:22:43 +10:00
|
|
|
@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
|
|
|
|
|
|
|
|
2019-04-27 11:16:45 +10:00
|
|
|
@pyqtSlot(str, str, str, result="QVariant")
|
|
|
|
@pyqtSlot(str, str, str, str, result="QVariant")
|
2019-04-12 03:22:43 +10:00
|
|
|
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-27 11:16:45 +10:00
|
|
|
future = client.login(password, self.defaultDeviceName)
|
|
|
|
future.add_done_callback(lambda _: self._on_connected(client))
|
|
|
|
return future
|
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-05-01 15:23:38 +10:00
|
|
|
@pyqtSlot()
|
|
|
|
def deleteAll(self) -> None:
|
|
|
|
for user_id in self.clients.copy():
|
|
|
|
self.delete(user_id)
|
|
|
|
print("deleted", user_id, self.clients)
|
|
|
|
|
|
|
|
|
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 {}
|
|
|
|
|
|
|
|
|
2019-04-28 11:07:20 +10:00
|
|
|
@pyqtSlot("QVariant")
|
|
|
|
def remember(self, client: Client) -> None:
|
2019-04-12 03:22:43 +10:00
|
|
|
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)
|
2019-04-28 11:07:20 +10:00
|
|
|
def forget(self, user_id: str) -> None:
|
2019-04-12 03:22:43 +10:00
|
|
|
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)
|