Welcome to mirror list, hosted at ThFree Co, Russian Federation.

x2go_plugin.c « x2go « plugins - gitlab.com/Remmina/Remmina.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8f26f56b827f2e74a68c435d2cce7cd4456fa95d (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
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
/*
 *     Project: Remmina Plugin X2Go
 * Description: Remmina protocol plugin to connect via X2Go using PyHocaCLI
 *      Author: Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
 *              Antenore Gatta <antenore@simbiosi.org>
 *   Copyright: 2010-2011 Vic Lee
 *              2014-2015 Antenore Gatta, Fabio Castelli, Giovanni Panozzo
 *              2015 Antenore Gatta
 *              2016-2018 Antenore Gatta, Giovanni Panozzo
 *              2019 Mike Gabriel
 *              2021 Daniel Teichmann
 *     License: GPL-2+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 *  In addition, as a special exception, the copyright holders give
 *  permission to link the code of portions of this program with the
 *  OpenSSL library under certain conditions as described in each
 *  individual source file, and distribute linked combinations
 *  including the two.
 *  You must obey the GNU General Public License in all respects
 *  for all of the code used other than OpenSSL. *  If you modify
 *  file(s) with this exception, you may extend this exception to your
 *  version of the file(s), but you are not obligated to do so. *  If you
 *  do not wish to do so, delete this exception statement from your
 *  version. *  If you delete this exception statement from all source
 *  files in the program, then also delete it here.
 *
 */

#include "x2go_plugin.h"
#include "common/remmina_plugin.h"

#include <gtk/gtkx.h>
#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#else
#include <gdk/gdkwayland.h>
#endif

#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XKBrules.h>

#include <sys/types.h>
#include <signal.h>
#include <time.h>
#include <ctype.h>

#define FEATURE_AVAILABLE(gpdata, feature) \
		gpdata->available_features ? (g_list_find_custom( \
						gpdata->available_features, \
						feature, \
						(GCompareFunc) g_strcmp0 \
					     ) ? TRUE : FALSE) : FALSE

#define FEATURE_NOT_AVAIL_STR(feature) \
	g_strdup_printf(_("The command-line feature '%s' is not available! Attempting " \
			  "to start PyHoca-CLI without using this feature…"), feature)

#define GET_PLUGIN_DATA(gp) \
		(RemminaPluginX2GoData*) g_object_get_data(G_OBJECT(gp), "plugin-data")

// --------- SESSIONS ------------
#define SET_RESUME_SESSION(gp, resume_data) \
		g_object_set_data_full(G_OBJECT(gp), "resume-session-data", \
				       resume_data, \
			               g_free)

#define GET_RESUME_SESSION(gp) \
		(gchar*) g_object_get_data(G_OBJECT(gp), "resume-session-data")

// A session is selected if the returning value is something other than 0.
#define IS_SESSION_SELECTED(gp) \
		g_object_get_data(G_OBJECT(gp), "session-selected") ? TRUE : FALSE

// We don't use the function as a real pointer but rather as a boolean value.
#define SET_SESSION_SELECTED(gp, is_session_selected) \
		g_object_set_data_full(G_OBJECT(gp), "session-selected", \
				       is_session_selected, \
				       NULL)
// -------------------

#define REMMINA_PLUGIN_INFO(fmt, ...) \
		rm_plugin_service->_remmina_info("[%s] " fmt, \
						 PLUGIN_NAME, ##__VA_ARGS__)

#define REMMINA_PLUGIN_MESSAGE(fmt, ...) \
		rm_plugin_service->_remmina_message("[%s] " fmt, \
						    PLUGIN_NAME, ##__VA_ARGS__)

#define REMMINA_PLUGIN_DEBUG(fmt, ...) \
		rm_plugin_service->_remmina_debug(__func__, "[%s] " fmt, \
						  PLUGIN_NAME, ##__VA_ARGS__)

#define REMMINA_PLUGIN_WARNING(fmt, ...) \
		rm_plugin_service->_remmina_warning(__func__, "[%s] " fmt, \
						    PLUGIN_NAME, ##__VA_ARGS__)

#define REMMINA_PLUGIN_AUDIT(fmt, ...) \
		rm_plugin_service->_remmina_audit(__func__, fmt, ##__VA_ARGS__)

#define REMMINA_PLUGIN_ERROR(fmt, ...) \
		rm_plugin_service->_remmina_error(__func__, "[%s] " fmt, \
						  PLUGIN_NAME, ##__VA_ARGS__)

#define REMMINA_PLUGIN_CRITICAL(fmt, ...) \
		rm_plugin_service->_remmina_critical(__func__, "[%s] " fmt, \
						     PLUGIN_NAME, ##__VA_ARGS__)

#define GET_PLUGIN_STRING(value) \
		g_strdup(rm_plugin_service->file_get_string(remminafile, value))

#define GET_PLUGIN_PASSWORD(value) \
		GET_PLUGIN_STRING(value)

#define GET_PLUGIN_INT(value, default_value) \
		rm_plugin_service->file_get_int(remminafile, value, default_value)

#define GET_PLUGIN_BOOLEAN(value) \
		rm_plugin_service->file_get_int(remminafile, value, FALSE)

static RemminaPluginService *rm_plugin_service = NULL;

typedef struct _RemminaPluginX2GoData {
	GtkWidget *socket;
	gint socket_id;

	pthread_t thread;

	Display *display;
	Window window_id;
	int (*orig_handler)(Display *, XErrorEvent *);

	GPid pidx2go;

	gboolean disconnected;

	GList* available_features;
} RemminaPluginX2GoData;

/**
 * @brief Can be used to pass custom user data between functions and threads.
 *	  *AND* pass the useful RemminaProtocolWidget with it along.
 */
typedef struct _X2GoCustomUserData {
	RemminaProtocolWidget* gp;
	gpointer dialog_data;
	gpointer connect_data;
	gpointer opt1;
	gpointer opt2;
} X2GoCustomUserData;

/**
 * @brief Used for the session chooser dialog (GtkListStore)
 *	  See the example at: https://docs.gtk.org/gtk3/class.ListStore.html
 *	  The order is the exact same as the user sees in the dialog.
 *	  SESSION_NUM_PROPERTIES is used to keep count of the properties
 *	  and it must be the last object.
 */
enum SESSION_PROPERTIES {
	SESSION_DISPLAY = 0,
	SESSION_STATUS,
	SESSION_SESSION_ID,
	SESSION_SUSPENDED_SINCE,
	SESSION_CREATE_DATE,
	SESSION_AGENT_PID,
	SESSION_USERNAME,
	SESSION_HOSTNAME,
	SESSION_COOKIE,
	SESSION_GRAPHIC_PORT,
	SESSION_SND_PORT,
	SESSION_SSHFS_PORT,
	SESSION_DIALOG_IS_VISIBLE,
	SESSION_NUM_PROPERTIES // Must be last. Counts all enum elements.
};

// Following str2int code was adapted from Stackoverflow:
// https://stackoverflow.com/questions/7021725/how-to-convert-a-string-to-integer-in-c
typedef enum _str2int_errno {
	STR2INT_SUCCESS,
	STR2INT_OVERFLOW,
	STR2INT_UNDERFLOW,
	STR2INT_INCONVERTIBLE,
	STR2INT_INVALID_DATA
} str2int_errno;

/**
 * @brief Convert string s to int out.
 *
 * @param out The converted int. Cannot be NULL.
 *
 * @param s Input string to be converted. \n
 * 	    The format is the same as strtol,
 *	    except that the following are inconvertible: \n
 *		* empty string \n
 *		* leading whitespace \n
 *		* or any trailing characters that are not part of the number \n
 *	    Cannot be NULL.
 * @param base Base to interpret string in. Same range as strtol (2 to 36).
 *
 * @return Indicates if the operation succeeded, or why it failed with str2int_errno enum.
 */
str2int_errno str2int(gint *out, gchar *s, gint base)
{
	gchar *end;

	if (!s || !out || base <= 0) return STR2INT_INVALID_DATA;

	if (s[0] == '\0' || isspace(s[0])) return STR2INT_INCONVERTIBLE;

	errno = 0;
	glong l = strtol(s, &end, base);

	/* Both checks are needed because INT_MAX == LONG_MAX is possible. */
	if (l > INT_MAX || (errno == ERANGE && l == LONG_MAX)) return STR2INT_OVERFLOW;
	if (l < INT_MIN || (errno == ERANGE && l == LONG_MIN)) return STR2INT_UNDERFLOW;
	if (*end != '\0') return STR2INT_INCONVERTIBLE;

	*out = l;
	return STR2INT_SUCCESS;
}

/**
 * DialogData:
 * @param flags see GtkDialogFlags
 * @param type see GtkMessageType
 * @param buttons see GtkButtonsType
 * @param title Title of the Dialog
 * @param message Message of the Dialog
 * @param callbackfunc A GCallback function which will be executed on the dialogs
 *		       'response' signal. Allowed to be NULL. \n
 *		       The callback function is obliged to destroy the dialog widget. \n
 * @param dialog_factory A user-defined callback function that is called when it is time
 *			 to build the actual GtkDialog. \n
 *			 Can be used to build custom dialogs. Allowed to be NULL.
 *
 *
 * The `DialogData` structure contains all info needed to open a GTK dialog with
 * rmplugin_x2go_open_dialog()
 *
 * Quick example of a callback function: \n
 * static gboolean rmplugin_x2go_test_callback(RemminaProtocolWidget *gp, gint response_id, \n
 *					       GtkDialog *self) { \n
 *	REMMINA_PLUGIN_DEBUG("response: %i", response_id); \n
 *	if (response_id == GTK_RESPONSE_OK) { \n
 *		REMMINA_PLUGIN_DEBUG("OK!"); \n
 *	} \n
 *	gtk_widget_destroy(self); \n
 *	return G_SOURCE_REMOVE; \n
 * }
 *
 */
struct _DialogData
{
	GtkWindow 	*parent;
	GtkDialogFlags	flags;
	GtkMessageType	type;
	GtkButtonsType	buttons;
	gchar		*title;
	gchar		*message;
	GCallback 	callbackfunc;

	// If the dialog needs to be custom.
	GCallback 	dialog_factory_func;
	gpointer 	dialog_factory_data;
};

/**
 * @param custom_data X2GoCustomUserData structure with the following: \n
 *				gp -> gp (RemminaProtocolWidget*) \n
 *				dialog_data -> dialog data (struct _DialogData*)
 * @returns: FALSE. This source should be removed from main loop.
 * 	     #G_SOURCE_CONTINUE and #G_SOURCE_REMOVE are more memorable
 *	     names for the return value.
 */
static gboolean rmplugin_x2go_open_dialog(X2GoCustomUserData *custom_data)
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	if (!custom_data || !custom_data->gp || !custom_data->dialog_data) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("Parameter 'custom_data' is not initialized!")
		));

		return G_SOURCE_REMOVE;
	}

	RemminaProtocolWidget *gp = (RemminaProtocolWidget*) custom_data->gp;
	struct _DialogData *ddata = (struct _DialogData*) custom_data->dialog_data;

	if (ddata) {
		// Can't check type, flags or buttons
		// because they are enums and '0' is a valid value
		if (!ddata->title || !ddata->message) {
			REMMINA_PLUGIN_CRITICAL("%s", _("Broken `DialogData`! Aborting…"));
			return G_SOURCE_REMOVE;
		}
	} else {
		REMMINA_PLUGIN_CRITICAL("%s", _("Can't retrieve `DialogData`! Aborting…"));
		return G_SOURCE_REMOVE;
	}

	REMMINA_PLUGIN_DEBUG("`DialogData` checks passed. Now showing dialog…");

	GtkWidget* widget_gtk_dialog = NULL;

	if (ddata->dialog_factory_func != NULL) {
		REMMINA_PLUGIN_DEBUG("Calling *custom* dialog factory function…");
		GCallback dialog_factory_func = G_CALLBACK(ddata->dialog_factory_func);
		gpointer  dialog_factory_data = ddata->dialog_factory_data;

		// Calling dialog_factory_func(custom_data, dialog_factory_data);
		widget_gtk_dialog = ((GtkWidget* (*)(X2GoCustomUserData*, gpointer))
			dialog_factory_func)(custom_data, dialog_factory_data);
	} else {
		widget_gtk_dialog = gtk_message_dialog_new(ddata->parent,
							   ddata->flags,
							   ddata->type,
							   ddata->buttons,
							   "%s", ddata->title);

		gtk_message_dialog_format_secondary_text(
			GTK_MESSAGE_DIALOG(widget_gtk_dialog), "%s", ddata->message);
	}

	if (!widget_gtk_dialog) {
		REMMINA_PLUGIN_CRITICAL("Error! Aborting.");
		return G_SOURCE_REMOVE;
	}

	if (ddata->callbackfunc) {
		g_signal_connect_swapped(G_OBJECT(widget_gtk_dialog), "response",
					 G_CALLBACK(ddata->callbackfunc),
					 custom_data);
	} else {
		g_signal_connect(G_OBJECT(widget_gtk_dialog), "response",
				 G_CALLBACK(gtk_widget_destroy),
				 NULL);
	}

	gtk_widget_show_all(widget_gtk_dialog);

	// Delete ddata object and reference 'dialog-data' in gp.
	g_object_set_data(G_OBJECT(gp), "dialog-data", NULL);

	return G_SOURCE_REMOVE;
}

/**
 * @brief These define the responses of session-chooser-dialog's buttons.
 */
enum SESSION_CHOOSER_RESPONSE_TYPE {
  SESSION_CHOOSER_RESPONSE_NEW = 0,
  SESSION_CHOOSER_RESPONSE_CHOOSE,
  SESSION_CHOOSER_RESPONSE_TERMINATE,
};

/**
 * @brief Finds a child GtkWidget of a parent GtkWidget.
 * 	  Copied from https://stackoverflow.com/a/23497087 ;)
 *
 * @param parent Parent GtkWidget*
 * @param name Name string of child. (Must be set before, er else it will be a
 *	       default string)
 * @return GtkWidget*
 */
static GtkWidget* rmplugin_x2go_find_child(GtkWidget* parent, const gchar* name)
{
	const gchar* parent_name = gtk_widget_get_name((GtkWidget*) parent);
	if (g_ascii_strcasecmp(parent_name, (gchar*) name) == 0) {
		return parent;
	}

	if (GTK_IS_BIN(parent)) {
		GtkWidget *child = gtk_bin_get_child(GTK_BIN(parent));
		return rmplugin_x2go_find_child(child, name);
	}

	if (GTK_IS_CONTAINER(parent)) {
		GList *children = gtk_container_get_children(GTK_CONTAINER(parent));
		while (children != NULL) {
			GtkWidget *widget = rmplugin_x2go_find_child(children->data, name);
			if (widget != NULL) {
				return widget;
			}

			children = g_list_next(children);
		}
	}

	return NULL;
}

/**
 * @brief Gets executed on "row-activated" signal. It is emitted when the method when
 *	  the user double clicks a treeview row. It is also emitted when a non-editable
 *	  row is selected and one of the keys: Space, Shift+Space, Return or Enter is
 *	  pressed.
 *
 * @param custom_data X2GoCustomUserData structure with the following: \n
 *				gp -> gp (RemminaProtocolWidget*) \n
 *				opt1 -> dialog widget (GtkWidget*)
 */
static gboolean rmplugin_x2go_session_chooser_row_activated(GtkTreeView *treeview,
							    GtkTreePath *path,
							    GtkTreeViewColumn *column,
							    X2GoCustomUserData *custom_data)
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	if (!custom_data || !custom_data->gp || !custom_data->opt1) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("Parameter 'custom_data' is not initialized!")
		));

		return G_SOURCE_REMOVE;
	}

	RemminaProtocolWidget* gp = (RemminaProtocolWidget*) custom_data->gp;
	// dialog_data (unused)
	// connect_data (unused)
	GtkWidget* dialog = GTK_WIDGET(custom_data->opt1);

	gchar *session_id;
	GtkTreeIter iter;
	GtkTreeModel *model = gtk_tree_view_get_model(treeview);

	if (gtk_tree_model_get_iter(model, &iter, path)) {
		gtk_tree_model_get(GTK_TREE_MODEL(model), &iter,
				   SESSION_SESSION_ID, &session_id, -1);

		// Silent bail out.
		if (!session_id || strlen(session_id) <= 0) return G_SOURCE_REMOVE;

		SET_RESUME_SESSION(gp, session_id);

		// Unstucking main process. Telling it that a session has been selected.
		// We use a trick here. As long as there is something other than 0
		// stored, a session is selected. So we use the gpointer as a gboolean.
		SET_SESSION_SELECTED(gp, (gpointer) TRUE);
		gtk_widget_hide(GTK_WIDGET(dialog));
		gtk_widget_destroy(GTK_WIDGET(dialog));
	}

	return G_SOURCE_REMOVE;
}

