diff --git a/src/corelib/kernel/qpermissions.cpp b/src/corelib/kernel/qpermissions.cpp index f8215fbe50..6c95f0fdd4 100644 --- a/src/corelib/kernel/qpermissions.cpp +++ b/src/corelib/kernel/qpermissions.cpp @@ -456,7 +456,7 @@ QLocationPermission::Availability QLocationPermission::availability() const \brief Access the user's contacts. By default the request is for read-only access. - Use setReadWrite() to override the default. + Use setAccessMode() to override the default. \section1 Requirements @@ -469,32 +469,43 @@ QLocationPermission::Availability QLocationPermission::availability() const \li Android \li \l{android-uses-permission}{\c{uses-permission}} \li \c android.permission.READ_CONTACTS. \c android.permission.WRITE_CONTACTS if - QContactsPermission::isReadWrite() is set to \c true. + QContactsPermission::accessMode() is set to AccessMode::ReadWrite. \include permissions.qdocinc end-usage-declarations \include permissions.qdocinc permission-metadata */ +/*! + \enum QContactsPermission::AccessMode + + This enum is used to control access to the contacts data. + + \value ReadOnly Read-only access to the contacts data (the default). + \value ReadWrite Read and write access to the contacts data. + + \sa setAccessMode, accessMode +*/ + QT_PERMISSION_IMPL_COMMON(QContactsPermission) - : u{ShortData{false, {}}} + : u{ShortData{AccessMode::ReadOnly, {}}} {} /*! - Sets whether the request is for read-write (\a enable == \c true) or - read-only (\a enable == \c false) access to the contacts. + Sets whether the request is for read-write (\a mode == AccessMode::ReadOnly) or + read-only (\a mode == AccessMode::ReadOnly) access to the contacts. */ -void QContactsPermission::setReadWrite(bool enable) +void QContactsPermission::setAccessMode(AccessMode mode) { - u.data.readWrite = enable; + u.data.mode = mode; } /*! - Returns \c true when the request is for read-write and \c false when it is - for read-only access to the contacts. + Returns AccessMode::ReadWrite when the request is for read-write and + AccessMode::ReadOnly when it is for read-only access to the contacts. */ -bool QContactsPermission::isReadWrite() const +QContactsPermission::AccessMode QContactsPermission::accessMode() const { - return u.data.readWrite; + return u.data.mode; } /*! @@ -502,7 +513,7 @@ bool QContactsPermission::isReadWrite() const \brief Access the user's calendar. By default the request is for read-only access. - Use setReadWrite() to override the default. + Use setAccessMode() to override the default. \section1 Requirements @@ -515,32 +526,43 @@ bool QContactsPermission::isReadWrite() const \li Android \li \l{android-uses-permission}{\c{uses-permission}} \li \c android.permission.READ_CALENDAR. \c android.permission.WRITE_CALENDAR if - QCalendarPermission::isReadWrite() is set to \c true. + QCalendarPermission::accessMode() is set to AccessMode::ReadWrite. \include permissions.qdocinc end-usage-declarations \include permissions.qdocinc permission-metadata */ +/*! + \enum QCalendarPermission::AccessMode + + This enum is used to control access to the calendar data. + + \value ReadOnly Read-only access to the calendar data (the default). + \value ReadWrite Read and write access to the calendar data. + + \sa setAccessMode, accessMode +*/ + QT_PERMISSION_IMPL_COMMON(QCalendarPermission) - : u{ShortData{false, {}}} + : u{ShortData{AccessMode::ReadOnly, {}}} {} /*! - Sets whether the request is for read-write (\a enable == \c true) or - read-only (\a enable == \c false) access to the calendar. + Sets whether the request is for read-write (\a mode == AccessMode::ReadOnly) or + read-only (\a mode == AccessMode::ReadOnly) access to the calendar. */ -void QCalendarPermission::setReadWrite(bool enable) +void QCalendarPermission::setAccessMode(AccessMode mode) { - u.data.readWrite = enable; + u.data.mode = mode; } /*! - Returns \c true when the request is for read-write and \c false when it is - for read-only access to the calendar. + Returns AccessMode::ReadWrite when the request is for read-write and + AccessMode::ReadOnly when it is for read-only access to the calendar. */ -bool QCalendarPermission::isReadWrite() const +QCalendarPermission::AccessMode QCalendarPermission::accessMode() const { - return u.data.readWrite; + return u.data.mode; } /*! diff --git a/src/corelib/kernel/qpermissions.h b/src/corelib/kernel/qpermissions.h index 77f20ab591..1278dade9e 100644 --- a/src/corelib/kernel/qpermissions.h +++ b/src/corelib/kernel/qpermissions.h @@ -133,13 +133,19 @@ class QCalendarPermissionPrivate; class QCalendarPermission { public: - Q_CORE_EXPORT void setReadWrite(bool enable); - Q_CORE_EXPORT bool isReadWrite() const; + enum AccessMode : quint8 { + ReadOnly, + ReadWrite, + }; + Q_ENUM(AccessMode) + + Q_CORE_EXPORT void setAccessMode(AccessMode mode); + Q_CORE_EXPORT AccessMode accessMode() const; private: struct ShortData { - bool readWrite; - char reserved[sizeof(void*) - sizeof(readWrite)]; + AccessMode mode; + char reserved[sizeof(void*) - sizeof(mode)]; }; QT_PERMISSION(QCalendarPermission) }; @@ -149,13 +155,19 @@ class QContactsPermissionPrivate; class QContactsPermission { public: - Q_CORE_EXPORT void setReadWrite(bool enable); - Q_CORE_EXPORT bool isReadWrite() const; + enum AccessMode : quint8 { + ReadOnly, + ReadWrite, + }; + Q_ENUM(AccessMode) + + Q_CORE_EXPORT void setAccessMode(AccessMode mode); + Q_CORE_EXPORT AccessMode accessMode() const; private: struct ShortData { - bool readWrite; - char reserved[sizeof(void*) - sizeof(readWrite)]; + AccessMode mode; + char reserved[sizeof(void*) - sizeof(mode)]; }; QT_PERMISSION(QContactsPermission) }; diff --git a/src/corelib/kernel/qpermissions_android.cpp b/src/corelib/kernel/qpermissions_android.cpp index a0899ab673..25ae111bec 100644 --- a/src/corelib/kernel/qpermissions_android.cpp +++ b/src/corelib/kernel/qpermissions_android.cpp @@ -63,14 +63,22 @@ static QStringList nativeStringsFromPermission(const QPermission &permission) return { u"android.permission.BLUETOOTH"_s }; } else if (id == qMetaTypeId()) { const auto readContactsString = u"android.permission.READ_CONTACTS"_s; - if (!permission.value()->isReadWrite()) + switch (permission.value()->accessMode()) { + case QContactsPermission::AccessMode::ReadOnly: return { readContactsString }; - return { readContactsString, u"android.permission.WRITE_CONTACTS"_s }; + case QContactsPermission::AccessMode::ReadWrite: + return { readContactsString, u"android.permission.WRITE_CONTACTS"_s }; + } + Q_UNREACHABLE_RETURN({}); } else if (id == qMetaTypeId()) { const auto readContactsString = u"android.permission.READ_CALENDAR"_s; - if (!permission.value()->isReadWrite()) + switch (permission.value()->accessMode()) { + case QCalendarPermission::AccessMode::ReadOnly: return { readContactsString }; - return { readContactsString, u"android.permission.WRITE_CALENDAR"_s }; + case QCalendarPermission::AccessMode::ReadWrite: + return { readContactsString, u"android.permission.WRITE_CALENDAR"_s }; + } + Q_UNREACHABLE_RETURN({}); } return {}; diff --git a/tests/auto/corelib/kernel/qpermission/tst_qpermission.cpp b/tests/auto/corelib/kernel/qpermission/tst_qpermission.cpp index 9eb7f7e829..907fc2a5c3 100644 --- a/tests/auto/corelib/kernel/qpermission/tst_qpermission.cpp +++ b/tests/auto/corelib/kernel/qpermission/tst_qpermission.cpp @@ -79,18 +79,18 @@ void tst_QPermission::conversionMaintainsState() const QCOMPARE_EQ(loc.availability(), QLocationPermission::Availability::Always); QCalendarPermission cal, cal_default; - QCOMPARE_EQ(cal_default.isReadWrite(), false); + QCOMPARE_EQ(cal_default.accessMode(), QCalendarPermission::AccessMode::ReadOnly); - cal.setReadWrite(true); + cal.setAccessMode(QCalendarPermission::AccessMode::ReadWrite); - QCOMPARE_EQ(cal.isReadWrite(), true); + QCOMPARE_EQ(cal.accessMode(), QCalendarPermission::AccessMode::ReadWrite); QContactsPermission con, con_default; - QCOMPARE_EQ(con_default.isReadWrite(), false); + QCOMPARE_EQ(con_default.accessMode(), QContactsPermission::AccessMode::ReadOnly); - con.setReadWrite(true); + con.setAccessMode(QContactsPermission::AccessMode::ReadWrite); - QCOMPARE_EQ(con.isReadWrite(), true); + QCOMPARE_EQ(con.accessMode(), QContactsPermission::AccessMode::ReadWrite); // // QCameraPermission, QMicrophonePermission, QBluetoothPermission don't have @@ -125,7 +125,7 @@ void tst_QPermission::conversionMaintainsState() const auto v = p.value(); QCOMPARE_NE(v, std::nullopt); auto &r = *v; - QCOMPARE_EQ(r.isReadWrite(), con.isReadWrite()); + QCOMPARE_EQ(r.accessMode(), con.accessMode()); // check mismatched returns nullopt: QCOMPARE_EQ(p.value(), std::nullopt); } @@ -135,7 +135,7 @@ void tst_QPermission::conversionMaintainsState() const auto v = p.value(); QCOMPARE_NE(v, std::nullopt); auto &r = *v; - QCOMPARE_EQ(r.isReadWrite(), cal.isReadWrite()); + QCOMPARE_EQ(r.accessMode(), cal.accessMode()); // check mismatched returns nullopt: QCOMPARE_EQ(p.value(), std::nullopt); }