164 lines
5.3 KiB
QML
164 lines
5.3 KiB
QML
import QtQuick 2.5
|
|
|
|
// État de connexion : serveurs connus, serveur et utilisateur courants.
|
|
// Les appels réseau sont SIMULÉS (_simulate) : à remplacer par l'API Jellyfin
|
|
// en gardant la forme des callbacks `callback(erreur, résultat)`.
|
|
QtObject {
|
|
id: session
|
|
|
|
property ListModel servers: ListModel {
|
|
// Données fictives
|
|
ListElement { name: "Maison"; address: "http://192.168.1.20:8096"; discovered: false }
|
|
ListElement { name: "jellyfin-nas"; address: "http://192.168.1.42:8096"; discovered: true }
|
|
}
|
|
|
|
property var currentServer: null // { name, address }
|
|
property var currentUser: null // { name }
|
|
|
|
function selectServer(index) {
|
|
var server = servers.get(index);
|
|
currentServer = { name: server.name, address: server.address };
|
|
}
|
|
|
|
// Ajoute (ou retrouve) un serveur à partir de l'hôte saisi et d'un port facultatif.
|
|
// Le clavier de la box n'a ni « : » ni « / » : le protocole et le port sont
|
|
// détectés en essayant chaque adresse de serverCandidates() dans l'ordre.
|
|
function connectToServer(host, port, callback) {
|
|
if ((host || "").trim() === "") {
|
|
callback("Veuillez saisir l'adresse du serveur.");
|
|
return;
|
|
}
|
|
var portText = (port || "").trim();
|
|
if (portText !== "" && !isValidPort(portText)) {
|
|
callback("Le port doit être un nombre entre 1 et 65535.");
|
|
return;
|
|
}
|
|
_probeCandidates(serverCandidates(host, portText), 0, function (err, address) {
|
|
if (err) {
|
|
callback(err);
|
|
return;
|
|
}
|
|
var index = _indexOfServer(address);
|
|
if (index < 0) {
|
|
servers.insert(0, { name: _hostOf(address), address: address, discovered: false });
|
|
index = 0;
|
|
}
|
|
selectServer(index);
|
|
callback(null, currentServer);
|
|
});
|
|
}
|
|
|
|
// Adresses à essayer, de la plus probable à la moins probable
|
|
// (même logique que les clients Jellyfin officiels).
|
|
function serverCandidates(host, port) {
|
|
var text = (host || "").trim().replace(/\/+$/, "");
|
|
if (text === "")
|
|
return [];
|
|
// Adresse complète (saisie depuis un clavier qui a « : ») : utilisée telle quelle.
|
|
if (/^https?:\/\//i.test(text)) {
|
|
if (port && !/:\d+$/.test(text.replace(/^https?:\/\//i, "")))
|
|
text += ":" + port;
|
|
return [text];
|
|
}
|
|
if (port)
|
|
return ["https://" + text + ":" + port, "http://" + text + ":" + port];
|
|
// Port déjà inclus dans l'hôte (ex. 192.168.1.20:8096).
|
|
if (/:\d+$/.test(text))
|
|
return ["https://" + text, "http://" + text];
|
|
return [
|
|
"https://" + text,
|
|
"http://" + text + ":8096",
|
|
"https://" + text + ":8920",
|
|
"http://" + text
|
|
];
|
|
}
|
|
|
|
// Essaie les adresses une par une ; renvoie la première qui répond.
|
|
// TODO API : GET <adresse>/System/Info/Public, accepter si la réponse contient un Id,
|
|
// et utiliser ServerName comme nom. Simulation : la première adresse « répond ».
|
|
function _probeCandidates(candidates, index, callback) {
|
|
if (index >= candidates.length) {
|
|
callback("Aucun serveur Jellyfin trouvé à cette adresse.");
|
|
return;
|
|
}
|
|
_simulate(function () {
|
|
callback(null, candidates[index]);
|
|
});
|
|
}
|
|
|
|
function isValidPort(text) {
|
|
if (!/^\d+$/.test(text))
|
|
return false;
|
|
var value = parseInt(text, 10);
|
|
return value >= 1 && value <= 65535;
|
|
}
|
|
|
|
// TODO API : GET /Users/Public
|
|
function publicUsers() {
|
|
return [
|
|
{ name: "Alice", hasPassword: true },
|
|
{ name: "Bob", hasPassword: false },
|
|
{ name: "Enfants", hasPassword: false }
|
|
];
|
|
}
|
|
|
|
// TODO API : POST /Users/AuthenticateByName
|
|
function authenticate(username, password, callback) {
|
|
var name = (username || "").trim();
|
|
if (name === "") {
|
|
callback("Veuillez saisir votre nom d'utilisateur.");
|
|
return;
|
|
}
|
|
_simulate(function () {
|
|
currentUser = { name: name };
|
|
callback(null, currentUser);
|
|
});
|
|
}
|
|
|
|
// TODO API : POST /QuickConnect/Initiate puis interroger /QuickConnect/Connect
|
|
function startQuickConnect() {
|
|
var code = "";
|
|
for (var i = 0; i < 6; i++)
|
|
code += Math.floor(Math.random() * 10);
|
|
return code;
|
|
}
|
|
|
|
function logout() {
|
|
currentUser = null;
|
|
}
|
|
|
|
function cancel() {
|
|
_pending = null;
|
|
_latency.stop();
|
|
}
|
|
|
|
function _hostOf(address) {
|
|
return address.replace(/^https?:\/\//i, "").split(/[:\/]/)[0];
|
|
}
|
|
|
|
function _indexOfServer(address) {
|
|
for (var i = 0; i < servers.count; i++) {
|
|
if (servers.get(i).address === address)
|
|
return i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// Simulation de latence réseau.
|
|
property var _pending: null
|
|
property Timer _latency: Timer {
|
|
interval: 700
|
|
onTriggered: {
|
|
var callback = session._pending;
|
|
session._pending = null;
|
|
if (callback)
|
|
callback();
|
|
}
|
|
}
|
|
|
|
function _simulate(callback) {
|
|
_pending = callback;
|
|
_latency.restart();
|
|
}
|
|
}
|