/**
 * @brief Translates a session property (described by SESSION_PROPERTIES enum) to a
 *	  string containing it's display name.
 *
 * @param session_property A session property. (as described by SESSION_PROPERTIES enum)
 * @return gchar* Translated display name. (Can be NULL, if session_property is invalid!)
 */
static gchar *rmplugin_x2go_session_property_to_string(guint session_property) {
	gchar* return_char = NULL;

	switch (session_property) {
		// I think we can close one eye here regarding max line-length.
		case SESSION_DISPLAY:		return_char = g_strdup(_("X Display"));		break;
		case SESSION_STATUS:		return_char = g_strdup(_("Status"));		break;
		case SESSION_SESSION_ID:	return_char = g_strdup(_("Session ID"));	break;
		case SESSION_CREATE_DATE:	return_char = g_strdup(_("Create date"));	break;
		case SESSION_SUSPENDED_SINCE:	return_char = g_strdup(_("Suspended since"));	break;
		case SESSION_AGENT_PID:		return_char = g_strdup(_("Agent PID"));		break;
		case SESSION_USERNAME:		return_char = g_strdup(_("Username"));		break;
		case SESSION_HOSTNAME:		return_char = g_strdup(_("Hostname"));		break;
		case SESSION_COOKIE:		return_char = g_strdup(_("Cookie"));		break;
		case SESSION_GRAPHIC_PORT:	return_char = g_strdup(_("Graphic port"));	break;
		case SESSION_SND_PORT:		return_char = g_strdup(_("SND port"));		break;
		case SESSION_SSHFS_PORT:	return_char = g_strdup(_("SSHFS port"));	break;
		case SESSION_DIALOG_IS_VISIBLE:	return_char = g_strdup(_("Visible"));		break;
	}

	return return_char;
}

/**
 * @brief Builds a dialog which contains all found X2Go-Sessions of the remote server.
 *	  The dialog gives the user the option to choose between resuming or terminating
 *	  an existing session or to create a new one.
 *
 * @param custom_data X2GoCustomUserData structure with the following: \n
 *				gp -> gp (RemminaProtocolWidget*) \n
 *				dialog_data -> dialog data (struct _DialogData*) \n
 *				connect_data -> connection data (struct _ConnectionData*)
 * @param sessions_list The GList* Should contain all found X2Go-Sessions.
 *			Sessions are string arrays of properties.
 *			The type of the GList is gchar**.
 *
 * @returns GtkWidget* custom dialog.
 */
static GtkWidget* rmplugin_x2go_choose_session_dialog_factory(X2GoCustomUserData *custom_data,
							      GList *sessions_list)
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	if (!custom_data || !custom_data->gp ||
	    !custom_data->dialog_data || !custom_data->connect_data) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("Parameter 'custom_data' is not initialized!")
		));

		return NULL;
	}

	struct _DialogData* ddata = (struct _DialogData*) custom_data->dialog_data;

	if (!ddata || !sessions_list || !ddata->title) {
		REMMINA_PLUGIN_CRITICAL("%s", _("Couldn't retrieve valid `DialogData` or "
					        "`sessions_list`! Aborting…"));
		return NULL;
	}

	GtkWidget *widget_gtk_dialog = NULL;
	widget_gtk_dialog = gtk_dialog_new_with_buttons(ddata->title, ddata->parent,
							ddata->flags,
	// TRANSLATORS: Stick to x2goclient's translation for terminate.
							_("_Terminate"),
							SESSION_CHOOSER_RESPONSE_TERMINATE,
	// TRANSLATORS: Stick to x2goclient's translation for resume.
							_("_Resume"),
							SESSION_CHOOSER_RESPONSE_CHOOSE,
							_("_New"),
							SESSION_CHOOSER_RESPONSE_NEW,
							NULL);

	GtkWidget *button = gtk_dialog_get_widget_for_response(
						GTK_DIALOG(widget_gtk_dialog),
						SESSION_CHOOSER_RESPONSE_TERMINATE);
	// TRANSLATORS: Tooltip for terminating button inside Session-Chooser-Dialog.
	// TRANSLATORS: Please stick to X2GoClient's way of translating.
	gtk_widget_set_tooltip_text(button, _("Terminating X2Go sessions can take a moment."));

	#define DEFAULT_DIALOG_WIDTH 720
	#define DEFAULT_DIALOG_HEIGHT (DEFAULT_DIALOG_WIDTH * 9) / 16

	gtk_widget_set_size_request(GTK_WIDGET(widget_gtk_dialog),
				    DEFAULT_DIALOG_WIDTH, DEFAULT_DIALOG_HEIGHT);
	gtk_window_set_default_size(GTK_WINDOW(widget_gtk_dialog),
				    DEFAULT_DIALOG_WIDTH, DEFAULT_DIALOG_HEIGHT);

	gtk_window_set_resizable(GTK_WINDOW(widget_gtk_dialog), TRUE);

	GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
	//gtk_widget_show(scrolled_window);

	gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(
				GTK_DIALOG(widget_gtk_dialog))
			   ), GTK_WIDGET(scrolled_window), TRUE, TRUE, 5);

	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolled_window),
				       GTK_POLICY_AUTOMATIC,
				       GTK_POLICY_AUTOMATIC);


	GType types[SESSION_NUM_PROPERTIES];

	// First to last in SESSION_PROPERTIES.
	for (gint i = 0; i < SESSION_NUM_PROPERTIES; ++i) {
		// Everything is a String. (Except IS_VISIBLE flag)
		// If that changes one day, you could extent the if statement here.
		// But you would propably need a *lot* of refactoring.
		// Especially in the session parser.
		if (i == SESSION_DIALOG_IS_VISIBLE) {
			types[i] = G_TYPE_BOOLEAN;
		} else {
			types[i] = G_TYPE_STRING;
		}
	}

	// create tree view
	GtkListStore *store = gtk_list_store_newv(SESSION_NUM_PROPERTIES, types);

	GtkTreeModelFilter *filter = GTK_TREE_MODEL_FILTER(
					gtk_tree_model_filter_new(GTK_TREE_MODEL(store),
								  NULL)
				     );
	gtk_tree_model_filter_set_visible_column(filter, SESSION_DIALOG_IS_VISIBLE);

	GtkWidget *tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(filter));
	g_object_unref (G_OBJECT (store));  // tree now holds reference
	gtk_widget_set_size_request(tree_view, -1, 300);

	// Gets name to be findable by rmplugin_x2go_find_child()
	gtk_widget_set_name(GTK_WIDGET(tree_view), "session_chooser_treeview");

	// create list view columns
	gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree_view), TRUE);
	gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(tree_view), FALSE);
	gtk_tree_view_set_enable_search(GTK_TREE_VIEW(tree_view), TRUE);
	gtk_widget_show (tree_view);
	gtk_container_add (GTK_CONTAINER(scrolled_window), tree_view);

	GtkTreeViewColumn *tree_view_col = NULL;
	GtkCellRenderer *cell_renderer = NULL;
	gchar *header_title = NULL;

	// First to last in SESSION_PROPERTIES.
	for (guint i = 0; i < SESSION_NUM_PROPERTIES; ++i) {
		// Do not display SESSION_DIALOG_IS_VISIBLE.
		if (i == SESSION_DIALOG_IS_VISIBLE) continue;

		header_title = rmplugin_x2go_session_property_to_string(i);
		if (!header_title) {
			REMMINA_PLUGIN_WARNING("%s", g_strdup_printf(
				_("Internal error: %s"), g_strdup_printf(
				_("Unknown property '%i'"), i
			)));
			header_title = g_strdup_printf(_("Unknown property '%i'"), i);
		}

		tree_view_col = gtk_tree_view_column_new();
		gtk_tree_view_column_set_title(tree_view_col, header_title);
		gtk_tree_view_column_set_clickable(tree_view_col, FALSE);
		gtk_tree_view_column_set_sizing (tree_view_col, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
		gtk_tree_view_column_set_resizable(tree_view_col, TRUE);

		cell_renderer = gtk_cell_renderer_text_new();
		gtk_tree_view_column_pack_start(tree_view_col, cell_renderer, TRUE);
		gtk_tree_view_column_add_attribute(tree_view_col, cell_renderer, "text", i);
		gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), tree_view_col);
	}

	GList *elem = NULL;
	GtkTreeIter iter;

	for (elem = sessions_list; elem; elem = elem->next) {
		gchar** session = (gchar**) elem->data;
		g_assert(session != NULL);

		gtk_list_store_append(store, &iter);

		for (gint i = 0; i < SESSION_NUM_PROPERTIES; i++) {
			gchar* property = session[i];
			GValue a = G_VALUE_INIT;

			// Everything here is a string (except SESSION_DIALOG_IS_VISIBLE)

			if (i == SESSION_DIALOG_IS_VISIBLE) {
				g_value_init(&a, G_TYPE_BOOLEAN);
				g_assert(G_VALUE_HOLDS_BOOLEAN(&a) && "GValue does not "
								      "hold a boolean!");
				// Default is to show every new session.
				g_value_set_boolean(&a, TRUE);
			} else {
				g_value_init(&a, G_TYPE_STRING);
				g_assert(G_VALUE_HOLDS_STRING(&a) && "GValue does not "
								      "hold a string!");
				g_value_set_static_string (&a, property);
			}

			gtk_list_store_set_value(store, &iter, i, &a);
		}
	}

	/* Prepare X2GoCustomUserData *custom_data
	 *	gp -> gp (RemminaProtocolWidget*)
	 *	dialog_data -> dialog data (struct _DialogData*)
	 *	connect_data -> connection data (struct _ConnectionData*)
	 *	opt1 -> dialog widget (GtkWidget*)
	 */
	// everything else is already initialized.
	custom_data->opt1 = widget_gtk_dialog;

	g_signal_connect(tree_view, "row-activated",
			 G_CALLBACK(rmplugin_x2go_session_chooser_row_activated),
			 custom_data);

	return widget_gtk_dialog;
}

/**
 * @brief Uses either 'dialog' or 'treeview' to return the GtkTreeModel of the
 *	  Session-Chooser-Dialog. Directly giving 'treeview' as a parameter is faster.
 *	  Only *one* parameter has to be given. The other one can be NULL.
 *	  Error messages are all handled already.
 *
 * @param dialog The Session-Chooser-Dialog itself. (Slower) Can be NULL.
 * @param treeview The GtkTreeView of the Session-Chooser-Dialog. (faster) Can be NULL.
 * @return GtkTreeModelFilter* (Does not contains all rows, only the visible ones!!!) \n
 *	   But you can get all rows with: \n
 *	   	gtk_tree_model_filter_get_model(GTK_TREE_MODEL_FILTER(return_model));
 */
static GtkTreeModelFilter* rmplugin_x2go_session_chooser_get_filter_model(GtkWidget *dialog,
							                  GtkTreeView* treeview)
{
	//REMMINA_PLUGIN_DEBUG("Function entry.");
	GtkTreeModel *return_model = NULL;

	if (!treeview && dialog) {
		GtkWidget *treeview_new = rmplugin_x2go_find_child(GTK_WIDGET(dialog),
						              "session_chooser_treeview");

		if (!treeview_new) {
			REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
				_("Internal error: %s"),
				_("Couldn't find child GtkTreeView of "
				  "session chooser dialog.")
			));
			return NULL;
		}

		return_model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview_new));
	} else if (treeview) {
		return_model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
	} else {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("Neither the 'dialog' nor 'treeview' parameters are initialized! "
			  "At least one of them must be given.")
		));
		return NULL;
	}

	if (!return_model || !GTK_TREE_MODEL_FILTER(return_model)) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("Could not obtain \"GtkTreeModelFilter*\" of the session chooser dialog, "
			  "for unknown reason.")
		));
	}

	return GTK_TREE_MODEL_FILTER(return_model);
}

/**
 * @brief Gets the selected row of the Session-Chooser-Dialog.
 *	  The path gets converted with gtk_tree_model_filter_convert_child_path_to_path()
 *	  before it gets returned. So path describes a row of 'filter' and *not* its
 *	  child GtkTreeModel.
 *
 * @param dialog The Session-Chooser-Dialog.
 * @return GtkTreePath* describing the path to the row.
 */
static GtkTreePath* rmplugin_x2go_session_chooser_get_selected_row(GtkWidget *dialog)
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	GtkWidget *treeview = rmplugin_x2go_find_child(GTK_WIDGET(dialog),
						       "session_chooser_treeview");
	if (!treeview) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("Couldn't find child GtkTreeView of session chooser dialog.")
		));
		return NULL;
	}

	GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
	if (!selection) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("Couldn't get currently selected row (session)!")
		));
		return NULL;
	}

	GtkTreeModelFilter *filter = rmplugin_x2go_session_chooser_get_filter_model(
							   NULL, GTK_TREE_VIEW(treeview));
	GtkTreeModel *model = gtk_tree_model_filter_get_model(filter);
	if (!model) return NULL; // error message was already handled.

	GtkTreeModel *filter_model = GTK_TREE_MODEL(filter);
	g_assert(filter_model && "Could not cast 'filter' to a GtkTreeModel!");
	GList *selected_rows = gtk_tree_selection_get_selected_rows(selection, &filter_model);

	// We only support single selection.
	gint selected_rows_num = gtk_tree_selection_count_selected_rows(selection);
	if (selected_rows_num != 1) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"), g_strdup_printf(
			_("Exactly one session should be selectable but '%i' rows "
			  "(sessions) are selected."),
			selected_rows_num
		)));
		return NULL;
	}

	// This would be very dangerous (we didn't check for NULL) if we hadn't just
	// checked that only one row is selected.
	GtkTreePath *path = selected_rows->data;

	// Convert to be path of GtkTreeModelFilter and *not* its child GtkTreeModel.
	path = gtk_tree_model_filter_convert_child_path_to_path(filter, path);

	return path;
}

/**
 * @brief Finds the GtkTreeView inside of the session chooser dialog,
 *	  determines the selected row and extracts a property.
 *
 * @param dialog GtkWidget* the dialog itself.
 * @param property_index Index of property.
 * @param row A specific row to get the property of. (Can be NULL)
 *
 * @return GValue The value of property. (Can be non-initialized!)
 */
static GValue rmplugin_x2go_session_chooser_get_property(GtkWidget *dialog,
							 gint property_index,
							 GtkTreePath *row)
{
	//REMMINA_PLUGIN_DEBUG("Function entry.");

	GValue ret_value = G_VALUE_INIT;

	if (!row) {
		GtkTreePath *selected_row = rmplugin_x2go_session_chooser_get_selected_row(dialog);
		if (!selected_row) return ret_value; // error message was already handled.
		row = selected_row;
	}

	GtkTreeModelFilter *filter = rmplugin_x2go_session_chooser_get_filter_model(dialog, NULL);
	GtkTreeModel *model = gtk_tree_model_filter_get_model(filter);
	if (!model) return ret_value; // error message was already handled.

	GtkTreeIter iter;
	gboolean success = gtk_tree_model_get_iter(model, &iter, row);
	if (!success) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("Failed to fill 'GtkTreeIter'.")
		));

		return ret_value;
	}

	GValue property = G_VALUE_INIT;
	gtk_tree_model_get_value(model, &iter, property_index, &property);

	return property;
}

/**
 * @brief This function dumps all properties of a session to the console.
 *	  It can/should be used with: \n
 *	      gtk_tree_model_foreach(GTK_TREE_MODEL(model), (GtkTreeModelForeachFunc) \n
 *				     rmplugin_x2go_dump_session_properties, \n
 *				     dialog);
 */
/*static void rmplugin_x2go_dump_session_properties(GtkTreeModel *model, GtkTreePath *path,
						  GtkTreeIter *iter, GtkWidget *dialog)
{
	//REMMINA_PLUGIN_DEBUG("Function entry.");

	g_debug(_("Properties for session with path '%s':"), gtk_tree_path_to_string(path));
	for (guint i = 0; i < SESSION_NUM_PROPERTIES; i++) {
		GValue property = G_VALUE_INIT;
		property = rmplugin_x2go_session_chooser_get_property(dialog, i, path);

		gchar* display_name = rmplugin_x2go_session_property_to_string(i);
		g_assert(display_name && "Couldn't get display name for a property!");

		if (i == SESSION_DIALOG_IS_VISIBLE) {
			g_assert(G_VALUE_HOLDS_BOOLEAN(&property) && "GValue does not "
								      "hold a boolean!");
			g_debug("\t%s: '%s'", display_name,
				g_value_get_boolean(&property) ? "TRUE" : "FALSE");
		} else {
			g_assert(G_VALUE_HOLDS_STRING(&property) && "GValue does not "
								      "hold a string!");
			g_debug("\t%s: '%s'", display_name, g_value_get_string(&property));
		}
	}
}*/

/**
 * @brief This function synchronously spawns a pyhoca-cli process with argv as arguments.
 * @param argc Number of arguments.
 * @param argv Arguments as string array. \n
 *	       Last elements has to be NULL. \n
 *	       Strings will get freed automatically.
 * @param error Will be filled with an error message on fail.
 * @param env String array of enviroment variables. \n
 *	      The list is NULL terminated and each item in
 *	      the list is of the form `NAME=VALUE`.
 *
 * @returns Returns either standard output string or NULL if it failed.
 */
static gchar* rmplugin_x2go_spawn_pyhoca_process(guint argc, gchar* argv[],
						 GError** error, gchar** env)
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	if (!argv) {
		gchar* errmsg = g_strdup_printf(
			_("Internal error: %s"),
			_("parameter 'argv' is 'NULL'.")
		);
		REMMINA_PLUGIN_CRITICAL("%s", errmsg);
		g_set_error(error, 1, 1, "%s", errmsg);
		return NULL;
	}

	if (!error) {
		// Can't report error message back since 'error' is NULL.
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("parameter 'error' is 'NULL'.")
		));
		return NULL;
	}

	if (!env || !env[0]) {
		gchar* errmsg = g_strdup_printf(
			_("Internal error: %s"),
			_("parameter 'env' is either invalid or uninitialized.")
		);
		REMMINA_PLUGIN_CRITICAL("%s", errmsg);
		g_set_error(error, 1, 1, "%s", errmsg);
		return NULL;
	}

	gint exit_code = 0;
	gchar *standard_out;
	// Just supresses pyhoca-cli's help message when pyhoca-cli's version is too old.
	gchar *standard_err;

	gboolean success_ret = g_spawn_sync(NULL, argv, env, G_SPAWN_SEARCH_PATH, NULL,
					    NULL, &standard_out, &standard_err,
					    &exit_code, error);

	REMMINA_PLUGIN_INFO("%s", _("Started PyHoca-CLI with the following arguments:"));
	// Print every argument except passwords. Free all arg strings.
	for (gint i = 0; i < argc - 1; i++) {
		gchar* curr_arg = argv[i];

		if (g_str_equal(curr_arg, "--password") ||
		    g_str_equal(curr_arg, "--ssh-passphrase")) {
			g_printf("%s ", curr_arg);
			g_printf("XXXXXX ");
			g_free(curr_arg);
			g_free(argv[++i]);
			continue;
		} else {
			g_printf("%s ", curr_arg);
			g_free(curr_arg);
		}
	}
	g_printf("\n");

	/* TOO VERBOSE: */
	/*
	REMMINA_PLUGIN_DEBUG("%s", _("Started PyHoca-CLI with the "
				    "following environment variables:"));
	REMMINA_PLUGIN_DEBUG("%s", g_strjoinv("\n", env));
	*/

	if (standard_err && strlen(standard_err) > 0) {
		if (g_str_has_prefix(standard_err, "pyhoca-cli: error: a socket error "
				     "occured while establishing the connection:")) {
			// Log error into GUI.
			gchar* errmsg = g_strdup_printf(
				_("The necessary PyHoca-CLI process has encountered a "
				  "internet connection problem.")
			);

			// Log error into debug window and stdout
			REMMINA_PLUGIN_CRITICAL("%s:\n%s", errmsg, standard_err);
			g_set_error(error, 1, 1, "%s", errmsg);
			return NULL;
		} else {
			gchar* errmsg = g_strdup_printf(
				_("Could not start "
				  "PyHoca-CLI:\n%s"),
				standard_err
			);
			REMMINA_PLUGIN_CRITICAL("%s", errmsg);
			g_set_error(error, 1, 1, "%s", errmsg);
			return NULL;
		}
	} else if (!success_ret || (*error) || strlen(standard_out) <= 0 || exit_code) {
		if (!(*error)) {
			gchar* errmsg = g_strdup_printf(
				_("An unknown error occured while trying "
				  "to start PyHoca-CLI. Exit code: %i"),
				exit_code);
			REMMINA_PLUGIN_WARNING("%s", errmsg);
			g_set_error(error, 1, 1, "%s", errmsg);
		} else {
			gchar* errmsg = g_strdup_printf(
				_("An unknown error occured while trying to start "
				  "PyHoca-CLI. Exit code: %i. Error: '%s'"),
				exit_code, (*error)->message);
			REMMINA_PLUGIN_WARNING("%s", errmsg);
		}

		return NULL;
	}

	return standard_out;
}

/**
 * @brief Stores all necessary information needed for retrieving sessions from
 *	  a X2Go server.
 */
struct _ConnectionData {
	gchar* host;
	gchar* username;
	gchar* password;
	gchar* ssh_privatekey;
	gchar* ssh_passphrase;
};

/**
 * @brief Either sets a specific row visible or invisible.
 *	  Also handles 'terminate' and 'resume' buttons of session-chooser-dialog.
 *	  If there are no sessions available anymore, disable all buttons which are not 'new'
 *	  and if a session is available again, enable them.
 *
 * @param path Describes which row. (GtkTreePath*) \n
 *	       Should be from GtkTreeModelFilter's perspective!
 * @param value TRUE = row is visible & FALSE = row is invisible (gboolean)
 * @param dialog Session-Chooser-Dialog (GtkDialog*)
 * @return Returns TRUE if successful. (gboolean)
 */
static gboolean rmplugin_x2go_session_chooser_set_row_visible(GtkTreePath *path,
							      gboolean value,
							      GtkDialog *dialog) {
	REMMINA_PLUGIN_DEBUG("Function entry.");

	if (!path || !dialog) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("Neither the 'path' nor 'dialog' parameters are initialized.")
		));
		return FALSE;
	}

	GtkTreeModelFilter *filter = rmplugin_x2go_session_chooser_get_filter_model(
								GTK_WIDGET(dialog), NULL);
	GtkTreeModel *model = gtk_tree_model_filter_get_model(filter);

	// error message was already handled.
	if (!model) return FALSE;

	GtkTreeIter iter;
	if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(model), &iter, path)) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("GtkTreePath 'path' describes a non-existing row!")
		));
		return FALSE;
	}


	// Make session either visible or invisible.
	gtk_list_store_set(GTK_LIST_STORE(model), &iter,
			   SESSION_DIALOG_IS_VISIBLE, value, -1);

	// Update row.
	gtk_tree_model_row_changed(GTK_TREE_MODEL(model), path, &iter);

	/* Get IS_VISIBLE flag of a session. */
	// GValue ret_value = G_VALUE_INIT;
	// ret_value = rmplugin_x2go_session_chooser_get_property(GTK_WIDGET(dialog),
	// 						       SESSION_DIALOG_IS_VISIBLE,
	// 						       path);
	// g_debug("Is visible: %s", g_value_get_boolean(&ret_value) ? "TRUE" : "FALSE");


	GtkWidget *term_button = gtk_dialog_get_widget_for_response(
					GTK_DIALOG(dialog),
					SESSION_CHOOSER_RESPONSE_TERMINATE);
	GtkWidget *resume_button = gtk_dialog_get_widget_for_response(
					GTK_DIALOG(dialog),
					SESSION_CHOOSER_RESPONSE_CHOOSE);

	// If no (visible) row is left to terminate disable terminate and resume buttons.
	gint rows_amount = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(filter), NULL);
	if (rows_amount <= 0) {
		gtk_widget_set_sensitive(term_button, FALSE);
		gtk_widget_set_sensitive(resume_button, FALSE);
	} else {
		gtk_widget_set_sensitive(term_button, TRUE);
		gtk_widget_set_sensitive(resume_button, TRUE);
	}

	// Success, yay!
	return TRUE;
}

static gboolean rmplugin_x2go_verify_connection_data(struct _ConnectionData *connect_data) {
	/* Check connect_data. */
	if (!connect_data ||
	    !connect_data->host ||
	    !connect_data->username ||
	    strlen(connect_data->host) <= 0 ||
	    strlen(connect_data->username) <= 0)
	{
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("'Invalid connection data.'")
		));

		return FALSE;
	}

	if (!connect_data->password && (!connect_data->ssh_privatekey ||
	    strlen(connect_data->ssh_privatekey) <= 0)) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("'Invalid connection data.'")
		));
	} else {
		return TRUE;
	}

	return FALSE;
}

/**
 * @brief Terminates a specific X2Go session using pyhoca-cli.
 *
 * @param custom_data X2GoCustomUserData structure with the following: \n
 *				gp -> gp (RemminaProtocolWidget*) \n
 *				dialog_data -> dialog data (struct _DialogData*) \n
 *				connect_data -> connection data (struct _ConnectionData*) \n
 *				opt1 -> selected row (GtkTreePath*) \n
 *				opt2 -> session-selection-dialog (GtkDialog*)
 *
 * @return G_SOURCE_REMOVE (FALSE), #G_SOURCE_CONTINUE and #G_SOURCE_REMOVE are more
 *	   memorable names for the return value. See GLib docs. \n
 *	   https://docs.gtk.org/glib/const.SOURCE_REMOVE.html
 */
static gboolean rmplugin_x2go_pyhoca_terminate_session(X2GoCustomUserData *custom_data)
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	if (!custom_data || !custom_data->gp ||
	    !custom_data->dialog_data || !custom_data->connect_data ||
	    !custom_data->opt1 || !custom_data->opt2) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("Parameter 'custom_data' is not fully initialized!")
		));

		return G_SOURCE_REMOVE;
	}

	// Extract data passed by X2GoCustomUserData *custom_data.
	RemminaPluginX2GoData *gpdata = GET_PLUGIN_DATA(custom_data->gp);
	//struct _DialogData *ddata = (struct _DialogData*) custom_data->dialog_data;
	struct _ConnectionData *connect_data = (struct _ConnectionData*) custom_data->connect_data;
	GtkTreePath* selected_row = (GtkTreePath*) custom_data->opt1;
	GtkDialog *dialog = GTK_DIALOG(custom_data->opt2);

	gchar *host = NULL;
	gchar *username = NULL;
	gchar *password = NULL;
	gchar *ssh_privatekey = NULL;
	gchar *ssh_passphrase = NULL;
	gboolean valid = rmplugin_x2go_verify_connection_data(connect_data);
	if (valid) {
		if (connect_data->password) password = connect_data->password;
		if (connect_data->ssh_privatekey) {
			ssh_privatekey = connect_data->ssh_privatekey;

			if (connect_data->ssh_passphrase) {
				ssh_passphrase = connect_data->ssh_passphrase;
			}
		}

		host = connect_data->host;
		username = connect_data->username;
	} else {
		return G_SOURCE_REMOVE;
	}

	GValue value = rmplugin_x2go_session_chooser_get_property(GTK_WIDGET(dialog),
								SESSION_SESSION_ID,
								selected_row);
	// error message was handled already.
	if (!G_VALUE_HOLDS_STRING(&value)) return G_SOURCE_REMOVE;
	const gchar *session_id = g_value_get_string(&value);

	// We will now start pyhoca-cli with only the '--terminate $SESSION_ID' option.
	// (and of course auth related stuff)
	gchar *argv[50];
	gint argc = 0;

	argv[argc++] = g_strdup("pyhoca-cli");

	argv[argc++] = g_strdup("--server"); // Not listed as feature.
	argv[argc++] = g_strdup_printf("%s", host);

	if (FEATURE_AVAILABLE(gpdata, "USERNAME")) {
		argv[argc++] = g_strdup("-u");
		if (username) {
			argv[argc++] = g_strdup_printf("%s", username);
		} else {
			argv[argc++] = g_strdup_printf("%s", g_get_user_name());
		}
	} else {
		REMMINA_PLUGIN_CRITICAL("%s", FEATURE_NOT_AVAIL_STR("USERNAME"));
		return G_SOURCE_REMOVE;
	}

	if (password && FEATURE_AVAILABLE(gpdata, "PASSWORD")) {
		if (FEATURE_AVAILABLE(gpdata, "AUTH_ATTEMPTS")) {
			argv[argc++] = g_strdup("--auth-attempts");
			argv[argc++] = g_strdup_printf ("%i", 0);
		} else {
			REMMINA_PLUGIN_WARNING("%s", FEATURE_NOT_AVAIL_STR("AUTH_ATTEMPTS"));
		}
		if (strlen(password) > 0) {
			argv[argc++] = g_strdup("--force-password");
			argv[argc++] = g_strdup("--password");
			argv[argc++] = g_strdup_printf("%s", password);
		}
	} else if (!password) {
		REMMINA_PLUGIN_CRITICAL("%s", FEATURE_NOT_AVAIL_STR("PASSWORD"));
		return G_SOURCE_REMOVE;
	}

	if (FEATURE_AVAILABLE(gpdata, "TERMINATE")) {
		argv[argc++] = g_strdup("--terminate");
		argv[argc++] = g_strdup_printf("%s", session_id);
	} else {
		REMMINA_PLUGIN_CRITICAL("%s", FEATURE_NOT_AVAIL_STR("TERMINATE"));
		return G_SOURCE_REMOVE;
	}

	if (FEATURE_AVAILABLE(gpdata, "NON_INTERACTIVE")) {
		argv[argc++] = g_strdup("--non-interactive");
	} else {
		REMMINA_PLUGIN_WARNING("%s", FEATURE_NOT_AVAIL_STR("NON_INTERACTIVE"));
	}

	if (FEATURE_AVAILABLE(gpdata, "SSH_PRIVKEY")) {
		if (ssh_privatekey && !g_str_equal(ssh_privatekey, "")) {
			argv[argc++] = g_strdup("--ssh-privkey");
			argv[argc++] = g_strdup_printf("%s", ssh_privatekey);

			if (ssh_passphrase && !g_str_equal(ssh_passphrase, "")) {
				if (FEATURE_AVAILABLE(gpdata, "SSH_PASSPHRASE")) {
					argv[argc++] = g_strdup("--ssh-passphrase");
					argv[argc++] = g_strdup_printf("%s", ssh_passphrase);
				} else {
					REMMINA_PLUGIN_MESSAGE("%s", FEATURE_NOT_AVAIL_STR("SSH_PASSPHRASE"));
				}
			}
		}
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("SSH_PRIVKEY"));
	}

	argv[argc++] = NULL;

	GError* error = NULL;
	gchar** envp = g_get_environ();
	rmplugin_x2go_spawn_pyhoca_process(argc, argv, &error, envp);
	g_strfreev(envp);

	if (error) {
		gchar *err_msg = g_strdup_printf(
			_("An error occured while trying to terminate X2Go session '%s':\n%s"),
			session_id,
			error->message
		);

		REMMINA_PLUGIN_CRITICAL("%s", err_msg);

		struct _DialogData *err_ddata = g_new0(struct _DialogData, 1);
		err_ddata->parent = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(dialog)));
		err_ddata->flags = GTK_DIALOG_MODAL;
		err_ddata->type = GTK_MESSAGE_ERROR;
		err_ddata->buttons = GTK_BUTTONS_OK;
		err_ddata->title = _("An error occured.");
		err_ddata->message = err_msg;
		// We don't need the response.
		err_ddata->callbackfunc = NULL;
		// We don't need a custom dialog either.
		err_ddata->dialog_factory_func = NULL;
		err_ddata->dialog_factory_data = NULL;

		/* Prepare X2GoCustomUserData *custom_data
		*	gp -> gp (RemminaProtocolWidget*)
		*	dialog_data -> dialog data (struct _DialogData*)
		*/
		custom_data->gp = custom_data->gp;
		custom_data->dialog_data = err_ddata;
		custom_data->connect_data = NULL;
		custom_data->opt1 = NULL;
		custom_data->opt2 = NULL;

		IDLE_ADD((GSourceFunc) rmplugin_x2go_open_dialog, custom_data);

		// Too verbose:
		// GtkTreeModel *model = gtk_tree_model_filter_get_model(
		// 					GTK_TREE_MODEL_FILTER(filter));
		// gtk_tree_model_foreach(GTK_TREE_MODEL(model), (GtkTreeModelForeachFunc)
		//			  rmplugin_x2go_dump_session_properties, dialog);

		// Set row visible again since we couldn't terminate the session.
		if (!rmplugin_x2go_session_chooser_set_row_visible(selected_row, TRUE,
								   dialog)) {
			// error message was already handled.
			return G_SOURCE_REMOVE;
		}
	}

	return G_SOURCE_REMOVE;
}

/**
 * @brief Gets executed on dialog's 'response' signal
 *
 * @param custom_data X2GoCustomUserData*: \n
 *				gp -> gp (RemminaProtocolWidget*) \n
 *				dialog_data -> dialog data (struct _DialogData*) \n
 *				connect_data -> connection data (struct _ConnectionData*)
 * @param response_id See GTK 'response' signal.
 * @param self The dialog itself.
 *
 * @return gboolean, #G_SOURCE_CONTINUE and #G_SOURCE_REMOVE are more memorable
 *	   names for the return value. See GLib docs. \n
 *	   https://docs.gtk.org/glib/const.SOURCE_REMOVE.html
 */
static gboolean rmplugin_x2go_session_chooser_callback(X2GoCustomUserData* custom_data,
						       gint response_id,
						       GtkDialog *self)
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	if (!custom_data || !custom_data->gp || !custom_data->dialog_data ||
	    !custom_data->connect_data) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("Parameter 'custom_data' is not initialized!")
		));

		return G_SOURCE_REMOVE;
	}
	RemminaProtocolWidget *gp = (RemminaProtocolWidget*) custom_data->gp;

	// Don't need to run other stuff, if the user just wants a new session.
	// Also it can happen, that no session is there anymore which can be selected!
	if (response_id == SESSION_CHOOSER_RESPONSE_NEW) {
		REMMINA_PLUGIN_DEBUG("The user explicitly requested a new session. "
			             "Creating a new session…");
		SET_RESUME_SESSION(gp, NULL);

		// Unstucking main process. Telling it that a session has been selected.
		// We use a trick here. As long as there is something other
		// than 0 stored, a session is selected. So we use the gpointer as a gboolean.
		SET_SESSION_SELECTED(gp, (gpointer) TRUE);

		gtk_widget_destroy(GTK_WIDGET(self));

		return G_SOURCE_REMOVE;
	}

	// This assumes that there are sessions which can be selected!
	GValue value = rmplugin_x2go_session_chooser_get_property(
				GTK_WIDGET(self),
				SESSION_SESSION_ID,
				NULL // Let the function search for the selected row.
		       );

	// error message was handled already.
	if (!G_VALUE_HOLDS_STRING(&value)) return G_SOURCE_REMOVE;

	gchar *session_id = (gchar*) g_value_get_string(&value);

	if (response_id == SESSION_CHOOSER_RESPONSE_CHOOSE) {
		if (!session_id || strlen(session_id) <= 0) {
			REMMINA_PLUGIN_DEBUG(
				"%s",
				_("Couldn't get session ID from session chooser dialog.")
			);
			SET_RESUME_SESSION(gp, NULL);
		} else {
			SET_RESUME_SESSION(gp, session_id);

			REMMINA_PLUGIN_MESSAGE("%s", g_strdup_printf(
				_("Resuming session: '%s'"),
				session_id
			));
		}
	} else if (response_id == SESSION_CHOOSER_RESPONSE_TERMINATE) {
		if (!session_id || strlen(session_id) <= 0) {
			REMMINA_PLUGIN_DEBUG(
				"%s",
				_("Couldn't get session ID from session chooser dialog.")
			);
			SET_RESUME_SESSION(gp, NULL);
		} else {
			SET_RESUME_SESSION(gp, session_id);

			REMMINA_PLUGIN_MESSAGE("%s", g_strdup_printf(
				_("Terminating session: '%s'"),
				session_id
			));
		}

		GtkTreePath *path = rmplugin_x2go_session_chooser_get_selected_row(
									GTK_WIDGET(self));
		// error message was already handled.
		if (!path) return G_SOURCE_REMOVE;

		// Actually set row invisible.
		if (!rmplugin_x2go_session_chooser_set_row_visible(path, FALSE, self)) {
			// error message was already handled.
			return G_SOURCE_REMOVE;
		}

		/* Prepare X2GoCustomUserData *custom_data
		 *	gp -> gp (RemminaProtocolWidget*)
		 *	dialog_data -> dialog data (struct _DialogData*)
		 *	connect_data -> connection data (struct _ConnectionData*)
		 *	opt1 -> selected row (GtkTreePath*)
		 *	opt2 -> session selection dialog (GtkDialog*)
		 */
		// everything else is already initialized.
		custom_data->opt1 = path;
		custom_data->opt2 = self;

		// Actually start pyhoca-cli process with --terminate $session_id.
		g_thread_new("terminate-session-thread",
			     (GThreadFunc) rmplugin_x2go_pyhoca_terminate_session,
			     custom_data);

		// Dialog should stay open.
		return G_SOURCE_CONTINUE;
	} else {
		REMMINA_PLUGIN_DEBUG("User clicked dialog away. "
				     "Creating a new session then.");
		SET_RESUME_SESSION(gp, NULL);
	}

	// Unstucking main process. Telling it that a session has been selected.
	// We use a trick here. As long as there is something other
	// than 0 stored, a session is selected. So we use the gpointer as a gboolean.
	SET_SESSION_SELECTED(gp, (gpointer) TRUE);

	gtk_widget_destroy(GTK_WIDGET(self));

	return G_SOURCE_REMOVE;
}

#define RMPLUGIN_X2GO_FEATURE_GTKSOCKET 1

/* Forward declaration */
static RemminaProtocolPlugin rmplugin_x2go;

/* When more than one NX sessions is connecting in progress, we need this mutex and array
 * to prevent them from stealing the same window ID.
 */
static pthread_mutex_t remmina_x2go_init_mutex;
static GArray *remmina_x2go_window_id_array;

/* ------------- Support for execution on main thread of GTK functions ------------- */
struct onMainThread_cb_data
{
	enum { FUNC_GTK_SOCKET_ADD_ID } func;

	GtkSocket* sk;
	Window w;

	/* Mutex for thread synchronization */
	pthread_mutex_t mu;
	/* Flag to catch cancellations */
	gboolean cancelled;
};

static gboolean onMainThread_cb(struct onMainThread_cb_data *d)
{
	TRACE_CALL(__func__);
	if (!d->cancelled) {
		switch (d->func) {
		case FUNC_GTK_SOCKET_ADD_ID:
			gtk_socket_add_id(d->sk, d->w);
			break;
		}
		pthread_mutex_unlock(&d->mu);
	} else {
		/* thread has been cancelled, so we must free d memory here */
		g_free(d);
	}
	return G_SOURCE_REMOVE;
}


static void onMainThread_cleanup_handler(gpointer data)
{
	TRACE_CALL(__func__);
	struct onMainThread_cb_data *d = data;
	d->cancelled = TRUE;
}

static void onMainThread_schedule_callback_and_wait(struct onMainThread_cb_data *d)
{
	TRACE_CALL(__func__);
	d->cancelled = FALSE;
	pthread_cleanup_push(onMainThread_cleanup_handler, d);
	pthread_mutex_init(&d->mu, NULL);
	pthread_mutex_lock(&d->mu);
	gdk_threads_add_idle((GSourceFunc)onMainThread_cb, (gpointer) d);

	pthread_mutex_lock(&d->mu);

	pthread_cleanup_pop(0);
	pthread_mutex_unlock(&d->mu);
	pthread_mutex_destroy(&d->mu);
}

static void onMainThread_gtk_socket_add_id(GtkSocket* sk, Window w)
{
	TRACE_CALL(__func__);

	struct onMainThread_cb_data *d;

	d = g_new0(struct onMainThread_cb_data, 1);
	d->func = FUNC_GTK_SOCKET_ADD_ID;
	d->sk = sk;
	d->w = w;

	onMainThread_schedule_callback_and_wait(d);
	g_free(d);
}
/* /-/-/-/-/-/-/ Support for execution on main thread of GTK functions /-/-/-/-/-/-/ */

static void rmplugin_x2go_remove_window_id (Window window_id)
{
	gint i;
	gboolean already_seen = FALSE;

	pthread_mutex_lock(&remmina_x2go_init_mutex);
	for (i = 0; i < remmina_x2go_window_id_array->len; i++) {
		if (g_array_index(remmina_x2go_window_id_array, Window, i) == window_id) {
			already_seen = TRUE;
			REMMINA_PLUGIN_DEBUG("Window of X2Go Agent with ID [0x%lx] seen already.",
					     window_id);
			break;
		}
	}

	if (already_seen) {
		g_array_remove_index_fast(remmina_x2go_window_id_array, i);
		REMMINA_PLUGIN_DEBUG("Forgetting about window of X2Go Agent with ID [0x%lx]…",
				     window_id);
	}

	pthread_mutex_unlock(&remmina_x2go_init_mutex);
}

/**
 * @returns: FALSE. This source should be removed from main loop.
 * 	     #G_SOURCE_CONTINUE and #G_SOURCE_REMOVE are more memorable
 *	     names for the return value.
 */
static gboolean rmplugin_x2go_cleanup(RemminaProtocolWidget *gp)
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	gchar *server;
	gint port;

	RemminaFile *remminafile = rm_plugin_service->protocol_plugin_get_file(gp);
	rm_plugin_service->get_server_port(rm_plugin_service->file_get_string(remminafile, "server"),
			22,
			&server,
			&port);

	REMMINA_PLUGIN_AUDIT(_("Disconnected from %s:%d via X2Go"), server, port);
	g_free(server), server = NULL;

	RemminaPluginX2GoData *gpdata = GET_PLUGIN_DATA(gp);
	if (gpdata == NULL) {
		REMMINA_PLUGIN_DEBUG("Exiting since gpdata is already 'NULL'…");
		return G_SOURCE_REMOVE;
	}

	if (gpdata->thread) {
		pthread_cancel(gpdata->thread);
		if (gpdata->thread) pthread_join(gpdata->thread, NULL);
	}

	if (gpdata->window_id) {
		rmplugin_x2go_remove_window_id(gpdata->window_id);
	}

	if (gpdata->pidx2go) {
		kill(gpdata->pidx2go, SIGTERM);
		g_spawn_close_pid(gpdata->pidx2go);
		gpdata->pidx2go = 0;
	}

	if (gpdata->display) {
		XSetErrorHandler(gpdata->orig_handler);
		XCloseDisplay(gpdata->display);
		gpdata->display = NULL;
	}

	g_object_steal_data(G_OBJECT(gp), "plugin-data");
	rm_plugin_service->protocol_plugin_signal_connection_closed(gp);

	return G_SOURCE_REMOVE;
}

static gboolean rmplugin_x2go_close_connection(RemminaProtocolWidget *gp)
{
	TRACE_CALL(__func__);
	RemminaPluginX2GoData *gpdata = GET_PLUGIN_DATA(gp);

	REMMINA_PLUGIN_DEBUG("Function entry.");

	if (gpdata->disconnected) {
		REMMINA_PLUGIN_DEBUG("Doing nothing since the plugin is already disconnected.");
		return G_SOURCE_REMOVE;
	}

	rmplugin_x2go_cleanup(gp);

	// Try again.
	return G_SOURCE_CONTINUE;
}

static void rmplugin_x2go_pyhoca_cli_exited(GPid pid,
					    gint status,
					    RemminaProtocolWidget *gp)
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	RemminaPluginX2GoData *gpdata = GET_PLUGIN_DATA(gp);
	if (!gpdata) {
		REMMINA_PLUGIN_DEBUG("Doing nothing as the disconnection "
				     "has already been handled.");
		return;
	}

	if (gpdata->pidx2go <= 0) {
		REMMINA_PLUGIN_DEBUG("Doing nothing since pyhoca-cli was expected to stop.");
		return;
	}

	REMMINA_PLUGIN_CRITICAL("%s", _("PyHoca-CLI exited unexpectedly. "
					"This connection will now be closed."));

	struct _DialogData *ddata = g_new0(struct _DialogData, 1);
	ddata->parent = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(gp)));
	ddata->flags = GTK_DIALOG_MODAL;
	ddata->type = GTK_MESSAGE_ERROR;
	ddata->buttons = GTK_BUTTONS_OK;
	ddata->title = _("An error occured.");
	ddata->message = _("The necessary child process 'pyhoca-cli' stopped unexpectedly.\n"
			   "Please check your profile settings and PyHoca-CLI's output for "
			   "possible errors. Also ensure the remote server is "
			   "reachable and you're using the right credentials.");
	// We don't need the response.
	ddata->callbackfunc = NULL;
	// We don't need a custom dialog either.
	ddata->dialog_factory_func = NULL;
	ddata->dialog_factory_data = NULL;

	/* Prepare X2GoCustomUserData *custom_data
	 *	gp -> gp (RemminaProtocolWidget*)
	 *	dialog_data -> dialog data (struct _DialogData*)
	 */
	X2GoCustomUserData *custom_data = g_new0(X2GoCustomUserData, 1);
	g_assert(custom_data && "custom_data could not be initialized.");

	custom_data->gp = gp;
	custom_data->dialog_data = ddata;
	custom_data->connect_data = NULL;
	custom_data->opt1 = NULL;

	IDLE_ADD((GSourceFunc) rmplugin_x2go_open_dialog, custom_data);

	// 1 Second. Give `Dialog` chance to open.
	usleep(1000 * 1000);

	rmplugin_x2go_close_connection(gp);
}

/**
 * @brief Saves s_password and s_username if set.
 * @returns either TRUE or FALSE. If FALSE gets returned, `errmsg` is set.
 */
static gboolean rmplugin_x2go_save_credentials(RemminaFile* remminafile,
					       gchar* s_username, gchar* s_password,
					       gchar* errmsg)
{
	// User has requested to save credentials. We put all the new credentials
	// into remminafile->settings. They will be saved later, on successful
	// connection, by rcw.c
	if (s_password && s_username) {
		if (g_strcmp0(s_username, "") == 0) {
			g_strlcpy(errmsg, _("Can't save empty username!"), 512);
			//REMMINA_PLUGIN_CRITICAL("%s", errmsg); // No need.
			return FALSE;
		}

		// We allow the possibility to set an empty password because a X2Go
		// session can be still made using keyfiles or similar.
		rm_plugin_service->file_set_string(remminafile, "password",
						   s_password);
		rm_plugin_service->file_set_string(remminafile, "username",
						   s_username);
	} else {
		g_strlcpy(errmsg, g_strdup_printf(
			_("Internal error: %s"),
			_("Could not save new credentials.")
		), 512);

		REMMINA_PLUGIN_CRITICAL("%s", _("An error occured while trying to save "
						"new credentials: 's_password' or "
						"'s_username' strings were not set."));
		return FALSE;
	}

	return TRUE;
}


/**
 * @brief Asks the user for a username and password.
 *
 * @param errmsg Pointer to error message string (set if function failed).
 * @param passphrase gchar** Passphrase which will be used to unlock SSH key.
 *
 * @returns FALSE if auth failed and TRUE on success.
 */
static gboolean rmplugin_x2go_get_ssh_passphrase(RemminaProtocolWidget *gp, gchar *errmsg,
						 gchar **passphrase)
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	g_assert(errmsg != NULL);
	g_assert(gp != NULL);

	if ((*passphrase) == NULL) {
		// Just setting NULL password to empty password.
		(*passphrase) = g_strdup("");
	}

	gint ret = rm_plugin_service->protocol_plugin_init_auth(
			gp, 0, _("Enter password to unlock the SSH key:"),
			NULL, *passphrase, NULL, NULL
	);

	if (ret == GTK_RESPONSE_OK) {
		gchar *s_passphrase = rm_plugin_service->protocol_plugin_init_get_password(gp);
		if (s_passphrase) {
			(*passphrase) = g_strdup(s_passphrase);
			g_free(s_passphrase);
		}
	} else  {
		g_strlcpy(errmsg, _("Password input cancelled. Aborting…"), 512);
		return FALSE;
	}

	return TRUE;
}


