Merge ~nteodosio/software-properties:network into software-properties:ubuntu/master

Proposed by Nathan Teodosio
Status: Merged
Approved by: Sebastien Bacher
Approved revision: a4dffcee9eca89b5c6fbe7d768d93a506eded610
Merged at revision: a4dffcee9eca89b5c6fbe7d768d93a506eded610
Proposed branch: ~nteodosio/software-properties:network
Merge into: software-properties:ubuntu/master
Diff against target: 126 lines (+53/-3)
2 files modified
data/gtkbuilder/dialog-ua-attach.ui (+33/-0)
softwareproperties/gtk/DialogUaAttach.py (+20/-3)
Reviewer Review Type Date Requested Status
Sebastien Bacher Approve
Review via email: mp+436689@code.launchpad.net

Commit message

Add internet connection awareness.

Description of the change

To post a comment you must log in.
Revision history for this message
Sebastien Bacher (seb128) wrote :

Quite an improvement, thanks Nathan!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/data/gtkbuilder/dialog-ua-attach.ui b/data/gtkbuilder/dialog-ua-attach.ui
2index 515f995..6e255a8 100644
3--- a/data/gtkbuilder/dialog-ua-attach.ui
4+++ b/data/gtkbuilder/dialog-ua-attach.ui
5@@ -26,6 +26,10 @@
6 <property name="max-width-chars">130</property>
7 </object>
8 </child>
9+ <child>
10+ <object class="GtkBox" id="radio_net_control_box">
11+ <property name="visible">True</property>
12+ <property name="orientation">vertical</property>
13 <child>
14 <object class="GtkRadioButton" id="magic_radio">
15 <property name="label" translatable="yes">Enter code on ubuntu.com/pro/attach</property>
16@@ -160,6 +164,30 @@
17 <property name="label" translatable="yes">From your admin, or from &lt;a href="https://ubuntu.com/pro"&gt;ubuntu.com/pro&lt;/a&gt;</property>
18 </object>
19 </child>
20+ </object>
21+ </child>
22+ <child>
23+ <object class="GtkBox" id="no_connection">
24+ <property name="visible">False</property>
25+ <property name="margin-left">5</property>
26+ <property name="margin-top">18</property>
27+ <property name="margin-bottom">18</property>
28+ <child>
29+ <object class="GtkImage">
30+ <property name="visible">True</property>
31+ <property name="margin-right">5</property>
32+ <property name="stock">gtk-dialog-warning</property>
33+ </object>
34+ </child>
35+ <child>
36+ <object class="GtkLabel">
37+ <property name="visible">True</property>
38+ <property name="wrap">True</property>
39+ <property name="label" translatable="yes">Unable to connect to Ubuntu Pro servers. Check your internet connection.</property>
40+ </object>
41+ </child>
42+ </object>
43+ </child>
44 <child>
45 <object class="GtkBox">
46 <property name="visible">True</property>
47@@ -174,6 +202,9 @@
48 </object>
49 </child>
50 <child>
51+ <object class="GtkBox" id="confirm_net_control_box">
52+ <property name="visible">True</property>
53+ <child>
54 <object class="GtkButton" id="confirm">
55 <property name="visible">True</property>
56 <property name="label" translatable="yes">Confirm</property>
57@@ -181,6 +212,8 @@
58 <signal name="clicked" handler="on_confirm_clicked" swapped="no"/>
59 </object>
60 </child>
61+ </object>
62+ </child>
63 </object>
64 </child>
65 </object>
66diff --git a/softwareproperties/gtk/DialogUaAttach.py b/softwareproperties/gtk/DialogUaAttach.py
67index 7c7f89d..42d5365 100644
68--- a/softwareproperties/gtk/DialogUaAttach.py
69+++ b/softwareproperties/gtk/DialogUaAttach.py
70@@ -20,7 +20,7 @@ import os
71 from gettext import gettext as _
72 import gi
73 gi.require_version("Gtk", "3.0")
74-from gi.repository import Gtk,GLib
75+from gi.repository import Gtk,GLib,Gio
76 from softwareproperties.gtk.utils import setup_ui
77 from uaclient.api.u.pro.attach.magic.initiate.v1 import initiate
78 from uaclient.api.u.pro.attach.magic.wait.v1 import MagicAttachWaitOptions, wait
79@@ -40,6 +40,12 @@ class DialogUaAttach:
80 self.poll = None
81 self.pin = ""
82
83+ self.net_monitor = Gio.network_monitor_get_default()
84+ self.net_monitor.connect("network-changed", self.net_status_changed, 0)
85+ self.net_status_changed(
86+ self.net_monitor, self.net_monitor.get_network_available(), 1
87+ )
88+
89 self.start_magic_attach()
90
91 def run(self):
92@@ -148,7 +154,7 @@ class DialogUaAttach:
93 except MagicAttachTokenError:
94 GLib.idle_add(self.update_state, 'expired')
95 except Exception as e:
96- print("Error getting the Ubuntu Pro token: ", e)
97+ print("Error getting the Ubuntu Pro token: ", e, flush = True)
98 finally:
99 self.poll = None
100
101@@ -167,7 +173,7 @@ class DialogUaAttach:
102 self.pin = response.user_code
103 self.req_id = response.token
104 except Exception as e:
105- print(e)
106+ print("Error retrieving magic token: ", e)
107 return
108 self.update_state()
109 self.poll = GLib.Thread.new("poll", self.poll_for_magic_token)
110@@ -178,5 +184,16 @@ class DialogUaAttach:
111 def on_magic_radio_clicked(self, button):
112 self.start_magic_attach()
113
114+ # Do not control the radio buttons and confirm button widgets directly,
115+ # since those former are controlled by update_state and this function must
116+ # be logically independent of it. Control the net_control_box'es instead.
117+ def net_status_changed(self, monitor, available, first_run):
118+ self.no_connection.set_visible(not available)
119+ self.radio_net_control_box.set_sensitive(available)
120+ self.confirm_net_control_box.set_sensitive(available)
121+ # wait() may have timed out without internet; Restart polling.
122+ if (available and self.poll == None and not first_run):
123+ self.poll = GLib.Thread.new("poll", self.poll_for_magic_token)
124+
125 def finish(self):
126 self.dialog.response(Gtk.ResponseType.OK)

Subscribers

People subscribed via source and target branches