diff --git a/autotests/urltest.cpp b/autotests/urltest.cpp --- a/autotests/urltest.cpp +++ b/autotests/urltest.cpp @@ -28,82 +28,173 @@ private Q_SLOTS: void testGDriveUrl_data(); void testGDriveUrl(); + void testGDriveParent_data(); + void testGDriveParent(); }; QTEST_GUILESS_MAIN(UrlTest) void UrlTest::testGDriveUrl_data() { const auto gdriveUrl = [](const QString &path) { QUrl url; - url.setScheme(QStringLiteral("gdrive")); + url.setScheme(GDriveUrl::Scheme); url.setPath(path); return url; }; QTest::addColumn("url"); QTest::addColumn("expectedToString"); QTest::addColumn("expectedAccount"); QTest::addColumn("expectedParentPath"); + QTest::addColumn("expectedInTrash"); QTest::addColumn("expectedPathComponents"); + QTest::addColumn("expectedFilename"); QTest::newRow("root url") << gdriveUrl(QStringLiteral("/")) << QStringLiteral("gdrive:/") << QString() << QString() - << QStringList(); + << false + << QStringList() + << ""; QTest::newRow("account root url") << gdriveUrl(QStringLiteral("/foo@gmail.com")) << QStringLiteral("gdrive:/foo@gmail.com") << QStringLiteral("foo@gmail.com") << QStringLiteral("/") - << QStringList {QStringLiteral("foo@gmail.com")}; + << false + << QStringList {QStringLiteral("foo@gmail.com")} + << QStringLiteral("foo@gmail.com"); + + QTest::newRow("account trash url") + << gdriveUrl(QStringLiteral("/foo@gmail.com/") + GDriveUrl::TrashDir) + << QStringLiteral("gdrive:/foo@gmail.com/") + GDriveUrl::TrashDir + << QStringLiteral("foo@gmail.com") + << QStringLiteral("/foo@gmail.com") + << false + << QStringList {QStringLiteral("foo@gmail.com"), GDriveUrl::TrashDir} + << GDriveUrl::TrashDir; + + QTest::newRow("file in trash") + << gdriveUrl(QStringLiteral("/foo@gmail.com/") + GDriveUrl::TrashDir + ("/baz.txt")) + << QStringLiteral("gdrive:/foo@gmail.com/") + GDriveUrl::TrashDir + ("/baz.txt") + << QStringLiteral("foo@gmail.com") + << QStringLiteral("/foo@gmail.com/") + GDriveUrl::TrashDir + << true + << QStringList {QStringLiteral("foo@gmail.com"), GDriveUrl::TrashDir, QStringLiteral("baz.txt")} + << QStringLiteral("baz.txt"); QTest::newRow("file in account root") << gdriveUrl(QStringLiteral("/foo@gmail.com/bar.txt")) << QStringLiteral("gdrive:/foo@gmail.com/bar.txt") << QStringLiteral("foo@gmail.com") << QStringLiteral("/foo@gmail.com") - << QStringList {QStringLiteral("foo@gmail.com"), QStringLiteral("bar.txt")}; + << false + << QStringList {QStringLiteral("foo@gmail.com"), QStringLiteral("bar.txt")} + << QStringLiteral("bar.txt"); QTest::newRow("folder in account root - no trailing slash") << gdriveUrl(QStringLiteral("/foo@gmail.com/bar")) << QStringLiteral("gdrive:/foo@gmail.com/bar") << QStringLiteral("foo@gmail.com") << QStringLiteral("/foo@gmail.com") - << QStringList {QStringLiteral("foo@gmail.com"), QStringLiteral("bar")}; + << false + << QStringList {QStringLiteral("foo@gmail.com"), QStringLiteral("bar")} + << QStringLiteral("bar"); + QTest::newRow("folder in account root - trailing slash") << gdriveUrl(QStringLiteral("/foo@gmail.com/bar/")) << QStringLiteral("gdrive:/foo@gmail.com/bar/") << QStringLiteral("foo@gmail.com") << QStringLiteral("/foo@gmail.com") - << QStringList {QStringLiteral("foo@gmail.com"), QStringLiteral("bar")}; + << false + << QStringList {QStringLiteral("foo@gmail.com"), QStringLiteral("bar")} + << QStringLiteral("bar"); QTest::newRow("file in subfolder") << gdriveUrl(QStringLiteral("/foo@gmail.com/bar/baz.txt")) << QStringLiteral("gdrive:/foo@gmail.com/bar/baz.txt") << QStringLiteral("foo@gmail.com") << QStringLiteral("/foo@gmail.com/bar") - << QStringList {QStringLiteral("foo@gmail.com"), QStringLiteral("bar"), QStringLiteral("baz.txt")}; + << false + << QStringList {QStringLiteral("foo@gmail.com"), QStringLiteral("bar"), QStringLiteral("baz.txt")} + << QStringLiteral("baz.txt"); } void UrlTest::testGDriveUrl() { QFETCH(QUrl, url); + const auto gdriveUrl = GDriveUrl(url); + QFETCH(QString, expectedToString); - QCOMPARE(url.toString(), expectedToString); + QCOMPARE(gdriveUrl.url(), QUrl(expectedToString)); QFETCH(QString, expectedAccount); QFETCH(QString, expectedParentPath); + QFETCH(bool, expectedInTrash); QFETCH(QStringList, expectedPathComponents); + QFETCH(QString, expectedFilename); - const auto gdriveUrl = GDriveUrl(url); + QCOMPARE(gdriveUrl.account(), expectedAccount); + QCOMPARE(gdriveUrl.parentPath(), expectedParentPath); + QCOMPARE(gdriveUrl.pathComponents(), expectedPathComponents); + QCOMPARE(gdriveUrl.inTrash(), expectedInTrash); + QCOMPARE(gdriveUrl.filename(), expectedFilename); + + if (expectedPathComponents.isEmpty()) { + QVERIFY(gdriveUrl.isRoot()); + } else if (expectedPathComponents.count() == 1) { + QVERIFY(gdriveUrl.isAccountRoot()); + } +} + +void UrlTest::testGDriveParent_data() { + const auto gdriveUrl = [](const QString &path) { + QUrl url; + url.setScheme(GDriveUrl::Scheme); + url.setPath(path); + return url; + }; + + QTest::addColumn("url"); + QTest::addColumn("expectedUrl"); + QTest::addColumn("expectedAccount"); + QTest::addColumn("expectedParentPath"); + QTest::addColumn("expectedInTrash"); + QTest::addColumn("expectedPathComponents"); + QTest::addColumn("expectedFilename"); + + QTest::newRow("file parent") + << gdriveUrl(QStringLiteral("/foo@gmail.com/bar.txt")) + << QUrl("gdrive:/foo@gmail.com") + << QStringLiteral("foo@gmail.com") + << QStringLiteral("/") + << false + << QStringList {QStringLiteral("foo@gmail.com")} + << QStringLiteral("foo@gmail.com"); +} + +void UrlTest::testGDriveParent() { + QFETCH(QUrl, url); + const auto gdriveUrl = GDriveUrl(url).parent(); + + QFETCH(QUrl, expectedUrl); + QCOMPARE(gdriveUrl.url(), expectedUrl); + + QFETCH(QString, expectedAccount); + QFETCH(QString, expectedParentPath); + QFETCH(bool, expectedInTrash); + QFETCH(QStringList, expectedPathComponents); + QFETCH(QString, expectedFilename); QCOMPARE(gdriveUrl.account(), expectedAccount); QCOMPARE(gdriveUrl.parentPath(), expectedParentPath); QCOMPARE(gdriveUrl.pathComponents(), expectedPathComponents); + QCOMPARE(gdriveUrl.inTrash(), expectedInTrash); + QCOMPARE(gdriveUrl.filename(), expectedFilename); if (expectedPathComponents.isEmpty()) { QVERIFY(gdriveUrl.isRoot()); diff --git a/src/gdrivehelper.cpp b/src/gdrivehelper.cpp --- a/src/gdrivehelper.cpp +++ b/src/gdrivehelper.cpp @@ -136,7 +136,7 @@ KIO::UDSEntry GDriveHelper::trash() { KIO::UDSEntry trashEntry; - trashEntry.insert(KIO::UDSEntry::UDS_NAME, QStringLiteral("trash")); + trashEntry.insert(KIO::UDSEntry::UDS_NAME, GDriveUrl::TrashDir); trashEntry.insert(KIO::UDSEntry::UDS_DISPLAY_NAME, i18n("Trash")); trashEntry.insert(KIO::UDSEntry::UDS_FILE_TYPE, S_IFDIR); trashEntry.insert(KIO::UDSEntry::UDS_ICON_NAME, QStringLiteral("user-trash")); diff --git a/src/gdriveurl.h b/src/gdriveurl.h --- a/src/gdriveurl.h +++ b/src/gdriveurl.h @@ -29,12 +29,20 @@ explicit GDriveUrl(const QUrl &url); QString account() const; + QString filename() const; bool isRoot() const; bool isAccountRoot() const; + bool isTrashDir() const; + bool inTrash() const; + QUrl url() const; QString parentPath() const; + GDriveUrl parent() const; QStringList pathComponents() const; + static const QString Scheme; + static const QString TrashDir; private: + GDriveUrl(const QUrl url, const QStringList components); QUrl m_url; QStringList m_components; }; diff --git a/src/gdriveurl.cpp b/src/gdriveurl.cpp --- a/src/gdriveurl.cpp +++ b/src/gdriveurl.cpp @@ -20,13 +20,21 @@ #include "gdriveurl.h" +const QString GDriveUrl::Scheme = QStringLiteral("gdrive"); +const QString GDriveUrl::TrashDir = QStringLiteral("trash"); + GDriveUrl::GDriveUrl(const QUrl &url) : m_url(url) { const auto path = url.adjusted(QUrl::StripTrailingSlash).path(); m_components = path.split(QLatin1Char('/'), QString::SkipEmptyParts); } +GDriveUrl::GDriveUrl(const QUrl url, const QStringList components) + : m_url(url), + m_components(components) +{ } + QString GDriveUrl::account() const { if (isRoot()) { @@ -36,6 +44,15 @@ return m_components.at(0); } +QString GDriveUrl::filename() const +{ + if (m_components.isEmpty()) { + return QString(); + } + + return m_components.last(); +} + bool GDriveUrl::isRoot() const { return m_components.isEmpty(); @@ -46,6 +63,21 @@ return m_components.length() == 1; } +bool GDriveUrl::isTrashDir() const +{ + return m_components.length() == 2 && m_components.at(1) == TrashDir; +} + +bool GDriveUrl::inTrash() const +{ + return m_components.length() > 2 && m_components.at(1) == TrashDir; +} + +QUrl GDriveUrl::url() const +{ + return m_url; +} + QString GDriveUrl::parentPath() const { if (isRoot()) { @@ -58,6 +90,15 @@ return QLatin1Char('/') + path.join(QLatin1Char('/')); } +GDriveUrl GDriveUrl::parent() const +{ + auto parent_components = m_components; + parent_components.removeLast(); + + QUrl parent_url = QUrl(GDriveUrl::Scheme + QStringLiteral(":/") + parent_components.join(QLatin1Char('/'))); + return GDriveUrl(parent_url, parent_components); +} + QStringList GDriveUrl::pathComponents() const { return m_components;