141 lines
5.2 KiB
JavaScript
141 lines
5.2 KiB
JavaScript
|
import Gio from 'gi://Gio';
|
||
|
import { ObjectManager } from './ObjectManager.js';
|
||
|
import { loadInterfaceXML } from './Utils.js';
|
||
|
import { Realm } from './Realm.js';
|
||
|
import { RealmFS } from './RealmFS.js';
|
||
|
import { LabelColorManager } from './LabelColors.js';
|
||
|
const Signals = imports.signals;
|
||
|
const RealmIface = loadInterfaceXML("com.subgraph.realms.Realm");
|
||
|
const RealmFSIface = loadInterfaceXML("com.subgraph.realms.RealmFS");
|
||
|
const BUS_NAME = 'com.subgraph.Realms2';
|
||
|
const OBJECT_PATH = '/com/subgraph/Realms2';
|
||
|
const ManagerInterface = loadInterfaceXML("com.subgraph.realms.Manager2");
|
||
|
const ManagerProxy = Gio.DBusProxy.makeProxyWrapper(ManagerInterface);
|
||
|
;
|
||
|
export class RealmManager {
|
||
|
static instance() {
|
||
|
return RealmManager.INSTANCE;
|
||
|
}
|
||
|
constructor() {
|
||
|
this._globalConfig = {};
|
||
|
this._realms = {};
|
||
|
this._realmfs = {};
|
||
|
this._realmfs = {};
|
||
|
this._objectManager = new ObjectManager({
|
||
|
connection: Gio.DBus.system,
|
||
|
name: 'com.subgraph.Realms2',
|
||
|
objectPath: '/com/subgraph/Realms2',
|
||
|
knownInterfaces: [RealmIface, RealmFSIface],
|
||
|
onLoaded: this._onLoaded.bind(this),
|
||
|
});
|
||
|
this._proxy = new ManagerProxy(Gio.DBus.system, BUS_NAME, OBJECT_PATH, (_proxy, error) => {
|
||
|
if (error) {
|
||
|
logError(error);
|
||
|
}
|
||
|
else {
|
||
|
this._setGlobalConfig().catch(logError);
|
||
|
}
|
||
|
});
|
||
|
this._labelColors = new LabelColorManager();
|
||
|
}
|
||
|
async _setGlobalConfig() {
|
||
|
let result = await this._proxy.GetGlobalConfigAsync();
|
||
|
this._globalConfig = result[0];
|
||
|
this._assignGlobalConfigToRealms();
|
||
|
}
|
||
|
async _onLoaded() {
|
||
|
let realms = this._objectManager.getProxiesForInterface('com.subgraph.realms.Realm');
|
||
|
for (let i = 0; i < realms.length; i++) {
|
||
|
this._addRealm(realms[i]);
|
||
|
}
|
||
|
let realmfs = this._objectManager.getProxiesForInterface('com.subgraph.realms.RealmFS');
|
||
|
for (let i = 0; i < realmfs.length; i++) {
|
||
|
this._addRealmFS(realmfs[i]);
|
||
|
}
|
||
|
this._objectManager.connect('interface-added', (_objectManager, interfaceName, proxy) => {
|
||
|
if (interfaceName === 'com.subgraph.Realm') {
|
||
|
this._addRealm(proxy);
|
||
|
}
|
||
|
else if (interfaceName === 'com.subgraph.RealmFS') {
|
||
|
this._addRealmFS(proxy);
|
||
|
}
|
||
|
});
|
||
|
this._objectManager.connect('interface-removed', (_objectManager, interfaceName, proxy) => {
|
||
|
if (interfaceName === 'com.subgraph.Realm') {
|
||
|
this._removeRealm(proxy);
|
||
|
}
|
||
|
else if (interfaceName === 'com.subgraph.RealmFS') {
|
||
|
this._removeRealmFS(proxy);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
async _addRealm(proxy) {
|
||
|
let objectPath = proxy.get_object_path();
|
||
|
let realm = new Realm(proxy, this._labelColors);
|
||
|
this._realms[objectPath] = realm;
|
||
|
this._findRealmFSForRealm(realm);
|
||
|
if (this._globalConfig) {
|
||
|
realm.set_global_config(this._globalConfig);
|
||
|
}
|
||
|
await realm.load_config();
|
||
|
this.emit('realm-added', realm);
|
||
|
}
|
||
|
_removeRealm(proxy) {
|
||
|
var _a;
|
||
|
let objectPath = proxy.get_object_path();
|
||
|
if (((_a = this._realms[objectPath]) === null || _a === void 0 ? void 0 : _a._proxy) === proxy) {
|
||
|
let realm = this._realms[objectPath];
|
||
|
delete this._realms[objectPath];
|
||
|
this.emit('realm-removed', realm);
|
||
|
}
|
||
|
proxy.disconnectAll();
|
||
|
}
|
||
|
_addRealmFS(proxy) {
|
||
|
let objectPath = proxy.get_object_path();
|
||
|
let realmfs = new RealmFS(proxy);
|
||
|
this._realmfs[objectPath] = realmfs;
|
||
|
this._assignRealmFSToRealms(realmfs);
|
||
|
this.emit('realmfs-added', realmfs);
|
||
|
}
|
||
|
_findRealmFSForRealm(realm) {
|
||
|
var _a;
|
||
|
if (realm.realmfs_index() > 0) {
|
||
|
realm.realmfs = (_a = Object.values(this._realmfs)
|
||
|
.find(realmfs => realmfs.index() === realm.realmfs_index())) !== null && _a !== void 0 ? _a : null;
|
||
|
}
|
||
|
}
|
||
|
realmfsList() {
|
||
|
return Object.values(this._realmfs);
|
||
|
}
|
||
|
_assignRealmFSToRealms(realmfs) {
|
||
|
Object.values(this._realms).forEach(r => {
|
||
|
if (r.realmfs_index() > 0 && r.realmfs_index() === realmfs.index() && r.realmfs != realmfs) {
|
||
|
r.realmfs = realmfs;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
_assignGlobalConfigToRealms() {
|
||
|
Object.values(this._realms).forEach(r => r.set_global_config(this._globalConfig));
|
||
|
}
|
||
|
_removeRealmFSFromRealms(realmfs) {
|
||
|
Object.values(this._realms).forEach(r => {
|
||
|
if (r.realmfs === realmfs) {
|
||
|
r.realmfs = null;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
_removeRealmFS(proxy) {
|
||
|
var _a;
|
||
|
let objectPath = proxy.get_object_path();
|
||
|
if (((_a = this._realmfs[objectPath]) === null || _a === void 0 ? void 0 : _a._proxy) === proxy) {
|
||
|
let realmfs = this._realmfs[objectPath];
|
||
|
delete this._realmfs[objectPath];
|
||
|
this._removeRealmFSFromRealms(realmfs);
|
||
|
this.emit('realmfs-removed', realmfs);
|
||
|
}
|
||
|
proxy.disconnectAll();
|
||
|
}
|
||
|
}
|
||
|
RealmManager.INSTANCE = new RealmManager();
|
||
|
Signals.addSignalMethods(RealmManager.prototype);
|