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
1=== modified file 'src/Agent.vala'
2--- src/Agent.vala 2017-02-25 00:36:08 +0000
3+++ src/Agent.vala 2017-02-25 15:02:07 +0000
4@@ -21,19 +21,20 @@
5
6 namespace Ag {
7 public class Agent : Gtk.Application, GeoClue2Agent {
8- private const string app_id = "org.pantheon.agent-geoclue2";
9-
10 public uint max_accuracy_level { get { return GeoClue2.AccuracyLevel.EXACT; } }
11+
12 private MainLoop loop;
13 private uint object_id;
14 private bool bus_registered = false;
15
16 private GeoClue2Client? client = null;
17- private Settings settings = new Settings (app_id);
18+ private Settings settings;
19 private VariantDict remembered_apps;
20
21- public Agent () {
22- Object (application_id: app_id);
23+ construct {
24+ application_id = "org.pantheon.agent-geoclue2";
25+ settings = new Settings (application_id);
26+
27 loop = new MainLoop ();
28 load_remembered_apps ();
29 }
30@@ -53,11 +54,9 @@
31 object_id = conn.register_object ("/org/freedesktop/GeoClue2/Agent", (GeoClue2Agent)this);
32 bus_registered = true;
33 register_with_geoclue.begin ();
34-
35-
36 } catch (Error e) {
37 error ("Error while registering the agent: %s \n", e.message);
38- }
39+ }
40 }
41
42 private void watch (DBusConnection connection) {
43@@ -75,10 +74,11 @@
44 if (bus_registered) {
45 connection.unregister_object (object_id);
46 }
47+
48 base.dbus_unregister (connection, object_path);
49 }
50
51- public void authorize_app (string id, uint req_accuracy, out bool authorized, out uint allowed_accuracy) {
52+ public async void authorize_app (string id, uint req_accuracy, out bool authorized, out uint allowed_accuracy) {
53 debug ("Request for '%s' at level '%u'", id, req_accuracy);
54
55 DesktopAppInfo app_info = new DesktopAppInfo (id + ".desktop");
56@@ -92,28 +92,28 @@
57 // Reload the config in case something else changed it
58 load_remembered_apps ();
59
60- Variant remembered_accuracy = get_remembered_accuracy (id);
61+ Variant? remembered_accuracy = get_remembered_accuracy (id);
62 if (remembered_accuracy != null) {
63 var stored_accuracy = remembered_accuracy.get_uint32();
64 if (req_accuracy <= stored_accuracy) {
65 authorized = true;
66 allowed_accuracy = req_accuracy;
67 return;
68- }
69+ }
70 }
71
72 string app_name = app_info.get_display_name ();
73 string accuracy_string = accuracy_to_string (app_name, req_accuracy);
74
75- debug ("Registering client...");
76- get_geoclue_client.begin ((obj, res) => {
77- client = get_geoclue_client.end (res);
78+ client = yield get_geoclue_client ();
79+ debug ("Starting client...");
80+ if (client != null) {
81 try {
82 client.start ();
83- } catch (Error e) {
84- warning ("Error while registering geoclue client: %s", e.message);
85- }
86- });
87+ } catch (Error e) {
88+ error ("Could not start client: %s", e.message);
89+ }
90+ }
91
92 var dialog = new Widgets.Geoclue2Dialog (accuracy_string, app_name, app_info.get_icon ().to_string ());
93 dialog.show_all ();
94@@ -137,15 +137,16 @@
95
96 dialog.destroy ();
97
98- if (client != null) {
99+ allowed_accuracy = req_accuracy;
100+
101+ debug ("Stopping client...");
102+ if (client != null) {
103 try {
104 client.stop ();
105- } catch (Error e) {
106- warning ("Error while stopping geoclue client: %s", e.message);
107- }
108- }
109-
110- allowed_accuracy = req_accuracy;
111+ } catch (Error e) {
112+ error ("Could not stop client: %s", e.message);
113+ }
114+ }
115 }
116
117 private string accuracy_to_string (string app_name, uint accuracy) {
118@@ -175,11 +176,11 @@
119 }
120
121 private async void register_with_geoclue () {
122- yield Utils.register_with_geoclue (app_id);
123+ yield Utils.register_with_geoclue (application_id);
124 }
125
126- private async GeoClue2Client get_geoclue_client () {
127- return yield Utils.get_geoclue2_client (app_id);
128+ private async GeoClue2Client? get_geoclue_client () {
129+ return yield Utils.get_geoclue2_client (application_id);
130 }
131
132 private void load_remembered_apps () {
133@@ -191,7 +192,7 @@
134 settings.set_value ("remembered-apps", remembered_apps.end ());
135 }
136
137- public Variant get_remembered_accuracy (string desktop_id) {
138+ public Variant? get_remembered_accuracy (string desktop_id) {
139 return remembered_apps.lookup_value (desktop_id, GLib.VariantType.UINT32);
140 }
141 }
142
143=== modified file 'src/Interfaces.vala'
144--- src/Interfaces.vala 2017-02-21 00:10:21 +0000
145+++ src/Interfaces.vala 2017-02-25 15:02:07 +0000
146@@ -29,7 +29,7 @@
147 [DBus (name = "org.freedesktop.GeoClue2.Agent")]
148 public interface GeoClue2Agent : GLib.Object {
149 [DBus (name = "AuthorizeApp")]
150- public abstract void authorize_app(string desktop_id, uint req_accuracy_level, out bool authorized, out uint allowed_accuracy_level) throws DBusError, IOError;
151+ public abstract async void authorize_app(string desktop_id, uint req_accuracy_level, out bool authorized, out uint allowed_accuracy_level) throws DBusError, IOError;
152 [DBus (name ="MaxAccuracyLevel")]
153 public abstract uint max_accuracy_level { get; }
154 }
155
156=== modified file 'src/Utils.vala'
157--- src/Utils.vala 2017-02-21 00:10:21 +0000
158+++ src/Utils.vala 2017-02-25 15:02:07 +0000
159@@ -23,28 +23,27 @@
160 public const string GEOCLUE2_MANAGER_IFACE = "org.freedesktop.GeoClue2";
161 public const string GEOCLUE2_MANAGER_PATH = "/org/freedesktop/GeoClue2/Manager";
162
163- public async void register_with_geoclue (string app_id) {
164- try {
165- GeoClue2Manager? manager = yield Bus.get_proxy (BusType.SYSTEM, GEOCLUE2_MANAGER_IFACE, GEOCLUE2_MANAGER_PATH);
166- yield manager.add_agent (app_id);
167- } catch (Error e) {
168- warning ("Unable to register with GeoClue2: %s", e.message);
169+ public async void register_with_geoclue (string app_id) {
170+ GeoClue2Manager? manager = yield get_geoclue_manager ();
171+ if (manager != null) {
172+ try {
173+ yield manager.add_agent (app_id);
174+ } catch (Error e) {
175+ warning ("Unable to register with GeoClue2: %s", e.message);
176+ }
177 }
178 }
179
180 public async GeoClue2Client? get_geoclue2_client (string app_id) {
181+ GeoClue2Manager? manager = yield get_geoclue_manager ();
182+ if (manager == null) {
183+ return null;
184+ }
185+
186 GeoClue2Client? client = null;
187- ObjectPath? path = null;
188-
189- try {
190- GeoClue2Manager? manager = yield Bus.get_proxy (BusType.SYSTEM, GEOCLUE2_MANAGER_IFACE, GEOCLUE2_MANAGER_PATH);
191- path = yield manager.get_client ();
192- } catch (Error e) {
193- warning ("Unable to register with GeoClue2: %s", e.message);
194- return null;
195- }
196-
197- try {
198+
199+ try {
200+ ObjectPath? path = yield manager.get_client ();
201 client = yield Bus.get_proxy (BusType.SYSTEM, GEOCLUE2_MANAGER_IFACE, path);
202 } catch (Error e) {
203 warning ("Unable to get Private Client proxy: %s", e.message);
204@@ -57,4 +56,17 @@
205
206 return client;
207 }
208+
209+ private static GeoClue2Manager? instance;
210+ private static async unowned GeoClue2Manager? get_geoclue_manager () {
211+ if (instance == null) {
212+ try {
213+ instance = yield Bus.get_proxy (BusType.SYSTEM, GEOCLUE2_MANAGER_IFACE, GEOCLUE2_MANAGER_PATH);
214+ } catch (Error e) {
215+ warning ("Unable to connect to %s: %s", GEOCLUE2_MANAGER_PATH, e.message);
216+ }
217+ }
218+
219+ return instance;
220+ }
221 }

Subscribers

People subscribed via source and target branches

to all changes: