File: pkgc-manage.c

package info (click to toggle)
packagekit 1.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,824 kB
  • sloc: ansic: 56,491; cpp: 15,652; xml: 5,532; python: 4,932; sh: 316; perl: 60; makefile: 56
file content (1068 lines) | stat: -rw-r--r-- 31,317 bytes parent folder | download
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2012-2025 Matthias Klumpp <matthias@tenstral.net>
 * Copyright (C) 2007-2014 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the license, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY 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/>.
 */

#include "config.h"
#include "pkgc-manage.h"

#include <glib.h>

#include "pkgc-util.h"

/* Options for various management operations */
static gboolean opt_download_only = FALSE;
static gboolean opt_allow_downgrade = FALSE;
static gboolean opt_allow_reinstall = FALSE;
static gboolean opt_allow_untrusted = FALSE;
static gboolean opt_no_autoremove = FALSE;
static gint opt_cache_age = -1;

static const GOptionEntry option_download_only[] = {
	{ "download-only", 'd', 0, G_OPTION_ARG_NONE, &opt_download_only,
		/* TRANSLATORS: command line argument, do we just download or apply changes */
		N_("Prepare the transaction by downloading packages only"), NULL },
	{ NULL, 0, 0, 0, NULL, NULL, NULL }
};

static const GOptionEntry option_allow_downgrade[] = {
	{ "allow-downgrade", 0, 0, G_OPTION_ARG_NONE, &opt_allow_downgrade,
		/* TRANSLATORS: command line argument, do we allow package downgrades */
		N_("Allow package downgrades"), NULL },
	{ NULL, 0, 0, 0, NULL, NULL, NULL }
};

static const GOptionEntry option_allow_reinstall[] = {
	{ "allow-reinstall", 0, 0, G_OPTION_ARG_NONE, &opt_allow_reinstall,
		/* TRANSLATORS: command line argument, do we allow package re-installations */
		N_("Allow package re-installations"), NULL },
	{ NULL, 0, 0, 0, NULL, NULL, NULL }
};

static const GOptionEntry option_allow_untrusted[] = {
	{ "allow-untrusted", 0, 0, G_OPTION_ARG_NONE, &opt_allow_untrusted,
		/* TRANSLATORS: command line argument */
		N_("Allow installation of untrusted packages"), NULL },
	{ NULL, 0, 0, 0, NULL, NULL, NULL }
};

static const GOptionEntry option_no_autoremove[] = {
	{ "no-autoremove", 0, 0, G_OPTION_ARG_NONE, &opt_no_autoremove,
		/* TRANSLATORS: command line argument */
		N_("Do not automatically remove unused dependencies"), NULL },
	{ NULL, 0, 0, 0, NULL, NULL, NULL }
};

static const GOptionEntry option_cache_age[] = {
	{ "cache-age", 'c', 0, G_OPTION_ARG_INT, &opt_cache_age,
		/* TRANSLATORS: command line argument */
		N_("Maximum metadata cache age in seconds (default: 3 days)"), N_("SECONDS") },
	{ NULL, 0, 0, 0, NULL, NULL, NULL }
};

/**
 * pkgc_manage_reset_options:
 *
 * Reset all option flags to their default values.
 */
static void
pkgc_manage_reset_options (void)
{
	opt_download_only = FALSE;
	opt_allow_downgrade = FALSE;
	opt_allow_reinstall = FALSE;
	opt_allow_untrusted = FALSE;
	opt_no_autoremove = FALSE;
	opt_cache_age = PKGC_DEFAULT_CACHE_AGE_SEC;
}

/**
 * pkgc_manage_apply_options:
 * @ctx: a valid #PkgctlContext
 *
 * Apply the parsed option flags to the context.
 */
static void
pkgc_manage_apply_options (PkgcliContext *ctx)
{
	ctx->only_download = opt_download_only;
	ctx->allow_downgrade = opt_allow_downgrade;
	ctx->allow_reinstall = opt_allow_reinstall;
	ctx->allow_untrusted = opt_allow_untrusted;
	if (opt_cache_age == 0)
		ctx->cache_age = 1; /* shortest possible cache-age, 0 is not allowed */
	else
		ctx->cache_age = (opt_cache_age < 0)? G_MAXUINT : (guint)opt_cache_age;

	pkgc_context_apply_settings (ctx);
}

/**
 * pkgc_manage_on_task_finished_cb:
 */
