Merge lp:~donadigo/pantheon-agent-geoclue2/code-improvements into lp:~elementary-pantheon/pantheon-agent-geoclue2/trunk

Proposed by Adam Bieńkowski
Status: Merged
Approved by: David Hewitt
Approved revision: 19
Merged at revision: 16
Proposed branch: lp:~donadigo/pantheon-agent-geoclue2/code-improvements
Merge into: lp:~elementary-pantheon/pantheon-agent-geoclue2/trunk
Diff against target: 221 lines (+60/-47)
3 files modified
src/Agent.vala (+30/-29)
src/Interfaces.vala (+1/-1)
src/Utils.vala (+29/-17)
To merge this branch: bzr merge lp:~donadigo/pantheon-agent-geoclue2/code-improvements
Reviewer Review Type Date Requested Status
David Hewitt code, function Approve
Review via email: mp+318306@code.launchpad.net

Commit message

* Initialize fields in construct
* Make GeoClue2Manager a singleton
* Add null checks for methods that need them

Description of the change

This branch improves the way initializing variables, client and manager handling works.

Initializing is now done in the construct clause, and the assignments in fields are now gone.

Instead of getting the proxy of the GeoClue Manager everytime, it is now a singleton, so it is initialized once.

There are also some null checks added because methods like get_geoclue_client could return a nullable object.

There are still some things to think about, but I think it's a good start.

To post a comment you must log in.
17. By Adam Bieńkowski

Resolve conflicts

18. By Adam Bieńkowski

Start / stop the client only when needed

19. By Adam Bieńkowski

Make get_remembered_accuracy nullable; initialize client everytime authorization happens

Revision history for this message
David Hewitt (davidmhewitt) wrote :

Thanks for this, makes some good improvements.

