kurl.cpp
来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C++ 代码 · 共 2,357 行 · 第 1/4 页
CPP
2,357 行
} 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 QString p1( cleanpath( path(), true, false ) ); if ( p1[p1.length()-1] != '/' ) p1 += '/'; QString 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 QString& _txt ){ m_strRef_encoded = QString::null; int i = 0; while( _txt[i] == '/' ) ++i; QString tmp; if ( i ) tmp = _txt.mid( i ); else tmp = _txt; QString 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 QString trailingSlash( int _trailing, const QString &path ){ QString result = path; if ( _trailing == 0 ) return result; else if ( _trailing == 1 ) { int len = result.length(); if ( (len == 0) || (result[ len - 1 ] != '/') ) result += "/"; return result; } else if ( _trailing == -1 ) { if ( result == "/" ) return result; int len = result.length(); while (len > 1 && result[ len - 1 ] == '/') { len--; } result.truncate( len ); return result; } else { assert( 0 ); return QString::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 );}QString KURL::encodedPathAndQuery( int _trailing, bool _no_empty_path, int encoding_hint ) const{ QString 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 QString& _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 = QString::null; if ( m_iUriMode == Auto ) m_iUriMode = URL;}void KURL::setEncodedPathAndQuery( const QString& _txt, int encoding_hint ){ int pos = _txt.find( '?' ); if ( pos == -1 ) { setEncodedPath(_txt, encoding_hint); m_strQuery_encoded = QString::null; } else { setEncodedPath(_txt.left( pos ), encoding_hint); _setQuery(_txt.right(_txt.length() - pos - 1), encoding_hint); }}QString 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 QString &encoding){ if (!isLocalFile()) return; QString q = query(); if (!q.isEmpty() && (q[0] == '?')) q = q.mid(1); QStringList args = QStringList::split('&', q); for(QStringList::Iterator it = args.begin(); it != args.end();) { QString 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(QString::null); else _setQuery(args.join("&"));}QString KURL::fileEncoding() const{ if (!isLocalFile()) return QString::null; QString q = query(); if (q.isEmpty()) return QString::null; if (q[0] == '?') q = q.mid(1); QStringList args = QStringList::split('&', q); for(QStringList::ConstIterator it = args.begin(); it != args.end(); ++it) { QString s = decode_string(*it); if (s.startsWith("charset=")) return s.mid(8); } return QString::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;}QString 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; } QString 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 ) { QString 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;}QString 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; } QString u = m_strProtocol; if (!u.isEmpty()) u += ":"; if ( hasHost() || (m_strProtocol == fileProt) ) { u += "//"; if ( hasUser() ) { u += lazy_encode(m_strUser); // 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 ) { QString 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;}QString KURL::prettyURL( int _trailing, AdjustementFlags _flags) const{ QString u = prettyURL(_trailing); if (_flags & StripFileProtocol && u.startsWith("file://")) { u.remove(0, 7);#ifdef Q_WS_WIN return QDir::convertSeparators(u);#endif } return u;}QString KURL::pathOrURL() const{ if ( isLocalFile() && m_strRef_encoded.isNull() && m_strQuery_encoded.isNull() ) { return path(); } else { return prettyURL(); }}QString KURL::htmlURL() const{ return QStyleSheet::escape(prettyURL());}KURL::List KURL::split( const KURL& _url ){ QString ref; KURL::List lst; KURL url = _url; while(true) { KURL u = url; u.m_strRef_encoded = QString::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 QString& _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;}QString KURL::fileName( bool _strip_trailing_slash ) const{ QString 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 QString &path = m_strPath; int len = path.length(); if ( len == 0 ) return fname; if ( _strip_trailing_slash ) { while ( len >= 1 && path[ len - 1 ] == '/' ) len--; } else if ( path[ len - 1 ] == '/' ) return fname; // Does the path only consist of '/' characters ? if ( len == 1 && path[ 0 ] == '/' ) 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( '/', len - 1 ); QString fileName_encoded = m_strPath_encoded.mid(i+1); n += fileName_encoded.contains("%2f", false); } int i = len; do { i = path.findRev( '/', 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 QString& _txt ){ if (hasSubURL()) { KURL::List lst = split( *this ); KURL &u = lst.last(); u.addPath(_txt); *this = join( lst ); return; } m_strPath_encoded = QString::null; if ( _txt.isEmpty() ) return; int i = 0; int len = m_strPath.length(); // Add the trailing '/' if it is missing if ( _txt[0] != '/' && ( len == 0 || m_strPath[ len - 1 ] != '/' ) ) m_strPath += "/"; // No double '/' characters i = 0; if ( len != 0 && m_strPath[ len - 1 ] == '/' ) { while( _txt[i] == '/' ) ++i; } m_strPath += _txt.mid( i );}QString KURL::directory( bool _strip_trailing_slash_from_result, bool _ignore_trailing_slash_in_path ) const{ QString result = m_strPath_encoded.isEmpty() ? m_strPath : m_strPath_encoded; if ( _ignore_trailing_slash_in_path )
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?