File: afcdevice.cpp

package info (click to toggle)
kio-extras 4%3A25.04.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 31,928 kB
  • sloc: cpp: 28,852; ansic: 3,084; perl: 1,048; xml: 116; sh: 92; python: 28; makefile: 9
file content (344 lines) | stat: -rw-r--r-- 9,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * SPDX-FileCopyrightText: 2022 Kai Uwe Broulik <kde@broulik.de>
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "afcdevice.h"

#include "afc_debug.h"

#include "afcapp.h"
#include "afcspringboard.h"
#include "afcutils.h"

#include <QDir>
#include <QFileInfo>
#include <QSaveFile>
#include <QScopeGuard>
#include <QStandardPaths>

#include <libimobiledevice/installation_proxy.h>

using namespace KIO;

static const char s_lockdownLabel[] = "kio_afc";

AfcDevice::AfcDevice(const QString &id)
    : m_id(id)
{
    idevice_new(&m_device, id.toUtf8().constData());
    if (!m_device) {
        qCWarning(KIO_AFC_LOG) << "Failed to create idevice for" << id;
        return;
    }

    lockdownd_client_t lockdowndClient = nullptr;
    auto ret = lockdownd_client_new(m_device, &lockdowndClient, s_lockdownLabel);
    if (ret != LOCKDOWN_E_SUCCESS) {
        qCWarning(KIO_AFC_LOG) << "Failed to create lockdown client for" << id << ret;
        return;
    }

    ScopedLockdowndClientPtr lockdowndClientPtr(lockdowndClient);

    char *name = nullptr;
    auto lockdownRet = lockdownd_get_device_name(lockdowndClientPtr.data(), &name);
    if (lockdownRet != LOCKDOWN_E_SUCCESS) {
        qCWarning(KIO_AFC_LOG) << "Failed to get device name for" << id << lockdownRet;
    } else {
        m_name = QString::fromUtf8(name);
        free(name);
    }

    plist_t deviceClassEntry = nullptr;
    lockdownRet = lockdownd_get_value(lockdowndClientPtr.data(), nullptr /*global domain*/, "DeviceClass", &deviceClassEntry);
    if (lockdownRet != LOCKDOWN_E_SUCCESS) {
        qCWarning(KIO_AFC_LOG) << "Failed to get device class for" << id << lockdownRet;
    } else {
        char *deviceClass = nullptr;
        plist_get_string_val(deviceClassEntry, &deviceClass);
        m_deviceClass = QString::fromUtf8(deviceClass);
        free(deviceClass);
    }
}

AfcDevice::~AfcDevice()
{
    if (m_afcClient) {
        afc_client_free(m_afcClient);
        m_afcClient = nullptr;
    }

    if (m_device) {
        idevice_free(m_device);
        m_device = nullptr;
    }
}

idevice_t AfcDevice::device() const
{
    return m_device;
}

QString AfcDevice::id() const
{
    return m_id;
}

bool AfcDevice::isValid() const
{
    return m_device && !m_name.isEmpty();
}

QString AfcDevice::name() const
{
    return m_name;
}

QString AfcDevice::deviceClass() const
{
    return m_deviceClass;
}

QString AfcDevice::cacheLocation() const
{
    return QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/kio_afc/") + m_id;
}

QString AfcDevice::appIconCachePath(const QString &bundleId) const
{
    return cacheLocation() + QLatin1String("/%1.png").arg(bundleId);
}

WorkerResult AfcDevice::handshake()
{
    if (!m_handshakeSuccessful) {
        lockdownd_client_t lockdownClient = nullptr;
        // libimobiledevice doesn't properly allow doing a handshake on an existing instance
        // Instead, create a new one, and when it works, swap the one created in the constructor with this one
        auto ret = lockdownd_client_new_with_handshake(m_device, &lockdownClient, s_lockdownLabel);
        if (ret != LOCKDOWN_E_SUCCESS) {
            qCWarning(KIO_AFC_LOG) << "Failed to create lockdownd client with handshake on" << id() << "- make sure the device is unlocked" << ret;
            return AfcUtils::Result::from(ret);
        }

        m_lockdowndClient.reset(lockdownClient);
        m_handshakeSuccessful = true;
    }

    return WorkerResult::pass();
}

WorkerResult AfcDevice::client(const QString &appId, AfcClient::Ptr &client)
{
    auto result = handshake();
    if (!result.success()) {
        return result;
    }

    if (m_lastClient && m_lastClient->appId() == appId) {
        client = m_lastClient;
        return WorkerResult::pass();
    }

    Q_ASSERT(m_lockdowndClient);

    AfcClient::Ptr clientPtr(new AfcClient(this));
    result = clientPtr->init(m_lockdowndClient.data(), appId);
    if (!result.success()) {
        return result;
    }

    m_lastClient = clientPtr;
    client = clientPtr;
    return WorkerResult::pass();
}

AfcApp AfcDevice::app(const QString &bundleId)
{
    auto it = m_apps.constFind(bundleId);
    if (it != m_apps.constEnd()) {
        return *it;
    }

    // Refresh cache
    QList<AfcApp> appsList;
    if (!apps(appsList).success()) {
        return AfcApp();
    }

    // See if we know it now
    it = m_apps.constFind(bundleId);
    if (it != m_apps.constEnd()) {
        return *it;
    }

    return AfcApp();
}