/**
 * @brief Asks the user for a username and password.
 *
 * @param errmsg Pointer to error message string (set if function failed).
 * @param username Pointer to default username. Gets set to new username on success.
 * @param password Pointer to default password. Gets set to new password on success.
 *
 * @returns FALSE if auth failed and TRUE on success.
 */
static gboolean rmplugin_x2go_get_auth(RemminaProtocolWidget *gp, gchar* errmsg,
				       gchar** default_username, gchar** default_password)
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	g_assert(errmsg != NULL);
	g_assert(gp != NULL);
	g_assert(default_username != NULL);
	g_assert(default_password != NULL);

	// default_username is probably NULL because the user didn't configure any
	// username in the profile settings.
	if ((*default_username) == NULL) {
		gchar* l_errmsg = g_strdup_printf(
			_("Tip: Check the 'Save password' checkbox or manually input your "
			  "X2Go username and password in the profile settings to store "
			  "them for faster logins.")
		);
		REMMINA_PLUGIN_MESSAGE("%s", l_errmsg);
		(*default_username) = g_strdup("");
	}

	// default_password is probably NULL because something did go wrong at the
	// secret-plugin. For example: The user didn't input a password for keyring or
	// the user simply didn't configure a password in the profile settings.
	if ((*default_password) == NULL) {
		(*default_password) = g_strdup("");
	}

	gchar *s_username, *s_password;
	gint ret;
	gboolean save;
	gboolean disable_password_storing;
	RemminaFile *remminafile;

	remminafile = rm_plugin_service->protocol_plugin_get_file(gp);

	disable_password_storing = rm_plugin_service->file_get_int(
		remminafile, "disablepasswordstoring", FALSE
	);

	ret = rm_plugin_service->protocol_plugin_init_auth(
			gp, (disable_password_storing ? 0 :
			     REMMINA_MESSAGE_PANEL_FLAG_SAVEPASSWORD |
			     REMMINA_MESSAGE_PANEL_FLAG_USERNAME),
			_("Enter X2Go credentials"),
			(*default_username), (*default_password), NULL, NULL
	);

	if (ret == GTK_RESPONSE_OK) {
		s_username = rm_plugin_service->protocol_plugin_init_get_username(gp);
		s_password = rm_plugin_service->protocol_plugin_init_get_password(gp);
		if (rm_plugin_service->protocol_plugin_init_get_savepassword(gp))
			rm_plugin_service->file_set_string(
				remminafile, "password", s_password
			);

		// Should be renamed to protocol_plugin_init_get_savecredentials()?!
		save = rm_plugin_service->protocol_plugin_init_get_savepassword(gp);
		if (save) {
			if (!rmplugin_x2go_save_credentials(remminafile, s_username,
							    s_password, errmsg)) {
				return FALSE;
			}
		}
		if (s_username) {
			(*default_username) = g_strdup(s_username);
			g_free(s_username);
		}
		if (s_password) {
			(*default_password) = g_strdup(s_password);
			g_free(s_password);
		}
	} else  {
		g_strlcpy(errmsg, _("Authentication cancelled. Aborting…"), 512);
		return FALSE;
	}

	return TRUE;
}

/**
 * @brief Executes 'pyhoca-cli --list-sessions' for username@host.
 *
 * @param gp RemminaProtocolWidget* is used to get the x2go-plugin data.
 * @param error	This is where a error message will be when NULL gets returned.
 * @param connect_data struct _ConnectionData* which stores all necessary information
 *		       needed for retrieving sessions from a X2Go server.
 *
 * @returns Standard output of pyhoca-cli command.
 *	    If NULL then errmsg is set to user-friendly error message.
 */
static gchar* rmplugin_x2go_get_pyhoca_sessions(RemminaProtocolWidget* gp, GError **error,
						struct _ConnectionData* connect_data)
{
	REMMINA_PLUGIN_DEBUG("Function entry.");
	RemminaPluginX2GoData* gpdata = GET_PLUGIN_DATA(gp);

	gchar *host = NULL;
	gchar *username = NULL;
	gchar *password = NULL;
	gchar *ssh_privatekey = NULL;
	gchar *ssh_passphrase = NULL;
	gboolean valid = rmplugin_x2go_verify_connection_data(connect_data);

	if (valid) {
		if (connect_data->password) password = connect_data->password;
		if (connect_data->ssh_privatekey) {
			ssh_privatekey = connect_data->ssh_privatekey;

			if (connect_data->ssh_passphrase) {
				ssh_passphrase = connect_data->ssh_passphrase;
			}
		}

		host = connect_data->host;
		username = connect_data->username;
	} else {
		return G_SOURCE_REMOVE;
	}

	// We will now start pyhoca-cli with only the '--list-sessions' option.

	gchar *argv[50];
	gint argc = 0;

	argv[argc++] = g_strdup("pyhoca-cli");
	argv[argc++] = g_strdup("--list-sessions");

	argv[argc++] = g_strdup("--server"); // Not listed as feature.
	argv[argc++] = g_strdup_printf("%s", host);

	if (FEATURE_AVAILABLE(gpdata, "USERNAME")) {
		argv[argc++] = g_strdup("-u");
		if (username) {
			argv[argc++] = g_strdup_printf("%s", username);
		} else {
			argv[argc++] = g_strdup_printf("%s", g_get_user_name());
		}
	} else {
		g_set_error(error, 1, 1, "%s", FEATURE_NOT_AVAIL_STR("USERNAME"));
		REMMINA_PLUGIN_CRITICAL("%s", FEATURE_NOT_AVAIL_STR("USERNAME"));
		return NULL;
	}

	if (FEATURE_AVAILABLE(gpdata, "NON_INTERACTIVE")) {
		argv[argc++] = g_strdup("--non-interactive");
	} else {
		REMMINA_PLUGIN_WARNING("%s", FEATURE_NOT_AVAIL_STR("NON_INTERACTIVE"));
	}

	if (password && FEATURE_AVAILABLE(gpdata, "PASSWORD")) {
		if (FEATURE_AVAILABLE(gpdata, "AUTH_ATTEMPTS")) {
			argv[argc++] = g_strdup("--auth-attempts");
			argv[argc++] = g_strdup_printf ("%i", 0);
		} else {
			REMMINA_PLUGIN_WARNING("%s", FEATURE_NOT_AVAIL_STR("AUTH_ATTEMPTS"));
		}
		if (strlen(password) > 0) {
			argv[argc++] = g_strdup("--force-password");
			argv[argc++] = g_strdup("--password");
			argv[argc++] = g_strdup_printf("%s", password);
		}
	} else if (!password) {
		g_set_error(error, 1, 1, "%s", FEATURE_NOT_AVAIL_STR("PASSWORD"));
		REMMINA_PLUGIN_CRITICAL("%s", FEATURE_NOT_AVAIL_STR("PASSWORD"));
		return NULL;
	}

	// No need to catch feature-not-available error.
	// `--quiet` is not that important.
	if (FEATURE_AVAILABLE(gpdata, "QUIET")) {
		argv[argc++] = g_strdup("--quiet");
	}

	if (FEATURE_AVAILABLE(gpdata, "SSH_PRIVKEY")) {
		if (ssh_privatekey && !g_str_equal(ssh_privatekey, "")) {
			argv[argc++] = g_strdup("--ssh-privkey");
			argv[argc++] = g_strdup_printf("%s", ssh_privatekey);

			if (ssh_passphrase && !g_str_equal(ssh_passphrase, "")) {
				if (FEATURE_AVAILABLE(gpdata, "SSH_PASSPHRASE")) {
					argv[argc++] = g_strdup("--ssh-passphrase");
					argv[argc++] = g_strdup_printf("%s", ssh_passphrase);
				} else {
					REMMINA_PLUGIN_MESSAGE("%s", FEATURE_NOT_AVAIL_STR("SSH_PASSPHRASE"));
				}
			}
		}
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("SSH_PRIVKEY"));
	}


	argv[argc++] = NULL;

	//#ifndef GLIB_AVAILABLE_IN_2_68
		gchar** envp = g_get_environ();
		gchar* envp_splitted = g_strjoinv(";", envp);
		envp_splitted = g_strconcat(envp_splitted, ";LANG=C", (void*) NULL);
		envp = g_strsplit(envp_splitted, ";", 0);
	/*
	* #else
	*	// Only available after glib version 2.68.
	*	// TODO: FIXME: NOT TESTED!
	*	GStrvBuilder* builder = g_strv_builder_new();
	*	g_strv_builder_add(builder, "LANG=C");
	*	GStrv envp = g_strv_builder_end(builder);
	* #endif
	*/

	gchar* std_out = rmplugin_x2go_spawn_pyhoca_process(argc, argv, error, envp);
	g_strfreev(envp);

	if (!std_out || *error) {
		// If no error is set but std_out is NULL
		// then something is not right at all.
		// Most likely the developer forgot to add an error message. Crash.
		g_assert((*error) != NULL);
		return NULL;
	}

	return std_out;
}

/**
 * @brief This function is used to parse the output of
 * 	  rmplugin_x2go_get_pyhoca_sessions().
 *
 * @param gp RemminaProtocolWidget* is used to get the x2go-plugin data.
 * @param error This is where a error message will be when NULL gets returned.
 * @param connect_data	struct _ConnectionData* which stores all necessary information
 *			needed for retrieving sessions from a X2Go server.
 *
 * @returns Returns either a GList containing the IDs of every already existing session
 *	    found or if the function failes, NULL.
 *
 * TODO: If pyhoca-cli (python-x2go) implements `--json` or similar option -> Replace
 *	 entire function with JSON parsing.
 */
static GList* rmplugin_x2go_parse_pyhoca_sessions(RemminaProtocolWidget* gp,
						  GError **error,
						  struct _ConnectionData* connect_data)
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	gchar *pyhoca_output = NULL;

	pyhoca_output = rmplugin_x2go_get_pyhoca_sessions(gp, error, connect_data);
	if (!pyhoca_output || *error) {
		// If no error is set but pyhoca_output is NULL
		// then something is not right at all.
		// Most likely the developer forgot to add an error message. Crash.
		g_assert((*error) != NULL);

		return NULL;
	}

	gchar **lines_list = g_strsplit(pyhoca_output, "\n", -1);
	// Assume at least two lines of output.
	if (lines_list == NULL || lines_list[0] == NULL || lines_list[1] == NULL) {
		g_set_error(error, 1, 1, "%s", _("Couldn't parse the output of PyHoca-CLI's "
				           "--list-sessions option. Creating a new "
				           "session now."));
		return NULL;
	}

	gboolean found_session = FALSE;
	GList* sessions = NULL;
	gchar** session = NULL;

	for (guint i = 0; lines_list[i] != NULL; i++) {
		gchar* current_line = lines_list[i];

		// TOO VERBOSE:
		//REMMINA_PLUGIN_DEBUG("pyhoca-cli: %s", current_line);

		// Hardcoded string "Session Name: " comes from python-x2go.
		if (!g_str_has_prefix(current_line, "Session Name: ") && !found_session) {
			// Doesn't begin with "Session Name: " and
			// the current line doesn't come after that either. Skipping.
			continue;
		}

		if (g_str_has_prefix(current_line, "Session Name: ")) {
			gchar* session_id = NULL;
			gchar** line_list = g_strsplit(current_line, ": ", 0);

			if (line_list == NULL ||
			    line_list[0] == NULL ||
			    line_list[1] == NULL ||
			    strlen(line_list[0]) <= 0 ||
			    strlen(line_list[1]) <= 0)
			{
				found_session = FALSE;
				continue;
			}

			session = malloc(sizeof(gchar*) * (SESSION_NUM_PROPERTIES+1));
			if (!session) {
				REMMINA_PLUGIN_CRITICAL("%s", _("Couldn't allocate "
								"enough memory!"));
			}
			session[SESSION_NUM_PROPERTIES] = NULL;
			sessions = g_list_append(sessions, session);

			session_id = line_list[1];
			session[SESSION_SESSION_ID] = session_id;

			REMMINA_PLUGIN_INFO("%s", g_strdup_printf(
				_("Found already existing X2Go session with ID: '%s'"),
				session[SESSION_SESSION_ID])
			);

			found_session = TRUE;
			continue;
		}

		if (!found_session) {
			continue;
		}

		if (g_strcmp0(current_line, "-------------") == 0) {
			continue;
		}

		gchar* value = NULL;
		gchar** line_list = g_strsplit(current_line, ": ", 0);

		if (line_list == NULL ||
		    line_list[0] == NULL ||
		    line_list[1] == NULL ||
		    strlen(line_list[0]) <= 0 ||
		    strlen(line_list[1]) <= 0)
		{
			// Probably the empty line at the end of every session.
			found_session = FALSE;
			continue;
		}
		value = line_list[1];

		if (g_str_has_prefix(current_line, "cookie: ")) {
			REMMINA_PLUGIN_DEBUG("cookie:\t'%s'", value);
			session[SESSION_COOKIE] = value;
		} else if (g_str_has_prefix(current_line, "agent PID: ")) {
			REMMINA_PLUGIN_DEBUG("agent PID:\t'%s'", value);
			session[SESSION_AGENT_PID] = value;
		} else if (g_str_has_prefix(current_line, "display: ")) {
			REMMINA_PLUGIN_DEBUG("display:\t'%s'", value);
			session[SESSION_DISPLAY] = value;
		} else if (g_str_has_prefix(current_line, "status: ")) {
			if (g_strcmp0(value, "S") == 0) {
				// TRANSLATORS: Please stick to X2GoClient's translation.
				value = _("Suspended");
			} else if (g_strcmp0(value, "R") == 0) {
				// TRANSLATORS: Please stick to X2GoClient's translation.
				value = _("Running");
			} else if (g_strcmp0(value, "T") == 0) {
				// TRANSLATORS: Please stick to X2GoClient's translation.
				value = _("Terminated");
			}
			REMMINA_PLUGIN_DEBUG("status:\t'%s'", value);
			session[SESSION_STATUS] = value;
		} else if (g_str_has_prefix(current_line, "graphic port: ")) {
			REMMINA_PLUGIN_DEBUG("graphic port:\t'%s'", value);
			session[SESSION_GRAPHIC_PORT] = value;
		} else if (g_str_has_prefix(current_line, "snd port: ")) {
			REMMINA_PLUGIN_DEBUG("snd port:\t'%s'", value);
			session[SESSION_SND_PORT] = value;
		} else if (g_str_has_prefix(current_line, "sshfs port: ")) {
			REMMINA_PLUGIN_DEBUG("sshfs port:\t'%s'", value);
			session[SESSION_SSHFS_PORT] = value;
		} else if (g_str_has_prefix(current_line, "username: ")) {
			REMMINA_PLUGIN_DEBUG("username:\t'%s'", value);
			session[SESSION_USERNAME] = value;
		} else if (g_str_has_prefix(current_line, "hostname: ")) {
			REMMINA_PLUGIN_DEBUG("hostname:\t'%s'", value);
			session[SESSION_HOSTNAME] = value;
		} else if (g_str_has_prefix(current_line, "create date: ")) {
			REMMINA_PLUGIN_DEBUG("create date:\t'%s'", value);
			session[SESSION_CREATE_DATE] = value;
		} else if (g_str_has_prefix(current_line, "suspended since: ")) {
			REMMINA_PLUGIN_DEBUG("suspended since:\t'%s'", value);
			session[SESSION_SUSPENDED_SINCE] = value;
		} else {
			REMMINA_PLUGIN_DEBUG("Not supported:\t'%s'", value);
			found_session = FALSE;
		}
	}

	if (!sessions) {
		g_set_error(error, 1, 1,
			"%s", _("Could not find any sessions on remote machine. Creating a new "
			  "session now.")
		);

		// returning NULL with `error` set.
	}

	return sessions;
}

/**
 * @brief Asks the user, with the help of a dialog, to continue an already existing
 *	  session, terminate or create a new one.
 *
 * @param error Is set if there is something to tell the user. \n
 *		Not necessarily an *error* message.
 * @param connect_data Stores all necessary information needed for
 *		       etrieving sessions from a X2Go server.
 * @return gchar* ID of session. Can be 'NULL' but then 'error' is set.
 */
