File: mainwindow.cpp

package info (click to toggle)
tellico 4.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,064 kB
  • sloc: cpp: 89,088; xml: 35,338; ansic: 7,011; javascript: 3,059; python: 302; perl: 36; ada: 32; sh: 28; makefile: 13
file content (2686 lines) | stat: -rw-r--r-- 105,433 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
/***************************************************************************
    Copyright (C) 2001-2020 Robby Stephenson <robby@periapsis.org>
    Copyright (C) 2011 Pedro Miguel Carvalho <kde@pmc.com.pt>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as        *
 *   published by the Free Software Foundation; either version 2 of        *
 *   the License or (at your option) version 3 or any later version        *
 *   accepted by the membership of KDE e.V. (or its successor approved     *
 *   by the membership of KDE e.V.), which shall act as a proxy            *
 *   defined in Section 14 of version 3 of the license.                     *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 *                                                                         *
 ***************************************************************************/

#include "mainwindow.h"
#include "tellico_kernel.h"
#include "document.h"
#include "detailedlistview.h"
#include "entryeditdialog.h"
#include "groupview.h"
#include "viewstack.h"
#include "collection.h"
#include "collectionfactory.h"
#include "entry.h"
#include "configdialog.h"
#include "filter.h"
#include "filterparser.h"
#include "filterdialog.h"
#include "collectionfieldsdialog.h"
#include "controller.h"
#include "importdialog.h"
#include "exportdialog.h"
#include "printhandler.h"
#include "entryview.h"
#include "entryiconview.h"
#include "filterview.h"
#include "loanview.h"
#include "images/imagefactory.h"
#include "collections/collectioninitializer.h"
#include "collections/bibtexcollection.h" // needed for bibtex string macro dialog
#include "fetchdialog.h"
#include "reportdialog.h"
#include "bibtexkeydialog.h"
#include "core/filehandler.h"
#include "core/logger.h"
#include "core/tellico_strings.h"
#include "fetch/fetchmanager.h"
#include "fetch/fetcherinitializer.h"
#include "cite/actionmanager.h"
#include "config/tellico_config.h"
#include "core/netaccess.h"
#include "dbusinterface.h"
#include "models/models.h"
#include "models/entrymodel.h"
#include "models/entryiconmodel.h"
#include "models/entryselectionmodel.h"
#include "newstuff/manager.h"
#include "gui/drophandler.h"
#include "gui/stringmapdialog.h"
#include "gui/lineedit.h"
#include "gui/statusbar.h"
#include "gui/tabwidget.h"
#include "gui/dockwidget.h"
#include "gui/collectiontemplatedialog.h"
#include "utils/cursorsaver.h"
#include "utils/guiproxy.h"
#include "utils/tellico_utils.h"
#include "utils/bibtexhandler.h" // needed for bibtex options
#include "utils/datafileregistry.h"
#include "tellico_debug.h"

#include <KComboBox>
#include <KToolBar>
#include <KLocalizedString>
#include <KConfig>
#include <KStandardAction>
#include <KWindowConfig>
#include <KMessageBox>
#include <KRecentDocument>
#include <KRecentDirs>
#include <KEditToolBar>
#include <KShortcutsDialog>
#include <KRecentFilesAction>
#include <KToggleAction>
#include <KActionCollection>
#include <KActionMenu>
#include <KFileWidget>
#include <KDualAction>
#include <KXMLGUIFactory>
#include <KAboutData>
#include <KIconLoader>
#include <kwidgetsaddons_version.h>

#define HAVE_STYLE_MANAGER __has_include(<KStyleManager>)
#if HAVE_STYLE_MANAGER
#include <KStyleManager>
#endif

#include <QApplication>
#include <QUndoStack>
#include <QAction>
#include <QSignalMapper>
#include <QTimer>
#include <QMetaObject> // needed for copy, cut, paste slots
#include <QMimeDatabase>
#include <QMimeType>
#include <QMenuBar>
#include <QFileDialog>
#include <QMetaMethod>
#include <QVBoxLayout>
#include <QTextEdit>

namespace {
  static const int MAX_IMAGES_WARN_PERFORMANCE = 200;

QIcon mimeIcon(const char* s) {
  QMimeDatabase db;
  QMimeType ptr = db.mimeTypeForName(QLatin1String(s));
  if(!ptr.isValid()) {
    myDebug() << "*** no icon for" << s;
  }
  return ptr.isValid() ? QIcon::fromTheme(ptr.iconName()) : QIcon();
}

QIcon mimeIcon(const char* s1, const char* s2) {
  QMimeDatabase db;
  QMimeType ptr = db.mimeTypeForName(QLatin1String(s1));
  if(!ptr.isValid()) {
    ptr = db.mimeTypeForName(QLatin1String(s2));
    if(!ptr.isValid()) {
      myDebug() << "*** no icon for" << s1 << "or" << s2;
    }
  }
  return ptr.isValid() ? QIcon::fromTheme(ptr.iconName()) : QIcon();
}

}

using namespace Tellico;
using Tellico::MainWindow;

MainWindow::MainWindow(QWidget* parent_/*=0*/) : KXmlGuiWindow(parent_),
    m_updateAll(nullptr),
    m_statusBar(nullptr),
    m_editDialog(nullptr),
    m_groupView(nullptr),
    m_filterView(nullptr),
    m_loanView(nullptr),
    m_configDlg(nullptr),
    m_filterDlg(nullptr),
    m_collFieldsDlg(nullptr),
    m_stringMacroDlg(nullptr),
    m_bibtexKeyDlg(nullptr),
    m_fetchDlg(nullptr),
    m_reportDlg(nullptr),
    m_printHandler(nullptr),
    m_queuedFilters(0),
    m_initialized(false),
    m_newDocument(true),
    m_dontQueueFilter(false),
    m_savingImageLocationChange(false) {

  Controller::init(this); // the only time this is ever called!
  // has to be after controller init
  Kernel::init(this); // the only time this is ever called!
  GUI::Proxy::setMainWidget(this);

  setWindowIcon(QIcon::fromTheme(QStringLiteral("tellico"),
                                 QIcon(QLatin1String(":/icons/tellico"))));

  // initialize the status bar and progress bar
  initStatusBar();

  // initialize all the collection types
  // which must be done before the document is created
  CollectionInitializer initCollections;
  // register all the fetcher types
  Fetch::FetcherInitializer initFetchers;

  // create a document, which also creates an empty book collection
  // must be done before the different widgets are created
  initDocument();

  // set up all the actions, some connect to the document, so this must be after initDocument()
  initActions();

  // create the different widgets in the view, some widgets connect to actions, so must be after initActions()
  initView();

  // The edit dialog is not created until after the main window is initialized, so it can be a child.
  // So don't make any connections, don't read options for it until initFileOpen
  readOptions();

  setAcceptDrops(true);
  DropHandler* drophandler = new DropHandler(this);
  installEventFilter(drophandler);

  new ApplicationInterface(this);
  new CollectionInterface(this);

  MARK_LINE;
  QTimer::singleShot(0, this, &MainWindow::slotInit);
}

MainWindow::~MainWindow() {
  qDeleteAll(m_fetchActions);
  m_fetchActions.clear();
  // when closing the mainwindow, immediately after running Tellico, often there was a long pause
  // before the application eventually quit, something related to polling on eventfd, I don't
  // know what. So when closing the window, make sure to immediately quit the application
  QTimer::singleShot(0, qApp, &QCoreApplication::quit);
}

void MainWindow::slotInit() {
  // if the edit dialog exists, we know we've already called this function
  if(m_editDialog) {
    return;
  }
  MARK;

  m_editDialog = new EntryEditDialog(this);
  Controller::self()->addObserver(m_editDialog);

  m_toggleEntryEditor->setChecked(Config::showEditWidget());
  slotToggleEntryEditor();
  m_lockLayout->setActive(Config::lockLayout());

  initConnections();
  connect(ImageFactory::self(), &ImageFactory::imageLocationMismatch,
          this, &MainWindow::slotImageLocationMismatch);
  // Init DBUS for new stuff manager
  NewStuff::Manager::self();
}

void MainWindow::initStatusBar() {
  MARK;
  m_statusBar = new Tellico::StatusBar(this);
  setStatusBar(m_statusBar);
}

void MainWindow::initActions() {
  MARK;
  /*************************************************
   * File->New menu
   *************************************************/
  QSignalMapper* collectionMapper = new QSignalMapper(this);
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
  void (QSignalMapper::* mappedInt)(int) = &QSignalMapper::mapped;
  connect(collectionMapper, mappedInt, this, &MainWindow::slotFileNew);
#else
  connect(collectionMapper, &QSignalMapper::mappedInt, this, &MainWindow::slotFileNew);
#endif

  m_newCollectionMenu = new KActionMenu(i18n("New Collection"), this);
  m_newCollectionMenu->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
  m_newCollectionMenu->setToolTip(i18n("Create a new collection"));
#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5,77,0)
  m_newCollectionMenu->setPopupMode(QToolButton::InstantPopup);
#else
  m_newCollectionMenu->setDelayed(false);
#endif
  actionCollection()->addAction(QStringLiteral("file_new_collection"), m_newCollectionMenu);

  QAction* action;

  void (QSignalMapper::* mapVoid)() = &QSignalMapper::map;
#define COLL_ACTION(TYPE, NAME, TEXT, TIP, ICON) \
  action = actionCollection()->addAction(QStringLiteral(NAME), collectionMapper, mapVoid); \
  action->setText(TEXT); \
  action->setToolTip(TIP); \
  action->setIcon(QIcon(QStringLiteral(":/icons/" ICON))); \
  m_newCollectionMenu->addAction(action); \
  collectionMapper->setMapping(action, Data::Collection::TYPE);

  COLL_ACTION(Book, "new_book_collection", i18n("New &Book Collection"),
              i18n("Create a new book collection"), "book");

  COLL_ACTION(Bibtex, "new_bibtex_collection", i18n("New B&ibliography"),
              i18n("Create a new bibtex bibliography"), "bibtex");

  COLL_ACTION(ComicBook, "new_comic_book_collection", i18n("New &Comic Book Collection"),
              i18n("Create a new comic book collection"), "comic");

  COLL_ACTION(Video, "new_video_collection", i18n("New &Video Collection"),
              i18n("Create a new video collection"), "video");

  COLL_ACTION(Album, "new_music_collection", i18n("New &Music Collection"),
              i18n("Create a new music collection"), "album");

  COLL_ACTION(Coin, "new_coin_collection", i18n("New C&oin Collection"),
              i18n("Create a new coin collection"), "coin");

  COLL_ACTION(Stamp, "new_stamp_collection", i18n("New &Stamp Collection"),
              i18n("Create a new stamp collection"), "stamp");

  COLL_ACTION(Card, "new_card_collection", i18n("New C&ard Collection"),
              i18n("Create a new trading card collection"), "card");

  COLL_ACTION(Wine, "new_wine_collection", i18n("New &Wine Collection"),
              i18n("Create a new wine collection"), "wine");

  COLL_ACTION(Game, "new_game_collection", i18n("New Video &Game Collection"),
              i18n("Create a new video game collection"), "game");

  COLL_ACTION(BoardGame, "new_boardgame_collection", i18n("New Boa&rd Game Collection"),
              i18n("Create a new board game collection"), "boardgame");

  COLL_ACTION(File, "new_file_catalog", i18n("New &File Catalog"),
              i18n("Create a new file catalog"), "file");

  action = actionCollection()->addAction(QStringLiteral("new_custom_collection"), collectionMapper, mapVoid);
  action->setText(i18n("New C&ustom Collection"));
  action->setToolTip(i18n("Create a new custom collection"));
  action->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
  m_newCollectionMenu->addAction(action);
  collectionMapper->setMapping(action, Data::Collection::Base);

#undef COLL_ACTION

  /*************************************************
   * File menu
   *************************************************/
  action = KStandardAction::open(this, SLOT(slotFileOpen()), actionCollection());
  action->setToolTip(i18n("Open an existing document"));
  m_fileOpenRecent = KStandardAction::openRecent(this, SLOT(slotFileOpenRecent(const QUrl&)), actionCollection());
  m_fileOpenRecent->setToolTip(i18n("Open a recently used file"));
  m_fileSave = KStandardAction::save(this, SLOT(slotFileSave()), actionCollection());
  m_fileSave->setToolTip(i18n("Save the document"));
  action = KStandardAction::saveAs(this, SLOT(slotFileSaveAs()), actionCollection());
  action->setToolTip(i18n("Save the document as a different file..."));

  action = actionCollection()->addAction(QStringLiteral("file_save_template"),
                                         this, SLOT(slotFileSaveAsTemplate()));
  action->setText(i18n("Save As Template..."));
  action->setIcon(QIcon::fromTheme(QStringLiteral("document-save-as-template")));
  action->setToolTip(i18n("Save as a collection template"));

  action = KStandardAction::print(this, SLOT(slotFilePrint()), actionCollection());
  action->setToolTip(i18n("Print the contents of the collection..."));
#ifdef USE_KHTML
  {
    KHTMLPart w;
    // KHTMLPart printing was broken in KDE until KHTML 5.16
    const QString version =  w.componentData().version();
    const uint major = version.section(QLatin1Char('.'), 0, 0).toUInt();
    const uint minor = version.section(QLatin1Char('.'), 1, 1).toUInt();
    if(major == 5 && minor < 16) {
      myWarning() << "Printing is broken for KDE Frameworks < 5.16. Please upgrade";
      action->setEnabled(false);
    }
  }
#else
  // print preview is only available with QWebEngine
  action = KStandardAction::printPreview(this, SLOT(slotFilePrintPreview()), actionCollection());
  action->setToolTip(i18n("Preview the contents of the collection..."));
#endif

  action = KStandardAction::quit(this, SLOT(slotFileQuit()), actionCollection());
  action->setToolTip(i18n("Quit the application"));

/**************** Import Menu ***************************/

  QSignalMapper* importMapper = new QSignalMapper(this);
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
  connect(importMapper, mappedInt, this, &MainWindow::slotFileImport);
#else
  connect(importMapper, &QSignalMapper::mappedInt, this, &MainWindow::slotFileImport);
#endif

  KActionMenu* importMenu = new KActionMenu(i18n("&Import"), this);
  importMenu->setIcon(QIcon::fromTheme(QStringLiteral("document-import")));
  importMenu->setToolTip(i18n("Import the collection data from other formats"));
#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5,77,0)
  importMenu->setPopupMode(QToolButton::InstantPopup);
#else
  importMenu->setDelayed(false);
#endif
  actionCollection()->addAction(QStringLiteral("file_import"), importMenu);

#define IMPORT_ACTION(TYPE, NAME, TEXT, TIP, ICON) \
  action = actionCollection()->addAction(QStringLiteral(NAME), importMapper, mapVoid); \
  action->setText(TEXT); \
  action->setToolTip(TIP); \
  action->setIcon(ICON); \
  importMenu->addAction(action); \
  importMapper->setMapping(action, TYPE);

  IMPORT_ACTION(Import::TellicoXML, "file_import_tellico", i18n("Import Tellico Data..."),
                i18n("Import another Tellico data file"),
                QIcon::fromTheme(QStringLiteral("tellico"), QIcon(QLatin1String(":/icons/tellico"))));

  IMPORT_ACTION(Import::CSV, "file_import_csv", i18n("Import CSV Data..."),
                i18n("Import a CSV file"), mimeIcon("text/csv", "text/x-csv"));

  IMPORT_ACTION(Import::MODS, "file_import_mods", i18n("Import MODS Data..."),
                i18n("Import a MODS data file"), mimeIcon("text/xml"));

  IMPORT_ACTION(Import::Alexandria, "file_import_alexandria", i18n("Import Alexandria Data..."),
                i18n("Import data from the Alexandria book collection manager"),
                QIcon::fromTheme(QStringLiteral("alexandria"), QIcon(QLatin1String(":/icons/alexandria"))));

  IMPORT_ACTION(Import::Delicious, "file_import_delicious", i18n("Import Delicious Library Data..."),
                i18n("Import data from Delicious Library"),
                QIcon::fromTheme(QStringLiteral("deliciouslibrary"), QIcon(QLatin1String(":/icons/deliciouslibrary"))));

  IMPORT_ACTION(Import::Collectorz, "file_import_collectorz", i18n("Import Collectorz Data..."),
                i18n("Import data from Collectorz"),
                QIcon::fromTheme(QStringLiteral("collectorz"), QIcon(QLatin1String(":/icons/collectorz"))));

  IMPORT_ACTION(Import::DataCrow, "file_import_datacrow", i18n("Import Data Crow Data..."),
                i18n("Import data from Data Crow"),
                QIcon::fromTheme(QStringLiteral("datacrow"), QIcon(QLatin1String(":/icons/datacrow"))));

  IMPORT_ACTION(Import::Referencer, "file_import_referencer", i18n("Import Referencer Data..."),
                i18n("Import data from Referencer"),
                QIcon::fromTheme(QStringLiteral("referencer"), QIcon(QLatin1String(":/icons/referencer"))));

  IMPORT_ACTION(Import::Bibtex, "file_import_bibtex", i18n("Import Bibtex Data..."),
                i18n("Import a bibtex bibliography file"), mimeIcon("text/x-bibtex"));
#ifndef ENABLE_BTPARSE
  action->setEnabled(false);
#endif

  IMPORT_ACTION(Import::Bibtexml, "file_import_bibtexml", i18n("Import Bibtexml Data..."),
                i18n("Import a Bibtexml bibliography file"), mimeIcon("text/xml"));

  IMPORT_ACTION(Import::RIS, "file_import_ris", i18n("Import RIS Data..."),
                i18n("Import an RIS reference file"), QIcon::fromTheme(QStringLiteral(":/icons/cite")));

  IMPORT_ACTION(Import::MARC, "file_import_marc", i18n("Import MARC Data..."),
                i18n("Import MARC data"), QIcon::fromTheme(QStringLiteral(":/icons/cite")));
  // disable this import action if the necessary executable is not available
  QTimer::singleShot(1000, this, [action]() {
    const QString ymd = QStandardPaths::findExecutable(QStringLiteral("yaz-marcdump"));
    action->setEnabled(!ymd.isEmpty());
  });

  IMPORT_ACTION(Import::Goodreads, "file_import_goodreads", i18n("Import Goodreads Collection..."),
                i18n("Import a collection from Goodreads.com"), QIcon::fromTheme(QStringLiteral(":/icons/goodreads")));

  IMPORT_ACTION(Import::LibraryThing, "file_import_librarything", i18n("Import LibraryThing Collection..."),
                i18n("Import a collection from LibraryThing.com"), QIcon::fromTheme(QStringLiteral(":/icons/librarything")));

  IMPORT_ACTION(Import::PDF, "file_import_pdf", i18n("Import PDF File..."),
                i18n("Import a PDF file"), mimeIcon("application/pdf"));

  IMPORT_ACTION(Import::AudioFile, "file_import_audiofile", i18n("Import Audio File Metadata..."),
                i18n("Import meta-data from audio files"), mimeIcon("audio/mp3", "audio/x-mp3"));
#ifndef HAVE_TAGLIB
  action->setEnabled(false);
#endif

  IMPORT_ACTION(Import::FreeDB, "file_import_freedb", i18n("Import Audio CD Data..."),
                i18n("Import audio CD information"), mimeIcon("media/audiocd", "application/x-cda"));
#if !defined (HAVE_OLD_KCDDB) && !defined (HAVE_KCDDB)
  action->setEnabled(false);
#endif

  IMPORT_ACTION(Import::Discogs, "file_import_discogs", i18n("Import Discogs Collection..."),
                i18n("Import a collection from Discogs.com"), QIcon::fromTheme(QStringLiteral(":/icons/discogs")));

  IMPORT_ACTION(Import::GCstar, "file_import_gcstar", i18n("Import GCstar Data..."),
                i18n("Import a GCstar data file"),
                QIcon::fromTheme(QStringLiteral("gcstar"), QIcon(QLatin1String(":/icons/gcstar"))));

  IMPORT_ACTION(Import::Griffith, "file_import_griffith", i18n("Import Griffith Data..."),
                i18n("Import a Griffith database"),
                QIcon::fromTheme(QStringLiteral("griffith"), QIcon(QLatin1String(":/icons/griffith"))));

  IMPORT_ACTION(Import::AMC, "file_import_amc", i18n("Import Ant Movie Catalog Data..."),
                i18n("Import an Ant Movie Catalog data file"),
                QIcon::fromTheme(QStringLiteral("amc"), QIcon(QLatin1String(":/icons/amc"))));

  IMPORT_ACTION(Import::BoardGameGeek, "file_import_boardgamegeek", i18n("Import BoardGameGeek Collection..."),
                i18n("Import a collection from BoardGameGeek.com"), QIcon(QLatin1String(":/icons/boardgamegeek")));

  IMPORT_ACTION(Import::VinoXML, "file_import_vinoxml", i18n("Import VinoXML..."),
                i18n("Import VinoXML data"), QIcon(QLatin1String(":/icons/vinoxml")));

  IMPORT_ACTION(Import::FileListing, "file_import_filelisting", i18n("Import File Listing..."),
                i18n("Import information about files in a folder"), mimeIcon("inode/directory"));

  IMPORT_ACTION(Import::XSLT, "file_import_xslt", i18n("Import XSL Transform..."),
                i18n("Import using an XSL Transform"), mimeIcon("application/xslt+xml", "text/x-xslt"));

#undef IMPORT_ACTION

/**************** Export Menu ***************************/

  QSignalMapper* exportMapper = new QSignalMapper(this);
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
  connect(exportMapper, mappedInt, this, &MainWindow::slotFileExport);
#else
  connect(exportMapper, &QSignalMapper::mappedInt, this, &MainWindow::slotFileExport);
#endif

  KActionMenu* exportMenu = new KActionMenu(i18n("&Export"), this);
  exportMenu->setIcon(QIcon::fromTheme(QStringLiteral("document-export")));
  exportMenu->setToolTip(i18n("Export the collection data to other formats"));
#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5,77,0)
  exportMenu->setPopupMode(QToolButton::InstantPopup);
#else
  exportMenu->setDelayed(false);
#endif
  actionCollection()->addAction(QStringLiteral("file_export"), exportMenu);

#define EXPORT_ACTION(TYPE, NAME, TEXT, TIP, ICON) \
  action = actionCollection()->addAction(QStringLiteral(NAME), exportMapper, mapVoid); \
  action->setText(TEXT); \
  action->setToolTip(TIP); \
  action->setIcon(ICON); \
  exportMenu->addAction(action); \
  exportMapper->setMapping(action, TYPE);

  EXPORT_ACTION(Export::TellicoXML, "file_export_xml", i18n("Export to XML..."),
                i18n("Export to a Tellico XML file"),
                QIcon::fromTheme(QStringLiteral("tellico"), QIcon(QStringLiteral(":/icons/tellico"))));

  EXPORT_ACTION(Export::TellicoZip, "file_export_zip", i18n("Export to Zip..."),
                i18n("Export to a Tellico Zip file"),
                QIcon::fromTheme(QStringLiteral("tellico"), QIcon(QStringLiteral(":/icons/tellico"))));

  EXPORT_ACTION(Export::HTML, "file_export_html", i18n("Export to HTML..."),
                i18n("Export to an HTML file"), mimeIcon("text/html"));

  EXPORT_ACTION(Export::CSV, "file_export_csv", i18n("Export to CSV..."),
                i18n("Export to a comma-separated values file"), mimeIcon("text/csv", "text/x-csv"));

  EXPORT_ACTION(Export::Alexandria, "file_export_alexandria", i18n("Export to Alexandria..."),
                i18n("Export to an Alexandria library"),
                QIcon::fromTheme(QStringLiteral("alexandria"), QIcon(QStringLiteral(":/icons/alexandria"))));

  EXPORT_ACTION(Export::Bibtex, "file_export_bibtex", i18n("Export to Bibtex..."),
                i18n("Export to a bibtex file"), mimeIcon("text/x-bibtex"));

  EXPORT_ACTION(Export::Bibtexml, "file_export_bibtexml", i18n("Export to Bibtexml..."),
                i18n("Export to a Bibtexml file"), mimeIcon("text/xml"));

  EXPORT_ACTION(Export::ONIX, "file_export_onix", i18n("Export to ONIX..."),
                i18n("Export to an ONIX file"), mimeIcon("text/xml"));

  EXPORT_ACTION(Export::GCstar, "file_export_gcstar", i18n("Export to GCstar..."),
                i18n("Export to a GCstar data file"),
                QIcon::fromTheme(QStringLiteral("gcstar"), QIcon(QStringLiteral(":/icons/gcstar"))));

  EXPORT_ACTION(Export::XSLT, "file_export_xslt", i18n("Export XSL Transform..."),
                i18n("Export using an XSL Transform"), mimeIcon("application/xslt+xml", "text/x-xslt"));

#undef EXPORT_ACTION

  /*************************************************
   * Edit menu
   *************************************************/
  KStandardAction::undo(Kernel::self()->commandHistory(), SLOT(undo()), actionCollection());
  KStandardAction::redo(Kernel::self()->commandHistory(), SLOT(undo()), actionCollection());

  action = KStandardAction::cut(this, SLOT(slotEditCut()), actionCollection());
  action->setToolTip(i18n("Cut the selected text and puts it in the clipboard"));
  action = KStandardAction::copy(this, SLOT(slotEditCopy()), actionCollection());
  action->setToolTip(i18n("Copy the selected text to the clipboard"));
  action = KStandardAction::paste(this, SLOT(slotEditPaste()), actionCollection());
  action->setToolTip(i18n("Paste the clipboard contents"));
  action = KStandardAction::selectAll(this, SLOT(slotEditSelectAll()), actionCollection());
  action->setToolTip(i18n("Select all the entries in the collection"));
  action = KStandardAction::deselect(this, SLOT(slotEditDeselect()), actionCollection());
  action->setToolTip(i18n("Deselect all the entries in the collection"));

  action = actionCollection()->addAction(QStringLiteral("filter_dialog"), this, SLOT(slotShowFilterDialog()));
  action->setText(i18n("Advanced &Filter..."));
  action->setIconText(i18n("Filter"));
  action->setIcon(QIcon::fromTheme(QStringLiteral("view-filter")));
  actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::Key_J));
  action->setToolTip(i18n("Filter the collection"));

  /*************************************************
   * Collection menu
   *************************************************/
  m_newEntry = actionCollection()->addAction(QStringLiteral("coll_new_entry"),
                                             this, SLOT(slotNewEntry()));
  m_newEntry->setText(i18n("&New Entry..."));
  m_newEntry->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
  m_newEntry->setIconText(i18n("New Entry"));
  actionCollection()->setDefaultShortcut(m_newEntry, QKeySequence(Qt::CTRL | Qt::Key_N));
  m_newEntry->setToolTip(i18n("Create a new entry"));

  KActionMenu* addEntryMenu = new KActionMenu(i18n("Add Entry"), this);
  addEntryMenu->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5,77,0)
  addEntryMenu->setPopupMode(QToolButton::InstantPopup);
#else
  addEntryMenu->setDelayed(false);
#endif
  actionCollection()->addAction(QStringLiteral("coll_add_entry"), addEntryMenu);

  action = actionCollection()->addAction(QStringLiteral("edit_search_internet"), this, SLOT(slotShowFetchDialog()));
  action->setText(i18n("Internet Search..."));
  action->setIconText(i18n("Internet Search"));
  action->setIcon(QIcon::fromTheme(QStringLiteral("tools-wizard")));
  actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::Key_I));
  action->setToolTip(i18n("Search the internet..."));

  addEntryMenu->addAction(m_newEntry);
  addEntryMenu->addAction(actionCollection()->action(QStringLiteral("edit_search_internet")));

  m_editEntry = actionCollection()->addAction(QStringLiteral("coll_edit_entry"),
                                              this, SLOT(slotShowEntryEditor()));
  m_editEntry->setText(i18n("&Edit Entry..."));
  m_editEntry->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
  actionCollection()->setDefaultShortcut(m_editEntry, QKeySequence(Qt::CTRL | Qt::Key_E));
  m_editEntry->setToolTip(i18n("Edit the selected entries"));

  m_copyEntry = actionCollection()->addAction(QStringLiteral("coll_copy_entry"),
                                              Controller::self(), SLOT(slotCopySelectedEntries()));
  m_copyEntry->setText(i18n("D&uplicate Entry"));
  m_copyEntry->setIcon(QIcon::fromTheme(QStringLiteral("edit-copy")));
  actionCollection()->setDefaultShortcut(m_copyEntry, QKeySequence(Qt::CTRL | Qt::Key_Y));
  m_copyEntry->setToolTip(i18n("Copy the selected entries"));

  m_deleteEntry = actionCollection()->addAction(QStringLiteral("coll_delete_entry"),
                                                Controller::self(), SLOT(slotDeleteSelectedEntries()));
  m_deleteEntry->setText(i18n("&Delete Entry"));
  m_deleteEntry->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete")));
  actionCollection()->setDefaultShortcut(m_deleteEntry, QKeySequence(Qt::CTRL | Qt::Key_D));
  m_deleteEntry->setToolTip(i18n("Delete the selected entries"));

  m_mergeEntry = actionCollection()->addAction(QStringLiteral("coll_merge_entry"),
                                               Controller::self(), SLOT(slotMergeSelectedEntries()));
  m_mergeEntry->setText(i18n("&Merge Entries"));
  m_mergeEntry->setIcon(QIcon::fromTheme(QStringLiteral("document-import")));
//  CTRL+G is ambiguous, pick another
//  actionCollection()->setDefaultShortcut(m_mergeEntry, QKeySequence(Qt::CTRL | Qt::Key_G));
  m_mergeEntry->setToolTip(i18n("Merge the selected entries"));
  m_mergeEntry->setEnabled(false); // gets enabled when more than 1 entry is selected

  m_checkOutEntry = actionCollection()->addAction(QStringLiteral("coll_checkout"), Controller::self(), SLOT(slotCheckOut()));
  m_checkOutEntry->setText(i18n("Check-&out..."));
  m_checkOutEntry->setIcon(QIcon::fromTheme(QStringLiteral("arrow-up-double")));
  m_checkOutEntry->setToolTip(i18n("Check-out the selected items"));

  m_checkInEntry = actionCollection()->addAction(QStringLiteral("coll_checkin"), Controller::self(), SLOT(slotCheckIn()));
  m_checkInEntry->setText(i18n("Check-&in"));
  m_checkInEntry->setIcon(QIcon::fromTheme(QStringLiteral("arrow-down-double")));
  m_checkInEntry->setToolTip(i18n("Check-in the selected items"));

  action = actionCollection()->addAction(QStringLiteral("coll_rename_collection"), this, SLOT(slotRenameCollection()));
  action->setText(i18n("&Rename Collection..."));
  action->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
  actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::Key_R));
  action->setToolTip(i18n("Rename the collection"));

  action = actionCollection()->addAction(QStringLiteral("coll_fields"), this, SLOT(slotShowCollectionFieldsDialog()));
  action->setText(i18n("Collection &Fields..."));
  action->setIconText(i18n("Fields"));
  action->setIcon(QIcon::fromTheme(QStringLiteral("preferences-other")));
  actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::Key_U));
  action->setToolTip(i18n("Modify the collection fields"));

  action = actionCollection()->addAction(QStringLiteral("coll_reports"), this, SLOT(slotShowReportDialog()));
  action->setText(i18n("&Generate Reports..."));
  action->setIconText(i18n("Reports"));
  action->setIcon(QIcon::fromTheme(QStringLiteral("document-multiple")));
  action->setToolTip(i18n("Generate collection reports"));

  action = actionCollection()->addAction(QStringLiteral("coll_convert_bibliography"), this, SLOT(slotConvertToBibliography()));
  action->setText(i18n("Convert to &Bibliography"));
  action->setIcon(QIcon(QLatin1String(":/icons/bibtex")));
  action->setToolTip(i18n("Convert a book collection to a bibliography"));

  action = actionCollection()->addAction(QStringLiteral("coll_string_macros"), this, SLOT(slotShowStringMacroDialog()));
  action->setText(i18n("String &Macros..."));
  action->setIcon(QIcon::fromTheme(QStringLiteral("view-list-text")));
  action->setToolTip(i18n("Edit the bibtex string macros"));

  action = actionCollection()->addAction(QStringLiteral("coll_key_manager"), this, SLOT(slotShowBibtexKeyDialog()));
  action->setText(i18n("Check for Duplicate Keys..."));
  action->setIcon(mimeIcon("text/x-bibtex"));
  action->setToolTip(i18n("Check for duplicate citation keys"));

  QSignalMapper* citeMapper = new QSignalMapper(this);
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
  connect(citeMapper, mappedInt, this, &MainWindow::slotCiteEntry);
#else
  connect(citeMapper, &QSignalMapper::mappedInt, this, &MainWindow::slotCiteEntry);
#endif

  action = actionCollection()->addAction(QStringLiteral("cite_clipboard"), citeMapper, mapVoid);
  action->setText(i18n("Copy Bibtex to Cli&pboard"));
  action->setToolTip(i18n("Copy bibtex citations to the clipboard"));
  action->setIcon(QIcon::fromTheme(QStringLiteral("edit-paste")));
  citeMapper->setMapping(action, Cite::CiteClipboard);

  action = actionCollection()->addAction(QStringLiteral("cite_lyxpipe"), citeMapper, mapVoid);
  action->setText(i18n("Cite Entry in &LyX"));
  action->setToolTip(i18n("Cite the selected entries in LyX"));
  action->setIcon(QIcon::fromTheme(QStringLiteral("lyx"), QIcon(QLatin1String(":/icons/lyx"))));
  citeMapper->setMapping(action, Cite::CiteLyxpipe);

  m_updateMapper = new QSignalMapper(this);
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
  void (QSignalMapper::* mappedString)(const QString&) = &QSignalMapper::mapped;
  connect(m_updateMapper, mappedString,
          Controller::self(), &Controller::slotUpdateSelectedEntries);
#else
  connect(m_updateMapper, &QSignalMapper::mappedString,
          Controller::self(), &Controller::slotUpdateSelectedEntries);
#endif

  m_updateEntryMenu = new KActionMenu(i18n("&Update Entry"), this);
  m_updateEntryMenu->setIcon(QIcon::fromTheme(QStringLiteral("document-export")));
  m_updateEntryMenu->setIconText(i18nc("Update Entry", "Update"));
#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5,77,0)
  m_updateEntryMenu->setPopupMode(QToolButton::InstantPopup);
#else
  m_updateEntryMenu->setDelayed(false);
#endif
  actionCollection()->addAction(QStringLiteral("coll_update_entry"), m_updateEntryMenu);

  m_updateAll = actionCollection()->addAction(QStringLiteral("update_entry_all"), m_updateMapper, mapVoid);
  m_updateAll->setText(i18n("All Sources"));
  m_updateAll->setToolTip(i18n("Update entry data from all available sources"));
  m_updateMapper->setMapping(m_updateAll, QStringLiteral("_all"));

  /*************************************************
   * Settings menu
   *************************************************/
  setStandardToolBarMenuEnabled(true);
  createStandardStatusBarAction();
  // style config
#if HAVE_STYLE_MANAGER
  actionCollection()->addAction(QStringLiteral("settings_style"), KStyleManager::createConfigureAction(this));
#endif

  m_lockLayout = new KDualAction(this);
  connect(m_lockLayout, &KDualAction::activeChanged, this, &MainWindow::slotToggleLayoutLock);
  m_lockLayout->setActiveText(i18n("Unlock Layout"));
  m_lockLayout->setActiveToolTip(i18n("Unlock the window's layout"));
  m_lockLayout->setActiveIcon(QIcon::fromTheme(QStringLiteral("object-unlocked")));
  m_lockLayout->setInactiveText(i18n("Lock Layout"));
  m_lockLayout->setInactiveToolTip(i18n("Lock the window's layout"));
  m_lockLayout->setInactiveIcon(QIcon::fromTheme(QStringLiteral("object-locked")));
  actionCollection()->addAction(QStringLiteral("lock_layout"), m_lockLayout);

  action = actionCollection()->addAction(QStringLiteral("reset_layout"), this, SLOT(slotResetLayout()));
  action->setText(i18n("Reset Layout"));
  action->setToolTip(i18n("Reset the window's layout"));
  action->setIcon(QIcon::fromTheme(QStringLiteral("resetview")));

  m_toggleEntryEditor = new KToggleAction(i18n("Entry &Editor"), this);
  connect(m_toggleEntryEditor, &QAction::triggered, this, &MainWindow::slotToggleEntryEditor);
  m_toggleEntryEditor->setToolTip(i18n("Enable/disable the editor"));
  actionCollection()->addAction(QStringLiteral("toggle_edit_widget"), m_toggleEntryEditor);

  KStandardAction::preferences(this, SLOT(slotShowConfigDialog()), actionCollection());

  /*************************************************
   * Help menu
   *************************************************/
  action = actionCollection()->addAction(QStringLiteral("show_log"), this, SLOT(showLog()));
  action->setText(i18n("Show Log"));
  action->setIcon(QIcon::fromTheme(QStringLiteral("view-history")));
  if(Logger::self()->logFile().isEmpty()) {
    action->setEnabled(false);
  }

  /*************************************************
   * Short cuts
   *************************************************/
  KStandardAction::fullScreen(this, SLOT(slotToggleFullScreen()), this, actionCollection());
  KStandardAction::showMenubar(this, SLOT(slotToggleMenuBarVisibility()), actionCollection());

  /*************************************************
   * Collection Toolbar
   *************************************************/
  action = actionCollection()->addAction(QStringLiteral("change_entry_grouping_accel"), this, SLOT(slotGroupLabelActivated()));
  action->setText(i18n("Change Grouping"));
  actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::Key_G));

  m_entryGrouping = new KSelectAction(i18n("&Group Selection"), this);
  m_entryGrouping->setToolTip(i18n("Change the grouping of the collection"));
#if KWIDGETSADDONS_VERSION >= QT_VERSION_CHECK(5,78,0)
  void (KSelectAction::* triggeredInt)(int) = &KSelectAction::indexTriggered;
#else
  void (KSelectAction::* triggeredInt)(int) = &KSelectAction::triggered;
#endif
  connect(m_entryGrouping, triggeredInt, this, &MainWindow::slotChangeGrouping);
  actionCollection()->addAction(QStringLiteral("change_entry_grouping"), m_entryGrouping);

  action = actionCollection()->addAction(QStringLiteral("quick_filter_accel"), this, SLOT(slotFilterLabelActivated()));
  action->setText(i18n("Filter"));
  actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::Key_F));

  m_quickFilter = new GUI::LineEdit(this);
  m_quickFilter->setPlaceholderText(i18n("Filter here...")); // same text as kdepim and amarok
  m_quickFilter->setClearButtonEnabled(true);
  // same as Dolphin text edit
  m_quickFilter->setMinimumWidth(150);
  m_quickFilter->setMaximumWidth(300);
  // want to update every time the filter text changes
  connect(m_quickFilter, &QLineEdit::textChanged,
          this, &MainWindow::slotQueueFilter);
  connect(m_quickFilter, &KLineEdit::clearButtonClicked,
          this, &MainWindow::slotClearFilter);
  m_quickFilter->installEventFilter(this); // intercept keyEvents

  QWidgetAction* widgetAction = new QWidgetAction(this);
  widgetAction->setDefaultWidget(m_quickFilter);
  widgetAction->setText(i18n("Filter"));
  widgetAction->setToolTip(i18n("Filter the collection"));
  widgetAction->setProperty("isShortcutConfigurable", false);
  actionCollection()->addAction(QStringLiteral("quick_filter"), widgetAction);

  // final GUI setup is in initView()
}

#undef mimeIcon

void MainWindow::initDocument() {
  MARK;
  // initialize the image factory before the document is created
  ImageFactory::init();
  Data::Document* doc = Data::Document::self();
  Kernel::self()->resetHistory();

  KConfigGroup config(KSharedConfig::openConfig(), QLatin1String("General Options"));
  doc->setLoadAllImages(config.readEntry("Load All Images", false));

  // allow status messages from the document
  connect(doc, &Data::Document::signalStatusMsg,
          this, &MainWindow::slotStatusMsg);

  // do stuff that changes when the doc is modified
  connect(doc, &Data::Document::signalModified,
          this, &MainWindow::slotEnableModifiedActions);

  connect(doc, &Data::Document::signalCollectionAdded,
          Controller::self(), &Controller::slotCollectionAdded);
  connect(doc, &Data::Document::signalCollectionDeleted,
          Controller::self(), &Controller::slotCollectionDeleted);
  connect(doc, &Data::Document::signalCollectionModified,
          Controller::self(), &Controller::slotCollectionModified);

  connect(Kernel::self()->commandHistory(), &QUndoStack::cleanChanged,
          doc, &Data::Document::slotSetClean);
}

void MainWindow::initView() {
  MARK;

  m_entryView = new EntryView(this);
#ifndef USE_KHTML
  m_entryView->setAcceptDrops(true);
  m_entryView->installEventFilter(new DropHandler(this));
#endif

  connect(m_entryView, &EntryView::signalTellicoAction,
          this, &MainWindow::slotURLAction);

  // trick to make sure the group views always extend along the entire left or right side
  // using QMainWindow::setCorner does not seem to work
  // https://wiki.qt.io/Technical_FAQ#Is_it_possible_for_either_the_left_or_right_dock_areas_to_have_full_height_of_their_side_rather_than_having_the_bottom_take_the_full_width.3F
  m_dummyWindow = new QMainWindow(this);
#ifdef USE_KHTML
  m_entryView->view()->setWhatsThis(i18n("<qt>The <i>Entry View</i> shows a formatted view of the entry's contents.</qt>"));
  m_dummyWindow->setCentralWidget(m_entryView->view());
#else
  m_entryView->setWhatsThis(i18n("<qt>The <i>Entry View</i> shows a formatted view of the entry's contents.</qt>"));
  m_dummyWindow->setCentralWidget(m_entryView);
#endif
  m_dummyWindow->setWindowFlags(Qt::Widget);
  setCentralWidget(m_dummyWindow);

  m_collectionViewDock = new GUI::DockWidget(i18n("Collection View"), m_dummyWindow);
  m_collectionViewDock->setObjectName(QStringLiteral("collection_dock"));

  m_viewStack = new ViewStack(this);

  m_detailedView = m_viewStack->listView();
  Controller::self()->addObserver(m_detailedView);
  m_detailedView->setWhatsThis(i18n("<qt>The <i>Column View</i> shows the value of multiple fields "
                                    "for each entry.</qt>"));
  connect(Data::Document::self(), &Data::Document::signalCollectionImagesLoaded,
          m_detailedView, &DetailedListView::slotRefreshImages);

  m_iconView = m_viewStack->iconView();
  EntryIconModel* iconModel = new EntryIconModel(m_iconView);
  iconModel->setSourceModel(m_detailedView->model());
  m_iconView->setModel(iconModel);
  Controller::self()->addObserver(m_iconView);
  m_iconView->setWhatsThis(i18n("<qt>The <i>Icon View</i> shows each entry in the collection or group using "
                                "an icon, which may be an image in the entry.</qt>"));

  m_collectionViewDock->setWidget(m_viewStack);
  m_dummyWindow->addDockWidget(Qt::TopDockWidgetArea, m_collectionViewDock);
  actionCollection()->addAction(QStringLiteral("toggle_column_widget"), m_collectionViewDock->toggleViewAction());

  m_groupViewDock = new GUI::DockWidget(i18n("Group View"), this);
  m_groupViewDock->setObjectName(QStringLiteral("group_dock"));
  m_groupViewDock->setAllowedAreas(Qt::DockWidgetAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea));

  m_viewTabs = new GUI::TabWidget(this);
  m_viewTabs->setTabBarHidden(true);
  m_viewTabs->setDocumentMode(true);
  m_groupView = new GroupView(m_viewTabs);
  Controller::self()->addObserver(m_groupView);
  m_viewTabs->addTab(m_groupView, QIcon::fromTheme(QStringLiteral("folder")), i18n("Groups"));
  m_groupView->setWhatsThis(i18n("<qt>The <i>Group View</i> sorts the entries into groupings "
                                 "based on a selected field.</qt>"));
  m_groupViewDock->setWidget(m_viewTabs);
  addDockWidget(Qt::LeftDockWidgetArea, m_groupViewDock);
  actionCollection()->addAction(QStringLiteral("toggle_group_widget"), m_groupViewDock->toggleViewAction());

  EntrySelectionModel* proxySelect = new EntrySelectionModel(m_iconView->model(),
                                                             m_detailedView->selectionModel(),
                                                             this);
  m_iconView->setSelectionModel(proxySelect);

#ifndef USE_KHTML
  // Do custom themes override widget palettes? Ensure the EntryView remains consistent with the others
  m_entryView->setPalette(m_iconView->palette());
#endif

  // setting up GUI now rather than in initActions
  // initial parameter is default window size
  setupGUI(QSize(1280,800), Keys | ToolBar);
  createGUI();
}

void MainWindow::initConnections() {
  // have to toggle the menu item if the dialog gets closed
  connect(m_editDialog, &QDialog::finished,
          this, &MainWindow::slotEditDialogFinished);

  EntrySelectionModel* proxySelect = static_cast<EntrySelectionModel*>(m_iconView->selectionModel());
  connect(proxySelect, &EntrySelectionModel::entriesSelected,
          Controller::self(), &Controller::slotUpdateSelection);
  connect(proxySelect, &EntrySelectionModel::entriesSelected,
          m_editDialog, &EntryEditDialog::setContents);
  connect(proxySelect, &EntrySelectionModel::entriesSelected,
          m_entryView, &EntryView::showEntries);

  // let the group view call filters, too
  connect(m_groupView, &GroupView::signalUpdateFilter,
          this, &MainWindow::slotUpdateFilter);
  // use the EntrySelectionModel as a proxy so when entries get selected in the group view
  // the edit dialog and entry view are updated
  proxySelect->addSelectionProxy(m_groupView->selectionModel());
}

void MainWindow::initFileOpen(bool nofile_) {
  MARK;
  slotInit();
  // check to see if most recent file should be opened
  bool happyStart = false;
  if(!nofile_ && Config::reopenLastFile()) {
    // Config::lastOpenFile() is the full URL, protocol included
    QUrl lastFile(Config::lastOpenFile()); // empty string is actually ok, it gets handled
    if(!lastFile.isEmpty() && lastFile.isValid()) {
      myLog() << "Opening previous file:" << lastFile.toDisplayString(QUrl::PreferLocalFile);
      slotFileOpen(lastFile);
      happyStart = true;
    }
  }
  if(!happyStart) {
    myLog() << "Creating default book collection";
    // the document is created with an initial book collection, continue with that
    Controller::self()->slotCollectionAdded(Data::Document::self()->collection());

    m_fileSave->setEnabled(false);
    slotEnableOpenedActions();
    slotEnableModifiedActions(false);

    slotEntryCount();
    // tell the entry views and models that there are no images to load
    m_detailedView->slotRefreshImages();
  }

  QString text;
  if(Config::showWelcome()) {
    // show welcome text, even when opening an existing collection
    const auto welcomeFile = DataFileRegistry::self()->locate(QStringLiteral("welcome.html"));
    text = FileHandler::readTextFile(QUrl::fromLocalFile(welcomeFile));
    const int type = Kernel::self()->collectionType();
    text.replace(QLatin1String("$FGCOLOR$"), Config::templateTextColor(type).name());
    text.replace(QLatin1String("$BGCOLOR$"), Config::templateBaseColor(type).name());
    text.replace(QLatin1String("$COLOR1$"),  Config::templateHighlightedTextColor(type).name());
    text.replace(QLatin1String("$COLOR2$"),  Config::templateHighlightedBaseColor(type).name());
    text.replace(QLatin1String("$LINKCOLOR$"), Config::templateLinkColor(type).name());
    text.replace(QLatin1String("$IMGDIR$"),  ImageFactory::imageDir().url());
    text.replace(QLatin1String("$SUBTITLE$"),  i18n("Collection management software, free and simple"));
    text.replace(QLatin1String("$BANNER$"),
                 i18n("Welcome to the Tellico Collection Manager"));
    text.replace(QLatin1String("$WELCOMETEXT$"),
                 i18n("<h3>Tellico is a tool for managing collections of books, "
                      "videos, music, and whatever else you want to catalog.</h3>"
                      "<h3>New entries can be added to your collection by "
                      "<a href=\"tc:///coll_new_entry\">entering data manually</a> or by "
                      "<a href=\"tc:///edit_search_internet\">downloading data</a> from "
                      "various Internet sources.</h3>")
                 .replace(QLatin1String("<h3>"),  QLatin1String("<p>"))
                 .replace(QLatin1String("</h3>"), QLatin1String("</p>")));
    text.replace(QLatin1String("$FOOTER$"),
                 i18n("More information can be found in the <a href=\"help:/tellico\">documentation</a>. "
                      "You may also <a href=\"tc:///disable_welcome\">disable this welcome screen</a>."));
    QString iconPath = KIconLoader::global()->iconPath(QLatin1String("tellico"), -KIconLoader::SizeEnormous);
    if(iconPath.startsWith(QLatin1String(":/"))) {
      iconPath = QStringLiteral("qrc") + iconPath;
    } else {
      iconPath = QStringLiteral("file://") + iconPath;
    }

    text.replace(QLatin1String("$ICON$"),
                 QStringLiteral("<img src=\"%1\" align=\"top\" height=\"%2\" width=\"%2\" title=\"tellico\" />")
                 .arg(iconPath)
                 .arg(KIconLoader::SizeEnormous));
  } else {
    const int type = Kernel::self()->collectionType();
    text = QStringLiteral("<html><style>html { background-color:%1; }</style></html>")
           .arg(Config::templateBaseColor(type).name());
  }
  m_entryView->showText(text);

  m_initialized = true;
}

// These are general options.
// The options that can be changed in the "Configuration..." dialog
// are taken care of by the ConfigDialog object.
void MainWindow::saveOptions() {
  KConfigGroup config(KSharedConfig::openConfig(), QLatin1String("Main Window Options"));
  saveMainWindowSettings(config);
  config.writeEntry(QStringLiteral("Central Dock State"), m_dummyWindow->saveState());

  Config::setShowEditWidget(m_toggleEntryEditor->isChecked());
  // check any single dock widget, they all get locked together
  Config::setLockLayout(m_groupViewDock->isLocked());

  KConfigGroup filesConfig(KSharedConfig::openConfig(), QLatin1String("Recent Files"));
  m_fileOpenRecent->saveEntries(filesConfig);
  if(!isNewDocument()) {
    Config::setLastOpenFile(Data::Document::self()->URL().url());
  }

  Config::setViewWidget(m_viewStack->currentWidget());

  // historical reasons
  // sorting by count was faked by sorting by phantom second column
  const int sortColumn = m_groupView->sortRole() == RowCountRole ? 1 : 0;
  Config::setGroupViewSortColumn(sortColumn); // ok to use SortColumn key, save semantics
  Config::setGroupViewSortAscending(m_groupView->sortOrder() == Qt::AscendingOrder);

  if(m_loanView) {
    const int sortColumn = m_loanView->sortRole() == RowCountRole ? 1 : 0;
    Config::setLoanViewSortAscending(sortColumn); // ok to use SortColumn key, save semantics
    Config::setLoanViewSortAscending(m_loanView->sortOrder() == Qt::AscendingOrder);
  }

  if(m_filterView) {
    const int sortColumn = m_filterView->sortRole() == RowCountRole ? 1 : 0;
    Config::setFilterViewSortAscending(sortColumn); // ok to use SortColumn key, save semantics
    Config::setFilterViewSortAscending(m_filterView->sortOrder() == Qt::AscendingOrder);
  }

  // this is used in the EntryEditDialog constructor, too
  KConfigGroup editDialogConfig(KSharedConfig::openConfig(), QLatin1String("Edit Dialog Options"));
  KWindowConfig::saveWindowSize(m_editDialog->windowHandle(), editDialogConfig);

  saveCollectionOptions(Data::Document::self()->collection());
  Config::self()->save();
}

void MainWindow::readCollectionOptions(Tellico::Data::CollPtr coll_) {
  if(!coll_) {
    myDebug() << "Bad, no collection in MainWindow::readCollectionOptions()";
    return;
  }
  const QString configGroup = QStringLiteral("Options - %1").arg(CollectionFactory::typeName(coll_));
  KConfigGroup group(KSharedConfig::openConfig(), configGroup);

  QString defaultGroup = coll_->defaultGroupField();
  QString entryGroup, groupSortField;
  if(coll_->type() != Data::Collection::Base) {
    entryGroup = group.readEntry("Group By", defaultGroup);
    groupSortField = group.readEntry("GroupEntrySortField", QString());
  } else {
    QUrl url = Kernel::self()->URL();
    for(int i = 0; i < Config::maxCustomURLSettings(); ++i) {
      QUrl u(group.readEntry(QStringLiteral("URL_%1").arg(i)));
      if(url == u) {
        entryGroup = group.readEntry(QStringLiteral("Group By_%1").arg(i), defaultGroup);
        groupSortField = group.readEntry(QStringLiteral("GroupEntrySortField_%1").arg(i), QString());
        break;
      }
    }
    // fall back to old setting
    if(entryGroup.isEmpty()) {
      entryGroup = group.readEntry("Group By", defaultGroup);
    }
  }
  if(entryGroup.isEmpty() ||
     (!coll_->entryGroups().contains(entryGroup) && entryGroup != Data::Collection::s_peopleGroupName)) {
    entryGroup = defaultGroup;
  }
  m_groupView->setGroupField(entryGroup);

  if(!groupSortField.isEmpty()) {
    m_groupView->setEntrySortField(groupSortField);
  }

  QString entryXSLTFile;
  if(coll_->type() == Data::Collection::Base &&
     Data::Document::self()->URL().fileName() != TC_I18N1(Tellico::untitledFilename)) {
    // use a nested config group for template specific to custom collections
    // using the filename alone as a keyEvents
    KConfigGroup subGroup(&group, Data::Document::self()->URL().fileName());
    entryXSLTFile = subGroup.readEntry(QStringLiteral("Template Name"));
  }
  if(entryXSLTFile.isEmpty()) {
    // lookup by collection type
    entryXSLTFile = Config::templateName(coll_->type());
  }
  if(entryXSLTFile.isEmpty()) {
    entryXSLTFile = QStringLiteral("Fancy"); // should never happen, but just in case
  }
  m_entryView->setXSLTFile(entryXSLTFile + QLatin1String(".xsl"));

  // make sure the right combo element is selected
  slotUpdateCollectionToolBar(coll_);
}

void MainWindow::saveCollectionOptions(Tellico::Data::CollPtr coll_) {
  // don't save initial collection options, or empty collections
  if(!coll_ || coll_->entryCount() == 0 || isNewDocument()) {
    return;
  }

  int configIndex = -1;
  QString configGroup = QStringLiteral("Options - %1").arg(CollectionFactory::typeName(coll_));
  KConfigGroup config(KSharedConfig::openConfig(), configGroup);
  QString groupName;
  const QString groupEntrySort = m_groupView->entrySortField();
  if(m_entryGrouping->currentItem() > -1 &&
     static_cast<int>(coll_->entryGroups().count()) > m_entryGrouping->currentItem()) {
    if(m_entryGrouping->currentText() == (QLatin1Char('<') + i18n("People") + QLatin1Char('>'))) {
      groupName = Data::Collection::s_peopleGroupName;
    } else {
      groupName = Data::Document::self()->collection()->fieldNameByTitle(m_entryGrouping->currentText());
    }
    if(coll_->type() != Data::Collection::Base) {
      config.writeEntry("Group By", groupName);
      if(!groupEntrySort.isEmpty()) {
        config.writeEntry("GroupEntrySortField", groupEntrySort);
      }
    }
  }

  if(coll_->type() == Data::Collection::Base) {
    // all of this is to have custom settings on a per file basis
    QUrl url = Kernel::self()->URL();
    QList<QUrl> urls = QList<QUrl>() << url;
    QStringList groupBys = QStringList() << groupName;
    QStringList groupSorts = QStringList() << groupEntrySort;
    for(int i = 0; i < Config::maxCustomURLSettings(); ++i) {
      QUrl u = config.readEntry(QStringLiteral("URL_%1").arg(i), QUrl());
      if(!u.isEmpty() && url != u) {
        urls.append(u);
        QString g = config.readEntry(QStringLiteral("Group By_%1").arg(i), QString());
        groupBys.append(g);
        QString gs = config.readEntry(QStringLiteral("GroupEntrySortField_%1").arg(i), QString());
        groupSorts.append(gs);
      } else if(!u.isEmpty()) {
        configIndex = i;
      }
    }
    int limit = qMin(urls.count(), Config::maxCustomURLSettings());
    for(int i = 0; i < limit; ++i) {
      config.writeEntry(QStringLiteral("URL_%1").arg(i), urls[i].url());
      config.writeEntry(QStringLiteral("Group By_%1").arg(i), groupBys[i]);
      config.writeEntry(QStringLiteral("GroupEntrySortField_%1").arg(i), groupSorts[i]);
    }
  }
  m_detailedView->saveConfig(coll_, configIndex);
}

void MainWindow::readOptions() {
  KConfigGroup mainWindowConfig(KSharedConfig::openConfig(), QLatin1String("Main Window Options"));
  applyMainWindowSettings(mainWindowConfig);
  m_dummyWindow->restoreState(mainWindowConfig.readEntry(QStringLiteral("Central Dock State"), QByteArray()));

  m_viewStack->setCurrentWidget(Config::viewWidget());
  m_iconView->setMaxAllowedIconWidth(Config::maxIconSize());

  connect(toolBar(QStringLiteral("collectionToolBar")), &QToolBar::iconSizeChanged, this, &MainWindow::slotUpdateToolbarIcons);

  // initialize the recent file list
  KConfigGroup filesConfig(KSharedConfig::openConfig(), QLatin1String("Recent Files"));
  m_fileOpenRecent->loadEntries(filesConfig);

  // sort by count if column = 1
  int sortRole = Config::groupViewSortColumn() == 0 ? static_cast<int>(Qt::DisplayRole) : static_cast<int>(RowCountRole);
  Qt::SortOrder sortOrder = Config::groupViewSortAscending() ? Qt::AscendingOrder : Qt::DescendingOrder;
  m_groupView->setSorting(sortOrder, sortRole);

  BibtexHandler::s_quoteStyle = Config::useBraces() ? BibtexHandler::BRACES : BibtexHandler::QUOTES;

  // Don't read any options for the edit dialog here, since it's not yet initialized.
  // Put them in init()
}

bool MainWindow::querySaveModified() {
  bool completed = true;

  if(Data::Document::self()->isModified()) {
    QString str = i18n("The current file has been modified.\n"
                       "Do you want to save it?");
#if KWIDGETSADDONS_VERSION < QT_VERSION_CHECK(5, 100, 0)
    auto want_save = KMessageBox::warningYesNoCancel(this, str, i18n("Unsaved Changes"),
                                                     KStandardGuiItem::save(), KStandardGuiItem::discard());
    switch(want_save) {
      case KMessageBox::Yes:
        completed = fileSave();
        break;

      case KMessageBox::No:
        Data::Document::self()->setModified(false);
        completed = true;
        break;

      case KMessageBox::Cancel:
      default:
        completed = false;
        break;
    }
#else
    auto want_save = KMessageBox::warningTwoActionsCancel(this, str, i18n("Unsaved Changes"),
                                                          KStandardGuiItem::save(), KStandardGuiItem::discard());
    switch(want_save) {
      case KMessageBox::ButtonCode::PrimaryAction:
        completed = fileSave();
        break;

      case KMessageBox::ButtonCode::SecondaryAction:
        Data::Document::self()->setModified(false);
        completed = true;
        break;

      case KMessageBox::ButtonCode::Cancel:
      default:
        completed = false;
        break;
    }
#endif
  }

  return completed;
}

bool MainWindow::queryClose() {
  // in case we're still loading the images, cancel that
  Data::Document::self()->cancelImageWriting();
  const bool willClose = m_editDialog->queryModified() && querySaveModified();
  if(willClose) {
    ImageFactory::clean(true);
    saveOptions();
  }
  return willClose;
}

void MainWindow::slotFileNew(int type_) {
  slotStatusMsg(i18n("Creating new collection..."));

  // close the fields dialog
  slotHideCollectionFieldsDialog();

  if(m_editDialog->queryModified() && querySaveModified()) {
    // remove filter and loan tabs, they'll get re-added if needed
    if(m_filterView) {
      m_viewTabs->removeTab(m_viewTabs->indexOf(m_filterView));
      Controller::self()->removeObserver(m_filterView);
      delete m_filterView;
      m_filterView = nullptr;
    }
    if(m_loanView) {
      m_viewTabs->removeTab(m_viewTabs->indexOf(m_loanView));
      Controller::self()->removeObserver(m_loanView);
      delete m_loanView;
      m_loanView = nullptr;
    }
    m_viewTabs->setTabBarHidden(true);
    Data::Document::self()->newDocument(type_);
    myLog() << "Creating new collection, type" << CollectionFactory::typeName(type_);
    Kernel::self()->resetHistory();
    m_fileOpenRecent->setCurrentItem(-1);
    slotEnableOpenedActions();
    slotEnableModifiedActions(false);
    m_newDocument = true;
    ImageFactory::clean(false);
  }

  StatusBar::self()->clearStatus();
}

void MainWindow::slotFileNewByTemplate(const QString& collectionTemplate_) {
  slotStatusMsg(i18n("Creating new collection..."));

  // close the fields dialog
  slotHideCollectionFieldsDialog();

  if(m_editDialog->queryModified() && querySaveModified()) {
    openURL(QUrl::fromLocalFile(collectionTemplate_));
    myLog() << "Creating new collection from template:" << collectionTemplate_;
    Data::Document::self()->setURL(QUrl::fromLocalFile(TC_I18N1(Tellico::untitledFilename)));
    Kernel::self()->resetHistory();
    m_fileOpenRecent->setCurrentItem(-1);
    slotEnableOpenedActions();
    slotEnableModifiedActions(false);
    m_newDocument = true;
    ImageFactory::clean(false);
  }

  StatusBar::self()->clearStatus();
}

void MainWindow::slotFileOpen() {
  slotStatusMsg(i18n("Opening file..."));

  if(m_editDialog->queryModified() && querySaveModified()) {
    QString filter = i18n("Tellico Files") + QLatin1String(" (*.tc *.bc)");
    filter += QLatin1String(";;");
    filter += i18n("XML Files") + QLatin1String(" (*.xml)");
    filter += QLatin1String(";;");
    filter += i18n("All Files") + QLatin1String(" (*)");
    // keyword 'open'
    QString fileClass;
    const QUrl startUrl = KFileWidget::getStartUrl(QUrl(QStringLiteral("kfiledialog:///open")), fileClass);
    QUrl url = QFileDialog::getOpenFileUrl(this, i18n("Open File"), startUrl, filter);
    if(!url.isEmpty() && url.isValid()) {
      slotFileOpen(url);
      if(url.isLocalFile()) {
        KRecentDirs::add(fileClass, url.adjusted(QUrl::RemoveFilename|QUrl::StripTrailingSlash).path());
      }
    }
  }
  StatusBar::self()->clearStatus();
}

void MainWindow::slotFileOpen(const QUrl& url_) {
  slotStatusMsg(i18n("Opening file..."));

  // close the fields dialog
  slotHideCollectionFieldsDialog();

  // there seems to be a race condition at start between slotInit() and initFileOpen()
  // which means the edit dialog might not have been created yet
  if((!m_editDialog || m_editDialog->queryModified()) && querySaveModified()) {
    if(openURL(url_)) {
      m_fileOpenRecent->addUrl(url_);
      m_fileOpenRecent->setCurrentItem(-1);
    }
  }

  StatusBar::self()->clearStatus();
}

void MainWindow::slotFileOpenRecent(const QUrl& url_) {
  slotStatusMsg(i18n("Opening file..."));

  // close the fields dialog
  slotHideCollectionFieldsDialog();

  if(m_editDialog->queryModified() && querySaveModified()) {
    if(!openURL(url_)) {
      m_fileOpenRecent->removeUrl(url_);
      m_fileOpenRecent->setCurrentItem(-1);
    }
  } else {
    // the QAction shouldn't be checked now
    m_fileOpenRecent->setCurrentItem(-1);
  }

  StatusBar::self()->clearStatus();
}

void MainWindow::openFile(const QString& file_) {
  QUrl url(file_);
  if(!url.isEmpty() && url.isValid()) {
    slotFileOpen(url);
  }
}

bool MainWindow::openURL(const QUrl& url_) {
  MARK;
  // try to open document
  GUI::CursorSaver cs(Qt::WaitCursor);

  myLog() << "Opening collection file:" << url_.toDisplayString(QUrl::PreferLocalFile);
  bool success = Data::Document::self()->openDocument(url_);

  if(success) {
    Kernel::self()->resetHistory();
    m_quickFilter->clear();
    slotEnableOpenedActions();
    m_newDocument = false;
    slotEnableModifiedActions(Data::Document::self()->isModified()); // doc might add some stuff
  } else if(!m_initialized) {
    // special case on startup when openURL() is called with a command line argument
    // and that URL can't be opened. The window still needs to be initialized
    // the doc object is created with an initial book collection, continue with that
    Controller::self()->slotCollectionAdded(Data::Document::self()->collection());

    m_fileSave->setEnabled(false);
    slotEnableOpenedActions();
    slotEnableModifiedActions(false);

    slotEntryCount();
  }
  // slotFileOpen(URL) gets called when opening files on the command line
  // so go ahead and make sure m_initialized is set.
  m_initialized = true;

  // remove filter and loan tabs, they'll get re-added if needed
  if(m_filterView && m_filterView->isEmpty()) {
    m_viewTabs->removeTab(m_viewTabs->indexOf(m_filterView));
    Controller::self()->removeObserver(m_filterView);
    delete m_filterView;
    m_filterView = nullptr;
  }
  if(m_loanView && m_loanView->isEmpty()) {
    m_viewTabs->removeTab(m_viewTabs->indexOf(m_loanView));
    Controller::self()->removeObserver(m_loanView);
    delete m_loanView;
    m_loanView = nullptr;
  }
  Controller::self()->hideTabs(); // does conditional check

  return success;
}

void MainWindow::slotFileSave() {
  fileSave();
}

