[go: up one dir, main page]

File: mainwindow.cpp

package info (click to toggle)
kylin-nm 3.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,312 kB
  • sloc: cpp: 15,740; ansic: 901; makefile: 21
file content (5087 lines) | stat: -rw-r--r-- 203,126 bytes parent folder | download | duplicates (2)
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
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
/*
 * Copyright (C) 2020 Tianjin KYLIN Information Technology Co., Ltd.
 *
 * 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 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/&gt;.
 *
 */

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "oneconnform.h"
#include "onelancform.h"
#include "wifi-auth-thread.h"
#include "hot-spot/dlghotspotcreate.h"
#include "wireless-security/dlghidewifi.h"
#include "sysdbusregister.h"

#include <algorithm>//sort函数包含的头文件
#include <syslog.h>

#include <KWindowEffects>
#include <QFont>
#include <QFontMetrics>
#include <QDir>
#include <QtConcurrent>

QString hideWiFiConn;
//QStringList lcards,wcards;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //QIcon::setThemeName("ukui-icon-theme-default");
    // 如果使用Qt::Popup 任务栏不显示且保留X事件如XCB_FOCUS_OUT, 但如果indicator点击鼠标右键触发,XCB_FOCUS_OUT事件依然会失效
    // 如果使用Qt::ToolTip, Qt::Tool + Qt::WindowStaysOnTopHint, Qt::X11BypassWindowManagerHint等flag则会导致X事件失效
    // this->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
    //this->setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);//QTool
    this->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);//QTool
    this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明

    firstlyStart(); //先执行一些不耗时的一级启动项
}

MainWindow::~MainWindow()
{
    trayIcon->deleteLater();
    trayIconMenu->deleteLater();
    delete ui;
}

/**
 * @brief MainWindow::firstlyStart 一级启动
 */
void MainWindow::firstlyStart()
{
//    qDebug()<<"Loading qss...";
//    UseQssFile::setStyle("style.qss");

    qDebug()<<"Painting blurRegion...";
    QPainterPath path;
    auto rect = this->rect();
    rect.adjust(1, 1, -1, -1);
    path.addRoundedRect(rect, 6, 6);
    setProperty("blurRegion", QRegion(path.toFillPolygon().toPolygon()));
    KWindowEffects::enableBlurBehind(this->winId(), true, QRegion(path.toFillPolygon().toPolygon()));

    editQssString(); //编辑部分控件QSS
    createTopLanUI(); //创建顶部有线网item
    createTopWifiUI(); //创建顶部无线网item
    createOtherUI(); //创建上传下载控件,列表区无item时的说明控件
    createListAreaUI(); //创建列表区域的控件
    createLeftAreaUI(); //创建左侧区域控件

    lcardname = "-1";
    wcardname = "-1";
    hideWiFiConn = "Connect to Hidden WLAN Network";

    createTrayIcon();
    connect(trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::iconActivated);
    connect(mShowWindow, &QAction::triggered, this, &MainWindow::on_showWindowAction);
    connect(mAdvConf, &QAction::triggered, this, &MainWindow::actionTriggerSlots);
    connect(this,&MainWindow::showPbSignal,trayIcon,&QSystemTrayIcon::activated);

    //checkSingleAndShowTrayicon();
    //trayIcon->setVisible(true);
    // 连接kds的dbus接收rfkill变化的信号&获取当前WIFI状态
    qDebug()<<"Initing kdsDbus...";
    kdsDbus = new QDBusInterface("org.ukui.kds", \
                               "/", \
                               "org.ukui.kds.interface", \
                               QDBusConnection::systemBus());
    QDBusConnection::systemBus().connect(kdsDbus->service(), kdsDbus->path(), kdsDbus->interface(), "signalRfkillStatusChanged", this, SLOT(onRfkillStatusChanged()));

    PrimaryManager();

    qDebug()<<"Init objKyDbus...";
    objKyDBus = new KylinDBus(this);
    objKyDBus->initConnectionInfo();
    connect(objKyDBus, SIGNAL(toGetWifiListFinished(QStringList)), this, SLOT(loadWifiListDone(QStringList)));

    objNetSpeed = new NetworkSpeed();

    qDebug()<<"Init confForm...";
    QApplication::setQuitOnLastWindowClosed(false);
    this->confForm = new ConfForm();

    qDebug()<<"Init ksnm...";
    this->ksnm = new KSimpleNM();
    connect(ksnm, SIGNAL(getLanListFinished(QStringList)), this, SLOT(getLanListDone(QStringList)));
    connect(ksnm, SIGNAL(getWifiListFinished(QStringList)), this, SLOT(getWifiListDone(QStringList)));
    connect(ksnm, SIGNAL(getConnListFinished(QStringList)), this, SLOT(getConnListDone(QStringList)));
    connect(ksnm, SIGNAL(requestRevalueUpdateWifi()), this, SLOT(onRequestRevalueUpdateWifi()));

    qDebug()<<"Init LoadingDiv...";
    loading = new LoadingDiv(this);
    loading->move(40,0);
    connect(loading, SIGNAL(toStopLoading() ), this, SLOT(on_checkOverTime() ));

    checkIsWirelessDevicePluggedIn(); //检测无线网卡是否插入
    initLanSlistAndGetReconnectNetList(); //初始化有线网列表
    initNetwork(); //初始化网络
    initTimer(); //初始化定时器
    initActNetDNS();//初始化已连接网络的DNS
    getSystemFontFamily();//建立GSetting监听系统字体

    qDebug()<<"Init button connections...";
    connect(ui->btnNetList, &QPushButton::clicked, this, &MainWindow::onBtnNetListClicked);
    connect(btnWireless, &SwitchButton::clicked,this, &MainWindow::onBtnWifiClicked);
    connect(btnWired, &SwitchButton::clicked, this, &MainWindow::onBtnLanClicked);
    connect(this, &MainWindow::onWiredDeviceChanged, this, &MainWindow::setLanSwitchStatus);

    QVariant wifi_state = BackThread::getSwitchState(WIFI_SWITCH_OPENED);
    QVariant lan_state = BackThread::getSwitchState(LAN_SWITCH_OPENED);
    if (!wifi_state.isNull() && wifi_state.isValid() && is_wireless_adapter_ready == 1) {
        //设置WiFi开关状态
        if (wifi_state.toBool() && !btnWireless->getSwitchStatus())
            onBtnWifiClicked(1);
        else if (!wifi_state.toBool() && btnWireless->getSwitchStatus())
            onBtnWifiClicked(0);
    }
    if (!lan_state.isNull() && lan_state.isValid()) {
        //设置lan开关状态
        objKyDBus->getPhysicalCarrierState();
        if (lan_state.toBool() && objKyDBus->isWiredCableOn)
            onBtnLanClicked(1);
        else if(!lan_state.toBool())
            onBtnLanClicked(0);
    }
    connect(btnWired, &SwitchButton::switchStatusChanged, this, [ = ]() {
        BackThread::saveSwitchButtonState(LAN_SWITCH_OPENED, btnWired->getSwitchStatus());
    });
    connect(btnWireless, &SwitchButton::switchStatusChanged, this, [ = ]() {
        BackThread::saveSwitchButtonState(WIFI_SWITCH_OPENED, btnWireless->getSwitchStatus());
    });

    //检查有线网络的个数是否为0,如果是0,则新建一个有线网络
    checkIfWiredNetExist();

    m_secondary_start_timer = new QTimer(this);
    connect(m_secondary_start_timer, &QTimer::timeout, this, [ = ]() {
        m_secondary_start_timer->stop();
        secondaryStart();//满足条件后执行比较耗时的二级启动
    });
    m_secondary_start_timer->start(5 * 1000);
}

/**
 * @brief MainWindow::secondaryStart 二级启动
 */
void MainWindow::secondaryStart()
{
    if (m_load_finished)
        return;
    toStart();

    ui->btnNetList->setAttribute(Qt::WA_Hover,true);
    ui->btnNetList->installEventFilter(this);
    ui->btnWifiList->setAttribute(Qt::WA_Hover,true);
    ui->btnWifiList->installEventFilter(this);
    hasWifiConnected = false;

    m_load_finished = true;
}

void MainWindow::checkSingleAndShowTrayicon()
{
    int fd = 0;
    try {
        QStringList homePath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
        QString lockPath = QString(homePath.at(0) + "/.config/kylin-nm-lock-%1.lock").arg(getenv("DISPLAY")).toUtf8().data();
        fd = open(lockPath.toUtf8().data(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
        if (fd < 0) {
            throw -1;
        }
    } catch(...) {
        fd = open("/tmp/kylin-nm-lock", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
        if (fd < 0) {
            exit(0);
        }
    }


    if (lockf(fd, F_TLOCK, 0)) {
        //syslog(LOG_ERR, "Can't lock single file, kylin-network-manager is already running!");
        //qDebug()<<"Can't lock single file, kylin-network-manager is already running!";
        //exit(0);
    } else {
        trayIcon->setVisible(true);
    }
}

void MainWindow::justShowTrayIcon()
{
    trayIcon->setVisible(true);
}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == ui->btnNetList) {
        if (event->type() == QEvent::HoverEnter) {
            if (!is_btnLanList_clicked) {
                ui->lbNetListBG->setStyleSheet(btnBgHoverQss);
            }
            return true;
        } else if(event->type() == QEvent::HoverLeave) {
            if (!is_btnLanList_clicked) {
                ui->lbNetListBG->setStyleSheet(btnBgLeaveQss);
            }
            return true;
        }
    }

    if (obj == ui->btnWifiList) {
        if (event->type() == QEvent::HoverEnter) {
            if (!is_btnWifiList_clicked) {
                ui->lbWifiListBG->setStyleSheet(btnBgHoverQss);
            }
            return true;
        } else if(event->type() == QEvent::HoverLeave) {
            if (!is_btnWifiList_clicked) {
                ui->lbWifiListBG->setStyleSheet(btnBgLeaveQss);
            }
            return true;
        }
    }

    return QWidget::eventFilter(obj,event);
}

///////////////////////////////////////////////////////////////////////////////
// 初始化控件、网络、定时器

// 初始化界面各控件
void MainWindow::editQssString()
{
    btnOffQss = "QLabel{min-width: 37px; min-height: 37px;max-width:37px; max-height: 37px;border-radius: 4px; background-color:rgba(255,255,255,0)}";
    btnOnQss = "QLabel{min-width: 37px; min-height: 37px;max-width:37px; max-height: 37px;border-radius: 4px; background-color:rgba(61,107,229,1)}";
    btnBgOffQss = "QLabel{min-width: 48px; min-height: 22px;max-width:48px; max-height: 22px;border-radius: 10px; background-color:rgba(255,255,255,0.2)}";
    btnBgOnQss = "QLabel{min-width: 48px; min-height: 22px;max-width:48px; max-height: 22px;border-radius: 10px; background-color:rgba(61,107,229,1);}";
    btnBgHoverQss = "QLabel{border-radius: 4px; background-color:rgba(156,156,156,0.3)}";
    btnBgLeaveQss = "QLabel{border-radius: 4px; background-color:rgba(255,255,255,0)}";
    leftBtnQss = "QPushButton{border:0px;border-radius:4px;background-color:rgba(255,255,255,0);}"
                 "QPushButton:Hover{border:0px solid rgba(255,255,255,0.2);border-radius:4px;background-color:rgba(156,156,156,0.3);}"
                 "QPushButton:Pressed{border-radius:4px;background-color:rgba(156,156,156,0.3);}";
    funcBtnQss = "QPushButton{border:0px;border-radius:4px;background-color:rgba(255,255,255,0);color:rgba(107,142,235,0.97);font-size:14px;}"
                 "QPushButton:Hover{border:0px;border-radius:4px;background-color:rgba(255,255,255,0);color:rgba(151,175,241,0.97);font-size:14px;}"
                 "QPushButton:Pressed{border-radius:4px;background-color:rgba(255,255,255,0);color:rgba(61,107,229,0.97);font-size:14px;}";
}

void MainWindow::createTopLanUI()
{
    qDebug()<<"Creating Top Lan UI...";
    topLanListWidget = new QWidget(ui->centralWidget);
    topLanListWidget->move(W_LEFT_AREA, Y_TOP_ITEM);
    topLanListWidget->resize(W_TOP_LIST_WIDGET, H_NORMAL_ITEM + H_GAP_UP + X_ITEM);
    /*顶部的一个item*/
    lbTopLanList = new QLabel(topLanListWidget);
    lbTopLanList->setText(tr("Ethernet Networks"));//"可用网络列表"
    lbTopLanList->resize(W_MIDDLE_WORD, H_MIDDLE_WORD);
    lbTopLanList->move(X_MIDDLE_WORD, H_NORMAL_ITEM + H_GAP_UP);
    QFont fontTopLanList( "Noto Sans CJK SC", 14);
    lbTopLanList->setFont(fontTopLanList);
    lbTopLanList->show();
    /*新建有线网按钮*/
    btnCreateNet = new QPushButton(topLanListWidget);
    btnCreateNet->resize(W_BTN_FUN, H_BTN_FUN);
    btnCreateNet->move(X_BTN_FUN, Y_BTN_FUN);
    btnCreateNet->setText(tr("New LAN"));//"新建网络"
    btnCreateNet->setStyleSheet(funcBtnQss);
    btnCreateNet->setFocusPolicy(Qt::NoFocus);
    btnCreateNet->show();
    connect(btnCreateNet,SIGNAL(clicked()),this,SLOT(onBtnCreateNetClicked()));
}

void MainWindow::createTopWifiUI()
{
    qDebug()<<"Creating Top Wifi UI...";
    topWifiListWidget = new QWidget(ui->centralWidget);
    topWifiListWidget->move(W_LEFT_AREA, Y_TOP_ITEM);
    topWifiListWidget->resize(W_TOP_LIST_WIDGET, H_NORMAL_ITEM + H_GAP_UP + X_ITEM);
    /*顶部的一个item*/
    lbTopWifiList = new QLabel(topWifiListWidget);
    lbTopWifiList->setText(tr("WLAN Networks"));//"可用网络列表"
    lbTopWifiList->resize(W_MIDDLE_WORD, H_MIDDLE_WORD);
    lbTopWifiList->move(X_MIDDLE_WORD, H_NORMAL_ITEM + H_GAP_UP);
    QFont fontTopWifiList( "Noto Sans CJK SC", 14);
    lbTopWifiList->setFont(fontTopWifiList);
    lbTopWifiList->show();
    /*新建有线网按钮*/
    btnAddNet = new QPushButton(topWifiListWidget);
    btnAddNet->move(X_BTN_FUN, Y_BTN_FUN);
    btnAddNet->setText(tr("Hide WLAN"));//"加入网络"
    btnAddNet->setStyleSheet(funcBtnQss);
    int textWidth = QFontMetrics(btnAddNet->font()).width(btnAddNet->text());
    btnAddNet->resize(textWidth > W_BTN_FUN ? textWidth : W_BTN_FUN, H_BTN_FUN);
    btnAddNet->setFocusPolicy(Qt::NoFocus);
    btnAddNet->show();
    connect(btnAddNet,SIGNAL(clicked()),this,SLOT(onBtnAddNetClicked()));
}

void MainWindow::createOtherUI()
{
    qDebug()<<"Creating Other Ui...";
    lbLoadDown = new QLabel(ui->centralWidget);
    lbLoadDown->move(X_ITEM + 129, Y_TOP_ITEM + 32);
    lbLoadDown->resize(65, 20);
    lbLoadDownImg = new QLabel(ui->centralWidget);
    lbLoadDownImg->move(X_ITEM + 112, Y_TOP_ITEM + 35);
    lbLoadDownImg->resize(16, 16);

    lbLoadUp = new QLabel(ui->centralWidget);
    lbLoadUp->move(X_ITEM + 187, Y_TOP_ITEM + 32);
    lbLoadUp->resize(65, 20);
    lbLoadUpImg = new QLabel(ui->centralWidget);
    lbLoadUpImg->move(X_ITEM + 170, Y_TOP_ITEM + 35);
    lbLoadUpImg->resize(16, 16);

    lbLoadDownImg->setStyleSheet("QLabel{background-image:url(:/res/x/load-down.png);}");
    lbLoadUpImg->setStyleSheet("QLabel{background-image:url(:/res/x/load-up.png);}");

    lbNoItemTip = new QLabel(ui->centralWidget);
    lbNoItemTip->resize(W_NO_ITEM_TIP, H_NO_ITEM_TIP);
    lbNoItemTip->move(this->width()/2 - W_NO_ITEM_TIP/2 + W_LEFT_AREA/2, this->height()/2);
    lbNoItemTip->setStyleSheet("QLabel{border:none;background:transparent;}");
    lbNoItemTip->setText(tr("No usable network in the list"));//列表暂无可连接网络
    lbNoItemTip->setAlignment(Qt::AlignCenter);
    lbNoItemTip->hide();
}

void MainWindow::createListAreaUI()
{
    qDebug()<<"Creating List Area Ui...";
    scrollAreal = new QScrollArea(ui->centralWidget);
    scrollAreal->move(W_LEFT_AREA, Y_TOP_ITEM + H_NORMAL_ITEM + H_GAP_UP + X_ITEM + H_GAP_DOWN);
    scrollAreal->resize(W_SCROLL_AREA, H_SCROLL_AREA);
    scrollAreal->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scrollAreal->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);

    scrollAreaw = new QScrollArea(ui->centralWidget);
    scrollAreaw->move(W_LEFT_AREA, Y_TOP_ITEM + H_NORMAL_ITEM + H_GAP_UP + X_ITEM + H_GAP_DOWN);
    scrollAreaw->resize(W_SCROLL_AREA, H_SCROLL_AREA);
    scrollAreaw->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scrollAreaw->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);


    lanListWidget = new QWidget(scrollAreal);
    wifiListWidget = new QWidget(scrollAreaw);
    lbLanList = new QLabel(lanListWidget);
    lbWifiList = new QLabel(wifiListWidget);

    ui->lbNetwork->setStyleSheet("QLabel{font-size:20px;}");
    ui->lbNetwork->show();

    topLanListWidget->setStyleSheet("QWidget{border:none;}");
    topLanListWidget->setStyleSheet("background-color:transparent;");

    topWifiListWidget->setStyleSheet("QWidget{border:none;}");
    topWifiListWidget->setStyleSheet("background-color:transparent;");

    lbLoadUp->setStyleSheet("QLabel{font-size:14px;}");
    lbLoadDown->setStyleSheet("QLabel{font-size:14px;}");
    lbLoadUp->setText("0KB/s");
    lbLoadDown->setText("0KB/s.");
    lbLoadUp->hide();
    lbLoadDown->hide();
    lbLoadUpImg->hide();
    lbLoadDownImg->hide();

    this->on_setNetSpeed();

//    scrollAreal->setStyleSheet("QScrollArea{border:none;}");
//    scrollAreal->viewport()->setStyleSheet("background-color:transparent;");
    //scrollAreal->verticalScrollBar()->setStyleSheet(scrollBarQss);////

    QPalette scrollAreaPalette = scrollAreaw->palette();
    scrollAreaPalette.setBrush(QPalette::Window, QColor(0,0,0,0));

    scrollAreal->setFrameShape(QFrame::NoFrame);
    scrollAreal->setPalette(scrollAreaPalette);
    scrollAreal->verticalScrollBar()->setAttribute((Qt::WA_TranslucentBackground),true);
    scrollAreal->verticalScrollBar()->setProperty("drawScrollBarGroove",false);     //!!!??不绘制滚动条的槽

    scrollAreaw->setFrameShape(QFrame::NoFrame);
    scrollAreaw->setPalette(scrollAreaPalette);
    scrollAreaw->verticalScrollBar()->setAttribute((Qt::WA_TranslucentBackground),true);
    scrollAreaw->verticalScrollBar()->setProperty("drawScrollBarGroove",false);     //!!!??不绘制滚动条的槽

    QPalette palette = scrollAreaw->verticalScrollBar()->palette();
    QColor color(255,255,255);
    color.setAlphaF(0.25);
    palette.setColor(QPalette::Active, QPalette::Button, color);
    scrollAreaw->verticalScrollBar()->setPalette(palette);
    scrollAreal->verticalScrollBar()->setPalette(palette);
}

void MainWindow::createLeftAreaUI()
{
    qDebug()<<"Creating Left Area Ui...";
    btnWireless = new SwitchButton(this);
    btnWireless->setStyleSheet("SwitchButton{border:none;background-color:rgba(255,255,255,0.12);}");
    btnWired = new SwitchButton(this);
    btnWired->setStyleSheet("SwitchButton{border:none;background-color:rgba(255,255,255,0.12);}");
    ui->btnNetList->setFocusPolicy(Qt::NoFocus);
    QString txtEthernet(tr("Ethernet"));
    ui->btnNetList->setToolTip(txtEthernet);
    ui->lbNetListBG->setStyleSheet(btnOffQss);
    //设置PushButton背景透明
    QPalette paletteLan = ui->btnNetListImg->palette();
    paletteLan.setColor(QPalette::Highlight, Qt::transparent);
    paletteLan.setBrush(QPalette::Button, QBrush(QColor(1,1,1,0)));
    ui->btnNetListImg->setPalette(paletteLan);
    //添加PushButton的svg图片
    if (!QIcon::fromTheme("network-wired-connected-symbolic").isNull()) {
        ui->btnNetListImg->setIcon(QIcon::fromTheme("network-wired-connected-symbolic"));
    } else if (!QIcon::fromTheme("network-wired-symbolic").isNull()) {
        ui->btnNetListImg->setIcon(QIcon::fromTheme("network-wired-symbolic"));
    } else {
        ui->btnNetListImg->setIcon(QIcon(":/res/x/net-list-bg.svg"));
    }
    ui->btnNetListImg->setProperty("useIconHighlightEffect", true);
    ui->btnNetListImg->setProperty("iconHighlightEffectMode", true);

    ui->btnWifiList->setFocusPolicy(Qt::NoFocus);
    QString txtWifi(tr("WLAN"));
    ui->btnWifiList->setToolTip(txtWifi);
    ui->lbWifiListBG->setStyleSheet(btnOffQss);
    //设置PushButton背景透明
    QPalette paletteWifi = ui->btnWifiListImg->palette();
    paletteWifi.setColor(QPalette::Highlight, Qt::transparent);
    paletteWifi.setBrush(QPalette::Button, QBrush(QColor(1,1,1,0)));
    ui->btnWifiListImg->setPalette(paletteWifi);
    //添加PushButton的svg图片
    if (!QIcon::fromTheme("network-wireless-signal-excellent-symbolic").isNull())
        ui->btnWifiListImg->setIcon(QIcon::fromTheme("network-wireless-signal-excellent-symbolic"));
    else
        ui->btnWifiListImg->setIcon(QIcon(":/res/x/wifi-list-bg.svg"));
    ui->btnWifiListImg->setProperty("useIconHighlightEffect", true);
    ui->btnWifiListImg->setProperty("iconHighlightEffectMode", true);

    ui->btnNet->hide();

    btnWireless->setGeometry(412,20,50,24);
    btnWired->setGeometry(412,20,50,24);

    ui->btnHotspot->setStyleSheet(leftBtnQss);
    ui->btnHotspot->setFocusPolicy(Qt::NoFocus);
    QString txtHotSpot(tr("HotSpot"));
    ui->btnHotspot->setToolTip(txtHotSpot);
    ui->btnHotspot->hide();
    ui->lbHotImg->hide();
    ui->lbHotImg->setStyleSheet("QLabel{background-image:url(:/res/x/hot-spot-off.svg);}");
    ui->lbHotBG->hide();
    ui->lbHotBG->setStyleSheet(btnOffQss);

    ui->btnFlyMode->setStyleSheet(leftBtnQss);
    ui->btnFlyMode->setFocusPolicy(Qt::NoFocus);
    QString txtFlyMode(tr("FlyMode"));
    ui->btnFlyMode->setToolTip(txtFlyMode);
    ui->btnFlyMode->hide();
    ui->lbFlyImg->hide();
    ui->lbFlyImg->setStyleSheet("QLabel{background-image:url(:/res/x/fly-mode-off.svg);}");
    ui->lbFlyBG->hide();
    ui->lbFlyBG->setStyleSheet(btnOffQss);

    ui->btnAdvConf->setStyleSheet(leftBtnQss);
    ui->btnAdvConf->setFocusPolicy(Qt::NoFocus);
    QString txtAdvanced(tr("Advanced"));
    ui->btnAdvConf->setToolTip(txtAdvanced);
    //设置PushButton背景透明
    QPalette paletteConf = ui->btnConfImg->palette();
    paletteConf.setColor(QPalette::Highlight, Qt::transparent);
    paletteConf.setBrush(QPalette::Button, QBrush(QColor(1,1,1,0)));
    ui->btnConfImg->setPalette(paletteConf);
    //添加PushButton的svg图片
    ui->btnConfImg->setIcon(QIcon(":/res/x/control.svg"));
    ui->btnConfImg->setProperty("useIconHighlightEffect", true);
    ui->btnConfImg->setProperty("iconHighlightEffectMode", true);
}

// 初始化有线网列表,初始化可回连wifi列表
void MainWindow::initLanSlistAndGetReconnectNetList()
{
    qDebug()<<"Init Net List...";
    canReconnectWifiList.clear();
    const int BUF_SIZE = 1024;
    char buf[BUF_SIZE];

    FILE * p_file = NULL;

    //p_file = popen("export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli -f type,device,name connection show", "r");
    //nmcli -f type,uuid,name connection show
    p_file = popen("export LANG='en_US.UTF-8';export LANGUAGE='en_US';nmcli -f type,uuid,name connection show", "r");
    if (!p_file) {
        //syslog(LOG_ERR, "Error occurred when popen cmd 'nmcli connection show'");
        qDebug()<<"Error occurred when popen cmd 'nmcli connection show";
    }

    int trimNamePos = 0;
    while (fgets(buf, BUF_SIZE, p_file) != NULL) {
        QString strSlist = "";
        QString line(buf);
        strSlist = line.trimmed();
        if (strSlist.indexOf("UUID") != -1 || strSlist.indexOf("NAME") != -1) {
            trimNamePos = strSlist.indexOf("NAME");
            oldLanSlist.append(strSlist);
        }
        if (strSlist.indexOf("802-3-ethernet") != -1 || strSlist.indexOf("ethernet") != -1) {
            oldLanSlist.append(strSlist);
        }
        if (strSlist.indexOf("802-11-wireless") != -1 || strSlist.indexOf("wifi") != -1) {
            if (trimNamePos != 0) {
                QString mywifiname = strSlist.mid(trimNamePos).trimmed();//这里没有考虑wifi为空格的情况
                if (!canReconnectWifiList.contains(mywifiname)) {
                    canReconnectWifiList.append(mywifiname);
                }
            }
        }
    }

    pclose(p_file);
}

// 初始化网络
void MainWindow::initNetwork()
{
    qDebug()<<"Init Net interface & list...";
    IFace *iface = BackThread::execGetIface();
    wcardname = iface->wname;
    lcardname = iface->lname;
    confForm->lcard = lcardname;
    confForm->wcard = wcardname;

//    mwBandWidth = bt->execChkLanWidth(lcardname);

    // 开关状态
    qDebug()<<"===";
    qDebug()<<"state of network: '0' is connected, '1' is disconnected, '2' is net device switch off";
    //syslog(LOG_DEBUG, "state of network: '0' is connected, '1' is disconnected, '2' is net device switch off");
    qDebug()<<"current network state:  lan state ="<<iface->lstate<<",  wifi state ="<<iface->wstate ;
    //syslog(LOG_DEBUG, "current network state:  wired state =%d,  wifi state =%d", iface->lstate, iface->wstate);
    qDebug()<<"===";

    if (iface->wstate == 0 || iface->wstate == 1) {
        objKyDBus->oldWifiSwitchState = true;
        btnWireless->setSwitchStatus(true);
    } else {
        objKyDBus->oldWifiSwitchState = false;
        btnWireless->setSwitchStatus(false);
    }

    // 初始化网络列表
    if (iface->wstate != 2) {
        if (iface->wstate == 0) {
            connWifiDone(3);
        } else {
            if (iface->lstate == DEVICE_CONNECTED) {
                connLanDone(3);
            }
        }
        is_init_wifi_list = 1;
        on_btnWifiList_clicked();

        ui->btnNetList->setStyleSheet("QPushButton{border:0px solid rgba(255,255,255,0);background-color:rgba(255,255,255,0);}");
        ui->btnWifiList->setStyleSheet("QPushButton{border:none;}");
    } else {
        objKyDBus->setWifiSwitchState(false); //通知控制面板wifi未开启
        if (iface->lstate != DEVICE_UNMANAGED) {
            if (iface->lstate == DEVICE_CONNECTED) {
                connLanDone(3);
            }
            onBtnNetListClicked();

            ui->btnNetList->setStyleSheet("QPushButton{border:0px solid rgba(255,255,255,0);background-color:rgba(255,255,255,0);}");
            ui->btnWifiList->setStyleSheet("QPushButton{border:none;}");
        } else {
            BackThread *m_bt = new BackThread();
            IFace *m_iface = m_bt->execGetIface();

            //写成信号监听自动执行,避免阻塞主线程
           disconnect_time = 1;
           connect (m_bt, &BackThread::btFinish, this, [ = ](){
               disconnect_time ++;
               if (disconnect_time <= 3) {
                   m_bt->disConnLanOrWifi("ethernet");
               } else {
                   m_bt->disconnect();
               }
           });
            m_bt->disConnLanOrWifi("ethernet");

            delete m_iface;
            m_bt->deleteLater();

            char *chr = "nmcli networking on";
            Utils::m_system(chr);

            onBtnNetListClicked();

            ui->btnNetList->setStyleSheet("QPushButton{border:0px solid rgba(255,255,255,0);background-color:rgba(255,255,255,0);}");
            ui->btnWifiList->setStyleSheet("QPushButton{border:none;}");
        }
    }
    ksnm->execGetConnList();

    QString actWifiUuid = objKyDBus->getActiveWifiUuid();
    objKyDBus->getConnectNetIp(actWifiUuid);
    objKyDBus->getWifiIp(actWifiUuid);
    confForm->actWifiIpv6Addr = objKyDBus->dbusActiveWifiIpv6;
    delete iface;
    iface = nullptr;
}

// 初始化定时器
void MainWindow::initTimer()
{
    qDebug()<<"Init Timer...";
    //应用启动后,需要连接可连接的网络
    QTimer::singleShot(1*1000, this, SLOT(toReconnectWifi() ));

    //循环检测wifi列表的变化,可用于更新wifi列表
    numberForWifiScan = 0;
    QObject::connect(this, SIGNAL(loadWifiListAfterScan()), this, SLOT(onLoadWifiListAfterScan()));
    QObject::connect(this, SIGNAL(refreshWifiListAfterScan()), this, SLOT(onRefreshWifiListAfterScan()));
    QObject::connect(this, SIGNAL(requestReconnecWifi()), this, SLOT(onRequestReconnecWifi()));
    checkWifiListChanged = new QTimer(this);
    checkWifiListChanged->setTimerType(Qt::PreciseTimer);
    QObject::connect(checkWifiListChanged, SIGNAL(timeout()), this, SLOT(onRequestScanAccesspoint()));
    checkWifiListChanged->start(10*1000);

    //网线插入时定时执行
    wiredCableUpTimer = new QTimer(this);
    wiredCableUpTimer->setTimerType(Qt::PreciseTimer);
    QObject::connect(wiredCableUpTimer, SIGNAL(timeout()), this, SLOT(onCarrierUpHandle()));

    //网线拔出时定时执行
//    wiredCableDownTimer = new QTimer(this);
//    wiredCableDownTimer->setTimerType(Qt::PreciseTimer);
//    QObject::connect(wiredCableDownTimer, SIGNAL(timeout()), this, SLOT(onCarrierDownHandle()));
    connect(this, SIGNAL(carrierDownHandle()), this, SLOT(onCarrierDownHandle()));

    //定时处理异常网络,即当点击Lan列表按钮时,若lstate=2,但任然有有线网连接的情况
    deleteLanTimer = new QTimer(this);
    deleteLanTimer->setTimerType(Qt::PreciseTimer);
    QObject::connect(deleteLanTimer, SIGNAL(timeout()), this, SLOT(onDeleteLan()));

    //定时获取网速
    setNetSpeed = new QTimer(this);
    setNetSpeed->setTimerType(Qt::PreciseTimer);
    QObject::connect(setNetSpeed, SIGNAL(timeout()), this, SLOT(on_setNetSpeed()));
    setNetSpeed->start(1000);
}

//初始化已经连接网络的DNS
void MainWindow::initActNetDNS()
{
    qDebug()<<"Init Active Net Dns...";
    QList<QString> currConnLanSsidUuidState =objKyDBus->getAtiveLanSsidUuidState();

    if (currConnLanSsidUuidState.size() > 0) {
        oldActLanName = currConnLanSsidUuidState.at(0);
        objKyDBus->getLanIpDNS(currConnLanSsidUuidState.at(1), true);
        oldDbusActLanDNS = objKyDBus->dbusActLanDNS;
    }
}

///////////////////////////////////////////////////////////////////////////////
// 任务栏托盘管理、托盘图标处理

void MainWindow::createTrayIcon()
{
    qDebug()<<"Creating Tray Icon...";
    trayIcon = new QSystemTrayIcon();
    trayIcon->setToolTip(QString(tr("kylin-nm")));

    trayIconMenu = new QMenu();

    mShowWindow = new QAction(tr("Show MainWindow"),this);
    mAdvConf = new QAction(tr("Advanced"),this);
    mAdvConf->setIcon(QIcon::fromTheme("document-page-setup-symbolic", QIcon(":/res/x/setup.png")) );

    trayIconMenu->addAction(mShowWindow);
    trayIconMenu->addAction(mAdvConf);
    trayIcon->setContextMenu(trayIconMenu);

    // 初始化托盘所有Icon
    if (!QIcon::fromTheme("network-wired-connected-symbolic").isNull()) {
        iconLanOnline = QIcon::fromTheme("network-wired-connected-symbolic");
    } else if (!QIcon::fromTheme("network-wired-symbolic").isNull()) {
        iconLanOnline = QIcon::fromTheme("network-wired-symbolic");
    } else {
        iconLanOnline = QIcon(":/res/x/net-list-bg.svg");
    }
    if (!QIcon::fromTheme("network-wired-disconnected-symbolic").isNull()) {
        iconLanOffline = QIcon::fromTheme("network-wired-disconnected-symbolic");
    } else {
        iconLanOffline = QIcon::fromTheme("network-wired-offline-symbolic");
    }
    iconLanOnlineNoInternet = QIcon::fromTheme("network-error-symbolic");
    iconWifiFull = QIcon::fromTheme("network-wireless-signal-excellent-symbolic");
    iconWifiHigh = QIcon::fromTheme("network-wireless-signal-good-symbolic");
    iconWifiMedium = QIcon::fromTheme("network-wireless-signal-ok-symbolic").isNull() ?
                    QIcon::fromTheme("network-wireless-signal-ok") : QIcon::fromTheme("network-wireless-signal-ok-symbolic");
    iconWifiLow = QIcon::fromTheme("network-wireless-signal-weak-symbolic").isNull() ?
                    QIcon::fromTheme("network-wireless-signal-low") : QIcon::fromTheme("network-wireless-signal-weak-symbolic");

    loadIcons.append(QIcon::fromTheme("kylin-network-1"));
    loadIcons.append(QIcon::fromTheme("kylin-network-2"));
    loadIcons.append(QIcon::fromTheme("kylin-network-3"));
    loadIcons.append(QIcon::fromTheme("kylin-network-4"));
    loadIcons.append(QIcon::fromTheme("kylin-network-5"));
    loadIcons.append(QIcon::fromTheme("kylin-network-6"));
    loadIcons.append(QIcon::fromTheme("kylin-network-7"));
    loadIcons.append(QIcon::fromTheme("kylin-network-8"));
    loadIcons.append(QIcon::fromTheme("kylin-network-9"));
    loadIcons.append(QIcon::fromTheme("kylin-network-10"));
    loadIcons.append(QIcon::fromTheme("kylin-network-11"));
    loadIcons.append(QIcon::fromTheme("kylin-network-12"));

    iconTimer = new QTimer(this);
    connect(iconTimer, SIGNAL(timeout()), this, SLOT(iconStep()));

    setTrayIcon(iconLanOnline);
}

void MainWindow::iconStep()
{
    if (currentIconIndex > 11) {
        currentIconIndex = 0;
    }
    setTrayIcon(loadIcons.at(currentIconIndex));
    currentIconIndex ++;
}

void MainWindow::setTrayIcon(QIcon icon)
{
    trayIcon->setIcon(icon);
}

void MainWindow::setTrayIconOfWifi(int signal){
    if (signal > 75) {
        setTrayIcon(iconWifiFull);
    } else if(signal > 55 && signal <= 75) {
        setTrayIcon(iconWifiHigh);
    } else if(signal > 35 && signal <= 55) {
        setTrayIcon(iconWifiMedium);
    } else if( signal <= 35) {
        setTrayIcon(iconWifiLow);
    }
}

void MainWindow::setTrayLoading(bool isLoading)
{
    if (isLoading) {
        currentIconIndex = 0;
        iconTimer->start(60);
    } else {
        iconTimer->stop();
    }
}

void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    if (!m_load_finished) {
        m_secondary_start_timer->stop();
        secondaryStart();
    }
    switch (reason) {
    case QSystemTrayIcon::Trigger:
//    case QSystemTrayIcon::MiddleClick:
        handleIconClicked();

        if (this->isHidden()) {
            this->showNormal();
            this->raise();
            this->activateWindow();
            if (is_btnLanList_clicked == 1&& is_stop_check_net_state==0) {
                onBtnNetListClicked(0);
            }

            if (!is_init_wifi_list && !is_connect_hide_wifi && is_stop_check_net_state==0) {
                is_stop_check_net_state = 1;
                qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
                if (is_btnWifiList_clicked == 1) {
                    BackThread *loop_bt = new BackThread();
                    IFace *loop_iface = loop_bt->execGetIface();

                    if (loop_iface->wstate != DEVICE_UNMANAGED) {
                        //current_wifi_list_state = UPDATE_WIFI_LIST;
                        //checkIfConnectedWifiExist();
                        //this->ksnm->execGetWifiList(); //更新wifi列表
                        this->on_btnWifiList_clicked(); //加载wifi列表
                    }

                    delete loop_iface;
                    loop_bt->deleteLater();
                }
                is_stop_check_net_state = 0;
                qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
            }
        } else {
            this->m_is_inputting_wifi_password = false;
            this->hide();
            numberForWifiScan = 0;
        }
        break;
    case QSystemTrayIcon::DoubleClick:
        //this->hide();
        break;
    case QSystemTrayIcon::Context:
        //右键点击托盘图标弹出菜单
//        showTrayIconMenu();
        break;
    default:
        break;
    }
}

void MainWindow::handleIconClicked()
{
#define MARGIN 4
    QDBusInterface iface("org.ukui.panel",
                         "/panel/position",
                         "org.ukui.panel",
                         QDBusConnection::sessionBus());
    QDBusReply<QVariantList> reply=iface.call("GetPrimaryScreenGeometry");
    //reply获取的参数共5个,分别是 主屏可用区域的起点x坐标,主屏可用区域的起点y坐标,主屏可用区域的宽度,主屏可用区域高度,任务栏位置
    //    reply.value();
    if (!iface.isValid() || !reply.isValid() || reply.value().size()<5) {
        qCritical() << QDBusConnection::sessionBus().lastError().message();
        this->setGeometry(0,0,this->width(),this->height());
        return;
    }

    qDebug()<<reply.value().at(4).toInt();
    QVariantList position_list=reply.value();

    switch(reply.value().at(4).toInt()){
    case 1:
        //任务栏位于上方
        this->setGeometry(position_list.at(0).toInt()+position_list.at(2).toInt()-this->width()-MARGIN,
                          position_list.at(1).toInt()+MARGIN,
                          this->width(),this->height());
        break;
        //任务栏位于左边
    case 2:
        this->setGeometry(position_list.at(0).toInt()+MARGIN,
                          position_list.at(1).toInt()+reply.value().at(3).toInt()-this->height()-MARGIN,
                          this->width(),this->height());
        break;
        //任务栏位于右边
    case 3:
        this->setGeometry(position_list.at(0).toInt()+position_list.at(2).toInt()-this->width()-MARGIN,
                          position_list.at(1).toInt()+reply.value().at(3).toInt()-this->height()-MARGIN,
                          this->width(),this->height());
        break;
        //任务栏位于下方
    default:
        this->setGeometry(position_list.at(0).toInt()+position_list.at(2).toInt()-this->width()-MARGIN,
                          position_list.at(1).toInt()+reply.value().at(3).toInt()-this->height()-MARGIN,
                          this->width(),this->height());
        break;
    }
}

void MainWindow::showTrayIconMenu()
{
    QRect availableGeometry = qApp->primaryScreen()->availableGeometry();
    QRect screenGeometry = qApp->primaryScreen()->geometry();

    QDesktopWidget* desktopWidget = QApplication::desktop();
    QRect screenMainRect = desktopWidget->screenGeometry(0);//获取设备屏幕大小
    QRect screenDupRect = desktopWidget->screenGeometry(1);//获取设备屏幕大小

    QPoint cursorPoint =  QCursor::pos();//返回相对显示器的全局坐标
    int cursor_x = cursorPoint.x();
    int cursor_y = cursorPoint.y();

    int n = objKyDBus->getTaskBarPos("position");
    int m = objKyDBus->getTaskBarHeight("height");
    int d = 0; //窗口边沿到任务栏距离
    int s = 80; //窗口边沿到屏幕边沿距离

    if (screenGeometry.width() == availableGeometry.width() && screenGeometry.height() == availableGeometry.height()) {
        if (n == 0) { //任务栏在下侧
            trayIconMenu->move(availableGeometry.x() + cursor_x - trayIconMenu->width()/2, screenMainRect.y() + availableGeometry.height() - trayIconMenu->height() - m - d);
        } else if(n == 1) { //任务栏在上侧
            trayIconMenu->move(availableGeometry.x() + cursor_x - trayIconMenu->width()/2, screenMainRect.y() + screenGeometry.height() - availableGeometry.height() + m + d);
        } else if (n == 2) { //任务栏在左侧
            trayIconMenu->move(m + d, cursor_y - trayIconMenu->height()/2);
        } else if (n == 3) { //任务栏在右侧
            trayIconMenu->move(screenMainRect.width() - trayIconMenu->width() - m - d, cursor_y - trayIconMenu->height()/2);
        }
    } else if(screenGeometry.width() == availableGeometry.width() ) {
        if (trayIcon->geometry().y() > availableGeometry.height()/2) { //任务栏在下侧
            trayIconMenu->move(availableGeometry.x() + cursor_x - trayIconMenu->width()/2, screenMainRect.y() + availableGeometry.height() - trayIconMenu->height() - d);
        } else { //任务栏在上侧
            trayIconMenu->move(availableGeometry.x() + cursor_x - trayIconMenu->width()/2, screenMainRect.y() + screenGeometry.height() - availableGeometry.height() + d);
        }
    } else if (screenGeometry.height() == availableGeometry.height()) {
        if (trayIcon->geometry().x() > availableGeometry.width()/2) { //任务栏在右侧
            trayIconMenu->move(availableGeometry.x() + availableGeometry.width() - trayIconMenu->width() - d, cursor_y - trayIconMenu->height()/2);
        } else { //任务栏在左侧
            trayIconMenu->move(screenGeometry.width() - availableGeometry.width() + d, cursor_y - trayIconMenu->height()/2);
        }
    }
}

void MainWindow::on_showWindowAction()
{
    if (!m_load_finished) {
        m_secondary_start_timer->stop();
        secondaryStart();
    }
    handleIconClicked();
    this->showNormal();
    this->raise();
    this->activateWindow();
}


///////////////////////////////////////////////////////////////////////////////
//加载动画,获取当前连接的网络和状态并设置图标

void MainWindow::startLoading()
{
    qDebug()<<"Start loading...";
    loading->startLoading();
    setTrayLoading(true);
}

void MainWindow::stopLoading()
{
    qDebug()<<"Stop loading!";
    loading->stopLoading();
    setTrayLoading(false);
    getActiveInfoAndSetTrayIcon();
}

void MainWindow::on_checkOverTime()
{
    QString cmd = "kill -9 $(pidof nmcli)"; //杀掉当前正在进行的有关nmcli命令的进程
    int status = system(cmd.toUtf8().data());
    if (status != 0) {
        qDebug()<<"execute 'kill -9 $(pidof nmcli)' in function 'on_checkOverTime' failed";
        //syslog(LOG_ERR, "execute 'kill -9 $(pidof nmcli)' in function 'on_checkOverTime' failed");
    }
    this->stopLoading(); //超时停止等待动画
    is_stop_check_net_state = 0;
    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
}

void MainWindow::getActiveInfoAndSetTrayIcon()
{
    // 获取网络的连接信息,是否有Lan连接,是否有WLAN连接,以及WLAN的信号强度
    QStringList connInfo = objKyDBus->getActiveWifiSignal();

    if (activeWifiSignalLv == 0) {
        if (connInfo.size() == 2) {
            if (connInfo.at(1).toInt() != 0) {
                activeWifiSignalLv = connInfo.at(1).toInt();
            }
        } else if (connInfo.size() == 3) {
            if (connInfo.at(2) != 0) {
                activeWifiSignalLv = connInfo.at(2).toInt();
            }
        }
    }

    // 设置图标
    if (connInfo.contains("802-3-ethernet")) {
        QList<QString> lanstate = objKyDBus->getAtiveLanSsidUuidState();
        //qDebug() << Q_FUNC_INFO << lanstate;
        if (lanstate.length() > 2 && lanstate[2] == "connected") {
            setTrayIconAfterGetConnectivity();
        } else if (lanstate.length() > 2 && lanstate[2] != "connected") {
            setTrayLoading(true);
        } else {
            qDebug()<<"lanstate length <= 2";
        }
    } else if (connInfo.contains("802-11-wireless") && currActWifiBssid != " ") {
        //currActWifiBssid是通过nmcli 命令行获取到连接wifi的bssid,如果为空格,则没有连接的wifi
        setTrayIconOfWifi(activeWifiSignalLv);
        emit this->actWifiSignalLvChanaged(activeWifiSignalLv);
        trayIcon->setToolTip(QString(tr("kylin-nm")));
    } else if (connInfo.contains("802-11-wireless") && currActWifiBssid == " ") {
        this->setTrayLoading(true);
        this->ksnm->execGetWifiList(this->wcardname);
    } else {
        setTrayIcon(iconLanOffline);
        trayIcon->setToolTip(QString(tr("kylin-nm")));
    }
}

void MainWindow::setTrayIconAfterGetConnectivity()
{
    int connectivity = objKyDBus->getNetworkConectivity();
    qDebug() << "Value of current network Connectivity property : "<< connectivity;
    switch (connectivity) {
    case UnknownConnectivity:
    case Portal:
    case Limited:
        setTrayIcon(iconLanOnlineNoInternet);
        trayIcon->setToolTip(QString(tr("Network Connected But Can Not Access Internet")));
        break;
    case NoConnectivity:
    case Full:
        setTrayIcon(iconLanOnline);
        trayIcon->setToolTip(QString(tr("kylin-nm")));
        break;
    }
}


///////////////////////////////////////////////////////////////////////////////
//网络设备管理

//网线插拔处理,由kylin-dbus-interface.cpp调用
void MainWindow::onPhysicalCarrierChanged(bool isCarrierLineOn)
{
    this->startLoading();
    if (isCarrierLineOn) {
        isHandlingWiredCableOn = true;
        is_stop_check_net_state = 1;
        qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
        qDebug()<<"wired physical cable is already plug in.";
        syslog(LOG_DEBUG,"wired physical cable is already plug in");
        wiredCableUpTimer->start(4000); //emit this signal to use function onCarrierUpHandle
        //onBtnLanClicked(4);
    } else {
        qDebug()<<"wired physical cable is already plug out";
        syslog(LOG_DEBUG,"wired physical cable is already plug out");
        QtConcurrent::run([=](){
            int count = 0;
            while (1) {
                if (count >= 3) {
                    onCarrierDownHandle();
                    break;
                }

                BackThread *bt = new BackThread();
                IFace *iface = bt->execGetIface();
                if (iface->lstate != DEVICE_CONNECTED) {
                    is_stop_check_net_state = 1;
                    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
                    sleep(2);
                    //wiredCableDownTimer->start(2000);
                    emit carrierDownHandle();
                    emit btnWired->clicked(5);
                    delete iface;
                    bt->deleteLater();
                    break;
                }
                delete iface;
                bt->deleteLater();
                sleep(2);
                count += 1;
            }
        });
    }
}

void MainWindow::onCarrierUpHandle()
{
    wiredCableUpTimer->stop();

    //检查有线网络的个数是否为0,如果是0,则新建一个有线网络
    checkIfWiredNetExist();

    this->stopLoading();
    onBtnNetListClicked(1);
    is_stop_check_net_state = 0;
    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
    isHandlingWiredCableOn = false;
    emit btnWired->clicked(4);
}

void MainWindow::onCarrierDownHandle()
{
    //syslog(LOG_DEBUG, "Wired net is disconnected");

    QString txt(tr("Wired net is disconnected"));
    objKyDBus->showDesktopNotify(txt);
    currSelNetName = "";
    oldActLanName = "";
    oldDbusActLanDNS = 0;
    //emit this->waitLanStop();
    //this->ksnm->execGetLanList();

    //wiredCableDownTimer->stop();
    this->stopLoading();
    onBtnNetListClicked(0);
    is_stop_check_net_state = 0;
    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
}

void MainWindow::onDeleteLan()
{
    deleteLanTimer->stop();
    BackThread *btn_bt = new BackThread();
    //写成信号监听自动执行,避免阻塞主线程
    disconnect_time = 1;
    connect (btn_bt, &BackThread::btFinish, this, [ = ](){
        disconnect_time ++;
        if (disconnect_time <= 3) {
            btn_bt->disConnLanOrWifi("ethernet");
        } else {
            btn_bt->disconnect();
        }
    });
    btn_bt->disConnLanOrWifi("ethernet");
    btn_bt->deleteLater();

    this->stopLoading();
    onBtnNetListClicked(0);
    is_stop_check_net_state = 0;
    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
}

void MainWindow::checkIfWiredNetExist()
{
    qDebug()<<"Checking Wired Net (num is 0?)...";
    if (objKyDBus->getWiredNetworkNumber() == 0) {
        objKyDBus->toCreateNewLan();
    }
}

//无线网卡插拔处理
void MainWindow::onNetworkDeviceAdded(QDBusObjectPath objPath)
{
    qDebug() <<"Network device added, path is: "<< objPath.path();

    IFace *iface = BackThread::execGetIface();
    wcardname = iface->wname;
    lcardname = iface->lname;
    confForm->lcard = lcardname;
    confForm->wcard = wcardname;
    delete iface;
    iface = nullptr;

    //仅处理无线网卡插入情况
    objKyDBus->isWirelessCardOn = false;
    objKyDBus->getObjectPath(); //更新网络设备路径
    if (objKyDBus->multiWirelessPaths.isEmpty()) {
        qDebug() << "No wifi card exist, return.";
        return;
    }

    if (objKyDBus->multiWirelessPaths.at(objKyDBus->multiWirelessPaths.size()-1).path() == objPath.path()) { //证明添加的是无线网卡
        is_wireless_adapter_ready = 0;
        if (objKyDBus->isWirelessCardOn) {
            syslog(LOG_DEBUG,"wireless device is already plug in");
            qDebug()<<"wireless device is already plug in";
            is_wireless_adapter_ready = 1;
            onBtnWifiClicked(4);
            objKyDBus->getWirelessCardName();
        }
    }
}

