~ci-train-bot/ubuntu-app-launch/ubuntu-app-launch-ubuntu-zesty-2434

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
/*
 * 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>
 */


#include <zeitgeist.h>
#include "libubuntu-app-launch/ubuntu-app-launch.h"

static gboolean
watchdog_timeout (gpointer user_data)
{
	g_warning("Watchdog triggered, took too long to submit into Zeitgeist Database!");
	g_main_loop_quit((GMainLoop *)user_data);

	return G_SOURCE_REMOVE;
}

static void
insert_complete (GObject * obj, GAsyncResult * res, gpointer user_data)
{
	GError * error = NULL;
	GArray * result = NULL;

	result = zeitgeist_log_insert_event_finish(ZEITGEIST_LOG(obj), res, &error);

	if (error != NULL) {
		g_warning("Unable to submit Zeitgeist Event: %s", error->message);
		g_error_free(error);
	}

	g_array_free(result, TRUE);
	g_main_loop_quit((GMainLoop *)user_data);
	return;
}

int
main (int argc, char * argv[])
{
	if (argc != 2 || (g_strcmp0(argv[1], "open") != 0 && g_strcmp0(argv[1], "close") != 0)) {
		g_printerr("Usage: %s [open|close]\n", argv[0]);
		return 1;
	}

	const gchar * appid = g_getenv("APP_ID");
	if (appid == NULL) {
		g_printerr("No App ID defined");
		return 1;
	}

	gchar * uri = NULL;
	gchar * pkg = NULL;
	gchar * app = NULL;

	if (ubuntu_app_launch_app_id_parse(appid, &pkg, &app, NULL)) {
		/* If it's parseable, use the short form */
		uri = g_strdup_printf("application://%s_%s.desktop", pkg, app);
		g_free(pkg);
		g_free(app);
	} else {
		uri = g_strdup_printf("application://%s.desktop", appid);
	}    

	ZeitgeistLog * log = zeitgeist_log_get_default();

	ZeitgeistEvent * event = zeitgeist_event_new();
	zeitgeist_event_set_actor(event, "application://ubuntu-app-launch.desktop");
	if (g_strcmp0(argv[1], "open") == 0) {
		zeitgeist_event_set_interpretation(event, ZEITGEIST_ZG_ACCESS_EVENT);
	} else {
		zeitgeist_event_set_interpretation(event, ZEITGEIST_ZG_LEAVE_EVENT);
	}
	zeitgeist_event_set_manifestation(event, ZEITGEIST_ZG_USER_ACTIVITY);

	ZeitgeistSubject * subject = zeitgeist_subject_new();
	zeitgeist_subject_set_interpretation(subject, ZEITGEIST_NFO_SOFTWARE);
	zeitgeist_subject_set_manifestation(subject, ZEITGEIST_NFO_SOFTWARE_ITEM);
	zeitgeist_subject_set_mimetype(subject, "application/x-desktop");
	zeitgeist_subject_set_uri(subject, uri);

	zeitgeist_event_add_subject(event, subject);

	GMainLoop * main_loop = g_main_loop_new(NULL, FALSE);

	zeitgeist_log_insert_event(log, event, NULL, insert_complete, main_loop);
	g_timeout_add_seconds(2, watchdog_timeout, main_loop);

	g_main_loop_run(main_loop);

	g_main_loop_unref(main_loop);
	g_free(uri);

	return 0;
}