modemManager: Define ModemBase GObject class for modems

Use GObject based objects for ModemGsm, ModemCdma and BroadbandModem.
This allows to define a base class that we can use to natively define
properties and notify property changes.

We can now remove the "fake" notify signals with proper properties
notifications.

https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/563
This commit is contained in:
Marco Trevisan (Treviño) 2019-05-29 04:25:57 +02:00 committed by Florian Müllner
parent 348e4ac901
commit d83d8f2c45

View File

@ -1,7 +1,7 @@
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
/* exported ModemBase, ModemGsm, ModemCdma, BroadbandModem */
const { Gio, NMA } = imports.gi;
const Signals = imports.signals;
const { Gio, GObject, NM, NMA } = imports.gi;
const { loadInterfaceXML } = imports.misc.fileUtils;
@ -98,21 +98,46 @@ const ModemGsmNetworkProxy = Gio.DBusProxy.makeProxyWrapper(ModemGsmNetworkInter
const ModemCdmaInterface = loadInterfaceXML('org.freedesktop.ModemManager.Modem.Cdma');
const ModemCdmaProxy = Gio.DBusProxy.makeProxyWrapper(ModemCdmaInterface);
var ModemGsm = class {
constructor(path) {
this._proxy = new ModemGsmNetworkProxy(Gio.DBus.system, 'org.freedesktop.ModemManager', path);
var ModemBase = GObject.registerClass({
GTypeFlags: GObject.TypeFlags.ABSTRACT,
Properties: {
'operator-name': GObject.ParamSpec.string(
'operator-name', 'operator-name', 'operator-name',
GObject.ParamFlags.READABLE,
null),
'signal-quality': GObject.ParamSpec.int(
'signal-quality', 'signal-quality', 'signal-quality',
GObject.ParamFlags.READABLE,
0, 100, 0),
},
}, class ModemBase extends GObject.Object {
_setOperatorName(operatorName) {
if (this.operator_name == operatorName)
return;
this.operator_name = operatorName;
this.notify('operator-name');
}
this.signal_quality = 0;
this.operator_name = null;
_setSignalQuality(signalQuality) {
if (this.signal_quality == signalQuality)
return;
this.signal_quality = signalQuality;
this.notify('signal-quality');
}
});
var ModemGsm = GObject.registerClass(
class ModemGsm extends ModemBase {
_init(path) {
super._init();
this._proxy = new ModemGsmNetworkProxy(Gio.DBus.system, 'org.freedesktop.ModemManager', path);
// Code is duplicated because the function have different signatures
this._proxy.connectSignal('SignalQuality', (proxy, sender, [quality]) => {
this.signal_quality = quality;
this.emit('notify::signal-quality');
this._setSignalQuality(quality);
});
this._proxy.connectSignal('RegistrationInfo', (proxy, sender, [_status, code, name]) => {
this.operator_name = _findProviderForMccMnc(name, code);
this.emit('notify::operator-name');
this._setOperatorName(_findProviderForMccMnc(name, code));
});
this._proxy.GetRegistrationInfoRemote(([result], err) => {
if (err) {
@ -121,32 +146,28 @@ var ModemGsm = class {
}
let [status_, code, name] = result;
this.operator_name = _findProviderForMccMnc(name, code);
this.emit('notify::operator-name');
this._setOperatorName(_findProviderForMccMnc(name, code));
});
this._proxy.GetSignalQualityRemote((result, err) => {
if (err) {
// it will return an error if the device is not connected
this.signal_quality = 0;
this._setSignalQuality(0);
} else {
let [quality] = result;
this.signal_quality = quality;
this._setSignalQuality(quality);
}
this.emit('notify::signal-quality');
});
}
};
Signals.addSignalMethods(ModemGsm.prototype);
});
var ModemCdma = class {
constructor(path) {
var ModemCdma = GObject.registerClass(
class ModemCdma extends ModemBase {
_init(path) {
super._init();
this._proxy = new ModemCdmaProxy(Gio.DBus.system, 'org.freedesktop.ModemManager', path);
this.signal_quality = 0;
this.operator_name = null;
this._proxy.connectSignal('SignalQuality', (proxy, sender, params) => {
this.signal_quality = params[0];
this.emit('notify::signal-quality');
this._setSignalQuality(params[0]);
// receiving this signal means the device got activated
// and we can finally call GetServingSystem
@ -156,12 +177,11 @@ var ModemCdma = class {
this._proxy.GetSignalQualityRemote((result, err) => {
if (err) {
// it will return an error if the device is not connected
this.signal_quality = 0;
this._setSignalQuality(0);
} else {
let [quality] = result;
this.signal_quality = quality;
this._setSignalQuality(quality);
}
this.emit('notify::signal-quality');
});
}
@ -169,17 +189,14 @@ var ModemCdma = class {
this._proxy.GetServingSystemRemote(([result], err) => {
if (err) {
// it will return an error if the device is not connected
this.operator_name = null;
this._setOperatorName(null);
} else {
let [bandClass_, band_, sid] = result;
this.operator_name = _findProviderForSid(sid);
this._setOperatorName(_findProviderForSid(sid));
}
this.emit('notify::operator-name');
});
}
};
Signals.addSignalMethods(ModemCdma.prototype);
});
// ------------------------------------------------------- //
@ -195,12 +212,20 @@ const BroadbandModem3gppProxy = Gio.DBusProxy.makeProxyWrapper(BroadbandModem3gp
const BroadbandModemCdmaInterface = loadInterfaceXML('org.freedesktop.ModemManager1.Modem.ModemCdma');
const BroadbandModemCdmaProxy = Gio.DBusProxy.makeProxyWrapper(BroadbandModemCdmaInterface);
var BroadbandModem = class {
constructor(path, capabilities) {
this._proxy = new BroadbandModemProxy(Gio.DBus.system, 'org.freedesktop.ModemManager1', path);
var BroadbandModem = GObject.registerClass({
Properties: {
'capabilities': GObject.ParamSpec.flags(
'capabilities', 'capabilities', 'capabilities',
GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY,
NM.DeviceModemCapabilities.$gtype,
NM.DeviceModemCapabilities.NONE)
},
}, class BroadbandModem extends ModemBase {
_init(path, capabilities) {
super._init({ capabilities });
this._proxy = new BroadbandModemProxy(Gio.DBus.system, 'org.freedesktop.ModemManager', path);
this._proxy_3gpp = new BroadbandModem3gppProxy(Gio.DBus.system, 'org.freedesktop.ModemManager1', path);
this._proxy_cdma = new BroadbandModemCdmaProxy(Gio.DBus.system, 'org.freedesktop.ModemManager1', path);
this._capabilities = capabilities;
this._proxy.connect('g-properties-changed', (proxy, properties) => {
if ('SignalQuality' in properties.deep_unpack())
@ -224,9 +249,8 @@ var BroadbandModem = class {
}
_reloadSignalQuality() {
let [quality, recent_] = this._proxy.SignalQuality;
this.signal_quality = quality;
this.emit('notify::signal-quality');
let [quality, recent_] = this.SignalQuality;
this._setSignalQuality(quality);
}
_reloadOperatorName() {
@ -240,8 +264,7 @@ var BroadbandModem = class {
newName += this.operator_name_cdma;
}
this.operator_name = newName;
this.emit('notify::operator-name');
this._setOperatorName(newName);
}
_reload3gppOperatorName() {
@ -256,5 +279,4 @@ var BroadbandModem = class {
this.operator_name_cdma = _findProviderForSid(sid);
this._reloadOperatorName();
}
};
Signals.addSignalMethods(BroadbandModem.prototype);
});