WorkerResult AfcDevice::apps(QList<AfcApp> &apps)
{
    auto result = handshake();
    if (!result.success()) {
        return result;
    }

    lockdownd_service_descriptor_t service = nullptr;
    auto ret = lockdownd_start_service(m_lockdowndClient.data(), INSTPROXY_SERVICE_NAME, &service);
    if (ret != LOCKDOWN_E_SUCCESS) {
        qCWarning(KIO_AFC_LOG) << "Failed to start instproxy for getting apps" << ret;
        return AfcUtils::Result::from(ret, m_id);
    }

    auto serviceCleanup = qScopeGuard([service] {
        lockdownd_service_descriptor_free(service);
    });

    instproxy_client_t instProxyClient = nullptr;
    auto instRet = instproxy_client_new(m_device, service, &instProxyClient);
    if (instRet != INSTPROXY_E_SUCCESS) {
        qCWarning(KIO_AFC_LOG) << "Failed to create instproxy instance" << instRet;
        return AfcUtils::Result::from(instRet);
    }

    auto instProxyCleanup = qScopeGuard([instProxyClient] {
        instproxy_client_free(instProxyClient);
    });

    auto opts = instproxy_client_options_new();
    auto optsCleanup = qScopeGuard([opts] {
        instproxy_client_options_free(opts);
    });
    instproxy_client_options_add(opts, "ApplicationType", "User", nullptr);

    // Browse apps.
    plist_t appsPlist = nullptr;
    instRet = instproxy_browse(instProxyClient, opts, &appsPlist);
    if (instRet != INSTPROXY_E_SUCCESS) {
        qCWarning(KIO_AFC_LOG) << "Failed to browse apps via instproxy" << instRet;
        return AfcUtils::Result::from(instRet);
    }

    auto appsPlistCleanup = qScopeGuard([appsPlist] {
        plist_free(appsPlist);
    });

    m_apps.clear();
    apps.clear();

    const int count = plist_array_get_size(appsPlist);
    m_apps.reserve(count);
    apps.reserve(count);
    for (int i = 0; i < count; ++i) {
        plist_t appPlist = plist_array_get_item(appsPlist, i);
        AfcApp app(appPlist);
        if (!app.isValid()) {
            continue;
        }

        const QString iconPath = appIconCachePath(app.bundleId());
        if (QFileInfo::exists(iconPath)) {
            app.m_iconPath = iconPath;
        }

        m_apps.insert(app.bundleId(), app);
        apps.append(app);
    }

    return WorkerResult::pass();
}

WorkerResult AfcDevice::fetchAppIcon(AfcApp &app)
{
    QList<AfcApp> apps{app};

    const auto result = fetchAppIcons(apps);
    if (!result.success()) {
        return result;
    }

    app.m_iconPath = apps.first().m_iconPath;
    return result;
}

WorkerResult AfcDevice::fetchAppIcons(QList<AfcApp> &apps)
{
    QStringList appIconsToFetch;

    for (const AfcApp &app : std::as_const(apps)) {
        if (app.iconPath().isEmpty()) {
            appIconsToFetch.append(app.bundleId());
        }
    }

    if (appIconsToFetch.isEmpty()) {
        // Nothing to do.
        return WorkerResult::pass();
    }

    qCDebug(KIO_AFC_LOG) << "About to fetch app icons for" << appIconsToFetch;

    AfcSpringBoard springBoard(m_device, m_lockdowndClient.data());
    if (!springBoard.result().success()) {
        return springBoard.result();
    }

    QDir cacheDir(cacheLocation());
    if (!cacheDir.mkpath(QStringLiteral("."))) { // Returns true if it already exists.
        qCWarning(KIO_AFC_LOG) << "Failed to create icon cache directory" << cacheLocation();
        return WorkerResult::fail(ERR_CANNOT_MKDIR, cacheLocation());
    }

    WorkerResult result = WorkerResult::pass();
    for (const QString &bundleId : appIconsToFetch) {
        QByteArray data;

        const auto fetchIconResult = springBoard.fetchAppIconData(bundleId, data);
        if (!fetchIconResult.success()) {
            result = fetchIconResult;
            continue;
        }

        if (data.isEmpty()) {
            result = WorkerResult::fail(ERR_CANNOT_READ); // NO_CONTENT is "success, but no content"
            continue;
        }

        // Basic sanity check whether we got a PNG file.
        if (!data.startsWith(QByteArrayLiteral("\x89PNG\x0d\x0a\x1a\x0a"))) {
            qCWarning(KIO_AFC_LOG) << "Got bogus app icon data for" << bundleId << data.left(20) << "...";
            result = WorkerResult::fail(ERR_CANNOT_READ);
            continue;
        }

        const QString path = appIconCachePath(bundleId);

        QSaveFile iconFile(path);
        if (!iconFile.open(QIODevice::WriteOnly)) {
            qCWarning(KIO_AFC_LOG) << "Failed to open icon cache file for writing" << path << iconFile.errorString();
            result = WorkerResult::fail(ERR_CANNOT_OPEN_FOR_WRITING, path);
            continue;
        }

        iconFile.write(data);

        if (!iconFile.commit()) {
            qCWarning(KIO_AFC_LOG) << "Failed to save icon cache of size" << data.count() << "to" << path;
            result = WorkerResult::fail(ERR_CANNOT_WRITE, path);
            continue;
        }

        // Update internal cache.
        auto &app = m_apps[bundleId];
        app.m_iconPath = path;

        // Update app list argument.
        auto it = std::find_if(apps.begin(), apps.end(), [&bundleId](const AfcApp &app) {
            return app.bundleId() == bundleId;
        });
        Q_ASSERT(it != apps.end());
        it->m_iconPath = path;
    }

    return result;
}