summaryrefslogtreecommitdiffstats
path: root/src/libs/installer/packagemanagercore_p.cpp
blob: 4aaa27cfd45f40f827ac19b708ea470798f17840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
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
/**************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Installer Framework.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
**************************************************************************/
#include "packagemanagercore_p.h"

#include "adminauthorization.h"
#include "binarycontent.h"
#include "binaryformatenginehandler.h"
#include "binarylayout.h"
#include "scriptengine.h"
#include "componentmodel.h"
#include "errors.h"
#include "fileio.h"
#include "remotefileengine.h"
#include "graph.h"
#include "messageboxhandler.h"
#include "packagemanagercore.h"
#include "progresscoordinator.h"
#include "qprocesswrapper.h"
#include "protocol.h"
#include "qsettingswrapper.h"
#include "installercalculator.h"
#include "uninstallercalculator.h"
#include "componentchecker.h"
#include "globals.h"
#include "binarycreator.h"
#include "loggingutils.h"

#include "selfrestarter.h"
#include "filedownloaderfactory.h"
#include "updateoperationfactory.h"

#include <productkeycheck.h>

#include <QSettings>
#include <QtConcurrentRun>
#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtCore/QDirIterator>
#include <QtCore/QUuid>
#include <QtCore/QFuture>
#include <QtCore/QFutureWatcher>
#include <QtCore/QTemporaryFile>

#include <QXmlStreamReader>
#include <QXmlStreamWriter>

#include <errno.h>

#ifdef Q_OS_WIN
#include <qt_windows.h>
#endif

#define QUOTE_(x) #x
#define QUOTE(x) QUOTE_(x)

