diff options
author | Jocelyn Turcotte <jocelyn.turcotte@nokia.com> | 2009-09-29 16:00:40 (GMT) |
---|---|---|
committer | Jocelyn Turcotte <jocelyn.turcotte@nokia.com> | 2009-09-29 16:03:14 (GMT) |
commit | dbe294cdf383d8c8c0da21730be155c5291541d8 (patch) | |
tree | 370f8ddde7e592dc34f5d84238579cedcadc0d8f /src/corelib | |
parent | c97b576a184a6ad57bf6bf043fa8b4b0be046730 (diff) | |
download | Qt-dbe294cdf383d8c8c0da21730be155c5291541d8.zip Qt-dbe294cdf383d8c8c0da21730be155c5291541d8.tar.gz Qt-dbe294cdf383d8c8c0da21730be155c5291541d8.tar.bz2 |
Adds QUrl::fromUserInput, gathered from QWebView::guessUrlFromString.
Reviewed-by: Thiago Macieira
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/io/qurl.cpp | 75 | ||||
-rw-r--r-- | src/corelib/io/qurl.h | 2 |
2 files changed, 77 insertions, 0 deletions
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index face923..c9a4cf1 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -172,6 +172,8 @@ #include "private/qunicodetables_p.h" #include "qatomic.h" #include "qbytearray.h" +#include "qdir.h" +#include "qfile.h" #include "qlist.h" #ifndef QT_NO_REGEXP #include "qregexp.h" @@ -5547,6 +5549,79 @@ QUrl QUrl::fromEncoded(const QByteArray &input, ParsingMode parsingMode) } /*! + Returns a valid URL from a user supplied \a userInput string if one can be + deducted. In the case that is not possible, an invalid QUrl() is returned. + + \since 4.6 + + Most applications that can browse the web, allow the user to input a URL + in the form of a plain string. This string can be manually typed into + a location bar, obtained from the clipboard, or passed in via command + line arguments. + + When the string is not already a valid URL, a best guess is performed, + making various web related assumptions. + + In the case the string corresponds to a valid file path on the system, + a file:// URL is constructed, using QUrl::fromLocalFile(). + + If that is not the case, an attempt is made to turn the string into a + http:// or ftp:// URL. The latter in the case the string starts with + 'ftp'. The result is then passed through QUrl's tolerant parser, and + in the case or success, a valid QUrl is returned, or else a QUrl(). + + \section1 Examples: + + \list + \o qt.nokia.com becomes http://qt.nokia.com + \o ftp.qt.nokia.com becomes ftp://ftp.qt.nokia.com + \o localhost becomes http://localhost + \o /home/user/test.html becomes file:///home/user/test.html (if exists) + \endlist + + \section2 Tips to avoid erroneous character conversion when dealing with + URLs and strings: + + \list + \o When creating an URL QString from a QByteArray or a char*, always use + QString::fromUtf8(). + \o Favor the use of QUrl::fromEncoded() and QUrl::toEncoded() instead of + QUrl(string) and QUrl::toString() when converting QUrl to/from string. + \endlist +*/ +QUrl QUrl::fromUserInput(const QString &userInput) +{ + QString trimmedString = userInput.trimmed(); + + // Absolute files + if (QDir::isAbsolutePath(trimmedString)) + return QUrl::fromLocalFile(trimmedString); + + // Check the most common case of a valid url with scheme and host first + QUrl url = QUrl::fromEncoded(trimmedString.toUtf8(), QUrl::TolerantMode); + if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) + return url; + + // If the string is missing the scheme or the scheme is not valid, prepend a scheme + QString scheme = url.scheme(); + if (scheme.isEmpty() || scheme.contains(QLatin1Char('.')) || scheme == QLatin1String("localhost")) { + // Do not do anything for strings such as "foo", only "foo.com" + int dotIndex = trimmedString.indexOf(QLatin1Char('.')); + if (dotIndex != -1 || trimmedString.startsWith(QLatin1String("localhost"))) { + const QString hostscheme = trimmedString.left(dotIndex).toLower(); + QByteArray scheme = (hostscheme == QLatin1String("ftp")) ? "ftp" : "http"; + trimmedString = QLatin1String(scheme) + QLatin1String("://") + trimmedString; + } + url = QUrl::fromEncoded(trimmedString.toUtf8(), QUrl::TolerantMode); + } + + if (url.isValid()) + return url; + + return QUrl(); +} + +/*! Returns a decoded copy of \a input. \a input is first decoded from percent encoding, then converted from UTF-8 to unicode. */ diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h index b00074a..f76d345 100644 --- a/src/corelib/io/qurl.h +++ b/src/corelib/io/qurl.h @@ -189,6 +189,8 @@ public: static QUrl fromEncoded(const QByteArray &url, ParsingMode mode); // ### Qt 5: merge the two fromEncoded() functions, with mode = TolerantMode + static QUrl fromUserInput(const QString &userInput); + void detach(); bool isDetached() const; |