File: NSPasteboard.m

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

   <abstract>Implementation of class for communicating with the
			pasteboard server.</abstract>

   Copyright (C) 1997,1999,2003 Free Software Foundation, Inc.

   Author: Richard Frith-Macdonald <richard@brainstorm.co.uk>
   Date: 1997
   
   This file is part of the GNUstep GUI Library.

   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 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; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.


<chapter>
  <heading>The pasteboard system</heading>
  <p>
    The pasteboard system is the core of OpenStep inter-application
    communications.  This chapter is concerned with the use of the system,
    for detailed reference see the [NSPasteboard] class.<br />
    For non-standard services provided by applications (ie those which
    do not fit the general <em>services</em> mechanism described below),
    you generally use the Distributed Objects system (see [NSConnection])
    directly, and some hints about that are provided at the end of this
    chapter.
  </p>
  <section>
    <heading>Cut and Paste</heading>
    <p>
      The most obvious use of the pasteboard system is to support cut and
      paste of text and other data, permitting the user to take selected
      information from a document open in an application, and move it
      around in the same document, or to another document open in the same
      application, or to a document open in another application entirely.
    </p>
    <p>
      While some objects (eg instances of [NSText]) will handle cut and
      paste for you automatically, you may often need to do this yourself
      in your own classes.  The mechanism for this is quite simple, and
      should be done in a method called when the user selects the
      <em>Cut</em> or <em>Copy</em> item on the <em>Edit</em> menu.<br />
      The methods to do this should be called <em>cut:</em> and <em>copy:</em>
      respectively, and will be called automatically when the menu items
      are selected.
    </p>
    <list>
      <item>
        <strong>Select a pasteboard to use</strong><br />
        There some standard pasteboards, or you can obtain or create other
	ones with the [NSPasteboard+pasteboardWithName:] method.<br />
	Usually you will want to use a standard pasteboard such as
        the one returned by the [NSPasteboard+generalPasteboard] method.
<example>
  NSPasteboard *pb = [NSPasteboard generalPasteboard];
</example>
      </item>
      <item>
	<strong>Declare ownership and types</strong><br />
	When you are going to supply data for pasting, you must take
	ownership of the pasteboard and specify what types of data can
        be provided.  If you are going to place the data on the pasteboard
        immediately, you don't need to set a pasteboard owner, but if
        you plan to supply the data <em>lazily</em> (ie on-demand), you
        need to specify an object which the system can ask to provide
        the data when it needs it.  In either case, you need to say what
        kinds of data the pasteboard will supply, and you use the
	[NSPasteboard-declareTypes:owner:] method to do this.
<example>
  // Provide string data immediately.
  [pb declareTypes: [NSArray arrayWithObject: NSStringPboardType]
	     owner: nil];
  [pb setString: myString forType: NSStringPboardType];
</example>
      </item>
      <item>
	<strong>Provide data for pasting</strong><br />
	If you decided to provide data lazily (recommended) then the
	pasteboard owner you declared will be asked to provide the
	data when it is needed for pasting.
<example>
- (void) pasteboard: (NSPasteboard*)pb provideDataForType: (NSString*)type
{
  // Place the data needed for pasting onto the pasteboard.
  [pb setData: data forType: type];
}
</example>
      </item>
      <item>
	<strong>Support multiple types</strong><br />
	Normally, it is best to support pasting of multiple types of data
	so that the object into which the data is being pasted can handle
        the pasted information readily.  To do this it is conventional to
        supply data in the <em>richest</em> possible format in the cut:
	or copy: method, and supply other forms of data lazily.
<example>
// Supply RTF data to the pasteboard system.
- (id) copy: (id)sender
{
  NSPasteboard *pb = [NSPasteboard generalPasteboard];
  [pb declareTypes: [NSArray arrayWithObjects: NSRTFPboardType,
    NSStringPboardType, nil]
	     owner: nil];
  [pb setData: myData forType: NSRTFPboardType];
}
</example>
	The providing object can retrieve the data initially stored in the
        pasteboard, and set the type of data actually needed.
<example>
- (void) pasteboard: (NSPasteboard*)pb provideDataForType: (NSString*)type
{
  if ([type isEqualToString: NSStringPboardType] == YES)
    {
      NSData *d = [pb dataForType: NSRTFPboardType];
      NSString *s = [self convertToString: d];
      [pb setString: s forType: NSStringPboardType];
    }
  else
    {
      // Unsupported type ... should not happen
      [pb setData: nil forType: type];
    }
}
</example>
      </item>
    </list>
    <p>
      Similarly, when the user selects the <em>Paste</em> item on the
      <em>Edit</em> menu, the <em>paste:</em> method in your code will
      be called, and this method should retrieve data from the pasteboard
      and insert it into your custom object so that the user can see it.
    </p>
    <list>
      <item>
	<strong>Retrieve data from pasteboard</strong>
	<example>
- (id) paste: (id)sender
{
  NSPasteboard *pb = [NSPasteboard generalPasteboard];
  NSString *info = [pb stringForType: NSStringPboardType];
  // Now make use of info
  return self;
}
	</example>
      </item>
    </list>
  </section>
  <section>
    <heading>Drag and Drop</heading>
    <p>
      The drag and drop system for transferring data is in essence a simple
      extension of copy and paste, where the data being dragged is a
      copy of some initially selected data, and the location to which it is
      pasted depends on where it is dropped.<br />
      To support drag and drop, you use a few standard methods to interact
      with pasteboards, but you need to extend this with DnD specific methods
      to handle the drag and drop process.
    </p>
  </section>
  <section>
    <heading>Services</heading>
    <p>
      The services system provides a standardised mechanism for an application
      to provide services to other applications.  Like cut and paste, or
      drag and drop, the use of an application service is normally initiated
      by the user selecting some data to work with.  The user then goes to
      the services menu, and selects a service listed there.  The selection
      of a menu item causes the data to be placed on a pasteboard and
      transferred to the service providing application, where the action of
      the service is performed on it, and resulting data transferred back
      to the original system via the pasteboard system again.
    </p>
    <p>
      To make use of a service then, you typically need to make <em>no</em>
      changes to your application, making the services facility supremely
      easy to deal with!<br />
      If however, you wish to make use of a service programmatically (rather
      than from the services menu), you can use the NSPerformService()
      function to invoke the service directly ...
    </p>
    <example>
  // Create a pasteboard and store a string in it.
  NSPasteboard *pb = [NSPasteboard pasteboardWithUniqueName];
  [pb declareTypes: [NSArray arrayWithObject: NSStringPboardType]
	     owner: nil];
  [pb setString: myString forType: NSStringPboardType];
  // Invoke a service which takes string input and produces data output.
  if (NSPerformService(@"TheServiceName", pb) == YES)
    {
      result = [pb dataForType: NSGeneralPboardType];
    }
    </example>
    <p>
      Providing a service is a bit trickier, it involves implementing a
      method to perform the service (usually in your [NSApplication-delegate]
      object) and specifying information about your service in the Info.plist
      file for your application.<br />
      When your application is installed in one of the standard locations,
      and the <em>make_services</em> tool is run to update the cache of
      services information, your service automatically becomes available
      on the services menu of every application you run.<br />
      At runtime, you use [NSApplication-setServicesProvider:] to specify
      the object which implements the method to perform the service,
      or, if you are providing the service from a process other than a
      GUI application, you use the NSRegisterServicesProvider() function.
    </p>
    <p>
      Your Info.plist should contain an array named <code>NSServices</code>
      listing all the services your application provides.  Each service
      definition should be a dictionary containing the following information -
    </p>
    <deflist>
      <term>NSSendTypes</term>
      <desc>
	This is an array containing the string values of the types of
	data that the service provider can handle (ie the types of data
	the application requesting the service may send).<br />
	The string values are the same as the standard constant names
	for these types, so the string "NSStringPboardType" would match
	the use of the <code>NSStringPboardType</code> in your code.<br />
	Similarly, the functions NSCreateFileContentsPboardType() and
	NSCreateFilenamePboardType() return types whose string values
	are found by appending the filename extension concerned to the
	strings "NSTypedFileContentsPboardType:" and
	"NSTypedFilenamesPboardType:" respectively.
      </desc>
      <term>NSReturnTypes</term>
      <desc>
	These are the types of data that the service provider may return
	and are specified in the same way as the NSSendTypes.<br />
	NB. A service must handle at least one send type or one return type,
	but it is OK to have a service which expects no input data or one
	which produces no output data.
      </desc>
      <term>NSMessage</term>
      <desc>
	This mandatory string value is the interesting part of
	the message which is sent to your service provider in
	order to perform the service.<br />
	The method in your application which does the work, must take three
	arguments and have a name formed of this value followed by
	<code>:userData:error:</code>
<example>
// If NSMessage=encryptData
- (void) encryptString: (NSPasteboard*)pboard
	      userData: (NSString*)userData
		 error: (NSString**)error;
