Skip to content

Commit

Permalink
feat: Ajouter un bouton pour arrêter Kodi.
Browse files Browse the repository at this point in the history
  • Loading branch information
regseb committed Jan 8, 2022
1 parent 1b74099 commit 2ab5a0b
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@
"popup_playerprocessinfo_title": {
"message": "Show player process information [O]"
},
"popup_openquit_title": {
"message": "Power Options [S]"
},
"popup_empty_textcontent": {
"message": "Playlist empty"
},
Expand Down Expand Up @@ -237,6 +240,18 @@
"popup_addsubtitle_textcontent": {
"message": "Send"
},
"popup_shutdown_textcontent": {
"message": "Power off system"
},
"popup_suspend_textcontent": {
"message": "Suspend"
},
"popup_hibernate_textcontent": {
"message": "Hibernate"
},
"popup_reboot_textcontent": {
"message": "Reboot"
},
"popup_configure_textcontent": {
"message": "Configure"
},
Expand Down
15 changes: 15 additions & 0 deletions src/_locales/fr/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@
"popup_playerprocessinfo_title": {
"message": "Afficher les informations sur le processus de lecture [O]"
},
"popup_openquit_title": {
"message": "Options d'alimentation [S]"
},
"popup_empty_textcontent": {
"message": "Liste de lecture vide"
},
Expand Down Expand Up @@ -237,6 +240,18 @@
"popup_addsubtitle_textcontent": {
"message": "Envoyer"
},
"popup_shutdown_textcontent": {
"message": "Éteindre le système"
},
"popup_suspend_textcontent": {
"message": "Suspendre"
},
"popup_hibernate_textcontent": {
"message": "Mettre en veille"
},
"popup_reboot_textcontent": {
"message": "Redémarrer"
},
"popup_configure_textcontent": {
"message": "Configurer"
},
Expand Down
15 changes: 15 additions & 0 deletions src/_locales/sk/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@
"popup_playerprocessinfo_title": {
"message": "Zobraziť informácie o prehrávaní [O]"
},
"popup_openquit_title": {
"message": "Možnosti napájania [S]"
},
"popup_empty_textcontent": {
"message": "Playlist prázdny"
},
Expand Down Expand Up @@ -237,6 +240,18 @@
"popup_addsubtitle_textcontent": {
"message": "Odoslať"
},
"popup_shutdown_textcontent": {
"message": "Vypnúť systém"
},
"popup_suspend_textcontent": {
"message": "Úsporný režim"
},
"popup_hibernate_textcontent": {
"message": "Dlhodobý spánok"
},
"popup_reboot_textcontent": {
"message": "Reštartovať"
},
"popup_configure_textcontent": {
"message": "Nastaviť"
},
Expand Down
20 changes: 20 additions & 0 deletions src/core/jsonrpc/kodi.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { GUI } from "./gui.js";
import { Input } from "./input.js";
import { Player } from "./player.js";
import { Playlist } from "./playlist.js";
import { System } from "./system.js";

/**
* La version minimale de l'API JSON-RPC de Kodi nécessaire.
Expand Down Expand Up @@ -147,6 +148,14 @@ export const Kodi = class {
*/
#playlist = new Playlist(this);

/**
* Le client JSON-RPC pour contacter l'espace de nom <em>System</em> de
* Kodi.
*
* @type {System}
*/
#system = new System(this);

/**
* Crée un client JSON-RPC pour contacter Kodi.
*
Expand Down Expand Up @@ -235,6 +244,17 @@ export const Kodi = class {
return this.#playlist;
}

/**
* Retourne le client JSON-RPC pour contacter l'espace de nom
* <em>System</em> de Kodi.
*
* @returns {System} Le client JSON-RPC pour contacter l'espace de nom
* <em>System</em> de Kodi.
*/
get system() {
return this.#system;
}

/**
* Ferme la connexion.
*/
Expand Down
78 changes: 78 additions & 0 deletions src/core/jsonrpc/system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @module
*/

/**
* @typedef {import("./kodi.js").Kodi} Kodi
*/

/**
* Le client JSON-RPC pour contacter l'espace de nom <em>System</em> de Kodi.
*
* @see https://kodi.wiki/view/JSON-RPC_API
*/
export const System = class {

/**
* Le client pour contacter Kodi.
*
* @type {Kodi}
*/
#kodi;

/**
* Crée un client JSON-RPC pour l'espace de nom <em>System</em>.
*
* @param {Kodi} kodi Le client pour contacter Kodi.
*/
constructor(kodi) {
this.#kodi = kodi;
}

/**
* Récupère des propriétés de l'espace de nom <em>System</em> de Kodi.
*
* @param {string[]} properties Les noms des propriétés demandées.
* @returns {Promise<Object>} Une promesse contenant les valeurs des
* propriétés.
*/
getProperties(properties) {
return this.#kodi.send("System.GetProperties", { properties });
}

/**
* Met en veille le système.
*
* @returns {Promise<string>} Une promesse contenant <code>"OK"</code>.
*/
hibernate() {
return this.#kodi.send("System.Hibernate");
}

/**
* Redémarre le système.
*
* @returns {Promise<string>} Une promesse contenant <code>"OK"</code>.
*/
reboot() {
return this.#kodi.send("System.Reboot");
}

/**
* Étient le système.
*
* @returns {Promise<string>} Une promesse contenant <code>"OK"</code>.
*/
shutdown() {
return this.#kodi.send("System.Shutdown");
}

/**
* Suspend le système.
*
* @returns {Promise<string>} Une promesse contenant <code>"OK"</code>.
*/
suspend() {
return this.#kodi.send("System.Suspend");
}
};
7 changes: 7 additions & 0 deletions src/design/icon/quit-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions src/popup/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@
<img src="/design/icon/playerprocessinfo-white.svg"
alt="playerprocessinfo-white">
</button>