namespace QInstaller {

/*!
    \inmodule QtInstallerFramework
    \class QInstaller::PackageManagerCorePrivate
    \internal
*/

class OperationTracer
{
public:
    OperationTracer(Operation *operation) : m_operation(nullptr)
    {
        // don't create output for that hacky pseudo operation
        if (operation->name() != QLatin1String("MinimumProgress"))
            m_operation = operation;
    }
    void trace(const QString &state)
    {
        if (!m_operation)
            return;
        qCDebug(QInstaller::lcInstallerInstallLog).noquote() << QString::fromLatin1("%1 %2 operation: %3")
            .arg(state, m_operation->value(QLatin1String("component")).toString(), m_operation->name());
        QStringList args = m_operation->arguments();
        if (m_operation->requiresUnreplacedVariables())
            args = m_operation->packageManager()->replaceVariables(m_operation->arguments());
        qCDebug(QInstaller::lcInstallerInstallLog).noquote() << QString::fromLatin1("\t- arguments: %1")
            .arg(args.join(QLatin1String(", ")));
    }
    ~OperationTracer() {
        if (!m_operation)
            return;
        qCDebug(QInstaller::lcInstallerInstallLog) << "Done";
    }
private:
    Operation *m_operation;
};

static bool runOperation(Operation *operation, Operation::OperationType type)
{
    OperationTracer tracer(operation);
    switch (type) {
        case Operation::Backup:
            tracer.trace(QLatin1String("backup"));
            operation->backup();
            return true;
        case Operation::Perform:
            tracer.trace(QLatin1String("perform"));
            return operation->performOperation();
        case Operation::Undo:
            tracer.trace(QLatin1String("undo"));
            return operation->undoOperation();
        default:
            Q_ASSERT(!"unexpected operation type");
    }
    return false;
}

static QStringList checkRunningProcessesFromList(const QStringList &processList)
{
    const QList<ProcessInfo> allProcesses = runningProcesses();
    QStringList stillRunningProcesses;
    foreach (const QString &process, processList) {
        if (!process.isEmpty() && PackageManagerCorePrivate::isProcessRunning(process, allProcesses))
            stillRunningProcesses.append(process);
    }
    return stillRunningProcesses;
}

static void deferredRename(const QString &oldName, const QString &newName, bool restart = false)
{
#ifdef Q_OS_WIN
    QStringList arguments;

    // Check if .vbs extension can be used for running renaming script. If not, create own extension
    QString extension = QLatin1String(".vbs");
    QSettingsWrapper settingRoot(QLatin1String("HKEY_CLASSES_ROOT\\.vbs"), QSettingsWrapper::NativeFormat);
    if (settingRoot.value(QLatin1String(".")).toString() != QLatin1String("VBSFile")) {
        extension = QLatin1String(".qtInstaller");
        QSettingsWrapper settingsUser(QLatin1String("HKEY_CURRENT_USER\\Software\\Classes"), QSettingsWrapper::NativeFormat);
        QString value = settingsUser.value(extension).toString();
        if (value != QLatin1String("VBSFile"))
            settingsUser.setValue(extension, QLatin1String("VBSFile"));
    }
    QTemporaryFile f(QDir::temp().absoluteFilePath(QLatin1String("deferredrenameXXXXXX%1")).arg(extension));

    QInstaller::openForWrite(&f);
    f.setAutoRemove(false);

    arguments << QDir::toNativeSeparators(f.fileName()) << QDir::toNativeSeparators(oldName)
        << QDir::toNativeSeparators(QFileInfo(oldName).dir().absoluteFilePath(QFileInfo(newName)
        .fileName()));

    QTextStream batch(&f);
    batch.setCodec("UTF-16");
    batch << "Set fso = WScript.CreateObject(\"Scripting.FileSystemObject\")\n";
    batch << "Set tmp = WScript.CreateObject(\"WScript.Shell\")\n";
    batch << QString::fromLatin1("file = \"%1\"\n").arg(arguments[2]);
    batch << "on error resume next\n";

    batch << "while fso.FileExists(file)\n";
    batch << "    fso.DeleteFile(file)\n";
    batch << "    WScript.Sleep(1000)\n";
    batch << "wend\n";
    batch << QString::fromLatin1("fso.MoveFile \"%1\", file\n").arg(arguments[1]);
    if (restart) {
        //Restart with same command line arguments as first executable
        QStringList commandLineArguments = QCoreApplication::arguments();
        batch <<  QString::fromLatin1("tmp.exec \"%1 --%2")
                  .arg(arguments[2]).arg(CommandLineOptions::scStartUpdaterLong);
        //Skip the first argument as that is executable itself
        for (int i = 1; i < commandLineArguments.count(); i++) {
            batch << QString::fromLatin1(" %1").arg(commandLineArguments.at(i));
        }
        batch << QString::fromLatin1("\"\n");
    }
    batch << "fso.DeleteFile(WScript.ScriptFullName)\n";

    QProcessWrapper::startDetached(QLatin1String("cscript"), QStringList() << QLatin1String("//Nologo")
        << arguments[0]);
#else
        QFile::remove(newName);
        QFile::rename(oldName, newName);
        SelfRestarter::setRestartOnQuit(restart);
#endif
}


// -- PackageManagerCorePrivate

PackageManagerCorePrivate::PackageManagerCorePrivate(PackageManagerCore *core)
    : m_updateFinder(nullptr)
    , m_localPackageHub(std::make_shared<LocalPackageHub>())
    , m_status(PackageManagerCore::Unfinished)
    , m_needsHardRestart(false)
    , m_testChecksum(false)
    , m_launchedAsRoot(AdminAuthorization::hasAdminRights())
    , m_completeUninstall(false)
    , m_needToWriteMaintenanceTool(false)
    , m_dependsOnLocalInstallerBinary(false)
    , m_core(core)
    , m_updates(false)
    , m_repoFetched(false)
    , m_updateSourcesAdded(false)
    , m_magicBinaryMarker(0) // initialize with pseudo marker
    , m_magicMarkerSupplement(BinaryContent::Default)
    , m_componentsToInstallCalculated(false)
    , m_componentScriptEngine(nullptr)
    , m_controlScriptEngine(nullptr)
    , m_installerCalculator(nullptr)
    , m_uninstallerCalculator(nullptr)
    , m_proxyFactory(nullptr)
    , m_defaultModel(nullptr)
    , m_updaterModel(nullptr)
    , m_guiObject(nullptr)
    , m_remoteFileEngineHandler(nullptr)
    , m_foundEssentialUpdate(false)
    , m_commandLineInstance(false)
    , m_defaultInstall(false)
    , m_userSetBinaryMarker(false)
    , m_checkAvailableSpace(true)
    , m_autoAcceptLicenses(false)
    , m_disableWriteMaintenanceTool(false)
    , m_autoConfirmCommand(false)
{
}

PackageManagerCorePrivate::PackageManagerCorePrivate(PackageManagerCore *core, qint64 magicInstallerMaker,
        const QList<OperationBlob> &performedOperations)
    : m_updateFinder(nullptr)
    , m_localPackageHub(std::make_shared<LocalPackageHub>())
    , m_status(PackageManagerCore::Unfinished)
    , m_needsHardRestart(false)
    , m_testChecksum(false)
    , m_launchedAsRoot(AdminAuthorization::hasAdminRights())
    , m_completeUninstall(false)
    , m_needToWriteMaintenanceTool(false)
    , m_dependsOnLocalInstallerBinary(false)
    , m_core(core)
    , m_updates(false)
    , m_repoFetched(false)
    , m_updateSourcesAdded(false)
    , m_magicBinaryMarker(magicInstallerMaker)
    , m_magicMarkerSupplement(BinaryContent::Default)
    , m_componentsToInstallCalculated(false)
    , m_componentScriptEngine(nullptr)
    , m_controlScriptEngine(nullptr)
    , m_installerCalculator(nullptr)
    , m_uninstallerCalculator(nullptr)
    , m_proxyFactory(nullptr)
    , m_defaultModel(nullptr)
    , m_updaterModel(nullptr)
    , m_guiObject(nullptr)
    , m_remoteFileEngineHandler(new RemoteFileEngineHandler)
    , m_foundEssentialUpdate(false)
    , m_commandLineInstance(false)
    , m_defaultInstall(false)
    , m_userSetBinaryMarker(false)
    , m_checkAvailableSpace(true)
    , m_autoAcceptLicenses(false)
    , m_disableWriteMaintenanceTool(false)
    , m_autoConfirmCommand(false)
{
    foreach (const OperationBlob &operation, performedOperations) {
        QScopedPointer<QInstaller::Operation> op(KDUpdater::UpdateOperationFactory::instance()
            .create(operation.name, core));
        if (op.isNull()) {
            qCWarning(QInstaller::lcInstallerInstallLog) << "Failed to load unknown operation"
                << operation.name;
            continue;
        }

        if (!op->fromXml(operation.xml)) {
            qCWarning(QInstaller::lcInstallerInstallLog) << "Failed to load XML for operation"
                << operation.name;
            continue;
        }
        m_performedOperationsOld.append(op.take());
    }

    connect(this, &PackageManagerCorePrivate::installationStarted,
            m_core, &PackageManagerCore::installationStarted);
    connect(this, &PackageManagerCorePrivate::installationFinished,
            m_core, &PackageManagerCore::installationFinished);
    connect(this, &PackageManagerCorePrivate::uninstallationStarted,
            m_core, &PackageManagerCore::uninstallationStarted);
    connect(this, &PackageManagerCorePrivate::uninstallationFinished,
            m_core, &PackageManagerCore::uninstallationFinished);
    connect(this, &PackageManagerCorePrivate::offlineGenerationStarted,
            m_core, &PackageManagerCore::offlineGenerationStarted);
    connect(this, &PackageManagerCorePrivate::offlineGenerationFinished,
            m_core, &PackageManagerCore::offlineGenerationFinished);
}

PackageManagerCorePrivate::~PackageManagerCorePrivate()
{
    clearAllComponentLists();
    clearUpdaterComponentLists();
    clearInstallerCalculator();
    clearUninstallerCalculator();

    qDeleteAll(m_ownedOperations);
    qDeleteAll(m_performedOperationsOld);
    qDeleteAll(m_performedOperationsCurrentSession);

    delete m_updateFinder;
    delete m_proxyFactory;

    delete m_defaultModel;
    delete m_updaterModel;

    // at the moment the tabcontroller deletes the m_gui, this needs to be changed in the future
    // delete m_gui;
}

/*
    Return true, if a process with \a name is running. On Windows, comparison is case-insensitive.
*/
/* static */
bool PackageManagerCorePrivate::isProcessRunning(const QString &name,
    const QList<ProcessInfo> &processes)
{
    QList<ProcessInfo>::const_iterator it;
    for (it = processes.constBegin(); it != processes.constEnd(); ++it) {
        if (it->name.isEmpty())
            continue;

#ifndef Q_OS_WIN
        if (it->name == name)
            return true;
        const QFileInfo fi(it->name);
        if (fi.fileName() == name || fi.baseName() == name)
            return true;
#else
        if (it->name.toLower() == name.toLower())
            return true;
        if (it->name.toLower() == QDir::toNativeSeparators(name.toLower()))
            return true;
        const QFileInfo fi(it->name);
        if (fi.fileName().toLower() == name.toLower() || fi.baseName().toLower() == name.toLower())
            return true;
#endif
    }
    return false;
}

/* static */
bool PackageManagerCorePrivate::performOperationThreaded(Operation *operation, Operation::OperationType type)
{
    QFutureWatcher<bool> futureWatcher;
    const QFuture<bool> future = QtConcurrent::run(runOperation, operation, type);

    QEventLoop loop;
    QObject::connect(&futureWatcher, &decltype(futureWatcher)::finished, &loop, &QEventLoop::quit,
                     Qt::QueuedConnection);
    futureWatcher.setFuture(future);

    if (!future.isFinished())
        loop.exec();

    return future.result();
}

QString PackageManagerCorePrivate::targetDir() const
{
    return m_core->value(scTargetDir);
}

bool PackageManagerCorePrivate::directoryWritable(const QString &path) const
{
    QTemporaryFile tempFile(path + QLatin1String("/tempFile.XXXXXX"));
    return (tempFile.open() && tempFile.isWritable());
}

QString PackageManagerCorePrivate::configurationFileName() const
{
    return m_core->value(scTargetConfigurationFile, QLatin1String("components.xml"));
}

QString PackageManagerCorePrivate::componentsXmlPath() const
{
    return QDir::toNativeSeparators(QDir(QDir::cleanPath(targetDir()))
        .absoluteFilePath(configurationFileName()));
}

bool PackageManagerCorePrivate::buildComponentTree(QHash<QString, Component*> &components, bool loadScript)
{
    try {
        if (statusCanceledOrFailed())
            return false;
        // append all components to their respective parents
        QHash<QString, Component*>::const_iterator it;
        for (it = components.constBegin(); it != components.constEnd(); ++it) {
            QString id = it.key();
            QInstaller::Component *component = it.value();
            while (!id.isEmpty() && component->parentComponent() == nullptr) {
                id = id.section(QLatin1Char('.'), 0, -2);
                if (components.contains(id))
                    components[id]->appendComponent(component);
            }
        }

        // append all components w/o parent to the direct list
        foreach (QInstaller::Component *component, components) {
            if (component->parentComponent() == nullptr)
                m_core->appendRootComponent(component);
        }

        // after everything is set up, load the scripts if needed
        if (loadScript) {
            foreach (QInstaller::Component *component, components)
                component->loadComponentScript();
        }

        // now we can preselect components in the tree
        foreach (QInstaller::Component *component, components) {
            // set the checked state for all components without child (means without tristate)
            // set checked state also for installed virtual tristate componets as otherwise
            // those will be uninstalled
            if (component->isCheckable() && !component->isTristate()) {
                if (component->isDefault() && isInstaller())
                    component->setCheckState(Qt::Checked);
                else if (component->isInstalled())
                    component->setCheckState(Qt::Checked);
            } else if (component->isVirtual() && component->isInstalled() && component->isTristate()) {
                component->setCheckState(Qt::Checked);
            }
        }

        std::sort(m_rootComponents.begin(), m_rootComponents.end(), Component::SortingPriorityGreaterThan());

        storeCheckState();

        foreach (QInstaller::Component *component, components)
            component->setCheckState(Qt::Checked);

        clearInstallerCalculator();
        if (installerCalculator()->appendComponentsToInstall(components.values()) == false) {
            setStatus(PackageManagerCore::Failure, installerCalculator()->componentsToInstallError());
            MessageBoxHandler::critical(MessageBoxHandler::currentBestSuitParent(), QLatin1String("Error"),
                tr("Unresolved dependencies"), installerCalculator()->componentsToInstallError());
            return false;
        }

        restoreCheckState();

        if (LoggingHandler::instance().verboseLevel() == LoggingHandler::Detailed) {
            foreach (QInstaller::Component *component, components) {
                const QStringList warnings = ComponentChecker::checkComponent(component);
                foreach (const QString &warning, warnings)
                    qCWarning(lcDeveloperBuild).noquote() << warning;
            }
        }

    } catch (const Error &error) {
        clearAllComponentLists();
        setStatus(PackageManagerCore::Failure, error.message());

        // TODO: make sure we remove all message boxes inside the library at some point.
        MessageBoxHandler::critical(MessageBoxHandler::currentBestSuitParent(), QLatin1String("Error"),
            tr("Error"), error.message());
        return false;
    }
    return true;
}

void PackageManagerCorePrivate::cleanUpComponentEnvironment()
{
    // clean up registered (downloaded) data
    if (m_core->isMaintainer())
        BinaryFormatEngineHandler::instance()->clear();

    // there could be still some references to already deleted components,
    // so we need to remove the current component script engine
    delete m_componentScriptEngine;
    m_componentScriptEngine = nullptr;
}

ScriptEngine *PackageManagerCorePrivate::componentScriptEngine() const
{
    if (!m_componentScriptEngine)
        m_componentScriptEngine = new ScriptEngine(m_core);
    return m_componentScriptEngine;
}

ScriptEngine *PackageManagerCorePrivate::controlScriptEngine() const
{
    if (!m_controlScriptEngine)
        m_controlScriptEngine = new ScriptEngine(m_core);
    return m_controlScriptEngine;
}

void PackageManagerCorePrivate::clearAllComponentLists()
{
    QList<QInstaller::Component*> toDelete;

    toDelete << m_rootComponents << m_deletedReplacedComponents;
    m_rootComponents.clear();
    m_rootDependencyReplacements.clear();
    m_deletedReplacedComponents.clear();

    m_componentsToReplaceAllMode.clear();
    m_componentsToInstallCalculated = false;

    qDeleteAll(toDelete);
    cleanUpComponentEnvironment();
}

void PackageManagerCorePrivate::clearUpdaterComponentLists()
{
    QSet<Component*> usedComponents =
        QSet<Component*>::fromList(m_updaterComponents + m_updaterComponentsDeps);

    const QList<QPair<Component*, Component*> > list = m_componentsToReplaceUpdaterMode.values();
    for (int i = 0; i < list.count(); ++i) {
        if (usedComponents.contains(list.at(i).second))
            qCWarning(QInstaller::lcDeveloperBuild) << "a replacement was already in the list - is that correct?";
        else
            usedComponents.insert(list.at(i).second);
    }

    m_updaterComponents.clear();
    m_updaterComponentsDeps.clear();

    m_updaterDependencyReplacements.clear();

    m_componentsToReplaceUpdaterMode.clear();
    m_componentsToInstallCalculated = false;

    qDeleteAll(usedComponents);
    cleanUpComponentEnvironment();
}

QList<Component *> &PackageManagerCorePrivate::replacementDependencyComponents()
{
    return (!isUpdater()) ? m_rootDependencyReplacements : m_updaterDependencyReplacements;
}

QHash<QString, QPair<Component*, Component*> > &PackageManagerCorePrivate::componentsToReplace()
{
    return (!isUpdater()) ? m_componentsToReplaceAllMode : m_componentsToReplaceUpdaterMode;
}

void PackageManagerCorePrivate::clearInstallerCalculator()
{
    delete m_installerCalculator;
    m_installerCalculator = nullptr;
}

InstallerCalculator *PackageManagerCorePrivate::installerCalculator() const
{
    if (!m_installerCalculator) {
        PackageManagerCorePrivate *const pmcp = const_cast<PackageManagerCorePrivate *> (this);
        pmcp->m_installerCalculator = new InstallerCalculator(
            m_core->components(PackageManagerCore::ComponentType::AllNoReplacements));
    }
    return m_installerCalculator;
}

void PackageManagerCorePrivate::clearUninstallerCalculator()
{
    delete m_uninstallerCalculator;
    m_uninstallerCalculator = nullptr;
}

UninstallerCalculator *PackageManagerCorePrivate::uninstallerCalculator() const
{
    if (!m_uninstallerCalculator) {
        PackageManagerCorePrivate *const pmcp = const_cast<PackageManagerCorePrivate *> (this);

        QList<Component*> installedComponents;
        foreach (const QString &name, pmcp->localInstalledPackages().keys()) {
            if (Component *component = m_core->componentByName(PackageManagerCore::checkableName(name))) {
                if (!component->uninstallationRequested())
                    installedComponents.append(component);
            }
        }

        pmcp->m_uninstallerCalculator = new UninstallerCalculator(installedComponents, m_core);
    }
    return m_uninstallerCalculator;
}

void PackageManagerCorePrivate::initialize(const QHash<QString, QString> &params)
{
    m_coreCheckedHash.clear();
    m_data = PackageManagerCoreData(params, isInstaller());
    m_componentsToInstallCalculated = false;

#ifdef Q_OS_LINUX
    if (m_launchedAsRoot && isInstaller())
        m_data.setValue(scTargetDir, replaceVariables(m_data.settings().adminTargetDir()));
#endif

    if (!m_core->isInstaller()) {
#ifdef Q_OS_MACOS
        readMaintenanceConfigFiles(QCoreApplication::applicationDirPath() + QLatin1String("/../../.."));
#else
        readMaintenanceConfigFiles(QCoreApplication::applicationDirPath());
#endif
        // Maintenancetool might have overwritten the variables
        // user has given from command line. Reset those variables.
        m_data.setUserDefinedVariables(params);
    }
    processFilesForDelayedDeletion();

    // Set shortcut path for command line interface, in GUI version
    // we have a separate page where the whole path is set.
#ifdef Q_OS_WIN
    if (m_core->isCommandLineInstance() && m_core->isInstaller()) {
        QString startMenuPath;
        if (params.value(QLatin1String("AllUsers")) == scTrue)
            startMenuPath = m_data.value(scAllUsersStartMenuProgramsPath).toString();
        else
            startMenuPath = m_data.value(scUserStartMenuProgramsPath).toString();
        QString startMenuDir = m_core->value(scStartMenuDir, m_core->value(QLatin1String("ProductName")));
        m_data.setValue(scStartMenuDir, startMenuPath + QDir::separator() + startMenuDir);
    }
#endif

    disconnect(this, &PackageManagerCorePrivate::installationStarted,
               ProgressCoordinator::instance(), &ProgressCoordinator::reset);
    connect(this, &PackageManagerCorePrivate::installationStarted,
            ProgressCoordinator::instance(), &ProgressCoordinator::reset);
    disconnect(this, &PackageManagerCorePrivate::uninstallationStarted,
               ProgressCoordinator::instance(), &ProgressCoordinator::reset);
    connect(this, &PackageManagerCorePrivate::uninstallationStarted,
            ProgressCoordinator::instance(), &ProgressCoordinator::reset);
    disconnect(this, &PackageManagerCorePrivate::offlineGenerationStarted,
               ProgressCoordinator::instance(), &ProgressCoordinator::reset);
    connect(this, &PackageManagerCorePrivate::offlineGenerationStarted,
            ProgressCoordinator::instance(), &ProgressCoordinator::reset);

    if (!isInstaller())
        m_localPackageHub->setFileName(componentsXmlPath());

    if (isInstaller() || m_localPackageHub->applicationName().isEmpty()) {
        // TODO: this seems to be wrong, we should ask for ProductName defaulting to applicationName...
        m_localPackageHub->setApplicationName(m_data.settings().applicationName());
    }

    if (isInstaller() || m_localPackageHub->applicationVersion().isEmpty())
        m_localPackageHub->setApplicationVersion(QLatin1String(QUOTE(IFW_REPOSITORY_FORMAT_VERSION)));

    if (isInstaller())
        m_packageSources.insert(PackageSource(QUrl(QLatin1String("resource://metadata/")), 0));

    m_metadataJob.disconnect();
    m_metadataJob.setAutoDelete(false);
    m_metadataJob.setPackageManagerCore(m_core);
    connect(&m_metadataJob, &Job::infoMessage, this, &PackageManagerCorePrivate::infoMessage);
    connect(&m_metadataJob, &Job::progress, this, &PackageManagerCorePrivate::infoProgress);
    connect(&m_metadataJob, &Job::totalProgress, this, &PackageManagerCorePrivate::totalProgress);
    KDUpdater::FileDownloaderFactory::instance().setProxyFactory(m_core->proxyFactory());
}

bool PackageManagerCorePrivate::isOfflineOnly() const
{
    if (!isInstaller())
        return false;

    QSettings confInternal(QLatin1String(":/config/config-internal.ini"), QSettings::IniFormat);
    return confInternal.value(QLatin1String("offlineOnly"), false).toBool();
}

QString PackageManagerCorePrivate::installerBinaryPath() const
{
    return qApp->applicationFilePath();
}

bool PackageManagerCorePrivate::isInstaller() const
{
    return m_magicBinaryMarker == BinaryContent::MagicInstallerMarker;
}

bool PackageManagerCorePrivate::isUninstaller() const
{
    return m_magicBinaryMarker == BinaryContent::MagicUninstallerMarker;
}

bool PackageManagerCorePrivate::isUpdater() const
{
    return m_magicBinaryMarker == BinaryContent::MagicUpdaterMarker;
}

bool PackageManagerCorePrivate::isPackageManager() const
{
    return m_magicBinaryMarker == BinaryContent::MagicPackageManagerMarker;
}

bool PackageManagerCorePrivate::isOfflineGenerator() const
{
    return m_magicMarkerSupplement == BinaryContent::OfflineGenerator;
}

bool PackageManagerCorePrivate::isPackageViewer() const
{
    return m_magicMarkerSupplement == BinaryContent::PackageViewer;
}

bool PackageManagerCorePrivate::statusCanceledOrFailed() const
{
    return m_status == PackageManagerCore::Canceled || m_status == PackageManagerCore::Failure;
}

void PackageManagerCorePrivate::setStatus(int status, const QString &error)
{
    m_error = error;
    if (!error.isEmpty())
        qCWarning(QInstaller::lcInstallerInstallLog) << m_error;
    if (m_status != status) {
        m_status = status;
        emit m_core->statusChanged(PackageManagerCore::Status(m_status));
    }
}

QString PackageManagerCorePrivate::replaceVariables(const QString &str) const
{
    return m_data.replaceVariables(str);
}

QByteArray PackageManagerCorePrivate::replaceVariables(const QByteArray &ba) const
{
    return m_data.replaceVariables(ba);
}

/*!
    \internal
    Creates an update operation owned by the installer, not by any component.
 */
Operation *PackageManagerCorePrivate::createOwnedOperation(const QString &type)
{
    m_ownedOperations.append(KDUpdater::UpdateOperationFactory::instance().create(type, m_core));
    return m_ownedOperations.last();
}

/*!
    \internal
    Removes \a operation from the operations owned by the installer, returns the very same operation if the
    operation was found, otherwise 0.
 */
Operation *PackageManagerCorePrivate::takeOwnedOperation(Operation *operation)
{
    if (!m_ownedOperations.contains(operation))
        return nullptr;

    m_ownedOperations.removeAll(operation);
    return operation;
}

QString PackageManagerCorePrivate::maintenanceToolName() const
{
    QString filename = m_data.settings().maintenanceToolName();
#if defined(Q_OS_MACOS)
    if (QInstaller::isInBundle(QCoreApplication::applicationDirPath()))
        filename += QLatin1String(".app/Contents/MacOS/") + filename;
#elif defined(Q_OS_WIN)
    filename += QLatin1String(".exe");
#endif
    return QString::fromLatin1("%1/%2").arg(targetDir()).arg(filename);
}

QString PackageManagerCorePrivate::offlineBinaryName() const
{
    QString filename = m_core->value(scOfflineBinaryName, qApp->applicationName()
        + QLatin1String("_offline-") + QDate::currentDate().toString(Qt::ISODate));
#if defined(Q_OS_WIN)
    const QString suffix = QLatin1String(".exe");
    if (!filename.endsWith(suffix))
        filename += suffix;
#endif
    return QString::fromLatin1("%1/%2").arg(targetDir()).arg(filename);
}

static QNetworkProxy readProxy(QXmlStreamReader &reader)
{
    QNetworkProxy proxy(QNetworkProxy::HttpProxy);
    while (reader.readNextStartElement()) {
        if (reader.name() == QLatin1String("Host"))
            proxy.setHostName(reader.readElementText());
        else if (reader.name() == QLatin1String("Port"))
            proxy.setPort(reader.readElementText().toInt());
        else if (reader.name() == QLatin1String("Username"))
            proxy.setUser(reader.readElementText());
        else if (reader.name() == QLatin1String("Password"))
            proxy.setPassword(reader.readElementText());
        else
            reader.skipCurrentElement();
    }
    return proxy;
}

static QSet<Repository> readRepositories(QXmlStreamReader &reader, bool isDefault)
{
    QSet<Repository> set;
    while (reader.readNextStartElement()) {
        if (reader.name() == QLatin1String("Repository")) {
            Repository repo(QString(), isDefault);
            while (reader.readNextStartElement()) {
                if (reader.name() == QLatin1String("Host"))
                    repo.setUrl(reader.readElementText());
                else if (reader.name() == QLatin1String("Username"))
                    repo.setUsername(reader.readElementText());
                else if (reader.name() == QLatin1String("Password"))
                    repo.setPassword(reader.readElementText());
                else if (reader.name() == QLatin1String("DisplayName"))
                    repo.setDisplayName(reader.readElementText());
                else if (reader.name() == QLatin1String("Enabled"))
                    repo.setEnabled(bool(reader.readElementText().toInt()));
                else
                    reader.skipCurrentElement();
            }
            set.insert(repo);
        } else {
            reader.skipCurrentElement();
        }
    }
    return set;
}

void PackageManagerCorePrivate::writeMaintenanceConfigFiles()
{
    // write current state (variables) to the maintenance tool ini file
    const QString iniPath = targetDir() + QLatin1Char('/') + m_data.settings().maintenanceToolIniFile();

    QVariantHash variables; // Do not change to QVariantMap! Breaks existing .ini files,
    // cause the variant types do not match while restoring the variables from the file.
    QSettingsWrapper cfg(iniPath, QSettingsWrapper::IniFormat);
    foreach (const QString &key, m_data.keys()) {
        if (key == scRunProgramDescription || key == scRunProgram || key == scRunProgramArguments)
            continue;
        QVariant value = m_data.value(key);
        if (value.canConvert(QVariant::String))
            value = replacePath(value.toString(), targetDir(), QLatin1String(scRelocatable));
        variables.insert(key, value);
    }
    cfg.setValue(QLatin1String("Variables"), variables);

    QVariantList repos; // Do not change either!
    if (m_data.settings().saveDefaultRepositories()) {
        foreach (const Repository &repo, m_data.settings().defaultRepositories())
            repos.append(QVariant().fromValue(repo));
    }
    cfg.setValue(QLatin1String("DefaultRepositories"), repos);
    cfg.setValue(QLatin1String("FilesForDelayedDeletion"), m_filesForDelayedDeletion);

    cfg.sync();
    if (cfg.status() != QSettingsWrapper::NoError) {
        const QString reason = cfg.status() == QSettingsWrapper::AccessError ? tr("Access error")
            : tr("Format error");
        throw Error(tr("Cannot write installer configuration to %1: %2").arg(iniPath, reason));
    }
    setDefaultFilePermissions(iniPath, DefaultFilePermissions::NonExecutable);

    QFile file(targetDir() + QLatin1Char('/') + QLatin1String("network.xml"));
    if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        QXmlStreamWriter writer(&file);
        writer.setCodec("UTF-8");
        writer.setAutoFormatting(true);
        writer.writeStartDocument();

        writer.writeStartElement(QLatin1String("Network"));
            writer.writeTextElement(QLatin1String("ProxyType"), QString::number(m_data.settings().proxyType()));
            writer.writeStartElement(QLatin1String("Ftp"));
                const QNetworkProxy &ftpProxy = m_data.settings().ftpProxy();
                writer.writeTextElement(QLatin1String("Host"), ftpProxy.hostName());
                writer.writeTextElement(QLatin1String("Port"), QString::number(ftpProxy.port()));
                writer.writeTextElement(QLatin1String("Username"), ftpProxy.user());
                writer.writeTextElement(QLatin1String("Password"), ftpProxy.password());
            writer.writeEndElement();
            writer.writeStartElement(QLatin1String("Http"));
                const QNetworkProxy &httpProxy = m_data.settings().httpProxy();
                writer.writeTextElement(QLatin1String("Host"), httpProxy.hostName());
                writer.writeTextElement(QLatin1String("Port"), QString::number(httpProxy.port()));
                writer.writeTextElement(QLatin1String("Username"), httpProxy.user());
                writer.writeTextElement(QLatin1String("Password"), httpProxy.password());
            writer.writeEndElement();

            writer.writeStartElement(QLatin1String("Repositories"));
            foreach (const Repository &repo, m_data.settings().userRepositories()) {
                writer.writeStartElement(QLatin1String("Repository"));
                    writer.writeTextElement(QLatin1String("Host"), repo.url().toString());
                    writer.writeTextElement(QLatin1String("Username"), repo.username());
                    writer.writeTextElement(QLatin1String("Password"), repo.password());
                    writer.writeTextElement(QLatin1String("Enabled"), QString::number(repo.isEnabled()));
                writer.writeEndElement();
            }
            writer.writeEndElement();
        writer.writeEndElement();
    }
    setDefaultFilePermissions(&file, DefaultFilePermissions::NonExecutable);
}

