summaryrefslogtreecommitdiff
path: root/csrc/services/application-service.c
blob: 42835ace9413331afb381b5057c51d063f7072df (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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#include "services/application-service.h"
#include "launcher-item.h"
#include "utils.h"
#include <gtk/gtk.h>
#include <gio/gdesktopappinfo.h>
#include <json-glib/json-glib.h>
#include <string.h>

//
// FoobarApplicationItem:
//
// Represents a single application/desktop file.
//

struct _FoobarApplicationItem
{
	GObject                   parent_instance;
	FoobarApplicationService* service;
	GAppInfo*                 info;
};

enum
{
	APP_PROP_ID = 1,
	APP_PROP_EXECUTABLE,
	APP_PROP_CATEGORIES,
	APP_PROP_FREQUENCY,
	APP_PROP_TITLE,
	APP_PROP_DESCRIPTION,
	APP_PROP_ICON,
	N_APP_PROPS,
};

static GParamSpec* app_props[N_APP_PROPS] = { 0 };

static void                   foobar_application_item_class_init                  ( FoobarApplicationItemClass* klass );
static void                   foobar_application_item_launcher_item_interface_init( FoobarLauncherItemInterface* iface );
static void                   foobar_application_item_init                        ( FoobarApplicationItem*      self );
static void                   foobar_application_item_get_property                ( GObject*                    object,
                                                                                    guint                       prop_id,
                                                                                    GValue*                     value,
                                                                                    GParamSpec*                 pspec );
static void                   foobar_application_item_finalize                    ( GObject*                    object );
static FoobarApplicationItem* foobar_application_item_new                         ( FoobarApplicationService*   service );
static gchar const*           foobar_application_item_get_title                   ( FoobarLauncherItem*         self );
static gchar const*           foobar_application_item_get_description             ( FoobarLauncherItem*         self );
static GIcon*                 foobar_application_item_get_icon                    ( FoobarLauncherItem*         self );
static void                   foobar_application_item_activate                    ( FoobarLauncherItem*         self );
static void                   foobar_application_item_set_info                    ( FoobarApplicationItem*      self,
                                                                                    GAppInfo*                   value );

G_DEFINE_FINAL_TYPE_WITH_CODE(
	FoobarApplicationItem,
	foobar_application_item,
	G_TYPE_OBJECT,
	G_IMPLEMENT_INTERFACE( FOOBAR_TYPE_LAUNCHER_ITEM, foobar_application_item_launcher_item_interface_init ) )

//
// FoobarApplicationService:
//
// Service providing a list of all installed applications. This implemented using GLib's AppInfo API.
//

struct _FoobarApplicationService
{
	GObject           parent_instance;
	GListStore*       items;
	GtkSortListModel* sorted_items;
	GAppInfoMonitor*  monitor;
	GHashTable*       frequencies; // only modified on the main thread, but possibly read on a background thread
	GMutex            frequencies_mutex;
	GMutex            write_cache_mutex;
	gchar*            cache_path;
	gulong            changed_handler_id;
};

enum
{
	PROP_ITEMS = 1,
	N_PROPS,
};

static GParamSpec* props[N_PROPS] = { 0 };

static void     foobar_application_service_class_init            ( FoobarApplicationServiceClass* klass );
static void     foobar_application_service_init                  ( FoobarApplicationService*      self );
static void     foobar_application_service_get_property          ( GObject*                       object,
                                                                   guint                          prop_id,
                                                                   GValue*                        value,
                                                                   GParamSpec*                    pspec );
static void     foobar_application_service_finalize              ( GObject*                       object );
static void     foobar_application_service_handle_changed        ( GAppInfoMonitor*               monitor,
                                                                   gpointer                       userdata );
static void     foobar_application_service_update                ( FoobarApplicationService*      self );
static void     foobar_application_service_read_cache            ( FoobarApplicationService*      self );
static void     foobar_application_service_read_cache_foreach_cb ( JsonObject*                    object,
                                                                   gchar const*                   member_name,
                                                                   JsonNode*                      member_node,
                                                                   gpointer                       userdata );
static void     foobar_application_service_write_cache           ( FoobarApplicationService*      self );
static void     foobar_application_service_write_cache_cb        ( GObject*                       object,
                                                                   GAsyncResult*                  result,
                                                                   gpointer                       userdata );
static void     foobar_application_service_write_cache_async     ( FoobarApplicationService*      self,
                                                                   GCancellable*                  cancellable,
                                                                   GAsyncReadyCallback            callback,
                                                                   gpointer                       userdata );
static gboolean foobar_application_service_write_cache_finish    ( FoobarApplicationService*      self,
                                                                   GAsyncResult*                  result,
                                                                   GError**                       error );
static void     foobar_application_service_write_cache_thread    ( GTask*                         task,
                                                                   gpointer                       source_object,
                                                                   gpointer                       task_data,
                                                                   GCancellable*                  cancellable );
static void     foobar_application_service_write_cache_foreach_cb( gpointer                       key,
                                                                   gpointer                       value,
                                                                   gpointer                       userdata );
static gint     foobar_application_service_sort_func             ( gconstpointer                  item_a,
                                                                   gconstpointer                  item_b,
                                                                   gpointer                       userdata );

G_DEFINE_FINAL_TYPE( FoobarApplicationService, foobar_application_service, G_TYPE_OBJECT )

// ---------------------------------------------------------------------------------------------------------------------
// Item Implementation
// ---------------------------------------------------------------------------------------------------------------------

//
// Static initialization for application items.
//
void foobar_application_item_class_init( FoobarApplicationItemClass* klass )
{
	GObjectClass* object_klass = G_OBJECT_CLASS( klass );
	object_klass->get_property = foobar_application_item_get_property;
	object_klass->finalize = foobar_application_item_finalize;

	gpointer launcher_item_iface = g_type_default_interface_peek( FOOBAR_TYPE_LAUNCHER_ITEM );
	app_props[APP_PROP_ID] = g_param_spec_string(
		"id",
		"ID",
		"Application identifier string.",
		NULL,
		G_PARAM_READABLE );
	app_props[APP_PROP_EXECUTABLE] = g_param_spec_string(
		"executable",
		"Executable",
		"The executable name of the application.",
		NULL,
		G_PARAM_READABLE );
	app_props[APP_PROP_CATEGORIES] = g_param_spec_string(
		"categories",
		"Categories",
		"Semicolon-separated list of application categories.",
		NULL,
		G_PARAM_READABLE );
	app_props[APP_PROP_FREQUENCY] = g_param_spec_int64(
		"frequency",
		"Frequency",
		"The number of times the application was opened.",
		0,
		INT64_MAX,
		0,
		G_PARAM_READABLE );
	app_props[APP_PROP_TITLE] = g_param_spec_override(
		"title",
		g_object_interface_find_property( launcher_item_iface, "title" ) );
	app_props[APP_PROP_DESCRIPTION] = g_param_spec_override(
		"description",
		g_object_interface_find_property( launcher_item_iface, "description" ) );
	app_props[APP_PROP_ICON] = g_param_spec_override(
		"icon",
		g_object_interface_find_property( launcher_item_iface, "icon" ) );
	g_object_class_install_properties( object_klass, N_APP_PROPS, app_props );
}

//
// Static initialization of the FoobarLauncherItem interface.
//
void foobar_application_item_launcher_item_interface_init( FoobarLauncherItemInterface* iface )
{
	iface->get_title = foobar_application_item_get_title;
	iface->get_description = foobar_application_item_get_description;
	iface->get_icon = foobar_application_item_get_icon;
	iface->activate = foobar_application_item_activate;
}

//
// Instance initialization for application items.
//
void foobar_application_item_init( FoobarApplicationItem* self )
{
	(void)self;
}

//
// Property getter implementation, mapping a property id to a method.
//
void foobar_application_item_get_property(
	GObject*    object,
	guint       prop_id,
	GValue*     value,
	GParamSpec* pspec )
{
	FoobarApplicationItem* self = (FoobarApplicationItem*)object;

	switch ( prop_id )
	{
		case APP_PROP_ID:
			g_value_set_string( value, foobar_application_item_get_id( self ) );
			break;
		case APP_PROP_EXECUTABLE:
			g_value_set_string( value, foobar_application_item_get_executable( self ) );
			break;
		case APP_PROP_CATEGORIES:
			g_value_set_string( value, foobar_application_item_get_categories( self ) );
			break;
		case APP_PROP_FREQUENCY:
			g_value_set_int64( value, foobar_application_item_get_frequency( self ) );
			break;
		case APP_PROP_TITLE:
			g_value_set_string( value, foobar_launcher_item_get_title( FOOBAR_LAUNCHER_ITEM ( self ) ) );
			break;
		case APP_PROP_DESCRIPTION:
			g_value_set_string( value, foobar_launcher_item_get_description( FOOBAR_LAUNCHER_ITEM( self ) ) );
			break;
		case APP_PROP_ICON:
			g_value_set_object( value, foobar_launcher_item_get_icon( FOOBAR_LAUNCHER_ITEM( self ) ) );
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID( object, prop_id, pspec );
			break;
	}
}

//
// Instance cleanup for application items.
//
void foobar_application_item_finalize( GObject* object )
{
	FoobarApplicationItem* self = (FoobarApplicationItem*)object;

	g_clear_object( &self->info );

	G_OBJECT_CLASS( foobar_application_item_parent_class )->finalize( object );
}

//
// Create a new application item belonging to the parent service (captured as an unowned reference).
//
FoobarApplicationItem* foobar_application_item_new( FoobarApplicationService* service )
{
	FoobarApplicationItem* self = g_object_new( FOOBAR_TYPE_APPLICATION_ITEM, NULL );
	self->service = service;
	return self;
}

//
// Get the application's unique textual identifier.
//
gchar const* foobar_application_item_get_id( FoobarApplicationItem* self )
{
	g_return_val_if_fail( FOOBAR_IS_APPLICATION_ITEM( self ), NULL );
	return self->info ? g_app_info_get_id( self->info ) : NULL;
}

//
// Get the application's executable name.
//
gchar const* foobar_application_item_get_executable( FoobarApplicationItem* self )
{
	g_return_val_if_fail( FOOBAR_IS_APPLICATION_ITEM( self ), NULL );
	return self->info ? g_app_info_get_executable( self->info ) : NULL;
}

//
// Get the semicolon-separated list of categories for this application.
//
gchar const* foobar_application_item_get_categories( FoobarApplicationItem* self )
{
	g_return_val_if_fail( FOOBAR_IS_APPLICATION_ITEM( self ), NULL );
	return G_IS_DESKTOP_APP_INFO( self->info )
		? g_desktop_app_info_get_categories( G_DESKTOP_APP_INFO( self->info ) )
		: NULL;
}

//
// Get the number of times this application was opened using the launcher.
//
gint64 foobar_application_item_get_frequency( FoobarApplicationItem* self )
{
	g_return_val_if_fail( FOOBAR_IS_APPLICATION_ITEM( self ), 0 );

	if ( !self->service ) { return 0; }
	gpointer value = g_hash_table_lookup( self->service->frequencies, foobar_application_item_get_id( self ) );
	return GPOINTER_TO_SIZE( value );
}

//
// Get the application's human-readable name.
//
gchar const* foobar_application_item_get_title( FoobarLauncherItem* item )
{
	FoobarApplicationItem* self = ( FoobarApplicationItem* )item;
	return self->info ? g_app_info_get_display_name( self->info ) : NULL;
}

//
// Get an optional short description for the application.
//
gchar const* foobar_application_item_get_description( FoobarLauncherItem* item )
{
	FoobarApplicationItem* self = ( FoobarApplicationItem* )item;
	return self->info ? g_app_info_get_description( self->info ) : NULL;
}

//
// Get an icon representing the application.
//
GIcon* foobar_application_item_get_icon( FoobarLauncherItem* item )
{
	FoobarApplicationItem* self = ( FoobarApplicationItem* )item;
	return self->info ? g_app_info_get_icon( self->info ) : NULL;
}

//
// Launch the application, increasing its frequency by one.
//
void foobar_application_item_activate( FoobarLauncherItem* item )
{
	FoobarApplicationItem* self = ( FoobarApplicationItem* )item;

	if ( !self->info ) { return; }

	g_autoptr( GError ) error = NULL;
	if ( !g_app_info_launch( self->info, NULL, NULL, &error ) )
	{
		g_warning( "Unable to launch application: %s", error->message );
	}

	gchar const* id = foobar_application_item_get_id( self );
	if ( !self->service || !id ) { return; }

	gpointer value = g_hash_table_lookup( self->service->frequencies, id );
	value = GSIZE_TO_POINTER( GPOINTER_TO_SIZE( value ) + 1 );
	g_mutex_lock( &self->service->frequencies_mutex );
	g_hash_table_insert( self->service->frequencies, g_strdup( id ), value );
	g_mutex_unlock( &self->service->frequencies_mutex );

	g_object_notify_by_pspec( G_OBJECT( self ), app_props[APP_PROP_FREQUENCY] );
	gtk_sorter_changed( gtk_sort_list_model_get_sorter( self->service->sorted_items ), GTK_SORTER_CHANGE_DIFFERENT );
	foobar_application_service_write_cache( self->service );
}

//
// Update the backing GAppInfo object for the application.
//
void foobar_application_item_set_info(
	FoobarApplicationItem* self,
	GAppInfo*              value )
{
	g_return_if_fail( FOOBAR_IS_APPLICATION_ITEM( self ) );

	if ( self->info != value )
	{
		g_clear_object( &self->info );

		if ( value ) { self->info = g_object_ref( value ); }

		g_object_notify_by_pspec( G_OBJECT( self ), app_props[APP_PROP_ID] );
		g_object_notify_by_pspec( G_OBJECT( self ), app_props[APP_PROP_TITLE] );
		g_object_notify_by_pspec( G_OBJECT( self ), app_props[APP_PROP_DESCRIPTION] );
		g_object_notify_by_pspec( G_OBJECT( self ), app_props[APP_PROP_EXECUTABLE] );
		g_object_notify_by_pspec( G_OBJECT( self ), app_props[APP_PROP_ICON] );
		g_object_notify_by_pspec( G_OBJECT( self ), app_props[APP_PROP_FREQUENCY] );
	}
}

//
// Match the item against the given search terms.
//
gboolean foobar_application_item_match(
	FoobarApplicationItem* self,
	gchar const* const*    terms )
{
	g_return_val_if_fail( FOOBAR_IS_APPLICATION_ITEM( self ), FALSE );
	g_return_val_if_fail( terms != NULL, FALSE );

	for ( gchar const* const* it = terms; *it; ++it )
	{
		gchar const* term = *it;
		gchar const* name = foobar_launcher_item_get_title( FOOBAR_LAUNCHER_ITEM( self ) );
		gchar const* description = foobar_launcher_item_get_description( FOOBAR_LAUNCHER_ITEM( self ) );
		gchar const* executable = foobar_application_item_get_executable( self );
		gchar const* categories = foobar_application_item_get_categories( self );
		gchar const* id = foobar_application_item_get_id( self );
		if ( name && strcasestr( name, term ) != NULL ) { continue; }
		if ( description && strcasestr( description, term ) != NULL ) { continue; }
		if ( executable && strcasestr( executable, term ) != NULL ) { continue; }
		if ( categories && strcasestr( categories, term ) != NULL ) { continue; }
		if ( id && strcasestr( id, term ) != NULL ) { continue; }

		return FALSE;
	}

	return TRUE;
}

// ---------------------------------------------------------------------------------------------------------------------
// Service Implementation
// ---------------------------------------------------------------------------------------------------------------------

//
// Static initialization for the application service.
//
void foobar_application_service_class_init( FoobarApplicationServiceClass* klass )
{
	GObjectClass* object_klass = G_OBJECT_CLASS( klass );
	object_klass->get_property = foobar_application_service_get_property;
	object_klass->finalize = foobar_application_service_finalize;

	props[PROP_ITEMS] = g_param_spec_object(
		"items",
		"Items",
		"Application items, sorted by their frequency.",
		G_TYPE_LIST_MODEL,
		G_PARAM_READABLE );
	g_object_class_install_properties( object_klass, N_PROPS, props );
}

//
// Instance initialization for the application service.
//
void foobar_application_service_init( FoobarApplicationService* self )
{
	self->items = g_list_store_new( FOOBAR_TYPE_APPLICATION_ITEM );

	GtkCustomSorter* sorter = gtk_custom_sorter_new( foobar_application_service_sort_func, NULL, NULL );
	self->sorted_items = gtk_sort_list_model_new( G_LIST_MODEL( g_object_ref( self->items ) ), GTK_SORTER( sorter ) );

	self->cache_path = foobar_get_cache_path( "application-frequencies.json" );
	self->frequencies = g_hash_table_new_full( g_str_hash, g_str_equal, g_free, NULL );
	g_mutex_init( &self->frequencies_mutex );
	g_mutex_init( &self->write_cache_mutex );

	foobar_application_service_read_cache( self );
	foobar_application_service_update( self );

	self->monitor = g_app_info_monitor_get( );
	self->changed_handler_id = g_signal_connect(
		self->monitor,
		"changed",
		G_CALLBACK( foobar_application_service_handle_changed ),
		self );
}

//
// Property getter implementation, mapping a property id to a method.
//
void foobar_application_service_get_property(
	GObject*    object,
	guint       prop_id,
	GValue*     value,
	GParamSpec* pspec )
{
	FoobarApplicationService* self = (FoobarApplicationService*)object;

	switch ( prop_id )
	{
		case PROP_ITEMS:
			g_value_set_object( value, foobar_application_service_get_items( self ) );
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID( object, prop_id, pspec );
			break;
	}
}

//
// Instance cleanup for the application service.
//
void foobar_application_service_finalize( GObject* object )
{
	FoobarApplicationService* self = (FoobarApplicationService*)object;

	for ( guint i = 0; i < g_list_model_get_n_items( G_LIST_MODEL( self->items ) ); ++i )
	{
		FoobarApplicationItem* item = g_list_model_get_item( G_LIST_MODEL( self->items ), i );
		item->service = NULL;
		g_object_notify_by_pspec( G_OBJECT( item ), app_props[APP_PROP_FREQUENCY] );
	}
	g_clear_signal_handler( &self->changed_handler_id, self->monitor );
	g_clear_object( &self->sorted_items );
	g_clear_object( &self->items );
	g_clear_object( &self->monitor );
	g_clear_pointer( &self->frequencies, g_hash_table_unref );
	g_clear_pointer( &self->cache_path, g_free );

	g_mutex_clear( &self->frequencies_mutex );
	g_mutex_clear( &self->write_cache_mutex );

	G_OBJECT_CLASS( foobar_application_service_parent_class )->finalize( object );
}

// ---------------------------------------------------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------------------------------------------------

//
// Create a new application service instance.
//
FoobarApplicationService* foobar_application_service_new( void )
{
	return g_object_new( FOOBAR_TYPE_APPLICATION_SERVICE, NULL );
}

//
// Get a sorted list of all available applications.
//
GListModel* foobar_application_service_get_items( FoobarApplicationService* self )
{
	g_return_val_if_fail( FOOBAR_IS_APPLICATION_SERVICE( self ), NULL );

	return G_LIST_MODEL( self->sorted_items );
}

// ---------------------------------------------------------------------------------------------------------------------
// Signal Handlers
// ---------------------------------------------------------------------------------------------------------------------

//
// Called by GLib when the list of applications has changed.
//
void foobar_application_service_handle_changed(
	GAppInfoMonitor* monitor,
	gpointer         userdata )
{
	(void)monitor;
	FoobarApplicationService* self = (FoobarApplicationService*)userdata;

	foobar_application_service_update( self );
}

// ---------------------------------------------------------------------------------------------------------------------
// Helper Methods
// ---------------------------------------------------------------------------------------------------------------------

//
// Reload the list of applications.
//
void foobar_application_service_update( FoobarApplicationService* self )
{
	g_list_store_remove_all( self->items );

	g_autolist( GAppInfo ) info = g_app_info_get_all( );
	for ( GList* it = info; it; it = it->next )
	{
		if ( g_app_info_should_show( it->data ) )
		{
			g_autoptr( FoobarApplicationItem ) item = foobar_application_item_new( self );
			foobar_application_item_set_info( item, it->data );
			g_list_store_append( self->items, item );
		}
	}
}

//
// Synchronously read the frequency cache file at self->cache_path, populating self->frequencies.
//
void foobar_application_service_read_cache( FoobarApplicationService* self )
{
	g_autoptr( GError ) error = NULL;

	if ( self->cache_path && g_file_test( self->cache_path, G_FILE_TEST_EXISTS ) )
	{
		g_autoptr( JsonParser ) parser = json_parser_new( );
		if ( !json_parser_load_from_mapped_file( parser, self->cache_path, &error ) )
		{
			g_warning( "Unable to read cached application frequencies: %s", error->message );
			return;
		}

		JsonNode* root_node = json_parser_get_root( parser );
		JsonObject* root_object = json_node_get_object( root_node );
		json_object_foreach_member( root_object, foobar_application_service_read_cache_foreach_cb, self );
	}
}

//
// Callback used by foobar_application_service_read_cache to read a single entry.
//
void foobar_application_service_read_cache_foreach_cb(
	JsonObject*  object,
	gchar const* member_name,
	JsonNode*    member_node,
	gpointer     userdata )
{
	(void)object;
	FoobarApplicationService* self = (FoobarApplicationService*)userdata;

	gsize frequency = json_node_get_int( member_node );
	g_hash_table_insert( self->frequencies, g_strdup( member_name ), GSIZE_TO_POINTER( frequency ) );
}

//
// Asynchronously start writing the frequency cache file at self->cache_path.
//
void foobar_application_service_write_cache( FoobarApplicationService* self )
{
	foobar_application_service_write_cache_async( self, NULL, foobar_application_service_write_cache_cb, NULL );
}

//
// Callback invoked when the frequency cache was written successfully or failed.
//
void foobar_application_service_write_cache_cb(
	GObject*      object,
	GAsyncResult* result,
	gpointer      userdata )
{
	(void)userdata;
	FoobarApplicationService* self = (FoobarApplicationService*)object;

	g_autoptr( GError ) error = NULL;
	if ( !foobar_application_service_write_cache_finish( self, result, &error ) )
	{
		g_warning( "Unable to write cached application frequencies: %s", error->message );
	}
}

//
// Asynchronously write the frequency cache file at self->cache_path.
//
void foobar_application_service_write_cache_async(
	FoobarApplicationService* self,
	GCancellable*             cancellable,
	GAsyncReadyCallback       callback,
	gpointer                  userdata )
{
	g_autoptr( GTask ) task = g_task_new( self, cancellable, callback, userdata );
	g_task_set_name( task, "write-application-cache" );
	g_task_run_in_thread( task, foobar_application_service_write_cache_thread );
}

//
// Get the asynchronous result for writing the frequency cache file, returning TRUE on success, or FALSE on error.
//
gboolean foobar_application_service_write_cache_finish(
	FoobarApplicationService* self,
	GAsyncResult*             result,
	GError**                  error )
{
	(void)self;

	return g_task_propagate_boolean( G_TASK( result ), error );
}

//
// Task implementation for foobar_application_service_write_cache_async, invoked on a background thread.
//
void foobar_application_service_write_cache_thread(
	GTask*        task,
	gpointer      source_object,
	gpointer      task_data,
	GCancellable* cancellable )
{
	(void)cancellable;
	(void)task_data;
	FoobarApplicationService* self = (FoobarApplicationService*)source_object;

	if ( !self->cache_path )
	{
		g_task_return_boolean( task, TRUE );
		return;
	}

	g_autoptr( GError ) error = NULL;

	g_autoptr( JsonBuilder ) builder = json_builder_new( );
	json_builder_begin_object( builder );

	g_mutex_lock( &self->frequencies_mutex );
	g_hash_table_foreach( self->frequencies, foobar_application_service_write_cache_foreach_cb, builder );
	g_mutex_unlock( &self->frequencies_mutex );

	json_builder_end_object( builder );

	g_autoptr( JsonNode ) root_node = json_builder_get_root( builder );
	g_autoptr( JsonGenerator ) generator = json_generator_new( );
	json_generator_set_root( generator, root_node );

	g_mutex_lock( &self->write_cache_mutex );
	gboolean success = json_generator_to_file( generator, self->cache_path, &error );
	g_mutex_unlock( &self->write_cache_mutex );
	if ( !success )
	{
		g_task_return_error( task, g_steal_pointer( &error ) );
		return;
	}

	g_task_return_boolean( task, TRUE );
}

//
// Callback used by foobar_application_service_write_cache_thread to build a single entry.
//
void foobar_application_service_write_cache_foreach_cb(
	gpointer key,
	gpointer value,
	gpointer userdata )
{
	JsonBuilder* builder = (JsonBuilder*)userdata;

	json_builder_set_member_name( builder, key );
	json_builder_add_int_value( builder, GPOINTER_TO_SIZE( value ) );
}

//
// Sorting callback for application items. Items are sorted based on:
// 1. frequency (descending)
// 2. name (ascending)
// 3. id (ascending)
//
gint foobar_application_service_sort_func(
	gconstpointer item_a,
	gconstpointer item_b,
	gpointer      userdata )
{
	(void)userdata;

	FoobarApplicationItem* app_a = (FoobarApplicationItem*)item_a;
	FoobarApplicationItem* app_b = (FoobarApplicationItem*)item_b;

	gint64 freq_a = foobar_application_item_get_frequency( app_a );
	gint64 freq_b = foobar_application_item_get_frequency( app_b );
	if ( freq_a > freq_b ) { return -1; }
	if ( freq_a < freq_b ) { return 1; }

	gchar const* name_a = foobar_launcher_item_get_title( FOOBAR_LAUNCHER_ITEM( app_a ) );
	gchar const* name_b = foobar_launcher_item_get_title( FOOBAR_LAUNCHER_ITEM( app_b ) );
	gint name_res = g_strcmp0( name_a, name_b );
	if ( name_res ) { return name_res; }

	gchar const* id_a = foobar_application_item_get_id( app_a );
	gchar const* id_b = foobar_application_item_get_id( app_b );
	return g_strcmp0( id_a, id_b );
}