summaryrefslogtreecommitdiff
path: root/src/util/locmgr.c
blob: 0f6beadbb367188909d2d2a1262af95a2d1f0d45 (plain)
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <Elementary.h>
#include <media_content.h>
#include <maps_service.h>
#include <app_debug.h>
#include <app_media.h>
#include <vconf.h>

#define MAP_LANG	"en-US"
#define MAP_MAX_RES	1
#define MAP_MAX_REQ	100

#define LOC_TAG_DEFAULT "Unknown"
#define LOC_INVALID	-200

#define KEY_LOC_PLUGIN	"db/app/mediahub/loc_plugin"
#define KEY_LOC_ID	"db/app/mediahub/loc_id"
#define KEY_LOC_CODE	"db/app/mediahub/loc_code"

struct locmgr {
	maps_service_h maps;
	maps_preference_h preference;
	bool supported;
	int queue;
	Eina_List *list;
	char *plugin;
	char *key;
};

struct _cb_data {
	struct locmgr *m;
	app_media *am;
};

static void _add_queue(struct locmgr *m);

static void _set_location_tag(app_media *am, const char *str)
{
	media_info_h media;
	int r;

	if (!str)
		return;

	media = app_media_get_media_handle(am);
	if (!media) {
		_ERR("failed to getting media handle");
		return;
	}

	r = media_info_set_location_tag(media, str);
	if (r != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to set location tag");
		return;
	}

	r = media_info_update_to_db(media);
	if (r != MEDIA_CONTENT_ERROR_NONE) {
		_ERR("failed to update db");
		return;
	}

	app_media_update(am);
}

static void _reverse_geocode_cb(maps_error_e result, int request_id,
		int index, int total, maps_address_h address, void *data)
{
	struct _cb_data *cdata;
	struct locmgr *m;
	app_media *am;
	char *country;
	char *city;
	char str[1024];
	int r;

	if (!data) {
		_ERR("invalid parameter");
		return;
	}

	cdata = data;

	m = cdata->m;
	m->queue--;

	if (!m->queue)
		_add_queue(m);

	am = cdata->am;
	if (!am) {
		_ERR("failed to get app media");
		goto out;
	}

	if (result != MAPS_ERROR_NONE) {
		_ERR("failed to get reverse geocode (%d)", result);
		goto out;
	}

	if (!address) {
		_ERR("failed to get address");
		goto out;
	}

	r = maps_address_get_country(address, &country);
	if (r != MAPS_ERROR_NONE || !country) {
		_ERR("failed to get country (%d)", r);
		maps_address_destroy(address);
		goto out;
	}

	r = maps_address_get_city(address, &city);
	if (r != MAPS_ERROR_NONE || !city) {
		_ERR("failed to get city (%d)", r);
		maps_address_destroy(address);
		free(country);
		goto out;
	}

	snprintf(str, sizeof(str), "%s/%s", city, country);
	_set_location_tag(am, str);

	maps_address_destroy(address);

	free(country);
	free(city);

out:
	free(cdata);
}

static void _reverse_geocode_req(struct locmgr *m, app_media *am)
{
	app_media_info *mi;
	struct _cb_data *cdata;
	int r;
	int id;

	if (!m || !am) {
		_ERR("invalid argument");
		return;
	}

	mi = app_media_get_info(am);
	if (!mi) {
		_ERR("failed to get media info");
		return;
	}

	if (mi->location_tag)
		return;

	if ((mi->latitude == LOC_INVALID && mi->longitude == LOC_INVALID) ||
			(mi->latitude == 0 && mi->longitude == 0)) {
		_set_location_tag(am, LOC_TAG_DEFAULT);
		return;
	}

	cdata = calloc(1, sizeof(*cdata));
	if (!cdata) {
		_ERR("failed to allocate");
		return;
	}

	cdata->m = m;
	cdata->am = am;

	r = maps_service_reverse_geocode(m->maps, mi->latitude, mi->longitude,
			m->preference, _reverse_geocode_cb, cdata, &id);
	if (r != MAPS_ERROR_NONE) {
		_ERR("failed to get geocode");
		return;
	}

	m->queue++;
}

static void _add_queue(struct locmgr *m)
{
	app_media *am;
	Eina_List *l;

	EINA_LIST_FOREACH(m->list, l, am) {
		if (m->queue >= MAP_MAX_REQ)
			break;

		_reverse_geocode_req(m, am);
		m->list = eina_list_remove_list(m->list, l);
	}
}

bool locmgr_set_location(struct locmgr *m, app_media *am)
{
	if (!m || !am) {
		_ERR("invalid argument");
		return false;
	}

	if (m->queue) {
		/* service is busy, add to the list */
		m->list = eina_list_append(m->list, am);
		return true;
	}

	_reverse_geocode_req(m, am);

	return true;
}

bool locmgr_set_locations(struct locmgr *m, Eina_List *list)
{
	Eina_List *l;

	if (!m || !list) {
		_ERR("invalid argument");
		return false;
	}

	l = eina_list_clone(list);

	if (m->queue) {
		/* service is busy, add to the list */
		m->list = eina_list_merge(m->list, l);
		return true;
	}

	m->list = l;

	_add_queue(m);

	return true;
}