static void
pkgc_manage_on_task_finished_cb (GObject *source_object, GAsyncResult *res, gpointer user_data)
{
	PkgcliContext *ctx = user_data;
	g_autoptr(GError) error = NULL;
	g_autoptr(PkResults) results = NULL;
	g_autoptr(PkError) pk_error = NULL;

	ctx->exit_code = PKGC_EXIT_SUCCESS;
	results = pk_task_generic_finish (PK_TASK (source_object), res, &error);

	if (ctx->progressbar != NULL && ctx->is_tty)
		pk_progress_bar_end (ctx->progressbar);

	if (error) {
		pkgc_print_error (ctx, "%s", error->message);
		ctx->exit_code = PKGC_EXIT_TRANSACTION_FAILED;

		goto out;
	}

	if (results == NULL)
		return;

	/* check exit code from results */
	pk_error = pk_results_get_error_code (results);
	if (pk_error) {
		pkgc_print_error (ctx, "%s", pk_error_get_details (pk_error));
		ctx->exit_code = PKGC_EXIT_TRANSACTION_FAILED;
	} else {
		g_autoptr(GPtrArray) pkgs = NULL;
		g_autoptr(GPtrArray) tas = NULL;
		g_autoptr(GPtrArray) repos = NULL;

		/* show packages that were processed */
		pkgs = pk_results_get_package_array (results);
		for (guint i = 0; i < pkgs->len; i++) {
			PkPackage *package = PK_PACKAGE (g_ptr_array_index (pkgs, i));
			pkgc_print_package (ctx, package);
		}

		/* show transactions */
		tas = pk_results_get_transaction_array (results);
		for (guint i = 0; i < tas->len; i++) {
			PkTransactionPast *transaction = PK_TRANSACTION_PAST (
			    g_ptr_array_index (tas, i));
			pkgc_print_transaction (ctx, transaction);
		}

		/* repo_detail */
		repos = pk_results_get_repo_detail_array (results);
		for (guint i = 0; i < repos->len; i++) {
			PkRepoDetail *repo = PK_REPO_DETAIL (g_ptr_array_index (repos, i));
			pkgc_print_repo (ctx, repo);
		}
	}

out:
	g_main_loop_quit (ctx->loop);
}

/**
 * pkgc_refresh:
 *
 * Refresh package metadata cache.
 */
static gint
pkgc_refresh (PkgcliContext *ctx, PkgcliCommand *cmd, gint argc, gchar **argv)
{
	gboolean force = FALSE;
	g_autoptr(GOptionContext) option_context = NULL;

	pkgc_manage_reset_options ();

	/* parse options */
	option_context = pkgc_option_context_for_command (
		ctx, cmd,
		"[force]",
		/* TRANSLATORS: Description for pkgcli refresh */
		_("Refresh the package metadata cache."));
	g_option_context_add_main_entries (option_context, option_cache_age, NULL);

	if (!pkgc_parse_command_options (ctx, cmd, option_context, &argc, &argv, 1))
		return PKGC_EXIT_SYNTAX_ERROR;

	/* Check for force flag */
	if (argc >= 2 && strcmp (argv[1], "force") == 0) {
		force = TRUE;
	}

	/* Apply options to context */
	pkgc_manage_apply_options (ctx);

	/* Refresh cache */
	pk_task_refresh_cache_async (PK_TASK (ctx->task),
				     force,
				     ctx->cancellable,
				     pkgc_context_on_progress_cb,
				     ctx,
				     pkgc_manage_on_task_finished_cb,
				     ctx);

	g_main_loop_run (ctx->loop);

	if (ctx->exit_code == PKGC_EXIT_SUCCESS)
		pkgc_print_success (ctx, _("Package metadata refreshed"));

	return ctx->exit_code;
}

/**
 * pkgc_install:
 *
 * Install packages or local package files.
 */