void PackageManagerCorePrivate::readMaintenanceConfigFiles(const QString &targetDir)
{
    QSettingsWrapper cfg(targetDir + QLatin1Char('/') + m_data.settings().maintenanceToolIniFile(),
        QSettingsWrapper::IniFormat);
    const QVariantHash v = cfg.value(QLatin1String("Variables")).toHash(); // Do not change to
    // QVariantMap! Breaks reading from existing .ini files, cause the variant types do not match.
    for (QVariantHash::const_iterator it = v.constBegin(); it != v.constEnd(); ++it) {
        if (m_data.contains(it.key()) && !m_data.value(it.key()).isNull()) {
            // Exception: StartMenuDir should be permanent after initial installation
            // and must be read from maintenancetool.ini
            if (it.key() != scStartMenuDir)
                continue;
        }
        m_data.setValue(it.key(), replacePath(it.value().toString(), QLatin1String(scRelocatable), targetDir));
    }
    QSet<Repository> repos;
    const QVariantList variants = cfg.value(QLatin1String("DefaultRepositories"))
        .toList(); // Do not change either!
    foreach (const QVariant &variant, variants)
        repos.insert(variant.value<Repository>());
    if (!repos.isEmpty())
        m_data.settings().setDefaultRepositories(repos);

    m_filesForDelayedDeletion = cfg.value(QLatin1String("FilesForDelayedDeletion")).toStringList();

    QFile file(targetDir + QLatin1String("/network.xml"));
    if (!file.open(QIODevice::ReadOnly))
        return;

    QXmlStreamReader reader(&file);
    while (!reader.atEnd()) {
        switch (reader.readNext()) {
            case QXmlStreamReader::StartElement: {
                if (reader.name() == QLatin1String("Network")) {
                    while (reader.readNextStartElement()) {
                        const QStringRef name = reader.name();
                        if (name == QLatin1String("Ftp")) {
                            m_data.settings().setFtpProxy(readProxy(reader));
                        } else if (name == QLatin1String("Http")) {
                            m_data.settings().setHttpProxy(readProxy(reader));
                        } else if (reader.name() == QLatin1String("Repositories")) {
                            m_data.settings().addUserRepositories(readRepositories(reader, false));
                        } else if (name == QLatin1String("ProxyType")) {
                            m_data.settings().setProxyType(Settings::ProxyType(reader.readElementText().toInt()));
                        } else {
                            reader.skipCurrentElement();
                        }
                    }
                }
            }   break;

            case QXmlStreamReader::Invalid: {
                qCWarning(QInstaller::lcInstallerInstallLog) << reader.errorString();
            }   break;

            default:
                break;
        }
    }
}

void PackageManagerCorePrivate::callBeginInstallation(const QList<Component*> &componentList)
{
    foreach (Component *component, componentList)
        component->beginInstallation();
}

void PackageManagerCorePrivate::stopProcessesForUpdates(const QList<Component*> &components)
{
    QStringList processList;
    foreach (const Component *component, components)
        processList << m_core->replaceVariables(component->stopProcessForUpdateRequests());

    std::sort(processList.begin(), processList.end());
    processList.erase(std::unique(processList.begin(), processList.end()), processList.end());
    if (processList.isEmpty())
        return;

    uint retryCount = 5;
    while (true) {
        const QStringList processes = checkRunningProcessesFromList(processList);
        if (processes.isEmpty())
            return;

        const QMessageBox::StandardButton button =
            MessageBoxHandler::warning(MessageBoxHandler::currentBestSuitParent(),
            QLatin1String("stopProcessesForUpdates"), tr("Stop Processes"), tr("These processes "
            "should be stopped to continue:\n\n%1").arg(QDir::toNativeSeparators(processes
            .join(QLatin1String("\n")))), QMessageBox::Retry | QMessageBox::Ignore
            | QMessageBox::Cancel, QMessageBox::Cancel);
        if (button == QMessageBox::Ignore)
            return;
        if (button == QMessageBox::Cancel) {
            m_core->setCanceled();
            throw Error(tr("Installation canceled by user"));
        }
        if (!m_core->isCommandLineInstance())
            continue;

        // Do not allow infinite retries with cli instance
        if (--retryCount == 0)
            throw Error(tr("Retry count exceeded"));
    }
}

int PackageManagerCorePrivate::countProgressOperations(const OperationList &operations)
{
    int operationCount = 0;
    foreach (Operation *operation, operations) {
        if (QObject *operationObject = dynamic_cast<QObject*> (operation)) {
            const QMetaObject *const mo = operationObject->metaObject();
            if (mo->indexOfSignal(QMetaObject::normalizedSignature("progressChanged(double)")) > -1)
                operationCount++;
        }
    }
    return operationCount;
}

int PackageManagerCorePrivate::countProgressOperations(const QList<Component*> &components)
{
    int operationCount = 0;
    foreach (Component *component, components)
        operationCount += countProgressOperations(component->operations());

    return operationCount;
}

void PackageManagerCorePrivate::connectOperationToInstaller(Operation *const operation, double operationPartSize)
{
    Q_ASSERT(operationPartSize);
    QObject *const operationObject = dynamic_cast< QObject*> (operation);
    if (operationObject != nullptr) {
        const QMetaObject *const mo = operationObject->metaObject();
        if (mo->indexOfSignal(QMetaObject::normalizedSignature("outputTextChanged(QString)")) > -1) {
            connect(operationObject, SIGNAL(outputTextChanged(QString)), ProgressCoordinator::instance(),
                SLOT(emitDetailTextChanged(QString)));
        }

        if (mo->indexOfSlot(QMetaObject::normalizedSignature("cancelOperation()")) > -1)
            connect(m_core, SIGNAL(installationInterrupted()), operationObject, SLOT(cancelOperation()));

        if (mo->indexOfSignal(QMetaObject::normalizedSignature("progressChanged(double)")) > -1) {
            ProgressCoordinator::instance()->registerPartProgress(operationObject,
                SIGNAL(progressChanged(double)), operationPartSize);
        }
    }
}

Operation *PackageManagerCorePrivate::createPathOperation(const QFileInfo &fileInfo,
    const QString &componentName)
{
    const bool isDir = fileInfo.isDir();
    // create an operation with the dir/ file as target, it will get deleted on undo
    Operation *operation = createOwnedOperation(QLatin1String(isDir ? "Mkdir" : "Copy"));
    if (isDir)
        operation->setValue(QLatin1String("createddir"), fileInfo.absoluteFilePath());
    operation->setValue(QLatin1String("component"), componentName);
    operation->setArguments(isDir ? QStringList() << fileInfo.absoluteFilePath()
        : QStringList() << QString() << fileInfo.absoluteFilePath());
    return operation;
}

/*!
    This creates fake operations which remove stuff which was registered for uninstallation afterwards
*/
void PackageManagerCorePrivate::registerPathsForUninstallation(
    const QList<QPair<QString, bool> > &pathsForUninstallation, const QString &componentName)
{
    if (pathsForUninstallation.isEmpty())
        return;

    QList<QPair<QString, bool> >::const_iterator it;
    for (it = pathsForUninstallation.begin(); it != pathsForUninstallation.end(); ++it) {
        const bool wipe = it->second;
        const QString path = replaceVariables(it->first);

        const QFileInfo fi(path);
        // create a copy operation with the file as target -> it will get deleted on undo
        Operation *op = createPathOperation(fi, componentName);
        if (fi.isDir())
            op->setValue(QLatin1String("forceremoval"), wipe ? scTrue : scFalse);
        addPerformed(takeOwnedOperation(op));

        // get recursive afterwards
        if (fi.isDir() && !wipe) {
            QDirIterator dirIt(path, QDir::Hidden | QDir::AllEntries | QDir::System
                | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
            while (dirIt.hasNext()) {
                dirIt.next();
                op = createPathOperation(dirIt.fileInfo(), componentName);
                addPerformed(takeOwnedOperation(op));
            }
        }
    }
}

void PackageManagerCorePrivate::writeMaintenanceToolBinary(QFile *const input, qint64 size, bool writeBinaryLayout)
{
    QString maintenanceToolRenamedName = maintenanceToolName() + QLatin1String(".new");
    qCDebug(QInstaller::lcInstallerInstallLog) << "Writing maintenance tool:" << maintenanceToolRenamedName;
    ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("Writing maintenance tool."));

    QFile out(generateTemporaryFileName());
    QInstaller::openForWrite(&out); // throws an exception in case of error

    if (!input->seek(0))
        throw Error(tr("Failed to seek in file %1: %2").arg(input->fileName(), input->errorString()));

    QInstaller::appendData(&out, input, size);
    if (writeBinaryLayout) {

        QDir resourcePath(QFileInfo(maintenanceToolRenamedName).dir());
#ifdef Q_OS_MACOS
        if (!resourcePath.path().endsWith(QLatin1String("Contents/MacOS")))
            throw Error(tr("Maintenance tool is not a bundle"));
        resourcePath.cdUp();
        resourcePath.cd(QLatin1String("Resources"));
#endif
        // It's a bit odd to have only the magic in the data file, but this simplifies
        // other code a lot (since installers don't have any appended data either)
        QFile dataOut(generateTemporaryFileName());
        QInstaller::openForWrite(&dataOut);
        QInstaller::appendInt64(&dataOut, 0);   // operations start
        QInstaller::appendInt64(&dataOut, 0);   // operations end
        QInstaller::appendInt64(&dataOut, 0);   // resource count
        QInstaller::appendInt64(&dataOut, 4 * sizeof(qint64));   // data block size
        QInstaller::appendInt64(&dataOut, BinaryContent::MagicUninstallerMarker);
        QInstaller::appendInt64(&dataOut, BinaryContent::MagicCookie);

        {
            QFile dummy(resourcePath.filePath(QLatin1String("installer.dat")));
            if (dummy.exists() && !dummy.remove()) {
                throw Error(tr("Cannot remove data file \"%1\": %2").arg(dummy.fileName(),
                    dummy.errorString()));
            }
        }

        if (!dataOut.rename(resourcePath.filePath(QLatin1String("installer.dat")))) {
            throw Error(tr("Cannot write maintenance tool data to %1: %2").arg(dataOut.fileName(),
                dataOut.errorString()));
        }
        setDefaultFilePermissions(&dataOut, DefaultFilePermissions::NonExecutable);
    }

    {
        QFile dummy(maintenanceToolRenamedName);
        if (dummy.exists() && !dummy.remove()) {
            throw Error(tr("Cannot remove data file \"%1\": %2").arg(dummy.fileName(),
                dummy.errorString()));
        }
    }

    if (!out.copy(maintenanceToolRenamedName)) {
        throw Error(tr("Cannot write maintenance tool to \"%1\": %2").arg(maintenanceToolRenamedName,
            out.errorString()));
    }

    QFile mt(maintenanceToolRenamedName);
    if (setDefaultFilePermissions(&mt, DefaultFilePermissions::Executable))
        qCDebug(QInstaller::lcInstallerInstallLog) << "Wrote permissions for maintenance tool.";
    else
        qCWarning(QInstaller::lcInstallerInstallLog) << "Failed to write permissions for maintenance tool.";

    if (out.exists() && !out.remove()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << tr("Cannot remove temporary data file \"%1\": %2")
            .arg(out.fileName(), out.errorString());
    }
}

