~dobey/ubuntu-app-launch/ship-app-mock

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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
 * Copyright 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *     Ted Gould <ted.gould@canonical.com>
 */

/*

INTRODUCTION:

This is a hook for Click packages.  You can find information on Click package hooks in
the click documentation:

https://click.readthedocs.org/en/latest/

Probably the biggest thing to understand for how this code works is that you need to
understand that this hook is run after one, or many packages are installed.  A set of
symbolic links are made to the desktop files per-application (not per-package) in the
directory specified in ubuntu-app-launcher-desktop.click-hook.in.  Those desktop files
give us the App ID of the packages that are installed and have applications needing
desktop files in them.  We then operate on each of them ensuring that they are synchronized
with the desktop files in ~/.local/share/applications/.

The desktop files that we're creating there ARE NOT used for execution by the
ubuntu-app-launch Upstart jobs.  They are there so that Unity can know which applications
are installed for this user and they provide an Exec line to allow compatibility with
desktop environments that are not using ubuntu-app-launch for launching applications.
You should not modify them and expect any executing under Unity to change.

*/

#include <gio/gio.h>
#include <glib/gstdio.h>
#include <click.h>
#include <string.h>
#include <errno.h>
#include <libwhoopsie/recoverable-problem.h>

#include "helpers.h"

typedef struct _app_state_t app_state_t;
struct _app_state_t {
	gchar * app_id;
	gboolean has_click;
	gboolean has_desktop;
	guint64 click_modified;
	guint64 desktop_modified;
};

/* Desktop Group */
#define DESKTOP_GROUP      "Desktop Entry"
/* Desktop Keys */
#define APP_ID_KEY         "X-Ubuntu-Application-ID"
#define PATH_KEY           "Path"
#define EXEC_KEY           "Exec"
#define ICON_KEY           "Icon"
#define SYMBOLIC_ICON_KEY  "X-Ubuntu-SymbolicIcon"
#define SOURCE_FILE_KEY    "X-Ubuntu-UAL-Source-Desktop"
/* Other */
#define OLD_KEY_PREFIX     "X-Ubuntu-Old-"

/* Find an entry in the app array */
app_state_t *
find_app_entry (const gchar * name, GArray * app_array)
{
	int i;
	for (i = 0; i < app_array->len; i++) {
		app_state_t * state = &g_array_index(app_array, app_state_t, i);

		if (g_strcmp0(state->app_id, name) == 0) {
			return state;
		}
	}

	app_state_t newstate;
	newstate.has_click = FALSE;
	newstate.has_desktop = FALSE;
	newstate.click_modified = 0;
	newstate.desktop_modified = 0;
	newstate.app_id = g_strdup(name);

	g_array_append_val(app_array, newstate);

	/* Note: The pointer needs to be the entry in the array, not the
	   one that we have on the stack.  Criticaly important. */
	app_state_t * statepntr = &g_array_index(app_array, app_state_t, app_array->len - 1);
	return statepntr;
}

/* Looks up the file creation time, which seems harder with GLib
   than it should be */
guint64
modified_time (const gchar * dir, const gchar * filename)
{
	gchar * path = g_build_filename(dir, filename, NULL);
	GFile * file = g_file_new_for_path(path);
	GFileInfo * info = g_file_query_info(file, G_FILE_ATTRIBUTE_TIME_MODIFIED, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL, NULL);

	guint64 time = g_file_info_get_attribute_uint64(info, G_FILE_ATTRIBUTE_TIME_MODIFIED);

	g_object_unref(info);
	g_object_unref(file);
	g_free(path);

	return time;
}

/* Look at an click package entry */
void
add_click_package (const gchar * dir, const gchar * name, GArray * app_array)
{
	if (!g_str_has_suffix(name, ".desktop")) {
		return;
	}

	gchar * appid = g_strdup(name);
	g_strstr_len(appid, -1, ".desktop")[0] = '\0';

	app_state_t * state = find_app_entry(appid, app_array);
	state->has_click = TRUE;
	state->click_modified = modified_time(dir, name);

	g_free(appid);

	return;
}

/* Look at the desktop file and ensure that it was built by us, and if it
   was that its source still exists */