<button class="ghost" id="openquit" disabled autocomplete="off"
data-i18n-title>
<img src="/design/icon/quit-white.svg" alt="quit-white">
</button>
</section>
</div>

Expand Down Expand Up @@ -286,6 +291,22 @@
</form>
</dialog>

<dialog id="dialogquit">
<form method="dialog">
<menu>
<button class="primary" value="shutdown"
data-i18n-textcontent="shutdown"></button>
<button class="primary" value="suspend"
data-i18n-textcontent="suspend"></button>
<button class="primary" value="hibernate"
data-i18n-textcontent="hibernate"></button>
<button class="primary" value="reboot"
data-i18n-textcontent="reboot"></button>
<button value="cancel" data-i18n-textcontent="cancel"></button>
</menu>
</form>
</dialog>

<article id="splash">
<h1></h1>
<p></p>
Expand Down
57 changes: 57 additions & 0 deletions src/popup/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,47 @@ const showPlayerProcessInfo = async function () {
}
};

const openQuit = function () {
// Annuler l'action (venant d'un raccourci clavier) si le bouton est
// désactivé.
if (document.querySelector("#openquit").disabled) {
return;
}

const dialog = document.querySelector("#dialogquit");
if (!dialog.open) {
dialog.showModal();
}
};

const quit = async function (event) {
const dialog = event.target;
try {
switch (dialog.returnValue) {
case "shutdown":
await kodi.system.shutdown();
close();
break;
case "suspend":
await kodi.system.suspend();
close();
break;
case "hibernate":
await kodi.system.hibernate();
close();
break;
case "reboot":
await kodi.system.reboot();
close();
break;
default:
// Ne rien faire avec le bouton Annuler.
}
} catch (err) {
splash(err);
}
};

const repeat = async function () {
// Annuler l'action (venant d'un raccourci clavier) si le bouton est
// désactivé.
Expand Down Expand Up @@ -1011,6 +1052,7 @@ const load = async function () {
document.querySelector("#fullscreen").disabled = false;
document.querySelector("#opensendtext").disabled = false;
document.querySelector("#playerprocessinfo").disabled = false;
document.querySelector("#openquit").disabled = false;

document.querySelector("#clear").disabled = false;

Expand All @@ -1027,6 +1069,16 @@ const load = async function () {
document.querySelector("#web").dataset.url = url;
document.querySelector("#web").style.display = "flex";
}

const cans = await kodi.system.getProperties([
"canhibernate", "canreboot", "canshutdown", "cansuspend",
]);
Object.entries(cans).filter(([_, v]) => !v)
.map(([k]) => k.slice(3))
.forEach((key) => {
document.querySelector(`#dialogquit button[value="${key}"]`)
.remove();
});
} catch (err) {
splash(err);
}
Expand Down Expand Up @@ -1073,6 +1125,7 @@ document.querySelector("#playerprocessinfo").addEventListener(
"click",
showPlayerProcessInfo,
);
document.querySelector("#openquit").addEventListener("click", openQuit);

for (const input of document.querySelectorAll("#repeat input")) {
input.addEventListener("click", repeat);
Expand All @@ -1094,6 +1147,9 @@ document.querySelector("#dialogsubtitle").addEventListener("close",
document.querySelector("#dialogsubtitle").addEventListener("click",
closeDialog);

document.querySelector("#dialogquit").addEventListener("close", quit);
document.querySelector("#dialogquit").addEventListener("click", closeDialog);

document.querySelector("#configure").addEventListener("click", preferences);

// Attention ! La popup n'a pas automatiquement le focus quand elle est ouverte
Expand Down Expand Up @@ -1155,6 +1211,7 @@ globalThis.addEventListener("keydown", (event) => {
case "Tab": setFullscreen(); break;
case "t": case "T": openSubtitle(); break;
case "o": case "O": showPlayerProcessInfo(); break;
case "s": case "S": openQuit(); break;
// Appliquer le traitement par défaut pour les autres entrées.
default: return;
}
Expand Down
7 changes: 7 additions & 0 deletions src/popup/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ input[type="checkbox"]:checked ~ img:last-of-type {
width: 100%;
}

#dialogquit {
width: calc(100% - 36px);
}
#dialogquit menu {
flex-direction: column;
}

#cast {
border-bottom: 1px solid var(--input-border-color);
display: flex;
Expand Down
8 changes: 8 additions & 0 deletions test/unit/core/jsonrpc/kodi.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Input } from "../../../../src/core/jsonrpc/input.js";
import { Kodi } from "../../../../src/core/jsonrpc/kodi.js";
import { Player } from "../../../../src/core/jsonrpc/player.js";
import { Playlist } from "../../../../src/core/jsonrpc/playlist.js";
import { System } from "../../../../src/core/jsonrpc/system.js";
import { JSONRPC } from "../../../../src/core/tools/jsonrpc.js";
import { NotificationEvent }
from "../../../../src/core/tools/notificationevent.js";
Expand Down Expand Up @@ -125,6 +126,13 @@ describe("core/jsonrpc/kodi.js", function () {
});
});

describe("get system()", function () {
it("should return System object", function () {
const kodi = new Kodi("localhost");
assert.ok(kodi.system instanceof System);
});
});

describe("close()", function () {
it("should close WebSocket", async function () {
const fake = sinon.fake();
Expand Down
Loading

0 comments on commit 2ab5a0b

Please sign in to comment.