static gchar* rmplugin_x2go_ask_session(RemminaProtocolWidget *gp, GError **error,
					struct _ConnectionData* connect_data)
{
	if (!connect_data ||
	    !connect_data->host ||
	    !connect_data->username ||
	    !connect_data->password ||
	    strlen(connect_data->host) <= 0 ||
	    strlen(connect_data->username) <= 0)
	    // Allow empty passwords. Maybe the user wants to connect via public key?
	{
		g_set_error(error, 1, 1, "%s", g_strdup_printf(
			_("Internal error: %s"),
			_("'Invalid connection data.'")
		));
		return NULL;
	}

	GList *sessions_list = NULL;
	sessions_list = rmplugin_x2go_parse_pyhoca_sessions(gp, error, connect_data);

	if (!sessions_list || *error) {
		// If no error is set but sessions_list is NULL
		// then something is not right at all.
		// Most likely the developer forgot to add an error message. Crash.
		g_assert(*error != NULL);
		return NULL;
	}

	// Prep new DialogData struct.
	struct _DialogData *ddata = g_new0(struct _DialogData, 1);
	ddata->parent = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(gp)));
	ddata->flags = GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT;
	//ddata->type = GTK_MESSAGE_QUESTION;
	//ddata->buttons = GTK_BUTTONS_OK; // Doesn't get used in our custom factory.
	ddata->title = _("Choose a session to resume:");
	ddata->message = "";

	// gboolean factory(X2GoCustomUserData*, gpointer)
	// 	X2GoCustomUserData*:
	//		gp -> gp (RemminaProtocolWidget*)
	//		dialog_data -> dialog data (struct _DialogData*)
	//		connect_data -> connection data (struct _ConnectionData*)
	//	gpointer: dialog_factory_data
	ddata->callbackfunc = G_CALLBACK(rmplugin_x2go_session_chooser_callback);

	// gboolean factory(X2GoCustomUserData*, gpointer)
	// 	X2GoCustomUserData*:
	//		gp -> gp (RemminaProtocolWidget*)
	//		dialog_data -> dialog data (struct _DialogData*)
	//		connect_data -> connection data (struct _ConnectionData*)
	//	gpointer: dialog_factory_data
	ddata->dialog_factory_data = sessions_list;
	ddata->dialog_factory_func = G_CALLBACK(rmplugin_x2go_choose_session_dialog_factory);

	/* Prepare X2GoCustomUserData *custom_data
	 *	gp -> gp (RemminaProtocolWidget*)
	 *	dialog_data -> dialog data (struct _DialogData*)
	 */
	X2GoCustomUserData *custom_data = g_new0(X2GoCustomUserData, 1);
	g_assert(custom_data && "custom_data could not be initialized.");

	custom_data->gp = gp;
	custom_data->dialog_data = ddata;
	custom_data->connect_data = connect_data;
	custom_data->opt1 = NULL;

	// Open dialog here. Dialog rmplugin_x2go_session_chooser_callback (callbackfunc)
	// should set SET_RESUME_SESSION.
	IDLE_ADD((GSourceFunc)rmplugin_x2go_open_dialog, custom_data);

	guint counter = 0;
	while (!IS_SESSION_SELECTED(gp)) {
		// 0.5 Seconds. Give dialog chance to open.
		usleep(500 * 1000);

		// Every 5 seconds
		if (counter % 10 == 0 || counter == 0) {
			REMMINA_PLUGIN_INFO("%s", _("Waiting for user to select a session…"));
		}
		counter++;
	}

	gchar* chosen_resume_session = GET_RESUME_SESSION(gp);

	if (!chosen_resume_session || strlen(chosen_resume_session) <= 0) {
		g_set_error(error, 1, 1, "%s", _("No session was selected. Creating a new one."));
		return NULL;
	}

	return chosen_resume_session;
}

static gboolean rmplugin_x2go_exec_x2go(gchar *host,
                                        gint   sshport,
                                        gchar *username,
                                        gchar *password,
                                        gchar *command,
                                        gchar *kbdlayout,
                                        gchar *kbdtype,
                                        gchar *audio,
                                        gchar *clipboard,
                                        gint   dpi,
                                        gchar *resolution,
                                        gchar *ssh_privatekey,
                                        RemminaProtocolWidget *gp,
                                        gchar *errmsg)
{
	TRACE_CALL(__func__);
	RemminaPluginX2GoData *gpdata = GET_PLUGIN_DATA(gp);

	gchar *argv[50];
	gint argc = 0;

	// We don't want to save any SSH passphrases on hard drive!
	// Thats why we will always ask if needed.
	gchar *ssh_passphrase = NULL;

	if (!username || strlen(username) <= 0) {
		// Sets `username` and `password`.
		if (!rmplugin_x2go_get_auth(gp, errmsg, &username, &password)) {
			return FALSE;
		}
	}

	// Password can be *empty* but not NULL.
	if (!password) {
		password = g_strdup("");
	}

	if (ssh_privatekey && strlen(ssh_privatekey) > 0) {
		// FIXME: Check if file exists and is legit private key.
		// See: https://security.stackexchange.com/a/245767

		// Get ssh_privatekey now via dialog.
		if (!rmplugin_x2go_get_ssh_passphrase(gp, errmsg, &ssh_passphrase)) {
			return FALSE;
		}
	}

	struct _ConnectionData* connect_data = g_new0(struct _ConnectionData, 1);
	connect_data->host = host;
	connect_data->username = username;
	connect_data->password = password;
	connect_data->ssh_privatekey = ssh_privatekey;
	connect_data->ssh_passphrase = ssh_passphrase;

	GError *session_error = NULL;
	gchar* resume_session_id = rmplugin_x2go_ask_session(gp, &session_error,
							     connect_data);

	if (!resume_session_id || session_error || strlen(resume_session_id) <= 0) {
		// If no error is set but session_id is NULL
		// then something is not right at all.
		// Most likely the developer forgot to add an error message. Crash.
		g_assert(session_error != NULL);

		REMMINA_PLUGIN_WARNING("%s", g_strdup_printf(
			_("A non-critical error happened: %s"),
			session_error->message
		));
	} else {
		REMMINA_PLUGIN_INFO("%s", g_strdup_printf(
			_("User chose to resume session with ID: '%s'"),
			resume_session_id
		));
	}

	argc = 0;
	argv[argc++] = g_strdup("pyhoca-cli");

	argv[argc++] = g_strdup("--server"); // Not listed as feature.
	argv[argc++] = g_strdup_printf ("%s", host);

	if (FEATURE_AVAILABLE(gpdata, "REMOTE_SSH_PORT")) {
		argv[argc++] = g_strdup("-p");
		argv[argc++] = g_strdup_printf ("%d", sshport);
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("REMOTE_SSH_PORT"));
	}

	if (resume_session_id && strlen(resume_session_id) > 0) {
		REMMINA_PLUGIN_INFO("%s", g_strdup_printf(
			// TRANSLATORS: Please stick to X2GoClient's way of translating.
			_("Resuming session '%s'…"),
			resume_session_id
		));

		if (FEATURE_AVAILABLE(gpdata, "RESUME")) {
			argv[argc++] = g_strdup("--resume");
			argv[argc++] = g_strdup_printf("%s", resume_session_id);
		} else {
			REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("RESUME"));
		}
	}

	// Deprecated. The user either wants to continue a
	// session or just not. No inbetween.
	// if (!resume_session_id) {
	// 	if (FEATURE_AVAILABLE(gpdata, "TRY_RESUME")) {
	// 		argv[argc++] = g_strdup("--try-resume");
	// 	} else {
	// 		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("TRY_RESUME"));
	// 	}
	// }

	if (FEATURE_AVAILABLE(gpdata, "USERNAME")) {
		argv[argc++] = g_strdup("-u");
		if (username){
			argv[argc++] = g_strdup_printf ("%s", username);
		} else {
			argv[argc++] = g_strdup_printf ("%s", g_get_user_name());
		}
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("USERNAME"));
	}

	if (password && FEATURE_AVAILABLE(gpdata, "PASSWORD")) {
		if (strlen(password) > 0) {
			argv[argc++] = g_strdup("--force-password");
			argv[argc++] = g_strdup("--password");
			argv[argc++] = g_strdup_printf ("%s", password);
		}
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("PASSWORD"));
	}

	if (FEATURE_AVAILABLE(gpdata, "AUTH_ATTEMPTS")) {
		argv[argc++] = g_strdup("--auth-attempts");
		argv[argc++] = g_strdup_printf ("%i", 0);
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("AUTH_ATTEMPTS"));
	}

	if (FEATURE_AVAILABLE(gpdata, "NON_INTERACTIVE")) {
		argv[argc++] = g_strdup("--non-interactive");
	} else {
		REMMINA_PLUGIN_WARNING("%s", FEATURE_NOT_AVAIL_STR("NON_INTERACTIVE"));
	}

	if (FEATURE_AVAILABLE(gpdata, "COMMAND")) {
		argv[argc++] = g_strdup("-c");
		// FIXME: pyhoca-cli is picky about multiple quotes around
		// 	  the command string...
		// argv[argc++] = g_strdup_printf ("%s", g_shell_quote(command));
		argv[argc++] = g_strdup(command);
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("COMMAND"));
	}

	if (FEATURE_AVAILABLE(gpdata, "KBD_LAYOUT")) {
		if (kbdlayout) {
			argv[argc++] = g_strdup("--kbd-layout");
			argv[argc++] = g_strdup_printf ("%s", kbdlayout);
		} else {
			argv[argc++] = g_strdup("--kbd-layout");
			argv[argc++] = g_strdup("auto");
		}
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("KBD_LAYOUT"));
	}

	if (FEATURE_AVAILABLE(gpdata, "KBD_TYPE")) {
		if (kbdtype) {
			argv[argc++] = g_strdup("--kbd-type");
			argv[argc++] = g_strdup_printf ("%s", kbdtype);
		} else {
			argv[argc++] = g_strdup("--kbd-type");
			argv[argc++] = g_strdup("auto");
		}
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("KBD_TYPE"));
	}

	if (FEATURE_AVAILABLE(gpdata, "GEOMETRY")) {
		if (!resolution)
			resolution = "800x600";
		argv[argc++] = g_strdup("-g");
		argv[argc++] = g_strdup_printf ("%s", resolution);
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("GEOMETRY"));
	}

	if (FEATURE_AVAILABLE(gpdata, "TERMINATE_ON_CTRL_C")) {
		argv[argc++] = g_strdup("--terminate-on-ctrl-c");
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("TERMINATE_ON_CTRL_C"));
	}

	if (FEATURE_AVAILABLE(gpdata, "SOUND")) {
		if (audio) {
			argv[argc++] = g_strdup("--sound");
			argv[argc++] = g_strdup_printf ("%s", audio);
		} else {
			argv[argc++] = g_strdup("--sound");
			argv[argc++] = g_strdup("none");
		}
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("SOUND"));
	}

	if (FEATURE_AVAILABLE(gpdata, "CLIPBOARD_MODE")) {
		if (clipboard) {
			argv[argc++] = g_strdup("--clipboard-mode");
			argv[argc++] = g_strdup_printf("%s", clipboard);
		}
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("CLIPBOARD_MODE"));
	}

	if (FEATURE_AVAILABLE(gpdata, "DPI")) {
		// Even though we validate the users input in the Remmina Editor,
		// manipulating profile files is still very possible…
		// Values are extracted from pyhoca-cli.
		if (dpi < 20 || dpi > 400) {
			g_strlcpy(errmsg, _("DPI setting is out of bounds. Please adjust "
					    "it in profile settings."), 512);
			// No need, start_session() will handle output.
			//REMMINA_PLUGIN_CRITICAL("%s", errmsg);
			return FALSE;
		}
		argv[argc++] = g_strdup("--dpi");
		argv[argc++] = g_strdup_printf ("%i", dpi);
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("DPI"));
	}

	if (FEATURE_AVAILABLE(gpdata, "SSH_PRIVKEY")) {
		if (ssh_privatekey && !g_str_equal(ssh_privatekey, "")) {
			argv[argc++] = g_strdup("--ssh-privkey");
			argv[argc++] = g_strdup_printf("%s", ssh_privatekey);

			if (ssh_passphrase && !g_str_equal(ssh_passphrase, "")) {
				if (FEATURE_AVAILABLE(gpdata, "SSH_PASSPHRASE")) {
					argv[argc++] = g_strdup("--ssh-passphrase");
					argv[argc++] = g_strdup_printf("%s", ssh_passphrase);
				} else {
					REMMINA_PLUGIN_MESSAGE("%s", FEATURE_NOT_AVAIL_STR("SSH_PASSPHRASE"));
				}
			}
		}
	} else {
		REMMINA_PLUGIN_DEBUG("%s", FEATURE_NOT_AVAIL_STR("SSH_PRIVKEY"));
	}

	argv[argc++] = NULL;

	GError *error = NULL;
	gchar **envp = g_get_environ();
	gboolean success = g_spawn_async_with_pipes (NULL, argv, envp,
						     (G_SPAWN_DO_NOT_REAP_CHILD |
						      G_SPAWN_SEARCH_PATH), NULL,
						     NULL, &gpdata->pidx2go,
						     NULL, NULL, NULL, &error);

	REMMINA_PLUGIN_INFO("%s", _("Started PyHoca-CLI with the following arguments:"));
	// Print every argument except passwords. Free all arg strings.
	for (gint i = 0; i < argc - 1; i++) {
		gchar* curr_arg = argv[i];

		if (g_str_equal(curr_arg, "--password") ||
		    g_str_equal(curr_arg, "--ssh-passphrase")) {
			g_printf("%s ", curr_arg);
			g_printf("XXXXXX ");
			g_free(curr_arg);
			g_free(argv[++i]);
			continue;
		} else {
			g_printf("%s ", curr_arg);
			g_free(curr_arg);
		}
	}
	g_printf("\n");

	if (!success || error) {
		// TRANSLATORS: Meta-error. Shouldn't be visible.
		if (!error) error = g_error_new(0, 0, _("Internal error."));

		gchar *error_title = _("An error occured while "
				       "starting an X2Go session…");

		struct _DialogData* ddata = g_new0(struct _DialogData, 1);
		ddata->parent = GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(gp)));
		ddata->flags = GTK_DIALOG_MODAL;
		ddata->type = GTK_MESSAGE_ERROR;
		ddata->buttons = GTK_BUTTONS_OK;
		ddata->title = _("Could not start X2Go session.");
		ddata->message = g_strdup_printf(_("Could not start PyHoca-CLI (%i): '%s'"),
						 error->code,
						 error->message);
		// We don't need the response.
		ddata->callbackfunc = NULL;
		// We don't need a custom dialog either.
		ddata->dialog_factory_func = NULL;
		ddata->dialog_factory_data = NULL;

		/* Prepare X2GoCustomUserData *custom_data
		*	gp -> gp (RemminaProtocolWidget*)
		*	dialog_data -> dialog data (struct _DialogData*)
		*/
		X2GoCustomUserData *custom_data = g_new0(X2GoCustomUserData, 1);
		g_assert(custom_data && "custom_data could not be initialized.");

		custom_data->gp = gp;
		custom_data->dialog_data = ddata;
		custom_data->connect_data = NULL;
		custom_data->opt1 = NULL;

		IDLE_ADD((GSourceFunc) rmplugin_x2go_open_dialog, custom_data);

		g_strlcpy(errmsg, error_title, 512);

		// No need to output here. rmplugin_x2go_start_session will do this.

		g_error_free(error);

		return FALSE;
	}

	// Prevent a race condition where pyhoca-cli is not
	// started yet (pidx2go == 0) but a watcher is added.

	struct timespec ts;
	// 0.001 seconds.
	ts.tv_nsec = 1 * 1000 * 1000;
	ts.tv_sec = 0;
	while (gpdata->pidx2go == 0) {
		nanosleep(&ts, NULL);
		REMMINA_PLUGIN_DEBUG("Waiting for PyHoca-CLI to start…");
	};

	REMMINA_PLUGIN_DEBUG("Watching child 'pyhoca-cli' process now…");
	g_child_watch_add(gpdata->pidx2go,
			  (GChildWatchFunc) rmplugin_x2go_pyhoca_cli_exited,
			  gp);

	return TRUE;
}

/**
 * @returns a GList* with all features which pyhoca-cli had before the feature system.
 */