void PackageManagerCorePrivate::writeMaintenanceToolBinaryData(QFileDevice *output, QFile *const input,
    const OperationList &performedOperations, const BinaryLayout &layout)
{
    const qint64 dataBlockStart = output->pos();

    QVector<Range<qint64> >resourceSegments;
    QVector<Range<qint64> >existingResourceSegments = layout.metaResourceSegments;

    const QString newDefaultResource = m_core->value(QString::fromLatin1("DefaultResourceReplacement"));
    if (!newDefaultResource.isEmpty()) {
        QFile file(newDefaultResource);
        if (file.open(QIODevice::ReadOnly)) {
            resourceSegments.append(Range<qint64>::fromStartAndLength(output->pos(), file.size()));
            QInstaller::appendData(output, &file, file.size());
            existingResourceSegments.remove(0);

            file.remove();  // clear all possible leftovers
            m_core->setValue(QString::fromLatin1("DefaultResourceReplacement"), QString());
        } else {
            qCWarning(QInstaller::lcInstallerInstallLog) << "Cannot replace default resource with"
                << QDir::toNativeSeparators(newDefaultResource);
        }
    }

    foreach (const Range<qint64> &segment, existingResourceSegments) {
        input->seek(segment.start());
        resourceSegments.append(Range<qint64>::fromStartAndLength(output->pos(), segment.length()));
        QInstaller::appendData(output, input, segment.length());
    }

    const qint64 operationsStart = output->pos();
    QInstaller::appendInt64(output, performedOperations.count());
    foreach (Operation *operation, performedOperations) {
        QInstaller::appendString(output, operation->name());
        QInstaller::appendString(output, operation->toXml().toString());

        // for the ui not to get blocked
        qApp->processEvents();
    }
    QInstaller::appendInt64(output, performedOperations.count());
    const qint64 operationsEnd = output->pos();

    // we don't save any component-indexes.
    const qint64 numComponents = 0;
    QInstaller::appendInt64(output, numComponents); // for the indexes
    // we don't save any components.
    const qint64 compIndexStart = output->pos();
    QInstaller::appendInt64(output, numComponents); // and 2 times number of components,
    QInstaller::appendInt64(output, numComponents); // one before and one after the components
    const qint64 compIndexEnd = output->pos();

    QInstaller::appendInt64Range(output, Range<qint64>::fromStartAndEnd(compIndexStart, compIndexEnd)
        .moved(-dataBlockStart));
    foreach (const Range<qint64> segment, resourceSegments)
        QInstaller::appendInt64Range(output, segment.moved(-dataBlockStart));
    QInstaller::appendInt64Range(output, Range<qint64>::fromStartAndEnd(operationsStart, operationsEnd)
        .moved(-dataBlockStart));
    QInstaller::appendInt64(output, layout.metaResourceSegments.count());
    // data block size, from end of .exe to end of file
    QInstaller::appendInt64(output, output->pos() + 3 * sizeof(qint64) -dataBlockStart);
    QInstaller::appendInt64(output, BinaryContent::MagicUninstallerMarker);
}

void PackageManagerCorePrivate::writeMaintenanceTool(OperationList performedOperations)
{
    if (m_disableWriteMaintenanceTool) {
        qCDebug(QInstaller::lcInstallerInstallLog()) << "Maintenance tool writing disabled.";
        return;
    }

    bool gainedAdminRights = false;
    if (!directoryWritable(targetDir())) {
        m_core->gainAdminRights();
        gainedAdminRights = true;
    }

    const QString targetAppDirPath = QFileInfo(maintenanceToolName()).path();
    if (!QDir().exists(targetAppDirPath)) {
        // create the directory containing the maintenance tool (like a bundle structure on macOS...)
        Operation *op = createOwnedOperation(QLatin1String("Mkdir"));
        op->setArguments(QStringList() << targetAppDirPath);
        performOperationThreaded(op, Operation::Backup);
        performOperationThreaded(op);
        performedOperations.append(takeOwnedOperation(op));
    }

#ifdef Q_OS_MACOS
    // if it is a bundle, we need some stuff in it...
    const QString sourceAppDirPath = QCoreApplication::applicationDirPath();
    if (isInstaller() && QInstaller::isInBundle(sourceAppDirPath)) {
        Operation *op = createOwnedOperation(QLatin1String("Copy"));
        op->setArguments(QStringList() << (sourceAppDirPath + QLatin1String("/../PkgInfo"))
            << (targetAppDirPath + QLatin1String("/../PkgInfo")));
        performOperationThreaded(op, Operation::Backup);
        performOperationThreaded(op);

        // copy Info.plist to target directory
        op = createOwnedOperation(QLatin1String("Copy"));
        op->setArguments(QStringList() << (sourceAppDirPath + QLatin1String("/../Info.plist"))
            << (targetAppDirPath + QLatin1String("/../Info.plist")));
        performOperationThreaded(op, Operation::Backup);
        performOperationThreaded(op);

        // patch the Info.plist after copying it
        QFile sourcePlist(sourceAppDirPath + QLatin1String("/../Info.plist"));
        QInstaller::openForRead(&sourcePlist);
        QFile targetPlist(targetAppDirPath + QLatin1String("/../Info.plist"));
        QInstaller::openForWrite(&targetPlist);

        QTextStream in(&sourcePlist);
        QTextStream out(&targetPlist);
        const QString before = QLatin1String("<string>") + QFileInfo(QCoreApplication::applicationFilePath())
            .fileName() + QLatin1String("</string>");
        const QString after = QLatin1String("<string>") + QFileInfo(maintenanceToolName()).baseName()
            + QLatin1String("</string>");
        while (!in.atEnd())
            out << in.readLine().replace(before, after) << endl;

        // copy qt_menu.nib if it exists
        op = createOwnedOperation(QLatin1String("Mkdir"));
        op->setArguments(QStringList() << (targetAppDirPath + QLatin1String("/../Resources/qt_menu.nib")));
        if (!op->performOperation()) {
            qCWarning(QInstaller::lcInstallerInstallLog) << "ERROR in Mkdir operation:"
                << op->errorString();
        }

        op = createOwnedOperation(QLatin1String("CopyDirectory"));
        op->setArguments(QStringList() << (sourceAppDirPath + QLatin1String("/../Resources/qt_menu.nib"))
            << (targetAppDirPath + QLatin1String("/../Resources/qt_menu.nib")));
        performOperationThreaded(op);

        op = createOwnedOperation(QLatin1String("Mkdir"));
        op->setArguments(QStringList() << (QFileInfo(targetAppDirPath).path() + QLatin1String("/Resources")));
        performOperationThreaded(op, Operation::Backup);
        performOperationThreaded(op);

        // copy application icons if it exists.
        const QString icon = QFileInfo(QCoreApplication::applicationFilePath()).fileName()
            + QLatin1String(".icns");
        op = createOwnedOperation(QLatin1String("Copy"));
        op->setArguments(QStringList() << (sourceAppDirPath + QLatin1String("/../Resources/") + icon)
            << (targetAppDirPath + QLatin1String("/../Resources/") + icon));
        performOperationThreaded(op, Operation::Backup);
        performOperationThreaded(op);

        // finally, copy everything within Frameworks and plugins
        op = createOwnedOperation(QLatin1String("CopyDirectory"));
        op->setArguments(QStringList() << (sourceAppDirPath + QLatin1String("/../Frameworks"))
            << (targetAppDirPath + QLatin1String("/../Frameworks")));
        performOperationThreaded(op);

        op = createOwnedOperation(QLatin1String("CopyDirectory"));
        op->setArguments(QStringList() << (sourceAppDirPath + QLatin1String("/../plugins"))
            << (targetAppDirPath + QLatin1String("/../plugins")));
        performOperationThreaded(op);
    }
#endif

    try {
        // 1 - check if we have a installer base replacement
        //   |--- if so, write out the new tool and remove the replacement
        //   |--- remember to restart and that we need to replace the original binary
        //
        // 2 - if we do not have a replacement, try to open the binary data file as input
        //   |--- try to read the binary layout
        //      |--- on error (see 2.1)
        //          |--- remember we might to append uncompressed resource data (see 3)
        //          |--- set the installer or maintenance binary as input to take over binary data
        //          |--- in case we did not have a replacement, write out an new maintenance tool binary
        //              |--- remember that we need to replace the original binary
        //
        // 3 - open a new binary data file
        //   |--- try to write out the binary data based on the loaded input file (see 2)
        //      |--- on error (see 3.1)
        //          |--- if we wrote a new maintenance tool, take this as output - if not, write out a new
        //                  one and set it as output file, remember we did this
        //          |--- append the binary data based on the loaded input file (see 2), make sure we force
        //                 uncompressing the resource section if we read from a binary data file (see 4.1).
        //
        // 4 - force a deferred rename on the .dat file (see 4.1)
        // 5 - force a deferred rename on the maintenance file (see 5.1)

        // 2.1 - Error cases are: no data file (in fact we are the installer or an old installation),
        //          could not find the data file magic cookie (unknown .dat file), failed to read binary
        //          layout (mostly likely the resource section or we couldn't seek inside the file)
        //
        // 3.1 - most likely the commit operation will fail
        // 4.1 - if 3 failed, this makes sure the .dat file will get removed and on the next run all
        //          binary data is read from the maintenance tool, otherwise it get replaced be the new one
        // 5.1 - this will only happen -if- we wrote out a new binary

        bool newBinaryWritten = false;
        bool replacementExists = false;
        const QString installerBaseBinary = replaceVariables(m_installerBaseBinaryUnreplaced);
        if (!installerBaseBinary.isEmpty() && QFileInfo(installerBaseBinary).exists()) {
            qCDebug(QInstaller::lcInstallerInstallLog) << "Got a replacement installer base binary:"
                << installerBaseBinary;

            QFile replacementBinary(installerBaseBinary);
            try {
                QInstaller::openForRead(&replacementBinary);
                writeMaintenanceToolBinary(&replacementBinary, replacementBinary.size(), true);
                qCDebug(QInstaller::lcInstallerInstallLog) << "Wrote the binary with the new replacement.";

                newBinaryWritten = true;
                replacementExists = true;
            } catch (const Error &error) {
                qCWarning(QInstaller::lcInstallerInstallLog) << error.message();
            }

            if (!replacementBinary.remove()) {
                // Is there anything more sensible we can do with this error? I think not. It's not serious
                // enough for throwing / aborting the process.
                qCDebug(QInstaller::lcInstallerInstallLog) << "Cannot remove installer base binary"
                    << installerBaseBinary << "after updating the maintenance tool:"
                    << replacementBinary.errorString();
            } else {
                qCDebug(QInstaller::lcInstallerInstallLog) << "Removed installer base binary"
                    << installerBaseBinary << "after updating the maintenance tool.";
            }
            m_installerBaseBinaryUnreplaced.clear();
        } else if (!installerBaseBinary.isEmpty() && !QFileInfo(installerBaseBinary).exists()) {
            qCWarning(QInstaller::lcInstallerInstallLog) << "The current maintenance tool could not be updated."
                << installerBaseBinary << "does not exist. Please fix the \"setInstallerBaseBinary"
                "(<temp_installer_base_binary_path>)\" call in your script.";
        }

        QFile input;
        BinaryLayout layout;
        const QString dataFile = targetDir() + QLatin1Char('/') + m_data.settings().maintenanceToolName()
            + QLatin1String(".dat");
        try {
            if (isInstaller()) {
                if (QFile::exists(dataFile)) {
                    qCWarning(QInstaller::lcInstallerInstallLog) << "Found binary data file" << dataFile
                        << "but deliberately not used. Running as installer requires to read the "
                        "resources from the application binary.";
                }
                throw Error();
            }
            input.setFileName(dataFile);
            QInstaller::openForRead(&input);
            layout = BinaryContent::binaryLayout(&input, BinaryContent::MagicCookieDat);
        } catch (const Error &/*error*/) {
            // We are only here when using installer
            QString binaryName = installerBinaryPath();
            // On Mac data is always in a separate file so that the binary can be signed.
            // On other platforms data is in separate file only after install so that the
            // maintenancetool sign does not break.
#ifdef Q_OS_MACOS
            QDir dataPath(QFileInfo(binaryName).dir());
            dataPath.cdUp();
            dataPath.cd(QLatin1String("Resources"));
            input.setFileName(dataPath.filePath(QLatin1String("installer.dat")));
#else
            input.setFileName(binaryName);
#endif
            QInstaller::openForRead(&input);
            layout = BinaryContent::binaryLayout(&input, BinaryContent::MagicCookie);

            if (!newBinaryWritten) {
                newBinaryWritten = true;
                QFile tmp(binaryName);
                QInstaller::openForRead(&tmp);
#ifdef Q_OS_MACOS
                writeMaintenanceToolBinary(&tmp, tmp.size(), true);
#else
                writeMaintenanceToolBinary(&tmp, layout.endOfBinaryContent - layout.binaryContentSize, true);
#endif
            }
        }

        performedOperations = sortOperationsBasedOnComponentDependencies(performedOperations);
        m_core->setValue(QLatin1String("installedOperationAreSorted"), QLatin1String("true"));

        try {
            QFile file(generateTemporaryFileName());
            QInstaller::openForWrite(&file);
            writeMaintenanceToolBinaryData(&file, &input, performedOperations, layout);
            QInstaller::appendInt64(&file, BinaryContent::MagicCookieDat);

            QFile dummy(dataFile + QLatin1String(".new"));
            if (dummy.exists() && !dummy.remove()) {
                throw Error(tr("Cannot remove data file \"%1\": %2").arg(dummy.fileName(),
                    dummy.errorString()));
            }

            if (!file.rename(dataFile + QLatin1String(".new"))) {
                throw Error(tr("Cannot write maintenance tool binary data to %1: %2")
                    .arg(file.fileName(), file.errorString()));
            }
            setDefaultFilePermissions(&file, DefaultFilePermissions::NonExecutable);
        } catch (const Error &/*error*/) {
            if (!newBinaryWritten) {
                newBinaryWritten = true;
                QFile tmp(isInstaller() ? installerBinaryPath() : maintenanceToolName());
                QInstaller::openForRead(&tmp);
                BinaryLayout tmpLayout = BinaryContent::binaryLayout(&tmp, BinaryContent::MagicCookie);
                writeMaintenanceToolBinary(&tmp, tmpLayout.endOfBinaryContent
                    - tmpLayout.binaryContentSize, false);
            }

            QFile file(maintenanceToolName() + QLatin1String(".new"));
            QInstaller::openForAppend(&file);
            file.seek(file.size());
            writeMaintenanceToolBinaryData(&file, &input, performedOperations, layout);
            QInstaller::appendInt64(&file, BinaryContent::MagicCookie);
        }
        input.close();
        if (m_core->isInstaller())
            registerMaintenanceTool();
        writeMaintenanceConfigFiles();

        QFile::remove(dataFile);
        QFile::rename(dataFile + QLatin1String(".new"), dataFile);

        const bool restart = !statusCanceledOrFailed() && m_needsHardRestart;
        qCDebug(QInstaller::lcInstallerInstallLog) << "Maintenance tool hard restart:"
            << (restart ? "true." : "false.");

        if (newBinaryWritten)
            deferredRename(maintenanceToolName() + QLatin1String(".new"), maintenanceToolName(), restart);
        else if (restart)
            SelfRestarter::setRestartOnQuit(true);

    } catch (const Error &err) {
        setStatus(PackageManagerCore::Failure);
        if (gainedAdminRights)
            m_core->dropAdminRights();
        m_needToWriteMaintenanceTool = false;
        throw err;
    }

    if (gainedAdminRights)
        m_core->dropAdminRights();

    commitSessionOperations();

    m_needToWriteMaintenanceTool = false;
}

