File: gs-plugin-flatpak.c

package info (click to toggle)
gnome-software 49.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,892 kB
  • sloc: ansic: 98,572; xml: 3,630; python: 1,055; makefile: 75; sh: 28
file content (3338 lines) | stat: -rw-r--r-- 126,308 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
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 * vi:set noexpandtab tabstop=8 shiftwidth=8:
 *
 * Copyright (C) 2016 Joaquim Rocha <jrocha@endlessm.com>
 * Copyright (C) 2016-2018 Richard Hughes <richard@hughsie.com>
 * Copyright (C) 2017-2020 Kalev Lember <klember@redhat.com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/*
 * SECTION:
 * Exposes flatpaks from the user and system repositories.
 *
 * All GsApp's created have management-plugin set to flatpak
 * Some GsApp's created have have flatpak::kind of app or runtime
 * The GsApp:origin is the remote name, e.g. test-repo
 *
 * The plugin has two worker threads which all operations are delegated to, as
 * the libflatpak API is entirely synchronous (and thread-safe). Message passing
 * to the worker threads is by gs_worker_thread_queue(). One worker thread is
 * for ‘long-running’ operations, such as updating, installing and removing
 * apps. These are operations which might take a long time to complete, and we
 * don’t want them blocking other flatpak plugin operations in the meantime,
 * such as listing apps. The other worker thread is for all other operations.
 */

#include <config.h>

#include <flatpak.h>
#include <glib/gi18n.h>
#include <gnome-software.h>

#include "gs-appstream.h"
#include "gs-flatpak-app.h"
#include "gs-flatpak.h"
#include "gs-flatpak-transaction.h"
#include "gs-flatpak-utils.h"
#include "gs-metered.h"
#include "gs-profiler.h"
#include "gs-worker-thread.h"

#include "gs-plugin-flatpak.h"

/* Timeout for pure of unused refs:
 * - A timer checks every 2h
 * - If the plugin is enabled, and unused refs have not yet been
 *   removed (successfully or not) in the last 24h, then `flatpak-purge-timestamp`
 *   is updated and a purge operation is started
 * - Timeout callbacks are ignored until another 24h has passed
 */
#define PURGE_TIMEOUT_SECONDS (60 * 60 * 2)

struct _GsPluginFlatpak
{
	GsPlugin		 parent;

	GsWorkerThread		*worker;  /* (owned) */
	GsWorkerThread		*long_running_worker;  /* (owned) */

	GPtrArray		*installations;  /* (element-type GsFlatpak) (owned); may be NULL before setup or after shutdown */
	gboolean		 has_system_helper;
	const gchar		*destdir_for_tests;

	GCancellable		*purge_cancellable;
	guint			 purge_timeout_id;

	GPtrArray		*cache_files_to_delete;  /* (element-type GFile) (nullable) */
};

G_DEFINE_TYPE (GsPluginFlatpak, gs_plugin_flatpak, GS_TYPE_PLUGIN)

#define assert_in_worker(self) \
	g_assert (gs_worker_thread_is_in_worker_context (self->worker) || \
		  gs_worker_thread_is_in_worker_context (self->long_running_worker))

/* Work around flatpak_transaction_get_no_interaction() not existing before
 * flatpak 1.13.0. */
#if !FLATPAK_CHECK_VERSION(1,13,0)
#define flatpak_transaction_get_no_interaction(transaction) \
	GPOINTER_TO_INT (g_object_get_data (G_OBJECT (transaction), "flatpak-no-interaction"))
#define flatpak_transaction_set_no_interaction(transaction, no_interaction) \
	G_STMT_START { \
		FlatpakTransaction *ftsni_transaction = (transaction); \
		gboolean ftsni_no_interaction = (no_interaction); \
		(flatpak_transaction_set_no_interaction) (ftsni_transaction, ftsni_no_interaction); \
		g_object_set_data (G_OBJECT (ftsni_transaction), "flatpak-no-interaction", GINT_TO_POINTER (ftsni_no_interaction)); \
	} G_STMT_END
#endif  /* flatpak < 1.13.0 */

static void
gs_plugin_flatpak_dispose (GObject *object)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (object);

	g_assert (self->cache_files_to_delete == NULL || self->cache_files_to_delete->len == 0);
	g_clear_pointer (&self->cache_files_to_delete, g_ptr_array_unref);

	g_cancellable_cancel (self->purge_cancellable);
	g_assert (self->purge_timeout_id == 0);

	g_clear_pointer (&self->installations, g_ptr_array_unref);
	g_clear_object (&self->purge_cancellable);
	g_clear_object (&self->worker);
	g_clear_object (&self->long_running_worker);

	G_OBJECT_CLASS (gs_plugin_flatpak_parent_class)->dispose (object);
}

static void
gs_plugin_flatpak_init (GsPluginFlatpak *self)
{
	GsPlugin *plugin = GS_PLUGIN (self);

	self->installations = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);

	/* getting app properties from appstream is quicker */
	gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_AFTER, "appstream");

	/* like appstream, we need the icon plugin to load cached icons into pixbufs */
	gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_RUN_BEFORE, "icons");

	/* prioritize over packages */
	gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_BETTER_THAN, "packagekit");
	gs_plugin_add_rule (plugin, GS_PLUGIN_RULE_BETTER_THAN, "rpm-ostree");

	/* used for self tests */
	self->destdir_for_tests = g_getenv ("GS_SELF_TEST_FLATPAK_DATADIR");
}

