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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
* vi:set noexpandtab tabstop=8 shiftwidth=8:
*
* Copyright (C) 2017-2018 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2018 Kalev Lember <klember@redhat.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include "config.h"
#include <glib.h>
#include "gs-plugin-private.h"
#include "gs-plugin-job-private.h"
struct _GsPluginJob
{
GObject parent_instance;
GsPluginRefineFlags refine_flags;
GsPluginRefineFlags filter_flags;
GsAppListFilterFlags dedupe_flags;
gboolean interactive;
guint max_results;
guint timeout;
guint64 age;
GsPlugin *plugin;
GsPluginAction action;
GsAppListSortFunc sort_func;
gpointer sort_func_data;
gchar *search;
GsApp *app;
GsAppList *list;
GFile *file;
GsCategory *category;
AsReview *review;
gint64 time_created;
};
enum {
PROP_0,
PROP_ACTION,
PROP_AGE,
PROP_SEARCH,
PROP_REFINE_FLAGS,
PROP_FILTER_FLAGS,
PROP_DEDUPE_FLAGS,
PROP_INTERACTIVE,
PROP_APP,
PROP_LIST,
PROP_FILE,
PROP_CATEGORY,
PROP_REVIEW,
PROP_MAX_RESULTS,
PROP_TIMEOUT,
PROP_LAST
};
G_DEFINE_TYPE (GsPluginJob, gs_plugin_job, G_TYPE_OBJECT)
gchar *
gs_plugin_job_to_string (GsPluginJob *self)
{
GString *str = g_string_new (NULL);
gint64 time_now = g_get_monotonic_time ();
g_string_append_printf (str, "running %s",
gs_plugin_action_to_string (self->action));
if (self->plugin != NULL) {
g_string_append_printf (str, " on plugin=%s",
gs_plugin_get_name (self->plugin));
}
if (self->filter_flags > 0) {
g_autofree gchar *tmp = gs_plugin_refine_flags_to_string (self->filter_flags);
g_string_append_printf (str, " with filter-flags=%s", tmp);
}
if (self->dedupe_flags > 0)
g_string_append_printf (str, " with dedupe-flags=%" G_GUINT64_FORMAT, self->dedupe_flags);
if (self->refine_flags > 0) {
g_autofree gchar *tmp = gs_plugin_refine_flags_to_string (self->refine_flags);
g_string_append_printf (str, " with refine-flags=%s", tmp);
}
if (self->interactive)
g_string_append_printf (str, " with interactive=True");
if (self->timeout > 0)
g_string_append_printf (str, " with timeout=%u", self->timeout);
if (self->max_results > 0)
g_string_append_printf (str, " with max-results=%u", self->max_results);
if (self->age != 0) {
if (self->age == G_MAXUINT) {
g_string_append (str, " with cache age=any");
} else {
g_string_append_printf (str, " with cache age=%" G_GUINT64_FORMAT,
self->age);
}
}
if (self->search != NULL) {
g_string_append_printf (str, " with search=%s",
self->search);
}
if (self->category != NULL) {
GsCategory *parent = gs_category_get_parent (self->category);
if (parent != NULL) {
g_string_append_printf (str, " with category=%s/%s",
gs_category_get_id (parent),
gs_category_get_id (self->category));
} else {
g_string_append_printf (str, " with category=%s",
gs_category_get_id (self->category));
}
}
if (self->review != NULL) {
g_string_append_printf (str, " with review=%s",
as_review_get_id (self->review));
}
if (self->file != NULL) {
g_autofree gchar *path = g_file_get_path (self->file);
g_string_append_printf (str, " with file=%s", path);
}
if (self->list != NULL && gs_app_list_length (self->list) > 0) {
g_autofree const gchar **unique_ids = NULL;
g_autofree gchar *unique_ids_str = NULL;
unique_ids = g_new0 (const gchar *, gs_app_list_length (self->list) + 1);
for (guint i = 0; i < gs_app_list_length (self->list); i++) {
GsApp *app = gs_app_list_index (self->list, i);
unique_ids[i] = gs_app_get_unique_id (app);
}
unique_ids_str = g_strjoinv (",", (gchar**) unique_ids);
g_string_append_printf (str, " on apps %s", unique_ids_str);
}
if (time_now - self->time_created > 1000) {
g_string_append_printf (str, ", elapsed time since creation %" G_GINT64_FORMAT "ms",
(time_now - self->time_created) / 1000);
}
return g_string_free (str, FALSE);
}
void
gs_plugin_job_set_refine_flags (GsPluginJob *self, GsPluginRefineFlags refine_flags)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
self->refine_flags = refine_flags;
}
void
gs_plugin_job_set_filter_flags (GsPluginJob *self, GsPluginRefineFlags filter_flags)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
self->filter_flags = filter_flags;
}
void
gs_plugin_job_set_dedupe_flags (GsPluginJob *self, GsAppListFilterFlags dedupe_flags)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
self->dedupe_flags = dedupe_flags;
}
GsPluginRefineFlags
gs_plugin_job_get_refine_flags (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), 0);
return self->refine_flags;
}
GsPluginRefineFlags
gs_plugin_job_get_filter_flags (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), 0);
return self->filter_flags;
}
GsAppListFilterFlags
gs_plugin_job_get_dedupe_flags (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), 0);
return self->dedupe_flags;
}
gboolean
gs_plugin_job_has_refine_flags (GsPluginJob *self, GsPluginRefineFlags refine_flags)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), FALSE);
return (self->refine_flags & refine_flags) > 0;
}
void
gs_plugin_job_add_refine_flags (GsPluginJob *self, GsPluginRefineFlags refine_flags)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
self->refine_flags |= refine_flags;
}
void
gs_plugin_job_remove_refine_flags (GsPluginJob *self, GsPluginRefineFlags refine_flags)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
self->refine_flags &= ~refine_flags;
}
void
gs_plugin_job_set_interactive (GsPluginJob *self, gboolean interactive)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
self->interactive = interactive;
}
gboolean
gs_plugin_job_get_interactive (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), FALSE);
return self->interactive;
}
void
gs_plugin_job_set_max_results (GsPluginJob *self, guint max_results)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
self->max_results = max_results;
}
guint
gs_plugin_job_get_max_results (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), 0);
return self->max_results;
}
void
gs_plugin_job_set_timeout (GsPluginJob *self, guint timeout)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
self->timeout = timeout;
}
guint
gs_plugin_job_get_timeout (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), 0);
return self->timeout;
}
void
gs_plugin_job_set_age (GsPluginJob *self, guint64 age)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
self->age = age;
}
guint64
gs_plugin_job_get_age (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), 0);
return self->age;
}
void
gs_plugin_job_set_action (GsPluginJob *self, GsPluginAction action)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
self->action = action;
}
GsPluginAction
gs_plugin_job_get_action (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), 0);
return self->action;
}
void
gs_plugin_job_set_sort_func (GsPluginJob *self, GsAppListSortFunc sort_func)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
self->sort_func = sort_func;
}
GsAppListSortFunc
gs_plugin_job_get_sort_func (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), 0);
return self->sort_func;
}
void
gs_plugin_job_set_sort_func_data (GsPluginJob *self, gpointer sort_func_data)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
self->sort_func_data = sort_func_data;
}
gpointer
gs_plugin_job_get_sort_func_data (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), NULL);
return self->sort_func_data;
}
void
gs_plugin_job_set_search (GsPluginJob *self, const gchar *search)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
g_free (self->search);
self->search = g_strdup (search);
}
const gchar *
gs_plugin_job_get_search (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), NULL);
return self->search;
}
void
gs_plugin_job_set_app (GsPluginJob *self, GsApp *app)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
g_set_object (&self->app, app);
/* ensure we can always operate on a list object */
if (self->list != NULL && app != NULL && gs_app_list_length (self->list) == 0)
gs_app_list_add (self->list, self->app);
}
GsApp *
gs_plugin_job_get_app (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), NULL);
return self->app;
}
void
gs_plugin_job_set_list (GsPluginJob *self, GsAppList *list)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
if (list == NULL)
g_warning ("trying to set list to NULL, not a good idea");
g_set_object (&self->list, list);
}
GsAppList *
gs_plugin_job_get_list (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), NULL);
return self->list;
}
void
gs_plugin_job_set_file (GsPluginJob *self, GFile *file)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
g_set_object (&self->file, file);
}
GFile *
gs_plugin_job_get_file (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), NULL);
return self->file;
}
void
gs_plugin_job_set_plugin (GsPluginJob *self, GsPlugin *plugin)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
g_set_object (&self->plugin, plugin);
}
GsPlugin *
gs_plugin_job_get_plugin (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), NULL);
return self->plugin;
}
void
gs_plugin_job_set_category (GsPluginJob *self, GsCategory *category)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
g_set_object (&self->category, category);
}
GsCategory *
gs_plugin_job_get_category (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), NULL);
return self->category;
}
void
gs_plugin_job_set_review (GsPluginJob *self, AsReview *review)
{
g_return_if_fail (GS_IS_PLUGIN_JOB (self));
g_set_object (&self->review, review);
}
AsReview *
gs_plugin_job_get_review (GsPluginJob *self)
{
g_return_val_if_fail (GS_IS_PLUGIN_JOB (self), NULL);
return self->review;
}
static void
gs_plugin_job_get_property (GObject *obj, guint prop_id, GValue *value, GParamSpec *pspec)
{
GsPluginJob *self = GS_PLUGIN_JOB (obj);
switch (prop_id) {
case PROP_ACTION:
g_value_set_uint (value, self->action);
break;
case PROP_AGE:
g_value_set_uint64 (value, self->age);
break;
case PROP_REFINE_FLAGS:
g_value_set_uint64 (value, self->refine_flags);
break;
case PROP_FILTER_FLAGS:
g_value_set_uint64 (value, self->filter_flags);
break;
case PROP_DEDUPE_FLAGS:
g_value_set_uint64 (value, self->dedupe_flags);
break;
case PROP_INTERACTIVE:
g_value_set_boolean (value, self->interactive);
break;
case PROP_SEARCH:
g_value_set_string (value, self->search);
break;
case PROP_APP:
g_value_set_object (value, self->app);
break;
case PROP_LIST:
g_value_set_object (value, self->list);
break;
case PROP_FILE:
g_value_set_object (value, self->file);
break;
case PROP_CATEGORY:
g_value_set_object (value, self->category);
break;
case PROP_REVIEW:
g_value_set_object (value, self->review);
break;
case PROP_MAX_RESULTS:
g_value_set_uint (value, self->max_results);
break;
case PROP_TIMEOUT:
g_value_set_uint (value, self->timeout);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
break;
}
}
static void
gs_plugin_job_set_property (GObject *obj, guint prop_id, const GValue *value, GParamSpec *pspec)
{
GsPluginJob *self = GS_PLUGIN_JOB (obj);
switch (prop_id) {
case PROP_ACTION:
gs_plugin_job_set_action (self, g_value_get_uint (value));
break;
case PROP_AGE:
gs_plugin_job_set_age (self, g_value_get_uint64 (value));
break;
case PROP_REFINE_FLAGS:
gs_plugin_job_set_refine_flags (self, g_value_get_uint64 (value));
break;
case PROP_FILTER_FLAGS:
gs_plugin_job_set_filter_flags (self, g_value_get_uint64 (value));
break;
case PROP_DEDUPE_FLAGS:
gs_plugin_job_set_dedupe_flags (self, g_value_get_uint64 (value));
break;
case PROP_INTERACTIVE:
gs_plugin_job_set_interactive (self, g_value_get_boolean (value));
break;
case PROP_SEARCH:
gs_plugin_job_set_search (self, g_value_get_string (value));
break;
case PROP_APP:
gs_plugin_job_set_app (self, g_value_get_object (value));
break;
case PROP_LIST:
gs_plugin_job_set_list (self, g_value_get_object (value));
break;
case PROP_FILE:
gs_plugin_job_set_file (self, g_value_get_object (value));
break;
case PROP_CATEGORY:
gs_plugin_job_set_category (self, g_value_get_object (value));
break;
case PROP_REVIEW:
gs_plugin_job_set_review (self, g_value_get_object (value));
break;
case PROP_MAX_RESULTS:
gs_plugin_job_set_max_results (self, g_value_get_uint (value));
break;
case PROP_TIMEOUT:
gs_plugin_job_set_timeout (self, g_value_get_uint (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
break;
}
}
static void
gs_plugin_job_finalize (GObject *obj)
{
GsPluginJob *self = GS_PLUGIN_JOB (obj);
g_free (self->search);
g_clear_object (&self->app);
g_clear_object (&self->list);
g_clear_object (&self->file);
g_clear_object (&self->plugin);
g_clear_object (&self->category);
g_clear_object (&self->review);
G_OBJECT_CLASS (gs_plugin_job_parent_class)->finalize (obj);
}
static void
gs_plugin_job_class_init (GsPluginJobClass *klass)
{
GParamSpec *pspec;
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gs_plugin_job_finalize;
object_class->get_property = gs_plugin_job_get_property;
object_class->set_property = gs_plugin_job_set_property;
pspec = g_param_spec_uint ("action", NULL, NULL,
GS_PLUGIN_ACTION_UNKNOWN,
GS_PLUGIN_ACTION_LAST,
GS_PLUGIN_ACTION_UNKNOWN,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_ACTION, pspec);
pspec = g_param_spec_uint64 ("age", NULL, NULL,
0, G_MAXUINT64, 0,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_AGE, pspec);
pspec = g_param_spec_uint64 ("refine-flags", NULL, NULL,
0, G_MAXUINT64, 0,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_REFINE_FLAGS, pspec);
pspec = g_param_spec_uint64 ("filter-flags", NULL, NULL,
0, G_MAXUINT64, 0,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_FILTER_FLAGS, pspec);
pspec = g_param_spec_uint64 ("dedupe-flags", NULL, NULL,
0, G_MAXUINT64, 0,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_DEDUPE_FLAGS, pspec);
pspec = g_param_spec_boolean ("interactive", NULL, NULL,
FALSE,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_INTERACTIVE, pspec);
pspec = g_param_spec_string ("search", NULL, NULL,
NULL,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_SEARCH, pspec);
pspec = g_param_spec_object ("app", NULL, NULL,
GS_TYPE_APP,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_APP, pspec);
pspec = g_param_spec_object ("list", NULL, NULL,
GS_TYPE_APP_LIST,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_LIST, pspec);
pspec = g_param_spec_object ("file", NULL, NULL,
G_TYPE_FILE,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_FILE, pspec);
pspec = g_param_spec_object ("category", NULL, NULL,
GS_TYPE_CATEGORY,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_CATEGORY, pspec);
pspec = g_param_spec_object ("review", NULL, NULL,
AS_TYPE_REVIEW,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_REVIEW, pspec);
pspec = g_param_spec_uint ("max-results", NULL, NULL,
0, G_MAXUINT, 0,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_MAX_RESULTS, pspec);
pspec = g_param_spec_uint ("timeout", NULL, NULL,
0, G_MAXUINT, 60,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
g_object_class_install_property (object_class, PROP_TIMEOUT, pspec);
}
static void
gs_plugin_job_init (GsPluginJob *self)
{
self->refine_flags = GS_PLUGIN_REFINE_FLAGS_DEFAULT;
self->filter_flags = GS_PLUGIN_REFINE_FLAGS_DEFAULT;
self->dedupe_flags = GS_APP_LIST_FILTER_FLAG_KEY_ID |
GS_APP_LIST_FILTER_FLAG_KEY_SOURCE |
GS_APP_LIST_FILTER_FLAG_KEY_VERSION;
self->list = gs_app_list_new ();
self->time_created = g_get_monotonic_time ();
}
|