• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
kurl.cpp
1 /*
2  Copyright (C) 1999 Torben Weis <weis@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 /*
21  * The currently active RFC for URL/URIs is RFC3986
22  * Previous (and now deprecated) RFCs are RFC1738 and RFC2396
23  */
24 
25 #include "kurl.h"
26 
27 // KDE_QT_ONLY is first used for dcop/client (e.g. marshalling)
28 #ifndef KDE_QT_ONLY
29 #include <kdebug.h>
30 #include <tdeglobal.h>
31 #include <kidna.h>
32 #include <kprotocolinfo.h>
33 #endif
34 
35 #include <stdio.h>
36 #include <assert.h>
37 #include <ctype.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 
41 #include <tqurl.h>
42 #include <tqdir.h>
43 #include <tqstringlist.h>
44 #include <tqregexp.h>
45 #include <tqstylesheet.h>
46 #include <tqmap.h>
47 #include <tqtextcodec.h>
48 #include <tqmutex.h>
49 
50 #ifdef Q_WS_WIN
51 # define KURL_ROOTDIR_PATH "C:/"
52 #else
53 # define KURL_ROOTDIR_PATH "/"
54 #endif
55 
56 static const TQString fileProt = "file";
57 
58 static TQTextCodec * codecForHint( int encoding_hint /* not 0 ! */ )
59 {
60  return TQTextCodec::codecForMib( encoding_hint );
61 }
62 
63 // encoding_offset:
64 // 0 encode both @ and /
65 // 1 encode @ but not /
66 // 2 encode neither @ or /
67 static TQString encode( const TQString& segment, int encoding_offset, int encoding_hint, bool isRawURI = false )
68 {
69  const char *encode_string = "/@<>#\"&?={}|^~[]\'`\\:+%";
70  encode_string += encoding_offset;
71 
72  TQCString local;
73  if (encoding_hint==0)
74  local = segment.local8Bit();
75  else
76  {
77  TQTextCodec * textCodec = codecForHint( encoding_hint );
78  if (!textCodec)
79  local = segment.local8Bit();
80  else
81  local = textCodec->fromUnicode( segment );
82  }
83 
84  int old_length = isRawURI ? local.size() - 1 : local.length();
85 
86  if ( old_length < 1 )
87  return segment.isNull() ? TQString::null : TQString(""); // differentiate null and empty
88 
89  // a worst case approximation
90  TQChar *new_segment = new TQChar[ old_length * 3 + 1 ];
91  int new_length = 0;
92 
93  for ( int i = 0; i < old_length; i++ )
94  {
95  // 'unsave' and 'reserved' characters
96  // according to RFC 1738,
97  // 2.2. URL Character Encoding Issues (pp. 3-4)
98  // WABA: Added non-ascii
99  unsigned char character = local[i];
100  if ( (character <= 32) || (character >= 127) ||
101  strchr(encode_string, character) )
102  {
103  new_segment[ new_length++ ] = '%';
104 
105  unsigned int c = character / 16;
106  c += (c > 9) ? ('A' - 10) : '0';
107  new_segment[ new_length++ ] = c;
108 
109  c = character % 16;
110  c += (c > 9) ? ('A' - 10) : '0';
111  new_segment[ new_length++ ] = c;
112 
113  }
114  else
115  new_segment[ new_length++ ] = (TQChar)local[i];
116  }
117 
118  TQString result = TQString(new_segment, new_length);
119  delete [] new_segment;
120  return result;
121 }
122 
123 static TQString encodeHost( const TQString& segment, bool encode_slash, int encoding_hint )
124 {
125  // Hostnames are encoded differently
126  // we use the IDNA transformation instead
127 
128  // Note: when merging qt-addon, use QResolver::domainToAscii here
129 #ifndef KDE_QT_ONLY
130  Q_UNUSED( encode_slash );
131  Q_UNUSED( encoding_hint );
132  TQString host = KIDNA::toAscii(segment);
133  if (host.isEmpty())
134  return segment;
135  return host;
136 #else
137  return encode(segment, encode_slash ? 0 : 1, encoding_hint);
138 #endif
139 }
140 
141 static int hex2int( unsigned int _char )
142 {
143  if ( _char >= 'A' && _char <='F')
144  return _char - 'A' + 10;
145  if ( _char >= 'a' && _char <='f')
146  return _char - 'a' + 10;
147  if ( _char >= '0' && _char <='9')
148  return _char - '0';
149  return -1;
150 }
151 
152 // WABA: The result of lazy_encode isn't usable for a URL which
153 // needs to satisfies RFC requirements. However, the following
154 // operation will make it usable again:
155 // encode(decode(...))
156 //
157 // As a result one can see that url.prettyURL() does not result in
158 // a RFC compliant URL but that the following sequence does:
159 // KURL(url.prettyURL()).url()
160 
161 
162 static TQString lazy_encode( const TQString& segment, bool encodeAt=true )
163 {
164  int old_length = segment.length();
165 
166  if ( !old_length )
167  return TQString::null;
168 
169  // a worst case approximation
170  TQChar *new_segment = new TQChar[ old_length * 3 + 1 ];
171  int new_length = 0;
172 
173  for ( int i = 0; i < old_length; i++ )
174  {
175  unsigned int character = segment[i].unicode(); // Don't use latin1()
176  // It returns 0 for non-latin1 values
177  // Small set of really ambiguous chars
178  if ((character < 32) || // Low ASCII
179  ((character == '%') && // The escape character itself
180  (i+2 < old_length) && // But only if part of a valid escape sequence!
181  (hex2int(segment[i+1].unicode())!= -1) &&
182  (hex2int(segment[i+2].unicode())!= -1)) ||
183  (character == '?') || // Start of query delimiter
184  ((character == '@') && encodeAt) || // Username delimiter
185  (character == '#') || // Start of reference delimiter
186  ((character == 32) && (i+1 == old_length || segment[i+1] == (TQChar)' '))) // A trailing space
187  {
188  new_segment[ new_length++ ] = '%';
189 
190  unsigned int c = character / 16;
191  c += (c > 9) ? ('A' - 10) : '0';
192  new_segment[ new_length++ ] = c;
193 
194  c = character % 16;
195  c += (c > 9) ? ('A' - 10) : '0';
196  new_segment[ new_length++ ] = c;
197  }
198  else
199  new_segment[ new_length++ ] = segment[i];
200  }
201 
202  TQString result = TQString(new_segment, new_length);
203  delete [] new_segment;
204  return result;
205 }
206 
207 static void decode( const TQString& segment, TQString &decoded, TQString &encoded, int encoding_hint=0, bool updateDecoded = true, bool isRawURI = false )
208 {
209  decoded = TQString::null;
210  encoded = segment;
211 
212  int old_length = segment.length();
213  if ( !old_length )
214  return;
215 
216  TQTextCodec *textCodec = 0;
217  if (encoding_hint)
218  textCodec = codecForHint( encoding_hint );
219 
220  if (!textCodec)
221  textCodec = TQTextCodec::codecForLocale();
222 
223  TQCString csegment = textCodec->fromUnicode(segment);
224  // Check if everything went ok
225  if (textCodec->toUnicode(csegment) != segment)
226  {
227  // Uh oh
228  textCodec = codecForHint( 106 ); // Fall back to utf-8
229  csegment = textCodec->fromUnicode(segment);
230  }
231  old_length = csegment.length();
232 
233  int new_length = 0;
234  int new_length2 = 0;
235 
236  // make a copy of the old one
237  char *new_segment = new char[ old_length + 1 ];
238  TQChar *new_usegment = new TQChar[ old_length * 3 + 1 ];
239 
240  int i = 0;
241  while( i < old_length )
242  {
243  bool bReencode = false;
244  unsigned char character = csegment[ i++ ];
245  if ((character <= ' ') || (character > 127))
246  bReencode = true;
247 
248  new_usegment [ new_length2++ ] = character;
249  if (character == '%' )
250  {
251  int a = i+1 < old_length ? hex2int( csegment[i] ) : -1;
252  int b = i+1 < old_length ? hex2int( csegment[i+1] ) : -1;
253  if ((a == -1) || (b == -1)) // Only replace if sequence is valid
254  {
255  // Contains stray %, make sure to re-encode!
256  bReencode = true;
257  }
258  else
259  {
260  // Valid %xx sequence
261  character = a * 16 + b; // Replace with value of %dd
262  if (!isRawURI && !character && updateDecoded)
263  break; // Stop at %00
264 
265  new_usegment [ new_length2++ ] = (unsigned char) csegment[i++];
266  new_usegment [ new_length2++ ] = (unsigned char) csegment[i++];
267  }
268  }
269  if (bReencode)
270  {
271  new_length2--;
272  new_usegment [ new_length2++ ] = '%';
273 
274  unsigned int c = character / 16;
275  c += (c > 9) ? ('A' - 10) : '0';
276  new_usegment[ new_length2++ ] = c;
277 
278  c = character % 16;
279  c += (c > 9) ? ('A' - 10) : '0';
280  new_usegment[ new_length2++ ] = c;
281  }
282 
283  new_segment [ new_length++ ] = character;
284  }
285  new_segment [ new_length ] = 0;
286 
287  encoded = TQString( new_usegment, new_length2);
288 
289  // Encoding specified
290  if (updateDecoded)
291  {
292  decoded = textCodec->toUnicode( new_segment );
293  if ( isRawURI ) {
294  int length = tqstrlen( new_segment );
295  while ( length < new_length ) {
296  decoded += TQChar::null;
297  length += 1;
298  decoded += textCodec->toUnicode( new_segment + length );
299  length += tqstrlen( new_segment + length );
300  }
301  }
302 
303  TQCString validate = textCodec->fromUnicode(decoded);
304 
305  if (strcmp(validate.data(), new_segment) != 0)
306  {
307  decoded = TQString::fromLocal8Bit(new_segment, new_length);
308  }
309  }
310 
311  delete [] new_segment;
312  delete [] new_usegment;
313 }
314 
315 static TQString decode(const TQString &segment, int encoding_hint = 0, bool isRawURI = false)
316 {
317  TQString result;
318  TQString tmp;
319  decode(segment, result, tmp, encoding_hint, true, isRawURI);
320  return result;
321 }
322 
323 static TQString cleanpath(const TQString &_path, bool cleanDirSeparator, bool decodeDots)
324 {
325  if (_path.isEmpty()) return TQString::null;
326 
327  if (TQDir::isRelativePath(_path))
328  return _path; // Don't mangle mailto-style URLs
329 
330  TQString path = _path;
331 
332  int len = path.length();
333 
334  if (decodeDots)
335  {
336 #ifndef KDE_QT_ONLY
337  static const TQString &encodedDot = TDEGlobal::staticQString("%2e");
338 #else
339  TQString encodedDot("%2e");
340 #endif
341  if (path.find(encodedDot, 0, false) != -1)
342  {
343 #ifndef KDE_QT_ONLY
344  static const TQString &encodedDOT = TDEGlobal::staticQString("%2E"); // Uppercase!
345 #else
346  TQString encodedDOT("%2E");
347 #endif
348  path.replace(encodedDot, ".");
349  path.replace(encodedDOT, ".");
350  len = path.length();
351  }
352  }
353 
354  bool slash = (len && path[len-1] == '/') ||
355  (len > 1 && path[len-2] == '/' && path[len-1] == '.');
356 
357  // The following code cleans up directory path much like
358  // TQDir::cleanDirPath() except it can be made to ignore multiple
359  // directory separators by setting the flag to false. That fixes
360  // bug# 15044, mail.altavista.com and other similar brain-dead server
361  // implementations that do not follow what has been specified in
362  // RFC 2396!! (dA)
363  TQString result;
364  int cdUp, orig_pos, pos;
365 
366  cdUp = 0;
367  pos = orig_pos = len;
368  while ( pos && (pos = path.findRev('/',--pos)) != -1 )
369  {
370  len = orig_pos - pos - 1;
371  if ( len == 2 && path[pos+1] == '.' && path[pos+2] == '.' )
372  cdUp++;
373  else
374  {
375  // Ignore any occurrences of '.'
376  // This includes entries that simply do not make sense like /..../
377  if ( (len || !cleanDirSeparator) &&
378  (len != 1 || path[pos+1] != '.' ) )
379  {
380  if ( !cdUp )
381  result.prepend(path.mid(pos, len+1));
382  else
383  cdUp--;
384  }
385  }
386  orig_pos = pos;
387  }
388 
389 #ifdef Q_WS_WIN // prepend drive letter if exists (js)
390  if (orig_pos >= 2 && isalpha(path[0].latin1()) && path[1]==':') {
391  result.prepend(TQString(path[0])+":");
392  }
393 #endif
394 
395  if ( result.isEmpty() )
396  result = KURL_ROOTDIR_PATH;
397  else if ( slash && result[result.length()-1] != '/' )
398  result.append('/');
399 
400  return result;
401 }
402 
403 class KURLPrivate
404 {
405 public:
406  TQString m_strInternalReferenceURL;
407 };
408 
409 bool KURL::isRelativeURL(const TQString &_url)
410 {
411  int len = _url.length();
412  if (!len) return true; // Very short relative URL.
413  const TQChar *str = _url.unicode();
414 
415  // Absolute URL must start with alpha-character
416  if (!isalpha(str[0].latin1()))
417  return true; // Relative URL
418 
419  for(int i = 1; i < len; i++)
420  {
421  char c = str[i].latin1(); // Note: non-latin1 chars return 0!
422  if (c == ':')
423  return false; // Absolute URL
424 
425  // Protocol part may only contain alpha, digit, + or -
426  if (!isalpha(c) && !isdigit(c) && (c != '+') && (c != '-'))
427  return true; // Relative URL
428  }
429  // URL did not contain ':'
430  return true; // Relative URL
431 }
432 
433 KURL::List::List(const KURL &url)
434 {
435  append( url );
436 }
437 
438 KURL::List::List(const TQStringList &list)
439 {
440  for (TQStringList::ConstIterator it = list.begin();
441  it != list.end();
442  it++)
443  {
444  append( KURL(*it) );
445  }
446 }
447 
448 TQStringList KURL::List::toStringList() const
449 {
450  TQStringList lst;
451  for( KURL::List::ConstIterator it = begin();
452  it != end();
453  it++)
454  {
455  lst.append( (*it).url() );
456  }
457  return lst;
458 }
459 
460 
461 KURL::KURL()
462 {
463  d = new KURLPrivate();
464  reset();
465 }
466 
467 KURL::~KURL()
468 {
469  if (d) {
470  delete d;
471  }
472 }
473 
474 
475 KURL::KURL( const TQString &url, int encoding_hint )
476 {
477  d = new KURLPrivate();
478  reset();
479  parse( url, encoding_hint );
480 }
481 
482 KURL::KURL( const char * url, int encoding_hint )
483 {
484  d = new KURLPrivate();
485  reset();
486  parse( TQString::fromLatin1(url), encoding_hint );
487 }
488 
489 KURL::KURL( const TQCString& url, int encoding_hint )
490 {
491  d = new KURLPrivate();
492  reset();
493  parse( TQString::fromLatin1(url), encoding_hint );
494 }
495 
496 KURL::KURL( const KURL& _u )
497 {
498  d = new KURLPrivate();
499  *this = _u;
500  d->m_strInternalReferenceURL = _u.d->m_strInternalReferenceURL;
501 }
502 
503 TQDataStream & operator<< (TQDataStream & s, const KURL & a)
504 {
505  TQString QueryForWire=a.m_strQuery_encoded;
506  if (!a.m_strQuery_encoded.isNull())
507  QueryForWire.prepend("?");
508 
509  s << a.m_strProtocol << a.m_strUser << a.m_strPass << a.m_strHost
510  << a.m_strPath << a.m_strPath_encoded << QueryForWire << a.m_strRef_encoded
511  << TQ_INT8(a.m_bIsMalformed ? 1 : 0) << a.m_iPort;
512  return s;
513 }
514 
515 TQDataStream & operator>> (TQDataStream & s, KURL & a)
516 {
517  TQ_INT8 malf;
518  TQString QueryFromWire;
519  s >> a.m_strProtocol >> a.m_strUser >> a.m_strPass >> a.m_strHost
520  >> a.m_strPath >> a.m_strPath_encoded >> QueryFromWire >> a.m_strRef_encoded
521  >> malf >> a.m_iPort;
522  a.m_bIsMalformed = (malf != 0);
523 
524  if ( QueryFromWire.isNull() )
525  a.m_strQuery_encoded = TQString::null;
526  else if ( QueryFromWire.length() == 1 ) // empty query
527  a.m_strQuery_encoded = "";
528  else
529  a.m_strQuery_encoded = QueryFromWire.mid(1);
530 
531  a.m_iUriMode = KURL::uriModeForProtocol( a.m_strProtocol );
532 
533  return s;
534 }
535 
536 #ifndef QT_NO_NETWORKPROTOCOL
537 KURL::KURL( const TQUrl &u )
538 {
539  d = new KURLPrivate();
540  *this = u;
541 }
542 #endif
543 
544 KURL::KURL( const KURL& _u, const TQString& _rel_url, int encoding_hint )
545 {
546  d = new KURLPrivate();
547  d->m_strInternalReferenceURL = _u.d->m_strInternalReferenceURL;
548 
549  if (_u.hasSubURL()) // Operate on the last suburl, not the first
550  {
551  KURL::List lst = split( _u );
552  KURL u(lst.last(), _rel_url, encoding_hint);
553  lst.remove( lst.last() );
554  lst.append( u );
555  *this = join( lst );
556  d->m_strInternalReferenceURL = _u.d->m_strInternalReferenceURL;
557  return;
558  }
559  // WORKAROUND THE RFC 1606 LOOPHOLE THAT ALLOWS
560  // http:/index.html AS A VALID SYNTAX FOR RELATIVE
561  // URLS. ( RFC 2396 section 5.2 item # 3 )
562  TQString rUrl = _rel_url;
563  int len = _u.m_strProtocol.length();
564  if ( !_u.m_strHost.isEmpty() && !rUrl.isEmpty() &&
565  rUrl.find( _u.m_strProtocol, 0, false ) == 0 &&
566  rUrl[len] == ':' && (rUrl[len+1] != '/' ||
567  (rUrl[len+1] == '/' && rUrl[len+2] != '/')) )
568  {
569  rUrl.remove( 0, rUrl.find( ':' ) + 1 );
570  }
571 
572  if ( rUrl.isEmpty() )
573  {
574  *this = _u;
575  }
576  else if ( rUrl[0] == '#' )
577  {
578  *this = _u;
579  m_strRef_encoded = rUrl.mid(1);
580  if ( m_strRef_encoded.isNull() )
581  m_strRef_encoded = ""; // we know there was an (empty) html ref, we saw the '#'
582  }
583  else if ( isRelativeURL( rUrl) )
584  {
585  *this = _u;
586  m_strQuery_encoded = TQString::null;
587  m_strRef_encoded = TQString::null;
588  if ( rUrl[0] == '/')
589  {
590  if ((rUrl.length() > 1) && (rUrl[1] == '/'))
591  {
592  m_strHost = TQString::null;
593  // File protocol returns file:/// without host, strip // from rUrl
594  if (_u.m_strProtocol == fileProt)
595  rUrl.remove(0, 2);
596  }
597  m_strPath = TQString::null;
598  m_strPath_encoded = TQString::null;
599  }
600  else if ( rUrl[0] != '?' )
601  {
602  int pos = m_strPath.findRev( '/' );
603  if (pos >= 0)
604  m_strPath.truncate(pos);
605  m_strPath += '/';
606  if (!m_strPath_encoded.isEmpty())
607  {
608  pos = m_strPath_encoded.findRev( '/' );
609  if (pos >= 0)
610  m_strPath_encoded.truncate(pos);
611  m_strPath_encoded += '/';
612  }
613  }
614  else
615  {
616  if ( m_strPath.isEmpty() )
617  m_strPath = '/';
618  }
619  KURL tmp( url() + rUrl, encoding_hint);
620  *this = tmp;
621  cleanPath(false);
622  }
623  else
624  {
625  KURL tmp( rUrl, encoding_hint);
626  *this = tmp;
627  // Preserve userinfo if applicable.
628  if (!_u.m_strUser.isEmpty() && m_strUser.isEmpty() && (_u.m_strHost == m_strHost) && (_u.m_strProtocol == m_strProtocol))
629  {
630  m_strUser = _u.m_strUser;
631  m_strPass = _u.m_strPass;
632  }
633  cleanPath(false);
634  }
635 }
636 
637 void KURL::reset()
638 {
639  m_strProtocol = TQString::null;
640  m_strUser = TQString::null;
641  m_strPass = TQString::null;
642  m_strHost = TQString::null;
643  m_strPath = TQString::null;
644  m_strPath_encoded = TQString::null;
645  m_strQuery_encoded = TQString::null;
646  m_strRef_encoded = TQString::null;
647  m_bIsMalformed = true;
648  m_iPort = 0;
649  m_iUriMode = Auto;
650 }
651 
652 bool KURL::isEmpty() const
653 {
654  return (m_strPath.isEmpty() && m_strProtocol.isEmpty());
655 }
656 
657 void KURL::parse( const TQString& _url, int encoding_hint )
658 {
659  if ( _url.isEmpty() || m_iUriMode == Invalid )
660  {
661  m_strProtocol = _url;
662  m_iUriMode = Invalid;
663  return;
664  }
665 
666  const TQChar* buf = _url.unicode();
667  const TQChar* orig = buf;
668  uint len = _url.length();
669  uint pos = 0;
670 
671  // Node 1: Accept alpha or slash
672  TQChar x = buf[pos++];
673 #ifdef Q_WS_WIN
674  /* win32: accept <letter>: or <letter>:/ or <letter>:\ */
675  const bool alpha = isalpha((int)x);
676  if (alpha && len<2)
677  goto NodeErr;
678  if (alpha && buf[pos]==':' && (len==2 || (len>2 && (buf[pos+1]=='/' || buf[pos+1]=='\\'))))
679 #else
680  if ( x == (TQChar)'/' )
681 #endif
682  {
683  // A slash means we immediately proceed to parse it as a file URL.
684  m_iUriMode = URL;
685  m_strProtocol = fileProt;
686  parseURL( _url, encoding_hint );
687  return;
688  }
689  if ( !isalpha( (int)x ) )
690  goto NodeErr;
691 
692  // Node 2: Accept any amount of (alpha|digit|'+'|'-')
693  // '.' is not currently accepted, because current KURL may be confused.
694  // Proceed with :// :/ or :
695  while( pos < len && (isalpha((int)buf[pos]) || isdigit((int)buf[pos]) ||
696  buf[pos] == (TQChar)'+' || buf[pos] == (TQChar)'-')) pos++;
697 
698  if (pos < len && buf[pos] == (TQChar)':' )
699  {
700  m_strProtocol = TQString( orig, pos ).lower();
701  if ( m_iUriMode == Auto )
702  m_iUriMode = uriModeForProtocol( m_strProtocol );
703  // Proceed to correct parse function.
704  switch ( m_iUriMode )
705  {
706  case RawURI:
707  parseRawURI( _url );
708  return;
709  case Mailto:
710  parseMailto( _url );
711  return;
712  case URL:
713  parseURL( _url, encoding_hint );
714  return;
715  default:
716  // Unknown URI mode results in an invalid URI.
717  break;
718  }
719  }
720 
721 NodeErr:
722  reset();
723  m_strProtocol = _url;
724  m_iUriMode = Invalid;
725 }
726 
727 void KURL::parseRawURI( const TQString& _url, int encoding_hint )
728 {
729  uint len = _url.length();
730  const TQChar* buf = _url.unicode();
731 
732  uint pos = 0;
733 
734  // Accept any amount of (alpha|digit|'+'|'-')
735  // '.' is not currently accepted, because current KURL may be confused.
736  // Proceed with :
737  while( pos < len && (isalpha((int)buf[pos]) || isdigit((int)buf[pos]) ||
738  buf[pos] == (TQChar)'+' || buf[pos] == (TQChar)'-')) pos++;
739 
740  // Note that m_strProtocol is already set here, so we just skip over the protocol.
741  if (pos < len && buf[pos] == (TQChar)':' )
742  pos++;
743  else { // can't happen, the caller checked all this already
744  reset();
745  m_strProtocol = _url;
746  m_iUriMode = Invalid;
747  return;
748  }
749 
750  if ( pos == len ) // can't happen, the caller checked this already
751  m_strPath = TQString::null;
752  else
753  m_strPath = decode( TQString( buf + pos, len - pos ), encoding_hint, true );
754 
755  m_bIsMalformed = false;
756 
757  return;
758 }
759 
760 void KURL::parseMailto( const TQString& _url, int encoding_hint )
761 {
762  parseURL( _url, encoding_hint);
763  if ( m_bIsMalformed )
764  return;
765  TQRegExp mailre("(.+@)(.+)");
766  if ( mailre.exactMatch( m_strPath ) )
767  {
768 #ifndef KDE_QT_ONLY
769  TQString host = KIDNA::toUnicode( mailre.cap( 2 ) );
770  if (host.isEmpty())
771  host = TQString(mailre.cap( 2 )).lower();
772 #else
773  TQString host = TQString(mailre.cap( 2 )).lower();
774 #endif
775  m_strPath = mailre.cap( 1 ) + host;
776  }
777 }
778 
779 void KURL::parseURL( const TQString& _url, int encoding_hint )
780 {
781  TQString port;
782  bool badHostName = false;
783  int start = 0;
784  uint len = _url.length();
785  const TQChar* buf = _url.unicode();
786 
787  TQChar delim;
788  TQString tmp;
789 
790  uint pos = 0;
791 
792  // Node 1: Accept alpha or slash
793  TQChar x = buf[pos++];
794 #ifdef Q_WS_WIN
795  /* win32: accept <letter>: or <letter>:/ or <letter>:\ */
796  const bool alpha = isalpha((int)x);
797  if (alpha && len<2)
798  goto NodeErr;
799  if (alpha && buf[pos]==(TQChar)':' && (len==2 || (len>2 && (buf[pos+1]==(TQChar)'/' || buf[pos+1]==(TQChar)'\\'))))
800 #else
801  if ( x == (TQChar)'/' )
802 #endif
803  goto Node9;
804  if ( !isalpha( (int)x ) )
805  goto NodeErr;
806 
807  // Node 2: Accept any amount of (alpha|digit|'+'|'-')
808  // '.' is not currently accepted, because current KURL may be confused.
809  // Proceed with :// :/ or :
810  while( pos < len && (isalpha((int)buf[pos]) || isdigit((int)buf[pos]) ||
811  buf[pos] == (TQChar)'+' || buf[pos] == (TQChar)'-')) pos++;
812 
813  // Note that m_strProtocol is already set here, so we just skip over the protocol.
814  if ( pos+2 < len && buf[pos] == (TQChar)':' && buf[pos+1] == (TQChar)'/' && buf[pos+2] == (TQChar)'/' )
815  {
816  pos += 3;
817  }
818  else if (pos+1 < len && buf[pos] == (TQChar)':' ) // Need to always compare length()-1 otherwise KURL passes "http:" as legal!!
819  {
820  pos++;
821  start = pos;
822  goto Node9;
823  }
824  else
825  goto NodeErr;
826 
827  //Node 3: We need at least one character here
828  if ( pos == len )
829  goto NodeErr;
830  start = pos;
831 
832  // Node 4: Accept any amount of characters.
833  if (buf[pos] == (TQChar)'[') // An IPv6 host follows.
834  goto Node8;
835  // Terminate on / or @ or ? or # or " or ; or <
836  x = buf[pos];
837  while( (x != (TQChar)':') && (x != (TQChar)'@') && (x != (TQChar)'/') && (x != (TQChar)'?') && (x != (TQChar)'#') )
838  {
839  if ((x == (TQChar)'\"') || (x == (TQChar)';') || (x == (TQChar)'<'))
840  badHostName = true;
841  if (++pos == len)
842  break;
843  x = buf[pos];
844  }
845  if ( pos == len )
846  {
847  if (badHostName)
848  goto NodeErr;
849 
850  setHost(decode(TQString( buf + start, pos - start ), encoding_hint));
851  goto NodeOk;
852  }
853  if ( x == (TQChar)'@' )
854  {
855  m_strUser = decode(TQString( buf + start, pos - start ), encoding_hint);
856  pos++;
857  goto Node7;
858  }
859  else if ( (x == (TQChar)'/') || (x == (TQChar)'?') || (x == (TQChar)'#'))
860  {
861  if (badHostName)
862  goto NodeErr;
863 
864  setHost(decode(TQString( buf + start, pos - start ), encoding_hint));
865  start = pos;
866  goto Node9;
867  }
868  else if ( x != (TQChar)':' )
869  goto NodeErr;
870  m_strUser = decode(TQString( buf + start, pos - start ), encoding_hint);
871  pos++;
872 
873  // Node 5: We need at least one character
874  if ( pos == len )
875  goto NodeErr;
876  start = pos++;
877 
878  // Node 6: Read everything until @, /, ? or #
879  while( (pos < len) &&
880  (buf[pos] != (TQChar)'@') &&
881  (buf[pos] != (TQChar)'/') &&
882  (buf[pos] != (TQChar)'?') &&
883  (buf[pos] != (TQChar)'#')) pos++;
884  // If we now have a '@' the ':' seperates user and password.
885  // Otherwise it seperates host and port.
886  if ( (pos == len) || (buf[pos] != (TQChar)'@') )
887  {
888  // Ok the : was used to separate host and port
889  if (badHostName)
890  goto NodeErr;
891  setHost(m_strUser);
892  m_strUser = TQString::null;
893  TQString tmp( buf + start, pos - start );
894  char *endptr;
895  m_iPort = (unsigned short int)strtol(tmp.ascii(), &endptr, 10);
896  if ((pos == len) && (strlen(endptr) == 0))
897  goto NodeOk;
898  // there is more after the digits
899  pos -= strlen(endptr);
900  if ((buf[pos] != (TQChar)'@') &&
901  (buf[pos] != (TQChar)'/') &&
902  (buf[pos] != (TQChar)'?') &&
903  (buf[pos] != (TQChar)'#'))
904  goto NodeErr;
905 
906  start = pos;
907  goto Node9;
908  }
909  m_strPass = decode(TQString( buf + start, pos - start), encoding_hint);
910  pos++;
911 
912  // Node 7: We need at least one character
913  Node7:
914  if ( pos == len )
915  goto NodeErr;
916 
917  Node8:
918  if (buf[pos] == (TQChar)'[')
919  {
920  // IPv6 address
921  start = ++pos; // Skip '['
922 
923  if (pos == len)
924  {
925  badHostName = true;
926  goto NodeErr;
927  }
928  // Node 8a: Read everything until ] or terminate
929  badHostName = false;
930  x = buf[pos];
931  while( (x != (TQChar)']') )
932  {
933  if ((x == (TQChar)'\"') || (x == (TQChar)';') || (x == (TQChar)'<'))
934  badHostName = true;
935  if (++pos == len)
936  {
937  badHostName = true;
938  break;
939  }
940  x = buf[pos];
941  }
942  if (badHostName)
943  goto NodeErr;
944  setHost(decode(TQString( buf + start, pos - start ), encoding_hint));
945  if (pos < len) pos++; // Skip ']'
946  if (pos == len)
947  goto NodeOk;
948  }
949  else
950  {
951  // Non IPv6 address, with a user
952  start = pos;
953 
954  // Node 8b: Read everything until / : or terminate
955  badHostName = false;
956  x = buf[pos];
957  while( (x != (TQChar)':') && (x != (TQChar)'@') && (x != (TQChar)'/') && (x != (TQChar)'?') && (x != (TQChar)'#') )
958  {
959  if ((x == (TQChar)'\"') || (x == (TQChar)';') || (x == (TQChar)'<'))
960  badHostName = true;
961  if (++pos == len)
962  break;
963  x = buf[pos];
964  }
965  if (badHostName)
966  goto NodeErr;
967  if ( pos == len )
968  {
969  setHost(decode(TQString( buf + start, pos - start ), encoding_hint));
970  goto NodeOk;
971  }
972  setHost(decode(TQString( buf + start, pos - start ), encoding_hint));
973  }
974  x = buf[pos];
975  if ( x == (TQChar)'/' || x == (TQChar)'#' || x == (TQChar)'?' )
976  {
977  start = pos;
978  goto Node9;
979  }
980  else if ( x != (TQChar)':' )
981  goto NodeErr;
982  pos++;
983 
984  // Node 8c: Accept at least one digit
985  if ( pos == len )
986  goto NodeErr;
987  start = pos;
988  if ( !isdigit( buf[pos++] ) )
989  goto NodeErr;
990 
991  // Node 8d: Accept any amount of digits
992  while( pos < len && isdigit( buf[pos] ) ) pos++;
993  port = TQString( buf + start, pos - start );
994  m_iPort = port.toUShort();
995  if ( pos == len )
996  goto NodeOk;
997  start = pos;
998 
999  Node9: // parse path until query or reference reached
1000 
1001  while( pos < len && buf[pos] != (TQChar)'#' && buf[pos]!=(TQChar)'?' ) pos++;
1002 
1003  tmp = TQString( buf + start, pos - start );
1004  //kdDebug(126)<<" setting encoded path to:"<<tmp<<endl;
1005  setEncodedPath( tmp, encoding_hint );
1006 
1007  if ( pos == len )
1008  goto NodeOk;
1009 
1010  //Node10: // parse query or reference depending on what comes first
1011  delim = (buf[pos++]==(TQChar)'#'?(TQChar)'?':(TQChar)'#');
1012 
1013  start = pos;
1014 
1015  while(pos < len && buf[pos]!=delim ) pos++;
1016 
1017  tmp = TQString(buf + start, pos - start);
1018  if (delim==(TQChar)'#')
1019  _setQuery(tmp, encoding_hint);
1020  else
1021  m_strRef_encoded = tmp;
1022 
1023  if (pos == len)
1024  goto NodeOk;
1025 
1026  //Node11: // feed the rest into the remaining variable
1027  tmp = TQString( buf + pos + 1, len - pos - 1);
1028  if (delim == (TQChar)'#')
1029  m_strRef_encoded = tmp;
1030  else
1031  _setQuery(tmp, encoding_hint);
1032 
1033  NodeOk:
1034  //kdDebug(126)<<"parsing finished. m_strProtocol="<<m_strProtocol<<" m_strHost="<<m_strHost<<" m_strPath="<<m_strPath<<endl;
1035  m_bIsMalformed = false; // Valid URL
1036 
1037  //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;
1038  if (m_strProtocol.isEmpty())
1039  {
1040  m_iUriMode = URL;
1041  m_strProtocol = fileProt;
1042  }
1043  return;
1044 
1045  NodeErr:
1046 // kdDebug(126) << "KURL couldn't parse URL \"" << _url << "\"" << endl;
1047  reset();
1048  m_strProtocol = _url;
1049  m_iUriMode = Invalid;
1050 }
1051 
1052 KURL& KURL::operator=( const TQString& _url )
1053 {
1054  reset();
1055  parse( _url );
1056 
1057  return *this;
1058 }
1059 
1060 KURL& KURL::operator=( const char * _url )
1061 {
1062  reset();
1063  parse( TQString::fromLatin1(_url) );
1064 
1065  return *this;
1066 }
1067 
1068 #ifndef QT_NO_NETWORKPROTOCOL
1069 KURL& KURL::operator=( const TQUrl & u )
1070 {
1071  m_strProtocol = u.protocol();
1072  m_iUriMode = Auto;
1073  m_strUser = u.user();
1074  m_strPass = u.password();
1075  m_strHost = u.host();
1076  m_strPath = u.path( false );
1077  m_strPath_encoded = TQString::null;
1078  m_strQuery_encoded = u.query();
1079  m_strRef_encoded = u.ref();
1080  m_bIsMalformed = !u.isValid();
1081  m_iPort = u.port();
1082 
1083  return *this;
1084 }
1085 #endif
1086 
1087 KURL& KURL::operator=( const KURL& _u )
1088 {
1089  m_strProtocol = _u.m_strProtocol;
1090  m_strUser = _u.m_strUser;
1091  m_strPass = _u.m_strPass;
1092  m_strHost = _u.m_strHost;
1093  m_strPath = _u.m_strPath;
1094  m_strPath_encoded = _u.m_strPath_encoded;
1095  m_strQuery_encoded = _u.m_strQuery_encoded;
1096  m_strRef_encoded = _u.m_strRef_encoded;
1097  m_bIsMalformed = _u.m_bIsMalformed;
1098  m_iPort = _u.m_iPort;
1099  m_iUriMode = _u.m_iUriMode;
1100  d->m_strInternalReferenceURL = _u.d->m_strInternalReferenceURL;
1101 
1102  return *this;
1103 }
1104 
1105 bool KURL::operator<( const KURL& _u) const
1106 {
1107  int i;
1108  if (!_u.isValid())
1109  {
1110  if (!isValid())
1111  {
1112  i = m_strProtocol.compare(_u.m_strProtocol);
1113  return (i < 0);
1114  }
1115  return false;
1116  }
1117  if (!isValid())
1118  return true;
1119 
1120  i = m_strProtocol.compare(_u.m_strProtocol);
1121  if (i) return (i < 0);
1122 
1123  i = m_strHost.compare(_u.m_strHost);
1124  if (i) return (i < 0);
1125 
1126  if (m_iPort != _u.m_iPort) return (m_iPort < _u.m_iPort);
1127 
1128  i = m_strPath.compare(_u.m_strPath);
1129  if (i) return (i < 0);
1130 
1131  i = m_strQuery_encoded.compare(_u.m_strQuery_encoded);
1132  if (i) return (i < 0);
1133 
1134  i = m_strRef_encoded.compare(_u.m_strRef_encoded);
1135  if (i) return (i < 0);
1136 
1137  i = m_strUser.compare(_u.m_strUser);
1138  if (i) return (i < 0);
1139 
1140  i = m_strPass.compare(_u.m_strPass);
1141  if (i) return (i < 0);
1142 
1143  i = d->m_strInternalReferenceURL.compare(_u.d->m_strInternalReferenceURL);
1144  if (i) return (i < 0);
1145 
1146  return false;
1147 }
1148 
1149 bool KURL::operator==( const KURL& _u ) const
1150 {
1151  if ( !isValid() || !_u.isValid() )
1152  return false;
1153 
1154  if ( m_strProtocol == _u.m_strProtocol &&
1155  m_strUser == _u.m_strUser &&
1156  m_strPass == _u.m_strPass &&
1157  m_strHost == _u.m_strHost &&
1158  m_strPath == _u.m_strPath &&
1159  // The encoded path may be null, but the URLs are still equal (David)
1160  ( m_strPath_encoded.isNull() || _u.m_strPath_encoded.isNull() ||
1161  m_strPath_encoded == _u.m_strPath_encoded ) &&
1162  m_strQuery_encoded == _u.m_strQuery_encoded &&
1163  m_strRef_encoded == _u.m_strRef_encoded &&
1164  m_iPort == _u.m_iPort &&
1165  d->m_strInternalReferenceURL == _u.d->m_strInternalReferenceURL )
1166  {
1167  return true;
1168  }
1169 
1170  return false;
1171 }
1172 
1173 bool KURL::operator==( const TQString& _u ) const
1174 {
1175  KURL u( _u );
1176  return ( *this == u );
1177 }
1178 
1179 bool KURL::cmp( const KURL &u, bool ignore_trailing ) const
1180 {
1181  return equals( u, ignore_trailing );
1182 }
1183 
1184 bool KURL::equals( const KURL &_u, bool ignore_trailing ) const
1185 {
1186  if ( !isValid() || !_u.isValid() )
1187  return false;
1188 
1189  if ( ignore_trailing )
1190  {
1191  TQString path1 = path(1);
1192  TQString path2 = _u.path(1);
1193  if ( path1 != path2 )
1194  return false;
1195 
1196  if ( m_strProtocol == _u.m_strProtocol &&
1197  m_strUser == _u.m_strUser &&
1198  m_strPass == _u.m_strPass &&
1199  m_strHost == _u.m_strHost &&
1200  m_strQuery_encoded == _u.m_strQuery_encoded &&
1201  m_strRef_encoded == _u.m_strRef_encoded &&
1202  m_iPort == _u.m_iPort &&
1203  d->m_strInternalReferenceURL == _u.d->m_strInternalReferenceURL )
1204  return true;
1205 
1206  return false;
1207  }
1208 
1209  return ( *this == _u );
1210 }
1211 
1212 bool KURL::isParentOf( const KURL& _u ) const
1213 {
1214  if ( !isValid() || !_u.isValid() )
1215  return false;
1216 
1217  if ( m_strProtocol == _u.m_strProtocol &&
1218  m_strUser == _u.m_strUser &&
1219  m_strPass == _u.m_strPass &&
1220  m_strHost == _u.m_strHost &&
1221  m_strQuery_encoded == _u.m_strQuery_encoded &&
1222  m_strRef_encoded == _u.m_strRef_encoded &&
1223  m_iPort == _u.m_iPort )
1224  {
1225  if ( path().isEmpty() || _u.path().isEmpty() )
1226  return false; // can't work with implicit paths
1227 
1228  TQString p1( cleanpath( path(), true, false ) );
1229  if ( p1[p1.length()-1] != '/' )
1230  p1 += '/';
1231  TQString p2( cleanpath( _u.path(), true, false ) );
1232  if ( p2[p2.length()-1] != '/' )
1233  p2 += '/';
1234 
1235  //kdDebug(126) << "p1=" << p1 << endl;
1236  //kdDebug(126) << "p2=" << p2 << endl;
1237  //kdDebug(126) << "p1.length()=" << p1.length() << endl;
1238  //kdDebug(126) << "p2.left(!$)=" << p2.left( p1.length() ) << endl;
1239  return p2.startsWith( p1 );
1240  }
1241  return false;
1242 }
1243 
1244 void KURL::setFileName( const TQString& _txt )
1245 {
1246  m_strRef_encoded = TQString::null;
1247  int i = 0;
1248  while( _txt[i] == (TQChar)'/' ) ++i;
1249  TQString tmp;
1250  if ( i )
1251  tmp = _txt.mid( i );
1252  else
1253  tmp = _txt;
1254 
1255  TQString path = m_strPath_encoded.isEmpty() ? m_strPath : m_strPath_encoded;
1256  if ( path.isEmpty() )
1257  path = "/";
1258  else
1259  {
1260  int lastSlash = path.findRev( '/' );
1261  if ( lastSlash == -1)
1262  {
1263  // The first character is not a '/' ???
1264  // This looks strange ...
1265  path = "/";
1266  }
1267  else if ( path.right(1) != "/" )
1268  path.truncate( lastSlash+1 ); // keep the "/"
1269  }
1270  if (m_strPath_encoded.isEmpty())
1271  {
1272  path += tmp;
1273  setPath( path );
1274  }
1275  else
1276  {
1277  path += encode_string(tmp);
1278  setEncodedPath( path );
1279  }
1280  cleanPath();
1281 }
1282 
1283 void KURL::cleanPath( bool cleanDirSeparator ) // taken from the old KURL
1284 {
1285  if (m_iUriMode != URL) return;
1286  m_strPath = cleanpath(m_strPath, cleanDirSeparator, false);
1287  // WABA: Is this safe when "/../" is encoded with %?
1288  m_strPath_encoded = cleanpath(m_strPath_encoded, cleanDirSeparator, true);
1289 }
1290 
1291 static TQString trailingSlash( int _trailing, const TQString &path )
1292 {
1293  TQString result = path;
1294 
1295  if ( _trailing == 0 )
1296  return result;
1297  else if ( _trailing == 1 )
1298  {
1299  int len = result.length();
1300  if ( (len == 0) || (result[ len - 1 ] != (TQChar)'/') )
1301  result += "/";
1302  return result;
1303  }
1304  else if ( _trailing == -1 )
1305  {
1306  if ( result == "/" )
1307  return result;
1308  int len = result.length();
1309  while (len > 1 && result[ len - 1 ] == (TQChar)'/')
1310  {
1311  len--;
1312  }
1313  result.truncate( len );
1314  return result;
1315  }
1316  else {
1317  assert( 0 );
1318  return TQString::null;
1319  }
1320 }
1321 
1322 void KURL::adjustPath( int _trailing )
1323 {
1324  if (!m_strPath_encoded.isEmpty())
1325  {
1326  m_strPath_encoded = trailingSlash( _trailing, m_strPath_encoded );
1327  }
1328  m_strPath = trailingSlash( _trailing, m_strPath );
1329 }
1330 
1331 
1332 TQString KURL::encodedPathAndQuery( int _trailing, bool _no_empty_path, int encoding_hint ) const
1333 {
1334  TQString tmp;
1335  if (!m_strPath_encoded.isEmpty() && encoding_hint == 0)
1336  {
1337  tmp = trailingSlash( _trailing, m_strPath_encoded );
1338  }
1339  else
1340  {
1341  tmp = path( _trailing );
1342  if ( _no_empty_path && tmp.isEmpty() )
1343  tmp = "/";
1344  if (m_iUriMode == Mailto)
1345  {
1346  tmp = encode( tmp, 2, encoding_hint );
1347  }
1348  else
1349  {
1350  tmp = encode( tmp, 1, encoding_hint );
1351  }
1352  }
1353 
1354  // TODO apply encoding_hint to the query
1355  if (!m_strQuery_encoded.isNull())
1356  tmp += '?' + m_strQuery_encoded;
1357  return tmp;
1358 }
1359 
1360 void KURL::setEncodedPath( const TQString& _txt, int encoding_hint )
1361 {
1362  m_strPath_encoded = _txt;
1363 
1364  decode( m_strPath_encoded, m_strPath, m_strPath_encoded, encoding_hint );
1365  // Throw away encoding for local files, makes file-operations faster.
1366  if (m_strProtocol == fileProt)
1367  m_strPath_encoded = TQString::null;
1368 
1369  if ( m_iUriMode == Auto )
1370  m_iUriMode = URL;
1371 }
1372 
1373 
1374 void KURL::setEncodedPathAndQuery( const TQString& _txt, int encoding_hint )
1375 {
1376  int pos = _txt.find( '?' );
1377  if ( pos == -1 )
1378  {
1379  setEncodedPath(_txt, encoding_hint);
1380  m_strQuery_encoded = TQString::null;
1381  }
1382  else
1383  {
1384  setEncodedPath(_txt.left( pos ), encoding_hint);
1385  _setQuery(_txt.right(_txt.length() - pos - 1), encoding_hint);
1386  }
1387 }
1388 
1389 TQString KURL::path( int _trailing ) const
1390 {
1391  return trailingSlash( _trailing, path() );
1392 }
1393 
1394 bool KURL::isLocalFile() const
1395 {
1396  if ( (m_strProtocol != fileProt ) || hasSubURL() )
1397  return false;
1398 
1399  if (m_strHost.isEmpty() || (m_strHost == "localhost"))
1400  return true;
1401 
1402  char hostname[ 256 ];
1403  hostname[ 0 ] = '\0';
1404  if (!gethostname( hostname, 255 ))
1405  hostname[sizeof(hostname)-1] = '\0';
1406 
1407  for(char *p = hostname; *p; p++)
1408  *p = tolower(*p);
1409 
1410  return (m_strHost == hostname);
1411 }
1412 
1413 void KURL::setFileEncoding(const TQString &encoding)
1414 {
1415  if (!isLocalFile())
1416  return;
1417 
1418  TQString q = query();
1419 
1420  if (!q.isEmpty() && (q[0] == '?'))
1421  q = q.mid(1);
1422 
1423  TQStringList args = TQStringList::split('&', q);
1424  for(TQStringList::Iterator it = args.begin();
1425  it != args.end();)
1426  {
1427  TQString s = decode_string(*it);
1428  if (s.startsWith("charset="))
1429  it = args.erase(it);
1430  else
1431  ++it;
1432  }
1433  if (!encoding.isEmpty())
1434  args.append("charset="+encode_string(encoding));
1435 
1436  if (args.isEmpty())
1437  _setQuery(TQString::null);
1438  else
1439  _setQuery(args.join("&"));
1440 }
1441 
1442 TQString KURL::fileEncoding() const
1443 {
1444  if (!isLocalFile())
1445  return TQString::null;
1446 
1447  TQString q = query();
1448 
1449  if (q.isEmpty())
1450  return TQString::null;
1451 
1452  if (q[0] == '?')
1453  q = q.mid(1);
1454 
1455  TQStringList args = TQStringList::split('&', q);
1456  for(TQStringList::ConstIterator it = args.begin();
1457  it != args.end();
1458  ++it)
1459  {
1460  TQString s = decode_string(*it);
1461  if (s.startsWith("charset="))
1462  return s.mid(8);
1463  }
1464  return TQString::null;
1465 }
1466 
1467 bool KURL::hasSubURL() const
1468 {
1469  if ( m_strProtocol.isEmpty() || m_bIsMalformed )
1470  return false;
1471  if (m_strRef_encoded.isEmpty())
1472  return false;
1473  if (m_strRef_encoded.startsWith("gzip:"))
1474  return true;
1475  if (m_strRef_encoded.startsWith("bzip:"))
1476  return true;
1477  if (m_strRef_encoded.startsWith("bzip2:"))
1478  return true;
1479  if (m_strRef_encoded.startsWith("tar:"))
1480  return true;
1481  if (m_strRef_encoded.startsWith("ar:"))
1482  return true;
1483  if (m_strRef_encoded.startsWith("zip:"))
1484  return true;
1485  if (m_strRef_encoded.startsWith("lzma:"))
1486  return true;
1487  if (m_strRef_encoded.startsWith("xz:"))
1488  return true;
1489  if ( m_strProtocol == "error" ) // anything that starts with error: has suburls
1490  return true;
1491  return false;
1492 }
1493 
1494 TQString KURL::url( int _trailing, int encoding_hint ) const
1495 {
1496  if( m_bIsMalformed )
1497  {
1498  // Return the whole url even when the url is
1499  // malformed. Under such conditions the url
1500  // is stored in m_strProtocol.
1501  return m_strProtocol;
1502  }
1503 
1504  TQString u = m_strProtocol;
1505  if (!u.isEmpty())
1506  u += ":";
1507 
1508  if ( hasHost() || (m_strProtocol == fileProt) )
1509  {
1510  u += "//";
1511  if ( hasUser() )
1512  {
1513  u += encode(m_strUser, 0, encoding_hint);
1514  if ( hasPass() )
1515  {
1516  u += ":";
1517  u += encode(m_strPass, 0, encoding_hint);
1518  }
1519  u += "@";
1520  }
1521  if ( m_iUriMode == URL )
1522  {
1523  bool IPv6 = (m_strHost.find(':') != -1);
1524  if (IPv6)
1525  u += '[' + m_strHost + ']';
1526  else
1527  u += encodeHost(m_strHost, true, encoding_hint);
1528  if ( m_iPort != 0 ) {
1529  TQString buffer;
1530  buffer.sprintf( ":%u", m_iPort );
1531  u += buffer;
1532  }
1533  }
1534  else
1535  {
1536  u += m_strHost;
1537  }
1538  }
1539 
1540  if ( m_iUriMode == URL || m_iUriMode == Mailto )
1541  u += encodedPathAndQuery( _trailing, false, encoding_hint );
1542  else
1543  u += encode( m_strPath, 21, encoding_hint, true );
1544 
1545  if ( hasRef() )
1546  {
1547  u += "#";
1548  u += m_strRef_encoded;
1549  }
1550 
1551  return u;
1552 }
1553 
1554 TQString KURL::prettyURL( int _trailing ) const
1555 {
1556  if( m_bIsMalformed )
1557  {
1558  // Return the whole url even when the url is
1559  // malformed. Under such conditions the url
1560  // is stored in m_strProtocol.
1561  return m_strProtocol;
1562  }
1563 
1564  TQString u = m_strProtocol;
1565  if (!u.isEmpty())
1566  u += ":";
1567 
1568  if ( hasHost() || (m_strProtocol == fileProt) )
1569  {
1570  u += "//";
1571  if ( hasUser() )
1572  {
1573  u += encode(m_strUser, 0, 0);
1574  // Don't show password!
1575  u += "@";
1576  }
1577  if ( m_iUriMode == URL )
1578  {
1579  bool IPv6 = (m_strHost.find(':') != -1);
1580  if (IPv6)
1581  {
1582  u += '[' + m_strHost + ']';
1583  }
1584  else
1585  {
1586  u += lazy_encode(m_strHost);
1587  }
1588  }
1589  else
1590  {
1591  u += lazy_encode(m_strHost);
1592  }
1593  if ( m_iPort != 0 ) {
1594  TQString buffer;
1595  buffer.sprintf( ":%u", m_iPort );
1596  u += buffer;
1597  }
1598  }
1599 
1600  if (m_iUriMode == Mailto)
1601  {
1602  u += lazy_encode( m_strPath, false );
1603  }
1604  else
1605  {
1606  u += trailingSlash( _trailing, lazy_encode( m_strPath ) );
1607  }
1608 
1609  if (!m_strQuery_encoded.isNull())
1610  u += '?' + m_strQuery_encoded;
1611 
1612  if ( hasRef() )
1613  {
1614  u += "#";
1615  u += m_strRef_encoded;
1616  }
1617 
1618  return u;
1619 }
1620 
1621 TQString KURL::prettyURL( int _trailing, AdjustementFlags _flags) const
1622 {
1623  TQString u = prettyURL(_trailing);
1624  if (_flags & StripFileProtocol && u.startsWith("file://")) {
1625  u.remove(0, 7);
1626 #ifdef Q_WS_WIN
1627  return TQDir::convertSeparators(u);
1628 #endif
1629  }
1630  return u;
1631 }
1632 
1633 TQString KURL::pathOrURL() const
1634 {
1635  if ( isLocalFile() && m_strRef_encoded.isNull() && m_strQuery_encoded.isNull() ) {
1636  return path();
1637  } else {
1638  return prettyURL();
1639  }
1640 }
1641 
1642 TQString KURL::htmlURL() const
1643 {
1644  return TQStyleSheet::escape(prettyURL());
1645 }
1646 
1647 KURL::List KURL::split( const KURL& _url )
1648 {
1649  TQString ref;
1650  KURL::List lst;
1651  KURL url = _url;
1652 
1653  while(true)
1654  {
1655  KURL u = url;
1656  u.m_strRef_encoded = TQString::null;
1657  lst.append(u);
1658  if (url.hasSubURL())
1659  {
1660  url = KURL(url.m_strRef_encoded);
1661  }
1662  else
1663  {
1664  ref = url.m_strRef_encoded;
1665  break;
1666  }
1667  }
1668 
1669  // Set HTML ref in all URLs.
1670  KURL::List::Iterator it;
1671  for( it = lst.begin() ; it != lst.end(); ++it )
1672  {
1673  (*it).m_strRef_encoded = ref;
1674  }
1675 
1676  return lst;
1677 }
1678 
1679 KURL::List KURL::split( const TQString& _url )
1680 {
1681  return split(KURL(_url));
1682 }
1683 
1684 KURL KURL::join( const KURL::List & lst )
1685 {
1686  if (lst.isEmpty()) return KURL();
1687  KURL tmp;
1688 
1689  KURL::List::ConstIterator first = lst.fromLast();
1690  for( KURL::List::ConstIterator it = first; it != lst.end(); --it )
1691  {
1692  KURL u(*it);
1693  if (it != first)
1694  {
1695  if (!u.m_strRef_encoded) u.m_strRef_encoded = tmp.url();
1696  else u.m_strRef_encoded += "#" + tmp.url(); // Support more than one suburl thingy
1697  }
1698  tmp = u;
1699  }
1700 
1701  return tmp;
1702 }
1703 
1704 TQString KURL::fileName( bool _strip_trailing_slash ) const
1705 {
1706  TQString fname;
1707  if (hasSubURL()) { // If we have a suburl, then return the filename from there
1708  KURL::List list = KURL::split(*this);
1709  KURL::List::Iterator it = list.fromLast();
1710  return (*it).fileName(_strip_trailing_slash);
1711  }
1712  const TQString &path = m_strPath;
1713 
1714  int len = path.length();
1715  if ( len == 0 )
1716  return fname;
1717 
1718  if ( _strip_trailing_slash )
1719  {
1720  while ( len >= 1 && path[ len - 1 ] == TQChar('/') )
1721  len--;
1722  }
1723  else if ( path[ len - 1 ] == TQChar('/') )
1724  return fname;
1725 
1726  // Does the path only consist of '/' characters ?
1727  if ( len == 1 && path[ 0 ] == TQChar('/') )
1728  return fname;
1729 
1730  // Skip last n slashes
1731  int n = 1;
1732  if (!m_strPath_encoded.isEmpty())
1733  {
1734  // This is hairy, we need the last unencoded slash.
1735  // Count in the encoded string how many encoded slashes follow the last
1736  // unencoded one.
1737  int i = m_strPath_encoded.findRev( TQChar('/'), len - 1 );
1738  TQString fileName_encoded = m_strPath_encoded.mid(i+1);
1739  n += fileName_encoded.contains("%2f", false);
1740  }
1741  int i = len;
1742  do {
1743  i = path.findRev( TQChar('/'), i - 1 );
1744  }
1745  while (--n && (i > 0));
1746 
1747  // If ( i == -1 ) => the first character is not a '/'
1748  // So it's some URL like file:blah.tgz, return the whole path
1749  if ( i == -1 ) {
1750  if ( len == (int)path.length() )
1751  fname = path;
1752  else
1753  // Might get here if _strip_trailing_slash is true
1754  fname = path.left( len );
1755  }
1756  else
1757  {
1758  fname = path.mid( i + 1, len - i - 1 ); // TO CHECK
1759  }
1760  return fname;
1761 }
1762 
1763 void KURL::addPath( const TQString& _txt )
1764 {
1765  if (hasSubURL())
1766  {
1767  KURL::List lst = split( *this );
1768  KURL &u = lst.last();
1769  u.addPath(_txt);
1770  *this = join( lst );
1771  return;
1772  }
1773 
1774  m_strPath_encoded = TQString::null;
1775 
1776  if ( _txt.isEmpty() )
1777  return;
1778 
1779  int i = 0;
1780  int len = m_strPath.length();
1781  // Add the trailing '/' if it is missing
1782  if ( _txt[0] != (TQChar)'/' && ( len == 0 || m_strPath[ len - 1 ] != (TQChar)'/' ) )
1783  m_strPath += "/";
1784 
1785  // No double '/' characters
1786  i = 0;
1787  if ( len != 0 && m_strPath[ len - 1 ] == (TQChar)'/' )
1788  {
1789  while( _txt[i] == (TQChar)'/' )
1790  ++i;
1791  }
1792 
1793  m_strPath += _txt.mid( i );
1794 }
1795 
1796 TQString KURL::directory( bool _strip_trailing_slash_from_result,
1797  bool _ignore_trailing_slash_in_path ) const
1798 {
1799  TQString result = m_strPath_encoded.isEmpty() ? m_strPath : m_strPath_encoded;
1800  if ( _ignore_trailing_slash_in_path )
1801  result = trailingSlash( -1, result );
1802 
1803  if ( result.isEmpty() || result == "/" )
1804  return result;
1805 
1806  int i = result.findRev( "/" );
1807  // If ( i == -1 ) => the first character is not a '/'
1808  // So it's some URL like file:blah.tgz, with no path
1809  if ( i == -1 )
1810  return TQString::null;
1811 
1812  if ( i == 0 )
1813  {
1814  result = "/";
1815  return result;
1816  }
1817 
1818  if ( _strip_trailing_slash_from_result )
1819  result = result.left( i );
1820  else
1821  result = result.left( i + 1 );
1822 
1823  if (!m_strPath_encoded.isEmpty())
1824  result = decode(result);
1825 
1826  return result;
1827 }
1828 
1829 
1830 bool KURL::cd( const TQString& _dir )
1831 {
1832  if ( _dir.isEmpty() || m_bIsMalformed )
1833  return false;
1834 
1835  if (hasSubURL())
1836  {
1837  KURL::List lst = split( *this );
1838  KURL &u = lst.last();
1839  u.cd(_dir);
1840  *this = join( lst );
1841  return true;
1842  }
1843 
1844  // absolute path ?
1845  if ( _dir[0] == (TQChar)'/' )
1846  {
1847  m_strPath_encoded = TQString::null;
1848  m_strPath = _dir;
1849  setHTMLRef( TQString::null );
1850  m_strQuery_encoded = TQString::null;
1851  return true;
1852  }
1853 
1854  // Users home directory on the local disk ?
1855  if ( ( _dir[0] == (TQChar)'~' ) && ( m_strProtocol == fileProt ))
1856  {
1857  m_strPath_encoded = TQString::null;
1858  m_strPath = TQDir::homeDirPath();
1859  m_strPath += "/";
1860  m_strPath += _dir.right(m_strPath.length() - 1);
1861  setHTMLRef( TQString::null );
1862  m_strQuery_encoded = TQString::null;
1863  return true;
1864  }
1865 
1866  // relative path
1867  // we always work on the past of the first url.
1868  // Sub URLs are not touched.
1869 
1870  // append '/' if necessary
1871  TQString p = path(1);
1872  p += _dir;
1873  p = cleanpath( p, true, false );
1874  setPath( p );
1875 
1876  setHTMLRef( TQString::null );
1877  m_strQuery_encoded = TQString::null;
1878 
1879  return true;
1880 }
1881 
1882 KURL KURL::upURL( ) const
1883 {
1884  if (!query().isEmpty())
1885  {
1886  KURL u(*this);
1887  u._setQuery(TQString::null);
1888  return u;
1889  };
1890 
1891  if (!hasSubURL())
1892  {
1893  KURL u(*this);
1894 
1895  u.cd("../");
1896 
1897  return u;
1898  }
1899 
1900  // We have a subURL.
1901  KURL::List lst = split( *this );
1902  if (lst.isEmpty())
1903  return KURL(); // Huh?
1904  while (true)
1905  {
1906  KURL &u = lst.last();
1907  TQString old = u.path();
1908  u.cd("../");
1909  if (u.path() != old)
1910  break; // Finshed.
1911  if (lst.count() == 1)
1912  break; // Finished.
1913  lst.remove(lst.fromLast());
1914  }
1915  return join( lst );
1916 }
1917 
1918 TQString KURL::htmlRef() const
1919 {
1920  if ( !hasSubURL() )
1921  {
1922  return decode( ref() );
1923  }
1924 
1925  List lst = split( *this );
1926  return decode( (*lst.begin()).ref() );
1927 }
1928 
1929 TQString KURL::encodedHtmlRef() const
1930 {
1931  if ( !hasSubURL() )
1932  {
1933  return ref();
1934  }
1935 
1936  List lst = split( *this );
1937  return (*lst.begin()).ref();
1938 }
1939 
1940 void KURL::setHTMLRef( const TQString& _ref )
1941 {
1942  if ( !hasSubURL() )
1943  {
1944  m_strRef_encoded = encode( _ref, 0, 0 /*?*/);
1945  return;
1946  }
1947 
1948  List lst = split( *this );
1949 
1950  (*lst.begin()).setRef( encode( _ref, 0, 0 /*?*/) );
1951 
1952  *this = join( lst );
1953 }
1954 
1955 bool KURL::hasHTMLRef() const
1956 {
1957  if ( !hasSubURL() )
1958  {
1959  return hasRef();
1960  }
1961 
1962  List lst = split( *this );
1963  return (*lst.begin()).hasRef();
1964 }
1965 
1966 void
1967 KURL::setProtocol( const TQString& _txt )
1968 {
1969  m_strProtocol = _txt;
1970  if ( m_iUriMode == Auto ) m_iUriMode = uriModeForProtocol( m_strProtocol );
1971  m_bIsMalformed = false;
1972 }
1973 
1974 void
1975 KURL::setUser( const TQString& _txt )
1976 {
1977  if ( _txt.isEmpty() )
1978  m_strUser = TQString::null;
1979  else
1980  m_strUser = _txt;
1981 }
1982 
1983 void
1984 KURL::setPass( const TQString& _txt )
1985 {
1986  if ( _txt.isEmpty() )
1987  m_strPass = TQString::null;
1988  else
1989  m_strPass = _txt;
1990 }
1991 
1992 void
1993 KURL::setHost( const TQString& _txt )
1994 {
1995  if ( m_iUriMode == Auto )
1996  m_iUriMode = URL;
1997  switch ( m_iUriMode )
1998  {
1999  case URL:
2000 #ifndef KDE_QT_ONLY
2001  m_strHost = KIDNA::toUnicode(_txt);
2002  if (m_strHost.isEmpty())
2003  m_strHost = _txt.lower(); // Probably an invalid hostname, but...
2004 #else
2005  m_strHost = _txt.lower();
2006 #endif
2007  break;
2008  default:
2009  m_strHost = _txt;
2010  break;
2011  }
2012 }
2013 
2014 void
2015 KURL::setPort( unsigned short int _p )
2016 {
2017  m_iPort = _p;
2018 }
2019 
2020 void KURL::setPath( const TQString & path )
2021 {
2022  if (isEmpty())
2023  m_bIsMalformed = false;
2024  if (m_strProtocol.isEmpty())
2025  {
2026  m_strProtocol = fileProt;
2027  }
2028  m_strPath = path;
2029  m_strPath_encoded = TQString::null;
2030  if ( m_iUriMode == Auto )
2031  m_iUriMode = URL;
2032 }
2033 
2034 void KURL::setDirectory( const TQString &dir)
2035 {
2036  if ( dir.endsWith("/"))
2037  setPath(dir);
2038  else
2039  setPath(dir+"/");
2040 }
2041 
2042 void KURL::setQuery( const TQString &_txt, int encoding_hint)
2043 {
2044  if (_txt[0] == (TQChar)'?')
2045  _setQuery( _txt.length() > 1 ? _txt.mid(1) : "" /*empty, not null*/, encoding_hint );
2046  else
2047  _setQuery( _txt, encoding_hint );
2048 }
2049 
2050 // This is a private function that expects a query without '?'
2051 void KURL::_setQuery( const TQString &_txt, int encoding_hint)
2052 {
2053  m_strQuery_encoded = _txt;
2054  if (!_txt.length())
2055  return;
2056 
2057  int l = m_strQuery_encoded.length();
2058  int i = 0;
2059  TQString result;
2060  while (i < l)
2061  {
2062  int s = i;
2063  // Re-encode. Break encoded string up according to the reserved
2064  // characters '&:;=/?' and re-encode part by part.
2065  while(i < l)
2066  {
2067  char c = m_strQuery_encoded[i].latin1();
2068  if ((c == '&') || (c == ':') || (c == ';') ||
2069  (c == '=') || (c == '/') || (c == '?'))
2070  break;
2071  i++;
2072  }
2073  if (i > s)
2074  {
2075  TQString tmp = m_strQuery_encoded.mid(s, i-s);
2076  TQString newTmp;
2077  decode( tmp, newTmp, tmp, encoding_hint, false );
2078  result += tmp;
2079  }
2080  if (i < l)
2081  {
2082  result += m_strQuery_encoded[i];
2083  i++;
2084  }
2085  }
2086  m_strQuery_encoded = result;
2087 }
2088 
2089 TQString KURL::query() const
2090 {
2091  if (m_strQuery_encoded.isNull())
2092  return TQString::null;
2093  return '?'+m_strQuery_encoded;
2094 }
2095 
2096 TQString KURL::decode_string(const TQString &str, int encoding_hint)
2097 {
2098  return decode(str, encoding_hint);
2099 }
2100 
2101 TQString KURL::encode_string(const TQString &str, int encoding_hint)
2102 {
2103  return encode(str, 1, encoding_hint);
2104 }
2105 
2106 TQString KURL::encode_string_no_slash(const TQString &str, int encoding_hint)
2107 {
2108  return encode(str, 0, encoding_hint);
2109 }
2110 
2111 bool urlcmp( const TQString& _url1, const TQString& _url2 )
2112 {
2113  // Both empty ?
2114  if ( _url1.isEmpty() && _url2.isEmpty() )
2115  return true;
2116  // Only one empty ?
2117  if ( _url1.isEmpty() || _url2.isEmpty() )
2118  return false;
2119 
2120  KURL::List list1 = KURL::split( _url1 );
2121  KURL::List list2 = KURL::split( _url2 );
2122 
2123  // Malformed ?
2124  if ( list1.isEmpty() || list2.isEmpty() )
2125  return false;
2126 
2127  return ( list1 == list2 );
2128 }
2129 
2130 bool urlcmp( const TQString& _url1, const TQString& _url2, bool _ignore_trailing, bool _ignore_ref )
2131 {
2132  // Both empty ?
2133  if ( _url1.isEmpty() && _url2.isEmpty() )
2134  return true;
2135  // Only one empty ?
2136  if ( _url1.isEmpty() || _url2.isEmpty() )
2137  return false;
2138 
2139  KURL::List list1 = KURL::split( _url1 );
2140  KURL::List list2 = KURL::split( _url2 );
2141 
2142  // Malformed ?
2143  if ( list1.isEmpty() || list2.isEmpty() )
2144  return false;
2145 
2146  unsigned int size = list1.count();
2147  if ( list2.count() != size )
2148  return false;
2149 
2150  if ( _ignore_ref )
2151  {
2152  (*list1.begin()).setRef(TQString::null);
2153  (*list2.begin()).setRef(TQString::null);
2154  }
2155 
2156  KURL::List::Iterator it1 = list1.begin();
2157  KURL::List::Iterator it2 = list2.begin();
2158  for( ; it1 != list1.end() ; ++it1, ++it2 )
2159  if ( !(*it1).equals( *it2, _ignore_trailing ) )
2160  return false;
2161 
2162  return true;
2163 }
2164 
2165 TQMap< TQString, TQString > KURL::queryItems( int options ) const {
2166  return queryItems(options, 0);
2167 }
2168 
2169 TQMap< TQString, TQString > KURL::queryItems( int options, int encoding_hint ) const {
2170  if ( m_strQuery_encoded.isEmpty() )
2171  return TQMap<TQString,TQString>();
2172 
2173  TQMap< TQString, TQString > result;
2174  TQStringList items = TQStringList::split( '&', m_strQuery_encoded );
2175  for ( TQStringList::const_iterator it = items.begin() ; it != items.end() ; ++it ) {
2176  int equal_pos = (*it).find( '=' );
2177  if ( equal_pos > 0 ) { // = is not the first char...
2178  TQString name = (*it).left( equal_pos );
2179  if ( options & CaseInsensitiveKeys )
2180  name = name.lower();
2181  TQString value = (*it).mid( equal_pos + 1 );
2182  if ( value.isEmpty() )
2183  result.insert( name, TQString::fromLatin1("") );
2184  else {
2185  // ### why is decoding name not necessary?
2186  value.replace( '+', ' ' ); // + in queries means space
2187  result.insert( name, decode_string( value, encoding_hint ) );
2188  }
2189  } else if ( equal_pos < 0 ) { // no =
2190  TQString name = (*it);
2191  if ( options & CaseInsensitiveKeys )
2192  name = name.lower();
2193  result.insert( name, TQString::null );
2194  }
2195  }
2196 
2197  return result;
2198 }
2199 
2200 TQString KURL::queryItem( const TQString& _item ) const
2201 {
2202  return queryItem( _item, 0 );
2203 }
2204 
2205 TQString KURL::queryItem( const TQString& _item, int encoding_hint ) const
2206 {
2207  TQString item = _item + '=';
2208  if ( m_strQuery_encoded.length() <= 1 )
2209  return TQString::null;
2210 
2211  TQStringList items = TQStringList::split( '&', m_strQuery_encoded );
2212  unsigned int _len = item.length();
2213  for ( TQStringList::ConstIterator it = items.begin(); it != items.end(); ++it )
2214  {
2215  if ( (*it).startsWith( item ) )
2216  {
2217  if ( (*it).length() > _len )
2218  {
2219  TQString str = (*it).mid( _len );
2220  str.replace( '+', ' ' ); // + in queries means space.
2221  return decode_string( str, encoding_hint );
2222  }
2223  else // empty value
2224  return TQString::fromLatin1("");
2225  }
2226  }
2227 
2228  return TQString::null;
2229 }
2230 
2231 void KURL::removeQueryItem( const TQString& _item )
2232 {
2233  TQString item = _item + '=';
2234  if ( m_strQuery_encoded.length() <= 1 )
2235  return;
2236 
2237  TQStringList items = TQStringList::split( '&', m_strQuery_encoded );
2238  for ( TQStringList::Iterator it = items.begin(); it != items.end(); )
2239  {
2240  if ( (*it).startsWith( item ) || (*it == _item) )
2241  {
2242  TQStringList::Iterator deleteIt = it;
2243  ++it;
2244  items.remove(deleteIt);
2245  }
2246  else
2247  {
2248  ++it;
2249  }
2250  }
2251  m_strQuery_encoded = items.join( "&" );
2252 }
2253 
2254 void KURL::addQueryItem( const TQString& _item, const TQString& _value, int encoding_hint )
2255 {
2256  TQString item = _item + '=';
2257  TQString value = encode( _value, 0, encoding_hint );
2258 
2259  if (!m_strQuery_encoded.isEmpty())
2260  m_strQuery_encoded += '&';
2261  m_strQuery_encoded += item + value;
2262 }
2263 
2264 // static
2265 KURL KURL::fromPathOrURL( const TQString& text )
2266 {
2267  if ( text.isEmpty() )
2268  return KURL();
2269 
2270  KURL url;
2271  if (!TQDir::isRelativePath(text))
2272  url.setPath( text );
2273  else
2274  url = text;
2275 
2276  return url;
2277 }
2278 
2279 static TQString _relativePath(const TQString &base_dir, const TQString &path, bool &isParent)
2280 {
2281  TQString _base_dir(TQDir::cleanDirPath(base_dir));
2282  TQString _path(TQDir::cleanDirPath(path.isEmpty() || (path[0] != (TQChar)'/') ? _base_dir+"/"+path : path));
2283 
2284  if (_base_dir.isEmpty())
2285  return _path;
2286 
2287  if (_base_dir[_base_dir.length()-1] != '/')
2288  _base_dir.append('/');
2289 
2290  TQStringList list1 = TQStringList::split('/', _base_dir);
2291  TQStringList list2 = TQStringList::split('/', _path);
2292 
2293  // Find where they meet
2294  uint level = 0;
2295  uint maxLevel = TQMIN(list1.count(), list2.count());
2296  while((level < maxLevel) && (list1[level] == list2[level])) level++;
2297 
2298  TQString result;
2299  // Need to go down out of the first path to the common branch.
2300  for(uint i = level; i < list1.count(); i++)
2301  result.append("../");
2302 
2303  // Now up up from the common branch to the second path.
2304  for(uint i = level; i < list2.count(); i++)
2305  result.append(list2[i]).append("/");
2306 
2307  if ((level < list2.count()) && (path[path.length()-1] != (TQChar)'/'))
2308  result.truncate(result.length()-1);
2309 
2310  isParent = (level == list1.count());
2311 
2312  return result;
2313 }
2314 
2315 TQString KURL::relativePath(const TQString &base_dir, const TQString &path, bool *isParent)
2316 {
2317  bool parent = false;
2318  TQString result = _relativePath(base_dir, path, parent);
2319  if (parent)
2320  result.prepend("./");
2321 
2322  if (isParent)
2323  *isParent = parent;
2324 
2325  return result;
2326 }
2327 
2328 void KURL::setInternalReferenceURL( const TQString& url ) {
2329  d->m_strInternalReferenceURL = url;
2330 }
2331 
2332 TQString KURL::internalReferenceURL( void ) const {
2333  return d->m_strInternalReferenceURL;
2334 }
2335 
2336 TQString KURL::relativeURL(const KURL &base_url, const KURL &url, int encoding_hint)
2337 {
2338  if ((url.protocol() != base_url.protocol()) ||
2339  (url.host() != base_url.host()) ||
2340  (url.port() && url.port() != base_url.port()) ||
2341  (url.hasUser() && url.user() != base_url.user()) ||
2342  (url.hasPass() && url.pass() != base_url.pass()))
2343  {
2344  return url.url(0, encoding_hint);
2345  }
2346 
2347  TQString relURL;
2348 
2349  if ((base_url.path() != url.path()) || (base_url.query() != url.query()))
2350  {
2351  bool dummy;
2352  TQString basePath = base_url.directory(false, false);
2353  relURL = encode( _relativePath(basePath, url.path(), dummy), 1, encoding_hint);
2354  relURL += url.query();
2355  }
2356 
2357  if ( url.hasRef() )
2358  {
2359  relURL += "#";
2360  relURL += url.ref();
2361  }
2362 
2363  if ( relURL.isEmpty() )
2364  return "./";
2365 
2366  return relURL;
2367 }
2368 
2369 int KURL::uriMode() const
2370 {
2371  return m_iUriMode;
2372 }
2373 
2374 KURL::URIMode KURL::uriModeForProtocol(const TQString& protocol)
2375 {
2376 #ifndef KDE_QT_ONLY
2377  KURL::URIMode mode = Auto;
2378  if (protocol == fileProt)
2379  return URL;
2380  if (TDEGlobal::_instance)
2381  mode = KProtocolInfo::uriParseMode(protocol);
2382  if (mode == Auto ) {
2383 #else
2384  KURL::URIMode mode = Auto;
2385 #endif
2386  if ( protocol == "ed2k" || protocol == "sig2dat" || protocol == "slsk" || protocol == "data" ) mode = RawURI;
2387  else if ( protocol == "mailto" ) mode = Mailto;
2388  else mode = URL;
2389 #ifndef KDE_QT_ONLY
2390  }
2391 #endif
2392  return mode;
2393 }

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • knewstuff
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.