static GList* rmplugin_x2go_old_pyhoca_features()
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	#define AMOUNT_FEATURES 43
	gchar* features[AMOUNT_FEATURES] = {
		"ADD_TO_KNOWN_HOSTS", "AUTH_ATTEMPTS", "BROKER_PASSWORD", "BROKER_URL",
		"CLEAN_SESSIONS", "COMMAND", "DEBUG", "FORCE_PASSWORD",	"FORWARD_SSHAGENT",
		"GEOMETRY", "KBD_LAYOUT", "KBD_TYPE", "LIBDEBUG", "LIBDEBUG_SFTPXFER", "LINK",
		"LIST_CLIENT_FEATURES", "LIST_DESKTOPS", "LIST_SESSIONS", "NEW", "PACK",
		"PASSWORD", "PDFVIEW_CMD", "PRINTER", "PRINTING", "PRINT_ACTION", "PRINT_CMD",
		"QUIET", "REMOTE_SSH_PORT", "RESUME", "SAVE_TO_FOLDER", "SESSION_PROFILE",
		"SESSION_TYPE", "SHARE_DESKTOP", "SHARE_LOCAL_FOLDERS", "SHARE_MODE", "SOUND",
		"SSH_PRIVKEY", "SUSPEND", "TERMINATE", "TERMINATE_ON_CTRL_C", "TRY_RESUME",
		"USERNAME", "XINERAMA"
	};

	GList *features_list = NULL;
	for (int i = 0; i < AMOUNT_FEATURES; i++) {
		features_list = g_list_append(features_list, features[i]);
	}

	return features_list;
}

/**
 *  @returns a GList* which includes all pyhoca-cli command line features we can use.
 */
static GList* rmplugin_x2go_populate_available_features_list()
{
	REMMINA_PLUGIN_DEBUG("Function entry.");

	GList* returning_glist = NULL;

	// We will now start pyhoca-cli with only the '--list-cmdline-features' option
	// and depending on the exit code and standard output we will determine if some
	// features are available or not.

	gchar* argv[50];
	gint argc = 0;

	argv[argc++] = g_strdup("pyhoca-cli");
	argv[argc++] = g_strdup("--list-cmdline-features");
	argv[argc++] = NULL;

	GError* error = NULL; // Won't be actually used.

	// Querying pyhoca-cli's command line features.
	gchar** envp = g_get_environ();
	gchar* features_string = rmplugin_x2go_spawn_pyhoca_process(argc, argv,
								    &error, envp);
	g_strfreev(envp);

	if (!features_string || error) {
		// We added the '--list-cmdline-features' on commit 17d1be1319ba6 of
		// pyhoca-cli. In order to protect setups which don't have the newest
		// version of pyhoca-cli available yet we artificially create a list
		// of an old limited set of features.

		REMMINA_PLUGIN_WARNING("%s",
			_("Couldn't get PyHoca-CLI's command-line features. This "
			  "indicates it is either too old, or not installed. "
			  "An old limited set of features will be used for now."));

		return rmplugin_x2go_old_pyhoca_features();
	} else {
		gchar **features_list = g_strsplit(features_string, "\n", 0);

		if (features_list == NULL) {
			gchar *error_msg = _("Could not parse PyHoca-CLI's command-line "
					     "features. Using a limited feature-set for now.");
			REMMINA_PLUGIN_WARNING("%s", error_msg);
			return rmplugin_x2go_old_pyhoca_features();
		}

		REMMINA_PLUGIN_INFO("%s", _("Retrieved the following PyHoca-CLI "
					    "command-line features:"));

		for(int k = 0; features_list[k] != NULL; k++) {
			// Filter out empty strings
			if (strlen(features_list[k]) <= 0) continue;

			REMMINA_PLUGIN_INFO("%s",
					 g_strdup_printf(_("Available feature[%i]: '%s'"),
							 k+1, features_list[k]));
			returning_glist = g_list_append(returning_glist, features_list[k]);
		}
		return returning_glist;
	}
}

static void rmplugin_x2go_on_plug_added(GtkSocket *socket, RemminaProtocolWidget *gp)
{
	TRACE_CALL(__func__);

	gchar *server;
	gint port;

	RemminaPluginX2GoData *gpdata = GET_PLUGIN_DATA(gp);
	REMMINA_PLUGIN_DEBUG("Socket %d", gpdata->socket_id);
	rm_plugin_service->protocol_plugin_signal_connection_opened(gp);

	RemminaFile *remminafile = rm_plugin_service->protocol_plugin_get_file(gp);
	rm_plugin_service->get_server_port(rm_plugin_service->file_get_string(remminafile, "server"),
			22,
			&server,
			&port);

	REMMINA_PLUGIN_AUDIT(_("Connected to %s:%d via X2Go"), server, port);
	g_free(server), server = NULL;

	return;
}

static gboolean rmplugin_x2go_on_plug_removed(GtkSocket *socket, RemminaProtocolWidget *gp)
{
	TRACE_CALL(__func__);
	REMMINA_PLUGIN_DEBUG("Function entry.");
	rmplugin_x2go_close_connection(gp);
	return G_SOURCE_CONTINUE;
}

static void rmplugin_x2go_init(RemminaProtocolWidget *gp)
{
	TRACE_CALL(__func__);
	REMMINA_PLUGIN_DEBUG("Function entry.", PLUGIN_NAME);
	RemminaPluginX2GoData *gpdata;

	gpdata = g_new0(RemminaPluginX2GoData, 1);
	g_object_set_data_full(G_OBJECT(gp), "plugin-data", gpdata, g_free);

	if (!rm_plugin_service->gtksocket_available()) {
		/* report this in open_connection, not reportable here... */
		return;
	}

	GList* available_features = rmplugin_x2go_populate_available_features_list();

	// available_features can't be NULL cause if it fails, it gets populated with an
	// old standard feature set.
	gpdata->available_features = available_features;

	gpdata->socket_id = 0;
	gpdata->thread = 0;

	gpdata->display = NULL;
	gpdata->window_id = 0;
	gpdata->pidx2go = 0;
	gpdata->orig_handler = NULL;

	gpdata->socket = gtk_socket_new();
	rm_plugin_service->protocol_plugin_register_hostkey(gp, gpdata->socket);
	gtk_widget_show(gpdata->socket);

	g_signal_connect(G_OBJECT(gpdata->socket), "plug-added",
			 G_CALLBACK(rmplugin_x2go_on_plug_added), gp);
	g_signal_connect(G_OBJECT(gpdata->socket), "plug-removed",
			 G_CALLBACK(rmplugin_x2go_on_plug_removed), gp);
	gtk_container_add(GTK_CONTAINER(gp), gpdata->socket);
}

static gboolean rmplugin_x2go_try_window_id(Window window_id)
{
	TRACE_CALL(__func__);
	gint i;
	gboolean already_seen = FALSE;

	REMMINA_PLUGIN_DEBUG("Check if the window of X2Go Agent with ID [0x%lx] is already known or if "
			     "it needs registration", window_id);

	pthread_mutex_lock(&remmina_x2go_init_mutex);
	for (i = 0; i < remmina_x2go_window_id_array->len; i++) {
		if (g_array_index(remmina_x2go_window_id_array, Window, i) == window_id) {
			already_seen = TRUE;
			REMMINA_PLUGIN_DEBUG("Window of X2Go Agent with ID [0x%lx] "
					     "already seen.", window_id);
			break;
		}
	}
	if (!already_seen) {
		g_array_append_val(remmina_x2go_window_id_array, window_id);
		REMMINA_PLUGIN_DEBUG("Registered new window for X2Go Agent with "
				     "ID [0x%lx].", window_id);
	}
	pthread_mutex_unlock(&remmina_x2go_init_mutex);

	return (!already_seen);
}

static int rmplugin_x2go_dummy_handler(Display *dsp, XErrorEvent *err)
{
	TRACE_CALL(__func__);
	return 0;
}

static gboolean rmplugin_x2go_start_create_notify(RemminaProtocolWidget *gp, gchar *errmsg)
{
	TRACE_CALL(__func__);
	RemminaPluginX2GoData *gpdata = GET_PLUGIN_DATA(gp);

	gpdata->display = XOpenDisplay(gdk_display_get_name(gdk_display_get_default()));
	if (gpdata->display == NULL) {
		g_strlcpy(errmsg, _("Could not open X11 DISPLAY."), 512);
		return FALSE;
	}

	gpdata->orig_handler = XSetErrorHandler(rmplugin_x2go_dummy_handler);

	XSelectInput(gpdata->display,
		     XDefaultRootWindow(gpdata->display),
		     SubstructureNotifyMask);

	REMMINA_PLUGIN_DEBUG("X11 event-watcher created.");

	return TRUE;
}

static gboolean rmplugin_x2go_monitor_create_notify(RemminaProtocolWidget *gp,
						    const gchar *cmd,
						    gchar *errmsg)
{
	TRACE_CALL(__func__);
	RemminaPluginX2GoData *gpdata;

	gboolean agent_window_found = FALSE;
	Atom atom;
	XEvent xev;
	Window w;
	Atom type;
	int format;
	unsigned long nitems, rest;
	unsigned char *data = NULL;

	guint16 non_createnotify_count = 0;

	struct timespec ts;
	// wait_amount * ts.tv_nsec = 20s
	// 100 * 0.2s = 20s
	int wait_amount = 100;

	CANCEL_DEFER

	REMMINA_PLUGIN_DEBUG("%s", _("Waiting for window of X2Go Agent to appear…"));

	gpdata = GET_PLUGIN_DATA(gp);
	atom = XInternAtom(gpdata->display, "WM_COMMAND", True);
	if (atom == None) {
		CANCEL_ASYNC
		return FALSE;
	}

	ts.tv_sec = 0;
	// 0.2s = 200000000ns
	ts.tv_nsec = 200000000;

	while (wait_amount > 0) {
		pthread_testcancel();
		if (!(gpdata->pidx2go > 0)) {
			nanosleep(&ts, NULL);
			REMMINA_PLUGIN_DEBUG("Waiting for X2Go session to start…");
			continue;
		}

		while (!XPending(gpdata->display)) {
			nanosleep(&ts, NULL);
			wait_amount--;
			// Don't spam the console. Print every second though.
			if (wait_amount % 5 == 0) {
				REMMINA_PLUGIN_INFO("%s", _("Waiting for PyHoca-CLI to "
							    "show the session's window…"));
			}
			continue;
		}

		XNextEvent(gpdata->display, &xev);
		// Just ignore non CreatNotify events.
		if (xev.type != CreateNotify) {
			non_createnotify_count++;
			if (non_createnotify_count % 5 == 0) {
				REMMINA_PLUGIN_DEBUG("Saw '%i' X11 events, which weren't "
						     "CreateNotify.", non_createnotify_count);
			}
			continue;
		}

		w = xev.xcreatewindow.window;
		if (XGetWindowProperty(gpdata->display, w, atom, 0, 255, False,
				       AnyPropertyType, &type, &format, &nitems, &rest,
				       &data) != Success) {
			REMMINA_PLUGIN_DEBUG("Could not get WM_COMMAND property from X11 "
					     "window ID [0x%lx].", w);
			continue;
		}

		if (data) {
			REMMINA_PLUGIN_DEBUG("Saw '%i' X11 events, which weren't "
					     "CreateNotify.", non_createnotify_count);
			REMMINA_PLUGIN_DEBUG("Found X11 window with WM_COMMAND set "
					     "to '%s', the window ID is [0x%lx].",
					     (char*)data, w);
		}
		if (data && g_strrstr((gchar*)data, cmd) &&
		    rmplugin_x2go_try_window_id(w)) {
			gpdata->window_id = w;
			agent_window_found = TRUE;
			XFree(data);
			break;
		}
		if (data)
			XFree(data);
	}

	XSetErrorHandler(gpdata->orig_handler);
	XCloseDisplay(gpdata->display);
	gpdata->display = NULL;

	CANCEL_ASYNC

	if (!agent_window_found) {
		g_strlcpy(errmsg, _("No X2Go session window appeared. "
				    "Something went wrong…"), 512);
		return FALSE;
	}

	return TRUE;
}

static gboolean rmplugin_x2go_start_session(RemminaProtocolWidget *gp)
{
	TRACE_CALL(__func__);
	REMMINA_PLUGIN_DEBUG("Function entry.");

	RemminaPluginX2GoData *gpdata = GET_PLUGIN_DATA(gp);;
	RemminaFile *remminafile;
	const gchar errmsg[512] = {0};
	gboolean ret = TRUE;

	gchar *servstr, *host, *username, *password, *command, *kbdlayout, *kbdtype,
	      *audio, *clipboard, *res, *ssh_privatekey;
	gint sshport, dpi;
	GdkDisplay *default_dsp;
	gint width, height;

	// We save the X Display name (:0) as we will need to synchronize the clipboards
	default_dsp = gdk_display_get_default();
	const gchar *default_dsp_name = gdk_display_get_name(default_dsp);
	REMMINA_PLUGIN_DEBUG("Default display is '%s'.", default_dsp_name);

	remminafile = rm_plugin_service->protocol_plugin_get_file(gp);

	servstr = GET_PLUGIN_STRING("server");
	if (servstr) {
		rm_plugin_service->get_server_port(servstr, 22, &host, &sshport);
	} else {
		return FALSE;
	}

	if (!sshport) sshport=22;

	username = GET_PLUGIN_STRING("username");
	password = GET_PLUGIN_PASSWORD("password");

	command = GET_PLUGIN_STRING("command");
	if (!command) command = "TERMINAL";

	kbdlayout = GET_PLUGIN_STRING("kbdlayout");
	kbdtype = GET_PLUGIN_STRING("kbdtype");

	audio = GET_PLUGIN_STRING("audio");

	clipboard = GET_PLUGIN_STRING("clipboard");

	dpi = GET_PLUGIN_INT("dpi", 80);

	ssh_privatekey = GET_PLUGIN_STRING("ssh_privatekey");

	// If empty set to NULL
	if(ssh_privatekey && g_str_equal(ssh_privatekey, "")) {
		ssh_privatekey = NULL;
	}

	width = rm_plugin_service->get_profile_remote_width(gp);
	height = rm_plugin_service->get_profile_remote_height(gp);
	/* multiple of 4 */
	width = (width + 3) & ~0x3;
	height = (height + 3) & ~0x3;
	if ((width > 0) && (height  > 0)) {
		res = g_strdup_printf ("%dx%d", width, height);
	} else {
		res = "800x600";
	}
	REMMINA_PLUGIN_DEBUG("Resolution set by user: '%s'.", res);

	REMMINA_PLUGIN_DEBUG("Attached window to socket '%d'.", gpdata->socket_id);

	/* register for notifications of window creation events */
	if (ret) ret = rmplugin_x2go_start_create_notify(gp, (gchar*)&errmsg);

	/* trigger the session start, session window should appear soon after this */
	if (ret) ret = rmplugin_x2go_exec_x2go(host, sshport, username, password, command,
					       kbdlayout, kbdtype, audio, clipboard, dpi,
					       res, ssh_privatekey, gp,
					       (gchar*)&errmsg);

	/* get the window ID of the remote x2goagent */
	if (ret) ret = rmplugin_x2go_monitor_create_notify(gp, "x2goagent",
						           (gchar*)&errmsg);

	if (!ret) {
		REMMINA_PLUGIN_CRITICAL("%s", errmsg);
		rm_plugin_service->protocol_plugin_set_error(gp, "%s", &errmsg);
		return FALSE;
	}

	/* embed it */
	onMainThread_gtk_socket_add_id(GTK_SOCKET(gpdata->socket), gpdata->window_id);

	return TRUE;
}

static gboolean rmplugin_x2go_main(RemminaProtocolWidget *gp)
{
	TRACE_CALL(__func__);
	RemminaPluginX2GoData *gpdata = GET_PLUGIN_DATA(gp);
	gboolean ret = FALSE;

	ret = rmplugin_x2go_start_session(gp);

	gpdata->thread = 0;
	return ret;
}

static gpointer rmplugin_x2go_main_thread(RemminaProtocolWidget* gp)
{
	TRACE_CALL(__func__);
	if (!gp) {
		REMMINA_PLUGIN_CRITICAL("%s", g_strdup_printf(
			_("Internal error: %s"),
			_("RemminaProtocolWidget* gp is 'NULL'!")
		));
		return NULL;
	}

	pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);

	CANCEL_ASYNC
	if (!rmplugin_x2go_main(gp)) {
		IDLE_ADD((GSourceFunc) rmplugin_x2go_cleanup, gp);
	}

	return NULL;
}

