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

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

/*
 * The currently active RFC for URL/URIs is RFC3986
 * Previous (and now deprecated) RFCs are RFC1738 and RFC2396
 */

#include "kurl.h"

// KDE_QT_ONLY is first used for dcop/client (e.g. marshalling)
#ifndef KDE_QT_ONLY
#include <kdebug.h>
#include <kglobal.h>
#include <kidna.h>
#include <kprotocolinfo.h>
#endif

#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>

#include <tqurl.h>
#include <tqdir.h>
#include <tqstringlist.h>
#include <tqregexp.h>
#include <stylesheet.h>
#include <tqmap.h>
#include <textcodec.h>
#include <tqmutex.h>

#ifdef Q_WS_WIN
# define KURL_ROOTDIR_PATH "C:/"
#else
# define KURL_ROOTDIR_PATH "/"
#endif

static const TQString fileProt = "file";

static TQTextCodec * codecForHint( int encoding_hint /* not 0 ! */ )
{
    return TQTextCodec::codecForMib( encoding_hint );
}

// encoding_offset:
// 0 encode both @ and /
// 1 encode @ but not /
// 2 encode neither @ or /
static TQString encode( const TQString& segment, int encoding_offset, int encoding_hint, bool isRawURI = false )
{
  const char *encode_string = "/@<>#\"&?={}|^~[]\'`\\:+%";
  encode_string += encoding_offset;

  TQCString local;
  if (encoding_hint==0)
    local = segment.local8Bit();
  else
  {
      TQTextCodec * textCodec = codecForHint( encoding_hint );
      if (!textCodec)
          local = segment.local8Bit();
      else
          local = textCodec->fromUnicode( segment );
  }

  int old_length = isRawURI ? local.size() - 1 : local.length();

  if ( old_length < 1 )
    return segment.isNull() ? TQString::null : TQString(""); // differentiate null and empty

  // a worst case approximation
  TQChar *new_segment = new TQChar[ old_length * 3 + 1 ];
  int new_length = 0;

  for ( int i = 0; i < old_length; i++ )
  {
    // 'unsave' and 'reserved' characters
    // according to RFC 1738,
    // 2.2. URL Character Encoding Issues (pp. 3-4)
    // WABA: Added non-ascii
    unsigned char character = local[i];
    if ( (character <= 32) || (character >= 127) ||
         strchr(encode_string, character) )
    {
      new_segment[ new_length++ ] = '%';

      unsigned int c = character / 16;
      c += (c > 9) ? ('A' - 10) : '0';
      new_segment[ new_length++ ] = c;

      c = character % 16;
      c += (c > 9) ? ('A' - 10) : '0';
      new_segment[ new_length++ ] = c;

    }
    else
      new_segment[ new_length++ ] = (TQChar)local[i];
  }

  TQString result = TQString(new_segment, new_length);
  delete [] new_segment;
  return result;
}

static TQString encodeHost( const TQString& segment, bool encode_slash, int encoding_hint )
{
  // Hostnames are encoded differently
  // we use the IDNA transformation instead

  // Note: when merging qt-addon, use QResolver::domainToAscii here
#ifndef KDE_QT_ONLY
  Q_UNUSED( encode_slash );
  Q_UNUSED( encoding_hint );
  TQString host = KIDNA::toAscii(segment);
  if (host.isEmpty())
     return segment;
  return host;
#else
  return encode(segment, encode_slash ? 0 : 1, encoding_hint);
#endif
}

static int hex2int( unsigned int _char )
{
  if ( _char >= 'A' && _char <='F')
    return _char - 'A' + 10;
  if ( _char >= 'a' && _char <='f')
    return _char - 'a' + 10;
  if ( _char >= '0' && _char <='9')
    return _char - '0';
  return -1;
}

// WABA: The result of lazy_encode isn't usable for a URL which
// needs to satisfies RFC requirements. However, the following
// operation will make it usable again:
//      encode(decode(...))
//
// As a result one can see that url.prettyURL() does not result in
// a RFC compliant URL but that the following sequence does:
//      KURL(url.prettyURL()).url()


static TQString lazy_encode( const TQString& segment, bool encodeAt=true )
{
  int old_length = segment.length();

  if ( !old_length )
    return TQString::null;

  // a worst case approximation
  TQChar *new_segment = new TQChar[ old_length * 3 + 1 ];
  int new_length = 0;

  for ( int i = 0; i < old_length; i++ )
  {
    unsigned int character = segment[i].unicode(); // Don't use latin1()
                                                   // It returns 0 for non-latin1 values
    // Small set of really ambiguous chars
    if ((character < 32) ||  // Low ASCII
        ((character == '%') && // The escape character itself
           (i+2 < old_length) && // But only if part of a valid escape sequence!
          (hex2int(segment[i+1].unicode())!= -1) &&
          (hex2int(segment[i+2].unicode())!= -1)) ||
        (character == '?') || // Start of query delimiter
        ((character == '@') && encodeAt) || // Username delimiter
        (character == '#') || // Start of reference delimiter
        ((character == 32) && (i+1 == old_length || segment[i+1] == (TQChar)' '))) // A trailing space
    {
      new_segment[ new_length++ ] = '%';

      unsigned int c = character / 16;
      c += (c > 9) ? ('A' - 10) : '0';
      new_segment[ new_length++ ] = c;

      c = character % 16;
      c += (c > 9) ? ('A' - 10) : '0';
      new_segment[ new_length++ ] = c;
    }
    else
    new_segment[ new_length++ ] = segment[i];
  }

  TQString result = TQString(new_segment, new_length);
  delete [] new_segment;
  return result;
}

static void decode( const TQString& segment, TQString &decoded, TQString &encoded, int encoding_hint=0, bool updateDecoded = true, bool isRawURI = false )
{
  decoded = TQString::null;
  encoded = segment;

  int old_length = segment.length();
  if ( !old_length )
    return;

  TQTextCodec *textCodec = 0;
  if (encoding_hint)
      textCodec = codecForHint( encoding_hint );

  if (!textCodec)
      textCodec = TQTextCodec::codecForLocale();

  TQCString csegment = textCodec->fromUnicode(segment);
  // Check if everything went ok
  if (textCodec->toUnicode(csegment) != segment)
  {
      // Uh oh
      textCodec = codecForHint( 106 ); // Fall back to utf-8
      csegment = textCodec->fromUnicode(segment);
  }
  old_length = csegment.length();

  int new_length = 0;
  int new_length2 = 0;

  // make a copy of the old one
  char *new_segment = new char[ old_length + 1 ];
  TQChar *new_usegment = new TQChar[ old_length * 3 + 1 ];

  int i = 0;
  while( i < old_length )
  {
    bool bReencode = false;
    unsigned char character = csegment[ i++ ];
    if ((character <= ' ') || (character > 127))
       bReencode = true;

    new_usegment [ new_length2++ ] = character;
    if (character == '%' )
    {
      int a = i+1 < old_length ? hex2int( csegment[i] ) : -1;
      int b = i+1 < old_length ? hex2int( csegment[i+1] ) : -1;
      if ((a == -1) || (b == -1)) // Only replace if sequence is valid
      {
         // Contains stray %, make sure to re-encode!
         bReencode = true;
      }
      else
      {
         // Valid %xx sequence
         character = a * 16 + b; // Replace with value of %dd
         if (!isRawURI && !character && updateDecoded)
            break; // Stop at %00

         new_usegment [ new_length2++ ] = (unsigned char) csegment[i++];
         new_usegment [ new_length2++ ] = (unsigned char) csegment[i++];
      }
    }
    if (bReencode)
    {
      new_length2--;
      new_usegment [ new_length2++ ] = '%';

      unsigned int c = character / 16;
      c += (c > 9) ? ('A' - 10) : '0';
      new_usegment[ new_length2++ ] = c;

      c = character % 16;
      c += (c > 9) ? ('A' - 10) : '0';
      new_usegment[ new_length2++ ] = c;
    }

    new_segment [ new_length++ ] = character;
  }
  new_segment [ new_length ] = 0;

  encoded = TQString( new_usegment, new_length2);

  // Encoding specified
  if (updateDecoded)
  {
     decoded = textCodec->toUnicode( new_segment );
     if ( isRawURI ) {
        int length = tqstrlen( new_segment );
        while ( length < new_length ) {
            decoded += TQChar::null;
            length += 1;
            decoded += textCodec->toUnicode( new_segment + length );
            length += tqstrlen( new_segment + length );
        }
     }

     TQCString validate = textCodec->fromUnicode(decoded);

     if (strcmp(validate.data(), new_segment) != 0)
     {
        decoded = TQString::fromLocal8Bit(new_segment, new_length);
     }
  }

  delete [] new_segment;
  delete [] new_usegment;
}

static TQString decode(const TQString &segment, int encoding_hint = 0, bool isRawURI = false)
{
  TQString result;
  TQString tmp;
  decode(segment, result, tmp, encoding_hint, true, isRawURI);
  return result;
}

static TQString cleanpath(const TQString &_path, bool cleanDirSeparator, bool decodeDots)
{
  if (_path.isEmpty()) return TQString::null;

  if (TQDir::isRelativePath(_path))
     return _path; // Don't mangle mailto-style URLs

  TQString path = _path;

  int len = path.length();

  if (decodeDots)
  {
#ifndef KDE_QT_ONLY
     static const TQString &encodedDot = KGlobal::staticQString("%2e");
#else
     TQString encodedDot("%2e");
#endif
     if (path.find(encodedDot, 0, false) != -1)
     {
#ifndef KDE_QT_ONLY
        static const TQString &encodedDOT = KGlobal::staticQString("%2E"); // Uppercase!
#else
        TQString encodedDOT("%2E");
#endif
        path.replace(encodedDot, ".");
        path.replace(encodedDOT, ".");
        len = path.length();
     }
  }

  bool slash = (len && path[len-1] == '/') ||
               (len > 1 && path[len-2] == '/' && path[len-1] == '.');

  // The following code cleans up directory path much like
  // TQDir::cleanDirPath() except it can be made to ignore multiple
  // directory separators by setting the flag to false.  That fixes
  // bug# 15044, mail.altavista.com and other similar brain-dead server
  // implementations that do not follow what has been specified in
  // RFC 2396!! (dA)
  TQString result;
  int cdUp, orig_pos, pos;

  cdUp = 0;
  pos = orig_pos = len;
  while ( pos && (pos = path.findRev('/',--pos)) != -1 )
  {
    len = orig_pos - pos - 1;
    if ( len == 2 && path[pos+1] == '.' && path[pos+2] == '.' )
      cdUp++;
    else
    {
      // Ignore any occurrences of '.'
      // This includes entries that simply do not make sense like /..../
      if ( (len || !cleanDirSeparator) &&
           (len != 1 || path[pos+1] != '.' ) )
      {
          if ( !cdUp )
              result.prepend(path.mid(pos, len+1));
          else
              cdUp--;
      }
    }
    orig_pos = pos;
  }

#ifdef Q_WS_WIN // prepend drive letter if exists (js)
  if (orig_pos >= 2 && isalpha(path[0].latin1()) && path[1]==':') {
    result.prepend(TQString(path[0])+":");
  }
#endif

  if ( result.isEmpty() )
    result = KURL_ROOTDIR_PATH;
  else if ( slash && result[result.length()-1] != '/' )
       result.append('/');

  return result;
}

bool KURL::isRelativeURL(const TQString &_url)
{
  int len = _url.length();
  if (!len) return true; // Very short relative URL.
  const TQChar *str = _url.unicode();

  // Absolute URL must start with alpha-character
  if (!isalpha(str[0].latin1()))
     return true; // Relative URL

  for(int i = 1; i < len; i++)
  {
     char c = str[i].latin1(); // Note: non-latin1 chars return 0!
     if (c == ':')
        return false; // Absolute URL

     // Protocol part may only contain alpha, digit, + or -
     if (!isalpha(c) && !isdigit(c) && (c != '+') && (c != '-'))
        return true; // Relative URL
  }
  // URL did not contain ':'
  return true; // Relative URL
}

KURL::List::List(const KURL &url)
{
    append( url );
}

KURL::List::List(const TQStringList &list)
{
  for (TQStringList::ConstIterator it = list.begin();
       it != list.end();
       it++)
    {
      append( KURL(*it) );
    }
}

TQStringList KURL::List::toStringList() const
{
  TQStringList lst;
   for( KURL::List::ConstIterator it = begin();
        it != end();
        it++)
   {
      lst.append( (*it).url() );
   }
   return lst;
}


KURL::KURL()
{
  reset();
}

KURL::~KURL()
{
}


KURL::KURL( const TQString &url, int encoding_hint )
{
  reset();
  parse( url, encoding_hint );
}

KURL::KURL( const char * url, int encoding_hint )
{
  reset();
  parse( TQString::fromLatin1(url), encoding_hint );
}

KURL::KURL( const TQCString& url, int encoding_hint )
{
  reset();
  parse( TQString::fromLatin1(url), encoding_hint );
}

KURL::KURL( const KURL& _u )
{
  *this = _u;
}

TQDataStream & operator<< (TQDataStream & s, const KURL & a)
{
  TQString QueryForWire=a.m_strQuery_encoded;
  if (!a.m_strQuery_encoded.isNull())
    QueryForWire.prepend("?");

    s << a.m_strProtocol << a.m_strUser << a.m_strPass << a.m_strHost
      << a.m_strPath << a.m_strPath_encoded << QueryForWire << a.m_strRef_encoded
      << TQ_INT8(a.m_bIsMalformed ? 1 : 0) << a.m_iPort;
    return s;
}

TQDataStream & operator>> (TQDataStream & s, KURL & a)
{
    TQ_INT8 malf;
    TQString QueryFromWire;
    s >> a.m_strProtocol >> a.m_strUser >> a.m_strPass >> a.m_strHost
      >> a.m_strPath >> a.m_strPath_encoded >> QueryFromWire >> a.m_strRef_encoded
      >> malf >> a.m_iPort;
    a.m_bIsMalformed = (malf != 0);

    if ( QueryFromWire.isNull() )
      a.m_strQuery_encoded = TQString::null;
    else if ( QueryFromWire.length() == 1 ) // empty query
      a.m_strQuery_encoded = "";
    else
      a.m_strQuery_encoded = QueryFromWire.mid(1);

    a.m_iUriMode = KURL::uriModeForProtocol( a.m_strProtocol );

    return s;
}

#ifndef QT_NO_NETWORKPROTOCOL
KURL::KURL( const TQUrl &u )
{
  *this = u;
}
#endif

KURL::KURL( const KURL& _u, const TQString& _rel_url, int encoding_hint )
{
  if (_u.hasSubURL()) // Operate on the last suburl, not the first
  {
    KURL::List lst = split( _u );
    KURL u(lst.last(), _rel_url, encoding_hint);
    lst.remove( lst.last() );
    lst.append( u );
    *this = join( lst );
    return;
  }
  // WORKAROUND THE RFC 1606 LOOPHOLE THAT ALLOWS
  // http:/index.html AS A VALID SYNTAX FOR RELATIVE
  // URLS. ( RFC 2396 section 5.2 item # 3 )
  TQString rUrl = _rel_url;
  int len = _u.m_strProtocol.length();
  if ( !_u.m_strHost.isEmpty() && !rUrl.isEmpty() &&
       rUrl.find( _u.m_strProtocol, 0, false ) == 0 &&
       rUrl[len] == ':' && (rUrl[len+1] != '/' ||
       (rUrl[len+1] == '/' && rUrl[len+2] != '/')) )
  {
    rUrl.remove( 0, rUrl.find( ':' ) + 1 );
  }

  if ( rUrl.isEmpty() )
  {
    *this = _u;
  }
  else if ( rUrl[0] == '#' )
  {
    *this = _u;
    m_strRef_encoded = rUrl.mid(1);
    if ( m_strRef_encoded.isNull() )
        m_strRef_encoded = ""; // we know there was an (empty) html ref, we saw the '#'
  }
  else if ( isRelativeURL( rUrl) )
  {
    *this = _u;
    m_strQuery_encoded = TQString::null;
    m_strRef_encoded = TQString::null;
    if ( rUrl[0] == '/')
    {
        if ((rUrl.length() > 1) && (rUrl[1] == '/'))
        {
           m_strHost = TQString::null;
           // File protocol returns file:/// without host, strip // from rUrl
           if (_u.m_strProtocol == fileProt)
              rUrl.remove(0, 2);
        }
        m_strPath = TQString::null;
        m_strPath_encoded = TQString::null;
    }
    else if ( rUrl[0] != '?' )
    {
       int pos = m_strPath.findRev( '/' );
       if (pos >= 0)
          m_strPath.truncate(pos);
       m_strPath += '/';
       if (!m_strPath_encoded.isEmpty())
       {
          pos = m_strPath_encoded.findRev( '/' );
          if (pos >= 0)
             m_strPath_encoded.truncate(pos);
          m_strPath_encoded += '/';
       }
    }
    else
    {
       if ( m_strPath.isEmpty() )
          m_strPath = '/';
    }
    KURL tmp( url() + rUrl, encoding_hint);
    *this = tmp;
    cleanPath(false);
  }
  else
  {
    KURL tmp( rUrl, encoding_hint);
    *this = tmp;
    // Preserve userinfo if applicable.
    if (!_u.m_strUser.isEmpty() && m_strUser.isEmpty() && (_u.m_strHost == m_strHost) && (_u.m_strProtocol == m_strProtocol))
    {
       m_strUser = _u.m_strUser;
       m_strPass = _u.m_strPass;
    }
    cleanPath(false);
  }
}

void KURL::reset()
{
  m_strProtocol = TQString::null;
  m_strUser = TQString::null;
  m_strPass = TQString::null;
  m_strHost = TQString::null;
  m_strPath = TQString::null;
  m_strPath_encoded = TQString::null;
  m_strQuery_encoded = TQString::null;
  m_strRef_encoded = TQString::null;
  m_bIsMalformed = true;
  m_iPort = 0;
  m_iUriMode = Auto;
}

bool KURL::isEmpty() const
{
  return (m_strPath.isEmpty() && m_strProtocol.isEmpty());
}

void KURL::parse( const TQString& _url, int encoding_hint )
{
    if ( _url.isEmpty() || m_iUriMode == Invalid )
    {
	m_strProtocol = _url;
	m_iUriMode = Invalid;
	return;
    }

    const TQChar* buf = _url.unicode();
    const TQChar* orig = buf;
    uint len = _url.length();
    uint pos = 0;

    // Node 1: Accept alpha or slash
    TQChar x = buf[pos++];
#ifdef Q_WS_WIN
    /* win32: accept <letter>: or <letter>:/ or <letter>:\ */
    const bool alpha = isalpha((int)x);
    if (alpha && len<2)
        goto NodeErr;
    if (alpha && buf[pos]==':' && (len==2 || (len>2 && (buf[pos+1]=='/' || buf[pos+1]=='\\'))))
#else
    if ( x == (TQChar)'/' )
#endif
    {
	// A slash means we immediately proceed to parse it as a file URL.
	m_iUriMode = URL;
	m_strProtocol = fileProt;
	parseURL( _url, encoding_hint );
	return;
    }
    if ( !isalpha( (int)x ) )
	goto NodeErr;

    // Node 2: Accept any amount of (alpha|digit|'+'|'-')
    // '.' is not currently accepted, because current KURL may be confused.
    // Proceed with :// :/ or :
    while( pos < len && (isalpha((int)buf[pos]) || isdigit((int)buf[pos]) ||
			 buf[pos] == (TQChar)'+' || buf[pos] == (TQChar)'-')) pos++;

    if (pos < len && buf[pos] == (TQChar)':' )
    {
	m_strProtocol = TQString( orig, pos ).lower();
	if ( m_iUriMode == Auto )
	    m_iUriMode = uriModeForProtocol( m_strProtocol );
	// Proceed to correct parse function.
	switch ( m_iUriMode )
	{
	case RawURI:
	    parseRawURI( _url );
	    return;
	case Mailto:
	    parseMailto( _url );
	    return;
	case URL:
	    parseURL( _url, encoding_hint );
	    return;
	default:
	    // Unknown URI mode results in an invalid URI.
	    break;
	}
    }

NodeErr:
    reset();
    m_strProtocol = _url;
    m_iUriMode = Invalid;
}

void KURL::parseRawURI( const TQString& _url, int encoding_hint )
{
    uint len = _url.length();
    const TQChar* buf = _url.unicode();

    uint pos = 0;

    // Accept any amount of (alpha|digit|'+'|'-')
    // '.' is not currently accepted, because current KURL may be confused.
    // Proceed with :
    while( pos < len && (isalpha((int)buf[pos]) || isdigit((int)buf[pos]) ||
			 buf[pos] == (TQChar)'+' || buf[pos] == (TQChar)'-')) pos++;

    // Note that m_strProtocol is already set here, so we just skip over the protocol.
    if (pos < len && buf[pos] == (TQChar)':' )
	pos++;
    else { // can't happen, the caller checked all this already
	reset();
	m_strProtocol = _url;
	m_iUriMode = Invalid;
	return;
    }

    if ( pos == len ) // can't happen, the caller checked this already
	m_strPath = TQString::null;
    else
	m_strPath = decode( TQString( buf + pos, len - pos ), encoding_hint, true );

    m_bIsMalformed = false;

    return;
}

void KURL::parseMailto( const TQString& _url, int encoding_hint )
{
    parseURL( _url, encoding_hint);
    if ( m_bIsMalformed )
        return;
    TQRegExp mailre("(.+@)(.+)");
    if ( mailre.exactMatch( m_strPath ) )
    {
#ifndef KDE_QT_ONLY
	TQString host = KIDNA::toUnicode( mailre.cap( 2 ) );
	if (host.isEmpty())
	    host = TQString(mailre.cap( 2 )).lower();
#else
	TQString host = TQString(mailre.cap( 2 )).lower();
#endif
	m_strPath = mailre.cap( 1 ) + host;
  }
}

void KURL::parseURL( const TQString& _url, int encoding_hint )
{
  TQString port;
  bool badHostName = false;
  int start = 0;
  uint len = _url.length();
  const TQChar* buf = _url.unicode();

  TQChar delim;
  TQString tmp;

  uint pos = 0;

  // Node 1: Accept alpha or slash
  TQChar x = buf[pos++];
#ifdef Q_WS_WIN
  /* win32: accept <letter>: or <letter>:/ or <letter>:\ */
  const bool alpha = isalpha((int)x);
  if (alpha && len<2)
    goto NodeErr;
  if (alpha && buf[pos]==(TQChar)':' && (len==2 || (len>2 && (buf[pos+1]==(TQChar)'/' || buf[pos+1]==(TQChar)'\\'))))
#else
  if ( x == (TQChar)'/' )
#endif
    goto Node9;
  if ( !isalpha( (int)x ) )
    goto NodeErr;

  // Node 2: Accept any amount of (alpha|digit|'+'|'-')
  // '.' is not currently accepted, because current KURL may be confused.
  // Proceed with :// :/ or :
  while( pos < len && (isalpha((int)buf[pos]) || isdigit((int)buf[pos]) ||
          buf[pos] == (TQChar)'+' || buf[pos] == (TQChar)'-')) pos++;

  // Note that m_strProtocol is already set here, so we just skip over the protocol.
  if ( pos+2 < len && buf[pos] == (TQChar)':' && buf[pos+1] == (TQChar)'/' && buf[pos+2] == (TQChar)'/' )
    {
      pos += 3;
    }
  else if (pos+1 < len && buf[pos] == (TQChar)':' ) // Need to always compare length()-1 otherwise KURL passes "http:" as legal!!
    {
      pos++;
      start = pos;
      goto Node9;
    }
  else
    goto NodeErr;

  //Node 3: We need at least one character here
  if ( pos == len )
      goto NodeErr;
  start = pos;

  // Node 4: Accept any amount of characters.
  if (buf[pos] == (TQChar)'[')     // An IPv6 host follows.
      goto Node8;
  // Terminate on / or @ or ? or # or " or ; or <
  x = buf[pos];
  while( (x != (TQChar)':') && (x != (TQChar)'@') && (x != (TQChar)'/') && (x != (TQChar)'?') && (x != (TQChar)'#') )
  {
     if ((x == (TQChar)'\"') || (x == (TQChar)';') || (x == (TQChar)'<'))
        badHostName = true;
     if (++pos == len)
        break;
     x = buf[pos];
  }
  if ( pos == len )
    {
      if (badHostName)
         goto NodeErr;

      setHost(decode(TQString( buf + start, pos - start ), encoding_hint));
      goto NodeOk;
    }
  if ( x == (TQChar)'@' )
    {
      m_strUser = decode(TQString( buf + start, pos - start ), encoding_hint);
      pos++;
      goto Node7;
    }
  else if ( (x == (TQChar)'/') || (x == (TQChar)'?') || (x == (TQChar)'#'))
    {
      if (badHostName)
         goto NodeErr;

      setHost(decode(TQString( buf + start, pos - start ), encoding_hint));
      start = pos;
      goto Node9;
    }
  else if ( x != (TQChar)':' )
    goto NodeErr;
  m_strUser = decode(TQString( buf + start, pos - start ), encoding_hint);
  pos++;

  // Node 5: We need at least one character
  if ( pos == len )
    goto NodeErr;
  start = pos++;

  // Node 6: Read everything until @, /, ? or #
  while( (pos < len) &&
		(buf[pos] != (TQChar)'@') &&
		(buf[pos] != (TQChar)'/') &&
		(buf[pos] != (TQChar)'?') &&
		(buf[pos] != (TQChar)'#')) pos++;
  // If we now have a '@' the ':' seperates user and password.
  // Otherwise it seperates host and port.
  if ( (pos == len) || (buf[pos] != (TQChar)'@') )
    {
      // Ok the : was used to separate host and port
      if (badHostName)
         goto NodeErr;
      setHost(m_strUser);
      m_strUser = TQString::null;
      TQString tmp( buf + start, pos - start );
      char *endptr;
      m_iPort = (unsigned short int)strtol(tmp.ascii(), &endptr, 10);
      if ((pos == len) && (strlen(endptr) == 0))
        goto NodeOk;
      // there is more after the digits
      pos -= strlen(endptr);
      if ((buf[pos] != (TQChar)'@') &&
          (buf[pos] != (TQChar)'/') &&
          (buf[pos] != (TQChar)'?') &&
          (buf[pos] != (TQChar)'#'))
        goto NodeErr;

      start = pos;
      goto Node9;
    }
  m_strPass = decode(TQString( buf + start, pos - start), encoding_hint);
  pos++;

  // Node 7: We need at least one character
 Node7:
  if ( pos == len )
    goto NodeErr;

 Node8:
  if (buf[pos] == (TQChar)'[')
  {
    // IPv6 address
    start = ++pos; // Skip '['

    if (pos == len)
    {
       badHostName = true;
       goto NodeErr;
    }
    // Node 8a: Read everything until ] or terminate
    badHostName = false;
    x = buf[pos];
    while( (x != (TQChar)']') )
    {
       if ((x == (TQChar)'\"') || (x == (TQChar)';') || (x == (TQChar)'<'))
          badHostName = true;
       if (++pos == len)
       {
          badHostName = true;
          break;
       }
       x = buf[pos];
    }
    if (badHostName)
       goto NodeErr;
    setHost(decode(TQString( buf + start, pos - start ), encoding_hint));
    if (pos < len) pos++; // Skip ']'
    if (pos == len)
       goto NodeOk;
  }
  else
  {
    // Non IPv6 address, with a user
    start = pos;

    // Node 8b: Read everything until / : or terminate
    badHostName = false;
    x = buf[pos];
    while( (x != (TQChar)':') && (x != (TQChar)'@') && (x != (TQChar)'/') && (x != (TQChar)'?') && (x != (TQChar)'#') )
    {
       if ((x == (TQChar)'\"') || (x == (TQChar)';') || (x == (TQChar)'<'))
          badHostName = true;
       if (++pos == len)
          break;
       x = buf[pos];
    }
    if (badHostName)
       goto NodeErr;
    if ( pos == len )
    {
       setHost(decode(TQString( buf + start, pos - start ), encoding_hint));
       goto NodeOk;
    }
    setHost(decode(TQString( buf + start, pos - start ), encoding_hint));
  }
  x = buf[pos];
  if ( x == (TQChar)'/' || x == (TQChar)'#' || x == (TQChar)'?' )
    {
      start = pos;
      goto Node9;
    }
  else if ( x != (TQChar)':' )
    goto NodeErr;
  pos++;

  // Node 8c: Accept at least one digit
  if ( pos == len )
    goto NodeErr;
  start = pos;
  if ( !isdigit( buf[pos++] ) )
    goto NodeErr;

  // Node 8d: Accept any amount of digits
  while( pos < len && isdigit( buf[pos] ) ) pos++;
  port = TQString( buf + start, pos - start );
  m_iPort = port.toUShort();
  if ( pos == len )
    goto NodeOk;
  start = pos;

 Node9: // parse path until query or reference reached

  while( pos < len && buf[pos] != (TQChar)'#' && buf[pos]!=(TQChar)'?' ) pos++;

  tmp = TQString( buf + start, pos - start );
  //kdDebug(126)<<" setting encoded path to:"<<tmp<<endl;
  setEncodedPath( tmp, encoding_hint );

  if ( pos == len )
      goto NodeOk;

 //Node10: // parse query or reference depending on what comes first
  delim = (buf[pos++]==(TQChar)'#'?(TQChar)'?':(TQChar)'#');

  start = pos;

  while(pos < len && buf[pos]!=delim ) pos++;

  tmp = TQString(buf + start, pos - start);
  if (delim==(TQChar)'#')
      _setQuery(tmp, encoding_hint);
  else
      m_strRef_encoded = tmp;

  if (pos == len)
      goto NodeOk;

 //Node11: // feed the rest into the remaining variable
  tmp = TQString( buf + pos + 1, len - pos - 1);
  if (delim == (TQChar)'#')
      m_strRef_encoded = tmp;
  else
      _setQuery(tmp, encoding_hint);

 NodeOk:
  //kdDebug(126)<<"parsing finished. m_strProtocol="<<m_strProtocol<<" m_strHost="<<m_strHost<<" m_strPath="<<m_strPath<<endl;
  m_bIsMalformed = false; // Valid URL

  //kdDebug()<<"Prot="<<m_strProtocol<<"\nUser="<<m_strUser<<"\nPass="<<m_strPass<<"\nHost="<<m_strHost<<"\nPath="<<m_strPath<<"\nQuery="<<m_strQuery_encoded<<"\nRef="<<m_strRef_encoded<<"\nPort="<<m_iPort<<endl;
  if (m_strProtocol.isEmpty())
  {
    m_iUriMode = URL;
    m_strProtocol = fileProt;
  }
  return;

 NodeErr:
//  kdDebug(126) << "KURL couldn't parse URL \"" << _url << "\"" << endl;
  reset();
  m_strProtocol = _url;
  m_iUriMode = Invalid;
}

KURL& KURL::operator=( const TQString& _url )
{
  reset();
  parse( _url );

  return *this;
}

KURL& KURL::operator=( const char * _url )
{
  reset();
  parse( TQString::fromLatin1(_url) );

  return *this;
}

#ifndef QT_NO_NETWORKPROTOCOL
KURL& KURL::operator=( const TQUrl & u )
{
  m_strProtocol = u.protocol();
  m_iUriMode = Auto;
  m_strUser = u.user();
  m_strPass = u.password();
  m_strHost = u.host();
  m_strPath = u.path( false );
  m_strPath_encoded = TQString::null;
  m_strQuery_encoded = u.query();
  m_strRef_encoded = u.ref();
  m_bIsMalformed = !u.isValid();
  m_iPort = u.port();

  return *this;
}
#endif

KURL& KURL::operator=( const KURL& _u )
{
  m_strProtocol = _u.m_strProtocol;
  m_strUser = _u.m_strUser;
  m_strPass = _u.m_strPass;
  m_strHost = _u.m_strHost;
  m_strPath = _u.m_strPath;
  m_strPath_encoded = _u.m_strPath_encoded;
  m_strQuery_encoded = _u.m_strQuery_encoded;
  m_strRef_encoded = _u.m_strRef_encoded;
  m_bIsMalformed = _u.m_bIsMalformed;
  m_iPort = _u.m_iPort;
  m_iUriMode = _u.m_iUriMode;

  return *this;
}

bool KURL::operator<( const KURL& _u) const
{
  int i;
  if (!_u.isValid())
  {
     if (!isValid())
     {
        i = m_strProtocol.compare(_u.m_strProtocol);
        return (i < 0);
     }
     return false;
  }
  if (!isValid())
     return true;

  i = m_strProtocol.compare(_u.m_strProtocol);
  if (i) return (i < 0);

  i = m_strHost.compare(_u.m_strHost);
  if (i) return (i < 0);

  if (m_iPort != _u.m_iPort) return (m_iPort < _u.m_iPort);

  i = m_strPath.compare(_u.m_strPath);
  if (i) return (i < 0);

  i = m_strQuery_encoded.compare(_u.m_strQuery_encoded);
  if (i) return (i < 0);

  i = m_strRef_encoded.compare(_u.m_strRef_encoded);
  if (i) return (i < 0);

  i = m_strUser.compare(_u.m_strUser);
  if (i) return (i < 0);

  i = m_strPass.compare(_u.m_strPass);
  if (i) return (i < 0);

  return false;
}

bool KURL::operator==( const KURL& _u ) const
{
  if ( !isValid() || !_u.isValid() )
    return false;

  if ( m_strProtocol == _u.m_strProtocol &&
       m_strUser == _u.m_strUser &&
       m_strPass == _u.m_strPass &&
       m_strHost == _u.m_strHost &&
       m_strPath == _u.m_strPath &&
       // The encoded path may be null, but the URLs are still equal (David)
       ( m_strPath_encoded.isNull() || _u.m_strPath_encoded.isNull() ||
         m_strPath_encoded == _u.m_strPath_encoded ) &&
       m_strQuery_encoded == _u.m_strQuery_encoded &&
       m_strRef_encoded == _u.m_strRef_encoded &&
       m_iPort == _u.m_iPort )
  {
    return true;
  }

  return false;
}

bool KURL::operator==( const TQString& _u ) const
{
  KURL u( _u );
  return ( *this == u );
}

bool KURL::cmp( const KURL &u, bool ignore_trailing ) const
{
  return equals( u, ignore_trailing );
}

bool KURL::equals( const KURL &_u, bool ignore_trailing ) const
{
  if ( !isValid() || !_u.isValid() )
    return false;

  if ( ignore_trailing )
  {
    TQString path1 = path(1);
    TQString path2 = _u.path(1);
    if ( path1 != path2 )
      return false;

    if ( m_strProtocol == _u.m_strProtocol &&
         m_strUser == _u.m_strUser &&
         m_strPass == _u.m_strPass &&
         m_strHost == _u.m_strHost &&
         m_strQuery_encoded == _u.m_strQuery_encoded &&
         m_strRef_encoded == _u.m_strRef_encoded &&
         m_iPort == _u.m_iPort )
      return true;

    return false;
  }

  return ( *this == _u );
}

bool KURL::isParentOf( const KURL& _u ) const
{
  if ( !isValid() || !_u.isValid() )
    return false;

  if ( m_strProtocol == _u.m_strProtocol &&
       m_strUser == _u.m_strUser &&
       m_strPass == _u.m_strPass &&
       m_strHost == _u.m_strHost &&
       m_strQuery_encoded == _u.m_strQuery_encoded &&
       m_strRef_encoded == _u.m_strRef_encoded &&
       m_iPort == _u.m_iPort )
  {
    if ( path().isEmpty() || _u.path().isEmpty() )
        return false; // can't work with implicit paths

    TQString p1( cleanpath( path(), true, false ) );
    if ( p1[p1.length()-1] != '/' )
        p1 += '/';
    TQString p2( cleanpath( _u.path(), true, false ) );
    if ( p2[p2.length()-1] != '/' )
        p2 += '/';

    //kdDebug(126) << "p1=" << p1 << endl;
    //kdDebug(126) << "p2=" << p2 << endl;
    //kdDebug(126) << "p1.length()=" << p1.length() << endl;
    //kdDebug(126) << "p2.left(!$)=" << p2.left( p1.length() ) << endl;
    return p2.startsWith( p1 );
  }
  return false;
}

void KURL::setFileName( const TQString& _txt )
{
  m_strRef_encoded = TQString::null;
  int i = 0;
  while( _txt[i] == (TQChar)'/' ) ++i;
  TQString tmp;
  if ( i )
    tmp = _txt.mid( i );
  else
    tmp = _txt;

  TQString path = m_strPath_encoded.isEmpty() ? m_strPath : m_strPath_encoded;
  if ( path.isEmpty() )
    path = "/";
  else
  {
    int lastSlash = path.findRev( '/' );
    if ( lastSlash == -1)
    {
      // The first character is not a '/' ???
      // This looks strange ...
      path = "/";
    }
    else if ( path.right(1) != "/" )
      path.truncate( lastSlash+1 ); // keep the "/"
  }
  if (m_strPath_encoded.isEmpty())
  {
     path += tmp;
     setPath( path );
  }
  else
  {
     path += encode_string(tmp);
     setEncodedPath( path );
  }
  cleanPath();
}

void KURL::cleanPath( bool cleanDirSeparator ) // taken from the old KURL
{
  if (m_iUriMode != URL) return;
  m_strPath = cleanpath(m_strPath, cleanDirSeparator, false);
  // WABA: Is this safe when "/../" is encoded with %?
  m_strPath_encoded = cleanpath(m_strPath_encoded, cleanDirSeparator, true);
}

static TQString trailingSlash( int _trailing, const TQString &path )
{
  TQString result = path;

  if ( _trailing == 0 )
    return result;
  else if ( _trailing == 1 )
  {
    int len = result.length();
    if ( (len == 0) || (result[ len - 1 ] != (TQChar)'/') )
      result += "/";
    return result;
  }
  else if ( _trailing == -1 )
  {
    if ( result == "/" )
      return result;
    int len = result.length();
    while (len > 1 && result[ len - 1 ] == (TQChar)'/')
    {
      len--;
    }
    result.truncate( len );
    return result;
  }
  else {
    assert( 0 );
    return TQString::null;
  }
}

void KURL::adjustPath( int _trailing )
{
  if (!m_strPath_encoded.isEmpty())
  {
     m_strPath_encoded = trailingSlash( _trailing, m_strPath_encoded );
  }
  m_strPath = trailingSlash( _trailing, m_strPath );
}


TQString KURL::encodedPathAndQuery( int _trailing, bool _no_empty_path, int encoding_hint ) const
{
  TQString tmp;
  if (!m_strPath_encoded.isEmpty() && encoding_hint == 0)
  {
     tmp = trailingSlash( _trailing, m_strPath_encoded );
  }
  else
  {
     tmp = path( _trailing );
     if ( _no_empty_path && tmp.isEmpty() )
        tmp = "/";
     if (m_iUriMode == Mailto)
     {
        tmp = encode( tmp, 2, encoding_hint );
     }
     else
     {
        tmp = encode( tmp, 1, encoding_hint );
     }
  }

  // TODO apply encoding_hint to the query
  if (!m_strQuery_encoded.isNull())
      tmp += '?' + m_strQuery_encoded;
  return tmp;
}

void KURL::setEncodedPath( const TQString& _txt, int encoding_hint )
{
  m_strPath_encoded = _txt;

  decode( m_strPath_encoded, m_strPath, m_strPath_encoded, encoding_hint );
  // Throw away encoding for local files, makes file-operations faster.
  if (m_strProtocol == fileProt)
     m_strPath_encoded = TQString::null;

  if ( m_iUriMode == Auto )
    m_iUriMode = URL;
}


void KURL::setEncodedPathAndQuery( const TQString& _txt, int encoding_hint )
{
  int pos = _txt.find( '?' );
  if ( pos == -1 )
  {
    setEncodedPath(_txt, encoding_hint);
    m_strQuery_encoded = TQString::null;
  }
  else
  {
    setEncodedPath(_txt.left( pos ), encoding_hint);
    _setQuery(_txt.right(_txt.length() - pos - 1), encoding_hint);
  }
}

TQString KURL::path( int _trailing ) const
{
  return trailingSlash( _trailing, path() );
}

bool KURL::isLocalFile() const
{
  if ( (m_strProtocol != fileProt ) || hasSubURL() )
     return false;

  if (m_strHost.isEmpty() || (m_strHost == "localhost"))
     return true;

  char hostname[ 256 ];
  hostname[ 0 ] = '\0';
  if (!gethostname( hostname, 255 ))
     hostname[sizeof(hostname)-1] = '\0';

  for(char *p = hostname; *p; p++)
     *p = tolower(*p);

  return (m_strHost == hostname);
}

void KURL::setFileEncoding(const TQString &encoding)
{
  if (!isLocalFile())
     return;

  TQString q = query();

  if (!q.isEmpty() && (q[0] == '?'))
     q = q.mid(1);

  TQStringList args = TQStringList::split('&', q);
  for(TQStringList::Iterator it = args.begin();
      it != args.end();)
  {
      TQString s = decode_string(*it);
      if (s.startsWith("charset="))
         it = args.erase(it);
      else
         ++it;
  }
  if (!encoding.isEmpty())
     args.append("charset="+encode_string(encoding));

  if (args.isEmpty())
     _setQuery(TQString::null);
  else
     _setQuery(args.join("&"));
}

TQString KURL::fileEncoding() const
{
  if (!isLocalFile())
     return TQString::null;

  TQString q = query();

  if (q.isEmpty())
     return TQString::null;

  if (q[0] == '?')
     q = q.mid(1);

  TQStringList args = TQStringList::split('&', q);
  for(TQStringList::ConstIterator it = args.begin();
      it != args.end();
      ++it)
  {
      TQString s = decode_string(*it);
      if (s.startsWith("charset="))
         return s.mid(8);
  }
  return TQString::null;
}

bool KURL::hasSubURL() const
{
  if ( m_strProtocol.isEmpty() || m_bIsMalformed )
    return false;
  if (m_strRef_encoded.isEmpty())
     return false;
  if (m_strRef_encoded.startsWith("gzip:"))
     return true;
  if (m_strRef_encoded.startsWith("bzip:"))
     return true;
  if (m_strRef_encoded.startsWith("bzip2:"))
     return true;
  if (m_strRef_encoded.startsWith("tar:"))
     return true;
  if (m_strRef_encoded.startsWith("ar:"))
     return true;
  if (m_strRef_encoded.startsWith("zip:"))
     return true;
  if ( m_strProtocol == "error" ) // anything that starts with error: has suburls
     return true;
  return false;
}

TQString KURL::url( int _trailing, int encoding_hint ) const
{
  if( m_bIsMalformed )
  {
    // Return the whole url even when the url is
    // malformed.  Under such conditions the url
    // is stored in m_strProtocol.
    return m_strProtocol;
  }

  TQString u = m_strProtocol;
  if (!u.isEmpty())
    u += ":";

  if ( hasHost() || (m_strProtocol == fileProt) )
  {
    u += "//";
    if ( hasUser() )
    {
      u += encode(m_strUser, 0, encoding_hint);
      if ( hasPass() )
      {
        u += ":";
        u += encode(m_strPass, 0, encoding_hint);
      }
      u += "@";
    }
    if ( m_iUriMode == URL )
    {
      bool IPv6 = (m_strHost.find(':') != -1);
      if (IPv6)
        u += '[' + m_strHost + ']';
      else
        u += encodeHost(m_strHost, true, encoding_hint);
      if ( m_iPort != 0 ) {
        TQString buffer;
        buffer.sprintf( ":%u", m_iPort );
        u += buffer;
      }
    }
    else
    {
      u += m_strHost;
    }
  }

  if ( m_iUriMode == URL || m_iUriMode == Mailto )
    u += encodedPathAndQuery( _trailing, false, encoding_hint );
  else
    u += encode( m_strPath, 21, encoding_hint, true );

  if ( hasRef() )
  {
    u += "#";
    u += m_strRef_encoded;
  }

  return u;
}

TQString KURL::prettyURL( int _trailing ) const
{
  if( m_bIsMalformed )
  {
    // Return the whole url even when the url is
    // malformed.  Under such conditions the url
    // is stored in m_strProtocol.
    return m_strProtocol;
  }

  TQString u = m_strProtocol;
  if (!u.isEmpty())
     u += ":";

  if ( hasHost() || (m_strProtocol == fileProt) )
  {
    u += "//";
    if ( hasUser() )
    {
      u += encode(m_strUser, 0, 0);
      // Don't show password!
      u += "@";
    }
    if ( m_iUriMode == URL )
    {
    bool IPv6 = (m_strHost.find(':') != -1);
    if (IPv6)
    {
       u += '[' + m_strHost + ']';
    }
    else
    {
       u += lazy_encode(m_strHost);
    }
    }
    else
    {
      u += lazy_encode(m_strHost);
    }
    if ( m_iPort != 0 ) {
      TQString buffer;
      buffer.sprintf( ":%u", m_iPort );
      u += buffer;
    }
  }

  if (m_iUriMode == Mailto)
  {
     u += lazy_encode( m_strPath, false );
  }
  else
  {
     u += trailingSlash( _trailing, lazy_encode( m_strPath ) );
  }

  if (!m_strQuery_encoded.isNull())
      u += '?' + m_strQuery_encoded;

  if ( hasRef() )
  {
    u += "#";
    u += m_strRef_encoded;
  }

  return u;
}

TQString KURL::prettyURL( int _trailing, AdjustementFlags _flags) const
{
  TQString u = prettyURL(_trailing);
  if (_flags & StripFileProtocol && u.startsWith("file://")) {
    u.remove(0, 7);
#ifdef Q_WS_WIN
    return TQDir::convertSeparators(u);
#endif
  }
  return u;
}

TQString KURL::pathOrURL() const
{
  if ( isLocalFile() && m_strRef_encoded.isNull() && m_strQuery_encoded.isNull() ) {
    return path();
  } else {
    return prettyURL();
  }
}

TQString KURL::htmlURL() const
{
  return TQStyleSheet::escape(prettyURL());
}

KURL::List KURL::split( const KURL& _url )
{
  TQString ref;
  KURL::List lst;
  KURL url = _url;

  while(true)
  {
     KURL u = url;
     u.m_strRef_encoded = TQString::null;
     lst.append(u);
     if (url.hasSubURL())
     {
        url = KURL(url.m_strRef_encoded);
     }
     else
     {
        ref = url.m_strRef_encoded;
        break;
     }
  }

  // Set HTML ref in all URLs.
  KURL::List::Iterator it;
  for( it = lst.begin() ; it != lst.end(); ++it )
  {
     (*it).m_strRef_encoded = ref;
  }

  return lst;
}

KURL::List KURL::split( const TQString& _url )
{
  return split(KURL(_url));
}

KURL KURL::join( const KURL::List & lst )
{
  if (lst.isEmpty()) return KURL();
  KURL tmp;

  KURL::List::ConstIterator first = lst.fromLast();
  for( KURL::List::ConstIterator it = first; it != lst.end(); --it )
  {
     KURL u(*it);
     if (it != first)
     {
        if (!u.m_strRef_encoded) u.m_strRef_encoded = tmp.url();
        else u.m_strRef_encoded += "#" + tmp.url(); // Support more than one suburl thingy
     }
     tmp = u;
  }

  return tmp;
}

TQString KURL::fileName( bool _strip_trailing_slash ) const
{
  TQString fname;
  if (hasSubURL()) { // If we have a suburl, then return the filename from there
    KURL::List list = KURL::split(*this);
    KURL::List::Iterator it = list.fromLast();
    return (*it).fileName(_strip_trailing_slash);
  }
  const TQString &path = m_strPath;

  int len = path.length();
  if ( len == 0 )
    return fname;

  if ( _strip_trailing_slash )
  {
    while ( len >= 1 && path[ len - 1 ] == TQChar('/') )
      len--;
  }
  else if ( path[ len - 1 ] == TQChar('/') )
    return fname;

  // Does the path only consist of '/' characters ?
  if ( len == 1 && path[ 0 ] == TQChar('/') )
    return fname;

  // Skip last n slashes
  int n = 1;
  if (!m_strPath_encoded.isEmpty())
  {
     // This is hairy, we need the last unencoded slash.
     // Count in the encoded string how many encoded slashes follow the last
     // unencoded one.
     int i = m_strPath_encoded.findRev( TQChar('/'), len - 1 );
     TQString fileName_encoded = m_strPath_encoded.mid(i+1);
     n += fileName_encoded.contains("%2f", false);
  }
  int i = len;
  do {
    i = path.findRev( TQChar('/'), i - 1 );
  }
  while (--n && (i > 0));

  // If ( i == -1 ) => the first character is not a '/'
  // So it's some URL like file:blah.tgz, return the whole path
  if ( i == -1 ) {
    if ( len == (int)path.length() )
      fname = path;
    else
      // Might get here if _strip_trailing_slash is true
      fname = path.left( len );
  }
  else
  {
     fname = path.mid( i + 1, len - i - 1 ); // TO CHECK
  }
  return fname;
}

void KURL::addPath( const TQString& _txt )
{
  if (hasSubURL())
  {
     KURL::List lst = split( *this );
     KURL &u = lst.last();
     u.addPath(_txt);
     *this = join( lst );
     return;
  }

  m_strPath_encoded = TQString::null;

  if ( _txt.isEmpty() )
    return;

  int i = 0;
  int len = m_strPath.length();
  // Add the trailing '/' if it is missing
  if ( _txt[0] != (TQChar)'/' && ( len == 0 || m_strPath[ len - 1 ] != (TQChar)'/' ) )
    m_strPath += "/";

  // No double '/' characters
  i = 0;
  if ( len != 0 && m_strPath[ len - 1 ] == (TQChar)'/' )
  {
    while( _txt[i] == (TQChar)'/' )
      ++i;
  }

  m_strPath += _txt.mid( i );
}

TQString KURL::directory( bool _strip_trailing_slash_from_result,
                         bool _ignore_trailing_slash_in_path ) const
{
  TQString result = m_strPath_encoded.isEmpty() ? m_strPath : m_strPath_encoded;
  if ( _ignore_trailing_slash_in_path )
    result = trailingSlash( -1, result );

  if ( result.isEmpty() || result == "/" )
    return result;

  int i = result.findRev( "/" );
  // If ( i == -1 ) => the first character is not a '/'
  // So it's some URL like file:blah.tgz, with no path
  if ( i == -1 )
    return TQString::null;

  if ( i == 0 )
  {
    result = "/";
    return result;
  }

  if ( _strip_trailing_slash_from_result )
    result = result.left( i );
  else
    result = result.left( i + 1 );

  if (!m_strPath_encoded.isEmpty())
    result = decode(result);

  return result;
}


bool KURL::cd( const TQString& _dir )
{
  if ( _dir.isEmpty() || m_bIsMalformed )
    return false;

  if (hasSubURL())
  {
     KURL::List lst = split( *this );
     KURL &u = lst.last();
     u.cd(_dir);
     *this = join( lst );
     return true;
  }

  // absolute path ?
  if ( _dir[0] == (TQChar)'/' )
  {
    m_strPath_encoded = TQString::null;
    m_strPath = _dir;
    setHTMLRef( TQString::null );
    m_strQuery_encoded = TQString::null;
    return true;
  }

  // Users home directory on the local disk ?
  if ( ( _dir[0] == (TQChar)'~' ) && ( m_strProtocol == fileProt ))
  {
    m_strPath_encoded = TQString::null;
    m_strPath = TQDir::homeDirPath();
    m_strPath += "/";
    m_strPath += _dir.right(m_strPath.length() - 1);
    setHTMLRef( TQString::null );
    m_strQuery_encoded = TQString::null;
    return true;
  }

  // relative path
  // we always work on the past of the first url.
  // Sub URLs are not touched.

  // append '/' if necessary
  TQString p = path(1);
  p += _dir;
  p = cleanpath( p, true, false );
  setPath( p );

  setHTMLRef( TQString::null );
  m_strQuery_encoded = TQString::null;

  return true;
}

KURL KURL::upURL( ) const
{
  if (!query().isEmpty())
  {
     KURL u(*this);
     u._setQuery(TQString::null);
     return u;
  };

  if (!hasSubURL())
  {
     KURL u(*this);

     u.cd("../");

     return u;
  }

  // We have a subURL.
  KURL::List lst = split( *this );
  if (lst.isEmpty())
      return KURL(); // Huh?
  while (true)
  {
     KURL &u = lst.last();
     TQString old = u.path();
     u.cd("../");
     if (u.path() != old)
         break; // Finshed.
     if (lst.count() == 1)
         break; // Finished.
     lst.remove(lst.fromLast());
  }
  return join( lst );
}

TQString KURL::htmlRef() const
{
  if ( !hasSubURL() )
  {
    return decode( ref() );
  }

  List lst = split( *this );
  return decode( (*lst.begin()).ref() );
}

TQString KURL::encodedHtmlRef() const
{
  if ( !hasSubURL() )
  {
    return ref();
  }

  List lst = split( *this );
  return (*lst.begin()).ref();
}

void KURL::setHTMLRef( const TQString& _ref )
{
  if ( !hasSubURL() )
  {
    m_strRef_encoded = encode( _ref, 0, 0 /*?*/);
    return;
  }

  List lst = split( *this );

  (*lst.begin()).setRef( encode( _ref, 0, 0 /*?*/) );

  *this = join( lst );
}

bool KURL::hasHTMLRef() const
{
  if ( !hasSubURL() )
  {
    return hasRef();
  }

  List lst = split( *this );
  return (*lst.begin()).hasRef();
}

void
KURL::setProtocol( const TQString& _txt )
{
   m_strProtocol = _txt;
   if ( m_iUriMode == Auto ) m_iUriMode = uriModeForProtocol( m_strProtocol );
   m_bIsMalformed = false;
}

void
KURL::setUser( const TQString& _txt )
{
   if ( _txt.isEmpty() )
     m_strUser = TQString::null;
   else
     m_strUser = _txt;
}

void
KURL::setPass( const TQString& _txt )
{
   if ( _txt.isEmpty() )
     m_strPass = TQString::null;
   else
     m_strPass = _txt;
}

void
KURL::setHost( const TQString& _txt )
{
  if ( m_iUriMode == Auto )
    m_iUriMode = URL;
  switch ( m_iUriMode )
  {
  case URL:
#ifndef KDE_QT_ONLY
   m_strHost = KIDNA::toUnicode(_txt);
   if (m_strHost.isEmpty())
      m_strHost = _txt.lower(); // Probably an invalid hostname, but...
#else
   m_strHost = _txt.lower();
#endif
    break;
  default:
    m_strHost = _txt;
    break;
  }
}

void
KURL::setPort( unsigned short int _p )
{
   m_iPort = _p;
}

void KURL::setPath( const TQString & path )
{
  if (isEmpty())
    m_bIsMalformed = false;
  if (m_strProtocol.isEmpty())
  {
    m_strProtocol = fileProt;
  }
  m_strPath = path;
  m_strPath_encoded = TQString::null;
  if ( m_iUriMode == Auto )
    m_iUriMode = URL;
}

void KURL::setDirectory( const TQString &dir)
{
  if ( dir.endsWith("/"))
     setPath(dir);
  else
     setPath(dir+"/");
}

void KURL::setQuery( const TQString &_txt, int encoding_hint)
{
   if (_txt[0] == (TQChar)'?')
      _setQuery( _txt.length() > 1 ? _txt.mid(1) : "" /*empty, not null*/, encoding_hint );
   else
      _setQuery( _txt, encoding_hint );
}

// This is a private function that expects a query without '?'
void KURL::_setQuery( const TQString &_txt, int encoding_hint)
{
   m_strQuery_encoded = _txt;
   if (!_txt.length())
      return;

   int l = m_strQuery_encoded.length();
   int i = 0;
   TQString result;
   while (i < l)
   {
      int s = i;
      // Re-encode. Break encoded string up according to the reserved
      // characters '&:;=/?' and re-encode part by part.
      while(i < l)
      {
         char c = m_strQuery_encoded[i].latin1();
         if ((c == '&') || (c == ':') || (c == ';') ||
             (c == '=') || (c == '/') || (c == '?'))
            break;
         i++;
      }
      if (i > s)
      {
         TQString tmp = m_strQuery_encoded.mid(s, i-s);
         TQString newTmp;
         decode( tmp, newTmp, tmp, encoding_hint, false );
         result += tmp;
      }
      if (i < l)
      {
         result += m_strQuery_encoded[i];
         i++;
      }
   }
   m_strQuery_encoded = result;
}

TQString KURL::query() const
{
    if (m_strQuery_encoded.isNull())
        return TQString::null;
    return '?'+m_strQuery_encoded;
}

TQString KURL::decode_string(const TQString &str, int encoding_hint)
{
   return decode(str, encoding_hint);
}

