-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathLocationPanel.vala
More file actions
324 lines (276 loc) · 11.2 KB
/
Copy pathLocationPanel.vala
File metadata and controls
324 lines (276 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2011-2026 elementary, Inc. (https://elementary.io)
*
* Authored by: Jaap Broekhuizen
*/
public class Maya.View.EventEdition.LocationPanel : Gtk.Box {
private EventDialog parent_dialog;
private Gtk.SearchEntry location_entry;
private Gtk.ListStore location_store;
private GtkChamplain.Embed champlain_embed;
private Maya.Marker point;
// Only set the geo property if map_selected is true, this is a smart behavior!
private bool map_selected = false;
private GLib.Cancellable search_cancellable;
private GLib.Cancellable find_cancellable;
public string location {
get { return location_entry.get_text (); }
set { location_entry.set_text (value); }
}
public LocationPanel (EventDialog parent_dialog) {
this.parent_dialog = parent_dialog;
location_store = new Gtk.ListStore (2, typeof (string), typeof (string));
var location_completion = new Gtk.EntryCompletion () {
minimum_key_length = 3,
model = location_store
};
location_completion.set_match_func ((completion, key, iter) => {
Value val1, val2;
Gtk.ListStore model = (Gtk.ListStore)completion.get_model ();
model.get_value (iter, 0, out val1);
model.get_value (iter, 1, out val2);
if (val1.get_string ().casefold (-1).contains (key) || val2.get_string ().casefold (-1).contains (key)) {
return true;
}
return false;
});
location_completion.set_text_column (0);
location_completion.set_text_column (1);
location_completion.match_selected.connect ((model, iter) => suggestion_selected (model, iter));
location_entry = new Gtk.SearchEntry () {
completion = location_completion,
hexpand = true,
placeholder_text = _("John Smith OR Example St.")
};
location_entry.activate.connect (() => {
compute_location.begin (location_entry.text);
});
var location_label = new Granite.HeaderLabel (_("Location:")) {
mnemonic_widget = location_entry
};
champlain_embed = new GtkChamplain.Embed ();
point = new Maya.Marker ();
point.draggable = parent_dialog.can_edit;
point.drag_finish.connect (() => {
map_selected = true;
find_location.begin (point.latitude, point.longitude);
});
var marker_layer = new Champlain.MarkerLayer.full (SINGLE);
marker_layer.add_marker (point);
var view = champlain_embed.champlain_view;
view.zoom_level = 10;
view.goto_animation_duration = 500;
view.add_layer (marker_layer);
view.center_on (point.latitude, point.longitude);
load_contact.begin ();
var frame = new Gtk.Frame (null) {
child = champlain_embed
};
margin_start = 12;
margin_end = 12;
orientation = VERTICAL;
spacing = 6;
sensitive = parent_dialog.can_edit;
add (location_label);
add (location_entry);
add (frame);
// Load the location
if (parent_dialog.ecal != null) {
unowned ICal.Component comp = parent_dialog.ecal.get_icalcomponent ();
unowned string location = comp.get_location ();
if (location != null) {
location_entry.text = location.dup ();
}
ICal.Geo? geo;
geo = parent_dialog.ecal.get_geo ();
bool need_relocation = true;
if (geo != null) {
var latitude = geo.get_lat ();
var longitude = geo.get_lon ();
if (latitude >= Champlain.MIN_LATITUDE && longitude >= Champlain.MIN_LONGITUDE &&
latitude <= Champlain.MAX_LATITUDE && longitude <= Champlain.MAX_LONGITUDE) {
need_relocation = false;
point.latitude = latitude;
point.longitude = longitude;
if (latitude == 0 && longitude == 0)
need_relocation = true;
}
}
if (need_relocation == true) {
if (location != null && location != "") {
compute_location.begin (location_entry.text);
} else {
// Use geoclue to find approximate location
discover_location.begin ();
}
}
}
location_entry.grab_focus ();
}
~LocationPanel () {
if (search_cancellable != null) {
search_cancellable.cancel ();
}
if (find_cancellable != null) {
find_cancellable.cancel ();
}
}
/**
* Save the values in the dialog into the component.
*/
public void save () {
// Save the location
unowned ICal.Component comp = parent_dialog.ecal.get_icalcomponent ();
string location = location_entry.text;
comp.set_location (location);
if (map_selected == true) {
// First, clear the geo
int count = comp.count_properties (ICal.PropertyKind.GEO_PROPERTY);
for (int i = 0; i < count; i++) {
ICal.Property remove_prop;
remove_prop = comp.get_first_property (ICal.PropertyKind.GEO_PROPERTY);
comp.remove_property (remove_prop);
}
// Add the comment
var property = new ICal.Property (ICal.PropertyKind.GEO_PROPERTY);
var geo = new ICal.Geo (point.latitude, point.longitude);
property.set_geo (geo);
comp.add_property (property);
}
}
private async void compute_location (string loc) {
if (search_cancellable != null)
search_cancellable.cancel ();
search_cancellable = new GLib.Cancellable ();
var forward = new Geocode.Forward.for_string (loc);
try {
forward.set_answer_count (1);
var places = yield forward.search_async (search_cancellable);
foreach (var place in places) {
point.latitude = place.location.latitude;
point.longitude = place.location.longitude;
Idle.add (() => {
if (search_cancellable.is_cancelled () == false)
champlain_embed.champlain_view.go_to (point.latitude, point.longitude);
return false;
});
}
if (loc == location_entry.text)
map_selected = true;
location_entry.has_focus = true;
} catch (Error error) {
debug (error.message);
}
}
private async void find_location (double latitude, double longitude) {
if (find_cancellable != null) {
find_cancellable.cancel ();
}
find_cancellable = new GLib.Cancellable ();
Geocode.Location location = new Geocode.Location (latitude, longitude);
var reverse = new Geocode.Reverse.for_location (location);
try {
var address = yield reverse.resolve_async (find_cancellable);
var builder = new StringBuilder ();
if (address.street != null) {
builder.append (address.street);
add_address_line (builder, address.town);
add_address_line (builder, address.county);
add_address_line (builder, address.postal_code);
add_address_line (builder, address.country);
} else {
builder.append (address.name);
add_address_line (builder, address.country);
}
location_entry.text = builder.str;
} catch (Error error) {
debug (error.message);
}
}
private async void discover_location () {
if (search_cancellable != null) {
search_cancellable.cancel ();
}
search_cancellable = new GLib.Cancellable ();
try {
var simple = yield new GClue.Simple ("io.elementary.calendar", GClue.AccuracyLevel.CITY, null);
point.latitude = simple.location.latitude;
point.longitude = simple.location.longitude;
Idle.add (() => {
if (search_cancellable.is_cancelled () == false)
champlain_embed.champlain_view.go_to (point.latitude, point.longitude);
return false;
});
} catch (Error e) {
/* Do NOT attempt a fallback. User intent is that they not be located.
* Attempting to locate anyway is perceived as a breach of consent
* https://github.com/elementary/calendar/issues/540
*/
warning ("Failed to connect to GeoClue2 service: %s", e.message);
return;
}
}
private void add_address_line (StringBuilder sb, string? text) {
if (text != null) {
sb.append (", ");
sb.append (text);
}
}
/**
* Filter all contacts with address information and
* add them to the location store.
*/
private async void add_contacts_store (Gee.Map<string, Folks.Individual> contacts) {
Gtk.TreeIter contact;
var map_iterator = contacts.map_iterator ();
while (map_iterator.next ()) {
foreach (var address in map_iterator.get_value ().postal_addresses) {
location_store.append (out contact);
location_store.set (contact, 0, map_iterator.get_value ().full_name, 1, address.value.street);
}
}
}
/**
* Load the backend and call add_contacts_store with all
* contacts.
*/
private async void load_contact () {
var aggregator = Folks.IndividualAggregator.dup ();
if (aggregator.is_prepared) {
add_contacts_store.begin (aggregator.individuals);
} else {
aggregator.notify["is-quiescent"].connect (() => {
add_contacts_store.begin (aggregator.individuals);
});
aggregator.prepare.begin ();
}
}
private bool suggestion_selected (Gtk.TreeModel model, Gtk.TreeIter iter) {
Value address;
model.get_value (iter, 1, out address);
location_entry.set_text (address.get_string ());
compute_location.begin (address.get_string ());
return true;
}
}
public class Maya.Marker : Champlain.Marker {
public Marker () {
try {
weak Gtk.IconTheme icon_theme = Gtk.IconTheme.get_default ();
var pixbuf = icon_theme.load_icon ("location-marker", 32, Gtk.IconLookupFlags.GENERIC_FALLBACK);
Clutter.Image image = new Clutter.Image ();
image.set_data (pixbuf.get_pixels (),
pixbuf.has_alpha ? Cogl.PixelFormat.RGBA_8888 : Cogl.PixelFormat.RGB_888,
pixbuf.width,
pixbuf.height,
pixbuf.rowstride);
content = image;
set_size (pixbuf.width, pixbuf.height);
translation_x = -pixbuf.width / 2;
translation_y = -pixbuf.height;
} catch (Error e) {
critical (e.message);
}
}
}