static gint
pkgc_install (PkgcliContext *ctx, PkgcliCommand *cmd, gint argc, gchar **argv)
{
	gboolean has_files;
	g_autoptr(GError) error = NULL;
	g_autoptr(GOptionContext) option_context = NULL;

	pkgc_manage_reset_options ();

	/* parse options */
	option_context = pkgc_option_context_for_command (
		ctx, cmd,
		"PACKAGE...",
		/* TRANSLATORS: Description for pkgcli install */
		_("Install one or more packages or local package files."));

	g_option_context_add_main_entries (option_context, option_download_only, NULL);
	g_option_context_add_main_entries (option_context, option_allow_downgrade, NULL);
	g_option_context_add_main_entries (option_context, option_allow_reinstall, NULL);
	g_option_context_add_main_entries (option_context, option_allow_untrusted, NULL);
	g_option_context_add_main_entries (option_context, option_no_autoremove, NULL);

	if (!pkgc_parse_command_options (ctx, cmd, option_context, &argc, &argv, 2))
		return PKGC_EXIT_SYNTAX_ERROR;

	/* apply options to context */
	pkgc_manage_apply_options (ctx);

	/* check if we have files or package names */
	has_files = FALSE;
	for (gint i = 1; i < argc; i++) {
		if (pkgc_is_local_package (argv[i])) {
			has_files = TRUE;
			break;
		}
	}

	if (has_files) {
		/* install local files */
		pk_task_install_files_async (PK_TASK (ctx->task),
					     argv + 1,
					     ctx->cancellable,
					     pkgc_context_on_progress_cb,
					     ctx,
					     pkgc_manage_on_task_finished_cb,
					     ctx);
	} else {
		/* install packages by name */
		g_auto(GStrv) package_ids = NULL;

		/* assume arch filter unless specified otherwise */
		if (!pk_bitfield_contain (ctx->filters, PK_FILTER_ENUM_ARCH) &&
			!pk_bitfield_contain (ctx->filters, PK_FILTER_ENUM_NOT_ARCH))
			pk_bitfield_add (ctx->filters, PK_FILTER_ENUM_ARCH);

		/* assume non-source packages unless specified */
		if (!pk_bitfield_contain (ctx->filters, PK_FILTER_ENUM_SOURCE) &&
			!pk_bitfield_contain (ctx->filters, PK_FILTER_ENUM_NOT_SOURCE))
			pk_bitfield_add (ctx->filters, PK_FILTER_ENUM_NOT_SOURCE);

		pk_bitfield_add (ctx->filters, PK_FILTER_ENUM_NEWEST);
		if (!opt_allow_reinstall)
			pk_bitfield_add (ctx->filters, PK_FILTER_ENUM_NOT_INSTALLED);

		/* resolve package names to IDs */
		package_ids = pkgc_resolve_packages (ctx, ctx->filters, argv + 1, &error);
		if (package_ids == NULL) {
			/* the the error was not "no package found", or we did allow reinstallations
			 * (and therefore didn't filter out already installed packages) we show the
			 * emitted error immediately. */
			if (g_error_matches (error, PKGC_ERROR, PK_ERROR_ENUM_PACKAGE_NOT_FOUND) ||
				 opt_allow_reinstall) {
				pkgc_print_error (ctx,
							/* TRANSLATORS: There was an error finding a package
							 * for installation. The detailed error follows. */
							_("Could not find any available package: %s"), error->message);
				return PKGC_EXIT_NOT_FOUND;
			}

			/* If we are here, the package may exist, but may already be installed.
			 * Check for that to provide a better error */
			pk_bitfield_remove (ctx->filters, PK_FILTER_ENUM_NOT_INSTALLED);
			pk_bitfield_add (ctx->filters, PK_FILTER_ENUM_INSTALLED);
			package_ids = pkgc_resolve_packages (ctx, ctx->filters, argv + 1, NULL);
			if (package_ids == NULL) {
				/* the package does not exist at all */
				pkgc_print_error (ctx,
							/* TRANSLATORS: We were unable to find a package for installation. */
							_("Could not find any available package: %s"), error->message);
				return PKGC_EXIT_NOT_FOUND;
			} else {
				/* the package exists, but is already installed */
				pkgc_print_info (ctx,
							_("The selected package is already installed."));
				return PKGC_EXIT_SUCCESS;
			}

			return PKGC_EXIT_FAILURE;
		}

		/* install packages */
		pk_task_install_packages_async (PK_TASK (ctx->task),
						package_ids,
						ctx->cancellable,
						pkgc_context_on_progress_cb,
						ctx,
						pkgc_manage_on_task_finished_cb,
						ctx);
	}

	g_main_loop_run (ctx->loop);
	return ctx->exit_code;
}

/**
 * pkgc_remove:
 *
 * Remove packages from the system.
 */
static gint
pkgc_remove (PkgcliContext *ctx, PkgcliCommand *cmd, gint argc, gchar **argv)
{
	g_autoptr(GOptionContext) option_context = NULL;
	g_auto(GStrv) package_ids = NULL;
	g_autoptr(GError) error = NULL;

	pkgc_manage_reset_options ();

	/* parse options */
	option_context = pkgc_option_context_for_command (
		ctx, cmd,
		"PACKAGE...",
		/* TRANSLATORS: Description for pkgcli remove */
		_("Remove one or more packages from the system."));
	g_option_context_add_main_entries (option_context, option_no_autoremove, NULL);

	if (!pkgc_parse_command_options (ctx, cmd, option_context, &argc, &argv, 2))
		return PKGC_EXIT_SYNTAX_ERROR;

	/* apply options to context */
	pkgc_manage_apply_options (ctx);

	/* only look at installed packages */
	pk_bitfield_add (ctx->filters, PK_FILTER_ENUM_INSTALLED);

	/* resolve package names to IDs */
	package_ids = pkgc_resolve_packages (ctx, ctx->filters, argv + 1, &error);
	if (package_ids == NULL) {
		if (error) {
			pkgc_print_error (ctx,
					  _("Could not find installed packages: %s"), error->message);
		}
		return PKGC_EXIT_FAILURE;
	}

	/* remove packages */
	pk_task_remove_packages_async (PK_TASK (ctx->task),
				       package_ids,
				       TRUE, /* allow deps */
				       !opt_no_autoremove, /* autoremove */
				       ctx->cancellable,
				       pkgc_context_on_progress_cb,
				       ctx,
				       pkgc_manage_on_task_finished_cb,
				       ctx);

	g_main_loop_run (ctx->loop);
	return ctx->exit_code;
}