void MainWindow::onNetworkDeviceRemoved(QDBusObjectPath objPath)
{
    qDebug() <<"Network device removed, path is: "<< objPath.path();

    IFace *iface = BackThread::execGetIface();
    wcardname = iface->wname;
    lcardname = iface->lname;
    confForm->lcard = lcardname;
    confForm->wcard = wcardname;
    delete iface;
    iface = nullptr;

#if 0
    objKyDBus->getObjectPath(); //更新网络设备路径
    //仅处理无线网卡拔出情况
    if (objKyDBus->multiWirelessPaths.isEmpty()) {
        qDebug() << "No wifi card exist, return.";
        dbus_wifiList.clear();
        dbus_wifiList.append(QStringList("--"));
        is_wireless_adapter_ready = 0;
        onBtnWifiClicked(5);
        emit this->getWifiListFinished();
        return;
    }
#endif

    for (int i = 0; i < objKyDBus->multiWirelessPaths.size(); ++i) {
        if (objKyDBus->multiWirelessPaths.at(i).path() == objPath.path()) {
            objKyDBus->isWirelessCardOn = false;
            objKyDBus->getObjectPath(); //检查是不是还有无线网卡
            if (!objKyDBus->isWirelessCardOn) {
                syslog(LOG_DEBUG,"wireless device is already plug out");
                qDebug()<<"wireless device is already plug out";
                dbus_wifiList.clear();
                dbus_wifiList.append(QStringList("--"));
                is_wireless_adapter_ready = 0;
                onBtnWifiClicked(5);
                emit this->getWifiListFinished();
            } else {
                objKyDBus->getWirelessCardName();
                syslog(LOG_DEBUG,"wireless device is already plug out, but one more wireless exist");
                qDebug()<<"wireless device is already plug out, but one more wireless exist";
            }
        }
    }

}

void MainWindow::checkIsWirelessDevicePluggedIn()
{
    qDebug()<<"Checking wireless device...";
    //启动时判断是否有无线网卡
    //KylinDBus kDBus3;
    if (objKyDBus->isWirelessCardOn) {
        is_wireless_adapter_ready = 1;
    } else {
        is_wireless_adapter_ready = 0;
    }
}

void MainWindow::getLanBandWidth()
{
    BackThread *bt = new BackThread();
    IFace *iface = bt->execGetIface();

    lcardname = iface->lname;

//    mwBandWidth = bt->execChkLanWidth(lcardname);
}

//检测网络设备状态
bool MainWindow::checkLanOn()
{
    BackThread *bt = new BackThread();
    IFace *iface = bt->execGetIface();
    bt->deleteLater();
    int state = iface->lstate;
    if (state == 2) {
        return false;
    } else {
        return true;
    }
}

bool MainWindow::checkWlOn()
{
    BackThread *bt = new BackThread();
    IFace *iface = bt->execGetIface();
    bt->deleteLater();
    int state = iface->wstate;
    if (state == 2) {
        return false;
    } else {
        return true;
    }
}


///////////////////////////////////////////////////////////////////////////////
//有线网与无线网按钮响应

void MainWindow::onBtnNetClicked()
{
    if (checkLanOn()) {
        QThread *t = new QThread();
        BackThread *bt = new BackThread();
        bt->moveToThread(t);
        connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
        connect(t, SIGNAL(started()), bt, SLOT(execDisNet()));
        connect(bt, SIGNAL(disNetDone()), this, SLOT(disNetDone()));
        connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));
        t->start();

    } else {
        is_stop_check_net_state = 1;
        qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
        QThread *t = new QThread();
        BackThread *bt = new BackThread();
        bt->moveToThread(t);
        connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
        connect(t, SIGNAL(started()), bt, SLOT(execEnNet()));
        connect(bt, SIGNAL(enNetDone()), this, SLOT(enNetDone()));
        connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));
        t->start();
    }

    this->startLoading();
}

void MainWindow::onBtnWifiClicked(int flag)
{
    qDebug()<<"Value of flag passed into function 'onBtnWifiClicked' is:  "<<flag;
    //syslog(LOG_DEBUG, "Value of flag passed into function 'onBtnWifiClicked' is: %d", flag);
    this->m_is_inputting_wifi_password = false; //wifi密码输入框一定会被收起

    if (is_wireless_adapter_ready == 1) {
        // flag: 0->UI点击关闭 1->UI点击打开 2->gsetting打开 3->gsetting关闭 4->网卡热插 5->网卡热拔
        // 当连接上无线网卡时才能打开wifi开关
        // 网络开关关闭时,点击Wifi开关时,程序先打开有线开关
        if (flag == 0 || flag == 1 || flag == 4 || flag == 5) {
            if (checkWlOn()) {
                if (flag != 4) { //以防第二张无线网卡插入时断网
                    is_stop_check_net_state = 1;
                    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
                    objKyDBus->setWifiSwitchState(false);
                    lbTopWifiList->hide();
                    btnAddNet->hide();

                    QThread *t = new QThread();
                    BackThread *bt = new BackThread();
                    bt->moveToThread(t);
                    objKyDBus->oldWifiSwitchState = true;
                    btnWireless->setSwitchStatus(true);
                    connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
                    connect(t, SIGNAL(started()), bt, SLOT(execDisWifi()));
                    connect(bt, SIGNAL(disWifiDone()), this, SLOT(disWifiDone()));
                    connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));
                    t->start();
                    this->startLoading();
                }
            } else {
                if (is_fly_mode_on == 0) {
                    //on_btnWifiList_clicked();
                    is_stop_check_net_state = 1;
                    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
                    objKyDBus->setWifiCardState(true);
                    objKyDBus->setWifiSwitchState(true);

                    QThread *t = new QThread();
                    BackThread *bt = new BackThread();
                    bt->moveToThread(t);
                    objKyDBus->oldWifiSwitchState = true;
                    btnWireless->setSwitchStatus(true);
                    connect(t, &QThread::finished, t, &QThread::deleteLater);
                    connect(t, &QThread::started, bt, &BackThread::execEnWifi);
                    connect(bt, &BackThread::enWifiDone, this, &MainWindow::enWifiDone);
                    connect(bt, &BackThread::btFinish, t, &QThread::quit);
                    t->start();
                    this->startLoading();
                }
            }
        } else if(flag == 2) {
            if (is_fly_mode_on == 0) {
                //on_btnWifiList_clicked();
                is_stop_check_net_state = 1;
                qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
                lbTopWifiList->show();
                btnAddNet->show();

                QThread *t = new QThread();
                BackThread *bt = new BackThread();
                objKyDBus->oldWifiSwitchState = true;
                btnWireless->setSwitchStatus(true);
                bt->moveToThread(t);
                connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
                connect(t, SIGNAL(started()), bt, SLOT(execEnWifi()));
                connect(bt, SIGNAL(enWifiDone()), this, SLOT(enWifiDone()));
                connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));
                t->start();
                this->startLoading();
            }
        } else if(flag == 3) {
            is_stop_check_net_state = 1;
            qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
            lbTopWifiList->hide();
            btnAddNet->hide();

            QThread *t = new QThread();
            BackThread *bt = new BackThread();
            objKyDBus->oldWifiSwitchState = true;
            btnWireless->setSwitchStatus(true);
            bt->moveToThread(t);
            connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
            connect(t, SIGNAL(started()), bt, SLOT(execDisWifi()));
            connect(bt, SIGNAL(disWifiDone()), this, SLOT(disWifiDone()));
            connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));
            t->start();
            this->startLoading();
        } else {
            qDebug()<<"receive an invalid value in function onBtnWifiClicked";
            //syslog(LOG_DEBUG, "receive an invalid value in function onBtnWifiClicked");
        }
    } else {
        lbTopWifiList->hide();
        btnAddNet->hide();

        if (flag == 0) {
             objKyDBus->setWifiSwitchState(false);
             objKyDBus->setWifiCardState(false);
        }

        QString txt(tr("No wireless card detected")); //未检测到无线网卡
        objKyDBus->showDesktopNotify(txt);
        qDebug()<<"No wireless card detected";
        //syslog(LOG_DEBUG, "No wireless card detected");

        disWifiStateKeep();
    }
}

/**
 * @brief MainWindow::onBtnLanClicked 有线网按钮状态更改
 * @param flag falg=0 0 UI关闭, 1 UI打开 ,2收到打开信息,3收到关闭信息,4有线设备插入,5有线设备拔出
 */
void MainWindow::onBtnLanClicked(int flag)
{
    switch (flag) {
    case 0: {
        qDebug()<<"On btnWired clicked! will close switch button";
        emit this->onWiredDeviceChanged(false);
        BackThread::saveSwitchButtonState(LAN_SWITCH_OPENED, false);
        this->startLoading();
        //记录已连接的lan
        BackThread::clearWiredConnectUuid();
        foreach (QString ifaceName, objKyDBus->multiWiredIfName) {
            QString uuid = objKyDBus->getConnLanNameByIfname(ifaceName);
            if(uuid != "--") {
                qDebug() << "save iface " << ifaceName << " uuid " << uuid;
                BackThread::saveWiredConnectUuid(ifaceName, uuid);
            }
        }
        objKyDBus->disConnectWiredConnect();
        QtConcurrent::run([=]() {
            foreach (QString lcard, BackThread::execGetIface()->lcards) {
                QString close_device_cmd = "nmcli device set " + lcard + " managed false";
                int res = system(close_device_cmd.toUtf8().data());
                qDebug()<<"Trying to close ethernet device : "<<lcard<<". res="<<res;
            }
            this->ksnm->execGetLanList();
        });
        break;
    }
    case 1: {
        objKyDBus->getPhysicalCarrierState();
        if (!objKyDBus->isWiredCableOn) {
            qWarning()<<"No ethernet device available!";
            QString txt(tr("No ethernet device available"));
            objKyDBus->showDesktopNotify(txt);
            return;
        }
        qDebug()<<"On btnWired clicked! will open switch button";
        emit this->onWiredDeviceChanged(true);
        BackThread::saveSwitchButtonState(LAN_SWITCH_OPENED, true);
        this->startLoading();
        QtConcurrent::run([=]() {
            foreach (QString lcard, BackThread::execGetIface()->lcards) {
                QString open_device_cmd = "nmcli device set " + lcard + " managed true";
                int res = system(open_device_cmd.toUtf8().data());
                qDebug()<<"Trying to open ethernet device : "<<lcard<<". res="<<res;
            }
            //重连开关关闭前已连接的有线网络
            objKyDBus->getWiredCardName();
            foreach (QString ifaceName, objKyDBus->multiWiredIfName) {
                qDebug() << "check " << ifaceName;
                QVariant uuid = BackThread::getWiredConnectUuid(ifaceName);
                if(uuid.isValid()) {
                    qDebug() << "start to reconnect iface " << ifaceName << " uuid " << uuid;
                    objKyDBus->toConnectWiredNet(uuid.toString(), ifaceName);
                }
            }
            BackThread::clearWiredConnectUuid();
            QTimer::singleShot(0.5 * 1000, this, [ = ]() {
                //防止卡顿,延时一小段时间后再获取列表
                this->ksnm->execGetLanList();
            });
        });
        break;
    }
    case 2: {
        emit this->onWiredDeviceChanged(true);
        break;
    }
    case 3: {
        emit this->onWiredDeviceChanged(false);
        break;
    }
    case 4: {
        qDebug()<<"Wired device plug in!";
//        btnWired->setEnabled(true);
//        qDebug()<<"Set btnwired enabled=true!";
        //获取上次设备拔出前的有线开关状态,以判断是否需要打开开关
//        if (BackThread::execGetIface()->lstate != 2) {
//            emit this->onWiredDeviceChanged(true);
//        }
        objKyDBus->getPhysicalCarrierState();
        if (objKyDBus->isWiredCableOn) {
            QVariant lan_state = BackThread::getSwitchState(LAN_SWITCH_OPENED);
            if (!lan_state.isNull() && lan_state.isValid() && lan_state.toBool() && !btnWired->getSwitchStatus()) {
                QString open_device_cmd = "nmcli device set " + lcardname + " managed true";
                int res = system(open_device_cmd.toUtf8().data());
                qDebug()<<"Trying to open ethernet device : "<<lcardname<<". res="<<res;
                if (res == 0) {
                    emit this->onWiredDeviceChanged(true);
                } else {
                    qWarning()<<"Open ethernet device failed!";
                }
            }
        }
        break;
    }
    case 5: {
        qDebug()<<"Wired device plug out!";
        IFace *iface = BackThread::execGetIface();
        if (iface && !iface->lmanaged) {
            emit this->onWiredDeviceChanged(false);
        }
        break;
    }
    default:
        break;
    }
}

void MainWindow::setLanSwitchStatus(bool is_opened)
{
    isLanSwitchOpend = is_opened;
    btnWired->blockSignals(true);
    btnWired->setSwitchStatus(is_opened);
    btnWired->blockSignals(false);
    if (is_opened) {
        currSelNetName = "";
    }
//    QTimer::singleShot(100, this, [=](){
//        //加一点点延时再刷新列表,避免刚刚触发设备开关时刷新列表调用的dbus卡住
//        ksnm->execGetLanList();
//    });
}

void MainWindow::onBtnNetListClicked(int flag)
{
    this->is_btnLanList_clicked = 1;
    this->is_btnWifiList_clicked = 0;
    end_rcv_rates = 0;
    end_tx_rates = 0;

    ui->lbNetListBG->setStyleSheet(btnOnQss);
    ui->lbWifiListBG->setStyleSheet(btnOffQss);

    BackThread *bt = new BackThread();
    IFace *iface = bt->execGetIface();

    lbLoadDown->hide();
    lbLoadUp->hide();
    lbLoadDownImg->hide();
    lbLoadUpImg->hide();

    lbNoItemTip->hide();

    ui->lbNetwork->setText(tr("Ethernet"));
    btnWireless->hide();
    btnWired->show();

    // 强行设置为打开
    if (flag == 1) {
        this->startLoading();
        this->ksnm->execGetLanList();
        this->scrollAreal->show();
        this->topLanListWidget->show();
        this->scrollAreaw->hide();
        this->topWifiListWidget->hide();
        return;
    }

//    if (iface->lstate == DEVICE_CONNECTED || iface->lstate == DEVICE_DISCONNECTED ||  iface->lstate == DEVICE_CONNECTING) {
//        this->startLoading();
//        this->ksnm->execGetLanList();
//    } else {
//        this->ksnm->isUseOldLanSlist = true;
//        QStringList slistLan;
//        slistLan.append("empty");
//        getLanListDone(slistLan);
//    }
    //不管有没有打开有线设备,有线列表都应该刷新
    this->startLoading();
    this->ksnm->execGetLanList();

    this->scrollAreal->show();
    this->topLanListWidget->show();
    this->scrollAreaw->hide();
    this->topWifiListWidget->hide();

    delete iface;
    bt->deleteLater();
}

void MainWindow::on_btnWifiList_clicked()
{
    m_is_inputting_wifi_password = false;
    is_stop_check_net_state = 1;
    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
    current_wifi_list_state = LOAD_WIFI_LIST;
    this->is_btnWifiList_clicked = 1;
    this->is_btnLanList_clicked = 0;
    end_rcv_rates = 0;
    end_tx_rates = 0;

    if (this->is_btnLanList_clicked == 1) {
        return;
    }

    BackThread *bt = new BackThread();
    IFace *iface = bt->execGetIface();
    if (iface->wstate == DEVICE_CONNECTED) {
        hasWifiConnected = true;
    } else {
        lbLoadDown->hide();
        lbLoadUp->hide();
        lbLoadDownImg->hide();
        lbLoadUpImg->hide();
        hasWifiConnected = false;
    }

    ui->lbNetListBG->setStyleSheet(btnOffQss);
    ui->lbWifiListBG->setStyleSheet(btnOnQss);

    lbNoItemTip->hide();

    ui->lbNetwork->setText(tr("WLAN"));
    btnWireless->show();
    btnWired->hide();

    if (iface->wstate == DEVICE_CONNECTED || iface->wstate == DEVICE_DISCONNECTED) {
        qDebug()<<"现在的WiFi的开关是已经打开状态";
        objKyDBus->oldWifiSwitchState = true;
        btnWireless->setSwitchStatus(true);
        lbTopWifiList->show();
        btnAddNet->show();
        this->startLoading();
        if (isHuaWeiPC) {
            QtConcurrent::run([=]() {
                if (m_connected_by_self) {
                    m_connected_by_self = false;
                } else {
                    if (this->isRadioWifiTurningOn) {
                        qDebug() << "Turning wifi switch on now, stop to load wifi list";
                    } else {
                        bShowPb = false;  //不可以showpb
                        objKyDBus->requestScanWifi(); //要求后台扫描AP
                        sleep(2);
                        qDebug() << "scan finished, will load wifi list";
                        emit loadWifiListAfterScan();
                    }
                }
            });
        } else {
            //this->objKyDBus->toGetWifiList(); //这一句是使用dbus的方法获取wifilist
            this->ksnm->execGetWifiList(this->wcardname);
        }
    } else if (iface->wstate == DEVICE_CONNECTING) {
        qDebug()<<"现在的WiFi的开关是正在配置状态";

        this->ksnm->isUseOldWifiSlist = true;
        QStringList slistWifi;
        slistWifi.append("empty");
        getWifiListDone(slistWifi);

        objKyDBus->oldWifiSwitchState = true;
        btnWireless->setSwitchStatus(true);
        lbTopWifiList->show();
        btnAddNet->show();
        is_stop_check_net_state = 0;
        qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
    } else {
        qDebug()<<"现在WiFi的开关是关闭状态";
        objKyDBus->oldWifiSwitchState = false;
        btnWireless->setSwitchStatus(false);
        if (topWifiListWidget) { delete topWifiListWidget; }//清空top列表
        createTopWifiUI(); //创建顶部无线网item
        lbTopWifiList->hide();
        btnAddNet->hide();

        // 清空wifi列表
        wifiListWidget = new QWidget(scrollAreaw);
        wifiListWidget->resize(W_LIST_WIDGET, H_WIFI_ITEM_BIG_EXTEND);
        scrollAreaw->setWidget(wifiListWidget);
        scrollAreaw->move(W_LEFT_AREA, Y_SCROLL_AREA);

        // 当前连接的wifi
        OneConnForm *ccf = new OneConnForm(topWifiListWidget, this, confForm, ksnm);
        ccf->setWifiName(tr("Not connected"), "--", "--", "--", isHuaWeiPC, isHuaWei9006C);//"当前未连接任何 Wifi"
        ccf->setSignal("0", "--", "0", false);
//        ccf->setRate("0");
        ccf->setConnedString(1, tr("Disconnected"), "");//"未连接"
        ccf->isConnected = false;
        ccf->setTopItem(false);
        ccf->setAct(true);
        ccf->move(L_VERTICAL_LINE_TO_ITEM, 0);
        ccf->show();
        ccf->lbFreq->hide();

        this->lanListWidget->hide();
        this->wifiListWidget->show();

        getActiveInfoAndSetTrayIcon();
        is_stop_check_net_state = 0;
        qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
    }

    this->scrollAreal->hide();
    this->topLanListWidget->hide();
    this->scrollAreaw->show();
    this->topWifiListWidget->show();

    delete iface;
    bt->deleteLater();
}

void MainWindow::onLoadWifiListAfterScan()
{
    current_wifi_list_state = LOAD_WIFI_LIST;
    this->ksnm->execGetWifiList(this->wcardname); //加载wifi列表
    objKyDBus->getWirelessCardName();
}

void MainWindow::on_wifi_changed()
{
    QString actWifiUuid = objKyDBus->getActiveWifiUuid();
    objKyDBus->getConnectNetIp(actWifiUuid);
    objKyDBus->getWifiIp(actWifiUuid);
    confForm->actWifiIpv6Addr = objKyDBus->dbusActiveWifiIpv6;
    if (oldWifiIpv4Method == "") {
        oldWifiIpv4Method = objKyDBus-> dbusWifiIpv4Method;
    }
    if (objKyDBus->dbusWifiIpv4 != "" && objKyDBus->dbusActiveWifiIpv4 != "" && objKyDBus->dbusWifiIpv4 != objKyDBus->dbusActiveWifiIpv4 &&objKyDBus-> dbusWifiIpv4Method == "manual") {
        //在第三方nm-connection-editor进行新的IP配置后,重新连接网络
        oldWifiIpv4Method = "manual";
        qDebug()<<"Ipv4.address of current activated wifi is:"<<objKyDBus->dbusActiveWifiIpv4 << ". Real ipv4.address is:" << objKyDBus->dbusWifiIpv4;
        emit this->reConnectWifi(actWifiUuid);
        emit this->configurationChanged();
    } else if (objKyDBus-> dbusWifiIpv4Method == "auto" && oldWifiIpv4Method == "manual") {
        oldWifiIpv4Method = "auto";
        qDebug()<<"Ipv4.method is set to auto.";
        emit this->reConnectWifi(actWifiUuid);
        emit this->configurationChanged();
    } else if (!objKyDBus->dbusActiveWifiIpv6.isEmpty() && objKyDBus->dbusActiveWifiIpv6 != objKyDBus->dbusWifiIpv6 && objKyDBus->dbusWifiIpv6Method == "manual") {
        //在第三方nm-connection-editor或kylin-nm配置页进行新的IPV6配置后,重新连接网络
        emit this->reConnectWifi(actWifiUuid);
        emit this->configurationChanged();
    }
}

/**
 * @brief MainWindow::onNewConnAdded 获取新的连接列表
 * @param type 0为有线,1为无线
 */
void MainWindow::onNewConnAdded(int type) {
    if (type == 1) {
        isAddedWifi = true;
    }
    this->ksnm->execGetConnList();
}

///////////////////////////////////////////////////////////////////////////////
//网络列表加载与更新