</example>
	This method will be pass the pasteboard to use and an optional
        user data string, and must return results in the pasteboard, or
        an error message in the error argument.
      </desc>
      <term>NSPortName</term>
      <desc>
	This specifies the name of the Distributed Objects port
	(see [NSConnection] and [NSPort]) on which the service provider
	will be listening for messages.  While its value depends on how
	you register the service, it is normally the name of the application
	providing the service.  This information is required in order for
	other applications to know how to contact the service provider.
      </desc>
      <term>NSUserData</term>
      <desc>
	This is an optional arbitrary string which (if present) is passed
	as the userData argument to the method implementing the service.
	This permits a service provider to implement a single method to
	handle a variety of similar services, whose exact characteristics
	are determined by this parameter.
      </desc>
      <term>NSMenuItem</term>
      <desc>
	This is a dictionary containing language names and the text to
	appear in the services menu for each language.  It may contain
	an entry where the language name is <code>default</code> and
	this entry will be used where none of the specific languages
	listed are found in the application user's preferences.<br />
	These text items may contain a single slash ('/') character,
	and if this is present, the text after the slash will appear
	in a submenu of the services menu, with the text before the
	slash being the name of that submenu.  This is very useful
	where a single application provides a variety of services and
	wishes to group them together.
      </desc>
      <term>NSKeyEquivalent</term>
      <desc>
	This is an optional dictionary specifying the key equivalents to
	select the menu items listed in the NSMenuItem specification.
      </desc>
      <term>NSTimeout</term>
      <desc>
	This is an optional timeout (in milliseconds) specifying how long
	the system should wait for the service provider to perform the
	service.  If omitted, it defaults to 30000 (30 seconds).
      </desc>
      <term>NSExecutable</term>
      <desc>
	This is an optional path to the executable binary of the program
	which performs the service .. it's used to launch the program if
	it is not already running.  Normally, for an application, this is
	not necessary, as the system knows how to launch any applications
	found installed in standard locations.
      </desc>
      <term>NSHost</term>
      <desc>
	Not yet implemented ... this provides for the system to launch the
	executable for this service on a different host on the network.
      </desc>
    </deflist>
    <p>
      The actual code to implement a service is very simple, even with
      error checking added -
    </p>
    <example>
- (void) encryptString: (NSPasteboard*)pboard
	      userData: (NSString*)userData
		 error: (NSString**)error
{
  NSString	*d;

  if ([pboard types] containsObject: NSStringPboardType] == NO)
    {
      *error = @"Bad types for encrypt service ... no string data";
      return;
    }
  s = [pboard stringForType: NSStringPboardType];
  if ([d length] == 0)
    {
      *error = @"No data supplied for encrypt service";
      return;
    }
  s = [self encryptString: s];	// Do the real work
  [pboard declareTypes: [NSArray arrayWithObject: NSStringPboardType
		 owner: nil];
  [pboard setString: s forType: NSStringPboardType];
  return;
}
    </example>
  </section>
  <section>
    <heading>Filter services</heading>
    <p>
      A filter service is a special case of an inter-application service.
      Its action is to take data of one type and convert it to another
      type.  Unlike general services, this is not directly initiated by
      user action clicking on an item in the services menu (indeed, filter
      services do not appear on the services menu), but is instead performed
      transparently when the application asks the pasteboard system for
      data of a particular type, but the pasteboard only contains data of
      some other type.
    </p>
    <p>
      A filter service definition in the Info.plist file differs from that
      of a standard service in that the <em>NSMessage</em> entry is replaced
      by an <em>NSFilter</em> entry, the <em>NSMenuItem</em> and
      <em>NSKeyEquivalent</em> entries are omitted, and a few other entries
      may be added -
    </p>
    <deflist>
      <term>NSFilter</term>
      <desc>
	This is the first part of the message name for the method
	which actually implements the filter service ... just like
	the NSMessage entry in a normal service.
      </desc>
      <term>NSInputMechanism</term>
      <desc>
	This (optional) entry is a string value specifying an alternative
	mechanism for performing the filer service (instead of sending a
	message to an application to ask it to do it).<br />
	Possible values are -
	<deflist>
	  <term>NSIdentity</term>
	  <desc>
	    The data to be filtered is simply placed upon the pasteboard
	    without any transformation.
	  </desc>
	  <term>NSMapFile</term>
	  <desc>
	    The data to be filtered is the name of a file, which is
	    loaded into memory and placed on the pasteboard without
	    any transformation.<br />
	    If the data to be filtered contains multiple file names,
	    only the first is used.
	  </desc>
	  <term>NSUnixStdio</term>
	  <desc>
	    The data to be filtered is the name of a file, which is
	    passed as the argument to a unix command-line program,
	    and the standard output of that program is captured and
	    placed on the pasteboard.  The program is run each time
	    data is requested, so this is inefficient in comparison
	    to a filter implemented using the standard method (of
	    sending a message to a running application).<br />
	    If the data to be filtered contains multiple file names,
	    only the first is used.
	  </desc>
	</deflist>
      </desc>
    </deflist>
    <p>
      Filter services are used implicitly whenever you get a pasteboard
      by using one of the methods +pasteboardByFilteringData:ofType:,
      +pasteboardByFilteringFile: or +pasteboardByFilteringTypesInPasteboard:
      as the pasteboard system will automatically invoke any available
      filter to convert the data in the pasteboard to any required
      type as long as a conversion can be done using a single filter.
    </p>
  </section>
  <section>
    <heading>Distributed Objects services</heading>
    <p>
      While the general <em>services</em> mechanism described above
      covers most eventualities, there are some circumstances where
      you might want your application to offer more complex services
      which require the client application to have been written to
      make use of those services and where the interaction between
      the two is much trickier.
    </p>
    <p>
      In most cases, such situations are handled by server processes
      rather than GUI applications, thus avoiding all the overheads
      of a GUI application ... linking with the GUI library and
      using the windowing system etc.  On occasion you may actually
      want the services to use facilities from the GUI library
      (such as the [NSPasteboard] or [NSWorkspace] class).
    </p>
    <p>
      Traditionally, NeXTstep and GNUstep applications permit you to
      connect to an application using the standard [NSConnection]
      mechanisms, with the name of the port you connect to being
      (by convention) the name of the application.  The root proxy
      of the NSConnection obtained this way would be the
      [NSApplication-delegate] object, and any messages sent to
      this object would be handled by the application delegate.
    </p>
    <p>
      In the interests of security, GNUstep provides a mechanism to
      ensure that <em>only</em> those methods you explicitly want to
      be available to remote processes are actually available.<br />
      Those methods are assumed to be any of the standard application
      methods, and any methods implementing the standard <em>services</em>
      mechanism (ie. methods whose names begin <code>application:</code>
      or end with <code>:userData:error:</code>), plus any methods
      listed in the array returned by the
      <code>GSPermittedMessages</code> user default.<br />
      If your application wishes to make non-standard methods available,
      it should use [NSUserDefaults-registerDefaults:] to set a standard
      value for GSPermittedMessages.  Users of the application can then
      use the defaults system to override that standard setting for the
      application in order to reduce or increase the list of messages
      available to remote processes.
    </p>
    <p>
      To make use of a service, you need to check to ensure that the
      application providing the service is running, connect to it,
      and then send messages to it.  You should take care to catch
      exceptions and deal with a loss of connection to the server
      application.<br />
      As an aid to using the services, GNUstep provides a helper function
      (GSContactApplication()) which encapsulates the process of
      establishing a connection and
      launching the server application if necessary.
    </p>
<example>
  id	proxy = GSContactApplication(@"pathToApp", nil, nil);
  if (proxy != nil)
    {
      NS_EXCEPTION
	{
	  id result = [proxy performTask: taskName withArgument: anArgument];

	  if (result == nil)
	    {
	      // handle error
	    }
	  else
	    {
	      // Use result
	    }
	}
      NS_HANDLER
        // Handle exception
      NS_ENDHANDLER
    }
</example>
    <p>
      If we want to send repeated messages, we may store the proxy to
      server application, and might want to keep track of the state of
      the connection to be sure that the proxy is still valid.
    </p>
<example>
  ASSIGN(remote, proxy);
  // We want to keep hold of the proxy for use later, so we need to know
  // if the connection dies ... we ask for a notification to call our
  // connectionBecameInvalid: method when the connection dies ... in that
  // method we can release the proxy.
  [[NSNotificationCenter defaultCenter]
    addObserver: self
       selector: @selector(connectionBecameInvalid:)
	   name: NSConnectionDidDieNotification
	 object: [remote connectionForProxy]];
</example>
  </section>
</chapter>
*/ 

#include "config.h"
#import <Foundation/NSArray.h>
#import <Foundation/NSData.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSHost.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSConnection.h>
#import <Foundation/NSDistantObject.h>
#import <Foundation/NSDistributedNotificationCenter.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSMapTable.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSException.h>
#import <Foundation/NSInvocation.h>
#import <Foundation/NSLock.h>
#import <Foundation/NSPathUtilities.h>
#import <Foundation/NSPortCoder.h>
#import <Foundation/NSPortNameServer.h>
#import <Foundation/NSProcessInfo.h>
#import <Foundation/NSSerialization.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/NSMethodSignature.h>
#import <Foundation/NSRunLoop.h>
#import <Foundation/NSSet.h>
#import <Foundation/NSTask.h>
#import <Foundation/NSTimer.h>
#import <GNUstepBase/NSTask+GNUstepBase.h>
#import "AppKit/NSPasteboard.h"
#import "AppKit/NSApplication.h"
#import "AppKit/NSWorkspace.h"
#import "AppKit/NSFileWrapper.h"
#import "GNUstepGUI/GSServicesManager.h"
#import "GNUstepGUI/GSPasteboardServer.h"

static NSString	*contentsPrefix = @"NSTypedFileContentsPboardType:";
static NSString	*namePrefix = @"NSTypedFilenamesPboardType:";

/* This is a proxy used to send objects over DO by reference when they
 * would normally be copied.
 * The idea is to use such a proxy when a filter sends data to a pasteboard.
 * Since the filtered data will only be used in the process which sets up
 * the filter, there's no point sending it on a round trip to the
 * pasteboard server and back ... instead we encode a GSByrefObject, which
 * appears as a proxy/reference on the remote system (pasteboard server)
 * but when the pasteboard server sends it back it decodes locally as the
 * original data object.
 */
@interface GSByrefObject : NSObject
{
  NSObject	*target;
}
+ (id) byrefWithObject: (NSObject*)object;
@end

@implementation	GSByrefObject

+ (id) byrefWithObject: (NSObject*)object
{
  GSByrefObject	*b = [GSByrefObject new];
  b->target = [object retain];
  return [b autorelease];
}

- (void) dealloc
{
  [target release];
  [super dealloc];
}

- (void) forwardInvocation: (NSInvocation*)anInvocation
{
  [anInvocation invokeWithTarget: target];
}

- (NSMethodSignature*) methodSignatureForSelector: (SEL)aSelector
{
  if (class_respondsToSelector(object_getClass(self), aSelector))
    {
      return [super methodSignatureForSelector: aSelector];
    }
  return [target methodSignatureForSelector: aSelector];
}

/* Encode this proxy as a reference to its target.
 * That way when the reference is passed back to this process it
 * will decode as the original target object rather than the proxy.
 */
- (id) replacementObjectForPortCoder: (NSPortCoder*)aCoder
{
  return [NSDistantObject proxyWithLocal: target
			      connection: [aCoder connection]];
}

@end

/*
 * A pasteboard class for lazily filtering data
 */
@interface GSFiltered : NSPasteboard
{
@public
  NSArray	*originalTypes;
  NSString	*file;
  NSData	*data;
  NSPasteboard	*pboard;
}
@end

@implementation	GSFiltered

/**
 * Given an array of types, produce an array of all the types we can
 * make from that using a single filter.
 */
+ (NSArray*) _typesFilterableFrom: (NSArray*)from
{
  NSMutableSet	*types = [NSMutableSet setWithCapacity: 8];
  NSArray	*filters = [[GSServicesManager manager] filters];
  unsigned	c = [filters count];
  unsigned 	i;

  for (i = 0; i < [from count]; i++)
    {
      NSString	*type = [from objectAtIndex: i];
      unsigned 	j;

      [types addObject: type];	// Always include original type

      for (j = 0; j < c; j++)
	{
	  NSDictionary	*info = [filters objectAtIndex: j];
	  NSArray	*sendTypes = [info objectForKey: @"NSSendTypes"];

	  if ([sendTypes containsObject: type] == YES)
	    {
	      NSArray	*returnTypes = [info objectForKey: @"NSReturnTypes"];

	      [types addObjectsFromArray: returnTypes];
	    }
	}
    }
  return [types allObjects];
}

- (void) dealloc
{
  DESTROY(originalTypes);
  DESTROY(file);
  DESTROY(data);
  DESTROY(pboard);
  [super dealloc];
}

/**
 * This method actually performs any filtering required.
 */
- (void) pasteboard: (NSPasteboard*)sender
 provideDataForType: (NSString*)type
{
  NSDictionary	*info;
  NSString	*fromType = nil;
  NSString	*mechanism;

  NSAssert(sender == self, NSInvalidArgumentException);

  /*
   * If the requested type is the same as one of the original types,
   * no filtering is required ... and we can just write what we have.
   */
  if ([originalTypes containsObject: type] == YES)
    {
      info = [NSDictionary dictionaryWithObjectsAndKeys:
	@"NSIdentity", @"NSInputMechanism",
        nil];
    }
  else
    {
      NSArray	*filters;
      unsigned	count;
      unsigned	filterNumber = 0;

      /*
       * Locate the filter information needed, including the type we are
       * converting from and the name of the filter to use.
       */
      info = nil;
      filters = [[GSServicesManager manager] filters];
      count = [filters count];
      while (fromType == nil && filterNumber < count)
	{
	  NSArray	*returnTypes;

	  info = [filters objectAtIndex: filterNumber++];
	  returnTypes = [info objectForKey: @"NSReturnTypes"];

	  if ([returnTypes containsObject: type] == YES)
	    {
	      NSArray	*sendTypes = [info objectForKey: @"NSSendTypes"];
	      unsigned 	i;

	      for (i = 0; i < [originalTypes count]; i++)
		{
		  fromType = [originalTypes objectAtIndex: i];
		  if ([sendTypes containsObject: fromType] == YES)
		    {
		      break;
		    }
		  fromType = nil;
		}
	    }
	}
      if (!info)
	{
	  NSWarnMLog(@"Unable to provide data of type '%@'.", type);
	  return;
	}
    }

  mechanism = [info objectForKey: @"NSInputMechanism"];

  if ([mechanism isEqualToString: @"NSUnixStdio"] == YES)
    {
      NSMutableData	*m = [NSMutableData dataWithCapacity: 1023];
      NSString		*filename;
      NSString		*path;
      NSData		*d;
      NSPipe		*p;
      NSFileHandle	*h;
      NSTask		*t;
      id		o;

      /*
       * The data for an NSUnixStdio filter must be one or more filenames
       */
      if ([fromType isEqualToString: NSStringPboardType] == NO
	&& [fromType isEqualToString: NSFilenamesPboardType] == NO
	&& [fromType hasPrefix: namePrefix] == NO)
	{
	  [sender setData: [NSData data] forType: type];
	  return;	// Not the name of a file to filter.
	}

      if (file != nil)
	{
	  filename = file;
	}
      else
        {
          if (data != nil)
            {
              d = data;
            }
          else
            {
              d = [pboard dataForType: fromType];
            }
          
          o = [NSDeserializer deserializePropertyListFromData: d
                                            mutableContainers: NO];
          if ([o isKindOfClass: [NSString class]] == YES)
            {
              filename = o;
            }
          else if ([o isKindOfClass: [NSArray class]] == YES
	    && [o count] > 0
	    && [[o objectAtIndex: 0] isKindOfClass: [NSString class]] == YES)
            {
              filename = [o objectAtIndex: 0];
            }
          else
            {
              [sender setData: [NSData data] forType: type];
              return;	// Not the name of a file to filter.
            }
        }

      /*
       * Set up and launch task to filter the named file.
       */
      t = [NSTask new];
      path = [info objectForKey: @"NSExecutable"];
      if ([path length] == 0)
	{
	  path = [info objectForKey: @"NSPortName"];
	}
      [t setLaunchPath: path];
      [t setArguments: [NSArray arrayWithObject: filename]];
      p = [NSPipe pipe];
      [t setStandardOutput: p];
      [t launch];

      /*
       * Read all the data that the task writes.
       */
      h = [p fileHandleForReading];
      while ([(d = [h availableData]) length] > 0)
	{
	  [m appendData: d];
	}
      [t waitUntilExit];
      RELEASE(t);

      /*
       * And send it on.
       */
      [sender setData: [GSByrefObject byrefWithObject: m] forType: type];
    }
  else if ([mechanism isEqualToString: @"NSMapFile"] == YES)
    {
      NSString	*filename;
      NSData	*d;
      id	o;

      if ([fromType isEqualToString: NSStringPboardType] == NO
	&& [fromType isEqualToString: NSFilenamesPboardType] == NO
	&& [fromType hasPrefix: namePrefix] == NO)
	{
	  [sender setData: [NSData data] forType: type];
	  return;	// Not the name of a file to filter.
	}

      /* TODO: d used to be used here before being initialized. Set it to
      nil and warn instead of crashing for now. */
      d = nil;
      NSWarnMLog(@"NSMapFile handling is broken.");

      o = [NSDeserializer deserializePropertyListFromData: d
					mutableContainers: NO];
      if ([o isKindOfClass: [NSString class]] == YES)
	{
	  filename = o;
	}
      else if ([o isKindOfClass: [NSArray class]] == YES
	&& [o count] > 0
	&& [[o objectAtIndex: 0] isKindOfClass: [NSString class]] == YES)
	{
	  filename = [o objectAtIndex: 0];
	}
      else
	{
	  [sender setData: [NSData data] forType: type];
	  return;	// Not the name of a file to filter.
	}

      d = [NSData dataWithContentsOfFile: filename];

      [sender setData: [GSByrefObject byrefWithObject: d] forType: type];
    }
  else if ([mechanism isEqualToString: @"NSIdentity"] == YES)
    {
      /*
       * An 'identity' filter simply places the required data on the
       * pasteboard.
       */
      if (data != nil)
	{
	  [sender setData: data forType: type];
	}
      else if (file != nil)
	{
	  [sender writeFileContents: file];
	}
      else
	{
	  NSData	*d = [pboard dataForType: type];

	  [sender setData: [GSByrefObject byrefWithObject: d] forType: type];
	}
    }
  else
    {
      NSPasteboard	*tmp;
      NSString		*port;
      NSString		*timeout;
      double		seconds;
      NSDate		*finishBy;
      NSString		*appPath;
      id		provider;
      NSString		*message;
      NSString		*selName;
      NSString		*userData;
      NSString		*error = nil;

      /*
       * Put data onto a pasteboard that can be used by the service provider.
       */
      if (data != nil)
	{
	  tmp = [NSPasteboard pasteboardWithUniqueName];
	  [tmp declareTypes: [NSArray arrayWithObject: fromType] owner: nil];
	  [tmp setData: data forType: fromType];
	}
      else if (file != nil)
	{
	  tmp = [NSPasteboard pasteboardWithUniqueName];
	  [tmp declareTypes: [NSArray arrayWithObject: fromType] owner: nil];
	  [tmp writeFileContents: file];
	}
      else
	{
	  tmp = pboard;		// Already in a pasteboard.
	}

      port = [info objectForKey: @"NSPortName"];
      timeout = [info objectForKey: @"NSTimeout"];
      if (timeout && [timeout floatValue] > 100)
	{
	  seconds = [timeout floatValue] / 1000.0;
	}
      else
	{
	  seconds = 30.0;
	}
      finishBy = [NSDate dateWithTimeIntervalSinceNow: seconds];
      appPath = [info objectForKey: @"NSExecutable"];
      if ([appPath length] > 0)
	{
	  /*
	   * A relative path for NSExecutable is relative to the bundle.
	   */
	  if ([appPath isAbsolutePath] == NO)
	    {
	      NSString	*bundlePath = [info objectForKey: @"ServicePath"];

	      appPath = [bundlePath stringByAppendingPathComponent: appPath];
	    }
	}
      else
	{
	  appPath = [info objectForKey: @"ServicePath"];
	}
      userData = [info objectForKey: @"NSUserData"];
      message = [info objectForKey: @"NSFilter"];
      selName = [message stringByAppendingString: @":userData:error:"];

      /*
       * Locate the service provider ... this will be a proxy to the remote
       * object, or a local object (if we provide the service ourself)
       */
      provider = GSContactApplication(appPath, port, finishBy);
      if (provider == nil)
	{
	  NSLog(@"Failed to contact service provider at '%@' '%@'",
	    appPath, port);
	  return;
	}

      /*
       * If the service provider is a remote object, we can set timeouts on
       * the NSConnection so we don't hang waiting for it to reply.
       */
      if ([provider isProxy] == YES)
	{
	  NSConnection	*connection;

	  connection = [(NSDistantObject*)provider connectionForProxy];
	  [connection enableMultipleThreads];
	  seconds = [finishBy timeIntervalSinceNow];
	  [connection setRequestTimeout: seconds];
	  [connection setReplyTimeout: seconds];
	}

      /*
       * At last, we ask for the service to be performed.
       */
      NS_DURING
	{
	  SEL			sel;
	  NSMethodSignature	*sig;

	  sel = NSSelectorFromString(selName);
	  sig = [provider methodSignatureForSelector: sel];
	  if (sig != nil)
	    {
	      NSInvocation	*inv;
	      NSString		**errPtr = &error;

	      inv = [NSInvocation invocationWithMethodSignature: sig];
	      [inv setTarget: provider];
	      [inv setSelector: sel];
	      [inv setArgument: (void*)&tmp atIndex: 2];
	      [inv setArgument: (void*)&userData atIndex: 3];
	      [inv setArgument: (void*)&errPtr atIndex: 4];
	      [inv invoke];
	    }
	  else
	    {
	      error = @"No remote object to handle filter";
	    }
	}
      NS_HANDLER
	{
	  error = [NSString stringWithFormat: @"%@", [localException reason]];
	}
      NS_ENDHANDLER

      if (error != nil)
	{
	  NSLog(@"Failed to contact service provider for '%@': %@",
	    appPath, error);
	  return;
	}

      /*
       * Finally, make it available.
       */
      [sender setData: [GSByrefObject byrefWithObject: [tmp dataForType: type]]
	      forType: type];
    }
}

@end



@interface NSPasteboard (Private)
+ (void) _localServer: (id<GSPasteboardSvr>)s;
+ (id) _lostServer: (NSNotification*)notification;
+ (id<GSPasteboardSvr>) _pbs;
+ (NSPasteboard*) _pasteboardWithTarget: (id<GSPasteboardObj>)aTarget
				   name: (NSString*)aName;
- (id) _target;
@end

/**
 * <p>The pasteboard system is the primary mechanism for data exchange
 * between OpenStep applications.  It is used for cut and paste of data,
 * as the exchange mechanism for <em>services</em> (as listed on the
 * services menu), for communicating with a spelling server in order to
 * perform spell checking, and for <em>filter services</em> which convert
 * data of one type to another transparently.
 * </p>
 * <p>Pasteboards are identified by names, some of which are standard
 * and are intended to exist permanently and be shared between all
 * applications, others are temporary or private and are used to handle
 * specific services.
 * </p>
 * <p>All data transferred to/from pasteboards is <em>typed</em>.  Mostly
 * using one of several standard types for common data or using standardised
 * names which identify particular kinds of files and their contents
 * (see the NSCreateFileContentsPboardType() an
 * NSCreateFilenamePboardType() functions for details).  It is also possible
 * for cooperating applications to use their own private types ... any string
 * value will do.
 * </p>
 * <p>Each pasteboard has an <em>owner</em> ... an object which declares the
 * types of data it can provide.  Unless versions of the pasteboard data
 * corresponding to all the declared types are written to the pasteboard,
 * the owner is responsible for producing the data for the pasteboard when
 * it is called for (lazy provision of data).<br />
 * The pasteboard owner needs to implement the methods of the
 * NSPasteboardOwner informal protocol in order to do this.
 * </p>
 */
@implementation NSPasteboard

static	NSRecursiveLock		*dictionary_lock = nil;
static	NSMapTable		*pasteboards = 0;
static	id<GSPasteboardSvr>	the_server = nil;
static  NSMapTable              *mimeMap = NULL;

/**
 * Returns the general pasteboard found by calling +pasteboardWithName:
 * with NSGeneralPboard as the name.
 */
+ (NSPasteboard*) generalPasteboard
{
  static NSPasteboard *generalPboard = nil;
  NSPasteboard *currentGeneralPboard;

  // call pasteboardWithName: every time, to update server connection if needed
  currentGeneralPboard = [self pasteboardWithName: NSGeneralPboard];
  if (currentGeneralPboard != generalPboard)
    {
      ASSIGN(generalPboard, currentGeneralPboard);
    }
  return generalPboard;
}

+ (void) initialize
{
  if (self == [NSPasteboard class])
    {
      // Initial version
      [self setVersion: 1];
      dictionary_lock = [[NSRecursiveLock alloc] init];
      pasteboards = NSCreateMapTable (NSObjectMapKeyCallBacks,
	NSNonRetainedObjectMapValueCallBacks, 0);
    }
}

/**
 * <p>Creates and returns a pasteboard from which the data in the named
 * file can be read in all the types to which it can be converted by
 * filter services.<br />
 * The type of data in the file is inferred from the file extension.
 * </p>
 * <p>No filtering is actually performed until some object asks the
 * pasteboard for the data, so calling this method is quite inexpensive.
 * </p>
 */
+ (NSPasteboard*) pasteboardByFilteringData: (NSData*)data
				     ofType: (NSString*)type
{
  GSFiltered	*p;
  NSArray	*types;
  NSArray	*originalTypes;

  originalTypes = [NSArray arrayWithObject: type];
  types = [GSFiltered _typesFilterableFrom: originalTypes];
  p = (GSFiltered*)[GSFiltered pasteboardWithUniqueName];
  p->originalTypes = [originalTypes copy];
  p->data = [data copy];
  [p declareTypes: types owner: p];
  return p;
}

/**
 * <p>Creates and returns a pasteboard from which the data in the named
 * file can be read in all the types to which it can be converted by
 * filter services.<br />
 * The type of data in the file is inferred from the file extension.
 * </p>
 */
+ (NSPasteboard*) pasteboardByFilteringFile: (NSString*)filename
{
  GSFiltered	*p;
  NSString	*ext = [filename pathExtension];
  NSArray	*types;
  NSArray	*originalTypes;

  if ([ext length] > 0)
    {
      originalTypes = [NSArray arrayWithObjects:
	NSCreateFilenamePboardType(ext),
	NSFilenamesPboardType,
	nil];
    }
  else
    {
      originalTypes = [NSArray arrayWithObject: NSFilenamesPboardType];
    }
  types = [GSFiltered _typesFilterableFrom: originalTypes];
  p = (GSFiltered*)[GSFiltered pasteboardWithUniqueName];
  p->originalTypes = [originalTypes copy];
  p->file = [filename copy];
  [p declareTypes: types owner: p];
  return p;
}

/**
 * <p>Creates and returns a pasteboard where the data contained in pboard
 * is available for reading in as many types as it can be converted to by
 * available filter services.  This normally expands on the range of types
 * available in pboard.
 * </p>
 * <p>NB. This only permits a single level of filtering ... if pboard was
 * previously returned by another filtering method, it is returned instead
 * of a new pasteboard.
 * </p>
 */
+ (NSPasteboard*) pasteboardByFilteringTypesInPasteboard: (NSPasteboard*)pboard
{
  GSFiltered	*p;
  NSArray		*types;
  NSArray		*originalTypes;

  if ([pboard isKindOfClass: [GSFiltered class]] == YES)
    {
      return pboard;
    }
  originalTypes = [pboard types];
  types = [GSFiltered _typesFilterableFrom: originalTypes];
  p = (GSFiltered*)[GSFiltered pasteboardWithUniqueName];
  p->originalTypes = [originalTypes copy];
  p->pboard = RETAIN(pboard);
  [p declareTypes: types owner: p];
  return p;
}

/**
 * <p>Returns the pasteboard for the specified name.  Creates a new pasteboard
 * if (and only if) one with the given name does not exist.
 * </p>
 * Standard pasteboard names are -
 * <list>
 *   <item><ref type="variable" id="NSGeneralPboard">
 *   NSGeneralPboard</ref></item>
 *   <item><ref type="variable" id="NSFontPboard">
 *   NSFontPboard</ref></item>
 *   <item><ref type="variable" id="NSRulerPboard">
 *   NSRulerPboard</ref></item>
 *   <item><ref type="variable" id="NSFindPboard">
 *   NSFindPboard</ref></item>
 *   <item><ref type="variable" id="NSDragPboard">
 *   NSDragPboard</ref></item>
 * </list>
 */
+ (NSPasteboard*) pasteboardWithName: (NSString*)aName
{
  NS_DURING
    {
      id<GSPasteboardObj>	anObj;

      anObj = [[self _pbs] pasteboardWithName: aName];
      if (anObj != nil)
	{
	  NSPasteboard	*ret;

	  if ([(id)anObj isProxy] == YES)
	    {
	      Protocol	*p = @protocol(GSPasteboardObj);

	      [(id)anObj setProtocolForProxy: p];
	    }
	  ret = [self _pasteboardWithTarget: anObj name: aName];
	  NS_VALRETURN(ret);
	}
    }
  NS_HANDLER
    {
      [NSException raise: NSPasteboardCommunicationException
		  format: @"%@", [localException reason]];
    }
  NS_ENDHANDLER

  return nil;
}

/**
 * Creates and returns a new pasteboard with a name guaranteed to be unique
 * within the pasteboard server.
 */
+ (NSPasteboard*) pasteboardWithUniqueName
{
  NS_DURING
    {
      id<GSPasteboardObj>	anObj;

      anObj = [[self _pbs] pasteboardWithName: nil];
      if (anObj)
	{
	  NSString	*aName;

	  aName = [anObj name];
	  if (aName)
	    {
	      NSPasteboard	*ret;

	      ret = [self _pasteboardWithTarget: anObj name: aName];
	      NS_VALRETURN(ret);
	    }
	}
    }
  NS_HANDLER
    {
      [NSException raise: NSPasteboardCommunicationException
		  format: @"%@", [localException reason]];
    }
  NS_ENDHANDLER

  return nil;
}

/**
 * Returns an array of the types from which data of the specified type
 * can be produced by registered filter services.<br />
 * The original type is always present in this array.<br />
 * Raises an exception if type is nil.
 */
+ (NSArray*) typesFilterableTo: (NSString*)type
{
  NSMutableSet	*types = [NSMutableSet setWithCapacity: 8];
  NSArray	*filters = [[GSServicesManager manager] filters];
  NSEnumerator	*enumerator = [filters objectEnumerator];
  NSDictionary	*info;

  [types addObject: type];	// Always include original type

  /*
   * Step through the filters looking for those which handle the type
   */
  while ((info = [enumerator nextObject]) != nil)
    {
      NSArray	*returnTypes = [info objectForKey: @"NSReturnTypes"];

      if ([returnTypes containsObject: type] == YES)
	{
	  NSArray	*sendTypes = [info objectForKey: @"NSSendTypes"];

	  [types addObjectsFromArray: sendTypes];
	}
    }

  return [types allObjects];
}

/**
 * <p>Adds newTypes to the pasteboard and declares newOwner to be the owner
 * of the pasteboard.  Use only after -declareTypes:owner: has been called
 * for the same owner, because the new owner may not support all the types
 * declared by a previous owner.
 * </p>
 * <p>Returns the new change count for the pasteboard, or zero if an error
 * occurs.
 * </p>
 */
- (int) addTypes: (NSArray*)newTypes
	   owner: (id)newOwner
{
  int	count = 0;

  NS_DURING
    {
      count = [target addTypes: newTypes
			 owner: newOwner
		    pasteboard: self
		      oldCount: changeCount];
      if (count > 0)
	{
	  changeCount = count;
	}
    }
  NS_HANDLER
    {
      count = 0;
      [NSException raise: NSPasteboardCommunicationException
		  format: @"%@", [localException reason]];
    }
  NS_ENDHANDLER
  return count;
}

/**
 * <p>Sets the owner of the pasteboard to be newOwner and declares newTypes
 * as the types of data supported by it.<br />
 * This invalidates existing data in the pasteboard (except where the GNUstep
 * -setHistory: extension allows multi-version data to be held).
 * </p>
 * <p>The value of newOwner may be nil, but if it is, data should
 * immediately be written to the pasteboard for all the value in newTypes
 * as a nil owner cannot be used for lazy supply of data.
 * </p>
 * <p>This increments the change count for the pasteboard and the new
 * count is returned, or zero is returned if an error occurs.<br />
 * Where -setChangeCount: has been used, the highest count to date
 * is incremented and returned, rather than the last value specified
 * by the -setChangeCount: method.
 * </p>
 * <p>The types you declare can be arbitrary strings, but as at least two
 * applications really need to be aware of the same type for it to be
 * of use, it is much more normal to use a predefined (standard) type
 * or a type representing the name or content of a particular kind of
 * file (returned by the NSCreateFilenamePboardType() or
 * NSCreateFilenamePboardType() function).<br />
 * The standard type for raw data is
 * <ref type="variable" id="NSGeneralPboardType">NSGeneralPboardType</ref>
 * </p>
 * The predefined pasteboard types are -
 * <list>
 *   <item><ref type="variable" id="NSStringPboardType">
 *   NSStringPboardType</ref></item>
 *   <item><ref type="variable" id="NSColorPboardType">
 *   NSColorPboardType</ref></item>
 *   <item><ref type="variable" id="NSFileContentsPboardType">
 *   NSFileContentsPboardType</ref></item>
 *   <item><ref type="variable" id="NSFilenamesPboardType">
 *   NSFilenamesPboardType</ref></item>
 *   <item><ref type="variable" id="NSFontPboardType">
 *   NSFontPboardType</ref></item>
 *   <item><ref type="variable" id="NSRulerPboardType">
 *   NSRulerPboardType</ref></item>
 *   <item><ref type="variable" id="NSPostScriptPboardType">
 *   NSPostScriptPboardType</ref></item>
 *   <item><ref type="variable" id="NSTabularTextPboardType">
 *   NSTabularTextPboardType</ref></item>
 *   <item><ref type="variable" id="NSRTFPboardType">
 *   NSRTFPboardType</ref></item>
 *   <item><ref type="variable" id="NSRTFDPboardType">
 *   NSRTFDPboardType</ref></item>
 *   <item><ref type="variable" id="NSTIFFPboardType">
 *   NSTIFFPboardType</ref></item>
 *   <item><ref type="variable" id="NSDataLinkPboardType">
 *   NSDataLinkPboardType</ref></item>
 *   <item><ref type="variable" id="NSGeneralPboardType">
 *   NSGeneralPboardType</ref></item>
 *   <item><ref type="variable" id="NSPDFPboardType">
 *   NSPDFPboardType</ref></item>
 *   <item><ref type="variable" id="NSPICTPboardType">
 *   NSPICTPboardType</ref></item>
 *   <item><ref type="variable" id="NSURLPboardType">
 *   NSURLPboardType</ref></item>
 *   <item><ref type="variable" id="NSHTMLPboardType">
 *   NSHTMLPboardType</ref></item>
 * </list>
 */
- (int) declareTypes: (NSArray*)newTypes
	       owner: (id)newOwner
{
  NS_DURING
    {
      changeCount = [target declareTypes: newTypes
				   owner: newOwner
			      pasteboard: self];
    }
  NS_HANDLER
    {
      [NSException raise: NSPasteboardCommunicationException
		  format: @"%@", [localException reason]];
    }
  NS_ENDHANDLER
  return changeCount;
}

- (void) dealloc
{
  DESTROY(target);
  [dictionary_lock lock];
  if (NSMapGet(pasteboards, (void*)name) == (void*)self)
    {
      NSMapRemove(pasteboards, (void*)name);
    }
  DESTROY(name);
  [dictionary_lock unlock];
  [super dealloc];
}

- (NSString*) description
{
  return [NSString stringWithFormat: @"%@ %@ %p",
    [super description], name, target];
}

/**
 * Encode for DO by using just our name.
 */
- (void) encodeWithCoder: (NSCoder*)aCoder
{
  [aCoder encodeObject: name];
}

/**
 * Decode from DO by creating a new pasteboard with the decoded name.
 */
- (id) initWithCoder: (NSCoder*)aCoder
{
  NSString	*n = [aCoder decodeObject];
  NSPasteboard	*p = [[self class] pasteboardWithName: n];

  ASSIGN(self, p);
  return self;
}

/**
 * Returns the pasteboard name (as given to +pasteboardWithName:)
 * for the receiver.
 */
- (NSString*) name
{
  return name;
}

/**
 * Releases the receiver in the pasteboard server so that no other application
 * can use the pasteboard.  This should not be called for any of the standard
 * pasteboards, only for temporary ones.
 */
- (void) releaseGlobally
{
  if ([name isEqualToString: NSGeneralPboard] == YES
    || [name isEqualToString: NSFontPboard] == YES
    || [name isEqualToString: NSRulerPboard] == YES
    || [name isEqualToString: NSFindPboard] == YES
    || [name isEqualToString: NSDragPboard] == YES)
    {
      [NSException raise: NSGenericException
		  format: @"Illegal attempt to globally release %@", name];
    }
  [target releaseGlobally];
  [dictionary_lock lock];
  if (NSMapGet(pasteboards, (void*)name) == (void*)self)
    {
      NSMapRemove(pasteboards, (void*)name);
    }
  [dictionary_lock unlock];
}

/**
 * Pasteboards sent over DO should always be copied so that a local
 * instance is created to communicate with the pasteboard server.
 */
- (id) replacementObjectForPortCoder: (NSPortCoder*)aCoder
{
  if ([self class] == [NSPasteboard class])
    {
      return self;	// Always encode bycopy.
    }

/* But ... if this is actually a filter rather than a 'real' pasteboard,
 * we don't want it copied to the pasteboard server.
 */ 
 if ([self class] == [GSFiltered class])
   {
     return [super replacementObjectForPortCoder: aCoder];
   }

  return [super replacementObjectForPortCoder: aCoder];
}

/**
 * <p>Writes data of type dataType to the pasteboard server so that other
 * applications can read it.  The dataType must be one of the types
 * previously declared for the pasteboard.<br />
 * All the other methods for writing data to the pasteboard call this one.
 * </p>
 * <p>Returns YES on success, NO if the data could not be written for some
 * reason.
 * </p>
 */
- (BOOL) setData: (NSData*)data
	 forType: (NSString*)dataType
{
  BOOL	ok = NO;

  NS_DURING
    {
      ok = [target setData: data
		   forType: dataType
		    isFile: NO
		  oldCount: changeCount];
    }
  NS_HANDLER
    {
      ok = NO;
      [NSException raise: NSPasteboardCommunicationException
		  format: @"%@", [localException reason]];
    }
  NS_ENDHANDLER
  return ok;
}

- (BOOL) writeObjects: (NSArray*)objects
{
  // FIXME: not implemented
  return NO;
}

/**
 * <p>Serialises the data in the supplied property list and writes it to the
 * pasteboard server using the -setData:forType: method.
 * </p>
 * <p>Data written using this method can be read by -propertyListForType:
 * or, if it was a simple string, by -stringForType:
 * </p>
 * <p>If the data is retrieved using -dataForType: then it needs to be
 * deserialized into a property list.
 * </p>
 */
- (BOOL) setPropertyList: (id)propertyList
		 forType: (NSString*)dataType
{
  NSData	*d = [NSSerializer serializePropertyList: propertyList];

  return [self setData: d forType: dataType];
}

/**
 * <p>Writes  string it to the pasteboard server using the
 * -setPropertyList:forType: method.
 * </p>
 * <p>The data may subsequently be read from the receiver using the
 * -stringForType: or -propertyListForType: method.
 * </p>
 * <p>If the data is retrieved using -dataForType: then it needs to be
 * deserialized into a property list.
 * </p>
 */
- (BOOL) setString: (NSString*)string
	   forType: (NSString*)dataType
{
  return [self setPropertyList: string forType: dataType];
}

/**
 * <p>Writes the contents of the file filename to the pasteboard server
 * after declaring the type NSFileContentsPboardType as well as a type
 * based on the file extension (given by the NSCreateFileContentsPboardType()
 * function) if those types have not already been declared.<br />
 * If the filename has no extension, only NSFileContentsPboardType is used.
 * </p>
 * <p>Data written to a pasteboard by this method should be read using
 * the -readFileContentsType:toFile: or -readFileWrapper method.
 * </p>
 * <p>If the data is retrieved using -dataForType: then it needs to be
 * deserialized by the NSFileWrapper class.
 * </p>
 */
- (BOOL) writeFileContents: (NSString*)filename
{
  NSFileWrapper *wrapper;
  NSData	*data;
  NSArray	*types;
  NSString	*ext = [filename pathExtension];
  BOOL		ok = NO;

  wrapper = [[NSFileWrapper alloc] initWithPath: filename];
  data = [wrapper serializedRepresentation];
  RELEASE(wrapper);
  if ([ext length] > 0)
    {
      types = [NSArray arrayWithObjects: NSFileContentsPboardType,
	NSCreateFileContentsPboardType(ext), nil];
    }
  else
    {
      types = [NSArray arrayWithObject: NSFileContentsPboardType];
    }
  if ([[self types] isEqual: types] == NO)
    {
      if ([self declareTypes: types owner: owner] == 0)
	{
	  return NO;	// Unable to declare types.
	}
    }
  NS_DURING
    {
      ok = [target setData: data
		   forType: NSFileContentsPboardType
		    isFile: YES
		  oldCount: changeCount];
    }
  NS_HANDLER
    {
      ok = NO;
      [NSException raise: NSPasteboardCommunicationException
		  format: @"%@", [localException reason]];
    }
  NS_ENDHANDLER
  return ok;
}

/**
 * <p>Writes the contents of the file wrapper to the pasteboard server
 * after declaring the type NSFileContentsPboardType as well as a type
 * based on the file extension of the wrappers preferred filename if 
 * those types have not already been declared.
 * </p>
 * <p>Raises an exception if there is no preferred filename.
 * </p>
 * <p>Data written to a pasteboard by this method should be read using
 * the -readFileContentsType:toFile: or -readFileWrapper method.
 * </p>
 * <p>If the data is retrieved using -dataForType: then it needs to be
 * deserialized by the NSFileWrapper class.
 * </p>
 */
- (BOOL) writeFileWrapper: (NSFileWrapper *)wrapper
{
  NSString	*filename = [wrapper preferredFilename];
  NSData	*data;
  NSArray	*types;
  NSString	*ext = [filename pathExtension];
  BOOL		ok = NO;

  if (filename == nil)
    {
      [NSException raise: NSInvalidArgumentException
		  format: @"Cannot put file on pasteboard with "
	@"no preferred filename"];
    }
  data = [wrapper serializedRepresentation];
  if ([ext length] > 0)
    {
      types = [NSArray arrayWithObjects: NSFileContentsPboardType,
	NSCreateFileContentsPboardType(ext), nil];
    }
  else
    {
      types = [NSArray arrayWithObject: NSFileContentsPboardType];
    }
  if ([[self types] isEqual: types] == NO)
    {
      if ([self declareTypes: types owner: owner] == 0)
	{
	  return NO;	// Unable to declare types.
	}
    }
  NS_DURING
    {
      ok = [target setData: data
		   forType: NSFileContentsPboardType
		    isFile: YES
		  oldCount: changeCount];
    }
  NS_HANDLER
    {
      ok = NO;
      [NSException raise: NSPasteboardCommunicationException
		  format: @"%@", [localException reason]];
    }
  NS_ENDHANDLER
  return ok;
}

/**
 * Returns the first type listed in types which the receiver has been
 * declared (see -declareTypes:owner:) to support.
 */
- (NSString*) availableTypeFromArray: (NSArray*)types
{
  NSString *type = nil;

  NS_DURING
    {
      int	count = 0;

      type = [target availableTypeFromArray: types
				changeCount: &count];
      changeCount = count;
    }
  NS_HANDLER
    {
      type = nil;
      [NSException raise: NSPasteboardCommunicationException
		  format: @"%@", [localException reason]];
    }
  NS_ENDHANDLER
  return type;
}

/**
 * Returns all the types that the receiver has been declared to support.<br />
 * See -declareTypes:owner: for details.
 */
- (NSArray*) types
{
  NSArray *result = nil;

  NS_DURING
    {
      int	count = 0;

      result = [target typesAndChangeCount: &count];
      changeCount = count;
    }
  NS_HANDLER
    {
      result = nil;
      [NSException raise: NSPasteboardCommunicationException
		  format: @"%@", [localException reason]];
    }
  NS_ENDHANDLER
  return result;
}

/**
 * Returns the change count for the receiving pasteboard.  This count
 * is incremented whenever the owner of the pasteboard is changed.
 */
- (int) changeCount
{
  NS_DURING
    {
      int	count;

      count = [target changeCount];
      changeCount = count;
    }
  NS_HANDLER
    {
      [NSException raise: NSPasteboardCommunicationException
		  format: @"%@", [localException reason]];
    }
  NS_ENDHANDLER
  return changeCount;
}

/**
 * Returns data from the pasteboard of the specified dataType, or nil
 * if no such data is available.<br />
 * May raise an exception if communication with the pasteboard server fails.
 */
- (NSData*) dataForType: (NSString*)dataType
{
  NSData	*d = nil;

  NS_DURING
    {
      d = [target dataForType: dataType
		     oldCount: changeCount
		mustBeCurrent: (useHistory == NO) ? YES : NO];
    }
  NS_HANDLER
    {
      d = nil;
      [NSException raise: NSPasteboardCommunicationException
		  format: @"%@", [localException reason]];
    }
  NS_ENDHANDLER
  return d;
}

/**
 * Calls -dataForType: to obtain data (expected to be a serialized property
 * list) and returns the object produced by deserializing it.
 */
- (id) propertyListForType: (NSString*)dataType
{
  NSData	*d = [self dataForType: dataType];

  if (d)
    return [NSDeserializer deserializePropertyListFromData: d
					 mutableContainers: NO];
  else
    return nil;
}

/**
 * <p>Obtains data of the specified dataType from the pasteboard, deserializes
 * it to the specified filename and returns the file name (or nil on failure).
 * </p>
 * <p>This method should only be used to read data written by
 * the -writeFileContents: or -writeFileWrapper: method.
 * </p>
 */
- (NSString*) readFileContentsType: (NSString*)type
			    toFile: (NSString*)filename
{
  NSData	*d;
  NSFileWrapper *wrapper;

  if (type == nil)
    {
      type = NSCreateFileContentsPboardType([filename pathExtension]);
    }
  d = [self dataForType: type];
  if (d == nil)
    {
      d = [self dataForType: NSFileContentsPboardType];
      if (d == nil)
	return nil;
    }

  wrapper = [[NSFileWrapper alloc] initWithSerializedRepresentation: d];
  if ([wrapper writeToFile: filename atomically: NO updateFilenames: NO] == NO)
    {
      RELEASE(wrapper);
      return nil;
    }
  RELEASE(wrapper);
  return filename;
}

/**
 * <p>Obtains data of the specified dataType from the pasteboard, deserializes
 * it and returns the resulting file wrapper (or nil).
 * </p>
 * <p>This method should only be used to read data written by
 * the -writeFileContents: or -writeFileWrapper: method.
 * </p>
 */
- (NSFileWrapper*) readFileWrapper
{
  NSData *d = [self dataForType: NSFileContentsPboardType];

  if (d == nil)
    return nil;

  return
    AUTORELEASE([[NSFileWrapper alloc] initWithSerializedRepresentation: d]);
}

/**
 * <p>Obtains data of the specified dataType from the pasteboard, deserializes
 * it and returns the resulting string (or nil).
 * </p>
 * <p>The string should have been written using the -setString:forType: or
 * -setPropertyList:forType: method.
 * </p>
 */
- (NSString*) stringForType: (NSString*)dataType
{
  NSString	*s = [self propertyListForType: dataType];

  if ([s isKindOfClass: [NSString class]] == NO)
    {
      s = nil;
    }
  return s;
}

@end

@implementation NSPasteboard (Private)

/*
 *	Special method to use a local server rather than connecting over DO
 */
+ (void) _localServer: (id<GSPasteboardSvr>)s
{
  the_server = s;
}

+ (id) _lostServer: (NSNotification*)notification
{
  id	obj = the_server;

  the_server = nil;
  [[NSNotificationCenter defaultCenter]
    removeObserver: self
	      name: NSConnectionDidDieNotification
	    object: [notification object]];
  RELEASE(obj);
  return self;
}

+ (id<GSPasteboardSvr>) _pbs
{
  if (the_server == nil)
    {
      NSString	*host;
      NSString	*description;

      host = [[NSUserDefaults standardUserDefaults] stringForKey: @"NSHost"];
      if (host == nil)
	{
	  host = @"";
	}
      else
	{
	  NSHost	*h;

	  /*
	   * If we have a host specified, but it is the current host,
	   * we do not need to ask for a host by name (nameserver lookup
	   * can be faster) and the empty host name can be used to
	   * indicate that we may start a pasteboard server locally.
	   */
	  h = [NSHost hostWithName: host];
	  if (h == nil)
	    {
	      NSLog(@"Unknown NSHost (%@) ignored", host);
	      host = @"";
	    }
	  else if ([h isEqual: [NSHost currentHost]] == YES)
	    {
	      host = @"";
	    }
	  else
	    {
	      host = [h name];
	    }
	}

      if ([host length] == 0)
	{
	  description = @"local host";
	}
      else
	{
	  description = host;
	}

      the_server = (id<GSPasteboardSvr>)[NSConnection
	rootProxyForConnectionWithRegisteredName: PBSNAME host: host];
      if (the_server == nil && [host length] > 0)
	{
	  NSString	*service;

	  service = [PBSNAME stringByAppendingFormat: @"-%@", host];
	  the_server = (id<GSPasteboardSvr>)[NSConnection
	    rootProxyForConnectionWithRegisteredName: service host: @"*"];
	}

      if (RETAIN((id)the_server) != nil)
	{
	  NSConnection	*conn = [(id)the_server connectionForProxy];
          Protocol      *p = @protocol(GSPasteboardSvr);

	  [conn enableMultipleThreads];
          [conn setReplyTimeout:2.0];
          [(id)the_server setProtocolForProxy: p];
	  [[NSNotificationCenter defaultCenter]
	    addObserver: self
	       selector: @selector(_lostServer:)
		   name: NSConnectionDidDieNotification
		 object: conn];
	}
      else
	{
	  static BOOL		recursion = NO;
	  static NSString	*cmd = nil;

	  if (cmd == nil && recursion ==NO)
	    {
	      cmd = RETAIN([NSTask launchPathForTool: @"gpbs"]);
	    }
	  if (recursion == YES || cmd == nil)
	    {
	      NSLog(@"Unable to contact pasteboard server - "
		@"please ensure that gpbs is running for %@.", description);
	      return nil;
	    }
	  else
	    {
	      NSNotificationCenter *nc;
	      NSMutableArray *startIndicator;
	      NSArray *args = nil;
	      NSDate *timeoutDate;

	      NSDebugLLog(@"NSPasteboard",
@"\nI couldn't contact the pasteboard server for %@ -\n"
@"so I'm attempting to start one - which might take a few seconds.\n"
@"Trying to launch gpbs from %@ or a machine/operating-system subdirectory.\n",
description, [cmd stringByDeletingLastPathComponent]);

	      if ([host length] > 0)
		{
		  args = [[NSArray alloc] initWithObjects:
		    @"-NSHost", host,
		    @"-GSStartupNotification", @"GSStartup-GPBS",
		    @"--auto",
		    nil];
		}
	      else
		{
		  args = [[NSArray alloc] initWithObjects:
		    @"-GSStartupNotification",@"GSStartup-GPBS",
		    @"--auto",
		    nil];
		}

	      /*
	      Trick: To avoid having to use global variables or new methods
	      to track whether the notification has been received or not, we
	      use a mutable array as an indicator. When the notification is
	      received, the array is emptied, so we just check the count.
	      */
	      startIndicator = [[NSMutableArray alloc] initWithObjects:
		AUTORELEASE([[NSObject alloc] init]), nil];

	      nc = [NSDistributedNotificationCenter defaultCenter];
	      [nc addObserver: startIndicator
		     selector: @selector(removeAllObjects)
			 name: @"GSStartup-GPBS"
		       object: nil];

	      [NSTask launchedTaskWithLaunchPath: cmd arguments: args];
	      RELEASE(args);

	      timeoutDate = [NSDate dateWithTimeIntervalSinceNow: 5.0];

	      while ([startIndicator count]
	        && [timeoutDate timeIntervalSinceNow] > 0.0)
		{
		  [[NSRunLoop currentRunLoop]
		       runMode: NSDefaultRunLoopMode
		    beforeDate: timeoutDate];
		}

	      [nc removeObserver: startIndicator];
	      DESTROY(startIndicator);

	      recursion = YES;
	      [self _pbs];
	      recursion = NO;
	    }
	}
    }
  return the_server;
}

/*
 * Creating and Releasing an NSPasteboard Object
 */
+ (NSPasteboard*) _pasteboardWithTarget: (id<GSPasteboardObj>)aTarget
				   name: (NSString*)aName
{
  NSPasteboard	*p = nil;

  [dictionary_lock lock];
  p = (NSPasteboard*)NSMapGet(pasteboards, (void*)aName);
  if (p != nil)
    {
      /*
       * It is conceivable that the following may have occurred -
       * 1. The pasteboard was created on the server
       * 2. We set up an NSPasteboard to point to it
       * 3. The pasteboard on the server was destroyed by a [-releaseGlobally]
       * 4. The named pasteboard was asked for again - resulting in a new
       *	object being created on the server.
       * If this is the case, our proxy for the object on the server will be
       *	out of date, so we swap it for the newly created one.
       */
      if (p->target != (id)aTarget)
	{
	  ASSIGN(p->target, (id)aTarget);
	}
    }
  else
    {
      /*
       * For a newly created NSPasteboard object, we must make an entry
       * in the dictionary so we can look it up safely.
       */
      p = [self alloc];
      if (p != nil)
	{
	  ASSIGN(p->target, (id)aTarget);
	  ASSIGNCOPY(p->name, aName);
	  NSMapInsert(pasteboards, (void*)p, (void*)p->name);
	  [p autorelease];
	}
    }
  p->changeCount = [p->target changeCount];
  [dictionary_lock unlock];
  return p;
}

- (id) _target
{
  return target;
}

@end



/**
 * GNUstep specific extensions ...<br />
 * <p>GNUstep adds a mechanism for mapping between OpenStep pasteboard
 * types and MIME types.  This is useful for inter-operation with other
 * systems, as MIME types have come into common usage (long after the
 * OpenStep specification was created).
 * </p>
 * <p>The other extension to the pasteboard system produced by GNUstep
 * is the ability to keep a history of recent items placed in a
 * pasteboard, and retrieve data from that history rather than just
 * the current item.
 * </p>
 */
@implementation NSPasteboard (GNUstepExtensions)