/**
 * pkgc_download:
 *
 * Download packages to the specified directory without installing.
 */
static gint
pkgc_download (PkgcliContext *ctx, PkgcliCommand *cmd, gint argc, gchar **argv)
{
	g_autoptr(GOptionContext) option_context = NULL;
	const char *directory = NULL;
	g_auto(GStrv) package_ids = NULL;
	g_autoptr(GError) error = NULL;

	pkgc_manage_reset_options ();

	/* parse options */
	option_context = pkgc_option_context_for_command (
		ctx, cmd,
		"DIRECTORY PACKAGE...",
		/* TRANSLATORS: Description for pkgcli download */
		_("Download packages to the specified directory without installing."));
	if (!pkgc_parse_command_options (ctx, cmd, option_context, &argc, &argv, 3))
		return PKGC_EXIT_SYNTAX_ERROR;

	/* Apply options to context */
	pkgc_manage_apply_options (ctx);

	directory = argv[1];
	package_ids = pkgc_resolve_packages (ctx, ctx->filters, argv + 2, &error);
	if (package_ids == NULL) {
		if (error) {
			/* TRANSLATORS: There was an error getting the
			 * details about the package. The detailed error follows */
			pkgc_print_error (ctx,
					  _("Could not find packages: %s"), error->message);
		}

		return PKGC_EXIT_FAILURE;
	}

	/* Check if directory exists */
	if (!g_file_test (directory, G_FILE_TEST_IS_DIR)) {
		pkgc_print_error (ctx, _("Directory does not exist: %s"), directory);
		return PKGC_EXIT_SYNTAX_ERROR;
	}

	/* Download packages */
	pk_task_download_packages_async (PK_TASK (ctx->task),
					 package_ids,
					 directory,
					 ctx->cancellable,
					 pkgc_context_on_progress_cb,
					 ctx,
					 pkgc_manage_on_task_finished_cb,
					 ctx);

	g_main_loop_run (ctx->loop);
	return ctx->exit_code;
}

static gboolean
pkgc_update_system_filter_helper (PkPackage *package, gpointer user_data)
{
	PkInfoEnum package_enum = pk_package_get_info (package);
	return package_enum != PK_INFO_ENUM_BLOCKED;
}

/**
 * pkgc_update:
 *
 * Update all packages or specific packages to their latest versions.
 */
static gint
pkgc_update (PkgcliContext *ctx, PkgcliCommand *cmd, gint argc, gchar **argv)
{
	g_autoptr(GOptionContext) option_context = NULL;
	g_autoptr(GError) error = NULL;

	pkgc_manage_reset_options ();

	/* parse options */
	option_context = pkgc_option_context_for_command (
		ctx, cmd,
		"[PACKAGE...]",
		/* TRANSLATORS: Description for pkgcli update */
		_("Update all packages or specific packages to their latest versions."));
	g_option_context_add_main_entries (option_context, option_download_only, NULL);
	g_option_context_add_main_entries (option_context, option_allow_downgrade, NULL);

	if (!pkgc_parse_command_options (ctx, cmd, option_context, &argc, &argv, 1))
		return PKGC_EXIT_SYNTAX_ERROR;

	/* apply options to context */
	pkgc_manage_apply_options (ctx);

	if (argc >= 2) {
		/* update specific packages */
		g_auto(GStrv) package_ids = NULL;

		pk_bitfield_add (ctx->filters, PK_FILTER_ENUM_NOT_INSTALLED);
		pk_bitfield_add (ctx->filters, PK_FILTER_ENUM_NOT_SOURCE);
		pk_bitfield_add (ctx->filters, PK_FILTER_ENUM_NEWEST);

		package_ids = pkgc_resolve_packages (ctx, ctx->filters, argv + 1, &error);
		if (package_ids == NULL) {
			if (error)
				pkgc_print_error (ctx,
						  _("Could not find packages to update: %s"), error->message);
			return PKGC_EXIT_FAILURE;
		}

		pk_task_update_packages_async (PK_TASK (ctx->task),
					       package_ids,
					       ctx->cancellable,
					       pkgc_context_on_progress_cb,
					       ctx,
					       pkgc_manage_on_task_finished_cb,
					       ctx);
	} else {
		/* update all packages - get updates first */
		g_autoptr(PkResults) results = NULL;
		g_autoptr(PkPackageSack) sack = NULL;
		g_auto(GStrv) package_ids = NULL;

		/* clear any previous error */
		g_clear_error (&error);

		/* get current updates */
		pk_bitfield_add (ctx->filters, PK_FILTER_ENUM_NEWEST);
		results = pk_task_get_updates_sync (PK_TASK (ctx->task),
						    ctx->filters,
						    ctx->cancellable,
						    pkgc_context_on_progress_cb,
						    ctx,
						    &error);
		if (results == NULL) {
			pkgc_print_error (ctx, _("Failed to get updates: %s"), error->message);
			ctx->exit_code = PKGC_EXIT_FAILURE;
			return ctx->exit_code;
		}

		/* drop blocked packages from the update set */
		sack = pk_results_get_package_sack (results);
		pk_package_sack_remove_by_filter (sack, &pkgc_update_system_filter_helper, NULL);

		package_ids = pk_package_sack_get_ids (sack);

		if (package_ids == NULL || g_strv_length (package_ids) == 0) {
			pkgc_print_info (ctx, _("No packages require updating"));
			return PKGC_EXIT_SUCCESS;
		}

		/* now update them */
		pk_task_update_packages_async (PK_TASK (ctx->task),
					       package_ids,
					       ctx->cancellable,
					       pkgc_context_on_progress_cb,
					       ctx,
					       pkgc_manage_on_task_finished_cb,
					       ctx);
	}

	g_main_loop_run (ctx->loop);
	return ctx->exit_code;
}

/**
 * pkgc_upgrade:
 *
 * Upgrade the system to a new distribution version or do an advanced update.
 */
static gint
pkgc_upgrade (PkgcliContext *ctx, PkgcliCommand *cmd, gint argc, gchar **argv)
{
	g_autoptr(GOptionContext) option_context = NULL;
	const char *distro_name = NULL;
	PkUpgradeKindEnum upgrade_kind = PK_UPGRADE_KIND_ENUM_DEFAULT;
	g_autoptr(GError) error = NULL;

	pkgc_manage_reset_options ();

	/* parse options */
	option_context = pkgc_option_context_for_command (
		ctx, cmd,
		"[DISTRO] [TYPE]",
		/* TRANSLATORS: Description of pkgcli upgrade.
		 * No not translate "minimal, default, complete", those are parameters */
		_("Upgrade all packages or perform a distribution upgrade.\n\n"
		  "Types: minimal, default, complete"));

	if (!pkgc_parse_command_options (ctx, cmd, option_context, &argc, &argv, 1))
		return PKGC_EXIT_SYNTAX_ERROR;

	/* Apply options to context */
	pkgc_manage_apply_options (ctx);

	/* Parse optional distro name and upgrade type */
	if (argc >= 2)
		distro_name = argv[1];


	if (argc >= 3) {
		if (g_strcmp0 (argv[2], "minimal") == 0)
			upgrade_kind = PK_UPGRADE_KIND_ENUM_MINIMAL;
		else if (g_strcmp0 (argv[2], "complete") == 0)
			upgrade_kind = PK_UPGRADE_KIND_ENUM_COMPLETE;
		else if (g_strcmp0 (argv[2], "default") == 0)
			upgrade_kind = PK_UPGRADE_KIND_ENUM_DEFAULT;
	}

	/* Upgrade system */
	if (distro_name) {
		pk_task_upgrade_system_async (PK_TASK (ctx->task),
					      distro_name,
					      upgrade_kind,
					      ctx->cancellable,
					      pkgc_context_on_progress_cb,
					      ctx,
					      pkgc_manage_on_task_finished_cb,
					      ctx);
	} else {
		/* Just update all packages */
		g_autoptr(PkResults) results = NULL;
		g_autoptr(PkPackageSack) sack = NULL;
		g_auto(GStrv) package_ids = NULL;

		/* Clear any previous error */
		g_clear_error (&error);

		/* Get current updates */
		results = pk_task_get_updates_sync (PK_TASK (ctx->task),
						    ctx->filters,
						    ctx->cancellable,
						    pkgc_context_on_progress_cb,
						    ctx,
						    &error);
		if (results == NULL) {
			pkgc_print_error (ctx, _("Failed to get updates: %s"), error->message);
			ctx->exit_code = PKGC_EXIT_FAILURE;
			return ctx->exit_code;
		}

		sack = pk_results_get_package_sack (results);
		package_ids = pk_package_sack_get_ids (sack);

		if (package_ids == NULL || g_strv_length (package_ids) == 0) {
			pkgc_print_info (ctx, _("No packages require updating"));
			return PKGC_EXIT_SUCCESS;
		}

		/* Now update them */
		pk_task_update_packages_async (PK_TASK (ctx->task),
					       package_ids,
					       ctx->cancellable,
					       pkgc_context_on_progress_cb,
					       ctx,
					       pkgc_manage_on_task_finished_cb,
					       ctx);
	}

	g_main_loop_run (ctx->loop);
	return ctx->exit_code;
}

