312 lines
8.2 KiB
JavaScript
Raw Normal View History

2020-03-30 10:45:26 +02:00
/* extension.js
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
/* Based on https://stackoverflow.com/questions/33001192/how-to-send-a-string-to-a-gnome-shell-extension */
const St = imports.gi.St;
const Gio = imports.gi.Gio;
const Lang = imports.lang;
const Main = imports.ui.main;
class MonitorWidget {
constructor(name, group, text, style, icon, iconStyle) {
this.name = name;
this.group = group;
this._createIcon(icon, iconStyle);
this._createText(text, style);
}
_createText(text, style)
{
this.style = style;
if (text == "")
this.widget = null;
else
{
this.widget = new St.Button({ label: text});
this.widget.set_style(this.style);
}
}
_createIcon(icon, style)
{
this.iconStyle = style;
if (icon == "")
this.icon = null;
else
{
let gicon = Gio.icon_new_for_string(icon);
this.icon = new St.Icon({ gicon });
this.icon.set_style(this.iconStyle);
}
}
update(text, style, icon, iconStyle)
{
if (!this.widget)
this._createText(text, style)
else
{
if (text != "")
this.widget.label = text;
this.style = style;
this.widget.set_style(this.style);
}
if (icon != "")
this._createIcon(icon, iconStyle);
else
{
if (iconStyle != "" && this.icon)
{
this.iconStyle = style;
this.icon.set_style(this.iconStyle);
}
}
}
}
// From https://github.com/ubuntu/gnome-shell-extension-appindicator/blob/master/interfaces.js
// loads a xml file into an in-memory string
function loadInterfaceXml(filename) {
let extension = imports.misc.extensionUtils.getCurrentExtension();
let interfaces_dir = extension.dir.get_child(".")
let file = interfaces_dir.get_child(filename);
let [ result, contents ] = imports.gi.GLib.file_get_contents(file.get_path());
if (result) {
//HACK: The "" + trick is important as hell because file_get_contents returns
// an object (WTF?) but Gio.makeProxyWrapper requires `typeof() == "string"`
// Otherwise, it will try to check `instanceof XML` and fail miserably because there
// is no `XML` on very recent SpiderMonkey releases (or, if SpiderMonkey is old enough,
// will spit out a TypeError soon).
if (contents instanceof Uint8Array)
contents = imports.byteArray.toString(contents);
return "<node>" + contents + "</node>";
} else {
throw new Error("Generic monitor: Could not load file: "+filename);
}
}
const GenericMonitorDBUS = new Lang.Class({
Name: 'GenericMonitor',
_init: function() {
this.monitor_groups = {};
this.box = Main.panel._centerBox;
this._dbusImpl = Gio.DBusExportedObject.wrapJSObject(loadInterfaceXml('dbus.xml'), this);
this._dbusImpl.export(Gio.DBus.session, '/com/soutade/GenericMonitor');
},
_checkParmeters: function(parameters)
{
if (!parameters.hasOwnProperty("group"))
throw new Error("No group defined");
if (!parameters.hasOwnProperty("items"))
throw new Error("No items defined");
for (let itemIndex in parameters["items"]) {
let item = parameters["items"][itemIndex];
if (!item.hasOwnProperty("name"))
throw new Error("No name defined for item");
if (!item.hasOwnProperty("text") && !item.hasOwnProperty("icon"))
throw new Error("No text not icon defined for item");
}
},
_getItemFromGroup: function(group, name)
{
for (let groupItemIndex in group)
{
let groupItem = group[groupItemIndex];
if (groupItem.name == name)
return groupItem;
}
return null;
},
notify: function(str) {
let parameters = JSON.parse(str);
this._checkParmeters(parameters);
let groupName = parameters["group"];
let group;
if (!this.monitor_groups.hasOwnProperty(groupName))
{
group = [];
this.monitor_groups[groupName] = group;
}
else
group = this.monitor_groups[groupName];
for (let itemIndex in parameters["items"]) {
let item = parameters["items"][itemIndex];
let style = "";
if (item.hasOwnProperty("style"))
style = item["style"];
let text = "";
if (item.hasOwnProperty("text"))
text = item["text"];
let icon = "";
if (item.hasOwnProperty("icon"))
icon = item["icon"];
let iconStyle = "";
if (item.hasOwnProperty("icon-style"))
iconStyle = item["icon-style"];
let monitorWidget = this._getItemFromGroup(group, item["name"]);
let lastWidget = null;
// New widget
if (monitorWidget == null)
{
monitorWidget = new MonitorWidget(item["name"], groupName, text, style, icon, iconStyle);
if (group.length)
lastWidget = group[group.length-1].widget;
group.push(monitorWidget);
// lastWidget => NULL, insert at the end
if (monitorWidget.icon)
{
this.box.insert_child_above(monitorWidget.icon, lastWidget);
lastWidget = monitorWidget.icon;
}
if (monitorWidget.widget)
this.box.insert_child_above(monitorWidget.widget, lastWidget);
}
else
{
let prevWidget = monitorWidget.widget;
let prevIcon = monitorWidget.icon;
monitorWidget.update(text, style, icon, iconStyle);
if (monitorWidget.icon)
{
if (prevIcon)
this.box.remove_child(prevIcon);
this.box.insert_child_above(monitorWidget.icon, lastWidget);
lastWidget = monitorWidget.icon;
}
if (!prevWidget && monitorWidget.widget)
this.box.insert_child_above(monitorWidget.widget, lastWidget);
}
}
},
_removeItemFromBox: function(item) {
if (item.widget)
this.box.remove_child(item.widget);
if (item.icon)
this.box.remove_child(item.icon);
},
deleteItems: function(str) {
let parameters = JSON.parse(str);
if (!parameters.hasOwnProperty("items"))
throw new Error("No items defined");
for (let itemIndex in parameters["items"]) {
let itemName = parameters["items"][itemIndex];
let fullName = itemName.split("@");
if (fullName.length != 2)
throw new Error("Invalid name " + itemName);
itemName = fullName[0];
let groupName = fullName[1];
if (!this.monitor_groups.hasOwnProperty(groupName))
continue;
let group = this.monitor_groups[groupName];
let item = this._getItemFromGroup(group, itemName);
if (item != null)
{
this._removeItemFromBox(item);
delete group[item["name"]];
if (group.length == 0)
delete this.monitor_groups[groupName];
}
}
},
deleteGroups: function(str) {
let parameters = JSON.parse(str);
if (!parameters.hasOwnProperty("groups"))
throw new Error("No groups defined");
let groupsToDelete = [];
for (let groupIndex in parameters["groups"]) {
groupName = parameters["groups"][groupIndex];
if (!this.monitor_groups.hasOwnProperty(groupName))
continue;
let group = this.monitor_groups[groupName];
for (let itemIndex in group) {
this._removeItemFromBox(group[itemIndex]);
}
groupsToDelete.push(groupName);
}
for(let groupDeleteIndex in groupsToDelete)
{
let groupName = groupsToDelete[groupDeleteIndex];
delete this.monitor_groups[groupName];
}
},
deleteAll: function() {
for (let groupIndex in this.monitor_groups) {
let group = this.monitor_groups[groupIndex];
for (let itemIndex in group) {
this._removeItemFromBox(group[itemIndex]);
}
}
this.monitor_groups = {}
},
});
class Extension {
constructor() {
this.textDBusService = new GenericMonitorDBUS();
}
enable() {
}
disable() {
this.textDBusService.deleteAll();
}
}
let extension = null;
function init() {
extension = new Extension();
}
function enable() {
extension.enable();
}
function disable() {
extension.disable();
}