/* Run in @worker. */
static void
gs_plugin_flatpak_purge_thread_cb (GTask        *task,
				   gpointer      source_object,
				   gpointer      task_data,
				   GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GPtrArray *flatpaks = task_data;

	assert_in_worker (self);

	for (guint i = 0; i < flatpaks->len; i++) {
		g_autoptr(GError) local_error = NULL;
		GsFlatpak *flatpak = g_ptr_array_index (flatpaks, i);

		if (!gs_flatpak_purge_sync (flatpak, cancellable, &local_error)) {
			gs_flatpak_error_convert (&local_error);
			g_debug ("Failed to purge unused refs at '%s': %s",
				 gs_flatpak_get_id (flatpak), local_error->message);
		}
	}

	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_flatpak_purge_timeout_cb (gpointer user_data)
{
	GsPluginFlatpak *self = user_data;
	if (gs_plugin_get_enabled (GS_PLUGIN (self))) {
		g_autoptr(GSettings) settings = g_settings_new ("org.gnome.software");
		gint64 current_time = g_get_real_time () / G_USEC_PER_SEC;
		if ((current_time / (60 * 60 * 24)) != (g_settings_get_int64 (settings, "flatpak-purge-timestamp") / (60 * 60 * 24))) {
			g_autoptr(GPtrArray) flatpaks = g_ptr_array_new_with_free_func (g_object_unref);
			g_settings_set_int64 (settings, "flatpak-purge-timestamp", current_time);
			g_cancellable_cancel (self->purge_cancellable);
			g_clear_object (&self->purge_cancellable);
			self->purge_cancellable = g_cancellable_new ();
			for (guint i = 0; i < self->installations->len; i++) {
				GsFlatpak *flatpak = g_ptr_array_index (self->installations, i);
				if (gs_flatpak_get_busy (flatpak)) {
					g_debug ("Skipping '%s' in this round, it's busy right now", gs_flatpak_get_id (flatpak));
					continue;
				}
				g_ptr_array_add (flatpaks, g_object_ref (flatpak));
			}
			if (flatpaks->len > 0) {
				g_autoptr(GTask) task = NULL;

				task = g_task_new (self, self->purge_cancellable, NULL, NULL);
				g_task_set_source_tag (task, gs_plugin_flatpak_purge_timeout_cb);
				g_task_set_task_data (task, g_steal_pointer (&flatpaks), (GDestroyNotify) g_ptr_array_unref);

				gs_worker_thread_queue (self->long_running_worker, G_PRIORITY_LOW,
						        gs_plugin_flatpak_purge_thread_cb, g_steal_pointer (&task));
			}
		}
	} else {
		self->purge_timeout_id = 0;
		return G_SOURCE_REMOVE;
	}
	return G_SOURCE_CONTINUE;
}

static gboolean
_as_component_scope_is_compatible (AsComponentScope scope1, AsComponentScope scope2)
{
	if (scope1 == AS_COMPONENT_SCOPE_UNKNOWN)
		return TRUE;
	if (scope2 == AS_COMPONENT_SCOPE_UNKNOWN)
		return TRUE;
	return scope1 == scope2;
}

static void
gs_plugin_flatpak_adopt_app (GsPlugin *plugin,
			     GsApp *app)
{
	if (gs_app_get_bundle_kind (app) == AS_BUNDLE_KIND_FLATPAK)
		gs_app_set_management_plugin (app, plugin);
}

static gboolean
gs_plugin_flatpak_add_installation (GsPluginFlatpak      *self,
                                    FlatpakInstallation  *installation,
                                    GCancellable         *cancellable,
                                    GError              **error)
{
	g_autoptr(GsFlatpak) flatpak = NULL;
	GsFlatpakFlags flags = GS_FLATPAK_FLAG_NONE;

	if (!flatpak_installation_get_is_user (installation) && !self->has_system_helper)
		flags |= GS_FLATPAK_FLAG_DISABLE_UPDATE;

	/* create and set up */
	flatpak = gs_flatpak_new (GS_PLUGIN (self), installation, flags);
	if (!gs_flatpak_setup (flatpak, cancellable, error))
		return FALSE;
	g_debug ("successfully set up %s", gs_flatpak_get_id (flatpak));

	/* add objects that set up correctly */
	g_ptr_array_add (self->installations, g_steal_pointer (&flatpak));
	return TRUE;
}

/* Reports a warning/error at the plugin level, rather than at the job level.
 * This should only be used for warnings/errors which aren’t associated with a
 * particular `GsPluginJob`; otherwise, use the `event_callback()` from the job
 * as that exposes more information to the shell. */
static void
gs_plugin_flatpak_report_warning (GsPlugin *plugin,
				  GError **error)
{
	g_autoptr(GsPluginEvent) event = NULL;
	g_assert (error != NULL);
	if (*error != NULL && (*error)->domain != GS_PLUGIN_ERROR)
		gs_flatpak_error_convert (error);

	event = gs_plugin_event_new ("error", *error,
				     NULL);
	gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING | GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
	gs_plugin_report_event (plugin, event);
}

static gint
get_priority_for_interactivity (gboolean interactive)
{
	return interactive ? G_PRIORITY_DEFAULT : G_PRIORITY_LOW;
}

static void setup_thread_cb (GTask        *task,
                             gpointer      source_object,
                             gpointer      task_data,
                             GCancellable *cancellable);

static void
gs_plugin_flatpak_setup_async (GsPlugin            *plugin,
                               GCancellable        *cancellable,
                               GAsyncReadyCallback  callback,
                               gpointer             user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;

	g_debug ("Flatpak version: %d.%d.%d",
		FLATPAK_MAJOR_VERSION,
		FLATPAK_MINOR_VERSION,
		FLATPAK_MICRO_VERSION);

	task = g_task_new (plugin, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_setup_async);

	/* Shouldn’t end up setting up twice */
	g_assert (self->installations == NULL || self->installations->len == 0);

	/* Start up two worker threads to process all the plugin’s function calls. */
	self->worker = gs_worker_thread_new ("gs-plugin-flatpak");
	self->long_running_worker = gs_worker_thread_new ("gs-plugin-flatpak-long");

	/* Queue a job to find and set up the installations. */
	gs_worker_thread_queue (self->worker, G_PRIORITY_DEFAULT,
				setup_thread_cb, g_steal_pointer (&task));

	if (!self->purge_timeout_id)
		self->purge_timeout_id = g_timeout_add_seconds (PURGE_TIMEOUT_SECONDS,
								gs_plugin_flatpak_purge_timeout_cb,
								self);
}

/* Run in @worker. */
static void
setup_thread_cb (GTask        *task,
                 gpointer      source_object,
                 gpointer      task_data,
                 GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsPlugin *plugin = GS_PLUGIN (self);
	g_autoptr(GPtrArray) installations = NULL;
	const gchar *action_id = "org.freedesktop.Flatpak.appstream-update";
	g_autoptr(GError) permission_error = NULL;
	g_autoptr(GPermission) permission = NULL;

	assert_in_worker (self);

	/* if we can't update the AppStream database system-wide don't even
	 * pull the data as we can't do anything with it */
	permission = gs_utils_get_permission (action_id, NULL, &permission_error);
	if (permission == NULL) {
		g_debug ("no permission for %s: %s", action_id, permission_error->message);
		g_clear_error (&permission_error);
	} else {
		self->has_system_helper = g_permission_get_allowed (permission) ||
					  g_permission_get_can_acquire (permission);
	}

	/* if we're not just running the tests */
	if (self->destdir_for_tests == NULL) {
		g_autoptr(GError) error_local = NULL;
		g_autoptr(FlatpakInstallation) installation = NULL;

		/* include the system installations */
		installations = flatpak_get_system_installations (cancellable,
								  &error_local);

		if (installations == NULL) {
			gs_plugin_flatpak_report_warning (plugin, &error_local);
			g_clear_error (&error_local);
		}

		/* include the user installation */
		installation = flatpak_installation_new_user (cancellable,
							      &error_local);
		if (installation == NULL) {
			/* if some error happened, report it as an event, but
			 * do not return it, otherwise it will disable the whole
			 * plugin (meaning that support for Flatpak will not be
			 * possible even if a system installation is working) */
			gs_plugin_flatpak_report_warning (plugin, &error_local);
		} else {
			if (installations == NULL)
				installations = g_ptr_array_new_with_free_func (g_object_unref);

			g_ptr_array_add (installations, g_steal_pointer (&installation));
		}
	} else {
		g_autoptr(GError) error_local = NULL;

		/* use the test installation */
		g_autofree gchar *full_path = g_build_filename (self->destdir_for_tests,
								"flatpak",
								NULL);
		g_autoptr(GFile) file = g_file_new_for_path (full_path);
		g_autoptr(FlatpakInstallation) installation = NULL;
		g_debug ("using custom flatpak path %s", full_path);
		installation = flatpak_installation_new_for_path (file, TRUE,
								  cancellable,
								  &error_local);
		if (installation == NULL) {
			gs_flatpak_error_convert (&error_local);
			g_task_return_error (task, g_steal_pointer (&error_local));
			return;
		}

		installations = g_ptr_array_new_with_free_func (g_object_unref);
		g_ptr_array_add (installations, g_steal_pointer (&installation));
	}

	/* add the installations */
	for (guint i = 0; installations != NULL && i < installations->len; i++) {
		g_autoptr(GError) error_local = NULL;

		FlatpakInstallation *installation = g_ptr_array_index (installations, i);
		if (!gs_plugin_flatpak_add_installation (self,
							 installation,
							 cancellable,
							 &error_local)) {
			gs_plugin_flatpak_report_warning (plugin,
							  &error_local);
			continue;
		}
	}

	/* when no installation has been loaded, return the error so the
	 * plugin gets disabled */
	if (self->installations->len == 0) {
		g_task_return_new_error (task,
					 GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED,
					 "Failed to load any Flatpak installations");
		return;
	}

	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_flatpak_setup_finish (GsPlugin      *plugin,
                                GAsyncResult  *result,
                                GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static void shutdown_cb (GObject      *source_object,
                         GAsyncResult *result,
                         gpointer      user_data);

static void
gs_plugin_flatpak_shutdown_async (GsPlugin            *plugin,
                                  GCancellable        *cancellable,
                                  GAsyncReadyCallback  callback,
                                  gpointer             user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;

	g_clear_handle_id (&self->purge_timeout_id, g_source_remove);
	g_cancellable_cancel (self->purge_cancellable);

	task = g_task_new (self, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_shutdown_async);

	/* Delete any cache files from this session. This should not really
	 * block, as they’re all local. */
	for (guint i = 0; self->cache_files_to_delete != NULL && i < self->cache_files_to_delete->len; i++) {
		GFile *cache_file = g_ptr_array_index (self->cache_files_to_delete, i);

		g_file_delete (cache_file, NULL, NULL);
	}

	g_clear_pointer (&self->cache_files_to_delete, g_ptr_array_unref);

	/* Stop the worker threads. */
	gs_worker_thread_shutdown_async (self->worker, cancellable, shutdown_cb, g_steal_pointer (&task));
}

static void
shutdown_cb (GObject      *source_object,
             GAsyncResult *result,
             gpointer      user_data)
{
	g_autoptr(GTask) task = G_TASK (user_data);
	GsPluginFlatpak *self = g_task_get_source_object (task);
	GCancellable *cancellable = g_task_get_cancellable (task);
	g_autoptr(GsWorkerThread) worker = NULL;
	g_autoptr(GError) local_error = NULL;

	if (self->worker != NULL)
		worker = g_steal_pointer (&self->worker);
	else
		worker = g_steal_pointer (&self->long_running_worker);

	if (!gs_worker_thread_shutdown_finish (worker, result, &local_error)) {
		g_task_return_error (task, g_steal_pointer (&local_error));
		return;
	}

	/* Clear the other worker. */
	if (self->long_running_worker != NULL) {
		gs_worker_thread_shutdown_async (self->long_running_worker, cancellable, shutdown_cb, g_steal_pointer (&task));
		return;
	}

	/* Clear the flatpak installations */
	g_ptr_array_set_size (self->installations, 0);

	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_flatpak_shutdown_finish (GsPlugin      *plugin,
                                   GAsyncResult  *result,
                                   GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

typedef struct {
	GsPlugin *plugin;  /* (owned) (not nullable) */
	GsPluginEvent *event;  /* (owned) (not nullable) */
	GsPluginEventCallback event_callback;
	void *event_user_data;
} EventCallbackData;

static void
event_callback_data_free (EventCallbackData *data)
{
	g_clear_object (&data->plugin);
	g_clear_object (&data->event);
	g_free (data);
}

G_DEFINE_AUTOPTR_CLEANUP_FUNC (EventCallbackData, event_callback_data_free)

static gboolean
event_callback_idle_cb (void *user_data)
{
	EventCallbackData *data = user_data;

	data->event_callback (data->plugin, data->event, data->event_user_data);
	return G_SOURCE_REMOVE;
}

static void
event_callback_invoke_take (GsPluginFlatpak       *plugin,
                            GsPluginEvent         *event,  /* (transfer full) */
                            GMainContext          *context,
                            GsPluginEventCallback  event_callback,
                            void                  *event_user_data)
{
	g_autoptr(EventCallbackData) event_data = NULL;
	g_autoptr(GsPluginEvent) event_owned = g_steal_pointer (&event);

	g_return_if_fail (event_callback != NULL);

	event_data = g_new0 (EventCallbackData, 1);
	event_data->plugin = GS_PLUGIN (g_object_ref (plugin));
	event_data->event = g_steal_pointer (&event_owned);
	event_data->event_callback = event_callback;
	event_data->event_user_data = event_user_data;

	g_main_context_invoke_full (context, G_PRIORITY_DEFAULT,
				    event_callback_idle_cb, g_steal_pointer (&event_data), (GDestroyNotify) event_callback_data_free);
}

static void refresh_metadata_thread_cb (GTask        *task,
                                        gpointer      source_object,
                                        gpointer      task_data,
                                        GCancellable *cancellable);

static void
gs_plugin_flatpak_refresh_metadata_async (GsPlugin                     *plugin,
                                          guint64                       cache_age_secs,
                                          GsPluginRefreshMetadataFlags  flags,
                                          GsPluginEventCallback         event_callback,
                                          void                         *event_user_data,
                                          GCancellable                 *cancellable,
                                          GAsyncReadyCallback           callback,
                                          gpointer                      user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = (flags & GS_PLUGIN_REFRESH_METADATA_FLAGS_INTERACTIVE);

	task = g_task_new (plugin, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_refresh_metadata_async);
	g_task_set_task_data (task, gs_plugin_refresh_metadata_data_new (cache_age_secs, flags, event_callback, event_user_data), (GDestroyNotify) gs_plugin_refresh_metadata_data_free);

	/* Queue a job to get the installed apps. */
	gs_worker_thread_queue (self->worker, get_priority_for_interactivity (interactive),
				refresh_metadata_thread_cb, g_steal_pointer (&task));
}

/* Run in @worker. */
static void
refresh_metadata_thread_cb (GTask        *task,
                            gpointer      source_object,
                            gpointer      task_data,
                            GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsPluginRefreshMetadataData *data = task_data;
	gboolean interactive = (data->flags & GS_PLUGIN_REFRESH_METADATA_FLAGS_INTERACTIVE);

	assert_in_worker (self);

	for (guint i = 0; i < self->installations->len; i++) {
		g_autoptr(GError) local_error = NULL;
		GsFlatpak *flatpak = g_ptr_array_index (self->installations, i);

		if (!self->has_system_helper && gs_flatpak_get_scope (flatpak) == AS_COMPONENT_SCOPE_SYSTEM)
			continue;

		if (!gs_flatpak_refresh (flatpak, data->cache_age_secs, interactive, data->event_callback, data->event_user_data, cancellable, &local_error))
			g_debug ("Failed to refresh metadata for '%s': %s", gs_flatpak_get_id (flatpak), local_error->message);
	}

	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_flatpak_refresh_metadata_finish (GsPlugin      *plugin,
                                           GAsyncResult  *result,
                                           GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static GsFlatpak *
gs_plugin_flatpak_get_handler (GsPluginFlatpak *self,
                               GsApp           *app)
{
	const gchar *object_id;

	/* only process this app if was created by this plugin */
	if (!gs_app_has_management_plugin (app, GS_PLUGIN (self)))
		return NULL;

	/* specified an explicit name */
	object_id = gs_flatpak_app_get_object_id (app);
	if (object_id != NULL) {
		for (guint i = 0; i < self->installations->len; i++) {
			GsFlatpak *flatpak = g_ptr_array_index (self->installations, i);
			if (g_strcmp0 (gs_flatpak_get_id (flatpak), object_id) == 0)
				return flatpak;
		}
	}

	/* find a scope that matches */
	for (guint i = 0; i < self->installations->len; i++) {
		GsFlatpak *flatpak = g_ptr_array_index (self->installations, i);
		if (_as_component_scope_is_compatible (gs_flatpak_get_scope (flatpak),
						 gs_app_get_scope (app)))
			return flatpak;
	}
	return NULL;
}

static gboolean
gs_plugin_flatpak_refine_app (GsPluginFlatpak             *self,
                              GsApp                       *app,
                              GsPluginRefineRequireFlags   require_flags,
                              gboolean                     interactive,
                              GsPluginEventCallback        event_callback,
                              void                        *event_user_data,
                              GCancellable                *cancellable,
                              GError                     **error)
{
	GsFlatpak *flatpak = NULL;

	/* not us */
	if (gs_app_get_bundle_kind (app) != AS_BUNDLE_KIND_FLATPAK) {
		g_debug ("%s not a package, ignoring", gs_app_get_unique_id (app));
		return TRUE;
	}

	/* we have to look for the app in all GsFlatpak stores */
	if (gs_app_get_scope (app) == AS_COMPONENT_SCOPE_UNKNOWN) {
		for (guint i = 0; i < self->installations->len; i++) {
			GsFlatpak *flatpak_tmp = g_ptr_array_index (self->installations, i);
			g_autoptr(GError) error_local = NULL;
			if (gs_flatpak_refine_app_state (flatpak_tmp, app, interactive, FALSE,
							 event_callback, event_user_data,
							 cancellable, &error_local)) {
				flatpak = flatpak_tmp;
				break;
			} else {
				g_debug ("%s", error_local->message);
			}
		}
	} else {
		flatpak = gs_plugin_flatpak_get_handler (self, app);
	}
	if (flatpak == NULL)
		return TRUE;
	return gs_flatpak_refine_app (flatpak, app, require_flags, interactive, FALSE, event_callback, event_user_data, cancellable, error);
}

static void
unref_nonnull_hash_table (gpointer ptr)
{
	GHashTable *hash_table = ptr;
	if (hash_table != NULL)
		g_hash_table_unref (hash_table);
}

static gboolean
refine_app (GsPluginFlatpak             *self,
            GsApp                       *app,
            GsPluginRefineRequireFlags   require_flags,
            gboolean                     interactive,
            GsPluginEventCallback        event_callback,
            void                        *event_user_data,
            GCancellable                *cancellable,
            GError                     **error)
{
	GS_PROFILER_BEGIN_SCOPED (FlatpakRefineApp, "Flatpak (refine app)", NULL);

	/* only process this app if was created by this plugin */
	if (!gs_app_has_management_plugin (app, GS_PLUGIN (self)))
		return TRUE;

	/* get the runtime first */
	if (!gs_plugin_flatpak_refine_app (self, app, require_flags, interactive, event_callback, event_user_data, cancellable, error))
		return FALSE;

	GS_PROFILER_END_SCOPED (FlatpakRefineApp);

	/* the runtime might be installed in a different scope */
	if (require_flags & GS_PLUGIN_REFINE_REQUIRE_FLAGS_RUNTIME) {
		GsApp *runtime = gs_app_get_runtime (app);
		if (runtime != NULL) {
			GS_PROFILER_BEGIN_SCOPED (FlatpakRefineAppRuntime, "Flatpak (refine runtime)", NULL);

			if (!gs_plugin_flatpak_refine_app (self, runtime,
							   require_flags,
							   interactive,
							   event_callback,
							   event_user_data,
							   cancellable,
							   error)) {
				return FALSE;
			}

			GS_PROFILER_END_SCOPED (FlatpakRefineAppRuntime);
		}
	}
	return TRUE;
}

static void refine_thread_cb (GTask        *task,
                              gpointer      source_object,
                              gpointer      task_data,
                              GCancellable *cancellable);

static void
gs_plugin_flatpak_refine_async (GsPlugin                   *plugin,
                                GsAppList                  *list,
                                GsPluginRefineFlags         job_flags,
                                GsPluginRefineRequireFlags  require_flags,
                                GsPluginEventCallback       event_callback,
                                void                       *event_user_data,
                                GCancellable               *cancellable,
                                GAsyncReadyCallback         callback,
                                gpointer                    user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = (job_flags & GS_PLUGIN_REFINE_FLAGS_INTERACTIVE) != 0;

	task = gs_plugin_refine_data_new_task (plugin, list, job_flags, require_flags, event_callback, event_user_data, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_refine_async);

	/* Queue a job to refine the apps. */
	gs_worker_thread_queue (self->worker, get_priority_for_interactivity (interactive),
				refine_thread_cb, g_steal_pointer (&task));
}

/* Run in @worker. */
static void
refine_thread_cb (GTask        *task,
                  gpointer      source_object,
                  gpointer      task_data,
                  GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsPluginRefineData *data = task_data;
	GsAppList *list = data->list;
	GsPluginRefineRequireFlags require_flags = data->require_flags;
	gboolean interactive = (data->job_flags & GS_PLUGIN_REFINE_FLAGS_INTERACTIVE) != 0;
	GsPluginEventCallback event_callback = data->event_callback;
	void *event_user_data = data->event_user_data;
	g_autoptr(GPtrArray) array_components_by_id = NULL; /* (element-type GHashTable) */
	g_autoptr(GPtrArray) array_components_by_bundle = NULL; /* (element-type GHashTable) */
	g_autoptr(GsAppList) app_list = NULL;
	g_autoptr(GError) local_error = NULL;

	assert_in_worker (self);

	for (guint i = 0; i < gs_app_list_length (list); i++) {
		GsApp *app = gs_app_list_index (list, i);
		if (!refine_app (self, app, require_flags, interactive, event_callback, event_user_data, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}
	}

	/* Refine wildcards.
	 *
	 * Use a copy of the list for the loop because a function called
	 * on the plugin may affect the list which can lead to problems
	 * (e.g. inserting an app in the list on every call results in
	 * an infinite loop) */
	app_list = gs_app_list_copy (list);
	array_components_by_id = g_ptr_array_new_full (self->installations->len, unref_nonnull_hash_table);
	g_ptr_array_set_size (array_components_by_id, self->installations->len);
	array_components_by_bundle = g_ptr_array_new_full (self->installations->len, unref_nonnull_hash_table);
	g_ptr_array_set_size (array_components_by_bundle, self->installations->len);

	for (guint j = 0; j < gs_app_list_length (app_list); j++) {
		GsApp *app = gs_app_list_index (app_list, j);

		if (!gs_app_has_quirk (app, GS_APP_QUIRK_IS_WILDCARD))
			continue;

		for (guint i = 0; i < self->installations->len; i++) {
			GsFlatpak *flatpak = g_ptr_array_index (self->installations, i);
			GHashTable *components_by_id = array_components_by_id->pdata[i];
			GHashTable *components_by_bundle = array_components_by_bundle->pdata[i];

			if (!gs_flatpak_refine_wildcard (flatpak, app, list, require_flags, interactive, &components_by_id, &components_by_bundle,
							 cancellable, &local_error)) {
				g_task_return_error (task, g_steal_pointer (&local_error));
				return;
			}
			array_components_by_id->pdata[i] = components_by_id;
			array_components_by_bundle->pdata[i] = components_by_bundle;
		}
	}

	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_flatpak_refine_finish (GsPlugin      *plugin,
                                 GAsyncResult  *result,
                                 GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

/* Run in @worker. */
static void
launch_thread_cb (GTask        *task,
                  gpointer      source_object,
                  gpointer      task_data,
                  GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsPluginLaunchData *data = task_data;
	GsFlatpak *flatpak;
	g_autoptr(GError) local_error = NULL;
	gboolean interactive = (data->flags & GS_PLUGIN_LAUNCH_FLAGS_INTERACTIVE) != 0;

	assert_in_worker (self);

	flatpak = gs_plugin_flatpak_get_handler (self, data->app);
	if (flatpak == NULL) {
		g_task_return_boolean (task, TRUE);
		return;
	}

	if (gs_flatpak_launch (flatpak, data->app, interactive, cancellable, &local_error))
		g_task_return_boolean (task, TRUE);
	else
		g_task_return_error (task, g_steal_pointer (&local_error));
}

static void
gs_plugin_flatpak_launch_async (GsPlugin            *plugin,
                                GsApp               *app,
                                GsPluginLaunchFlags  flags,
                                GCancellable        *cancellable,
                                GAsyncReadyCallback  callback,
                                gpointer             user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = (flags & GS_PLUGIN_LAUNCH_FLAGS_INTERACTIVE) != 0;

	task = gs_plugin_launch_data_new_task (plugin, app, flags, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_launch_async);

	/* only process this app if was created by this plugin */
	if (!gs_app_has_management_plugin (app, plugin)) {
		g_task_return_boolean (task, TRUE);
		return;
	}

	/* Queue a job to launch the app. */
	gs_worker_thread_queue (self->worker, get_priority_for_interactivity (interactive),
				launch_thread_cb, g_steal_pointer (&task));
}

static gboolean
gs_plugin_flatpak_launch_finish (GsPlugin      *plugin,
                                 GAsyncResult  *result,
                                 GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

/* ref full */
static GsApp *
gs_plugin_flatpak_find_app_by_ref (GsPluginFlatpak  *self,
                                   const gchar      *ref,
                                   gboolean          interactive,
                                   GsApp            *alternate_of,
                                   GCancellable     *cancellable,
                                   GError          **error)
{
	g_debug ("finding ref %s", ref);
	for (guint i = 0; i < self->installations->len; i++) {
		GsFlatpak *flatpak_tmp = g_ptr_array_index (self->installations, i);
		g_autoptr(GsApp) app = NULL;
		g_autoptr(GError) error_local = NULL;

		app = gs_flatpak_ref_to_app (flatpak_tmp, ref, interactive, cancellable, &error_local);
		if (app == NULL) {
			g_debug ("%s", error_local->message);
			continue;
		}
		if (alternate_of != NULL && alternate_of == app) {
			g_debug ("skipping ref=%s->%s, due to being alternate_of", ref, gs_app_get_unique_id (app));
			continue;
		}
		g_debug ("found ref=%s->%s", ref, gs_app_get_unique_id (app));
		return g_steal_pointer (&app);
	}
	return NULL;
}

/* ref full */
static GsApp *
_ref_to_app (FlatpakTransaction *transaction,
             const gchar        *ref,
             GsPluginFlatpak    *self)
{
	g_return_val_if_fail (GS_IS_FLATPAK_TRANSACTION (transaction), NULL);
	g_return_val_if_fail (ref != NULL, NULL);
	g_return_val_if_fail (GS_IS_PLUGIN_FLATPAK (self), NULL);

	/* search through each GsFlatpak */
	return gs_plugin_flatpak_find_app_by_ref (self, ref,
						  !flatpak_transaction_get_no_interaction (transaction),
						  NULL, NULL, NULL);
}

static void
_group_apps_by_installation_recurse (GsPluginFlatpak *self,
                                     GsAppList       *list,
                                     GHashTable      *applist_by_flatpaks)
{
	if (!list)
		return;

	for (guint i = 0; i < gs_app_list_length (list); i++) {
		GsApp *app = gs_app_list_index (list, i);
		GsFlatpak *flatpak = gs_plugin_flatpak_get_handler (self, app);
		if (flatpak != NULL) {
			GsAppList *list_tmp = g_hash_table_lookup (applist_by_flatpaks, flatpak);
			GsAppList *related_list;
			if (list_tmp == NULL) {
				list_tmp = gs_app_list_new ();
				g_hash_table_insert (applist_by_flatpaks,
						     g_object_ref (flatpak),
						     list_tmp);
			}
			gs_app_list_add (list_tmp, app);

			/* Add also related apps, which can be those recognized for update,
			   while the 'app' is already up to date. */
			related_list = gs_app_get_related (app);
			_group_apps_by_installation_recurse (self, related_list, applist_by_flatpaks);
		}
	}
}

/*
 * Returns: (transfer full) (element-type GsFlatpak GsAppList):
 *  a map from GsFlatpak to non-empty lists of apps from @list associated
 *  with that installation.
 */
static GHashTable *
_group_apps_by_installation (GsPluginFlatpak *self,
                             GsAppList       *list)
{
	g_autoptr(GHashTable) applist_by_flatpaks = NULL;

	/* list of apps to be handled by each flatpak installation */
	applist_by_flatpaks = g_hash_table_new_full (g_direct_hash, g_direct_equal,
						     (GDestroyNotify) g_object_unref,
						     (GDestroyNotify) g_object_unref);

	/* put each app into the correct per-GsFlatpak list */
	_group_apps_by_installation_recurse (self, list, applist_by_flatpaks);

	return g_steal_pointer (&applist_by_flatpaks);
}

typedef struct {
	FlatpakTransaction *transaction;
	guint id;
} BasicAuthData;

static void
basic_auth_data_free (BasicAuthData *data)
{
	g_object_unref (data->transaction);
	g_slice_free (BasicAuthData, data);
}

G_DEFINE_AUTOPTR_CLEANUP_FUNC(BasicAuthData, basic_auth_data_free)

static void
_basic_auth_cb (const gchar *user, const gchar *password, gpointer user_data)
{
	g_autoptr(BasicAuthData) data = user_data;

	g_debug ("Submitting basic auth data");

	/* NULL user aborts the basic auth request */
	flatpak_transaction_complete_basic_auth (data->transaction, data->id, user, password, NULL /* options */);
}

static gboolean
_basic_auth_start (FlatpakTransaction *transaction,
                   const char *remote,
                   const char *realm,
                   GVariant *options,
                   guint id,
                   GsPlugin *plugin)
{
	BasicAuthData *data;

	if (flatpak_transaction_get_no_interaction (transaction))
		return FALSE;

	data = g_slice_new0 (BasicAuthData);
	data->transaction = g_object_ref (transaction);
	data->id = id;

	g_debug ("Login required remote %s (realm %s)\n", remote, realm);
	gs_plugin_basic_auth_start (plugin, remote, realm, G_CALLBACK (_basic_auth_cb), data);
	return TRUE;
}

typedef struct {
	GsPluginFlatpak *plugin;  /* (not owned) (not nullable) */
	GMainContext *callback_context;  /* (owned) (not nullable) */
	GsPluginEventCallback event_callback;  /* (nullable) */
	void *event_user_data;
} WebflowStartData;

static void
webflow_start_data_free (WebflowStartData *data)
{
	g_clear_pointer (&data->callback_context, g_main_context_unref);
	g_free (data);
}

G_DEFINE_AUTOPTR_CLEANUP_FUNC (WebflowStartData, webflow_start_data_free)

static void
webflow_start_data_free_closure (WebflowStartData *data,
                                 GClosure         *closure)
{
	webflow_start_data_free (data);
}

static gboolean
_webflow_start (FlatpakTransaction *transaction,
                const char *remote,
                const char *url,
                GVariant *options,
                guint id,
                void *user_data)
{
	WebflowStartData *data = user_data;
	const char *browser;
	g_autoptr(GError) error_local = NULL;

	if (flatpak_transaction_get_no_interaction (transaction))
		return FALSE;

	g_debug ("Authentication required for remote '%s'", remote);

	/* Allow hard overrides with $BROWSER */
	browser = g_getenv ("BROWSER");
	if (browser != NULL) {
		const char *args[3] = { NULL, url, NULL };
		args[0] = browser;
		if (!g_spawn_async (NULL, (char **)args, NULL, G_SPAWN_SEARCH_PATH,
		                    NULL, NULL, NULL, &error_local)) {
			g_warning ("Failed to start browser %s: %s", browser, error_local->message);

			gs_flatpak_error_convert (&error_local);

			if (data->event_callback != NULL) {
				g_autoptr(GsPluginEvent) event = NULL;

				event = gs_plugin_event_new ("error", error_local,
							     NULL);
				gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING | GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
				event_callback_invoke_take (data->plugin, g_steal_pointer (&event), data->callback_context,
							    data->event_callback, data->event_user_data);
			}

			return FALSE;
		}
	} else {
		if (!g_app_info_launch_default_for_uri (url, NULL, &error_local)) {
			g_warning ("Failed to show url: %s", error_local->message);

			gs_flatpak_error_convert (&error_local);

			if (data->event_callback != NULL) {
				g_autoptr(GsPluginEvent) event = NULL;

				event = gs_plugin_event_new ("error", error_local,
							     NULL);
				gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING | GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
				event_callback_invoke_take (data->plugin, g_steal_pointer (&event), data->callback_context,
							    data->event_callback, data->event_user_data);
			}

			return FALSE;
		}
	}

	g_debug ("Waiting for browser...");

	return TRUE;
}

static void
_webflow_done (FlatpakTransaction *transaction,
               GVariant *options,
               guint id,
               GsPlugin *plugin)
{
	g_debug ("Browser done");
}

/* This can only fail if flatpak_dir_ensure_repo() fails, for example if the
 * repo is configured but doesn’t exist and can’t be created on disk. */
static FlatpakTransaction *
_build_transaction (GsPluginFlatpak        *plugin,
                    GsFlatpak              *flatpak,
                    gboolean                stop_on_first_error,
                    gboolean                interactive,
                    GMainContext           *callback_context,
                    GsPluginEventCallback   event_callback,
                    void                   *event_user_data,
                    GCancellable           *cancellable,
                    GError                **error)
{
	FlatpakInstallation *installation;
	g_autoptr(FlatpakInstallation) installation_clone = NULL;
	g_autoptr(FlatpakTransaction) transaction = NULL;
	g_autoptr(WebflowStartData) webflow_start_data = NULL;

	installation = gs_flatpak_get_installation (flatpak, interactive);

	installation_clone = g_object_ref (installation);

	/* create transaction */
	transaction = gs_flatpak_transaction_new (installation_clone, stop_on_first_error, cancellable, error);
	if (transaction == NULL) {
		g_prefix_error (error, "failed to build transaction: ");
		gs_flatpak_error_convert (error);
		return NULL;
	}

	/* Let flatpak know if it is a background operation */
	flatpak_transaction_set_no_interaction (transaction, !interactive);

	/* connect up signals */
	webflow_start_data = g_new0 (WebflowStartData, 1);
	webflow_start_data->plugin = plugin;
	webflow_start_data->callback_context = g_main_context_ref (callback_context);
	webflow_start_data->event_callback = event_callback;
	webflow_start_data->event_user_data = event_user_data;

	g_signal_connect (transaction, "ref-to-app",
			  G_CALLBACK (_ref_to_app), plugin);
	g_signal_connect (transaction, "basic-auth-start",
			  G_CALLBACK (_basic_auth_start), plugin);
	g_signal_connect_data (transaction, "webflow-start",
			       G_CALLBACK (_webflow_start),
			       g_steal_pointer (&webflow_start_data),
			       (GClosureNotify) webflow_start_data_free_closure,
			       G_CONNECT_DEFAULT);
	g_signal_connect (transaction, "webflow-done",
			  G_CALLBACK (_webflow_done), plugin);

	/* use system installations as dependency sources for user installations */
	flatpak_transaction_add_default_dependency_sources (transaction);

	return g_steal_pointer (&transaction);
}

static void
remove_schedule_entry (gpointer schedule_entry_handle)
{
	g_autoptr(GError) error_local = NULL;

	if (!gs_metered_remove_from_download_scheduler (schedule_entry_handle, NULL, &error_local))
		g_warning ("Failed to remove schedule entry: %s", error_local->message);
}

static void update_apps_thread_cb (GTask        *task,
                                   gpointer      source_object,
                                   gpointer      task_data,
                                   GCancellable *cancellable);

static void
gs_plugin_flatpak_update_apps_async (GsPlugin                           *plugin,
                                     GsAppList                          *apps,
                                     GsPluginUpdateAppsFlags             flags,
                                     GsPluginProgressCallback            progress_callback,
                                     gpointer                            progress_user_data,
                                     GsPluginEventCallback               event_callback,
                                     void                               *event_user_data,
                                     GsPluginAppNeedsUserActionCallback  app_needs_user_action_callback,
                                     gpointer                            app_needs_user_action_data,
                                     GCancellable                       *cancellable,
                                     GAsyncReadyCallback                 callback,
                                     gpointer                            user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = (flags & GS_PLUGIN_UPDATE_APPS_FLAGS_INTERACTIVE);

	task = gs_plugin_update_apps_data_new_task (plugin, apps, flags,
						    progress_callback, progress_user_data,
						    event_callback, event_user_data,
						    app_needs_user_action_callback, app_needs_user_action_data,
						    cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_update_apps_async);

	/* Queue a job to get the apps. */
	gs_worker_thread_queue (self->long_running_worker, get_priority_for_interactivity (interactive),
				update_apps_thread_cb, g_steal_pointer (&task));
}

/* Run in @worker. */
static void
update_apps_thread_cb (GTask        *task,
                       gpointer      source_object,
                       gpointer      task_data,
                       GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsPluginUpdateAppsData *data = task_data;
	gboolean interactive = (data->flags & GS_PLUGIN_UPDATE_APPS_FLAGS_INTERACTIVE);
	g_autoptr(GHashTable) applist_by_flatpaks = NULL;
	GHashTableIter iter;
	gpointer key, value;
	g_autoptr(GError) local_error = NULL;

	assert_in_worker (self);

	/* Mark all the apps as pending installation. While the op/progress
	 * handling code in #GsFlatpakTransaction does this more accurately and
	 * in more detail, we need to pre-emptively do it here, since multiple
	 * transactions are run sequentially below. That means that all the apps
	 * from the 2nd, 3rd, etc. transactions will not have their state
	 * updated until that transaction is prepared. That’s a long time for
	 * the apps to look like they’ve been left out of the update in the UI. */
	applist_by_flatpaks = _group_apps_by_installation (self, data->apps);

	g_hash_table_iter_init (&iter, applist_by_flatpaks);
	while (g_hash_table_iter_next (&iter, NULL, &value)) {
		GsAppList *apps_for_installation = GS_APP_LIST (value);

		for (guint i = 0; i < gs_app_list_length (apps_for_installation); i++) {
			GsApp *app = gs_app_list_index (apps_for_installation, i);

			gs_app_set_state (app, GS_APP_STATE_INSTALLING);
		}
	}

	/* build and run transaction for each flatpak installation */
	g_hash_table_iter_init (&iter, applist_by_flatpaks);
	while (g_hash_table_iter_next (&iter, &key, &value)) {
		GsFlatpak *flatpak = GS_FLATPAK (key);
		GsAppList *list_tmp = GS_APP_LIST (value);
		g_autoptr(FlatpakTransaction) transaction = NULL;
		gpointer schedule_entry_handle = NULL;

		g_assert (GS_IS_FLATPAK (flatpak));
		g_assert (list_tmp != NULL);
		g_assert (gs_app_list_length (list_tmp) > 0);

		if (!interactive) {
			if (!gs_metered_block_app_list_on_download_scheduler (list_tmp, &schedule_entry_handle, cancellable, &local_error)) {
				g_warning ("Failed to block on download scheduler: %s",
					   local_error->message);
				g_clear_error (&local_error);
			}
		}

		/* Now apply the updates. */
		gs_flatpak_set_busy (flatpak, TRUE);

		/* Build and run transaction. Pass %FALSE to stop_on_first_error
		 * so that the transaction continues past the first fatal error
		 * in an attempt to try and update as many apps as possible.
		 *
		 * Internally, `FlatpakTransaction` uses `op->fail_if_op_fails`
		 * and `op->non_fatal` to track the relationships between ops
		 * (such as updating an app and its runtime, or add-ons and
		 * their app). If, for example, updating a runtime fails, the
		 * ops to update apps which use that runtime will automatically
		 * be skipped and will fail with `FLATPAK_ERROR_SKIPPED`.
		 *
		 * %GS_FLATPAK_ERROR_MODE_IGNORE_ERRORS does not ignore
		 * `FLATPAK_ERROR_SKIPPED` errors, so this will not cause
		 * corruption of the transaction.
		 *
		 * This approach is the same as what the `flatpak` CLI uses in
		 * `flatpak-builtins-update.c` in flatpak.
		 */
		transaction = _build_transaction (self, flatpak, GS_FLATPAK_ERROR_MODE_IGNORE_ERRORS, interactive, g_task_get_context (task), data->event_callback, data->event_user_data, cancellable, &local_error);
		if (transaction == NULL) {
			g_autoptr(GsPluginEvent) event = NULL;

			/* Reset the state of all the apps in this transaction. */
			for (guint i = 0; i < gs_app_list_length (list_tmp); i++) {
				GsApp *app = gs_app_list_index (list_tmp, i);
				gs_app_set_state_recover (app);
			}

			/* This can only fail if the repo doesn’t exist and can’t
			 * be created, which is unlikely. */
			gs_flatpak_error_convert (&local_error);

			if (data->event_callback != NULL) {
				event = gs_plugin_event_new ("error", local_error,
							     NULL);
				if (interactive)
					gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
				gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
				event_callback_invoke_take (self, g_steal_pointer (&event), g_task_get_context (task),
							    data->event_callback, data->event_user_data);
			}

			g_clear_error (&local_error);

			remove_schedule_entry (schedule_entry_handle);
			gs_flatpak_set_busy (flatpak, FALSE);

			continue;
		}

		for (guint i = 0; i < gs_app_list_length (list_tmp); i++) {
			GsApp *app = gs_app_list_index (list_tmp, i);
			g_autofree gchar *ref = NULL;

			ref = gs_flatpak_app_get_ref_display (app);
			if (flatpak_transaction_add_update (transaction, ref, NULL, NULL, &local_error)) {
				/* add to the transaction cache for quick look up -- other unrelated
				 * refs will be matched using gs_plugin_flatpak_find_app_by_ref() */
				gs_flatpak_transaction_add_app (transaction, app);

				continue;
			}

			/* Errors are not fatal, as otherwise a single app
			 * failure will take down the whole update, blocking
			 * updates for all other apps.
			 *
			 * The common two errors to see here are
			 *  - FLATPAK_ERROR_REMOTE_NOT_FOUND
			 *  - FLATPAK_ERROR_NOT_INSTALLED
			 */
			{
				g_autoptr(GsPluginEvent) event = NULL;

				g_warning ("Skipping update for ‘%s’: %s", ref, local_error->message);

				/* Reset the state of the app. */
				gs_app_set_state_recover (app);

				gs_flatpak_error_convert (&local_error);

				if (data->event_callback != NULL) {
					event = gs_plugin_event_new ("error", local_error,
								     "app", app,
								     NULL);
					if (interactive)
						gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
					gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
					event_callback_invoke_take (self, g_steal_pointer (&event), g_task_get_context (task),
								    data->event_callback, data->event_user_data);
				}

				g_clear_error (&local_error);
				continue;
			}
		}

		/* automatically clean up unused EOL runtimes when updating */
		flatpak_transaction_set_include_unused_uninstall_ops (transaction, TRUE);

		/* FIXME: Link progress reporting from #FlatpakTransaction
		 * up to `data->progress_callback`. */
		if (!gs_flatpak_transaction_run (transaction, cancellable, &local_error)) {
			g_autoptr(GError) prune_error = NULL;

			/* Reset the state of all the apps in this transaction. */
			for (guint i = 0; i < gs_app_list_length (list_tmp); i++) {
				GsApp *app = gs_app_list_index (list_tmp, i);
				gs_app_set_state_recover (app);
			}

			/* Try pruning the repo, just in case this is a failure
			 * caused by running out of disk space. The transaction
			 * typically won’t try this itself, and will only prune
			 * on success (if it knows an update has potentially
			 * left dangling objects). */
			if (!flatpak_installation_prune_local_repo (gs_flatpak_get_installation (flatpak, interactive),
								    NULL, &prune_error)) {
				gs_flatpak_error_convert (&prune_error);
				g_warning ("Error pruning flatpak repo for %s after failed update: %s",
					   gs_flatpak_get_id (flatpak), prune_error->message);
			}

			gs_flatpak_error_convert (&local_error);

			if (data->event_callback != NULL) {
				g_autoptr(GsPluginEvent) event = NULL;

				event = gs_plugin_event_new ("error", local_error,
							     NULL);
				if (interactive)
					gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
				gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);
				event_callback_invoke_take (self, g_steal_pointer (&event), g_task_get_context (task),
							    data->event_callback, data->event_user_data);
			}

			g_clear_error (&local_error);

			remove_schedule_entry (schedule_entry_handle);
			gs_flatpak_set_busy (flatpak, FALSE);

			continue;
		}

		remove_schedule_entry (schedule_entry_handle);
		gs_plugin_updates_changed (GS_PLUGIN (self));

		/* Get any new state. Ignore failure and fall through to
		 * refining the apps, since refreshing is not an entirely
		 * necessary part of the update operation. */
		if (!gs_flatpak_refresh (flatpak, G_MAXUINT, interactive, data->event_callback, data->event_user_data, cancellable, &local_error)) {
			gs_flatpak_error_convert (&local_error);
			g_warning ("Error refreshing flatpak data for ‘%s’ after update: %s",
				   gs_flatpak_get_id (flatpak), local_error->message);
			g_clear_error (&local_error);
		}

		/* Refine all the updated apps to make sure they’re up to date
		 * in the UI. Ignore failure since it’s not an entirely
		 * necessary part of the update operation. */
		for (guint i = 0; i < gs_app_list_length (list_tmp); i++) {
			GsApp *app = gs_app_list_index (list_tmp, i);
			g_autofree gchar *ref = NULL;

			ref = gs_flatpak_app_get_ref_display (app);
			if (!gs_flatpak_refine_app (flatpak, app,
						    GS_PLUGIN_REFINE_REQUIRE_FLAGS_RUNTIME,
						    interactive, TRUE,
						    data->event_callback, data->event_user_data,
						    cancellable, &local_error)) {
				gs_flatpak_error_convert (&local_error);
				g_warning ("Error refining app ‘%s’ after update: %s", ref, local_error->message);
				g_clear_error (&local_error);
				continue;
			}
		}

		gs_flatpak_set_busy (flatpak, FALSE);
	}

	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_flatpak_update_apps_finish (GsPlugin      *plugin,
                                      GAsyncResult  *result,
                                      GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static void
gs_flatpak_cover_addons_in_transaction (GsPluginFlatpak       *plugin,
                                        FlatpakTransaction    *transaction,
                                        GsApp                 *parent_app,
                                        GsAppState             state,
                                        gboolean               interactive,
                                        GMainContext          *callback_context,
                                        GsPluginEventCallback  event_callback,
                                        void                  *event_user_data)
{
	g_autoptr(GsAppList) addons = NULL;
	g_autoptr(GString) errors = NULL;
	guint ii, sz;

	g_return_if_fail (transaction != NULL);
	g_return_if_fail (GS_IS_APP (parent_app));

	addons = gs_app_dup_addons (parent_app);
	sz = addons ? gs_app_list_length (addons) : 0;

	for (ii = 0; ii < sz; ii++) {
		GsApp *addon = gs_app_list_index (addons, ii);
		g_autoptr(GError) local_error = NULL;

		if (state == GS_APP_STATE_INSTALLING && gs_app_get_to_be_installed (addon)) {
			g_autofree gchar *ref = NULL;

			ref = gs_flatpak_app_get_ref_display (addon);
			if (flatpak_transaction_add_install (transaction, gs_app_get_origin (addon), ref, NULL, &local_error)) {
				gs_app_set_state (addon, state);
			} else {
				if (errors)
					g_string_append_c (errors, '\n');
				else
					errors = g_string_new (NULL);
				g_string_append_printf (errors, _("Failed to add to install for addon ‘%s’: %s"),
					gs_app_get_name (addon), local_error->message);
			}
		} else if (state == GS_APP_STATE_REMOVING && gs_app_get_state (addon) == GS_APP_STATE_INSTALLED) {
			g_autofree gchar *ref = NULL;

			ref = gs_flatpak_app_get_ref_display (addon);
			if (flatpak_transaction_add_uninstall (transaction, ref, &local_error)) {
				gs_app_set_state (addon, state);
			} else {
				if (errors)
					g_string_append_c (errors, '\n');
				else
					errors = g_string_new (NULL);
				g_string_append_printf (errors, _("Failed to add to uninstall for addon ‘%s’: %s"),
					gs_app_get_name (addon), local_error->message);
			}
		}
	}

	if (errors && event_callback != NULL) {
		g_autoptr(GsPluginEvent) event = NULL;
		g_autoptr(GError) error_local = g_error_new_literal (GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED,
			errors->str);

		event = gs_plugin_event_new ("error", error_local,
					     NULL);
		if (interactive)
			gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
		gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);

		event_callback_invoke_take (plugin, g_steal_pointer (&event), callback_context,
					    event_callback, event_user_data);
	}
}

static void uninstall_apps_thread_cb (GTask        *task,
                                      gpointer      source_object,
                                      gpointer      task_data,
                                      GCancellable *cancellable);

static void
gs_plugin_flatpak_uninstall_apps_async (GsPlugin                           *plugin,
                                        GsAppList                          *apps,
                                        GsPluginUninstallAppsFlags          flags,
                                        GsPluginProgressCallback            progress_callback,
                                        gpointer                            progress_user_data,
                                        GsPluginEventCallback               event_callback,
                                        gpointer                            event_user_data,
                                        GsPluginAppNeedsUserActionCallback  app_needs_user_action_callback,
                                        gpointer                            app_needs_user_action_data,
                                        GCancellable                       *cancellable,
                                        GAsyncReadyCallback                 callback,
                                        gpointer                            user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = (flags & GS_PLUGIN_UNINSTALL_APPS_FLAGS_INTERACTIVE);

	task = gs_plugin_uninstall_apps_data_new_task (plugin, apps, flags,
						       progress_callback, progress_user_data,
						       event_callback, event_user_data,
						       app_needs_user_action_callback, app_needs_user_action_data,
						       cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_uninstall_apps_async);

	/* Queue a job to uninstall the apps. */
	gs_worker_thread_queue (self->long_running_worker, get_priority_for_interactivity (interactive),
				uninstall_apps_thread_cb, g_steal_pointer (&task));
}

/* Run in @worker. */
static void
uninstall_apps_thread_cb (GTask        *task,
                          gpointer      source_object,
                          gpointer      task_data,
                          GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsPluginUninstallAppsData *data = task_data;
	gboolean interactive = (data->flags & GS_PLUGIN_UNINSTALL_APPS_FLAGS_INTERACTIVE);
	g_autoptr(GHashTable) applist_by_flatpaks = NULL;
	GHashTableIter iter;
	gpointer key, value;
	g_autoptr(GError) local_error = NULL;

	assert_in_worker (self);

	/* Mark all the apps as pending uninstallation. While the op/progress
	 * handling code in #GsFlatpakTransaction does this more accurately and
	 * in more detail, we need to pre-emptively do it here, since multiple
	 * transactions are run sequentially below. That means that all the apps
	 * from the 2nd, 3rd, etc. transactions will not have their state
	 * updated until that transaction is prepared. That’s a long time for
	 * the apps to look like they’ve been left out of the uninstall in the UI. */
	applist_by_flatpaks = _group_apps_by_installation (self, data->apps);

	g_hash_table_iter_init (&iter, applist_by_flatpaks);
	while (g_hash_table_iter_next (&iter, NULL, &value)) {
		GsAppList *apps_for_installation = GS_APP_LIST (value);

		for (guint i = 0; i < gs_app_list_length (apps_for_installation); i++) {
			GsApp *app = gs_app_list_index (apps_for_installation, i);

			gs_app_set_state (app, GS_APP_STATE_REMOVING);
		}
	}

	/* build and run transaction for each flatpak installation */
	g_hash_table_iter_init (&iter, applist_by_flatpaks);
	while (g_hash_table_iter_next (&iter, &key, &value)) {
		GsFlatpak *flatpak = GS_FLATPAK (key);
		GsAppList *list_tmp = GS_APP_LIST (value);
		g_autoptr(FlatpakTransaction) transaction = NULL;

		g_assert (GS_IS_FLATPAK (flatpak));
		g_assert (list_tmp != NULL);
		g_assert (gs_app_list_length (list_tmp) > 0);

		gs_flatpak_set_busy (flatpak, TRUE);

		/* build */
		transaction = _build_transaction (self, flatpak, GS_FLATPAK_ERROR_MODE_STOP_ON_FIRST_ERROR, interactive, g_task_get_context (task), data->event_callback, data->event_user_data, cancellable, &local_error);
		if (transaction == NULL) {
			/* Reset the state of all the apps in this transaction. */
			for (guint i = 0; i < gs_app_list_length (list_tmp); i++) {
				GsApp *app = gs_app_list_index (list_tmp, i);
				gs_app_set_state_recover (app);
			}

			/* This can only fail if the repo doesn’t exist and can’t
			 * be created, which is unlikely. */
			gs_flatpak_error_convert (&local_error);

			if (data->event_callback != NULL) {
				g_autoptr(GsPluginEvent) event = NULL;

				event = gs_plugin_event_new ("error", local_error,
							     NULL);
				if (interactive)
					gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
				gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);

				event_callback_invoke_take (self, g_steal_pointer (&event), g_task_get_context (task),
							    data->event_callback, data->event_user_data);
			}

			g_clear_error (&local_error);

			gs_flatpak_set_busy (flatpak, FALSE);

			continue;
		}

		for (guint i = 0; i < gs_app_list_length (list_tmp); i++) {
			GsApp *app = gs_app_list_index (list_tmp, i);
			g_autofree char *ref = NULL;

			/* not supported */
			if (gs_plugin_flatpak_get_handler (self, app) == NULL)
				continue;

			/* is a source, handled by dedicated function */
			g_assert (gs_app_get_kind (app) != AS_COMPONENT_KIND_REPOSITORY);

			/* add to the transaction cache for quick look up -- other unrelated
			 * refs will be matched using gs_plugin_flatpak_find_app_by_ref() */
			gs_flatpak_transaction_add_app (transaction, app);

			/* add to the transaction */
			ref = gs_flatpak_app_get_ref_display (app);

			if (!flatpak_transaction_add_uninstall (transaction, ref, &local_error)) {
				/* Somehow, the app might already be uninstalled. */
				if (g_error_matches (local_error, FLATPAK_ERROR,
						     FLATPAK_ERROR_NOT_INSTALLED)) {
					g_clear_error (&local_error);
				}
			}

			/* Reset state if adding the app to the transaction failed. */
			if (local_error != NULL) {
				/* Reset the state of all the apps in this transaction. */
				for (guint j = 0; j < gs_app_list_length (list_tmp); j++) {
					GsApp *recover_app = gs_app_list_index (list_tmp, j);
					gs_app_set_state_recover (recover_app);
				}

				gs_flatpak_error_convert (&local_error);

				if (data->event_callback != NULL) {
					g_autoptr(GsPluginEvent) event = NULL;

					event = gs_plugin_event_new ("error", local_error,
								     NULL);
					if (interactive)
						gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
					gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);

					event_callback_invoke_take (self, g_steal_pointer (&event), g_task_get_context (task),
								    data->event_callback, data->event_user_data);
				}

				g_clear_error (&local_error);

				gs_flatpak_set_busy (flatpak, FALSE);

				continue;
			}

			gs_flatpak_cover_addons_in_transaction (self, transaction, app, GS_APP_STATE_REMOVING, interactive,
								g_task_get_context (task),
								data->event_callback,
								data->event_user_data);
		}

		/* run transaction */
		/* FIXME: Link progress reporting from #FlatpakTransaction
		 * up to `data->progress_callback`. */
		if (!gs_flatpak_transaction_run (transaction, cancellable, &local_error)) {
			GsApp *error_app = NULL;

			gs_flatpak_transaction_get_error_operation (GS_FLATPAK_TRANSACTION (transaction), &error_app);

			/* Reset the state of all the apps in this transaction. */
			for (guint i = 0; i < gs_app_list_length (list_tmp); i++) {
				GsApp *app = gs_app_list_index (list_tmp, i);
				gs_app_set_state_recover (app);
			}

			/* Somehow, the app might already be uninstalled. */
			if (g_error_matches (local_error, FLATPAK_ERROR,
					     FLATPAK_ERROR_NOT_INSTALLED)) {
				g_clear_error (&local_error);

				/* Set the app back to UNKNOWN so that refining it gets all the right details. */
				if (error_app != NULL) {
					g_debug ("App %s is already uninstalled", gs_app_get_unique_id (error_app));
					gs_app_set_state (error_app, GS_APP_STATE_UNKNOWN);
				}
			} else {
				gs_flatpak_error_convert (&local_error);

				if (data->event_callback != NULL) {
					g_autoptr(GsPluginEvent) event = NULL;

					event = gs_plugin_event_new ("error", local_error,
								     NULL);
					if (interactive)
						gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
					gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);

					event_callback_invoke_take (self, g_steal_pointer (&event), g_task_get_context (task),
								    data->event_callback, data->event_user_data);
				}

				g_clear_error (&local_error);

				gs_flatpak_set_busy (flatpak, FALSE);

				continue;
			}
		}

		/* Get any new state. Ignore failure and fall through to
		 * refining the apps, since refreshing is not an entirely
		 * necessary part of the uninstall operation. */
		if (!gs_flatpak_refresh (flatpak, G_MAXUINT, interactive, data->event_callback, data->event_user_data, cancellable, &local_error)) {
			gs_flatpak_error_convert (&local_error);
			g_debug ("Error refreshing flatpak data for ‘%s’ after uninstall: %s",
				 gs_flatpak_get_id (flatpak), local_error->message);
			g_clear_error (&local_error);
		}

		/* Refine all the uninstalled apps to make sure they’re up to date
		 * in the UI. Ignore failure since it’s not an entirely
		 * necessary part of the uninstall operation. */
		for (guint i = 0; i < gs_app_list_length (list_tmp); i++) {
			GsApp *app = gs_app_list_index (list_tmp, i);
			g_autofree gchar *ref = NULL;

			gs_app_set_size_download (app, GS_SIZE_TYPE_UNKNOWN, 0);
			gs_app_set_size_installed (app, GS_SIZE_TYPE_UNKNOWN, 0);

			ref = gs_flatpak_app_get_ref_display (app);
			if (!gs_flatpak_refine_app (flatpak, app,
						    GS_PLUGIN_REFINE_REQUIRE_FLAGS_ID |
						    GS_PLUGIN_REFINE_REQUIRE_FLAGS_ORIGIN |
						    GS_PLUGIN_REFINE_REQUIRE_FLAGS_SETUP_ACTION,
						    interactive, FALSE,
						    data->event_callback, data->event_user_data,
						    cancellable, &local_error)) {
				gs_flatpak_error_convert (&local_error);
				g_debug ("Error refining app ‘%s’ after uninstall: %s", ref, local_error->message);
				g_clear_error (&local_error);
				continue;
			}

			gs_flatpak_refine_addons (flatpak,
						  app,
						  GS_PLUGIN_REFINE_REQUIRE_FLAGS_ID,
						  GS_APP_STATE_REMOVING,
						  interactive,
						  data->event_callback,
						  data->event_user_data,
						  cancellable);
		}

		gs_flatpak_set_busy (flatpak, FALSE);
	}

	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_flatpak_uninstall_apps_finish (GsPlugin      *plugin,
                                         GAsyncResult  *result,
                                         GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static gboolean
app_has_local_source (GsApp *app)
{
	const gchar *url = gs_app_get_origin_hostname (app);

	if (gs_flatpak_app_get_file_kind (app) == GS_FLATPAK_APP_FILE_KIND_BUNDLE)
		return TRUE;

	if (gs_flatpak_app_get_file_kind (app) == GS_FLATPAK_APP_FILE_KIND_REF &&
	    g_strcmp0 (url, "localhost") == 0)
		return TRUE;

	return FALSE;
}

static void
gs_plugin_flatpak_ensure_scope (GsPlugin *plugin,
				GsApp *app)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);

	if (gs_app_get_scope (app) == AS_COMPONENT_SCOPE_UNKNOWN) {
		g_autoptr(GSettings) settings = g_settings_new ("org.gnome.software");

		/* get the new GsFlatpak for handling of local files */
		gs_app_set_scope (app, g_settings_get_boolean (settings, "install-bundles-system-wide") ?
					AS_COMPONENT_SCOPE_SYSTEM : AS_COMPONENT_SCOPE_USER);
		if (!self->has_system_helper) {
			g_info ("no flatpak system helper is available, using user");
			gs_app_set_scope (app, AS_COMPONENT_SCOPE_USER);
		}
		if (self->destdir_for_tests != NULL) {
			g_debug ("in self tests, using user");
			gs_app_set_scope (app, AS_COMPONENT_SCOPE_USER);
		}
	}
}

static void install_apps_thread_cb (GTask        *task,
                                    gpointer      source_object,
                                    gpointer      task_data,
                                    GCancellable *cancellable);

static void
gs_plugin_flatpak_install_apps_async (GsPlugin                           *plugin,
                                      GsAppList                          *apps,
                                      GsPluginInstallAppsFlags            flags,
                                      GsPluginProgressCallback            progress_callback,
                                      gpointer                            progress_user_data,
                                      GsPluginEventCallback               event_callback,
                                      void                               *event_user_data,
                                      GsPluginAppNeedsUserActionCallback  app_needs_user_action_callback,
                                      gpointer                            app_needs_user_action_data,
                                      GCancellable                       *cancellable,
                                      GAsyncReadyCallback                 callback,
                                      gpointer                            user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = (flags & GS_PLUGIN_INSTALL_APPS_FLAGS_INTERACTIVE);

	task = gs_plugin_install_apps_data_new_task (plugin, apps, flags,
						     progress_callback, progress_user_data,
						     event_callback, event_user_data,
						     app_needs_user_action_callback, app_needs_user_action_data,
						     cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_install_apps_async);

	/* Queue a job to install the apps. */
	gs_worker_thread_queue (self->long_running_worker, get_priority_for_interactivity (interactive),
				install_apps_thread_cb, g_steal_pointer (&task));
}

/* Run in @worker. */
static void
install_apps_thread_cb (GTask        *task,
                        gpointer      source_object,
                        gpointer      task_data,
                        GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsPlugin *plugin = GS_PLUGIN (self);
	GsPluginInstallAppsData *data = task_data;
	gboolean interactive = (data->flags & GS_PLUGIN_INSTALL_APPS_FLAGS_INTERACTIVE);
	g_autoptr(GHashTable) applist_by_flatpaks = NULL;
	GHashTableIter iter;
	gpointer key, value;
	g_autoptr(GError) local_error = NULL;

	assert_in_worker (self);

	/* Mark all the apps as pending installation. While the op/progress
	 * handling code in #GsFlatpakTransaction does this more accurately and
	 * in more detail, we need to pre-emptively do it here, since multiple
	 * transactions are run sequentially below. That means that all the apps
	 * from the 2nd, 3rd, etc. transactions will not have their state
	 * updated until that transaction is prepared. That’s a long time for
	 * the apps to look like they’ve been left out of the install in the UI. */
	applist_by_flatpaks = _group_apps_by_installation (self, data->apps);

	g_hash_table_iter_init (&iter, applist_by_flatpaks);
	while (g_hash_table_iter_next (&iter, NULL, &value)) {
		GsAppList *apps_for_installation = GS_APP_LIST (value);

		for (guint i = 0; i < gs_app_list_length (apps_for_installation); i++) {
			GsApp *app = gs_app_list_index (apps_for_installation, i);

			gs_app_set_state (app, GS_APP_STATE_INSTALLING);
		}
	}

	/* build and run transaction for each flatpak installation */
	g_hash_table_iter_init (&iter, applist_by_flatpaks);
	while (g_hash_table_iter_next (&iter, &key, &value)) {
		GsFlatpak *flatpak = GS_FLATPAK (key);
		GsAppList *list_tmp = GS_APP_LIST (value);
		g_autoptr(FlatpakTransaction) transaction = NULL;
		gpointer schedule_entry_handle = NULL;

		g_assert (GS_IS_FLATPAK (flatpak));
		g_assert (list_tmp != NULL);
		g_assert (gs_app_list_length (list_tmp) > 0);

		if (!interactive) {
			if (!(data->flags & GS_PLUGIN_INSTALL_APPS_FLAGS_NO_DOWNLOAD) &&
			    !gs_metered_block_app_list_on_download_scheduler (list_tmp, &schedule_entry_handle, cancellable, &local_error)) {
				g_warning ("Failed to block on download scheduler: %s",
					   local_error->message);
				g_clear_error (&local_error);
			}
		}

		gs_flatpak_set_busy (flatpak, TRUE);

		/* build */
		transaction = _build_transaction (self, flatpak, GS_FLATPAK_ERROR_MODE_STOP_ON_FIRST_ERROR, interactive, g_task_get_context (task), data->event_callback, data->event_user_data, cancellable, &local_error);
		if (transaction == NULL) {
			/* Reset the state of all the apps in this transaction. */
			for (guint i = 0; i < gs_app_list_length (list_tmp); i++) {
				GsApp *app = gs_app_list_index (list_tmp, i);
				gs_app_set_state_recover (app);
			}

			/* This can only fail if the repo doesn’t exist and can’t
			 * be created, which is unlikely. */
			gs_flatpak_error_convert (&local_error);

			if (data->event_callback != NULL) {
				g_autoptr(GsPluginEvent) event = NULL;

				event = gs_plugin_event_new ("error", local_error,
							     NULL);
				if (interactive)
					gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
				gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);

				event_callback_invoke_take (self, g_steal_pointer (&event), g_task_get_context (task),
							    data->event_callback, data->event_user_data);
			}

			g_clear_error (&local_error);

			remove_schedule_entry (schedule_entry_handle);
			gs_flatpak_set_busy (flatpak, FALSE);

			continue;
		}

		/* Apply flags to the transaction. */
		if (data->flags & GS_PLUGIN_INSTALL_APPS_FLAGS_NO_DOWNLOAD)
			flatpak_transaction_set_no_pull (transaction, TRUE);
		if (data->flags & GS_PLUGIN_INSTALL_APPS_FLAGS_NO_APPLY)
			flatpak_transaction_set_no_deploy (transaction, TRUE);

		for (guint i = 0; i < gs_app_list_length (list_tmp); i++) {
			GsApp *app = gs_app_list_index (list_tmp, i);

			/* queue for install if installation needs the network */
			if (!app_has_local_source (app) &&
			    !gs_plugin_get_network_available (plugin)) {
				gs_app_set_state (app, GS_APP_STATE_QUEUED_FOR_INSTALL);
				continue;
			}

			/* set the app scope */
			gs_plugin_flatpak_ensure_scope (plugin, app);

			/* not supported */
			if (gs_plugin_flatpak_get_handler (self, app) == NULL)
				continue;

			/* is a source, handled by dedicated function */
			g_assert (gs_app_get_kind (app) != AS_COMPONENT_KIND_REPOSITORY);

			/* add to the transaction cache for quick look up -- other unrelated
			 * refs will be matched using gs_plugin_flatpak_find_app_by_ref() */
			gs_flatpak_transaction_add_app (transaction, app);

			/* add flatpakref */
			if (gs_flatpak_app_get_file_kind (app) == GS_FLATPAK_APP_FILE_KIND_REF) {
				GFile *file = gs_app_get_local_file (app);

				if (file == NULL) {
					g_set_error (&local_error,
						     GS_PLUGIN_ERROR,
						     GS_PLUGIN_ERROR_NOT_SUPPORTED,
						     "no local file set for bundle %s",
						     gs_app_get_unique_id (app));
				} else {
					g_autoptr(GBytes) blob = g_file_load_bytes (file, cancellable, NULL, &local_error);

					if (blob != NULL)
						flatpak_transaction_add_install_flatpakref (transaction, blob, &local_error);
				}

			/* add bundle */
			} else if (gs_flatpak_app_get_file_kind (app) == GS_FLATPAK_APP_FILE_KIND_BUNDLE) {
				GFile *file = gs_app_get_local_file (app);

				if (file == NULL) {
					g_set_error (&local_error,
						     GS_PLUGIN_ERROR,
						     GS_PLUGIN_ERROR_NOT_SUPPORTED,
						     "no local file set for bundle %s",
						     gs_app_get_unique_id (app));
				} else {
					flatpak_transaction_add_install_bundle (transaction, file, NULL, &local_error);
				}

			/* add normal ref */
			} else {
				g_autofree gchar *ref = gs_flatpak_app_get_ref_display (app);

				if (!flatpak_transaction_add_install (transaction,
								      gs_app_get_origin (app),
								      ref, NULL, &local_error)) {
					/* Somehow, the app might already be installed. */
					if (g_error_matches (local_error, FLATPAK_ERROR,
							     FLATPAK_ERROR_ALREADY_INSTALLED)) {
						g_clear_error (&local_error);
					}
				}
			}

			/* Reset state if adding the app to the transaction failed. */
			if (local_error != NULL) {
				/* Reset the state of the failed app */
				gs_app_set_state_recover (app);

				gs_flatpak_error_convert (&local_error);

				if (data->event_callback != NULL) {
					g_autoptr(GsPluginEvent) event = NULL;

					event = gs_plugin_event_new ("error", local_error,
								     "app", app,
								     NULL);
					if (interactive)
						gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
					gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);

					event_callback_invoke_take (self, g_steal_pointer (&event), g_task_get_context (task),
								    data->event_callback, data->event_user_data);
				}

				g_clear_error (&local_error);

				continue;
			}

			gs_flatpak_cover_addons_in_transaction (self, transaction, app, GS_APP_STATE_INSTALLING, interactive,
								g_task_get_context (task),
								data->event_callback,
								data->event_user_data);
		}

		/* run transaction */
		/* FIXME: Link progress reporting from #FlatpakTransaction
		 * up to `data->progress_callback`. */
		if (!gs_flatpak_transaction_run (transaction, cancellable, &local_error)) {
			GsApp *error_app = NULL;

			gs_flatpak_transaction_get_error_operation (GS_FLATPAK_TRANSACTION (transaction), &error_app);

			/* Reset the state of all the apps in this transaction. */
			for (guint i = 0; i < gs_app_list_length (list_tmp); i++) {
				GsApp *app = gs_app_list_index (list_tmp, i);
				gs_app_set_state_recover (app);
			}

			/* Somehow, the app might already be installed. */
			if (g_error_matches (local_error, FLATPAK_ERROR,
					     FLATPAK_ERROR_ALREADY_INSTALLED)) {
				g_clear_error (&local_error);

				/* Set the app back to UNKNOWN so that refining it gets all the right details. */
				if (error_app != NULL) {
					g_debug ("App %s is already installed", gs_app_get_unique_id (error_app));
					gs_app_set_state (error_app, GS_APP_STATE_UNKNOWN);
				}
			} else {
				if (error_app != NULL &&
				    g_error_matches (local_error, FLATPAK_ERROR, FLATPAK_ERROR_REF_NOT_FOUND)) {
					const gchar *origin = gs_app_get_origin (error_app);
					if (origin != NULL) {
						g_autoptr(FlatpakRemote) remote = NULL;
						remote = flatpak_installation_get_remote_by_name (gs_flatpak_get_installation (flatpak, interactive),
												  origin, cancellable, NULL);
						if (remote != NULL) {
							g_autofree gchar *filter = flatpak_remote_get_filter (remote);
							if (filter != NULL && *filter != '\0') {
								/* It's a filtered remote, create a user friendly error message for it */
								g_autoptr(GError) error_tmp = NULL;
								g_set_error (&error_tmp, GS_PLUGIN_ERROR, GS_PLUGIN_ERROR_FAILED,
									     _("Remote “%s” doesn't allow install of “%s”, possibly due to its filter. Remove the filter and repeat the install. Detailed error: %s"),
									     flatpak_remote_get_title (remote),
									     gs_app_get_name (error_app),
									     local_error->message);
								g_clear_error (&local_error);
								local_error = g_steal_pointer (&error_tmp);
							}
						}
					}
				}

				gs_flatpak_error_convert (&local_error);

				if (data->event_callback != NULL) {
					g_autoptr(GsPluginEvent) event = NULL;

					event = gs_plugin_event_new ("error", local_error,
								     "app", error_app,
								     NULL);
					if (interactive)
						gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_INTERACTIVE);
					gs_plugin_event_add_flag (event, GS_PLUGIN_EVENT_FLAG_WARNING);

					event_callback_invoke_take (self, g_steal_pointer (&event), g_task_get_context (task),
								    data->event_callback, data->event_user_data);
				}

				g_clear_error (&local_error);

				remove_schedule_entry (schedule_entry_handle);
				gs_flatpak_set_busy (flatpak, FALSE);

				continue;
			}
		}

		remove_schedule_entry (schedule_entry_handle);

		/* Get any new state. Ignore failure and fall through to
		 * refining the apps, since refreshing is not an entirely
		 * necessary part of the install operation. */
		if (!(data->flags & GS_PLUGIN_INSTALL_APPS_FLAGS_NO_DOWNLOAD) &&
		    !gs_flatpak_refresh (flatpak, G_MAXUINT, interactive, data->event_callback, data->event_user_data, cancellable, &local_error)) {
			gs_flatpak_error_convert (&local_error);
			g_warning ("Error refreshing flatpak data for ‘%s’ after install: %s",
				   gs_flatpak_get_id (flatpak), local_error->message);
			g_clear_error (&local_error);
		}

		/* Refine all the installed apps to make sure they’re up to date
		 * in the UI. Ignore failure since it’s not an entirely
		 * necessary part of the install operation. */
		for (guint i = 0; i < gs_app_list_length (list_tmp); i++) {
			GsApp *app = gs_app_list_index (list_tmp, i);
			g_autofree gchar *ref = NULL;

			ref = gs_flatpak_app_get_ref_display (app);
			if (!gs_flatpak_refine_app (flatpak, app,
						    GS_PLUGIN_REFINE_REQUIRE_FLAGS_ID |
						    GS_PLUGIN_REFINE_REQUIRE_FLAGS_ORIGIN |
						    GS_PLUGIN_REFINE_REQUIRE_FLAGS_SETUP_ACTION,
						    interactive, FALSE,
						    data->event_callback, data->event_user_data,
						    cancellable, &local_error)) {
				gs_flatpak_error_convert (&local_error);
				g_warning ("Error refining app ‘%s’ after install: %s", ref, local_error->message);
				g_clear_error (&local_error);
				continue;
			}

			gs_flatpak_refine_addons (flatpak,
						  app,
						  GS_PLUGIN_REFINE_REQUIRE_FLAGS_ID,
						  GS_APP_STATE_INSTALLING,
						  interactive,
						  data->event_callback,
						  data->event_user_data,
						  cancellable);
		}

		gs_flatpak_set_busy (flatpak, FALSE);
	}

	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_flatpak_install_apps_finish (GsPlugin      *plugin,
                                       GAsyncResult  *result,
                                       GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static GsApp *
gs_plugin_flatpak_file_to_app_repo (GsPluginFlatpak  *self,
                                    GFile            *file,
                                    gboolean          interactive,
                                    GCancellable     *cancellable,
                                    GError          **error)
{
	g_autoptr(GsApp) app = NULL;

	/* parse the repo file */
	app = gs_flatpak_app_new_from_repo_file (file, cancellable, error);
	if (app == NULL)
		return NULL;

	/* already exists */
	for (guint i = 0; i < self->installations->len; i++) {
		GsFlatpak *flatpak = g_ptr_array_index (self->installations, i);
		g_autoptr(GError) error_local = NULL;
		g_autoptr(GsApp) app_tmp = NULL;
		app_tmp = gs_flatpak_find_repository_by_url (flatpak,
							     gs_flatpak_app_get_repo_url (app),
							     interactive,
							     cancellable, &error_local);
		if (app_tmp == NULL) {
			g_debug ("%s", error_local->message);
			continue;
		}
		if (g_strcmp0 (gs_flatpak_app_get_repo_filter (app), gs_flatpak_app_get_repo_filter (app_tmp)) != 0)
			continue;
		return g_steal_pointer (&app_tmp);
	}

	/* this is new */
	gs_app_set_management_plugin (app, GS_PLUGIN (self));
	return g_steal_pointer (&app);
}

static GsFlatpak *
gs_plugin_flatpak_create_temporary (GsPluginFlatpak  *self,
                                    GCancellable     *cancellable,
                                    GError          **error)
{
	g_autofree gchar *installation_path = NULL;
	g_autoptr(FlatpakInstallation) installation = NULL;
	g_autoptr(GFile) installation_file = NULL;

	/* create new per-user installation in a cache dir */
	installation_path = gs_utils_get_cache_filename ("flatpak",
							 "installation-tmp",
							 GS_UTILS_CACHE_FLAG_WRITEABLE |
							 GS_UTILS_CACHE_FLAG_ENSURE_EMPTY |
							 GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
							 error);
	if (installation_path == NULL)
		return NULL;
	installation_file = g_file_new_for_path (installation_path);
	installation = flatpak_installation_new_for_path (installation_file,
							  TRUE, /* user */
							  cancellable,
							  error);
	if (installation == NULL) {
		gs_flatpak_error_convert (error);
		return NULL;
	}
	return gs_flatpak_new (GS_PLUGIN (self), installation, GS_FLATPAK_FLAG_IS_TEMPORARY);
}

static GsApp *
gs_plugin_flatpak_file_to_app_bundle (GsPluginFlatpak  *self,
                                      GFile            *file,
                                      gboolean          interactive,
                                      GsApp            *alternate_of,
                                      GCancellable     *cancellable,
                                      GError          **error)
{
	g_autofree gchar *ref = NULL;
	g_autoptr(GsApp) app = NULL;
	g_autoptr(GsApp) app_tmp = NULL;
	g_autoptr(GsFlatpak) flatpak_tmp = NULL;
	GsApp *runtime;

	/* only use the temporary GsFlatpak to avoid the auth dialog */
	flatpak_tmp = gs_plugin_flatpak_create_temporary (self, cancellable, error);
	if (flatpak_tmp == NULL)
		return NULL;

	/* First make a quick GsApp to get the ref */
	app = gs_flatpak_file_to_app_bundle (flatpak_tmp, file, TRUE /* unrefined */,
					     interactive, cancellable, error);
	if (app == NULL)
		return NULL;

	/* is this already installed or available in a configured remote */
	ref = gs_flatpak_app_get_ref_display (app);
	app_tmp = gs_plugin_flatpak_find_app_by_ref (self, ref, interactive, alternate_of, cancellable, NULL);
	if (app_tmp != NULL)
		return g_steal_pointer (&app_tmp);

	/* If not installed/available, make a fully refined GsApp */
	g_clear_object (&app);
	app = gs_flatpak_file_to_app_bundle (flatpak_tmp, file, FALSE /* unrefined */,
					     interactive, cancellable, error);
	if (app == NULL)
		return NULL;

	/* force this to be 'any' scope for installation */
	gs_app_set_scope (app, AS_COMPONENT_SCOPE_UNKNOWN);

	runtime = gs_app_get_runtime (app);
	if (runtime != NULL)
		gs_app_set_scope (runtime, AS_COMPONENT_SCOPE_UNKNOWN);

	/* this is new */
	return g_steal_pointer (&app);
}

static GsApp *
gs_plugin_flatpak_file_to_app_ref (GsPluginFlatpak        *self,
                                   GFile                  *file,
                                   gboolean                interactive,
                                   GsApp                  *alternate_of,
                                   GsPluginEventCallback   event_callback,
                                   void                   *event_user_data,
                                   GCancellable           *cancellable,
                                   GError                **error)
{
	GsApp *runtime;
	g_autofree gchar *ref = NULL;
	g_autoptr(GsApp) app = NULL;
	g_autoptr(GsApp) app_tmp = NULL;
	g_autoptr(GsFlatpak) flatpak_tmp = NULL;

	/* only use the temporary GsFlatpak to avoid the auth dialog */
	flatpak_tmp = gs_plugin_flatpak_create_temporary (self, cancellable, error);
	if (flatpak_tmp == NULL)
		return NULL;

	/* First make a quick GsApp to get the ref */
	app = gs_flatpak_file_to_app_ref (flatpak_tmp, file, TRUE /* unrefined */,
					  interactive, event_callback, event_user_data, cancellable, error);
	if (app == NULL)
		return NULL;

	/* is this already installed or available in a configured remote */
	ref = gs_flatpak_app_get_ref_display (app);
	app_tmp = gs_plugin_flatpak_find_app_by_ref (self, ref, interactive, alternate_of, cancellable, NULL);
	if (app_tmp != NULL)
		return g_steal_pointer (&app_tmp);

	/* If not installed/available, make a fully refined GsApp */
	g_clear_object (&app);
	app = gs_flatpak_file_to_app_ref (flatpak_tmp, file, FALSE /* unrefined */,
					  interactive, event_callback, event_user_data, cancellable, error);
	if (app == NULL)
		return NULL;

	/* force this to be 'any' scope for installation */
	gs_app_set_scope (app, AS_COMPONENT_SCOPE_UNKNOWN);

	/* do we have a system runtime available */
	runtime = gs_app_get_runtime (app);
	if (runtime != NULL) {
		g_autoptr(GsApp) runtime_tmp = NULL;
		g_autofree gchar *runtime_ref = gs_flatpak_app_get_ref_display (runtime);
		runtime_tmp = gs_plugin_flatpak_find_app_by_ref (self,
								 runtime_ref,
								 interactive,
								 alternate_of,
								 cancellable,
								 NULL);
		if (runtime_tmp != NULL) {
			gs_app_set_runtime (app, runtime_tmp);
		} else {
			/* the new runtime is available from the RuntimeRepo */
			if (gs_flatpak_app_get_runtime_url (runtime) != NULL)
				gs_app_set_state (runtime, GS_APP_STATE_AVAILABLE);
		}
	}

	/* this is new */
	return g_steal_pointer (&app);
}

static GsApp * /* (transfer full) */
gs_plugin_flatpak_file_to_app (GsPluginFlatpak *self,
			       GFile *file,
			       gboolean interactive,
			       GsApp *alternate_of,
			       GsPluginEventCallback event_callback,
			       void *event_user_data,
			       GCancellable *cancellable,
			       GError **error)
{
	g_autofree gchar *content_type = NULL;
	GsApp *app = NULL;
	const gchar *mimetypes_bundle[] = {
		"application/vnd.flatpak",
		NULL };
	const gchar *mimetypes_repo[] = {
		"application/vnd.flatpak.repo",
		NULL };
	const gchar *mimetypes_ref[] = {
		"application/vnd.flatpak.ref",
		NULL };

	/* does this match any of the mimetypes we support */
	content_type = gs_utils_get_content_type (file, cancellable, error);
	if (content_type == NULL)
		return NULL;

	if (g_strv_contains (mimetypes_bundle, content_type))
		app = gs_plugin_flatpak_file_to_app_bundle (self, file, interactive, alternate_of, cancellable, error);
	else if (g_strv_contains (mimetypes_repo, content_type))
		app = gs_plugin_flatpak_file_to_app_repo (self, file, interactive, cancellable, error);
	else if (g_strv_contains (mimetypes_ref, content_type))
		app = gs_plugin_flatpak_file_to_app_ref (self, file, interactive, alternate_of, event_callback, event_user_data, cancellable, error);

	if (app != NULL) {
		GsApp *runtime = gs_app_get_runtime (app);
		/* Ensure the origin for the runtime is set */
		if (runtime != NULL && gs_app_get_origin (runtime) == NULL) {
			g_autoptr(GError) error_local = NULL;
			if (!gs_plugin_flatpak_refine_app (self, runtime, GS_PLUGIN_REFINE_REQUIRE_FLAGS_ORIGIN, interactive, event_callback, event_user_data, cancellable, &error_local))
				g_debug ("Failed to refine runtime: %s", error_local->message);
		}
		gs_plugin_flatpak_ensure_scope (GS_PLUGIN (self), app);
		/* It can return a cached app when the app is available in one of the remotes.
		   Cached apps cannot have set the local file property. */
		if (gs_plugin_cache_lookup (GS_PLUGIN (self), gs_app_get_unique_id (app)) != app)
			gs_app_set_local_file (app, file);
	}

	return app;
}

static void
file_to_app_thread_cb (GTask *task,
		       gpointer source_object,
		       gpointer task_data,
		       GCancellable *cancellable)
{
	g_autoptr(GsApp) app = NULL;
	g_autoptr(GError) local_error = NULL;
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsPluginFileToAppData *data = task_data;
	gboolean interactive = (data->flags & GS_PLUGIN_FILE_TO_APP_FLAGS_INTERACTIVE) != 0;

	app = gs_plugin_flatpak_file_to_app (self, data->file, interactive, NULL, data->event_callback, data->event_user_data, cancellable, &local_error);
	if (app != NULL) {
		g_autoptr(GsAppList) list = gs_app_list_new ();
		gs_app_list_add (list, app);
		g_task_return_pointer (task, g_steal_pointer (&list), g_object_unref);
	} else if (local_error != NULL) {
		g_task_return_error (task, g_steal_pointer (&local_error));
	} else {
		g_task_return_pointer (task, gs_app_list_new (), g_object_unref);
	}
}

static void
gs_plugin_flatpak_file_to_app_async (GsPlugin *plugin,
				     GFile *file,
				     GsPluginFileToAppFlags flags,
				     GsPluginEventCallback event_callback,
				     void *event_user_data,
				     GCancellable *cancellable,
				     GAsyncReadyCallback callback,
				     gpointer user_data)
{
	g_autoptr(GTask) task = NULL;
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	gboolean interactive = (flags & GS_PLUGIN_FILE_TO_APP_FLAGS_INTERACTIVE) != 0;

	task = gs_plugin_file_to_app_data_new_task (plugin, file, flags, event_callback, event_user_data, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_file_to_app_async);

	/* Queue a job to get the apps. */
	gs_worker_thread_queue (self->worker, get_priority_for_interactivity (interactive),
				file_to_app_thread_cb, g_steal_pointer (&task));
}

static GsAppList *
gs_plugin_flatpak_file_to_app_finish (GsPlugin      *plugin,
				      GAsyncResult  *result,
				      GError       **error)
{
	return g_task_propagate_pointer (G_TASK (result), error);
}

static void refine_categories_thread_cb (GTask        *task,
                                         gpointer      source_object,
                                         gpointer      task_data,
                                         GCancellable *cancellable);

static void
gs_plugin_flatpak_refine_categories_async (GsPlugin                      *plugin,
                                           GPtrArray                     *list,
                                           GsPluginRefineCategoriesFlags  flags,
                                           GsPluginEventCallback          event_callback,
                                           void                          *event_user_data,
                                           GCancellable                  *cancellable,
                                           GAsyncReadyCallback            callback,
                                           gpointer                       user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = (flags & GS_PLUGIN_REFINE_CATEGORIES_FLAGS_INTERACTIVE);

	task = gs_plugin_refine_categories_data_new_task (plugin, list, flags,
							  event_callback, event_user_data,
							  cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_refine_categories_async);

	/* All we actually do is add the sizes of each category. If that’s
	 * not been requested, avoid queueing a worker job. */
	if (!(flags & GS_PLUGIN_REFINE_CATEGORIES_FLAGS_SIZE)) {
		g_task_return_boolean (task, TRUE);
		return;
	}

	/* Queue a job to get the apps. */
	gs_worker_thread_queue (self->worker, get_priority_for_interactivity (interactive),
				refine_categories_thread_cb, g_steal_pointer (&task));
}

/* Run in @worker. */
static void
refine_categories_thread_cb (GTask        *task,
                             gpointer      source_object,
                             gpointer      task_data,
                             GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsPluginRefineCategoriesData *data = task_data;
	gboolean interactive = (data->flags & GS_PLUGIN_REFINE_CATEGORIES_FLAGS_INTERACTIVE);
	g_autoptr(GError) local_error = NULL;

	assert_in_worker (self);

	for (guint i = 0; i < self->installations->len; i++) {
		GsFlatpak *flatpak = g_ptr_array_index (self->installations, i);

		if (!gs_flatpak_refine_category_sizes (flatpak, data->list, interactive, data->event_callback, data->event_user_data, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}
	}

	g_task_return_boolean (task, TRUE);
}

static gboolean
gs_plugin_flatpak_refine_categories_finish (GsPlugin      *plugin,
                                            GAsyncResult  *result,
                                            GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static void list_apps_thread_cb (GTask        *task,
                                 gpointer      source_object,
                                 gpointer      task_data,
                                 GCancellable *cancellable);

static void
gs_plugin_flatpak_list_apps_async (GsPlugin              *plugin,
                                   GsAppQuery            *query,
                                   GsPluginListAppsFlags  flags,
                                   GsPluginEventCallback  event_callback,
                                   void                  *event_user_data,
                                   GCancellable          *cancellable,
                                   GAsyncReadyCallback    callback,
                                   gpointer               user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = (flags & GS_PLUGIN_LIST_APPS_FLAGS_INTERACTIVE);

	task = gs_plugin_list_apps_data_new_task (plugin, query, flags,
						  event_callback, event_user_data,
						  cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_list_apps_async);

	/* Queue a job to get the apps. */
	gs_worker_thread_queue (self->worker, get_priority_for_interactivity (interactive),
				list_apps_thread_cb, g_steal_pointer (&task));
}

/* Run in @worker. */
static void
list_apps_thread_cb (GTask        *task,
                     gpointer      source_object,
                     gpointer      task_data,
                     GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	g_autoptr(GsAppList) list = gs_app_list_new ();
	GsPluginListAppsData *data = task_data;
	gboolean interactive = (data->flags & GS_PLUGIN_LIST_APPS_FLAGS_INTERACTIVE);
	GsPluginEventCallback event_callback = data->event_callback;
	void *event_user_data = data->event_user_data;
	GDateTime *released_since = NULL;
	GsAppQueryTristate is_curated = GS_APP_QUERY_TRISTATE_UNSET;
	GsAppQueryTristate is_featured = GS_APP_QUERY_TRISTATE_UNSET;
	GsCategory *category = NULL;
	GsAppQueryTristate is_installed = GS_APP_QUERY_TRISTATE_UNSET;
	GsAppQueryTristate is_for_update = GS_APP_QUERY_TRISTATE_UNSET;
	const AsComponentKind *component_kinds = NULL;
	guint64 age_secs = 0;
	const gchar * const *deployment_featured = NULL;
	const gchar *const *developers = NULL;
	const gchar * const *keywords = NULL;
	GsApp *alternate_of = NULL;
	const gchar *provides_tag = NULL;
	GsAppQueryProvidesType provides_type = GS_APP_QUERY_PROVIDES_UNKNOWN;
	g_autoptr(GError) local_error = NULL;

	assert_in_worker (self);

	if (data->query != NULL) {
		released_since = gs_app_query_get_released_since (data->query);
		is_curated = gs_app_query_get_is_curated (data->query);
		is_featured = gs_app_query_get_is_featured (data->query);
		category = gs_app_query_get_category (data->query);
		is_installed = gs_app_query_get_is_installed (data->query);
		deployment_featured = gs_app_query_get_deployment_featured (data->query);
		developers = gs_app_query_get_developers (data->query);
		keywords = gs_app_query_get_keywords (data->query);
		alternate_of = gs_app_query_get_alternate_of (data->query);
		provides_type = gs_app_query_get_provides (data->query, &provides_tag);
		is_for_update = gs_app_query_get_is_for_update (data->query);
		component_kinds = gs_app_query_get_component_kinds (data->query);
	}

	if (released_since != NULL) {
		g_autoptr(GDateTime) now = g_date_time_new_now_local ();
		age_secs = g_date_time_difference (now, released_since) / G_TIME_SPAN_SECOND;
	}

	/* Currently only support a subset of query properties, and only one set at once.
	 * Also don’t currently support GS_APP_QUERY_TRISTATE_FALSE. */
	if ((released_since == NULL &&
	     is_curated == GS_APP_QUERY_TRISTATE_UNSET &&
	     is_featured == GS_APP_QUERY_TRISTATE_UNSET &&
	     category == NULL &&
	     is_installed == GS_APP_QUERY_TRISTATE_UNSET &&
	     deployment_featured == NULL &&
	     developers == NULL &&
	     keywords == NULL &&
	     alternate_of == NULL &&
	     provides_tag == NULL &&
	     is_for_update == GS_APP_QUERY_TRISTATE_UNSET &&
	     component_kinds == NULL) ||
	    is_curated == GS_APP_QUERY_TRISTATE_FALSE ||
	    is_featured == GS_APP_QUERY_TRISTATE_FALSE ||
	    is_installed == GS_APP_QUERY_TRISTATE_FALSE ||
	    is_for_update == GS_APP_QUERY_TRISTATE_FALSE ||
	    (component_kinds != NULL && !gs_component_kind_array_contains (component_kinds, AS_COMPONENT_KIND_REPOSITORY)) ||
	    gs_app_query_get_n_properties_set (data->query) != 1) {
		g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
					 "Unsupported query");
		return;
	}

	if (alternate_of != NULL &&
	    gs_app_get_bundle_kind (alternate_of) == AS_BUNDLE_KIND_FLATPAK &&
	    gs_app_get_scope (alternate_of) != AS_COMPONENT_SCOPE_UNKNOWN &&
	    gs_app_get_local_file (alternate_of) != NULL) {
		g_autoptr(GsApp) app = NULL;
		GFile *file = gs_app_get_local_file (alternate_of);
		app = gs_plugin_flatpak_file_to_app (self, file, interactive, alternate_of, event_callback, event_user_data, cancellable, NULL);
		if (app != NULL && app != alternate_of) {
			gs_app_set_local_file (app, file);
			if (gs_app_get_scope (alternate_of) == AS_COMPONENT_SCOPE_SYSTEM)
				gs_app_set_scope (app, AS_COMPONENT_SCOPE_USER);
			else
				gs_app_set_scope (app, AS_COMPONENT_SCOPE_SYSTEM);
			/* ensure both are considered */
			gs_app_list_add (list, alternate_of);
			gs_app_list_add (list, app);
		}
	}

	for (guint i = 0; i < self->installations->len; i++) {
		GsFlatpak *flatpak = g_ptr_array_index (self->installations, i);
		const gchar * const provides_tag_strv[2] = { provides_tag, NULL };

		if (released_since != NULL &&
		    !gs_flatpak_add_recent (flatpak, list, age_secs, interactive, event_callback, event_user_data, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}

		if (is_curated != GS_APP_QUERY_TRISTATE_UNSET &&
		    !gs_flatpak_add_popular (flatpak, list, interactive, event_callback, event_user_data, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}

		if (is_featured != GS_APP_QUERY_TRISTATE_UNSET &&
		    !gs_flatpak_add_featured (flatpak, list, interactive, event_callback, event_user_data, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}

		if (category != NULL &&
		    !gs_flatpak_add_category_apps (flatpak, category, list, interactive, event_callback, event_user_data, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}

		if (is_installed != GS_APP_QUERY_TRISTATE_UNSET &&
		    !gs_flatpak_add_installed (flatpak, list, interactive, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}

		if (deployment_featured != NULL &&
		    !gs_flatpak_add_deployment_featured (flatpak, list, interactive, event_callback, event_user_data, deployment_featured, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}

		if (developers != NULL &&
		    !gs_flatpak_search_developer_apps (flatpak, developers, list, interactive, event_callback, event_user_data, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}

		if (keywords != NULL &&
		    !gs_flatpak_search (flatpak, keywords, list, interactive, event_callback, event_user_data, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}

		if (alternate_of != NULL &&
		    !gs_flatpak_add_alternates (flatpak, alternate_of, list, interactive, event_callback, event_user_data, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}

		/* The @provides_type is deliberately ignored here, as flatpak
		 * wants to try and match anything. This could be changed in
		 * future. */
		if (provides_tag != NULL &&
		    provides_type != GS_APP_QUERY_PROVIDES_UNKNOWN &&
		    !gs_flatpak_search (flatpak, provides_tag_strv, list, interactive, event_callback, event_user_data, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}

		if (is_for_update == GS_APP_QUERY_TRISTATE_TRUE) {
			g_autoptr(GError) local_error2 = NULL;
			if (!gs_flatpak_add_updates (flatpak, list, interactive, event_callback, event_user_data, cancellable, &local_error2))
				g_debug ("Failed to get updates for '%s': %s", gs_flatpak_get_id (flatpak), local_error2->message);
		}

		if (gs_component_kind_array_contains (component_kinds, AS_COMPONENT_KIND_REPOSITORY) &&
		    !gs_flatpak_add_repositories (flatpak, list, interactive, event_callback, event_user_data, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}
	}

	if (is_for_update == GS_APP_QUERY_TRISTATE_TRUE)
		gs_plugin_cache_lookup_by_state (GS_PLUGIN (self), list, GS_APP_STATE_INSTALLING);

	g_task_return_pointer (task, g_steal_pointer (&list), g_object_unref);
}

static GsAppList *
gs_plugin_flatpak_list_apps_finish (GsPlugin      *plugin,
                                    GAsyncResult  *result,
                                    GError       **error)
{
	return g_task_propagate_pointer (G_TASK (result), error);
}

typedef struct {
	gboolean interactive;
	GsPluginEventCallback event_callback;
	void *event_user_data;
	GFile *cache_file;  /* (owned) (not nullable) */
} UrlToAppDownloadData;

static void
url_to_app_download_data_free (UrlToAppDownloadData *data)
{
	g_clear_object (&data->cache_file);
	g_free (data);
}

G_DEFINE_AUTOPTR_CLEANUP_FUNC (UrlToAppDownloadData, url_to_app_download_data_free)

static void url_to_app_download_cb (GObject      *object,
                                    GAsyncResult *result,
                                    gpointer      user_data);
static void url_to_app_file_cb (GObject      *object,
                                GAsyncResult *result,
                                gpointer      user_data);

static void
url_to_app_thread_cb (GTask *task,
		      gpointer source_object,
		      gpointer task_data,
		      GCancellable *cancellable)
{
	g_autoptr(GsAppList) list = gs_app_list_new ();
	g_autoptr(GError) local_error = NULL;
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsPluginUrlToAppData *data = task_data;
	gboolean interactive = (data->flags & GS_PLUGIN_URL_TO_APP_FLAGS_INTERACTIVE) != 0;

	for (guint i = 0; i < self->installations->len; i++) {
		GsFlatpak *flatpak = g_ptr_array_index (self->installations, i);
		if (!gs_flatpak_url_to_app (flatpak, list, data->url, interactive, data->event_callback, data->event_user_data, cancellable, &local_error)) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}
	}

	g_task_return_pointer (task, g_steal_pointer (&list), g_object_unref);
}

static void
gs_plugin_flatpak_url_to_app_async (GsPlugin *plugin,
				    const gchar *url,
				    GsPluginUrlToAppFlags flags,
				    GsPluginEventCallback event_callback,
				    void *event_user_data,
				    GCancellable *cancellable,
				    GAsyncReadyCallback callback,
				    gpointer user_data)
{
	g_autoptr(GTask) task = NULL;
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	gboolean interactive = (flags & GS_PLUGIN_URL_TO_APP_FLAGS_INTERACTIVE) != 0;
	g_autofree char *scheme = NULL;

	task = gs_plugin_url_to_app_data_new_task (plugin, url, flags, event_callback, event_user_data, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_url_to_app_async);

	/* Firstly, try and support `flatpak+https` URIs. This needs to be done
	 * at the #GsPluginFlatpak level rather than the #GsFlatpak level,
	 * because we need to hand off to the plugin’s file-to-app code.
	 *
	 * The flatpak+https URI scheme points towards a .flatpakref file which
	 * we can download and then treat like a normal local file. This code
	 * actually also supports the URI pointing at a bundle or a repo file,
	 * since that also seems sensible to support.
	 *
	 * https://gitlab.gnome.org/GNOME/gnome-software/-/issues/2240#note_1787991 */
	scheme = gs_utils_get_url_scheme (url);

	if (g_strcmp0 (scheme, "flatpak+https") == 0) {
		g_autofree gchar *cache_filename = NULL;
		g_autoptr(GFile) cache_file = NULL;
		g_autoptr(SoupSession) soup_session = NULL;
		g_autoptr(GError) local_error = NULL;
		g_autoptr(UrlToAppDownloadData) data = NULL;

		/* Download and cache the file. */
		cache_filename = gs_utils_get_cache_filename ("flatpak-downloaded-refs",
							      url,
							      GS_UTILS_CACHE_FLAG_WRITEABLE |
							      GS_UTILS_CACHE_FLAG_CREATE_DIRECTORY,
							      &local_error);
		if (cache_filename == NULL) {
			g_task_return_error (task, g_steal_pointer (&local_error));
			return;
		}

		cache_file = g_file_new_for_path (cache_filename);
		soup_session = gs_build_soup_session ();
		if (self->cache_files_to_delete == NULL)
			self->cache_files_to_delete = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
		g_ptr_array_add (self->cache_files_to_delete, g_object_ref (cache_file));

		data = g_new0 (UrlToAppDownloadData, 1);
		data->interactive = interactive;
		data->event_callback = event_callback;
		data->event_user_data = event_user_data;
		data->cache_file = g_object_ref (cache_file);
		g_task_set_task_data (task, g_steal_pointer (&data), (GDestroyNotify) url_to_app_download_data_free);

		gs_download_file_async (soup_session,
					url + strlen ("flatpak+"),
					cache_file,
					G_PRIORITY_DEFAULT,
					NULL, NULL,  /* progress */
					cancellable,
					url_to_app_download_cb,
					g_steal_pointer (&task));
	} else {
		/* If it’s not a file we need to download, queue a job to get the apps. */
		gs_worker_thread_queue (self->worker, get_priority_for_interactivity (interactive),
					url_to_app_thread_cb, g_steal_pointer (&task));
	}
}

static void
url_to_app_download_cb (GObject      *object,
                        GAsyncResult *result,
                        gpointer      user_data)
{
	SoupSession *soup_session = SOUP_SESSION (object);
	g_autoptr(GTask) task = g_steal_pointer (&user_data);
	GsPluginFlatpak *self = g_task_get_source_object (task);
	UrlToAppDownloadData *data = g_task_get_task_data (task);
	GCancellable *cancellable = g_task_get_cancellable (task);
	g_autoptr(GError) local_error = NULL;

	if (!gs_download_file_finish (soup_session, result, &local_error) &&
	    !g_error_matches (local_error, GS_DOWNLOAD_ERROR, GS_DOWNLOAD_ERROR_NOT_MODIFIED)) {
		g_task_return_error (task, g_steal_pointer (&local_error));
		return;
	}

	g_clear_error (&local_error);

	/* Now load and display the downloaded flatpakref file. */
	gs_plugin_flatpak_file_to_app_async (GS_PLUGIN (self),
					     data->cache_file,
					     data->interactive ? GS_PLUGIN_FILE_TO_APP_FLAGS_INTERACTIVE : GS_PLUGIN_FILE_TO_APP_FLAGS_NONE,
					     data->event_callback,
					     data->event_user_data,
					     cancellable,
					     url_to_app_file_cb, g_steal_pointer (&task));
}

static void
url_to_app_file_cb (GObject      *object,
                    GAsyncResult *result,
                    gpointer      user_data)
{
	g_autoptr(GTask) task = g_steal_pointer (&user_data);
	GsPluginFlatpak *self = g_task_get_source_object (task);
	g_autoptr(GsAppList) list = NULL;
	g_autoptr(GError) local_error = NULL;

	list = gs_plugin_flatpak_file_to_app_finish (GS_PLUGIN (self), result, &local_error);

	if (list != NULL)
		g_task_return_pointer (task, g_steal_pointer (&list), g_object_unref);
	else
		g_task_return_error (task, g_steal_pointer (&local_error));
}

static GsAppList *
gs_plugin_flatpak_url_to_app_finish (GsPlugin      *plugin,
				     GAsyncResult  *result,
				     GError       **error)
{
	return g_task_propagate_pointer (G_TASK (result), error);
}

static void install_repository_thread_cb (GTask        *task,
					  gpointer      source_object,
					  gpointer      task_data,
					  GCancellable *cancellable);

static void
gs_plugin_flatpak_install_repository_async (GsPlugin                     *plugin,
					    GsApp			 *repository,
                                            GsPluginManageRepositoryFlags flags,
                                            GsPluginEventCallback         event_callback,
                                            void                         *event_user_data,
                                            GCancellable		 *cancellable,
                                            GAsyncReadyCallback		  callback,
                                            gpointer			  user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = (flags & GS_PLUGIN_MANAGE_REPOSITORY_FLAGS_INTERACTIVE);

	task = gs_plugin_manage_repository_data_new_task (plugin, repository, flags, event_callback, event_user_data, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_install_repository_async);

	/* only process this app if was created by this plugin */
	if (!gs_app_has_management_plugin (repository, plugin)) {
		g_task_return_boolean (task, TRUE);
		return;
	}

	/* is a source */
	g_assert (gs_app_get_kind (repository) == AS_COMPONENT_KIND_REPOSITORY);

	gs_worker_thread_queue (self->long_running_worker, get_priority_for_interactivity (interactive),
				install_repository_thread_cb, g_steal_pointer (&task));
}

/* Run in @worker. */
static void
install_repository_thread_cb (GTask        *task,
			      gpointer      source_object,
			      gpointer      task_data,
			      GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsFlatpak *flatpak;
	GsPluginManageRepositoryData *data = task_data;
	gboolean interactive = (data->flags & GS_PLUGIN_MANAGE_REPOSITORY_FLAGS_INTERACTIVE);
	g_autoptr(GError) local_error = NULL;

	assert_in_worker (self);

	/* queue for install if installation needs the network */
	if (!app_has_local_source (data->repository) &&
	    !gs_plugin_get_network_available (GS_PLUGIN (self))) {
		gs_app_set_state (data->repository, GS_APP_STATE_QUEUED_FOR_INSTALL);
		g_task_return_boolean (task, TRUE);
		return;
	}

	gs_plugin_flatpak_ensure_scope (GS_PLUGIN (self), data->repository);

	flatpak = gs_plugin_flatpak_get_handler (self, data->repository);
	if (flatpak == NULL) {
		g_task_return_boolean (task, TRUE);
		return;
	}

	if (gs_flatpak_add_repository_app (flatpak, data->repository, TRUE, interactive, cancellable, &local_error))
		g_task_return_boolean (task, TRUE);
	else
		g_task_return_error (task, g_steal_pointer (&local_error));
}

static gboolean
gs_plugin_flatpak_install_repository_finish (GsPlugin      *plugin,
					     GAsyncResult  *result,
					     GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static void remove_repository_thread_cb (GTask        *task,
					 gpointer      source_object,
					 gpointer      task_data,
					 GCancellable *cancellable);

static void
gs_plugin_flatpak_remove_repository_async (GsPlugin                     *plugin,
					   GsApp			*repository,
                                           GsPluginManageRepositoryFlags flags,
                                           GsPluginEventCallback         event_callback,
                                           void                         *event_user_data,
                                           GCancellable		 	*cancellable,
                                           GAsyncReadyCallback		 callback,
                                           gpointer			 user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = (flags & GS_PLUGIN_MANAGE_REPOSITORY_FLAGS_INTERACTIVE);

	task = gs_plugin_manage_repository_data_new_task (plugin, repository, flags, event_callback, event_user_data, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_remove_repository_async);

	/* only process this app if was created by this plugin */
	if (!gs_app_has_management_plugin (repository, plugin)) {
		g_task_return_boolean (task, TRUE);
		return;
	}

	/* is a source */
	g_assert (gs_app_get_kind (repository) == AS_COMPONENT_KIND_REPOSITORY);

	gs_worker_thread_queue (self->long_running_worker, get_priority_for_interactivity (interactive),
				remove_repository_thread_cb, g_steal_pointer (&task));
}

/* Run in @worker. */
static void
remove_repository_thread_cb (GTask        *task,
			     gpointer      source_object,
			     gpointer      task_data,
			     GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsFlatpak *flatpak;
	GsPluginManageRepositoryData *data = task_data;
	gboolean interactive = (data->flags & GS_PLUGIN_MANAGE_REPOSITORY_FLAGS_INTERACTIVE);
	g_autoptr(GError) local_error = NULL;

	assert_in_worker (self);

	flatpak = gs_plugin_flatpak_get_handler (self, data->repository);
	if (flatpak == NULL) {
		g_task_return_boolean (task, TRUE);
		return;
	}

	if (gs_flatpak_remove_repository_app (flatpak, data->repository, TRUE, interactive, cancellable, &local_error))
		g_task_return_boolean (task, TRUE);
	else
		g_task_return_error (task, g_steal_pointer (&local_error));
}

static gboolean
gs_plugin_flatpak_remove_repository_finish (GsPlugin      *plugin,
					    GAsyncResult  *result,
					    GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static void enable_repository_thread_cb (GTask        *task,
					 gpointer      source_object,
					 gpointer      task_data,
					 GCancellable *cancellable);

static void
gs_plugin_flatpak_enable_repository_async (GsPlugin                     *plugin,
					   GsApp			*repository,
                                           GsPluginManageRepositoryFlags flags,
                                           GsPluginEventCallback         event_callback,
                                           void                         *event_user_data,
                                           GCancellable		 	*cancellable,
                                           GAsyncReadyCallback		 callback,
                                           gpointer			 user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = (flags & GS_PLUGIN_MANAGE_REPOSITORY_FLAGS_INTERACTIVE);

	task = gs_plugin_manage_repository_data_new_task (plugin, repository, flags, event_callback, event_user_data, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_enable_repository_async);

	/* only process this app if was created by this plugin */
	if (!gs_app_has_management_plugin (repository, plugin)) {
		g_task_return_boolean (task, TRUE);
		return;
	}

	/* is a source */
	g_assert (gs_app_get_kind (repository) == AS_COMPONENT_KIND_REPOSITORY);

	gs_worker_thread_queue (self->long_running_worker, get_priority_for_interactivity (interactive),
				enable_repository_thread_cb, g_steal_pointer (&task));
}

/* Run in @worker. */
static void
enable_repository_thread_cb (GTask        *task,
			     gpointer      source_object,
			     gpointer      task_data,
			     GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsFlatpak *flatpak;
	GsPluginManageRepositoryData *data = task_data;
	gboolean interactive = (data->flags & GS_PLUGIN_MANAGE_REPOSITORY_FLAGS_INTERACTIVE);
	g_autoptr(GError) local_error = NULL;

	assert_in_worker (self);

	flatpak = gs_plugin_flatpak_get_handler (self, data->repository);
	if (flatpak == NULL) {
		g_task_return_boolean (task, TRUE);
		return;
	}

	if (gs_flatpak_add_repository_app (flatpak, data->repository, FALSE, interactive, cancellable, &local_error))
		g_task_return_boolean (task, TRUE);
	else
		g_task_return_error (task, g_steal_pointer (&local_error));
}

static gboolean
gs_plugin_flatpak_enable_repository_finish (GsPlugin      *plugin,
					    GAsyncResult  *result,
					    GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static void disable_repository_thread_cb (GTask        *task,
					  gpointer      source_object,
					  gpointer      task_data,
					  GCancellable *cancellable);

static void
gs_plugin_flatpak_disable_repository_async (GsPlugin                     *plugin,
					    GsApp			 *repository,
                                            GsPluginManageRepositoryFlags flags,
                                            GsPluginEventCallback         event_callback,
                                            void                         *event_user_data,
                                            GCancellable	 	 *cancellable,
                                            GAsyncReadyCallback		  callback,
                                            gpointer			  user_data)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (plugin);
	g_autoptr(GTask) task = NULL;
	gboolean interactive = (flags & GS_PLUGIN_MANAGE_REPOSITORY_FLAGS_INTERACTIVE);

	task = gs_plugin_manage_repository_data_new_task (plugin, repository, flags, event_callback, event_user_data, cancellable, callback, user_data);
	g_task_set_source_tag (task, gs_plugin_flatpak_disable_repository_async);

	/* only process this app if was created by this plugin */
	if (!gs_app_has_management_plugin (repository, plugin)) {
		g_task_return_boolean (task, TRUE);
		return;
	}

	/* is a source */
	g_assert (gs_app_get_kind (repository) == AS_COMPONENT_KIND_REPOSITORY);

	gs_worker_thread_queue (self->long_running_worker, get_priority_for_interactivity (interactive),
				disable_repository_thread_cb, g_steal_pointer (&task));
}

/* Run in @worker. */
static void
disable_repository_thread_cb (GTask        *task,
			      gpointer      source_object,
			      gpointer      task_data,
			      GCancellable *cancellable)
{
	GsPluginFlatpak *self = GS_PLUGIN_FLATPAK (source_object);
	GsFlatpak *flatpak;
	GsPluginManageRepositoryData *data = task_data;
	gboolean interactive = (data->flags & GS_PLUGIN_MANAGE_REPOSITORY_FLAGS_INTERACTIVE);
	g_autoptr(GError) local_error = NULL;

	assert_in_worker (self);

	flatpak = gs_plugin_flatpak_get_handler (self, data->repository);
	if (flatpak == NULL) {
		g_task_return_boolean (task, TRUE);
		return;
	}

	if (gs_flatpak_remove_repository_app (flatpak, data->repository, FALSE, interactive, cancellable, &local_error))
		g_task_return_boolean (task, TRUE);
	else
		g_task_return_error (task, g_steal_pointer (&local_error));
}

static gboolean
gs_plugin_flatpak_disable_repository_finish (GsPlugin      *plugin,
					     GAsyncResult  *result,
					     GError       **error)
{
	return g_task_propagate_boolean (G_TASK (result), error);
}

static void
gs_plugin_flatpak_class_init (GsPluginFlatpakClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	GsPluginClass *plugin_class = GS_PLUGIN_CLASS (klass);

	object_class->dispose = gs_plugin_flatpak_dispose;

	plugin_class->adopt_app = gs_plugin_flatpak_adopt_app;
	plugin_class->setup_async = gs_plugin_flatpak_setup_async;
	plugin_class->setup_finish = gs_plugin_flatpak_setup_finish;
	plugin_class->shutdown_async = gs_plugin_flatpak_shutdown_async;
	plugin_class->shutdown_finish = gs_plugin_flatpak_shutdown_finish;
	plugin_class->refine_async = gs_plugin_flatpak_refine_async;
	plugin_class->refine_finish = gs_plugin_flatpak_refine_finish;
	plugin_class->list_apps_async = gs_plugin_flatpak_list_apps_async;
	plugin_class->list_apps_finish = gs_plugin_flatpak_list_apps_finish;
	plugin_class->refresh_metadata_async = gs_plugin_flatpak_refresh_metadata_async;
	plugin_class->refresh_metadata_finish = gs_plugin_flatpak_refresh_metadata_finish;
	plugin_class->install_repository_async = gs_plugin_flatpak_install_repository_async;
	plugin_class->install_repository_finish = gs_plugin_flatpak_install_repository_finish;
	plugin_class->remove_repository_async = gs_plugin_flatpak_remove_repository_async;
	plugin_class->remove_repository_finish = gs_plugin_flatpak_remove_repository_finish;
	plugin_class->enable_repository_async = gs_plugin_flatpak_enable_repository_async;
	plugin_class->enable_repository_finish = gs_plugin_flatpak_enable_repository_finish;
	plugin_class->disable_repository_async = gs_plugin_flatpak_disable_repository_async;
	plugin_class->disable_repository_finish = gs_plugin_flatpak_disable_repository_finish;
	plugin_class->refine_categories_async = gs_plugin_flatpak_refine_categories_async;
	plugin_class->refine_categories_finish = gs_plugin_flatpak_refine_categories_finish;
	plugin_class->install_apps_async = gs_plugin_flatpak_install_apps_async;
	plugin_class->install_apps_finish = gs_plugin_flatpak_install_apps_finish;
	plugin_class->uninstall_apps_async = gs_plugin_flatpak_uninstall_apps_async;
	plugin_class->uninstall_apps_finish = gs_plugin_flatpak_uninstall_apps_finish;
	plugin_class->update_apps_async = gs_plugin_flatpak_update_apps_async;
	plugin_class->update_apps_finish = gs_plugin_flatpak_update_apps_finish;
	plugin_class->launch_async = gs_plugin_flatpak_launch_async;
	plugin_class->launch_finish = gs_plugin_flatpak_launch_finish;
	plugin_class->file_to_app_async = gs_plugin_flatpak_file_to_app_async;
	plugin_class->file_to_app_finish = gs_plugin_flatpak_file_to_app_finish;
	plugin_class->url_to_app_async = gs_plugin_flatpak_url_to_app_async;
	plugin_class->url_to_app_finish = gs_plugin_flatpak_url_to_app_finish;
}

GType
gs_plugin_query_type (void)
{
	return GS_TYPE_PLUGIN_FLATPAK;
}