/**
 * pkgc_print_offline_update_status:
 *
 * Helper for pkgc_offline_update().
 */
static gint
pkgc_print_offline_update_status (PkgcliContext *ctx)
{
	g_autoptr(GError) error = NULL;
	g_autoptr(PkResults) results = NULL;
	g_autoptr(PkError) pk_error = NULL;
	g_auto(GStrv) package_ids = NULL;
	g_autoptr(GPtrArray) packages = NULL;
	PkOfflineAction action;

	/* Check if offline update is armed */
	action = pk_offline_get_action (&error);
	if (action == PK_OFFLINE_ACTION_UNKNOWN) {
		pkgc_print_error (ctx, _("Failed to read offline update action: %s"), error->message);
		return PKGC_EXIT_FAILURE;
	}

	if (action == PK_OFFLINE_ACTION_UNSET) {
		if (ctx->output_mode != PKGCLI_MODE_JSON)
			g_print ("%s%s%s",
				 pkgc_get_ansi_color (ctx, PKGC_COLOR_BLUE),
				 "⏾ ",
				 pkgc_get_ansi_color (ctx, PKGC_COLOR_RESET));
		pkgc_print_info (ctx, _("Offline update is not triggered."));
		g_print ("\n");
	} else {
		if (ctx->output_mode != PKGCLI_MODE_JSON)
			g_print ("%s%s%s",
				 pkgc_get_ansi_color (ctx, PKGC_COLOR_YELLOW),
				 "⚠ ",
				 pkgc_get_ansi_color (ctx, PKGC_COLOR_RESET));
		pkgc_print_info (ctx, _("Offline update is triggered. Action after update: %s"),
			pk_offline_action_to_string(action));
		g_print ("\n");
	}

	g_clear_error (&error);

	/* Check if there are prepared updates */
	package_ids = pk_offline_get_prepared_ids (&error);
	if (package_ids != NULL) {
		/* TRANSLATORS: Packages that were prepared for an offline update */
		pkgc_print_info (ctx, _("Prepared packages:"));
		for (guint i = 0; package_ids[i] != NULL; i++) {
			g_autofree gchar *printable = pk_package_id_to_printable (package_ids[i]);
			if (ctx->output_mode == PKGCLI_MODE_JSON) {
				json_t *root = json_object ();
				json_object_set_new (root, "pkid", json_string (package_ids[i]));
				pkgc_print_json_decref (root);
			} else {
				g_print ("  %s\n", printable);
			}
		}
		g_print ("\n");
	} else if (error != NULL) {
		if (error->code == PK_OFFLINE_ERROR_NO_DATA) {
			pkgc_print_info (ctx, _("No offline update is prepared."));
		} else {
			pkgc_print_error (ctx, _("Failed to read prepared offline updates: %s"), error->message);
			return PKGC_EXIT_FAILURE;
		}
	}

	/* Check if there are results from last update */
	g_clear_error (&error);
	results = pk_offline_get_results (&error);

	if (!results) {
		pkgc_print_info (ctx, _("No results from last offline update available."));
		return PKGC_EXIT_SUCCESS;
	}

	pk_error = pk_results_get_error_code (results);
	if (pk_error) {
		pkgc_print_error (ctx,
				  _("Last offline update failed: %s: %s"),
					pk_error_enum_to_string (pk_error_get_code (pk_error)),
					pk_error_get_details (pk_error));
		return PKGC_EXIT_TRANSACTION_FAILED;
	}

	packages = pk_results_get_package_array (results);
	pkgc_print_success (ctx, _("Last offline update completed successfully"));
	for (guint i = 0; i < packages->len; i++) {
		PkPackage *pkg = PK_PACKAGE (g_ptr_array_index (packages, i));
		g_autofree gchar *printable = pk_package_id_to_printable (
			pk_package_get_id (pkg));
		if (ctx->output_mode == PKGCLI_MODE_JSON) {
			json_t *root = json_object ();
			json_object_set_new (root, "pkid", json_string (pk_package_get_id (pkg)));
			pkgc_print_json_decref (root);
		} else {
			g_print ("  ");
			g_print (_("Updated: %s"), printable);
			g_print ("\n");
		}
	}

	return PKGC_EXIT_SUCCESS;
}

/**
 * pkgc_trigger_offline_update:
 *
 * Helper for pkgc_offline_update().
 */
