aboutsummaryrefslogtreecommitdiff
path: root/src/application-service-lru-file.c
blob: c69a20f24c1a89229198be92c38fd226dc75d744 (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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
This object manages the database of when the entires were touched
and loved.  And writes that out to disk occationally as well.

Copyright 2010 Canonical Ltd.

Authors:
    Ted Gould <ted@canonical.com>

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

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <gio/gio.h>
#include <json-glib/json-glib.h>
#include "application-service-lru-file.h"

#define ENTRY_CATEGORY   "category"
#define ENTRY_FIRST_TIME "first-time"
#define ENTRY_LAST_TIME  "last-time"
#define ENTRY_VERSION    "version"

#define CONFIG_DIR       ("indicators" G_DIR_SEPARATOR_S "application")
#define CONFIG_FILE      "lru-file.json"

typedef struct _AppLruFilePrivate AppLruFilePrivate;
struct _AppLruFilePrivate {
	GHashTable * apps;
	gboolean dirty;
	guint timer;
	gchar * filename;
};

typedef struct _AppData AppData;
struct _AppData {
	gchar * category;
	GTimeVal last_touched;
	GTimeVal first_touched;
};

#define APP_LRU_FILE_GET_PRIVATE(o) \
		(G_TYPE_INSTANCE_GET_PRIVATE ((o), APP_LRU_FILE_TYPE, AppLruFilePrivate))

static void app_lru_file_class_init (AppLruFileClass *klass);
static void app_lru_file_init       (AppLruFile *self);
static void app_lru_file_dispose    (GObject *object);
static void app_lru_file_finalize   (GObject *object);
static void app_data_free           (gpointer data);
static void get_dirty (AppLruFile * lrufile);
static gboolean load_from_file      (gpointer data);
static void load_file_object_cb (JsonObject * obj, const gchar * key, JsonNode * value, gpointer data);
static void clean_off_hash_cb (gpointer key, gpointer value, gpointer data);
static void clean_off_write_end_cb (GObject * obj, GAsyncResult * res, gpointer data);

G_DEFINE_TYPE (AppLruFile, app_lru_file, G_TYPE_OBJECT);

/* Set up the class variable stuff */
static void
app_lru_file_class_init (AppLruFileClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	g_type_class_add_private (klass, sizeof (AppLruFilePrivate));

	object_class->dispose = app_lru_file_dispose;
	object_class->finalize = app_lru_file_finalize;

	return;
}

/* Set all the data of the per-instance variables */
static void
app_lru_file_init (AppLruFile *self)
{
	AppLruFilePrivate * priv = APP_LRU_FILE_GET_PRIVATE(self);

	/* Default values */
	priv->apps = NULL;
	priv->dirty = FALSE;
	priv->timer = 0;
	priv->filename = NULL;

	/* Now let's build some stuff */
	priv->apps = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, app_data_free);
	priv->filename = g_build_filename(g_get_user_config_dir(), CONFIG_DIR, CONFIG_FILE, NULL);

	/* No reason to delay other stuff for this, we'll
	   merge any values that get touched. */
	g_idle_add(load_from_file, self);

	return;
}

/* Get rid of objects and other big things */
static void
app_lru_file_dispose (GObject *object)
{
	AppLruFilePrivate * priv = APP_LRU_FILE_GET_PRIVATE(object);

	if (priv->timer != 0) {
		g_source_remove(priv->timer);
		priv->timer = 0;
	}

	if (priv->apps != NULL) {
		/* Cleans up the keys and entries itself */
		g_hash_table_destroy(priv->apps);
		priv->apps = NULL;
	}

	G_OBJECT_CLASS (app_lru_file_parent_class)->dispose (object);
	return;
}

/* Deallocate memory */
static void
app_lru_file_finalize (GObject *object)
{
	AppLruFilePrivate * priv = APP_LRU_FILE_GET_PRIVATE(object);
	
	if (priv->filename != NULL) {
		g_free(priv->filename);
		priv->filename = NULL;
	}

	G_OBJECT_CLASS (app_lru_file_parent_class)->finalize (object);
	return;
}

/* Takes an AppData structure and free's the
   memory from it. */
static void
app_data_free (gpointer data)
{
	AppData * appdata = (AppData *)data;

	if (appdata->category != NULL) {
		g_free(appdata->category);
	}

	g_free(appdata);

	return;
}

/* Loads all of the data out of a json file */
static gboolean
load_from_file (gpointer data)
{
	AppLruFile * lrufile = (AppLruFile *)data;
	AppLruFilePrivate * priv = APP_LRU_FILE_GET_PRIVATE(lrufile);

	if (!g_file_test(priv->filename, G_FILE_TEST_EXISTS)) {
		return FALSE;
	}

	JsonParser * parser = json_parser_new();

	if (!json_parser_load_from_file(parser, priv->filename, NULL)) {
		g_warning("Unable to parse JSON file '%s'", priv->filename);
		g_object_unref(parser);
		return FALSE;
	}

	JsonNode * root = json_parser_get_root(parser);
	JsonObject * rootobj = json_node_get_object(root);
	if (rootobj == NULL) {
		g_warning("Malformed LRU file.  The root node is not an object.");
		g_object_unref(parser);
		return FALSE;
	}

	json_object_foreach_member(rootobj, load_file_object_cb, lrufile);

	g_object_unref(parser);
	return FALSE;
}

