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
|
/*
* base-connection.c - Source for TpBaseConnection
*
* Copyright (C) 2005-2008 Collabora Ltd.
* Copyright (C) 2005-2008 Nokia Corporation
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* SECTION:base-connection
* @title: TpBaseConnection
* @short_description: base class for #TpSvcConnection implementations
* @see_also: #TpBaseConnectionManager, #TpSvcConnection
*
* This base class makes it easier to write #TpSvcConnection implementations
* by managing connection status, channel factories and handle tracking.
* A subclass should often not need to implement any of the Connection
* methods itself.
*
* However, methods may be reimplemented if needed: for instance, Gabble
* overrides RequestHandles so it can validate MUC rooms, which must be done
* asynchronously.
*/
/**
* TpBaseConnectionProc:
* @self: The connection object
*
* Signature of a virtual method on #TpBaseConnection that takes no
* additional parameters and returns nothing.
*/
/**
* TpBaseConnectionStartConnectingImpl:
* @self: The connection object
* @error: Set to the error if %FALSE is returned
*
* Signature of an implementation of the start_connecting method
* of #TpBaseConnection.
*
* On entry, the implementation may assume that it is in state NEW.
*
* If %TRUE is returned, the Connect D-Bus method succeeds; the
* implementation must either have already set the status to CONNECTED by
* calling tp_base_connection_change_status(), or have arranged for a
* status change to either state DISCONNECTED or CONNECTED to be signalled by
* calling tp_base_connection_change_status() at some later time.
* If the status is still NEW after returning %TRUE, #TpBaseConnection will
* automatically change it to CONNECTING for reason REQUESTED.
*
* If %FALSE is returned, the error will be raised from Connect as an
* exception. If the status is not DISCONNECTED after %FALSE is returned,
* #TpBaseConnection will automatically change it to DISCONNECTED
* with a reason appropriate to the error; NetworkError results in
* NETWORK_ERROR, PermissionDenied results in AUTHENTICATION_FAILED, and all
* other errors currently result in NONE_SPECIFIED.
*
* All except the simplest connection managers are expected to implement this
* asynchronously, returning %TRUE in most cases and changing the status
* to CONNECTED or DISCONNECTED later.
*
* Returns: %FALSE if failure has already occurred, else %TRUE.
*/
/**
* TpBaseConnectionCreateHandleReposImpl:
* @self: The connection object
* @repos: An array of pointers to be filled in; the implementation
* may assume all are initially NULL.
*
* Signature of an implementation of the create_handle_repos method
* of #TpBaseConnection.
*/
/**
* TpBaseConnectionCreateChannelFactoriesImpl:
* @self: The implementation, a subclass of TpBaseConnection
*
* Signature of an implementation of the create_channel_factories method
* of #TpBaseConnection.
*
* Returns: a GPtrArray of objects implementing #TpChannelFactoryIface
* which, between them, implement all channel types this Connection
* supports.
*/
/**
* TpBaseConnectionCreateChannelManagersImpl:
* @self: The implementation, a subclass of TpBaseConnection
*
* Signature of an implementation of the create_channel_managers method
* of #TpBaseConnection.
*
* Returns: a GPtrArray of objects implementing #TpChannelManager
* which, between them, implement all channel types this Connection
* supports.
*/
/**
* TpBaseConnectionGetUniqueConnectionNameImpl:
* @self: The implementation, a subclass of TpBaseConnection
*
* Signature of the @get_unique_connection_name virtual method
* on #TpBaseConnection.
*
* Returns: a name for this connection which will be unique within this
* connection manager process, as a string which the caller must free
* with #g_free.
*/
/**
* TpBaseConnectionClass:
* @parent_class: The superclass' structure
* @create_handle_repos: Fill in suitable handle repositories in the
* given array for all those handle types this Connection supports.
* Must be set by subclasses to a non-%NULL value; the function must create
* at least a CONTACT handle repository (failing to do so will cause a crash).
* @create_channel_factories: Create an array of channel factories for this
* Connection. At least one of this or @create_channel_managers must be set by
* subclasses to a non-%NULL value; in new code, setting this to %NULL and
* using channel managers exclusively is recommended.
* @get_unique_connection_name: Construct a unique name for this connection
* (for example using the protocol's format for usernames). If %NULL (the
* default), a unique name will be generated. Subclasses should usually
* override this to get more obvious names, to aid debugging and prevent
* multiple connections to the same account.
* @connecting: If set by subclasses, will be called just after the state
* changes to CONNECTING. May be %NULL if nothing special needs to happen.
* @connected: If set by subclasses, will be called just after the state
* changes to CONNECTED. May be %NULL if nothing special needs to happen.
* @disconnected: If set by subclasses, will be called just after the state
* changes to DISCONNECTED. May be %NULL if nothing special needs to happen.
* @shut_down: Called after disconnected() is called, to clean up the
* connection. Must start the shutdown process for the underlying
* network connection, and arrange for tp_base_connection_finish_shutdown()
* to be called after the underlying connection has been closed. May not
* be left as %NULL.
* @start_connecting: Asynchronously start connecting - called to implement
* the Connect D-Bus method. See #TpBaseConnectionStartConnectingImpl for
* details. May not be left as %NULL.
* @interfaces_always_present: A strv of extra D-Bus interfaces which are
* always implemented by instances of this class, which may be filled in
* by subclasses. The default is to list no additional interfaces.
* Individual instances may detect which additional interfaces they support
* and signal them before going to state CONNECTED by calling
* tp_base_connection_add_interfaces().
* @create_channel_managers: Create an array of channel managers for this
* Connection. At least one of this or @create_channel_factories must be set
* by subclasses to a non-%NULL value.
* Since: 0.7.15
*
* The class of a #TpBaseConnection. Many members are virtual methods etc.
* to be filled in in the subclass' class_init function.
*
* In addition to the fields documented here, there are three gpointer fields
* which must currently be %NULL (a meaning may be defined for these in a
* future version of telepathy-glib), and a pointer to opaque private data.
*/
/**
* TP_INTERNAL_CONNECTION_STATUS_NEW:
*
* A special value for #TpConnectionStatus, used within GLib connection
* managers to indicate that the connection is disconnected because
* connection has never been attempted (as distinct from disconnected
* after connection has started, either by user request or an error).
*
* Must never be visible on the D-Bus - %TP_CONNECTION_STATUS_DISCONNECTED
* is sent instead.
*/
/**
* TpBaseConnection:
* @parent: Fields shared by the superclass.
* @bus_name: A D-Bus well-known bus name, owned by the connection manager
* process and associated with this connection. Set by
* tp_base_connection_register; should be considered read-only by subclasses.
* @object_path: The object-path of this connection. Set by
* tp_base_connection_register; should be considered read-only by subclasses.
* @status: Connection status - may either be a valid TpConnectionStatus or
* TP_INTERNAL_CONNECTION_STATUS_NEW. Should be considered read-only by
* subclasses: use tp_base_connection_change_status() to set it.
* @self_handle: The handle of type %TP_HANDLE_TYPE_CONTACT representing the
* local user. Must be set nonzero by the subclass before moving to state
* CONNECTED. Since 0.7.15, setting this property directly is
* deprecated, in favour of tp_base_connection_set_self_handle(); if this
* property is set directly, the connection must ensure it holds a reference
* to the handle. Changing this property directly having moved to state
* CONNECTED is very strongly discouraged, as this will prevent the
* SelfHandleChanged signal being emitted.
*
* Data structure representing a generic #TpSvcConnection implementation.
*
* In addition to the fields documented here, there are four gpointer fields
* which must currently be %NULL (a meaning may be defined for these in a
* future version of telepathy-glib), and a pointer to opaque private data.
*/
/**
* TpChannelManagerIter:
* @see_also: tp_base_connection_channel_manager_iter_init(),
* tp_base_connection_channel_manager_iter_next()
*
* An iterator over the #TpChannelManager objects known to a #TpBaseConnection.
* It has no public fields.
*
* Since: 0.7.15
*/
/**
* TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED:
* @conn: A TpBaseConnection
* @context: A DBusGMethodInvocation
*
* If @conn is not in state #TP_CONNECTION_STATUS_CONNECTED, complete the
* D-Bus method invocation @context by raising the Telepathy error
* #TP_ERROR_DISCONNECTED, and return from the current function (which
* must be void). For use in D-Bus method implementations.
*/
#include <telepathy-glib/base-connection.h>
#include <string.h>
#include <dbus/dbus-glib-lowlevel.h>
#include <telepathy-glib/channel-factory-iface.h>
#include <telepathy-glib/channel-manager.h>
#include <telepathy-glib/connection-manager.h>
#include <telepathy-glib/contacts-mixin.h>
#include <telepathy-glib/dbus-properties-mixin.h>
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/exportable-channel.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/svc-generic.h>
#include <telepathy-glib/util.h>
#define DEBUG_FLAG TP_DEBUG_CONNECTION
#include "telepathy-glib/debug-internal.h"
static void conn_iface_init (gpointer, gpointer);
static void requests_iface_init (gpointer, gpointer);
G_DEFINE_ABSTRACT_TYPE_WITH_CODE(TpBaseConnection,
tp_base_connection,
G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CONNECTION,
conn_iface_init);
G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_DBUS_PROPERTIES,
tp_dbus_properties_mixin_iface_init);
G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CONNECTION_INTERFACE_REQUESTS,
requests_iface_init));
enum
{
PROP_PROTOCOL = 1,
PROP_SELF_HANDLE,
};
/* signal enum */
enum
{
INVALID_SIGNAL,
SHUTDOWN_FINISHED,
N_SIGNALS
};
static guint signals[N_SIGNALS] = {0};
typedef struct _ChannelRequest ChannelRequest;
typedef enum {
METHOD_REQUEST_CHANNEL,
METHOD_CREATE_CHANNEL,
METHOD_ENSURE_CHANNEL,
NUM_METHODS
} ChannelRequestMethod;
struct _ChannelRequest
{
DBusGMethodInvocation *context;
ChannelRequestMethod method;
gchar *channel_type;
guint handle_type;
guint handle;
/* always TRUE for CREATE; always FALSE for ENSURE */
unsigned suppress_handler : 1;
/* only meaningful for METHOD_ENSURE_CHANNEL; only true if this is the first
* request to be satisfied with a particular channel, and no other request
* satisfied by that channel has a different method.
*/
unsigned yours : 1;
};
static ChannelRequest *
channel_request_new (DBusGMethodInvocation *context,
ChannelRequestMethod method,
const char *channel_type,
guint handle_type,
guint handle,
gboolean suppress_handler)
{
ChannelRequest *ret;
g_assert (NULL != context);
g_assert (NULL != channel_type);
g_assert (method < NUM_METHODS);
ret = g_slice_new0 (ChannelRequest);
ret->context = context;
ret->method = method;
ret->channel_type = g_strdup (channel_type);
ret->handle_type = handle_type;
ret->handle = handle;
ret->suppress_handler = suppress_handler;
ret->yours = FALSE;
DEBUG("New channel request at %p: ctype=%s htype=%d handle=%d suppress=%d",
ret, channel_type, handle_type, handle, suppress_handler);
return ret;
}
static void
channel_request_free (ChannelRequest *request)
{
g_assert (NULL == request->context);
DEBUG("Freeing channel request at %p: ctype=%s htype=%d handle=%d "
"suppress=%d", request, request->channel_type, request->handle_type,
request->handle, request->suppress_handler);
g_free (request->channel_type);
g_slice_free (ChannelRequest, request);
}
static void
channel_request_cancel (gpointer data, gpointer user_data)
{
ChannelRequest *request = (ChannelRequest *) data;
GError error = { TP_ERRORS, TP_ERROR_DISCONNECTED,
"unable to service this channel request, we're disconnecting!" };
DEBUG ("cancelling request at %p for %s/%u/%u", request,
request->channel_type, request->handle_type, request->handle);
dbus_g_method_return_error (request->context, &error);
request->context = NULL;
channel_request_free (request);
}
struct _TpBaseConnectionPrivate
{
/* Telepathy properties */
gchar *protocol;
/* if TRUE, the object has gone away */
gboolean dispose_has_run;
/* array of (TpChannelFactoryIface *) */
GPtrArray *channel_factories;
/* array of (TpChannelManager *) */
GPtrArray *channel_managers;
/* array of (ChannelRequest *) */
GPtrArray *channel_requests;
TpHandleRepoIface *handles[NUM_TP_HANDLE_TYPES];
/* If not %NULL, contains strings representing our interfaces.
* If %NULL, we have no interfaces except those in
* klass->interfaces_always_present (i.e. this is lazily allocated).
*
* Note that this is a GArray of gchar*, not a GPtrArray,
* so that we can use GArray's convenient auto-null-termination. */
GArray *interfaces;
/* Array of DBusGMethodInvocation * representing Disconnect calls.
* If NULL and we are in a state != DISCONNECTED, then we have not started
* shutting down yet.
* If NULL and we are in state DISCONNECTED, then we have finished shutting
* down.
* If not NULL, we are trying to shut down (and must be in state
* DISCONNECTED). */
GPtrArray *disconnect_requests;
/* Only non-NULL if we have taken our ->bus_name. */
TpDBusDaemon *bus_proxy;
};
static void
tp_base_connection_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
TpBaseConnection *self = (TpBaseConnection *) object;
TpBaseConnectionPrivate *priv = self->priv;
switch (property_id) {
case PROP_PROTOCOL:
g_value_set_string (value, priv->protocol);
break;
case PROP_SELF_HANDLE:
g_value_set_uint (value, self->self_handle);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
tp_base_connection_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
TpBaseConnection *self = (TpBaseConnection *) object;
TpBaseConnectionPrivate *priv = self->priv;
switch (property_id) {
case PROP_PROTOCOL:
g_free (priv->protocol);
priv->protocol = g_value_dup_string (value);
g_assert (priv->protocol != NULL);
break;
case PROP_SELF_HANDLE:
{
TpHandle new_self_handle = g_value_get_uint (value);
if (self->status == TP_CONNECTION_STATUS_CONNECTED)
g_return_if_fail (new_self_handle != 0);
if (self->self_handle == new_self_handle)
return;
if (self->self_handle != 0)
tp_handle_unref (priv->handles[TP_HANDLE_TYPE_CONTACT],
self->self_handle);
self->self_handle = new_self_handle;
if (self->self_handle != 0)
tp_handle_ref (priv->handles[TP_HANDLE_TYPE_CONTACT],
self->self_handle);
tp_svc_connection_emit_self_handle_changed (self, self->self_handle);
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
tp_base_connection_dispose (GObject *object)
{
TpBaseConnection *self = TP_BASE_CONNECTION (object);
TpBaseConnectionPrivate *priv = self->priv;
guint i;
if (priv->dispose_has_run)
return;
priv->dispose_has_run = TRUE;
g_assert ((self->status == TP_CONNECTION_STATUS_DISCONNECTED) ||
(self->status == TP_INTERNAL_CONNECTION_STATUS_NEW));
if (self->self_handle != 0)
{
tp_handle_unref (self->priv->handles[TP_HANDLE_TYPE_CONTACT],
self->self_handle);
self->self_handle = 0;
}
if (priv->bus_proxy != NULL)
{
if (self->bus_name != NULL)
{
tp_dbus_daemon_release_name (priv->bus_proxy, self->bus_name, NULL);
}
g_object_unref (priv->bus_proxy);
priv->bus_proxy = NULL;
}
g_ptr_array_foreach (priv->channel_factories, (GFunc) g_object_unref, NULL);
g_ptr_array_free (priv->channel_factories, TRUE);
priv->channel_factories = NULL;
g_ptr_array_foreach (priv->channel_managers, (GFunc) g_object_unref, NULL);
g_ptr_array_free (priv->channel_managers, TRUE);
priv->channel_managers = NULL;
if (priv->channel_requests)
{
g_assert (priv->channel_requests->len == 0);
g_ptr_array_free (priv->channel_requests, TRUE);
priv->channel_requests = NULL;
}
for (i = 0; i < NUM_TP_HANDLE_TYPES; i++)
{
if (priv->handles[i])
{
g_object_unref ((GObject *) priv->handles[i]);
priv->handles[i] = NULL;
}
}
if (priv->interfaces)
{
g_array_free (priv->interfaces, TRUE);
}
if (G_OBJECT_CLASS (tp_base_connection_parent_class)->dispose)
G_OBJECT_CLASS (tp_base_connection_parent_class)->dispose (object);
}
static void
tp_base_connection_finalize (GObject *object)
{
TpBaseConnection *self = TP_BASE_CONNECTION (object);
TpBaseConnectionPrivate *priv = self->priv;
g_free (priv->protocol);
g_free (self->bus_name);
g_free (self->object_path);
G_OBJECT_CLASS (tp_base_connection_parent_class)->finalize (object);
}
/**
* exportable_channel_get_old_info:
* @channel: a channel
* @object_path_out: address at which to store the channel's object path,
* which the caller should g_free()
* @channel_type_out: address at which to store the channel's type, which the
* caller should g_free()
* @handle_type_out: address at which to store the channel's associated handle
* type
* @handle_out: address at which to store the channel's associated
* handle, if any. This is a borrowed reference; the caller
* does not need to tp_handle_unref() it.
*
* Given a new-style exportable channel, as used by the Requests interface's
* API, fetches the information needed for the old-style ListChannels method
* on Connections.
*/
static void
exportable_channel_get_old_info (TpExportableChannel *channel,
gchar **object_path_out,
gchar **channel_type_out,
guint *handle_type_out,
guint *handle_out)
{
gchar *object_path;
GHashTable *channel_properties;
gboolean valid;
g_object_get (channel,
"object-path", &object_path,
"channel-properties", &channel_properties,
NULL);
g_assert (object_path != NULL);
g_assert (tp_dbus_check_valid_object_path (object_path, NULL));
if (object_path_out != NULL)
*object_path_out = object_path;
else
g_free (object_path);
if (channel_type_out != NULL)
{
*channel_type_out = g_strdup (tp_asv_get_string (channel_properties,
TP_PROP_CHANNEL_CHANNEL_TYPE));
g_assert (*channel_type_out != NULL);
g_assert (tp_dbus_check_valid_interface_name (*channel_type_out, NULL));
}
if (handle_type_out != NULL)
{
*handle_type_out = tp_asv_get_uint32 (channel_properties,
TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, &valid);
g_assert (valid);
}
if (handle_out != NULL)
{
*handle_out = tp_asv_get_uint32 (channel_properties,
TP_PROP_CHANNEL_TARGET_HANDLE, &valid);
g_assert (valid);
if (handle_type_out != NULL)
{
if (*handle_type_out == TP_HANDLE_TYPE_NONE)
g_assert (*handle_out == 0);
else
g_assert (*handle_out != 0);
}
}
g_hash_table_destroy (channel_properties);
}
/*
* get_channel_details:
* @obj: a channel, which must implement one of #TpExportableChannel and
* #TpChannelIface
*
* Returns: (oa{sv}: o.fd.T.Conn.Iface.Requests.Channel_Details), suitable for
* inclusion in the NewChannels signal.
*/
static GValueArray *
get_channel_details (GObject *obj)
{
GValueArray *structure = g_value_array_new (2);
GHashTable *table;
GValue *value;
gchar *object_path;
g_object_get (obj,
"object-path", &object_path,
NULL);
g_value_array_append (structure, NULL);
value = g_value_array_get_nth (structure, 0);
g_value_init (value, DBUS_TYPE_G_OBJECT_PATH);
g_value_take_boxed (value, object_path);
object_path = NULL;
g_assert (TP_IS_EXPORTABLE_CHANNEL (obj) || TP_IS_CHANNEL_IFACE (obj));
if (TP_IS_EXPORTABLE_CHANNEL (obj))
{
g_object_get (obj,
"channel-properties", &table,
NULL);
}
else
{
table = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, (GDestroyNotify) tp_g_value_slice_free);
value = tp_g_value_slice_new (G_TYPE_UINT);
g_object_get_property (obj, "handle", value);
g_hash_table_insert (table, TP_PROP_CHANNEL_TARGET_HANDLE, value);
value = tp_g_value_slice_new (G_TYPE_UINT);
g_object_get_property (obj, "handle-type", value);
g_hash_table_insert (table, TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, value);
value = tp_g_value_slice_new (G_TYPE_STRING);
g_object_get_property (obj, "channel-type", value);
g_hash_table_insert (table, TP_PROP_CHANNEL_CHANNEL_TYPE, value);
}
g_value_array_append (structure, NULL);
value = g_value_array_get_nth (structure, 1);
g_value_init (value, TP_HASH_TYPE_QUALIFIED_PROPERTY_VALUE_MAP);
g_value_take_boxed (value, table);
return structure;
}
static GPtrArray *
find_matching_channel_requests (TpBaseConnection *conn,
const gchar *channel_type,
guint handle_type,
guint handle,
ChannelRequest *channel_request,
gboolean *suppress_handler)
{
TpBaseConnectionPrivate *priv = conn->priv;
GPtrArray *requests;
guint i;
requests = g_ptr_array_sized_new (1);
if (handle_type == 0)
{
/* It's an anonymous channel, which can only satisfy the request for
* which it was created (or if it's returned as EXISTING, it can only
* satisfy the request for which it was returned as EXISTING).
*/
g_assert (handle == 0);
g_assert (channel_request == NULL ||
tp_g_ptr_array_contains (priv->channel_requests, channel_request));
if (channel_request)
{
g_ptr_array_add (requests, channel_request);
if (suppress_handler && channel_request->suppress_handler)
*suppress_handler = TRUE;
}
/* whether we've put any matches in requests or not */
return requests;
}
/* for identifiable channels (those which are to a particular handle),
* satisfy any queued requests.
*/
for (i = 0; i < priv->channel_requests->len; i++)
{
ChannelRequest *request = g_ptr_array_index (priv->channel_requests, i);
if (tp_strdiff (request->channel_type, channel_type))
continue;
if (handle_type != request->handle_type)
continue;
if (handle != request->handle)
continue;
if (request->suppress_handler && suppress_handler)
*suppress_handler = TRUE;
g_ptr_array_add (requests, request);
}
/* if this channel was created or returned as a result of a particular
* request, that request had better be among the matching ones in the queue
*/
g_assert (channel_request == NULL ||
tp_g_ptr_array_contains (requests, channel_request));
return requests;
}
static void
satisfy_request (TpBaseConnection *conn,
ChannelRequest *request,
GObject *channel,
const gchar *object_path)
{
TpBaseConnectionPrivate *priv = conn->priv;
DEBUG ("completing queued request %p with success, "
"channel_type=%s, handle_type=%u, "
"handle=%u, suppress_handler=%u", request, request->channel_type,
request->handle_type, request->handle, request->suppress_handler);
switch (request->method)
{
case METHOD_REQUEST_CHANNEL:
tp_svc_connection_return_from_request_channel (request->context,
object_path);
break;
case METHOD_CREATE_CHANNEL:
{
GHashTable *properties;
g_assert (TP_IS_EXPORTABLE_CHANNEL (channel));
g_object_get (channel,
"channel-properties", &properties,
NULL);
tp_svc_connection_interface_requests_return_from_create_channel (
request->context, object_path, properties);
g_hash_table_destroy (properties);
}
break;
case METHOD_ENSURE_CHANNEL:
{
GHashTable *properties;
g_assert (TP_IS_EXPORTABLE_CHANNEL (channel));
g_object_get (channel,
"channel-properties", &properties,
NULL);
tp_svc_connection_interface_requests_return_from_ensure_channel (
request->context, request->yours, object_path, properties);
g_hash_table_destroy (properties);
}
break;
default:
g_assert_not_reached ();
}
request->context = NULL;
g_ptr_array_remove (priv->channel_requests, request);
channel_request_free (request);
}
static void
factory_satisfy_requests (TpBaseConnection *conn,
TpChannelFactoryIface *factory,
TpChannelIface *chan,
ChannelRequest *channel_request,
gboolean is_new)
{
gchar *object_path = NULL, *channel_type = NULL;
guint handle_type = 0, handle = 0;
gboolean suppress_handler = FALSE;
GPtrArray *tmp;
guint i;
g_object_get (chan,
"object-path", &object_path,
"channel-type", &channel_type,
"handle-type", &handle_type,
"handle", &handle,
NULL);
DEBUG ("called for %s", object_path);
tmp = find_matching_channel_requests (conn, channel_type, handle_type,
handle, channel_request,
&suppress_handler);
for (i = 0; i < tmp->len; i++)
satisfy_request (conn, g_ptr_array_index (tmp, i), G_OBJECT (chan),
object_path);
if (is_new)
{
GPtrArray *array = g_ptr_array_sized_new (1);
g_ptr_array_add (array, get_channel_details (G_OBJECT (chan)));
tp_svc_connection_interface_requests_emit_new_channels (conn, array);
g_value_array_free (g_ptr_array_index (array, 0));
g_ptr_array_free (array, TRUE);
tp_svc_connection_emit_new_channel (conn, object_path, channel_type,
handle_type, handle, suppress_handler);
}
g_ptr_array_free (tmp, TRUE);
g_free (object_path);
g_free (channel_type);
}
static void
fail_channel_request (TpBaseConnection *conn,
ChannelRequest *request,
GError *error)
{
TpBaseConnectionPrivate *priv = conn->priv;
DEBUG ("completing queued request %p with error, channel_type=%s, "
"handle_type=%u, handle=%u, suppress_handler=%u",
request, request->channel_type,
request->handle_type, request->handle, request->suppress_handler);
dbus_g_method_return_error (request->context, error);
request->context = NULL;
g_ptr_array_remove (priv->channel_requests, request);
channel_request_free (request);
}
/* Channel factory signal handlers */
static void
factory_channel_closed_cb (GObject *channel,
TpBaseConnection *conn)
{
gchar *object_path;
g_object_get (channel,
"object-path", &object_path,
NULL);
tp_svc_connection_interface_requests_emit_channel_closed (conn,
object_path);
g_free (object_path);
}
static void
factory_new_channel_cb (TpChannelFactoryIface *factory,
GObject *chan,
ChannelRequest *channel_request,
gpointer data)
{
factory_satisfy_requests (TP_BASE_CONNECTION (data), factory,
TP_CHANNEL_IFACE (chan), channel_request, TRUE);
g_signal_connect (chan, "closed", (GCallback) factory_channel_closed_cb,
data);
}
static void
factory_channel_error_cb (TpChannelFactoryIface *factory,
GObject *chan,
GError *error,
ChannelRequest *channel_request,
gpointer data)
{
TpBaseConnection *conn = TP_BASE_CONNECTION (data);
gchar *channel_type = NULL;
guint handle_type = 0, handle = 0;
GPtrArray *tmp;
guint i;
DEBUG ("channel_type=%s, handle_type=%u, handle=%u, error_code=%u, "
"error_message=\"%s\"", channel_type, handle_type, handle,
error->code, error->message);
g_object_get (chan,
"channel-type", &channel_type,
"handle-type", &handle_type,
"handle", &handle,
NULL);
tmp = find_matching_channel_requests (conn, channel_type, handle_type,
handle, channel_request, NULL);
for (i = 0; i < tmp->len; i++)
fail_channel_request (conn, g_ptr_array_index (tmp, i), error);
g_ptr_array_free (tmp, TRUE);
g_free (channel_type);
}
/* Channel manager signal handlers */
typedef struct {
TpBaseConnection *self;
/* borrowed TpExportableChannel => itself if suppress_handler,
* omitted otherwise */
GHashTable *suppress_handler;
} ManagerNewChannelContext;
static void
manager_new_channel (gpointer key,
gpointer value,
gpointer data)
{
TpExportableChannel *channel = TP_EXPORTABLE_CHANNEL (key);
GSList *request_tokens = value;
ManagerNewChannelContext *context = data;
TpBaseConnection *self = TP_BASE_CONNECTION (context->self);
gchar *object_path;
GSList *iter;
gboolean suppress_handler = FALSE;
gboolean satisfies_create_channel = FALSE;
gboolean satisfies_request_channel = FALSE;
ChannelRequest *first_ensure = NULL;
g_object_get (channel,
"object-path", &object_path,
NULL);
/* suppress_handler on Connection.NewChannel should be TRUE if:
* - any satisfied requests were calls to CreateChannel; or
* - at least one satisfied RequestChannel call had suppress_handler=TRUE;
* or
* - any EnsureChannel call will receive Yours=TRUE (that is, if the
* channel satisfies no CreateChannel or RequestChannel calls).
*
* So, it should be FALSE if:
* - all the requests were RequestChannel(..., suppress_handler=FALSE) or
* EnsureChannel and there was at least one RequestChannel; or
* - no requests were satisfied by the channel.
*/
for (iter = request_tokens; iter != NULL; iter = iter->next)
{
ChannelRequest *request = iter->data;
switch (request->method)
{
case METHOD_REQUEST_CHANNEL:
satisfies_request_channel = TRUE;
if (request->suppress_handler)
{
suppress_handler = TRUE;
goto break_loop_early;
}
break;
case METHOD_CREATE_CHANNEL:
satisfies_create_channel = TRUE;
goto break_loop_early;
break;
case METHOD_ENSURE_CHANNEL:
if (first_ensure == NULL)
first_ensure = request;
break;
case NUM_METHODS:
g_assert_not_reached ();
}
}
break_loop_early:
/* put the channel in the suppress_handler hash table if it needs
* suppress_handler set when signalling NewChannel */
if (request_tokens != NULL &&
(satisfies_create_channel || !satisfies_request_channel))
suppress_handler = TRUE;
if (suppress_handler)
g_hash_table_insert (context->suppress_handler, channel, channel);
/* If the only type of request satisfied by this new channel is
* EnsureChannel, give exactly one request Yours=True.
* If other kinds of requests are involved, don't give anyone Yours=True.
*/
if (!satisfies_request_channel
&& !satisfies_create_channel
&& first_ensure != NULL)
{
first_ensure->yours = TRUE;
}
for (iter = request_tokens; iter != NULL; iter = iter->next)
{
satisfy_request (self, iter->data, G_OBJECT (channel),
object_path);
}
g_free (object_path);
}
static void
manager_new_channels_cb (TpChannelManager *manager,
GHashTable *channels,
TpBaseConnection *self)
{
GPtrArray *array;
ManagerNewChannelContext context = { self, g_hash_table_new (NULL, NULL) };
GHashTableIter iter;
gpointer key, value;
g_assert (TP_IS_CHANNEL_MANAGER (manager));
g_assert (TP_IS_BASE_CONNECTION (self));
/* satisfy the RequestChannel/CreateChannel/EnsureChannel calls; as
* a side-effect, fill in context.suppress_handler with those channels
* that will have to be signalled with suppress_handler = TRUE */
g_hash_table_foreach (channels, manager_new_channel, &context);
/* Emit NewChannels */
array = g_ptr_array_sized_new (g_hash_table_size (channels));
g_hash_table_iter_init (&iter, channels);
while (g_hash_table_iter_next (&iter, &key, &value))
{
g_ptr_array_add (array, get_channel_details (G_OBJECT (key)));
}
tp_svc_connection_interface_requests_emit_new_channels (self,
array);
g_ptr_array_foreach (array, (GFunc) g_value_array_free, NULL);
g_ptr_array_free (array, TRUE);
/* Emit NewChannel */
g_hash_table_iter_init (&iter, channels);
while (g_hash_table_iter_next (&iter, &key, &value))
{
gboolean suppress_handler = (
g_hash_table_lookup (context.suppress_handler, key) != NULL);
gchar *object_path, *channel_type;
guint handle_type, handle;
exportable_channel_get_old_info (TP_EXPORTABLE_CHANNEL (key),
&object_path, &channel_type, &handle_type, &handle);
tp_svc_connection_emit_new_channel (self, object_path, channel_type,
handle_type, handle, suppress_handler);
g_free (object_path);
g_free (channel_type);
}
g_hash_table_destroy (context.suppress_handler);
}
static void
manager_request_already_satisfied_cb (TpChannelManager *manager,
gpointer request_token,
TpExportableChannel *channel,
TpBaseConnection *self)
{
gchar *object_path;
g_assert (TP_IS_CHANNEL_MANAGER (manager));
g_assert (TP_IS_EXPORTABLE_CHANNEL (channel));
g_assert (TP_IS_BASE_CONNECTION (self));
g_object_get (channel,
"object-path", &object_path,
NULL);
satisfy_request (self, request_token, G_OBJECT (channel), object_path);
g_free (object_path);
}
static void
manager_request_failed_cb (TpChannelManager *manager,
gpointer request_token,
guint domain,
gint code,
gchar *message,
TpBaseConnection *self)
{
GError error = { domain, code, message };
g_assert (TP_IS_CHANNEL_MANAGER (manager));
g_assert (domain > 0);
g_assert (message != NULL);
g_assert (TP_IS_BASE_CONNECTION (self));
fail_channel_request (self, request_token, &error);
}
static void
manager_channel_closed_cb (TpChannelManager *manager,
const gchar *path,
TpBaseConnection *self)
{
g_assert (TP_IS_CHANNEL_MANAGER (manager));
g_assert (path != NULL);
g_assert (TP_IS_BASE_CONNECTION (self));
tp_svc_connection_interface_requests_emit_channel_closed (self, path);
}
static GObject *
tp_base_connection_constructor (GType type, guint n_construct_properties,
GObjectConstructParam *construct_params)
{
guint i;
TpBaseConnection *self = TP_BASE_CONNECTION (
G_OBJECT_CLASS (tp_base_connection_parent_class)->constructor (
type, n_construct_properties, construct_params));
TpBaseConnectionPrivate *priv = self->priv;
TpBaseConnectionClass *cls = TP_BASE_CONNECTION_GET_CLASS (self);
DEBUG("Post-construction: (TpBaseConnection *)%p", self);
g_assert (cls->create_handle_repos != NULL);
g_assert (cls->create_channel_factories != NULL ||
cls->create_channel_managers != NULL);
g_assert (cls->shut_down != NULL);
g_assert (cls->start_connecting != NULL);
(cls->create_handle_repos) (self, priv->handles);
/* a connection that doesn't support contacts is no use to anyone */
g_assert (priv->handles[TP_HANDLE_TYPE_CONTACT] != NULL);
if (DEBUGGING)
{
for (i = 0; i < NUM_TP_HANDLE_TYPES; i++)
{
DEBUG("Handle repo for type #%u at %p", i, priv->handles[i]);
}
}
if (cls->create_channel_factories != NULL)
priv->channel_factories = cls->create_channel_factories (self);
else
priv->channel_factories = g_ptr_array_sized_new (0);
if (cls->create_channel_managers != NULL)
priv->channel_managers = cls->create_channel_managers (self);
else
priv->channel_managers = g_ptr_array_sized_new (0);
for (i = 0; i < priv->channel_factories->len; i++)
{
GObject *factory = g_ptr_array_index (priv->channel_factories, i);
DEBUG("Channel factory #%u at %p", i, factory);
g_signal_connect (factory, "new-channel", G_CALLBACK
(factory_new_channel_cb), self);
g_signal_connect (factory, "channel-error", G_CALLBACK
(factory_channel_error_cb), self);
}
for (i = 0; i < priv->channel_managers->len; i++)
{
TpChannelManager *manager = TP_CHANNEL_MANAGER (
g_ptr_array_index (priv->channel_managers, i));
DEBUG("Channel manager #%u at %p", i, manager);
g_signal_connect (manager, "new-channels",
(GCallback) manager_new_channels_cb, self);
g_signal_connect (manager, "request-already-satisfied",
(GCallback) manager_request_already_satisfied_cb, self);
g_signal_connect (manager, "request-failed",
(GCallback) manager_request_failed_cb, self);
g_signal_connect (manager, "channel-closed",
(GCallback) manager_channel_closed_cb, self);
}
return (GObject *) self;
}
/* D-Bus properties for the Requests interface */
static void
factory_get_channel_details_foreach (TpChannelIface *chan,
gpointer data)
{
GPtrArray *details = data;
g_ptr_array_add (details, get_channel_details (G_OBJECT (chan)));
}
static void
manager_get_channel_details_foreach (TpExportableChannel *chan,
gpointer data)
{
GPtrArray *details = data;
g_ptr_array_add (details, get_channel_details (G_OBJECT (chan)));
}
static GPtrArray *
conn_requests_get_channel_details (TpBaseConnection *self)
{
TpBaseConnectionPrivate *priv = self->priv;
/* guess that each ChannelManager and each ChannelFactory has two
* channels, on average */
GPtrArray *details = g_ptr_array_sized_new (priv->channel_managers->len * 2
+ priv->channel_factories->len * 2);
guint i;
for (i = 0; i < priv->channel_factories->len; i++)
{
TpChannelFactoryIface *factory = TP_CHANNEL_FACTORY_IFACE (
g_ptr_array_index (priv->channel_factories, i));
tp_channel_factory_iface_foreach (factory,
factory_get_channel_details_foreach, details);
}
for (i = 0; i < priv->channel_managers->len; i++)
{
TpChannelManager *manager = TP_CHANNEL_MANAGER (
g_ptr_array_index (priv->channel_managers, i));
tp_channel_manager_foreach_channel (manager,
manager_get_channel_details_foreach, details);
}
return details;
}
static void
get_requestables_foreach (TpChannelManager *manager,
GHashTable *fixed_properties,
const gchar * const *allowed_properties,
gpointer user_data)
{
GPtrArray *details = user_data;
GValueArray *requestable = g_value_array_new (2);
GValue *value;
g_value_array_append (requestable, NULL);
value = g_value_array_get_nth (requestable, 0);
g_value_init (value, TP_HASH_TYPE_CHANNEL_CLASS);
g_value_set_boxed (value, fixed_properties);
g_value_array_append (requestable, NULL);
value = g_value_array_get_nth (requestable, 1);
g_value_init (value, G_TYPE_STRV);
g_value_set_boxed (value, allowed_properties);
g_ptr_array_add (details, requestable);
}
static GPtrArray *
conn_requests_get_requestables (TpBaseConnection *self)
{
TpBaseConnectionPrivate *priv = self->priv;
/* generously guess that each ChannelManager has about 2 ChannelClasses */
GPtrArray *details = g_ptr_array_sized_new (priv->channel_managers->len * 2);
guint i;
for (i = 0; i < priv->channel_managers->len; i++)
{
TpChannelManager *manager = TP_CHANNEL_MANAGER (
g_ptr_array_index (priv->channel_managers, i));
tp_channel_manager_foreach_channel_class (manager,
get_requestables_foreach, details);
}
return details;
}
static void
conn_requests_get_dbus_property (GObject *object,
GQuark interface,
GQuark name,
GValue *value,
gpointer unused G_GNUC_UNUSED)
{
TpBaseConnection *self = TP_BASE_CONNECTION (object);
g_return_if_fail (interface == TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS);
if (name == g_quark_from_static_string ("Channels"))
{
g_value_take_boxed (value, conn_requests_get_channel_details (self));
}
else if (name == g_quark_from_static_string ("RequestableChannelClasses"))
{
g_value_take_boxed (value, conn_requests_get_requestables (self));
}
else
{
g_return_if_reached ();
}
}
static void
tp_base_connection_class_init (TpBaseConnectionClass *klass)
{
static TpDBusPropertiesMixinPropImpl connection_properties[] = {
{ "SelfHandle", "self-handle", NULL },
{ NULL }
};
static TpDBusPropertiesMixinPropImpl requests_properties[] = {
{ "Channels", NULL, NULL },
{ "RequestableChannelClasses", NULL, NULL },
{ NULL }
};
GParamSpec *param_spec;
GObjectClass *object_class = G_OBJECT_CLASS (klass);
DEBUG("Initializing (TpBaseConnectionClass *)%p", klass);
g_type_class_add_private (klass, sizeof (TpBaseConnectionPrivate));
object_class->dispose = tp_base_connection_dispose;
object_class->finalize = tp_base_connection_finalize;
object_class->constructor = tp_base_connection_constructor;
object_class->get_property = tp_base_connection_get_property;
object_class->set_property = tp_base_connection_set_property;
/**
* TpBaseConnection:protocol:
*
* Identifier used in the Telepathy protocol when this connection's protocol
* name is required.
*/
param_spec = g_param_spec_string ("protocol",
"Telepathy identifier for protocol",
"Identifier string used when the protocol name is required.",
NULL,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK);
g_object_class_install_property (object_class, PROP_PROTOCOL, param_spec);
/**
* TpBaseConnection:self-handle:
*
* The handle of type %TP_HANDLE_TYPE_CONTACT representing the local user.
* Must be set nonzero by the subclass before moving to state CONNECTED.
*
* Since: 0.7.15
*/
param_spec = g_param_spec_uint ("self-handle",
"Connection.SelfHandle",
"The handle of type %TP_HANDLE_TYPE_CONTACT representing the local user.",
0, G_MAXUINT, 0,
G_PARAM_READWRITE |
G_PARAM_STATIC_BLURB);
g_object_class_install_property (object_class, PROP_SELF_HANDLE, param_spec);
/* signal definitions */
/**
* TpBaseConnection::shutdown-finished:
*
* Emitted by tp_base_connection_finish_shutdown() when the underlying
* network connection has been closed; #TpBaseConnectionManager listens
* for this signal and removes connections from its table of active
* connections when it is received.
*/
signals[SHUTDOWN_FINISHED] =
g_signal_new ("shutdown-finished",
G_OBJECT_CLASS_TYPE (klass),
G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
tp_dbus_properties_mixin_class_init (object_class, 0);
tp_dbus_properties_mixin_implement_interface (object_class,
TP_IFACE_QUARK_CONNECTION,
tp_dbus_properties_mixin_getter_gobject_properties, NULL,
connection_properties);
tp_dbus_properties_mixin_implement_interface (object_class,
TP_IFACE_QUARK_CONNECTION_INTERFACE_REQUESTS,
conn_requests_get_dbus_property, NULL,
requests_properties);
}
static void
tp_base_connection_init (TpBaseConnection *self)
{
TpBaseConnectionPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
TP_TYPE_BASE_CONNECTION, TpBaseConnectionPrivate);
guint i;
DEBUG("Initializing (TpBaseConnection *)%p", self);
self->priv = priv;
self->status = TP_INTERNAL_CONNECTION_STATUS_NEW;
for (i = 0; i < NUM_TP_HANDLE_TYPES; i++)
{
priv->handles[i] = NULL;
}
priv->channel_requests = g_ptr_array_new ();
}
static gchar *
squash_name (const gchar *name, guint length)
{
GChecksum *checksum;
gchar *squashed;
g_assert (length >= 10);
checksum = g_checksum_new (G_CHECKSUM_MD5);
g_checksum_update (checksum, (guchar *) name, -1);
squashed = g_strdup_printf (
"%.*s_%.8s", length - 9, name, g_checksum_get_string (checksum));
g_checksum_free (checksum);
return squashed;
}
/**
* tp_base_connection_register:
* @self: A connection
* @cm_name: The name of the connection manager in the Telepathy protocol
* @bus_name: Used to return the bus name corresponding to the connection
* if %TRUE is returned; must not be %NULL. To be freed by the caller.
* @object_path: Used to return the object path of the connection if
* %TRUE is returned; must not be %NULL. To be freed by the caller.
* @error: Used to return an error if %FALSE is returned; may be %NULL
*
* Make the connection object appear on the bus, returning the bus
* name and object path used. If %TRUE is returned, the connection owns the
* bus name, and will release it when destroyed.
*
* Returns: %TRUE on success, %FALSE on error.
*/
gboolean
tp_base_connection_register (TpBaseConnection *self,
const gchar *cm_name,
gchar **bus_name,
gchar **object_path,
GError **error)
{
TpBaseConnectionClass *cls = TP_BASE_CONNECTION_GET_CLASS (self);
TpBaseConnectionPrivate *priv = self->priv;
gchar *tmp;
gchar *safe_proto;
gchar *unique_name;
guint prefix_length;
const guint dbus_max_name_length = 255;
g_return_val_if_fail (TP_IS_BASE_CONNECTION (self), FALSE);
g_return_val_if_fail (cm_name != NULL, FALSE);
g_return_val_if_fail (bus_name != NULL, FALSE);
g_return_val_if_fail (object_path != NULL, FALSE);
if (tp_connection_manager_check_valid_protocol_name (priv->protocol, NULL))
{
safe_proto = g_strdelimit (g_strdup (priv->protocol), "-", '_');
}
else
{
g_warning ("Protocol name %s is not valid - should match "
"[A-Za-z][A-Za-z0-9-]+", priv->protocol);
safe_proto = tp_escape_as_identifier (priv->protocol);
}
/* Plus two for the dots. */
prefix_length = strlen (TP_CONN_BUS_NAME_BASE) +
strlen (cm_name) + strlen (safe_proto) + 2;
if (cls->get_unique_connection_name)
{
tmp = cls->get_unique_connection_name (self);
g_assert (tmp != NULL);
unique_name = tp_escape_as_identifier (tmp);
g_free (tmp);
if (prefix_length + strlen (unique_name) > dbus_max_name_length)
{
/* Is prefix is too long to make reasonable bus name? Ten = one
* character of the original unique name plus underscore plus
* 8-character hash.
*/
if (prefix_length >= dbus_max_name_length - 10)
{
g_warning (
"Couldn't fit CM name + protocol name + unique name into "
"255 characters.");
g_free (unique_name);
return FALSE;
}
tmp = unique_name;
unique_name = squash_name (
tmp, dbus_max_name_length - prefix_length);
g_free (tmp);
}
}
else
{
unique_name = g_strdup_printf ("_%p", self);
}
if (priv->bus_proxy == NULL)
priv->bus_proxy = tp_dbus_daemon_dup (error);
if (priv->bus_proxy == NULL)
return FALSE;
self->bus_name = g_strdup_printf (TP_CONN_BUS_NAME_BASE "%s.%s.%s",
cm_name, safe_proto, unique_name);
g_assert (strlen (self->bus_name) <= 255);
self->object_path = g_strdup_printf (TP_CONN_OBJECT_PATH_BASE "%s/%s/%s",
cm_name, safe_proto, unique_name);
g_free (safe_proto);
g_free (unique_name);
if (!tp_dbus_daemon_request_name (priv->bus_proxy, self->bus_name, FALSE,
error))
{
g_free (self->bus_name);
self->bus_name = NULL;
return FALSE;
}
DEBUG ("bus name %s", self->bus_name);
dbus_g_connection_register_g_object (
tp_proxy_get_dbus_connection (priv->bus_proxy), self->object_path,
G_OBJECT (self));
DEBUG ("object path %s", self->object_path);
*bus_name = g_strdup (self->bus_name);
*object_path = g_strdup (self->object_path);
return TRUE;
}
static void
tp_base_connection_close_all_channels (TpBaseConnection *self)
{
TpBaseConnectionPrivate *priv = self->priv;
/* We deliberately don't iterate over channel managers here -
* they don't need this, and are expected to listen to
* TpSvcConnection::status-changed on the connection for themselves.
*/
/* trigger close_all on all channel factories */
g_ptr_array_foreach (priv->channel_factories, (GFunc)
tp_channel_factory_iface_close_all, NULL);
}
/* D-Bus methods on Connection interface ----------------------------*/
static inline TpConnectionStatusReason
conn_status_reason_from_g_error (GError *error)
{
if (error->domain == TP_ERRORS)
{
switch (error->code)
{
case TP_ERROR_NETWORK_ERROR:
return TP_CONNECTION_STATUS_REASON_NETWORK_ERROR;
case TP_ERROR_PERMISSION_DENIED:
return TP_CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED;
}
}
return TP_CONNECTION_STATUS_REASON_NONE_SPECIFIED;
}
/**
* tp_base_connection_connect:
*
* @context: Used to return the result onto D-Bus.
*
* Implements D-Bus method Connect
* on interface org.freedesktop.Telepathy.Connection
*/
static void
tp_base_connection_connect (TpSvcConnection *iface,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (iface);
TpBaseConnectionClass *cls = TP_BASE_CONNECTION_GET_CLASS (self);
GError *error = NULL;
g_assert (TP_IS_BASE_CONNECTION (self));
if (self->status == TP_INTERNAL_CONNECTION_STATUS_NEW)
{
if (cls->start_connecting (self, &error))
{
if (self->status == TP_INTERNAL_CONNECTION_STATUS_NEW)
{
tp_base_connection_change_status (self,
TP_CONNECTION_STATUS_CONNECTING,
TP_CONNECTION_STATUS_REASON_REQUESTED);
}
}
else
{
if (self->status != TP_CONNECTION_STATUS_DISCONNECTED)
{
tp_base_connection_change_status (self,
TP_CONNECTION_STATUS_DISCONNECTED,
conn_status_reason_from_g_error (error));
}
dbus_g_method_return_error (context, error);
g_error_free (error);
return;
}
}
tp_svc_connection_return_from_connect (context);
}
/**
* tp_base_connection_disconnect
*
* Implements D-Bus method Disconnect
* on interface org.freedesktop.Telepathy.Connection
*
* @error: Used to return a pointer to a GError detailing any error
* that occurred, D-Bus will throw the error only if this
* function returns FALSE.
*
* Returns: TRUE if successful, FALSE if an error was thrown.
*/
static void
tp_base_connection_disconnect (TpSvcConnection *iface,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (iface);
g_assert (TP_IS_BASE_CONNECTION (self));
if (self->priv->disconnect_requests != NULL)
{
g_assert (self->status == TP_CONNECTION_STATUS_DISCONNECTED);
g_ptr_array_add (self->priv->disconnect_requests, context);
return;
}
if (self->status == TP_CONNECTION_STATUS_DISCONNECTED)
{
/* status DISCONNECTED and disconnect_requests NULL => already dead */
tp_svc_connection_return_from_disconnect (context);
return;
}
self->priv->disconnect_requests = g_ptr_array_sized_new (1);
g_ptr_array_add (self->priv->disconnect_requests, context);
tp_base_connection_change_status (self,
TP_CONNECTION_STATUS_DISCONNECTED,
TP_CONNECTION_STATUS_REASON_REQUESTED);
}
/**
* tp_base_connection_get_interfaces
*
* Implements D-Bus method GetInterfaces
* on interface org.freedesktop.Telepathy.Connection
*/
static void
tp_base_connection_get_interfaces (TpSvcConnection *iface,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (iface);
TpBaseConnectionPrivate *priv;
TpBaseConnectionClass *klass;
const gchar **interfaces;
g_assert (TP_IS_BASE_CONNECTION (self));
priv = self->priv;
klass = TP_BASE_CONNECTION_GET_CLASS (self);
if (priv->interfaces)
{
/* There are some extra interfaces for this connection */
interfaces = (const gchar **)(priv->interfaces->data);
}
else
{
/* We only have the interfaces that are always present.
* Instead of bothering to duplicate the static
* array into the GArray, we just use it directly */
interfaces = klass->interfaces_always_present;
}
tp_svc_connection_return_from_get_interfaces (context, interfaces);
}
/**
* tp_base_connection_get_protocol
*
* Implements D-Bus method GetProtocol
* on interface org.freedesktop.Telepathy.Connection
*
* @error: Used to return a pointer to a GError detailing any error
* that occurred, D-Bus will throw the error only if this
* function returns FALSE.
*
* Returns: TRUE if successful, FALSE if an error was thrown.
*/
static void
tp_base_connection_get_protocol (TpSvcConnection *iface,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (iface);
TpBaseConnectionPrivate *priv;
g_assert (TP_IS_BASE_CONNECTION (self));
TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (self, context);
priv = self->priv;
tp_svc_connection_return_from_get_protocol (context, priv->protocol);
}
/**
* tp_base_connection_dbus_get_self_handle
*
* Implements D-Bus method GetSelfHandle
* on interface org.freedesktop.Telepathy.Connection
*/
static void
tp_base_connection_dbus_get_self_handle (TpSvcConnection *iface,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (iface);
g_assert (TP_IS_BASE_CONNECTION (self));
TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (self, context);
tp_svc_connection_return_from_get_self_handle (
context, self->self_handle);
}
/**
* tp_base_connection_get_status
*
* Implements D-Bus method GetStatus
* on interface org.freedesktop.Telepathy.Connection
*/
static void
tp_base_connection_get_status (TpSvcConnection *iface,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (iface);
if (self->status == TP_INTERNAL_CONNECTION_STATUS_NEW)
{
tp_svc_connection_return_from_get_status (
context, TP_CONNECTION_STATUS_DISCONNECTED);
}
else
{
tp_svc_connection_return_from_get_status (
context, self->status);
}
}
#undef DEBUG_FLAG
#define DEBUG_FLAG TP_DEBUG_HANDLES
#include "telepathy-glib/debug-internal.h"
/**
* tp_base_connection_hold_handles
*
* Implements D-Bus method HoldHandles
* on interface org.freedesktop.Telepathy.Connection
*
* @context: The D-Bus invocation context to use to return values
* or throw an error.
*/
static void
tp_base_connection_hold_handles (TpSvcConnection *iface,
guint handle_type,
const GArray *handles,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (iface);
TpBaseConnectionPrivate *priv;
GError *error = NULL;
gchar *sender;
g_assert (TP_IS_BASE_CONNECTION (self));
priv = self->priv;
TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (self, context);
if (!tp_handles_supported_and_valid (priv->handles,
handle_type, handles, FALSE, &error))
{
dbus_g_method_return_error (context, error);
g_error_free (error);
return;
}
sender = dbus_g_method_get_sender (context);
DEBUG ("%u handles of type %u, for %s", handles->len, handle_type, sender);
if (!tp_handles_client_hold (priv->handles[handle_type], sender,
handles, &error))
{
dbus_g_method_return_error (context, error);
g_error_free (error);
g_free (sender);
return;
}
g_free (sender);
tp_svc_connection_return_from_hold_handles (context);
}
/**
* tp_base_connection_inspect_handles
*
* Implements D-Bus method InspectHandles
* on interface org.freedesktop.Telepathy.Connection
*
* Returns: TRUE if successful, FALSE if an error was thrown.
*/
static void
tp_base_connection_inspect_handles (TpSvcConnection *iface,
guint handle_type,
const GArray *handles,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (iface);
TpBaseConnectionPrivate *priv = self->priv;
GError *error = NULL;
const gchar **ret;
guint i;
g_assert (TP_IS_BASE_CONNECTION (self));
TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (self, context);
if (!tp_handles_supported_and_valid (priv->handles,
handle_type, handles, FALSE, &error))
{
dbus_g_method_return_error (context, error);
g_error_free (error);
return;
}
ret = g_new (const gchar *, handles->len + 1);
for (i = 0; i < handles->len; i++)
{
TpHandle handle;
const gchar *tmp;
handle = g_array_index (handles, TpHandle, i);
tmp = tp_handle_inspect (priv->handles[handle_type], handle);
g_assert (tmp != NULL);
ret[i] = tmp;
}
ret[i] = NULL;
tp_svc_connection_return_from_inspect_handles (context, ret);
g_free (ret);
}
#undef DEBUG_FLAG
#define DEBUG_FLAG TP_DEBUG_CONNECTION
#include "telepathy-glib/debug-internal.h"
/**
* list_channel_factory_foreach_one:
* @chan: a channel
* @data: a GPtrArray in which channel information should be stored
*
* Called by the exported ListChannels function for each channel in a channel
* factory, this should add to the GPtrArray (in the data pointer) a
* GValueArray containing the following:
* a D-Bus object path for the channel object on this service
* a D-Bus interface name representing the channel type
* an integer representing the handle type this channel communicates with,
* or zero
* an integer handle representing the contact, room or list this channel
* communicates with, or zero
*/
static void
list_channel_factory_foreach_one (TpChannelIface *chan,
gpointer data)
{
GObject *channel = G_OBJECT (chan);
GPtrArray *values = (GPtrArray *) data;
gchar *path, *type;
guint handle_type, handle;
GValue *entry = tp_dbus_specialized_value_slice_new
(TP_STRUCT_TYPE_CHANNEL_INFO);
g_object_get (channel,
"object-path", &path,
"channel-type", &type,
"handle-type", &handle_type,
"handle", &handle,
NULL);
dbus_g_type_struct_set (entry,
0, path,
1, type,
2, handle_type,
3, handle,
G_MAXUINT);
g_ptr_array_add (values, entry);
g_free (path);
g_free (type);
}
/**
* list_channel_manager_foreach_one:
* @chan: a channel
* @data: a GPtrArray in which channel information should be stored
*
* Called by the exported ListChannels function for each channel in a channel
* manager, this should add to the GPtrArray (in the data pointer) a
* GValueArray containing the following:
* a D-Bus object path for the channel object on this service
* a D-Bus interface name representing the channel type
* an integer representing the handle type this channel communicates with,
* or zero
* an integer handle representing the contact, room or list this channel
* communicates with, or zero
*/
static void
list_channel_manager_foreach_one (TpExportableChannel *channel,
gpointer data)
{
GPtrArray *values = (GPtrArray *) data;
gchar *path, *type;
guint handle_type, handle;
GValue *entry = tp_dbus_specialized_value_slice_new
(TP_STRUCT_TYPE_CHANNEL_INFO);
g_assert (TP_IS_EXPORTABLE_CHANNEL (channel));
exportable_channel_get_old_info (channel, &path, &type, &handle_type,
&handle);
dbus_g_type_struct_set (entry,
0, path,
1, type,
2, handle_type,
3, handle,
G_MAXUINT);
g_ptr_array_add (values, entry);
g_free (path);
g_free (type);
}
static void
tp_base_connection_list_channels (TpSvcConnection *iface,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (iface);
TpBaseConnectionPrivate *priv;
GPtrArray *channels, *values;
guint i;
g_assert (TP_IS_BASE_CONNECTION (self));
priv = self->priv;
TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (self, context);
/* I think on average, each factory will have 2 channels :D */
values = g_ptr_array_sized_new (priv->channel_factories->len * 2
+ priv->channel_managers->len * 2);
for (i = 0; i < priv->channel_factories->len; i++)
{
TpChannelFactoryIface *factory = g_ptr_array_index
(priv->channel_factories, i);
tp_channel_factory_iface_foreach (factory,
list_channel_factory_foreach_one, values);
}
for (i = 0; i < priv->channel_managers->len; i++)
{
TpChannelManager *manager = g_ptr_array_index
(priv->channel_managers, i);
tp_channel_manager_foreach_channel (manager,
list_channel_manager_foreach_one, values);
}
channels = g_ptr_array_sized_new (values->len);
for (i = 0; i < values->len; i++)
{
g_ptr_array_add (channels, g_value_get_boxed (g_ptr_array_index
(values, i)));
}
tp_svc_connection_return_from_list_channels (context, channels);
g_ptr_array_free (channels, TRUE);
for (i = 0; i < values->len; i++)
{
tp_g_value_slice_free (g_ptr_array_index (values, i));
}
g_ptr_array_free (values, TRUE);
}
/**
* tp_base_connection_request_channel
*
* Implements D-Bus method RequestChannel
* on interface org.freedesktop.Telepathy.Connection
*
* @context: The D-Bus invocation context to use to return values
* or throw an error.
*/
static void
tp_base_connection_request_channel (TpSvcConnection *iface,
const gchar *type,
guint handle_type,
guint handle,
gboolean suppress_handler,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (iface);
TpBaseConnectionPrivate *priv;
TpChannelFactoryRequestStatus status =
TP_CHANNEL_FACTORY_REQUEST_STATUS_NOT_IMPLEMENTED;
GError *error = NULL;
guint i;
ChannelRequest *request;
GHashTable *request_properties;
gboolean claimed_by_channel_manager = FALSE;
g_assert (TP_IS_BASE_CONNECTION (self));
priv = self->priv;
TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (self, context);
if (handle_type == TP_HANDLE_TYPE_NONE)
{
if (handle != 0)
{
GError e = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
"When handle type is NONE, handle must be 0" };
dbus_g_method_return_error (context, &e);
return;
}
}
else
{
TpHandleRepoIface *handle_repo = tp_base_connection_get_handles (self,
handle_type);
if (handle_repo == NULL)
{
GError e = { TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
"Handle type not supported by this connection manager" };
dbus_g_method_return_error (context, &e);
return;
}
if (!tp_handle_is_valid (handle_repo, handle, &error))
{
dbus_g_method_return_error (context, error);
g_error_free (error);
return;
}
}
request = channel_request_new (context, METHOD_REQUEST_CHANNEL,
type, handle_type, handle, suppress_handler);
g_ptr_array_add (priv->channel_requests, request);
/* First try the channel managers */
request_properties = tp_asv_new (
TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, type,
TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, handle_type,
NULL);
if (handle != 0)
{
g_hash_table_insert (request_properties,
TP_PROP_CHANNEL_TARGET_HANDLE,
tp_g_value_slice_new_uint (handle));
}
for (i = 0; i < priv->channel_managers->len; i++)
{
TpChannelManager *manager = TP_CHANNEL_MANAGER (
g_ptr_array_index (priv->channel_managers, i));
if (tp_channel_manager_request_channel (manager, request,
request_properties))
{
claimed_by_channel_manager = TRUE;
break;
}
}
g_hash_table_destroy (request_properties);
if (claimed_by_channel_manager)
return;
/* OK, none of them wanted it. Now try the channel factories */
for (i = 0; i < priv->channel_factories->len; i++)
{
TpChannelFactoryIface *factory = g_ptr_array_index
(priv->channel_factories, i);
TpChannelFactoryRequestStatus cur_status;
TpChannelIface *chan = NULL;
cur_status = tp_channel_factory_iface_request (factory, type,
(TpHandleType) handle_type, handle, request, &chan, &error);
switch (cur_status)
{
case TP_CHANNEL_FACTORY_REQUEST_STATUS_EXISTING:
{
g_assert (NULL != chan);
factory_satisfy_requests (self, factory, chan, request, FALSE);
/* factory_satisfy_requests should remove the request */
g_assert (!tp_g_ptr_array_contains (priv->channel_requests,
request));
return;
}
case TP_CHANNEL_FACTORY_REQUEST_STATUS_CREATED:
g_assert (NULL != chan);
/* the signal handler should have completed the queued request
* and freed the ChannelRequest already */
g_assert (!tp_g_ptr_array_contains (priv->channel_requests,
request));
return;
case TP_CHANNEL_FACTORY_REQUEST_STATUS_QUEUED:
DEBUG ("queued request, channel_type=%s, handle_type=%u, "
"handle=%u, suppress_handler=%u", type, handle_type,
handle, suppress_handler);
return;
case TP_CHANNEL_FACTORY_REQUEST_STATUS_ERROR:
/* pass through error */
goto ERROR;
default:
/* always return the most specific error */
if (cur_status > status)
status = cur_status;
}
}
switch (status)
{
case TP_CHANNEL_FACTORY_REQUEST_STATUS_INVALID_HANDLE:
DEBUG ("invalid handle %u", handle);
error = g_error_new (TP_ERRORS, TP_ERROR_INVALID_HANDLE,
"invalid handle %u", handle);
break;
case TP_CHANNEL_FACTORY_REQUEST_STATUS_NOT_AVAILABLE:
DEBUG ("requested channel is unavailable with "
"handle type %u", handle_type);
error = g_error_new (TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
"requested channel is not available with "
"handle type %u", handle_type);
break;
case TP_CHANNEL_FACTORY_REQUEST_STATUS_NOT_IMPLEMENTED:
DEBUG ("unsupported channel type %s", type);
error = g_error_new (TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED,
"unsupported channel type %s", type);
break;
default:
g_assert_not_reached ();
}
ERROR:
g_assert (error != NULL);
dbus_g_method_return_error (request->context, error);
request->context = NULL;
g_error_free (error);
g_ptr_array_remove (priv->channel_requests, request);
channel_request_free (request);
}
#undef DEBUG_FLAG
#define DEBUG_FLAG TP_DEBUG_HANDLES
#include "telepathy-glib/debug-internal.h"
/**
* tp_base_connection_release_handles
*
* Implements D-Bus method ReleaseHandles
* on interface org.freedesktop.Telepathy.Connection
*
* @context: The D-Bus invocation context to use to return values
* or throw an error.
*/
static void
tp_base_connection_release_handles (TpSvcConnection *iface,
guint handle_type,
const GArray * handles,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (iface);
TpBaseConnectionPrivate *priv = self->priv;
char *sender;
GError *error = NULL;
g_assert (TP_IS_BASE_CONNECTION (self));
TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (self, context);
if (!tp_handles_supported_and_valid (priv->handles,
handle_type, handles, FALSE, &error))
{
dbus_g_method_return_error (context, error);
g_error_free (error);
return;
}
sender = dbus_g_method_get_sender (context);
DEBUG ("%u handles of type %u, for %s", handles->len, handle_type, sender);
if (!tp_handles_client_release (priv->handles[handle_type],
sender, handles, &error))
{
dbus_g_method_return_error (context, error);
g_error_free (error);
g_free (sender);
return;
}
g_free (sender);
tp_svc_connection_return_from_release_handles (context);
}
/**
* tp_base_connection_dbus_request_handles:
* @iface: A pointer to #TpBaseConnection, cast to a pointer to
* #TpSvcConnection
* @handle_type: The handle type (#TpHandleType) as a guint
* @names: A strv of handle names
* @context: The dbus-glib method invocation context
*
* Implements D-Bus method RequestHandles on interface
* org.freedesktop.Telepathy.Connection. Exported so subclasses can
* use it as a basis for their own implementations (for instance,
* at the time of writing Gabble's GabbleConnection does its own processing
* for room handles, in order to validate them asynchronously, but delegates
* to this implementation for all other types).
*/
void
tp_base_connection_dbus_request_handles (TpSvcConnection *iface,
guint handle_type,
const gchar **names,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (iface);
TpHandleRepoIface *handle_repo = tp_base_connection_get_handles (self,
handle_type);
guint count = 0, i;
const gchar **cur_name;
GError *error = NULL;
GArray *handles = NULL;
gchar *sender;
g_return_if_fail (TP_IS_BASE_CONNECTION (self));
TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (self, context);
for (cur_name = names; *cur_name != NULL; cur_name++)
{
count++;
}
if (!tp_handle_type_is_valid (handle_type, &error))
{
g_assert (error != NULL);
goto out;
}
if (handle_repo == NULL)
{
DEBUG ("unimplemented handle type %u", handle_type);
error = g_error_new (TP_ERRORS, TP_ERROR_NOT_IMPLEMENTED,
"unimplemented handle type %u", handle_type);
goto out;
}
handles = g_array_sized_new (FALSE, FALSE, sizeof (guint), count);
for (i = 0; i < count; i++)
{
TpHandle handle;
const gchar *name = names[i];
handle = tp_handle_ensure (handle_repo, name, NULL, &error);
if (handle == 0)
{
DEBUG("RequestHandles of type %d failed because '%s' is invalid: %s",
handle_type, name, error->message);
g_assert (error != NULL);
goto out;
}
g_array_append_val (handles, handle);
}
sender = dbus_g_method_get_sender (context);
DEBUG ("%u handles of type %u, for %s", handles->len, handle_type, sender);
if (!tp_handles_client_hold (handle_repo, sender, handles, &error))
{
g_assert (error != NULL);
}
g_free (sender);
out:
if (error == NULL)
{
tp_svc_connection_return_from_request_handles (context, handles);
}
else
{
dbus_g_method_return_error (context, error);
g_error_free (error);
}
if (handles != NULL)
{
tp_handles_unref (handle_repo, handles);
g_array_free (handles, TRUE);
}
}
#undef DEBUG_FLAG
#define DEBUG_FLAG TP_DEBUG_CONNECTION
#include "telepathy-glib/debug-internal.h"
/**
* tp_base_connection_get_handles:
* @self: A connection
* @handle_type: The handle type
*
* <!---->
*
* Returns: the handle repository corresponding to the given handle type,
* or #NULL if it's unsupported or invalid.
*/
TpHandleRepoIface *
tp_base_connection_get_handles (TpBaseConnection *self,
TpHandleType handle_type)
{
g_return_val_if_fail (TP_IS_BASE_CONNECTION (self), NULL);
if (handle_type >= NUM_TP_HANDLE_TYPES)
return NULL;
return self->priv->handles[handle_type];
}
/**
* tp_base_connection_get_self_handle:
* @self: A connection
*
* Returns the #TpBaseConnection:self-handle property, which is guaranteed not
* to be 0 once the connection has moved to the CONNECTED state.
*
* Returns: the current self handle of the connection.
*
* Since: 0.7.15
*/
TpHandle
tp_base_connection_get_self_handle (TpBaseConnection *self)
{
g_return_val_if_fail (TP_IS_BASE_CONNECTION (self), 0);
return self->self_handle;
}
/**
* tp_base_connection_set_self_handle:
* @self: A connection
* @self_handle: The new self handle for the connection.
*
* Sets the #TpBaseConnection:self-handle property. self_handle may not be 0
* once the connection has moved to the CONNECTED state.
*
* Since: 0.7.15
*/
void
tp_base_connection_set_self_handle (TpBaseConnection *self,
TpHandle self_handle)
{
g_object_set (self, "self-handle", self_handle, NULL);
}
/**
* tp_base_connection_finish_shutdown:
* @self: The connection
*
* Tell the connection manager that this Connection has been disconnected,
* has emitted StatusChanged and is ready to be removed from D-Bus.
*/
void tp_base_connection_finish_shutdown (TpBaseConnection *self)
{
GPtrArray *contexts;
guint i;
g_return_if_fail (TP_IS_BASE_CONNECTION (self));
g_return_if_fail (self->status == TP_CONNECTION_STATUS_DISCONNECTED);
g_return_if_fail (self->priv->disconnect_requests != NULL);
contexts = self->priv->disconnect_requests;
self->priv->disconnect_requests = NULL;
for (i = 0; i < contexts->len; i++)
{
tp_svc_connection_return_from_disconnect (g_ptr_array_index (contexts,
i));
}
g_ptr_array_free (contexts, TRUE);
g_signal_emit (self, signals[SHUTDOWN_FINISHED], 0);
}
/**
* tp_base_connection_disconnect_with_dbus_error:
* @self: The connection
* @error_name: The D-Bus error with which the connection changed status to
* Disconnected
* @details: Further details of the error, as a hash table where the keys
* are strings as defined in the Telepathy specification, and the
* values are GValues. %NULL is allowed, and treated as empty.
* @reason: The reason code to use in the StatusChanged signal
* (a less specific, non-extensible version of @error_name)
*
* Change the status of the connection to %TP_CONNECTION_STATUS_DISCONNECTED,
* as if by a call to tp_base_connection_change_status(). Before doing so,
* emit the ConnectionError D-Bus signal to give more details of the error.
*
* @details may contain, among other entries, the well-known key
* "debug-message", whose value should have type G_TYPE_STRING.
*
* Since: 0.7.24
*/
void
tp_base_connection_disconnect_with_dbus_error (TpBaseConnection *self,
const gchar *error_name,
GHashTable *details,
TpConnectionStatusReason reason)
{
GHashTable *dup = NULL;
g_return_if_fail (TP_IS_BASE_CONNECTION (self));
g_return_if_fail (tp_dbus_check_valid_interface_name (error_name, NULL));
if (details == NULL)
{
dup = g_hash_table_new (g_str_hash, g_str_equal);
details = dup;
}
tp_svc_connection_emit_connection_error (self, error_name, details);
tp_base_connection_change_status (self, TP_CONNECTION_STATUS_DISCONNECTED,
reason);
if (dup != NULL)
g_hash_table_destroy (dup);
}
/**
* tp_base_connection_change_status:
* @self: The connection
* @status: The new status
* @reason: The reason for the status change
*
* Change the status of the connection. The allowed state transitions are:
*
* <itemizedlist>
* <listitem>NEW -> CONNECTING</listitem>
* <listitem>CONNECTING -> CONNECTED</listitem>
* <listitem>NEW -> CONNECTED (equivalent to both of the above one after the
* other - see below)</listitem>
* <listitem>(anything except DISCONNECTED) -> DISCONNECTED</listitem>
* </itemizedlist>
*
* Before the transition to CONNECTED, the implementation must have discovered
* the handle for the local user, obtained a reference to that handle and
* stored it in the @self_handle member of #TpBaseConnection.
*
* Changing from NEW to CONNECTED is implemented by doing the transition from
* NEW to CONNECTING, followed by the transition from CONNECTING to CONNECTED;
* it's exactly equivalent to calling tp_base_connection_change_status for
* those two transitions one after the other.
*
* Any other valid transition does the following, in this order:
*
* <itemizedlist>
* <listitem>Update the @status member of #TpBaseConnection</listitem>
* <listitem>If the new state is DISCONNECTED, call the close_all_channels
* callback on all channel factories</listitem>
* <listitem>Emit the D-Bus StatusChanged signal</listitem>
* <listitem>Call the subclass' status change callback</listitem>
* <listitem>Call the channel factories' status change callbacks</listitem>
* <listitem>If the new state is DISCONNECTED, call the subclass'
* @shut_down callback</listitem>
* </itemizedlist>
*
* Changed in 0.7.35: the @self_handle member of #TpBaseConnection was
* previously set to 0 at this stage. It now remains non-zero until the object
* is disposed.
*/
void
tp_base_connection_change_status (TpBaseConnection *self,
TpConnectionStatus status,
TpConnectionStatusReason reason)
{
TpBaseConnectionPrivate *priv;
TpBaseConnectionClass *klass;
TpConnectionStatus prev_status;
g_assert (TP_IS_BASE_CONNECTION (self));
priv = self->priv;
klass = TP_BASE_CONNECTION_GET_CLASS (self);
if (self->status == TP_INTERNAL_CONNECTION_STATUS_NEW
&& status == TP_CONNECTION_STATUS_CONNECTED)
{
/* going straight from NEW to CONNECTED would cause confusion, so before
* we do anything else, go via CONNECTING */
DEBUG("from NEW to CONNECTED: going via CONNECTING first");
tp_base_connection_change_status (self, TP_CONNECTION_STATUS_CONNECTING,
reason);
}
DEBUG("was %u, now %u, for reason %u", self->status, status, reason);
g_return_if_fail (status != TP_INTERNAL_CONNECTION_STATUS_NEW);
if (self->status == status)
{
g_warning ("%s: attempted to re-emit the current status %u, reason %u",
G_STRFUNC, status, reason);
return;
}
prev_status = self->status;
self->status = status;
/* make appropriate assertions about our state */
switch (status)
{
case TP_CONNECTION_STATUS_DISCONNECTED:
/* you can go from any state to DISCONNECTED, except DISCONNECTED;
* and we already warned and returned if that was the case, so
* nothing to do here */
break;
case TP_CONNECTION_STATUS_CONNECTED:
/* you can only go to CONNECTED if you're CONNECTING (or NEW, but we
* covered that by forcing a transition to CONNECTING above) */
g_return_if_fail (prev_status == TP_CONNECTION_STATUS_CONNECTING);
/* by the time we go CONNECTED we must have the self handle */
g_return_if_fail (self->self_handle != 0);
break;
case TP_CONNECTION_STATUS_CONNECTING:
/* you can't go CONNECTING if a connection attempt has been made
* before */
g_return_if_fail (prev_status == TP_INTERNAL_CONNECTION_STATUS_NEW);
break;
default:
g_warning ("%s: invalid connection status %d", G_STRFUNC, status);
g_assert_not_reached ();
return;
}
if (status == TP_CONNECTION_STATUS_DISCONNECTED)
{
/* the presence of this array indicates that we are shutting down */
if (self->priv->disconnect_requests == NULL)
self->priv->disconnect_requests = g_ptr_array_sized_new (0);
/* remove all channels and shut down all factories, so we don't get
* any race conditions where method calls are delivered to a channel
* after we've started disconnecting
*/
tp_base_connection_close_all_channels (self);
}
DEBUG("emitting status-changed to %u, for reason %u", status, reason);
tp_svc_connection_emit_status_changed (self, status, reason);
/* tell subclass and factories about the state change. In the case of
* disconnection, shut down afterwards */
switch (status)
{
case TP_CONNECTION_STATUS_CONNECTING:
if (klass->connecting)
(klass->connecting) (self);
g_ptr_array_foreach (priv->channel_factories, (GFunc)
tp_channel_factory_iface_connecting, NULL);
break;
case TP_CONNECTION_STATUS_CONNECTED:
/* the implementation should have ensured we have a valid self_handle
* before changing the state to CONNECTED */
g_assert (self->self_handle != 0);
g_assert (tp_handle_is_valid (priv->handles[TP_HANDLE_TYPE_CONTACT],
self->self_handle, NULL));
if (klass->connected)
(klass->connected) (self);
g_ptr_array_foreach (priv->channel_factories, (GFunc)
tp_channel_factory_iface_connected, NULL);
break;
case TP_CONNECTION_STATUS_DISCONNECTED:
/* cancel all queued channel requests that weren't already cancelled by
* the channel managers.
*/
if (priv->channel_requests->len > 0)
{
g_ptr_array_foreach (priv->channel_requests, (GFunc)
channel_request_cancel, NULL);
g_ptr_array_remove_range (priv->channel_requests, 0,
priv->channel_requests->len);
}
if (prev_status != TP_INTERNAL_CONNECTION_STATUS_NEW)
{
if (klass->disconnected)
(klass->disconnected) (self);
g_ptr_array_foreach (priv->channel_factories, (GFunc)
tp_channel_factory_iface_disconnected, NULL);
}
(klass->shut_down) (self);
break;
default:
g_assert_not_reached ();
}
}
/**
* tp_base_connection_add_interfaces:
* @self: A TpBaseConnection in state #TP_INTERNAL_CONNECTION_STATUS_NEW
* or #TP_CONNECTION_STATUS_CONNECTING
* @interfaces: A %NULL-terminated array of D-Bus interface names, which
* must remain valid at least until the connection enters state
* #TP_CONNECTION_STATUS_DISCONNECTED (in practice, you should either
* use static strings, or use strdup'd strings and free them in the dispose
* callback).
*
* Add some interfaces to the list supported by this Connection. If you're
* going to call this function at all, you must do so before moving to state
* CONNECTED (or DISCONNECTED); if you don't call it, only the set of
* interfaces always present (@interfaces_always_present in
* #TpBaseConnectionClass) will be supported.
*/
void
tp_base_connection_add_interfaces (TpBaseConnection *self,
const gchar **interfaces)
{
guint i, n_new;
TpBaseConnectionPrivate *priv = self->priv;
TpBaseConnectionClass *klass = TP_BASE_CONNECTION_GET_CLASS (self);
g_return_if_fail (TP_IS_BASE_CONNECTION (self));
g_return_if_fail (self->status != TP_CONNECTION_STATUS_CONNECTED);
g_return_if_fail (self->status != TP_CONNECTION_STATUS_DISCONNECTED);
if (interfaces == NULL || interfaces[0] == NULL)
{
/* If user tries to add no new interfaces, ignore it */
return;
}
n_new = g_strv_length ((gchar **) interfaces);
if (priv->interfaces)
{
guint size = priv->interfaces->len;
g_array_set_size (priv->interfaces, size + n_new);
for (i = 0; i < n_new; i++)
{
g_array_index (priv->interfaces, const gchar *, size + i) =
interfaces[i];
}
}
else
{
/* It's the first time anyone has added interfaces - create the array */
guint n_static = 0;
if (klass->interfaces_always_present)
{
n_static = g_strv_length (
(gchar **) klass->interfaces_always_present);
}
priv->interfaces = g_array_sized_new (TRUE, FALSE, sizeof (gchar *),
n_static + n_new);
for (i = 0; i < n_static; i++)
{
g_array_append_val (priv->interfaces,
klass->interfaces_always_present[i]);
}
for (i = 0; i < n_new; i++)
{
g_array_append_val (priv->interfaces, interfaces[i]);
}
}
}
static void
conn_iface_init (gpointer g_iface, gpointer iface_data)
{
TpSvcConnectionClass *klass = g_iface;
#define IMPLEMENT(prefix,x) tp_svc_connection_implement_##x (klass, \
tp_base_connection_##prefix##x)
IMPLEMENT(,connect);
IMPLEMENT(,disconnect);
IMPLEMENT(,get_interfaces);
IMPLEMENT(,get_protocol);
IMPLEMENT(dbus_,get_self_handle);
IMPLEMENT(,get_status);
IMPLEMENT(,hold_handles);
IMPLEMENT(,inspect_handles);
IMPLEMENT(,list_channels);
IMPLEMENT(,request_channel);
IMPLEMENT(,release_handles);
IMPLEMENT(dbus_,request_handles);
#undef IMPLEMENT
}
/* The handling of calls to Connection.Interface.Requests.CreateChannel is
* split into three chained functions, which each call the next function in
* the chain unless an error has occured.
*/
static void conn_requests_check_basic_properties (TpBaseConnection *self,
GHashTable *requested_properties, ChannelRequestMethod method,
DBusGMethodInvocation *context);
static void
conn_requests_requestotron_validate_handle (TpBaseConnection *self,
GHashTable *requested_properties, ChannelRequestMethod method,
const gchar *type, TpHandleType target_handle_type,
TpHandle target_handle, const gchar *target_id,
DBusGMethodInvocation *context);
static void conn_requests_offer_request (TpBaseConnection *self,
GHashTable *requested_properties, ChannelRequestMethod method,
const gchar *type, TpHandleType target_handle_type,
TpHandle target_handle, DBusGMethodInvocation *context);
#define RETURN_INVALID_ARGUMENT(message) \
G_STMT_START { \
GError e = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT, message }; \
dbus_g_method_return_error (context, &e); \
return; \
} G_STMT_END
static void
conn_requests_requestotron (TpBaseConnection *self,
GHashTable *requested_properties,
ChannelRequestMethod method,
DBusGMethodInvocation *context)
{
TP_BASE_CONNECTION_ERROR_IF_NOT_CONNECTED (self, context);
/* Call the first function in the chain handling incoming requests; it will
* call the next steps.
*/
conn_requests_check_basic_properties (self, requested_properties, method,
context);
}
static void
conn_requests_check_basic_properties (TpBaseConnection *self,
GHashTable *requested_properties,
ChannelRequestMethod method,
DBusGMethodInvocation *context)
{
/* Step 1:
* Check that ChannelType, TargetHandleType, TargetHandle, TargetID have
* the correct types, and that ChannelType is not omitted.
*/
const gchar *type;
TpHandleType target_handle_type;
TpHandle target_handle;
const gchar *target_id;
gboolean valid;
type = tp_asv_get_string (requested_properties,
TP_PROP_CHANNEL_CHANNEL_TYPE);
if (type == NULL)
RETURN_INVALID_ARGUMENT ("ChannelType is required");
target_handle_type = tp_asv_get_uint32 (requested_properties,
TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, &valid);
/* Allow TargetHandleType to be missing, but not to be otherwise broken */
if (!valid && tp_asv_lookup (requested_properties,
TP_PROP_CHANNEL_TARGET_HANDLE_TYPE) != NULL)
RETURN_INVALID_ARGUMENT (
"TargetHandleType must be an integer in range 0 to 2**32-1");
target_handle = tp_asv_get_uint32 (requested_properties,
TP_PROP_CHANNEL_TARGET_HANDLE, &valid);
/* Allow TargetHandle to be missing, but not to be otherwise broken */
if (!valid && tp_asv_lookup (requested_properties,
TP_PROP_CHANNEL_TARGET_HANDLE) != NULL)
RETURN_INVALID_ARGUMENT (
"TargetHandle must be an integer in range 1 to 2**32-1");
/* TargetHandle may not be 0 */
if (valid && target_handle == 0)
RETURN_INVALID_ARGUMENT ("TargetHandle may not be 0");
target_id = tp_asv_get_string (requested_properties,
TP_PROP_CHANNEL_TARGET_ID);
/* Allow TargetID to be missing, but not to be otherwise broken */
if (target_id == NULL && tp_asv_lookup (requested_properties,
TP_PROP_CHANNEL_TARGET_ID) != NULL)
RETURN_INVALID_ARGUMENT ("TargetID must be a string");
if (tp_asv_lookup (requested_properties, TP_PROP_CHANNEL_INITIATOR_HANDLE)
!= NULL)
RETURN_INVALID_ARGUMENT ("InitiatorHandle may not be requested");
if (tp_asv_lookup (requested_properties, TP_PROP_CHANNEL_INITIATOR_ID)
!= NULL)
RETURN_INVALID_ARGUMENT ("InitiatorID may not be requested");
if (tp_asv_lookup (requested_properties, TP_PROP_CHANNEL_REQUESTED)
!= NULL)
RETURN_INVALID_ARGUMENT ("Requested may not be requested");
conn_requests_requestotron_validate_handle (self,
requested_properties, method,
type, target_handle_type, target_handle, target_id,
context);
}
/*
* @target_handle: non-zero if a TargetHandle property was in the request;
* zero if TargetHandle was not in the request.
*/
static void
conn_requests_requestotron_validate_handle (TpBaseConnection *self,
GHashTable *requested_properties,
ChannelRequestMethod method,
const gchar *type,
TpHandleType target_handle_type,
TpHandle target_handle,
const gchar *target_id,
DBusGMethodInvocation *context)
{
/* Step 2: Validate the supplied set of Handle properties */
TpHandleRepoIface *handles = NULL;
GHashTable *altered_properties = NULL;
GValue *target_handle_value = NULL;
/* Handle type 0 cannot have a handle */
if (target_handle_type == TP_HANDLE_TYPE_NONE && target_handle != 0)
RETURN_INVALID_ARGUMENT (
"When TargetHandleType is NONE, TargetHandle must be omitted");
/* Handle type 0 cannot have a target id */
if (target_handle_type == TP_HANDLE_TYPE_NONE && target_id != NULL)
RETURN_INVALID_ARGUMENT (
"When TargetHandleType is NONE, TargetID must be omitted");
if (target_handle_type != TP_HANDLE_TYPE_NONE)
{
GError *error = NULL;
if (target_handle == 0 && target_id == NULL)
RETURN_INVALID_ARGUMENT ("When TargetHandleType is not None, either "
"TargetHandle or TargetID must also be given");
if (target_handle != 0 && target_id != NULL)
RETURN_INVALID_ARGUMENT (
"TargetHandle and TargetID must not both be given");
handles = tp_base_connection_get_handles (self, target_handle_type);
if (handles == NULL)
{
GError e = { TP_ERRORS, TP_ERROR_NOT_AVAILABLE,
"Handle type not supported by this connection manager" };
dbus_g_method_return_error (context, &e);
return;
}
if (target_handle == 0)
{
/* Turn TargetID into TargetHandle */
target_handle = tp_handle_ensure (handles, target_id, NULL, &error);
if (target_handle == 0)
{
/* tp_handle_ensure can return any error in any domain; force
* the domain and code to be as documented for CreateChannel.
*/
error->domain = TP_ERRORS;
error->code = TP_ERROR_INVALID_HANDLE;
dbus_g_method_return_error (context, error);
g_error_free (error);
return;
}
altered_properties = g_hash_table_new_full (g_str_hash, g_str_equal,
NULL, NULL);
tp_g_hash_table_update (altered_properties, requested_properties,
NULL, NULL);
target_handle_value = tp_g_value_slice_new_uint (target_handle);
g_hash_table_insert (altered_properties,
TP_PROP_CHANNEL_TARGET_HANDLE, target_handle_value);
g_hash_table_remove (altered_properties,
TP_PROP_CHANNEL_TARGET_ID);
requested_properties = altered_properties;
}
else
{
/* Check the supplied TargetHandle is valid */
if (!tp_handle_is_valid (handles, target_handle, &error))
{
error->domain = TP_ERRORS;
error->code = TP_ERROR_INVALID_HANDLE;
dbus_g_method_return_error (context, error);
g_error_free (error);
return;
}
tp_handle_ref (handles, target_handle);
}
}
conn_requests_offer_request (self, requested_properties, method, type,
target_handle_type, target_handle, context);
/* If HandleType was not None, we got the relevant repo and took a reference
* to target_handle; release it before returning
*/
if (handles != NULL)
{
tp_handle_unref (handles, target_handle);
}
/* If we made a new table, we should destroy it, and the GValue holding
* TargetHandle. The other GValues are borrowed from the supplied
* requested_properties table.
*/
if (altered_properties != NULL)
{
g_hash_table_destroy (altered_properties);
tp_g_value_slice_free (target_handle_value);
}
}
static void
conn_requests_offer_request (TpBaseConnection *self,
GHashTable *requested_properties,
ChannelRequestMethod method,
const gchar *type,
TpHandleType target_handle_type,
TpHandle target_handle,
DBusGMethodInvocation *context)
{
/* Step 3: offer the incoming, vaguely sanitized request to the channel
* managers.
*/
TpBaseConnectionPrivate *priv = self->priv;
TpChannelManagerRequestFunc func;
ChannelRequest *request;
gboolean suppress_handler;
guint i;
switch (method)
{
case METHOD_CREATE_CHANNEL:
func = tp_channel_manager_create_channel;
suppress_handler = TRUE;
break;
case METHOD_ENSURE_CHANNEL:
func = tp_channel_manager_ensure_channel;
suppress_handler = FALSE;
break;
default:
g_assert_not_reached ();
}
request = channel_request_new (context, method,
type, target_handle_type, target_handle, suppress_handler);
g_ptr_array_add (priv->channel_requests, request);
for (i = 0; i < priv->channel_managers->len; i++)
{
TpChannelManager *manager = TP_CHANNEL_MANAGER (
g_ptr_array_index (priv->channel_managers, i));
if (func (manager, request, requested_properties))
return;
}
/* Nobody accepted the request */
tp_dbus_g_method_return_not_implemented (context);
request->context = NULL;
g_ptr_array_remove (priv->channel_requests, request);
channel_request_free (request);
}
static void
conn_requests_create_channel (TpSvcConnectionInterfaceRequests *svc,
GHashTable *requested_properties,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (svc);
conn_requests_requestotron (self, requested_properties,
METHOD_CREATE_CHANNEL, context);
}
static void
conn_requests_ensure_channel (TpSvcConnectionInterfaceRequests *svc,
GHashTable *requested_properties,
DBusGMethodInvocation *context)
{
TpBaseConnection *self = TP_BASE_CONNECTION (svc);
conn_requests_requestotron (self, requested_properties,
METHOD_ENSURE_CHANNEL, context);
}
static void
requests_iface_init (gpointer g_iface,
gpointer iface_data G_GNUC_UNUSED)
{
TpSvcConnectionInterfaceRequestsClass *iface = g_iface;
#define IMPLEMENT(x) \
tp_svc_connection_interface_requests_implement_##x (\
iface, conn_requests_##x)
IMPLEMENT (create_channel);
IMPLEMENT (ensure_channel);
#undef IMPLEMENT
}
/**
* tp_base_connection_channel_manager_iter_init:
* @iter: an uninitialized #TpChannelManagerIter
* @self: a connection
*
* Initializes an iterator over the #TpChannelManager objects known to
* @self. It is intended to be used as followed:
*
* <informalexample><programlisting>
* TpChannelManagerIter iter;
* TpChannelManager *manager;
*
* tp_base_connection_channel_manager_iter_init (&iter, base_conn);
* while (tp_base_connection_channel_manager_iter_next (&iter, &manager))
* {
* ...do something with manager...
* }
* </programlisting></informalexample>
*
* Since: 0.7.15
*/
void
tp_base_connection_channel_manager_iter_init (TpChannelManagerIter *iter,
TpBaseConnection *self)
{
g_return_if_fail (TP_IS_BASE_CONNECTION (self));
iter->self = self;
iter->index = 0;
}
/**
* tp_base_connection_channel_manager_iter_next:
* @iter: an initialized #TpChannelManagerIter
* @manager_out: a location to store the channel manager, or %NULL.
*
* Advances @iter, and retrieves the #TpChannelManager it now points to. If
* there are no more channel managers, @manager_out is not set and %FALSE is
* returned.
*
* Returns: %FALSE if there are no more channel managers; else %TRUE.
*
* Since: 0.7.15
*/
gboolean
tp_base_connection_channel_manager_iter_next (TpChannelManagerIter *iter,
TpChannelManager **manager_out)
{
TpBaseConnectionPrivate *priv;
/* Check the caller initialized the iterator properly. */
g_assert (TP_IS_BASE_CONNECTION (iter->self));
priv = iter->self->priv;
/* Be noisy if something's gone really wrong */
g_return_val_if_fail (iter->index <= priv->channel_managers->len, FALSE);
if (iter->index == priv->channel_managers->len)
return FALSE;
if (manager_out != NULL)
*manager_out = TP_CHANNEL_MANAGER (
g_ptr_array_index (priv->channel_managers, iter->index));
iter->index++;
return TRUE;
}
static void
tp_base_connection_fill_contact_attributes (GObject *obj,
const GArray *contacts, GHashTable *attributes_hash)
{
TpBaseConnection *self = TP_BASE_CONNECTION (obj);
TpBaseConnectionPrivate *priv = self->priv;
guint i;
for (i = 0; i < contacts->len; i++)
{
TpHandle handle;
const gchar *tmp;
handle = g_array_index (contacts, TpHandle, i);
tmp = tp_handle_inspect (priv->handles[TP_HANDLE_TYPE_CONTACT], handle);
g_assert (tmp != NULL);
tp_contacts_mixin_set_contact_attribute (attributes_hash,
handle, TP_IFACE_CONNECTION"/contact-id",
tp_g_value_slice_new_string (tmp));
}
}
/**
* tp_base_connection_register_with_contacts_mixin:
* @self: An instance of the #TpBaseConnections that uses the Contacts
* mixin
*
* Register the Connection interface with the Contacts interface to make it
* inspectable. The Contacts mixin should be initialized before this function
* is called
*/
void
tp_base_connection_register_with_contacts_mixin (TpBaseConnection *self)
{
g_return_if_fail (TP_IS_BASE_CONNECTION (self));
tp_contacts_mixin_add_contact_attributes_iface (G_OBJECT (self),
TP_IFACE_CONNECTION,
tp_base_connection_fill_contact_attributes);
}
|