static gint
pkgc_trigger_offline_update (PkgcliContext *ctx)
{
	gboolean ret;
	g_autoptr(GError) error = NULL;

	ret = pk_offline_trigger_with_flags (PK_OFFLINE_ACTION_REBOOT, PK_OFFLINE_FLAGS_INTERACTIVE, NULL, &error);
	if (!ret) {
		pkgc_print_error (ctx, _("Failed to trigger offline update: %s"), error->message);
		return PKGC_EXIT_FAILURE;
	}

	pkgc_print_success (ctx, _("Offline update scheduled. System will update on next reboot."));
	return PKGC_EXIT_SUCCESS;
}

/**
 * pkgc_offline_update:
 *
 * Manage offline updates (for update installation on next (soft)reboot).
 */
static gint
pkgc_offline_update (PkgcliContext *ctx, PkgcliCommand *cmd, gint argc, gchar **argv)
{
	g_autoptr(GOptionContext) option_context = NULL;
	const gchar *cmd_description = NULL;
	const gchar *request = NULL;
	gboolean ret;
	g_autoptr(GError) error = NULL;

	/* TRANSLATORS: Description of the pkgcli offline-update command.
	 * The request values (trigger, prepare, etc.) are parameters and MUST NOT be translated. */
	cmd_description = _("Trigger & manage offline system updates.\n\n"
						"You can select one of these requests:\n"
						"  prepare - prepare an offline update and trigger it (default)\n"
						"  trigger - trigger a (manually prepared) offline update\n"
						"  cancel  - cancel a planned offline update\n"
						"  status  - show status information about a prepared or finished offline update");

	/* parse options */
	option_context = pkgc_option_context_for_command (
		ctx, cmd,
		"[REQUEST]",
		cmd_description);
	if (!pkgc_parse_command_options (ctx, cmd, option_context, &argc, &argv, 1))
		return PKGC_EXIT_SYNTAX_ERROR;

	request = (argc >= 2)? argv[1] : "prepare";

	if (g_strcmp0 (request, "trigger") == 0) {
		return pkgc_trigger_offline_update (ctx);
	}

	if (g_strcmp0 (request, "cancel") == 0) {
		ret = pk_offline_cancel_with_flags (PK_OFFLINE_FLAGS_INTERACTIVE, NULL, &error);
		if (!ret) {
			pkgc_print_error (ctx, _("Failed to cancel offline update: %s"), error->message);
			return PKGC_EXIT_FAILURE;
		}

		pkgc_print_success (ctx, _("Offline update cancelled"));
		return PKGC_EXIT_SUCCESS;
	}

	if (g_strcmp0 (request, "status") == 0) {
		return pkgc_print_offline_update_status (ctx);
	}

	if (g_strcmp0 (request, "prepare") == 0) {
		/* Update all packages - get updates first */
		g_autoptr(PkResults) results = NULL;
		g_autoptr(PkPackageSack) sack = NULL;
		g_auto(GStrv) package_ids = NULL;

		/* set parameters to prepare offline updates */
		ctx->only_download = TRUE;
		ctx->allow_downgrade = FALSE;
		ctx->allow_untrusted = FALSE;
		pkgc_context_apply_settings (ctx);

		/* download all updates */
		results = pk_task_get_updates_sync (PK_TASK (ctx->task),
							ctx->filters,
							ctx->cancellable,
							pkgc_context_on_progress_cb,
							ctx,
							&error);
		if (results == NULL) {
			pkgc_print_error (ctx, _("Failed to get updates: %s"), error->message);
			ctx->exit_code = PKGC_EXIT_FAILURE;
			return ctx->exit_code;
		}

		sack = pk_results_get_package_sack (results);
		package_ids = pk_package_sack_get_ids (sack);
		if (package_ids == NULL || g_strv_length (package_ids) == 0) {
			pkgc_print_info (ctx, _("No packages require updating"));
			return PKGC_EXIT_SUCCESS;
		}

		/* download packages */
		pk_task_update_packages_async (PK_TASK (ctx->task),
						   package_ids,
						   ctx->cancellable,
						   pkgc_context_on_progress_cb,
						   ctx,
						   pkgc_manage_on_task_finished_cb,
						   ctx);
		g_main_loop_run (ctx->loop);

		/* don't trigger offline update if download failed */
		if (ctx->exit_code != PKGC_EXIT_SUCCESS)
			return ctx->exit_code;

		return pkgc_trigger_offline_update (ctx);
	}

	pkgc_print_error (ctx, _("Unknown offline-update request: %s"), request);
	return PKGC_EXIT_SYNTAX_ERROR;
}

/**
 * pkgc_install_sig:
 *
 * Install a package signature (for GPG verification).
 */
