summaryrefslogtreecommitdiff
path: root/libempathy-gtk/empathy-chat.c
blob: 100223645685e1189dc322d1806996636d9f1d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2002-2007 Imendio AB
 * Copyright (C) 2007-2010 Collabora Ltd.
 * Copyright (C) 2012 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 *
 * Authors: Mikael Hallendal <micke@imendio.com>
 *          Richard Hult <richard@imendio.com>
 *          Martyn Russell <martyn@imendio.com>
 *          Geert-Jan Van den Bogaerde <geertjan@gnome.org>
 *          Xavier Claessens <xclaesse@gmail.com>
 */

/* for GCompletion */
#define GLIB_DISABLE_DEPRECATION_WARNINGS 1

#include "config.h"
#include "empathy-chat.h"

#include <glib/gi18n-lib.h>
#include <tp-account-widgets/tpaw-keyring.h>
#include <tp-account-widgets/tpaw-builder.h>
#include <tp-account-widgets/tpaw-utils.h>
#include <telepathy-glib/telepathy-glib-dbus.h>

#include "empathy-client-factory.h"
#include "empathy-gsettings.h"
#include "empathy-individual-information-dialog.h"
#include "empathy-individual-store-channel.h"
#include "empathy-individual-view.h"
#include "empathy-input-text-view.h"
#include "empathy-request-util.h"
#include "empathy-search-bar.h"
#include "empathy-spell.h"
#include "empathy-string-parser.h"
#include "empathy-theme-manager.h"
#include "empathy-ui-utils.h"
#include "empathy-utils.h"

#define DEBUG_FLAG EMPATHY_DEBUG_CHAT
#include "empathy-debug.h"

#define IS_ENTER(v) (v == GDK_KEY_Return || v == GDK_KEY_ISO_Enter || v == GDK_KEY_KP_Enter)
#define COMPOSING_STOP_TIMEOUT 5

#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyChat)
struct _EmpathyChatPriv {
	EmpathyTpChat     *tp_chat;
	TpAccount         *account;
	gchar             *id;
	gchar             *name;
	gchar             *subject;
	EmpathyContact    *self_contact;
	EmpathyContact    *remote_contact;
	gboolean           show_contacts;

	GSettings         *gsettings_chat;
	GSettings         *gsettings_ui;

	TplLogManager     *log_manager;
	TplLogWalker      *log_walker;
	/* Are we watching for scrolling movements? */
	gboolean           watch_scroll;
	/* Maximum page size of the chat->view. */
	guint              max_page_size;
	/* The offset from the lower edge of the chat->view before it
	 * expanded to fit in the newly fetched logs. This is to
	 * restore the chat->view to the page it was on before the
	 * latest batch of logs were inserted. */
	guint              scroll_offset;

	TpAccountManager  *account_manager;
	GList             *input_history;
	GList             *input_history_current;
	GList             *compositors;
	GCompletion       *completion;
	guint              composing_stop_timeout_id;
	guint              block_events_timeout_id;
	TpEntityType       handle_type;
	gint               contacts_width;
	gboolean           has_input_vscroll;

	/* TRUE if spell checking is enabled, FALSE otherwise.
	 * This is to keep track of the last state of spell checking
	 * when it changes. */
	gboolean	   spell_checking_enabled;

	/* These store the signal handler ids for the enclosed text entry. */
	gulong		   insert_text_id;
	gulong		   delete_range_id;
	gulong		   notify_cursor_position_id;

	/* Source func ID for update_misspelled_words () */
	guint              update_misspelled_words_id;
	/* Source func ID for save_paned_pos_timeout () */
	guint              save_paned_pos_id;
	/* Source func ID for chat_contacts_visible_timeout_cb () */
	guint              contacts_visible_id;

	GtkWidget         *widget;
	GtkWidget         *hpaned;
	GtkWidget         *vbox_left;
	GtkWidget         *scrolled_window_chat;
	GtkWidget         *scrolled_window_input;
	GtkWidget         *scrolled_window_contacts;
	GtkWidget         *hbox_topic;
	GtkWidget         *expander_topic;
	GtkWidget         *label_topic;
	GtkWidget         *contact_list_view;
	GtkWidget         *info_bar_vbox;
	GtkWidget         *search_bar;

	guint              unread_messages;
	guint              unread_messages_when_offline;
	/* TRUE if the pending messages can be displayed. This is to avoid to show
	 * pending messages *before* messages from logs. (#603980) */
	gboolean           can_show_pending;

	/* FIXME: retrieving_backlogs flag is a workaround for Bug#610994 and should
	 * be differently handled since it introduces another race condition, which
	 * is really hard to occur, but still possible.
	 *
	 * With the current workaround (which has the race above), we need to be
	 * sure to ACK any pending messages only when the retrieval of backlogs is
	 * finished, that's why using retrieving_backlogs flag.
	 * empathy_chat_messages_read () will check this variable and not ACK
	 * anything when TRUE. It will be set TRUE at chat_constructed () and set
	 * back to FALSE when the backlog has been retrieved and the pending
	 * messages actually showed to the user.
	 *
	 * Race condition introduced with this workaround:
	 * Scenario: a message is pending, the user is notified and selects the tab.
	 * the tab with a pending message is focused before the messages are properly
	 * shown (since the preparation of the window is slower AND async WRT the
	 * tab showing), which means the user won't see any new messages (rare but
	 * possible), if he/she will change tab focus before the messages are
	 * properly shown, the tab will be set as 'seen' and the user won't be
	 * notified again about the already notified pending messages when the
	 * messages in tab will be properly shown */
	gboolean           retrieving_backlogs;
	gboolean           sms_channel;

	/* we need to know whether populate-popup happened in response to
	 * the keyboard or the mouse. We can't ask GTK for the most recent
	 * event, because it will be a notify event. Instead we track it here */
	GdkEventType       most_recent_event_type;

	/* A regex matching our own current nickname in the room, or %NULL if
	 * !empathy_chat_is_room (). */
	GRegex            *highlight_regex;

	/* TRUE if empathy_chat_is_room () and there are unread highlighted messages.
	 * Cleared by empathy_chat_messages_read (). */
	gboolean           highlighted;
};

typedef struct {
	gchar *text; /* Original message that was specified
	              * upon entry creation. */
	gchar *modified_text; /* Message that was modified by user.
	                       * When no modifications were made, it is NULL */
} InputHistoryEntry;

enum {
	COMPOSING,
	NEW_MESSAGE,
	PART_COMMAND_ENTERED,
	LAST_SIGNAL
};

enum {
	PROP_0,
	PROP_TP_CHAT,
	PROP_ACCOUNT,
	PROP_ID,
	PROP_NAME,
	PROP_SUBJECT,
	PROP_REMOTE_CONTACT,
	PROP_SHOW_CONTACTS,
	PROP_SMS_CHANNEL,
	PROP_N_MESSAGES_SENDING,
	PROP_NB_UNREAD_MESSAGES,
};

static guint signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE (EmpathyChat, empathy_chat, GTK_TYPE_BOX);

static gboolean chat_scrollable_connect (gpointer user_data);
static gboolean update_misspelled_words (gpointer data);

static void
chat_get_property (GObject    *object,
		   guint       param_id,
		   GValue     *value,
		   GParamSpec *pspec)
{
	EmpathyChat *chat = EMPATHY_CHAT (object);
	EmpathyChatPriv *priv = GET_PRIV (object);

	switch (param_id) {
	case PROP_TP_CHAT:
		g_value_set_object (value, priv->tp_chat);
		break;
	case PROP_ACCOUNT:
		g_value_set_object (value, priv->account);
		break;
	case PROP_NAME:
		g_value_take_string (value, empathy_chat_dup_name (chat));
		break;
	case PROP_ID:
		g_value_set_string (value, priv->id);
		break;
	case PROP_SUBJECT:
		g_value_set_string (value, priv->subject);
		break;
	case PROP_REMOTE_CONTACT:
		g_value_set_object (value, priv->remote_contact);
		break;
	case PROP_SHOW_CONTACTS:
		g_value_set_boolean (value, priv->show_contacts);
		break;
	case PROP_SMS_CHANNEL:
		g_value_set_boolean (value, priv->sms_channel);
		break;
	case PROP_N_MESSAGES_SENDING:
		g_value_set_uint (value,
			empathy_chat_get_n_messages_sending (chat));
		break;
	case PROP_NB_UNREAD_MESSAGES:
		g_value_set_uint (value,
			empathy_chat_get_nb_unread_messages (chat));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

static void
chat_set_property (GObject      *object,
		   guint         param_id,
		   const GValue *value,
		   GParamSpec   *pspec)
{
	EmpathyChat *chat = EMPATHY_CHAT (object);

	switch (param_id) {
	case PROP_TP_CHAT:
		empathy_chat_set_tp_chat (chat, EMPATHY_TP_CHAT (g_value_get_object (value)));
		break;
	case PROP_SHOW_CONTACTS:
		empathy_chat_set_show_contacts (chat, g_value_get_boolean (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	};
}

static void
account_reconnected (EmpathyChat *chat,
			TpAccount *account)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	DEBUG ("Account reconnected, request a new Text channel");

	/* FIXME: Ideally we should ask to handle ourself the channel so we can
	* report the error if any but this is blocked by
	* https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
	switch (priv->handle_type) {
		case TP_ENTITY_TYPE_CONTACT:
			if (priv->sms_channel)
				empathy_sms_contact_id (
					account, priv->id,
					TP_USER_ACTION_TIME_NOT_USER_ACTION,
					NULL, NULL);
			else
				empathy_chat_with_contact_id (
					account, priv->id,
					TP_USER_ACTION_TIME_NOT_USER_ACTION,
					NULL, NULL);
			break;
		case TP_ENTITY_TYPE_ROOM:
			empathy_join_muc (account, priv->id,
				TP_USER_ACTION_TIME_NOT_USER_ACTION);
			break;
		case TP_ENTITY_TYPE_NONE:
		default:
			g_assert_not_reached ();
			break;
	}

	g_object_unref (chat);
}

static void
chat_new_connection_cb (TpAccount   *account,
			guint        old_status,
			guint        new_status,
			guint        reason,
			gchar       *dbus_error_name,
			GHashTable  *details,
			EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	if (new_status != TP_CONNECTION_STATUS_CONNECTED)
		return;

	if (priv->tp_chat != NULL || account != priv->account ||
	    priv->handle_type == TP_ENTITY_TYPE_NONE ||
	    TPAW_STR_EMPTY (priv->id))
		return;

	g_object_ref (chat);

	account_reconnected (chat, account);
}

static void
chat_composing_remove_timeout (EmpathyChat *chat)
{
	EmpathyChatPriv *priv;

	priv = GET_PRIV (chat);

	if (priv->composing_stop_timeout_id) {
		g_source_remove (priv->composing_stop_timeout_id);
		priv->composing_stop_timeout_id = 0;
	}
}

static void
set_chate_state_cb (GObject *source,
		    GAsyncResult *result,
		    gpointer user_data)
{
	GError *error = NULL;

	if (!tp_text_channel_set_chat_state_finish (TP_TEXT_CHANNEL (source), result,
		&error)) {
		DEBUG ("Failed to set chat state: %s", error->message);
		g_error_free (error);
	}
}

static void
set_chat_state (EmpathyChat *self,
		TpChannelChatState state)
{
	EmpathyChatPriv *priv = GET_PRIV (self);

	if (!tp_proxy_has_interface_by_id (priv->tp_chat,
		TP_IFACE_QUARK_CHANNEL_INTERFACE_CHAT_STATE1))
		return;

	tp_text_channel_set_chat_state_async (TP_TEXT_CHANNEL (priv->tp_chat), state,
		set_chate_state_cb, self);
}

static gboolean
chat_composing_stop_timeout_cb (EmpathyChat *chat)
{
	EmpathyChatPriv *priv;
	gboolean send_chat_states;

	priv = GET_PRIV (chat);

	priv->composing_stop_timeout_id = 0;
	send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
	                                 EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
	if (!send_chat_states) {
		set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
	} else {
		set_chat_state (chat, TP_CHANNEL_CHAT_STATE_PAUSED);
	}

	return FALSE;
}

static void
chat_composing_start (EmpathyChat *chat)
{
	EmpathyChatPriv *priv;
	gboolean send_chat_states;

	priv = GET_PRIV (chat);

	send_chat_states = g_settings_get_boolean (priv->gsettings_chat,
	                                  EMPATHY_PREFS_CHAT_SEND_CHAT_STATES);
	if (!send_chat_states) {
		return;
	}

	if (priv->composing_stop_timeout_id) {
		/* Just restart the timeout */
		chat_composing_remove_timeout (chat);
	} else {
		set_chat_state (chat, TP_CHANNEL_CHAT_STATE_COMPOSING);
	}

	priv->composing_stop_timeout_id = g_timeout_add_seconds (
		COMPOSING_STOP_TIMEOUT,
		(GSourceFunc) chat_composing_stop_timeout_cb,
		chat);
}

static void
chat_composing_stop (EmpathyChat *chat)
{
	chat_composing_remove_timeout (chat);
	set_chat_state (chat, TP_CHANNEL_CHAT_STATE_ACTIVE);
}

static gint
chat_input_history_entry_cmp (InputHistoryEntry *entry,
                              const gchar *text)
{
	if (!tp_strdiff (entry->text, text)) {
		if (entry->modified_text != NULL) {
			/* Modified entry and single string cannot be equal. */
			return 1;
		}
		return 0;
	}
	return 1;
}

static InputHistoryEntry *
chat_input_history_entry_new_with_text (const gchar *text)
{
	InputHistoryEntry *entry;
	entry = g_slice_new0 (InputHistoryEntry);
	entry->text = g_strdup (text);

	return entry;
}

static void
chat_input_history_entry_free (InputHistoryEntry *entry)
{
	g_free (entry->text);
	g_free (entry->modified_text);
	g_slice_free (InputHistoryEntry, entry);
}

static void
chat_input_history_entry_revert (InputHistoryEntry *entry)
{
	g_free (entry->modified_text);
	entry->modified_text = NULL;
}

static void
chat_input_history_entry_update_text (InputHistoryEntry *entry,
                                      const gchar *text)
{
	gchar *old;

	if (!tp_strdiff (text, entry->text)) {
		g_free (entry->modified_text);
		entry->modified_text = NULL;
		return;
	}

	old = entry->modified_text;
	entry->modified_text = g_strdup (text);
	g_free (old);
}

static const gchar *
chat_input_history_entry_get_text (InputHistoryEntry *entry)
{
	if (entry == NULL) {
		return NULL;
	}

	if (entry->modified_text != NULL) {
		return entry->modified_text;
	}
	return entry->text;
}

static GList *
chat_input_history_remove_item (GList *list,
                                GList *item)
{
	list = g_list_remove_link (list, item);
	chat_input_history_entry_free (item->data);
	g_list_free_1 (item);
	return list;
}

static void
chat_input_history_revert (EmpathyChat *chat)
{
	EmpathyChatPriv   *priv;
	GList             *list;
	GList             *item1;
	GList             *item2;
	InputHistoryEntry *entry;

	priv = GET_PRIV (chat);
	list = priv->input_history;

	if (list == NULL) {
		DEBUG ("No input history");
		return;
	}

	/* Delete temporary entry */
	if (priv->input_history_current != NULL) {
		item1 = list;
		list = chat_input_history_remove_item (list, item1);
		if (priv->input_history_current == item1) {
			/* Removed temporary entry was current entry */
			priv->input_history = list;
			priv->input_history_current = NULL;
			return;
		}
	}
	else {
		/* There is no entry to revert */
		return;
	}

	/* Restore the current history entry to original value */
	item1 = priv->input_history_current;
	entry = item1->data;
	chat_input_history_entry_revert (entry);

	/* Remove restored entry if there is other occurance before this entry */
	item2 = g_list_find_custom (list, chat_input_history_entry_get_text (entry),
	                            (GCompareFunc) chat_input_history_entry_cmp);
	if (item2 != item1) {
		list = chat_input_history_remove_item (list, item1);
	}
	else {
		/* Remove other occurance of the restored entry */
		item2 = g_list_find_custom (item1->next,
		                            chat_input_history_entry_get_text (entry),
		                            (GCompareFunc) chat_input_history_entry_cmp);
		if (item2 != NULL) {
			list = chat_input_history_remove_item (list, item2);
		}
	}

	priv->input_history_current = NULL;
	priv->input_history = list;
}

static void
chat_input_history_add (EmpathyChat  *chat,
                        const gchar *str,
                        gboolean temporary)
{
	EmpathyChatPriv   *priv;
	GList             *list;
	GList             *item;
	InputHistoryEntry *entry;

	priv = GET_PRIV (chat);

	list = priv->input_history;

	/* Remove any other occurances of this entry, if not temporary */
	if (!temporary) {
		while ((item = g_list_find_custom (list, str,
		    (GCompareFunc) chat_input_history_entry_cmp)) != NULL) {
			list = chat_input_history_remove_item (list, item);
		}

		/* Trim the list to the last 10 items */
		while (g_list_length (list) > 10) {
			item = g_list_last (list);
			if (item != NULL) {
				list = chat_input_history_remove_item (list, item);
			}
		}
	}



	/* Add new entry */
	entry = chat_input_history_entry_new_with_text (str);
	list = g_list_prepend (list, entry);

	/* Set the list and the current item pointer */
	priv->input_history = list;
	if (temporary) {
		priv->input_history_current = list;
	}
	else {
		priv->input_history_current = NULL;
	}
}

static const gchar *
chat_input_history_get_next (EmpathyChat *chat)
{
	EmpathyChatPriv *priv;
	GList           *item;
	const gchar     *msg;

	priv = GET_PRIV (chat);

	if (priv->input_history == NULL) {
		DEBUG ("No input history, next entry is NULL");
		return NULL;
	}
	g_assert (priv->input_history_current != NULL);

	if ((item = g_list_next (priv->input_history_current)) == NULL)
	{
		item = priv->input_history_current;
	}

	msg = chat_input_history_entry_get_text (item->data);

	DEBUG ("Returning next entry: '%s'", msg);

	priv->input_history_current = item;

	return msg;
}

static const gchar *
chat_input_history_get_prev (EmpathyChat *chat)
{
	EmpathyChatPriv *priv;
	GList           *item;
	const gchar     *msg;

	g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);

	priv = GET_PRIV (chat);

	if (priv->input_history == NULL) {
		DEBUG ("No input history, previous entry is NULL");
		return NULL;
	}

	if (priv->input_history_current == NULL)
	{
		return NULL;
	}
	else if ((item = g_list_previous (priv->input_history_current)) == NULL)
	{
		item = priv->input_history_current;
	}

	msg = chat_input_history_entry_get_text (item->data);

	DEBUG ("Returning previous entry: '%s'", msg);

	priv->input_history_current = item;

	return msg;
}

static void
chat_input_history_update (EmpathyChat *chat,
                           GtkTextBuffer *buffer)
{
	EmpathyChatPriv      *priv;
	GtkTextIter           start, end;
	gchar                *text;
	InputHistoryEntry    *entry;

	priv = GET_PRIV (chat);

	gtk_text_buffer_get_bounds (buffer, &start, &end);
	text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);

	if (priv->input_history_current == NULL) {
		/* Add the current text temporarily to the history */
		chat_input_history_add (chat, text, TRUE);
		g_free (text);
		return;
	}

	/* Save the changes in the history */
	entry = priv->input_history_current->data;
	if (tp_strdiff (chat_input_history_entry_get_text (entry), text)) {
		chat_input_history_entry_update_text (entry, text);
	}

	g_free (text);
}