void PackageManagerCorePrivate::writeOfflineBaseBinary()
{
    qint64 size;
    QFile input(installerBinaryPath());

    QInstaller::openForRead(&input);
#ifndef Q_OS_MACOS
    BinaryLayout layout = BinaryContent::binaryLayout(&input, BinaryContent::MagicCookie);
    size = layout.endOfExectuable;
#else
    // On macOS the data is on a separate file so we can just get the size
    size = input.size();
#endif

    const QString offlineBinaryTempName = offlineBinaryName() + QLatin1String(".new");
    qCDebug(QInstaller::lcInstallerInstallLog) << "Writing offline base binary:" << offlineBinaryTempName;
    ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("Writing offline base binary."));

    QFile out(generateTemporaryFileName());
    QInstaller::openForWrite(&out); // throws an exception in case of error

    if (!input.seek(0))
        throw Error(tr("Failed to seek in file %1: %2").arg(input.fileName(), input.errorString()));

    QInstaller::appendData(&out, &input, size);

    {
        // Check if we have an existing binary, for any reason
        QFile dummy(offlineBinaryTempName);
        if (dummy.exists() && !dummy.remove()) {
            throw Error(tr("Cannot remove file \"%1\": %2").arg(dummy.fileName(),
                dummy.errorString()));
        }
        // Offline binary name might contain non-existing leading directories
        const QString offlineBinaryAbsolutePath = QFileInfo(offlineBinaryTempName).absolutePath();
        QDir dummyDir(offlineBinaryAbsolutePath);
        if (!dummyDir.exists() && !dummyDir.mkpath(offlineBinaryAbsolutePath)) {
            throw Error(tr("Cannot create directory \"%1\".").arg(dummyDir.absolutePath()));
        }
    }

    if (!out.copy(offlineBinaryTempName)) {
        throw Error(tr("Cannot write offline binary to \"%1\": %2").arg(offlineBinaryTempName,
            out.errorString()));
    }

    if (out.exists() && !out.remove()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << tr("Cannot remove temporary file \"%1\": %2")
            .arg(out.fileName(), out.errorString());
    }
}

QString PackageManagerCorePrivate::registerPath()
{
#ifdef Q_OS_WIN
    QString guid = m_data.value(scProductUUID).toString();
    if (guid.isEmpty()) {
        guid = QUuid::createUuid().toString();
        m_data.setValue(scProductUUID, guid);
        writeMaintenanceConfigFiles(); // save uuid persistently
    }

    QString path = QLatin1String("HKEY_CURRENT_USER");
    if (m_data.value(scAllUsers, scFalse).toString() == scTrue)
        path = QLatin1String("HKEY_LOCAL_MACHINE");

    return path + QLatin1String("\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\")
        + guid;
#endif
    return QString();
}

bool PackageManagerCorePrivate::runInstaller()
{
    bool adminRightsGained = false;
    try {
        setStatus(PackageManagerCore::Running);
        emit installationStarted(); // resets also the ProgressCoordninator

        // to have some progress for writeMaintenanceTool
        ProgressCoordinator::instance()->addReservePercentagePoints(1);

        const QString target = QDir::cleanPath(targetDir().replace(QLatin1Char('\\'), QLatin1Char('/')));
        if (target.isEmpty())
            throw Error(tr("Variable 'TargetDir' not set."));

        if (!QDir(target).exists()) {
            const QString &pathToTarget = target.mid(0, target.lastIndexOf(QLatin1Char('/')));
            if (!QDir(pathToTarget).exists()) {
                Operation *pathToTargetOp = createOwnedOperation(QLatin1String("Mkdir"));
                pathToTargetOp->setArguments(QStringList() << pathToTarget);
                if (!performOperationThreaded(pathToTargetOp)) {
                    adminRightsGained = m_core->gainAdminRights();
                    if (!performOperationThreaded(pathToTargetOp))
                        throw Error(pathToTargetOp->errorString());
                }
            }
        } else if (QDir(target).exists()) {
            if (!directoryWritable(targetDir()))
                adminRightsGained = m_core->gainAdminRights();
        }

        // add the operation to create the target directory
        Operation *mkdirOp = createOwnedOperation(QLatin1String("Mkdir"));
        mkdirOp->setArguments(QStringList() << target);
        mkdirOp->setValue(QLatin1String("forceremoval"), true);
        mkdirOp->setValue(QLatin1String("uninstall-only"), true);

        performOperationThreaded(mkdirOp, Operation::Backup);
        if (!performOperationThreaded(mkdirOp)) {
            // if we cannot create the target dir, we try to activate the admin rights
            adminRightsGained = m_core->gainAdminRights();
            if (!performOperationThreaded(mkdirOp))
                throw Error(mkdirOp->errorString());
        }
        setDefaultFilePermissions(target, DefaultFilePermissions::Executable);

        const QString remove = m_core->value(scRemoveTargetDir);
        if (QVariant(remove).toBool())
            addPerformed(takeOwnedOperation(mkdirOp));

        // to show that there was some work
        ProgressCoordinator::instance()->addManualPercentagePoints(1);
        ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("Preparing the installation..."));

        const QList<Component*> componentsToInstall = m_core->orderedComponentsToInstall();
        qCDebug(QInstaller::lcInstallerInstallLog) << "Install size:" << componentsToInstall.size()
            << "components";

        callBeginInstallation(componentsToInstall);
        stopProcessesForUpdates(componentsToInstall);

        if (m_dependsOnLocalInstallerBinary && !KDUpdater::pathIsOnLocalDevice(qApp->applicationFilePath())) {
            throw Error(tr("It is not possible to install from network location"));
        }

        if (!adminRightsGained) {
            foreach (Component *component, componentsToInstall) {
                if (component->value(scRequiresAdminRights, scFalse) == scFalse)
                    continue;

                m_core->gainAdminRights();
                m_core->dropAdminRights();
                break;
            }
        }

        const double downloadPartProgressSize = double(1) / double(3);
        double componentsInstallPartProgressSize = double(2) / double(3);
        const int downloadedArchivesCount = m_core->downloadNeededArchives(downloadPartProgressSize);

        // if there was no download we have the whole progress for installing components
        if (!downloadedArchivesCount)
            componentsInstallPartProgressSize = double(1);

        // Force an update on the components xml as the install dir might have changed.
        m_localPackageHub->setFileName(componentsXmlPath());
        // Clear the packages as we might install into an already existing installation folder.
        m_localPackageHub->clearPackageInfos();
        // also update the application name, might be set from a script as well
        m_localPackageHub->setApplicationName(m_data.value(QLatin1String("ProductName"),
            m_data.settings().applicationName()).toString());
        m_localPackageHub->setApplicationVersion(QLatin1String(QUOTE(IFW_REPOSITORY_FORMAT_VERSION)));

        const int progressOperationCount = countProgressOperations(componentsToInstall)
            // add one more operation as we support progress
            + (PackageManagerCore::createLocalRepositoryFromBinary() ? 1 : 0);
        double progressOperationSize = componentsInstallPartProgressSize / progressOperationCount;

        foreach (Component *component, componentsToInstall)
            installComponent(component, progressOperationSize, adminRightsGained);

        if (m_core->isOfflineOnly() && PackageManagerCore::createLocalRepositoryFromBinary()) {
            emit m_core->titleMessageChanged(tr("Creating local repository"));
            ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(QString());
            ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("Creating local repository"));

            Operation *createRepo = createOwnedOperation(QLatin1String("CreateLocalRepository"));
            if (createRepo) {
                QString binaryFile = QCoreApplication::applicationFilePath();
#ifdef Q_OS_MACOS
                // The installer binary on macOS does not contain the binary content, it's put into
                // the resources folder as separate file. Adjust the actual binary path. No error
                // checking here since we will fail later while reading the binary content.
                QDir resourcePath(QFileInfo(binaryFile).dir());
                resourcePath.cdUp();
                resourcePath.cd(QLatin1String("Resources"));
                binaryFile = resourcePath.filePath(QLatin1String("installer.dat"));
#endif
                createRepo->setValue(QLatin1String("uninstall-only"), true);
                createRepo->setArguments(QStringList() << binaryFile << target
                    + QLatin1String("/repository"));

                connectOperationToInstaller(createRepo, progressOperationSize);

                bool success = performOperationThreaded(createRepo);
                if (!success) {
                    adminRightsGained = m_core->gainAdminRights();
                    success = performOperationThreaded(createRepo);
                }

                if (success) {
                    QSet<Repository> repos;
                    foreach (Repository repo, m_data.settings().defaultRepositories()) {
                        repo.setEnabled(false);
                        repos.insert(repo);
                    }
                    repos.insert(Repository(QUrl::fromUserInput(createRepo
                        ->value(QLatin1String("local-repo")).toString()), true));
                    m_data.settings().setDefaultRepositories(repos);
                    addPerformed(takeOwnedOperation(createRepo));
                } else {
                    MessageBoxHandler::critical(MessageBoxHandler::currentBestSuitParent(),
                        QLatin1String("installationError"), tr("Error"), createRepo->errorString());
                    createRepo->undoOperation();
                }
            }
        }
        emit m_core->titleMessageChanged(tr("Creating Maintenance Tool"));

        m_needToWriteMaintenanceTool = true;
        m_core->writeMaintenanceTool();

        // fake a possible wrong value to show a full progress bar
        const int progress = ProgressCoordinator::instance()->progressInPercentage();
        // usually this should be only the reserved one from the beginning
        if (progress < 100)
            ProgressCoordinator::instance()->addManualPercentagePoints(100 - progress);

        ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("\nInstallation finished!"));

        if (adminRightsGained)
            m_core->dropAdminRights();

        setStatus(PackageManagerCore::Success);
        emit installationFinished();
    } catch (const Error &err) {
        if (m_core->status() != PackageManagerCore::Canceled) {
            setStatus(PackageManagerCore::Failure);
            MessageBoxHandler::critical(MessageBoxHandler::currentBestSuitParent(),
                QLatin1String("installationError"), tr("Error"), err.message());
            qCDebug(QInstaller::lcInstallerInstallLog) << "ROLLING BACK operations="
                << m_performedOperationsCurrentSession.count();
        }

        m_core->rollBackInstallation();

        ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("\nInstallation aborted!"));
        if (adminRightsGained)
            m_core->dropAdminRights();
        emit installationFinished();
        return false;
    }
    return true;
}

