summaryrefslogtreecommitdiffstats
path: root/src/corelib/platform/darwin/qdarwinpermissionplugin_bluetooth.mm
blob: 0cd375561fbe4060e3489b58a120166c35caacb1 (plain)
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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qdarwinpermissionplugin_p_p.h"

#include <deque>

#include <CoreBluetooth/CoreBluetooth.h>

@interface QDarwinBluetoothPermissionHandler () <CBCentralManagerDelegate>
@property (nonatomic, retain) CBCentralManager *manager;
@end

@implementation QDarwinBluetoothPermissionHandler {
    std::deque<PermissionCallback> m_callbacks;
}

- (instancetype)init
{
    if ((self = [super init]))
        self.manager = nil;

    return self;
}

- (Qt::PermissionStatus)checkPermission:(QPermission)permission
{
    Q_UNUSED(permission);
    return [self currentStatus];
}

- (Qt::PermissionStatus)currentStatus
{
    auto status = CBCentralManager.authorization;
    switch (status) {
    case CBManagerAuthorizationNotDetermined:
        return Qt::PermissionStatus::Undetermined;
    case CBManagerAuthorizationRestricted:
    case CBManagerAuthorizationDenied:
        return Qt::PermissionStatus::Denied;
    case CBManagerAuthorizationAllowedAlways:
        return Qt::PermissionStatus::Granted;
    }

    qCWarning(lcPermissions) << "Unknown permission status" << status << "detected in" << self;
    return Qt::PermissionStatus::Denied;
}

- (void)requestPermission:(QPermission)permission withCallback:(PermissionCallback)callback
{
    m_callbacks.push_back(callback);
    if (!self.manager) {
        self.manager = [[[CBCentralManager alloc]
            initWithDelegate:self queue:dispatch_get_main_queue()] autorelease];
    }
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)manager
{
    Q_ASSERT(manager == self.manager);
    Q_ASSERT(!m_callbacks.empty());

    auto status = [self currentStatus];

    for (auto callback : m_callbacks)
        callback(status);

    m_callbacks = {};
    self.manager = nil;
}

- (QStringList)usageDescriptionsFor:(QPermission)permission
{
    Q_UNUSED(permission);
#ifdef Q_OS_MACOS
    if (QOperatingSystemVersion::current() > QOperatingSystemVersion::MacOSBigSur)
#endif
    {
        return { "NSBluetoothAlwaysUsageDescription" };
    }

    return {};
}
@end

#include "moc_qdarwinpermissionplugin_p_p.cpp"