File indexing completed on 2024-11-10 04:30:22

0001 /*
0002  * SPDX-FileCopyrightText: 2022 Kai Uwe Broulik <kde@broulik.de>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 
0006 #include "afcspringboard.h"
0007 #include "afcutils.h"
0008 
0009 #include "afc_debug.h"
0010 
0011 #include <QScopeGuard>
0012 
0013 #include <libimobiledevice/sbservices.h>
0014 
0015 using namespace KIO;
0016 
0017 static const char s_lockdownLabel[] = "kio_afc";
0018 
0019 AfcSpringBoard::AfcSpringBoard(idevice_t device, lockdownd_client_t lockdowndClient)
0020 {
0021     auto lockdownRet = lockdownd_start_service(lockdowndClient, SBSERVICES_SERVICE_NAME, &m_springBoardService);
0022     if (lockdownRet != LOCKDOWN_E_SUCCESS) {
0023         qCWarning(KIO_AFC_LOG) << "Failed to start sbservices for getting app icons" << lockdownRet;
0024         m_result = AfcUtils::Result::from(lockdownRet);
0025         return;
0026     }
0027 
0028     auto springBoardRet = sbservices_client_new(device, m_springBoardService, &m_springBoardClient);
0029     if (springBoardRet != SBSERVICES_E_SUCCESS) {
0030         qCWarning(KIO_AFC_LOG) << "Failed to create sbservices instance for getting app icons" << springBoardRet;
0031         m_result = AfcUtils::Result::from(springBoardRet);
0032         return;
0033     }
0034 
0035     m_result = WorkerResult::pass();
0036 }
0037 
0038 AfcSpringBoard::~AfcSpringBoard()
0039 {
0040     if (m_springBoardClient) {
0041         sbservices_client_free(m_springBoardClient);
0042     }
0043     if (m_springBoardService) {
0044         lockdownd_service_descriptor_free(m_springBoardService);
0045     }
0046 }
0047 
0048 WorkerResult AfcSpringBoard::result() const
0049 {
0050     return m_result;
0051 }
0052 
0053 WorkerResult AfcSpringBoard::fetchAppIconData(const QString &bundleId, QByteArray &data)
0054 {
0055     Q_ASSERT(m_springBoardClient);
0056 
0057     char *rawData = nullptr;
0058     uint64_t len = 0;
0059 
0060     auto dataCleanup = qScopeGuard([rawData] {
0061         free(rawData);
0062     });
0063 
0064     auto ret = sbservices_get_icon_pngdata(m_springBoardClient, bundleId.toUtf8().constData(), &rawData, &len);
0065     if (ret != SBSERVICES_E_SUCCESS || !rawData || len == 0) {
0066         qCWarning(KIO_AFC_LOG) << "Failed to get pngdata from" << bundleId << ret;
0067         return AfcUtils::Result::from(ret);
0068     }
0069 
0070     data = QByteArray::fromRawData(rawData, len);
0071     return WorkerResult::pass();
0072 }