typedef struct {
	EmpathyChat *chat;
	gchar *message;
} ChatCommandMsgData;

static void
chat_command_msg_cb (GObject *source,
			      GAsyncResult *result,
			      gpointer                  user_data)
{
	ChatCommandMsgData *data = user_data;
	GError *error = NULL;
	TpChannel *channel;

	channel = tp_account_channel_request_ensure_channel_finish (
					TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error);

	if (channel == NULL) {
		DEBUG ("Failed to get channel: %s", error->message);
		g_error_free (error);

		empathy_theme_adium_append_event (data->chat->view,
			_("Failed to open private chat"));
		goto OUT;
	}

	if (!TPAW_STR_EMPTY (data->message) && TP_IS_TEXT_CHANNEL (channel)) {
		TpTextChannel *text = (TpTextChannel *) channel;
		TpMessage *msg;

		msg = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
			data->message);

		tp_text_channel_send_message_async (text, msg, 0, NULL, NULL);

		g_object_unref (msg);
	}

	g_object_unref (channel);

OUT:
	g_free (data->message);
	g_slice_free (ChatCommandMsgData, data);
}

static gboolean
nick_command_supported (EmpathyChat *chat)
{
	EmpathyChatPriv * priv = GET_PRIV (chat);
	TpConnection    *connection;

	connection = tp_channel_get_connection (TP_CHANNEL (priv->tp_chat));
	return tp_proxy_has_interface_by_id (connection,
		TP_IFACE_QUARK_CONNECTION_INTERFACE_RENAMING1);
}

static gboolean
part_command_supported (EmpathyChat *chat)
{
	EmpathyChatPriv * priv = GET_PRIV (chat);

	return tp_proxy_has_interface_by_id (priv->tp_chat,
			TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP1);
}

static void
chat_command_clear (EmpathyChat *chat,
		    GStrv        strv)
{
	empathy_theme_adium_clear (chat->view);
}

static void
chat_command_topic (EmpathyChat *chat,
		    GStrv        strv)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	if (!empathy_tp_chat_supports_subject (priv->tp_chat)) {
		empathy_theme_adium_append_event (chat->view,
			_("Topic not supported on this conversation"));
		return;
	}

	if (!empathy_tp_chat_can_set_subject (priv->tp_chat)) {
		empathy_theme_adium_append_event (chat->view,
			_("You are not allowed to change the topic"));
		return;
	}

	empathy_tp_chat_set_subject (priv->tp_chat, strv[1]);
}

void
empathy_chat_join_muc (EmpathyChat *chat,
		   const gchar   *room)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	empathy_join_muc (priv->account, room,
		empathy_get_current_action_time ());
}

static void
chat_command_join (EmpathyChat *chat,
		   GStrv        strv)
{
	guint i = 0;

	GStrv rooms = g_strsplit_set (strv[1], ", ", -1);

	/* FIXME: Ideally we should ask to handle ourself the channel so we can
	* report the error if any but this is blocked by
	* https://bugs.freedesktop.org/show_bug.cgi?id=13422 */
	while (rooms[i] != NULL) {
		/* ignore empty strings */
		if (!TPAW_STR_EMPTY (rooms[i])) {
			empathy_chat_join_muc (chat, rooms[i]);
		}
		i++;
	}
	g_strfreev (rooms);
}

static void
chat_command_part (EmpathyChat *chat,
			   GStrv        strv)
{
	g_signal_emit (chat, signals[PART_COMMAND_ENTERED], 0, strv);
}

static void
chat_command_msg_internal (EmpathyChat *chat,
			   const gchar *contact_id,
			   const gchar *message)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	ChatCommandMsgData *data;
	TpAccountChannelRequest *req;

	req = tp_account_channel_request_new_text (priv->account,
		empathy_get_current_action_time ());

	tp_account_channel_request_set_target_id (req, TP_ENTITY_TYPE_CONTACT,
		contact_id);

	/* FIXME: We should probably search in members alias. But this
	 * is enough for IRC */
	data = g_slice_new (ChatCommandMsgData);
	data->chat = chat;
	data->message = g_strdup (message);

	tp_account_channel_request_ensure_channel_async (req,
		EMPATHY_CHAT_TP_BUS_NAME, NULL, chat_command_msg_cb, data);

	g_object_unref (req);
}

static void
chat_command_query (EmpathyChat *chat,
		    GStrv        strv)
{
	/* If <message> part is not defined,
	 * strv[2] will be the terminal NULL */
	chat_command_msg_internal (chat, strv[1], strv[2]);
}

static void
chat_command_msg (EmpathyChat *chat,
		  GStrv        strv)
{
	chat_command_msg_internal (chat, strv[1], strv[2]);
}

static void
callback_for_request_rename (TpConnection *conn,
		  const GError *error,
		  gpointer user_data,
		  GObject *weak_object)
{
	if (error != NULL) {
		DEBUG ("Call to RequestRename method failed: %s",error->message);
	}
}

static void
chat_command_nick (EmpathyChat *chat,
		   GStrv        strv)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	TpConnection *conn;

	conn = tp_account_get_connection (priv->account);

	tp_cli_connection_interface_renaming1_call_request_rename (conn, -1,
		strv[1], callback_for_request_rename, NULL, NULL, NULL);
}

static void
chat_command_me (EmpathyChat *chat,
		  GStrv        strv)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	TpMessage *message;
	TpTextChannel *channel;

	channel = (TpTextChannel *) (priv->tp_chat);

	if (!tp_text_channel_supports_message_type (channel,
			TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION)) {
		/* Action message are not supported, 'simulate' the action */
		gchar *tmp;

		/* The TpChat can't be ready if it doesn't have the self contact */
		g_assert (priv->self_contact != NULL);

		tmp = g_strdup_printf ("%s %s", empathy_contact_get_alias (priv->self_contact),
			strv[1]);
		message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
			tmp);
		g_free (tmp);
	}
	else {
		message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION,
			strv[1]);
	}

	empathy_tp_chat_send (priv->tp_chat, message);
	g_object_unref (message);
}

static void
chat_command_say (EmpathyChat *chat,
		  GStrv        strv)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	TpMessage *message;

	message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
		strv[1]);
	empathy_tp_chat_send (priv->tp_chat, message);
	g_object_unref (message);
}

static void
whois_got_contact_cb (GObject *source,
		      GAsyncResult *result,
		      gpointer user_data)
{
	EmpathyChat *chat = user_data;
	EmpathyContact *contact;
	FolksIndividual *individual;

	contact = empathy_client_factory_dup_contact_by_id_finish (
		EMPATHY_CLIENT_FACTORY (source), result, NULL);

	if (contact == NULL) {
		empathy_theme_adium_append_event (chat->view, _("Invalid contact ID"));
		goto out;
	}

	individual = empathy_ensure_individual_from_tp_contact (
		empathy_contact_get_tp_contact (contact));
	empathy_display_individual_info (individual);

	g_object_unref (individual);
	g_object_unref (contact);

out:
	g_object_unref (chat);
}

static void
chat_command_whois (EmpathyChat *chat,
		    GStrv strv)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	TpConnection *conn;
	EmpathyClientFactory *factory;

	conn = tp_channel_get_connection ((TpChannel *) priv->tp_chat);
	factory = empathy_client_factory_dup ();

	/* Element 0 of 'strv' is "whois"; element 1 is the contact ID
	 * entered by the user (including spaces, if any). */
	empathy_client_factory_dup_contact_by_id_async (factory, conn, strv[1],
		whois_got_contact_cb, g_object_ref (chat));

	g_object_unref (factory);
}

static void
chat_command_whale (EmpathyChat *chat,
		    GStrv        strv)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	TpMessage *message;

	message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
		"\n\n\n"
		"•_______________•");
	empathy_tp_chat_send (priv->tp_chat, message);
	g_object_unref (message);
}

static void
chat_command_babywhale (EmpathyChat *chat,
			GStrv        strv)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	TpMessage *message;

	message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
		"\n"
		"•_____•");
	empathy_tp_chat_send (priv->tp_chat, message);
	g_object_unref (message);
}

static void
chat_command_inspector (EmpathyChat *chat,
		    GStrv        strv)
{
	if (EMPATHY_IS_THEME_ADIUM (chat->view)) {
		empathy_theme_adium_show_inspector (
			EMPATHY_THEME_ADIUM (chat->view));
	}
}

static void chat_command_help (EmpathyChat *chat, GStrv strv);

typedef void (*ChatCommandFunc) (EmpathyChat *chat, GStrv strv);

typedef struct {
	const gchar *prefix;
	guint min_parts;
	guint max_parts;
	ChatCommandFunc func;
	gboolean (*is_supported)(EmpathyChat *chat);
	const gchar *help;
} ChatCommandItem;

static ChatCommandItem commands[] = {
	{"clear", 1, 1, chat_command_clear, NULL,
	 N_("/clear: clear all messages from the current conversation")},

	{"topic", 2, 2, chat_command_topic, NULL,
	 N_("/topic <topic>: set the topic of the current conversation")},

	{"join", 2, 2, chat_command_join, NULL,
	 N_("/join <chat room ID>: join a new chat room")},

	{"j", 2, 2, chat_command_join, NULL,
	 N_("/j <chat room ID>: join a new chat room")},

	/* FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=643295 */
	{"part", 1, 3, chat_command_part, part_command_supported,
	 N_("/part [<chat room ID>] [<reason>]: leave the chat room, "
	    "by default the current one")},

	{"query", 2, 3, chat_command_query, NULL,
	 N_("/query <contact ID> [<message>]: open a private chat")},

	{"msg", 3, 3, chat_command_msg, NULL,
	 N_("/msg <contact ID> <message>: open a private chat")},

	{"nick", 2, 2, chat_command_nick, nick_command_supported,
	 N_("/nick <nickname>: change your nickname on the current server")},

	{"me", 2, 2, chat_command_me, NULL,
	 N_("/me <message>: send an ACTION message to the current conversation")},

	{"say", 2, 2, chat_command_say, NULL,
	 N_("/say <message>: send <message> to the current conversation. "
	    "This is used to send a message starting with a '/'. For example: "
	    "\"/say /join is used to join a new chat room\"")},

	{"whois", 2, 2, chat_command_whois, NULL,
	 N_("/whois <contact ID>: display information about a contact")},

	{"help", 1, 2, chat_command_help, NULL,
	 N_("/help [<command>]: show all supported commands. "
	    "If <command> is defined, show its usage.")},

	{"inspector", 1, 1, chat_command_inspector, NULL, NULL},

	{"whale", 1, 1, chat_command_whale, NULL, NULL},
	{"babywhale", 1, 1, chat_command_babywhale, NULL, NULL},
};

static void
chat_command_show_help (EmpathyChat     *chat,
			ChatCommandItem *item)
{
	gchar *str;

	if (item->help == NULL) {
		return;
	}

	str = g_strdup_printf (_("Usage: %s"), _(item->help));
	empathy_theme_adium_append_event (chat->view, str);
	g_free (str);
}

static void
chat_command_help (EmpathyChat *chat,
		   GStrv        strv)
{
	guint i;

	/* If <command> part is not defined,
	 * strv[1] will be the terminal NULL */
	if (strv[1] == NULL) {
		for (i = 0; i < G_N_ELEMENTS (commands); i++) {
			if (commands[i].is_supported != NULL) {
				if (!commands[i].is_supported (chat)) {
					continue;
				}
			}
			if (commands[i].help == NULL) {
				continue;
			}
			empathy_theme_adium_append_event (chat->view,
				_(commands[i].help));
		}
		return;
	}

	for (i = 0; i < G_N_ELEMENTS (commands); i++) {
		if (g_ascii_strcasecmp (strv[1], commands[i].prefix) == 0) {
			if (commands[i].is_supported != NULL) {
				if (!commands[i].is_supported (chat)) {
					break;
				}
			}
			if (commands[i].help == NULL) {
				break;
			}
			chat_command_show_help (chat, &commands[i]);
			return;
		}
	}

	empathy_theme_adium_append_event (chat->view,
		_("Unknown command"));
}

static GStrv
chat_command_parse (const gchar *text, guint max_parts)
{
	GPtrArray *array;
	gchar *item;

	DEBUG ("Parse command, parts=%d text=\"%s\":", max_parts, text);

	array = g_ptr_array_sized_new (max_parts + 1);
	while (max_parts > 1) {
		const gchar *end;

		/* Skip white spaces */
		while (g_ascii_isspace (*text)) {
			text++;
		}

		/* Search the end of this part, until first space. */
		for (end = text; *end != '\0' && !g_ascii_isspace (*end); end++)
			/* Do nothing */;
		if (*end == '\0') {
			break;
		}

		item = g_strndup (text, end - text);
		g_ptr_array_add (array, item);
		DEBUG ("\tITEM: \"%s\"", item);

		text = end;
		max_parts--;
	}

	/* Append last part if not empty */
	item = g_strstrip (g_strdup (text));
	if (!TPAW_STR_EMPTY (item)) {
		g_ptr_array_add (array, item);
		DEBUG ("\tITEM: \"%s\"", item);
	} else {
		g_free (item);
	}

	/* Make the array NULL-terminated */
	g_ptr_array_add (array, NULL);

	return (GStrv) g_ptr_array_free (array, FALSE);
}

static gboolean
has_prefix_case (const gchar *s,
		  const gchar *prefix)
{
	return g_ascii_strncasecmp (s, prefix, strlen (prefix)) == 0;
}

