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