5554 tmp.setEncodedUrl(input, parsingMode); |
5554 tmp.setEncodedUrl(input, parsingMode); |
5555 return tmp; |
5555 return tmp; |
5556 } |
5556 } |
5557 |
5557 |
5558 /*! |
5558 /*! |
5559 Returns a valid URL from a user supplied \a userInput string if one can be |
|
5560 deducted. In the case that is not possible, an invalid QUrl() is returned. |
|
5561 |
|
5562 \since 4.6 |
|
5563 |
|
5564 Most applications that can browse the web, allow the user to input a URL |
|
5565 in the form of a plain string. This string can be manually typed into |
|
5566 a location bar, obtained from the clipboard, or passed in via command |
|
5567 line arguments. |
|
5568 |
|
5569 When the string is not already a valid URL, a best guess is performed, |
|
5570 making various web related assumptions. |
|
5571 |
|
5572 In the case the string corresponds to a valid file path on the system, |
|
5573 a file:// URL is constructed, using QUrl::fromLocalFile(). |
|
5574 |
|
5575 If that is not the case, an attempt is made to turn the string into a |
|
5576 http:// or ftp:// URL. The latter in the case the string starts with |
|
5577 'ftp'. The result is then passed through QUrl's tolerant parser, and |
|
5578 in the case or success, a valid QUrl is returned, or else a QUrl(). |
|
5579 |
|
5580 \section1 Examples: |
|
5581 |
|
5582 \list |
|
5583 \o qt.nokia.com becomes http://qt.nokia.com |
|
5584 \o ftp.qt.nokia.com becomes ftp://ftp.qt.nokia.com |
|
5585 \o localhost becomes http://localhost |
|
5586 \o /home/user/test.html becomes file:///home/user/test.html (if exists) |
|
5587 \endlist |
|
5588 |
|
5589 \section2 Tips to avoid erroneous character conversion when dealing with |
|
5590 URLs and strings: |
|
5591 |
|
5592 \list |
|
5593 \o When creating an URL QString from a QByteArray or a char*, always use |
|
5594 QString::fromUtf8(). |
|
5595 \o Favor the use of QUrl::fromEncoded() and QUrl::toEncoded() instead of |
|
5596 QUrl(string) and QUrl::toString() when converting QUrl to/from string. |
|
5597 \endlist |
|
5598 */ |
|
5599 QUrl QUrl::fromUserInput(const QString &userInput) |
|
5600 { |
|
5601 QString trimmedString = userInput.trimmed(); |
|
5602 |
|
5603 // Absolute files |
|
5604 if (QDir::isAbsolutePath(trimmedString)) |
|
5605 return QUrl::fromLocalFile(trimmedString); |
|
5606 |
|
5607 // Check the most common case of a valid url with scheme and host first |
|
5608 QUrl url = QUrl::fromEncoded(trimmedString.toUtf8(), QUrl::TolerantMode); |
|
5609 if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) |
|
5610 return url; |
|
5611 |
|
5612 // If the string is missing the scheme or the scheme is not valid, prepend a scheme |
|
5613 QString scheme = url.scheme(); |
|
5614 if (scheme.isEmpty() || scheme.contains(QLatin1Char('.')) || scheme == QLatin1String("localhost")) { |
|
5615 // Do not do anything for strings such as "foo", only "foo.com" |
|
5616 int dotIndex = trimmedString.indexOf(QLatin1Char('.')); |
|
5617 if (dotIndex != -1 || trimmedString.startsWith(QLatin1String("localhost"))) { |
|
5618 const QString hostscheme = trimmedString.left(dotIndex).toLower(); |
|
5619 QByteArray scheme = (hostscheme == QLatin1String("ftp")) ? "ftp" : "http"; |
|
5620 trimmedString = QLatin1String(scheme) + QLatin1String("://") + trimmedString; |
|
5621 } |
|
5622 url = QUrl::fromEncoded(trimmedString.toUtf8(), QUrl::TolerantMode); |
|
5623 } |
|
5624 |
|
5625 if (url.isValid()) |
|
5626 return url; |
|
5627 |
|
5628 return QUrl(); |
|
5629 } |
|
5630 |
|
5631 /*! |
|
5632 Returns a decoded copy of \a input. \a input is first decoded from |
5559 Returns a decoded copy of \a input. \a input is first decoded from |
5633 percent encoding, then converted from UTF-8 to unicode. |
5560 percent encoding, then converted from UTF-8 to unicode. |
5634 */ |
5561 */ |
5635 QString QUrl::fromPercentEncoding(const QByteArray &input) |
5562 QString QUrl::fromPercentEncoding(const QByteArray &input) |
5636 { |
5563 { |
6225 /*! |
6152 /*! |
6226 \fn DataPtr &QUrl::data_ptr() |
6153 \fn DataPtr &QUrl::data_ptr() |
6227 \internal |
6154 \internal |
6228 */ |
6155 */ |
6229 |
6156 |
|
6157 // The following code has the following copyright: |
|
6158 /* |
|
6159 Copyright (C) Research In Motion Limited 2009. All rights reserved. |
|
6160 |
|
6161 Redistribution and use in source and binary forms, with or without |
|
6162 modification, are permitted provided that the following conditions are met: |
|
6163 * Redistributions of source code must retain the above copyright |
|
6164 notice, this list of conditions and the following disclaimer. |
|
6165 * Redistributions in binary form must reproduce the above copyright |
|
6166 notice, this list of conditions and the following disclaimer in the |
|
6167 documentation and/or other materials provided with the distribution. |
|
6168 * Neither the name of Research In Motion Limited nor the |
|
6169 names of its contributors may be used to endorse or promote products |
|
6170 derived from this software without specific prior written permission. |
|
6171 |
|
6172 THIS SOFTWARE IS PROVIDED BY Research In Motion Limited ''AS IS'' AND ANY |
|
6173 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
6174 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
6175 DISCLAIMED. IN NO EVENT SHALL Research In Motion Limited BE LIABLE FOR ANY |
|
6176 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|
6177 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
6178 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
6179 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
6180 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
6181 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
6182 |
|
6183 */ |
|
6184 |
|
6185 |
|
6186 /*! |
|
6187 Returns a valid URL from a user supplied \a userInput string if one can be |
|
6188 deducted. In the case that is not possible, an invalid QUrl() is returned. |
|
6189 |
|
6190 \since 4.6 |
|
6191 |
|
6192 Most applications that can browse the web, allow the user to input a URL |
|
6193 in the form of a plain string. This string can be manually typed into |
|
6194 a location bar, obtained from the clipboard, or passed in via command |
|
6195 line arguments. |
|
6196 |
|
6197 When the string is not already a valid URL, a best guess is performed, |
|
6198 making various web related assumptions. |
|
6199 |
|
6200 In the case the string corresponds to a valid file path on the system, |
|
6201 a file:// URL is constructed, using QUrl::fromLocalFile(). |
|
6202 |
|
6203 If that is not the case, an attempt is made to turn the string into a |
|
6204 http:// or ftp:// URL. The latter in the case the string starts with |
|
6205 'ftp'. The result is then passed through QUrl's tolerant parser, and |
|
6206 in the case or success, a valid QUrl is returned, or else a QUrl(). |
|
6207 |
|
6208 \section1 Examples: |
|
6209 |
|
6210 \list |
|
6211 \o qt.nokia.com becomes http://qt.nokia.com |
|
6212 \o ftp.qt.nokia.com becomes ftp://ftp.qt.nokia.com |
|
6213 \o hostname becomes http://hostname |
|
6214 \o /home/user/test.html becomes file:///home/user/test.html |
|
6215 \endlist |
|
6216 |
|
6217 \section2 Tips to avoid erroneous character conversion when dealing with |
|
6218 URLs and strings: |
|
6219 |
|
6220 \list |
|
6221 \o When creating an URL QString from a QByteArray or a char*, always use |
|
6222 QString::fromUtf8(). |
|
6223 \o Favor the use of QUrl::fromEncoded() and QUrl::toEncoded() instead of |
|
6224 QUrl(string) and QUrl::toString() when converting QUrl to/from string. |
|
6225 \endlist |
|
6226 */ |
|
6227 QUrl QUrl::fromUserInput(const QString &userInput) |
|
6228 { |
|
6229 QString trimmedString = userInput.trimmed(); |
|
6230 |
|
6231 // Check first for files, since on Windows drive letters can be interpretted as schemes |
|
6232 if (QDir::isAbsolutePath(trimmedString)) |
|
6233 return QUrl::fromLocalFile(trimmedString); |
|
6234 |
|
6235 QUrl url = QUrl::fromEncoded(trimmedString.toUtf8(), QUrl::TolerantMode); |
|
6236 QUrl urlPrepended = QUrl::fromEncoded((QLatin1String("http://") + trimmedString).toUtf8(), QUrl::TolerantMode); |
|
6237 |
|
6238 // Check the most common case of a valid url with scheme and host |
|
6239 // We check if the port would be valid by adding the scheme to handle the case host:port |
|
6240 // where the host would be interpretted as the scheme |
|
6241 if (url.isValid() |
|
6242 && !url.scheme().isEmpty() |
|
6243 && (!url.host().isEmpty() || !url.path().isEmpty()) |
|
6244 && urlPrepended.port() == -1) |
|
6245 return url; |
|
6246 |
|
6247 // Else, try the prepended one and adjust the scheme from the host name |
|
6248 if (urlPrepended.isValid() && (!urlPrepended.host().isEmpty() || !urlPrepended.path().isEmpty())) |
|
6249 { |
|
6250 int dotIndex = trimmedString.indexOf(QLatin1Char('.')); |
|
6251 const QString hostscheme = trimmedString.left(dotIndex).toLower(); |
|
6252 if (hostscheme == QLatin1String("ftp")) |
|
6253 urlPrepended.setScheme(QLatin1String("ftp")); |
|
6254 return urlPrepended; |
|
6255 } |
|
6256 |
|
6257 return QUrl(); |
|
6258 } |
|
6259 // end of BSD code |
|
6260 |
6230 QT_END_NAMESPACE |
6261 QT_END_NAMESPACE |