static void
chat_send (EmpathyChat  *chat,
	   const gchar *msg)
{
	EmpathyChatPriv *priv;
	TpMessage  *message;
	guint            i;

	if (TPAW_STR_EMPTY (msg)) {
		return;
	}

	priv = GET_PRIV (chat);

	chat_input_history_add (chat, msg, FALSE);

	if (msg[0] == '/') {
		gboolean second_slash = FALSE;
		const gchar *iter = msg + 1;

		for (i = 0; i < G_N_ELEMENTS (commands); i++) {
			GStrv strv;
			guint strv_len;
			gchar c;

			if (!has_prefix_case (msg + 1, commands[i].prefix)) {
				continue;
			}
			c = *(msg + 1 + strlen (commands[i].prefix));
			if (c != '\0' && !g_ascii_isspace (c)) {
				continue;
			}
			if (commands[i].is_supported != NULL) {
				if (!commands[i].is_supported (chat)) {
					continue;
				}
			}

			/* We can't use g_strsplit here because it does
			 * not deal correctly if we have more than one space
			 * between args */
			strv = chat_command_parse (msg + 1, commands[i].max_parts);

			strv_len = g_strv_length (strv);
			if (strv_len < commands[i].min_parts ||
			    strv_len > commands[i].max_parts) {
				chat_command_show_help (chat, &commands[i]);
				g_strfreev (strv);
				return;
			}

			commands[i].func (chat, strv);
			g_strfreev (strv);
			return;
		}

		/* Also allow messages with two slashes before the
		 * first space, so it is possible to send a /unix/path.
		 * This heuristic is kind of crap. */
		while (*iter != '\0' && !g_ascii_isspace (*iter)) {
			if (*iter == '/') {
				second_slash = TRUE;
				break;
			}
			iter++;
		}

		if (!second_slash) {
			empathy_theme_adium_append_event (chat->view,
				_("Unknown command; see /help for the available"
				  " commands"));
			return;
		}
	}

	message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL,
		msg);
	empathy_tp_chat_send (priv->tp_chat, message);
	g_object_unref (message);
}

static void
chat_input_text_view_send (EmpathyChat *chat)
{
	GtkTextBuffer  *buffer;
	GtkTextIter     start, end;
	gchar	       *msg;

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));

	gtk_text_buffer_get_bounds (buffer, &start, &end);
	msg = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);

	/* clear the input field */
	gtk_text_buffer_set_text (buffer, "", -1);
	/* delete input history modifications */
	chat_input_history_revert (chat);

	chat_send (chat, msg);
	g_free (msg);
}

static void
chat_state_changed_cb (EmpathyTpChat      *tp_chat,
		       TpContact     *tp_contact,
		       TpChannelChatState  state,
		       EmpathyChat        *chat)
{
	EmpathyChatPriv *priv;
	GList          *l;
	gboolean        was_composing;
	EmpathyContact *contact;

	priv = GET_PRIV (chat);

	contact = empathy_contact_dup_from_tp_contact (tp_contact);

	if (empathy_contact_is_user (contact)) {
		/* We don't care about our own chat state */
		goto out;
	}

	was_composing = (priv->compositors != NULL);

	/* Find the contact in the list. After that l is the list elem or NULL */
	for (l = priv->compositors; l; l = l->next) {
		if (contact == l->data) {
			break;
		}
	}

	switch (state) {
	case TP_CHANNEL_CHAT_STATE_GONE:
	case TP_CHANNEL_CHAT_STATE_INACTIVE:
	case TP_CHANNEL_CHAT_STATE_PAUSED:
	case TP_CHANNEL_CHAT_STATE_ACTIVE:
		/* Contact is not composing */
		if (l) {
			priv->compositors = g_list_remove_link (priv->compositors, l);
			g_object_unref (l->data);
			g_list_free1 (l);
		}
		break;
	case TP_CHANNEL_CHAT_STATE_COMPOSING:
		/* Contact is composing */
		if (!l) {
			priv->compositors = g_list_prepend (priv->compositors,
							    g_object_ref (contact));
		}
		break;
	default:
		g_assert_not_reached ();
	}

	DEBUG ("Was composing: %s now composing: %s",
		was_composing ? "yes" : "no",
		priv->compositors ? "yes" : "no");

	if ((was_composing && !priv->compositors) ||
	    (!was_composing && priv->compositors)) {
		/* Composing state changed */
		g_signal_emit (chat, signals[COMPOSING], 0,
			       priv->compositors != NULL);
	}

out:
	g_object_unref (contact);
}

static GRegex *
get_highlight_regex_for (const gchar *name)
{
	GRegex *regex;
	gchar *name_esc, *pattern;
	GError *error = NULL;

	name_esc = g_regex_escape_string (name, -1);
	pattern = g_strdup_printf ("\\b%s\\b", name_esc);
	regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
		&error);

	if (regex == NULL) {
		DEBUG ("couldn't compile regex /%s/: %s", pattern,
			error->message);

		g_error_free (error);
	}

	g_free (pattern);
	g_free (name_esc);

	return regex;
}

/* Called when priv->self_contact changes, or priv->self_contact:alias changes.
 * Only connected if empathy_chat_is_room() is TRUE, for obvious-ish reasons.
 */
static void
chat_self_contact_alias_changed_cb (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	tp_clear_pointer (&priv->highlight_regex, g_regex_unref);

	if (priv->self_contact != NULL) {
		const gchar *alias = empathy_contact_get_alias (priv->self_contact);

		g_return_if_fail (alias != NULL);
		priv->highlight_regex = get_highlight_regex_for (alias);
	}
}

static gboolean
chat_should_highlight (EmpathyChat *chat,
	EmpathyMessage *message)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	const gchar   *msg;

	g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);

	if (!empathy_chat_is_room (chat)) {
		return FALSE;
	}

	if (!empathy_message_is_incoming (message)) {
		return FALSE;
	}

	msg = empathy_message_get_body (message);
	if (!msg) {
		return FALSE;
	}

	if (empathy_message_is_backlog (message)) {
		/* FIXME: Ideally we shouldn't highlight scrollback messages only if they
		 * have already been received by the user before (and so are in the logs) */
		return FALSE;
	}

	if (priv->highlight_regex == NULL) {
		return FALSE;
	}

	return g_regex_match (priv->highlight_regex, msg, 0, NULL);
}

static void
chat_message_received (EmpathyChat *chat,
	EmpathyMessage *message,
	gboolean pending)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	EmpathyContact  *sender;

	sender = empathy_message_get_sender (message);

	if (empathy_message_is_edit (message)) {
		DEBUG ("Editing message '%s' to '%s'",
			empathy_message_get_supersedes (message),
			empathy_message_get_body (message));

		empathy_theme_adium_edit_message (chat->view, message);
	} else {
		gboolean should_highlight = chat_should_highlight (chat, message);

		if (should_highlight) {
			priv->highlighted = TRUE;
		}

		DEBUG ("Appending new message '%s' from %s (%d)",
			empathy_message_get_token (message),
			empathy_contact_get_alias (sender),
			empathy_contact_get_handle (sender));

		empathy_theme_adium_append_message (chat->view, message, should_highlight);

		if (empathy_message_is_incoming (message)) {
			priv->unread_messages++;
			g_object_notify (G_OBJECT (chat), "nb-unread-messages");
		}

		g_signal_emit (chat, signals[NEW_MESSAGE], 0, message, pending,
			       should_highlight);
	}

	/* We received a message so the contact is no longer
	 * composing */
	chat_state_changed_cb (priv->tp_chat, empathy_contact_get_tp_contact (sender),
			       TP_CHANNEL_CHAT_STATE_ACTIVE,
			       chat);
}

static void
chat_message_received_cb (EmpathyTpChat  *tp_chat,
			  EmpathyMessage *message,
			  EmpathyChat    *chat)
{
	chat_message_received (chat, message, FALSE);
}

static void
chat_message_acknowledged_cb (EmpathyTpChat  *tp_chat,
			      EmpathyMessage *message,
			      EmpathyChat    *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	empathy_theme_adium_message_acknowledged (chat->view,
	    message);

	if (!empathy_message_is_edit (message)) {
		priv->unread_messages--;
		g_object_notify (G_OBJECT (chat), "nb-unread-messages");
	}
}

static void
append_balance_error (EmpathyChat *chat,
		      const gchar *message_body)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	TpConnection *conn = tp_channel_get_connection (TP_CHANNEL (priv->tp_chat));
	const gchar *uri = tp_connection_get_balance_uri (conn);
	const gchar *error = _("insufficient balance to send message");
	gchar *str, *str_markup = NULL;

	if (message_body != NULL) {
		str = g_strdup_printf (_("Error sending message '%s': %s"), message_body, error);
	} else {
		str = g_strdup_printf (_("Error sending message: %s"), error);
	}

	if (!tp_str_empty (uri)) {
		/* translators: error used when user doesn't have enough credit on his
		 * account to send the message. */
		gchar *markup_error = g_strdup_printf (_("insufficient balance to send message."
							 " <a href='%s'>Top up</a>."), uri);

		if (message_body != NULL) {
			gchar *escaped_body = g_markup_escape_text (message_body, -1);

			str_markup = g_strdup_printf (_("Error sending message '%s': %s"),
				escaped_body, markup_error);

			g_free (escaped_body);
		} else {
			str_markup = g_strdup_printf (_("Error sending message: %s"), markup_error);
		}

		g_free (markup_error);
	}

	if (str_markup != NULL)
		empathy_theme_adium_append_event_markup (chat->view, str_markup, str);
	else
		empathy_theme_adium_append_event (chat->view, str);

	g_free (str);
	g_free (str_markup);
}

static void
chat_send_error_cb (EmpathyTpChat          *tp_chat,
		    const gchar            *message_body,
		    TpChannelTextSendError  error_code,
		    const gchar            *dbus_error,
		    EmpathyChat            *chat)
{
	const gchar *error = NULL;
	gchar       *str;

	if (!tp_strdiff (dbus_error, TP_ERROR_STR_INSUFFICIENT_BALANCE)) {
		append_balance_error (chat, message_body);
		return;
	} else if (!tp_strdiff (dbus_error, TP_ERROR_STR_NOT_CAPABLE)) {
		error = _("not capable");
	}

	if (error == NULL) {
		/* if we didn't find a dbus-error, try the old error */
		switch (error_code) {
		case TP_CHANNEL_TEXT_SEND_ERROR_OFFLINE:
			error = _("offline");
			break;
		case TP_CHANNEL_TEXT_SEND_ERROR_INVALID_CONTACT:
			error = _("invalid contact");
			break;
		case TP_CHANNEL_TEXT_SEND_ERROR_PERMISSION_DENIED:
			error = _("permission denied");
			break;
		case TP_CHANNEL_TEXT_SEND_ERROR_TOO_LONG:
			error = _("too long message");
			break;
		case TP_CHANNEL_TEXT_SEND_ERROR_NOT_IMPLEMENTED:
			error = _("not implemented");
			break;
		case TP_CHANNEL_TEXT_SEND_ERROR_UNKNOWN:
		default:
			error = _("unknown");
			break;
		}
	}

	if (message_body != NULL) {
			str = g_strdup_printf (_("Error sending message '%s': %s"),
				message_body, error);
	}
	else {
			str = g_strdup_printf (_("Error sending message: %s"), error);
	}

	empathy_theme_adium_append_event (chat->view, str);
	g_free (str);
}

static void
chat_topic_label_size_allocate_cb (GtkLabel *label,
				   GtkAllocation *allocation,
				   EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	if (!gtk_label_get_line_wrap (label)) {
		if (pango_layout_is_ellipsized (gtk_label_get_layout (label)))
			gtk_widget_show (priv->expander_topic);
		else
			gtk_widget_hide (priv->expander_topic);

		return;
	}
}

static void
chat_topic_expander_activate_cb (GtkExpander *expander,
				 GParamSpec *param_spec,
				 EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	if (gtk_expander_get_expanded (expander)) {
		gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_NONE);
		gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), TRUE);
	} else {
		gtk_label_set_ellipsize (GTK_LABEL (priv->label_topic), PANGO_ELLIPSIZE_END);
		gtk_label_set_line_wrap (GTK_LABEL (priv->label_topic), FALSE);
	}
}

static void
chat_subject_changed_cb (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

		g_free (priv->subject);
		priv->subject = g_strdup (empathy_tp_chat_get_subject (priv->tp_chat));
		g_object_notify (G_OBJECT (chat), "subject");

		if (TPAW_STR_EMPTY (priv->subject)) {
			gtk_widget_hide (priv->hbox_topic);
		} else {
			gchar *markup_topic;
			gchar *markup_text;

			markup_topic = tpaw_add_link_markup (priv->subject);
			markup_text = g_strdup_printf ("<span weight=\"bold\">%s</span> %s",
				_("Topic:"), markup_topic);

			gtk_label_set_markup (GTK_LABEL (priv->label_topic), markup_text);
			g_free (markup_text);
			g_free (markup_topic);

			gtk_widget_show (priv->hbox_topic);
		}
		if (priv->block_events_timeout_id == 0) {
			gchar *str = NULL;

			if (!TPAW_STR_EMPTY (priv->subject)) {
				const gchar *actor = empathy_tp_chat_get_subject_actor (priv->tp_chat);

				if (tp_str_empty (actor)) {
					str = g_strdup_printf (_("Topic set to: %s"), priv->subject);
				} else {
					str = g_strdup_printf (_("Topic set by %s to: %s"),
							       actor, priv->subject);
				}
			} else if (empathy_tp_chat_supports_subject (priv->tp_chat)) {
				/* No need to display this 'event' is no topic can be defined anyway */
				str = g_strdup (_("No topic defined"));
			}

			if (str != NULL) {
				empathy_theme_adium_append_event (EMPATHY_CHAT (chat)->view, str);
				g_free (str);
			}
		}
}

static void
chat_title_changed_cb (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

		g_free (priv->name);
		priv->name = g_strdup (empathy_tp_chat_get_title (priv->tp_chat));
		g_object_notify (G_OBJECT (chat), "name");
}

static gboolean
chat_input_text_get_word_from_iter (GtkTextIter   *iter,
                                    GtkTextIter   *start,
                                    GtkTextIter   *end)
{
	GtkTextIter word_start = *iter;
	GtkTextIter word_end = *iter;
	GtkTextIter tmp;

	if (gtk_text_iter_inside_word (&word_end) &&
			!gtk_text_iter_ends_word (&word_end)) {
		gtk_text_iter_forward_word_end (&word_end);
	}

	tmp = word_end;

	if (gtk_text_iter_get_char (&tmp) == '\'') {
		gtk_text_iter_forward_char (&tmp);

		if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
			gtk_text_iter_forward_word_end (&word_end);
		}
	}


	if (gtk_text_iter_inside_word (&word_start) ||
			gtk_text_iter_ends_word (&word_start)) {
		if (!gtk_text_iter_starts_word (&word_start) ||
				gtk_text_iter_equal (&word_start, &word_end)) {
			gtk_text_iter_backward_word_start (&word_start);
		}

		tmp = word_start;
		gtk_text_iter_backward_char (&tmp);

		if (gtk_text_iter_get_char (&tmp) == '\'') {
			gtk_text_iter_backward_char (&tmp);

			if (g_unichar_isalpha (gtk_text_iter_get_char (&tmp))) {
				gtk_text_iter_backward_word_start (&word_start);
			}
		}
	}

	*start = word_start;
	*end = word_end;
	return TRUE;
}

static void
chat_input_text_buffer_insert_text_cb (GtkTextBuffer *buffer,
                                       GtkTextIter   *location,
                                       gchar         *text,
                                       gint           len,
                                       EmpathyChat   *chat)
{
	GtkTextIter iter, pos;

	/* Remove all misspelled tags in the inserted text.
	 * This happens when text is inserted within a misspelled word. */
	gtk_text_buffer_get_iter_at_offset (buffer, &iter,
					    gtk_text_iter_get_offset (location) - len);
	gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
					    &iter, location);

	gtk_text_buffer_get_iter_at_mark (buffer, &pos, gtk_text_buffer_get_insert (buffer));

	do {
		GtkTextIter start, end;
		gchar *str;

		if (!chat_input_text_get_word_from_iter (&iter, &start, &end))
			continue;

		str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);

		if (gtk_text_iter_in_range (&pos, &start, &end) ||
				gtk_text_iter_equal (&pos, &end) ||
				empathy_spell_check (str)) {
			gtk_text_buffer_remove_tag_by_name (buffer, "misspelled", &start, &end);
		} else {
			gtk_text_buffer_apply_tag_by_name (buffer, "misspelled", &start, &end);
		}

		g_free (str);

	} while (gtk_text_iter_forward_word_end (&iter) &&
		 gtk_text_iter_compare (&iter, location) <= 0);
}

static void
chat_input_text_buffer_delete_range_cb (GtkTextBuffer *buffer,
                                        GtkTextIter   *start,
                                        GtkTextIter   *end,
                                        EmpathyChat   *chat)
{
	GtkTextIter word_start, word_end;

	if (chat_input_text_get_word_from_iter (start, &word_start, &word_end)) {
		gtk_text_buffer_remove_tag_by_name (buffer, "misspelled",
						    &word_start, &word_end);
	}
}

static void
chat_input_text_buffer_changed_cb (GtkTextBuffer *buffer,
                                   EmpathyChat    *chat)
{
	if (gtk_text_buffer_get_char_count (buffer) == 0) {
		chat_composing_stop (chat);
	} else {
		chat_composing_start (chat);
	}
}

static void
chat_input_text_buffer_notify_cursor_position_cb (GtkTextBuffer *buffer,
                                                  GParamSpec    *pspec,
                                                  EmpathyChat    *chat)
{
	GtkTextIter pos;
	GtkTextIter prev_pos;
	GtkTextIter word_start;
	GtkTextIter word_end;
	GtkTextMark *mark;
	gchar *str;

	mark = gtk_text_buffer_get_mark (buffer, "previous-cursor-position");

	gtk_text_buffer_get_iter_at_mark (buffer, &pos,
					  gtk_text_buffer_get_insert (buffer));
	gtk_text_buffer_get_iter_at_mark (buffer, &prev_pos, mark);

	if (!chat_input_text_get_word_from_iter (&prev_pos, &word_start, &word_end))
		goto out;

	if (!gtk_text_iter_in_range (&pos, &word_start, &word_end) &&
			!gtk_text_iter_equal (&pos, &word_end)) {
		str = gtk_text_buffer_get_text (buffer,
					&word_start, &word_end, FALSE);

		if (!empathy_spell_check (str)) {
			gtk_text_buffer_apply_tag_by_name (buffer,
					"misspelled", &word_start, &word_end);
		} else {
			gtk_text_buffer_remove_tag_by_name (buffer,
					"misspelled", &word_start, &word_end);
		}

		g_free (str);
	}

out:
	gtk_text_buffer_move_mark (buffer, mark, &pos);
}

static gboolean
empathy_isspace_cb (gunichar c,
		 gpointer data)
{
	return g_unichar_isspace (c);
}

static gboolean
chat_input_key_press_event_cb (GtkWidget   *widget,
			       GdkEventKey *event,
			       EmpathyChat *chat)
{
	EmpathyChatPriv *priv;
	GtkAdjustment  *adj;
	gdouble         val;
	GtkWidget      *text_view_sw;

	priv = GET_PRIV (chat);

	priv->most_recent_event_type = event->type;

	/* Catch ctrl+up/down so we can traverse messages we sent */
	if ((event->state & GDK_CONTROL_MASK) &&
	    (event->keyval == GDK_KEY_Up ||
	     event->keyval == GDK_KEY_Down)) {
		GtkTextBuffer *buffer;
		const gchar   *str;

		buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
		chat_input_history_update (chat, buffer);

		if (event->keyval == GDK_KEY_Up) {
			str = chat_input_history_get_next (chat);
		} else {
			str = chat_input_history_get_prev (chat);
		}

		g_signal_handlers_block_by_func (buffer,
						 chat_input_text_buffer_changed_cb,
						 chat);
		gtk_text_buffer_set_text (buffer, str ? str : "", -1);
		g_signal_handlers_unblock_by_func (buffer,
						   chat_input_text_buffer_changed_cb,
						   chat);

		return TRUE;
	}

	/* Catch enter but not ctrl/shift-enter */
	if (IS_ENTER (event->keyval) &&
	    !(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
		GtkTextView *view;

		/* This is to make sure that kinput2 gets the enter. And if
		 * it's handled there we shouldn't send on it. This is because
		 * kinput2 uses Enter to commit letters. See:
		 * http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=104299
		 */

		view = GTK_TEXT_VIEW (chat->input_text_view);
		if (gtk_text_view_im_context_filter_keypress (view, event)) {
			gtk_text_view_reset_im_context (view);
			return TRUE;
		}

		chat_input_text_view_send (chat);
		return TRUE;
	}

	text_view_sw = gtk_widget_get_parent (GTK_WIDGET (chat->view));

	if (IS_ENTER (event->keyval) &&
	    (event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK))) {
		/* Newline for shift/control-enter. */
		return FALSE;
	}
	if (!(event->state & GDK_CONTROL_MASK) &&
	    event->keyval == GDK_KEY_Page_Up) {
		adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
		gtk_adjustment_set_value (adj, gtk_adjustment_get_value (adj) - gtk_adjustment_get_page_size (adj));
		return TRUE;
	}
	if ((event->state & GDK_CONTROL_MASK) != GDK_CONTROL_MASK &&
	    event->keyval == GDK_KEY_Page_Down) {
		adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (text_view_sw));
		val = MIN (gtk_adjustment_get_value (adj) + gtk_adjustment_get_page_size (adj),
			   gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj));
		gtk_adjustment_set_value (adj, val);
		return TRUE;
	}
	if (event->keyval == GDK_KEY_Escape) {
		empathy_search_bar_hide (EMPATHY_SEARCH_BAR (priv->search_bar));
	}
	if (!(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) &&
	    event->keyval == GDK_KEY_Tab) {
		GtkTextBuffer *buffer;
		GtkTextIter    start, current;
		gchar         *nick, *completed;
		GList         *list, *completed_list;
		gboolean       is_start_of_buffer;

		buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (EMPATHY_CHAT (chat)->input_text_view));
		gtk_text_buffer_get_iter_at_mark (buffer, &current, gtk_text_buffer_get_insert (buffer));

		/* Get the start of the nick to complete. */
		gtk_text_buffer_get_iter_at_mark (buffer, &start, gtk_text_buffer_get_insert (buffer));
		if (gtk_text_iter_backward_find_char (&start, &empathy_isspace_cb, NULL, NULL)) {
			gtk_text_iter_set_offset (&start, gtk_text_iter_get_offset (&start) + 1);
		}
		is_start_of_buffer = gtk_text_iter_is_start (&start);

		list = empathy_tp_chat_get_members (priv->tp_chat);
		g_completion_add_items (priv->completion, list);

		nick = gtk_text_buffer_get_text (buffer, &start, &current, FALSE);
		completed_list = g_completion_complete (priv->completion,
							nick,
							&completed);

		g_free (nick);

		if (completed) {
			guint        len;
			const gchar *text;
			GString     *message = NULL;
			GList       *l;

			gtk_text_buffer_delete (buffer, &start, &current);

			len = g_list_length (completed_list);

			if (len == 1) {
				/* If we only have one hit, use that text
				 * instead of the text in completed since the
				 * completed text will use the typed string
				 * which might be cased all wrong.
				 * Fixes #120876
				 * */
				text = empathy_contact_get_alias (completed_list->data);
			} else {
				text = completed;

				/* Print all hits to the scrollback view, so the
				 * user knows what possibilities he has.
				 * Fixes #599779
				 * */
				 message = g_string_new ("");
				 for (l = completed_list; l != NULL; l = l->next) {
					g_string_append (message, empathy_contact_get_alias (l->data));
					g_string_append (message, " - ");
				 }
				 empathy_theme_adium_append_event (chat->view, message->str);
				 g_string_free (message, TRUE);
			}

			gtk_text_buffer_insert_at_cursor (buffer, text, strlen (text));

			if (len == 1 && is_start_of_buffer) {
			    gchar *complete_char;

			    complete_char = g_settings_get_string (
				    priv->gsettings_chat,
				    EMPATHY_PREFS_CHAT_NICK_COMPLETION_CHAR);

			    if (complete_char != NULL) {
				gtk_text_buffer_insert_at_cursor (buffer,
								  complete_char,
								  strlen (complete_char));
				gtk_text_buffer_insert_at_cursor (buffer, " ", 1);
				g_free (complete_char);
			    }
			}

			g_free (completed);
		}

		g_completion_clear_items (priv->completion);

		g_list_foreach (list, (GFunc) g_object_unref, NULL);
		g_list_free (list);

		return TRUE;
	}

	return FALSE;
}

static gboolean
chat_text_view_focus_in_event_cb (GtkWidget  *widget,
				  GdkEvent   *event,
				  EmpathyChat *chat)
{
	gtk_widget_grab_focus (chat->input_text_view);

	return TRUE;
}

static void
chat_input_realize_cb (GtkWidget   *widget,
		       EmpathyChat *chat)
{
	DEBUG ("Setting focus to the input text view");
	if (gtk_widget_is_sensitive (widget)) {
		gtk_widget_grab_focus (widget);
	}
}

static void
chat_input_has_focus_notify_cb (GtkWidget   *widget,
				GParamSpec  *pspec,
				EmpathyChat *chat)
{
	empathy_theme_adium_focus_toggled (chat->view, gtk_widget_has_focus (widget));
}

void
empathy_chat_insert_smiley (GtkTextBuffer *buffer,
				EmpathySmiley        *smiley)
{
	gtk_text_buffer_insert_at_cursor (buffer, smiley->str, -1);
}

static void
chat_insert_smiley_activate_cb (EmpathySmileyManager *manager,
				EmpathySmiley        *smiley,
				gpointer              user_data)
{
	EmpathyChat   *chat = EMPATHY_CHAT (user_data);
	GtkTextBuffer *buffer;

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));

	empathy_chat_insert_smiley (buffer, smiley);
}

typedef struct {
	EmpathyChat  *chat;
	gchar       *word;

	GtkTextIter  start;
	GtkTextIter  end;
} EmpathyChatSpell;

static EmpathyChatSpell *
chat_spell_new (EmpathyChat  *chat,
		const gchar *word,
		GtkTextIter  start,
		GtkTextIter  end)
{
	EmpathyChatSpell *chat_spell;

	chat_spell = g_slice_new0 (EmpathyChatSpell);

	chat_spell->chat = g_object_ref (chat);
	chat_spell->word = g_strdup (word);
	chat_spell->start = start;
	chat_spell->end = end;

	return chat_spell;
}

static void
chat_spell_free (EmpathyChatSpell *chat_spell)
{
	g_object_unref (chat_spell->chat);
	g_free (chat_spell->word);
	g_slice_free (EmpathyChatSpell, chat_spell);
}

static void
chat_spelling_menu_activate_cb (GtkMenuItem     *menu_item,
				                EmpathyChatSpell *chat_spell)
{
    empathy_chat_correct_word (chat_spell->chat,
                               &(chat_spell->start),
                               &(chat_spell->end),
                               gtk_menu_item_get_label (menu_item));
}


static GtkWidget *
chat_spelling_build_suggestions_menu (const gchar *code,
				      EmpathyChatSpell *chat_spell)
{
	GList     *suggestions, *l;
	GtkWidget *menu, *menu_item;

	suggestions = empathy_spell_get_suggestions (code, chat_spell->word);
	if (suggestions == NULL)
		return NULL;

	menu = gtk_menu_new ();
	for (l = suggestions; l; l = l->next) {
		menu_item = gtk_menu_item_new_with_label (l->data);
		g_signal_connect (G_OBJECT (menu_item), "activate",
				  G_CALLBACK (chat_spelling_menu_activate_cb),
				  chat_spell);
		gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
	}
	empathy_spell_free_suggestions (suggestions);

	gtk_widget_show_all (menu);

	return menu;
}

static GtkWidget *
chat_spelling_build_menu (EmpathyChatSpell *chat_spell)
{
	GtkWidget *menu, *submenu, *item;
	GList     *codes, *l;

	codes = empathy_spell_get_enabled_language_codes ();
	g_assert (codes != NULL);

	if (g_list_length (codes) > 1) {
		menu = gtk_menu_new ();

		for (l = codes; l; l = l->next) {
			const gchar *code, *name;

			code = l->data;
			name = empathy_spell_get_language_name (code);
			if (!name)
				continue;

			item = gtk_image_menu_item_new_with_label (name);

			submenu = chat_spelling_build_suggestions_menu (
					code, chat_spell);
			if (submenu == NULL)
				gtk_widget_set_sensitive (item, FALSE);
			else
				gtk_menu_item_set_submenu (GTK_MENU_ITEM (item),
							   submenu);
			gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
		}
	} else {
		menu = chat_spelling_build_suggestions_menu (codes->data,
							     chat_spell);
		if (menu == NULL) {
			menu = gtk_menu_new ();
			item = gtk_menu_item_new_with_label (_("(No Suggestions)"));
			gtk_widget_set_sensitive (item, FALSE);
			gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
		}
	}
	g_list_free (codes);

	gtk_widget_show_all (menu);

	return menu;
}

typedef struct {
	EmpathyChat  *chat;
	gchar        *word;
	gchar        *code;
} EmpathyChatWord;

static EmpathyChatWord *
chat_word_new (EmpathyChat  *chat,
		const gchar  *word,
		const gchar  *code)
{
	EmpathyChatWord *chat_word;

	chat_word = g_slice_new0 (EmpathyChatWord);

	chat_word->chat = g_object_ref (chat);
	chat_word->word = g_strdup (word);
	chat_word->code = g_strdup (code);

	return chat_word;
}

static void
chat_word_free (EmpathyChatWord *chat_word)
{
	g_object_unref (chat_word->chat);
	g_free (chat_word->word);
	g_free (chat_word->code);
	g_slice_free (EmpathyChatWord, chat_word);
}

static void
chat_add_to_dictionary_activate_cb (GtkMenuItem     *menu_item,
				    EmpathyChatWord *chat_word)
{
	EmpathyChatPriv *priv = GET_PRIV (chat_word->chat);

	empathy_spell_add_to_dictionary (chat_word->code,
					 chat_word->word);
	priv->update_misspelled_words_id =
		g_idle_add (update_misspelled_words, chat_word->chat);
}

static GtkWidget *
chat_spelling_build_add_to_dictionary_item (EmpathyChatSpell *chat_spell)
{
	GtkWidget       *menu, *item, *lang_item, *image;
	GList           *codes, *l;
	gchar           *label;
	const gchar     *code, *name;
	EmpathyChatWord *chat_word;

	codes = empathy_spell_get_enabled_language_codes ();
	g_assert (codes != NULL);
	if (g_list_length (codes) > 1) {
		/* translators: %s is the selected word */
		label = g_strdup_printf (_("Add '%s' to Dictionary"),
					chat_spell->word);
		item = gtk_image_menu_item_new_with_mnemonic (label);
		g_free (label);
		image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
						      GTK_ICON_SIZE_MENU);
		gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
					       image);

		menu = gtk_menu_new ();

		for (l = codes; l; l = l->next) {
			code = l->data;
			name = empathy_spell_get_language_name (code);
			if (name == NULL)
				continue;

			lang_item = gtk_image_menu_item_new_with_label (name);

			chat_word= chat_word_new (chat_spell->chat,
						  chat_spell->word, code);
			g_object_set_data_full (G_OBJECT (lang_item),
				"chat-word", chat_word,
				(GDestroyNotify) chat_word_free);

			g_signal_connect (G_OBJECT (lang_item), "activate",
				G_CALLBACK (chat_add_to_dictionary_activate_cb),
				chat_word);
			gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), lang_item);
		}
		gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
	} else {
		code = codes->data;
		name = empathy_spell_get_language_name (code);
		g_assert (name != NULL);
		/* translators: first %s is the selected word,
		 * second %s is the language name of the target dictionary */
		label = g_strdup_printf (_("Add '%s' to %s Dictionary"),
					chat_spell->word, name);
		item = gtk_image_menu_item_new_with_mnemonic (label);
		g_free (label);
		image = gtk_image_new_from_icon_name (GTK_STOCK_ADD,
						      GTK_ICON_SIZE_MENU);
		gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);

		chat_word = chat_word_new (chat_spell->chat, chat_spell->word,
					   code);
		g_object_set_data_full (G_OBJECT (item), "chat-word", chat_word,
					(GDestroyNotify) chat_word_free);

		g_signal_connect (G_OBJECT (item), "activate",
				  G_CALLBACK (chat_add_to_dictionary_activate_cb),
				  chat_word);
	}
	g_list_free (codes);

	gtk_widget_show_all (item);

	return item;
}

static void
chat_text_send_cb (GtkMenuItem *menuitem,
		   EmpathyChat *chat)
{
	chat_input_text_view_send (chat);
}

static gboolean
chat_input_button_press_event_cb (GtkTextView    *view,
				  GdkEventButton *event,
				  EmpathyChat    *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	priv->most_recent_event_type = event->type;

	return FALSE;
}

static void
chat_input_populate_popup_cb (GtkTextView *view,
			      GtkMenu     *menu,
			      EmpathyChat *chat)
{
	EmpathyChatPriv      *priv = GET_PRIV (chat);
	GtkTextBuffer        *buffer;
	GtkTextTagTable      *table;
	GtkTextTag           *tag;
	gint                  x, y;
	GtkTextIter           iter, start, end;
	GtkWidget            *item;
	gchar                *str = NULL;
	EmpathyChatSpell     *chat_spell;
	GtkWidget            *spell_menu;
	GtkWidget            *spell_item;
	EmpathySmileyManager *smiley_manager;
	GtkWidget            *smiley_menu;
	GtkWidget            *image;

	buffer = gtk_text_view_get_buffer (view);

	/* Add the emoticon menu. */
	item = gtk_separator_menu_item_new ();
	gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
	gtk_widget_show (item);

	item = gtk_image_menu_item_new_with_mnemonic (_("Insert Smiley"));
	image = gtk_image_new_from_icon_name ("face-smile",
					      GTK_ICON_SIZE_MENU);
	gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
	gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
	gtk_widget_show (item);

	smiley_manager = empathy_smiley_manager_dup_singleton ();
	smiley_menu = empathy_smiley_menu_new (smiley_manager,
					       chat_insert_smiley_activate_cb,
					       chat);
	gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), smiley_menu);
	g_object_unref (smiley_manager);

	/* Add the Send menu item. */
	gtk_text_buffer_get_bounds (buffer, &start, &end);
	str = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
	if (!TPAW_STR_EMPTY (str)) {
		item = gtk_menu_item_new_with_mnemonic (_("_Send"));
		g_signal_connect (G_OBJECT (item), "activate",
				  G_CALLBACK (chat_text_send_cb), chat);
		gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
		gtk_widget_show (item);
	}
	str = NULL;

	/* Add the spell check menu item. */
	table = gtk_text_buffer_get_tag_table (buffer);
	tag = gtk_text_tag_table_lookup (table, "misspelled");

	switch (priv->most_recent_event_type) {
	    case GDK_BUTTON_PRESS:
		/* get the location from the pointer */
		gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
			gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
				gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);

		gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
						       GTK_TEXT_WINDOW_WIDGET,
						       x, y,
						       &x, &y);
		gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (view),
						    &iter, x, y);
		break;

	    default:
		g_warn_if_reached ();
		/* assume the KEY_PRESS case */

	    case GDK_KEY_PRESS:
		/* get the location from the cursor */
		gtk_text_buffer_get_iter_at_mark (buffer, &iter,
				gtk_text_buffer_get_insert (buffer));
		break;

	}

	start = end = iter;
	if (gtk_text_iter_backward_to_tag_toggle (&start, tag) &&
	    gtk_text_iter_forward_to_tag_toggle (&end, tag)) {

		str = gtk_text_buffer_get_text (buffer,
						&start, &end, FALSE);
	}
	if (!TPAW_STR_EMPTY (str)) {
		chat_spell = chat_spell_new (chat, str, start, end);
		g_object_set_data_full (G_OBJECT (menu),
					"chat-spell", chat_spell,
					(GDestroyNotify) chat_spell_free);

		item = gtk_separator_menu_item_new ();
		gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
		gtk_widget_show (item);

		/* Spelling suggestions */
		item = gtk_image_menu_item_new_with_mnemonic (_("_Spelling Suggestions"));
		image = gtk_image_new_from_icon_name (GTK_STOCK_SPELL_CHECK,
						      GTK_ICON_SIZE_MENU);
		gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);

		spell_menu = chat_spelling_build_menu (chat_spell);
		gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), spell_menu);


		spell_item = gtk_separator_menu_item_new ();
		gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
		gtk_widget_show (spell_item);

		/* Add to dictionary */
		spell_item = chat_spelling_build_add_to_dictionary_item (chat_spell);

		gtk_menu_shell_append (GTK_MENU_SHELL (spell_menu), spell_item);
		gtk_widget_show (spell_item);

		gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
		gtk_widget_show (item);
	}
}


static gboolean
chat_log_filter (TplEvent *event,
		 gpointer user_data)
{
	EmpathyChat *chat = EMPATHY_CHAT (user_data);
	EmpathyChatPriv *priv = GET_PRIV (chat);
	EmpathyMessage *message;
	const GList *pending;
	bool retval = FALSE;

	g_return_val_if_fail (TPL_IS_EVENT (event), FALSE);
	g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);

	pending = empathy_tp_chat_get_pending_messages (priv->tp_chat);
	message = empathy_message_from_tpl_log_event (event);

	for (; pending; pending = g_list_next (pending)) {
		if (empathy_message_equal (message, pending->data))
			goto out;
	}

	retval = TRUE;

out:
	g_object_unref (message);
	return retval;
}


static void
show_pending_messages (EmpathyChat *chat) {
	EmpathyChatPriv *priv = GET_PRIV (chat);
	const GList *messages, *l;

	g_return_if_fail (EMPATHY_IS_CHAT (chat));

	if (chat->view == NULL || priv->tp_chat == NULL)
		return;

	if (!priv->can_show_pending)
		return;

	messages = empathy_tp_chat_get_pending_messages (priv->tp_chat);

	for (l = messages; l != NULL ; l = g_list_next (l)) {
		EmpathyMessage *message = EMPATHY_MESSAGE (l->data);
		chat_message_received (chat, message, TRUE);
	}
}


static gboolean
chat_scrollable_set_value (gpointer user_data)
{
	EmpathyChat *chat = EMPATHY_CHAT (user_data);
	EmpathyChatPriv *priv = GET_PRIV (chat);
	GtkAdjustment *adjustment;
	guint upper;

	adjustment = gtk_scrollable_get_vadjustment (
	    GTK_SCROLLABLE (chat->view));

	/* Set the chat->view's adjustment back to the value it had
	 * before it grew as a result of new logs being inserted.
	 */
	upper  = (guint) gtk_adjustment_get_upper (adjustment);
	gtk_adjustment_set_value (adjustment, upper - priv->scroll_offset);

	return G_SOURCE_REMOVE;
}

static void
got_filtered_messages_cb (GObject *walker,
		GAsyncResult *result,
		gpointer user_data)
{
	GList *l;
	GList *messages;
	EmpathyChat *chat = EMPATHY_CHAT (user_data);
	EmpathyChatPriv *priv = GET_PRIV (chat);
	GError *error = NULL;

	if (!tpl_log_walker_get_events_finish (TPL_LOG_WALKER (walker),
		result, &messages, &error)) {
		DEBUG ("%s. Aborting.", error->message);
		empathy_theme_adium_append_event (chat->view,
			_("Failed to retrieve recent logs"));
		g_error_free (error);
		goto out;
	}

	for (l = g_list_last (messages); l; l = g_list_previous (l)) {
		EmpathyMessage *message;

		g_assert (TPL_IS_EVENT (l->data));

		message = empathy_message_from_tpl_log_event (l->data);
		g_object_unref (l->data);

		if (empathy_message_is_edit (message)) {
			/* this is an edited message, create a synthetic event
			 * using the supersedes token and
			 * original-message-sent timestamp, that we can then
			 * replace */
			EmpathyMessage *syn_msg = g_object_new (
				EMPATHY_TYPE_MESSAGE,
				"body", "",
				"token", empathy_message_get_supersedes (message),
				"type", empathy_message_get_tptype (message),
				"timestamp", empathy_message_get_original_timestamp (message),
				"incoming", empathy_message_is_incoming (message),
				"is-backlog", TRUE,
				"receiver", empathy_message_get_receiver (message),
				"sender", empathy_message_get_sender (message),
				NULL);

			empathy_theme_adium_prepend_message (chat->view, syn_msg,
							  chat_should_highlight (chat, syn_msg));
			empathy_theme_adium_edit_message (chat->view, message);

			g_object_unref (syn_msg);
		} else {
			/* append the latest message */
			empathy_theme_adium_prepend_message (chat->view, message,
							  chat_should_highlight (chat, message));
		}

		g_object_unref (message);
	}
	g_list_free (messages);

out:
	/* FIXME: See Bug#610994, we are forcing the ACK of the queue. See comments
	 * about it in EmpathyChatPriv definition */
	priv->retrieving_backlogs = FALSE;
	empathy_chat_messages_read (chat);

	/* Turn back on scrolling */
	empathy_theme_adium_scroll (chat->view, TRUE);

	/* We start watching the scrolling movements only after the first
	 * batch of logs have been fetched. Otherwise, if the
	 * chat->view's page size is too small the scrollbar might hit
	 * the upper edge and trigger another batch of logs to be
	 * fetched.
	 */
	if (G_UNLIKELY (!priv->watch_scroll &&
			!tpl_log_walker_is_end (priv->log_walker))) {
		/* The pending messages need not be shown after the
		 * first batch of logs have been displayed */
		priv->can_show_pending = TRUE;
		show_pending_messages (chat);

		priv->watch_scroll = TRUE;
		g_idle_add_full (G_PRIORITY_LOW, chat_scrollable_connect,
		    g_object_ref (chat), g_object_unref);
	}
	else {
		GtkAdjustment *adjustment;
		guint upper;
		guint value;

		/* The chat->view's adjustment won't change unless we
		 * return to the main loop. Save the current offset
		 * from the lower edge (or the upper value of the
		 * adjustment) so that we can restore it later once the
		 * adjustment grows.
		 */
		adjustment = gtk_scrollable_get_vadjustment (
		    GTK_SCROLLABLE (chat->view));
		upper = (guint) gtk_adjustment_get_upper (adjustment);
		value = (guint) gtk_adjustment_get_value (adjustment);
		priv->scroll_offset = upper - value;

		g_idle_add_full (G_PRIORITY_LOW, chat_scrollable_set_value,
		    g_object_ref (chat), g_object_unref);
	}

	g_object_unref (chat);
}

static gboolean
chat_add_logs (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	if (!priv->id) {
		return G_SOURCE_REMOVE;
	}

	/* Turn off scrolling temporarily */
	empathy_theme_adium_scroll (chat->view, FALSE);

	tpl_log_walker_get_events_async (priv->log_walker, 5,
	    got_filtered_messages_cb, g_object_ref (chat));

	return G_SOURCE_REMOVE;
}

static void
chat_schedule_logs (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	if (priv->retrieving_backlogs)
		return;

	priv->retrieving_backlogs = TRUE;
	g_timeout_add_full (G_PRIORITY_LOW, 500, /* ms */
	    (GSourceFunc) chat_add_logs, g_object_ref (chat), g_object_unref);
}

static void
chat_view_adjustment_changed_cb (GtkAdjustment *adjustment,
				 gpointer user_data)
{
	EmpathyChat *chat = EMPATHY_CHAT (user_data);
	EmpathyChatPriv *priv = GET_PRIV (chat);
	guint page_size;

	if (tpl_log_walker_is_end (priv->log_walker)) {
		g_signal_handlers_disconnect_by_func (adjustment,
		    chat_view_adjustment_changed_cb, user_data);
		return;
	}

	page_size = (guint) gtk_adjustment_get_page_size (adjustment);
	if (page_size <= priv->max_page_size)
		return;

	/* We need to fetch more logs if the page size of the view
	 * increases, so that there is no empty space at the top.
	 */
	if (G_LIKELY (priv->max_page_size != 0))
		chat_schedule_logs (chat);

	priv->max_page_size = page_size;
}

static void
chat_view_adjustment_value_changed_cb (GtkAdjustment *adjustment,
				       gpointer user_data)
{
	EmpathyChat *chat = EMPATHY_CHAT (user_data);
	EmpathyChatPriv *priv = GET_PRIV (chat);
	guint lower;
	guint value;

	if (tpl_log_walker_is_end (priv->log_walker)) {
		g_signal_handlers_disconnect_by_func (adjustment,
		    chat_view_adjustment_value_changed_cb, user_data);
		return;
	}

	lower = (guint) gtk_adjustment_get_lower (adjustment);
	value = (guint) gtk_adjustment_get_value (adjustment);
	if (value != lower)
		return;

	/* Request for more logs to be fetched if the user hit the
	 * upper edge of the chat->view.
	 */
	chat_schedule_logs (chat);
}

static gboolean
chat_scrollable_connect (gpointer user_data)
{
	EmpathyChat *chat = EMPATHY_CHAT (user_data);
	GtkAdjustment *adjustment;

	adjustment = gtk_scrollable_get_vadjustment (
	    GTK_SCROLLABLE (chat->view));

	g_signal_connect (adjustment, "changed",
	    G_CALLBACK (chat_view_adjustment_changed_cb), chat);
	g_signal_connect (adjustment, "value-changed",
	    G_CALLBACK (chat_view_adjustment_value_changed_cb), chat);

	return G_SOURCE_REMOVE;
}

static gint
chat_contacts_completion_func (const gchar *s1,
			       const gchar *s2,
			       gsize        n)
{
	gchar *tmp, *nick1, *nick2;
	gint   ret;

	if (s1 == s2) {
		return 0;
	}
	if (!s1 || !s2) {
		return s1 ? -1 : +1;
	}

	tmp = g_utf8_normalize (s1, -1, G_NORMALIZE_DEFAULT);
	nick1 = g_utf8_casefold (tmp, -1);
	g_free (tmp);

	tmp = g_utf8_normalize (s2, -1, G_NORMALIZE_DEFAULT);
	nick2 = g_utf8_casefold (tmp, -1);
	g_free (tmp);

	ret = strncmp (nick1, nick2, n);

	g_free (nick1);
	g_free (nick2);

	return ret;
}

static gchar *
build_part_message (guint           reason,
		    const gchar    *name,
		    EmpathyContact *actor,
		    const gchar    *message)
{
	GString *s = g_string_new ("");
	const gchar *actor_name = NULL;

	if (actor != NULL) {
		actor_name = empathy_contact_get_alias (actor);
	}

	/* Having an actor only really makes sense for a few actions... */
	switch (reason) {
	case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
		g_string_append_printf (s, _("%s has disconnected"), name);
		break;
	case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
		if (actor_name != NULL) {
			/* translators: reverse the order of these arguments
			 * if the kicked should come before the kicker in your locale.
			 */
			g_string_append_printf (s, _("%1$s was kicked by %2$s"),
				name, actor_name);
		} else {
			g_string_append_printf (s, _("%s was kicked"), name);
		}
		break;
	case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
		if (actor_name != NULL) {
			/* translators: reverse the order of these arguments
			 * if the banned should come before the banner in your locale.
			 */
			g_string_append_printf (s, _("%1$s was banned by %2$s"),
				name, actor_name);
		} else {
			g_string_append_printf (s, _("%s was banned"), name);
		}
		break;
	default:
		g_string_append_printf (s, _("%s has left the room"), name);
	}

	if (!TPAW_STR_EMPTY (message)) {
		/* Note to translators: this string is appended to
		 * notifications like "foo has left the room", with the message
		 * given by the user living the room. If this poses a problem,
		 * please let us know. :-)
		 */
		g_string_append_printf (s, _(" (%s)"), message);
	}

	return g_string_free (s, FALSE);
}

static void
chat_members_changed_cb (EmpathyTpChat  *tp_chat,
			 EmpathyContact *contact,
			 EmpathyContact *actor,
			 guint           reason,
			 gchar          *message,
			 gboolean        is_member,
			 EmpathyChat    *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	const gchar *name = empathy_contact_get_alias (contact);
	gchar *str;

	g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED != reason);

	if (priv->block_events_timeout_id != 0)
		return;

	if (is_member) {
		str = g_strdup_printf (_("%s has joined the room"),
				       name);
	} else {
		str = build_part_message (reason, name, actor, message);
	}

	empathy_theme_adium_append_event (chat->view, str);
	g_free (str);
}

static void
chat_member_renamed_cb (EmpathyTpChat  *tp_chat,
			 EmpathyContact *old_contact,
			 EmpathyContact *new_contact,
			 guint           reason,
			 gchar          *message,
			 EmpathyChat    *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	g_return_if_fail (TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED == reason);

	if (priv->block_events_timeout_id == 0) {
		gchar *str;

		str = g_strdup_printf (_("%s is now known as %s"),
				       empathy_contact_get_alias (old_contact),
				       empathy_contact_get_alias (new_contact));
		empathy_theme_adium_append_event (chat->view, str);
		g_free (str);
	}

}

static gboolean
chat_contacts_visible_timeout_cb (gpointer chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	/* Relax the size request */
	gtk_widget_set_size_request (priv->vbox_left, -1, -1);

	/* Set the position of the slider. This must be done here because
	 * GtkPaned need to know its size allocation and it will be settled only
	 * after the gtk_window_resize () tough effect. */
	if (priv->contacts_width > 0) {
		gtk_paned_set_position (GTK_PANED (priv->hpaned),
					priv->contacts_width);
	}

	priv->contacts_visible_id = 0;

	return FALSE;
}

static void
chat_update_contacts_visibility (EmpathyChat *chat,
			 gboolean show)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	if (!priv->scrolled_window_contacts) {
		return;
	}

	if (priv->remote_contact != NULL) {
		show = FALSE;
	}

	if (show && priv->contact_list_view == NULL) {
		EmpathyIndividualStore *store;
		gint                     min_width;
		GtkAllocation            allocation;

		/* We are adding the contact list to the chat, we don't want the
		 * chat view to become too small. If the chat view is already
		 * smaller than 250 make sure that size won't change. If the
		 * chat view is bigger the contact list will take some space on
		 * it but we make sure the chat view don't become smaller than
		 * 250. Relax the size request once the resize is done */
		gtk_widget_get_allocation (priv->vbox_left, &allocation);
		min_width = MIN (allocation.width, 250);
		gtk_widget_set_size_request (priv->vbox_left, min_width, -1);

		/* There is no way to know when the window resize will happen
		 * since it is WM's decision. Let's hope it won't be longer. */
		if (priv->contacts_visible_id != 0)
			g_source_remove (priv->contacts_visible_id);
		priv->contacts_visible_id = g_timeout_add (500,
			chat_contacts_visible_timeout_cb, chat);

		store = EMPATHY_INDIVIDUAL_STORE (
				empathy_individual_store_channel_new ((TpChannel *) priv->tp_chat));

		empathy_individual_store_set_show_groups (store, FALSE);

		priv->contact_list_view = GTK_WIDGET (empathy_individual_view_new (store,
			EMPATHY_INDIVIDUAL_VIEW_FEATURE_INDIVIDUAL_TOOLTIP,
			EMPATHY_INDIVIDUAL_FEATURE_ADD_CONTACT |
			EMPATHY_INDIVIDUAL_FEATURE_CHAT |
			EMPATHY_INDIVIDUAL_FEATURE_CALL |
			EMPATHY_INDIVIDUAL_FEATURE_LOG |
			EMPATHY_INDIVIDUAL_FEATURE_INFO));

		empathy_individual_view_set_show_offline (
			EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);
		empathy_individual_view_set_show_uninteresting (
			EMPATHY_INDIVIDUAL_VIEW (priv->contact_list_view), TRUE);

		gtk_container_add (GTK_CONTAINER (priv->scrolled_window_contacts),
				   priv->contact_list_view);

		gtk_widget_show (priv->contact_list_view);
		gtk_widget_show (priv->scrolled_window_contacts);
		g_object_unref (store);
	} else if (!show) {
		priv->contacts_width = gtk_paned_get_position (GTK_PANED (priv->hpaned));
		gtk_widget_hide (priv->scrolled_window_contacts);
		if (priv->contact_list_view != NULL) {
			gtk_widget_destroy (priv->contact_list_view);
			priv->contact_list_view = NULL;
		}
	}
}

void
empathy_chat_set_show_contacts (EmpathyChat *chat,
				gboolean     show)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	priv->show_contacts = show;

	chat_update_contacts_visibility (chat, show);

	g_object_notify (G_OBJECT (chat), "show-contacts");
}

static void
chat_self_contact_changed_cb (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	if (priv->self_contact != NULL) {
		g_signal_handlers_disconnect_by_func (priv->self_contact,
						      chat_self_contact_alias_changed_cb,
						      chat);
	}
	g_clear_object (&priv->self_contact);

	priv->self_contact = empathy_tp_chat_get_self_contact (priv->tp_chat);
	if (priv->self_contact != NULL) {
		g_object_ref (priv->self_contact);

		if (empathy_chat_is_room (chat)) {
			g_signal_connect_swapped (priv->self_contact, "notify::alias",
					  G_CALLBACK (chat_self_contact_alias_changed_cb),
					  chat);
		}
	}

	chat_self_contact_alias_changed_cb (chat);
}

static void
chat_remote_contact_changed_cb (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	if (priv->remote_contact != NULL) {
		g_object_unref (priv->remote_contact);
		priv->remote_contact = NULL;
	}

	g_free (priv->id);

	priv->id = g_strdup (empathy_tp_chat_get_id (priv->tp_chat));
	priv->remote_contact = empathy_tp_chat_get_remote_contact (priv->tp_chat);
	if (priv->remote_contact != NULL) {
		g_object_ref (priv->remote_contact);
		priv->handle_type = TP_ENTITY_TYPE_CONTACT;
	}
	else if (priv->tp_chat != NULL) {
		tp_channel_get_handle ((TpChannel *) priv->tp_chat, &priv->handle_type);
	}

	chat_update_contacts_visibility (chat, priv->show_contacts);

	g_object_notify (G_OBJECT (chat), "remote-contact");
	g_object_notify (G_OBJECT (chat), "id");
}

static void
chat_invalidated_cb (EmpathyTpChat *tp_chat,
		 guint domain,
		 gint code,
		 gchar *message,
		 EmpathyChat   *chat)
{
	EmpathyChatPriv *priv;

	priv = GET_PRIV (chat);

	if (!priv->tp_chat) {
		return;
	}

	chat_composing_remove_timeout (chat);
	g_object_unref (priv->tp_chat);
	priv->tp_chat = NULL;
	g_object_notify (G_OBJECT (chat), "tp-chat");

	empathy_theme_adium_append_event (chat->view, _("Disconnected"));
	gtk_widget_set_sensitive (chat->input_text_view, FALSE);

	chat_update_contacts_visibility (chat, FALSE);

	priv->unread_messages_when_offline = priv->unread_messages;
}

static gboolean
update_misspelled_words (gpointer data)
{
	EmpathyChat *chat = EMPATHY_CHAT (data);
	EmpathyChatPriv *priv = GET_PRIV (chat);
	GtkTextBuffer *buffer;
	GtkTextIter iter;
	gint length;

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));

	gtk_text_buffer_get_end_iter (buffer, &iter);
	length = gtk_text_iter_get_offset (&iter);
	chat_input_text_buffer_insert_text_cb (buffer, &iter,
					       NULL, length, chat);

	priv->update_misspelled_words_id = 0;

	return FALSE;
}

static void
conf_spell_checking_cb (GSettings *gsettings_chat,
			const gchar *key,
			gpointer user_data)
{
	EmpathyChat *chat = EMPATHY_CHAT (user_data);
	EmpathyChatPriv *priv = GET_PRIV (chat);
	gboolean spell_checker;
	GtkTextBuffer *buffer;

	if (strcmp (key, EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED) != 0)
		return;

	spell_checker = g_settings_get_boolean (gsettings_chat,
			EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED);

	if (!empathy_spell_supported ()) {
		spell_checker = FALSE;
	}

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));

	if (spell_checker == priv->spell_checking_enabled) {
		if (spell_checker) {
			/* Possibly changed dictionaries,
			 * update misspelled words. Need to do so in idle
			 * so the spell checker is updated. */
			priv->update_misspelled_words_id =
				g_idle_add (update_misspelled_words, chat);
		}

		return;
	}

	if (spell_checker) {
		GtkTextIter iter;

		priv->notify_cursor_position_id = tp_g_signal_connect_object  (
				buffer, "notify::cursor-position",
				G_CALLBACK (chat_input_text_buffer_notify_cursor_position_cb),
				chat, 0);
		priv->insert_text_id = tp_g_signal_connect_object  (
				buffer, "insert-text",
				G_CALLBACK (chat_input_text_buffer_insert_text_cb),
				chat, G_CONNECT_AFTER);
		priv->delete_range_id = tp_g_signal_connect_object  (
				buffer, "delete-range",
				G_CALLBACK (chat_input_text_buffer_delete_range_cb),
				chat, G_CONNECT_AFTER);

		gtk_text_buffer_create_tag (buffer, "misspelled",
					    "underline", PANGO_UNDERLINE_ERROR,
					    NULL);

		gtk_text_buffer_get_iter_at_mark (buffer, &iter,
	                                          gtk_text_buffer_get_insert (buffer));
		gtk_text_buffer_create_mark (buffer, "previous-cursor-position",
					     &iter, TRUE);

		/* Mark misspelled words in the existing buffer.
		 * Need to do so in idle so the spell checker is updated. */
		priv->update_misspelled_words_id =
			g_idle_add (update_misspelled_words, chat);
	} else {
		GtkTextTagTable *table;
		GtkTextTag *tag;

		g_signal_handler_disconnect (buffer, priv->notify_cursor_position_id);
		priv->notify_cursor_position_id = 0;
		g_signal_handler_disconnect (buffer, priv->insert_text_id);
		priv->insert_text_id = 0;
		g_signal_handler_disconnect (buffer, priv->delete_range_id);
		priv->delete_range_id = 0;

		table = gtk_text_buffer_get_tag_table (buffer);
		tag = gtk_text_tag_table_lookup (table, "misspelled");
		gtk_text_tag_table_remove (table, tag);

		gtk_text_buffer_delete_mark_by_name (buffer,
						     "previous-cursor-position");
	}

	priv->spell_checking_enabled = spell_checker;
}

static gboolean
save_paned_pos_timeout (gpointer data)
{
	EmpathyChat *self = data;
	gint hpaned_pos;

	hpaned_pos = gtk_paned_get_position (GTK_PANED (self->priv->hpaned));

	g_settings_set_int (self->priv->gsettings_ui,
			    EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS,
			    hpaned_pos);

	return FALSE;
}

static gboolean
chat_hpaned_pos_changed_cb (GtkWidget* hpaned,
		GParamSpec *spec,
		gpointer user_data)
{
	EmpathyChat *chat = EMPATHY_CHAT (user_data);

	if (chat->priv->save_paned_pos_id != 0)
		g_source_remove (chat->priv->save_paned_pos_id);

	chat->priv->save_paned_pos_id = g_timeout_add_seconds (1,
		save_paned_pos_timeout, chat);

	return TRUE;
}

static void
chat_create_ui (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	GtkBuilder      *gui;
 	GList           *list = NULL;
	gchar           *filename;
	GtkTextBuffer   *buffer;
	EmpathyThemeManager *theme_mgr;

	filename = empathy_file_lookup ("empathy-chat.ui",
					"libempathy-gtk");
	gui = tpaw_builder_get_file (filename,
					"chat_widget", &priv->widget,
					"hpaned", &priv->hpaned,
					"vbox_left", &priv->vbox_left,
					"scrolled_window_chat", &priv->scrolled_window_chat,
					"scrolled_window_input", &priv->scrolled_window_input,
					"hbox_topic", &priv->hbox_topic,
					"expander_topic", &priv->expander_topic,
					"label_topic", &priv->label_topic,
					"scrolled_window_contacts", &priv->scrolled_window_contacts,
					"info_bar_vbox", &priv->info_bar_vbox,
					NULL);

	tpaw_builder_connect (gui, chat,
		"expander_topic", "notify::expanded", chat_topic_expander_activate_cb,
		"label_topic", "size-allocate", chat_topic_label_size_allocate_cb,
		NULL);

	g_free (filename);

	/* Add message view. */
	theme_mgr = empathy_theme_manager_dup_singleton ();
	chat->view = empathy_theme_manager_create_view (theme_mgr);
	g_object_unref (theme_mgr);
	/* If this is a GtkTextView, it's set as a drag destination for text/plain
	   and other types, even though it's non-editable and doesn't accept any
	   drags.  This steals drag motion for anything inside the scrollbars,
	   making drag destinations on chat windows far less useful.
	 */
	gtk_drag_dest_unset (GTK_WIDGET (chat->view));
	g_signal_connect (chat->view, "focus_in_event",
			  G_CALLBACK (chat_text_view_focus_in_event_cb),
			  chat);
	gtk_container_add (GTK_CONTAINER (priv->scrolled_window_chat),
			   GTK_WIDGET (chat->view));
	gtk_widget_show (GTK_WIDGET (chat->view));

	/* Add input GtkTextView */
	chat->input_text_view = empathy_input_text_view_new ();
	g_signal_connect (chat->input_text_view, "notify::has-focus",
			  G_CALLBACK (chat_input_has_focus_notify_cb),
			  chat);
	g_signal_connect (chat->input_text_view, "key-press-event",
			  G_CALLBACK (chat_input_key_press_event_cb),
			  chat);
	g_signal_connect (chat->input_text_view, "realize",
			  G_CALLBACK (chat_input_realize_cb),
			  chat);
	g_signal_connect (chat->input_text_view, "button-press-event",
			  G_CALLBACK (chat_input_button_press_event_cb),
			  chat);
	g_signal_connect (chat->input_text_view, "populate-popup",
			  G_CALLBACK (chat_input_populate_popup_cb),
			  chat);
	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
	tp_g_signal_connect_object  (buffer, "changed",
			  G_CALLBACK (chat_input_text_buffer_changed_cb),
			  chat, 0);
	tp_g_signal_connect_object (priv->gsettings_chat,
			"changed::" EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED,
			G_CALLBACK (conf_spell_checking_cb), chat, 0);
	conf_spell_checking_cb (priv->gsettings_chat,
				EMPATHY_PREFS_CHAT_SPELL_CHECKER_ENABLED, chat);
	gtk_container_add (GTK_CONTAINER (priv->scrolled_window_input),
			   chat->input_text_view);
	gtk_widget_show (chat->input_text_view);

	/* Add the (invisible) search bar */
	priv->search_bar = empathy_search_bar_new (chat->view);
	gtk_box_pack_start (GTK_BOX(priv->vbox_left),
	                    priv->search_bar,
	                    FALSE, FALSE, 0);
	gtk_box_reorder_child (GTK_BOX(priv->vbox_left), priv->search_bar, 1);

	/* Initialy hide the topic, will be shown if not empty */
	gtk_widget_hide (priv->hbox_topic);

	g_signal_connect (priv->hpaned, "notify::position",
			  G_CALLBACK (chat_hpaned_pos_changed_cb),
			  chat);

	/* Set widget focus order */
	list = g_list_append (NULL, priv->search_bar);
	list = g_list_append (list, priv->scrolled_window_input);
	gtk_container_set_focus_chain (GTK_CONTAINER (priv->vbox_left), list);
	g_list_free (list);

	list = g_list_append (NULL, priv->vbox_left);
	list = g_list_append (list, priv->scrolled_window_contacts);
	gtk_container_set_focus_chain (GTK_CONTAINER (priv->hpaned), list);
	g_list_free (list);

	list = g_list_append (NULL, priv->hpaned);
	list = g_list_append (list, priv->hbox_topic);
	gtk_container_set_focus_chain (GTK_CONTAINER (priv->widget), list);
	g_list_free (list);

	/* Add the main widget in the chat widget */
	gtk_box_pack_start (GTK_BOX (chat), priv->widget, TRUE, TRUE, 0);
	g_object_unref (gui);
}

static void
chat_finalize (GObject *object)
{
	EmpathyChat     *chat;
	EmpathyChatPriv *priv;

	chat = EMPATHY_CHAT (object);
	priv = GET_PRIV (chat);

	DEBUG ("Finalized: %p", object);

	if (priv->update_misspelled_words_id != 0)
		g_source_remove (priv->update_misspelled_words_id);

	if (priv->save_paned_pos_id != 0)
		g_source_remove (priv->save_paned_pos_id);

	if (priv->contacts_visible_id != 0)
		g_source_remove (priv->contacts_visible_id);

	g_object_unref (priv->gsettings_chat);
	g_object_unref (priv->gsettings_ui);

	g_list_foreach (priv->input_history, (GFunc) chat_input_history_entry_free, NULL);
	g_list_free (priv->input_history);

	g_list_foreach (priv->compositors, (GFunc) g_object_unref, NULL);
	g_list_free (priv->compositors);

	chat_composing_remove_timeout (chat);

	g_object_unref (priv->account_manager);
	g_object_unref (priv->log_manager);
	g_object_unref (priv->log_walker);

	if (priv->tp_chat) {
		g_signal_handlers_disconnect_by_func (priv->tp_chat,
			chat_invalidated_cb, chat);
		g_signal_handlers_disconnect_by_func (priv->tp_chat,
			chat_message_received_cb, chat);
		g_signal_handlers_disconnect_by_func (priv->tp_chat,
			chat_message_acknowledged_cb, chat);
		g_signal_handlers_disconnect_by_func (priv->tp_chat,
			chat_send_error_cb, chat);
		g_signal_handlers_disconnect_by_func (priv->tp_chat,
			chat_state_changed_cb, chat);
		g_signal_handlers_disconnect_by_func (priv->tp_chat,
			chat_members_changed_cb, chat);
		g_signal_handlers_disconnect_by_func (priv->tp_chat,
			chat_self_contact_changed_cb, chat);
		g_signal_handlers_disconnect_by_func (priv->tp_chat,
			chat_remote_contact_changed_cb, chat);
		g_signal_handlers_disconnect_by_func (priv->tp_chat,
			chat_title_changed_cb, chat);
		g_signal_handlers_disconnect_by_func (priv->tp_chat,
			chat_subject_changed_cb, chat);
		empathy_tp_chat_leave (priv->tp_chat, "");
		g_object_unref (priv->tp_chat);
	}
	if (priv->account) {
		g_object_unref (priv->account);
	}
	if (priv->self_contact) {
		g_signal_handlers_disconnect_by_func (priv->self_contact,
						      chat_self_contact_alias_changed_cb,
						      chat);
		g_object_unref (priv->self_contact);
	}
	if (priv->remote_contact) {
		g_object_unref (priv->remote_contact);
	}

	if (priv->block_events_timeout_id) {
		g_source_remove (priv->block_events_timeout_id);
	}

	g_free (priv->id);
	g_free (priv->name);
	g_free (priv->subject);
	g_completion_free (priv->completion);

	tp_clear_pointer (&priv->highlight_regex, g_regex_unref);

	G_OBJECT_CLASS (empathy_chat_parent_class)->finalize (object);
}

static void
chat_constructed (GObject *object)
{
	EmpathyChat *chat = EMPATHY_CHAT (object);
	EmpathyChatPriv *priv = GET_PRIV (chat);
	TplEntity *target;

	if (priv->tp_chat != NULL) {
		TpChannel *channel = TP_CHANNEL (priv->tp_chat);
		TpConnection *conn = tp_channel_get_connection (channel);
		gboolean supports_avatars =
			tp_proxy_has_interface_by_id (conn,
						      TP_IFACE_QUARK_CONNECTION_INTERFACE_AVATARS1);

		empathy_theme_adium_set_show_avatars (chat->view,
						    supports_avatars);
	}

	/* Add messages from last conversation */
	if (priv->handle_type == TP_ENTITY_TYPE_ROOM)
		target = tpl_entity_new_from_room_id (priv->id);
	else
		target = tpl_entity_new (priv->id, TP_ENTITY_TYPE_CONTACT, NULL, NULL);

	priv->log_walker = tpl_log_manager_walk_filtered_events (priv->log_manager, priv->account, target,
								 TPL_EVENT_MASK_TEXT, chat_log_filter, chat);
	g_object_unref (target);

	if (priv->handle_type != TP_ENTITY_TYPE_ROOM) {
		/* First display logs from the logger and then display pending messages */
		chat_add_logs (chat);
	}
	 else {
		/* Just display pending messages for rooms */
		priv->can_show_pending = TRUE;
		show_pending_messages (chat);
	}
}

static void
empathy_chat_class_init (EmpathyChatClass *klass)
{
	GObjectClass   *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = chat_finalize;
	object_class->get_property = chat_get_property;
	object_class->set_property = chat_set_property;
	object_class->constructed = chat_constructed;

	g_object_class_install_property (object_class,
					 PROP_TP_CHAT,
					 g_param_spec_object ("tp-chat",
							      "Empathy tp chat",
							      "The tp chat object",
							      EMPATHY_TYPE_TP_CHAT,
							      G_PARAM_CONSTRUCT |
							      G_PARAM_READWRITE |
							      G_PARAM_STATIC_STRINGS));
	g_object_class_install_property (object_class,
					 PROP_ACCOUNT,
					 g_param_spec_object ("account",
							      "Account of the chat",
							      "The account of the chat",
							      TP_TYPE_ACCOUNT,
							      G_PARAM_READABLE |
							      G_PARAM_STATIC_STRINGS));
	g_object_class_install_property (object_class,
					 PROP_ID,
					 g_param_spec_string ("id",
							      "Chat's id",
							      "The id of the chat",
							      NULL,
							      G_PARAM_READABLE |
							      G_PARAM_STATIC_STRINGS));
	g_object_class_install_property (object_class,
					 PROP_NAME,
					 g_param_spec_string ("name",
							      "Chat's name",
							      "The name of the chat",
							      NULL,
							      G_PARAM_READABLE |
							      G_PARAM_STATIC_STRINGS));
	g_object_class_install_property (object_class,
					 PROP_SUBJECT,
					 g_param_spec_string ("subject",
							      "Chat's subject",
							      "The subject or topic of the chat",
							      NULL,
							      G_PARAM_READABLE |
							      G_PARAM_STATIC_STRINGS));
	g_object_class_install_property (object_class,
					 PROP_REMOTE_CONTACT,
					 g_param_spec_object ("remote-contact",
							      "The remote contact",
							      "The remote contact is any",
							      EMPATHY_TYPE_CONTACT,
							      G_PARAM_READABLE |
							      G_PARAM_STATIC_STRINGS));
	g_object_class_install_property (object_class,
					 PROP_SHOW_CONTACTS,
					 g_param_spec_boolean ("show-contacts",
							       "Contacts' visibility",
							       "The visibility of the contacts' list",
							       TRUE,
							       G_PARAM_READWRITE |
							       G_PARAM_STATIC_STRINGS));

	g_object_class_install_property (object_class,
					 PROP_SMS_CHANNEL,
					 g_param_spec_boolean ("sms-channel",
						 	       "SMS Channel",
							       "TRUE if this channel is for sending SMSes",
							       FALSE,
							       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

	g_object_class_install_property (object_class,
					 PROP_N_MESSAGES_SENDING,
					 g_param_spec_uint ("n-messages-sending",
						 	    "Num Messages Sending",
							    "The number of messages being sent",
							    0, G_MAXUINT, 0,
							    G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

	g_object_class_install_property (object_class,
					 PROP_NB_UNREAD_MESSAGES,
					 g_param_spec_uint ("nb-unread-messages",
							    "Num Unread Messages",
							    "The number of unread messages",
							    0, G_MAXUINT, 0,
							    G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

	signals[COMPOSING] =
		g_signal_new ("composing",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_LAST,
			      0,
			      NULL, NULL,
			      g_cclosure_marshal_generic,
			      G_TYPE_NONE,
			      1, G_TYPE_BOOLEAN);

	/**
	 * EmpathyChat::new-message:
	 * @self: the #EmpathyChat
	 * @message: the new message
	 * @pending: whether the message was in the pending queue when @self
	 *  was created
	 * @should_highlight: %TRUE if the message mentions the local user
	 */
	signals[NEW_MESSAGE] =
		g_signal_new ("new-message",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_LAST,
			      0,
			      NULL, NULL,
			      g_cclosure_marshal_generic,
			      G_TYPE_NONE,
			      3, EMPATHY_TYPE_MESSAGE, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);

	signals[PART_COMMAND_ENTERED] =
			g_signal_new ("part-command-entered",
				  G_OBJECT_CLASS_TYPE (object_class),
				  G_SIGNAL_RUN_LAST,
				  0,
				  NULL, NULL,
				  g_cclosure_marshal_generic,
				  G_TYPE_NONE,
				  1, G_TYPE_STRV);

	g_type_class_add_private (object_class, sizeof (EmpathyChatPriv));
}

static gboolean
chat_block_events_timeout_cb (gpointer data)
{
	EmpathyChatPriv *priv = GET_PRIV (data);

	priv->block_events_timeout_id = 0;

	return FALSE;
}

static void
account_manager_prepared_cb (GObject *source_object,
			     GAsyncResult *result,
			     gpointer user_data)
{
	GList *accounts, *l;
	TpAccountManager *account_manager = TP_ACCOUNT_MANAGER (source_object);
	EmpathyChat *chat = user_data;
	GError *error = NULL;

	if (!tp_proxy_prepare_finish (account_manager, result, &error)) {
		DEBUG ("Failed to prepare the account manager: %s", error->message);
		g_error_free (error);
		return;
	}

	accounts = tp_account_manager_dup_usable_accounts (account_manager);

	for (l = accounts; l != NULL; l = l->next) {
		TpAccount *account = l->data;
		tp_g_signal_connect_object (account, "status-changed",
					     G_CALLBACK (chat_new_connection_cb),
					     chat, 0);
	}

	g_list_free_full (accounts, g_object_unref);
}

static void
empathy_chat_init (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (chat,
		EMPATHY_TYPE_CHAT, EmpathyChatPriv);

	chat->priv = priv;
	priv->log_manager = tpl_log_manager_dup_singleton ();
	priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
	priv->gsettings_ui = g_settings_new (EMPATHY_PREFS_UI_SCHEMA);

	priv->contacts_width = g_settings_get_int (priv->gsettings_ui,
		EMPATHY_PREFS_UI_CHAT_WINDOW_PANED_POS);
	priv->input_history = NULL;
	priv->input_history_current = NULL;
	priv->account_manager = tp_account_manager_dup ();

	tp_proxy_prepare_async (priv->account_manager, NULL,
					  account_manager_prepared_cb, chat);

	priv->show_contacts = g_settings_get_boolean (priv->gsettings_chat,
			EMPATHY_PREFS_CHAT_SHOW_CONTACTS_IN_ROOMS);

	/* Block events for some time to avoid having "has come online" or
	 * "joined" messages. */
	priv->block_events_timeout_id =
		g_timeout_add_seconds (1, chat_block_events_timeout_cb, chat);

	/* Add nick name completion */
	priv->completion = g_completion_new ((GCompletionFunc) empathy_contact_get_alias);
	g_completion_set_compare (priv->completion, chat_contacts_completion_func);

	chat_create_ui (chat);
}

EmpathyChat *
empathy_chat_new (EmpathyTpChat *tp_chat)
{
	return g_object_new (EMPATHY_TYPE_CHAT, "tp-chat", tp_chat, NULL);
}

EmpathyTpChat *
empathy_chat_get_tp_chat (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);

	return priv->tp_chat;
}

typedef struct
{
	EmpathyChat *self;
	GtkWidget *info_bar;
	gulong response_id;
	GtkWidget *button;
	GtkWidget *label;
	GtkWidget *entry;
	GtkWidget *spinner;
	gchar *password;
} PasswordData;

static void
passwd_remember_button_cb (GtkButton *button,
			  PasswordData *data)
{
	gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
}

static void
passwd_not_now_button_cb (GtkButton *button,
			  PasswordData *data)
{
	gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_NO);
}

static void
remember_password_infobar_response_cb (GtkWidget *info_bar,
				       gint response_id,
				       PasswordData *data)
{
	EmpathyChatPriv *priv = GET_PRIV (data->self);

	if (response_id == GTK_RESPONSE_OK) {
		DEBUG ("Saving room password");
		tpaw_keyring_set_room_password_async (priv->account,
							 empathy_tp_chat_get_id (priv->tp_chat),
							 data->password,
							 NULL, NULL);
	}

	gtk_widget_destroy (info_bar);
	g_free (data->password);
	g_slice_free (PasswordData, data);
}

static void
chat_prompt_to_save_password (EmpathyChat *self,
			      PasswordData *data)
{
	GtkWidget *content_area;
	GtkWidget *hbox;
	GtkWidget *image;
	GtkWidget *label;
	GtkWidget *alig;
	GtkWidget *button;

	/* save the password in case it needs to be saved */
	data->password = g_strdup (gtk_entry_get_text (GTK_ENTRY (data->entry)));

	/* Remove all previous widgets */
	content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (data->info_bar));
	gtk_container_forall (GTK_CONTAINER (content_area),
			      (GtkCallback) gtk_widget_destroy, NULL);
	data->button = NULL;
	data->label = NULL;
	data->entry = NULL;
	data->spinner = NULL;

	gtk_info_bar_set_message_type (GTK_INFO_BAR (data->info_bar),
				       GTK_MESSAGE_QUESTION);

	hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
	gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);

	/* Add image */
	image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
					  GTK_ICON_SIZE_DIALOG);
	gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);

	/* Add message */
	label = gtk_label_new (_("Would you like to store this password?"));
	gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);

	/* Add 'Remember' button */
	alig = gtk_alignment_new (0, 0.5, 1, 0);

	button = gtk_button_new_with_label (_("Remember"));
	gtk_container_add (GTK_CONTAINER (alig), button);
	gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);

	g_signal_connect (button, "clicked", G_CALLBACK (passwd_remember_button_cb),
			  data);

	/* Add 'Not now' button */
	alig = gtk_alignment_new (0, 0.5, 1, 0);

	button = gtk_button_new_with_label (_("Not now"));
	gtk_container_add (GTK_CONTAINER (alig), button);
	gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);

	g_signal_connect (button, "clicked", G_CALLBACK (passwd_not_now_button_cb),
			  data);

	/* go! */
	g_signal_handler_disconnect (data->info_bar, data->response_id);
	g_signal_connect (data->info_bar, "response",
			  G_CALLBACK (remember_password_infobar_response_cb), data);

	gtk_widget_show_all (data->info_bar);
}

static void
provide_password_cb (GObject *tp_chat,
		     GAsyncResult *res,
		     gpointer user_data)
{
	PasswordData *data = user_data;
	EmpathyChat *self = data->self;
	EmpathyChatPriv *priv = GET_PRIV (self);
	GError *error = NULL;

	if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
						      &error)) {
		DEBUG ("error: %s", error->message);
		/* FIXME: what should we do if that's another error? Close the channel?
		 * Display the raw D-Bus error to the user isn't very useful */
		if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
			/* entry */
			gtk_entry_set_text (GTK_ENTRY (data->entry), "");
			gtk_widget_set_sensitive (data->entry, TRUE);
			gtk_widget_grab_focus (data->entry);

			/* info bar */
			gtk_info_bar_set_message_type (
			    GTK_INFO_BAR (data->info_bar),
			    GTK_MESSAGE_ERROR);

			/* button */
			gtk_widget_set_sensitive (data->button, TRUE);
			gtk_button_set_label (GTK_BUTTON (data->button),
			    _("Retry"));

			/* label */
			gtk_label_set_text (GTK_LABEL (data->label),
			    _("Wrong password; please try again:"));

			/* spinner */
			gtk_spinner_stop (GTK_SPINNER (data->spinner));
			gtk_widget_hide (data->spinner);
		}
		g_error_free (error);
		return;
	}

    /* ask whether they want to save the password */
    chat_prompt_to_save_password (self, data);

	/* Room joined */
	gtk_widget_set_sensitive (priv->hpaned, TRUE);
	gtk_widget_set_sensitive (self->input_text_view, TRUE);
	gtk_widget_grab_focus (self->input_text_view);
}

static void
password_infobar_response_cb (GtkWidget *info_bar,
			      gint response_id,
			      PasswordData *data)
{
	EmpathyChatPriv *priv = GET_PRIV (data->self);
	const gchar *password;

	if (response_id != GTK_RESPONSE_OK) {
		gtk_widget_destroy (info_bar);
		g_slice_free (PasswordData, data);
		return;
	}

	password = gtk_entry_get_text (GTK_ENTRY (data->entry));

	tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
						provide_password_cb, data);

	gtk_widget_set_sensitive (data->button, FALSE);
	gtk_widget_set_sensitive (data->entry, FALSE);

	gtk_spinner_start (GTK_SPINNER (data->spinner));
	gtk_widget_show (data->spinner);
}

static void
password_entry_activate_cb (GtkWidget *entry,
			  PasswordData *data)
{
	gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
}

static void
passwd_join_button_cb (GtkButton *button,
			  PasswordData *data)
{
	gtk_info_bar_response (GTK_INFO_BAR (data->info_bar), GTK_RESPONSE_OK);
}

static void
clear_icon_released_cb (GtkEntry *entry,
			GtkEntryIconPosition icon_pos,
			GdkEvent *event,
			PasswordData *data)
{
	gtk_entry_set_text (entry, "");
}

static void
password_entry_changed_cb (GtkEditable *entry,
			   PasswordData *data)
{
	const gchar *str;

	str = gtk_entry_get_text (GTK_ENTRY (entry));

	gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
	    GTK_ENTRY_ICON_SECONDARY, !TPAW_STR_EMPTY (str));
}

static void
infobar_chat_invalidated_cb (TpProxy       *proxy,
			   guint          domain,
			   gint           code,
			   gchar         *message,
			   gpointer       password_infobar)
{
	/* Destroy the password infobar whenever a channel is invalidated
	 * so we don't have multiple infobars when the MUC is rejoined */
	gtk_widget_destroy (GTK_WIDGET (password_infobar));
}