static gboolean rmplugin_x2go_open_connection(RemminaProtocolWidget *gp)
{
	TRACE_CALL(__func__);
	RemminaPluginX2GoData *gpdata = GET_PLUGIN_DATA(gp);

	if (!rm_plugin_service->gtksocket_available()) {
		rm_plugin_service->protocol_plugin_set_error(gp, _("The %s protocol is "
				  "unavailable because GtkSocket only works under X.org"),
				  PLUGIN_NAME);
		return FALSE;
	}

	gpdata->socket_id = gtk_socket_get_id(GTK_SOCKET(gpdata->socket));
	// casting to void* is allowed since return type 'gpointer' is actually void*.
	if (pthread_create(&gpdata->thread, NULL, (void*) rmplugin_x2go_main_thread, gp)) {
		rm_plugin_service->protocol_plugin_set_error(gp, _("Could not initialize "
					  "pthread. Falling back to non-threaded mode…"));
		gpdata->thread = 0;
		return FALSE;
	} else  {
		return TRUE;
	}
}

static gboolean rmplugin_x2go_query_feature(RemminaProtocolWidget* gp,
					    const RemminaProtocolFeature* feature)
{
	TRACE_CALL(__func__);
	return TRUE;
}

static const RemminaProtocolFeature rmplugin_x2go_features[] = {
	{REMMINA_PROTOCOL_FEATURE_TYPE_GTKSOCKET, RMPLUGIN_X2GO_FEATURE_GTKSOCKET, NULL, NULL, NULL},
	{REMMINA_PROTOCOL_FEATURE_TYPE_END, 0, NULL, NULL, NULL}
};


/**
 * @brief This function builds a string like: "'value1', 'value2' and 'value3'" \n
 * 	  To be used in a loop. \n
 *	  See rmplugin_x2go_string_setting_validator() for an example.
 *
 * @param max_elements Number of maximum elements.
 * @param element_to_add Next element to add to the string
 * @param current_element Which element is element_to_add?
 * @param string The string to which `element_to_add` will be added.
 */
static gchar* rmplugin_x2go_enumeration_prettifier(const guint max_elements,
						   const guint current_element,
						   gchar* element_to_add,
						   gchar* string)
{
	if (max_elements > 2) {
		if (current_element == max_elements - 1) {
			// TRANSLATORS: Presumably you just want to translate 'and' into
			// your language.
			// (Except your listing-grammar differs from english.)
			// 'value1', 'value2', 'valueN-1' and 'valueN'
			return g_strdup_printf(_("%sand '%s'"), string, element_to_add);
		} else if (current_element == max_elements - 2) {
			// TRANSLATORS: Presumably you just want to leave it english.
			// (Except your listing-grammar differs from english.)
			// 'value1', 'value2', 'valueN-1' and 'valueN'
			return g_strdup_printf(_("%s'%s' "), string, element_to_add);
		} else {
			// TRANSLATORS: Presumably you just want to leave it english.
			// (Except your listing-grammar differs from english.)
			// 'value1', 'value2', 'valueN-1' and 'valueN'
			return g_strdup_printf(_("%s'%s', "), string, element_to_add);
		}
	} else if (max_elements == 2) {
		if (current_element == max_elements - 1) {
			// TRANSLATORS: Presumably you just want to translate 'and' into
			// your language.
			// (Except your listing-grammar differs from english.)
			// 'value1' and 'value2'
			return g_strdup_printf(_("%sand '%s'"), string, element_to_add);
		} else {
			// TRANSLATORS: Presumably you just want to leave it english.
			// (Except your listing-grammar differs from english.)
			// 'value1' and 'value2'
			return g_strdup_printf(_("%s'%s' "), string, element_to_add);
		}
	} else {
		return g_strdup(element_to_add);
	}
}

/**
 * @brief Validator-functions are getting executed when the user wants to save profile
 *	  settings. It uses the given data (See RemminaProtocolSetting array) to determine
 *	  which strings are allowed and returns a end-user friendly error message.
 *
 * @param key Key is the setting's name.
 * @param value Value to validate.
 * @param data Data needed for validation process. See RemminaProtocolSetting array.
 *
 * @returns End-user friendly and translated error message, explaining why the given
 *	    value is invalid. If the given value is error-free then NULL gets returned.
 *
 */
static GError* rmplugin_x2go_string_setting_validator(gchar* key, gchar* value,
						      gchar* data)
{
	GError *error = NULL;

	if (!data) {
		gchar *error_msg = _("Invalid validation data in ProtocolSettings array!");
		REMMINA_PLUGIN_CRITICAL("%s", error_msg);
		g_set_error(&error, 1, 1, "%s", error_msg);
		return error;
	}

	gchar **elements_list = g_strsplit(data, ",", 0);

	guint elements_amount = 0;
	elements_amount = g_strv_length(elements_list);

	if (elements_list == NULL ||
	    elements_list[0] == NULL ||
	    strlen(elements_list[0]) <= 0)
	{
		gchar *error_msg = _("Validation data in ProtocolSettings array is invalid!");
		REMMINA_PLUGIN_CRITICAL("%s", error_msg);
		g_set_error(&error, 1, 1, "%s", error_msg);
		return error;
	}

	gchar *data_str = "";

	if (!key || !value) {
		REMMINA_PLUGIN_CRITICAL("%s", _("Parameters 'key' or 'value' are 'NULL'!"));
		g_set_error(&error, 1, 1, "%s", _("Internal error."));
		return error;
	}

	for (guint i = 0; elements_list[i] != NULL; i++) {
		// Don't wanna crash if elements_list[i] is NULL.
		gchar* element = elements_list[i] ? elements_list[i] : "";
		if (g_strcmp0(value, element) == 0) {
			// We found value in elements_list. Value passed validation.
			return NULL;
		}

		data_str = rmplugin_x2go_enumeration_prettifier(elements_amount, i,
						     		element, data_str);
	}

	if (elements_amount > 1) {
		g_set_error(&error, 1, 1, _("Allowed values are %s."), data_str);
	} else {
		g_set_error(&error, 1, 1, _("The only allowed value is '%s'."), data_str);
	}

	g_free(data_str);
	g_strfreev(elements_list);

	return error;
}

/**
 * @brief Validator-functions are getting executed when the user wants to save profile
 *	  settings. It uses the given data (See RemminaProtocolSetting array) to determine
 *	  if the given value is a valid integer is in range and returns a end-user
 *	  friendly error message.
 *
 * @param key Key is the setting's name.
 * @param value Value to validate.
 * @param data Data needed for validation process. See RemminaProtocolSetting array.
 *
 * @returns End-user friendly and translated error message, explaining why the given
 *	    value is invalid. If the given value is error-free then NULL gets returned.
 *
 */
static GError* rmplugin_x2go_int_setting_validator(gchar* key, gpointer value,
						   gchar* data)
{
	GError *error = NULL;

	gchar **integer_list = g_strsplit(data, ";", 0);

	if (integer_list == NULL ||
	    integer_list[0] == NULL ||
	    integer_list[1] == NULL ||
	    strlen(integer_list[0]) <= 0 ||
	    strlen(integer_list[1]) <= 0)
	{
		gchar *error_msg = _("Validation data in ProtocolSettings array is invalid!");
		REMMINA_PLUGIN_CRITICAL("%s", error_msg);
		g_set_error(&error, 1, 1, "%s", error_msg);
		return error;
	}

	gint minimum;
	str2int_errno err = str2int(&minimum, integer_list[0], 10);
	if (err == STR2INT_INCONVERTIBLE) {
		g_set_error(&error, 1, 1, "%s", g_strdup_printf(
			_("Internal error: %s"),
			_("The lower limit is not a valid integer!")
		));
	} else if (err == STR2INT_OVERFLOW) {
		g_set_error(&error, 1, 1, "%s", g_strdup_printf(
			_("Internal error: %s"),
			_("The lower limit is too high!")
		));
	} else if (err == STR2INT_UNDERFLOW) {
		g_set_error(&error, 1, 1, "%s", g_strdup_printf(
			_("Internal error: %s"),
			_("The lower limit is too low!")
		));
	} else if (err == STR2INT_INVALID_DATA) {
		g_set_error(&error, 1, 1, "%s", g_strdup_printf(
			_("Internal error: %s"),
			_("Something unknown went wrong.")
		));
	}

	if (error) {
		REMMINA_PLUGIN_CRITICAL("%s", _("Please check the RemminaProtocolSetting "
						"array for possible errors."));
		return error;
	}

	gint maximum;
	err = str2int(&maximum, integer_list[1], 10);
	if (err == STR2INT_INCONVERTIBLE) {
		g_set_error(&error, 1, 1, "%s", g_strdup_printf(
			_("Internal error: %s"),
			_("The upper limit is not a valid integer!")
		));
	} else if (err == STR2INT_OVERFLOW) {
		g_set_error(&error, 1, 1, "%s", g_strdup_printf(
			_("Internal error: %s"),
			_("The upper limit is too high!")
		));
	} else if (err == STR2INT_UNDERFLOW) {
		g_set_error(&error, 1, 1, "%s", g_strdup_printf(
			_("Internal error: %s"),
			_("The upper limit is too low!")
		));
	} else if (err == STR2INT_INVALID_DATA) {
		g_set_error(&error, 1, 1, "%s", g_strdup_printf(
			_("Internal error: %s"),
			_("Something unknown went wrong.")
		));
	}

	if (error) {
		REMMINA_PLUGIN_CRITICAL("%s", _("Please check the RemminaProtocolSetting "
						"array for possible errors."));
		return error;
	}

	gint int_value;
	err = str2int(&int_value, value, 10);
	if (err == STR2INT_INCONVERTIBLE) {
		// non-numerical characters are can't be entered but, the user can
		// input an empty string.
		g_set_error(&error, 1, 1, "%s", _("The input is not a valid integer!"));
	} else if (err == STR2INT_OVERFLOW || err == STR2INT_UNDERFLOW) {
		g_set_error(&error, 1, 1, _("Input must be a number between %i and %i."),
					minimum, maximum);
	} else if (err == STR2INT_INVALID_DATA) {
		g_set_error(&error, 1, 1, "%s", _("Something unknown went wrong."));
	}

	if (error) {
		return error;
	}

	/*REMMINA_PLUGIN_DEBUG("Key:  \t%s", (gchar*) key);
	REMMINA_PLUGIN_DEBUG("Value:\t%s", (gchar*) value);
	REMMINA_PLUGIN_DEBUG("Data: \t%s", data);
	REMMINA_PLUGIN_DEBUG("Min: %i, Max: %i", minimum, maximum);
	REMMINA_PLUGIN_DEBUG("Value converted:\t%i", int_value);*/

	if (err == STR2INT_SUCCESS && (minimum > int_value || int_value > maximum)) {
		g_set_error(&error, 1, 1, _("Input must be a number between %i and %i."),
			    minimum, maximum);
	}

	// Should be NULL.
	return error;
}

/* Array of RemminaProtocolSetting for basic settings.
 * Each item is composed by:
 * a) RemminaProtocolSettingType for setting type
 * b) Setting name
 * c) Setting description
 * d) Compact disposition
 * e) Values for REMMINA_PROTOCOL_SETTING_TYPE_SELECT or REMMINA_PROTOCOL_SETTING_TYPE_COMBO
 * f) Setting tooltip
 * g) Validation data pointer, will be passed to the validation callback method.
 * h) Validation callback method (Can be NULL. Every entry will be valid then.)
 *		use following prototype:
 *		gboolean mysetting_validator_method(gpointer key, gpointer value,
 *						    gpointer validator_data);
 *		gpointer key is a gchar* containing the setting's name,
 *		gpointer value contains the value which should be validated,
 *		gpointer validator_data contains your passed data.
 */
static const RemminaProtocolSetting rmplugin_x2go_basic_settings[] = {
    {REMMINA_PROTOCOL_SETTING_TYPE_SERVER,	"server",	NULL,				FALSE, NULL, NULL, NULL, NULL},
    {REMMINA_PROTOCOL_SETTING_TYPE_TEXT,	"username",	N_("Username"), 		FALSE, NULL, NULL, NULL, NULL},
    {REMMINA_PROTOCOL_SETTING_TYPE_PASSWORD,	"password",	N_("Password"), 		FALSE, NULL, NULL, NULL, NULL},
    {REMMINA_PROTOCOL_SETTING_TYPE_COMBO,	"command",	N_("Startup program"),		FALSE,
     /* SELECT & COMBO Values */ "MATE,KDE,XFCE,LXDE,TERMINAL",
     /* Tooltip */ N_("Which command should be executed after creating the X2Go session?"), 			   NULL, NULL},
    {REMMINA_PROTOCOL_SETTING_TYPE_RESOLUTION,	"resolution",	NULL, 				FALSE, NULL, NULL, NULL, NULL},
    {REMMINA_PROTOCOL_SETTING_TYPE_TEXT,	"kbdlayout", 	N_("Keyboard Layout (auto)"), 	FALSE, NULL, NULL, NULL, NULL},
    {REMMINA_PROTOCOL_SETTING_TYPE_TEXT,	"kbdtype", 	N_("Keyboard type (auto)"), 	FALSE, NULL, NULL, NULL, NULL},
    {REMMINA_PROTOCOL_SETTING_TYPE_COMBO,	"audio", 	N_("Audio support"), 		FALSE,
     /* SELECT & COMBO Values */ "pulse,esd,none",
     /* Tooltip */ N_("The sound system of the X2Go server (default: 'pulse')."),
     /* Validation data */ "pulse,esd,none",
     /* Validation method */ G_CALLBACK(rmplugin_x2go_string_setting_validator)},
    {REMMINA_PROTOCOL_SETTING_TYPE_COMBO,	"clipboard", 	N_("Clipboard direction"), 	FALSE,
     /* SELECT & COMBO Values */ "none,server,client,both",
     /* Tooltip */ N_("Which direction should clipboard content be copied? "
		      "(default: 'both')."),
     /* Validation data */ "none,server,client,both",
     /* Validation method */ G_CALLBACK(rmplugin_x2go_string_setting_validator)},
    {REMMINA_PROTOCOL_SETTING_TYPE_INT,		"dpi", 		N_("DPI resolution"), 		FALSE,	NULL,
     /* Tooltip */ N_("Launch session with a specific resolution (in dots per inch). "
		      "Must be between 20 and 400."),
     /* Validation data */ "20;400", // "<min>;<max>;"
     /* Validation method */ G_CALLBACK(rmplugin_x2go_int_setting_validator)},
    {REMMINA_PROTOCOL_SETTING_TYPE_FILE,	"ssh_privatekey",	N_("SSH identity file"),		FALSE, NULL, N_("Your private key"), NULL, NULL },
    {REMMINA_PROTOCOL_SETTING_TYPE_END, NULL, NULL, FALSE, NULL, NULL, NULL, NULL}};

/* Protocol plugin definition and features */
static RemminaProtocolPlugin rmplugin_x2go = {
	REMMINA_PLUGIN_TYPE_PROTOCOL,		// Type
	PLUGIN_NAME,				// Name
	PLUGIN_DESCRIPTION,			// Description
	GETTEXT_PACKAGE,			// Translation domain
	PLUGIN_VERSION,				// Version number
	PLUGIN_APPICON,				// Icon for normal connection
	PLUGIN_SSH_APPICON,			// Icon for SSH connection
	rmplugin_x2go_basic_settings,		// Array for basic settings
	NULL,					// Array for advanced settings
	REMMINA_PROTOCOL_SSH_SETTING_NONE,	// SSH settings type
	rmplugin_x2go_features,			// Array for available features
	rmplugin_x2go_init,			// Plugin initialization method
	rmplugin_x2go_open_connection,		// Plugin open connection method
	rmplugin_x2go_close_connection,		// Plugin connection-method closure
	rmplugin_x2go_query_feature,		// Query for available features
	NULL,					// Call a feature
	NULL,					// Send a keystroke
	NULL,					// Screenshot
};

G_MODULE_EXPORT gboolean remmina_plugin_entry(RemminaPluginService *service)
{
	TRACE_CALL("remmina_plugin_entry");
	rm_plugin_service = service;

	bindtextdomain(GETTEXT_PACKAGE, REMMINA_RUNTIME_LOCALEDIR);
	bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");

	if (!service->register_plugin((RemminaPlugin *) &rmplugin_x2go)) {
		return FALSE;
	}

	pthread_mutex_init(&remmina_x2go_init_mutex, NULL);
	remmina_x2go_window_id_array = g_array_new(FALSE, TRUE, sizeof(Window));

	REMMINA_PLUGIN_MESSAGE("%s", _("X2Go plugin loaded."));

	return TRUE;
}