1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
|
# qtmaildir v1 Implementation Plan
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
**Goal:** Build a Qt6 desktop mail client that reads and organizes a local notmuch-indexed Maildir, with correct HTML mail rendering and no network protocol code.
**Architecture:** One process, two threads. A `NotmuchWorker` on a dedicated thread owns the only `notmuch_database_t*` and is the only translation unit that includes `notmuch.h`; it communicates with the UI exclusively through queued signals carrying plain value structs. The UI thread runs Qt Widgets with a `QAbstractTableModel` fed in batches, and renders message bodies through a locked-down `QWebEngineView` whose request interceptor denies every request by default.
**Tech Stack:** C++17, Qt6 (Widgets, WebEngineWidgets, Test), libnotmuch 0.39, GMime 3.0, CMake + Ninja.
**Spec:** `docs/superpowers/specs/2026-08-02-qtmaildir-design.md`
---
## Environment notes (verified 2026-08-02)
Read these before Task 1; they explain build choices that are otherwise surprising.
- **Qt 6.11.1**, including WebEngine, ships inside Slackware's monolithic `qt6` package. There is no separate `qt6-webengine` package to install.
- **notmuch installs no `notmuch.pc`.** Verified absent on disk, absent from the package file list, and `pkg-config --exists notmuch` fails. This is upstream behaviour. CMake must use `find_path`/`find_library`, never `pkg_check_modules`, for notmuch.
- **GMime 3.2.15** does ship `gmime-3.0.pc`, so it uses `pkg_check_modules`.
- **CMake is 4.3.4**, which rejects `cmake_minimum_required(VERSION <3.5)`. Use 3.21.
- Toolchain: GCC 15.3.0, Ninja 1.13.2.
Build and test commands used throughout:
```bash
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug
cmake --build build
ctest --test-dir build --output-on-failure
```
Run a single test binary directly for a tighter loop, e.g. `./build/tests/test_keymap`.
**Commits must be GPG-signed** (`git commit -S`). Never disable signing. If pinentry times out because the machine was unattended, simply re-run the same command.
---
## File Structure
Created over the course of the plan. Each file has one responsibility.
| File | Responsibility |
|---|---|
| `CMakeLists.txt` | Top-level build: dependency discovery, options. |
| `src/CMakeLists.txt` | Application target. |
| `tests/CMakeLists.txt` | Test targets. |
| `src/types.h` | Plain value structs crossing the thread boundary. No logic. |
| `src/keymap.{h,cpp}` | Key sequence to action-name mapping; defaults plus INI overrides. |
| `src/config.{h,cpp}` | INI load/save: accounts, saved queries, sync command, keys. |
| `src/mimeparser.{h,cpp}` | Message file to parts, bodies, attachments (GMime). Includes safe attachment-name resolution. |
| `src/nmraii.h` | RAII wrappers for libnotmuch C handles. Header-only. |
| `src/notmuchworker.{h,cpp}` | The only file including `notmuch.h`. Queries and tag mutations. |
| `src/threadlistmodel.{h,cpp}` | `QAbstractTableModel` over `ThreadSummary`, batch append. |
| `src/requestinterceptor.{h,cpp}` | `QWebEngineUrlRequestInterceptor`: deny-by-default policy. |
| `src/cidschemehandler.{h,cpp}` | Serves `cid:` parts of the current message only. |
| `src/htmlbuilder.{h,cpp}` | Turns a parsed message into the HTML string the web view loads. |
| `src/messageview.{h,cpp}` | Message pane widget: headers, web view, attachment bar. |
| `src/mailsync.{h,cpp}` | `QProcess` wrapper around the configured sync command. |
| `src/mainwindow.{h,cpp}` | Wiring only: layout, signal connections, action registration. |
| `src/main.cpp` | Entry point, profile setup, startup checks. |
| `tests/test_keymap.cpp` | Keymap defaults, overrides, chords, unknown actions. |
| `tests/test_config.cpp` | INI parsing, account round-trip, missing-field handling. |
| `tests/test_mimeparser.cpp` | Part selection, decoding, attachments, filename safety. |
| `tests/test_interceptor.cpp` | The security-critical deny-by-default assertions. |
| `tests/fixtures/*.eml` | Hand-written message fixtures. |
Build order is dependency order: pure-logic units (keymap, config, mimeparser, interceptor) come first and are fully tested, then the notmuch layer, then the UI that wires them together.
---
## Task 1: Project skeleton and build system
**Files:**
- Create: `CMakeLists.txt`
- Create: `src/CMakeLists.txt`
- Create: `tests/CMakeLists.txt`
- Create: `src/main.cpp`
- [ ] **Step 1: Write the top-level CMakeLists.txt**
```cmake
cmake_minimum_required(VERSION 3.21)
project(qtmaildir VERSION 0.1.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
find_package(Qt6 6.5 REQUIRED COMPONENTS Widgets WebEngineWidgets Test)
# notmuch ships no pkg-config file; locate it by hand.
find_path(NOTMUCH_INCLUDE_DIR notmuch.h)
find_library(NOTMUCH_LIBRARY NAMES notmuch)
if(NOT NOTMUCH_INCLUDE_DIR OR NOT NOTMUCH_LIBRARY)
message(FATAL_ERROR
"libnotmuch not found. Need notmuch.h and libnotmuch on the system.")
endif()
message(STATUS "Found notmuch: ${NOTMUCH_LIBRARY}")
find_package(PkgConfig REQUIRED)
pkg_check_modules(GMIME REQUIRED IMPORTED_TARGET gmime-3.0)
enable_testing()
add_subdirectory(src)
add_subdirectory(tests)
```
- [ ] **Step 2: Write src/CMakeLists.txt**
The application logic lives in a static library so tests can link it without
duplicating source lists. Only `main.cpp` is in the executable.
```cmake
add_library(qtmaildir_lib STATIC
main_placeholder.cpp
)
target_include_directories(qtmaildir_lib
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${NOTMUCH_INCLUDE_DIR})
target_link_libraries(qtmaildir_lib
PUBLIC Qt6::Widgets Qt6::WebEngineWidgets PkgConfig::GMIME ${NOTMUCH_LIBRARY})
add_executable(qtmaildir main.cpp)
target_link_libraries(qtmaildir PRIVATE qtmaildir_lib)
install(TARGETS qtmaildir RUNTIME DESTINATION bin)
```
- [ ] **Step 3: Create the placeholder translation unit**
`add_library` needs at least one source. Create `src/main_placeholder.cpp`
containing exactly this; Task 2 replaces it with the first real source.
```cpp
// Placeholder so the library target has a source file before real code lands.
// Removed in Task 2.
namespace { int qtmaildir_placeholder = 0; }
```
- [ ] **Step 4: Write a minimal src/main.cpp**
```cpp
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel label(QStringLiteral("qtmaildir"));
label.show();
return app.exec();
}
```
- [ ] **Step 5: Write tests/CMakeLists.txt**
Empty for now except the helper function later tasks call.
```cmake
# add_qtmaildir_test(<name>) builds tests/test_<name>.cpp and registers it.
function(add_qtmaildir_test name)
add_executable(test_${name} test_${name}.cpp)
target_link_libraries(test_${name} PRIVATE qtmaildir_lib Qt6::Test)
add_test(NAME ${name} COMMAND test_${name})
endfunction()
```
- [ ] **Step 6: Configure and build**
Run:
```bash
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug
cmake --build build
```
Expected: configure prints `Found notmuch: /usr/lib64/libnotmuch.so`, build
succeeds, `./build/src/qtmaildir` exists.
- [ ] **Step 7: Commit**
```bash
git add CMakeLists.txt src/ tests/
git commit -S -m "build: add CMake skeleton and dependency discovery"
```
---
## Task 2: KeyMap — defaults, INI overrides, chords
Start here because it is pure logic with no dependencies, so it proves the
test harness works before anything harder lands.
**Files:**
- Create: `src/keymap.h`, `src/keymap.cpp`
- Create: `tests/test_keymap.cpp`
- Modify: `src/CMakeLists.txt`, `tests/CMakeLists.txt`
- Delete: `src/main_placeholder.cpp`
- [ ] **Step 1: Write the failing test**
Create `tests/test_keymap.cpp`:
```cpp
#include <QtTest>
#include <QTemporaryDir>
#include <QSettings>
#include "keymap.h"
class TestKeyMap : public QObject
{
Q_OBJECT
private slots:
void defaultsAreLoaded();
void iniOverridesDefault();
void iniAddsNewBinding();
void chordSequenceParses();
void unknownActionIsReported();
void invalidSequenceIsReported();
};
void TestKeyMap::defaultsAreLoaded()
{
KeyMap map;
map.loadDefaults();
QCOMPARE(map.actionFor(QKeySequence(QStringLiteral("j"))),
QStringLiteral("next_thread"));
QCOMPARE(map.actionFor(QKeySequence(QStringLiteral("a"))),
QStringLiteral("archive"));
}
void TestKeyMap::iniOverridesDefault()
{
QTemporaryDir dir;
const QString path = dir.filePath(QStringLiteral("t.conf"));
{
QSettings s(path, QSettings::IniFormat);
s.beginGroup(QStringLiteral("keys"));
s.setValue(QStringLiteral("j"), QStringLiteral("archive"));
s.endGroup();
}
KeyMap map;
map.loadDefaults();
QSettings s(path, QSettings::IniFormat);
map.loadOverrides(s);
QCOMPARE(map.actionFor(QKeySequence(QStringLiteral("j"))),
QStringLiteral("archive"));
// An untouched default survives.
QCOMPARE(map.actionFor(QKeySequence(QStringLiteral("k"))),
QStringLiteral("prev_thread"));
}
void TestKeyMap::iniAddsNewBinding()
{
QTemporaryDir dir;
const QString path = dir.filePath(QStringLiteral("t.conf"));
{
QSettings s(path, QSettings::IniFormat);
s.beginGroup(QStringLiteral("keys"));
s.setValue(QStringLiteral("Ctrl+Shift+A"), QStringLiteral("archive"));
s.endGroup();
}
KeyMap map;
map.loadDefaults();
QSettings s(path, QSettings::IniFormat);
map.loadOverrides(s);
QCOMPARE(map.actionFor(QKeySequence(QStringLiteral("Ctrl+Shift+A"))),
QStringLiteral("archive"));
}
void TestKeyMap::chordSequenceParses()
{
QTemporaryDir dir;
const QString path = dir.filePath(QStringLiteral("t.conf"));
{
QSettings s(path, QSettings::IniFormat);
s.beginGroup(QStringLiteral("keys"));
s.setValue(QStringLiteral("g,i"), QStringLiteral("focus_query"));
s.endGroup();
}
KeyMap map;
QSettings s(path, QSettings::IniFormat);
map.loadOverrides(s);
const QKeySequence chord = QKeySequence::fromString(QStringLiteral("g,i"));
QCOMPARE(chord.count(), 2);
QCOMPARE(map.actionFor(chord), QStringLiteral("focus_query"));
}
void TestKeyMap::unknownActionIsReported()
{
QTemporaryDir dir;
const QString path = dir.filePath(QStringLiteral("t.conf"));
{
QSettings s(path, QSettings::IniFormat);
s.beginGroup(QStringLiteral("keys"));
s.setValue(QStringLiteral("z"), QStringLiteral("no_such_action"));
s.endGroup();
}
KeyMap map;
QSettings s(path, QSettings::IniFormat);
map.loadOverrides(s);
// Reported, not fatal, and not bound.
QCOMPARE(map.warnings().size(), 1);
QVERIFY(map.warnings().first().contains(QStringLiteral("no_such_action")));
QVERIFY(map.actionFor(QKeySequence(QStringLiteral("z"))).isEmpty());
}
void TestKeyMap::invalidSequenceIsReported()
{
QTemporaryDir dir;
const QString path = dir.filePath(QStringLiteral("t.conf"));
{
QSettings s(path, QSettings::IniFormat);
s.beginGroup(QStringLiteral("keys"));
s.setValue(QStringLiteral("NotAKey++"), QStringLiteral("archive"));
s.endGroup();
}
KeyMap map;
QSettings s(path, QSettings::IniFormat);
map.loadOverrides(s);
QCOMPARE(map.warnings().size(), 1);
}
QTEST_MAIN(TestKeyMap)
#include "test_keymap.moc"
```
- [ ] **Step 2: Register the test and run it to verify it fails**
Append to `tests/CMakeLists.txt`:
```cmake
add_qtmaildir_test(keymap)
```
Run:
```bash
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug && cmake --build build
```
Expected: FAIL at compile time, `keymap.h: No such file or directory`.
- [ ] **Step 3: Write src/keymap.h**
```cpp
#pragma once
#include <QHash>
#include <QKeySequence>
#include <QStringList>
class QSettings;
/// Maps key sequences to action names. Action names are plain strings so this
/// class has no dependency on the widgets that implement the actions.
class KeyMap
{
public:
/// Every action name the application understands. loadOverrides() rejects
/// anything not in this set, so a typo in the config cannot bind silently.
static QStringList knownActions();
void loadDefaults();
/// Reads the [keys] group. Invalid sequences and unknown action names are
/// collected into warnings() rather than throwing or aborting.
void loadOverrides(QSettings &settings);
/// Empty string when nothing is bound.
QString actionFor(const QKeySequence &sequence) const;
QStringList warnings() const { return m_warnings; }
private:
QHash<QKeySequence, QString> m_bindings;
QStringList m_warnings;
};
```
- [ ] **Step 4: Write src/keymap.cpp**
```cpp
#include "keymap.h"
#include <QSettings>
QStringList KeyMap::knownActions()
{
// Keep in sync with the actions MainWindow registers.
return {
QStringLiteral("next_thread"),
QStringLiteral("prev_thread"),
QStringLiteral("open_thread"),
QStringLiteral("archive"),
QStringLiteral("delete"),
QStringLiteral("spam"),
QStringLiteral("toggle_unread"),
QStringLiteral("flag"),
QStringLiteral("focus_query"),
QStringLiteral("toggle_html"),
QStringLiteral("load_remote"),
QStringLiteral("undo"),
QStringLiteral("sync"),
QStringLiteral("quit"),
};
}
void KeyMap::loadDefaults()
{
const QHash<QString, QString> defaults = {
{ QStringLiteral("j"), QStringLiteral("next_thread") },
{ QStringLiteral("k"), QStringLiteral("prev_thread") },
{ QStringLiteral("Return"), QStringLiteral("open_thread") },
{ QStringLiteral("a"), QStringLiteral("archive") },
{ QStringLiteral("d"), QStringLiteral("delete") },
{ QStringLiteral("N"), QStringLiteral("toggle_unread") },
{ QStringLiteral("F"), QStringLiteral("flag") },
{ QStringLiteral("/"), QStringLiteral("focus_query") },
{ QStringLiteral("h"), QStringLiteral("toggle_html") },
{ QStringLiteral("u"), QStringLiteral("undo") },
{ QStringLiteral("G"), QStringLiteral("sync") },
{ QStringLiteral("Ctrl+Q"), QStringLiteral("quit") },
};
for (auto it = defaults.cbegin(); it != defaults.cend(); ++it)
m_bindings.insert(QKeySequence::fromString(it.key()), it.value());
}
void KeyMap::loadOverrides(QSettings &settings)
{
const QStringList known = knownActions();
settings.beginGroup(QStringLiteral("keys"));
const QStringList keys = settings.childKeys();
for (const QString &key : keys) {
const QString action = settings.value(key).toString();
const QKeySequence sequence = QKeySequence::fromString(key);
if (sequence.isEmpty()) {
m_warnings.append(
QStringLiteral("Unparseable key sequence '%1' in [keys]").arg(key));
continue;
}
if (!known.contains(action)) {
m_warnings.append(
QStringLiteral("Unknown action '%1' bound to '%2' in [keys]")
.arg(action, key));
continue;
}
m_bindings.insert(sequence, action);
}
settings.endGroup();
}
QString KeyMap::actionFor(const QKeySequence &sequence) const
{
return m_bindings.value(sequence);
}
```
- [ ] **Step 5: Add sources to the library and drop the placeholder**
Edit `src/CMakeLists.txt`, replacing the `add_library` call:
```cmake
add_library(qtmaildir_lib STATIC
keymap.cpp
)
```
Then delete the placeholder:
```bash
rm src/main_placeholder.cpp
```
- [ ] **Step 6: Run tests to verify they pass**
Run:
```bash
cmake --build build && ctest --test-dir build --output-on-failure
```
Expected: `test_keymap` PASSes, 6 test functions, 0 failed.
- [ ] **Step 7: Commit**
```bash
git add src/keymap.h src/keymap.cpp src/CMakeLists.txt tests/
git rm --cached src/main_placeholder.cpp 2>/dev/null || true
git commit -S -m "feat: add KeyMap with defaults and INI overrides"
```
---
## Task 3: Config — accounts, queries, sync command
**Files:**
- Create: `src/config.h`, `src/config.cpp`
- Create: `tests/test_config.cpp`
- Modify: `src/CMakeLists.txt`, `tests/CMakeLists.txt`
- [ ] **Step 1: Write the failing test**
Create `tests/test_config.cpp`:
```cpp
#include <QtTest>
#include <QTemporaryDir>
#include <QSettings>
#include "config.h"
class TestConfig : public QObject
{
Q_OBJECT
private slots:
void parsesAccounts();
void parsesSavedQueries();
void missingSyncCommandIsEmpty();
void accountWithoutMaildirIsRejected();
void scopedQueryWrapsCorrectly();
};
static QString writeIni(const QTemporaryDir &dir, const QString &body)
{
const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
QFile f(path);
f.open(QIODevice::WriteOnly | QIODevice::Text);
f.write(body.toUtf8());
f.close();
return path;
}
void TestConfig::parsesAccounts()
{
QTemporaryDir dir;
const QString path = writeIni(dir, QStringLiteral(
"[account.work]\n"
"name=Test User\n"
"address=user@example.org\n"
"maildir=work-mail\n"
"drafts=Drafts\n"
"\n"
"[account.personal]\n"
"name=Test User\n"
"address=me@example.net\n"
"maildir=personal\n"
));
Config config;
config.load(path);
QCOMPARE(config.accounts().size(), 2);
const Account work = config.account(QStringLiteral("work"));
QCOMPARE(work.key, QStringLiteral("work"));
QCOMPARE(work.name, QStringLiteral("Test User"));
QCOMPARE(work.address, QStringLiteral("user@example.org"));
QCOMPARE(work.maildir, QStringLiteral("work-mail"));
QCOMPARE(work.drafts, QStringLiteral("Drafts"));
// drafts is optional in v1 (send is v2).
const Account personal = config.account(QStringLiteral("personal"));
QVERIFY(personal.drafts.isEmpty());
QVERIFY(personal.isValid());
}
void TestConfig::parsesSavedQueries()
{
QTemporaryDir dir;
const QString path = writeIni(dir, QStringLiteral(
"[queries]\n"
"Inbox=tag:inbox\n"
"Unread=tag:unread\n"
));
Config config;
config.load(path);
const QList<SavedQuery> queries = config.savedQueries();
QCOMPARE(queries.size(), 2);
// Order follows the file, so the UI button order is predictable.
QCOMPARE(queries.at(0).name, QStringLiteral("Inbox"));
QCOMPARE(queries.at(0).query, QStringLiteral("tag:inbox"));
}
void TestConfig::missingSyncCommandIsEmpty()
{
QTemporaryDir dir;
const QString path = writeIni(dir, QStringLiteral("[general]\n"));
Config config;
config.load(path);
QVERIFY(config.syncCommand().isEmpty());
// The UI uses this to disable the Sync button with a tooltip.
QVERIFY(!config.warnings().isEmpty());
}
void TestConfig::accountWithoutMaildirIsRejected()
{
QTemporaryDir dir;
const QString path = writeIni(dir, QStringLiteral(
"[account.broken]\n"
"name=No Maildir\n"
"address=x@example.org\n"
));
Config config;
config.load(path);
// Rejected, reported, and not offered to the user as a scope.
QCOMPARE(config.accounts().size(), 0);
QCOMPARE(config.warnings().size(), 1);
QVERIFY(config.warnings().first().contains(QStringLiteral("broken")));
}
void TestConfig::scopedQueryWrapsCorrectly()
{
Account account;
account.key = QStringLiteral("work");
account.maildir = QStringLiteral("work-mail");
QCOMPARE(account.scopedQuery(QStringLiteral("tag:inbox")),
QStringLiteral("path:\"work-mail/**\" and (tag:inbox)"));
// An empty query still scopes to the account rather than matching nothing.
QCOMPARE(account.scopedQuery(QString()),
QStringLiteral("path:\"work-mail/**\""));
}
QTEST_MAIN(TestConfig)
#include "test_config.moc"
```
- [ ] **Step 2: Register and run to verify it fails**
Append to `tests/CMakeLists.txt`:
```cmake
add_qtmaildir_test(config)
```
Run: `cmake -S . -B build -G Ninja && cmake --build build`
Expected: FAIL, `config.h: No such file or directory`.
- [ ] **Step 3: Write src/config.h**
```cpp
#pragma once
#include <QList>
#include <QString>
#include <QStringList>
/// One mail account. notmuch has no concept of accounts; it sees a single flat
/// tree. An account is therefore a path prefix within that tree plus an
/// identity.
struct Account
{
QString key; ///< INI group suffix, e.g. "work" from [account.work].
QString name;
QString address;
QString maildir; ///< Relative to notmuch's database.path.
QString drafts; ///< Unused in v1; send is v2.
bool isValid() const { return !key.isEmpty() && !maildir.isEmpty(); }
/// Restricts a notmuch query to this account's subtree.
QString scopedQuery(const QString &query) const;
};
struct SavedQuery
{
QString name;
QString query;
};
/// Reads ~/.config/qtmaildir/qtmaildir.conf.
///
/// The Maildir path is deliberately NOT configurable here: notmuch already
/// stores it as database.path and libnotmuch reads it. Duplicating it would
/// allow the GUI to index a different tree than the CLI.
class Config
{
public:
/// Path used when load() is called with no argument.
static QString defaultPath();
void load(const QString &path);
QList<Account> accounts() const { return m_accounts; }
Account account(const QString &key) const;
QList<SavedQuery> savedQueries() const { return m_savedQueries; }
/// Empty when unset; the caller disables the Sync button in that case.
QString syncCommand() const { return m_syncCommand; }
/// Optional alternate notmuch config file. Empty means "let notmuch decide".
QString notmuchConfig() const { return m_notmuchConfig; }
/// Non-fatal problems, shown once in a startup banner.
QStringList warnings() const { return m_warnings; }
private:
QList<Account> m_accounts;
QList<SavedQuery> m_savedQueries;
QString m_syncCommand;
QString m_notmuchConfig;
QStringList m_warnings;
};
```
- [ ] **Step 4: Write src/config.cpp**
```cpp
#include "config.h"
#include <QFileInfo>
#include <QSettings>
#include <QStandardPaths>
QString Account::scopedQuery(const QString &query) const
{
const QString prefix = QStringLiteral("path:\"%1/**\"").arg(maildir);
if (query.trimmed().isEmpty())
return prefix;
return QStringLiteral("%1 and (%2)").arg(prefix, query);
}
QString Config::defaultPath()
{
const QString base =
QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
return base + QStringLiteral("/qtmaildir/qtmaildir.conf");
}
void Config::load(const QString &path)
{
QSettings settings(path, QSettings::IniFormat);
m_notmuchConfig =
settings.value(QStringLiteral("general/notmuch_config")).toString();
m_syncCommand = settings.value(QStringLiteral("sync/command")).toString();
if (m_syncCommand.isEmpty()) {
m_warnings.append(QStringLiteral(
"No sync command configured ([sync] command); syncing is disabled."));
} else if (!QFileInfo::exists(m_syncCommand.split(QLatin1Char(' ')).first())) {
m_warnings.append(
QStringLiteral("Sync command '%1' does not exist; syncing is disabled.")
.arg(m_syncCommand));
m_syncCommand.clear();
}
for (const QString &group : settings.childGroups()) {
if (!group.startsWith(QStringLiteral("account.")))
continue;
Account account;
account.key = group.mid(QStringLiteral("account.").size());
settings.beginGroup(group);
account.name = settings.value(QStringLiteral("name")).toString();
account.address = settings.value(QStringLiteral("address")).toString();
account.maildir = settings.value(QStringLiteral("maildir")).toString();
account.drafts = settings.value(QStringLiteral("drafts")).toString();
settings.endGroup();
if (!account.isValid()) {
m_warnings.append(
QStringLiteral("Account '%1' has no maildir; ignoring it.")
.arg(account.key));
continue;
}
m_accounts.append(account);
}
settings.beginGroup(QStringLiteral("queries"));
for (const QString &name : settings.childKeys())
m_savedQueries.append({ name, settings.value(name).toString() });
settings.endGroup();
}
Account Config::account(const QString &key) const
{
for (const Account &a : m_accounts) {
if (a.key == key)
return a;
}
return {};
}
```
- [ ] **Step 5: Add to the library**
In `src/CMakeLists.txt`, add `config.cpp` to `add_library(qtmaildir_lib STATIC ...)`:
```cmake
add_library(qtmaildir_lib STATIC
keymap.cpp
config.cpp
)
```
- [ ] **Step 6: Run tests to verify they pass**
Run: `cmake --build build && ctest --test-dir build --output-on-failure`
Expected: `keymap` and `config` both PASS.
Note: `QSettings::childKeys()` returns keys sorted, so the `parsesSavedQueries`
ordering assertion holds for `Inbox` before `Unread` alphabetically. If a future
config needs file order, that requires a hand-rolled parser; not needed in v1.
- [ ] **Step 7: Commit**
```bash
git add src/config.h src/config.cpp src/CMakeLists.txt tests/
git commit -S -m "feat: add Config with account, query, and sync parsing"
```
---
## Task 4: MimeParser — part selection and decoding
**Files:**
- Create: `src/mimeparser.h`, `src/mimeparser.cpp`
- Create: `tests/test_mimeparser.cpp`
- Create: `tests/fixtures/plain.eml`, `alternative.eml`, `inline_image.eml`, `attachment.eml`, `encoded_subject.eml`, `truncated.eml`, `hostile_filename.eml`
- Modify: `src/CMakeLists.txt`, `tests/CMakeLists.txt`
- [ ] **Step 1: Write the fixtures**
Create `tests/fixtures/plain.eml`:
```
From: Alice <alice@example.org>
To: Bob <bob@example.net>
Subject: Plain hello
Date: Sat, 01 Aug 2026 10:00:00 +0000
Message-ID: <plain-1@example.org>
Content-Type: text/plain; charset=utf-8
Hello Bob.
> quoted line
Regards,
Alice
```
Create `tests/fixtures/alternative.eml`:
```
From: Alice <alice@example.org>
Subject: Both parts
Date: Sat, 01 Aug 2026 10:00:00 +0000
Message-ID: <alt-1@example.org>
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="BOUND"
--BOUND
Content-Type: text/plain; charset=utf-8
plain version
--BOUND
Content-Type: text/html; charset=utf-8
<html><body><p>html version</p></body></html>
--BOUND--
```
Create `tests/fixtures/inline_image.eml`:
```
From: Alice <alice@example.org>
Subject: Inline image
Date: Sat, 01 Aug 2026 10:00:00 +0000
Message-ID: <cid-1@example.org>
MIME-Version: 1.0
Content-Type: multipart/related; boundary="REL"
--REL
Content-Type: text/html; charset=utf-8
<html><body><img src="cid:logo@example.org"></body></html>
--REL
Content-Type: image/png
Content-Transfer-Encoding: base64
Content-ID: <logo@example.org>
iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9
awAAAABJRU5ErkJggg==
--REL--
```
Create `tests/fixtures/attachment.eml`:
```
From: Alice <alice@example.org>
Subject: With attachment
Date: Sat, 01 Aug 2026 10:00:00 +0000
Message-ID: <att-1@example.org>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="MIX"
--MIX
Content-Type: text/plain; charset=utf-8
see attached
--MIX
Content-Type: text/plain; charset=utf-8; name="notes.txt"
Content-Disposition: attachment; filename="notes.txt"
Content-Transfer-Encoding: quoted-printable
caf=C3=A9 notes
--MIX--
```
Create `tests/fixtures/encoded_subject.eml`:
```
From: =?utf-8?B?w4RsaWNl?= <alice@example.org>
Subject: =?utf-8?Q?Caf=C3=A9_meeting?=
Date: Sat, 01 Aug 2026 10:00:00 +0000
Message-ID: <enc-1@example.org>
Content-Type: text/plain; charset=utf-8
body
```
Create `tests/fixtures/truncated.eml` (deliberately cut off mid-part):
```
From: Alice <alice@example.org>
Subject: Truncated
Date: Sat, 01 Aug 2026 10:00:00 +0000
Message-ID: <trunc-1@example.org>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="CUT"
--CUT
Content-Type: text/plain; charset=utf-8
this part never closes
```
Create `tests/fixtures/hostile_filename.eml`:
```
From: Attacker <bad@example.org>
Subject: Hostile attachment name
Date: Sat, 01 Aug 2026 10:00:00 +0000
Message-ID: <evil-1@example.org>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="EVIL"
--EVIL
Content-Type: text/plain; charset=utf-8
body
--EVIL
Content-Type: text/plain; name="../../../../tmp/pwned.txt"
Content-Disposition: attachment; filename="../../../../tmp/pwned.txt"
owned
--EVIL--
```
- [ ] **Step 2: Write the failing test**
Create `tests/test_mimeparser.cpp`:
```cpp
#include <QtTest>
#include <QTemporaryDir>
#include <QDir>
#include "mimeparser.h"
class TestMimeParser : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void parsesPlainText();
void prefersHtmlWhenAvailable();
void fallsBackToPlainWhenHtmlDisabled();
void collectsInlineCidParts();
void decodesQuotedPrintableAttachment();
void decodesEncodedHeaders();
void malformedMessageDoesNotCrash();
void missingFileIsReported();
void hostileFilenameIsSanitised();
void savedAttachmentMatchesBytes();
void safeFilenameStripsPathComponents();
private:
QString fixture(const QString &name) const
{ return m_fixtureDir + QLatin1Char('/') + name; }
QString m_fixtureDir;
};
void TestMimeParser::initTestCase()
{
// FIXTURE_DIR is defined by CMake so the test can run from any cwd.
m_fixtureDir = QStringLiteral(FIXTURE_DIR);
QVERIFY2(QDir(m_fixtureDir).exists(), "fixture directory missing");
}
void TestMimeParser::parsesPlainText()
{
MimeParser parser;
const ParsedMessage msg = parser.parse(fixture(QStringLiteral("plain.eml")));
QVERIFY(msg.ok);
QCOMPARE(msg.subject, QStringLiteral("Plain hello"));
QCOMPARE(msg.from, QStringLiteral("Alice <alice@example.org>"));
QVERIFY(msg.plainBody.contains(QStringLiteral("Hello Bob.")));
QVERIFY(msg.htmlBody.isEmpty());
QVERIFY(msg.attachments.isEmpty());
}
void TestMimeParser::prefersHtmlWhenAvailable()
{
MimeParser parser;
const ParsedMessage msg =
parser.parse(fixture(QStringLiteral("alternative.eml")));
QVERIFY(msg.ok);
QVERIFY(msg.htmlBody.contains(QStringLiteral("html version")));
// The plain alternative is kept so the user can toggle to it.
QVERIFY(msg.plainBody.contains(QStringLiteral("plain version")));
QVERIFY(msg.hasHtml());
}
void TestMimeParser::fallsBackToPlainWhenHtmlDisabled()
{
MimeParser parser;
const ParsedMessage msg = parser.parse(fixture(QStringLiteral("plain.eml")));
QVERIFY(!msg.hasHtml());
QVERIFY(!msg.plainBody.isEmpty());
}
void TestMimeParser::collectsInlineCidParts()
{
MimeParser parser;
const ParsedMessage msg =
parser.parse(fixture(QStringLiteral("inline_image.eml")));
QVERIFY(msg.ok);
QCOMPARE(msg.inlineParts.size(), 1);
// Content-ID angle brackets are stripped so it matches the cid: URL body.
QVERIFY(msg.inlineParts.contains(QStringLiteral("logo@example.org")));
const InlinePart part = msg.inlineParts.value(QStringLiteral("logo@example.org"));
QCOMPARE(part.mimeType, QStringLiteral("image/png"));
// Decoded 1x1 PNG starts with the PNG magic bytes.
QVERIFY(part.data.startsWith(QByteArray("\x89PNG", 4)));
}
void TestMimeParser::decodesQuotedPrintableAttachment()
{
MimeParser parser;
const ParsedMessage msg =
parser.parse(fixture(QStringLiteral("attachment.eml")));
QVERIFY(msg.ok);
QCOMPARE(msg.attachments.size(), 1);
QCOMPARE(msg.attachments.first().filename, QStringLiteral("notes.txt"));
QCOMPARE(QString::fromUtf8(msg.attachments.first().data),
QStringLiteral("café notes"));
}
void TestMimeParser::decodesEncodedHeaders()
{
MimeParser parser;
const ParsedMessage msg =
parser.parse(fixture(QStringLiteral("encoded_subject.eml")));
QVERIFY(msg.ok);
QCOMPARE(msg.subject, QStringLiteral("Café meeting"));
QVERIFY(msg.from.contains(QStringLiteral("Älice")));
}
void TestMimeParser::malformedMessageDoesNotCrash()
{
MimeParser parser;
const ParsedMessage msg =
parser.parse(fixture(QStringLiteral("truncated.eml")));
// GMime is tolerant: it recovers the headers and whatever body it found.
// The requirement is only that parsing terminates and reports something.
QCOMPARE(msg.subject, QStringLiteral("Truncated"));
}
void TestMimeParser::missingFileIsReported()
{
MimeParser parser;
const ParsedMessage msg =
parser.parse(fixture(QStringLiteral("does_not_exist.eml")));
QVERIFY(!msg.ok);
QVERIFY(!msg.error.isEmpty());
}
void TestMimeParser::hostileFilenameIsSanitised()
{
MimeParser parser;
const ParsedMessage msg =
parser.parse(fixture(QStringLiteral("hostile_filename.eml")));
QVERIFY(msg.ok);
QCOMPARE(msg.attachments.size(), 1);
// The raw header value is preserved for display...
QVERIFY(msg.attachments.first().filename.contains(QStringLiteral("..")));
// ...but the name used on disk is reduced to a basename.
QCOMPARE(msg.attachments.first().safeFilename(), QStringLiteral("pwned.txt"));
}
void TestMimeParser::savedAttachmentMatchesBytes()
{
MimeParser parser;
const ParsedMessage msg =
parser.parse(fixture(QStringLiteral("attachment.eml")));
QVERIFY(msg.ok);
QTemporaryDir dir;
QString error;
const QString written =
msg.attachments.first().saveTo(dir.path(), &error);
QVERIFY2(!written.isEmpty(), qPrintable(error));
// Never escapes the target directory.
QVERIFY(written.startsWith(dir.path()));
QFile f(written);
QVERIFY(f.open(QIODevice::ReadOnly));
QCOMPARE(f.readAll(), msg.attachments.first().data);
}
QTEST_MAIN(TestMimeParser)
#include "test_mimeparser.moc"
```
- [ ] **Step 3: Register the test and run to verify it fails**
Append to `tests/CMakeLists.txt`:
```cmake
add_qtmaildir_test(mimeparser)
target_compile_definitions(test_mimeparser PRIVATE
FIXTURE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/fixtures")
```
Run: `cmake -S . -B build -G Ninja && cmake --build build`
Expected: FAIL, `mimeparser.h: No such file or directory`.
- [ ] **Step 4: Write src/mimeparser.h**
```cpp
#pragma once
#include <QByteArray>
#include <QHash>
#include <QList>
#include <QString>
/// An inline part referenced by a cid: URL from the HTML body.
struct InlinePart
{
QString mimeType;
QByteArray data;
};
struct Attachment
{
QString filename; ///< As it appeared in the message. Untrusted.
QString mimeType;
QByteArray data;
/// filename reduced to a basename safe to join onto a directory.
/// Attacker-controlled input: a filename may contain path separators or
/// "..", so anything that could escape the target directory is stripped.
/// Returns a generated name when nothing usable remains.
QString safeFilename() const;
/// Writes the attachment into directory. Returns the full path written, or
/// an empty string on failure with *error set.
QString saveTo(const QString &directory, QString *error) const;
};
struct ParsedMessage
{
bool ok = false;
QString error;
QString subject;
QString from;
QString to;
QString cc;
QString date;
QString messageId;
QString plainBody;
QString htmlBody;
QHash<QString, InlinePart> inlineParts; ///< Keyed by Content-ID, no <>.
QList<Attachment> attachments;
bool hasHtml() const { return !htmlBody.isEmpty(); }
};
/// Parses a single message file using GMime.
///
/// Hand-rolling this would mean reimplementing RFC 2047 encoded words, RFC 2231
/// parameter continuations, transfer encodings, and charset conversion, plus
/// tolerance for malformed real-world mail.
class MimeParser
{
public:
MimeParser();
ParsedMessage parse(const QString &filePath) const;
};
```
- [ ] **Step 5: Write src/mimeparser.cpp**
```cpp
#include "mimeparser.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QRegularExpression>
#include <QUuid>
#include <gmime/gmime.h>
namespace {
/// GMime must be initialised exactly once per process.
void ensureGMimeInit()
{
static bool initialised = false;
if (!initialised) {
g_mime_init();
initialised = true;
}
}
QString fromGChar(char *owned)
{
if (!owned)
return {};
const QString result = QString::fromUtf8(owned);
g_free(owned);
return result;
}
QString headerText(GMimeMessage *message, const char *name)
{
GMimeHeaderList *headers = g_mime_object_get_header_list(
GMIME_OBJECT(message));
if (!headers)
return {};
GMimeHeader *header = g_mime_header_list_get_header(headers, name);
if (!header)
return {};
// get_value() returns the RFC 2047-decoded value.
return QString::fromUtf8(g_mime_header_get_value(header));
}
QByteArray decodePart(GMimePart *part)
{
GMimeDataWrapper *content = g_mime_part_get_content(part);
if (!content)
return {};
GMimeStream *memStream = g_mime_stream_mem_new();
g_mime_data_wrapper_write_to_stream(content, memStream);
g_mime_stream_flush(memStream);
GByteArray *bytes = g_mime_stream_mem_get_byte_array(
GMIME_STREAM_MEM(memStream));
QByteArray result(reinterpret_cast<const char *>(bytes->data), bytes->len);
g_object_unref(memStream);
return result;
}
/// Walks the MIME tree, filling the parsed message.
void collectParts(GMimeObject *object, ParsedMessage &out)
{
if (GMIME_IS_MULTIPART(object)) {
GMimeMultipart *multipart = GMIME_MULTIPART(object);
const int count = g_mime_multipart_get_count(multipart);
for (int i = 0; i < count; ++i)
collectParts(g_mime_multipart_get_part(multipart, i), out);
return;
}
if (GMIME_IS_MESSAGE_PART(object)) {
GMimeMessage *sub = g_mime_message_part_get_message(
GMIME_MESSAGE_PART(object));
if (sub)
collectParts(g_mime_message_get_mime_part(sub), out);
return;
}
if (!GMIME_IS_PART(object))
return;
GMimePart *part = GMIME_PART(object);
GMimeContentType *contentType = g_mime_object_get_content_type(object);
const QString mimeType = contentType
? fromGChar(g_mime_content_type_get_mime_type(contentType))
: QStringLiteral("application/octet-stream");
const char *disposition = g_mime_object_get_disposition(object);
const bool isAttachment =
disposition && g_ascii_strcasecmp(disposition, "attachment") == 0;
const char *contentId = g_mime_part_get_content_id(part);
if (isAttachment) {
Attachment attachment;
attachment.mimeType = mimeType;
attachment.data = decodePart(part);
const char *filename = g_mime_part_get_filename(part);
attachment.filename = filename
? QString::fromUtf8(filename)
: QStringLiteral("attachment");
out.attachments.append(attachment);
return;
}
if (contentId) {
// Strip the angle brackets so the key matches a cid: URL body.
QString id = QString::fromUtf8(contentId);
if (id.startsWith(QLatin1Char('<')) && id.endsWith(QLatin1Char('>')))
id = id.mid(1, id.size() - 2);
out.inlineParts.insert(id, InlinePart{ mimeType, decodePart(part) });
return;
}
if (mimeType == QLatin1String("text/plain") && out.plainBody.isEmpty()) {
out.plainBody = QString::fromUtf8(decodePart(part));
} else if (mimeType == QLatin1String("text/html") && out.htmlBody.isEmpty()) {
out.htmlBody = QString::fromUtf8(decodePart(part));
}
}
} // namespace
QString Attachment::safeFilename() const
{
// Reduce to a basename: QFileInfo handles '/', and backslashes are stripped
// explicitly because a Windows-authored name can carry them.
QString name = filename;
name.replace(QLatin1Char('\\'), QLatin1Char('/'));
name = QFileInfo(name).fileName();
// A name of "..", "." or empty leaves nothing usable.
if (name.isEmpty() || name == QLatin1String(".") || name == QLatin1String(".."))
return QStringLiteral("attachment-%1").arg(
QUuid::createUuid().toString(QUuid::Id128).left(8));
return name;
}
QString Attachment::saveTo(const QString &directory, QString *error) const
{
const QDir dir(directory);
const QString target = dir.absoluteFilePath(safeFilename());
// Belt and braces, and currently UNREACHABLE through this function:
// safeFilename() above already reduces any name to a basename, so no
// caller-supplied filename can produce a target outside `directory`.
// The guard exists so that a future change which stops sanitising, or
// which lets a caller pass a subpath, still cannot escape. Do not write
// a test that drives saveTo() expecting a refusal: it cannot happen
// while safeFilename() runs first. Test safeFilename() instead, which
// is the control that actually stops traversal today.
//
// The comparison must be separator-aware. A bare startsWith() on the
// strings would accept "/tmp/safe-evil/x" as being inside "/tmp/safe",
// since one is a string prefix of the other with no path boundary
// between them. cleanPath() also resolves ".." before comparison rather
// than leaving it to be compared textually.
const QString cleanDir = QDir::cleanPath(QDir(directory).absolutePath());
const QString cleanTarget = QDir::cleanPath(target);
if (cleanTarget != cleanDir
&& !cleanTarget.startsWith(cleanDir + QLatin1Char('/'))) {
if (error)
*error = QStringLiteral("Refusing to write outside %1").arg(cleanDir);
return {};
}
QFile file(target);
if (!file.open(QIODevice::WriteOnly)) {
if (error)
*error = file.errorString();
return {};
}
file.write(data);
file.close();
return target;
}
MimeParser::MimeParser()
{
ensureGMimeInit();
}
ParsedMessage MimeParser::parse(const QString &filePath) const
{
ParsedMessage out;
FILE *fp = fopen(filePath.toLocal8Bit().constData(), "r");
if (!fp) {
out.error = QStringLiteral("Cannot open %1").arg(filePath);
return out;
}
GMimeStream *stream = g_mime_stream_file_new(fp);
GMimeParser *parser = g_mime_parser_new_with_stream(stream);
GMimeMessage *message = g_mime_parser_construct_message(parser, nullptr);
g_object_unref(parser);
g_object_unref(stream);
if (!message) {
out.error = QStringLiteral("Cannot parse %1").arg(filePath);
return out;
}
out.subject = QString::fromUtf8(
g_mime_message_get_subject(message) ?: "");
out.from = headerText(message, "From");
out.to = headerText(message, "To");
out.cc = headerText(message, "Cc");
out.date = headerText(message, "Date");
out.messageId = QString::fromUtf8(
g_mime_message_get_message_id(message) ?: "");
GMimeObject *body = g_mime_message_get_mime_part(message);
if (body)
collectParts(body, out);
g_object_unref(message);
out.ok = true;
return out;
}
```
- [ ] **Step 6: Add to the library**
```cmake
add_library(qtmaildir_lib STATIC
keymap.cpp
config.cpp
mimeparser.cpp
)
```
- [ ] **Step 7: Run tests to verify they pass**
Run: `cmake --build build && ctest --test-dir build --output-on-failure`
Expected: `keymap`, `config`, `mimeparser` all PASS.
If `decodesEncodedHeaders` fails on the From value, check that GMime's
`g_mime_header_get_value` is returning the decoded form; older GMime needs
`g_mime_utils_header_decode_text` applied to the raw value instead.
- [ ] **Step 8: Commit**
```bash
git add src/mimeparser.h src/mimeparser.cpp src/CMakeLists.txt tests/
git commit -S -m "feat: add MimeParser with GMime and safe attachment naming"
```
---
## Task 5: Request interceptor — deny by default
This is the security-critical component. It gets the most careful test in the
project.
**Files:**
- Create: `src/requestinterceptor.h`, `src/requestinterceptor.cpp`
- Create: `tests/test_interceptor.cpp`
- Modify: `src/CMakeLists.txt`, `tests/CMakeLists.txt`
- [ ] **Step 1: Write the failing test**
Create `tests/test_interceptor.cpp`:
```cpp
#include <QtTest>
#include "requestinterceptor.h"
class TestInterceptor : public QObject
{
Q_OBJECT
private slots:
void blocksRemoteHttpByDefault();
void blocksRemoteHttpsByDefault();
void blocksFileUrlsAlways();
void allowsCidForCurrentMessage();
void blocksCidForForeignMessage();
void allowRemoteFlagPermitsHttpButNotFile();
void recordsThatSomethingWasBlocked();
void resetClearsBlockedFlag();
};
void TestInterceptor::blocksRemoteHttpByDefault()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("http://tracker.example/pixel.gif"))));
}
void TestInterceptor::blocksRemoteHttpsByDefault()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("https://cdn.example/style.css"))));
}
void TestInterceptor::blocksFileUrlsAlways()
{
RequestInterceptor interceptor;
interceptor.setAllowRemote(true);
// Even with remote content explicitly allowed, local files stay blocked:
// a message must never read the filesystem.
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("file:///etc/passwd"))));
}
void TestInterceptor::allowsCidForCurrentMessage()
{
RequestInterceptor interceptor;
interceptor.setAllowedCids({ QStringLiteral("logo@example.org") });
QVERIFY(interceptor.shouldAllow(QUrl(QStringLiteral("cid:logo@example.org"))));
}
void TestInterceptor::blocksCidForForeignMessage()
{
RequestInterceptor interceptor;
interceptor.setAllowedCids({ QStringLiteral("logo@example.org") });
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("cid:other@example.org"))));
}
void TestInterceptor::allowRemoteFlagPermitsHttpButNotFile()
{
RequestInterceptor interceptor;
interceptor.setAllowRemote(true);
QVERIFY(interceptor.shouldAllow(QUrl(QStringLiteral("https://cdn.example/img.png"))));
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("file:///etc/passwd"))));
}
void TestInterceptor::recordsThatSomethingWasBlocked()
{
RequestInterceptor interceptor;
QVERIFY(!interceptor.blockedAnything());
interceptor.shouldAllow(QUrl(QStringLiteral("http://tracker.example/p.gif")));
// Drives the "Remote content blocked" banner in the message header.
QVERIFY(interceptor.blockedAnything());
}
void TestInterceptor::resetClearsBlockedFlag()
{
RequestInterceptor interceptor;
interceptor.shouldAllow(QUrl(QStringLiteral("http://tracker.example/p.gif")));
QVERIFY(interceptor.blockedAnything());
interceptor.resetForNewMessage();
QVERIFY(!interceptor.blockedAnything());
// Remote permission never carries over to the next message.
QVERIFY(!interceptor.shouldAllow(QUrl(QStringLiteral("https://cdn.example/x.png"))));
}
QTEST_MAIN(TestInterceptor)
#include "test_interceptor.moc"
```
- [ ] **Step 2: Register and run to verify it fails**
Append to `tests/CMakeLists.txt`:
```cmake
add_qtmaildir_test(interceptor)
```
Run: `cmake -S . -B build -G Ninja && cmake --build build`
Expected: FAIL, `requestinterceptor.h: No such file or directory`.
- [ ] **Step 3: Write src/requestinterceptor.h**
The policy lives in `shouldAllow()`, a pure function of URL and state, so it is
testable without constructing a web engine profile. `interceptRequest()` is a
thin adapter over it.
```cpp
#pragma once
#include <QSet>
#include <QUrl>
#include <QWebEngineUrlRequestInterceptor>
/// Deny-by-default request policy for the message view.
///
/// A message body is untrusted input from a stranger. Everything is blocked
/// unless explicitly permitted: remote loads leak the fact that a message was
/// read (tracking pixels) and file: loads would expose the local filesystem.
class RequestInterceptor : public QWebEngineUrlRequestInterceptor
{
Q_OBJECT
public:
explicit RequestInterceptor(QObject *parent = nullptr);
/// The whole policy, as a pure function so it can be tested directly.
bool shouldAllow(const QUrl &url);
void interceptRequest(QWebEngineUrlRequestInfo &info) override;
/// Content-IDs belonging to the currently displayed message.
void setAllowedCids(const QSet<QString> &cids) { m_allowedCids = cids; }
/// The exact base URL passed to setHtml(). REQUIRED: without it every
/// qtmaildir: URL is denied and nothing renders. Only this exact URL is
/// trusted on that scheme; the scheme alone is not sufficient.
void setDocumentUrl(const QUrl &url) { m_documentUrl = url; }
/// Per-message opt-in, triggered by the user clicking "Load remote content".
/// Never persisted, never carried to the next message.
void setAllowRemote(bool allow) { m_allowRemote = allow; }
bool allowRemote() const { return m_allowRemote; }
/// True once any request has been denied, so the UI can offer the button.
bool blockedAnything() const { return m_blockedAnything; }
/// Called before rendering a new message: clears both the remote grant and
/// the blocked flag.
void resetForNewMessage();
private:
QSet<QString> m_allowedCids;
QUrl m_documentUrl;
bool m_allowRemote = false;
bool m_blockedAnything = false;
};
```
- [ ] **Step 4: Write src/requestinterceptor.cpp**
```cpp
#include "requestinterceptor.h"
#include <QWebEngineUrlRequestInfo>
RequestInterceptor::RequestInterceptor(QObject *parent)
: QWebEngineUrlRequestInterceptor(parent)
{
}
bool RequestInterceptor::shouldAllow(const QUrl &url)
{
const QString scheme = url.scheme();
// The document itself is loaded via setHtml() with a qtmaildir: base URL,
// so that exact URL must pass or nothing renders at all. Allowing the
// whole SCHEME would be a hole: a hostile body could reference
// qtmaildir://anything and be trusted, which would make this object's
// correctness depend on the scheme handler's. Fails closed when no
// document URL has been set.
if (scheme == QLatin1String("qtmaildir")) {
if (!m_documentUrl.isEmpty() && url == m_documentUrl)
return true;
m_blockedAnything = true;
return false;
}
// Inline parts of the current message only.
if (scheme == QLatin1String("cid")) {
// QUrl keeps a cid: body in path(), not host().
const QString id = url.path();
if (m_allowedCids.contains(id))
return true;
m_blockedAnything = true;
return false;
}
if (scheme == QLatin1String("http") || scheme == QLatin1String("https")) {
if (m_allowRemote)
return true;
m_blockedAnything = true;
return false;
}
// Everything else, file: above all, is denied unconditionally. There is no
// flag that enables it.
m_blockedAnything = true;
return false;
}
void RequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
if (!shouldAllow(info.requestUrl()))
info.block(true);
}
void RequestInterceptor::resetForNewMessage()
{
m_allowRemote = false;
m_blockedAnything = false;
}
```
- [ ] **Step 5: Add to the library**
```cmake
add_library(qtmaildir_lib STATIC
keymap.cpp
config.cpp
mimeparser.cpp
requestinterceptor.cpp
)
```
- [ ] **Step 6: Run tests to verify they pass**
Run: `cmake --build build && ctest --test-dir build --output-on-failure`
Expected: all four test binaries PASS, `interceptor` with 8 functions.
- [ ] **Step 7: Commit**
```bash
git add src/requestinterceptor.h src/requestinterceptor.cpp src/CMakeLists.txt tests/
git commit -S -m "feat: add deny-by-default web request interceptor"
```
---
## Task 6: CID scheme handler and HTML builder
**Files:**
- Create: `src/cidschemehandler.h`, `src/cidschemehandler.cpp`
- Create: `src/htmlbuilder.h`, `src/htmlbuilder.cpp`
- Modify: `src/CMakeLists.txt`
`HtmlBuilder` gets its own test binary rather than extending
`tests/test_mimeparser.cpp`, since escaping is a separate unit from parsing.
`CidSchemeHandler` has no unit test: it needs a live `QWebEngineUrlRequestJob`,
and the access rule it enforces is already asserted in Task 5.
- [ ] **Step 1: Write the failing test**
Create `tests/test_htmlbuilder.cpp`:
```cpp
#include <QtTest>
#include "htmlbuilder.h"
class TestHtmlBuilder : public QObject
{
Q_OBJECT
private slots:
void escapesPlainText();
void preservesHtmlBodyWhenHtmlRequested();
void marksQuotedLines();
void plainTextScriptTagIsNeutralised();
void buildsThreadWithAllMessages();
void collapsedMessageShowsStubOnly();
void threadNamespacesCidUrls();
};
void TestHtmlBuilder::escapesPlainText()
{
ParsedMessage msg;
msg.ok = true;
msg.plainBody = QStringLiteral("a < b & c > d");
const QString html = HtmlBuilder::build(msg, HtmlBuilder::ForcePlain);
QVERIFY(html.contains(QStringLiteral("a < b & c > d")));
}
void TestHtmlBuilder::preservesHtmlBodyWhenHtmlRequested()
{
ParsedMessage msg;
msg.ok = true;
msg.htmlBody = QStringLiteral("<p>hello</p>");
const QString html = HtmlBuilder::build(msg, HtmlBuilder::PreferHtml);
QVERIFY(html.contains(QStringLiteral("<p>hello</p>")));
}
void TestHtmlBuilder::marksQuotedLines()
{
ParsedMessage msg;
msg.ok = true;
msg.plainBody = QStringLiteral("reply\n> quoted\nend");
const QString html = HtmlBuilder::build(msg, HtmlBuilder::ForcePlain);
QVERIFY(html.contains(QStringLiteral("class=\"quote\"")));
}
void TestHtmlBuilder::plainTextScriptTagIsNeutralised()
{
ParsedMessage msg;
msg.ok = true;
msg.plainBody = QStringLiteral("<script>alert(1)</script>");
const QString html = HtmlBuilder::build(msg, HtmlBuilder::ForcePlain);
// Escaped, not embedded. (JavaScript is also disabled at the profile level,
// so this is the second of two independent defences.)
QVERIFY(!html.contains(QStringLiteral("<script>")));
QVERIFY(html.contains(QStringLiteral("<script>")));
}
void TestHtmlBuilder::buildsThreadWithAllMessages()
{
ThreadRenderItem first;
first.message.ok = true;
first.message.subject = QStringLiteral("First");
first.message.from = QStringLiteral("Alice");
first.message.plainBody = QStringLiteral("first body");
first.expanded = true;
ThreadRenderItem second;
second.message.ok = true;
second.message.subject = QStringLiteral("Second");
second.message.from = QStringLiteral("Bob");
second.message.plainBody = QStringLiteral("second body");
second.expanded = true;
const QString html =
HtmlBuilder::buildThread({ first, second }, HtmlBuilder::ForcePlain);
QVERIFY(html.contains(QStringLiteral("first body")));
QVERIFY(html.contains(QStringLiteral("second body")));
// Each message is its own section, so per-message CSS and anchors work.
QCOMPARE(html.count(QStringLiteral("class=\"message\"")), 2);
}
void TestHtmlBuilder::collapsedMessageShowsStubOnly()
{
ThreadRenderItem item;
item.message.ok = true;
item.message.from = QStringLiteral("Carol");
item.message.subject = QStringLiteral("Old news");
item.message.plainBody = QStringLiteral("secret body text");
item.expanded = false;
const QString html =
HtmlBuilder::buildThread({ item }, HtmlBuilder::ForcePlain);
// Unmatched messages collapse to a one-line stub; the body is not emitted.
QVERIFY(html.contains(QStringLiteral("Carol")));
QVERIFY(!html.contains(QStringLiteral("secret body text")));
QVERIFY(html.contains(QStringLiteral("class=\"stub\"")));
}
void TestHtmlBuilder::threadNamespacesCidUrls()
{
// Two messages in one document may both reference cid:logo@x. Without
// namespacing, the second would show the first's image.
ThreadRenderItem first;
first.message.ok = true;
first.message.htmlBody =
QStringLiteral("<img src=\"cid:logo@example.org\">");
first.expanded = true;
first.cidPrefix = QStringLiteral("m0");
ThreadRenderItem second;
second.message.ok = true;
second.message.htmlBody =
QStringLiteral("<img src=\"cid:logo@example.org\">");
second.expanded = true;
second.cidPrefix = QStringLiteral("m1");
const QString html =
HtmlBuilder::buildThread({ first, second }, HtmlBuilder::PreferHtml);
QVERIFY(html.contains(QStringLiteral("cid:m0!logo@example.org")));
QVERIFY(html.contains(QStringLiteral("cid:m1!logo@example.org")));
// The bare form must not survive, or it would resolve ambiguously.
QVERIFY(!html.contains(QStringLiteral("\"cid:logo@example.org\"")));
}
QTEST_MAIN(TestHtmlBuilder)
#include "test_htmlbuilder.moc"
```
- [ ] **Step 2: Register and run to verify it fails**
Append to `tests/CMakeLists.txt`:
```cmake
add_qtmaildir_test(htmlbuilder)
```
Run: `cmake -S . -B build -G Ninja && cmake --build build`
Expected: FAIL, `htmlbuilder.h: No such file or directory`.
- [ ] **Step 3: Write src/htmlbuilder.h**
```cpp
#pragma once
#include <QString>
#include <QList>
#include "mimeparser.h"
/// One message's place in a rendered thread.
struct ThreadRenderItem
{
ParsedMessage message;
/// Matched messages render in full; unmatched collapse to a one-line stub.
bool expanded = true;
/// Disambiguates cid: references. Two newsletters in one thread commonly
/// use the same Content-ID (cid:logo@example.org), which would collide in
/// a single document, so every reference is rewritten to
/// cid:<prefix>!<id>.
QString cidPrefix;
};
/// Turns parsed messages into the HTML string handed to the web view.
///
/// Plain text goes through the same path as HTML so the view has one render
/// path rather than two. A whole thread renders as ONE document rather than one
/// view per message: a thread of newsletters can hold dozens of messages, and a
/// QWebEngineView each would spawn a Chromium render process each.
class HtmlBuilder
{
public:
enum Mode {
PreferHtml, ///< Use the HTML part when the message has one.
ForcePlain, ///< Always render the plain part, escaped.
};
/// Single message, used for the error card and for tests.
static QString build(const ParsedMessage &message, Mode mode);
/// The whole thread, oldest first.
static QString buildThread(const QList<ThreadRenderItem> &items, Mode mode);
/// Rewrites cid: URLs in an HTML body to their namespaced form.
static QString namespaceCids(const QString &html, const QString &prefix);
private:
static QString renderPlain(const QString &text);
static QString renderBody(const ThreadRenderItem &item, Mode mode);
static QString renderStub(const ParsedMessage &message);
static QString document(const QString &bodyHtml);
};
```
- [ ] **Step 4: Write src/htmlbuilder.cpp**
```cpp
#include "htmlbuilder.h"
#include <QRegularExpression>
namespace {
const char *kStyle = R"CSS(
body { font-family: sans-serif; font-size: 10pt; margin: 12px; }
pre.plain { white-space: pre-wrap; word-wrap: break-word;
font-family: monospace; margin: 0; }
span.quote { color: #4a6f8a; }
.message { border-top: 1px solid #bbb; padding: 10px 0; }
.message:first-child { border-top: none; }
.msg-header { font-size: 9pt; color: #555; margin-bottom: 8px; }
.msg-header .who { font-weight: bold; color: #000; }
.stub { font-size: 9pt; color: #666; padding: 4px 0;
border-top: 1px solid #ddd; }
)CSS";
} // namespace
QString HtmlBuilder::renderPlain(const QString &text)
{
QString out;
out += QStringLiteral("<pre class=\"plain\">");
const QStringList lines = text.split(QLatin1Char('\n'));
for (int i = 0; i < lines.size(); ++i) {
const QString &line = lines.at(i);
const bool quoted = line.startsWith(QLatin1Char('>'));
if (quoted)
out += QStringLiteral("<span class=\"quote\">");
out += line.toHtmlEscaped();
if (quoted)
out += QStringLiteral("</span>");
if (i + 1 < lines.size())
out += QLatin1Char('\n');
}
out += QStringLiteral("</pre>");
return out;
}
QString HtmlBuilder::document(const QString &bodyHtml)
{
return QStringLiteral(
"<!DOCTYPE html><html><head><meta charset=\"utf-8\">"
"<style>%1</style></head><body>%2</body></html>")
.arg(QString::fromUtf8(kStyle), bodyHtml);
}
QString HtmlBuilder::namespaceCids(const QString &html, const QString &prefix)
{
if (prefix.isEmpty())
return html;
// Matches cid: in src/href attribute values, quoted either way.
static const QRegularExpression re(
QStringLiteral("(?<attr>src|href)\\s*=\\s*(?<q>[\"'])cid:(?<id>[^\"']+)\\k<q>"),
QRegularExpression::CaseInsensitiveOption);
QString out;
qsizetype last = 0;
auto it = re.globalMatch(html);
while (it.hasNext()) {
const QRegularExpressionMatch match = it.next();
out += html.mid(last, match.capturedStart() - last);
out += QStringLiteral("%1=%2cid:%3!%4%2")
.arg(match.captured(QStringLiteral("attr")),
match.captured(QStringLiteral("q")),
prefix,
match.captured(QStringLiteral("id")));
last = match.capturedEnd();
}
out += html.mid(last);
return out;
}
QString HtmlBuilder::renderBody(const ThreadRenderItem &item, Mode mode)
{
if (mode == PreferHtml && item.message.hasHtml())
return namespaceCids(item.message.htmlBody, item.cidPrefix);
return renderPlain(item.message.plainBody);
}
QString HtmlBuilder::renderStub(const ParsedMessage &message)
{
return QStringLiteral("<div class=\"stub\">%1 — %2</div>")
.arg(message.from.toHtmlEscaped(), message.subject.toHtmlEscaped());
}
QString HtmlBuilder::build(const ParsedMessage &message, Mode mode)
{
ThreadRenderItem item;
item.message = message;
item.expanded = true;
return document(renderBody(item, mode));
}
QString HtmlBuilder::buildThread(const QList<ThreadRenderItem> &items, Mode mode)
{
QString body;
for (int i = 0; i < items.size(); ++i) {
const ThreadRenderItem &item = items.at(i);
if (!item.expanded) {
body += renderStub(item.message);
continue;
}
body += QStringLiteral(
"<div class=\"message\" id=\"msg-%1\">"
"<div class=\"msg-header\"><span class=\"who\">%2</span><br>%3</div>"
"%4</div>")
.arg(QString::number(i),
item.message.from.toHtmlEscaped(),
item.message.date.toHtmlEscaped(),
renderBody(item, mode));
}
return document(body);
}
```
- [ ] **Step 5: Write src/cidschemehandler.h**
```cpp
#pragma once
#include <QHash>
#include <QWebEngineUrlSchemeHandler>
#include "mimeparser.h"
/// Serves cid: URLs from the currently displayed thread only.
///
/// Keys are the namespaced form "<prefix>!<content-id>" produced by
/// HtmlBuilder, so two messages in one thread that share a Content-ID do not
/// collide. The map is replaced wholesale on every thread change, so a thread
/// can never reference another thread's parts.
class CidSchemeHandler : public QWebEngineUrlSchemeHandler
{
Q_OBJECT
public:
explicit CidSchemeHandler(QObject *parent = nullptr);
void setParts(const QHash<QString, InlinePart> &parts) { m_parts = parts; }
/// Builds the namespaced key HtmlBuilder's rewritten URLs will request.
static QString namespacedKey(const QString &prefix, const QString &contentId)
{ return prefix + QLatin1Char('!') + contentId; }
void requestStarted(QWebEngineUrlRequestJob *job) override;
private:
QHash<QString, InlinePart> m_parts;
};
```
- [ ] **Step 6: Write src/cidschemehandler.cpp**
```cpp
#include "cidschemehandler.h"
#include <QBuffer>
#include <QWebEngineUrlRequestJob>
CidSchemeHandler::CidSchemeHandler(QObject *parent)
: QWebEngineUrlSchemeHandler(parent)
{
}
void CidSchemeHandler::requestStarted(QWebEngineUrlRequestJob *job)
{
const QString id = job->requestUrl().path();
if (!m_parts.contains(id)) {
job->fail(QWebEngineUrlRequestJob::UrlNotFound);
return;
}
const InlinePart part = m_parts.value(id);
// The buffer is parented to the job so it lives exactly as long as needed.
auto *buffer = new QBuffer(job);
buffer->setData(part.data);
buffer->open(QIODevice::ReadOnly);
job->reply(part.mimeType.toUtf8(), buffer);
}
```
- [ ] **Step 7: Add to the library and run tests**
```cmake
add_library(qtmaildir_lib STATIC
keymap.cpp
config.cpp
mimeparser.cpp
requestinterceptor.cpp
htmlbuilder.cpp
cidschemehandler.cpp
)
```
Run: `cmake --build build && ctest --test-dir build --output-on-failure`
Expected: five test binaries PASS.
- [ ] **Step 8: Commit**
```bash
git add src/htmlbuilder.h src/htmlbuilder.cpp src/cidschemehandler.h \
src/cidschemehandler.cpp src/CMakeLists.txt tests/
git commit -S -m "feat: add HTML builder and cid: scheme handler"
```
---
## Task 7: Value types and notmuch RAII wrappers
**Files:**
- Create: `src/types.h`
- Create: `src/nmraii.h`
- Modify: `src/CMakeLists.txt` (headers only, no new .cpp)
- [ ] **Step 1: Write src/types.h**
These are the only types that cross the thread boundary. They are plain values
with no pointers into notmuch, so a queued signal can copy them safely.
```cpp
#pragma once
#include <QDateTime>
#include <QMetaType>
#include <QString>
#include <QStringList>
struct ThreadSummary
{
QString threadId;
QString subject;
QString authors;
QDateTime date;
int totalCount = 0;
int matchedCount = 0;
QStringList tags;
bool isUnread() const { return tags.contains(QStringLiteral("unread")); }
bool isFlagged() const { return tags.contains(QStringLiteral("flagged")); }
};
struct MessageRef
{
QString messageId;
QString filePath;
QStringList tags;
/// True when the message itself matched the user's query, as opposed to
/// being pulled in only because a sibling in its thread matched. Drives
/// whether it renders expanded or as a stub.
bool matched = true;
};
/// One tag mutation, kept so it can be inverted for undo.
struct TagChange
{
QStringList messageIds;
QStringList added;
QStringList removed;
QString description; ///< Shown in the undo action's text.
TagChange inverted() const
{
return TagChange{ messageIds, removed, added,
QStringLiteral("Undo %1").arg(description) };
}
};
Q_DECLARE_METATYPE(ThreadSummary)
Q_DECLARE_METATYPE(MessageRef)
Q_DECLARE_METATYPE(TagChange)
```
- [ ] **Step 2: Write src/nmraii.h**
libnotmuch hands out raw C pointers with manual destroy calls. These wrappers
make leaks impossible on early return, which matters because the query paths
have several.
```cpp
#pragma once
#include <notmuch.h>
#include <utility>
/// Generic owner for a notmuch handle with a destroy function.
template <typename T, void (*Destroy)(T *)>
class NmHandle
{
public:
NmHandle() = default;
explicit NmHandle(T *handle) : m_handle(handle) {}
~NmHandle() { reset(); }
NmHandle(const NmHandle &) = delete;
NmHandle &operator=(const NmHandle &) = delete;
NmHandle(NmHandle &&other) noexcept
: m_handle(std::exchange(other.m_handle, nullptr)) {}
NmHandle &operator=(NmHandle &&other) noexcept
{
if (this != &other) {
reset();
m_handle = std::exchange(other.m_handle, nullptr);
}
return *this;
}
void reset(T *handle = nullptr)
{
if (m_handle)
Destroy(m_handle);
m_handle = handle;
}
T *get() const { return m_handle; }
explicit operator bool() const { return m_handle != nullptr; }
private:
T *m_handle = nullptr;
};
using NmQuery = NmHandle<notmuch_query_t, notmuch_query_destroy>;
using NmThreads = NmHandle<notmuch_threads_t, notmuch_threads_destroy>;
using NmMessages = NmHandle<notmuch_messages_t, notmuch_messages_destroy>;
using NmThread = NmHandle<notmuch_thread_t, notmuch_thread_destroy>;
using NmMessage = NmHandle<notmuch_message_t, notmuch_message_destroy>;
using NmTags = NmHandle<notmuch_tags_t, notmuch_tags_destroy>;
```
- [ ] **Step 3: Verify it compiles**
Both are headers, so add a trivial compile check by including them from
`src/notmuchworker.cpp` in the next task. For now confirm the include path
resolves:
Run:
```bash
echo '#include "src/nmraii.h"
#include "src/types.h"
int main() { return 0; }' > /tmp/nmcheck.cpp && \
g++ -fsyntax-only -std=c++17 -I/usr/include \
$(pkg-config --cflags Qt6Core) /tmp/nmcheck.cpp && echo OK
```
Expected: `OK`. If `Qt6Core` is not a valid pkg-config name on this system, use
`-I/usr/include/qt6 -I/usr/include/qt6/QtCore` instead.
- [ ] **Step 4: Commit**
```bash
git add src/types.h src/nmraii.h
git commit -S -m "feat: add cross-thread value types and notmuch RAII wrappers"
```
---
## Task 8: NotmuchWorker — batched queries
**Files:**
- Create: `src/notmuchworker.h`, `src/notmuchworker.cpp`
- Modify: `src/CMakeLists.txt`
**Changed 2026-08-02, superseding the spec's "no unit test" position.**
`NotmuchWorker` IS unit-tested, against a throwaway notmuch database built in
a temporary directory. The spec deferred this to manual verification on the
grounds that testing needs a real database; the answer is to build a fake one
rather than to skip the tests. This matters more than for any other class,
because `applyTags` is the only code in the project that WRITES to a notmuch
index, and a bug there corrupts real mail state.
The fixture creates a Maildir tree with a handful of messages, runs
`notmuch new` against a generated config pointing at it, and sets
`NOTMUCH_CONFIG` for the test process. Nothing touches the developer's own
`~/Mail` or `~/.notmuch-config`.
Task 13's manual checklist remains, but as confirmation against real data
rather than as the only coverage.
- [ ] **Step 1: Write src/notmuchworker.h**
```cpp
#pragma once
#include <QObject>
#include <QStringList>
#include <QVector>
#include "types.h"
struct _notmuch_database;
typedef struct _notmuch_database notmuch_database_t;
/// Owns the only notmuch database handle in the process.
///
/// libnotmuch is not thread-safe and queries over a large database block, so
/// this object lives on its own thread and the UI reaches it only through
/// queued signals. No notmuch pointer ever leaves this class.
class NotmuchWorker : public QObject
{
Q_OBJECT
public:
/// notmuchConfigPath may be empty, in which case notmuch resolves its own
/// config and therefore its own database.path.
explicit NotmuchWorker(const QString ¬muchConfigPath, QObject *parent = nullptr);
~NotmuchWorker() override;
/// Threads emitted per threadsReady() signal.
static constexpr int kBatchSize = 200;
public slots:
/// Runs a query. generation lets the UI discard results from a superseded
/// query without the worker needing to know about cancellation.
void runQuery(const QString &query, quint64 generation);
/// Loads the messages of one thread, oldest first. matchQuery is the
/// user's current query; messages matching it render expanded, the rest
/// as stubs.
void loadThread(const QString &threadId, const QString &matchQuery,
quint64 generation);
/// Applies tag changes. Opens the database read-write, applies, and closes
/// immediately: notmuch's write lock is exclusive process-wide, so holding
/// it would block the user's cron `notmuch new`.
void applyTags(const TagChange &change);
/// Batch tagging over whole threads. The UI holds thread ids, not message
/// ids, for rows it has not opened, so the resolution happens here where
/// the database handle lives. This is the path the archive/flag/delete
/// actions use on a multi-row selection.
void applyTagsToThreads(const QStringList &threadIds,
const QStringList &add,
const QStringList &remove,
const QString &description);
signals:
void threadsReady(const QVector<ThreadSummary> &threads, quint64 generation);
void queryFinished(int totalThreads, quint64 generation);
void threadLoaded(const QVector<MessageRef> &messages, quint64 generation);
void tagsApplied(const TagChange &change);
void errorOccurred(const QString &message);
private:
bool openReadOnly();
void close();
QString m_configPath;
notmuch_database_t *m_db = nullptr;
};
```
- [ ] **Step 2: Write src/notmuchworker.cpp**
```cpp
#include "notmuchworker.h"
#include <notmuch.h>
#include <QSet>
#include "nmraii.h"
namespace {
QStringList tagsOf(notmuch_message_t *message)
{
QStringList result;
NmTags tags(notmuch_message_get_tags(message));
for (; notmuch_tags_valid(tags.get()); notmuch_tags_move_to_next(tags.get()))
result.append(QString::fromUtf8(notmuch_tags_get(tags.get())));
return result;
}
QStringList tagsOf(notmuch_thread_t *thread)
{
QStringList result;
NmTags tags(notmuch_thread_get_tags(thread));
for (; notmuch_tags_valid(tags.get()); notmuch_tags_move_to_next(tags.get()))
result.append(QString::fromUtf8(notmuch_tags_get(tags.get())));
return result;
}
} // namespace
NotmuchWorker::NotmuchWorker(const QString ¬muchConfigPath, QObject *parent)
: QObject(parent), m_configPath(notmuchConfigPath)
{
}
NotmuchWorker::~NotmuchWorker()
{
close();
}
bool NotmuchWorker::openReadOnly()
{
if (m_db)
return true;
char *error = nullptr;
const notmuch_status_t status = notmuch_database_open_with_config(
nullptr, // let config decide path
NOTMUCH_DATABASE_MODE_READ_ONLY,
m_configPath.isEmpty() ? nullptr
: m_configPath.toLocal8Bit().constData(),
nullptr,
&m_db,
&error);
if (status != NOTMUCH_STATUS_SUCCESS) {
emit errorOccurred(QStringLiteral("Cannot open notmuch database: %1")
.arg(QString::fromUtf8(error ? error : notmuch_status_to_string(status))));
free(error);
m_db = nullptr;
return false;
}
return true;
}
void NotmuchWorker::close()
{
if (m_db) {
notmuch_database_destroy(m_db);
m_db = nullptr;
}
}
void NotmuchWorker::runQuery(const QString &query, quint64 generation)
{
if (!openReadOnly())
return;
NmQuery nmQuery(notmuch_query_create(m_db, query.toUtf8().constData()));
if (!nmQuery) {
emit errorOccurred(QStringLiteral("Invalid query: %1").arg(query));
return;
}
notmuch_query_set_sort(nmQuery.get(), NOTMUCH_SORT_NEWEST_FIRST);
notmuch_threads_t *rawThreads = nullptr;
const notmuch_status_t status =
notmuch_query_search_threads(nmQuery.get(), &rawThreads);
if (status != NOTMUCH_STATUS_SUCCESS) {
emit errorOccurred(QStringLiteral("Query failed: %1")
.arg(QString::fromUtf8(notmuch_status_to_string(status))));
return;
}
NmThreads threads(rawThreads);
QVector<ThreadSummary> batch;
batch.reserve(kBatchSize);
int total = 0;
for (; notmuch_threads_valid(threads.get());
notmuch_threads_move_to_next(threads.get())) {
NmThread thread(notmuch_threads_get(threads.get()));
if (!thread)
continue;
ThreadSummary summary;
summary.threadId = QString::fromUtf8(notmuch_thread_get_thread_id(thread.get()));
summary.subject = QString::fromUtf8(notmuch_thread_get_subject(thread.get()));
summary.authors = QString::fromUtf8(notmuch_thread_get_authors(thread.get()));
summary.date = QDateTime::fromSecsSinceEpoch(
notmuch_thread_get_newest_date(thread.get()));
summary.totalCount = notmuch_thread_get_total_messages(thread.get());
summary.matchedCount = notmuch_thread_get_matched_messages(thread.get());
summary.tags = tagsOf(thread.get());
batch.append(summary);
++total;
if (batch.size() >= kBatchSize) {
emit threadsReady(batch, generation);
batch.clear();
batch.reserve(kBatchSize);
}
}
if (!batch.isEmpty())
emit threadsReady(batch, generation);
emit queryFinished(total, generation);
}
void NotmuchWorker::loadThread(const QString &threadId,
const QString &matchQuery,
quint64 generation)
{
if (!openReadOnly())
return;
// Which messages of the thread matched the user's query. Running the query
// intersected with the thread is cheaper than testing each message.
QSet<QString> matchedIds;
if (!matchQuery.trimmed().isEmpty()) {
const QString intersect =
QStringLiteral("thread:%1 and (%2)").arg(threadId, matchQuery);
NmQuery matchQ(
notmuch_query_create(m_db, intersect.toUtf8().constData()));
if (matchQ) {
notmuch_messages_t *rawMatched = nullptr;
if (notmuch_query_search_messages(matchQ.get(), &rawMatched)
== NOTMUCH_STATUS_SUCCESS) {
NmMessages matched(rawMatched);
for (; notmuch_messages_valid(matched.get());
notmuch_messages_move_to_next(matched.get())) {
NmMessage message(notmuch_messages_get(matched.get()));
if (message) {
matchedIds.insert(QString::fromUtf8(
notmuch_message_get_message_id(message.get())));
}
}
}
}
}
const QString query = QStringLiteral("thread:%1").arg(threadId);
NmQuery nmQuery(notmuch_query_create(m_db, query.toUtf8().constData()));
if (!nmQuery) {
emit errorOccurred(QStringLiteral("Cannot load thread %1").arg(threadId));
return;
}
notmuch_query_set_sort(nmQuery.get(), NOTMUCH_SORT_OLDEST_FIRST);
notmuch_messages_t *rawMessages = nullptr;
if (notmuch_query_search_messages(nmQuery.get(), &rawMessages)
!= NOTMUCH_STATUS_SUCCESS) {
emit errorOccurred(QStringLiteral("Cannot search thread %1").arg(threadId));
return;
}
NmMessages messages(rawMessages);
QVector<MessageRef> result;
for (; notmuch_messages_valid(messages.get());
notmuch_messages_move_to_next(messages.get())) {
NmMessage message(notmuch_messages_get(messages.get()));
if (!message)
continue;
MessageRef ref;
ref.messageId = QString::fromUtf8(notmuch_message_get_message_id(message.get()));
ref.filePath = QString::fromUtf8(notmuch_message_get_filename(message.get()));
ref.tags = tagsOf(message.get());
// With no query to intersect, everything counts as matched.
ref.matched = matchedIds.isEmpty() || matchedIds.contains(ref.messageId);
result.append(ref);
}
emit threadLoaded(result, generation);
}
void NotmuchWorker::applyTagsToThreads(const QStringList &threadIds,
const QStringList &add,
const QStringList &remove,
const QString &description)
{
if (threadIds.isEmpty())
return;
if (!openReadOnly())
return;
// Resolve every thread to its message ids in ONE query. Issuing a query per
// thread would reopen the same Xapian cursor hundreds of times on a large
// selection.
QStringList terms;
terms.reserve(threadIds.size());
for (const QString &id : threadIds)
terms.append(QStringLiteral("thread:%1").arg(id));
const QString query = terms.join(QStringLiteral(" or "));
NmQuery nmQuery(notmuch_query_create(m_db, query.toUtf8().constData()));
if (!nmQuery) {
emit errorOccurred(QStringLiteral("Cannot resolve selected threads"));
return;
}
notmuch_messages_t *rawMessages = nullptr;
if (notmuch_query_search_messages(nmQuery.get(), &rawMessages)
!= NOTMUCH_STATUS_SUCCESS) {
emit errorOccurred(QStringLiteral("Cannot resolve selected threads"));
return;
}
NmMessages messages(rawMessages);
QStringList messageIds;
for (; notmuch_messages_valid(messages.get());
notmuch_messages_move_to_next(messages.get())) {
NmMessage message(notmuch_messages_get(messages.get()));
if (message) {
messageIds.append(
QString::fromUtf8(notmuch_message_get_message_id(message.get())));
}
}
if (messageIds.isEmpty()) {
emit errorOccurred(QStringLiteral("Selected threads contain no messages"));
return;
}
applyTags(TagChange{ messageIds, add, remove, description });
}
void NotmuchWorker::applyTags(const TagChange &change)
{
if (change.messageIds.isEmpty())
return;
// The read-only handle must be closed first: notmuch allows only one open
// handle per process.
close();
notmuch_database_t *db = nullptr;
char *error = nullptr;
const notmuch_status_t status = notmuch_database_open_with_config(
nullptr,
NOTMUCH_DATABASE_MODE_READ_WRITE,
m_configPath.isEmpty() ? nullptr
: m_configPath.toLocal8Bit().constData(),
nullptr,
&db,
&error);
if (status != NOTMUCH_STATUS_SUCCESS) {
emit errorOccurred(
QStringLiteral("Cannot open database for writing (is a sync running?): %1")
.arg(QString::fromUtf8(error ? error
: notmuch_status_to_string(status))));
free(error);
return;
}
for (const QString &id : change.messageIds) {
notmuch_message_t *raw = nullptr;
if (notmuch_database_find_message(db, id.toUtf8().constData(), &raw)
!= NOTMUCH_STATUS_SUCCESS || !raw) {
continue;
}
NmMessage message(raw);
notmuch_message_freeze(message.get());
for (const QString &tag : change.removed)
notmuch_message_remove_tag(message.get(), tag.toUtf8().constData());
for (const QString &tag : change.added)
notmuch_message_add_tag(message.get(), tag.toUtf8().constData());
notmuch_message_thaw(message.get());
notmuch_message_tags_to_maildir_flags(message.get());
}
notmuch_database_close(db);
notmuch_database_destroy(db);
emit tagsApplied(change);
}
```
- [ ] **Step 3: Add to the library**
```cmake
add_library(qtmaildir_lib STATIC
keymap.cpp
config.cpp
mimeparser.cpp
requestinterceptor.cpp
htmlbuilder.cpp
cidschemehandler.cpp
notmuchworker.cpp
)
```
- [ ] **Step 4: Build and verify existing tests still pass**
Run: `cmake --build build && ctest --test-dir build --output-on-failure`
Expected: builds clean, five test binaries still PASS.
If `notmuch_database_open_with_config` is not declared, the installed notmuch is
older than 0.32. Check with `grep open_with_config /usr/include/notmuch.h`; the
verified system has 0.39, which has it.
- [ ] **Step 5: Commit**
```bash
git add src/notmuchworker.h src/notmuchworker.cpp src/CMakeLists.txt
git commit -S -m "feat: add NotmuchWorker with batched queries and tag mutation"
```
---
## Task 9: ThreadListModel
**Files:**
- Create: `src/threadlistmodel.h`, `src/threadlistmodel.cpp`
- Create: `tests/test_threadlistmodel.cpp`
- Modify: `src/CMakeLists.txt`, `tests/CMakeLists.txt`
- [ ] **Step 1: Write the failing test**
Create `tests/test_threadlistmodel.cpp`:
```cpp
#include <QtTest>
#include <QAbstractItemModelTester>
#include "threadlistmodel.h"
class TestThreadListModel : public QObject
{
Q_OBJECT
private slots:
void startsEmpty();
void appendsBatches();
void clearResetsModel();
void reportsSubjectAndAuthors();
void updatesTagsForMessage();
void modelPassesQtTester();
};
static ThreadSummary makeThread(const QString &id, const QString &subject)
{
ThreadSummary t;
t.threadId = id;
t.subject = subject;
t.authors = QStringLiteral("Alice");
t.date = QDateTime::fromSecsSinceEpoch(1750000000);
t.totalCount = 2;
t.matchedCount = 1;
t.tags = QStringList{ QStringLiteral("inbox"), QStringLiteral("unread") };
return t;
}
void TestThreadListModel::startsEmpty()
{
ThreadListModel model;
QCOMPARE(model.rowCount(), 0);
QCOMPARE(model.columnCount(), ThreadListModel::ColumnCount);
}
void TestThreadListModel::appendsBatches()
{
ThreadListModel model;
model.appendBatch({ makeThread(QStringLiteral("t1"), QStringLiteral("one")) });
QCOMPARE(model.rowCount(), 1);
model.appendBatch({ makeThread(QStringLiteral("t2"), QStringLiteral("two")),
makeThread(QStringLiteral("t3"), QStringLiteral("three")) });
QCOMPARE(model.rowCount(), 3);
QCOMPARE(model.threadAt(2).threadId, QStringLiteral("t3"));
}
void TestThreadListModel::clearResetsModel()
{
ThreadListModel model;
model.appendBatch({ makeThread(QStringLiteral("t1"), QStringLiteral("one")) });
QSignalSpy spy(&model, &QAbstractItemModel::modelReset);
model.clear();
QCOMPARE(model.rowCount(), 0);
QCOMPARE(spy.count(), 1);
}
void TestThreadListModel::reportsSubjectAndAuthors()
{
ThreadListModel model;
model.appendBatch({ makeThread(QStringLiteral("t1"), QStringLiteral("hello")) });
const QModelIndex subject = model.index(0, ThreadListModel::SubjectColumn);
QCOMPARE(model.data(subject, Qt::DisplayRole).toString(),
QStringLiteral("hello"));
const QModelIndex authors = model.index(0, ThreadListModel::AuthorsColumn);
QCOMPARE(model.data(authors, Qt::DisplayRole).toString(),
QStringLiteral("Alice"));
}
void TestThreadListModel::updatesTagsForMessage()
{
ThreadListModel model;
model.appendBatch({ makeThread(QStringLiteral("t1"), QStringLiteral("one")) });
QVERIFY(model.threadAt(0).isUnread());
// Optimistic UI: the model changes before the worker confirms.
model.applyTagChange(QStringLiteral("t1"), {}, { QStringLiteral("unread") });
QVERIFY(!model.threadAt(0).isUnread());
}
void TestThreadListModel::modelPassesQtTester()
{
ThreadListModel model;
// Catches signal/rowCount contract violations that hand-written tests miss.
QAbstractItemModelTester tester(&model,
QAbstractItemModelTester::FailureReportingMode::QtTest);
model.appendBatch({ makeThread(QStringLiteral("t1"), QStringLiteral("one")),
makeThread(QStringLiteral("t2"), QStringLiteral("two")) });
model.clear();
}
QTEST_MAIN(TestThreadListModel)
#include "test_threadlistmodel.moc"
```
- [ ] **Step 2: Register and run to verify it fails**
Append to `tests/CMakeLists.txt`:
```cmake
add_qtmaildir_test(threadlistmodel)
```
Run: `cmake -S . -B build -G Ninja && cmake --build build`
Expected: FAIL, `threadlistmodel.h: No such file or directory`.
- [ ] **Step 3: Write src/threadlistmodel.h**
```cpp
#pragma once
#include <QAbstractTableModel>
#include <QVector>
#include "types.h"
/// Table model over query results, filled in batches so a large query paints
/// its first screenful immediately.
class ThreadListModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum Column {
DateColumn = 0,
AuthorsColumn,
SubjectColumn,
TagsColumn,
ColumnCount,
};
explicit ThreadListModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = {}) const override;
int columnCount(const QModelIndex &parent = {}) const override;
QVariant data(const QModelIndex &index, int role) const override;
QVariant headerData(int section, Qt::Orientation orientation,
int role) const override;
void appendBatch(const QVector<ThreadSummary> &batch);
void clear();
ThreadSummary threadAt(int row) const;
/// Applies a tag change locally so the UI updates before the worker
/// confirms. Reverted by the caller if the worker reports failure.
void applyTagChange(const QString &threadId, const QStringList &added,
const QStringList &removed);
private:
QVector<ThreadSummary> m_threads;
};
```
- [ ] **Step 4: Write src/threadlistmodel.cpp**
```cpp
#include "threadlistmodel.h"
#include <QFont>
ThreadListModel::ThreadListModel(QObject *parent)
: QAbstractTableModel(parent)
{
}
int ThreadListModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : m_threads.size();
}
int ThreadListModel::columnCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : ColumnCount;
}
QVariant ThreadListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= m_threads.size())
return {};
const ThreadSummary &thread = m_threads.at(index.row());
if (role == Qt::DisplayRole) {
switch (index.column()) {
case DateColumn:
return thread.date.toString(QStringLiteral("yyyy-MM-dd hh:mm"));
case AuthorsColumn:
return thread.authors;
case SubjectColumn:
return thread.totalCount > 1
? QStringLiteral("%1 (%2)").arg(thread.subject)
.arg(thread.totalCount)
: thread.subject;
case TagsColumn:
return thread.tags.join(QLatin1Char(' '));
default:
return {};
}
}
if (role == Qt::FontRole && thread.isUnread()) {
QFont font;
font.setBold(true);
return font;
}
return {};
}
QVariant ThreadListModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
return {};
switch (section) {
case DateColumn: return QStringLiteral("Date");
case AuthorsColumn: return QStringLiteral("From");
case SubjectColumn: return QStringLiteral("Subject");
case TagsColumn: return QStringLiteral("Tags");
default: return {};
}
}
void ThreadListModel::appendBatch(const QVector<ThreadSummary> &batch)
{
if (batch.isEmpty())
return;
const int first = m_threads.size();
beginInsertRows({}, first, first + batch.size() - 1);
m_threads.append(batch);
endInsertRows();
}
void ThreadListModel::clear()
{
beginResetModel();
m_threads.clear();
endResetModel();
}
ThreadSummary ThreadListModel::threadAt(int row) const
{
if (row < 0 || row >= m_threads.size())
return {};
return m_threads.at(row);
}
void ThreadListModel::applyTagChange(const QString &threadId,
const QStringList &added,
const QStringList &removed)
{
for (int row = 0; row < m_threads.size(); ++row) {
if (m_threads.at(row).threadId != threadId)
continue;
QStringList &tags = m_threads[row].tags;
for (const QString &tag : removed)
tags.removeAll(tag);
for (const QString &tag : added) {
if (!tags.contains(tag))
tags.append(tag);
}
emit dataChanged(index(row, 0), index(row, ColumnCount - 1));
return;
}
}
```
- [ ] **Step 5: Add to the library and run tests**
```cmake
add_library(qtmaildir_lib STATIC
keymap.cpp
config.cpp
mimeparser.cpp
requestinterceptor.cpp
htmlbuilder.cpp
cidschemehandler.cpp
notmuchworker.cpp
threadlistmodel.cpp
)
```
Run: `cmake --build build && ctest --test-dir build --output-on-failure`
Expected: six test binaries PASS.
- [ ] **Step 6: Commit**
```bash
git add src/threadlistmodel.h src/threadlistmodel.cpp src/CMakeLists.txt tests/
git commit -S -m "feat: add ThreadListModel with batch append"
```
---
## Task 10: MailSync — QProcess wrapper
**Files:**
- Create: `src/mailsync.h`, `src/mailsync.cpp`
- Create: `tests/test_mailsync.cpp`
- Modify: `src/CMakeLists.txt`, `tests/CMakeLists.txt`
- [ ] **Step 1: Write the failing test**
Create `tests/test_mailsync.cpp`:
```cpp
#include <QtTest>
#include <QSignalSpy>
#include "mailsync.h"
class TestMailSync : public QObject
{
Q_OBJECT
private slots:
void unavailableWhenCommandEmpty();
void successfulRunEmitsFinished();
void failedRunReportsExitCode();
void capturesOutput();
void refusesConcurrentRuns();
};
void TestMailSync::unavailableWhenCommandEmpty()
{
MailSync sync(QString());
QVERIFY(!sync.isAvailable());
QVERIFY(!sync.start());
}
void TestMailSync::successfulRunEmitsFinished()
{
MailSync sync(QStringLiteral("/bin/true"));
QVERIFY(sync.isAvailable());
QSignalSpy spy(&sync, &MailSync::finished);
QVERIFY(sync.start());
QVERIFY(spy.wait(5000));
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.first().at(0).toBool(), true);
}
void TestMailSync::failedRunReportsExitCode()
{
MailSync sync(QStringLiteral("/bin/false"));
QSignalSpy spy(&sync, &MailSync::finished);
QVERIFY(sync.start());
QVERIFY(spy.wait(5000));
QCOMPARE(spy.first().at(0).toBool(), false);
}
void TestMailSync::capturesOutput()
{
MailSync sync(QStringLiteral("/bin/echo syncing"));
QSignalSpy spy(&sync, &MailSync::finished);
QVERIFY(sync.start());
QVERIFY(spy.wait(5000));
QVERIFY(sync.log().contains(QStringLiteral("syncing")));
}
void TestMailSync::refusesConcurrentRuns()
{
MailSync sync(QStringLiteral("/bin/sleep 2"));
QVERIFY(sync.start());
// The cron sync and this one share a flock; starting twice from the GUI is
// still refused locally so the button cannot queue runs.
QVERIFY(!sync.start());
QVERIFY(sync.isRunning());
}
QTEST_MAIN(TestMailSync)
#include "test_mailsync.moc"
```
- [ ] **Step 2: Register and run to verify it fails**
Append to `tests/CMakeLists.txt`:
```cmake
add_qtmaildir_test(mailsync)
```
Run: `cmake -S . -B build -G Ninja && cmake --build build`
Expected: FAIL, `mailsync.h: No such file or directory`.
- [ ] **Step 3: Write src/mailsync.h**
```cpp
#pragma once
#include <QObject>
#include <QProcess>
#include <QString>
/// Runs the configured external sync command.
///
/// qtmaildir deliberately does not implement sync itself. The existing script
/// holds a flock that is the shared mutex between the user's cron sync, which
/// runs every 10 minutes, and any manual sync; running the script joins that
/// mutex, whereas a built-in implementation would sit outside it and could run
/// mbsync concurrently with cron, corrupting Maildir UID state.
class MailSync : public QObject
{
Q_OBJECT
public:
explicit MailSync(const QString &command, QObject *parent = nullptr);
/// False when no command is configured; the UI disables its Sync button.
bool isAvailable() const { return !m_command.isEmpty(); }
bool isRunning() const;
/// Returns false if unavailable or already running.
bool start();
QString log() const { return m_log; }
signals:
void started();
void outputReceived(const QString &chunk);
void finished(bool success, int exitCode);
private:
void handleReadyRead();
void handleFinished(int exitCode, QProcess::ExitStatus status);
QString m_command;
QProcess m_process;
QString m_log;
};
```
- [ ] **Step 4: Write src/mailsync.cpp**
```cpp
#include "mailsync.h"
MailSync::MailSync(const QString &command, QObject *parent)
: QObject(parent), m_command(command)
{
m_process.setProcessChannelMode(QProcess::MergedChannels);
connect(&m_process, &QProcess::readyRead,
this, &MailSync::handleReadyRead);
connect(&m_process, &QProcess::finished,
this, &MailSync::handleFinished);
}
bool MailSync::isRunning() const
{
return m_process.state() != QProcess::NotRunning;
}
bool MailSync::start()
{
if (!isAvailable() || isRunning())
return false;
m_log.clear();
// splitCommand handles quoted arguments; running through a shell would make
// a config value into an injection point.
const QStringList parts = QProcess::splitCommand(m_command);
if (parts.isEmpty())
return false;
m_process.setProgram(parts.first());
m_process.setArguments(parts.mid(1));
m_process.start();
if (!m_process.waitForStarted(5000))
return false;
emit started();
return true;
}
void MailSync::handleReadyRead()
{
const QString chunk = QString::fromUtf8(m_process.readAll());
m_log += chunk;
emit outputReceived(chunk);
}
void MailSync::handleFinished(int exitCode, QProcess::ExitStatus status)
{
// Drain anything buffered at exit.
handleReadyRead();
const bool success =
status == QProcess::NormalExit && exitCode == 0;
emit finished(success, exitCode);
}
```
- [ ] **Step 5: Add to the library and run tests**
```cmake
add_library(qtmaildir_lib STATIC
keymap.cpp
config.cpp
mimeparser.cpp
requestinterceptor.cpp
htmlbuilder.cpp
cidschemehandler.cpp
notmuchworker.cpp
threadlistmodel.cpp
mailsync.cpp
)
```
Run: `cmake --build build && ctest --test-dir build --output-on-failure`
Expected: seven test binaries PASS.
- [ ] **Step 6: Commit**
```bash
git add src/mailsync.h src/mailsync.cpp src/CMakeLists.txt tests/
git commit -S -m "feat: add MailSync process wrapper"
```
---
## Task 11: MessageView — the locked-down web view
**Files:**
- Create: `src/messageview.h`, `src/messageview.cpp`
- Modify: `src/CMakeLists.txt`
No unit test: this is a widget that needs a live `QWebEngineProfile`. Its policy
logic is already tested in Task 5; this task only wires it. Verified manually in
Task 13.
- [ ] **Step 1: Write src/messageview.h**
```cpp
#pragma once
#include <QList>
#include <QWidget>
#include "htmlbuilder.h"
#include "mimeparser.h"
class QLabel;
class QPushButton;
class QWebEngineView;
class QWebEngineProfile;
class CidSchemeHandler;
class RequestInterceptor;
/// The message pane: thread header, body, attachment bar.
///
/// A whole thread renders into one web view. A newsletter thread can hold
/// dozens of messages, and one view per message would spawn one Chromium
/// render process per message.
class MessageView : public QWidget
{
Q_OBJECT
public:
explicit MessageView(QWidget *parent = nullptr);
~MessageView() override;
/// Renders a whole thread, oldest first. Items whose expanded flag is
/// false collapse to a one-line stub.
void showThread(const QList<ThreadRenderItem> &items);
void showError(const QString &text, const QString &filePath);
void clear();
public slots:
void toggleHtml();
void loadRemoteContent();
signals:
void statusMessage(const QString &text);
private:
void render();
void updateHeader();
QList<ThreadRenderItem> m_items;
bool m_preferHtml = true;
QWebEngineProfile *m_profile = nullptr;
QWebEngineView *m_view = nullptr;
RequestInterceptor *m_interceptor = nullptr;
CidSchemeHandler *m_cidHandler = nullptr;
QLabel *m_headerLabel = nullptr;
QLabel *m_blockedLabel = nullptr;
QPushButton *m_loadRemoteButton = nullptr;
QWidget *m_attachmentBar = nullptr;
};
```
- [ ] **Step 2: Write src/messageview.cpp**
```cpp
#include "messageview.h"
#include <QDesktopServices>
#include <QFileDialog>
#include <QHBoxLayout>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>
#include <QTimer>
#include <QVBoxLayout>
#include <QWebEngineProfile>
#include <QWebEngineSettings>
#include <QWebEngineView>
#include <QWebEnginePage>
#include <algorithm>
#include "cidschemehandler.h"
#include "htmlbuilder.h"
#include "requestinterceptor.h"
namespace {
/// Intercepts link clicks so a message can never navigate the pane.
class MessagePage : public QWebEnginePage
{
public:
MessagePage(QWebEngineProfile *profile, QObject *parent)
: QWebEnginePage(profile, parent) {}
protected:
bool acceptNavigationRequest(const QUrl &url, NavigationType type,
bool isMainFrame) override
{
if (type == NavigationTypeTyped || url.scheme() == QLatin1String("qtmaildir"))
return true;
if (type == NavigationTypeLinkClicked) {
QDesktopServices::openUrl(url);
return false;
}
return isMainFrame ? false : true;
}
};
} // namespace
MessageView::MessageView(QWidget *parent)
: QWidget(parent)
{
// Off-the-record profile: no cookies, no cache, nothing persisted.
m_profile = new QWebEngineProfile(this);
m_profile->setHttpCacheType(QWebEngineProfile::NoCache);
m_profile->setPersistentCookiesPolicy(QWebEngineProfile::NoPersistentCookies);
m_interceptor = new RequestInterceptor(this);
m_profile->setUrlRequestInterceptor(m_interceptor);
m_cidHandler = new CidSchemeHandler(this);
m_profile->installUrlSchemeHandler(QByteArrayLiteral("cid"), m_cidHandler);
m_view = new QWebEngineView(this);
m_view->setPage(new MessagePage(m_profile, m_view));
QWebEngineSettings *settings = m_view->settings();
settings->setAttribute(QWebEngineSettings::JavascriptEnabled, false);
settings->setAttribute(QWebEngineSettings::LocalContentCanAccessRemoteUrls, false);
settings->setAttribute(QWebEngineSettings::LocalContentCanAccessFileUrls, false);
settings->setAttribute(QWebEngineSettings::PluginsEnabled, false);
settings->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, false);
m_headerLabel = new QLabel(this);
m_headerLabel->setTextFormat(Qt::RichText);
m_headerLabel->setWordWrap(true);
m_headerLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
m_blockedLabel = new QLabel(tr("Remote content blocked"), this);
m_loadRemoteButton = new QPushButton(tr("Load remote content"), this);
connect(m_loadRemoteButton, &QPushButton::clicked,
this, &MessageView::loadRemoteContent);
auto *blockedRow = new QHBoxLayout;
blockedRow->addWidget(m_blockedLabel);
blockedRow->addWidget(m_loadRemoteButton);
blockedRow->addStretch();
m_attachmentBar = new QWidget(this);
new QHBoxLayout(m_attachmentBar);
auto *layout = new QVBoxLayout(this);
layout->addWidget(m_headerLabel);
layout->addLayout(blockedRow);
layout->addWidget(m_view, 1);
layout->addWidget(m_attachmentBar);
clear();
}
MessageView::~MessageView() = default;
void MessageView::clear()
{
m_items.clear();
m_view->setHtml(QString());
m_headerLabel->clear();
m_blockedLabel->hide();
m_loadRemoteButton->hide();
}
void MessageView::showThread(const QList<ThreadRenderItem> &items)
{
m_items = items;
m_preferHtml = true;
// Every thread starts from a clean policy: no remote grant carries over.
m_interceptor->resetForNewMessage();
// Flatten every message's inline parts into one namespaced map, so two
// messages sharing a Content-ID resolve to different parts.
QHash<QString, InlinePart> allParts;
QSet<QString> cids;
for (const ThreadRenderItem &item : m_items) {
for (auto it = item.message.inlineParts.cbegin();
it != item.message.inlineParts.cend(); ++it) {
const QString key =
CidSchemeHandler::namespacedKey(item.cidPrefix, it.key());
allParts.insert(key, it.value());
cids.insert(key);
}
}
m_interceptor->setAllowedCids(cids);
m_cidHandler->setParts(allParts);
// REQUIRED by RequestInterceptor: it trusts only this exact URL on the
// qtmaildir: scheme and fails closed otherwise, so this must match the
// base URL passed to setHtml() in render() or nothing renders at all.
m_interceptor->setDocumentUrl(QUrl(QStringLiteral("qtmaildir://message")));
updateHeader();
render();
}
void MessageView::showError(const QString &text, const QString &filePath)
{
m_items.clear();
m_headerLabel->setText(tr("<b>Cannot display message</b>"));
m_blockedLabel->hide();
m_loadRemoteButton->hide();
const QString html = QStringLiteral(
"<html><body><p>%1</p><p><code>%2</code></p></body></html>")
.arg(text.toHtmlEscaped(), filePath.toHtmlEscaped());
m_view->setHtml(html, QUrl(QStringLiteral("qtmaildir://message")));
}
void MessageView::updateHeader()
{
if (m_items.isEmpty()) {
m_headerLabel->clear();
return;
}
// The thread's subject comes from its first message; later replies carry
// Re: prefixes that add nothing.
const QString subject = m_items.first().message.subject;
m_headerLabel->setText(
QStringLiteral("<b>%1</b><br><small>%2</small>")
.arg(subject.toHtmlEscaped(),
tr("%n message(s) in thread", "", m_items.size())));
}
void MessageView::render()
{
const HtmlBuilder::Mode mode =
m_preferHtml ? HtmlBuilder::PreferHtml : HtmlBuilder::ForcePlain;
// The base URL uses a scheme the interceptor recognises, so the document
// itself loads while everything it references is still filtered.
m_view->setHtml(HtmlBuilder::buildThread(m_items, mode),
QUrl(QStringLiteral("qtmaildir://message")));
// Blocking is discovered during load, so check shortly afterwards.
QTimer::singleShot(300, this, [this]() {
const bool blocked = m_interceptor->blockedAnything()
&& !m_interceptor->allowRemote();
m_blockedLabel->setVisible(blocked);
m_loadRemoteButton->setVisible(blocked);
});
}
void MessageView::toggleHtml()
{
const bool anyHtml = std::any_of(
m_items.cbegin(), m_items.cend(),
[](const ThreadRenderItem &item) { return item.message.hasHtml(); });
if (!anyHtml) {
emit statusMessage(tr("No message in this thread has an HTML part"));
return;
}
m_preferHtml = !m_preferHtml;
render();
}
void MessageView::loadRemoteContent()
{
// Applies to this thread only and is cleared by the next showThread().
m_interceptor->setAllowRemote(true);
m_blockedLabel->hide();
m_loadRemoteButton->hide();
render();
}
```
- [ ] **Step 3: Add to the library and build**
```cmake
add_library(qtmaildir_lib STATIC
keymap.cpp
config.cpp
mimeparser.cpp
requestinterceptor.cpp
htmlbuilder.cpp
cidschemehandler.cpp
notmuchworker.cpp
threadlistmodel.cpp
mailsync.cpp
messageview.cpp
)
```
Run: `cmake --build build && ctest --test-dir build --output-on-failure`
Expected: builds clean, seven test binaries still PASS.
- [ ] **Step 4: Commit**
```bash
git add src/messageview.h src/messageview.cpp src/CMakeLists.txt
git commit -S -m "feat: add MessageView with locked-down web engine profile"
```
---
## Task 12: MainWindow and application entry point
**Files:**
- Create: `src/mainwindow.h`, `src/mainwindow.cpp`
- Modify: `src/main.cpp`, `src/CMakeLists.txt`
- [ ] **Step 1: Write src/mainwindow.h**
```cpp
#pragma once
#include <QHash>
#include <QMainWindow>
#include <QThread>
#include <QUndoCommand>
#include <QUndoStack>
#include <functional>
#include "config.h"
#include "keymap.h"
#include "types.h"
class QLineEdit;
class QTableView;
class QLabel;
class QPushButton;
class QComboBox;
class QPlainTextEdit;
class ThreadListModel;
class MessageView;
class MailSync;
class NotmuchWorker;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(const Config &config, QWidget *parent = nullptr);
~MainWindow() override;
protected:
bool eventFilter(QObject *watched, QEvent *event) override;
private slots:
void runCurrentQuery();
void onThreadsReady(const QVector<ThreadSummary> &threads, quint64 generation);
void onQueryFinished(int total, quint64 generation);
void onThreadSelected(const QModelIndex ¤t, const QModelIndex &previous);
void onThreadLoaded(const QVector<MessageRef> &messages, quint64 generation);
void onWorkerError(const QString &message);
void onSyncFinished(bool success, int exitCode);
private:
void buildUi();
void registerActions();
void wireWorker();
void showWarnings();
void tagSelected(const QStringList &add, const QStringList &remove,
const QString &description);
/// Sends a tag change for a set of threads without touching the undo stack.
/// Both tagSelected() and ThreadTagCommand route through this.
void sendThreadTagChange(const QStringList &threadIds,
const QStringList &add,
const QStringList &remove,
const QString &description);
friend class ThreadTagCommand;
Config m_config;
KeyMap m_keyMap;
QThread m_workerThread;
NotmuchWorker *m_worker = nullptr;
ThreadListModel *m_model = nullptr;
MessageView *m_messageView = nullptr;
MailSync *m_sync = nullptr;
QUndoStack m_undoStack;
QLineEdit *m_queryEdit = nullptr;
QTableView *m_threadView = nullptr;
QComboBox *m_accountBox = nullptr;
QPushButton *m_syncButton = nullptr;
QLabel *m_statusLabel = nullptr;
QPlainTextEdit *m_syncLog = nullptr;
QHash<QString, std::function<void()>> m_actions;
quint64 m_generation = 0;
QString m_lastQuery;
QString m_currentThreadId;
QVector<MessageRef> m_currentMessages;
};
/// Undo entry for a tag change over a set of threads.
///
/// Stores thread ids rather than message ids, so undo re-resolves them on the
/// worker and stays correct even if the selection has moved on.
class ThreadTagCommand : public QUndoCommand
{
public:
ThreadTagCommand(MainWindow *window, const QStringList &threadIds,
const QStringList &add, const QStringList &remove,
const QString &description)
: QUndoCommand(description), m_window(window), m_threadIds(threadIds),
m_add(add), m_remove(remove), m_description(description) {}
/// The stack calls redo() when the command is pushed. The change has
/// already been sent by that point, so the first call is skipped.
void redo() override
{
if (m_firstRedo) {
m_firstRedo = false;
return;
}
m_window->sendThreadTagChange(m_threadIds, m_add, m_remove,
m_description);
}
void undo() override
{
// Inverted: what was added is removed and vice versa.
m_window->sendThreadTagChange(m_threadIds, m_remove, m_add,
QStringLiteral("Undo %1").arg(m_description));
}
private:
MainWindow *m_window;
QStringList m_threadIds;
QStringList m_add;
QStringList m_remove;
QString m_description;
bool m_firstRedo = true;
};
```
- [ ] **Step 2: Write src/mainwindow.cpp**
```cpp
#include "mainwindow.h"
#include <QComboBox>
#include <QEvent>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QKeyEvent>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QSettings>
#include <QSplitter>
#include <QStatusBar>
#include <QTableView>
#include <QVBoxLayout>
#include "mailsync.h"
#include "messageview.h"
#include "mimeparser.h"
#include "notmuchworker.h"
#include "threadlistmodel.h"
MainWindow::MainWindow(const Config &config, QWidget *parent)
: QMainWindow(parent), m_config(config)
{
qRegisterMetaType<ThreadSummary>();
qRegisterMetaType<MessageRef>();
qRegisterMetaType<TagChange>();
qRegisterMetaType<QVector<ThreadSummary>>();
qRegisterMetaType<QVector<MessageRef>>();
m_keyMap.loadDefaults();
{
QSettings settings(Config::defaultPath(), QSettings::IniFormat);
m_keyMap.loadOverrides(settings);
}
buildUi();
registerActions();
wireWorker();
showWarnings();
installEventFilter(this);
if (!m_config.savedQueries().isEmpty()) {
m_queryEdit->setText(m_config.savedQueries().first().query);
runCurrentQuery();
}
}
MainWindow::~MainWindow()
{
m_workerThread.quit();
m_workerThread.wait();
}
void MainWindow::buildUi()
{
auto *central = new QWidget(this);
auto *layout = new QVBoxLayout(central);
// Query row.
auto *queryRow = new QHBoxLayout;
m_accountBox = new QComboBox(central);
m_accountBox->addItem(tr("All accounts"), QString());
for (const Account &account : m_config.accounts())
m_accountBox->addItem(account.key, account.key);
m_queryEdit = new QLineEdit(central);
m_queryEdit->setPlaceholderText(tr("notmuch query, e.g. tag:inbox"));
connect(m_queryEdit, &QLineEdit::returnPressed,
this, &MainWindow::runCurrentQuery);
m_syncButton = new QPushButton(tr("Sync"), central);
m_sync = new MailSync(m_config.syncCommand(), this);
m_syncButton->setEnabled(m_sync->isAvailable());
if (!m_sync->isAvailable()) {
m_syncButton->setToolTip(
tr("No sync command configured ([sync] command in qtmaildir.conf)"));
}
connect(m_syncButton, &QPushButton::clicked, this, [this]() {
if (!m_sync->start())
m_statusLabel->setText(tr("Sync already running"));
});
connect(m_sync, &MailSync::finished, this, &MainWindow::onSyncFinished);
connect(m_sync, &MailSync::outputReceived, this, [this](const QString &chunk) {
m_syncLog->appendPlainText(chunk.trimmed());
});
queryRow->addWidget(m_accountBox);
queryRow->addWidget(m_queryEdit, 1);
queryRow->addWidget(m_syncButton);
layout->addLayout(queryRow);
// Saved query buttons.
auto *savedRow = new QHBoxLayout;
for (const SavedQuery &saved : m_config.savedQueries()) {
auto *button = new QPushButton(saved.name, central);
connect(button, &QPushButton::clicked, this, [this, saved]() {
m_queryEdit->setText(saved.query);
runCurrentQuery();
});
savedRow->addWidget(button);
}
savedRow->addStretch();
layout->addLayout(savedRow);
// Thread list and message pane.
m_model = new ThreadListModel(this);
m_threadView = new QTableView(central);
m_threadView->setModel(m_model);
m_threadView->setSelectionBehavior(QAbstractItemView::SelectRows);
m_threadView->setSelectionMode(QAbstractItemView::ExtendedSelection);
m_threadView->verticalHeader()->hide();
m_threadView->horizontalHeader()->setStretchLastSection(false);
m_threadView->horizontalHeader()->setSectionResizeMode(
ThreadListModel::SubjectColumn, QHeaderView::Stretch);
connect(m_threadView->selectionModel(),
&QItemSelectionModel::currentRowChanged,
this, &MainWindow::onThreadSelected);
m_messageView = new MessageView(central);
connect(m_messageView, &MessageView::statusMessage,
this, [this](const QString &text) { m_statusLabel->setText(text); });
auto *splitter = new QSplitter(Qt::Horizontal, central);
splitter->addWidget(m_threadView);
splitter->addWidget(m_messageView);
splitter->setStretchFactor(1, 2);
layout->addWidget(splitter, 1);
m_syncLog = new QPlainTextEdit(central);
m_syncLog->setReadOnly(true);
m_syncLog->setMaximumHeight(120);
m_syncLog->hide();
layout->addWidget(m_syncLog);
setCentralWidget(central);
m_statusLabel = new QLabel(this);
statusBar()->addWidget(m_statusLabel);
resize(1200, 800);
setWindowTitle(tr("qtmaildir"));
}
void MainWindow::registerActions()
{
m_actions[QStringLiteral("focus_query")] = [this]() {
m_queryEdit->setFocus();
m_queryEdit->selectAll();
};
m_actions[QStringLiteral("next_thread")] = [this]() {
const QModelIndex current = m_threadView->currentIndex();
const int row = current.isValid() ? current.row() + 1 : 0;
if (row < m_model->rowCount())
m_threadView->selectRow(row);
};
m_actions[QStringLiteral("prev_thread")] = [this]() {
const QModelIndex current = m_threadView->currentIndex();
if (current.isValid() && current.row() > 0)
m_threadView->selectRow(current.row() - 1);
};
m_actions[QStringLiteral("open_thread")] = [this]() {
m_threadView->setFocus();
};
m_actions[QStringLiteral("archive")] = [this]() {
tagSelected({}, { QStringLiteral("inbox") }, tr("Archive"));
};
m_actions[QStringLiteral("delete")] = [this]() {
tagSelected({ QStringLiteral("deleted") }, {}, tr("Delete"));
};
m_actions[QStringLiteral("spam")] = [this]() {
tagSelected({ QStringLiteral("spam") }, { QStringLiteral("inbox") },
tr("Mark spam"));
};
m_actions[QStringLiteral("flag")] = [this]() {
tagSelected({ QStringLiteral("flagged") }, {}, tr("Flag"));
};
m_actions[QStringLiteral("toggle_unread")] = [this]() {
const QModelIndex current = m_threadView->currentIndex();
if (!current.isValid())
return;
const ThreadSummary thread = m_model->threadAt(current.row());
if (thread.isUnread())
tagSelected({}, { QStringLiteral("unread") }, tr("Mark read"));
else
tagSelected({ QStringLiteral("unread") }, {}, tr("Mark unread"));
};
m_actions[QStringLiteral("toggle_html")] = [this]() {
m_messageView->toggleHtml();
};
m_actions[QStringLiteral("load_remote")] = [this]() {
m_messageView->loadRemoteContent();
};
m_actions[QStringLiteral("undo")] = [this]() {
if (m_undoStack.canUndo())
m_undoStack.undo();
else
m_statusLabel->setText(tr("Nothing to undo"));
};
m_actions[QStringLiteral("sync")] = [this]() {
if (m_sync->isAvailable())
m_sync->start();
};
m_actions[QStringLiteral("quit")] = [this]() { close(); };
}
void MainWindow::wireWorker()
{
m_worker = new NotmuchWorker(m_config.notmuchConfig());
m_worker->moveToThread(&m_workerThread);
connect(&m_workerThread, &QThread::finished, m_worker, &QObject::deleteLater);
connect(m_worker, &NotmuchWorker::threadsReady,
this, &MainWindow::onThreadsReady);
connect(m_worker, &NotmuchWorker::queryFinished,
this, &MainWindow::onQueryFinished);
connect(m_worker, &NotmuchWorker::threadLoaded,
this, &MainWindow::onThreadLoaded);
connect(m_worker, &NotmuchWorker::errorOccurred,
this, &MainWindow::onWorkerError);
m_workerThread.start();
}
void MainWindow::showWarnings()
{
QStringList warnings = m_config.warnings() + m_keyMap.warnings();
if (warnings.isEmpty())
return;
// Non-fatal: the app runs degraded rather than refusing to start.
m_statusLabel->setText(
tr("%1 configuration warning(s); see Help").arg(warnings.size()));
QMessageBox::warning(this, tr("Configuration warnings"),
warnings.join(QLatin1Char('\n')));
}
void MainWindow::runCurrentQuery()
{
QString query = m_queryEdit->text().trimmed();
const QString accountKey = m_accountBox->currentData().toString();
if (!accountKey.isEmpty())
query = m_config.account(accountKey).scopedQuery(query);
if (query.isEmpty())
return;
// Kept so loadThread() can work out which messages of a thread matched.
m_lastQuery = query;
++m_generation;
m_model->clear();
m_messageView->clear();
m_statusLabel->setText(tr("Searching..."));
QMetaObject::invokeMethod(m_worker, "runQuery", Qt::QueuedConnection,
Q_ARG(QString, query),
Q_ARG(quint64, m_generation));
}
void MainWindow::onThreadsReady(const QVector<ThreadSummary> &threads,
quint64 generation)
{
if (generation != m_generation)
return; // Superseded by a newer query.
m_model->appendBatch(threads);
}
void MainWindow::onQueryFinished(int total, quint64 generation)
{
if (generation != m_generation)
return;
m_statusLabel->setText(tr("%n thread(s)", "", total));
}
void MainWindow::onThreadSelected(const QModelIndex ¤t,
const QModelIndex &)
{
if (!current.isValid())
return;
m_currentThreadId = m_model->threadAt(current.row()).threadId;
QMetaObject::invokeMethod(m_worker, "loadThread", Qt::QueuedConnection,
Q_ARG(QString, m_currentThreadId),
Q_ARG(QString, m_lastQuery),
Q_ARG(quint64, m_generation));
}
void MainWindow::onThreadLoaded(const QVector<MessageRef> &messages,
quint64 generation)
{
if (generation != m_generation || messages.isEmpty())
return;
m_currentMessages = messages;
MimeParser parser;
QList<ThreadRenderItem> items;
items.reserve(messages.size());
for (int i = 0; i < messages.size(); ++i) {
const MessageRef &ref = messages.at(i);
ThreadRenderItem item;
item.message = parser.parse(ref.filePath);
if (!item.message.ok) {
// One unreadable message must not lose the rest of the thread, so
// it becomes an inline note rather than replacing the whole pane.
item.message = {};
item.message.ok = true;
item.message.from = tr("(unreadable message)");
item.message.subject = ref.filePath;
item.message.plainBody =
tr("This message could not be parsed.\n%1").arg(ref.filePath);
}
// Namespace prefix keeps cid: references distinct across the thread.
item.cidPrefix = QStringLiteral("m%1").arg(i);
// Matched messages open; the rest collapse to a stub. The last message
// always opens, so a thread never renders as nothing but stubs.
item.expanded = ref.matched || i == messages.size() - 1;
items.append(item);
}
m_messageView->showThread(items);
}
void MainWindow::onWorkerError(const QString &message)
{
m_statusLabel->setText(message);
}
void MainWindow::onSyncFinished(bool success, int exitCode)
{
if (success) {
m_statusLabel->setText(tr("Sync complete"));
runCurrentQuery();
} else {
m_statusLabel->setText(tr("Sync failed (exit %1)").arg(exitCode));
m_syncLog->show();
}
}
void MainWindow::tagSelected(const QStringList &add, const QStringList &remove,
const QString &description)
{
const QModelIndexList rows =
m_threadView->selectionModel()->selectedRows();
if (rows.isEmpty())
return;
QStringList threadIds;
threadIds.reserve(rows.size());
for (const QModelIndex &index : rows)
threadIds.append(m_model->threadAt(index.row()).threadId);
sendThreadTagChange(threadIds, add, remove, description);
// Pushed for undo. The inverse re-resolves the same threads, so it works
// whether or not those rows are still selected.
m_undoStack.push(new ThreadTagCommand(this, threadIds, add, remove,
description));
m_statusLabel->setText(
tr("%1: %n thread(s)", "", threadIds.size()).arg(description));
}
void MainWindow::sendThreadTagChange(const QStringList &threadIds,
const QStringList &add,
const QStringList &remove,
const QString &description)
{
// Optimistic: the rows change now, so a bulk archive of hundreds of threads
// feels instant. onWorkerError() reverts if the write fails.
for (const QString &threadId : threadIds)
m_model->applyTagChange(threadId, add, remove);
// The worker resolves thread ids to message ids: the UI does not hold
// message ids for rows it never opened.
QMetaObject::invokeMethod(m_worker, "applyTagsToThreads",
Qt::QueuedConnection,
Q_ARG(QStringList, threadIds),
Q_ARG(QStringList, add),
Q_ARG(QStringList, remove),
Q_ARG(QString, description));
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() != QEvent::KeyPress)
return QMainWindow::eventFilter(watched, event);
// The query bar must receive ordinary typing, so single-key bindings are
// suppressed while it has focus.
if (m_queryEdit->hasFocus())
return QMainWindow::eventFilter(watched, event);
auto *keyEvent = static_cast<QKeyEvent *>(event);
const QKeySequence sequence(keyEvent->keyCombination());
const QString action = m_keyMap.actionFor(sequence);
if (action.isEmpty() || !m_actions.contains(action))
return QMainWindow::eventFilter(watched, event);
m_actions.value(action)();
return true;
}
```
- [ ] **Step 3: Rewrite src/main.cpp**
```cpp
#include <QApplication>
#include <QMessageBox>
#include <QWebEngineUrlScheme>
#include <notmuch.h>
#include "config.h"
#include "mainwindow.h"
int main(int argc, char *argv[])
{
// Custom schemes must be registered before QApplication is constructed.
{
QWebEngineUrlScheme scheme(QByteArrayLiteral("cid"));
scheme.setFlags(QWebEngineUrlScheme::SecureScheme
| QWebEngineUrlScheme::ContentSecurityPolicyIgnored);
QWebEngineUrlScheme::registerScheme(scheme);
}
{
QWebEngineUrlScheme scheme(QByteArrayLiteral("qtmaildir"));
scheme.setFlags(QWebEngineUrlScheme::SecureScheme);
QWebEngineUrlScheme::registerScheme(scheme);
}
QApplication app(argc, argv);
app.setApplicationName(QStringLiteral("qtmaildir"));
app.setOrganizationName(QStringLiteral("qtmaildir"));
// Fail loudly on an ABI mismatch rather than crashing later.
if (LIBNOTMUCH_MAJOR_VERSION < 5) {
QMessageBox::critical(nullptr, QObject::tr("qtmaildir"),
QObject::tr("libnotmuch 5 or newer is required."));
return 1;
}
Config config;
config.load(Config::defaultPath());
MainWindow window(config);
window.show();
return app.exec();
}
```
- [ ] **Step 4: Update src/CMakeLists.txt and build**
```cmake
add_library(qtmaildir_lib STATIC
keymap.cpp
config.cpp
mimeparser.cpp
requestinterceptor.cpp
htmlbuilder.cpp
cidschemehandler.cpp
notmuchworker.cpp
threadlistmodel.cpp
mailsync.cpp
messageview.cpp
mainwindow.cpp
)
```
Run: `cmake --build build && ctest --test-dir build --output-on-failure`
Expected: builds clean, seven test binaries PASS.
- [ ] **Step 5: Commit**
```bash
git add src/mainwindow.h src/mainwindow.cpp src/main.cpp src/CMakeLists.txt
git commit -S -m "feat: add MainWindow wiring query, list, message, and sync"
```
---
## Task 13: Manual verification against a real database
The notmuch layer has no automated test by design. This task is the compensating
control. Do not skip it.
**Files:**
- Create: `docs/manual-verification.md`
- [ ] **Step 1: Write a starter config**
```bash
mkdir -p ~/.config/qtmaildir
```
Create `~/.config/qtmaildir/qtmaildir.conf` with the real account keys and
maildir subdirectories from the user's `~/Mail`. Confirm each maildir exists:
```bash
ls ~/Mail
```
- [ ] **Step 2: Run the application**
```bash
./build/src/qtmaildir
```
- [ ] **Step 3: Walk the checklist, recording results**
Create `docs/manual-verification.md` and record pass/fail for each:
1. Startup shows no configuration warnings with a valid config.
2. `tag:inbox` returns threads; the count in the status bar matches
`notmuch count --output=threads tag:inbox`.
3. A large query (`*`) paints the first rows within a second and keeps filling.
4. Typing a new query while one is running discards the old results.
5. A malformed query (`tag:`) reports an error and does not crash.
6. Selecting a thread renders every message in it, oldest first.
7. In a thread where only some messages matched the query, the rest appear as
one-line stubs.
8. A thread with many messages (find one with `notmuch search --output=threads`
sorted by message count) renders without a noticeable stall, and
`pgrep -c QtWebEngineProcess` does not grow with the message count.
9. An HTML newsletter renders with layout, and shows "Remote content blocked".
10. Clicking "Load remote content" re-renders with images.
11. Selecting a different thread clears the remote grant (banner returns).
12. A message with an inline image displays it without any remote load.
13. A thread containing two messages that use the same Content-ID shows each
message its own image, not the same one twice. (Two newsletters from the
same sender is the usual way to hit this.)
14. `h` toggles the whole thread to plain text and back.
15. Clicking a link in a message opens the system browser, and the pane does
not navigate.
16. `a` archives the selected thread; `notmuch search` confirms `inbox` is gone.
17. Selecting several threads and pressing `a` archives all of them; confirm the
count with `notmuch count`.
18. `u` after a bulk archive restores every thread it touched.
19. Sync runs, the log fills, and the query refreshes on completion.
20. Sync while `notmuch new` runs from cron reports a lock error rather than
corrupting anything.
- [ ] **Step 4: Commit the results**
```bash
git add docs/manual-verification.md
git commit -S -m "docs: record manual verification results"
```
---
## Task 14: README and license
**Files:**
- Create: `README.md`
- Create: `LICENSE`
- [ ] **Step 1: Fetch the GPLv2 text**
```bash
curl -o LICENSE https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
```
Confirm the user wants GPLv2-only (the default) before committing.
- [ ] **Step 2: Add per-file license headers**
Prepend to every file in `src/`:
```cpp
/*
* qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs
* Copyright (C) 2026 Danilo M. <danix@danix.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
```
- [ ] **Step 3: Write README.md**
Cover: what it is, what it deliberately does not do (no POP/IMAP/SMTP, no
send in v1), requirements with the verified versions, build instructions, the
full example config, the default keybindings table, and the security posture of
the message view.
End the file with the standard Development Approach section:
```markdown
## Development Approach
This project is developed using AI-assisted tools. Code is generated with the help of AI based on human-provided specifications, design decisions, and iterative feedback.
All contributions are reviewed, tested, and curated by the maintainer before being included in the codebase. AI is used as a productivity and exploration tool, while human oversight remains central to all decisions.
The goal is to combine the flexibility of AI-assisted development with standard open-source practices such as transparency, review, and accountability.
```
- [ ] **Step 4: Commit**
```bash
git add README.md LICENSE src/
git commit -S -m "docs: add README and GPLv2 license"
```
---
## Self-review notes
Checked against the spec on 2026-08-02:
- Spec §5 file table maps to Tasks 2-12; every file listed there has a task.
- Spec §8 web view security maps to Task 5 (policy, tested) and Task 11
(profile settings, navigation interception).
- Spec §9 mutations maps to Task 8 (`applyTags`) and Task 12 (undo stack,
optimistic update).
- Spec §13 testing maps to Tasks 2, 3, 4, 5, 6, 9, 10. The spec named three
test targets; this plan has seven, because config, htmlbuilder, model, and
sync each earned one.
- Spec §14 known gaps are preserved: `NotmuchWorker` has no unit test, and
Task 13 is the compensating manual check.
No deviations from the spec remain. Two narrowings were present in the first
draft and were folded back in at the user's direction on 2026-08-02:
1. **Full thread rendering** (spec §7). The whole thread renders, oldest first,
matched messages expanded and unmatched collapsed to stubs. This was called
out as fundamental for newsletter threads.
Folding it in surfaced two design consequences worth recording:
- The thread renders as **one document in one web view**, not one view per
message. A newsletter thread can hold dozens of messages, and a
`QWebEngineView` each would spawn a Chromium render process each.
- Because messages share a document, their `cid:` references **collide**:
two newsletters both using `cid:logo@example.org` would resolve to
whichever part won. Every reference is therefore rewritten to
`cid:<prefix>!<id>` with a per-message prefix. This is covered by
`threadNamespacesCidUrls` in Task 6.
Determining which messages matched required a real change to the worker:
`loadThread` now takes the user's query and intersects it with the thread,
and `MessageRef` carries a `matched` flag.
2. **Batch tagging** (spec §9). Archive, flag, delete, and spam apply to every
selected thread. The UI holds thread ids, not message ids, for rows it never
opened, so `NotmuchWorker::applyTagsToThreads` resolves them in a single
combined query rather than one query per thread.
Undo follows: `ThreadTagCommand` stores thread ids and re-resolves on undo,
so it stays correct even after the selection moves.
|