static void
display_password_info_bar (EmpathyChat *self)
{
	EmpathyChatPriv *priv = GET_PRIV (self);
	GtkWidget *info_bar;
	GtkWidget *content_area;
	GtkWidget *hbox;
	GtkWidget *image;
	GtkWidget *label;
	GtkWidget *entry;
	GtkWidget *alig;
	GtkWidget *button;
	GtkWidget *spinner;
	PasswordData *data;

	data = g_slice_new0 (PasswordData);

	info_bar = gtk_info_bar_new ();
	gtk_info_bar_set_message_type (GTK_INFO_BAR (info_bar),
	    GTK_MESSAGE_QUESTION);

	content_area = gtk_info_bar_get_content_area (GTK_INFO_BAR (info_bar));

	hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5);
	gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);

	/* Add image */
	image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
					  GTK_ICON_SIZE_DIALOG);
	gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);

	/* Add message */
	label = gtk_label_new (_("This room is protected by a password:"));
	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

	/* Add password entry */
	entry = gtk_entry_new ();
	gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
	gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);

	gtk_entry_set_icon_from_stock (GTK_ENTRY (entry),
				       GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
	gtk_entry_set_icon_sensitive (GTK_ENTRY (entry),
				      GTK_ENTRY_ICON_SECONDARY, FALSE);

	g_signal_connect (entry, "icon-release",
			  G_CALLBACK (clear_icon_released_cb), data);
	g_signal_connect (entry, "changed",
			  G_CALLBACK (password_entry_changed_cb), data);

	g_signal_connect (entry, "activate",
			  G_CALLBACK (password_entry_activate_cb), data);

	/* Focus the password entry once it's realized */
	g_signal_connect (entry, "realize", G_CALLBACK (gtk_widget_grab_focus), NULL);

	/* Add 'Join' button */
	alig = gtk_alignment_new (0, 0.5, 1, 0);

	button = gtk_button_new_with_label (_("Join"));
	gtk_container_add (GTK_CONTAINER (alig), button);
	gtk_box_pack_start (GTK_BOX (hbox), alig, FALSE, FALSE, 0);

	g_signal_connect (button, "clicked", G_CALLBACK (passwd_join_button_cb),
			  data);

	/* Add spinner */
	spinner = gtk_spinner_new ();
	gtk_box_pack_end (GTK_BOX (hbox), spinner, FALSE, FALSE, 0);

	/* Save some data for messing around with later */
	data->self = self;
	data->info_bar = info_bar;
	data->button = button;
	data->label = label;
	data->entry = entry;
	data->spinner = spinner;

	gtk_box_pack_start (GTK_BOX (priv->info_bar_vbox), info_bar,
			    TRUE, TRUE, 3);
	gtk_widget_show_all (hbox);

	tp_g_signal_connect_object (priv->tp_chat,
				  "invalidated", G_CALLBACK (infobar_chat_invalidated_cb),
				  info_bar, 0);

	data->response_id = g_signal_connect (info_bar, "response",
					      G_CALLBACK (password_infobar_response_cb), data);

	gtk_widget_show_all (info_bar);
	/* ... but hide the spinner */
	gtk_widget_hide (spinner);

	/* prevent the user from typing anything */
	gtk_widget_set_sensitive (self->input_text_view, FALSE);
}

static void
provide_saved_password_cb (GObject *tp_chat,
			   GAsyncResult *res,
			   gpointer user_data)
{
	EmpathyChat *self = user_data;
	EmpathyChatPriv *priv = GET_PRIV (self);
	GError *error = NULL;

	if (!tp_channel_provide_password_finish (TP_CHANNEL (tp_chat), res,
						      &error)) {
		DEBUG ("error: %s", error->message);
		/* FIXME: what should we do if that's another error? Close the channel?
		 * Display the raw D-Bus error to the user isn't very useful */
		if (g_error_matches (error, TP_ERROR, TP_ERROR_AUTHENTICATION_FAILED)) {
			display_password_info_bar (self);
			gtk_widget_set_sensitive (priv->hpaned, FALSE);
		}
		g_error_free (error);
		return;
	}

	/* Room joined */
	gtk_widget_set_sensitive (priv->hpaned, TRUE);
	gtk_widget_grab_focus (self->input_text_view);
}

static void
chat_room_got_password_cb (GObject *source,
			   GAsyncResult *result,
			   gpointer user_data)
{
	EmpathyChat *self = user_data;
	EmpathyChatPriv *priv = GET_PRIV (self);
	const gchar *password;
	GError *error = NULL;

	password = tpaw_keyring_get_room_password_finish (priv->account,
	    result, &error);

	if (error != NULL) {
		DEBUG ("Couldn't get room password: %s\n", error->message);
		g_clear_error (&error);

		display_password_info_bar (self);
		gtk_widget_set_sensitive (priv->hpaned, FALSE);
		return;
	}

	tp_channel_provide_password_async (TP_CHANNEL (priv->tp_chat), password,
						provide_saved_password_cb, self);
}

static void
chat_password_needed_changed_cb (EmpathyChat *self)
{
	EmpathyChatPriv *priv = GET_PRIV (self);

	if (tp_channel_password_needed (TP_CHANNEL (priv->tp_chat))) {
		tpaw_keyring_get_room_password_async (priv->account,
							 empathy_tp_chat_get_id (priv->tp_chat),
							 chat_room_got_password_cb, self);
	}
}

static void
chat_sms_channel_changed_cb (EmpathyChat *self)
{
	EmpathyChatPriv *priv = GET_PRIV (self);

	priv->sms_channel = tp_text_channel_is_sms_channel (
		(TpTextChannel *) priv->tp_chat);
	g_object_notify (G_OBJECT (self), "sms-channel");
}

static void
chat_n_messages_sending_changed_cb (EmpathyChat *self)
{
	g_object_notify (G_OBJECT (self), "n-messages-sending");
}

void
empathy_chat_set_tp_chat (EmpathyChat   *chat,
			  EmpathyTpChat *tp_chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	g_return_if_fail (EMPATHY_IS_CHAT (chat));
	g_return_if_fail (EMPATHY_IS_TP_CHAT (tp_chat));

	if (priv->tp_chat) {
		return;
	}

	if (priv->account) {
		g_object_unref (priv->account);
	}

	priv->tp_chat = g_object_ref (tp_chat);
	priv->account = g_object_ref (empathy_tp_chat_get_account (priv->tp_chat));

	g_signal_connect (tp_chat, "invalidated",
			  G_CALLBACK (chat_invalidated_cb),
			  chat);
	g_signal_connect (tp_chat, "message-received-empathy",
			  G_CALLBACK (chat_message_received_cb),
			  chat);
	g_signal_connect (tp_chat, "message_acknowledged",
			  G_CALLBACK (chat_message_acknowledged_cb),
			  chat);
	g_signal_connect (tp_chat, "send-error",
			  G_CALLBACK (chat_send_error_cb),
			  chat);
	g_signal_connect (tp_chat, "chat-state-changed",
			  G_CALLBACK (chat_state_changed_cb),
			  chat);
	g_signal_connect (tp_chat, "members-changed",
			  G_CALLBACK (chat_members_changed_cb),
			  chat);
	g_signal_connect (tp_chat, "member-renamed",
			  G_CALLBACK (chat_member_renamed_cb),
			  chat);
	g_signal_connect_swapped (tp_chat, "notify::self-contact",
				  G_CALLBACK (chat_self_contact_changed_cb),
				  chat);
	g_signal_connect_swapped (tp_chat, "notify::remote-contact",
				  G_CALLBACK (chat_remote_contact_changed_cb),
				  chat);
	g_signal_connect_swapped (tp_chat, "notify::password-needed",
				  G_CALLBACK (chat_password_needed_changed_cb),
				  chat);
	g_signal_connect_swapped (tp_chat, "notify::is-sms-channel",
				  G_CALLBACK (chat_sms_channel_changed_cb),
				  chat);
	g_signal_connect_swapped (tp_chat, "notify::n-messages-sending",
				  G_CALLBACK (chat_n_messages_sending_changed_cb),
				  chat);
	g_signal_connect_swapped (tp_chat, "notify::title",
				  G_CALLBACK (chat_title_changed_cb),
				  chat);
	g_signal_connect_swapped (tp_chat, "notify::subject",
				  G_CALLBACK (chat_subject_changed_cb),
				  chat);

	/* Get initial value of properties */
	chat_sms_channel_changed_cb (chat);
	chat_self_contact_changed_cb (chat);
	chat_remote_contact_changed_cb (chat);
	chat_title_changed_cb (chat);
	chat_subject_changed_cb (chat);

	if (chat->input_text_view) {
		gtk_widget_set_sensitive (chat->input_text_view, TRUE);
		if (priv->block_events_timeout_id == 0) {
			empathy_theme_adium_append_event (chat->view, _("Connected"));
		}
	}

	g_object_notify (G_OBJECT (chat), "tp-chat");
	g_object_notify (G_OBJECT (chat), "id");
	g_object_notify (G_OBJECT (chat), "account");

	/* This is a noop when tp-chat is set at object construction time and causes
	 * the pending messages to be show when it's set on the object after it has
	 * been created */
	show_pending_messages (chat);

	/* check if a password is needed */
	chat_password_needed_changed_cb (chat);
}

TpAccount *
empathy_chat_get_account (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);

	return priv->account;
}

const gchar *
empathy_chat_get_id (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);

	return priv->id;
}

gchar *
empathy_chat_dup_name (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	const gchar *ret;

	g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);

	ret = priv->name;

	if (!ret && priv->remote_contact) {
		ret = empathy_contact_get_alias (priv->remote_contact);
	}

	if (!ret)
		ret = priv->id;

	if (!ret)
		ret = _("Conversation");

	if (priv->sms_channel)
		/* Translators: this string is a something like
		 * "Escher Cat (SMS)" */
		return g_strdup_printf (_("%s (SMS)"), ret);
	else
		return g_strdup (ret);
}

const gchar *
empathy_chat_get_subject (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);

	return priv->subject;
}

EmpathyContact *
empathy_chat_get_remote_contact (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);

	return priv->remote_contact;
}

GtkWidget *
empathy_chat_get_contact_menu (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	GtkWidget       *menu = NULL;
	FolksIndividual *individual;
	TpContact *contact;

	g_return_val_if_fail (EMPATHY_IS_CHAT (chat), NULL);

	if (priv->remote_contact == NULL)
		return NULL;

	contact = empathy_contact_get_tp_contact (priv->remote_contact);
	if (contact == NULL)
		return NULL;

	individual = empathy_ensure_individual_from_tp_contact (contact);

	if (individual == NULL)
		return NULL;

	menu = empathy_individual_menu_new (individual, NULL,
					 EMPATHY_INDIVIDUAL_FEATURE_CALL |
					 EMPATHY_INDIVIDUAL_FEATURE_LOG |
					 EMPATHY_INDIVIDUAL_FEATURE_INFO |
					 EMPATHY_INDIVIDUAL_FEATURE_BLOCK, NULL);

	g_object_unref (individual);

	return menu;
}

void
empathy_chat_clear (EmpathyChat *chat)
{
	g_return_if_fail (EMPATHY_IS_CHAT (chat));

	empathy_theme_adium_clear (chat->view);
}

void
empathy_chat_scroll_down (EmpathyChat *chat)
{
	g_return_if_fail (EMPATHY_IS_CHAT (chat));

	empathy_theme_adium_scroll_down (chat->view);
}

void
empathy_chat_cut (EmpathyChat *chat)
{
	GtkTextBuffer *buffer;

	g_return_if_fail (EMPATHY_IS_CHAT (chat));

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
	if (gtk_text_buffer_get_has_selection (buffer)) {
		GtkClipboard *clipboard;

		clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);

		gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE);
	}
}

static gboolean
copy_from_chat_view (EmpathyChat *chat)
{
	if (!empathy_theme_adium_get_has_selection (chat->view))
		return FALSE;

	empathy_theme_adium_copy_clipboard (chat->view);
	return TRUE;
}

static gboolean
copy_from_input (EmpathyChat *chat)
{
	GtkTextBuffer *buffer;
	GtkClipboard *clipboard;

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
	if (!gtk_text_buffer_get_has_selection (buffer))
		return FALSE;

	clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
	gtk_text_buffer_copy_clipboard (buffer, clipboard);
	return TRUE;
}

static gboolean
copy_from_topic (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);
	gint start_offset;
	gint end_offset;
	gchar *start;
	gchar *end;
	gchar *selection;
	const gchar *topic;
	GtkClipboard *clipboard;

	if (!gtk_label_get_selection_bounds (GTK_LABEL (priv->label_topic),
						       &start_offset,
						       &end_offset))
		return FALSE;

	topic = gtk_label_get_text (GTK_LABEL (priv->label_topic));
	start = g_utf8_offset_to_pointer (topic, start_offset);
	end = g_utf8_offset_to_pointer (topic, end_offset);
	selection = g_strndup (start, end - start);

	clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
	gtk_clipboard_set_text (clipboard, selection, -1);

	g_free (selection);
	return TRUE;
}

void
empathy_chat_copy (EmpathyChat *chat)
{
	g_return_if_fail (EMPATHY_IS_CHAT (chat));

	if (copy_from_chat_view (chat))
		return;

	if (copy_from_input (chat))
		return;

	copy_from_topic (chat);
}

void
empathy_chat_paste (EmpathyChat *chat)
{
	GtkTextBuffer *buffer;
	GtkClipboard  *clipboard;
	EmpathyChatPriv *priv;

	g_return_if_fail (EMPATHY_IS_CHAT (chat));

	priv = GET_PRIV (chat);

	if (gtk_widget_get_visible (priv->search_bar)) {
		empathy_search_bar_paste_clipboard (EMPATHY_SEARCH_BAR (priv->search_bar));
		return;
	}

	if (priv->tp_chat == NULL ||
	    !gtk_widget_is_sensitive (chat->input_text_view))
		return;

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));
	clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);

	gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE);
}

void
empathy_chat_find (EmpathyChat *chat)
{
	EmpathyChatPriv *priv;

	g_return_if_fail (EMPATHY_IS_CHAT (chat));

	priv = GET_PRIV (chat);

	empathy_search_bar_show (EMPATHY_SEARCH_BAR (priv->search_bar));
}

void
empathy_chat_correct_word (EmpathyChat  *chat,
			  GtkTextIter *start,
			  GtkTextIter *end,
			  const gchar *new_word)
{
	GtkTextBuffer *buffer;

	g_return_if_fail (chat != NULL);
	g_return_if_fail (new_word != NULL);

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (chat->input_text_view));

	gtk_text_buffer_delete (buffer, start, end);
	gtk_text_buffer_insert (buffer, start,
				new_word,
				-1);
}

gboolean
empathy_chat_is_room (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);

	return (priv->handle_type == TP_ENTITY_TYPE_ROOM);
}

gboolean
empathy_chat_is_highlighted (EmpathyChat *chat)
{
	EmpathyChatPriv *priv = GET_PRIV (chat);

	g_return_val_if_fail (EMPATHY_IS_CHAT (chat), FALSE);

	return priv->highlighted;
}

guint
empathy_chat_get_nb_unread_messages (EmpathyChat *self)
{
	EmpathyChatPriv *priv = GET_PRIV (self);

	g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);

	return priv->unread_messages;
}

/* called when the messages have been read by user */
void
empathy_chat_messages_read (EmpathyChat *self)
{
	EmpathyChatPriv *priv = GET_PRIV (self);

	g_return_if_fail (EMPATHY_IS_CHAT (self));

	/* FIXME: See Bug#610994, See comments about it in EmpathyChatPriv
	 * definition. If we are still retrieving the backlogs, do not ACK */
	if (priv->retrieving_backlogs)
		return;

	if (priv->tp_chat != NULL) {
		tp_text_channel_ack_all_pending_messages_async (
			TP_TEXT_CHANNEL (priv->tp_chat), NULL, NULL);
	}

	priv->highlighted = FALSE;

	if (priv->unread_messages_when_offline > 0) {
		/* We can't ack those as the connection has gone away so just consider
		* them as read. */
		priv->unread_messages -= priv->unread_messages_when_offline;
		g_object_notify (G_OBJECT (self), "nb-unread-messages");
		priv->unread_messages_when_offline = 0;
	}
}

/* Return TRUE if on of the contacts in this chat is composing */
gboolean
empathy_chat_is_composing (EmpathyChat *chat)
{
  return chat->priv->compositors != NULL;
}

gboolean
empathy_chat_is_sms_channel (EmpathyChat *self)
{
	EmpathyChatPriv *priv = GET_PRIV (self);

	g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);

	return priv->sms_channel;
}

guint
empathy_chat_get_n_messages_sending (EmpathyChat *self)
{
	EmpathyChatPriv *priv;

	g_return_val_if_fail (EMPATHY_IS_CHAT (self), 0);

	priv = GET_PRIV (self);

	if (priv->tp_chat == NULL) {
		return 0;
	} else {
		guint n_messages;

		g_object_get (priv->tp_chat,
			"n-messages-sending", &n_messages,
			NULL);

		return n_messages;
	}
}

gchar *
empathy_chat_dup_text (EmpathyChat *self)
{
	GtkTextBuffer *buffer;
	GtkTextIter     start, end;

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));

	gtk_text_buffer_get_bounds (buffer, &start, &end);
	return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
}

void
empathy_chat_set_text (EmpathyChat *self,
		       const gchar *text)
{
	GtkTextBuffer *buffer;

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->input_text_view));

	gtk_text_buffer_set_text (buffer, text, -1);
}