File indexing completed on 2024-04-21 08:47:10

0001 /*
0002  * SPDX-FileCopyrightText: 2022 Kai Uwe Broulik <kde@broulik.de>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 
0006 #include "afcurl.h"
0007 
0008 AfcUrl::AfcUrl(const QUrl &url)
0009     : m_url(url)
0010 {
0011     if (!url.isValid() || url.scheme() != QLatin1String("afc")) {
0012         return;
0013     }
0014 
0015     m_device = url.host();
0016 
0017     const int defaultPort = static_cast<int>(BrowseMode::FileSystem);
0018     m_browseMode = static_cast<BrowseMode>(url.port(defaultPort));
0019 
0020     m_path = url.path();
0021 
0022     if (!m_path.isEmpty()) {
0023         Q_ASSERT(m_path.startsWith(QLatin1Char('/')));
0024     }
0025 
0026     if (m_browseMode == BrowseMode::Apps) {
0027         int slashAfterAppIdx = m_path.indexOf(QLatin1Char('/'), 1);
0028         if (slashAfterAppIdx == -1) {
0029             slashAfterAppIdx = m_path.length();
0030         }
0031 
0032         m_appId = m_path.mid(1, slashAfterAppIdx - 1); // exclude slashes in app ID
0033         m_path = m_path.mid(slashAfterAppIdx); // include leading slash in path
0034     }
0035 
0036     if (m_path == QLatin1Char('/')) {
0037         m_path.clear();
0038     }
0039 }
0040 
0041 QUrl AfcUrl::url() const
0042 {
0043     return m_url;
0044 }
0045 
0046 AfcUrl::BrowseMode AfcUrl::browseMode() const
0047 {
0048     return m_browseMode;
0049 }
0050 
0051 QString AfcUrl::device() const
0052 {
0053     return m_device;
0054 }
0055 
0056 QString AfcUrl::appId() const
0057 {
0058     return m_appId;
0059 }
0060 
0061 QString AfcUrl::path() const
0062 {
0063     return m_path;
0064 }
0065 
0066 bool AfcUrl::isValid() const
0067 {
0068     return m_browseMode == BrowseMode::FileSystem || m_browseMode == BrowseMode::Apps;
0069 }