TQString KURL::encode_string(const TQString &str, int encoding_hint)
{
   return encode(str, 1, encoding_hint);
}

TQString KURL::encode_string_no_slash(const TQString &str, int encoding_hint)
{
   return encode(str, 0, encoding_hint);
}

bool urlcmp( const TQString& _url1, const TQString& _url2 )
{
  // Both empty ?
  if ( _url1.isEmpty() && _url2.isEmpty() )
    return true;
  // Only one empty ?
  if ( _url1.isEmpty() || _url2.isEmpty() )
    return false;

  KURL::List list1 = KURL::split( _url1 );
  KURL::List list2 = KURL::split( _url2 );

  // Malformed ?
  if ( list1.isEmpty() || list2.isEmpty() )
    return false;

  return ( list1 == list2 );
}

bool urlcmp( const TQString& _url1, const TQString& _url2, bool _ignore_trailing, bool _ignore_ref )
{
  // Both empty ?
  if ( _url1.isEmpty() && _url2.isEmpty() )
    return true;
  // Only one empty ?
  if ( _url1.isEmpty() || _url2.isEmpty() )
    return false;

  KURL::List list1 = KURL::split( _url1 );
  KURL::List list2 = KURL::split( _url2 );

  // Malformed ?
  if ( list1.isEmpty() || list2.isEmpty() )
    return false;

  unsigned int size = list1.count();
  if ( list2.count() != size )
    return false;

  if ( _ignore_ref )
  {
    (*list1.begin()).setRef(TQString::null);
    (*list2.begin()).setRef(TQString::null);
  }

  KURL::List::Iterator it1 = list1.begin();
  KURL::List::Iterator it2 = list2.begin();
  for( ; it1 != list1.end() ; ++it1, ++it2 )
    if ( !(*it1).equals( *it2, _ignore_trailing ) )
      return false;

  return true;
}

TQMap< TQString, TQString > KURL::queryItems( int options ) const {
  return queryItems(options, 0);
}

TQMap< TQString, TQString > KURL::queryItems( int options, int encoding_hint ) const {
  if ( m_strQuery_encoded.isEmpty() )
    return TQMap<TQString,TQString>();

  TQMap< TQString, TQString > result;
  TQStringList items = TQStringList::split( '&', m_strQuery_encoded );
  for ( TQStringList::const_iterator it = items.begin() ; it != items.end() ; ++it ) {
    int equal_pos = (*it).find( '=' );
    if ( equal_pos > 0 ) { // = is not the first char...
      TQString name = (*it).left( equal_pos );
      if ( options & CaseInsensitiveKeys )
	name = name.lower();
      TQString value = (*it).mid( equal_pos + 1 );
      if ( value.isEmpty() )
	result.insert( name, TQString::fromLatin1("") );
      else {
	// ### why is decoding name not necessary?
	value.replace( '+', ' ' ); // + in queries means space
	result.insert( name, decode_string( value, encoding_hint ) );
      }
    } else if ( equal_pos < 0 ) { // no =
      TQString name = (*it);
      if ( options & CaseInsensitiveKeys )
	name = name.lower();
      result.insert( name, TQString::null );
    }
  }

  return result;
}

TQString KURL::queryItem( const TQString& _item ) const
{
  return queryItem( _item, 0 );
}

TQString KURL::queryItem( const TQString& _item, int encoding_hint ) const
{
  TQString item = _item + '=';
  if ( m_strQuery_encoded.length() <= 1 )
    return TQString::null;

  TQStringList items = TQStringList::split( '&', m_strQuery_encoded );
  unsigned int _len = item.length();
  for ( TQStringList::ConstIterator it = items.begin(); it != items.end(); ++it )
  {
    if ( (*it).startsWith( item ) )
    {
      if ( (*it).length() > _len )
      {
        TQString str = (*it).mid( _len );
        str.replace( '+', ' ' ); // + in queries means space.
        return decode_string( str, encoding_hint );
      }
      else // empty value
        return TQString::fromLatin1("");
    }
  }

  return TQString::null;
}

void KURL::removeQueryItem( const TQString& _item )
{
  TQString item = _item + '=';
  if ( m_strQuery_encoded.length() <= 1 )
    return;

  TQStringList items = TQStringList::split( '&', m_strQuery_encoded );
  for ( TQStringList::Iterator it = items.begin(); it != items.end(); )
  {
    if ( (*it).startsWith( item ) || (*it == _item) )
    {
      TQStringList::Iterator deleteIt = it;
      ++it;
      items.remove(deleteIt);
    }
    else
    {
       ++it;
    }
  }
  m_strQuery_encoded = items.join( "&" );
}

void KURL::addQueryItem( const TQString& _item, const TQString& _value, int encoding_hint )
{
  TQString item = _item + '=';
  TQString value = encode( _value, 0, encoding_hint );

  if (!m_strQuery_encoded.isEmpty())
     m_strQuery_encoded += '&';
  m_strQuery_encoded += item + value;
}

// static
KURL KURL::fromPathOrURL( const TQString& text )
{
    if ( text.isEmpty() )
        return KURL();

    KURL url;
    if (!TQDir::isRelativePath(text))
        url.setPath( text );
    else
        url = text;

    return url;
}

static TQString _relativePath(const TQString &base_dir, const TQString &path, bool &isParent)
{
   TQString _base_dir(TQDir::cleanDirPath(base_dir));
   TQString _path(TQDir::cleanDirPath(path.isEmpty() || (path[0] != (TQChar)'/') ? _base_dir+"/"+path : path));

   if (_base_dir.isEmpty())
      return _path;

   if (_base_dir[_base_dir.length()-1] != '/')
      _base_dir.append('/');

   TQStringList list1 = TQStringList::split('/', _base_dir);
   TQStringList list2 = TQStringList::split('/', _path);

   // Find where they meet
   uint level = 0;
   uint maxLevel = TQMIN(list1.count(), list2.count());
   while((level < maxLevel) && (list1[level] == list2[level])) level++;

   TQString result;
   // Need to go down out of the first path to the common branch.
   for(uint i = level; i < list1.count(); i++)
      result.append("../");

   // Now up up from the common branch to the second path.
   for(uint i = level; i < list2.count(); i++)
      result.append(list2[i]).append("/");

   if ((level < list2.count()) && (path[path.length()-1] != (TQChar)'/'))
      result.truncate(result.length()-1);

   isParent = (level == list1.count());

   return result;
}

TQString KURL::relativePath(const TQString &base_dir, const TQString &path, bool *isParent)
{
   bool parent = false;
   TQString result = _relativePath(base_dir, path, parent);
   if (parent)
      result.prepend("./");

   if (isParent)
      *isParent = parent;

   return result;
}


TQString KURL::relativeURL(const KURL &base_url, const KURL &url, int encoding_hint)
{
   if ((url.protocol() != base_url.protocol()) ||
       (url.host() != base_url.host()) ||
       (url.port() && url.port() != base_url.port()) ||
       (url.hasUser() && url.user() != base_url.user()) ||
       (url.hasPass() && url.pass() != base_url.pass()))
   {
      return url.url(0, encoding_hint);
   }

   TQString relURL;

   if ((base_url.path() != url.path()) || (base_url.query() != url.query()))
   {
      bool dummy;
      TQString basePath = base_url.directory(false, false);
      relURL = encode( _relativePath(basePath, url.path(), dummy), 1, encoding_hint);
      relURL += url.query();
   }

   if ( url.hasRef() )
   {
      relURL += "#";
      relURL += url.ref();
   }

   if ( relURL.isEmpty() )
      return "./";

   return relURL;
}

int KURL::uriMode() const
{
  return m_iUriMode;
}

KURL::URIMode KURL::uriModeForProtocol(const TQString& protocol)
{
#ifndef KDE_QT_ONLY
    KURL::URIMode mode = Auto;
    if (protocol == fileProt)
        return URL;
    if (KGlobal::_instance)
        mode = KProtocolInfo::uriParseMode(protocol);
    if (mode == Auto ) {
#else
        KURL::URIMode mode = Auto;
#endif
	if ( protocol == "ed2k" || protocol == "sig2dat" || protocol == "slsk" || protocol == "data" ) mode = RawURI;
	else if ( protocol == "mailto" ) mode = Mailto;
	else mode = URL;
#ifndef KDE_QT_ONLY
    }
#endif
    return mode;
}