// 获取lan列表回调
void MainWindow::getLanListDone(QStringList slist)
{
    if (this->is_btnWifiList_clicked == 1) {
        return;
    }

    //要求使用上一次获取到的列表
    if (this->ksnm->isUseOldLanSlist) {
        slist = oldLanSlist;
        this->ksnm->isUseOldLanSlist = false;
    }

    //若slist为空,则也使用上一次获取到的列表
    if (slist.size() == 1 && slist.at(0) == "") {
        if (oldLanSlist.size() == 1 && oldLanSlist.at(0) == "") {
            return;
        } else {
            slist = oldLanSlist;
        }
    }

    delete topLanListWidget; // 清空top列表
    createTopLanUI(); //创建顶部有线网item

    // 清空lan列表
    lanListWidget = new QWidget(scrollAreal);
    lanListWidget->resize(W_LIST_WIDGET, H_NORMAL_ITEM + H_LAN_ITEM_EXTEND);
    scrollAreal->setWidget(lanListWidget);
    scrollAreal->move(W_LEFT_AREA, Y_SCROLL_AREA);

    // 获取当前连接有线网的SSID和UUID
    QList<QString> actLanSsidName;
    QList<QString> actLanUuidName;
    QList<QString> actLanStateName;
    QList<QString> connectingLanSsidName;
    QList<QString> connectingLanUuidName;
    QList<QString> connectingLanStateName;
    QList<QString> currConnLanSsidUuidState =objKyDBus->getAtiveLanSsidUuidState();

    bool hasCurrentLanConnected = false;
    if (currConnLanSsidUuidState.contains("connected")) {
        hasCurrentLanConnected = true;
    }

    // 若当前没有任何一个有线网连接,设置有线列表顶部的item状态为未连接
    if (!hasCurrentLanConnected) {
        OneLancForm *ccf = new OneLancForm(topLanListWidget, this, confForm, ksnm);
        ccf->setLanName(tr("Not connected"), tr("Not connected"), "--", "--");//"当前未连接任何 以太网"
        ccf->setIcon(false);
        ccf->setConnedString(1, tr("Disconnected"), "");//"未连接"
        ccf->isConnected = false;
        ifLanConnected = false;
        lbLoadDown->hide();
        lbLoadUp->hide();
        lbLoadDownImg->hide();
        lbLoadUpImg->hide();
        ccf->setTopItem(false);//"当前未连接任何 以太网"
        ccf->setAct(true);
        ccf->move(L_VERTICAL_LINE_TO_ITEM, 0);
        ccf->show();
        ccf->setLine(false);
        currTopLanItem = 1;
    }
    if (currConnLanSsidUuidState.size() != 0) {
        int i = 0;
        while((i + 2) < currConnLanSsidUuidState.size()) {
            if (currConnLanSsidUuidState.at(i+2) == "connecting") {
                connectingLanSsidName.append(currConnLanSsidUuidState.at(i)); //有线网络名称
                connectingLanUuidName.append(currConnLanSsidUuidState.at(i+1)); //有线网络唯一ID
                connectingLanStateName.append(currConnLanSsidUuidState.at(i+2)); //有线网络连接状态
            } else {
                actLanSsidName.append(currConnLanSsidUuidState.at(i)); //有线网络名称
                actLanUuidName.append(currConnLanSsidUuidState.at(i+1)); //有线网络唯一ID
                actLanStateName.append(currConnLanSsidUuidState.at(i+2)); //有线网络连接状态
            }
            i += 3;
        }
        currTopLanItem = actLanSsidName.size();
    }

    // 填充可用网络列表
    QString headLine = slist.at(0);
    int indexUuid, indexName, indexDevice;
    headLine = headLine.trimmed();

    bool isChineseExist = headLine.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
    if (isChineseExist) {
        indexUuid = headLine.indexOf("UUID") + 2;
        indexDevice = headLine.indexOf("设备") + 2;
        indexName = headLine.indexOf("名称") + 2;
    } else {
        indexUuid = headLine.indexOf("UUID");
        indexDevice = headLine.indexOf("DEVICE");
        indexName = headLine.indexOf("NAME");
    }
    for(int i = 1, j = 0; i < slist.size(); i ++) {
        QString line = slist.at(i);
        if(!line.size()){
            continue;
        }
        QString ltype = line.mid(0, indexUuid).trimmed();
        QString nuuid = line.mid(indexUuid, indexDevice - indexUuid).trimmed();
        QString ndevice = line.mid(indexDevice,indexName-indexDevice).trimmed();
        QString nname = line.mid(indexName);
        while(nname.endsWith(' ')) {
            nname.chop(1);
        }

        if (nname=="") {
            nname = " "; //防止有线网络的名称为空
        }
        bool isActiveNet = false; //isActiveNet用来表明nname是否是活动的连接

        //仅仅对有线网络进行添加列表处理
//        if (ltype != "802-11-wireless" && ltype != "wifi" && ltype != "bridge" && ltype != "bluetooth" && ltype != "" && ltype != "--") {
        if (ltype == "802-3-ethernet" || ltype == "ethernet" || ltype == "vpn") {
            objKyDBus->getLanIpDNS(nuuid, true); //使用UUID获取有线网的ip和dns信息
            QString macLan = getMacByUuid(nuuid); //有线网对应的mac地址

            QString macInterface = "--";
            QString mIfName = ndevice;
            macInterface = objKyDBus->getLanMAC(mIfName);
            if (macLan!="" && macLan!="--" && macLan != macInterface) {
                //continue; //有线网的permenant mac地址与网卡的地址不同,则不在列表中显示
                macInterface = macLan;
            }

            //**********************创建已经连接的有线网item********************//
            if (currConnLanSsidUuidState.size() != 0) {//证明有已经连接的有线网络
                for (int kk=0; kk<actLanSsidName.size(); kk++) {
                    //actLanSsidName.at(kk).contains(nname)是为了防止名称中部分空格被trimmed()删除导致名称无法对应,witch caused 显示错误
                    if ((nname == actLanSsidName.at(kk) || actLanSsidName.at(kk).contains(nname)) && nuuid == actLanUuidName.at(kk) && actLanStateName.at(kk) == "connected") {
                        topLanListWidget->resize(topLanListWidget->width(), topLanListWidget->height() + H_NORMAL_ITEM*kk);
                        isActiveNet = true; //名为nname的网络是已经连接的有线网络
                        ifLanConnected = true;

                        objKyDBus->getConnectNetIp(nuuid);
                        confForm->actLanIpv6Addr = objKyDBus->dbusActiveLanIpv6;
                        getLanBandWidth();
                        //QString strLanName = TranslateLanName(nname); //进行中英文系统环境下有线网络名称的汉化

                        OneLancForm *ccfAct = new OneLancForm(topLanListWidget, this, confForm, ksnm);
                        connect(ccfAct, SIGNAL(selectedOneLanForm(QString, QString)), this, SLOT(oneTopLanFormSelected(QString, QString)));
                        connect(ccfAct, SIGNAL(requestHandleLanDisconn()), this, SLOT(handleLanDisconn()));
                        ccfAct->setLanName(nname, ltype, nuuid, mIfName);//第二个参数本来是strLanName,但目前不需要翻译
                        ccfAct->setIcon(true);
//                        BackThread *bt = new BackThread();
//                        mwBandWidth = bt->execChkLanWidth(mIfName);
//                        delete bt;
                        QString bandWidth = BackThread::execChkLanWidth(mIfName);
                        ccfAct->setLanInfo(objKyDBus->dbusActiveLanIpv4, objKyDBus->dbusActiveLanIpv6, bandWidth, macInterface);
                        ccfAct->isConnected = true;
                        ccfAct->setTopItem(false);
                        ccfAct->setAct(true);
                        ccfAct->move(L_VERTICAL_LINE_TO_ITEM, kk*H_NORMAL_ITEM);
                        ccfAct->show();

                        if (actLanSsidName.size() == 1) {
                            lbLoadDown->show();
                            lbLoadUp->show();
                            lbLoadDownImg->show();
                            lbLoadUpImg->show();
                            currConnIfname = mIfName;
                            ccfAct->setConnedString(1, tr("NetOn"), "");//"已连接"
                        } else {
                            lbLoadDown->hide();
                            lbLoadUp->hide();
                            lbLoadDownImg->hide();
                            lbLoadUpImg->hide();
                            ccfAct->setConnedString(1, tr("NetOn,IfName:"), mIfName);//"已连接"
                        }

                        if (kk != actLanSsidName.size() - 1) {
                            ccfAct->setLine(true);
                        } else {
                            ccfAct->setLine(false); //最后一个item不显示下划线
                        }
                        if (!objKyDBus->dbusLanIpv4.isEmpty()) {
                            if (!objKyDBus->dbusActiveLanIpv4.isEmpty() && objKyDBus->dbusActiveLanIpv4 != objKyDBus->dbusLanIpv4) {
//                                qDebug() << Q_FUNC_INFO << __LINE__ << objKyDBus->dbusActiveLanIpv4 << objKyDBus->dbusLanIpv4;
                                //在第三方nm-connection-editor进行新的IP配置后,重新连接网络
                                objKyDBus->reConnectWiredNet(nuuid);
                                emit this->configurationChanged();
                            } else if ((oldActLanName == actLanSsidName.at(kk)) && (oldDbusActLanDNS != objKyDBus->dbusActLanDNS)) {
                                //在第三方nm-connection-editor进行新的DNS配置后,重新连接网络
                                objKyDBus->reConnectWiredNet(nuuid);
                                emit this->configurationChanged();
                            } else if (!objKyDBus->dbusActiveLanIpv6.isEmpty() && objKyDBus->dbusActiveLanIpv6 != objKyDBus->dbusLanIpv6 && objKyDBus->dbusLanIpv6Method == "manual") {
//                                qDebug() << Q_FUNC_INFO << __LINE__ << objKyDBus->dbusActiveLanIpv6 << objKyDBus->dbusLanIpv6 <<  objKyDBus->dbusLanIpv6Method;
                                //在第三方nm-connection-editor或kylin-nm配置页进行新的IPV6配置后,重新连接网络
                                objKyDBus->reConnectWiredNet(nuuid);
                                emit this->configurationChanged();
                            }
                            actLanUuid = nuuid;
                            actLanIpv4Method = "manual";
                        } else {
                            //已连接WiFi未改变但IP获取方式改变,重连之
                            if (actLanUuid == nuuid && actLanIpv4Method == "manual") {
                                objKyDBus->reConnectWiredNet(nuuid);
                                emit this->configurationChanged();
                            }
                            actLanUuid = nuuid;
                            actLanIpv4Method = "auto";
                        }

                        currSelNetName = "";
                        objKyDBus->dbusActiveLanIpv4 = "";
                        objKyDBus->dbusActiveLanIpv6 = "";
                        oldActLanName = actLanSsidName.at(kk);
                        oldDbusActLanDNS = objKyDBus->dbusActLanDNS;
                        int topLanNum = (currTopLanItem >= 1) ? currTopLanItem : 1;
                        lbTopLanList->move(X_MIDDLE_WORD, H_NORMAL_ITEM * topLanNum + H_GAP_UP);
                        btnCreateNet->move(X_BTN_FUN, Y_BTN_FUN + H_NORMAL_ITEM * (topLanNum-1));
                        scrollAreal->move(W_LEFT_AREA, Y_SCROLL_AREA + H_NORMAL_ITEM * (topLanNum-1));
                        qDebug()<<"already insert an active lan network item in the top of lan list";
                        //syslog(LOG_DEBUG, "already insert an active lan network item in the top of lan list");
                    }
                }
            }
            lbNoItemTip->move(this->width()/2 - W_NO_ITEM_TIP/2 + W_LEFT_AREA/2, this->height()/2 + H_NORMAL_ITEM*(currTopLanItem-1)/2);

            //**********************创建未连接的有线网item********************//
            if (!isActiveNet && btnWired->getSwitchStatus()) {
                lanListWidget->resize(W_LIST_WIDGET, lanListWidget->height() + H_NORMAL_ITEM);
                //QString strLanName = TranslateLanName(nname);

                OneLancForm *ocf = new OneLancForm(lanListWidget, this, confForm, ksnm);
                connect(ocf, SIGNAL(selectedOneLanForm(QString, QString)), this, SLOT(oneLanFormSelected(QString, QString)));
                ocf->setLanName(nname, ltype, nuuid, mIfName);
                ocf->setIcon(true);
                ocf->setLine(true);
                ocf->setLanInfo(objKyDBus->dbusLanIpv4, objKyDBus->dbusLanIpv6, tr("Disconnected"), macInterface);
                ocf->setConnedString(0, tr("Disconnected"), "");//"未连接"
                ocf->move(L_VERTICAL_LINE_TO_ITEM, j * H_NORMAL_ITEM);
                ocf->setSelected(false, false);
                ocf->show();

//                for (int kk=0; kk<actLanSsidName.size(); kk++) {
//                    if (nname == actLanSsidName.at(kk) && nuuid == actLanUuidName.at(kk) &&  actLanStateName.at(kk) == "connecting") {
//                            ocf->startWaiting(true);
//                    }
//                }
                if (connectingLanSsidName.contains(nname) && connectingLanUuidName.contains(nuuid)) {
                    ocf->startWaiting(true);
                }
                j ++;
            }
        }
    }

    QList<OneLancForm *> itemList = lanListWidget->findChildren<OneLancForm *>();
    int n = itemList.size();
    if (n >= 1) {
        OneLancForm *lastItem = itemList.at(n-1);
        lastItem->setLine(false); //最后一个item不显示下划线
        lbNoItemTip->hide();
    } else {
        if (btnWired->getSwitchStatus()) {
            lbTopLanList->show();
            btnCreateNet->show();
            lbNoItemTip->show();
            lbNoItemTip->setText(tr("No Other Wired Network Scheme"));
        } else {
            lbTopLanList->hide();
            btnCreateNet->hide();
            lbNoItemTip->hide();
        }
    }
    currSelNetName = "";

    this->lanListWidget->show();
    this->topLanListWidget->show();
    this->wifiListWidget->hide();
    this->topWifiListWidget->hide();

    this->stopLoading();
    oldLanSlist = slist;
    is_stop_check_net_state = 0;
    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
    //有线网按钮状态校准
    IFace *iface = BackThread::execGetIface();
    if (iface && !iface->lmanaged) {
        btnWired->blockSignals(true);
        btnWired->setSwitchStatus(false);
        btnWired->blockSignals(false);
    } else {
        btnWired->blockSignals(true);
        btnWired->setSwitchStatus(true);
        btnWired->blockSignals(false);
    }
    if (iface)
        delete iface;
}

// 获取wifi列表回调
void MainWindow::onRequestRevalueUpdateWifi()
{
    if (!isReConnAfterTurnOnWifi) {
        isReConnAfterTurnOnWifi = false;
        is_stop_check_net_state = 1;
        qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
        current_wifi_list_state = LOAD_WIFI_LIST;
    }
}

void MainWindow::setBtnWirelessStatus() {
    QThread *btnStatus = new QThread();
    BackThread *backThread = new BackThread();
    backThread->moveToThread(btnStatus);
    connect(btnStatus, &QThread::started, backThread, &BackThread::getInitStatus);
    connect(backThread, &BackThread::wifiStatus, this, [=] (bool status) {
        objKyDBus->oldWifiSwitchState = status;
        btnWireless->setSwitchStatus(status);
        btnStatus->quit(); //退出事件循环
        btnStatus->wait(); //释放资源
    });
    connect(btnStatus, SIGNAL(finished()), btnStatus, SLOT(deleteLater()));
    connect(backThread, SIGNAL(getWifiStatusComplete()), btnStatus, SLOT(quit()));
    btnStatus->start();
}
// 获取wifi列表回调
void MainWindow::getWifiListDone(QStringList slist)
{
    qDebug() << "Get wifi list done, current_wifi_list_state = " << current_wifi_list_state << ". m_is_inputting_pwd = " << m_is_inputting_wifi_password;
    setBtnWirelessStatus();
    //要求使用上一次获取到的列表
    if (this->ksnm->isUseOldWifiSlist) {
        slist = oldWifiSlist;
        this->ksnm->isUseOldWifiSlist = false;
    }

    if (slist.isEmpty() || slist.size() == 1) {
        if (oldWifiSlist.isEmpty() || oldWifiSlist.size() == 1) {
            this->stopLoading();
            return;
        } else {
            slist = oldWifiSlist;
        }
    }


    slist = priorityList(slist);

//    if (this->is_btnLanList_clicked == 1 && current_wifi_list_state != REFRESH_WIFI) {
//        oldWifiSlist = slist;
//        return;
//    }

    if ((current_wifi_list_state == LOAD_WIFI_LIST || current_wifi_list_state == REFRESH_WIFI) && !this->m_is_inputting_wifi_password) {
        if (!isReconnectingWifi) {
            loadWifiListDone(slist);
            is_init_wifi_list = 0;
        } else {
            qDebug() << "正在进行wifi的回连操作,现在停止加载wifi界面";
            oldWifiSlist = slist;
            return;
        }
    }

    if (current_wifi_list_state == UPDATE_WIFI_LIST || this->m_is_inputting_wifi_password) {
        //如果WiFi连接状态发生了改变,需要刷新整个列表,否则只需要比对新旧列表更新即可
        if (m_isWifiConnected && objKyDBus->checkWifiConnectivity() != WIFI_CONNECTED) {
            //qDebug() << "loadwifi的列表";
            loadWifiListDone(slist);
        } else if (!m_isWifiConnected && objKyDBus->checkWifiConnectivity() == WIFI_CONNECTED) {
            loadWifiListDone(slist);
        } else {
            //qDebug() << "updatewifi的列表";
            updateWifiListDone(slist);
            current_wifi_list_state = LOAD_WIFI_LIST;
        }
    }

    oldWifiSlist = slist;
}

// 获取已保存的连接列表回调
void MainWindow::getConnListDone(QStringList slist)
{
    if (isInitConnList) {
        for (int i = 1; i < slist.length() - 1; i++) {
            oldConnSlist << slist.at(i).trimmed();
        }
        isInitConnList = false;
        return;
    } else {
        QStringList newConnSlist;
        for (int i = 1; i < slist.length() - 1; i++) {
            newConnSlist << slist.at(i).trimmed();
        }
        for (auto s : newConnSlist) {
            if (!oldConnSlist.contains(s)) {
                lastAddedConn = s;
                break;
            }
        }
        if (isAddedWifi) {
            isAddedWifi = false;
            //如果是新添加的wifi,尝试激活这个wifi
            if (! is_stop_check_net_state) {
                this->is_stop_check_net_state = 1;
                qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
                QThread *t = new QThread();
                BackThread *bt = new BackThread();
                bt->moveToThread(t);
                connect(t, &QThread::finished, t, &QThread::deleteLater);
                connect(t, &QThread::started, this, [ = ]() {
                    bt->execConnWifi(lastAddedConn, objKyDBus->dbusWiFiCardName);
                });
                connect(bt, &BackThread::connDone, this, [ = ](int res) {
                    connWifiDone(res);
                    bt->deleteLater();
                });
                t->start();
            }
        }
        oldConnSlist.clear();
        oldConnSlist = newConnSlist;
        return;
    }
}
//QStringList MainWindow::priorityList(QStringList slist){
//    QStringList ret;
//    ret.append(slist[0]);
//    QString headLine = slist.at(0);
//    int indexSignal,indexSecu, indexFreq, indexBSsid, indexName,indexPath,indexCate;
//    headLine = headLine.trimmed();
//    bool isChineseExist = headLine.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
//    if (isChineseExist) {
//        indexSignal = headLine.indexOf("SIGNAL");
//        indexSecu = headLine.indexOf("安全性");
//        indexFreq = headLine.indexOf("频率") + 4;
//        indexBSsid = headLine.indexOf("BSSID") + 6;
//        indexName = indexBSsid + 19;
//        indexPath = headLine.indexOf("DBUS-PATH");
//        indexCate = headLine.indexOf("CATEGORY");
//    } else {
//        indexSignal = headLine.indexOf("SIGNAL");
//        indexSecu = headLine.indexOf("SECURITY");
//        indexFreq = headLine.indexOf("FREQ");
//        indexBSsid = headLine.indexOf("BSSID");
//        indexName = indexBSsid + 19;
//        indexPath = headLine.indexOf("DBUS-PATH");
//        indexCate = headLine.indexOf("CATEGORY");
//    }
//    QStringList p1,p2,p3,p4,p5,p6,p7;//按照信号与频段划分为6个列表
//    for(int i=1;i<slist.size();i++){
//        QString line = slist.at(i);
//        int conSignal = line.mid(indexSignal,3).trimmed().toInt();
//        int conFreq = line.mid(indexFreq,4).trimmed().toInt();
//        if(conSignal > 75 && conFreq >= 5000){
//            p1.append(line);
//            continue;
//        }else if(conSignal > 55 && conFreq >= 5000){
//            p2.append(line);
//            continue;
//        }else if(conSignal > 75){
//            p3.append(line);
//            continue;
//        }else if(conSignal > 55){
//            p4.append(line);
//            continue;
//        }else if(conSignal > 35){
//            p5.append(line);
//            continue;
//        }else if(conSignal > 15){
//            p6.append(line);
//            continue;
//        }else{
//            p7.append(line);
//        }
//    }
//    QVector<QStringList> listVec;
//    listVec<<p1<<p2<<p3<<p4<<p5<<p6<<p7;
//    for(auto list:listVec){
//        list = sortApByCategory(list,indexCate);
//        ret += list;
//    }
//    return ret;
//}

QStringList MainWindow::priorityList(QStringList slist){
    QStringList ret;
    ret.append(slist[0]);
    QString headLine = slist.at(0);
    int indexSignal,indexSecu, indexFreq, indexBSsid, indexName, indexPath, indexCate;
    headLine = headLine.trimmed();
    bool isChineseExist = headLine.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
    if (isChineseExist) {
        indexSignal = headLine.indexOf("SIGNAL");
        indexSecu = headLine.indexOf("安全性");
        indexFreq = headLine.indexOf("频率") + 4;
        indexBSsid = headLine.indexOf("BSSID") + 6;
        indexPath = headLine.indexOf("DBUS-PATH");
        indexCate = headLine.indexOf("CATEGORY");
        indexName = headLine.lastIndexOf("SSID");

    } else {
        indexSignal = headLine.indexOf("SIGNAL");
        indexSecu = headLine.indexOf("SECURITY");
        indexFreq = headLine.indexOf("FREQ");
        indexBSsid = headLine.indexOf("BSSID");
        indexPath = headLine.indexOf("DBUS-PATH");
        indexCate = headLine.indexOf("CATEGORY");
        indexName = headLine.lastIndexOf("SSID");
    }
    QStringList p1,p2,p3;//按照信号与category划分为3个列表
    for(int i=1;i<slist.size();i++){
        QString line = slist.at(i);
        int conSignal = line.mid(indexSignal,3).trimmed().toInt();
        int conCate = line.mid(indexCate, 1).trimmed().toInt();
        if (!isHuaWei9006C) {
            conCate = 0;
        }
        if(conSignal > 55 && conCate == 2){
            p1.append(line);
            continue;
        }else if(conSignal > 55 && conCate == 1){
            p2.append(line);
            continue;
        }else{
            p3.append(line);
            continue;
        }
    }
    QVector<QStringList> listVec;
    listVec<<p1<<p2<<p3;
    for(auto list:listVec){
        list = sortApByFreq(list,indexFreq, indexSignal);
        ret += list;
    }

    return ret;
}

QStringList MainWindow::sortApByFreq(QStringList list, int freqIndex, int signalIndex)
{
    QStringList ret;
    QStringList p1,p2,p3,p4,p5,p6,p7;
    for(auto line:list){
        int conSignal = line.mid(signalIndex,3).trimmed().toInt();
        int conFreq = line.mid(freqIndex,4).trimmed().toInt();
        if(conSignal > 75 && conFreq >= 5000){
            p1.append(line);
            continue;
        }else if(conSignal > 55 && conFreq >= 5000){
            p2.append(line);
            continue;
        }else if(conSignal > 75){
            p3.append(line);
            continue;
        }else if(conSignal > 55){
            p4.append(line);
            continue;
        }else if(conSignal > 35){
            p5.append(line);
            continue;
        }else if(conSignal > 15){
            p6.append(line);
            continue;
        }else{
            p7.append(line);
        }
    }
    ret<<p1<<p2<<p3<<p4<<p5<<p6<<p7;
    return ret;
}

QVector<QStringList> MainWindow::repetitionFilter(QVector<QStringList>){
    QVector<QStringList> ret;
    return ret;
}

QStringList MainWindow::sortApByCategory(QStringList list,int cateIndex){
    QStringList ret;
    for(auto line:list){
        int conCate = line.mid(cateIndex).trimmed().toInt();
        if(conCate == 2){
            ret.append(line);
        }
    }
    for(auto line:list){
        int conCate = line.mid(cateIndex).trimmed().toInt();
        if(conCate == 1){
            ret.append(line);
        }
    }
    for(auto line:list){
        int conCate = line.mid(cateIndex).trimmed().toInt();
        if(conCate == 0){
            ret.append(line);
        }
    }
    return ret;
}

//进行wifi列表优化选择,分为2.4G和5G进行选择,先每种频率形成一个列表
//同一个列表中同名wifi只有一个,再按信号强度由大到小合并列表
void MainWindow::wifiListOptimize(QStringList& slist)
{
    if (slist.size() < 2) return ;

    QString headLine = slist.at(0);
    int indexSignal, indexSecu, indexFreq, indexBSsid, indexName, indexPath, indexCate;
    headLine = headLine.trimmed();
    bool isChineseExist = headLine.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
    if (isChineseExist) {
        indexSignal = headLine.indexOf("SIGNAL");
        indexSecu = headLine.indexOf("安全性");
        indexFreq = headLine.indexOf("频率") + 4;
        indexBSsid = headLine.indexOf("BSSID") + 6;
        indexPath = headLine.indexOf("DBUS-PATH");
        indexCate = headLine.indexOf("CATEGORY");
        indexName = headLine.lastIndexOf("SSID");
    } else {
        indexSignal = headLine.indexOf("SIGNAL");
        indexSecu = headLine.indexOf("SECURITY");
        indexFreq = headLine.indexOf("FREQ");
        indexBSsid = headLine.indexOf("BSSID");
        indexPath = headLine.indexOf("DBUS-PATH");
        indexCate = headLine.indexOf("CATEGORY");
        indexName = headLine.lastIndexOf("SSID");
    }

    QStringList targetList; //slist优化,同名同频同类别(category)AP中只留信号最强
    targetList<<slist.at(0);    //把第一行加进去
//    hasStarWifiInfo = "";
//    hasStarWifiName = "";
    for (int ii = 1;ii < slist.size();ii++) {
        if ((ii+1) == slist.size()) {
            break;
        }
        QString currentWifiInfo = slist.at(ii);
        bool ifContinue = false;
        bool isConnected = false;

        QString conName = currentWifiInfo.mid(indexName).trimmed();
//        int conSignal = currentWifiInfo.mid(indexSignal,3).trimmed().toInt();
        int conFreq = currentWifiInfo.mid(indexFreq,4).trimmed().toInt();
        int conCate = currentWifiInfo.mid(indexCate,1).trimmed().toInt();
        if (!isHuaWei9006C) {
            conCate = 0;
        }

        if ("*" == currentWifiInfo.mid(0,indexSignal).trimmed()) {
//            hasStarWifiInfo = currentWifiInfo;
//            hasStarWifiName = conName;
            isConnected = true;
        }

        for (int jj=1;jj<ii;jj++) {//仅与排在它前面的wifi比较即可
            QString compareWifiInfo = slist.at(jj);
            QString name = compareWifiInfo.mid(indexName).trimmed();
//            int signal = compareWifiInfo.mid(indexSignal,3).trimmed().toInt();
            int freq = compareWifiInfo.mid(indexFreq,4).trimmed().toInt();
            int category = compareWifiInfo.mid(indexCate,1).trimmed().toInt();
            if (!isHuaWei9006C) {
                category = 0;
            }
            if (conName == name) {
                if (conFreq < 5000 && freq < 5000 && conCate == category) {
                    //若前面有同频同category的同名wifi,它的信号一定比此wifi强
                    ifContinue = true;
                    break;
                }
                if (conFreq >= 5000 && freq >= 5000 && conCate == category) {
                    ifContinue = true;
                    break;
                }
            }
        }
        if (ifContinue && !isConnected)  continue;
        targetList << currentWifiInfo;
    }

//    //上面的选网方法容易把存在同名wifi的情况下把已经连接的那个wifi给去掉
//    //在这种情况下,把连接的wifi信息加回去
//    int changePosition = 100000;
//    for (int kk=1;kk<targetList.size();kk++) {
//        QString wifiInfo = slist.at(kk);
//        QString wifiName = wifiInfo.mid(indexName).trimmed();
//        if (hasStarWifiName == wifiName) {
//            changePosition = kk;
//            break;
//        }
//    }

//    if (changePosition < 100000) {
//        //证明确实有已经连接的wifi被去掉了
//        targetList.replace(changePosition, hasStarWifiInfo);
//    }

    slist = targetList;
}

//最终优选出来的wifi列表
void MainWindow::getFinalWifiList(QStringList &slist)
{
    if(slist.size() < 2) return ;
    QString headLine = slist.at(0);
    int indexSignal,indexSecu, indexFreq, indexBSsid, indexName,indexPath;
    headLine = headLine.trimmed();
    bool isChineseExist = headLine.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
    if (isChineseExist) {
        indexSignal = headLine.indexOf("SIGNAL");
        indexSecu = headLine.indexOf("安全性");
        indexFreq = headLine.indexOf("频率") + 4;
        indexBSsid = headLine.indexOf("BSSID") + 6;
        indexPath = headLine.indexOf("DBUS-PATH");
        indexName = headLine.lastIndexOf("SSID");
    } else {
        indexSignal = headLine.indexOf("SIGNAL");
        indexSecu = headLine.indexOf("SECURITY");
        indexFreq = headLine.indexOf("FREQ");
        indexBSsid = headLine.indexOf("BSSID");
        indexPath = headLine.indexOf("DBUS-PATH");
        indexName = headLine.lastIndexOf("SSID");
    }

    QStringList deleteWifiStr;
    for(int ii = 1;ii < slist.size();ii++){
        if ((ii+1) == slist.size()) {
            break;
        }
        QString wifiInfo = slist.at(ii);
        QString conName = wifiInfo.mid(indexName).trimmed();
        int conSignal = wifiInfo.mid(indexSignal,3).trimmed().toInt();
        int conFreq = wifiInfo.mid(indexFreq,4).trimmed().toInt();
        for(int jj=ii+1;jj<slist.size();jj++){
            QString wifiStr = slist.at(jj);
            QString name = wifiStr.mid(indexName).trimmed();
            int signal = wifiStr.mid(indexSignal,3).trimmed().toInt();
            int freq = wifiStr.mid(indexFreq,4).trimmed().toInt();
            if(conName == name){
                if (conFreq >= 5000) {
                    //排在前面的一个是5Gwifi,并且信号也强一些
                    deleteWifiStr.append(wifiStr);
                } else {
                    //排在前面的一个是2.4G
                    if (freq >= 5000) {
                        //排在后面的一个是5G
                        if (signal >= 55) {
                            //排在后面的一个是5Gwifi信号强度 >= 3格,选5G
                            deleteWifiStr.append(wifiInfo);
                        } else {
                            deleteWifiStr.append(wifiStr);
                        }
                    } else {
                        //排在后面的一个是2.4G
                        deleteWifiStr.append(wifiStr);
                    }
                }
                break;
            }
        }
    }
    foreach (QString deleteStr, deleteWifiStr) {
        slist.removeOne(deleteStr);
    }
}