/* Looks at the various things that we need on a node, makes
   sure that we have them, and then copies them into the
   application hash table of love. */
static void
load_file_object_cb (JsonObject * rootobj, const gchar * key, JsonNode * value, gpointer data)
{
	AppLruFile * lrufile = (AppLruFile *)data;
	AppLruFilePrivate * priv = APP_LRU_FILE_GET_PRIVATE(lrufile);

	/* We're not looking at this today. */
	if (!g_strcmp0(key, ENTRY_VERSION)) {
		return;
	}

	JsonObject * obj = json_node_get_object(value);
	if (obj == NULL) {
		g_warning("Data for node '%s' is not an object.", key);
		return;
	}

	const gchar * obj_category = json_object_get_string_member(obj, ENTRY_CATEGORY);
	const gchar * obj_first    = json_object_get_string_member(obj, ENTRY_FIRST_TIME);
	const gchar * obj_last     = json_object_get_string_member(obj, ENTRY_LAST_TIME);

	if (obj_category == NULL || obj_first == NULL || obj_last == NULL) {
		g_warning("Node '%s' is missing data.  Got: ('%s', '%s', '%s')", key, obj_category, obj_first, obj_last);
		get_dirty(lrufile);
		return;
	}

	/* Check to see how old this entry is.  If it hasn't been
	   used in the last year, we remove the cruft. */
	GTimeVal currenttime;
	g_get_current_time(&currenttime);
	GDate * currentdate = g_date_new();
	g_date_set_time_val(currentdate, &currenttime);

	GTimeVal lasttouch;
	g_time_val_from_iso8601(obj_last, &lasttouch);
	GDate * lastdate = g_date_new();
	g_date_set_time_val(lastdate, &lasttouch);

	gint spread = g_date_days_between(lastdate, currentdate);

	g_date_free(currentdate);
	g_date_free(lastdate);

	if (spread > 365) {
		g_debug("Removing node '%s' as it's %d days old.", key, spread);
		get_dirty(lrufile);
		return;
	}

	/* See if we already have one of these.  It's a little bit
	   unlikely, but since we're async, we need to check */
	gpointer datapntr = g_hash_table_lookup(priv->apps, key);
	if (datapntr == NULL) {
		/* Build a new node */
		AppData * appdata = g_new0(AppData, 1);
		appdata->category = g_strdup(obj_category);
		g_time_val_from_iso8601(obj_first, &appdata->first_touched);
		g_time_val_from_iso8601(obj_last, &appdata->last_touched);

		g_hash_table_insert(priv->apps, g_strdup(key), appdata);
	} else {
		/* Merge nodes */
		AppData * appdata = (AppData *)datapntr;
		GTimeVal temptime;
		g_time_val_from_iso8601(obj_first, &temptime);

		if (temptime.tv_sec < appdata->first_touched.tv_sec) {
			g_time_val_from_iso8601(obj_first, &appdata->first_touched);
		}
	}

	return;
}

/* Write out our cache to a file so that we can unmark the dirty
   bit and be happy. */