gboolean
desktop_source_exists (const gchar * dir, const gchar * name)
{
	gchar * desktopfile = g_build_filename(dir, name, NULL);

	GKeyFile * keyfile = g_key_file_new();
	g_key_file_load_from_file(keyfile,
		desktopfile,
		G_KEY_FILE_NONE,
		NULL); /* No error */

	if (!g_key_file_has_key(keyfile, DESKTOP_GROUP, SOURCE_FILE_KEY, NULL)) {
		gboolean hasappid = g_key_file_has_key(keyfile, DESKTOP_GROUP, APP_ID_KEY, NULL);
		g_free(desktopfile);
		g_key_file_free(keyfile);
		return hasappid;
	}

	/* At this point we know the key exists, so if we can't find the source
	   file we want to delete the file as well. We need to replace it. */
	gchar * originalfile = g_key_file_get_string(keyfile, DESKTOP_GROUP, SOURCE_FILE_KEY, NULL);
	g_key_file_free(keyfile);
	gboolean found = TRUE;

	if (!g_file_test(originalfile, G_FILE_TEST_EXISTS)) {
		g_remove(desktopfile);
		found = FALSE;
	}

	g_free(originalfile);
	g_free(desktopfile);

	return found;
}

/* Look at an desktop file entry */
void
add_desktop_file (const gchar * dir, const gchar * name, GArray * app_array)
{
	if (!g_str_has_suffix(name, ".desktop")) {
		return;
	}

	if (!desktop_source_exists(dir, name)) {
		return;
	}

	gchar * appid = g_strdup(name);
	g_strstr_len(appid, -1, ".desktop")[0] = '\0';

	/* We only want valid APP IDs as desktop files */
	if (!app_id_to_triplet(appid, NULL, NULL, NULL)) {
		g_free(appid);
		return;
	}

	app_state_t * state = find_app_entry(appid, app_array);
	state->has_desktop = TRUE;
	state->desktop_modified = modified_time(dir, name);

	g_free(appid);
	return;
}

/* Open a directory and look at all the entries */
void
dir_for_each (const gchar * dirname, void(*func)(const gchar * dir, const gchar * name, GArray * app_array), GArray * app_array)
{
	GError * error = NULL;
	GDir * directory = g_dir_open(dirname, 0, &error);

	if (error != NULL) {
		g_warning("Unable to read directory '%s': %s", dirname, error->message);
		g_error_free(error);
		return;
	}

	const gchar * filename = NULL;
	while ((filename = g_dir_read_name(directory)) != NULL) {
		func(dirname, filename, app_array);
	}

	g_dir_close(directory);
	return;
}

/* Code to report an error, so we can start tracking how important this is */
static void
report_recoverable_error (const gchar * app_id, const gchar * iconfield, const gchar * originalicon, const gchar * iconpath)
{
	const char * properties[9] = {
		"IconValue", NULL,
		"AppID", NULL,
		"IconPath", NULL,
		"IconField", NULL,
		NULL
	};

	properties[1] = originalicon;
	properties[3] = app_id;
	properties[5] = iconpath;
	properties[7] = iconfield;

	whoopsie_report_recoverable_problem("icon-path-unhandled", 0, TRUE, properties);

	return;
}

/* Function to take the source Desktop file and build a new
   one with similar, but not the same data in it */
