File indexing completed on 2024-05-05 04:59:27

0001 /*
0002  * SPDX-FileCopyrightText: 2022 Kai Uwe Broulik <kde@broulik.de>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 
0006 #pragma once
0007 
0008 #include <QObject>
0009 #include <QVector>
0010 
0011 #include <KIO/WorkerBase>
0012 
0013 #include <libimobiledevice/afc.h>
0014 #include <libimobiledevice/libimobiledevice.h>
0015 #include <libimobiledevice/lockdown.h>
0016 
0017 #include "afcclient.h"
0018 #include "afcdiskusage.h"
0019 #include "afcfilereader.h"
0020 
0021 #include <KIO/UDSEntry>
0022 
0023 class AfcApp;
0024 
0025 struct LockdowndClientCleanup {
0026     static inline void cleanup(lockdownd_client_t lockdowndClient)
0027     {
0028         if (lockdowndClient) {
0029             lockdownd_client_free(lockdowndClient);
0030         }
0031     }
0032 };
0033 using ScopedLockdowndClientPtr = QScopedPointer<lockdownd_client_private, LockdowndClientCleanup>;
0034 
0035 class AfcDevice
0036 {
0037 public:
0038     explicit AfcDevice(const QString &id);
0039     ~AfcDevice();
0040 
0041     idevice_t device() const;
0042 
0043     QString id() const;
0044     bool isValid() const;
0045     QString errorText() const;
0046 
0047     QString name() const;
0048     QString deviceClass() const;
0049 
0050     QString cacheLocation() const;
0051 
0052     KIO::WorkerResult client(const QString &appId, AfcClient::Ptr &client);
0053 
0054     AfcApp app(const QString &bundleId);
0055     KIO::WorkerResult apps(QVector<AfcApp> &apps);
0056 
0057     KIO::WorkerResult fetchAppIcon(AfcApp &app);
0058     // Fetches app icons for the list of apps provided.
0059     KIO::WorkerResult fetchAppIcons(QVector<AfcApp> &apps);
0060 
0061 private:
0062     KIO::WorkerResult handshake();
0063     QString appIconCachePath(const QString &bundleId) const;
0064 
0065     idevice_t m_device = nullptr;
0066 
0067     ScopedLockdowndClientPtr m_lockdowndClient;
0068     bool m_handshakeSuccessful = false;
0069 
0070     afc_client_t m_afcClient = nullptr;
0071 
0072     QString m_id;
0073     QString m_name;
0074     QString m_deviceClass;
0075 
0076     QHash<QString, AfcApp> m_apps;
0077 
0078     // We can't have too many simultaneous house arrest connections
0079     // so store only the last requested client which in the majority case
0080     // should be the one where subsequent requests go to
0081     AfcClient::Ptr m_lastClient;
0082 };