From 4480d9de3823a2c223b02bb8ad516947d0854986 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guido=20G=C3=BCnther?= <agx@sigxcpu.org>
Date: Mon, 6 Apr 2020 15:37:53 +0200
Subject: [PATCH] Fix modem enablement

This got broken when switching to the ModemManager1 API. Instead
of checking the error, check state upfront.
---
 src/ppm/modemproxy.py         | 10 ++++++++--
 src/prepaid-manager-applet.py | 14 +++++++++-----
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/src/ppm/modemproxy.py b/src/ppm/modemproxy.py
index 31aa802..3943b7f 100644
--- a/src/ppm/modemproxy.py
+++ b/src/ppm/modemproxy.py
@@ -25,7 +25,6 @@ MM_DBUS_TIMEOUT = 5000
 MM_DBUS_FLAGS = (Gio.DBusProxyFlags.DO_NOT_LOAD_PROPERTIES |
                  Gio.DBusProxyFlags.DO_NOT_CONNECT_SIGNALS)
 
-
 class ModemError(Exception):
     def __init__(self, msg):
         self.msg = msg
@@ -41,6 +40,8 @@ class Modem(GObject.GObject):
     MM_DBUS_INTERFACE_MODEM = 'org.freedesktop.ModemManager1.Modem'
     MM_DBUS_INTERFACE_MODEM_GSM_USSD = "{}.Modem3gpp.Ussd".format(MM_DBUS_INTERFACE_MODEM)
 
+    MM_STATE_ENABLED = 6
+
     def on_new_proxy_done(self, proxy, res, iface_name):
         try:
             _proxy = proxy.new_for_bus_finish(res)
@@ -55,7 +56,7 @@ class Modem(GObject.GObject):
 
         self._modem_proxy = None
         Gio.DBusProxy.new_for_bus(Gio.BusType.SYSTEM,
-                                  MM_DBUS_FLAGS,
+                                  Gio.DBusProxyFlags.DO_NOT_CONNECT_SIGNALS,
                                   None,
                                   MM_DBUS_SERVICE,
                                   self.path,
@@ -87,6 +88,11 @@ class Modem(GObject.GObject):
     def ussd_proxy(self):
         return self._ussd_proxy
 
+    @property
+    def enabled(self):
+        variant = self.modem_proxy.get_cached_property("State")
+        return variant.get_int32() >= self.MM_STATE_ENABLED
+
 
 class ModemManagerProxy(GObject.GObject):
     """Interface to ModemManager DBus API
diff --git a/src/prepaid-manager-applet.py b/src/prepaid-manager-applet.py
index 9c44f29..155b7d2 100755
--- a/src/prepaid-manager-applet.py
+++ b/src/prepaid-manager-applet.py
@@ -88,6 +88,9 @@ class PPMController(Gtk.Application):
 
     def fetch_balance(self):
         """Fetch the current account balance from the  network"""
+        if not self.mm.modem.enabled:
+            self.view.show_modem_enable()
+
         if not self.provider.fetch_balance(self.mm,
                                            reply_func=self.on_balance_info_fetched,
                                            error_func=self.on_modem_error):
@@ -170,6 +173,11 @@ class PPMController(Gtk.Application):
         """Fetch the imsi and deduce account and provider information"""
 
         logging.debug("Fetching account information")
+
+        if not self.mm.modem.enabled:
+            self.view.show_modem_enable()
+            return False
+
         try:
             self.imsi = self.mm.get_imsi()
         except ModemError as me:
@@ -177,12 +185,8 @@ class PPMController(Gtk.Application):
             if me.is_forbidden():
                 self.view.show_provider_assistant()
                 return False
-            if not me.is_disabled():
-                self.view.show_modem_error(me.msg)
-                return False
 
-            logging.info("modem not enabled.")
-            self.view.show_modem_enable()
+            self.view.show_modem_error(me.msg)
             return False
 
         try:
-- 
GitLab