static bool _maps_provider_cb(char *provider, void *data)
{
	struct locmgr *m;

	if (!data)
		return false;

	m = data;

	if (!strcmp(provider, m->plugin))
		m->supported = true;

	return true;
}

static int _set_preference(maps_preference_h *preference)
{
	int r;

	r = maps_preference_create(preference);
	if (r != MAPS_ERROR_NONE) {
		_ERR("failed to create preference");
		return r;
	}

	r = maps_preference_set_language(*preference, MAP_LANG);
	if (r != MAPS_ERROR_NONE) {
		_ERR("failed to set language");
		maps_preference_destroy(*preference);
		return r;
	}

	r = maps_preference_set_max_results(*preference, MAP_MAX_RES);
	if (r != MAPS_ERROR_NONE) {
		_ERR("failed to set max result");
		maps_preference_destroy(*preference);
		return r;
	}

	return MAPS_ERROR_NONE;
}

static bool _set_plugin(struct locmgr *m)
{
	char str[1024];
	char *id;
	char *code;

	/*
	 * NOTE:
	 * map service need to set one of map providers
	 * and provider demand a security key for using it.
	 *
	 * mediahub was tested with "HERE" plugin.
	 * You can get app_id and app_code for "HERE" plugin at below website
	 * https://developer.here.com
	 *
	 * please set those 3 vconf values.
	 * plugin: db/app/mediahub/loc_plugin
	 * app_id: db/app/mediahub/loc_id
	 * app_code: db/app/mediahub/loc_code
	 */

	m->plugin = vconf_get_str(KEY_LOC_PLUGIN);
	if (!m->plugin) {
		_ERR("Please set plugin to %s", KEY_LOC_PLUGIN);
		return false;
	}

	id = vconf_get_str(KEY_LOC_ID);
	if (!id) {
		_ERR("Please set app_id to %s", KEY_LOC_ID);
		goto err_id;
	}

	code = vconf_get_str(KEY_LOC_CODE);
	if (!id) {
		_ERR("Please set app_code to %s", KEY_LOC_CODE);
		goto err_code;
	}

	snprintf(str, sizeof(str), "%s/%s", id, code);
	m->key = strdup(str);

	return true;

err_code:
	free(id);
err_id:
	free(m->plugin);
	return false;
}

struct locmgr *locmgr_create(void)
{
	struct locmgr *m;
	maps_service_h maps;
	maps_preference_h preference;
	int r;
	bool s;

	m = calloc(1, sizeof(*m));
	if (!m) {
		_ERR("failed to allocate");
		return false;
	}

	s = _set_plugin(m);
	if (!s) {
		_ERR("can't get location information");
		goto err;
	}

	r = maps_service_foreach_provider(_maps_provider_cb, m);
	if (r != MAPS_ERROR_NONE) {
		_ERR("failed to create maps service");
		goto err_plugin;
	}

	if (!m->supported) {
		_ERR("%s does not support", m->plugin);
		goto err_plugin;
	}

	r = maps_service_create(m->plugin, &maps);
	if (r != MAPS_ERROR_NONE) {
		_ERR("failed to create maps service");
		goto err_plugin;
	}

	r = maps_service_set_provider_key(maps, m->key);
	if (r != MAPS_ERROR_NONE) {
		_ERR("failed to set provider key");
		goto err_maps;
	}

	r = maps_service_provider_is_service_supported(maps,
			MAPS_SERVICE_REVERSE_GEOCODE, &s);
	if (r != MAPS_ERROR_NONE || !s) {
		_ERR("not support reverse geocode");
		goto err_maps;
	}

	r = _set_preference(&preference);
	if (r != MAPS_ERROR_NONE) {
		_ERR("failed to create preference");
		goto err_maps;
	}

	r = maps_service_set_preference(maps, preference);
	if (r != MAPS_ERROR_NONE) {
		_ERR("failed to set preference");
		goto err_preference;
	}

	media_content_connect();

	m->maps = maps;
	m->preference = preference;
	m->queue = 0;

	return m;

err_preference:
	maps_preference_destroy(preference);
err_maps:
	maps_service_destroy(maps);
err_plugin:
	free(m->plugin);
	free(m->key);
err:
	free(m);
	return NULL;
}

void locmgr_destroy(struct locmgr *m)
{
	if (!m) {
		_ERR("invalid argument");
		return;
	}

	media_content_disconnect();

	if (m->preference) {
		maps_preference_destroy(m->preference);
		m->preference = NULL;
	}

	if (m->maps) {
		/*
		 * FIXME:
		 * because maps_service_destroy function have a problem,
		 * disable calling this function temporary
		 */
		/*maps_service_destroy(m->maps);*/
		m->maps = NULL;
	}

	if (m->list) {
		eina_list_free(m->list);
		m->list = NULL;
	}

	free(m->plugin);
	free(m->key);
	free(m);
}