bool MainWindow::fileSave() {
  if(!m_editDialog->queryModified()) {
    return false;
  }
  slotStatusMsg(i18n("Saving file..."));

  bool ret = true;
  if(isNewDocument()) {
    ret = fileSaveAs();
  } else {
    // special check: if there are more than 200 images AND the "Write Images In File" config key
    // is set, then warn user that performance may suffer, and write result
    if(Config::imageLocation() == Config::ImagesInFile &&
       Config::askWriteImagesInFile() &&
       Data::Document::self()->imageCount() > MAX_IMAGES_WARN_PERFORMANCE) {
      QString msg = i18n("<qt><p>You are saving a file with many images, which causes Tellico to "
                         "slow down significantly. Do you want to save the images separately in "
                         "Tellico's data directory to improve performance?</p><p>Your choice can "
                         "always be changed in the configuration dialog.</p></qt>");

      KGuiItem yes(i18n("Save Images Separately"));
      KGuiItem no(i18n("Save Images in File"));

#if KWIDGETSADDONS_VERSION < QT_VERSION_CHECK(5, 100, 0)
      auto res = KMessageBox::warningYesNo(this, msg, QString() /* caption */, yes, no);
      if(res == KMessageBox::No) {
#else
      auto res = KMessageBox::warningTwoActions(this, msg, QString() /* caption */, yes, no);
      if(res == KMessageBox::ButtonCode::SecondaryAction) {
#endif
        Config::setImageLocation(Config::ImagesInAppDir);
      }
      Config::setAskWriteImagesInFile(false);
    }

    GUI::CursorSaver cs(Qt::WaitCursor);
    myLog() << "Saving collection file:" << Data::Document::self()->URL().toDisplayString(QUrl::PreferLocalFile);
    if(Data::Document::self()->saveDocument(Data::Document::self()->URL())) {
      Kernel::self()->resetHistory();
      m_newDocument = false;
      updateCaption(false);
      m_fileSave->setEnabled(false);
      m_detailedView->sourceModel()->clearSaveState();
    } else {
      ret = false;
    }
  }

  StatusBar::self()->clearStatus();
  return ret;
}

void MainWindow::slotFileSaveAs() {
  fileSaveAs();
}

bool MainWindow::fileSaveAs() {
  if(!m_editDialog->queryModified()) {
    return false;
  }

  slotStatusMsg(i18n("Saving file with a new filename..."));

  QString filter = i18n("Tellico Files") + QLatin1String(" (*.tc *.bc)");
  filter += QLatin1String(";;");
  filter += i18n("All Files") + QLatin1String(" (*)");

  // keyword 'open'
  QString fileClass;
  const QUrl startUrl = KFileWidget::getStartUrl(QUrl(QStringLiteral("kfiledialog:///open")), fileClass);
  const QUrl url = QFileDialog::getSaveFileUrl(this, i18n("Save As"), startUrl, filter);

  if(url.isEmpty()) {
    StatusBar::self()->clearStatus();
    return false;
  }
  if(url.isLocalFile()) {
    KRecentDirs::add(fileClass, url.adjusted(QUrl::RemoveFilename|QUrl::StripTrailingSlash).path());
  }

  bool ret = true;
  if(url.isValid()) {
    GUI::CursorSaver cs(Qt::WaitCursor);
    m_savingImageLocationChange = true;
    // Overwriting an existing file was already confirmed in QFileDialog::getSaveFileUrl()
    if(Data::Document::self()->saveDocument(url, true /* force */)) {
      Kernel::self()->resetHistory();
      KRecentDocument::add(url);
      m_fileOpenRecent->addUrl(url);
      updateCaption(false);
      m_newDocument = false;
      m_fileSave->setEnabled(false);
      m_detailedView->sourceModel()->clearSaveState();
    } else {
      ret = false;
    }
    m_savingImageLocationChange = false;
  }

  StatusBar::self()->clearStatus();
  return ret;
}

void MainWindow::slotFileSaveAsTemplate() {
  QScopedPointer<CollectionTemplateDialog> dlg(new CollectionTemplateDialog(this));
  if(dlg->exec() != QDialog::Accepted) {
    return;
  }

  const QString templateName = dlg->templateName();
  if(templateName.isEmpty()) {
    return;
  }
  const QString baseName = Tellico::saveLocation(QStringLiteral("collection-templates/")) + templateName;

  // first, save the collection template, which copies the collection fields and filters, but nothing else
  const QString collFile = baseName + QLatin1String(".tc");
  Data::Document::self()->saveDocumentTemplate(QUrl::fromLocalFile(collFile), templateName);

  // next, save the template descriptions in a config file
  const QString specFile = baseName + QLatin1String(".spec");
  auto spec = KSharedConfig::openConfig(specFile, KConfig::SimpleConfig)->group(QString());
  spec.writeEntry("Name", templateName);
  spec.writeEntry("Comment", dlg->templateComment());
  spec.writeEntry("Icon", dlg->templateIcon());
}

void MainWindow::slotFilePrint() {
  doPrint(Print);
}

void MainWindow::slotFilePrintPreview() {
  doPrint(PrintPreview);
}

void MainWindow::doPrint(PrintAction action_) {
  slotStatusMsg(i18n("Printing..."));

  // If the collection is being filtered, warn the user
  if(m_detailedView->filter()) {
    QString str = i18n("The collection is currently being filtered to show a limited subset of "
                       "the entries. Only the visible entries will be printed. Continue?");
    int ret = KMessageBox::warningContinueCancel(this, str, QString(), KStandardGuiItem::print(),
                                                 KStandardGuiItem::cancel(), QStringLiteral("WarnPrintVisible"));
    if(ret == KMessageBox::Cancel) {
      StatusBar::self()->clearStatus();
      return;
    }
  }

  if(!m_printHandler) {
    m_printHandler = new PrintHandler(this);
  }
  m_printHandler->setEntries(m_detailedView->visibleEntries());
  m_printHandler->setColumns(m_detailedView->visibleColumns());
  if(action_ == Print) {
    m_printHandler->print();
  } else {
    m_printHandler->printPreview();
  }

  StatusBar::self()->clearStatus();
}

void MainWindow::slotFileQuit() {
  slotStatusMsg(i18n("Exiting..."));

  close(); // will call queryClose()

  StatusBar::self()->clearStatus();
}

void MainWindow::slotEditCut() {
  activateEditSlot("cut()");
}

void MainWindow::slotEditCopy() {
  activateEditSlot("copy()");
}

void MainWindow::slotEditPaste() {
  activateEditSlot("paste()");
}

void MainWindow::activateEditSlot(const char* slot_) {
  // the edit widget is the only one that copies, cuts, and pastes
  // the entry view can copy
  QWidget* w;
  if(m_editDialog->isVisible()) {
    w = m_editDialog->focusWidget();
  } else {
    w = qApp->focusWidget();
  }

  while(w && w->isVisible()) {
    const QMetaObject* meta = w->metaObject();
    const int idx = meta->indexOfSlot(slot_);
    if(idx > -1) {
//      myDebug() << "MainWindow invoking" << meta->method(idx).methodSignature();
      meta->method(idx).invoke(w, Qt::DirectConnection);
      break;
    } else {
//      myDebug() << "did not find" << slot_ << "in" << meta->className();
      w = qobject_cast<QWidget*>(w->parent());
    }
  }
}

void MainWindow::slotEditSelectAll() {
  m_detailedView->selectAllVisible();
}

void MainWindow::slotEditDeselect() {
  Controller::self()->slotUpdateSelection(Data::EntryList());
}

void MainWindow::slotToggleEntryEditor() {
  if(m_toggleEntryEditor->isChecked()) {
    m_editDialog->show();
  } else {
    m_editDialog->hide();
  }
}

void MainWindow::slotShowConfigDialog() {
  if(!m_configDlg) {
    m_configDlg = new ConfigDialog(this);
    connect(m_configDlg, &ConfigDialog::signalConfigChanged,
            this, &MainWindow::slotHandleConfigChange);
    connect(m_configDlg, &QDialog::finished,
            this, &MainWindow::slotHideConfigDialog);
  } else {
    activateDialog(m_configDlg);
  }
  m_configDlg->show();
}

void MainWindow::slotHideConfigDialog() {
  if(m_configDlg) {
    m_configDlg->hide();
    m_configDlg->deleteLater();
    m_configDlg = nullptr;
  }
}

void MainWindow::slotStatusMsg(const QString& text_) {
  m_statusBar->setStatus(text_);
}

void MainWindow::slotClearStatus() {
  StatusBar::self()->clearStatus();
}

void MainWindow::slotEntryCount() {
  Data::CollPtr coll = Data::Document::self()->collection();
  if(!coll) {
    return;
  }

  int count = coll->entryCount();
  QString text = i18n("Total entries: %1", count);

  int selectCount = Controller::self()->selectedEntries().count();
  int filterCount = m_detailedView->visibleItems();
  // if more than one book is selected, add the number of selected books
  if(filterCount < count && selectCount > 1) {
    text += QLatin1Char(' ');
    text += i18n("(%1 filtered; %2 selected)", filterCount, selectCount);
  } else if(filterCount < count) {
    text += QLatin1Char(' ');
    text += i18n("(%1 filtered)", filterCount);
  } else if(selectCount > 1) {
    text += QLatin1Char(' ');
    text += i18n("(%1 selected)", selectCount);
  }

  m_statusBar->setCount(text);
}

void MainWindow::slotEnableOpenedActions() {
  slotUpdateToolbarIcons();

  updateCollectionActions();

  // close the filter dialog when a new collection is opened
  slotHideFilterDialog();
  slotStringMacroDialogFinished();
}

void MainWindow::slotEnableModifiedActions(bool modified_ /*= true*/) {
  updateCaption(modified_);
  updateCollectionActions();
  m_fileSave->setEnabled(modified_);
}

void MainWindow::slotHandleConfigChange() {
  const int imageLocation = Config::imageLocation();
  const bool autoCapitalize = Config::autoCapitalization();
  const bool autoFormat = Config::autoFormat();
  const QStringList articles = Config::articleList();
  const QStringList nocaps = Config::noCapitalizationList();
  const QStringList suffixes = Config::nameSuffixList();
  const QStringList prefixes = Config::surnamePrefixList();

  m_configDlg->saveConfiguration();

  // only modified if there are entries and image location is changed
  if(imageLocation != Config::imageLocation() && !Data::Document::self()->isEmpty()) {
    slotImageLocationChanged();
  }

  if(autoCapitalize != Config::autoCapitalization() ||
    autoFormat != Config::autoFormat() ||
    articles != Config::articleList() ||
    nocaps != Config::noCapitalizationList() ||
    suffixes != Config::nameSuffixList() ||
    prefixes != Config::surnamePrefixList()) {
    // invalidate all groups
    Data::Document::self()->collection()->invalidateGroups();
    // refreshing the title causes the group view to refresh
    Controller::self()->slotRefreshField(Data::Document::self()->collection()->fieldByName(QStringLiteral("title")));
  }

  QString entryXSLTFile = Config::templateName(Kernel::self()->collectionType());
  m_entryView->setXSLTFile(entryXSLTFile + QLatin1String(".xsl"));
}

void MainWindow::slotUpdateCollectionToolBar(Tellico::Data::CollPtr coll_) {
  if(!coll_) {
    myWarning() << "no collection pointer!";
    return;
  }

  QString current = m_groupView->groupBy();
  if(current.isEmpty() || !coll_->entryGroups().contains(current)) {
    current = coll_->defaultGroupField();
  }

  const QStringList groups = coll_->entryGroups();
  if(groups.isEmpty()) {
    m_entryGrouping->clear();
    return;
  }

  QMap<QString, QString> groupMap; // use a map so they get sorted
  foreach(const QString& groupName, groups) {
    // special case for people "pseudo-group"
    if(groupName == Data::Collection::s_peopleGroupName) {
      groupMap.insert(groupName, QLatin1Char('<') + i18n("People") + QLatin1Char('>'));
    } else {
      groupMap.insert(groupName, coll_->fieldTitleByName(groupName));
    }
  }

  const QStringList titles = groupMap.values();
  if(titles == m_entryGrouping->items()) {
    // no need to update anything
    return;
  }
  const QStringList names = groupMap.keys();
  int index = names.indexOf(current);
  if(index == -1) {
    current = names[0];
    index = 0;
  }
  m_entryGrouping->setItems(titles);
  m_entryGrouping->setCurrentItem(index);
  // in case the current grouping field get modified to be non-grouping...
  m_groupView->setGroupField(current); // don't call slotChangeGrouping() since it adds an undo item

  // TODO::I have no idea how to get the combobox to update its size
  // this is the hackiest of hacks, taken from KXmlGuiWindow::saveNewToolbarConfig()
  // the window flickers as toolbar resizes, unavoidable?
  // crashes if removeClient//addClient is called here, need to do later in event loop
  QTimer::singleShot(0, this, &MainWindow::guiFactoryReset);
}

void MainWindow::slotChangeGrouping() {
  const QString title = m_entryGrouping->currentText();

  QString groupName = Data::Document::self()->collection()->fieldNameByTitle(title);
  if(groupName.isEmpty()) {
    if(title == (QLatin1Char('<') + i18n("People") + QLatin1Char('>'))) {
      groupName = Data::Collection::s_peopleGroupName;
    } else {
      groupName = Data::Document::self()->collection()->defaultGroupField();
    }
  }
  m_groupView->setGroupField(groupName);
  m_viewTabs->setCurrentWidget(m_groupView);
}

void MainWindow::slotShowReportDialog() {
  if(!m_reportDlg) {
    m_reportDlg = new ReportDialog(this);
    connect(m_reportDlg, &QDialog::finished,
            this, &MainWindow::slotHideReportDialog);
  } else {
    activateDialog(m_reportDlg);
  }
  m_reportDlg->show();
}

void MainWindow::slotHideReportDialog() {
  if(m_reportDlg) {
    m_reportDlg->hide();
    m_reportDlg->deleteLater();
    m_reportDlg = nullptr;
  }
}

void MainWindow::XSLTError() {
  QString str = i18n("Tellico encountered an error in XSLT processing.") + QLatin1Char('\n');
  str += i18n("Please check your installation.");
  Kernel::self()->sorry(str);
}

void MainWindow::slotShowFilterDialog() {
  if(!m_filterDlg) {
    m_filterDlg = new FilterDialog(FilterDialog::CreateFilter, this); // allow saving
    m_quickFilter->setEnabled(false);
    connect(m_filterDlg, &FilterDialog::signalCollectionModified,
            Data::Document::self(), &Data::Document::slotSetModified);
    connect(m_filterDlg, &FilterDialog::signalUpdateFilter,
            this, &MainWindow::slotUpdateFilter);
    connect(m_filterDlg, &QDialog::finished,
            this, &MainWindow::slotHideFilterDialog);
  } else {
    activateDialog(m_filterDlg);
  }
  m_filterDlg->setFilter(m_detailedView->filter());
  m_filterDlg->show();
}

void MainWindow::slotHideFilterDialog() {
//  m_quickFilter->blockSignals(false);
  m_quickFilter->setEnabled(true);
  if(m_filterDlg) {
    m_filterDlg->hide();
    m_filterDlg->deleteLater();
    m_filterDlg = nullptr;
  }
}

void MainWindow::slotQueueFilter() {
  if(m_dontQueueFilter) {
    return;
  }
  m_queuedFilters++;
  QTimer::singleShot(200, this, &MainWindow::slotCheckFilterQueue);
}

void MainWindow::slotCheckFilterQueue() {
  m_queuedFilters--;
  if(m_queuedFilters > 0) {
    return;
  }

  setFilter(m_quickFilter->text());
}

void MainWindow::slotUpdateFilter(FilterPtr filter_) {
  // Can't just block signals because clear button won't show then
  m_dontQueueFilter = true;
  if(filter_) {
    // for a saved filter, show the filter name and a leading icon
    if(m_quickFilter->actions().isEmpty()) {
      m_quickFilter->addAction(QIcon::fromTheme(QStringLiteral("view-filter")), QLineEdit::LeadingPosition);
    }
    m_quickFilter->setText(QLatin1Char('<') + filter_->name() + QLatin1Char('>'));
  } else {
    m_quickFilter->setText(QStringLiteral(" ")); // To be able to clear custom filter
  }
  Controller::self()->slotUpdateFilter(filter_);
  m_dontQueueFilter = false;
}

void MainWindow::setFilter(const QString& text_) {
  // might have an "action" associated if a saved filter was displayed
  auto actions = m_quickFilter->actions();
  if(!actions.isEmpty()) {
    // clear all of the saved filter name
    slotClearFilter();
    return;
  }
  // update the line edit in case the filter was set by DBUS
  m_quickFilter->setText(text_);

  FilterParser parser(text_.trimmed(), Config::quickFilterRegExp());
  parser.setCollection(Data::Document::self()->collection());
  FilterPtr filter = parser.filter();
  // only update filter if one exists or did exist
  if(filter || m_detailedView->filter()) {
    Controller::self()->slotUpdateFilter(filter);
  }
}

void MainWindow::slotShowCollectionFieldsDialog() {
  if(!m_collFieldsDlg) {
    m_collFieldsDlg = new CollectionFieldsDialog(Data::Document::self()->collection(), this);
    m_collFieldsDlg->setNotifyKernel(true);
    connect(m_collFieldsDlg, &CollectionFieldsDialog::beginCommandGroup,
            Kernel::self(), &Kernel::beginCommandGroup);
    connect(m_collFieldsDlg, &CollectionFieldsDialog::endCommandGroup,
            Kernel::self(), &Kernel::endCommandGroup);
    connect(m_collFieldsDlg, &CollectionFieldsDialog::addField,
            Kernel::self(), &Kernel::addField);
    connect(m_collFieldsDlg, &CollectionFieldsDialog::modifyField,
            Kernel::self(), &Kernel::modifyField);
    connect(m_collFieldsDlg, &CollectionFieldsDialog::removeField,
            Kernel::self(), &Kernel::removeField);
    connect(m_collFieldsDlg, &CollectionFieldsDialog::reorderFields,
            Kernel::self(), &Kernel::reorderFields);
    connect(m_collFieldsDlg, &QDialog::finished,
            this, &MainWindow::slotHideCollectionFieldsDialog);
  } else {
    activateDialog(m_collFieldsDlg);
  }
  m_collFieldsDlg->show();
}

void MainWindow::slotHideCollectionFieldsDialog() {
  if(m_collFieldsDlg) {
    m_collFieldsDlg->hide();
    m_collFieldsDlg->deleteLater();
    m_collFieldsDlg = nullptr;
  }
}

void MainWindow::slotFileImport(int format_) {
  slotStatusMsg(i18n("Importing data..."));
  m_quickFilter->clear();

  Import::Format format = static_cast<Import::Format>(format_);
  bool checkURL = true;
  QUrl url;
  switch(ImportDialog::importTarget(format)) {
    case Import::File:
      {
        QString fileClass;
        const QUrl startUrl = KFileWidget::getStartUrl(QUrl(QStringLiteral("kfiledialog:///import")), fileClass);
        url = QFileDialog::getOpenFileUrl(this, i18n("Import File"), startUrl, ImportDialog::fileFilter(format));
        KRecentDirs::add(fileClass, url.adjusted(QUrl::RemoveFilename|QUrl::StripTrailingSlash).path());
      }
      break;

    case Import::Dir:
      // TODO: allow remote audiofile importing
      {
        const QString fileClass(QStringLiteral("ImportDir"));
        QString dirName = ImportDialog::startDir(format);
        if(dirName.isEmpty()) {
          dirName = KRecentDirs::dir(fileClass);
        }
        QString chosenDir = QFileDialog::getExistingDirectory(this, i18n("Import Directory"), dirName);
        url = QUrl::fromLocalFile(chosenDir);
        KRecentDirs::add(fileClass, chosenDir);
      }
      break;

    case Import::None:
    default:
      checkURL = false;
      break;
  }

  if(checkURL) {
    bool ok = !url.isEmpty() && url.isValid() && QFile::exists(url.toLocalFile());
    if(!ok) {
      StatusBar::self()->clearStatus();
      return;
    }
  }
  importFile(format, QList<QUrl>() << url);
  StatusBar::self()->clearStatus();
}

void MainWindow::slotFileExport(int format_) {
  slotStatusMsg(i18n("Exporting data..."));

  Export::Format format = static_cast<Export::Format>(format_);
  ExportDialog dlg(format, Data::Document::self()->collection(), this);

  if(dlg.exec() == QDialog::Rejected) {
    StatusBar::self()->clearStatus();
    return;
  }

  switch(ExportDialog::exportTarget(format)) {
    case Export::None:
      dlg.exportURL();
      break;

    case Export::Dir:
      myDebug() << "ExportDir not implemented!";
      break;

    case Export::File:
    {
      QString fileClass;
      const QUrl startUrl = KFileWidget::getStartUrl(QUrl(QStringLiteral("kfiledialog:///export")), fileClass);
      QUrl url = QFileDialog::getSaveFileUrl(this, i18n("Export As"), startUrl, dlg.fileFilter());
      if(url.isEmpty()) {
        StatusBar::self()->clearStatus();
        return;
      }

      if(url.isValid()) {
        if(url.isLocalFile()) {
          KRecentDirs::add(fileClass, url.adjusted(QUrl::RemoveFilename|QUrl::StripTrailingSlash).path());
        }
        GUI::CursorSaver cs(Qt::WaitCursor);
        dlg.exportURL(url);
      }
    }
    break;
  }

  StatusBar::self()->clearStatus();
}

void MainWindow::slotShowStringMacroDialog() {
  if(Data::Document::self()->collection()->type() != Data::Collection::Bibtex) {
    return;
  }

  if(!m_stringMacroDlg) {
    const Data::BibtexCollection* c = static_cast<Data::BibtexCollection*>(Data::Document::self()->collection().data());
    m_stringMacroDlg = new StringMapDialog(c->macroList(), this, false);
    m_stringMacroDlg->setWindowTitle(i18n("String Macros"));
    m_stringMacroDlg->setLabels(i18n("Macro"), i18n("String"));
    connect(m_stringMacroDlg, &QDialog::finished, this, &MainWindow::slotStringMacroDialogFinished);
  } else {
    activateDialog(m_stringMacroDlg);
  }
  m_stringMacroDlg->show();
}

void MainWindow::slotStringMacroDialogFinished(int result_) {
  // no point in checking if collection is bibtex, as dialog would never have been created
  if(!m_stringMacroDlg) {
    return;
  }
  if(result_ == QDialog::Accepted) {
    static_cast<Data::BibtexCollection*>(Data::Document::self()->collection().data())->setMacroList(m_stringMacroDlg->stringMap());
    Data::Document::self()->setModified(true);
  }
  m_stringMacroDlg->hide();
  m_stringMacroDlg->deleteLater();
  m_stringMacroDlg = nullptr;
}

void MainWindow::slotShowBibtexKeyDialog() {
  if(Data::Document::self()->collection()->type() != Data::Collection::Bibtex) {
    return;
  }

  if(!m_bibtexKeyDlg) {
    m_bibtexKeyDlg = new BibtexKeyDialog(Data::Document::self()->collection(), this);
    connect(m_bibtexKeyDlg, &QDialog::finished, this, &MainWindow::slotHideBibtexKeyDialog);
    connect(m_bibtexKeyDlg, &BibtexKeyDialog::signalUpdateFilter,
            this, &MainWindow::slotUpdateFilter);
  } else {
    activateDialog(m_bibtexKeyDlg);
  }
  m_bibtexKeyDlg->show();
}

void MainWindow::slotHideBibtexKeyDialog() {
  if(m_bibtexKeyDlg) {
    m_bibtexKeyDlg->deleteLater();
    m_bibtexKeyDlg = nullptr;
  }
}

void MainWindow::slotNewEntry() {
  m_toggleEntryEditor->setChecked(true);
  slotToggleEntryEditor();
  m_editDialog->slotHandleNew();
}

void MainWindow::slotEditDialogFinished() {
  m_toggleEntryEditor->setChecked(false);
}

void MainWindow::slotShowEntryEditor() {
  m_toggleEntryEditor->setChecked(true);
  m_editDialog->show();
  activateDialog(m_editDialog);
}

void MainWindow::slotConvertToBibliography() {
  // only book collections can be converted to bibtex
  Data::CollPtr coll = Data::Document::self()->collection();
  if(!coll || coll->type() != Data::Collection::Book) {
    return;
  }

  GUI::CursorSaver cs;

  // need to make sure all images are transferred
  Data::Document::self()->loadAllImagesNow();

  Data::CollPtr newColl = Data::BibtexCollection::convertBookCollection(coll);
  if(newColl) {
    m_newDocument = true;
    Kernel::self()->replaceCollection(newColl);
    m_fileOpenRecent->setCurrentItem(-1);
    slotUpdateToolbarIcons();
    updateCollectionActions();
  } else {
    myWarning() << "ERROR: no bibliography created!";
  }
}

void MainWindow::slotCiteEntry(int action_) {
  StatusBar::self()->setStatus(i18n("Creating citations..."));
  Cite::ActionManager* man = Cite::ActionManager::self();
  man->cite(static_cast<Cite::CiteAction>(action_), Controller::self()->selectedEntries());
  if(man->hasError()) {
    Kernel::self()->sorry(man->errorString());
  }
  StatusBar::self()->clearStatus();
}

void MainWindow::slotShowFetchDialog() {
  if(!m_fetchDlg) {
    m_fetchDlg = new FetchDialog(this);
    connect(m_fetchDlg, &QDialog::finished, this, &MainWindow::slotHideFetchDialog);
    connect(Controller::self(), &Controller::collectionAdded, m_fetchDlg, &FetchDialog::slotResetCollection);
  } else {
    activateDialog(m_fetchDlg);
  }
  m_fetchDlg->show();
}

void MainWindow::slotHideFetchDialog() {
  if(m_fetchDlg) {
    m_fetchDlg->hide();
    m_fetchDlg->deleteLater();
    m_fetchDlg = nullptr;
  }
}

bool MainWindow::importFile(Tellico::Import::Format format_, const QUrl& url_, Tellico::Import::Action action_) {
  // try to open document
  GUI::CursorSaver cs(Qt::WaitCursor);

  bool failed = false;
  Data::CollPtr coll;
  if(!url_.isEmpty() && url_.isValid() && NetAccess::exists(url_, true, this)) {
    coll = ImportDialog::importURL(format_, url_);
  } else {
    Kernel::self()->sorry(TC_I18N2(errorLoad, url_.fileName()));
    failed = true;
  }

  if(!coll && !m_initialized) {
    // special case on startup when openURL() is called with a command line argument
    // and that URL can't be opened. The window still needs to be initialized
    // the doc object is created with an initial book collection, continue with that
    Controller::self()->slotCollectionAdded(Data::Document::self()->collection());
    m_fileSave->setEnabled(false);
    slotEnableOpenedActions();
    slotEnableModifiedActions(false);
    slotEntryCount();
    m_fileOpenRecent->setCurrentItem(-1);
    m_initialized = true;
    failed = true;
  } else if(coll) {
    // this is rather dumb, but I'm too lazy to find the bug
    // if the document isn't initialized, then Tellico crashes
    // since Document::replaceCollection() ends up calling lots of stuff that isn't initialized
    if(!m_initialized) {
      Controller::self()->slotCollectionAdded(Data::Document::self()->collection());
      m_initialized = true;
    }
    failed = !importCollection(coll, action_);
  }

  StatusBar::self()->clearStatus();
  return !failed; // return true means success
}

bool MainWindow::exportCollection(Tellico::Export::Format format_, const QUrl& url_, bool filtered_) {
  if(!url_.isValid()) {
    myDebug() << "invalid URL:" << url_;
    return false;
  }

  GUI::CursorSaver cs;
  const Data::CollPtr coll = Data::Document::self()->collection();
  if(!coll) {
    return false;
  }

  // only bibliographies can export to bibtex or bibtexml
  const bool isBibtex = (coll->type() == Data::Collection::Bibtex);
  if(!isBibtex && (format_ == Export::Bibtex || format_ == Export::Bibtexml)) {
    return false;
  }
  // only books and bibliographies can export to alexandria
  const bool isBook = (coll->type() == Data::Collection::Book);
  if(!isBibtex && !isBook && format_ == Export::Alexandria) {
    return false;
  }

  return ExportDialog::exportCollection(coll, filtered_ ? Controller::self()->visibleEntries() : coll->entries(),
                                        format_, url_);
}

bool MainWindow::showEntry(Data::ID id) {
  Data::EntryPtr entry = Data::Document::self()->collection()->entryById(id);
  if(entry) {
    m_entryView->showEntry(entry);
  }
  return entry;
}

void MainWindow::addFilterView() {
  if(m_filterView) {
    return;
  }

  m_filterView = new FilterView(m_viewTabs);
  Controller::self()->addObserver(m_filterView);
  m_viewTabs->insertTab(1, m_filterView, QIcon::fromTheme(QStringLiteral("view-filter")), i18n("Filters"));
  m_filterView->setWhatsThis(i18n("<qt>The <i>Filter View</i> shows the entries which meet certain "
                                  "filter rules.</qt>"));

  connect(m_filterView, &FilterView::signalUpdateFilter,
          this, &MainWindow::slotUpdateFilter);
  // use the EntrySelectionModel as a proxy so when entries get selected in the filter view
  // the edit dialog and entry view are updated
  // TODO: consider using KSelectionProxyModel
  static_cast<EntrySelectionModel*>(m_iconView->selectionModel())->addSelectionProxy(m_filterView->selectionModel());

  // sort by count if column = 1
  int sortRole = Config::filterViewSortColumn() == 0 ? static_cast<int>(Qt::DisplayRole) : static_cast<int>(RowCountRole);
  Qt::SortOrder sortOrder = Config::filterViewSortAscending() ? Qt::AscendingOrder : Qt::DescendingOrder;
  m_filterView->setSorting(sortOrder, sortRole);
}

void MainWindow::addLoanView() {
  if(m_loanView) {
    return;
  }

  m_loanView = new LoanView(m_viewTabs);
  Controller::self()->addObserver(m_loanView);
  m_viewTabs->insertTab(2, m_loanView, QIcon::fromTheme(QStringLiteral("kaddressbook")), i18n("Loans"));
  m_loanView->setWhatsThis(i18n("<qt>The <i>Loan View</i> shows a list of all the people who "
                                "have borrowed items from your collection.</qt>"));

  // use the EntrySelectionModel as a proxy so when entries get selected in the loan view
  // the edit dialog and entry view are updated
  // TODO: consider using KSelectionProxyModel
  static_cast<EntrySelectionModel*>(m_iconView->selectionModel())->addSelectionProxy(m_loanView->selectionModel());

  // sort by count if column = 1
  int sortRole = Config::loanViewSortColumn() == 0 ? static_cast<int>(Qt::DisplayRole) : static_cast<int>(RowCountRole);
  Qt::SortOrder sortOrder = Config::loanViewSortAscending() ? Qt::AscendingOrder : Qt::DescendingOrder;
  m_loanView->setSorting(sortOrder, sortRole);
}

void MainWindow::updateCaption(bool modified_) {
  QString caption;
  if(Data::Document::self()->collection()) {
    caption = Data::Document::self()->collection()->title();
  }
  if(!m_newDocument) {
    if(!caption.isEmpty()) {
       caption += QLatin1String(" - ");
    }
    QUrl u = Data::Document::self()->URL();
    if(u.isLocalFile() && u.fileName() == TC_I18N1(Tellico::untitledFilename)) {
      // for new files, the filename is set to Untitled in Data::Document
      caption += u.fileName();
    } else {
      caption += u.toDisplayString(QUrl::PreferLocalFile);
    }
  }
  setCaption(caption, modified_);
}

void MainWindow::slotUpdateToolbarIcons() {
  // first change the icon for the menu item
  if(Kernel::self()->collectionType() == Data::Collection::Base) {
    m_newEntry->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
  } else {
    m_newEntry->setIcon(QIcon(QLatin1String(":/icons/") + Kernel::self()->collectionTypeName()));
  }
}

void MainWindow::slotGroupLabelActivated() {
  // need entry grouping combo id
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
  for(auto obj : m_entryGrouping->associatedWidgets()) {
#else
  for(auto obj : m_entryGrouping->associatedObjects()) {
#endif
    if(auto widget = ::qobject_cast<KToolBar*>(obj)) {
      auto container = m_entryGrouping->requestWidget(widget);
      auto combo = ::qobject_cast<QComboBox*>(container); //krazy:exclude=qclasses
      if(combo) {
        combo->showPopup();
        break;
      }
    }
  }
}

void MainWindow::slotFilterLabelActivated() {
  m_quickFilter->setFocus();
  m_quickFilter->selectAll();
}

void MainWindow::slotClearFilter() {
  auto actions = m_quickFilter->actions();
  if(!actions.isEmpty()) {
    m_quickFilter->removeAction(actions.first());
  }
  m_quickFilter->clear();
  slotQueueFilter();
}

void MainWindow::slotRenameCollection() {
  Kernel::self()->renameCollection();
}

void MainWindow::slotImageLocationMismatch() {
  // TODO: having a single image location mismatch should not be reason to completely save the whole document
  QTimer::singleShot(0, this, &MainWindow::slotImageLocationChanged);
}

void MainWindow::slotImageLocationChanged() {
  if(m_savingImageLocationChange) {
    return;
  }
  m_savingImageLocationChange = true;
  Data::Document::self()->slotSetModified();
  KMessageBox::information(this, QLatin1String("<qt>") +
                                 i18n("Some images are not saved in the configured location. The current file "
                                      "must be saved and the images will be transferred to the new location.") +
                                 QLatin1String("</qt>"));
  fileSave();
  m_savingImageLocationChange = false;
}

void MainWindow::updateCollectionActions() {
  if(!Data::Document::self()->collection()) {
    return;
  }

  stateChanged(QStringLiteral("collection_reset"));

  Data::Collection::Type type = Data::Document::self()->collection()->type();
  stateChanged(QLatin1String("is_") + CollectionFactory::typeName(type));

  Controller::self()->updateActions();
  // special case when there are no available data sources
  if(m_fetchActions.isEmpty() && m_updateAll) {
    m_updateAll->setEnabled(false);
  }
}

void MainWindow::updateEntrySources() {
  const QString actionListName = QStringLiteral("update_entry_actions");
  unplugActionList(actionListName);
  for(auto action : m_fetchActions) {
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    for(auto obj : action->associatedWidgets()) {
#else
    for(auto obj : action->associatedObjects()) {
#endif
      if(auto widget = ::qobject_cast<QWidget*>(obj)) {
        widget->removeAction(action);
      }
    }
    m_updateMapper->removeMappings(action);
  }
  qDeleteAll(m_fetchActions);
  m_fetchActions.clear();

  void (QAction::* triggeredBool)(bool) = &QAction::triggered;
  void (QSignalMapper::* mapVoid)() = &QSignalMapper::map;
  Fetch::FetcherVec vec = Fetch::Manager::self()->fetchers(Kernel::self()->collectionType());
  foreach(Fetch::Fetcher::Ptr fetcher, vec) {
    QAction* action = new QAction(Fetch::Manager::fetcherIcon(fetcher.data()), fetcher->source(), actionCollection());
    action->setToolTip(i18n("Update entry data from %1", fetcher->source()));
    connect(action, triggeredBool, m_updateMapper, mapVoid);
    m_updateMapper->setMapping(action, fetcher->source());
    m_fetchActions.append(action);
  }

  plugActionList(actionListName, m_fetchActions);
}

void MainWindow::importFile(Tellico::Import::Format format_, const QList<QUrl>& urls_) {
  QList<QUrl> urls = urls_;
  // update as DropHandler and Importer classes are updated
  if(urls_.count() > 1 &&
     format_ != Import::Bibtex &&
     format_ != Import::RIS &&
     format_ != Import::CIW &&
     format_ != Import::PDF) {
    QUrl u = urls_.front();
    QString url = u.isLocalFile() ? u.path() : u.toDisplayString();
    Kernel::self()->sorry(i18n("Tellico can only import one file of this type at a time. "
                               "Only %1 will be imported.", url));
    urls.clear();
    urls += u;
  }

  ImportDialog dlg(format_, urls, this);
  if(dlg.exec() != QDialog::Accepted) {
    return;
  }

//  if edit dialog is saved ok and if replacing, then the doc is saved ok
  if(m_editDialog->queryModified() &&
     (dlg.action() != Import::Replace || querySaveModified())) {
    GUI::CursorSaver cs(Qt::WaitCursor);
    Data::CollPtr coll = dlg.collection();
    if(!coll) {
      if(!dlg.statusMessage().isEmpty()) {
        Kernel::self()->sorry(dlg.statusMessage());
      }
      return;
    }
    importCollection(coll, dlg.action());
  }
}

void MainWindow::importText(Tellico::Import::Format format_, const QString& text_) {
  if(text_.isEmpty()) {
    return;
  }
  Data::CollPtr coll = ImportDialog::importText(format_, text_);
  if(coll) {
    importCollection(coll, Import::Merge);
  }
}

bool MainWindow::importCollection(Tellico::Data::CollPtr coll_, Tellico::Import::Action action_) {
  bool failed = false;
  switch(action_) {
    case Import::Append:
      {
        // only append if match, but special case importing books into bibliographies
        Data::CollPtr c = Data::Document::self()->collection();
        if(c->type() == coll_->type()
          || (c->type() == Data::Collection::Bibtex && coll_->type() == Data::Collection::Book)) {
          Kernel::self()->appendCollection(coll_);
          slotEnableModifiedActions(true);
        } else {
          Kernel::self()->sorry(TC_I18N1(errorAppendType));
          failed = true;
        }
      }
      break;

    case Import::Merge:
      {
        // only merge if match, but special case importing books into bibliographies
        Data::CollPtr c = Data::Document::self()->collection();
        if(c->type() == coll_->type()
          || (c->type() == Data::Collection::Bibtex && coll_->type() == Data::Collection::Book)) {
          Kernel::self()->mergeCollection(coll_);
          slotEnableModifiedActions(true);
        } else {
          Kernel::self()->sorry(TC_I18N1(errorMergeType));
          failed = true;
        }
      }
      break;

    default: // replace
      Kernel::self()->replaceCollection(coll_);
      m_fileOpenRecent->setCurrentItem(-1);
      m_newDocument = true;
      slotEnableOpenedActions();
      slotEnableModifiedActions(false);
      break;
  }
  // tell the entry views and models that there are no further images to load
  m_detailedView->slotRefreshImages();
  return !failed;
}

void MainWindow::slotURLAction(const QUrl& url_) {
  Q_ASSERT(url_.scheme() == QLatin1String("tc"));
  const QString actionName = url_.fileName();
  if(actionName == QLatin1String("disable_welcome")) {
    Config::setShowWelcome(false);
    m_entryView->showText(QString());
    return; // done
  }
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
  QAction* action = this->action(actionName.toLatin1().constData());
#else
  QAction* action = this->action(actionName);
#endif
  if(action) {
    action->activate(QAction::Trigger);
  } else {
    myWarning() << "unknown action: " << actionName;
  }
}

bool MainWindow::eventFilter(QObject* obj_, QEvent* ev_) {
  if(ev_->type() == QEvent::KeyPress && obj_ == m_quickFilter) {
    switch(static_cast<QKeyEvent*>(ev_)->key()) {
      case Qt::Key_Escape:
        m_quickFilter->clear();
        return true;
    }
  }
  return KXmlGuiWindow::eventFilter(obj_, ev_);
}

void MainWindow::slotToggleFullScreen() {
  Qt::WindowStates ws = windowState();
  setWindowState((ws & Qt::WindowFullScreen) ? (ws & ~Qt::WindowFullScreen) : (ws | Qt::WindowFullScreen));
}

void MainWindow::slotToggleMenuBarVisibility() {
  QMenuBar* mb = menuBar();
  mb->isHidden() ? mb->show() : mb->hide();
}

void MainWindow::slotToggleLayoutLock(bool lock_) {
  m_groupViewDock->setLocked(lock_);
  m_collectionViewDock->setLocked(lock_);
}

void MainWindow::slotResetLayout() {
  removeDockWidget(m_groupViewDock);
  addDockWidget(Qt::LeftDockWidgetArea, m_groupViewDock);
  m_groupViewDock->show();

  m_dummyWindow->removeDockWidget(m_collectionViewDock);
  m_dummyWindow->addDockWidget(Qt::TopDockWidgetArea, m_collectionViewDock);
  m_collectionViewDock->show();
}

void MainWindow::guiFactoryReset() {
  guiFactory()->removeClient(this);
  guiFactory()->reset();
  guiFactory()->addClient(this);

  // set up custom actions for collection templates, have to do this AFTER createGUI() or factory() reset
  const QString actionListName = QStringLiteral("collection_template_list");
  unplugActionList(actionListName);
  QSignalMapper* collectionTemplateMapper = new QSignalMapper(this);
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
  void (QSignalMapper::* mappedString)(QString) = &QSignalMapper::mapped;
  connect(collectionTemplateMapper, mappedString, this, &MainWindow::slotFileNewByTemplate);
#else
  connect(collectionTemplateMapper, &QSignalMapper::mappedString, this, &MainWindow::slotFileNewByTemplate);
#endif

  void (QAction::* triggeredBool)(bool) = &QAction::triggered;
  void (QSignalMapper::* mapVoid)() = &QSignalMapper::map;
  QList<QAction*> coll_actions;
  const QStringList customCollections = Tellico::locateAllFiles(QStringLiteral("tellico/collection-templates/*.tc"));
  if(!customCollections.isEmpty()) {
    m_newCollectionMenu->addSeparator();
  }
  foreach(const QString& collectionFile, customCollections) {
    QFileInfo info(collectionFile);
    auto action = new QAction(info.completeBaseName(), actionCollection());
    connect(action, triggeredBool, collectionTemplateMapper, mapVoid);
    const QString specFile = info.canonicalPath() + QDir::separator() + info.completeBaseName() + QLatin1String(".spec");
    if(QFileInfo::exists(specFile)) {
      KConfig config(specFile, KConfig::SimpleConfig);
      const KConfigGroup cg = config.group(QString());
      action->setText(cg.readEntry("Name", info.completeBaseName()));
      action->setToolTip(cg.readEntry("Comment"));
      action->setIcon(QIcon::fromTheme(cg.readEntry("Icon"), QIcon::fromTheme(QStringLiteral("document-new"))));
    } else {
      myDebug() << "No spec file for" << info.completeBaseName();
      action->setText(info.completeBaseName());
      action->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
    }
    collectionTemplateMapper->setMapping(action, collectionFile);
    coll_actions.append(action);
    m_newCollectionMenu->addAction(action);
  }
  plugActionList(actionListName, coll_actions);
}

void MainWindow::showLog() {
  auto dlg = new QDialog(this);
  auto layout = new QVBoxLayout();
  dlg->setLayout(layout);
  dlg->setWindowTitle(i18nc("@title:window", "Tellico Log"));

  auto viewer = new QTextEdit(dlg);
  viewer->setWordWrapMode(QTextOption::NoWrap);
  viewer->setReadOnly(true);
  viewer->setStyleSheet(QStringLiteral("QTextEdit { font-family: monospace; }"));
  layout->addWidget(viewer);

  auto buttonBox = new QDialogButtonBox(dlg);
  buttonBox->setStandardButtons(QDialogButtonBox::Close);
  connect(buttonBox, &QDialogButtonBox::rejected, dlg, &QDialog::reject);
  layout->addWidget(buttonBox);

  auto logFile = Logger::self()->logFile();
  if(!logFile.isEmpty()) {
    auto timer = new QTimer(dlg);
    timer->setSingleShot(true);
    timer->setInterval(1000);
    timer->callOnTimeout([logFile, viewer]() {
      Logger::self()->flush();
      QFile file(logFile);
      if(file.open(QFile::ReadOnly | QIODevice::Text)) {
        QTextStream in(&file);
        viewer->setPlainText(in.readAll());
        auto cursor = viewer->textCursor();
        cursor.movePosition(QTextCursor::End);
        viewer->setTextCursor(cursor);
        viewer->ensureCursorVisible(); // scroll to bottom
      }
    });
    connect(Logger::self(), &Logger::updated, timer, QOverload<>::of(&QTimer::start));
    myLog() << "Showing log viewer"; // this triggers the first read of the log file
  }

  dlg->setMinimumSize(800, 600);
  dlg->setAttribute(Qt::WA_DeleteOnClose, true);
  dlg->show();
}