permissions: Remove need to check QT_CONFIG(permissions) in user code

The idea was to avoid having to define return values for platforms
that did not have a permission backend (yet), but the approach does
not work for QML, and was a bit awkward in C++ as well.

We now return Qt::PermissionStatus::Granted on all platforms without
a permission backend, which allows the user code to have a single
code path for checking and requesting permissions. Importantly, that
code path will not be any different now that we always grant the
permission then what it was when the user ifdefed out the permission
check via QT_CONFIG(permissions).

Task-number: QTBUG-90498
Change-Id: I564a24ccfd6b335cb90b7a621778fba61d53a56c
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Tor Arne Vestbø 2023-05-23 17:02:02 +02:00
parent cbc239b52b
commit a1b1a30f8a
2 changed files with 23 additions and 8 deletions

View File

@ -1016,7 +1016,6 @@ qt_feature("permissions" PUBLIC
SECTION "Utilities"
LABEL "Application permissions"
PURPOSE "Provides support for requesting user permission to access restricted data or APIs"
CONDITION APPLE OR ANDROID OR WASM
)
qt_configure_add_summary_section(NAME "Qt Core")

View File

@ -46,7 +46,6 @@ Q_LOGGING_CATEGORY(lcPermissions, "qt.permissions", QtWarningMsg);
\code
void VoiceMemoWidget::onRecordingInitiated()
{
#if QT_CONFIG(permissions)
QMicrophonePermission microphonePermission;
switch (qApp->checkPermission(microphonePermission)) {
case Qt::PermissionStatus::Undetermined:
@ -57,10 +56,8 @@ Q_LOGGING_CATEGORY(lcPermissions, "qt.permissions", QtWarningMsg);
m_permissionInstructionsDialog->show();
return;
case Qt::PermissionStatus::Granted:
break; // Proceed
m_microphone->startRecording();
}
#endif
m_microphone->startRecording();
}
\endcode
@ -76,9 +73,6 @@ Q_LOGGING_CATEGORY(lcPermissions, "qt.permissions", QtWarningMsg);
why we can not record voice memos at this time (if the permission was denied),
or proceed to using the microphone (if permission was granted).
The use of the \c{QT_CONFIG(permissions)} macro ensures that the code
will work as before on platforms where permissions are not available.
\note On \macOS and iOS permissions can currently only be requested for
GUI applications.
@ -672,6 +666,28 @@ QDebug operator<<(QDebug debug, const QPermission &permission)
#undef QT_PERMISSION_IMPL_COMMON
#if !defined(Q_OS_DARWIN) && !defined(Q_OS_ANDROID) && !defined(Q_OS_WASM)
// Default backend for platforms without a permission implementation.
// Always returns Granted, to match behavior when not using permission APIs
// https://bugreports.qt.io/browse/QTBUG-90498?focusedCommentId=725085#comment-725085
namespace QPermissions::Private
{
Qt::PermissionStatus checkPermission(const QPermission &permission)
{
qCDebug(lcPermissions) << "No permission backend on this platform."
<< "Optimistically returning Granted for" << permission;
return Qt::PermissionStatus::Granted;
}
void requestPermission(const QPermission &permission, const PermissionCallback &callback)
{
qCDebug(lcPermissions) << "No permission backend on this platform."
<< "Optimistically returning Granted for" << permission;
callback(Qt::PermissionStatus::Granted);
}
}
#endif
QT_END_NAMESPACE
#include "moc_qpermissions.cpp"