File indexing completed on 2024-05-19 05:00:39
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com> 0003 * SPDX-FileCopyrightText: 2016 Elvis Angelaccio <elvis.angelaccio@kde.org> 0004 * SPDX-FileCopyrightText: 2019 David Barchiesi <david@barchie.si> 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 * 0008 */ 0009 0010 #include "gdriveurl.h" 0011 0012 const QString GDriveUrl::Scheme = QLatin1String("gdrive"); 0013 const QString GDriveUrl::SharedWithMeDir = QLatin1String("Shared With Me"); 0014 const QString GDriveUrl::SharedDrivesDir = QLatin1String("Shared Drives"); 0015 const QString GDriveUrl::TrashDir = QLatin1String("trash"); 0016 const QString GDriveUrl::NewAccountPath = QLatin1String("new-account"); 0017 0018 GDriveUrl::GDriveUrl(const QUrl &url) 0019 : m_url(url) 0020 { 0021 const auto path = url.adjusted(QUrl::StripTrailingSlash).path(); 0022 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) 0023 m_components = path.split(QLatin1Char('/'), QString::SkipEmptyParts); 0024 #else 0025 m_components = path.split(QLatin1Char('/'), Qt::SkipEmptyParts); 0026 #endif 0027 } 0028 0029 QString GDriveUrl::account() const 0030 { 0031 if (isRoot()) { 0032 return QString(); 0033 } 0034 0035 return m_components.at(0); 0036 } 0037 0038 QString GDriveUrl::filename() const 0039 { 0040 if (m_components.isEmpty()) { 0041 return QString(); 0042 } 0043 0044 return m_components.last(); 0045 } 0046 0047 bool GDriveUrl::isRoot() const 0048 { 0049 return m_components.isEmpty(); 0050 } 0051 0052 bool GDriveUrl::isAccountRoot() const 0053 { 0054 return m_components.length() == 1 && !isNewAccountPath(); 0055 } 0056 0057 bool GDriveUrl::isNewAccountPath() const 0058 { 0059 return m_components.length() == 1 && m_components.at(0) == NewAccountPath; 0060 } 0061 0062 bool GDriveUrl::isTopLevel() const 0063 { 0064 return m_components.length() == 2; 0065 } 0066 0067 bool GDriveUrl::isSharedWithMeRoot() const 0068 { 0069 return m_components.length() == 2 && m_components.at(1) == SharedWithMeDir; 0070 } 0071 0072 bool GDriveUrl::isSharedWithMeTopLevel() const 0073 { 0074 return m_components.length() == 3 && m_components.at(1) == SharedWithMeDir; 0075 } 0076 0077 bool GDriveUrl::isSharedWithMe() const 0078 { 0079 return m_components.length() > 2 && m_components.at(1) == SharedWithMeDir; 0080 } 0081 0082 bool GDriveUrl::isSharedDrivesRoot() const 0083 { 0084 return m_components.length() == 2 && m_components.at(1) == SharedDrivesDir; 0085 } 0086 0087 bool GDriveUrl::isSharedDrive() const 0088 { 0089 return m_components.length() == 3 && m_components.at(1) == SharedDrivesDir; 0090 } 0091 0092 bool GDriveUrl::isTrashDir() const 0093 { 0094 return m_components.length() == 2 && m_components.at(1) == TrashDir; 0095 } 0096 0097 bool GDriveUrl::isTrashed() const 0098 { 0099 return m_components.length() > 2 && m_components.at(1) == TrashDir; 0100 } 0101 0102 QUrl GDriveUrl::url() const 0103 { 0104 return m_url; 0105 } 0106 0107 QString GDriveUrl::parentPath() const 0108 { 0109 if (isRoot()) { 0110 return QString(); 0111 } 0112 0113 auto path = m_components; 0114 path.removeLast(); 0115 0116 return QLatin1Char('/') + path.join(QLatin1Char('/')); 0117 } 0118 0119 QStringList GDriveUrl::pathComponents() const 0120 { 0121 return m_components; 0122 } 0123 0124 QString GDriveUrl::buildSharedDrivePath(const QString &accountId, const QString &drive) 0125 { 0126 return QStringLiteral("/%1/%2/%3").arg(accountId, SharedDrivesDir, drive); 0127 }