/**
 * <p>Once the -setChangeCount: message has been sent to an NSPasteboard
 * the object will gain an extra GNUstep behaviour - when getting data
 * from the pasteboard, the data need no longer be from the latest
 * version but may be a version from a previous representation with
 * the specified change count.
 * </p>
 * <p>The value of count must be one which has previously been returned
 * by -declareTypes:owner: and should not be further in the past than
 * specified by the -setHistory: method.
 * </p>
 */
- (void) setChangeCount: (int)count
{
  useHistory = YES;
  changeCount = count;
}

/**
 * Sets the number of changes for which pasteboard data is kept.<br />
 * This is 1 by default.
 */
- (void) setHistory: (unsigned)length
{
  NS_DURING
    {
      [target setHistory: length];
    }
  NS_HANDLER
    {
      [NSException raise: NSPasteboardCommunicationException
		  format: @"%@", [localException reason]];
    }
  NS_ENDHANDLER
}

+ (void) _initMimeMappings
{
  mimeMap = NSCreateMapTable(NSObjectMapKeyCallBacks,
    NSObjectMapValueCallBacks, 0);

  NSMapInsert(mimeMap, (void *)NSStringPboardType,
    (void *)@"text/plain");
  NSMapInsert(mimeMap, (void *)NSFileContentsPboardType, 
    (void *)@"text/plain");
  NSMapInsert(mimeMap, (void *)NSFilenamesPboardType, 
    (void *)@"text/uri-list");
  NSMapInsert(mimeMap, (void *)NSPostScriptPboardType, 
    (void *)@"application/postscript");
  NSMapInsert(mimeMap, (void *)NSTabularTextPboardType, 
    (void *)@"text/tab-separated-values");
  NSMapInsert(mimeMap, (void *)NSRTFPboardType,
    (void *)@"text/richtext");
  NSMapInsert(mimeMap, (void *)NSTIFFPboardType,
    (void *)@"image/tiff");
  NSMapInsert(mimeMap, (void *)NSGeneralPboardType,
    (void *)@"text/plain");
}

/**
 * Return the mapping for pasteboard->mime, or return the original pasteboard
 * type if no mapping is found
 */
+ (NSString *) mimeTypeForPasteboardType: (NSString *)type
{
  NSString	*mime;

  if (mimeMap == NULL)
    {
      [self _initMimeMappings];
    }
  mime = NSMapGet(mimeMap, (void *)type);
  if (mime == nil)
    {
      mime = type;
    }
  return mime;
}

/**
 * Return the mapping for mime->pasteboard, or return the original pasteboard
 * type if no mapping is found. This method may not have a one-to-one 
 * mapping
 */
+ (NSString *) pasteboardTypeForMimeType: (NSString *)mimeType
{
  BOOL			found;
  NSString		*type;
  NSString		*mime;
  NSMapEnumerator	enumerator;
  
  if (mimeMap == NULL)
    {
      [self _initMimeMappings];
    }
  enumerator = NSEnumerateMapTable(mimeMap);
  while ((found = NSNextMapEnumeratorPair(&enumerator, 
    (void **)(&type), (void **)(&mime))))
    {
      if ([mimeType isEqual: mime])
	{
	  break;
	}
    }

  if (found == NO)
    {
      type = mimeType;
    }
  return type;
}

@end

/**
 * Category of NSURL providing convenience methods.
 */
@implementation NSURL (NSPasteboard)
/**
 * Creates a URL with data (of NSURLPboardType) from pasteBoard.
 */
+ (NSURL *) URLFromPasteboard: (NSPasteboard *)pasteBoard
{
  return [self URLWithString: [pasteBoard stringForType: NSURLPboardType]];
}

/**
 * Writes the receiver (as data of NSURLPboardType) to pasteBoard.
 */
- (void) writeToPasteboard: (NSPasteboard *)pasteBoard
{
  [pasteBoard setString: [self absoluteString]
		forType: NSURLPboardType];
}

@end


/**
 * <p>Returns a standardised pasteboard type for file contents,
 * formed from the supplied file extension.
 * </p>
 * <p>Data written to a pasteboard with a file contents type should
 * be written using the [NSPasteboard-writeFileContents:] or
 * [NSPasteboard-writeFileWrapper:] method.  Similarly, the data should
 * be read using the [NSPasteboard-readFileContentsType:toFile:] or
 * [NSPasteboard-readFileWrapper] method.
 * </p>
 */
NSString*
NSCreateFileContentsPboardType(NSString *fileType)
{
  NSString	*ext = [fileType pathExtension];

  if ([ext length] == 0)
    {
      ext = fileType;
    }
  return [NSString stringWithFormat: @"%@%@", contentsPrefix, ext];
}

/**
 * <p>Returns a standardised pasteboard type for file names,
 * formed from the supplied file extension.
 * </p>
 * <p>Data written to a pasteboard with a file names type should
 * be a single name written using [NSPasteboard-setString:forType:] or
 * an array of strings written using
 * [NSPasteboard-setPropertyList:forType:].<br />
 * Similarly, the data should be read using 
 * the [NSPasteboard-stringForType:] or
 * [NSPasteboard-propertyListForType:] method.
 * </p>
 * <p>See also the NSGetFileType() and NSGetFileTypes() functions.
 * </p>
 */
NSString*
NSCreateFilenamePboardType(NSString *fileType)
{
  NSString	*ext = [fileType pathExtension];

  if ([ext length] == 0)
    {
      ext = fileType;
    }
  return [NSString stringWithFormat: @"%@%@", namePrefix, ext];
}

/**
 * Returns the file type (fileType extension) corresponding to the
 * pasteboard type given.<br />
 * This is a counterpart to the NSCreateFilenamePboardType() function.
 */
NSString*
NSGetFileType(NSString *pboardType)
{
  if ([pboardType hasPrefix: contentsPrefix])
    {
      return [pboardType substringFromIndex: [contentsPrefix length]];
    }
  if ([pboardType hasPrefix: namePrefix])
    {
      return [pboardType substringFromIndex: [namePrefix length]];
    }
  return nil;
}

/**
 * Returns the file types (filename extensions) corresponding to the
 * pasteboard types given.
 */
NSArray*
NSGetFileTypes(NSArray *pboardTypes)
{
  NSMutableArray *a = [NSMutableArray arrayWithCapacity: [pboardTypes count]];
  unsigned int	i;

  for (i = 0; i < [pboardTypes count]; i++)
    {
      NSString	*s = NSGetFileType([pboardTypes objectAtIndex: i]);

      if (s && ! [a containsObject: s])
	{
	  [a addObject: s];
	}
    }
  if ([a count] > 0)
    {
      return AUTORELEASE([a copy]);
    }
  return nil;
}

/*
 * The following dummy classes are here solely as a workaround for pre 3.3
 * versions of gcc where protocols didn't work properly unless implemented
 * in the source where the '@protocol()' directive is used.
 */
@interface NSPasteboardServerDummy : NSObject <GSPasteboardSvr>
- (id<GSPasteboardObj>) pasteboardWithName: (in bycopy NSString*)name;
@end
@implementation NSPasteboardServerDummy
- (id<GSPasteboardObj>) pasteboardWithName: (in bycopy NSString*)name
{
  return nil;
}
@end
@interface NSPasteboardObjectDummy : NSObject <GSPasteboardObj>
- (int) addTypes: (in bycopy NSArray*)types
           owner: (id)owner
      pasteboard: (NSPasteboard*)pb
        oldCount: (int)count;
- (NSString*) availableTypeFromArray: (in bycopy NSArray*)types
                         changeCount: (int*)count;
- (int) changeCount;
- (NSData*) dataForType: (in bycopy NSString*)type
               oldCount: (int)count
          mustBeCurrent: (BOOL)flag;
- (int) declareTypes: (in bycopy NSArray*)types
               owner: (id)owner
          pasteboard: (NSPasteboard*)pb;
- (NSString*) name;
- (void) releaseGlobally;
- (BOOL) setData: (in bycopy NSData*)data
         forType: (in bycopy NSString*)type
          isFile: (BOOL)flag
        oldCount: (int)count;
- (void) setHistory: (unsigned)length;
- (bycopy NSArray*) typesAndChangeCount: (int*)count;
@end
@implementation NSPasteboardObjectDummy
- (int) addTypes: (in bycopy NSArray*)types
           owner: (id)owner
      pasteboard: (NSPasteboard*)pb
        oldCount: (int)count
{
  return 0;
}
- (NSString*) availableTypeFromArray: (in bycopy NSArray*)types
                         changeCount: (int*)count
{
  return nil;
}
- (int) changeCount
{
  return 0;
}
- (NSData*) dataForType: (in bycopy NSString*)type
               oldCount: (int)count
          mustBeCurrent: (BOOL)flag
{
  return nil;
}
- (int) declareTypes: (in bycopy NSArray*)types
               owner: (id)owner
          pasteboard: (NSPasteboard*)pb
{
  return 0;
}
- (NSString*) name
{
  return nil;
}
- (void) releaseGlobally
{
}
- (BOOL) setData: (in bycopy NSData*)data
         forType: (in bycopy NSString*)type
          isFile: (BOOL)flag
        oldCount: (int)count
{
  return NO;
}
- (void) setHistory: (unsigned)length
{
}
- (bycopy NSArray*) typesAndChangeCount: (int*)count
{
  return nil;
}
@end