//从有配置文件的wifi选出最优wifi进行连接,同时考虑用户手动设置的优先级,这个函数在回连中用到
QVector<structWifiProperty> MainWindow::connectableWifiPriorityList(const QStringList slist){
    QVector<structWifiProperty> selectedWifiListStruct;
    if(slist.size()<2) return selectedWifiListStruct;
    OneConnForm *ocf = new OneConnForm();
    QString headLine = slist.at(0);
    int indexSignal,indexSecu, indexFreq, indexBSsid, indexName, indexPath,indexCate;
    headLine = headLine.trimmed();

    bool isChineseExist = headLine.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
    if (isChineseExist) {
        indexSignal = headLine.indexOf("SIGNAL");
        indexSecu = headLine.indexOf("安全性");
        indexFreq = headLine.indexOf("频率") + 4;
        indexBSsid = headLine.indexOf("BSSID") + 6;
        indexPath = headLine.indexOf("DBUS-PATH");
        indexCate = headLine.indexOf("CATEGORY");
        indexName = headLine.lastIndexOf("SSID");
    } else {
        indexSignal = headLine.indexOf("SIGNAL");
        indexSecu = headLine.indexOf("SECURITY");
        indexFreq = headLine.indexOf("FREQ");
        indexBSsid = headLine.indexOf("BSSID");
        indexPath = headLine.indexOf("DBUS-PATH");
        indexCate = headLine.indexOf("CATEGORY");
        indexName = headLine.lastIndexOf("SSID");
    }

    QStringList tmp = slist;
    for (int iter=1; iter<tmp.size(); iter++) {
        QString line = tmp.at(iter);
        QString wifiname = line.mid(indexName).trimmed();
        QString wifibssid = line.mid(indexBSsid, indexPath-indexBSsid).trimmed();
        QString wificate = line.mid(indexCate, 1).trimmed();
        if (!isHuaWei9006C) {
            wificate = "0";
        }
        QString wififreq = line.mid(indexFreq, 4).trimmed();
        QString wifiObjectPath;
        if (indexCate >= 0) {
            wifiObjectPath = line.mid(indexPath,indexCate-indexPath).trimmed();
        } else {
            wifiObjectPath = line.mid(indexPath,indexName-indexPath).trimmed();
        }
        QString wifiAutoConnection = "no";
        QString wifiPriority;

        if (ocf->isWifiConfExist(wifiname) && canReconnectWifiList.contains(wifiname)) {
            QString tmpPath = "/tmp/kylin-nm-lanprop-" + QDir::home().dirName();
            QString getInfoCmd = "nmcli -f connection.autoconnect,connection.autoconnect-priority connection show '" + wifiname + "' > " + tmpPath;
            Utils::m_system(getInfoCmd.toUtf8().data());
            QFile file(tmpPath);
            if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                //syslog(LOG_ERR, "Can't open the file /tmp/kylin-nm-lanprop in function connectableWifiPriorityList!");
                qDebug()<<"Can't open the file /tmp/kylin-nm-lanprop in function connectableWifiPriorityList!";
            }
            QString txt = file.readAll();
            QStringList txtLine = txt.split("\n");
            file.close();
            foreach (QString line, txtLine) {
                if (line.startsWith("connection.autoconnect:")) {
                    wifiAutoConnection = line.mid(25).trimmed(); //connection.autoconnect:有23个字符
                }
                if (line.startsWith("connection.autoconnect-priority:")) {
                    wifiPriority = line.mid(34).trimmed(); //connection.autoconnect-priority:有32个字符
                }
            }

            //可以自动回连,则加入列表
            if (wifiAutoConnection == "是" || wifiAutoConnection == "yes") {
                qDebug("Connectable wifi SSID:%s , category:%s ,frequence:%s",wifibssid.toUtf8().data(),wificate.toUtf8().data(),wififreq.toUtf8().data());
                structWifiProperty myWifiProStruct;
                myWifiProStruct.objectPath = wifiObjectPath;
                myWifiProStruct.bssid = wifibssid;
                myWifiProStruct.priority = wifiPriority.toInt();
                selectedWifiListStruct.append(myWifiProStruct);
            }
        }
    }

    //再按照wifiPriority进行排序,先简单的选出最高优先级的wifi进行连接
    if (selectedWifiListStruct.size() > 1) {
        devListSort(&selectedWifiListStruct);
        //foreach (structWifiProperty wifiPriorityAfterSort, selectedWifiListStruct) {
        //    qDebug() << wifiPriorityAfterSort.bssid << " 的自动连接优先级是 : " << wifiPriorityAfterSort.priority;
        //}
    }
    ocf->deleteLater();
    return selectedWifiListStruct;
}

//排序
void MainWindow::devListSort(QVector<structWifiProperty> *list)
{
//    qSort(list->begin(), list->end(), subDevListSort);
    std::sort(list->begin(), list->end(), subDevListSort);
}
bool MainWindow::subDevListSort(const structWifiProperty &info1, const structWifiProperty &info2)
{
    return info1.priority > info2.priority;
}

// 加载wifi列表
void MainWindow::loadWifiListDone(QStringList slist)
{
    if (this->is_btnLanList_clicked == 1) {
        onlyRefreshWifiList(slist);
        return;
    }
//    qDebug() << "kkkkkkkkkkkkkkkkkkkkkkkk";
//    foreach (QString kkkkk, slist) {
//        qDebug() << kkkkk;
//    }
//    qDebug() << "kkkkkkkkkkkkkkkkkkkkkkkk";
//    qDebug() << "                        ";

    delete topWifiListWidget; //清空top列表
    createTopWifiUI(); //创建topWifiListWidget
    // 清空wifi列表
    wifiListWidget = new QWidget(scrollAreaw);
    wifiListWidget->resize(W_LIST_WIDGET, H_WIFI_ITEM_BIG_EXTEND);
    scrollAreaw->setWidget(wifiListWidget);
    scrollAreaw->move(W_LEFT_AREA, Y_SCROLL_AREA);
    dbus_wifiList.clear();

    // 获取当前无线网的连接状态,正在连接wifiActState==1,已经连接wifiActState==2, 未连接wifiActState==3
    int wifiActState = objKyDBus->checkWifiConnectivity(); //检查wifi的连接状态
    if (wifiActState == WIFI_CONNECTED) {
        m_isWifiConnected = true;
    } else {
        m_isWifiConnected = false;
    }
//    if (isWifiBeConnUp && wifiActState == 1) {
//        wifiActState = 2;
//    }

    QList<QString> currConnWifiBSsidUuid;
    currConnWifiBSsidUuid = objKyDBus->getAtiveWifiBSsidUuid(slist);
//    bool isLoop = true;
//    do {
//        currConnWifiBSsidUuid = objKyDBus->getAtiveWifiBSsidUuid(slist);
//        if (currConnWifiBSsidUuid.size() == 1 && currConnWifiBSsidUuid.at(0).length() != 17) {
//            sleep(1); //等于1说明只获取到uuid,1秒后再获取一次
//        } else {
//            isLoop = false;
//        }
//    } while (isLoop);

    // 获取当前连接的wifi name
    QString actWifiName = "--";
    QString actWifiId = "--";
    actWifiSsid = "--";
    actWifiUuid = "--";
    if (currConnWifiBSsidUuid.size() > 1) {
        actWifiUuid = currConnWifiBSsidUuid.at(0);
        for (int i=1; i<currConnWifiBSsidUuid.size(); i++) {
            actWifiBssidList.append(currConnWifiBSsidUuid.at(i));
            //qDebug() << "debug: 获取到的bssid是:" << currConnWifiBSsidUuid.at(i);
        }
        //qDebug() << "debug: 获取到的uuid是:" << actWifiUuid;
    } else {
        actWifiBssidList.append("--");
    }

    activecon *act = kylin_network_get_activecon_info();
    int index = 0;
    while (act[index].con_name != NULL) {
        if (QString(act[index].type) == "wifi" || QString(act[index].type) == "802-11-wireless") {
            actWifiName = QString(act[index].con_name);
            break;
        }
        index ++;
    }

    // 填充可用网络列表
    QString headLine = slist.at(0);
    int indexSignal,indexSecu, indexFreq, indexBSsid, indexName, indexPath, indexCate;
    headLine = headLine.trimmed();

    bool isChineseExist = headLine.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
    if (isChineseExist) {
        indexSignal = headLine.indexOf("SIGNAL");
        indexSecu = headLine.indexOf("安全性");
        indexFreq = headLine.indexOf("频率") + 4;
        indexBSsid = headLine.indexOf("BSSID") + 6;
        indexPath = headLine.indexOf("DBUS-PATH");
        indexCate = headLine.indexOf("CATEGORY");
        indexName = headLine.lastIndexOf("SSID");
    } else {
        indexSignal = headLine.indexOf("SIGNAL");
        indexSecu = headLine.indexOf("SECURITY");
        indexFreq = headLine.indexOf("FREQ");
        indexBSsid = headLine.indexOf("BSSID");
        indexPath = headLine.indexOf("DBUS-PATH");
        indexCate = headLine.indexOf("CATEGORY");
        indexName = headLine.lastIndexOf("SSID");
    }
    QStringList wnames;
    int count = 0;
    QString actWifiBssid = " ";
    currActWifiBssid = " ";
    for (int i = 1; i < slist.size(); i ++) {
        QString line = slist.at(i);
        QString wbssid = line.mid(indexBSsid, 17).trimmed();
        int Path = line.indexOf("/org/");
        QString wDbusPath;
        if (indexCate >= 0) {
            wDbusPath = line.mid(Path,indexCate-Path).trimmed();
        } else {
            wDbusPath = line.mid(Path,indexName-Path).trimmed();
        }
        QDBusInterface interface("org.freedesktop.NetworkManager",
                                  wDbusPath,
                                  "org.freedesktop.DBus.Properties",
                                  QDBusConnection::systemBus() );
        QDBusReply<QVariant> reply = interface.call("Get","org.freedesktop.NetworkManager.AccessPoint","Ssid");
        QString wname = reply.value().toString();

        if (actWifiBssidList.contains(wbssid)) {
            actWifiName = wname;
        }
        if ("*" == line.mid(0,indexSignal).trimmed()) {
            //在华为的电脑中,因为前面的优选工作,即使有已经连接的wifi,也可能会被筛选出去
            actWifiBssid = wbssid;
            currActWifiBssid = wbssid;
        }
    }

    // 根据当前连接的wifi 设置OneConnForm
    OneConnForm *ccf = new OneConnForm(topWifiListWidget, this, confForm, ksnm);
    if (actWifiName == "--" || wifiActState == WIFI_CONNECTING || actWifiBssidList.at(0) == "--" || actWifiBssid == " ") {
        ccf->setWifiName(tr("Not connected"), "--", "--", "--", isHuaWeiPC, isHuaWei9006C);//"当前未连接任何 Wifi"
        ccf->setSignal("0", "--" , "0", false);
        activeWifiSignalLv = 0;
        ccf->setConnedString(1, tr("Disconnected"), "");//"未连接"
        ccf->isConnected = false;
        ccf->lbFreq->hide();
        ifWLanConnected = false;
        lbLoadDown->hide();
        lbLoadUp->hide();
        lbLoadDownImg->hide();
        lbLoadUpImg->hide();
        ccf->setTopItem(false);
        dbus_wifiList.append(QStringList("--")); //没有已连接wifi时,第一个元素为--
    } else {
        QProcess * process = new QProcess;
        QString name = actWifiName;
        process->start(QString("nmcli -f 802-11-wireless.ssid connection show \"%1\"").arg(name));
        connect(process, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, [ = ]() {
            process->deleteLater();
        });
        connect(process, &QProcess::readyReadStandardOutput, this, [ = ]() {
            QString str = process->readAllStandardOutput();
            actWifiSsid = str.mid(str.lastIndexOf(" ") + 1, str.length() - str.lastIndexOf(" ") - 2); //获取到ssid时,以ssid为准
        });
        connect(process, &QProcess::readyReadStandardError, this, [ = ]() {
            actWifiSsid = actWifiName; //没有获取到ssid时,以wifi名为准
        });
        process->waitForFinished();
    }
    ccf->setAct(true);
    ccf->move(L_VERTICAL_LINE_TO_ITEM, 0);
    ccf->show();

    if (actWifiBssidList.size()==1 && actWifiBssidList.at(0)=="--") {
        actWifiId = actWifiName;
        actWifiName = "--";
    }
    for (int i = 1, j = 0; i < slist.size(); i ++) {
        QString line = slist.at(i);
        QString wsignal = line.mid(indexSignal, 3).trimmed();
        QString wsecu = line.mid(indexSecu, indexFreq - indexSecu).trimmed();
        QString wbssid = line.mid(indexBSsid, 17).trimmed();
        QString wfreq = line.mid(indexFreq, 4).trimmed();
        QString wcate;
        if (indexCate >= 0)
            wcate = line.mid(indexCate, 1).trimmed();
        else
            wcate = QString::number(0);
        QString wDbusPath;
        if (indexCate >= 0) {
            wDbusPath = line.mid(indexPath,indexCate-indexPath).trimmed();
        } else {
            wDbusPath = line.mid(indexPath,indexName-indexPath).trimmed();
        }
        QDBusInterface interface("org.freedesktop.NetworkManager",
                                  wDbusPath,
                                  "org.freedesktop.DBus.Properties",
                                  QDBusConnection::systemBus() );
        QDBusReply<QVariant> reply = interface.call("Get","org.freedesktop.NetworkManager.AccessPoint","Ssid");
        QString wname = reply.value().toString();

        if (!isHuaWeiPC) {
            //如果不是华为的电脑,选择wifi在这里执行
            if (actWifiName != "--" && actWifiName == wname) {
//                if (!actWifiBssidList.contains(wbssid)) {
                if(actWifiBssid != wbssid) {
                    continue; //若当前热点ssid名称和已经连接的wifi的ssid名称相同,但bssid不同,则跳过
                }
            }
            if ((wnames.contains(wname) && wbssid != actWifiBssid)) {
                continue; //过滤相同名称的wifi
            }
        } else {
//            if ((actWifiName == wname) && actWifiBssidList.size()>=1) {
//                //防止列表中没有已经连接的那个wifi
//                wbssid = actWifiBssidList.at(0);
//                actWifiBssid = actWifiBssidList.at(0);
//            }
            if ((wnames.contains(wname) && wbssid != actWifiBssid)) {
                continue; //过滤相同名称的wifi
            }
        }

        int max_freq = wfreq.toInt();
        int min_freq = wfreq.toInt();
        for (int k = i; k < slist.size(); k ++) {
            QString m_DbusPath;
            if (indexCate >= 0) {
                m_DbusPath = slist.at(k).mid(indexPath,indexCate-indexPath).trimmed();
            } else {
                m_DbusPath = slist.at(k).mid(indexPath,indexName-indexPath).trimmed();
            }
            QDBusInterface m_interface("org.freedesktop.NetworkManager",
                                    m_DbusPath,
                                    "org.freedesktop.DBus.Properties",
                                    QDBusConnection::systemBus() );
            QDBusReply<QVariant> m_reply = m_interface.call("Get","org.freedesktop.NetworkManager.AccessPoint","Ssid");
            QString m_name = m_reply.value().toString();

            if (wname == m_name) {
                if (slist.at(k).mid(indexFreq, 4).trimmed().toInt() > max_freq) {
                    max_freq = slist.at(k).mid(indexFreq, 4).trimmed().toInt();
                } else if (slist.at(k).mid(indexFreq, 4).trimmed().toInt() < min_freq) {
                    min_freq = slist.at(k).mid(indexFreq, 4).trimmed().toInt();
                }
            }
        }
        //qDebug()<<"Wifi ssid="<<wname<<". max_freq="<<max_freq<<". min_freq="<<min_freq;
        int freqState = 0;
        if (max_freq < 3000) {
            //只有2.4GHZ
            freqState = 1;
        } else if (min_freq >= 5000) {
            //只有5GHZ
            freqState = 2;
        }
        if (wname != "" && wname != "--") {
            //qDebug() << "wifi的 actWifiBssid: " << actWifiBssid << "      wcate = " << wcate;
            //qDebug() << "wifi的 bssid: " << wbssid << "当前连接的wifi的bssid: " << actWifiBssidList;
            if (actWifiBssid == wbssid && wifiActState == WIFI_CONNECTED) {
                //对于已经连接的wifi
                connect(this, &MainWindow::actWifiSignalLvChanaged, ccf, [ = ](const int &signalLv) {
                    ccf->setSignal(QString::number(signalLv), wsecu, wcate, true);
                });
                connect(ccf, SIGNAL(selectedOneWifiForm(QString,int)), this, SLOT(oneTopWifiFormSelected(QString,int)));
                connect(ccf, SIGNAL(requestHandleWifiDisconn()), this, SLOT(handleWifiDisconn()));
                QString path;
                if (indexCate >= 0) {
                    path = line.mid(indexPath, indexCate - indexPath).trimmed();
                } else {
                    path = line.mid(indexPath, indexName - indexPath).trimmed();
                }
                QString m_name;
                if (path != "" && !path.isEmpty()) m_name= this->objKyDBus->getWifiSsid(path);
                if (m_name.isEmpty() || m_name == "") {
                    ccf->setWifiName(wname, wbssid, actWifiUuid, objKyDBus->dbusWiFiCardName, isHuaWeiPC, isHuaWei9006C);
                    if (!canReconnectWifiList.contains(wname)) {
                        canReconnectWifiList.append(wname);
                    } else {
                        canReconnectWifiList.removeOne(wname);
                        canReconnectWifiList.append(wname);
                    }
                } else {
                    ccf->setWifiName(m_name, wbssid, actWifiUuid, objKyDBus->dbusWiFiCardName, isHuaWeiPC, isHuaWei9006C);
                    if (!canReconnectWifiList.contains(m_name)) {
                        canReconnectWifiList.append(m_name);
                    } else {
                        canReconnectWifiList.removeOne(m_name);
                        canReconnectWifiList.append(m_name);
                    }
                }

                //ccf->setRate(wrate);
                int signal;
                if (wsignal.toInt() == 0)
                    signal = ccf->getSignal();
                else
                    signal = wsignal.toInt();
                ccf->setSignal(QString::number(signal), wsecu, wcate, true);
                setTrayIconOfWifi(wsignal.toInt());
                activeWifiSignalLv = wsignal.toInt();
                //objKyDBus->getWifiMac(wname);
                if (freqState == 0) {
                    //该WiFi含2.4G和5G的AP,需要判断当前连接的是那种类型
                    if (wfreq.toInt() >= 5000)
                        ccf->setWifiInfo(wsecu, wsignal, wbssid, 2);
                    else
                        ccf->setWifiInfo(wsecu, wsignal, wbssid, 1);
                } else {
                    ccf->setWifiInfo(wsecu, wsignal, wbssid, freqState);
                }
                ccf->setConnedString(1, tr("NetOn"), wsecu);//"已连接"
                ccf->isConnected = true;
                ifWLanConnected = true;
                lbLoadDown->show();
                lbLoadUp->show();
                lbLoadDownImg->show();
                lbLoadUpImg->show();
                ccf->setTopItem(false);
                currSelNetName = "";
                qDebug()<<"already insert an active wifi item in the top of wifi list";
                //syslog(LOG_DEBUG, "already insert an active wifi item in the top of wifi list");
                if (m_name.isEmpty() || m_name == "") {
                    dbus_wifiList.insert(0, QStringList()<<wname<<wsignal<<wsecu<<QString::number(max_freq)<<QString::number(min_freq)<<wcate);
                } else {
                    dbus_wifiList.insert(0, QStringList()<<m_name<<wsignal<<wsecu<<QString::number(max_freq)<<QString::number(min_freq)<<wcate);
                }
            } else {
                //对于未连接的wifi
                wifiListWidget->resize(W_LIST_WIDGET, wifiListWidget->height() + H_NORMAL_ITEM);

                OneConnForm *ocf = new OneConnForm(wifiListWidget, this, confForm, ksnm);
                connect(ocf, SIGNAL(selectedOneWifiForm(QString,int)), this, SLOT(oneWifiFormSelected(QString,int)));
                QString path;
                if (indexCate >= 0) {
                    path = line.mid(indexPath, indexCate - indexPath).trimmed();
                } else {
                    path = line.mid(indexPath, indexName - indexPath).trimmed();
                }
                QString m_name;
                if (path != "" && !path.isEmpty()) m_name= this->objKyDBus->getWifiSsid(path);
                if (m_name.isEmpty() || m_name == "") {
                    ocf->setWifiName(wname, wbssid, actWifiUuid, objKyDBus->dbusWiFiCardName, isHuaWeiPC, isHuaWei9006C);
                } else {
                    ocf->setWifiName(m_name, wbssid, actWifiUuid, objKyDBus->dbusWiFiCardName, isHuaWeiPC, isHuaWei9006C);
                }
                if (m_wifi_list_pwd_changed.contains(ocf->getName())) {
                    ocf->setlbPwdTipVisble(true);
                } else {
                    ocf->setlbPwdTipVisble(false);
                }
                //ocf->setRate(wrate);
                ocf->setLine(true);
                ocf->setSignal(wsignal, wsecu, wcate, true);
                //objKyDBus->getWifiMac(wname);
                ocf->setWifiInfo(wsecu, wsignal, wbssid, freqState);
                ocf->setConnedString(0, tr("Disconnected"), wsecu);
                ocf->move(L_VERTICAL_LINE_TO_ITEM, j * H_NORMAL_ITEM);
                ocf->setSelected(false, false);
                ocf->show();

                if ((actWifiBssidList.contains(wbssid) && wifiActState == WIFI_CONNECTING) || (actWifiId == wname && wifiActState == WIFI_CONNECTING)) {
                    ocf->startWifiWaiting(true);
                }

                j ++;
                count ++;

                if (m_name.isEmpty() || m_name == "") {
                    dbus_wifiList.append(QStringList()<<wname<<wsignal<<wsecu<<QString::number(max_freq)<<QString::number(min_freq)<<wcate);
                } else {
                    dbus_wifiList.append(QStringList()<<m_name<<wsignal<<wsecu<<QString::number(max_freq)<<QString::number(min_freq)<<wcate);
                }
            }

            wnames.append(wname);
        }
    }

    QList<OneConnForm *> itemList = wifiListWidget->findChildren<OneConnForm *>();
    int n = itemList.size();
    if (n >= 1) {
        OneConnForm *lastItem = itemList.at(n-1);
        lastItem->setLine(false); //最后一个item不需要下划线
        lbNoItemTip->hide();
        lbTopWifiList->show();
        btnAddNet->show();
    } else {
        if (ifWLanConnected) {
            lbNoItemTip->show();
            lbNoItemTip->setText(tr("No Other Wireless Network Scheme"));
        } else {
            lbNoItemTip->hide();
            lbTopWifiList->hide();
            btnAddNet->hide();
        }
    }

    this->lanListWidget->hide();
    this->topLanListWidget->hide();
    this->wifiListWidget->show();
    this->topWifiListWidget->show();

    if (!this->isReconnectingWifi)
        this->stopLoading();
    is_stop_check_net_state = 0;
    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
    is_connect_hide_wifi = 0;

    actWifiBssidList.clear();
    wnames.clear();
    emit this->getWifiListFinished();
    bShowPb = true;  //可以showpb
}

