diff options
author | Arttu Tarkiainen <arttu.tarkiainen@qt.io> | 2021-10-05 13:00:26 +0300 |
---|---|---|
committer | Arttu Tarkiainen <arttu.tarkiainen@qt.io> | 2021-10-07 16:40:38 +0300 |
commit | 9a21e64f19cb467ee17e3889d244aba21b2cc117 (patch) | |
tree | befb9dc9b987ccb8d2091f9c66fb22c5132a97b6 | |
parent | 669d46f092ed0dbde1be0d0c75b3483597ce8b42 (diff) |
Add invokable methods for converting paths to/from native separators
Task-number: QTIFW-2344
Change-Id: I971e96ed5b1f1e52c5779a04b7edab0952d89d9a
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Katja Marttila <katja.marttila@qt.io>
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
-rw-r--r-- | doc/scripting-api/packagemanagercore.qdoc | 21 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore.cpp | 27 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore.h | 3 | ||||
-rw-r--r-- | tests/auto/installer/packagemanagercore/tst_packagemanagercore.cpp | 22 |
4 files changed, 73 insertions, 0 deletions
diff --git a/doc/scripting-api/packagemanagercore.qdoc b/doc/scripting-api/packagemanagercore.qdoc index e51b0ec17..ea32a59b7 100644 --- a/doc/scripting-api/packagemanagercore.qdoc +++ b/doc/scripting-api/packagemanagercore.qdoc @@ -438,6 +438,27 @@ */ /*! + \qmlmethod string installer::toNativeSeparators(string path) + + Returns \a path with the '/' separators converted to separators that are + appropriate for the underlying operating system. + + On Unix platforms the returned string is the same as the argument. + + \sa fromNativeSeparators() +*/ + +/*! + \qmlmethod string installer::fromNativeSeparators(string path) + + Returns \a path using '/' as file separator. + + On Unix platforms the returned string is the same as the argument. + + \sa toNativeSeparators() +*/ + +/*! \qmlmethod boolean installer::fileExists(string filePath) Returns \c true if the \a filePath exists; otherwise returns \c false. diff --git a/src/libs/installer/packagemanagercore.cpp b/src/libs/installer/packagemanagercore.cpp index 232024f66..311ac544d 100644 --- a/src/libs/installer/packagemanagercore.cpp +++ b/src/libs/installer/packagemanagercore.cpp @@ -1003,6 +1003,33 @@ QString PackageManagerCore::readConsoleLine(const QString &title, qint64 maxlen) } /*! + Returns \a path with the '/' separators converted to separators that are + appropriate for the underlying operating system. + + On Unix platforms the returned string is the same as the argument. + + \sa {installer::toNativeSeparators}{installer.toNativeSeparators} + \sa fromNativeSeparators() +*/ +QString PackageManagerCore::toNativeSeparators(const QString &path) +{ + return QDir::toNativeSeparators(path); +} + +/*! + Returns \a path using '/' as file separator. + + On Unix platforms the returned string is the same as the argument. + + \sa {installer::fromNativeSeparators}{installer.fromNativeSeparators} + \sa toNativeSeparators() +*/ +QString PackageManagerCore::fromNativeSeparators(const QString &path) +{ + return QDir::fromNativeSeparators(path); +} + +/*! Checks whether the target directory \a targetDirectory exists and has contents: \list \li Returns \c true if the directory exists and is empty. diff --git a/src/libs/installer/packagemanagercore.h b/src/libs/installer/packagemanagercore.h index b5ef6304e..6698155c3 100644 --- a/src/libs/installer/packagemanagercore.h +++ b/src/libs/installer/packagemanagercore.h @@ -213,6 +213,9 @@ public: Q_INVOKABLE QString readFile(const QString &filePath, const QString &codecName) const; Q_INVOKABLE QString readConsoleLine(const QString &title = QString(), qint64 maxlen = 0) const; + Q_INVOKABLE QString toNativeSeparators(const QString &path); + Q_INVOKABLE QString fromNativeSeparators(const QString &path); + bool checkTargetDir(const QString &targetDirectory); QString targetDirWarning(const QString &targetDirectory) const; diff --git a/tests/auto/installer/packagemanagercore/tst_packagemanagercore.cpp b/tests/auto/installer/packagemanagercore/tst_packagemanagercore.cpp index a3e86c2f8..aa74d8dbe 100644 --- a/tests/auto/installer/packagemanagercore/tst_packagemanagercore.cpp +++ b/tests/auto/installer/packagemanagercore/tst_packagemanagercore.cpp @@ -370,6 +370,28 @@ private slots: QCOMPARE(core->value("RootDir"), QLatin1String("Overwritten RootDir")); core->deleteLater(); } + + void testToFromNativeSeparators_data() + { + QTest::addColumn<QString>("path"); + QTest::newRow("Slash separator") << "a/test/path"; + QTest::newRow("Backslash separator") << "a\\test\\path"; + QTest::newRow("Mixed separators") << "a/test\\path"; + } + + void testToFromNativeSeparators() + { + QFETCH(QString, path); + + PackageManagerCore core; +#ifdef Q_OS_WIN + QCOMPARE(core.toNativeSeparators(path), "a\\test\\path"); + QCOMPARE(core.fromNativeSeparators(path), "a/test/path"); +#else + QCOMPARE(core.toNativeSeparators(path), path); + QCOMPARE(core.fromNativeSeparators(path), path); +#endif + } }; |