static void
copy_desktop_file (const gchar * from, const gchar * to, const gchar * appdir, const gchar * app_id)
{
	GError * error = NULL;
	GKeyFile * keyfile = g_key_file_new();
	g_key_file_load_from_file(keyfile,
		from,
		G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS,
		&error);

	if (error != NULL) {
		g_warning("Unable to read the desktop file '%s' in the application directory: %s", from, error->message);
		g_error_free(error);
		g_key_file_unref(keyfile);
		return;
	}

	/* Path Hanlding */
	if (g_key_file_has_key(keyfile, DESKTOP_GROUP, PATH_KEY, NULL)) {
		gchar * oldpath = g_key_file_get_string(keyfile, DESKTOP_GROUP, PATH_KEY, NULL);
		g_debug("Desktop file '%s' has a Path set to '%s'.  Setting as " OLD_KEY_PREFIX PATH_KEY ".", from, oldpath);

		g_key_file_set_string(keyfile, DESKTOP_GROUP, OLD_KEY_PREFIX PATH_KEY, oldpath);

		g_free(oldpath);
	}

	g_key_file_set_string(keyfile, DESKTOP_GROUP, PATH_KEY, appdir);

	/* Icon Handling */
	if (g_key_file_has_key(keyfile, DESKTOP_GROUP, ICON_KEY, NULL)) {
		gchar * originalicon = g_key_file_get_string(keyfile, DESKTOP_GROUP, ICON_KEY, NULL);
		gchar * iconpath = g_build_filename(appdir, originalicon, NULL);

		/* If the icon in the path exists, let's use that */
		if (g_file_test(iconpath, G_FILE_TEST_EXISTS)) {
			g_key_file_set_string(keyfile, DESKTOP_GROUP, ICON_KEY, iconpath);
			/* Save the old value, because, debugging */
			g_key_file_set_string(keyfile, DESKTOP_GROUP, OLD_KEY_PREFIX ICON_KEY, originalicon);
		} else {
			/* So here we are, realizing all is lost.  Let's file a bug. */
			/* The goal here is to realize how often this case is, so we know how to prioritize fixing it */

			report_recoverable_error(app_id, ICON_KEY, originalicon, iconpath);
		}

		g_free(iconpath);
		g_free(originalicon);
	}

	/* SymbolicIcon Handling */
	if (g_key_file_has_key(keyfile, DESKTOP_GROUP, SYMBOLIC_ICON_KEY, NULL)) {
		gchar * originalicon = g_key_file_get_string(keyfile, DESKTOP_GROUP, SYMBOLIC_ICON_KEY, NULL);
		gchar * iconpath = g_build_filename(appdir, originalicon, NULL);

		/* If the icon in the path exists, let's use that */
		if (g_file_test(iconpath, G_FILE_TEST_EXISTS)) {
			g_key_file_set_string(keyfile, DESKTOP_GROUP, SYMBOLIC_ICON_KEY, iconpath);
			/* Save the old value, because, debugging */
			g_key_file_set_string(keyfile, DESKTOP_GROUP, OLD_KEY_PREFIX SYMBOLIC_ICON_KEY, originalicon);
		} else {
			/* So here we are, realizing all is lost.  Let's file a bug. */
			/* The goal here is to realize how often this case is, so we know how to prioritize fixing it */

			report_recoverable_error(app_id, SYMBOLIC_ICON_KEY, originalicon, iconpath);
		}

		g_free(iconpath);
		g_free(originalicon);
	}

	/* Exec Handling */
	gchar * oldexec = desktop_to_exec(keyfile, from);
	if (oldexec == NULL) {
		g_key_file_unref(keyfile);
		return;
	}

	gchar * newexec = g_strdup_printf("aa-exec-click -p %s -- %s", app_id, oldexec);
	g_key_file_set_string(keyfile, DESKTOP_GROUP, EXEC_KEY, newexec);
	g_free(newexec);
	g_free(oldexec);

	/* Adding an Application ID */
	g_key_file_set_string(keyfile, DESKTOP_GROUP, APP_ID_KEY, app_id);

	/* Adding the source file path */
	g_key_file_set_string(keyfile, DESKTOP_GROUP, SOURCE_FILE_KEY, from);

	/* Output */
	gsize datalen = 0;
	gchar * data = g_key_file_to_data(keyfile, &datalen, &error);
	g_key_file_unref(keyfile);

	if (error != NULL) {
		g_warning("Unable serialize keyfile built from '%s': %s", from, error->message);
		g_error_free(error);
		return;
	}

	g_file_set_contents(to, data, datalen, &error);
	g_free(data);

	if (error != NULL) {
		g_warning("Unable to write out desktop file to '%s': %s", to, error->message);
		g_error_free(error);
		return;
	}

	return;
}