// 更新wifi列表
void MainWindow::updateWifiListDone(QStringList slist)
{
    if (this->is_btnLanList_clicked == 1) {
        onlyRefreshWifiList(slist);
        return;
    }
    qDebug()<<"Refreshed wifi list.";

    if (this->ksnm->isExecutingGetLanList){ return;}

    //获取表头信息
    QString lastHeadLine = oldWifiSlist.at(0);
    int lastIndexName;
    lastHeadLine = lastHeadLine.trimmed();
//    bool isChineseInIt = lastHeadLine.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
    lastIndexName = lastHeadLine.lastIndexOf("SSID");

    QString headLine = slist.at(0);
    int indexSecu, indexFreq, indexBSsid, indexName, indexPath, indexCate;
    headLine = headLine.trimmed();
    bool isChineseExist = headLine.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
    if (isChineseExist) {
        indexSecu = headLine.indexOf("安全性");
        indexFreq = headLine.indexOf("频率") + 4;
        indexBSsid = headLine.indexOf("BSSID") + 6;
        indexPath = headLine.indexOf("DBUS-PATH");
        indexCate = headLine.indexOf("CATEGORY");
        indexName = headLine.lastIndexOf("SSID");
    } else {
        indexSecu = headLine.indexOf("SECURITY");
        indexFreq = headLine.indexOf("FREQ");
        indexBSsid = headLine.indexOf("BSSID");
        indexPath = headLine.indexOf("DBUS-PATH");
        indexCate = headLine.indexOf("CATEGORY");
        indexName = headLine.lastIndexOf("SSID");
    }
    //列表中去除已经减少的wifi
    for (int i=1; i<oldWifiSlist.size(); i++){
        QString line = oldWifiSlist.at(i);
        QString lastWname = line.mid(lastIndexName).trimmed();
        for (int j=1; j<slist.size(); j++){
            QString line = slist.at(j);
            QString wname = line.mid(indexName).trimmed();

            if (lastWname == wname){break;} //在slist最后之前找到了lastWname,则停止
            if (j == slist.size()-1) {
                QList<OneConnForm *> wifiList = wifiListWidget->findChildren<OneConnForm *>();
                for (int pos = 0; pos < wifiList.size(); pos ++) {
                    OneConnForm *ocf = wifiList.at(pos);
                    if (ocf->getName() == lastWname) {
                        if (ocf->isActive == true){break;
                        } else {
                            bool is_inputting = ocf->isInputtingPwd();
                            delete ocf;
                            //删除元素下面的的所有元素上移
                            for (int after_pos = pos+1; after_pos < wifiList.size(); after_pos ++) {
                                OneConnForm *after_ocf = wifiList.at(after_pos);
                                if (lastWname == currSelNetName) {after_ocf->move(L_VERTICAL_LINE_TO_ITEM, after_ocf->y() - H_NORMAL_ITEM - H_WIFI_ITEM_BIG_EXTEND);}
                                else if (is_inputting) {
                                    after_ocf->move(L_VERTICAL_LINE_TO_ITEM, after_ocf->y() - H_NORMAL_ITEM - H_WIFI_ITEM_SMALL_EXTEND);
                                    this->m_is_inputting_wifi_password = false; //正在输入密码的wifi消失了
                                } else {after_ocf->move(L_VERTICAL_LINE_TO_ITEM, after_ocf->y() - H_NORMAL_ITEM);}
                            }
                            wifiListWidget->resize(W_LIST_WIDGET, wifiListWidget->height() - H_NORMAL_ITEM);
                            //从向外提供的wifi列表中找到并删除这一行
                            QStringList list_to_remove;
                            foreach (QStringList list, dbus_wifiList) {
                                if (list.at(0).trimmed() == lastWname) {
                                    list_to_remove = list;
                                    break;
                                }
                            }
                            if (!list_to_remove.isEmpty()) {
                                dbus_wifiList.removeOne(list_to_remove);
                            }
//                            qDebug()<<"移除了一个WiFi,将会向控制面板发送信号。ssid="<<lastWname;
//                            emit this->getWifiListFinished();
                            break;
                        }
                    }
                }

            } //end if (j == slist.size()-1)
        } //end (int j=1; j<slist.size(); j++)
    }

    //列表中插入新增的wifi
    QStringList wnames;
    int count = 0;
    bool isConnected = false;
    for(int i = 1; i < slist.size(); i++){
        QString line = slist.at(i);
        QString wsignal = line.mid(0, indexSecu).trimmed();
        QString wsecu = line.mid(indexSecu, indexFreq - indexSecu).trimmed();
        QString wbssid = line.mid(indexBSsid, 17).trimmed();
        QString wname = line.mid(indexName).trimmed();
        QString wfreq = line.mid(indexFreq, 4).trimmed();
        QString wuse = line.mid(0,indexSecu).trimmed();
        QString wpath;
        if(wuse == "*"){
            isConnected = true;
        }

        if (indexCate >= 0) {
            wpath = line.mid(indexPath, indexCate - indexPath).trimmed();
        } else {
            wpath = line.mid(indexPath, indexName - indexPath).trimmed();
        }
        QString wcate = line.mid(indexCate, 1).trimmed();

        if(wname == "" || wname == "--"){continue;}

        bool isContinue = false;
        foreach (QString addName, wnames) {
            // 重复的网络名称,跳过不处理
            if(addName == wname){isContinue = true;}
        }
        if(isContinue){continue;}

        int max_freq = wfreq.toInt();
        int min_freq = wfreq.toInt();
        for (int k = 0; k < slist.size(); k ++) {
            QString m_name = slist.at(k).mid(indexName).trimmed();
//            QString m_name = this->objKyDBus->getWifiSsid(QString("/org/freedesktop/NetworkManager/AccessPoint/%1").arg(slist.at(k).mid(slist.at(k).lastIndexOf("/") + 1).trimmed()));
            if (wname == m_name) {
                if (slist.at(k).mid(indexFreq, 4).trimmed().toInt() > max_freq) {
                    max_freq = slist.at(k).mid(indexFreq, 4).trimmed().toInt();
                } else if (slist.at(k).mid(indexFreq, 4).trimmed().toInt() < min_freq) {
                    min_freq = slist.at(k).mid(indexFreq, 4).trimmed().toInt();
                }
            }
        }

        //qDebug()<<"Wifi ssid="<<wname<<". max_freq="<<max_freq<<". min_freq="<<min_freq;
        int freqState = 0;
        if (max_freq < 3000) {
            //只有2.4GHZ
            freqState = 1;
        } else if (min_freq >= 5000) {
            //只有5GHZ
            freqState = 2;
        }

        wnames.append(wname);
        for (int j=1; j < oldWifiSlist.size(); j++) {
            QString line = oldWifiSlist.at(j);
            QString lastWname = line.mid(lastIndexName).trimmed();
            if (lastWname == wname){break;} //上一次的wifi列表已经有名为wname的wifi,则停止
            if (j == oldWifiSlist.size()-1) { //到lastSlist最后一个都没找到,执行下面流程
                QList<OneConnForm *> wifiList = wifiListWidget->findChildren<OneConnForm *>();
                int n = wifiList.size();
                int posY = 0;
                if (n >= 1) {
                    OneConnForm *lastOcf = wifiList.at(n-1);
                    lastOcf->setLine(true);
                    if (lastOcf->wifiName == currSelNetName) {
                        posY = lastOcf->y()+H_NORMAL_ITEM + H_WIFI_ITEM_BIG_EXTEND;
                    } else {
                        posY = lastOcf->y()+H_NORMAL_ITEM;
                    }
                }

                wifiListWidget->resize(W_LIST_WIDGET, wifiListWidget->height() + H_NORMAL_ITEM);
                OneConnForm *addItem = new OneConnForm(wifiListWidget, this, confForm, ksnm);
                connect(addItem, SIGNAL(selectedOneWifiForm(QString,int)), this, SLOT(oneWifiFormSelected(QString,int)));
                QString m_name;
                if (wpath != "" && !wpath.isEmpty()) m_name= this->objKyDBus->getWifiSsid(wpath);
                if (m_name.isEmpty() || m_name == "") {
                    addItem->setWifiName(wname, wbssid, actWifiUuid, objKyDBus->dbusWiFiCardName, isHuaWeiPC, isHuaWei9006C);
                } else {
                    addItem->setWifiName(m_name, wbssid, actWifiUuid, objKyDBus->dbusWiFiCardName, isHuaWeiPC, isHuaWei9006C);
                }
                if (m_wifi_list_pwd_changed.contains(addItem->getName())) {
                    addItem->setlbPwdTipVisble(true);
                } else {
                    addItem->setlbPwdTipVisble(false);
                }
                //addItem->setRate(wrate);
                addItem->setLine(false);
                addItem->setSignal(wsignal, wsecu, wcate, true);
                //objKyDBus->getWifiMac(wname);
                addItem->setWifiInfo(wsecu, wsignal, wbssid, freqState);
                addItem->setConnedString(0, tr("Disconnected"), wsecu);//"未连接"
                addItem->move(L_VERTICAL_LINE_TO_ITEM, posY);
                addItem->setSelected(false, false);
                addItem->show();
                if (m_name.isEmpty() || m_name == "") {
                    dbus_wifiList.append(QStringList()<<wname<<wsignal<<wsecu<<QString::number(max_freq)<<QString::number(min_freq)<<wcate);
                } else {
                    dbus_wifiList.append(QStringList()<<m_name<<wsignal<<wsecu<<QString::number(max_freq)<<QString::number(min_freq)<<wcate);
                }

                count += 1;
//                qDebug()<<"新增了一个WiFi,将会向控制面板发送信号。ssid="<<lastWname;
//                emit this->getWifiListFinished();
            }
        }
    }
    if (isConnected) {
        lbLoadDown->show();
        lbLoadUp->show();
        lbLoadDownImg->show();
        lbLoadUpImg->show();
    }
    this->lanListWidget->hide();
    this->topLanListWidget->hide();
    this->wifiListWidget->show();
    this->topWifiListWidget->show();
    this->stopLoading();
    emit this->getWifiListFinished();
}

/**
 * @brief MainWindow::onlyRefreshWifiList 当停留在有线页面时,不重绘列表,仅更新
 * @param slist
 */
void MainWindow::onlyRefreshWifiList(QStringList slist)
{
    dbus_wifiList.clear();
    // 获取当前无线网的连接状态,正在连接wifiActState==1,已经连接wifiActState==2, 未连接wifiActState==3
    int wifiActState = objKyDBus->checkWifiConnectivity(); //检查wifi的连接状态
    if (wifiActState == WIFI_CONNECTED) {
        m_isWifiConnected = true;
    } else {
        m_isWifiConnected = false;
    }
    QList<QString> currConnWifiBSsidUuid;
    currConnWifiBSsidUuid = objKyDBus->getAtiveWifiBSsidUuid(slist);

    // 获取当前连接的wifi name
    QString actWifiName = "--";
    QString actWifiId = "--";
    actWifiSsid = "--";
    actWifiUuid = "--";
    if (currConnWifiBSsidUuid.size() > 1) {
        actWifiUuid = currConnWifiBSsidUuid.at(0);
        for (int i=1; i<currConnWifiBSsidUuid.size(); i++) {
            actWifiBssidList.append(currConnWifiBSsidUuid.at(i));
        }
    } else {
        actWifiBssidList.append("--");
    }

    activecon *act = kylin_network_get_activecon_info();
    int index = 0;
    while (act[index].con_name != NULL) {
        if (QString(act[index].type) == "wifi" || QString(act[index].type) == "802-11-wireless") {
            actWifiName = QString(act[index].con_name);
            break;
        }
        index ++;
    }

    QString headLine = slist.at(0);
    int indexSignal,indexSecu, indexFreq, indexBSsid, indexName, indexPath, indexCate;
    headLine = headLine.trimmed();

    bool isChineseExist = headLine.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
    if (isChineseExist) {
        indexSignal = headLine.indexOf("SIGNAL");
        indexSecu = headLine.indexOf("安全性");
        indexFreq = headLine.indexOf("频率") + 4;
        indexBSsid = headLine.indexOf("BSSID") + 6;
        indexPath = headLine.indexOf("DBUS-PATH");
        indexCate = headLine.indexOf("CATEGORY");
        indexName = headLine.lastIndexOf("SSID");
    } else {
        indexSignal = headLine.indexOf("SIGNAL");
        indexSecu = headLine.indexOf("SECURITY");
        indexFreq = headLine.indexOf("FREQ");
        indexBSsid = headLine.indexOf("BSSID");
        indexPath = headLine.indexOf("DBUS-PATH");
        indexCate = headLine.indexOf("CATEGORY");
        indexName = headLine.lastIndexOf("SSID");
    }
    QStringList wnames;
    QString actWifiBssid = " ";
    for (int i = 1; i < slist.size(); i ++) {
        QString line = slist.at(i);
        QString wbssid = line.mid(indexBSsid, 17).trimmed();
        int Path = line.indexOf("/org/");
        QString wDbusPath;
        if (indexCate >= 0) {
            wDbusPath = line.mid(Path,indexCate-Path).trimmed();
        } else {
            wDbusPath = line.mid(Path,indexName-Path).trimmed();
        }
        QDBusInterface interface("org.freedesktop.NetworkManager",
                                  wDbusPath,
                                  "org.freedesktop.DBus.Properties",
                                  QDBusConnection::systemBus() );
        QDBusReply<QVariant> reply = interface.call("Get","org.freedesktop.NetworkManager.AccessPoint","Ssid");
        QString wname = reply.value().toString();

        if (actWifiBssidList.contains(wbssid)) {
            actWifiName = wname;
        }
        if ("*" == line.mid(0,indexSignal).trimmed()) {
            //在华为的电脑中,因为前面的优选工作,即使有已经连接的wifi,也可能会被筛选出去
            actWifiBssid = wbssid;
        }
    }

    if (actWifiName == "--" || wifiActState == WIFI_CONNECTING || actWifiBssidList.at(0) == "--" || actWifiBssid == " ") {
        dbus_wifiList.append(QStringList("--")); //没有已连接wifi时,第一个元素为--
    } else {
        QProcess * process = new QProcess;
        QString name = actWifiName;
        process->start(QString("nmcli -f 802-11-wireless.ssid connection show \"%1\"").arg(name));
        connect(process, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, [ = ]() {
            process->deleteLater();
        });
        connect(process, &QProcess::readyReadStandardOutput, this, [ = ]() {
            QString str = process->readAllStandardOutput();
            actWifiSsid = str.mid(str.lastIndexOf(" ") + 1, str.length() - str.lastIndexOf(" ") - 2); //获取到ssid时,以ssid为准
        });
        connect(process, &QProcess::readyReadStandardError, this, [ = ]() {
            actWifiSsid = actWifiName; //没有获取到ssid时,以wifi名为准
        });
        process->waitForFinished();
        process->deleteLater();
    }

    if (actWifiBssidList.size()==1 && actWifiBssidList.at(0)=="--") {
        actWifiId = actWifiName;
        actWifiName = "--";
    }
    for (int i = 1, j = 0; i < slist.size(); i ++) {
        QString line = slist.at(i);
        QString wsignal = line.mid(indexSignal, 3).trimmed();
        QString wsecu = line.mid(indexSecu, indexFreq - indexSecu).trimmed();
        QString wbssid = line.mid(indexBSsid, 17).trimmed();
        QString wfreq = line.mid(indexFreq, 4).trimmed();
        QString wcate;
        if (indexCate >= 0)
            wcate = line.mid(indexCate, 1).trimmed();
        else
            wcate = QString::number(0);
        QString wDbusPath;
        if (indexCate >= 0) {
            wDbusPath = line.mid(indexPath,indexCate-indexPath).trimmed();
        } else {
            wDbusPath = line.mid(indexPath,indexName-indexPath).trimmed();
        }
        QDBusInterface interface("org.freedesktop.NetworkManager",
                                  wDbusPath,
                                  "org.freedesktop.DBus.Properties",
                                  QDBusConnection::systemBus() );
        QDBusReply<QVariant> reply = interface.call("Get","org.freedesktop.NetworkManager.AccessPoint","Ssid");
        QString wname = reply.value().toString();

        if (!isHuaWeiPC) {
            //如果不是华为的电脑,选择wifi在这里执行
            if (actWifiName != "--" && actWifiName == wname) {
                if (!actWifiBssidList.contains(wbssid)) {
                    continue; //若当前热点ssid名称和已经连接的wifi的ssid名称相同,但bssid不同,则跳过
                }
            }
            if ((wnames.contains(wname) && wbssid != actWifiBssid)) {
                continue; //过滤相同名称的wifi
            }
        } else {
            if ((wnames.contains(wname) && wbssid != actWifiBssid)) {
                continue; //过滤相同名称的wifi
            }
        }

        int max_freq = wfreq.toInt();
        int min_freq = wfreq.toInt();
        for (int k = i; k < slist.size(); k ++) {
            QString m_DbusPath;
            if (indexCate >= 0) {
                m_DbusPath = slist.at(k).mid(indexPath,indexCate-indexPath).trimmed();
            } else {
                m_DbusPath = slist.at(k).mid(indexPath,indexName-indexPath).trimmed();
            }
            QDBusInterface m_interface("org.freedesktop.NetworkManager",
                                    m_DbusPath,
                                    "org.freedesktop.DBus.Properties",
                                    QDBusConnection::systemBus() );
            QDBusReply<QVariant> m_reply = m_interface.call("Get","org.freedesktop.NetworkManager.AccessPoint","Ssid");
            QString m_name = m_reply.value().toString();

            if (wname == m_name) {
                if (slist.at(k).mid(indexFreq, 4).trimmed().toInt() > max_freq) {
                    max_freq = slist.at(k).mid(indexFreq, 4).trimmed().toInt();
                } else if (slist.at(k).mid(indexFreq, 4).trimmed().toInt() < min_freq) {
                    min_freq = slist.at(k).mid(indexFreq, 4).trimmed().toInt();
                }
            }
        }
        if (wname != "" && wname != "--") {
            QString path;
            if (indexCate >= 0) {
                path = line.mid(indexPath, indexCate - indexPath).trimmed();
            } else {
                path = line.mid(indexPath, indexName - indexPath).trimmed();
            }
            QString m_name;
            if (path != "" && !path.isEmpty()) m_name= this->objKyDBus->getWifiSsid(path);
            if (actWifiBssid == wbssid && wifiActState == WIFI_CONNECTED) {
                //对于已经连接的wifi
                if (m_name.isEmpty() || m_name == "") {
                    dbus_wifiList.insert(0, QStringList()<<wname<<wsignal<<wsecu<<QString::number(max_freq)<<QString::number(min_freq)<<wcate);
                } else {
                    dbus_wifiList.insert(0, QStringList()<<m_name<<wsignal<<wsecu<<QString::number(max_freq)<<QString::number(min_freq)<<wcate);
                }
            } else {
                //对于未连接的wifi
                j ++;
                if (m_name.isEmpty() || m_name == "") {
                    dbus_wifiList.append(QStringList()<<wname<<wsignal<<wsecu<<QString::number(max_freq)<<QString::number(min_freq)<<wcate);
                } else {
                    dbus_wifiList.append(QStringList()<<m_name<<wsignal<<wsecu<<QString::number(max_freq)<<QString::number(min_freq)<<wcate);
                }
            }
            wnames.append(wname);
        }
    }

    if (!this->isReconnectingWifi)
        this->stopLoading();
    is_stop_check_net_state = 0;
    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
    is_connect_hide_wifi = 0;

    actWifiBssidList.clear();
    wnames.clear();
    emit this->getWifiListFinished();
}

//用于中英文系统有线网络名称国际话
QString MainWindow::TranslateLanName(QString lanName)
{
    QString returnLanName = lanName;

    if (lanName == "有线连接") {
        returnLanName = tr("Wired connection");
    } else if (lanName.indexOf("有线连接") != -1) {
        QStringList strList = lanName.split(" ");
        if (strList.size() >= 2 && strList.at(1) != "") {
            returnLanName = tr("Wired connection") + " " + strList.at(1);
        }
    }

    if (lanName == "Wired connection") {
        returnLanName = tr("Wired connection");
    } else if (lanName.indexOf("Wired connection") != -1) {
        QStringList strList = lanName.split(" ");
        if (strList.size() >= 3 && strList.at(2) != "") {
            returnLanName = tr("Wired connection") + " " + strList.at(2);
        }
    }

    if (lanName == "以太网连接") {
        returnLanName = tr("Ethernet connection");
    } else if (lanName.indexOf("以太网连接") != -1) {
        QStringList strList = lanName.split(" ");
        if (strList.size() >= 2 && strList.at(1) != "") {
            returnLanName = tr("Ethernet connection") + " " + strList.at(1);
        }
    }

    if (lanName == "Ethernet connection") {
        returnLanName = tr("Ethernet connection");
    } else if (lanName.indexOf("Ethernet connection") != -1) {
        QStringList strList = lanName.split(" ");
        if (strList.size() >= 3 && strList.at(2) != "") {
            returnLanName = tr("Ethernet connection") + " " + strList.at(2);
        }
    }

    return returnLanName;
}

//使用uuid获取对应的mac地址
QString MainWindow::getMacByUuid(QString uuidName)
{
    QString resultMac = "";

    QString tmpPath = "/tmp/kylin-nm-lanprop-" + QDir::home().dirName();
    QString cmd = "nmcli connection show '" + uuidName + "' > " + tmpPath;
    Utils::m_system(cmd.toUtf8().data());

    QFile file(tmpPath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        //syslog(LOG_ERR, "Can't open the file /tmp/kylin-nm-lanprop!");
        qDebug()<<"Can't open the file /tmp/kylin-nm-lanprop!"<<endl;
    }

    QString txt = file.readAll();
    QStringList txtLine = txt.split("\n");
    file.close();

    foreach (QString line, txtLine) {
        if (line.startsWith("802-3-ethernet.mac-address:")) {
            resultMac = line.mid(30).trimmed();
        }
    }

    return resultMac;
}

void MainWindow::checkIfConnectedWifiExist()
{
    //在点击托盘图标时,判读有无wifi的连接,若wstate不为0,但是有wifi连接,就断开
    BackThread *bt = new BackThread();
    IFace *iface = bt->execGetIface();
    if (iface->wstate != 0) {
        QString toDisConnWifi = "nmcli connection down '" + actWifiUuid + "'";
        system(toDisConnWifi.toUtf8().data());
    }
}

///////////////////////////////////////////////////////////////////////////////
//主窗口其他按钮点击响应

void MainWindow::on_btnAdvConf_clicked()
{
//    if (!kysec_is_disabled() && kysec_get_3adm_status()) {
//        //三权分立启用的操作
//        qDebug() << "三权分立启用的操作------>";
//        QMessageBox msgBox;
//        msgBox.setWindowTitle(QObject::tr("kylin-nm"));
//        msgBox.setIcon(QMessageBox::Warning);
//        msgBox.setText(QObject::tr("Insufficient permissions"));
//        msgBox.addButton(QMessageBox::Ok);
//        msgBox.button(QMessageBox::Ok)->setText(QObject::tr("OK"));
//        msgBox.exec();
//    } else {
//        //三权分立未启用的操作
//        qDebug() << "三权分立未启用的操作";
//        QProcess *qprocess = new QProcess(this);
//        qprocess->start("nm-connection-editor &");
//    }
    QProcess p(0);
    p.startDetached("nm-connection-editor &");
    p.waitForStarted();
    return;
}

void MainWindow::on_btnFlyMode_clicked()
{
    if (is_fly_mode_on == 0) {
        is_fly_mode_on = 1;

        onBtnWifiClicked(0);
        on_btnWifiList_clicked();
    } else {
        is_fly_mode_on = 0;
    }
}

void MainWindow::on_btnHotspot_clicked()
{
    if (is_wireless_adapter_ready == 1) {
        if (is_hot_sopt_on == 0) {
            ui->lbHotImg->setStyleSheet("QLabel{background-image:url(:/res/x/hot-spot-on.svg);}");
            ui->lbHotBG->setStyleSheet(btnOnQss);
            is_hot_sopt_on = 1;

            QApplication::setQuitOnLastWindowClosed(false);
            DlgHotspotCreate *hotCreate = new DlgHotspotCreate(objKyDBus->dbusWiFiCardName);
            connect(hotCreate,SIGNAL(updateHotspotList()),this,SLOT(on_btnWifiList_clicked() ));
            connect(hotCreate,SIGNAL(btnHotspotState()),this,SLOT(on_btnHotspotState() ));
            hotCreate->show();
        } else {
            on_btnHotspotState();

            BackThread objBT;
            objBT.disConnLanOrWifi("wifi");

            sleep(2);
            on_btnWifiList_clicked();
        }
    }
}

void MainWindow::onBtnAddNetClicked()
{
    QApplication::setQuitOnLastWindowClosed(false);
    DlgHideWifi *connHidWifi = new DlgHideWifi(0, this);
    connect(connHidWifi, SIGNAL(reSetWifiList() ), this, SLOT(on_btnWifiList_clicked()) );
    connHidWifi->show();
}

void MainWindow::onBtnCreateNetClicked()
{
    QPoint pos = QCursor::pos();
    QRect primaryGeometry;
    for (QScreen *screen : qApp->screens()) {
        if (screen->geometry().contains(pos)) {
            primaryGeometry = screen->geometry();
        }
    }

    if (primaryGeometry.isEmpty()) {
        primaryGeometry = qApp->primaryScreen()->geometry();
    }

    QApplication::setQuitOnLastWindowClosed(false);
    ConfForm *m_cf = new ConfForm();
    m_cf->cbTypeChanged(3);
    m_cf->move(primaryGeometry.width() / 2 - m_cf->width() / 2, primaryGeometry.height() / 2 - m_cf->height() / 2);
    m_cf->show();
}

/* 右键菜单打开网络设置界面 */
void MainWindow::actionTriggerSlots()
{
    if (!m_load_finished) {
        m_secondary_start_timer->stop();
        secondaryStart();
    }
    this->move(-500, -500);
    this->showNormal();
    this->raise();
    this->activateWindow();
    QProcess p(0);
    p.startDetached("nm-connection-editor &");
    p.waitForStarted();
    return;
}

///////////////////////////////////////////////////////////////////////////////
//处理窗口变化、网络状态变化