bool PackageManagerCorePrivate::runPackageUpdater()
{
    bool adminRightsGained = false;
    if (m_completeUninstall) {
        return runUninstaller();
    }
    try {
        setStatus(PackageManagerCore::Running);
        emit installationStarted(); //resets also the ProgressCoordninator

        //to have some progress for the cleanup/write component.xml step
        ProgressCoordinator::instance()->addReservePercentagePoints(1);

        // check if we need admin rights and ask before the action happens
        if (!directoryWritable(targetDir()))
            adminRightsGained = m_core->gainAdminRights();

        const QList<Component *> componentsToInstall = m_core->orderedComponentsToInstall();
        qCDebug(QInstaller::lcInstallerInstallLog) << "Install size:" << componentsToInstall.size()
            << "components";

        callBeginInstallation(componentsToInstall);
        stopProcessesForUpdates(componentsToInstall);

        if (m_dependsOnLocalInstallerBinary && !KDUpdater::pathIsOnLocalDevice(qApp->applicationFilePath())) {
            throw Error(tr("It is not possible to run that operation from a network location"));
        }

        bool updateAdminRights = false;
        if (!adminRightsGained) {
            foreach (Component *component, componentsToInstall) {
                if (component->value(scRequiresAdminRights, scFalse) == scFalse)
                    continue;

                updateAdminRights = true;
                break;
            }
        }

        OperationList undoOperations;
        OperationList nonRevertedOperations;
        QHash<QString, Component *> componentsByName;

        // order the operations in the right component dependency order
        // next loop will save the needed operations in reverse order for uninstallation
        OperationList performedOperationsOld = m_performedOperationsOld;
        if (m_core->value(QLatin1String("installedOperationAreSorted")) != QLatin1String("true"))
            performedOperationsOld = sortOperationsBasedOnComponentDependencies(m_performedOperationsOld);

        // build a list of undo operations based on the checked state of the component
        foreach (Operation *operation, performedOperationsOld) {
            const QString &name = operation->value(QLatin1String("component")).toString();
            Component *component = componentsByName.value(name, nullptr);
            if (!component)
                component = m_core->componentByName(PackageManagerCore::checkableName(name));
            if (component)
                componentsByName.insert(name, component);

            if (isUpdater()) {
                // We found the component, the component is not scheduled for update, the dependency solver
                // did not add the component as install dependency and there is no replacement, keep it.
                if ((component && !component->updateRequested() && !componentsToInstall.contains(component)
                    && !m_componentsToReplaceUpdaterMode.contains(name))) {
                        nonRevertedOperations.append(operation);
                        continue;
                }

                // There is a replacement, but the replacement is not scheduled for update, keep it as well.
                if (m_componentsToReplaceUpdaterMode.contains(name)
                    && !m_componentsToReplaceUpdaterMode.value(name).first->updateRequested()) {
                        nonRevertedOperations.append(operation);
                        continue;
                }
            } else if (isPackageManager()) {
                // We found the component, the component is still checked and the dependency solver did not
                // add the component as install dependency, keep it.
                if (component
                        && component->installAction() == ComponentModelHelper::KeepInstalled
                        && !componentsToInstall.contains(component)) {
                    nonRevertedOperations.append(operation);
                    continue;
                }

                // There is a replacement, but the replacement is not scheduled for update, keep it as well.
                if (m_componentsToReplaceAllMode.contains(name)
                    && !m_componentsToReplaceAllMode.value(name).first->isSelectedForInstallation()) {
                        nonRevertedOperations.append(operation);
                        continue;
                }
            } else {
                Q_ASSERT_X(false, Q_FUNC_INFO, "Invalid package manager mode!");
            }

            // Filter out the create target dir undo operation, it's only needed for full uninstall.
            // Note: We filter for unnamed operations as well, since old installations had the remove target
            //  dir operation without the "uninstall-only", which will result in a complete uninstallation
            //  during an update for the maintenance tool.
            if (operation->value(QLatin1String("uninstall-only")).toBool()
                || operation->value(QLatin1String("component")).toString().isEmpty()) {
                    nonRevertedOperations.append(operation);
                    continue;
            }

            // uninstallation should be in reverse order so prepend it here
            undoOperations.prepend(operation);
            updateAdminRights |= operation->value(QLatin1String("admin")).toBool();
        }

        // we did not request admin rights till we found out that a component/ undo needs admin rights
        if (updateAdminRights && !adminRightsGained) {
            m_core->gainAdminRights();
            m_core->dropAdminRights();
        }

        double undoOperationProgressSize = 0;
        const double downloadPartProgressSize = double(2) / double(5);
        double componentsInstallPartProgressSize = double(3) / double(5);
        if (undoOperations.count() > 0) {
            undoOperationProgressSize = double(1) / double(5);
            componentsInstallPartProgressSize = downloadPartProgressSize;
            undoOperationProgressSize /= countProgressOperations(undoOperations);
        }

        ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("Preparing the installation..."));

        // following, we download the needed archives
        m_core->downloadNeededArchives(downloadPartProgressSize);

        if (undoOperations.count() > 0) {
            ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("Removing deselected components..."));
            runUndoOperations(undoOperations, undoOperationProgressSize, adminRightsGained, true);
        }
        m_performedOperationsOld = nonRevertedOperations; // these are all operations left: those not reverted

        const double progressOperationCount = countProgressOperations(componentsToInstall);
        const double progressOperationSize = componentsInstallPartProgressSize / progressOperationCount;

        foreach (Component *component, componentsToInstall)
            installComponent(component, progressOperationSize, adminRightsGained);

        emit m_core->titleMessageChanged(tr("Creating Maintenance Tool"));

        commitSessionOperations(); //end session, move ops to "old"
        m_needToWriteMaintenanceTool = true;

        // fake a possible wrong value to show a full progress bar
        const int progress = ProgressCoordinator::instance()->progressInPercentage();
        // usually this should be only the reserved one from the beginning
        if (progress < 100)
            ProgressCoordinator::instance()->addManualPercentagePoints(100 - progress);
        ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("\nUpdate finished!"));

        if (adminRightsGained)
            m_core->dropAdminRights();
        if (m_foundEssentialUpdate)
            setStatus(PackageManagerCore::EssentialUpdated);
        else
            setStatus(PackageManagerCore::Success);
        emit installationFinished();
    } catch (const Error &err) {
        if (m_core->status() != PackageManagerCore::Canceled) {
            setStatus(PackageManagerCore::Failure);
            MessageBoxHandler::critical(MessageBoxHandler::currentBestSuitParent(),
                QLatin1String("installationError"), tr("Error"), err.message());
            qCDebug(QInstaller::lcInstallerInstallLog) << "ROLLING BACK operations="
                << m_performedOperationsCurrentSession.count();
        }

        m_core->rollBackInstallation();

        ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("\nUpdate aborted!"));
        if (adminRightsGained)
            m_core->dropAdminRights();
        emit installationFinished();
        return false;
    }
    return true;
}

bool PackageManagerCorePrivate::runUninstaller()
{
    emit uninstallationStarted();
    bool adminRightsGained = false;

    try {
        setStatus(PackageManagerCore::Running);

        // check if we need to run elevated and ask before the action happens
        if (!directoryWritable(targetDir()))
            adminRightsGained = m_core->gainAdminRights();

        OperationList undoOperations = m_performedOperationsOld;
        std::reverse(undoOperations.begin(), undoOperations.end());

        bool updateAdminRights = false;
        if (!adminRightsGained) {
            foreach (Operation *op, m_performedOperationsOld) {
                updateAdminRights |= op->value(QLatin1String("admin")).toBool();
                if (updateAdminRights)
                    break;  // an operation needs elevation to be able to perform their undo
            }
        }

        // We did not yet request elevated permissions but they are required.
        if (updateAdminRights && !adminRightsGained) {
            m_core->gainAdminRights();
            m_core->dropAdminRights();
        }

        const int uninstallOperationCount = countProgressOperations(undoOperations);
        const double undoOperationProgressSize = double(1) / double(uninstallOperationCount);

        runUndoOperations(undoOperations, undoOperationProgressSize, adminRightsGained, false);
        // No operation delete here, as all old undo operations are deleted in the destructor.

        deleteMaintenanceTool();    // this will also delete the TargetDir on Windows

        // If not on Windows, we need to remove TargetDir manually.
#ifndef Q_OS_WIN
        if (QVariant(m_core->value(scRemoveTargetDir)).toBool() && !targetDir().isEmpty()) {
            if (updateAdminRights && !adminRightsGained)
                adminRightsGained = m_core->gainAdminRights();
            removeDirectoryThreaded(targetDir(), true);
            qCDebug(QInstaller::lcInstallerInstallLog) << "Complete uninstallation was chosen.";
        }
#endif

        unregisterMaintenanceTool();
        m_needToWriteMaintenanceTool = false;
        setStatus(PackageManagerCore::Success);
    } catch (const Error &err) {
        if (m_core->status() != PackageManagerCore::Canceled) {
            setStatus(PackageManagerCore::Failure);
            MessageBoxHandler::critical(MessageBoxHandler::currentBestSuitParent(),
                QLatin1String("installationError"), tr("Error"), err.message());
        }
    }

    const bool success = (m_core->status() == PackageManagerCore::Success);
    if (adminRightsGained)
        m_core->dropAdminRights();

    ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(QString::fromLatin1("\n%1").arg(
        success ? tr("Removal completed successfully.") : tr("Removal aborted.")));

    emit uninstallationFinished();
    return success;
}

bool PackageManagerCorePrivate::runOfflineGenerator()
{
    const QString offlineBinaryTempName = offlineBinaryName() + QLatin1String(".new");
    const QString tempSettingsFilePath = generateTemporaryFileName()
        + QDir::separator() + QLatin1String("config.xml");

    bool adminRightsGained = false;
    try {
        setStatus(PackageManagerCore::Running);
        emit offlineGenerationStarted(); // Resets also the ProgressCoordninator

        // Never write the maintenance tool when generating offline installer
        m_needToWriteMaintenanceTool = false;

        // Reserve some progress for the final writing, it should take
        // only a fraction of time spent in the download part
        ProgressCoordinator::instance()->addReservePercentagePoints(1);

        const QString target = QDir::cleanPath(targetDir().replace(QLatin1Char('\\'), QLatin1Char('/')));
        if (target.isEmpty())
            throw Error(tr("Variable 'TargetDir' not set."));

        // Create target directory for installer to be generated
        if (!QDir(target).exists()) {
            if (!QDir().mkpath(target)) {
                adminRightsGained = m_core->gainAdminRights();
                // Try again with admin privileges
                if (!QDir().mkpath(target))
                    throw Error(tr("Cannot create target directory for installer."));
            }
        } else if (QDir(target).exists()) {
            if (!directoryWritable(targetDir()))
                adminRightsGained = m_core->gainAdminRights();
        }
        setDefaultFilePermissions(target, DefaultFilePermissions::Executable);

        // Show that there was some work
        ProgressCoordinator::instance()->addManualPercentagePoints(1);
        ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("Preparing offline generation..."));

        const QList<Component*> componentsToInclude = m_core->orderedComponentsToInstall();
        qCDebug(QInstaller::lcInstallerInstallLog) << "Included components:" << componentsToInclude.size();

        // Give full part progress size as this is the most time consuming step
        m_core->downloadNeededArchives(double(1));

        const QString installerBaseReplacement = replaceVariables(m_offlineBaseBinaryUnreplaced);
        if (!installerBaseReplacement.isEmpty() && QFileInfo(installerBaseReplacement).exists()) {
            qCDebug(QInstaller::lcInstallerInstallLog) << "Got a replacement installer base binary:"
                << offlineBinaryTempName;

            if (!QFile::copy(installerBaseReplacement, offlineBinaryTempName)) {
                qCWarning(QInstaller::lcInstallerInstallLog) << QString::fromLatin1("Cannot copy "
                    "replacemement binary to temporary location \"%1\" from \"%2\".")
                    .arg(offlineBinaryTempName, installerBaseReplacement);
            }
        } else {
            writeOfflineBaseBinary();
        }

        ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("Preparing installer configuration..."));

        // Create copy of internal config file and data, remove repository related elements
        QInstaller::trimmedCopyConfigData(m_data.settingsFilePath(), tempSettingsFilePath,
            QStringList() << scRemoteRepositories << scRepositoryCategories);

        // Assemble final installer binary
        QInstallerTools::BinaryCreatorArgs args;
        args.target = offlineBinaryName();
#ifdef Q_OS_MACOS
        // Target is a disk image on macOS
        args.target.append(QLatin1String(".dmg"));
#endif
        args.templateBinary = offlineBinaryTempName;
        args.offlineOnly = true;
        args.configFile = tempSettingsFilePath;
        args.ftype = QInstallerTools::Include;
        // Add possible custom resources
        if (!m_offlineGeneratorResourceCollections.isEmpty())
            args.resources = m_offlineGeneratorResourceCollections;

        foreach (auto component, componentsToInclude) {
            args.filteredPackages.append(component->name());
            args.repositoryDirectories.append(component->localTempPath());
        }
        args.repositoryDirectories.removeDuplicates();

        ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("Creating the installer..."));

        QString errorMessage;
        if (QInstallerTools::createBinary(args, errorMessage) == EXIT_FAILURE) {
            throw Error(tr("Failed to create offline installer. %1").arg(errorMessage));
        } else {
            setStatus(PackageManagerCore::Success);
        }

        // Fake a possible wrong value to show a full progress
        const int progress = ProgressCoordinator::instance()->progressInPercentage();
        // This should be only the reserved one (1) from the beginning
        if (progress < 100)
            ProgressCoordinator::instance()->addManualPercentagePoints(100 - progress);

    } catch (const Error &err) {
        if (m_core->status() != PackageManagerCore::Canceled) {
            setStatus(PackageManagerCore::Failure);
            MessageBoxHandler::critical(MessageBoxHandler::currentBestSuitParent(),
                QLatin1String("installationError"), tr("Error"), err.message());
        }
    }
    QFile tempBinary(offlineBinaryTempName);
    if (tempBinary.exists() && !tempBinary.remove()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << tr("Cannot remove temporary file \"%1\": %2")
            .arg(tempBinary.fileName(), tempBinary.errorString());
    }

    QDir tempSettingsDir(QFileInfo(tempSettingsFilePath).absolutePath());
    if (tempSettingsDir.exists() && !tempSettingsDir.removeRecursively()) {
        qCWarning(QInstaller::lcInstallerInstallLog) << tr("Cannot remove temporary directory \"%1\".")
            .arg(tempSettingsDir.path());
    }

    const bool success = m_core->status() == PackageManagerCore::Success;
    if (adminRightsGained)
        m_core->dropAdminRights();

    ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(QString::fromLatin1("\n%1").arg(
        success ? tr("Offline generation completed successfully.") : tr("Offline generation aborted!")));

    emit offlineGenerationFinished();
    return success;
}