static gint
pkgc_install_sig (PkgcliContext *ctx, PkgcliCommand *cmd, gint argc, gchar **argv)
{
	g_autoptr(GError) error = NULL;
	g_autoptr(GOptionContext) option_context = NULL;

	/* parse options */
	option_context = pkgc_option_context_for_command (
		ctx, cmd,
		"TYPE KEY_ID PACKAGE_ID",
		/* TRANSLATORS: Description for pkgcli install-sig */
		_("Install a package signature for GPG verification."));

	if (!pkgc_parse_command_options (ctx, cmd, option_context, &argc, &argv, 4))
		return PKGC_EXIT_SYNTAX_ERROR;

	/* Install signature */
	pk_client_install_signature_async (PK_CLIENT (ctx->task),
					   PK_SIGTYPE_ENUM_GPG,
					   argv[2], /* key_id */
					   argv[3], /* package_id */
					   ctx->cancellable,
					   pkgc_context_on_progress_cb,
					   ctx,
					   pkgc_manage_on_task_finished_cb,
					   ctx);

	g_main_loop_run (ctx->loop);
	return ctx->exit_code;
}

/**
 * pkgc_repair:
 *
 * Repair broken package management.
 */
static gint
pkgc_repair (PkgcliContext *ctx, PkgcliCommand *cmd, gint argc, gchar **argv)
{
	g_autoptr(GOptionContext) option_context = NULL;

	/* parse options */
	option_context = pkgc_option_context_for_command (
		ctx, cmd,
		NULL,
		/* TRANSLATORS: Description for pkgcli repair */
		_("Attempt to repair the package management system."));

	if (!pkgc_parse_command_options (ctx, cmd, option_context, &argc, &argv, 1))
		return PKGC_EXIT_SYNTAX_ERROR;

	/* repair system */
	pk_task_repair_system_async (PK_TASK (ctx->task),
				     ctx->cancellable,
				     pkgc_context_on_progress_cb,
				     ctx,
				     pkgc_manage_on_task_finished_cb,
				     ctx);

	g_main_loop_run (ctx->loop);

	if (ctx->exit_code == PKGC_EXIT_SUCCESS)
		pkgc_print_success (ctx, _("System repaired successfully"));

	return ctx->exit_code;
}

/**
 * pkgc_suggest_quit:
 *
 * Suggest to safely stop the PackageKit daemon.
 */
static gint
pkgc_suggest_quit (PkgcliContext *ctx, PkgcliCommand *cmd, gint argc, gchar **argv)
{
	g_autoptr(GOptionContext) option_context = NULL;
	g_autoptr(GError) error = NULL;

	/* parse options */
	option_context = pkgc_option_context_for_command (
		ctx, cmd,
		NULL,
		/* TRANSLATORS: Description for pkgcli quit */
		_("Safely terminate the PackageKit daemon."));

	if (!pkgc_parse_command_options (ctx, cmd, option_context, &argc, &argv, 1))
		return PKGC_EXIT_SYNTAX_ERROR;

	if (!pk_control_suggest_daemon_quit (ctx->control, ctx->cancellable, &error)) {
		pkgc_print_error (ctx, _("Failed to send daemon quit request: %s"), error->message);
		return PKGC_EXIT_FAILURE;
	}

	return PKGC_EXIT_SUCCESS;
}

/**
 * pkgc_register_manage_commands:
 *
 * Register package management commands
 */
void
pkgc_register_manage_commands (PkgcliContext *ctx)
{
	pkgc_context_register_command (
		ctx,
       "refresh",
       pkgc_refresh,
       _("Refresh package metadata"));

	pkgc_context_register_command (
		ctx,
		"install",
		pkgc_install,
		_("Install packages"));

	pkgc_context_register_command (
		ctx,
		"remove",
		pkgc_remove,
		_("Remove packages"));

	pkgc_context_register_command (
	    ctx,
	    "update",
	    pkgc_update,
	    _("Update packages"));

	pkgc_context_register_command (
	    ctx,
	    "upgrade",
	    pkgc_upgrade,
	    _("Upgrade the system"));

	pkgc_context_register_command (
	    ctx,
	    "download",
	    pkgc_download,
	    _("Download packages"));

	pkgc_context_register_command (
		ctx,
		"offline-update",
		pkgc_offline_update,
		_("Manage offline system updates"));

	pkgc_context_register_command (
		ctx,
		"install-sig",
		pkgc_install_sig,
		_("Install package signature"));

	pkgc_context_register_command (
		ctx,
		"repair",
		pkgc_repair,
		_("Repair package system"));

	pkgc_context_register_command (
		ctx,
		"quit",
		pkgc_suggest_quit,
		_("Safely stop the PackageKit daemon"));
}