//列表中item的扩展与收缩
void MainWindow::oneLanFormSelected(QString lanName, QString uniqueName)
{
    if (currSelNetName == uniqueName) {
        return;
    }
    QList<OneLancForm *> topLanList = topLanListWidget->findChildren<OneLancForm *>();
    QList<OneLancForm *> lanList = lanListWidget->findChildren<OneLancForm *>();

    //**********************先处理下方列表********************//
    // 下方所有元素回到原位
    for (int i = 0, j = 0;i < lanList.size(); i ++) {
        OneLancForm *ocf = lanList.at(i);
        if (ocf->isActive == true) {
            ocf->move(L_VERTICAL_LINE_TO_ITEM, 0);
        }
        if (ocf->isActive == false) {
            ocf->move(L_VERTICAL_LINE_TO_ITEM, j * H_NORMAL_ITEM);
            j ++;
        }
    }

    //是否与上一次选中同一个网络框
    if (currSelNetName == uniqueName) {
        // 缩小所有选项卡
//        for (int i = 0;i < lanList.size(); i ++) {
//            OneLancForm *ocf = lanList.at(i);
//            if (ocf->uuidName == uniqueName) {
//                ocf->setSelected(false, true);
//            } else {
//                ocf->setSelected(false, false);
//            }
//        }

//        currSelNetName = "";
    } else {
        int selectY = 0;
        for (int i = 0;i < lanList.size(); i ++) {
            OneLancForm *ocf = lanList.at(i);
            if (ocf->uuidName == uniqueName) {
                selectY = ocf->y(); //获取选中item的y坐标
                break;
            }
        }

        // 选中元素下面的所有元素下移 H_LAN_ITEM_EXTEND
        for (int i = 0;i < lanList.size(); i ++) {
            OneLancForm *ocf = lanList.at(i);
            if (ocf->y() > selectY) {
                ocf->move(L_VERTICAL_LINE_TO_ITEM, ocf->y() + H_LAN_ITEM_EXTEND);
            }
        }

        for (int i = 0;i < lanList.size(); i ++) {
            OneLancForm *ocf = lanList.at(i);
            if (ocf->uuidName == uniqueName) {
                ocf->setSelected(true, false);
                selectY = ocf->y();
            } else {
                ocf->setSelected(false, false);
            }
        }

        currSelNetName = uniqueName;
    }

    QList<OneLancForm *> itemList = lanListWidget->findChildren<OneLancForm *>();
    int n = itemList.size();
    if (n >= 1) {
        OneLancForm *lastItem = itemList.at(n-1);
        lastItem->setLine(false);
    }

    //**********************处理上方列表-界面所有控件回原位********************//
    int topLanNum = (currTopLanItem >= 1) ? currTopLanItem : 1;
    topLanListWidget->resize(W_TOP_LIST_WIDGET, H_NORMAL_ITEM*topLanNum + H_GAP_UP + X_ITEM); // 顶部的item缩小
    lbTopLanList->move(X_MIDDLE_WORD, H_NORMAL_ITEM*topLanNum + H_GAP_UP);
    btnCreateNet->move(X_BTN_FUN, Y_BTN_FUN + H_NORMAL_ITEM*(topLanNum-1));
    scrollAreal->move(W_LEFT_AREA, Y_SCROLL_AREA + H_NORMAL_ITEM*(topLanNum-1));
    lbNoItemTip->move(this->width()/2 - W_NO_ITEM_TIP/2 + W_LEFT_AREA/2, this->height()/2 + H_NORMAL_ITEM*(topLanNum-1)/2);

    for (int i = topLanList.size() - 1;i >= 0; i --) {
        OneLancForm *ocf = topLanList.at(i);
        ocf->setTopItem(false);
        ocf->move(L_VERTICAL_LINE_TO_ITEM, (topLanList.size() - 1 - i) * H_NORMAL_ITEM);
    }
}
void MainWindow::oneTopLanFormSelected(QString lanName, QString uniqueName)
{
    //应设计要求,选中同一选项卡不再缩小
    if (currSelNetName == uniqueName) return;
    QList<OneLancForm *> topLanList = topLanListWidget->findChildren<OneLancForm *>();
    QList<OneLancForm *> lanList = lanListWidget->findChildren<OneLancForm *>();

    // 顶部所有元素回到原位
    for (int i = topLanList.size() - 1;i >= 0; i --) {
        OneLancForm *ocf = topLanList.at(i);
        ocf->move(L_VERTICAL_LINE_TO_ITEM, (topLanList.size() - 1 - i) * H_NORMAL_ITEM);
    }

    if (currSelNetName == uniqueName) {
        // 与上一次选中同一个网络框,缩小当前选项卡
//        topLanListWidget->resize(W_TOP_LIST_WIDGET, H_NORMAL_ITEM*currTopLanItem + H_GAP_UP + X_ITEM);
//        lbTopLanList->move(X_MIDDLE_WORD, H_NORMAL_ITEM*currTopLanItem + H_GAP_UP);
//        btnCreateNet->move(X_BTN_FUN, Y_BTN_FUN + H_NORMAL_ITEM*(currTopLanItem-1));
//        scrollAreal->move(W_LEFT_AREA, Y_SCROLL_AREA + H_NORMAL_ITEM*(currTopLanItem-1));
//        lbNoItemTip->move(this->width()/2 - W_NO_ITEM_TIP/2 + W_LEFT_AREA/2, this->height()/2 + H_NORMAL_ITEM*(currTopLanItem-1)/2);

//        foreach (OneLancForm *ocf, topLanList) {
//            ocf->setTopItem(false);
//        }

//        currSelNetName = "";
    } else {
        // 没有与上一次选中同一个网络框,放大当前选项卡

        //下方列表所有元素缩小并回到原位
        for (int i = 0; i < lanList.size(); i ++) {
            OneLancForm *ocf = lanList.at(i);
            ocf->setSelected(false, false);
            ocf->move(L_VERTICAL_LINE_TO_ITEM, i*H_NORMAL_ITEM);
        }

        int topLanNum = (currTopLanItem >= 1) ? currTopLanItem : 1;
        topLanListWidget->resize(W_TOP_LIST_WIDGET, H_NORMAL_ITEM*topLanNum + H_LAN_ITEM_EXTEND + H_GAP_UP + X_ITEM);
        lbTopLanList->move(X_MIDDLE_WORD, H_NORMAL_ITEM*topLanNum + H_LAN_ITEM_EXTEND + H_GAP_UP);
        btnCreateNet->move(X_BTN_FUN, Y_BTN_FUN + H_LAN_ITEM_EXTEND + H_NORMAL_ITEM*(topLanNum-1));
        scrollAreal->move(W_LEFT_AREA, Y_SCROLL_AREA + H_LAN_ITEM_EXTEND + H_NORMAL_ITEM*(topLanNum-1));
        lbNoItemTip->move(this->width()/2 - W_NO_ITEM_TIP/2 + W_LEFT_AREA/2, this->height()/2 + 80 +  + H_NORMAL_ITEM*(topLanNum-1)/2);

        int selectY = 0;
        for (int i = 0;i < topLanList.size(); i ++) {
            OneLancForm *ocf = topLanList.at(i);
            if (ocf->uuidName == uniqueName) {
                selectY = ocf->y(); //获取选中item的y坐标
                break;
            }
        }

        // 选中元素下面的所有元素下移 H_LAN_ITEM_EXTEND
        for (int i = 0;i < topLanList.size(); i ++) {
            OneLancForm *ocf = topLanList.at(i);
            if (ocf->y() > selectY) {
                ocf->move(L_VERTICAL_LINE_TO_ITEM, ocf->y() + H_LAN_ITEM_EXTEND);
            }
        }

        //选中的元素进行扩展
        for (int i = 0;i < topLanList.size(); i ++) {
            OneLancForm *ocf = topLanList.at(i);
            if (ocf->uuidName == uniqueName) {
                ocf->setTopItem(true);
                selectY = ocf->y();
            } else {
                ocf->setTopItem(false);
            }
        }

        currSelNetName = uniqueName;
    }
}

void MainWindow::oneWifiFormSelected(QString wifibssid, int extendLength)
{
    if (currSelNetName == wifibssid) {
        //与win逻辑一致,点击同一选项不再缩小选项卡
        return;
    }
    if (extendLength == H_WIFI_ITEM_SMALL_EXTEND) {
        this->m_is_inputting_wifi_password = true;
    } else {
        this->m_is_inputting_wifi_password = false;
    }

    QList<OneConnForm *>topWifiList = topWifiListWidget->findChildren<OneConnForm *>();
    QList<OneConnForm *> wifiList = wifiListWidget->findChildren<OneConnForm *>();

    //******************先处理下方列表****************//
    // 下方所有元素回到原位
    for (int i = 0, j = 0;i < wifiList.size(); i ++) {
        OneConnForm *ocf = wifiList.at(i);
        if (ocf->isActive == true) {
            ocf->move(L_VERTICAL_LINE_TO_ITEM, 0);
        }
        if (ocf->isActive == false) {
            ocf->move(L_VERTICAL_LINE_TO_ITEM, j * H_NORMAL_ITEM);
            j ++;
        }
    }

    //是否与上一次选中同一个网络框
    if (currSelNetName == wifibssid) {
//        // 缩小所有选项卡
//        for (int i = 0;i < wifiList.size(); i ++) {
//            OneConnForm *ocf = wifiList.at(i);
//            if (ocf->wifiBSsid == wifibssid) {
//                if (ocf->wifiBSsid == hideWiFiConn) {
//                    ocf->setHideItem(true, true);
//                } else {
//                    ocf->setSelected(false, true);
//                }
//            } else {
//                if (ocf->wifiBSsid == hideWiFiConn) {
//                    ocf->setHideItem(true, true);
//                } else {
//                    ocf->setSelected(false, false);
//                }
//            }

//        }
//        currSelNetName = "";
    } else {
        int selectY = 0;
        for (int i = 0;i < wifiList.size(); i ++) {
            OneConnForm *ocf = wifiList.at(i);
            if (ocf->wifiBSsid == wifibssid) {
                selectY = ocf->y(); //获取选中item的y坐标
//                scrollAreaw->verticalScrollBar()->setValue(selectY);
                scrollAreaw->ensureVisible(ocf->x(), ocf->y() + extendLength);
                break;
            }
        }

        // 选中元素下面的所有元素下移 H_WIFI_ITEM_BIG_EXTEND
        for (int i = 0;i < wifiList.size(); i ++) {
            OneConnForm *ocf = wifiList.at(i);
            if (ocf->y() > selectY) {
                ocf->move(L_VERTICAL_LINE_TO_ITEM, ocf->y() + extendLength);
            }
        }

        for (int i = 0;i < wifiList.size(); i ++) {
            OneConnForm *ocf = wifiList.at(i);
            if (ocf->wifiBSsid == wifibssid) {
                if (ocf->wifiBSsid == hideWiFiConn) {
                    ocf->setHideItem(true, true);
                } else {
                    ocf->setSelected(true, false);
                }
            } else {
                if (ocf->wifiBSsid == hideWiFiConn) {
                    ocf->setHideItem(true, true);
                } else {
                    ocf->setSelected(false, false);
                }
            }
        }

        currSelNetName = wifibssid;
    }

    //最后一个item没有下划线
    QList<OneConnForm *> itemList = wifiListWidget->findChildren<OneConnForm *>();
    int n = itemList.size();
    if (n >= 1) {
        OneConnForm *lastItem = itemList.at(n-1);
        lastItem->setLine(false);
    }

    //********************处理上方列表-界面所有控件回原位******************//
    // 顶部的item缩小
    topWifiListWidget->resize(W_TOP_LIST_WIDGET, H_NORMAL_ITEM + H_GAP_UP + X_ITEM);
    lbTopWifiList->move(X_MIDDLE_WORD, H_NORMAL_ITEM + H_GAP_UP);
    btnAddNet->move(X_BTN_FUN, Y_BTN_FUN);
    scrollAreaw->move(W_LEFT_AREA, Y_SCROLL_AREA);
    lbNoItemTip->move(this->width()/2 - W_NO_ITEM_TIP/2 + W_LEFT_AREA/2, this->height()/2);

    OneConnForm *ocf = topWifiList.at(0);
    ocf->setTopItem(false);
}
void MainWindow::oneTopWifiFormSelected(QString wifibssid, int extendLength)
{
    //应设计师要求,此处展开后点击不再收起
    if (currSelNetName == wifibssid) return;
    QList<OneConnForm *>topWifiList = topWifiListWidget->findChildren<OneConnForm *>();
    QList<OneConnForm *> wifiList = wifiListWidget->findChildren<OneConnForm *>();

    if (currSelNetName == wifibssid) {
        // 与上一次选中同一个网络框,缩小当前选项卡
//        topWifiListWidget->resize(W_TOP_LIST_WIDGET, H_NORMAL_ITEM + H_GAP_UP + X_ITEM);
//        lbTopWifiList->move(X_MIDDLE_WORD, H_NORMAL_ITEM + H_GAP_UP);
//        btnAddNet->move(X_BTN_FUN, Y_BTN_FUN);
//        scrollAreaw->move(W_LEFT_AREA, Y_SCROLL_AREA);
//        lbNoItemTip->move(this->width()/2 - W_NO_ITEM_TIP/2 + W_LEFT_AREA/2, this->height()/2);

//        OneConnForm *ocf = topWifiList.at(0);
//        ocf->setTopItem(false);

//        currSelNetName = "";
    } else {
        // 没有与上一次选中同一个网络框,放大当前选项卡

        for(int i = 0;i < wifiList.size(); i ++) {
            // 所有元素回到原位
            OneConnForm *ocf = wifiList.at(i);
            ocf->setSelected(false, false);
            ocf->move(L_VERTICAL_LINE_TO_ITEM, i * H_NORMAL_ITEM);
        }

        topWifiListWidget->resize(W_TOP_LIST_WIDGET, H_NORMAL_ITEM + H_WIFI_ITEM_BIG_EXTEND + H_GAP_UP + X_ITEM);
        lbTopWifiList->move(X_MIDDLE_WORD, H_NORMAL_ITEM + H_WIFI_ITEM_BIG_EXTEND + H_GAP_UP);
        btnAddNet->move(X_BTN_FUN, Y_BTN_FUN + H_WIFI_ITEM_BIG_EXTEND);
        scrollAreaw->move(W_LEFT_AREA, Y_SCROLL_AREA + H_WIFI_ITEM_BIG_EXTEND);
        lbNoItemTip->move(this->width()/2 - W_NO_ITEM_TIP/2 + W_LEFT_AREA/2, this->height()/2 + 65);

        OneConnForm *ocf = topWifiList.at(0);
        ocf->setTopItem(true);

        currSelNetName = wifibssid;
    }
}

//断开网络处理
void MainWindow::handleLanDisconn()
{
    //syslog(LOG_DEBUG, "Wired net is disconnected");
    qDebug()<<"Wired net is disconnected!";

    QString txt(tr("Wired net is disconnected"));
    emit this->actWiredConnectionChanged();
    objKyDBus->showDesktopNotify(txt);
    currSelNetName = "";
    oldActLanName = "";
    oldDbusActLanDNS = 0;
    emit this->waitLanStop();
    this->ksnm->execGetLanList();
}

void MainWindow::handleWifiDisconn()
{
    hasWifiConnected = false;
    currSelNetName = "";
    this->ksnm->execGetWifiList(this->wcardname);
    QtConcurrent::run([=]() {
        handleWifiDisconnLoading();
    });
}
void MainWindow::handleWifiDisconnLoading()
{
    //syslog(LOG_DEBUG, "WLAN is disconnected");
    qDebug()<<"WLAN is disconnected!";
    QString txt(tr("WLAN is disconnected"));
    objKyDBus->showDesktopNotify(txt);

    //setTrayLoading(false);
    //getActiveInfoAndSetTrayIcon();
}

//网络开关处理,打开与关闭网络
void MainWindow::enNetDone()
{
//    BackThread *bt = new BackThread();
//    mwBandWidth = bt->execChkLanWidth(lcardname);

    // 打开网络开关时如果Wifi开关是打开的,设置其样式
    if (checkWlOn()) {
        objKyDBus->oldWifiSwitchState = true;
        btnWireless->setSwitchStatus(true);
    }

    onBtnNetListClicked(1);
    is_stop_check_net_state = 0;
    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
    qDebug()<<"debug: already turn on the switch of lan network";
    //syslog(LOG_DEBUG, "Already turn on the switch of lan network");
}
void MainWindow::disNetDone()
{
    this->is_btnLanList_clicked = 1;
    this->is_btnWifiList_clicked = 0;

    ui->lbNetListBG->setStyleSheet(btnOnQss);
    ui->lbWifiListBG->setStyleSheet(btnOffQss);

    ui->lbNetwork->setText(tr("Ethernet"));
    btnWireless->hide();
    btnWired->show();

    delete topLanListWidget; // 清空top列表
    createTopLanUI(); //创建顶部有线网item

    // 清空lan列表
    lanListWidget = new QWidget(scrollAreal);
    lanListWidget->resize(W_LIST_WIDGET, H_NORMAL_ITEM + H_LAN_ITEM_EXTEND);
    scrollAreal->setWidget(lanListWidget);
    scrollAreal->move(W_LEFT_AREA, Y_SCROLL_AREA);

    // 当前连接的lan
    OneLancForm *ccf = new OneLancForm(topLanListWidget, this, confForm, ksnm);
    ccf->setLanName(tr("Not connected"), tr("Not connected"), "--", "--");//"当前未连接任何 以太网"
    ccf->setIcon(false);
    ccf->setConnedString(1, tr("Disconnected"), "");//"未连接"
    ccf->isConnected = false;
    ccf->setTopItem(false);
    ccf->setAct(true);
    ccf->move(L_VERTICAL_LINE_TO_ITEM, 0);
    ccf->show();

    objKyDBus->oldWifiSwitchState = false;
    btnWireless->setSwitchStatus(false);

    this->lanListWidget->show();
    this->wifiListWidget->hide();
    this->scrollAreal->show();
    this->topLanListWidget->show();
    this->scrollAreaw->hide();
    this->topWifiListWidget->hide();

    qDebug()<<"debug: already turn off the switch of lan network";
    //syslog(LOG_DEBUG, "Already turn off the switch of lan network");

    this->stopLoading();
}

void MainWindow::enWifiDone()
{
    if (isHuaWeiPC) {
        //这里先不做什么,因为onRfkillStatusChanged这个函数也在处理wifi开关
        qDebug()<<"enWifiDone is hua wei pc, no thing to do!";
    } else {
        //on_btnWifiList_clicked();
        current_wifi_list_state = LOAD_WIFI_LIST;
        if (is_btnWifiList_clicked) {
            this->ksnm->execGetWifiList(this->wcardname);
        } else {
            stopLoading();
        }
        objKyDBus->getWirelessCardName();
        qDebug()<<"debug: already turn on the switch of wifi network";
        syslog(LOG_DEBUG, "Already turn on the switch of wifi network");
    }
}
void MainWindow::disWifiDone()
{
    if (isHuaWeiPC) {
        //这里先不做什么,因为onRfkillStatusChanged这个函数也在处理wifi开关
        qDebug()<<"disWifiDone is hua wei pc, no thing to do!";
    } else {
        disWifiDoneChangeUI();
        this->stopLoading();
        is_stop_check_net_state = 0;
        qDebug()<<"debug: already turn off the switch of wifi network";
        syslog(LOG_DEBUG, "Already turn off the switch of wifi network");
    }
}
void MainWindow::disWifiStateKeep()
{
    if (this->is_btnLanList_clicked == 1) {
        objKyDBus->oldWifiSwitchState = false;
        btnWireless->setSwitchStatus(false);
    }
    if (this->is_btnWifiList_clicked== 1) {
        disWifiDoneChangeUI();
        getActiveInfoAndSetTrayIcon();
    }
}
void MainWindow::disWifiDoneChangeUI()
{
    wifiListWidget = new QWidget(scrollAreaw);
    wifiListWidget->resize(W_LIST_WIDGET, H_WIFI_ITEM_BIG_EXTEND);
    scrollAreaw->setWidget(wifiListWidget);
    scrollAreaw->move(W_LEFT_AREA, Y_SCROLL_AREA);

    lbTopWifiList->move(X_MIDDLE_WORD, H_NORMAL_ITEM + H_GAP_UP);
    btnAddNet->move(X_BTN_FUN, Y_BTN_FUN);
    topWifiListWidget->resize(W_TOP_LIST_WIDGET, H_NORMAL_ITEM + H_GAP_UP + X_ITEM);

    QList<OneConnForm *> wifiList = topWifiListWidget->findChildren<OneConnForm *>();
    for (int i = 0; i < wifiList.size(); i ++) {
        OneConnForm *ocf = wifiList.at(i);
        if (ocf->isActive == true) {
            ocf->setSelected(false, false);
            ocf->setWifiName(tr("Not connected"), "--", "--", "--", isHuaWeiPC, isHuaWei9006C);//"当前未连接任何 Wifi"
            ocf->setSignal("0", "--", "0", false);
            ocf->setConnedString(1, tr("Disconnected"), "");//"未连接"
            ocf->lbFreq->hide();
            lbLoadDown->hide();
            lbLoadUp->hide();
            lbLoadDownImg->hide();
            lbLoadUpImg->hide();
            ocf->isConnected = false;
            ocf->setTopItem(false);
            disconnect(ocf, SIGNAL(selectedOneWifiForm(QString,int)), this, SLOT(oneTopWifiFormSelected(QString,int)));
        } else {
            ocf->deleteLater();
        }
    }

    objKyDBus->oldWifiSwitchState = false;
    btnWireless->setSwitchStatus(false);

    this->lanListWidget->hide();
    this->topLanListWidget->hide();
    this->wifiListWidget->show();
    this->topWifiListWidget->show();
    this->scrollAreal->hide();
    this->scrollAreaw->show();
}

void MainWindow::on_btnHotspotState()
{
    ui->lbHotImg->setStyleSheet("QLabel{background-image:url(:/res/x/hot-spot-off.svg);}");
    ui->lbHotBG->setStyleSheet(btnOffQss);
    is_hot_sopt_on = 0;
}

//执行wifi的重新连接
void MainWindow::toReconnectWifi()
{
    if (ifCanReconnectWifiNow) {
        if (isHuaWeiPC) {
            qDebug() << "Execute reconnect wifi now, first get wifi list then reconnect";
            current_wifi_list_state = RECONNECT_WIFI;
            this->ksnm->execGetWifiList(this->wcardname);
        }
    } else {
        qDebug() << "Execute reconnect wifi now or wlan already connected, stop to exec if another request launched";
    }

}

//处理外界对网络的连接与断开
void MainWindow::onExternalConnectionChange(QString type, bool isConnUp)
{
    isReconnectingWifi = false;

    if ( (type == "802-11-wireless" || type == "wifi") && !isConnUp ){
        QTimer::singleShot(2*1000, this, SLOT(onToResetValue() ));
    }
    if (type == "802-11-wireless" || type == "wifi") {
        addNumberForWifi += 1;
    }

    if (addNumberForWifi == 2) {
        //断开一个wifi的时候,如果存在回连,可能接连发出两个信号
        //当连续发出wifi断开与连接的信号时,短时间内addNumberForWifi值为2
        is_stop_check_net_state = 1;
        qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
        if (is_connect_net_failed) {
            qDebug()<<"debug: connect wifi failed just now, no need to refresh wifi interface";
            is_connect_net_failed = 0;
            is_stop_check_net_state = 0;
            qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
        } else if (is_wifi_reconnected) {
            qDebug()<<"debug: wifi reconnected just now, no need to refresh wifi interface";
            is_wifi_reconnected = 0;
            is_stop_check_net_state = 0;
            qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
        }else {
            QTimer::singleShot(2*1000, this, SLOT(onExternalWifiChange() ));
        }
        addNumberForWifi = 0;
        return;
    }

    if ( (type == "802-11-wireless" || type == "wifi") && isConnUp ){
        addNumberForWifi = 0;
    }

    QTimer::singleShot(4*1000, this, SLOT(onToSetTrayIcon() ));

    if (type == "") {
        is_stop_check_net_state = 0;
        qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
        return;
    }

    if (isToSetLanValue) {
        if (type == "802-3-ethernet" || type == "ethernet") {
            isLanBeConnUp = true;
            isLanBeConnUp = isConnUp;
        }
    }

    if (isToSetWifiValue) {
        if (type == "802-11-wireless" || type == "wifi") {
            isWifiBeConnUp = true;
            isWifiBeConnUp = isConnUp;
        }
    }

    if (!is_connect_hide_wifi && !is_stop_check_net_state) {
        is_stop_check_net_state = 1;
        qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;

        if (type == "802-3-ethernet" || type == "ethernet") {
            if (is_connect_net_failed) {
                qDebug()<<"debug: connect wired network failed, no need to refresh wired interface";
                is_connect_net_failed = 0;
                is_stop_check_net_state = 0;
                qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
            } else {
                isToSetLanValue = false;
                QTimer::singleShot(2*1000, this, SLOT(onExternalLanChange() ));
            }
        }

        if (type == "802-11-wireless" || type == "wifi") {
            addNumberForWifi = 0;
            if (is_connect_net_failed) {
                qDebug()<<"debug: connect wifi failed just now, no need to refresh wifi interface";
                is_connect_net_failed = 0;
                is_stop_check_net_state = 0;
                qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
            } else if (is_wifi_reconnected) {
                qDebug()<<"debug: wifi reconnected just now, no need to refresh wifi interface";
                is_wifi_reconnected = 0;
                is_stop_check_net_state = 0;
                qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
            }else {
                isToSetWifiValue = false;
                QTimer::singleShot(2*1000, this, SLOT(onExternalWifiChange() ));
            }
        }
    }
}

void MainWindow::onExternalLanChange()
{
    emit this->actWiredConnectionChanged();
    if (is_btnLanList_clicked) {
        onBtnNetListClicked(0);
    } else {
        is_stop_check_net_state = 0;
        qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
    }

    isToSetLanValue = true;
}

void MainWindow::onExternalWifiChange()
{
    if (!isWifiBeConnUp) {
        QString txt(tr("WiFi already disconnect"));
        objKyDBus->showDesktopNotify(txt);
    }
    if (isWifiBeConnUp) {
        QString txt(tr("WiFi already connected external"));
        objKyDBus->showDesktopNotify(txt);
    }
    if (m_connected_by_self) {
        is_stop_check_net_state = 0;
        qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
        m_connected_by_self = false;
        return;
    }

    if (is_btnWifiList_clicked) {
         on_btnWifiList_clicked();
    } else {
        is_stop_check_net_state = 0;
        qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
    }

    isToSetWifiValue = true;
}

void MainWindow::onToSetTrayIcon()
{
    getActiveInfoAndSetTrayIcon();
}

void MainWindow::onToResetValue()
{
    addNumberForWifi = 0;
}

void MainWindow::onWifiSwitchChange()
{
    if (is_btnWifiList_clicked) {
        //this->ksnm->execGetWifiList();
        on_btnWifiList_clicked();
    }
}

//处理外界对wifi开关的打开与关闭
void MainWindow::onExternalWifiSwitchChange(bool wifiEnabled)
{
    if (!is_stop_check_net_state) {
        is_stop_check_net_state = 1;
        qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
        if (wifiEnabled) {
            qDebug()<<"debug: external wifi switch turn on";
            syslog(LOG_DEBUG, "debug: external wifi switch turn on");
            QTimer::singleShot(4*1000, this, SLOT(onWifiSwitchChange() ));
            objKyDBus->setWifiSwitchState(true);
        } else {
            qDebug()<<"debug: external wifi switch turn off";
            syslog(LOG_DEBUG, "debug: external wifi switch turn off");
            QTimer::singleShot(3*1000, this, SLOT(onWifiSwitchChange() ));
            objKyDBus->setWifiSwitchState(false);//通知控制面板wifi开关已经关闭
        }
    }
}