static gboolean
clean_off (gpointer data)
{
	AppLruFile * lrufile = (AppLruFile *)data;
	AppLruFilePrivate * priv = APP_LRU_FILE_GET_PRIVATE(lrufile);
	priv->timer = 0;

	GError * error = NULL;
	
	/* Check to see if our directory exists.  Build it if not. */
	gchar * dirname = g_build_filename(g_get_user_config_dir(), CONFIG_DIR, NULL);
	if (!g_file_test(dirname, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
		GFile * dirfile = g_file_new_for_path(dirname);
		g_file_make_directory_with_parents(dirfile, NULL, NULL);
		g_object_unref(dirfile);
	}
	g_free(dirname);

	GFile * file = g_file_new_for_path(priv->filename);
	GFileOutputStream * ostream = g_file_replace(file,
	                                             NULL, /* etag */
	                                             TRUE, /* backup */
	                                             G_FILE_CREATE_NONE, /* flags */
	                                             NULL, /* cancelable */
	                                             &error);
	if (error != NULL) {
		g_warning("Unable to open a file to store LRU file: %s", error->message);
		return FALSE;
	}

	/* This is how the file will start */
	GString * filestring = g_string_new("{\n  \"" ENTRY_VERSION "\": 1");

	/* Put the middle in. */
	g_hash_table_foreach (priv->apps, clean_off_hash_cb, filestring);

	/* And then tack on the end. */
	g_string_append(filestring, "\n}\n");
	gchar * filedata = g_string_free(filestring, FALSE);
	g_output_stream_write_async(G_OUTPUT_STREAM(ostream),
	                            filedata,
	                            strlen(filedata),
	                            G_PRIORITY_DEFAULT_IDLE,
	                            NULL,
	                            clean_off_write_end_cb,
	                            filedata);

	return FALSE; /* drop the timer */
}

/* Looks at every value in the applications hash table and
   turns it into a string for writing out. */
static void
clean_off_hash_cb (gpointer key, gpointer value, gpointer data)
{
	/* Mega-cast */
	gchar * id = (gchar *)key;
	AppData * appdata = (AppData *)value;
	GString * string = (GString *)data;

	gchar * firsttime = g_time_val_to_iso8601(&appdata->first_touched);
	gchar * lasttime = g_time_val_to_iso8601(&appdata->last_touched);

	g_string_append_printf(string, ",\n  \"%s\": { \"" ENTRY_FIRST_TIME "\": \"%s\", \"" ENTRY_LAST_TIME "\": \"%s\", \"" ENTRY_CATEGORY "\": \"%s\"}", id, firsttime, lasttime, appdata->category);

	g_free(lasttime);
	g_free(firsttime);

	return;
}

/* Very much like clean_off_write_cb except that it is the
   last actor on this Output Stream so it closes it. */
static void
clean_off_write_end_cb (GObject * obj, GAsyncResult * res, gpointer data)
{
	g_free(data);

	GError * error = NULL;
	g_output_stream_close(G_OUTPUT_STREAM(obj), NULL, &error);

	if (error != NULL) {
		g_warning("Unable to close LRU File: %s", error->message);
		g_error_free(error);
	}

	return;
}

/* Sets the dirty bit if not already set and makes sure that
   we have a timer to fix that at some point. */
static void
get_dirty (AppLruFile * lrufile)
{
	AppLruFilePrivate * priv = APP_LRU_FILE_GET_PRIVATE(lrufile);

	priv->dirty = TRUE;

	if (priv->timer == 0) {
		priv->timer = g_timeout_add_seconds(60, clean_off, lrufile);
	}

	return;
}

/* API */

/* Simple helper to create a new object */
AppLruFile *
app_lru_file_new (void)
{
	return APP_LRU_FILE(g_object_new(APP_LRU_FILE_TYPE, NULL));
}

/* This updates the timestamp for a particular
   entry in the database.  It also queues up a 
   write out of the database.  But that'll happen
   later. */
void
app_lru_file_touch (AppLruFile * lrufile, const gchar * id, const gchar * category)
{
	g_return_if_fail(IS_APP_LRU_FILE(lrufile));
	g_return_if_fail(id != NULL && id[0] != '\0');
	g_return_if_fail(category != NULL && category[0] != '\0');

	AppData * appdata = NULL;
	AppLruFilePrivate * priv = APP_LRU_FILE_GET_PRIVATE(lrufile);
	gpointer data = g_hash_table_lookup(priv->apps, id);

	if (data == NULL) {
		/* Oh, we don't have one, let's build it and put it
		   into the hash table ourselves */
		appdata = g_new0(AppData, 1);

		appdata->category = g_strdup(category);
		g_get_current_time(&(appdata->first_touched));
		/* NOTE: last touched set below */

		g_hash_table_insert(priv->apps, g_strdup(id), appdata);
	} else {
		/* Boring, we've got this one already */
		appdata = (AppData *)data;
	}

	/* Touch it and mark the DB as dirty */
	g_get_current_time(&(appdata->last_touched));
	get_dirty(lrufile);
	return;
}

/* Used to sort or compare different applications. */
gint
app_lru_file_sort (AppLruFile * lrufile, const gchar * id_a, const gchar * id_b)
{
	g_return_val_if_fail(IS_APP_LRU_FILE(lrufile), -1);

	/* Let's first look to see if the IDs are the same, this
	   really shouldn't happen, but it'll be confusing if we
	   don't get it out of the way to start. */
	if (g_strcmp0(id_a, id_b) == 0) {
		return 0;
	}

	AppLruFilePrivate * priv = APP_LRU_FILE_GET_PRIVATE(lrufile);

	/* Now make sure we have app data for both of these.  If
	   not we'll assume that the one without is newer? */
	gpointer data_a = g_hash_table_lookup(priv->apps, id_a);
	if (data_a == NULL) {
		return -1;
	}

	gpointer data_b = g_hash_table_lookup(priv->apps, id_b);
	if (data_b == NULL) {
		return 1;
	}

	/* Ugly casting */
	AppData * appdata_a = (AppData *)data_a;
	AppData * appdata_b = (AppData *)data_b;

	/* Look at categories, we'll put the categories in alpha
	   order if they're not the same. */
	gint catcompare = g_strcmp0(appdata_a->category, appdata_b->category);
	if (catcompare != 0) {
		return catcompare;
	}

	/* Now we're looking at the first time that these two were
	   seen in this account.  Only using seconds.  I mean, seriously. */
	if (appdata_a->first_touched.tv_sec < appdata_b->first_touched.tv_sec) {
		return -1;
	}
	if (appdata_b->first_touched.tv_sec < appdata_a->first_touched.tv_sec) {
		return 1;
	}

	/* Eh, this seems roughly impossible.  But if we have to choose,
	   I like A better. */
	return 1;
}