void PackageManagerCorePrivate::installComponent(Component *component, double progressOperationSize,
    bool adminRightsGained)
{
    const OperationList operations = component->operations();
    if (!component->operationsCreatedSuccessfully())
        m_core->setCanceled();

    const int opCount = operations.count();
    // show only components which do something, MinimumProgress is only for progress calculation safeness
    bool showDetailsLog = false;
    if (opCount > 1 || (opCount == 1 && operations.at(0)->name() != QLatin1String("MinimumProgress"))) {
        ProgressCoordinator::instance()->emitLabelAndDetailTextChanged(tr("\nInstalling component %1")
            .arg(component->displayName()));
        showDetailsLog = true;
    }

    foreach (Operation *operation, operations) {
        if (statusCanceledOrFailed())
            throw Error(tr("Installation canceled by user"));

        // maybe this operations wants us to be admin...
        bool becameAdmin = false;
        if (!adminRightsGained && operation->value(QLatin1String("admin")).toBool()) {
            becameAdmin = m_core->gainAdminRights();
            qCDebug(QInstaller::lcInstallerInstallLog) << operation->name() << "as admin:" << becameAdmin;
        }

        connectOperationToInstaller(operation, progressOperationSize);
        connectOperationCallMethodRequest(operation);

        // allow the operation to backup stuff before performing the operation
        performOperationThreaded(operation, Operation::Backup);

        bool ignoreError = false;
        bool ok = performOperationThreaded(operation);
        while (!ok && !ignoreError && m_core->status() != PackageManagerCore::Canceled) {
            qCDebug(QInstaller::lcInstallerInstallLog) << QString::fromLatin1("Operation \"%1\" with arguments "
                "\"%2\" failed: %3").arg(operation->name(), operation->arguments()
                .join(QLatin1String("; ")), operation->errorString());
            const QMessageBox::StandardButton button =
                MessageBoxHandler::warning(MessageBoxHandler::currentBestSuitParent(),
                QLatin1String("installationErrorWithCancel"), tr("Installer Error"),
                tr("Error during installation process (%1):\n%2").arg(component->name(),
                operation->errorString()),
                QMessageBox::Retry | QMessageBox::Ignore | QMessageBox::Cancel, QMessageBox::Cancel);

            if (button == QMessageBox::Retry)
                ok = performOperationThreaded(operation);
            else if (button == QMessageBox::Ignore)
                ignoreError = true;
            else if (button == QMessageBox::Cancel)
                m_core->interrupt();
        }

        if (ok || operation->error() > Operation::InvalidArguments) {
            // Remember that the operation was performed, that allows us to undo it if a following operation
            // fails or if this operation failed but still needs an undo call to cleanup.
            addPerformed(operation);
        }

        if (becameAdmin)
            m_core->dropAdminRights();

        if (!ok && !ignoreError)
            throw Error(operation->errorString());

        if (!m_core->isCommandLineInstance()) {
            if ((component->value(scEssential, scFalse) == scTrue) && !isInstaller())
                m_needsHardRestart = true;
            else if ((component->value(scForcedUpdate, scFalse) == scTrue) && isUpdater())
                m_needsHardRestart = true;
        }
    }

    registerPathsForUninstallation(component->pathsForUninstallation(), component->name());

    if (!component->stopProcessForUpdateRequests().isEmpty()) {
        Operation *stopProcessForUpdatesOp = KDUpdater::UpdateOperationFactory::instance()
            .create(QLatin1String("FakeStopProcessForUpdate"), m_core);
        const QStringList arguments(component->stopProcessForUpdateRequests().join(QLatin1String(",")));
        stopProcessForUpdatesOp->setArguments(arguments);
        addPerformed(stopProcessForUpdatesOp);
        stopProcessForUpdatesOp->setValue(QLatin1String("component"), component->name());
    }

    // now mark the component as installed
    m_localPackageHub->addPackage(component->name(),
                                  component->value(scVersion),
                                  component->value(scDisplayName),
                                  QPair<QString, bool>(component->value(scTreeName),
                                                       component->treeNameMoveChildren()),
                                  component->value(scDescription),
                                  component->dependencies(),
                                  component->autoDependencies(),
                                  component->forcedInstallation(),
                                  component->isVirtual(),
                                  component->value(scUncompressedSize).toULongLong(),
                                  component->value(scInheritVersion),
                                  component->isCheckable(),
                                  component->isExpandedByDefault(),
                                  component->value(scContentSha1));
    m_localPackageHub->writeToDisk();

    component->setInstalled();
    component->markAsPerformedInstallation();

    if (showDetailsLog)
        ProgressCoordinator::instance()->emitDetailTextChanged(tr("Done"));
}

bool PackageManagerCorePrivate::runningProcessesFound()
{
    //Check if there are processes running in the install
    QStringList excludeFiles = m_allowedRunningProcesses;
    excludeFiles.append(maintenanceToolName());

    const QString performModeWarning = m_completeUninstall
        ? QLatin1String("Unable to remove components.")
        : QLatin1String("Unable to update components.");

    QStringList runningProcesses = runningInstallerProcesses(excludeFiles);
    if (!runningProcesses.isEmpty()) {
        qCWarning(QInstaller::lcInstallerInstallLog).noquote().nospace() << performModeWarning
                 << " Please stop these processes: " << runningProcesses << " and try again.";
        return true;
    }
    return false;
}

void PackageManagerCorePrivate::setComponentSelection(const QString &id, Qt::CheckState state)
{
    ComponentModel *model = m_core->isUpdater() ? m_core->updaterComponentModel() : m_core->defaultComponentModel();
    Component *component = m_core->componentByName(id);
    if (!component) {
        qCWarning(QInstaller::lcInstallerInstallLog).nospace()
            << "Unable to set selection for: " << id << ". Component not found.";
        return;
    }
    const QModelIndex &idx = model->indexFromComponentName(component->treeName());
    if (idx.isValid())
        model->setData(idx, state, Qt::CheckStateRole);
}

// -- private

void PackageManagerCorePrivate::deleteMaintenanceTool()
{
#ifdef Q_OS_WIN
    // Since Windows does not support that the maintenance tool deletes itself we have to go with a rather dirty
    // hack. What we do is to create a batchfile that will try to remove the maintenance tool once per second. Then
    // we start that batchfile detached, finished our job and close ourselves. Once that's done the batchfile
    // will succeed in deleting our uninstall.exe and, if the installation directory was created but us and if
    // it's empty after the uninstall, deletes the installation-directory.
    const QString batchfile = QDir::toNativeSeparators(QFileInfo(QDir::tempPath(),
        QLatin1String("uninstall.vbs")).absoluteFilePath());
    QFile f(batchfile);
    if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
        throw Error(tr("Cannot prepare removal"));

    QTextStream batch(&f);
    batch << "Set fso = WScript.CreateObject(\"Scripting.FileSystemObject\")\n";
    batch << "file = WScript.Arguments.Item(0)\n";
    batch << "folderpath = WScript.Arguments.Item(1)\n";
    batch << "Set folder = fso.GetFolder(folderpath)\n";
    batch << "on error resume next\n";

    batch << "while fso.FileExists(file)\n";
    batch << "    fso.DeleteFile(file)\n";
    batch << "    WScript.Sleep(1000)\n";
    batch << "wend\n";
//    batch << "if folder.SubFolders.Count = 0 and folder.Files.Count = 0 then\n";
    batch << "    Set folder = Nothing\n";
    batch << "    fso.DeleteFolder folderpath, true\n";
//    batch << "end if\n";
    batch << "fso.DeleteFile(WScript.ScriptFullName)\n";

    f.close();

    QStringList arguments;
    arguments << QLatin1String("//Nologo") << batchfile; // execute the batchfile
    arguments << QDir::toNativeSeparators(QFileInfo(installerBinaryPath()).absoluteFilePath());
    if (!m_performedOperationsOld.isEmpty()) {
        const Operation *const op = m_performedOperationsOld.first();
        if (op->name() == QLatin1String("Mkdir")) // the target directory name
            arguments << QDir::toNativeSeparators(QFileInfo(op->arguments().first()).absoluteFilePath());
    }

    if (!QProcessWrapper::startDetached(QLatin1String("cscript"), arguments, QDir::rootPath()))
        throw Error(tr("Cannot start removal"));
#else
    // every other platform has no problem if we just delete ourselves now
    QFile maintenanceTool(QFileInfo(installerBinaryPath()).absoluteFilePath());
    maintenanceTool.remove();
# ifdef Q_OS_MACOS
    if (QInstaller::isInBundle(installerBinaryPath())) {
        const QLatin1String cdUp("/../../..");
        removeDirectoryThreaded(QFileInfo(installerBinaryPath() + cdUp).absoluteFilePath());
        QFile::remove(QFileInfo(installerBinaryPath() + cdUp).absolutePath()
            + QLatin1String("/") + configurationFileName());
    } else
# endif
#endif
    {
        // finally remove the components.xml, since it still exists now
        QFile::remove(QFileInfo(installerBinaryPath()).absolutePath() + QLatin1String("/")
            + configurationFileName());
    }
}

void PackageManagerCorePrivate::registerMaintenanceTool()
{
#ifdef Q_OS_WIN
    QSettingsWrapper settings(registerPath(), QSettingsWrapper::NativeFormat);
    settings.setValue(scDisplayName, m_data.value(QLatin1String("ProductName")));
    settings.setValue(QLatin1String("DisplayVersion"), m_data.value(QLatin1String("ProductVersion")));
    const QString maintenanceTool = QDir::toNativeSeparators(maintenanceToolName());
    settings.setValue(QLatin1String("DisplayIcon"), maintenanceTool);
    settings.setValue(scPublisher, m_data.value(scPublisher));
    settings.setValue(QLatin1String("UrlInfoAbout"), m_data.value(QLatin1String("Url")));
    settings.setValue(QLatin1String("Comments"), m_data.value(scTitle));
    settings.setValue(QLatin1String("InstallDate"), QDateTime::currentDateTime().toString());
    settings.setValue(QLatin1String("InstallLocation"), QDir::toNativeSeparators(targetDir()));
    settings.setValue(QLatin1String("UninstallString"), maintenanceTool);
    settings.setValue(QLatin1String("ModifyPath"), QString(maintenanceTool
        + QLatin1String(" --manage-packages")));
    // required disk space of the installed components
    quint64 estimatedSizeKB = m_core->requiredDiskSpace() / 1024;
    // add required space for the maintenance tool
    estimatedSizeKB += QFileInfo(maintenanceTool).size() / 1024;
    if (m_core->createLocalRepositoryFromBinary()) {
        // add required space for a local repository
        quint64 result(0);
        foreach (QInstaller::Component *component,
            m_core->components(PackageManagerCore::ComponentType::All)) {
            result += m_core->size(component, scCompressedSize);
        }
        estimatedSizeKB += result / 1024;
    }
    // Windows can only handle 32bit REG_DWORD (max. recordable installation size is 4TiB)
    const quint64 limit = std::numeric_limits<quint32>::max(); // maximum 32 bit value
    if (estimatedSizeKB <= limit)
        settings.setValue(QLatin1String("EstimatedSize"), static_cast<quint32>(estimatedSizeKB));

    const bool supportsModify = m_core->value(scSupportsModify, scTrue) == scTrue;
    if (supportsModify)
        settings.setValue(QLatin1String("NoModify"), 0);
    else
        settings.setValue(QLatin1String("NoModify"), 1);

    settings.setValue(QLatin1String("NoRepair"), 1);
#endif
}

void PackageManagerCorePrivate::unregisterMaintenanceTool()
{
#ifdef Q_OS_WIN
    QSettingsWrapper settings(registerPath(), QSettingsWrapper::NativeFormat);
    settings.remove(QString());
#endif
}

void PackageManagerCorePrivate::runUndoOperations(const OperationList &undoOperations, double progressSize,
    bool adminRightsGained, bool deleteOperation)
{
    try {
        foreach (Operation *undoOperation, undoOperations) {
            if (statusCanceledOrFailed())
                throw Error(tr("Installation canceled by user"));

            bool becameAdmin = false;
            if (!adminRightsGained && undoOperation->value(QLatin1String("admin")).toBool())
                becameAdmin = m_core->gainAdminRights();

            connectOperationToInstaller(undoOperation, progressSize);
            qCDebug(QInstaller::lcInstallerInstallLog) << "undo operation=" << undoOperation->name();

            bool ignoreError = false;
            bool ok = performOperationThreaded(undoOperation, Operation::Undo);

            const QString componentName = undoOperation->value(QLatin1String("component")).toString();

            if (!componentName.isEmpty()) {
                while (!ok && !ignoreError && m_core->status() != PackageManagerCore::Canceled) {
                    const QMessageBox::StandardButton button =
                        MessageBoxHandler::warning(MessageBoxHandler::currentBestSuitParent(),
                        QLatin1String("installationErrorWithIgnore"), tr("Installer Error"),
                        tr("Error during removal process:\n%1").arg(undoOperation->errorString()),
                        QMessageBox::Retry | QMessageBox::Ignore, QMessageBox::Ignore);

                    if (button == QMessageBox::Retry)
                        ok = performOperationThreaded(undoOperation, Operation::Undo);
                    else if (button == QMessageBox::Ignore)
                        ignoreError = true;
                }
                Component *component = m_core->componentByName(PackageManagerCore::checkableName(componentName));
                if (!component)
                    component = componentsToReplace().value(componentName).second;
                if (component) {
                    component->setUninstalled();
                    m_localPackageHub->removePackage(component->name());
                }
            }

            if (becameAdmin)
                m_core->dropAdminRights();

            if (deleteOperation)
                delete undoOperation;
        }
    } catch (const Error &error) {
        m_localPackageHub->writeToDisk();
        throw Error(error.message());
    } catch (...) {
        m_localPackageHub->writeToDisk();
        throw Error(tr("Unknown error"));
    }
    m_localPackageHub->writeToDisk();
}

PackagesList PackageManagerCorePrivate::remotePackages()
{
    if (m_updates && m_updateFinder)
        return m_updateFinder->updates();

    m_updates = false;
    delete m_updateFinder;

    m_updateFinder = new KDUpdater::UpdateFinder;
    m_updateFinder->setAutoDelete(false);
    m_updateFinder->setPackageSources(m_packageSources + m_compressedPackageSources);
    m_updateFinder->setLocalPackageHub(m_localPackageHub);
    m_updateFinder->run();

    if (m_updateFinder->updates().isEmpty()) {
        setStatus(PackageManagerCore::Failure, tr("Cannot retrieve remote tree %1.")
            .arg(m_updateFinder->errorString()));
        return PackagesList();
    }

    m_updates = true;
    return m_updateFinder->updates();
}

/*!
    Returns a hash containing the installed package name and it's associated package information. If
    the application is running in installer mode or the local components file could not be parsed, the
    hash is empty.
*/
LocalPackagesHash PackageManagerCorePrivate::localInstalledPackages()
{
    if (isInstaller())
        return LocalPackagesHash();

    LocalPackagesHash installedPackages;
    if (m_localPackageHub->error() != LocalPackageHub::NoError) {
        if (m_localPackageHub->fileName().isEmpty())
            m_localPackageHub->setFileName(componentsXmlPath());
        else
            m_localPackageHub->refresh();

        if (m_localPackageHub->applicationName().isEmpty())
            m_localPackageHub->setApplicationName(m_data.settings().applicationName());
        if (m_localPackageHub->applicationVersion().isEmpty())
            m_localPackageHub->setApplicationVersion(QLatin1String(QUOTE(IFW_REPOSITORY_FORMAT_VERSION)));
    }

    if (m_localPackageHub->error() != LocalPackageHub::NoError) {
        setStatus(PackageManagerCore::Failure, tr("Failure to read packages from %1.")
            .arg(componentsXmlPath()));
    }

    foreach (const LocalPackage &package, m_localPackageHub->packageInfos()) {
        if (statusCanceledOrFailed())
            break;
        installedPackages.insert(package.name, package);
    }

    return installedPackages;
}

bool PackageManagerCorePrivate::fetchMetaInformationFromRepositories(DownloadType type)
{
    m_updates = false;
    m_repoFetched = false;
    m_updateSourcesAdded = false;

    try {
        m_metadataJob.addDownloadType(type);
        m_metadataJob.start();
        m_metadataJob.waitForFinished();
    } catch (Error &error) {
        setStatus(PackageManagerCore::Failure, tr("Cannot retrieve meta information: %1")
            .arg(error.message()));
        return m_repoFetched;
    }

    if (m_metadataJob.error() != Job::NoError) {
        switch (m_metadataJob.error()) {
            case QInstaller::UserIgnoreError:
                break;  // we can simply ignore this error, the user knows about it
            default:
                setStatus(PackageManagerCore::Failure, m_metadataJob.errorString());
                return m_repoFetched;
        }
    }

    m_repoFetched = true;
    return m_repoFetched;
}

bool PackageManagerCorePrivate::addUpdateResourcesFromRepositories(bool parseChecksum, bool compressedRepository)
{
    if (!compressedRepository && m_updateSourcesAdded)
        return m_updateSourcesAdded;

    const QList<Metadata> metadata = m_metadataJob.metadata();
    if (metadata.isEmpty()) {
        m_updateSourcesAdded = true;
        return m_updateSourcesAdded;
    }
    if (compressedRepository) {
        m_compressedPackageSources.clear();
    }
    else {
        m_packageSources.clear();
        m_updates = false;
        m_updateSourcesAdded = false;
        if (isInstaller())
            m_packageSources.insert(PackageSource(QUrl(QLatin1String("resource://metadata/")), 0));
    }

    foreach (const Metadata &data, metadata) {
        if (compressedRepository && !data.repository.isCompressed()) {
            continue;
        }
        if (statusCanceledOrFailed())
            return false;

        if (data.directory.isEmpty())
            continue;

        if (parseChecksum) {
            const QString updatesXmlPath = data.directory + QLatin1String("/Updates.xml");
            QFile updatesFile(updatesXmlPath);
            try {
                QInstaller::openForRead(&updatesFile);
            } catch(const Error &e) {
                qCWarning(QInstaller::lcInstallerInstallLog) << "Error opening Updates.xml:"
                    << e.message();
                setStatus(PackageManagerCore::Failure, tr("Cannot add temporary update source information."));
                return false;
            }

            int line = 0;
            int column = 0;
            QString error;
            QDomDocument doc;
            if (!doc.setContent(&updatesFile, &error, &line, &column)) {
                qCWarning(QInstaller::lcInstallerInstallLog).nospace() << "Parse error in file "
                    << updatesFile.fileName() << ": " << error << " at line " << line
                    << " col " << column;
                setStatus(PackageManagerCore::Failure, tr("Cannot add temporary update source information."));
                return false;
            }

            const QDomNode checksum = doc.documentElement().firstChildElement(QLatin1String("Checksum"));
            if (!checksum.isNull())
                m_core->setTestChecksum(checksum.toElement().text().toLower() == scTrue);
        }
        if (data.repository.isCompressed())
            m_compressedPackageSources.insert(PackageSource(QUrl::fromLocalFile(data.directory), 2));
        else
            m_packageSources.insert(PackageSource(QUrl::fromLocalFile(data.directory), 1));

        ProductKeyCheck::instance()->addPackagesFromXml(data.directory + QLatin1String("/Updates.xml"));
    }
    if ((compressedRepository && m_compressedPackageSources.count() == 0 ) ||
         (!compressedRepository && m_packageSources.count() == 0)) {
        setStatus(PackageManagerCore::Failure, tr("Cannot find any update source information."));
        return false;
    }

    m_updateSourcesAdded = true;
    return m_updateSourcesAdded;
}

void PackageManagerCorePrivate::restoreCheckState()
{
    if (m_coreCheckedHash.isEmpty())
        return;

    foreach (Component *component, m_coreCheckedHash.keys()) {
        component->setCheckState(m_coreCheckedHash.value(component));
        // Never allow component to be checked when it is unstable
        // and not installed
        if (component->isUnstable() && !component->isInstalled())
            component->setCheckState(Qt::Unchecked);
    }

    m_coreCheckedHash.clear();
    m_componentsToInstallCalculated = false;
}

void PackageManagerCorePrivate::storeCheckState()
{
    m_coreCheckedHash.clear();

   const QList<Component *> components =
       m_core->components(PackageManagerCore::ComponentType::AllNoReplacements);
    foreach (Component *component, components)
        m_coreCheckedHash.insert(component, component->checkState());
}

void PackageManagerCorePrivate::connectOperationCallMethodRequest(Operation *const operation)
{
    QObject *const operationObject = dynamic_cast<QObject *> (operation);
    if (operationObject != nullptr) {
        const QMetaObject *const mo = operationObject->metaObject();
        if (mo->indexOfSignal(QMetaObject::normalizedSignature("requestBlockingExecution(QString)")) > -1) {
            connect(operationObject, SIGNAL(requestBlockingExecution(QString)),
                    this, SLOT(handleMethodInvocationRequest(QString)), Qt::BlockingQueuedConnection);
        }
    }
}

OperationList PackageManagerCorePrivate::sortOperationsBasedOnComponentDependencies(const OperationList &operationList)
{
    OperationList sortedOperations;
    QHash<QString, OperationList> componentOperationHash;

    // sort component unrelated operations to the beginning
    foreach (Operation *operation, operationList) {
        const QString componentName = operation->value(QLatin1String("component")).toString();
        if (componentName.isEmpty())
            sortedOperations.append(operation);
        else
            componentOperationHash[componentName].append(operation);
    }

    Graph<QString> componentGraph;  // create the complete component graph
    foreach (const Component* node, m_core->components(PackageManagerCore::ComponentType::All)) {
        componentGraph.addNode(node->name());
        componentGraph.addEdges(node->name(), m_core->parseNames(node->dependencies()));
    }

    const QStringList resolvedComponents = componentGraph.sort();
    if (componentGraph.hasCycle()) {
        throw Error(tr("Dependency cycle between components \"%1\" and \"%2\" detected.")
            .arg(componentGraph.cycle().first, componentGraph.cycle().second));
    }
    foreach (const QString &componentName, resolvedComponents)
        sortedOperations.append(componentOperationHash.value(componentName));

    return sortedOperations;
}

void PackageManagerCorePrivate::handleMethodInvocationRequest(const QString &invokableMethodName)
{
    QObject *obj = QObject::sender();
    if (obj != nullptr)
        QMetaObject::invokeMethod(obj, qPrintable(invokableMethodName));
}

void PackageManagerCorePrivate::processFilesForDelayedDeletion()
{
    if (m_filesForDelayedDeletion.isEmpty())
        return;

    const QStringList filesForDelayedDeletion = std::move(m_filesForDelayedDeletion);
    foreach (const QString &i, filesForDelayedDeletion) {
        QFile file(i);   //TODO: this should happen asnyc and report errors, I guess
        if (file.exists() && !file.remove()) {
            qCWarning(QInstaller::lcInstallerInstallLog) << "Cannot delete file " << qPrintable(i) <<
                ": " << qPrintable(file.errorString());

            m_filesForDelayedDeletion << i; // try again next time
        }
    }
}

void PackageManagerCorePrivate::findExecutablesRecursive(const QString &path, const QStringList &excludeFiles, QStringList *result)
{
    QDirIterator it(path, QDir::NoDotAndDotDot | QDir::Executable | QDir::Files | QDir::System, QDirIterator::Subdirectories );

    while (it.hasNext())
        result->append(QDir::toNativeSeparators(it.next().toLower()));

    foreach (const QString &process, excludeFiles)
        result->removeAll(QDir::toNativeSeparators(process.toLower()));
}

QStringList PackageManagerCorePrivate::runningInstallerProcesses(const QStringList &excludeFiles)
{
    QStringList resultFiles;
    findExecutablesRecursive(QCoreApplication::applicationDirPath(), excludeFiles, &resultFiles);
    return checkRunningProcessesFromList(resultFiles);
}

bool PackageManagerCorePrivate::calculateComponentsAndRun()
{
    QString htmlOutput;
    bool componentsOk = m_core->calculateComponents(&htmlOutput);
    if (statusCanceledOrFailed()) {
        qCDebug(QInstaller::lcInstallerInstallLog) << "Installation canceled.";
    } else if (componentsOk && acceptLicenseAgreements()) {
        qCDebug(QInstaller::lcInstallerInstallLog).noquote() << htmlToString(htmlOutput);

        QString spaceInfo;
        const bool spaceOk = m_core->checkAvailableSpace(spaceInfo);
        qCDebug(QInstaller::lcInstallerInstallLog) << spaceInfo;

        if (!spaceOk || !(m_autoConfirmCommand || askUserConfirmCommand())) {
            qCDebug(QInstaller::lcInstallerInstallLog) << "Installation aborted.";
        } else if (m_core->run()) {
            // Write maintenance tool if required
            m_core->writeMaintenanceTool();
            return true;
        }
    }
    return false;
}

void PackageManagerCorePrivate::calculateUninstallComponents()
{
    clearUninstallerCalculator();
    const QList<Component *> componentsToInstallList = installerCalculator()->orderedComponentsToInstall();
    QSet<Component*> componentsToInstall(componentsToInstallList.begin(), componentsToInstallList.end());

    QList<Component *> selectedComponentsToUninstall;
    foreach (Component* component, m_core->components(PackageManagerCore::ComponentType::Replacements)) {
        // Uninstall the component if replacement is selected for install or update
        QPair<Component*, Component*> comp = componentsToReplace().value(component->name());
        if (comp.first) {
            if (comp.first->isSelectedForInstallation() || comp.first->updateRequested()) {
                selectedComponentsToUninstall.append(comp.second);
            }
        }
    }
    foreach (Component *component, m_core->components(PackageManagerCore::ComponentType::AllNoReplacements)) {
        if (component->uninstallationRequested() && !componentsToInstallList.contains(component))
            selectedComponentsToUninstall.append(component);
    }
    uninstallerCalculator()->appendComponentsToUninstall(selectedComponentsToUninstall);
}

bool PackageManagerCorePrivate::acceptLicenseAgreements() const
{
    // Always skip for uninstaller
    if (isUninstaller())
        return true;

    foreach (Component *component, m_core->orderedComponentsToInstall()) {
        // Package manager or updater, no need to accept again as long as
        // the component is installed.
        if (m_core->isMaintainer() && component->isInstalled())
            continue;
        m_core->addLicenseItem(component->licenses());
    }

    QHash<QString, QMap<QString, QString>> priorityHash = m_core->sortedLicenses();
    QStringList priorities = priorityHash.keys();
    priorities.sort();
    for (int i = priorities.length() - 1; i >= 0; --i) {
        QString priority = priorities.at(i);
        QMap<QString, QString> licenses = priorityHash.value(priority);

        QStringList licenseNames = licenses.keys();
        licenseNames.sort(Qt::CaseInsensitive);
        for (QString licenseName : licenseNames) {
            if (m_autoAcceptLicenses
                    || askUserAcceptLicense(licenseName, licenses.value(licenseName))) {
                qCDebug(QInstaller::lcInstallerInstallLog) << "License"
                    << licenseName << "accepted by user.";
            } else {
                qCDebug(QInstaller::lcInstallerInstallLog) << "License"
                    << licenseName<< "not accepted by user. Aborting.";
                return false;
            }
        }
    }
    return true;
}

bool PackageManagerCorePrivate::askUserAcceptLicense(const QString &name, const QString &content) const
{
    qCDebug(QInstaller::lcInstallerInstallLog) << "You must accept "
        "the terms contained in the following license agreement "
        "before continuing with the installation:" << name;

    forever {
        const QString input = m_core->readConsoleLine(QLatin1String("Accept|Reject|Show"));

        if (QString::compare(input, QLatin1String("Accept"), Qt::CaseInsensitive) == 0
                || QString::compare(input, QLatin1String("A"), Qt::CaseInsensitive) == 0) {
            return true;
        } else if (QString::compare(input, QLatin1String("Reject"), Qt::CaseInsensitive) == 0
                || QString::compare(input, QLatin1String("R"), Qt::CaseInsensitive) == 0) {
            return false;
        } else if (QString::compare(input, QLatin1String("Show"), Qt::CaseInsensitive) == 0
                || QString::compare(input, QLatin1String("S"), Qt::CaseInsensitive) == 0) {
            qCDebug(QInstaller::lcInstallerInstallLog).noquote() << content;
        } else {
            qCDebug(QInstaller::lcInstallerInstallLog) << "Unknown answer:" << input;
        }
    }
}

bool PackageManagerCorePrivate::askUserConfirmCommand() const
{
    qCDebug(QInstaller::lcInstallerInstallLog) << "Do you want to continue?";

    forever {
        const QString input = m_core->readConsoleLine(QLatin1String("Yes|No"));

        if (QString::compare(input, QLatin1String("Yes"), Qt::CaseInsensitive) == 0
                || QString::compare(input, QLatin1String("Y"), Qt::CaseInsensitive) == 0) {
            return true;
        } else if (QString::compare(input, QLatin1String("No"), Qt::CaseInsensitive) == 0
                || QString::compare(input, QLatin1String("N"), Qt::CaseInsensitive) == 0) {
            return false;
        } else {
            qCDebug(QInstaller::lcInstallerInstallLog) << "Unknown answer:" << input;
        }
    }
}

bool PackageManagerCorePrivate::packageNeedsUpdate(const LocalPackage &localPackage, const Package *update) const
{
    bool updateNeeded = true;
    const QString contentSha1 = update->data(scContentSha1).toString();
    if (!contentSha1.isEmpty()) {
        if (contentSha1 == localPackage.contentSha1)
            updateNeeded = false;
    } else {
        const QString updateVersion = update->data(scVersion).toString();
        if (KDUpdater::compareVersion(updateVersion, localPackage.version) <= 0)
            updateNeeded = false;
    }
    return updateNeeded;
}

void PackageManagerCorePrivate::commitPendingUnstableComponents()
{
    if (m_pendingUnstableComponents.isEmpty())
        return;

    for (auto &componentName : m_pendingUnstableComponents.keys()) {
        Component *const component = m_core->componentByName(componentName);
        if (!component) {
            qCWarning(lcInstallerInstallLog) << "Failure while marking component "
                "unstable. No such component exists:" << componentName;
            continue;
        }

        const QPair<Component::UnstableError, QString> unstableError
            = m_pendingUnstableComponents.value(componentName);

        component->setUnstable(unstableError.first, unstableError.second);
    }
    m_pendingUnstableComponents.clear();
}

} // namespace QInstaller