///////////////////////////////////////////////////////////////////////////////
//循环处理部分,目前仅on_checkWifiListChanged 与on_setNetSpeed两个函数在运行

void MainWindow::onRequestScanAccesspoint()
{
    if (isHuaWeiPC) {
        numberForWifiScan += 1;
        int numScanPerSecond0 = 0;
        int numScanPerSecond1 = 0;
        if (numberForWifiScan > 12) {
            numScanPerSecond0 = (numberForWifiScan-12)/6; //获取除数
            numScanPerSecond1 = (numberForWifiScan-12)%6; //获取余数
        }

        if (is_init_wifi_list || is_connect_hide_wifi) {
            return; //遇到启动软件第一次加载wifi列表的时候,或正在连接隐藏wifi,停止更新
        }

        if (is_stop_check_net_state==0 && this->is_btnWifiList_clicked==1) {
            if (this->isVisible()) {
                this->toScanWifi(1);
            } else {
                switch (numberForWifiScan) {
                case 1 :
                    this->toScanWifi(0);
                    break;
                case 2:
                    this->toScanWifi(0);
                    break;
                case 3:
                    this->toScanWifi(0);
                    break;
                case 6:
                    this->toScanWifi(0);
                    break;
                case 9:
                    this->toScanWifi(0);
                    break;
                case 12:
                    this->toScanWifi(0);
                    break;
                }

                if ((numScanPerSecond0 != 0) && (numScanPerSecond1 == 0)) {
                    this->toScanWifi(0);
                }
            } // end if(this->isVisible())
        }

        if (numberForWifiScan > 500) numberForWifiScan = 0;
    } else {
        if (is_init_wifi_list || is_connect_hide_wifi) {
            return; //遇到启动软件第一次加载wifi列表的时候,或正在连接隐藏wifi,停止更新
        }

        if (is_stop_check_net_state==0 && this->is_btnWifiList_clicked==1 && this->isVisible()) {
            BackThread *loop_bt = new BackThread();
            IFace *loop_iface = loop_bt->execGetIface();

            if (loop_iface->wstate != 2) {
                current_wifi_list_state = UPDATE_WIFI_LIST;
                this->ksnm->execGetWifiList(this->wcardname); //更新wifi列表
            }

            delete loop_iface;
            loop_bt->deleteLater();
        }
    }
}

void MainWindow::toScanWifi(bool isShow)
{
    QtConcurrent::run([=](){
        BackThread *loop_bt = new BackThread();
        IFace *loop_iface = loop_bt->execGetIface();

        if (loop_iface->wstate != 2) {
            if (loop_iface->wstate == 0) {
                if (isShow) {
                    objKyDBus->requestScanWifi(); //要求后台扫描AP
                }
            }

            if (loop_iface->wstate == 1) {
                objKyDBus->requestScanWifi(); //要求后台扫描AP
            }

            sleep(2);
            if (isShow) {
                //只有在显示窗口时更新wifi列表
                emit this->refreshWifiListAfterScan();
            }
        }

        delete loop_iface;
        loop_bt->deleteLater();
    });
}

void MainWindow::onRefreshWifiListAfterScan()
{
    current_wifi_list_state = REFRESH_WIFI;
    this->ksnm->execGetWifiList(this->wcardname); //更新wifi列表
}

void MainWindow::onRequestReconnecWifi()
{
    qDebug() << "Receive request reconnect wifi signal, then to reconnect wifi";
    toReconnectWifi();
}

void MainWindow::on_setNetSpeed()
{
    if (this->isVisible() && is_stop_check_net_state==0) {
        if (is_btnWifiList_clicked == 1) {
            if ( objNetSpeed->getCurrentDownloadRates(objKyDBus->dbusWiFiCardName.toUtf8().data(), &start_rcv_rates, &start_tx_rates) == -1) {
                start_rcv_rates = end_rcv_rates;
            }
        }
        else if(is_btnLanList_clicked == 1) {
            if ( objNetSpeed->getCurrentDownloadRates(currConnIfname.toUtf8().data(), &start_rcv_rates, &start_tx_rates) == -1) {
                start_tx_rates = end_tx_rates;
            }
        }
        long int delta_rcv = (start_rcv_rates - end_rcv_rates)/1024;
        long int delta_tx = (start_tx_rates - end_tx_rates)/1024;

        //简易滤波
        if (delta_rcv < 0 || delta_tx < 0) {
            delta_rcv = 0;
            delta_tx = 0;
        }
        else if (end_rcv_rates == 0 || end_tx_rates == 0){
            delta_rcv = 0;
            delta_tx = 0;
        }

        end_rcv_rates = start_rcv_rates;
        end_tx_rates = start_tx_rates;

        int rcv_num = delta_rcv;
        int tx_num = delta_tx;

        QString str_rcv;
        QString str_tx;

        if (rcv_num < 1024) {
            str_rcv = QString::number(rcv_num) + "KB/s.";
        } else {
            int remainder;
            if (rcv_num%1024 < 100) {
                remainder = 0;
            } else {
                remainder = (rcv_num%1024)/100;
            }
            str_rcv = QString::number(rcv_num/1024) + "."  + QString::number(remainder) + "MB/s.";
        }

        if (tx_num < 1024) {
            str_tx = QString::number(tx_num) + "KB/s";
        } else {
            int remainder;
            if (tx_num%1024 < 100) {
                remainder = 0;
            } else {
                remainder = (tx_num%1024)/100;
            }
            str_tx = QString::number(tx_num/1024) + "."  + QString::number(remainder) + "MB/s";
        }

        lbLoadDown->setText(str_rcv);
        lbLoadUp->setText(str_tx);

        int offset = str_rcv.length() * 7;
        lbLoadUp->move(X_ITEM + offset + 145, Y_TOP_ITEM + 32);
        lbLoadUpImg->move(X_ITEM + offset + 128, Y_TOP_ITEM + 35);
    }
    else {
        end_rcv_rates = 0;
        end_tx_rates = 0;
    }
}

void MainWindow::connLanDone(int connFlag)
{
    emit this->waitLanStop(); //停止加载动画

    // Lan连接结果,0点击连接成功 1因网线未插入失败 2因mac地址匹配不上失败 3开机启动网络工具时已经连接
    if (connFlag == 0) {
        //syslog(LOG_DEBUG, "Wired net already connected by clicking button");
        qDebug()<<"Wired net already connected by clicking button!";
        //this->ksnm->execGetLanList();

        QString txt(tr("Conn Ethernet Success"));
        emit this->actWiredConnectionChanged();
        objKyDBus->showDesktopNotify(txt);
    }

    if (connFlag == 1) {
        //syslog(LOG_DEBUG, "without network line connect to computer.");
        qDebug()<<"without network line connect to computer.!";

        QString txt(tr("Without Lan Cable"));
        objKyDBus->showDesktopNotify(txt);
    }

    if (connFlag == 2) {
        qDebug()<<"Conn Ethernet Success";
        //syslog(LOG_DEBUG, "Conn Ethernet Success");
        this->ksnm->execGetLanList();
        QString txt(tr("Conn Ethernet Success"));
        objKyDBus->showDesktopNotify(txt);
    }

    if (connFlag == 3) {
        qDebug()<<"Launch kylin-nm, wired network already connected";
        //syslog(LOG_DEBUG, "Launch kylin-nm, wired network already connected");
    }

    if (connFlag == 4) {
        qDebug()<<"IP configuration could not be reserved";
        //syslog(LOG_DEBUG, "IP configuration could not be reserved");
        is_connect_net_failed = 1;
        QString txt(tr("IP configuration could not be reserved"));
        objKyDBus->showDesktopNotify(txt);
    }

    if (connFlag == 5) {
        //syslog(LOG_DEBUG, "The MACs of the device and the connection do not match.");
        qDebug()<<"The MACs of the device and the connection do not match.";
        is_connect_net_failed = 1;
        QString txt(tr("MAC Address Mismatch"));
        objKyDBus->showDesktopNotify(txt);
    }

    if (connFlag == 6) {
        //syslog(LOG_DEBUG, "Connection Be Killed");
        qDebug()<<"Connection Be Killed";
        is_connect_net_failed = 1;
        QString txt(tr("Connection Be Killed"));
        objKyDBus->showDesktopNotify(txt);
    }

    if (connFlag == 7) {
        qDebug()<<"Connect Bluetooth Network Failed";
        //syslog(LOG_DEBUG, "Connect Bluetooth Network Failed");
        is_connect_net_failed = 1;
        QString txt(tr("Connect Bluetooth Network Failed"));
        objKyDBus->showDesktopNotify(txt);
    }

    if (connFlag == 8) {
        qDebug()<<"Carrier/link changed";
        //syslog(LOG_DEBUG, "Carrier/link changed");
        is_connect_net_failed = 1;
        QString txt(tr("Carrier/link changed"));
        objKyDBus->showDesktopNotify(txt);
    }

    if (connFlag == 9) {
        qDebug()<<"Connect Wired Network Failed";
        //syslog(LOG_DEBUG, "Connect Wired Network Failed");
        is_connect_net_failed = 1;
        QString txt(tr("Connect Wired Network Failed"));
        objKyDBus->showDesktopNotify(txt);
    }

    if (connFlag == 10) {
        qDebug()<<"Connect VPN Network Failed";
        is_connect_net_failed = 1;
        QString txt(tr("Connect VPN Network Failed"));
        objKyDBus->showDesktopNotify(txt);
    }

    this->stopLoading();
    this->is_stop_check_net_state = 0;
    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
}

void MainWindow::connWifiDone(int connFlag)
{
    // Wifi连接结果,0点击连接成功 1失败 2没有配置文件 3开机启动网络工具时已经连接 4密码错误 5找不到已保存的网络
    if (connFlag == 0) {
        //syslog(LOG_DEBUG, "Wi-Fi already connected by clicking button");
        qDebug()<<"Wi-Fi connected succeed. res=0";
        if (!isHuaWeiPC) {
            //如果不是华为电脑,使用获取连接信号的方式更新列表
            this->ksnm->execGetWifiList(this->wcardname);
        } else {
            //如果是华为电脑,连接wifi后判断到portal网络弹出认证框
            WifiAuthThread *wifi_auth_thread=new WifiAuthThread();
            wifi_auth_thread->start();
        }

        QString txt(tr("Conn WLAN Success"));
        objKyDBus->showDesktopNotify(txt);
    } else if (connFlag == 1) {
        //syslog(LOG_DEBUG, "Confirm your WLAN password or usable of wireless card");
        qWarning()<<"Wi-Fi connected failed. res=1";
        //is_stop_check_net_state = 0;
        is_connect_net_failed = 1;
        is_connect_hide_wifi = 0;
        QString txt(tr("Confirm your WLAN password or usable of wireless card"));
        objKyDBus->showDesktopNotify(txt);
    } else if (connFlag == 3) {
        //syslog(LOG_DEBUG, "Launch kylin-nm, Wi-Fi already connected");
        qWarning()<<"Wi-Fi connected failed. res=3";
    } else if (connFlag == 4) {
        //syslog(LOG_DEBUG, "Confirm your WLAN password");
        is_connect_net_failed = 1;
        QString txt(tr("Confirm your WLAN password"));
        qWarning()<<"Wi-Fi connected failed. res=4";
        objKyDBus->showDesktopNotify(txt);
    } else if (connFlag == 5) {
        //syslog(LOG_DEBUG, "Selected WLAN has not been scanned.");
        is_connect_net_failed = 1;
        QString txt(tr("Selected WLAN has not been scanned."));
        qWarning()<<"Wi-Fi connected failed. res=5";
        objKyDBus->showDesktopNotify(txt);
    } else if (connFlag == 6) {
        //syslog(LOG_DEBUG, "Connect Hidden WLAN Success.");
        this->ksnm->execGetWifiList(this->wcardname);
        QString txt(tr("Connect Hidden WLAN Success"));
        qWarning()<<"Connect Hidden WLAN Success. res=6";
        objKyDBus->showDesktopNotify(txt);
    } else if (connFlag == 7) {
        //syslog(LOG_DEBUG, "Secrets were required, but not provided. res=7");
        qWarning()<<"Secrets were required, but not provided. res=7";
        is_connect_net_failed = 1;
        QString txt(tr("Confirm your WLAN password"));
        objKyDBus->showDesktopNotify(txt);
    } else if (connFlag == 8) {
        //syslog(LOG_DEBUG, "Error: 802-11-wireless-security.psk: ????. res=8");
        qWarning()<<"Error: 802-11-wireless-security.psk: ????. res=8";
        is_connect_net_failed = 1;
        QString txt(tr("Confirm your WLAN password"));
        objKyDBus->showDesktopNotify(txt);
    } else if (connFlag == 9) {
        //syslog(LOG_DEBUG, "Passwords or encryption keys are required to access the wireless network. res=9");
        qWarning()<<"Passwords or encryption keys are required to access the wireless network. res=9";
        is_connect_net_failed = 1;
        QString txt(tr("Confirm your WLAN password"));
        objKyDBus->showDesktopNotify(txt);
    }

    this->stopLoading();
    is_stop_check_net_state = 0;
    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
}

void MainWindow::onRequestRefreshWifiList()
{
    this->ksnm->execGetWifiList(this->wcardname);
}

//重新绘制背景色
void MainWindow::paintEvent(QPaintEvent *event)
{
    double trans = objKyDBus->getTransparentData();

    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    QRect rect = this->rect();
    p.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    p.setBrush(opt.palette.color(QPalette::Base));
    p.setOpacity(trans);
    p.setPen(Qt::NoPen);
    p.drawRoundedRect(rect, 6, 6);
    QWidget::paintEvent(event);
}

bool MainWindow::event(QEvent *event)
{
    if (event->type() == QEvent::ActivationChange) {
        if (QApplication::activeWindow() != this) {
            this->m_is_inputting_wifi_password = false;
            this->hide();
            numberForWifiScan = 0;
        }
    }

    return QMainWindow::event(event);
}

///////////////////////////////////////////////////////////////////////////////
QPixmap MainWindow::drawSymbolicColoredPixmap(const QPixmap &source)
{
    QColor gray(128,128,128);
    QColor standard (31,32,34);
    QImage img = source.toImage();
    for (int x = 0; x < img.width(); x++) {
        for (int y = 0; y < img.height(); y++) {
            auto color = img.pixelColor(x, y);
            if (color.alpha() > 0) {
                if (qAbs(color.red()-gray.red())<20 && qAbs(color.green()-gray.green())<20 && qAbs(color.blue()-gray.blue())<20) {
                    color.setRed(255);
                    color.setGreen(255);
                    color.setBlue(255);
                    img.setPixelColor(x, y, color);
                } else if(qAbs(color.red()-standard.red())<20 && qAbs(color.green()-standard.green())<20 && qAbs(color.blue()-standard.blue())<20) {
                    color.setRed(255);
                    color.setGreen(255);
                    color.setBlue(255);
                    img.setPixelColor(x, y, color);
                } else {
                    img.setPixelColor(x, y, color);
                }
            }
        }
    }
    return QPixmap::fromImage(img);
}

QPixmap MainWindow::drawSymbolicBlackColoredPixmap(const QPixmap &source)
{
    QImage img = source.toImage();
    for (int x = 0; x < img.width(); x++) {
        for (int y = 0; y < img.height(); y++) {
            auto color = img.pixelColor(x, y);
            if (color.alpha() > 0) {
                if (qAbs(color.red())>=200 && qAbs(color.green())>=200 && qAbs(color.blue())>=200) {
                    color.setRed(56);
                    color.setGreen(56);
                    color.setBlue(56);
                    img.setPixelColor(x, y, color);
                }
            }
        }
    }
    return QPixmap::fromImage(img);
}

const QPixmap MainWindow::loadSvg(const QString &fileName, const int size)
{
    QPixmap pixmap(size, size);
    QSvgRenderer renderer(fileName);
    pixmap.fill(Qt::transparent);

    QPainter painter;
    painter.begin(&pixmap);
    renderer.render(&painter);
    painter.end();

    return pixmap;
}

void MainWindow::getSystemFontFamily()
{
    qDebug()<<"Init Font Gsettings...";
    const QByteArray id("org.ukui.style");
    QGSettings * fontSetting = new QGSettings(id, QByteArray(), this);
    connect(fontSetting, &QGSettings::changed,[=](QString key) {
        if ("systemFont" == key || "systemFontSize" ==key) {
            QFont font = this->font();
            int width = font.pointSize();
            for (auto widget : qApp->allWidgets()) {
                widget->setFont(font);
            }
        }
    });
}

///////////////////////////////////////////////////////////////////////////////
//解决双屏幕问题用到的dbus方法
void MainWindow::PrimaryManager()
{
    qDebug()<<"Init primary settings...";

    isHuaWeiPC = false;
    isHuaWei9006C = false;

#if 0
    //QDBusConnection conn = QDBusConnection::sessionBus();
    mDbusXrandInter = new QDBusInterface(DBUS_NAME,
                                         DBUS_PATH,
                                         DBUS_INTERFACE,
                                         QDBusConnection::sessionBus());

    isHuaWeiPC = false;
    isHuaWei9006C = false;
    QDBusMessage message = QDBusMessage::createMethodCall(DBUS_NAME,
                               DBUS_PATH,
                               DBUS_INTERFACE,
                               "height");
    QDBusMessage response = QDBusConnection::sessionBus().call(message);
    if (response.type() == QDBusMessage::ReplyMessage) {
        isHuaWeiPC = true;

        //继续判断是990或者9006c
        QProcess * processCpuinfo = new QProcess(this);
        processCpuinfo->start(QString("cat /proc/cpuinfo"));
        connect(processCpuinfo, static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished), this, [ = ]() {
            processCpuinfo->deleteLater();
        });
        connect(processCpuinfo, &QProcess::readyReadStandardOutput, this, [ = ]() {
            QString ctrCpuinfo = processCpuinfo->readAllStandardOutput();
            if (ctrCpuinfo.indexOf("HUAWEI Kirin 9006C") != -1) {
                isHuaWei9006C = true;
            }
        });
        processCpuinfo->waitForFinished();
    }

    connect(mDbusXrandInter, SIGNAL(screenPrimaryChanged(int,int,int,int)),
            this, SLOT(priScreenChanged(int,int,int,int)));
#endif
}

void MainWindow::toStart()
{
    qDebug()<<"Init geometry...";
    m_priX = getScreenGeometry("x");
    m_priY = getScreenGeometry("y");
    m_priWid = getScreenGeometry("width");
    m_priHei = getScreenGeometry("height");
    if (m_priWid == 0 || m_priHei == 0) {
        QRect rect = QApplication::desktop()->screenGeometry(0);
        m_priX=rect.x();
        m_priY=rect.y();
        m_priWid=rect.width();
        m_priHei=rect.height();
    }

    //qDebug("Start: Primary screen geometry is x=%d, y=%d, windth=%d, height=%d", m_priX, m_priY, m_priWid, m_priHei);
}

int MainWindow::getScreenGeometry(QString methodName)
{
    int res = 0;
    QDBusMessage message = QDBusMessage::createMethodCall(DBUS_NAME,
                               DBUS_PATH,
                               DBUS_INTERFACE,
                               methodName);
    QDBusMessage response = QDBusConnection::sessionBus().call(message);
    if (response.type() == QDBusMessage::ReplyMessage)
    {
        if(!response.arguments().isEmpty()) {
            int value = response.arguments().takeFirst().toInt();
            res = value;
            qDebug() << value;
        }
    } else {
        qDebug()<< "to get message about "<< methodName<<" of screen failed";
        //syslog(LOG_DEBUG, "to get message about %s of screen failed", methodName.toUtf8().data());
    }
    return res;
}

void MainWindow::requestRefreshWifiList()
{
    if (isHuaWeiPC) {
        QtConcurrent::run([=](){
            qDebug()<<"Received signal to refresh wifi from ukcc, current_wifi_list_state="<<current_wifi_list_state;
            objKyDBus->requestScanWifi(); //要求后台扫描AP
            sleep(2);
            emit refreshWifiListAfterScan();
        });
    } else {
        current_wifi_list_state = REFRESH_WIFI;
        qDebug()<<"Received signal to refresh wifi from ukcc, current_wifi_list_state="<<current_wifi_list_state;
        this->ksnm->execGetWifiList(this->wcardname);
    }
}

/* get primary screen changed */
void MainWindow::priScreenChanged(int x, int y, int width, int height)
{
    m_priX = x;
    m_priY = y;
    m_priWid = width;
    m_priHei = height;
    qDebug("primary screen  changed, geometry is  x=%d, y=%d, windth=%d, height=%d", x, y, width, height);
    //syslog(LOG_DEBUG, "primary screen  changed, geometry is  x=%d, y=%d, windth=%d, height=%d", x, y, width, height);
}

// 通过kds的dbus发现rfkill状态变化
void MainWindow::onRfkillStatusChanged()
{
    qDebug() << "收到信号了,开始执行函数onRfkillStatusChanged()";
    if (canExecHandleWifiSwitchChange) {
        canExecHandleWifiSwitchChange = false;
        qDebug() << "收到信号了,开始处理wifi开关的处理问题";
        if (isHuaWeiPC) {
            QDBusReply<int> reply = kdsDbus->call("getCurrentWlanMode");
            if (reply.isValid()){
                int current = reply.value();

                if (current == -1){
                    qWarning("Error Occur When Get Current WlanMode");
                    //syslog(LOG_DEBUG, "Error Occur When Get Current WlanMode");
                    return;
                }
                if (!current) {
                    is_stop_check_net_state = 1;
                    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
                    lbTopWifiList->hide();
                    btnAddNet->hide();
                    objKyDBus->setWifiSwitchState(false);

                    QThread *rfkill_t = new QThread();
                    BackThread *rfkill_bt = new BackThread();
                    rfkill_bt->moveToThread(rfkill_t);
                    objKyDBus->oldWifiSwitchState = false;
                    btnWireless->setSwitchStatus(false);
                    connect(rfkill_t, SIGNAL(finished()), rfkill_t, SLOT(deleteLater()));
                    connect(rfkill_t, SIGNAL(started()), rfkill_bt, SLOT(rfkillExecDisWifi()));
                    connect(rfkill_bt, SIGNAL(disWifiDoneByRfkill()), this, SLOT(rfkillDisableWifiDone()));
                    connect(rfkill_bt, SIGNAL(btFinishByRfkill()), rfkill_t, SLOT(quit()));
                    rfkill_t->start();
                    this->startLoading();
                } else {
                    if (is_fly_mode_on == 0) {
                        on_btnWifiList_clicked();
                        is_stop_check_net_state = 1;
                        qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
                        isRadioWifiTurningOn = true;
                        objKyDBus->setWifiCardState(true);
                        objKyDBus->setWifiSwitchState(true);

                        QThread *rfkill_t = new QThread();
                        BackThread *rfkill_bt = new BackThread();
                        rfkill_bt->moveToThread(rfkill_t);
                        objKyDBus->oldWifiSwitchState = true;
                        btnWireless->setSwitchStatus(true);
                        connect(rfkill_t, SIGNAL(finished()), rfkill_t, SLOT(deleteLater()));
                        connect(rfkill_t, SIGNAL(started()), rfkill_bt, SLOT(rfKillexecEnWifi()));
                        connect(rfkill_bt, SIGNAL(enWifiDoneByRfkill()), this, SLOT(rfkillEnableWifiDone()));
                        connect(rfkill_bt, SIGNAL(btFinishByRfkill()), rfkill_t, SLOT(quit()));
                        rfkill_t->start();
                        this->startLoading();
                    }
                }
            }
        }
    } else {
        qDebug() << "但是函数onRfkillStatusChanged()正在执行,停止执行这个函数";
    }
}

//wifi开关关闭
void MainWindow::rfkillDisableWifiDone()
{
    canExecHandleWifiSwitchChange = true;
    if (is_btnWifiList_clicked)
        disWifiDoneChangeUI();

    qDebug()<<"debug: already turn off the switch of wifi network detected by kds policy";
    //syslog(LOG_DEBUG, "Already turn off the switch of wifi network detected by kds policy");

    this->stopLoading();
    is_stop_check_net_state = 0;
    qDebug()<< Q_FUNC_INFO << __LINE__ <<":set is_stop_check_net_state to"<<is_stop_check_net_state;
}

//wifi开关打开
void MainWindow::rfkillEnableWifiDone()
{
    canExecHandleWifiSwitchChange = true;
    if (is_btnWifiList_clicked) {
        QtConcurrent::run([=]() {
            qDebug()<<"Already turn on the switch of wifi network detected by kds policy";
            usleep(1600*1000);
            objKyDBus->requestScanWifi(); //要求后台扫描AP
            isReConnAfterTurnOnWifi = true;
            qDebug() << "Scan finished after truning wifi switch on, ready to reconnect wifi";
            usleep(1600*1000);
            emit this->requestReconnecWifi();

            //emit loadWifiListAfterScan();
            isRadioWifiTurningOn = false;
        });
    } else {
        current_wifi_list_state = LOAD_WIFI_LIST;
        //on_btnWifiList_clicked();
        isRadioWifiTurningOn = false;
    }
}

void MainWindow::showPb(QString type, QString name)
{
    bShowPb = false;
    if (type != "wifi") {
        onBtnNetListClicked(1);
    }
    else {
        on_btnWifiList_clicked();
    }
    emit showPbSignal(QSystemTrayIcon::Trigger);
    if (name.isEmpty() || name == "") return;
    qDebug()<<"A "<<type<<" named "<<name<<" clicked! mainwindow.cpp ShowPb(QString,QString):void";
    if (type == "wifi") {
        QtConcurrent::run([=]() {
            int runtime = 0;
            //等待扫描完成
            while(!bShowPb)
            {
                usleep(500*1000);
                runtime++;
                if(runtime > 10)
                    break;
            }
            qDebug()<<__FUNCTION__<<__LINE__<<runtime;
            emit this->wifiClicked(name);
        });
    } else {
        emit this->lanClicked(name);
    }
}