/* Build a desktop file in the user's home directory */
static void
build_desktop_file (app_state_t * state, const gchar * symlinkdir, const gchar * desktopdir)
{
	GError * error = NULL;
	gchar * package = NULL;
	/* 'Parse' the App ID */
	if (!app_id_to_triplet(state->app_id, &package, NULL, NULL)) {
		return;
	}

	/* Read in the database */
	ClickDB * db = click_db_new();
	click_db_read(db, g_getenv("TEST_CLICK_DB"), &error);
	if (error != NULL) {
		g_warning("Unable to read Click database: %s", error->message);
		g_error_free(error);
		g_free(package);
		g_object_unref(db);
		return;
	}

	/* Check click to find out where the files are */
	ClickUser * user = click_user_new_for_user(db, g_getenv("TEST_CLICK_USER"), &error);
	g_object_unref(db);
	if (error != NULL) {
		g_warning("Unable to read Click database: %s", error->message);
		g_error_free(error);
		g_free(package);
		return;
	}

	gchar * pkgdir = click_user_get_path(user, package, &error);
	if (error != NULL) {
		g_warning("Unable to get the Click package directory for %s: %s", package, error->message);
		g_error_free(error);
		g_free(package);
		return;
	}
	g_object_unref(user);
	g_free(package);

	if (!g_file_test(pkgdir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
		g_warning("Directory returned by click '%s' couldn't be found", pkgdir);
		g_free(pkgdir);
		return;
	}

	gchar * indesktop = manifest_to_desktop(pkgdir, state->app_id);
	if (indesktop == NULL) {
		g_free(pkgdir);
		return;
	}

	/* Determine the desktop file name */
	gchar * desktopfile = g_strdup_printf("%s.desktop", state->app_id);
	gchar * desktoppath = g_build_filename(desktopdir, desktopfile, NULL);
	g_free(desktopfile);

	copy_desktop_file(indesktop, desktoppath, pkgdir, state->app_id);

	g_free(desktoppath);
	g_free(indesktop);
	g_free(pkgdir);

	return;
}

/* Remove the desktop file from the user's home directory */
static gboolean
remove_desktop_file (app_state_t * state, const gchar * desktopdir)
{
	gchar * desktopfile = g_strdup_printf("%s.desktop", state->app_id);
	gchar * desktoppath = g_build_filename(desktopdir, desktopfile, NULL);
	g_free(desktopfile);

	GKeyFile * keyfile = g_key_file_new();
	g_key_file_load_from_file(keyfile,
		desktoppath,
		G_KEY_FILE_NONE,
		NULL);

	if (!g_key_file_has_key(keyfile, DESKTOP_GROUP, APP_ID_KEY, NULL)) {
		g_debug("Desktop file '%s' is not one created by us.", desktoppath);
		g_key_file_unref(keyfile);
		g_free(desktoppath);
		return FALSE;
	}
	g_key_file_unref(keyfile);

	if (g_unlink(desktoppath) != 0) {
		g_warning("Unable to delete desktop file: %s", desktoppath);
	}

	g_free(desktoppath);

	return TRUE;
}

/* The main function */
int
main (int argc, char * argv[])
{
	if (argc != 1) {
		g_error("Shouldn't have arguments");
		return 1;
	}

	GArray * apparray = g_array_new(FALSE, FALSE, sizeof(app_state_t));

	/* Find all the symlinks of desktop files */
	gchar * symlinkdir = g_build_filename(g_get_user_cache_dir(), "ubuntu-app-launch", "desktop", NULL);
	if (!g_file_test(symlinkdir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
		g_debug("No installed click packages");
	} else {
		dir_for_each(symlinkdir, add_click_package, apparray);
	}

	/* Find all the click desktop files */
	gchar * desktopdir = g_build_filename(g_get_user_data_dir(), "applications", NULL);
	gboolean desktopdirexists = FALSE;
	if (!g_file_test(desktopdir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
		g_debug("No applications defined");
	} else {
		dir_for_each(desktopdir, add_desktop_file, apparray);
		desktopdirexists = TRUE;
	}

	/* Process the merge */
	int i;
	for (i = 0; i < apparray->len; i++) {
		app_state_t * state = &g_array_index(apparray, app_state_t, i);
		g_debug("Processing App ID: %s", state->app_id);

		if (state->has_click && state->has_desktop) {
			if (state->click_modified > state->desktop_modified) {
				g_debug("\tClick updated more recently");
				g_debug("\tRemoving desktop file");
				if (remove_desktop_file(state, desktopdir)) {
					g_debug("\tBuilding desktop file");
					build_desktop_file(state, symlinkdir, desktopdir);
				}
			} else {
				g_debug("\tAlready synchronized");
			}
		} else if (state->has_click) {
			if (!desktopdirexists) {
				if (g_mkdir_with_parents(desktopdir, 0755) == 0) {
					g_debug("\tCreated applications directory");
					desktopdirexists = TRUE;
				} else {
					g_warning("\tUnable to create applications directory");
				}
			}
			if (desktopdirexists) {
				g_debug("\tBuilding desktop file");
				build_desktop_file(state, symlinkdir, desktopdir);
			}
		} else if (state->has_desktop) {
			g_debug("\tRemoving desktop file");
			remove_desktop_file(state, desktopdir);
		}

		g_free(state->app_id);
	}

	g_array_free(apparray, TRUE);
	g_free(desktopdir);
	g_free(symlinkdir);

	return 0;
}