review: Approve (code, function)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/Agent.vala'
--- src/Agent.vala 2017-02-25 00:36:08 +0000
+++ src/Agent.vala 2017-02-25 15:02:07 +0000
@@ -21,19 +21,20 @@
2121
22namespace Ag {22namespace Ag {
23 public class Agent : Gtk.Application, GeoClue2Agent {23 public class Agent : Gtk.Application, GeoClue2Agent {
24 private const string app_id = "org.pantheon.agent-geoclue2";
25
26 public uint max_accuracy_level { get { return GeoClue2.AccuracyLevel.EXACT; } }24 public uint max_accuracy_level { get { return GeoClue2.AccuracyLevel.EXACT; } }
25
27 private MainLoop loop;26 private MainLoop loop;
28 private uint object_id;27 private uint object_id;
29 private bool bus_registered = false;28 private bool bus_registered = false;
3029
31 private GeoClue2Client? client = null;30 private GeoClue2Client? client = null;
32 private Settings settings = new Settings (app_id);31 private Settings settings;
33 private VariantDict remembered_apps;32 private VariantDict remembered_apps;
3433
35 public Agent () {34 construct {
36 Object (application_id: app_id);35 application_id = "org.pantheon.agent-geoclue2";
36 settings = new Settings (application_id);
37
37 loop = new MainLoop (); 38 loop = new MainLoop ();
38 load_remembered_apps (); 39 load_remembered_apps ();
39 }40 }
@@ -53,11 +54,9 @@
53 object_id = conn.register_object ("/org/freedesktop/GeoClue2/Agent", (GeoClue2Agent)this);54 object_id = conn.register_object ("/org/freedesktop/GeoClue2/Agent", (GeoClue2Agent)this);
54 bus_registered = true;55 bus_registered = true;
55 register_with_geoclue.begin ();56 register_with_geoclue.begin ();
56
57
58 } catch (Error e) {57 } catch (Error e) {
59 error ("Error while registering the agent: %s \n", e.message);58 error ("Error while registering the agent: %s \n", e.message);
60 }59 }
61 }60 }
6261
63 private void watch (DBusConnection connection) {62 private void watch (DBusConnection connection) {
@@ -75,10 +74,11 @@
75 if (bus_registered) {74 if (bus_registered) {
76 connection.unregister_object (object_id);75 connection.unregister_object (object_id);
77 }76 }
77
78 base.dbus_unregister (connection, object_path);78 base.dbus_unregister (connection, object_path);
79 }79 }
80 80
81 public void authorize_app (string id, uint req_accuracy, out bool authorized, out uint allowed_accuracy) {81 public async void authorize_app (string id, uint req_accuracy, out bool authorized, out uint allowed_accuracy) {
82 debug ("Request for '%s' at level '%u'", id, req_accuracy);82 debug ("Request for '%s' at level '%u'", id, req_accuracy);
8383
84 DesktopAppInfo app_info = new DesktopAppInfo (id + ".desktop");84 DesktopAppInfo app_info = new DesktopAppInfo (id + ".desktop");
@@ -92,28 +92,28 @@
92 // Reload the config in case something else changed it92 // Reload the config in case something else changed it
93 load_remembered_apps ();93 load_remembered_apps ();
9494
95 Variant remembered_accuracy = get_remembered_accuracy (id);95 Variant? remembered_accuracy = get_remembered_accuracy (id);
96 if (remembered_accuracy != null) {96 if (remembered_accuracy != null) {
97 var stored_accuracy = remembered_accuracy.get_uint32();97 var stored_accuracy = remembered_accuracy.get_uint32();
98 if (req_accuracy <= stored_accuracy) {98 if (req_accuracy <= stored_accuracy) {
99 authorized = true;99 authorized = true;
100 allowed_accuracy = req_accuracy;100 allowed_accuracy = req_accuracy;
101 return;101 return;
102 } 102 }
103 }103 }
104104
105 string app_name = app_info.get_display_name ();105 string app_name = app_info.get_display_name ();
106 string accuracy_string = accuracy_to_string (app_name, req_accuracy);106 string accuracy_string = accuracy_to_string (app_name, req_accuracy);
107107
108 debug ("Registering client...");108 client = yield get_geoclue_client ();
109 get_geoclue_client.begin ((obj, res) => {109 debug ("Starting client...");
110 client = get_geoclue_client.end (res);110 if (client != null) {
111 try {111 try {
112 client.start ();112 client.start ();
113 } catch (Error e) {113 } catch (Error e) {
114 warning ("Error while registering geoclue client: %s", e.message);114 error ("Could not start client: %s", e.message);
115 }115 }
116 }); 116 }
117117
118 var dialog = new Widgets.Geoclue2Dialog (accuracy_string, app_name, app_info.get_icon ().to_string ());118 var dialog = new Widgets.Geoclue2Dialog (accuracy_string, app_name, app_info.get_icon ().to_string ());
119 dialog.show_all ();119 dialog.show_all ();
@@ -137,15 +137,16 @@
137137
138 dialog.destroy ();138 dialog.destroy ();
139139
140 if (client != null) {140 allowed_accuracy = req_accuracy;
141
142 debug ("Stopping client...");
143 if (client != null) {
141 try {144 try {
142 client.stop ();145 client.stop ();
143 } catch (Error e) {146 } catch (Error e) {
144 warning ("Error while stopping geoclue client: %s", e.message);147 error ("Could not stop client: %s", e.message);
145 }148 }
146 }149 }
147
148 allowed_accuracy = req_accuracy;
149 }150 }
150151
151 private string accuracy_to_string (string app_name, uint accuracy) {152 private string accuracy_to_string (string app_name, uint accuracy) {
@@ -175,11 +176,11 @@
175 }176 }
176177
177 private async void register_with_geoclue () {178 private async void register_with_geoclue () {
178 yield Utils.register_with_geoclue (app_id);179 yield Utils.register_with_geoclue (application_id);
179 }180 }
180181
181 private async GeoClue2Client get_geoclue_client () {182 private async GeoClue2Client? get_geoclue_client () {
182 return yield Utils.get_geoclue2_client (app_id);183 return yield Utils.get_geoclue2_client (application_id);
183 }184 }
184185
185 private void load_remembered_apps () {186 private void load_remembered_apps () {
@@ -191,7 +192,7 @@
191 settings.set_value ("remembered-apps", remembered_apps.end ());192 settings.set_value ("remembered-apps", remembered_apps.end ());
192 }193 }
193194
194 public Variant get_remembered_accuracy (string desktop_id) {195 public Variant? get_remembered_accuracy (string desktop_id) {
195 return remembered_apps.lookup_value (desktop_id, GLib.VariantType.UINT32);196 return remembered_apps.lookup_value (desktop_id, GLib.VariantType.UINT32);
196 }197 }
197 }198 }
198199
=== modified file 'src/Interfaces.vala'
--- src/Interfaces.vala 2017-02-21 00:10:21 +0000
+++ src/Interfaces.vala 2017-02-25 15:02:07 +0000
@@ -29,7 +29,7 @@
29 [DBus (name = "org.freedesktop.GeoClue2.Agent")]29 [DBus (name = "org.freedesktop.GeoClue2.Agent")]
30 public interface GeoClue2Agent : GLib.Object {30 public interface GeoClue2Agent : GLib.Object {
31 [DBus (name = "AuthorizeApp")]31 [DBus (name = "AuthorizeApp")]
32 public abstract void authorize_app(string desktop_id, uint req_accuracy_level, out bool authorized, out uint allowed_accuracy_level) throws DBusError, IOError;32 public abstract async void authorize_app(string desktop_id, uint req_accuracy_level, out bool authorized, out uint allowed_accuracy_level) throws DBusError, IOError;
33 [DBus (name ="MaxAccuracyLevel")]33 [DBus (name ="MaxAccuracyLevel")]
34 public abstract uint max_accuracy_level { get; }34 public abstract uint max_accuracy_level { get; }
35 }35 }
3636
=== modified file 'src/Utils.vala'
--- src/Utils.vala 2017-02-21 00:10:21 +0000
+++ src/Utils.vala 2017-02-25 15:02:07 +0000
@@ -23,28 +23,27 @@
23 public const string GEOCLUE2_MANAGER_IFACE = "org.freedesktop.GeoClue2";23 public const string GEOCLUE2_MANAGER_IFACE = "org.freedesktop.GeoClue2";
24 public const string GEOCLUE2_MANAGER_PATH = "/org/freedesktop/GeoClue2/Manager";24 public const string GEOCLUE2_MANAGER_PATH = "/org/freedesktop/GeoClue2/Manager";
2525
26 public async void register_with_geoclue (string app_id) {26 public async void register_with_geoclue (string app_id) {
27 try {27 GeoClue2Manager? manager = yield get_geoclue_manager ();
28 GeoClue2Manager? manager = yield Bus.get_proxy (BusType.SYSTEM, GEOCLUE2_MANAGER_IFACE, GEOCLUE2_MANAGER_PATH);28 if (manager != null) {
29 yield manager.add_agent (app_id);29 try {
30 } catch (Error e) {30 yield manager.add_agent (app_id);
31 warning ("Unable to register with GeoClue2: %s", e.message);31 } catch (Error e) {
32 warning ("Unable to register with GeoClue2: %s", e.message);
33 }
32 }34 }
33 }35 }
3436
35 public async GeoClue2Client? get_geoclue2_client (string app_id) {37 public async GeoClue2Client? get_geoclue2_client (string app_id) {
38 GeoClue2Manager? manager = yield get_geoclue_manager ();
39 if (manager == null) {
40 return null;
41 }
42
36 GeoClue2Client? client = null;43 GeoClue2Client? client = null;
37 ObjectPath? path = null;44
3845 try {
39 try {46 ObjectPath? path = yield manager.get_client ();
40 GeoClue2Manager? manager = yield Bus.get_proxy (BusType.SYSTEM, GEOCLUE2_MANAGER_IFACE, GEOCLUE2_MANAGER_PATH);
41 path = yield manager.get_client ();
42 } catch (Error e) {
43 warning ("Unable to register with GeoClue2: %s", e.message);
44 return null;
45 }
46
47 try {
48 client = yield Bus.get_proxy (BusType.SYSTEM, GEOCLUE2_MANAGER_IFACE, path);47 client = yield Bus.get_proxy (BusType.SYSTEM, GEOCLUE2_MANAGER_IFACE, path);
49 } catch (Error e) {48 } catch (Error e) {
50 warning ("Unable to get Private Client proxy: %s", e.message);49 warning ("Unable to get Private Client proxy: %s", e.message);
@@ -57,4 +56,17 @@
5756
58 return client;57 return client;
59 }58 }
59
60 private static GeoClue2Manager? instance;
61 private static async unowned GeoClue2Manager? get_geoclue_manager () {
62 if (instance == null) {
63 try {
64 instance = yield Bus.get_proxy (BusType.SYSTEM, GEOCLUE2_MANAGER_IFACE, GEOCLUE2_MANAGER_PATH);
65 } catch (Error e) {
66 warning ("Unable to connect to %s: %s", GEOCLUE2_MANAGER_PATH, e.message);
67 }
68 }
69
70 return instance;
71 }
60}72}

Subscribers

People subscribed via source and target branches

to all changes: