📄 q3url.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "q3url.h"#ifndef QT_NO_URL#include "q3cstring.h"#include "qdir.h"// used by q3filedialog.cppbool qt_resolve_symlinks = true;class Q3UrlPrivate{public: QString protocol; QString user; QString pass; QString host; QString path, cleanPath; QString refEncoded; QString queryEncoded; bool isValid; int port; bool cleanPathDirty;};/*! Replaces backslashes with slashes and removes multiple occurrences of slashes or backslashes if \c allowMultiple is false.*/static void slashify( QString& s, bool allowMultiple = true ){ bool justHadSlash = false; for ( int i = 0; i < (int)s.length(); i++ ) { if ( !allowMultiple && justHadSlash && ( s[ i ] == QLatin1Char('/') || s[ i ] == QLatin1Char('\\') ) ) { s.remove( i, 1 ); --i; continue; } if ( s[ i ] == QLatin1Char('\\') ) s[ i ] = QLatin1Char('/');#if defined (Q_WS_MAC9) if ( s[ i ] == QLatin1Char(':') && (i == (int)s.length()-1 || s[ i + 1 ] != QLatin1Char('/') ) ) //mac colon's go away, unless after a protocol s[ i ] = QLatin1Char('/');#endif if ( s[ i ] == QLatin1Char('/') ) justHadSlash = true; else justHadSlash = false; }}/*! \class Q3Url q3url.h \brief The Q3Url class provides a URL parser and simplifies working with URLs. \compat The Q3Url class is provided for simple work with URLs. It can parse, decode, encode, etc. Q3Url works with the decoded path and encoded query in turn. Example: <tt>http://www.trolltech.com:80/cgi-bin/test%20me.pl?cmd=Hello%20you</tt> \table \header \i Function \i Returns \row \i \l protocol() \i "http" \row \i \l host() \i "www.trolltech.com" \row \i \l port() \i 80 \row \i \l path() \i "/cgi-bin/test me.pl" \row \i \l fileName() \i "test me.pl" \row \i \l query() \i "cmd=Hello%20you" \endtable Example: <tt>http://doc.trolltech.com/qdockarea.html#lines</tt> \table \header \i Function \i Returns \row \i \l protocol() \i "http" \row \i \l host() \i "doc.trolltech.com" \row \i \l fileName() \i "qdockarea.html" \row \i \l ref() \i "lines" \endtable The individual parts of a URL can be set with setProtocol(), setHost(), setPort(), setPath(), setFileName(), setRef() and setQuery(). A URL could contain, for example, an ftp address which requires a user name and password; these can be set with setUser() and setPassword(). Because path is always encoded internally you must not use "%00" in the path, although this is okay (but not recommended) for the query. Q3Url is normally used like this: \code Q3Url url( "http://www.trolltech.com" ); // or Q3Url url( "file:///home/myself/Mail", "Inbox" ); \endcode You can then access and manipulate the various parts of the URL. To make it easy to work with Q3Urls and QStrings, Q3Url implements the necessary cast and assignment operators so you can do following: \code Q3Url url( "http://www.trolltech.com" ); QString s = url; // or QString s( "http://www.trolltech.com" ); Q3Url url( s ); \endcode Use the static functions, encode() and decode() to encode or decode a URL in a string. (They operate on the string in-place.) The isRelativeUrl() static function returns true if the given string is a relative URL. If you want to use a URL to work on a hierarchical structure (e.g. a local or remote filesystem), you might want to use the subclass Q3UrlOperator. \sa Q3UrlOperator*//*! Constructs an empty URL that is invalid.*/Q3Url::Q3Url(){ d = new Q3UrlPrivate; d->isValid = false; d->port = -1; d->cleanPathDirty = true;}/*! Constructs a URL by parsing the string \a url. If you pass a string like "/home/qt", the "file" protocol is assumed.*/Q3Url::Q3Url( const QString& url ){ d = new Q3UrlPrivate; d->protocol = QLatin1String("file"); d->port = -1; parse( url );}/*! Copy constructor. Copies the data of \a url.*/Q3Url::Q3Url( const Q3Url& url ){ d = new Q3UrlPrivate; *d = *url.d;}/*! Returns true if \a url is relative; otherwise returns false.*/bool Q3Url::isRelativeUrl( const QString &url ){ int colon = url.find( QLatin1String(":") ); int slash = url.find( QLatin1String("/") ); return ( slash != 0 && ( colon == -1 || ( slash != -1 && colon > slash ) ) );}/*! Constructs an URL taking \a url as the base (context) and \a relUrl as a relative URL to \a url. If \a relUrl is not relative, \a relUrl is taken as the new URL. For example, the path of \code Q3Url url( "ftp://ftp.trolltech.com/qt/source", "qt-2.1.0.tar.gz" ); \endcode will be "/qt/srource/qt-2.1.0.tar.gz". On the other hand, \code Q3Url url( "ftp://ftp.trolltech.com/qt/source", "/usr/local" ); \endcode will result in a new URL, "ftp://ftp.trolltech.com/usr/local", because "/usr/local" isn't relative. Similarly, \code Q3Url url( "ftp://ftp.trolltech.com/qt/source", "file:///usr/local" ); \endcode will result in a new URL, with "/usr/local" as the path and "file" as the protocol. Normally it is expected that the path of \a url points to a directory, even if the path has no slash at the end. But if you want the constructor to handle the last part of the path as a file name if there is no slash at the end, and to let it be replaced by the file name of \a relUrl (if it contains one), set \a checkSlash to true.*/Q3Url::Q3Url( const Q3Url& url, const QString& relUrl, bool checkSlash ){ d = new Q3UrlPrivate; QString rel = relUrl; slashify( rel ); Q3Url urlTmp( url ); if ( !urlTmp.isValid() ) { urlTmp.reset(); } if ( isRelativeUrl( rel ) ) { if ( rel[ 0 ] == QLatin1Char('#') ) { *this = urlTmp; rel.remove( (uint)0, 1 ); decode( rel ); setRef( rel ); } else if ( rel[ 0 ] == QLatin1Char('?') ) { *this = urlTmp; rel.remove( (uint)0, 1 ); setQuery( rel ); } else { decode( rel ); *this = urlTmp; setRef( QString() ); if ( checkSlash && d->cleanPath[(int)path().length()-1] != QLatin1Char('/') ) { if ( isRelativeUrl( path() ) ) setEncodedPathAndQuery( rel ); else setFileName( rel ); } else { QString p = urlTmp.path(); if ( p.isEmpty() ) { // allow URLs like "file:foo" if ( !d->host.isEmpty() && !d->user.isEmpty() && !d->pass.isEmpty() ) p = QLatin1String("/"); } if ( !p.isEmpty() && p.right(1)!=QLatin1String("/") ) p += QLatin1String("/"); p += rel; d->path = p; d->cleanPathDirty = true; } } } else { if ( rel[ 0 ] == QChar( QLatin1Char('/') ) ) { *this = urlTmp; setEncodedPathAndQuery( rel ); } else { *this = rel; } }}/*! Destructor.*/Q3Url::~Q3Url(){ delete d; d = 0;}/*! Returns the protocol of the URL. Typically, "file", "http", "ftp", etc. \sa setProtocol()*/QString Q3Url::protocol() const{ return d->protocol;}/*! Sets the protocol of the URL to \a protocol. Typically, "file", "http", "ftp", etc. \sa protocol()*/void Q3Url::setProtocol( const QString& protocol ){ d->protocol = protocol; if ( hasHost() ) d->isValid = true;}/*! Returns the username of the URL. \sa setUser() setPassword()*/QString Q3Url::user() const{ return d->user;}/*! Sets the username of the URL to \a user. \sa user() setPassword()*/void Q3Url::setUser( const QString& user ){ d->user = user;}/*! Returns true if the URL contains a username; otherwise returns false. \sa setUser() setPassword()*/bool Q3Url::hasUser() const{ return !d->user.isEmpty();}/*! Returns the password of the URL. \warning Passwords passed in URLs are normally \e insecure; this is due to the mechanism, not because of Qt. \sa setPassword() setUser()*/QString Q3Url::password() const{ return d->pass;}/*! Sets the password of the URL to \a pass. \warning Passwords passed in URLs are normally \e insecure; this is due to the mechanism, not because of Qt. \sa password() setUser()*/void Q3Url::setPassword( const QString& pass ){ d->pass = pass;}/*! Returns true if the URL contains a password; otherwise returns false. \warning Passwords passed in URLs are normally \e insecure; this is due to the mechanism, not because of Qt. \sa setPassword() setUser()*/bool Q3Url::hasPassword() const{ return !d->pass.isEmpty();}/*! Returns the hostname of the URL. \sa setHost() hasHost()*/QString Q3Url::host() const{ return d->host;}/*! Sets the hostname of the URL to \a host. \sa host() hasHost()*/void Q3Url::setHost( const QString& host ){ d->host = host; if ( !d->protocol.isNull() && d->protocol != QLatin1String("file") ) d->isValid = true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -