summaryrefslogtreecommitdiffstats
path: root/tests/manual/examples/corelib/permissions/main.cpp
blob: 913aed2fec98e29516a920b61b9c11c19d160c9e (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
87
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include <QtCore/qmetaobject.h>
#include <QtWidgets/qapplication.h>
#include <QtWidgets/qwidget.h>
#include <QtWidgets/qpushbutton.h>
#include <QtWidgets/qlayout.h>
#include <QtWidgets/qmessagebox.h>

#if !QT_CONFIG(permissions)
#error "This example requires the permissions feature, which is not enabled on this platform"
#endif

#include <QtCore/qpermissions.h>

class PermissionWidget : public QWidget
{
    Q_OBJECT
public:
    explicit PermissionWidget(QWidget *parent = nullptr) : QWidget(parent)
    {
        QVBoxLayout *layout = new QVBoxLayout(this);

        static const QPermission permissions[] = {
            QCameraPermission{},
            QMicrophonePermission{},
            QBluetoothPermission{},
            QContactsPermission{},
            QCalendarPermission{},
            QLocationPermission{}
        };

        for (auto permission : permissions) {
            auto permissionName = QString::fromLatin1(permission.type().name());
            QPushButton *button = new QPushButton(permissionName.sliced(1, permissionName.length() - 11));
            connect(button, &QPushButton::clicked, this, &PermissionWidget::buttonClicked);
            button->setProperty("permission", QVariant::fromValue(permission));
            layout->addWidget(button);
        }

        QPalette pal = palette();
        pal.setBrush(QPalette::Window, QGradient(QGradient::HappyAcid));
        setPalette(pal);
    }

private:
    void buttonClicked()
    {
        auto *button = static_cast<QPushButton*>(sender());

        auto permission = button->property("permission").value<QPermission>();
        Q_ASSERT(permission.type().isValid());

        switch (qApp->checkPermission(permission)) {
        case Qt::PermissionStatus::Undetermined:
                qApp->requestPermission(permission, this,
                    [button](const QPermission &permission) {
                        Q_UNUSED(permission);
                        emit button->clicked(); // Try again
                    }
                );
            return;
        case Qt::PermissionStatus::Denied:
            QMessageBox::warning(this, button->text(),
                tr("Permission is needed to use %1. Please grant permission "\
                "to this application in the system settings.").arg(button->text()));
            return;
        case Qt::PermissionStatus::Granted:
            break; // Proceed
        }

        // All good, can use the feature
        QMessageBox::information(this, button->text(),
            tr("Accessing %1").arg(button->text()));
    }
};

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    PermissionWidget widget;
    widget.show();
    return app.exec();
}

#include "main.moc"