diff --git a/components/mobileshell/quicksettings/quicksetting.cpp b/components/mobileshell/quicksettings/quicksetting.cpp index 4a8f125fe716e6fac8bc8ca882fd94692189a213..e9864d2fb61997f073a4c276b0bda268f1789c43 100644 --- a/components/mobileshell/quicksettings/quicksetting.cpp +++ b/components/mobileshell/quicksettings/quicksetting.cpp @@ -20,6 +20,15 @@ void QuickSetting::setEnabled(bool enabled) Q_EMIT enabledChanged(enabled); } +void QuickSetting::setAvailable(bool available) +{ + if (m_available == available) + return; + + m_available = available; + Q_EMIT availableChanged(available); +} + void QuickSetting::setSettingsCommand(const QString &settingsCommand) { if (m_settingsCommand == settingsCommand) diff --git a/components/mobileshell/quicksettings/quicksetting.h b/components/mobileshell/quicksettings/quicksetting.h index c29e9e4bf5e6e73979bd2bbe36949fce0f7d1874..20c52551a3394fc8c1bdf429427de957edfadbf9 100644 --- a/components/mobileshell/quicksettings/quicksetting.h +++ b/components/mobileshell/quicksettings/quicksetting.h @@ -18,6 +18,7 @@ class QuickSetting : public QObject Q_PROPERTY(QString icon READ iconName WRITE setIconName REQUIRED NOTIFY iconNameChanged) Q_PROPERTY(QString settingsCommand READ settingsCommand WRITE setSettingsCommand NOTIFY settingsCommandChanged) Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged) + Q_PROPERTY(bool available READ isAvailable WRITE setAvailable NOTIFY availableChanged) Q_PROPERTY(QQmlListProperty children READ children CONSTANT) Q_CLASSINFO("DefaultProperty", "children") QML_NAMED_ELEMENT("QuickSetting") @@ -44,16 +45,22 @@ public: { return m_enabled; } + bool isAvailable() const + { + return m_available; + } void setText(const QString &text); void setStatus(const QString &status); void setIconName(const QString &iconName); void setSettingsCommand(const QString &settingsCommand); void setEnabled(bool enabled); + void setAvailable(bool available); QQmlListProperty children(); Q_SIGNALS: void enabledChanged(bool enabled); + void availableChanged(bool available); void textChanged(const QString &text); void statusChanged(const QString &text); void iconNameChanged(const QString &icon); @@ -61,6 +68,7 @@ Q_SIGNALS: private: bool m_enabled = true; + bool m_available = true; QString m_text; QString m_status; QString m_iconName; diff --git a/components/mobileshell/quicksettings/quicksettingsmodel.cpp b/components/mobileshell/quicksettings/quicksettingsmodel.cpp index 1d0cba0b9c4ce2ece0c076645f8c757a690a2499..12f5910488d4c9e18919926560600472f8c74fab 100644 --- a/components/mobileshell/quicksettings/quicksettingsmodel.cpp +++ b/components/mobileshell/quicksettings/quicksettingsmodel.cpp @@ -91,7 +91,10 @@ void QuickSettingsModel::loadQuickSettings() } delete created; } else { - m_quickSettings.push_back(createdSetting); + if (createdSetting->isAvailable()) { + m_quickSettings.push_back(createdSetting); + } + connect(createdSetting, &QuickSetting::availableChanged, this, &QuickSettingsModel::availabilityChanged); } } @@ -99,3 +102,24 @@ void QuickSettingsModel::loadQuickSettings() endResetModel(); } + +void QuickSettingsModel::availabilityChanged() +{ + auto setting = qobject_cast(sender()); + + if (setting->isAvailable()) { + if (!m_quickSettings.contains(setting)) { + auto idx = m_quickSettings.count(); + beginInsertRows({}, idx, idx); + m_quickSettings.append(setting); + endInsertRows(); + } + } else { + auto idx = m_quickSettings.indexOf(setting); + if (idx >= 0) { + beginRemoveRows({}, idx, idx); + m_quickSettings.removeAt(idx); + endRemoveRows(); + } + } +} diff --git a/components/mobileshell/quicksettings/quicksettingsmodel.h b/components/mobileshell/quicksettings/quicksettingsmodel.h index 6e7ba9d9156155e23b6b49c35b82693fa81f4656..260f953ccef1205e04c0e0ed096c9a36e7b40b2f 100644 --- a/components/mobileshell/quicksettings/quicksettingsmodel.h +++ b/components/mobileshell/quicksettings/quicksettingsmodel.h @@ -37,6 +37,7 @@ public: private: void loadQuickSettings(); + void availabilityChanged(); bool m_loaded = false; QList m_quickSettings; diff --git a/quicksettings/flashlight/flashlightutil.cpp b/quicksettings/flashlight/flashlightutil.cpp index 3d04b6589220b0b16a62e2622b46dcae5cf836bb..bff7fc3d69f37a0ea2924eeca4ded50a57665be2 100644 --- a/quicksettings/flashlight/flashlightutil.cpp +++ b/quicksettings/flashlight/flashlightutil.cpp @@ -11,6 +11,10 @@ #include #include +#include + +// FIXME this is hardcoded to the PinePhone for now +static const char *FLASH_SYSFS_PATH = "/sys/devices/platform/led-controller/leds/white:flash/brightness"; FlashlightUtil::FlashlightUtil(QObject *parent) : QObject{parent} @@ -20,8 +24,6 @@ FlashlightUtil::FlashlightUtil(QObject *parent) void FlashlightUtil::toggleTorch() { - // FIXME this is hardcoded to the PinePhone for now - static auto FLASH_SYSFS_PATH = "/sys/devices/platform/led-controller/leds/white:flash/brightness"; int fd = open(FLASH_SYSFS_PATH, O_WRONLY); if (fd < 0) { @@ -39,3 +41,8 @@ bool FlashlightUtil::torchEnabled() const { return m_torchEnabled; } + +bool FlashlightUtil::isAvailable() const +{ + return QFileInfo::exists(FLASH_SYSFS_PATH); +} diff --git a/quicksettings/flashlight/flashlightutil.h b/quicksettings/flashlight/flashlightutil.h index f9c69af03fcef2f6b5aecc08bcf37f7660f04cdf..fa14102b2c5ee62f17757378cf56bcab1e660574 100644 --- a/quicksettings/flashlight/flashlightutil.h +++ b/quicksettings/flashlight/flashlightutil.h @@ -12,12 +12,14 @@ class FlashlightUtil : public QObject { Q_OBJECT Q_PROPERTY(bool torchEnabled READ torchEnabled NOTIFY torchChanged); + Q_PROPERTY(bool available READ isAvailable CONSTANT); public: FlashlightUtil(QObject *parent = nullptr); Q_INVOKABLE void toggleTorch(); bool torchEnabled() const; + bool isAvailable() const; Q_SIGNALS: void torchChanged(bool value); diff --git a/quicksettings/flashlight/package/contents/ui/main.qml b/quicksettings/flashlight/package/contents/ui/main.qml index 5acc0a9f05a10109874115b881fcd6d8cdeac677..aeb53ccfeac2b694917d43f0408bfbd1a38f1302 100644 --- a/quicksettings/flashlight/package/contents/ui/main.qml +++ b/quicksettings/flashlight/package/contents/ui/main.qml @@ -10,6 +10,7 @@ MobileShell.QuickSetting { text: i18n("Flashlight") icon: "flashlight-on" enabled: FlashlightUtil.torchEnabled + available: FlashlightUtil.available function toggle() { FlashlightUtil.toggleTorch() }