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
|
/****************************************************************************
**
** Implementation of TQFtp class.
**
** Created : 970521
**
** Copyright (C) 1997-2008 Trolltech ASA. All rights reserved.
**
** This file is part of the network module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file. Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/
#include "tqftp.h"
#ifndef TQT_NO_NETWORKPROTOCOL_FTP
#include "tqsocket.h"
#include "tqsocketdevice.h"
#include "tqurlinfo.h"
#include "tqurloperator.h"
#include "tqstringlist.h"
#include "tqregexp.h"
#include "tqtimer.h"
#include "tqfileinfo.h"
#include "tqptrdict.h" // binary compatibility
#ifndef TQT_NO_TEXTCODEC
#include "tqtextcodec.h"
#endif
//#define TQFTPPI_DEBUG
//#define TQFTPDTP_DEBUG
class TQFtpPI;
class TQFtpDTP : public TQObject
{
Q_OBJECT
TQ_OBJECT
public:
enum ConnectState {
CsHostFound,
CsConnected,
CsClosed,
CsHostNotFound,
CsConnectionRefused
};
TQFtpDTP( TQFtpPI *p, TQObject *tqparent=0, const char *name=0 );
void setData( TQByteArray * );
void setDevice( TQIODevice * );
void writeData();
void setBytesTotal( int bytes )
{
bytesTotal = bytes;
bytesDone = 0;
emit dataTransferProgress( bytesDone, bytesTotal );
}
bool hasError() const;
TQString errorMessage() const;
void clearError();
void connectToHost( const TQString & host, TQ_UINT16 port )
{ socket.connectToHost( host, port ); }
TQSocket::State socketState() const
{ return socket.state(); }
TQ_ULONG bytesAvailable() const
{ return socket.bytesAvailable(); }
TQ_LONG readBlock( char *data, TQ_ULONG maxlen )
{
TQ_LONG read = socket.readBlock( data, maxlen );
bytesDone += read;
return read;
}
TQByteArray readAll()
{
TQByteArray tmp = TQT_TQBYTEARRAY_OBJECT(socket.readAll());
bytesDone += tmp.size();
return tmp;
}
void abortConnection();
static bool parseDir( const TQString &buffer, const TQString &userName, TQUrlInfo *info );
Q_SIGNALS:
void listInfo( const TQUrlInfo& );
void readyRead();
void dataTransferProgress( int, int );
void connectState( int );
private Q_SLOTS:
void socketConnected();
void socketReadyRead();
void socketError( int );
void socketConnectionClosed();
void socketBytesWritten( int );
private:
void clearData()
{
is_ba = FALSE;
data.dev = 0;
}
TQSocket socket;
TQFtpPI *pi;
TQString err;
int bytesDone;
int bytesTotal;
bool callWriteData;
// If is_ba is TRUE, ba is used; ba is never 0.
// Otherwise dev is used; dev can be 0 or not.
union {
TQByteArray *ba;
TQIODevice *dev;
} data;
bool is_ba;
};
class TQFtpPI : public TQObject
{
Q_OBJECT
TQ_OBJECT
public:
TQFtpPI( TQObject *tqparent = 0 );
void connectToHost( const TQString &host, TQ_UINT16 port );
bool sendCommands( const TQStringList &cmds );
bool sendCommand( const TQString &cmd )
{ return sendCommands( TQStringList( cmd ) ); }
void clearPendingCommands();
void abort();
TQString currentCommand() const
{ return currentCmd; }
bool rawCommand;
TQFtpDTP dtp; // the PI has a DTP which is not the design of RFC 959, but it
// makes the design simpler this way
Q_SIGNALS:
void connectState( int );
void finished( const TQString& );
void error( int, const TQString& );
void rawFtpReply( int, const TQString& );
private Q_SLOTS:
void hostFound();
void connected();
void connectionClosed();
void delayedCloseFinished();
void readyRead();
void error( int );
void dtpConnectState( int );
private:
// the states are modelled after the generalized state diagram of RFC 959,
// page 58
enum State {
Begin,
Idle,
Waiting,
Success,
Failure
};
enum AbortState {
None,
AbortStarted,
WaitForAbortToFinish
};
bool processReply();
bool startNextCmd();
TQSocket commandSocket;
TQString replyText;
char replyCode[3];
State state;
AbortState abortState;
TQStringList pendingCommands;
TQString currentCmd;
bool waitForDtpToConnect;
bool waitForDtpToClose;
};
/**********************************************************************
*
* TQFtpCommand implemenatation
*
*********************************************************************/
class TQFtpCommand
{
public:
TQFtpCommand( TQFtp::Command cmd, TQStringList raw );
TQFtpCommand( TQFtp::Command cmd, TQStringList raw, const TQByteArray &ba );
TQFtpCommand( TQFtp::Command cmd, TQStringList raw, TQIODevice *dev );
~TQFtpCommand();
int id;
TQFtp::Command command;
TQStringList rawCmds;
// If is_ba is TRUE, ba is used; ba is never 0.
// Otherwise dev is used; dev can be 0 or not.
union {
TQByteArray *ba;
TQIODevice *dev;
} data;
bool is_ba;
static int idCounter;
};
int TQFtpCommand::idCounter = 0;
TQFtpCommand::TQFtpCommand( TQFtp::Command cmd, TQStringList raw )
: command(cmd), rawCmds(raw), is_ba(FALSE)
{
id = ++idCounter;
data.dev = 0;
}
TQFtpCommand::TQFtpCommand( TQFtp::Command cmd, TQStringList raw, const TQByteArray &ba )
: command(cmd), rawCmds(raw), is_ba(TRUE)
{
id = ++idCounter;
data.ba = new TQByteArray( ba );
}
TQFtpCommand::TQFtpCommand( TQFtp::Command cmd, TQStringList raw, TQIODevice *dev )
: command(cmd), rawCmds(raw), is_ba(FALSE)
{
id = ++idCounter;
data.dev = dev;
}
TQFtpCommand::~TQFtpCommand()
{
if ( is_ba )
delete data.ba;
}
/**********************************************************************
*
* TQFtpDTP implemenatation
*
*********************************************************************/
TQFtpDTP::TQFtpDTP( TQFtpPI *p, TQObject *tqparent, const char *name ) :
TQObject( tqparent, name ),
socket( 0, "TQFtpDTP_socket" ),
pi( p ),
callWriteData( FALSE )
{
clearData();
connect( &socket, TQT_SIGNAL( connected() ),
TQT_SLOT( socketConnected() ) );
connect( &socket, TQT_SIGNAL( readyRead() ),
TQT_SLOT( socketReadyRead() ) );
connect( &socket, TQT_SIGNAL( error(int) ),
TQT_SLOT( socketError(int) ) );
connect( &socket, TQT_SIGNAL( connectionClosed() ),
TQT_SLOT( socketConnectionClosed() ) );
connect( &socket, TQT_SIGNAL( bytesWritten(int) ),
TQT_SLOT( socketBytesWritten(int) ) );
}
void TQFtpDTP::setData( TQByteArray *ba )
{
is_ba = TRUE;
data.ba = ba;
}
void TQFtpDTP::setDevice( TQIODevice *dev )
{
is_ba = FALSE;
data.dev = dev;
}
void TQFtpDTP::writeData()
{
if ( is_ba ) {
#if defined(TQFTPDTP_DEBUG)
qDebug( "TQFtpDTP::writeData: write %d bytes", data.ba->size() );
#endif
if ( data.ba->size() == 0 )
emit dataTransferProgress( 0, bytesTotal );
else
socket.writeBlock( data.ba->data(), data.ba->size() );
socket.close();
clearData();
} else if ( data.dev ) {
callWriteData = FALSE;
const int blockSize = 16*1024;
char buf[blockSize];
while ( !data.dev->atEnd() && socket.bytesToWrite()==0 ) {
TQ_LONG read = data.dev->readBlock( buf, blockSize );
#if defined(TQFTPDTP_DEBUG)
qDebug( "TQFtpDTP::writeData: writeBlock() of size %d bytes", (int)read );
#endif
socket.writeBlock( buf, read );
if ( !data.dev )
return; // this can happen when a command is aborted
}
if ( data.dev->atEnd() ) {
if ( bytesDone==0 && socket.bytesToWrite()==0 )
emit dataTransferProgress( 0, bytesTotal );
socket.close();
clearData();
} else {
callWriteData = TRUE;
}
}
}
inline bool TQFtpDTP::hasError() const
{
return !err.isNull();
}
inline TQString TQFtpDTP::errorMessage() const
{
return err;
}
inline void TQFtpDTP::clearError()
{
err = TQString::null;
}
void TQFtpDTP::abortConnection()
{
#if defined(TQFTPDTP_DEBUG)
qDebug( "TQFtpDTP::abortConnection" );
#endif
callWriteData = FALSE;
clearData();
socket.clearPendingData();
socket.close();
}
bool TQFtpDTP::parseDir( const TQString &buffer, const TQString &userName, TQUrlInfo *info )
{
TQStringList lst = TQStringList::split( " ", buffer );
if ( lst.count() < 9 )
return FALSE;
TQString tmp;
// permissions
tmp = lst[ 0 ];
if ( tmp[ 0 ] == TQChar( 'd' ) ) {
info->setDir( TRUE );
info->setFile( FALSE );
info->setSymLink( FALSE );
} else if ( tmp[ 0 ] == TQChar( '-' ) ) {
info->setDir( FALSE );
info->setFile( TRUE );
info->setSymLink( FALSE );
} else if ( tmp[ 0 ] == TQChar( 'l' ) ) {
info->setDir( TRUE ); // #### todo
info->setFile( FALSE );
info->setSymLink( TRUE );
} else {
return FALSE;
}
static int user = 0;
static int group = 1;
static int other = 2;
static int readable = 0;
static int writable = 1;
static int executable = 2;
bool perms[ 3 ][ 3 ];
perms[0][0] = (tmp[ 1 ] == 'r');
perms[0][1] = (tmp[ 2 ] == 'w');
perms[0][2] = (tmp[ 3 ] == 'x');
perms[1][0] = (tmp[ 4 ] == 'r');
perms[1][1] = (tmp[ 5 ] == 'w');
perms[1][2] = (tmp[ 6 ] == 'x');
perms[2][0] = (tmp[ 7 ] == 'r');
perms[2][1] = (tmp[ 8 ] == 'w');
perms[2][2] = (tmp[ 9 ] == 'x');
// owner
tmp = lst[ 2 ];
info->setOwner( tmp );
// group
tmp = lst[ 3 ];
info->setGroup( tmp );
// ### not correct
info->setWritable( ( userName == info->owner() && perms[ user ][ writable ] ) ||
perms[ other ][ writable ] );
info->setReadable( ( userName == info->owner() && perms[ user ][ readable ] ) ||
perms[ other ][ readable ] );
int p = 0;
if ( perms[ user ][ readable ] )
p |= TQUrlInfo::ReadOwner;
if ( perms[ user ][ writable ] )
p |= TQUrlInfo::WriteOwner;
if ( perms[ user ][ executable ] )
p |= TQUrlInfo::ExeOwner;
if ( perms[ group ][ readable ] )
p |= TQUrlInfo::ReadGroup;
if ( perms[ group ][ writable ] )
p |= TQUrlInfo::WriteGroup;
if ( perms[ group ][ executable ] )
p |= TQUrlInfo::ExeGroup;
if ( perms[ other ][ readable ] )
p |= TQUrlInfo::ReadOther;
if ( perms[ other ][ writable ] )
p |= TQUrlInfo::WriteOther;
if ( perms[ other ][ executable ] )
p |= TQUrlInfo::ExeOther;
info->setPermissions( p );
// size
tmp = lst[ 4 ];
info->setSize( tmp.toInt() );
// date and time
TQTime time;
TQString dateStr;
dateStr += "Sun ";
lst[ 5 ][ 0 ] = lst[ 5 ][ 0 ].upper();
dateStr += lst[ 5 ];
dateStr += ' ';
dateStr += lst[ 6 ];
dateStr += ' ';
if ( lst[ 7 ].contains( ":" ) ) {
time = TQTime( lst[ 7 ].left( 2 ).toInt(), lst[ 7 ].right( 2 ).toInt() );
dateStr += TQString::number( TQDate::currentDate().year() );
} else {
dateStr += lst[ 7 ];
}
TQDate date = TQT_TQDATE_OBJECT(TQDate::fromString( dateStr ));
info->setLastModified( TQDateTime( date, time ) );
if ( lst[ 7 ].contains( ":" ) ) {
const int futureTolerance = 600;
if( info->lastModified().secsTo( TQDateTime::tqcurrentDateTime() ) < -futureTolerance ) {
TQDateTime dt = info->lastModified();
TQDate d = TQT_TQDATE_OBJECT(dt.date());
d.setYMD(d.year()-1, d.month(), d.day());
dt.setDate(d);
info->setLastModified(dt);
}
}
// name
if ( info->isSymLink() )
info->setName( lst[ 8 ].stripWhiteSpace() );
else {
TQString n;
for ( uint i = 8; i < lst.count(); ++i )
n += lst[ i ] + " ";
n = n.stripWhiteSpace();
info->setName( n );
}
return TRUE;
}
void TQFtpDTP::socketConnected()
{
#if !defined (TQ_WS_TQWS)
// Use a large send buffer to reduce the number
// of writeBlocks when download and uploading files.
// The actual size used here (128k) is default on most
// Unixes.
socket.socketDevice()->setSendBufferSize(128 * 1024);
socket.socketDevice()->setReceiveBufferSize(128 * 1024);
#endif
bytesDone = 0;
#if defined(TQFTPDTP_DEBUG)
qDebug( "TQFtpDTP::connectState( CsConnected )" );
#endif
emit connectState( TQFtpDTP::CsConnected );
}
void TQFtpDTP::socketReadyRead()
{
if ( pi->currentCommand().isEmpty() ) {
socket.close();
#if defined(TQFTPDTP_DEBUG)
qDebug( "TQFtpDTP::connectState( CsClosed )" );
#endif
emit connectState( TQFtpDTP::CsClosed );
return;
}
if ( pi->currentCommand().startsWith("LIST") ) {
while ( socket.canReadLine() ) {
TQUrlInfo i;
TQString line = socket.readLine();
#if defined(TQFTPDTP_DEBUG)
qDebug( "TQFtpDTP read (list): '%s'", line.latin1() );
#endif
if ( parseDir( line, "", &i ) ) {
emit listInfo( i );
} else {
// some FTP servers don't return a 550 if the file or directory
// does not exist, but rather write a text to the data socket
// -- try to catch these cases
if ( line.endsWith( "No such file or directory\r\n" ) )
err = line;
}
}
} else {
if ( !is_ba && data.dev ) {
TQByteArray ba( socket.bytesAvailable() );
TQ_LONG bytesRead = socket.readBlock( ba.data(), ba.size() );
if ( bytesRead < 0 ) {
// ### error handling
return;
}
ba.resize( bytesRead );
bytesDone += bytesRead;
#if defined(TQFTPDTP_DEBUG)
qDebug( "TQFtpDTP read: %d bytes (total %d bytes)", (int)bytesRead, bytesDone );
#endif
emit dataTransferProgress( bytesDone, bytesTotal );
if (data.dev) // make sure it wasn't deleted in the slot
data.dev->writeBlock( ba );
} else {
#if defined(TQFTPDTP_DEBUG)
qDebug( "TQFtpDTP readyRead: %d bytes available (total %d bytes read)", (int)bytesAvailable(), bytesDone );
#endif
emit dataTransferProgress( bytesDone+socket.bytesAvailable(), bytesTotal );
emit readyRead();
}
}
}
void TQFtpDTP::socketError( int e )
{
if ( e == TQSocket::ErrHostNotFound ) {
#if defined(TQFTPDTP_DEBUG)
qDebug( "TQFtpDTP::connectState( CsHostNotFound )" );
#endif
emit connectState( TQFtpDTP::CsHostNotFound );
} else if ( e == TQSocket::ErrConnectionRefused ) {
#if defined(TQFTPDTP_DEBUG)
qDebug( "TQFtpDTP::connectState( CsConnectionRefused )" );
#endif
emit connectState( TQFtpDTP::CsConnectionRefused );
}
}
void TQFtpDTP::socketConnectionClosed()
{
if ( !is_ba && data.dev ) {
clearData();
}
#if defined(TQFTPDTP_DEBUG)
qDebug( "TQFtpDTP::connectState( CsClosed )" );
#endif
emit connectState( TQFtpDTP::CsClosed );
}
void TQFtpDTP::socketBytesWritten( int bytes )
{
bytesDone += bytes;
#if defined(TQFTPDTP_DEBUG)
qDebug( "TQFtpDTP::bytesWritten( %d )", bytesDone );
#endif
emit dataTransferProgress( bytesDone, bytesTotal );
if ( callWriteData )
writeData();
}
/**********************************************************************
*
* TQFtpPI implemenatation
*
*********************************************************************/
TQFtpPI::TQFtpPI( TQObject *tqparent ) :
TQObject( tqparent ),
rawCommand(FALSE),
dtp( this ),
commandSocket( 0, "TQFtpPI_socket" ),
state( Begin ), abortState( None ),
currentCmd( TQString::null ),
waitForDtpToConnect( FALSE ),
waitForDtpToClose( FALSE )
{
connect( &commandSocket, TQT_SIGNAL(hostFound()),
TQT_SLOT(hostFound()) );
connect( &commandSocket, TQT_SIGNAL(connected()),
TQT_SLOT(connected()) );
connect( &commandSocket, TQT_SIGNAL(connectionClosed()),
TQT_SLOT(connectionClosed()) );
connect( &commandSocket, TQT_SIGNAL(delayedCloseFinished()),
TQT_SLOT(delayedCloseFinished()) );
connect( &commandSocket, TQT_SIGNAL(readyRead()),
TQT_SLOT(readyRead()) );
connect( &commandSocket, TQT_SIGNAL(error(int)),
TQT_SLOT(error(int)) );
connect( &dtp, TQT_SIGNAL(connectState(int)),
TQT_SLOT(dtpConnectState(int)) );
}
void TQFtpPI::connectToHost( const TQString &host, TQ_UINT16 port )
{
emit connectState( TQFtp::HostLookup );
commandSocket.connectToHost( host, port );
}
/*
Sends the sequence of commands \a cmds to the FTP server. When the commands
are all done the finished() signal is emitted. When an error occurs, the
error() signal is emitted.
If there are pending commands in the queue this functions returns FALSE and
the \a cmds are not added to the queue; otherwise it returns TRUE.
*/
bool TQFtpPI::sendCommands( const TQStringList &cmds )
{
if ( !pendingCommands.isEmpty() )
return FALSE;
if ( commandSocket.state()!=TQSocket::Connected || state!=Idle ) {
emit error( TQFtp::NotConnected, TQFtp::tr( "Not connected" ) );
return TRUE; // there are no pending commands
}
pendingCommands = cmds;
startNextCmd();
return TRUE;
}
void TQFtpPI::clearPendingCommands()
{
pendingCommands.clear();
dtp.abortConnection();
currentCmd = TQString::null;
state = Idle;
}
void TQFtpPI::abort()
{
pendingCommands.clear();
if ( abortState != None )
// ABOR already sent
return;
abortState = AbortStarted;
#if defined(TQFTPPI_DEBUG)
qDebug( "TQFtpPI send: ABOR" );
#endif
commandSocket.writeBlock( "ABOR\r\n", 6 );
if ( currentCmd.startsWith("STOR ") )
dtp.abortConnection();
}
void TQFtpPI::hostFound()
{
emit connectState( TQFtp::Connecting );
}
void TQFtpPI::connected()
{
state = Begin;
#if defined(TQFTPPI_DEBUG)
// qDebug( "TQFtpPI state: %d [connected()]", state );
#endif
emit connectState( TQFtp::Connected );
}
void TQFtpPI::connectionClosed()
{
commandSocket.close();
emit connectState( TQFtp::Unconnected );
}
void TQFtpPI::delayedCloseFinished()
{
emit connectState( TQFtp::Unconnected );
}
void TQFtpPI::error( int e )
{
if ( e == TQSocket::ErrHostNotFound ) {
emit connectState( TQFtp::Unconnected );
emit error( TQFtp::HostNotFound,
TQFtp::tr( "Host %1 not found" ).arg( commandSocket.peerName() ) );
} else if ( e == TQSocket::ErrConnectionRefused ) {
emit connectState( TQFtp::Unconnected );
emit error( TQFtp::ConnectionRefused,
TQFtp::tr( "Connection refused to host %1" ).arg( commandSocket.peerName() ) );
}
}
void TQFtpPI::readyRead()
{
if ( waitForDtpToClose )
return;
while ( commandSocket.canReadLine() ) {
// read line with respect to line continuation
TQString line = commandSocket.readLine();
if ( replyText.isEmpty() ) {
if ( line.length() < 3 ) {
// ### protocol error
return;
}
const int lowerLimit[3] = {1,0,0};
const int upperLimit[3] = {5,5,9};
for ( int i=0; i<3; i++ ) {
replyCode[i] = line[i].digitValue();
if ( replyCode[i]<lowerLimit[i] || replyCode[i]>upperLimit[i] ) {
// ### protocol error
return;
}
}
}
TQString endOfMultiLine;
endOfMultiLine[0] = '0' + replyCode[0];
endOfMultiLine[1] = '0' + replyCode[1];
endOfMultiLine[2] = '0' + replyCode[2];
endOfMultiLine[3] = ' ';
TQString lineCont( endOfMultiLine );
lineCont[3] = '-';
TQString lineLeft4 = line.left(4);
while ( lineLeft4 != endOfMultiLine ) {
if ( lineLeft4 == lineCont )
replyText += line.mid( 4 ); // strip 'xyz-'
else
replyText += line;
if ( !commandSocket.canReadLine() )
return;
line = commandSocket.readLine();
lineLeft4 = line.left(4);
}
replyText += line.mid( 4 ); // strip reply code 'xyz '
if ( replyText.endsWith("\r\n") )
replyText.truncate( replyText.length()-2 );
if ( processReply() )
replyText = "";
}
}
/*
Process a reply from the FTP server.
Returns TRUE if the reply was processed or FALSE if the reply has to be
processed at a later point.
*/
bool TQFtpPI::processReply()
{
#if defined(TQFTPPI_DEBUG)
// qDebug( "TQFtpPI state: %d [processReply() begin]", state );
if ( replyText.length() < 400 )
qDebug( "TQFtpPI recv: %d %s", 100*replyCode[0]+10*replyCode[1]+replyCode[2], replyText.latin1() );
else
qDebug( "TQFtpPI recv: %d (text skipped)", 100*replyCode[0]+10*replyCode[1]+replyCode[2] );
#endif
// process 226 replies ("Closing Data Connection") only when the data
// connection is really closed to avoid short reads of the DTP
if ( 100*replyCode[0]+10*replyCode[1]+replyCode[2] == 226 ) {
if ( dtp.socketState() != TQSocket::Idle ) {
waitForDtpToClose = TRUE;
return FALSE;
}
}
switch ( abortState ) {
case AbortStarted:
abortState = WaitForAbortToFinish;
break;
case WaitForAbortToFinish:
abortState = None;
return TRUE;
default:
break;
}
// get new state
static const State table[5] = {
/* 1yz 2yz 3yz 4yz 5yz */
Waiting, Success, Idle, Failure, Failure
};
switch ( state ) {
case Begin:
if ( replyCode[0] == 1 ) {
return TRUE;
} else if ( replyCode[0] == 2 ) {
state = Idle;
emit finished( TQFtp::tr( "Connected to host %1" ).arg( commandSocket.peerName() ) );
break;
}
// ### error handling
return TRUE;
case Waiting:
if ( replyCode[0]<0 || replyCode[0]>5 )
state = Failure;
else
state = table[ replyCode[0] - 1 ];
break;
default:
// ### spontaneous message
return TRUE;
}
#if defined(TQFTPPI_DEBUG)
// qDebug( "TQFtpPI state: %d [processReply() intermediate]", state );
#endif
// special actions on certain replies
int replyCodeInt = 100*replyCode[0] + 10*replyCode[1] + replyCode[2];
emit rawFtpReply( replyCodeInt, replyText );
if ( rawCommand ) {
rawCommand = FALSE;
} else if ( replyCodeInt == 227 ) {
// 227 Entering Passive Mode (h1,h2,h3,h4,p1,p2)
// rfc959 does not define this response precisely, and gives
// both examples where the parenthesis are used, and where
// they are missing. We need to scan for the address and host
// info.
TQRegExp addrPortPattern("(\\d+),(\\d+),(\\d+),(\\d+),(\\d+),(\\d+)");
if (addrPortPattern.search(replyText) == -1) {
#if defined(TQFTPPI_DEBUG)
qDebug( "TQFtp: bad 227 response -- address and port information missing" );
#endif
// ### error handling
} else {
QStringList lst = addrPortPattern.capturedTexts();
TQString host = lst[1] + "." + lst[2] + "." + lst[3] + "." + lst[4];
TQ_UINT16 port = ( lst[5].toUInt() << 8 ) + lst[6].toUInt();
waitForDtpToConnect = TRUE;
dtp.connectToHost( host, port );
}
} else if ( replyCodeInt == 230 ) {
if ( currentCmd.startsWith("USER ") && pendingCommands.count()>0 &&
pendingCommands.first().startsWith("PASS ") ) {
// no need to send the PASS -- we are already logged in
pendingCommands.pop_front();
}
// 230 User logged in, proceed.
emit connectState( TQFtp::LoggedIn );
} else if ( replyCodeInt == 213 ) {
// 213 File status.
if ( currentCmd.startsWith("SIZE ") )
dtp.setBytesTotal( replyText.simplifyWhiteSpace().toInt() );
} else if ( replyCode[0]==1 && currentCmd.startsWith("STOR ") ) {
dtp.writeData();
}
// react on new state
switch ( state ) {
case Begin:
// ### should never happen
break;
case Success:
// ### success handling
state = Idle;
// no break!
case Idle:
if ( dtp.hasError() ) {
emit error( TQFtp::UnknownError, dtp.errorMessage() );
dtp.clearError();
}
startNextCmd();
break;
case Waiting:
// ### do nothing
break;
case Failure:
emit error( TQFtp::UnknownError, replyText );
state = Idle;
startNextCmd();
break;
}
#if defined(TQFTPPI_DEBUG)
// qDebug( "TQFtpPI state: %d [processReply() end]", state );
#endif
return TRUE;
}
#ifndef TQT_NO_TEXTCODEC
TQM_EXPORT_FTP TQTextCodec *qt_ftp_filename_codec = 0;
#endif
/*
Starts next pending command. Returns FALSE if there are no pending commands,
otherwise it returns TRUE.
*/
bool TQFtpPI::startNextCmd()
{
if ( waitForDtpToConnect )
// don't process any new commands until we are connected
return TRUE;
#if defined(TQFTPPI_DEBUG)
if ( state != Idle )
qDebug( "TQFtpPI startNextCmd: Internal error! TQFtpPI called in non-Idle state %d", state );
#endif
if ( pendingCommands.isEmpty() ) {
currentCmd = TQString::null;
emit finished( replyText );
return FALSE;
}
currentCmd = pendingCommands.first();
pendingCommands.pop_front();
#if defined(TQFTPPI_DEBUG)
qDebug( "TQFtpPI send: %s", currentCmd.left( currentCmd.length()-2 ).latin1() );
#endif
state = Waiting;
#ifndef TQT_NO_TEXTCODEC
if ( qt_ftp_filename_codec ) {
int len = 0;
TQCString enc = qt_ftp_filename_codec->fromUnicode(currentCmd,len);
commandSocket.writeBlock( enc.data(), len );
} else
#endif
{
commandSocket.writeBlock( currentCmd.latin1(), currentCmd.length() );
}
return TRUE;
}
void TQFtpPI::dtpConnectState( int s )
{
switch ( s ) {
case TQFtpDTP::CsClosed:
if ( waitForDtpToClose ) {
// there is an unprocessed reply
if ( processReply() )
replyText = "";
else
return;
}
waitForDtpToClose = FALSE;
readyRead();
return;
case TQFtpDTP::CsConnected:
waitForDtpToConnect = FALSE;
startNextCmd();
return;
case TQFtpDTP::CsHostNotFound:
case TQFtpDTP::CsConnectionRefused:
emit error( TQFtp::ConnectionRefused,
TQFtp::tr( "Connection refused for data connection" ) );
startNextCmd();
return;
default:
return;
}
}
/**********************************************************************
*
* TQFtpPrivate
*
*********************************************************************/
class TQFtpPrivate
{
public:
TQFtpPrivate() :
close_waitForStateChange(FALSE),
state( TQFtp::Unconnected ),
error( TQFtp::NoError ),
npWaitForLoginDone( FALSE )
{ pending.setAutoDelete( TRUE ); }
TQFtpPI pi;
TQPtrList<TQFtpCommand> pending;
bool close_waitForStateChange;
TQFtp::State state;
TQFtp::Error error;
TQString errorString;
bool npWaitForLoginDone;
};
static TQPtrDict<TQFtpPrivate> *d_ptr = 0;
static void cleanup_d_ptr()
{
delete d_ptr;
d_ptr = 0;
}
static TQFtpPrivate* d( const TQFtp* foo )
{
if ( !d_ptr ) {
d_ptr = new TQPtrDict<TQFtpPrivate>;
d_ptr->setAutoDelete( TRUE );
qAddPostRoutine( cleanup_d_ptr );
}
TQFtpPrivate* ret = d_ptr->find( (void*)foo );
if ( ! ret ) {
ret = new TQFtpPrivate;
d_ptr->replace( (void*) foo, ret );
}
return ret;
}
static void delete_d( const TQFtp* foo )
{
if ( d_ptr )
d_ptr->remove( (void*) foo );
}
/**********************************************************************
*
* TQFtp implementation
*
*********************************************************************/
/*!
\class TQFtp tqftp.h
\brief The TQFtp class provides an implementation of the FTP protocol.
\if defined(commercial)
It is part of the <a href="commercialeditions.html">TQt Enterprise Edition</a>.
\endif
\ingroup io
\module network
This class provides two different interfaces: one is the
TQNetworkProtocol interface that allows you to use FTP through the
TQUrlOperator abstraction. The other is a direct interface to FTP
that gives you lower-level access to the FTP protocol for finer
control. Using the direct interface you can also execute arbitrary
FTP commands.
Don't mix the two interfaces, since the behavior is not
well-defined.
If you want to use TQFtp with the TQNetworkProtocol interface, you
do not use it directly, but rather through a TQUrlOperator, for
example:
\code
TQUrlOperator op( "ftp://ftp.trolltech.com" );
op.listChildren(); // Asks the server to provide a directory listing
\endcode
This code will only work if the TQFtp class is registered; to
register the class, you must call qInitNetworkProtocols() before
using a TQUrlOperator with TQFtp.
The rest of this descrption describes the direct interface to FTP.
The class works asynchronously, so there are no blocking
functions. If an operation cannot be executed immediately, the
function will still return straight away and the operation will be
scheduled for later execution. The results of scheduled operations
are reported via Q_SIGNALS. This approach depends on the event loop
being in operation.
The operations that can be scheduled (they are called "commands"
in the rest of the documentation) are the following:
connectToHost(), login(), close(), list(), cd(), get(), put(),
remove(), mkdir(), rmdir(), rename() and rawCommand().
All of these commands return a unique identifier that allows you
to keep track of the command that is currently being executed.
When the execution of a command starts, the commandStarted()
signal with the command's identifier is emitted. When the command
is finished, the commandFinished() signal is emitted with the
command's identifier and a bool that indicates whether the command
finished with an error.
In some cases, you might want to execute a sequence of commands,
e.g. if you want to connect and login to a FTP server. This is
simply achieved:
\code
TQFtp *ftp = new TQFtp( this ); // this is an optional TQObject tqparent
ftp->connectToHost( "ftp.trolltech.com" );
ftp->login();
\endcode
In this case two FTP commands have been scheduled. When the last
scheduled command has finished, a done() signal is emitted with
a bool argument that tells you whether the sequence finished with
an error.
If an error occurs during the execution of one of the commands in
a sequence of commands, all the pending commands (i.e. scheduled,
but not yet executed commands) are cleared and no Q_SIGNALS are
emitted for them.
Some commands, e.g. list(), emit additional Q_SIGNALS to report
their results.
Example: If you want to download the INSTALL file from Trolltech's
FTP server, you would write this:
\code
ftp->connectToHost( "ftp.trolltech.com" ); // id == 1
ftp->login(); // id == 2
ftp->cd( "qt" ); // id == 3
ftp->get( "INSTALL" ); // id == 4
ftp->close(); // id == 5
\endcode
For this example the following sequence of Q_SIGNALS is emitted
(with small variations, depending on network traffic, etc.):
\code
start( 1 )
stateChanged( HostLookup )
stateChanged( Connecting )
stateChanged( Connected )
finished( 1, FALSE )
start( 2 )
stateChanged( LoggedIn )
finished( 2, FALSE )
start( 3 )
finished( 3, FALSE )
start( 4 )
dataTransferProgress( 0, 3798 )
dataTransferProgress( 2896, 3798 )
readyRead()
dataTransferProgress( 3798, 3798 )
readyRead()
finished( 4, FALSE )
start( 5 )
stateChanged( Closing )
stateChanged( Unconnected )
finished( 5, FALSE )
done( FALSE )
\endcode
The dataTransferProgress() signal in the above example is useful
if you want to show a \link TQProgressBar progressbar \endlink to
inform the user about the progress of the download. The
readyRead() signal tells you that there is data ready to be read.
The amount of data can be queried then with the bytesAvailable()
function and it can be read with the readBlock() or readAll()
function.
If the login fails for the above example, the Q_SIGNALS would look
like this:
\code
start( 1 )
stateChanged( HostLookup )
stateChanged( Connecting )
stateChanged( Connected )
finished( 1, FALSE )
start( 2 )
finished( 2, TRUE )
done( TRUE )
\endcode
You can then get details about the error with the error() and
errorString() functions.
The functions currentId() and currentCommand() provide more
information about the currently executing command.
The functions hasPendingCommands() and clearPendingCommands()
allow you to query and clear the list of pending commands.
The safest and easiest way to use the FTP protocol is to use
TQUrlOperator() or the FTP commands described above. If you are an
experienced network programmer and want to have complete control
you can use rawCommand() to execute arbitrary FTP commands.
\sa \link network.html TQt Network Documentation \endlink TQNetworkProtocol, TQUrlOperator TQHttp
*/
/*!
Constructs a TQFtp object.
*/
TQFtp::TQFtp() : TQNetworkProtocol()
{
init();
}
/*!
Constructs a TQFtp object. The \a tqparent and \a name parameters
are passed to the TQObject constructor.
*/
TQFtp::TQFtp( TQObject *tqparent, const char *name ) : TQNetworkProtocol()
{
if ( tqparent )
tqparent->insertChild( this );
TQT_TQOBJECT(this)->setName( name );
init();
}
void TQFtp::init()
{
TQFtpPrivate *d = ::d( this );
d->errorString = tr( "Unknown error" );
connect( &d->pi, TQT_SIGNAL(connectState(int)),
TQT_SLOT(piConnectState(int)) );
connect( &d->pi, TQT_SIGNAL(finished(const TQString&)),
TQT_SLOT(piFinished(const TQString&)) );
connect( &d->pi, TQT_SIGNAL(error(int,const TQString&)),
TQT_SLOT(piError(int,const TQString&)) );
connect( &d->pi, TQT_SIGNAL(rawFtpReply(int,const TQString&)),
TQT_SLOT(piFtpReply(int,const TQString&)) );
connect( &d->pi.dtp, TQT_SIGNAL(readyRead()),
TQT_SIGNAL(readyRead()) );
connect( &d->pi.dtp, TQT_SIGNAL(dataTransferProgress(int,int)),
TQT_SIGNAL(dataTransferProgress(int,int)) );
connect( &d->pi.dtp, TQT_SIGNAL(listInfo(const TQUrlInfo&)),
TQT_SIGNAL(listInfo(const TQUrlInfo&)) );
}
/*!
\enum TQFtp::State
This enum defines the connection state:
\value Unconnected There is no connection to the host.
\value HostLookup A host name lookup is in progress.
\value Connecting An attempt to connect to the host is in progress.
\value Connected Connection to the host has been achieved.
\value LoggedIn Connection and user login have been achieved.
\value Closing The connection is closing down, but it is not yet
closed. (The state will be \c Unconnected when the connection is
closed.)
\sa stateChanged() state()
*/
/*!
\enum TQFtp::Error
This enum identifies the error that occurred.
\value NoError No error occurred.
\value HostNotFound The host name lookup failed.
\value ConnectionRefused The server refused the connection.
\value NotConnected Tried to send a command, but there is no connection to
a server.
\value UnknownError An error other than those specified above
occurred.
\sa error()
*/
/*!
\enum TQFtp::Command
This enum is used as the return value for the currentCommand() function.
This allows you to perform specific actions for particular
commands, e.g. in a FTP client, you might want to clear the
directory view when a list() command is started; in this case you
can simply check in the slot connected to the start() signal if
the currentCommand() is \c List.
\value None No command is being executed.
\value ConnectToHost connectToHost() is being executed.
\value Login login() is being executed.
\value Close close() is being executed.
\value List list() is being executed.
\value Cd cd() is being executed.
\value Get get() is being executed.
\value Put put() is being executed.
\value Remove remove() is being executed.
\value Mkdir mkdir() is being executed.
\value Rmdir rmdir() is being executed.
\value Rename rename() is being executed.
\value RawCommand rawCommand() is being executed.
\sa currentCommand()
*/
/*!
\fn void TQFtp::stateChanged( int state )
This signal is emitted when the state of the connection changes.
The argument \a state is the new state of the connection; it is
one of the \l State values.
It is usually emitted in response to a connectToHost() or close()
command, but it can also be emitted "spontaneously", e.g. when the
server closes the connection unexpectedly.
\sa connectToHost() close() state() State
*/
/*!
\fn void TQFtp::listInfo( const TQUrlInfo &i );
This signal is emitted for each directory entry the list() command
finds. The details of the entry are stored in \a i.
\sa list()
*/
/*!
\fn void TQFtp::commandStarted( int id )
This signal is emitted when processing the command identified by
\a id starts.
\sa commandFinished() done()
*/
/*!
\fn void TQFtp::commandFinished( int id, bool error )
This signal is emitted when processing the command identified by
\a id has finished. \a error is TRUE if an error occurred during
the processing; otherwise \a error is FALSE.
\sa commandStarted() done() error() errorString()
*/
/*!
\fn void TQFtp::done( bool error )
This signal is emitted when the last pending command has finished;
(it is emitted after the last command's commandFinished() signal).
\a error is TRUE if an error occurred during the processing;
otherwise \a error is FALSE.
\sa commandFinished() error() errorString()
*/
/*!
\fn void TQFtp::readyRead()
This signal is emitted in response to a get() command when there
is new data to read.
If you specify a tqdevice as the second argument in the get()
command, this signal is \e not emitted; instead the data is
written directly to the tqdevice.
You can read the data with the readAll() or readBlock() functions.
This signal is useful if you want to process the data in chunks as
soon as it becomes available. If you are only interested in the
complete data, just connect to the commandFinished() signal and
read the data then instead.
\sa get() readBlock() readAll() bytesAvailable()
*/
/*!
\fn void TQFtp::dataTransferProgress( int done, int total )
This signal is emitted in response to a get() or put() request to
indicate the current progress of the download or upload.
\a done is the amount of data that has already been transferred
and \a total is the total amount of data to be read or written. It
is possible that the TQFtp class is not able to determine the total
amount of data that should be transferred, in which case \a total
is 0. (If you connect this signal to a TQProgressBar, the progress
bar shows a busy indicator if the total is 0).
\warning \a done and \a total are not necessarily the size in
bytes, since for large files these values might need to be
"scaled" to avoid overflow.
\sa get() put() TQProgressBar::setProgress()
*/
/*!
\fn void TQFtp::rawCommandReply( int replyCode, const TQString &detail );
This signal is emitted in response to the rawCommand() function.
\a replyCode is the 3 digit reply code and \a detail is the text
that follows the reply code.
\sa rawCommand()
*/
/*!
Connects to the FTP server \a host using port \a port.
The stateChanged() signal is emitted when the state of the
connecting process changes, e.g. to \c HostLookup, then \c
Connecting, then \c Connected.
The function does not block and returns immediately. The command
is scheduled, and its execution is performed asynchronously. The
function returns a unique identifier which is passed by
commandStarted() and commandFinished().
When the command is started the commandStarted() signal is
emitted. When it is finished the commandFinished() signal is
emitted.
\sa stateChanged() commandStarted() commandFinished()
*/
int TQFtp::connectToHost( const TQString &host, TQ_UINT16 port )
{
TQStringList cmds;
cmds << host;
cmds << TQString::number( (uint)port );
return addCommand( new TQFtpCommand( ConnectToHost, cmds ) );
}
/*!
Logs in to the FTP server with the username \a user and the
password \a password.
The stateChanged() signal is emitted when the state of the
connecting process changes, e.g. to \c LoggedIn.
The function does not block and returns immediately. The command
is scheduled, and its execution is performed asynchronously. The
function returns a unique identifier which is passed by
commandStarted() and commandFinished().
When the command is started the commandStarted() signal is
emitted. When it is finished the commandFinished() signal is
emitted.
\sa commandStarted() commandFinished()
*/
int TQFtp::login( const TQString &user, const TQString &password )
{
TQStringList cmds;
cmds << ( TQString("USER ") + ( user.isNull() ? TQString("anonymous") : user ) + "\r\n" );
cmds << ( TQString("PASS ") + ( password.isNull() ? TQString("anonymous@") : password ) + "\r\n" );
return addCommand( new TQFtpCommand( Login, cmds ) );
}
/*!
Closes the connection to the FTP server.
The stateChanged() signal is emitted when the state of the
connecting process changes, e.g. to \c Closing, then \c
Unconnected.
The function does not block and returns immediately. The command
is scheduled, and its execution is performed asynchronously. The
function returns a unique identifier which is passed by
commandStarted() and commandFinished().
When the command is started the commandStarted() signal is
emitted. When it is finished the commandFinished() signal is
emitted.
\sa stateChanged() commandStarted() commandFinished()
*/
int TQFtp::close()
{
return addCommand( new TQFtpCommand( Close, TQStringList("TQUIT\r\n") ) );
}
/*!
Lists the contents of directory \a dir on the FTP server. If \a
dir is empty, it lists the contents of the current directory.
The listInfo() signal is emitted for each directory entry found.
The function does not block and returns immediately. The command
is scheduled, and its execution is performed asynchronously. The
function returns a unique identifier which is passed by
commandStarted() and commandFinished().
When the command is started the commandStarted() signal is
emitted. When it is finished the commandFinished() signal is
emitted.
\sa listInfo() commandStarted() commandFinished()
*/
int TQFtp::list( const TQString &dir )
{
TQStringList cmds;
cmds << "TYPE A\r\n";
cmds << "PASV\r\n";
if ( dir.isEmpty() )
cmds << "LIST\r\n";
else
cmds << ( "LIST " + dir + "\r\n" );
return addCommand( new TQFtpCommand( List, cmds ) );
}
/*!
Changes the working directory of the server to \a dir.
The function does not block and returns immediately. The command
is scheduled, and its execution is performed asynchronously. The
function returns a unique identifier which is passed by
commandStarted() and commandFinished().
When the command is started the commandStarted() signal is
emitted. When it is finished the commandFinished() signal is
emitted.
\sa commandStarted() commandFinished()
*/
int TQFtp::cd( const TQString &dir )
{
return addCommand( new TQFtpCommand( Cd, TQStringList("CWD "+dir+"\r\n") ) );
}
/*!
Downloads the file \a file from the server.
If \a dev is 0, then the readyRead() signal is emitted when there
is data available to read. You can then read the data with the
readBlock() or readAll() functions.
If \a dev is not 0, the data is written directly to the tqdevice \a
dev. Make sure that the \a dev pointer is valid for the duration
of the operation (it is safe to delete it when the
commandFinished() signal is emitted). In this case the readyRead()
signal is \e not emitted and you cannot read data with the
readBlock() or readAll() functions.
If you don't read the data immediately it becomes available, i.e.
when the readyRead() signal is emitted, it is still available
until the next command is started.
For example, if you want to present the data to the user as soon
as there is something available, connect to the readyRead() signal
and read the data immediately. On the other hand, if you only want
to work with the complete data, you can connect to the
commandFinished() signal and read the data when the get() command
is finished.
The function does not block and returns immediately. The command
is scheduled, and its execution is performed asynchronously. The
function returns a unique identifier which is passed by
commandStarted() and commandFinished().
When the command is started the commandStarted() signal is
emitted. When it is finished the commandFinished() signal is
emitted.
\sa readyRead() dataTransferProgress() commandStarted()
commandFinished()
*/
int TQFtp::get( const TQString &file, TQIODevice *dev )
{
TQStringList cmds;
cmds << ( "SIZE " + file + "\r\n" );
cmds << "TYPE I\r\n";
cmds << "PASV\r\n";
cmds << ( "RETR " + file + "\r\n" );
if ( dev )
return addCommand( new TQFtpCommand( Get, cmds, dev ) );
return addCommand( new TQFtpCommand( Get, cmds ) );
}
/*!
\overload
Writes the data \a data to the file called \a file on the server.
The progress of the upload is reported by the
dataTransferProgress() signal.
The function does not block and returns immediately. The command
is scheduled, and its execution is performed asynchronously. The
function returns a unique identifier which is passed by
commandStarted() and commandFinished().
When the command is started the commandStarted() signal is
emitted. When it is finished the commandFinished() signal is
emitted.
\sa dataTransferProgress() commandStarted() commandFinished()
*/
int TQFtp::put( const TQByteArray &data, const TQString &file )
{
TQStringList cmds;
cmds << "TYPE I\r\n";
cmds << "PASV\r\n";
cmds << ( "ALLO " + TQString::number(data.size()) + "\r\n" );
cmds << ( "STOR " + file + "\r\n" );
return addCommand( new TQFtpCommand( Put, cmds, data ) );
}
/*!
Reads the data from the IO tqdevice \a dev, and writes it to the
file called \a file on the server. The data is read in chunks from
the IO tqdevice, so this overload allows you to transmit large
amounts of data without the need to read all the data into memory
at once.
Make sure that the \a dev pointer is valid for the duration of the
operation (it is safe to delete it when the commandFinished() is
emitted).
*/
int TQFtp::put( TQIODevice *dev, const TQString &file )
{
TQStringList cmds;
cmds << "TYPE I\r\n";
cmds << "PASV\r\n";
if ( !dev->isSequentialAccess() )
cmds << ( "ALLO " + TQString::number(dev->size()) + "\r\n" );
cmds << ( "STOR " + file + "\r\n" );
return addCommand( new TQFtpCommand( Put, cmds, dev ) );
}
/*!
Deletes the file called \a file from the server.
The function does not block and returns immediately. The command
is scheduled, and its execution is performed asynchronously. The
function returns a unique identifier which is passed by
commandStarted() and commandFinished().
When the command is started the commandStarted() signal is
emitted. When it is finished the commandFinished() signal is
emitted.
\sa commandStarted() commandFinished()
*/
int TQFtp::remove( const TQString &file )
{
return addCommand( new TQFtpCommand( Remove, TQStringList("DELE "+file+"\r\n") ) );
}
/*!
Creates a directory called \a dir on the server.
The function does not block and returns immediately. The command
is scheduled, and its execution is performed asynchronously. The
function returns a unique identifier which is passed by
commandStarted() and commandFinished().
When the command is started the commandStarted() signal is
emitted. When it is finished the commandFinished() signal is
emitted.
\sa commandStarted() commandFinished()
*/
int TQFtp::mkdir( const TQString &dir )
{
return addCommand( new TQFtpCommand( Mkdir, TQStringList("MKD "+dir+"\r\n") ) );
}
/*!
Removes the directory called \a dir from the server.
The function does not block and returns immediately. The command
is scheduled, and its execution is performed asynchronously. The
function returns a unique identifier which is passed by
commandStarted() and commandFinished().
When the command is started the commandStarted() signal is
emitted. When it is finished the commandFinished() signal is
emitted.
\sa commandStarted() commandFinished()
*/
int TQFtp::rmdir( const TQString &dir )
{
return addCommand( new TQFtpCommand( Rmdir, TQStringList("RMD "+dir+"\r\n") ) );
}
/*!
Renames the file called \a oldname to \a newname on the server.
The function does not block and returns immediately. The command
is scheduled, and its execution is performed asynchronously. The
function returns a unique identifier which is passed by
commandStarted() and commandFinished().
When the command is started the commandStarted() signal is
emitted. When it is finished the commandFinished() signal is
emitted.
\sa commandStarted() commandFinished()
*/
int TQFtp::rename( const TQString &oldname, const TQString &newname )
{
TQStringList cmds;
cmds << ( "RNFR " + oldname + "\r\n" );
cmds << ( "RNTO " + newname + "\r\n" );
return addCommand( new TQFtpCommand( Rename, cmds ) );
}
/*!
Sends the raw FTP command \a command to the FTP server. This is
useful for low-level FTP access. If the operation you wish to
perform has an equivalent TQFtp function, we recommend using the
function instead of raw FTP commands since the functions are
easier and safer.
The function does not block and returns immediately. The command
is scheduled, and its execution is performed asynchronously. The
function returns a unique identifier which is passed by
commandStarted() and commandFinished().
When the command is started the commandStarted() signal is
emitted. When it is finished the commandFinished() signal is
emitted.
\sa rawCommandReply() commandStarted() commandFinished()
*/
int TQFtp::rawCommand( const TQString &command )
{
TQString cmd = command.stripWhiteSpace() + "\r\n";
return addCommand( new TQFtpCommand( RawCommand, TQStringList(cmd) ) );
}
/*!
Returns the number of bytes that can be read from the data socket
at the moment.
\sa get() readyRead() readBlock() readAll()
*/
TQ_ULONG TQFtp::bytesAvailable() const
{
TQFtpPrivate *d = ::d( this );
return d->pi.dtp.bytesAvailable();
}
/*!
Reads \a maxlen bytes from the data socket into \a data and
returns the number of bytes read. Returns -1 if an error occurred.
\sa get() readyRead() bytesAvailable() readAll()
*/
TQ_LONG TQFtp::readBlock( char *data, TQ_ULONG maxlen )
{
TQFtpPrivate *d = ::d( this );
return d->pi.dtp.readBlock( data, maxlen );
}
/*!
Reads all the bytes available from the data socket and returns
them.
\sa get() readyRead() bytesAvailable() readBlock()
*/
TQByteArray TQFtp::readAll()
{
TQFtpPrivate *d = ::d( this );
return d->pi.dtp.readAll();
}
/*!
Aborts the current command and deletes all scheduled commands.
If there is an unfinished command (i.e. a command for which the
commandStarted() signal has been emitted, but for which the
commandFinished() signal has not been emitted), this function
sends an \c ABORT command to the server. When the server replies
that the command is aborted, the commandFinished() signal with the
\c error argument set to \c TRUE is emitted for the command. Due
to timing issues, it is possible that the command had already
finished before the abort request reached the server, in which
case, the commandFinished() signal is emitted with the \c error
argument set to \c FALSE.
For all other commands that are affected by the abort(), no
Q_SIGNALS are emitted.
If you don't start further FTP commands directly after the
abort(), there won't be any scheduled commands and the done()
signal is emitted.
\warning Some FTP servers, for example the BSD FTP daemon (version
0.3), wrongly return a positive reply even when an abort has
occurred. For these servers the commandFinished() signal has its
error flag set to \c FALSE, even though the command did not
complete successfully.
\sa clearPendingCommands()
*/
void TQFtp::abort()
{
TQFtpPrivate *d = ::d( this );
if ( d->pending.isEmpty() )
return;
clearPendingCommands();
d->pi.abort();
}
/*!
Returns the identifier of the FTP command that is being executed
or 0 if there is no command being executed.
\sa currentCommand()
*/
int TQFtp::currentId() const
{
TQFtpPrivate *d = ::d( this );
TQFtpCommand *c = d->pending.getFirst();
if ( c == 0 )
return 0;
return c->id;
}
/*!
Returns the command type of the FTP command being executed or \c
None if there is no command being executed.
\sa currentId()
*/
TQFtp::Command TQFtp::currentCommand() const
{
TQFtpPrivate *d = ::d( this );
TQFtpCommand *c = d->pending.getFirst();
if ( c == 0 )
return None;
return c->command;
}
/*!
Returns the TQIODevice pointer that is used by the FTP command to read data
from or store data to. If there is no current FTP command being executed or
if the command does not use an IO tqdevice, this function returns 0.
This function can be used to delete the TQIODevice in the slot connected to
the commandFinished() signal.
\sa get() put()
*/
TQIODevice* TQFtp::currentDevice() const
{
TQFtpPrivate *d = ::d( this );
TQFtpCommand *c = d->pending.getFirst();
if ( !c )
return 0;
if ( c->is_ba )
return 0;
return c->data.dev;
}
/*!
Returns TRUE if there are any commands scheduled that have not yet
been executed; otherwise returns FALSE.
The command that is being executed is \e not considered as a
scheduled command.
\sa clearPendingCommands() currentId() currentCommand()
*/
bool TQFtp::hasPendingCommands() const
{
TQFtpPrivate *d = ::d( this );
return d->pending.count() > 1;
}
/*!
Deletes all pending commands from the list of scheduled commands.
This does not affect the command that is being executed. If you
want to stop this this as well, use abort().
\sa hasPendingCommands() abort()
*/
void TQFtp::clearPendingCommands()
{
TQFtpPrivate *d = ::d( this );
TQFtpCommand *c = 0;
if ( d->pending.count() > 0 )
c = d->pending.take( 0 );
d->pending.clear();
if ( c )
d->pending.append( c );
}
/*!
Returns the current state of the object. When the state changes,
the stateChanged() signal is emitted.
\sa State stateChanged()
*/
TQFtp::State TQFtp::state() const
{
TQFtpPrivate *d = ::d( this );
return d->state;
}
/*!
Returns the last error that occurred. This is useful to find out
what when wrong when receiving a commandFinished() or a done()
signal with the \c error argument set to \c TRUE.
If you start a new command, the error status is reset to \c NoError.
*/
TQFtp::Error TQFtp::error() const
{
TQFtpPrivate *d = ::d( this );
return d->error;
}
/*!
Returns a human-readable description of the last error that
occurred. This is useful for presenting a error message to the
user when receiving a commandFinished() or a done() signal with
the \c error argument set to \c TRUE.
The error string is often (but not always) the reply from the
server, so it is not always possible to translate the string. If
the message comes from TQt, the string has already passed through
tr().
*/
TQString TQFtp::errorString() const
{
TQFtpPrivate *d = ::d( this );
return d->errorString;
}
int TQFtp::addCommand( TQFtpCommand *cmd )
{
TQFtpPrivate *d = ::d( this );
d->pending.append( cmd );
if ( d->pending.count() == 1 )
// don't emit the commandStarted() signal before the id is returned
TQTimer::singleShot( 0, this, TQT_SLOT(startNextCommand()) );
return cmd->id;
}
void TQFtp::startNextCommand()
{
TQFtpPrivate *d = ::d( this );
TQFtpCommand *c = d->pending.getFirst();
if ( c == 0 )
return;
d->error = NoError;
d->errorString = tr( "Unknown error" );
if ( bytesAvailable() )
readAll(); // clear the data
emit commandStarted( c->id );
if ( c->command == ConnectToHost ) {
d->pi.connectToHost( c->rawCmds[0], c->rawCmds[1].toUInt() );
} else {
if ( c->command == Put ) {
if ( c->is_ba ) {
d->pi.dtp.setData( c->data.ba );
d->pi.dtp.setBytesTotal( c->data.ba->size() );
} else if ( c->data.dev && (c->data.dev->isOpen() || c->data.dev->open(IO_ReadOnly) ) ) {
d->pi.dtp.setDevice( c->data.dev );
if ( c->data.dev->isSequentialAccess() )
d->pi.dtp.setBytesTotal( 0 );
else
d->pi.dtp.setBytesTotal( c->data.dev->size() );
}
} else if ( c->command == Get ) {
if ( !c->is_ba && c->data.dev ) {
d->pi.dtp.setDevice( c->data.dev );
}
} else if ( c->command == Close ) {
d->state = TQFtp::Closing;
emit stateChanged( d->state );
}
if ( !d->pi.sendCommands( c->rawCmds ) ) {
// ### error handling (this case should not happen)
}
}
}
void TQFtp::piFinished( const TQString& )
{
TQFtpPrivate *d = ::d( this );
TQFtpCommand *c = d->pending.getFirst();
if ( c == 0 )
return;
if ( c->command == Close ) {
// The order of in which the Q_SLOTS are called is arbitrary, so
// disconnect the SIGNAL-TQT_SIGNAL temporary to make sure that we
// don't get the commandFinished() signal before the stateChanged()
// signal.
if ( d->state != TQFtp::Unconnected ) {
d->close_waitForStateChange = TRUE;
return;
}
}
emit commandFinished( c->id, FALSE );
d->pending.removeFirst();
if ( d->pending.isEmpty() ) {
emit done( FALSE );
} else {
startNextCommand();
}
}
void TQFtp::piError( int errorCode, const TQString &text )
{
TQFtpPrivate *d = ::d( this );
TQFtpCommand *c = d->pending.getFirst();
// non-fatal errors
if ( c->command==Get && d->pi.currentCommand().startsWith("SIZE ") ) {
d->pi.dtp.setBytesTotal( -1 );
return;
} else if ( c->command==Put && d->pi.currentCommand().startsWith("ALLO ") ) {
return;
}
d->error = (Error)errorCode;
switch ( currentCommand() ) {
case ConnectToHost:
d->errorString = tr( "Connecting to host failed:\n%1" ).arg( text );
break;
case Login:
d->errorString = tr( "Login failed:\n%1" ).arg( text );
break;
case List:
d->errorString = tr( "Listing directory failed:\n%1" ).arg( text );
break;
case Cd:
d->errorString = tr( "Changing directory failed:\n%1" ).arg( text );
break;
case Get:
d->errorString = tr( "Downloading file failed:\n%1" ).arg( text );
break;
case Put:
d->errorString = tr( "Uploading file failed:\n%1" ).arg( text );
break;
case Remove:
d->errorString = tr( "Removing file failed:\n%1" ).arg( text );
break;
case Mkdir:
d->errorString = tr( "Creating directory failed:\n%1" ).arg( text );
break;
case Rmdir:
d->errorString = tr( "Removing directory failed:\n%1" ).arg( text );
break;
default:
d->errorString = text;
break;
}
d->pi.clearPendingCommands();
clearPendingCommands();
emit commandFinished( c->id, TRUE );
d->pending.removeFirst();
if ( d->pending.isEmpty() )
emit done( TRUE );
else
startNextCommand();
}
void TQFtp::piConnectState( int state )
{
TQFtpPrivate *d = ::d( this );
d->state = (State)state;
emit stateChanged( d->state );
if ( d->close_waitForStateChange ) {
d->close_waitForStateChange = FALSE;
piFinished( tr( "Connection closed" ) );
}
}
void TQFtp::piFtpReply( int code, const TQString &text )
{
if ( currentCommand() == RawCommand ) {
TQFtpPrivate *d = ::d( this );
d->pi.rawCommand = TRUE;
emit rawCommandReply( code, text );
}
}
/*!
Destructor.
*/
TQFtp::~TQFtp()
{
abort();
close();
delete_d( this );
}
/**********************************************************************
*
* TQFtp implementation of the TQNetworkProtocol interface
*
*********************************************************************/
/*! \reimp
*/
void TQFtp::operationListChildren( TQNetworkOperation *op )
{
op->setState( StInProgress );
cd( ( url()->path().isEmpty() ? TQString( "/" ) : url()->path() ) );
list();
emit start( op );
}
/*! \reimp
*/
void TQFtp::operationMkDir( TQNetworkOperation *op )
{
op->setState( StInProgress );
mkdir( op->arg( 0 ) );
}
/*! \reimp
*/
void TQFtp::operationRemove( TQNetworkOperation *op )
{
op->setState( StInProgress );
cd( ( url()->path().isEmpty() ? TQString( "/" ) : url()->path() ) );
remove( TQUrl( op->arg( 0 ) ).path() );
}
/*! \reimp
*/
void TQFtp::operationRename( TQNetworkOperation *op )
{
op->setState( StInProgress );
cd( ( url()->path().isEmpty() ? TQString( "/" ) : url()->path() ) );
rename( op->arg( 0 ), op->arg( 1 ));
}
/*! \reimp
*/
void TQFtp::operationGet( TQNetworkOperation *op )
{
op->setState( StInProgress );
TQUrl u( op->arg( 0 ) );
get( u.path() );
}
/*! \reimp
*/
void TQFtp::operationPut( TQNetworkOperation *op )
{
op->setState( StInProgress );
TQUrl u( op->arg( 0 ) );
put( op->rawArg(1), u.path() );
}
/*! \reimp
*/
bool TQFtp::checkConnection( TQNetworkOperation *op )
{
TQFtpPrivate *d = ::d( this );
if ( state() == Unconnected && !d->npWaitForLoginDone ) {
connect( this, TQT_SIGNAL(listInfo(const TQUrlInfo&)),
this, TQT_SLOT(npListInfo(const TQUrlInfo&)) );
connect( this, TQT_SIGNAL(done(bool)),
this, TQT_SLOT(npDone(bool)) );
connect( this, TQT_SIGNAL(stateChanged(int)),
this, TQT_SLOT(npStateChanged(int)) );
connect( this, TQT_SIGNAL(dataTransferProgress(int,int)),
this, TQT_SLOT(npDataTransferProgress(int,int)) );
connect( this, TQT_SIGNAL(readyRead()),
this, TQT_SLOT(npReadyRead()) );
d->npWaitForLoginDone = TRUE;
switch ( op->operation() ) {
case OpGet:
case OpPut:
{
TQUrl u( op->arg( 0 ) );
connectToHost( u.host(), u.port() != -1 ? u.port() : 21 );
}
break;
default:
connectToHost( url()->host(), url()->port() != -1 ? url()->port() : 21 );
break;
}
TQString user = url()->user().isEmpty() ? TQString( "anonymous" ) : url()->user();
TQString pass = url()->password().isEmpty() ? TQString( "anonymous@" ) : url()->password();
login( user, pass );
}
if ( state() == LoggedIn )
return TRUE;
return FALSE;
}
/*! \reimp
*/
int TQFtp::supportedOperations() const
{
return OpListChildren | OpMkDir | OpRemove | OpRename | OpGet | OpPut;
}
/*! \internal
Parses the string, \a buffer, which is one line of a directory
listing which came from the FTP server, and sets the values which
have been parsed to the url info object, \a info.
*/
void TQFtp::parseDir( const TQString &buffer, TQUrlInfo &info )
{
TQFtpDTP::parseDir( buffer, url()->user(), &info );
}
void TQFtp::npListInfo( const TQUrlInfo & i )
{
if ( url() ) {
TQRegExp filt( url()->nameFilter(), FALSE, TRUE );
if ( i.isDir() || filt.search( i.name() ) != -1 ) {
emit newChild( i, operationInProgress() );
}
} else {
emit newChild( i, operationInProgress() );
}
}
void TQFtp::npDone( bool err )
{
TQFtpPrivate *d = ::d( this );
bool emitFinishedSignal = FALSE;
TQNetworkOperation *op = operationInProgress();
if ( op ) {
if ( err ) {
op->setProtocolDetail( errorString() );
op->setState( StFailed );
if ( error() == HostNotFound ) {
op->setErrorCode( (int)ErrHostNotFound );
} else {
switch ( op->operation() ) {
case OpListChildren:
op->setErrorCode( (int)ErrListChildren );
break;
case OpMkDir:
op->setErrorCode( (int)ErrMkDir );
break;
case OpRemove:
op->setErrorCode( (int)ErrRemove );
break;
case OpRename:
op->setErrorCode( (int)ErrRename );
break;
case OpGet:
op->setErrorCode( (int)ErrGet );
break;
case OpPut:
op->setErrorCode( (int)ErrPut );
break;
}
}
emitFinishedSignal = TRUE;
} else if ( !d->npWaitForLoginDone ) {
switch ( op->operation() ) {
case OpRemove:
emit removed( op );
break;
case OpMkDir:
{
TQUrlInfo inf( op->arg( 0 ), 0, "", "", 0, TQDateTime(),
TQDateTime(), TRUE, FALSE, FALSE, TRUE, TRUE, TRUE );
emit newChild( inf, op );
emit createdDirectory( inf, op );
}
break;
case OpRename:
emit itemChanged( operationInProgress() );
break;
default:
break;
}
op->setState( StDone );
emitFinishedSignal = TRUE;
}
}
d->npWaitForLoginDone = FALSE;
if ( state() == Unconnected ) {
disconnect( this, TQT_SIGNAL(listInfo(const TQUrlInfo&)),
this, TQT_SLOT(npListInfo(const TQUrlInfo&)) );
disconnect( this, TQT_SIGNAL(done(bool)),
this, TQT_SLOT(npDone(bool)) );
disconnect( this, TQT_SIGNAL(stateChanged(int)),
this, TQT_SLOT(npStateChanged(int)) );
disconnect( this, TQT_SIGNAL(dataTransferProgress(int,int)),
this, TQT_SLOT(npDataTransferProgress(int,int)) );
disconnect( this, TQT_SIGNAL(readyRead()),
this, TQT_SLOT(npReadyRead()) );
}
// emit the finished() signal at the very end to avoid reentrance problems
if ( emitFinishedSignal )
emit finished( op );
}
void TQFtp::npStateChanged( int state )
{
if ( url() ) {
if ( state == Connecting )
emit connectionStateChanged( ConHostFound, tr( "Host %1 found" ).arg( url()->host() ) );
else if ( state == Connected )
emit connectionStateChanged( ConConnected, tr( "Connected to host %1" ).arg( url()->host() ) );
else if ( state == Unconnected )
emit connectionStateChanged( ConClosed, tr( "Connection to %1 closed" ).arg( url()->host() ) );
} else {
if ( state == Connecting )
emit connectionStateChanged( ConHostFound, tr( "Host found" ) );
else if ( state == Connected )
emit connectionStateChanged( ConConnected, tr( "Connected to host" ) );
else if ( state == Unconnected )
emit connectionStateChanged( ConClosed, tr( "Connection closed" ) );
}
}
void TQFtp::npDataTransferProgress( int bDone, int bTotal )
{
emit TQNetworkProtocol::dataTransferProgress( bDone, bTotal, operationInProgress() );
}
void TQFtp::npReadyRead()
{
emit data( readAll(), operationInProgress() );
}
// ### unused -- delete in TQt 4.0
/*! \internal
*/
void TQFtp::hostFound()
{
}
/*! \internal
*/
void TQFtp::connected()
{
}
/*! \internal
*/
void TQFtp::closed()
{
}
/*! \internal
*/
void TQFtp::dataHostFound()
{
}
/*! \internal
*/
void TQFtp::dataConnected()
{
}
/*! \internal
*/
void TQFtp::dataClosed()
{
}
/*! \internal
*/
void TQFtp::dataReadyRead()
{
}
/*! \internal
*/
void TQFtp::dataBytesWritten( int )
{
}
/*! \internal
*/
void TQFtp::error( int )
{
}
#include "tqftp.tqmoc"
#endif // TQT_NO_NETWORKPROTOCOL_FTP
|