kurl.cpp
来自「konqueror3 embedded版本, KDE环境下的当家浏览器的嵌入式版」· C++ 代码 · 共 2,357 行 · 第 1/4 页
CPP
2,357 行
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 QString::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 QString& _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] == '/' ) { m_strPath_encoded = QString::null; m_strPath = _dir; setHTMLRef( QString::null ); m_strQuery_encoded = QString::null; return true; } // Users home directory on the local disk ? if ( ( _dir[0] == '~' ) && ( m_strProtocol == fileProt )) { m_strPath_encoded = QString::null; m_strPath = QDir::homeDirPath(); m_strPath += "/"; m_strPath += _dir.right(m_strPath.length() - 1); setHTMLRef( QString::null ); m_strQuery_encoded = QString::null; return true; } // relative path // we always work on the past of the first url. // Sub URLs are not touched. // append '/' if necessary QString p = path(1); p += _dir; p = cleanpath( p, true, false ); setPath( p ); setHTMLRef( QString::null ); m_strQuery_encoded = QString::null; return true;}KURL KURL::upURL( ) const{ if (!query().isEmpty()) { KURL u(*this); u._setQuery(QString::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(); QString old = u.path(); u.cd("../"); if (u.path() != old) break; // Finshed. if (lst.count() == 1) break; // Finished. lst.remove(lst.fromLast()); } return join( lst );}QString KURL::htmlRef() const{ if ( !hasSubURL() ) { return decode( ref() ); } List lst = split( *this ); return decode( (*lst.begin()).ref() );}QString KURL::encodedHtmlRef() const{ if ( !hasSubURL() ) { return ref(); } List lst = split( *this ); return (*lst.begin()).ref();}void KURL::setHTMLRef( const QString& _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();}voidKURL::setProtocol( const QString& _txt ){ m_strProtocol = _txt; if ( m_iUriMode == Auto ) m_iUriMode = uriModeForProtocol( m_strProtocol ); m_bIsMalformed = false;}voidKURL::setUser( const QString& _txt ){ if ( _txt.isEmpty() ) m_strUser = QString::null; else m_strUser = _txt;}voidKURL::setPass( const QString& _txt ){ if ( _txt.isEmpty() ) m_strPass = QString::null; else m_strPass = _txt;}voidKURL::setHost( const QString& _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; }}voidKURL::setPort( unsigned short int _p ){ m_iPort = _p;}void KURL::setPath( const QString & path ){ if (isEmpty()) m_bIsMalformed = false; if (m_strProtocol.isEmpty()) { m_strProtocol = fileProt; } m_strPath = path; m_strPath_encoded = QString::null; if ( m_iUriMode == Auto ) m_iUriMode = URL;}void KURL::setDirectory( const QString &dir){ if ( dir.endsWith("/")) setPath(dir); else setPath(dir+"/");}void KURL::setQuery( const QString &_txt, int encoding_hint){ if (_txt[0] == '?') _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 QString &_txt, int encoding_hint){ m_strQuery_encoded = _txt; if (!_txt.length()) return; int l = m_strQuery_encoded.length(); int i = 0; QString 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) { QString tmp = m_strQuery_encoded.mid(s, i-s); QString newTmp; decode( tmp, newTmp, tmp, encoding_hint, false ); result += tmp; } if (i < l) { result += m_strQuery_encoded[i]; i++; } } m_strQuery_encoded = result;}QString KURL::query() const{ if (m_strQuery_encoded.isNull()) return QString::null; return '?'+m_strQuery_encoded;}QString KURL::decode_string(const QString &str, int encoding_hint){ return decode(str, encoding_hint);}QString KURL::encode_string(const QString &str, int encoding_hint){ return encode(str, 1, encoding_hint);}QString KURL::encode_string_no_slash(const QString &str, int encoding_hint){ return encode(str, 0, encoding_hint);}bool urlcmp( const QString& _url1, const QString& _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 QString& _url1, const QString& _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(QString::null); (*list2.begin()).setRef(QString::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;}QMap< QString, QString > KURL::queryItems( int options ) const { return queryItems(options, 0);}QMap< QString, QString > KURL::queryItems( int options, int encoding_hint ) const { if ( m_strQuery_encoded.isEmpty() ) return QMap<QString,QString>(); QMap< QString, QString > result; QStringList items = QStringList::split( '&', m_strQuery_encoded ); for ( QStringList::const_iterator it = items.begin() ; it != items.end() ; ++it ) { int equal_pos = (*it).find( '=' ); if ( equal_pos > 0 ) { // = is not the first char... QString name = (*it).left( equal_pos ); if ( options & CaseInsensitiveKeys ) name = name.lower(); QString value = (*it).mid( equal_pos + 1 ); if ( value.isEmpty() ) result.insert( name, QString::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 = QString name = (*it); if ( options & CaseInsensitiveKeys ) name = name.lower(); result.insert( name, QString::null ); } } return result;}QString KURL::queryItem( const QString& _item ) const{ return queryItem( _item, 0 );}QString KURL::queryItem( const QString& _item, int encoding_hint ) const{ QString item = _item + '='; if ( m_strQuery_encoded.length() <= 1 ) return QString::null; QStringList items = QStringList::split( '&', m_strQuery_encoded ); unsigned int _len = item.length(); for ( QStringList::ConstIterator it = items.begin(); it != items.end(); ++it ) { if ( (*it).startsWith( item ) ) { if ( (*it).length() > _len ) { QString str = (*it).mid( _len ); str.replace( '+', ' ' ); // + in queries means space. return decode_string( str, encoding_hint ); } else // empty value return QString::fromLatin1(""); } } return QString::null;}void KURL::removeQueryItem( const QString& _item ){ QString item = _item + '='; if ( m_strQuery_encoded.length() <= 1 ) return; QStringList items = QStringList::split( '&', m_strQuery_encoded ); for ( QStringList::Iterator it = items.begin(); it != items.end(); ) { if ( (*it).startsWith( item ) || (*it == _item) ) { QStringList::Iterator deleteIt = it; ++it; items.remove(deleteIt); } else { ++it; } } m_strQuery_encoded = items.join( "&" );}void KURL::addQueryItem( const QString& _item, const QString& _value, int encoding_hint ){ QString item = _item + '='; QString value = encode( _value, 0, encoding_hint ); if (!m_strQuery_encoded.isEmpty()) m_strQuery_encoded += '&'; m_strQuery_encoded += item + value;}// staticKURL KURL::fromPathOrURL( const QString& text ){ if ( text.isEmpty() ) return KURL(); KURL url; if (!QDir::isRelativePath(text)) url.setPath( text ); else url = text; return url;}static QString _relativePath(const QString &base_dir, const QString &path, bool &isParent){ QString _base_dir(QDir::cleanDirPath(base_dir)); QString _path(QDir::cleanDirPath(path.isEmpty() || (path[0] != '/') ? _base_dir+"/"+path : path)); if (_base_dir.isEmpty()) return _path; if (_base_dir[_base_dir.length()-1] != '/') _base_dir.append('/'); QStringList list1 = QStringList::split('/', _base_dir); QStringList list2 = QStringList::split('/', _path); // Find where they meet uint level = 0; uint maxLevel = QMIN(list1.count(), list2.count()); while((level < maxLevel) && (list1[level] == list2[level])) level++; QString 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] != '/')) result.truncate(result.length()-1); isParent = (level == list1.count()); return result;}QString KURL::relativePath(const QString &base_dir, const QString &path, bool *isParent){ bool parent = false; QString result = _relativePath(base_dir, path, parent); if (parent) result.prepend("./"); if (isParent) *isParent = parent; return result;}QString 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); } QString relURL; if ((base_url.path() != url.path()) || (base_url.query() != url.query())) { bool dummy; QString 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 QString& 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;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?