diff options
author | David Faure <david.faure@kdab.com> | 2014-04-23 22:59:34 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-05-06 01:20:17 +0200 |
commit | 31ce6f50c679e61dc53f09ee1b1637918da38a82 (patch) | |
tree | 12f8fe00be487990f586b2675e40294e486a1245 /src/corelib | |
parent | da24bfe1f7f8a4ece2c1bb9998d6f99f7b4fb137 (diff) |
Add QUrl::fromUserInput overload with a cwd argument.
Useful for any application that can take URLs on the command-line, so that
full paths and relative paths can also be accepted.
Change-Id: I8a2c50f36d60bdc49c065b3065972fd5d45fa12a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/io/qurl.cpp | 60 | ||||
-rw-r--r-- | src/corelib/io/qurl.h | 9 |
2 files changed, 69 insertions, 0 deletions
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index 7018b333f2..58b169e588 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -364,6 +364,29 @@ */ /*! + \enum QUrl::UserInputResolutionOption + \since 5.4 + + The user input resolution options define how fromUserInput() should + interpret strings that could either be a relative path or the short + form of a HTTP URL. For instance \c{file.pl} can be either a local file + or the URL \c{http://file.pl}. + + \value DefaultResolution The default resolution mechanism is to check + whether a local file exists, in the working + directory given to fromUserInput, and only + return a local path in that case. Otherwise a URL + is assumed. + \value AssumeLocalFile This option makes fromUserInput() always return + a local path unless the input contains a scheme, such as + \c{http://file.pl}. This is useful for applications + such as text editors, which are able to create + the file if it doesn't exist. + + \sa fromUserInput() +*/ + +/*! \fn QUrl::QUrl(QUrl &&other) Move-constructs a QUrl instance, making it point at the same @@ -4095,6 +4118,43 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 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. + This overload takes a \a workingDirectory path, in order to be able to + handle relative paths. This is especially useful when handling command + line arguments. + If \a workingDirectory is empty, no handling of relative paths will be done, + so this method will behave like its one argument overload. + + By default, an input string that looks like a relative path will only be treated + as such if the file actually exists in the given working directory. + + If the application can handle files that don't exist yet, it should pass the + flag AssumeLocalFile in \a options. + + \since 5.4 +*/ +QUrl QUrl::fromUserInput(const QString &userInput, const QString &workingDirectory, + UserInputResolutionOptions options) +{ + QString trimmedString = userInput.trimmed(); + + if (trimmedString.isEmpty()) + return QUrl(); + + // Check both QUrl::isRelative (to detect full URLs) and QDir::isAbsolutePath (since on Windows drive letters can be interpreted as schemes) + QUrl url = QUrl(trimmedString, QUrl::TolerantMode); + if (url.isRelative() && !QDir::isAbsolutePath(trimmedString)) { + QFileInfo fileInfo(QDir(workingDirectory), trimmedString); + if ((options & AssumeLocalFile) || fileInfo.exists()) + return QUrl::fromLocalFile(fileInfo.absoluteFilePath()); + } + + return fromUserInput(trimmedString); +} + +/*! + 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 diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h index 602e91ce30..d621d7ef03 100644 --- a/src/corelib/io/qurl.h +++ b/src/corelib/io/qurl.h @@ -199,7 +199,16 @@ public: QByteArray toEncoded(FormattingOptions options = FullyEncoded) const; static QUrl fromEncoded(const QByteArray &url, ParsingMode mode = TolerantMode); + enum UserInputResolutionOption { + DefaultResolution, + AssumeLocalFile + }; + Q_DECLARE_FLAGS(UserInputResolutionOptions, UserInputResolutionOption) + static QUrl fromUserInput(const QString &userInput); + // ### Qt6 merge with fromUserInput(QString), by adding = QString() + static QUrl fromUserInput(const QString &userInput, const QString &workingDirectory, + UserInputResolutionOptions options = DefaultResolution); bool isValid() const; QString errorString() const; |