diff --git a/system/wallpaper.cpp b/system/wallpaper.cpp index 5f36b95..a637f0d 100644 --- a/system/wallpaper.cpp +++ b/system/wallpaper.cpp @@ -1,37 +1,73 @@ #include "wallpaper.h" +#include + Wallpaper::Wallpaper(QObject *parent) : QObject(parent) - , m_interface("com.cutefish.Settings", - "/Theme", "com.cutefish.Theme", - QDBusConnection::sessionBus(), this) + , m_interface(nullptr) { - if (m_interface.isValid()) { - connect(&m_interface, SIGNAL(wallpaperChanged(QString)), this, SLOT(onPathChanged(QString))); - connect(&m_interface, SIGNAL(darkModeDimsWallpaerChanged()), this, SIGNAL(dimsWallpaperChanged())); - connect(&m_interface, SIGNAL(backgroundTypeChanged()), this, SIGNAL(typeChanged())); - connect(&m_interface, SIGNAL(backgroundColorChanged()), this, SIGNAL(colorChanged())); - } + // The shell starts before cutefish-settings, so the signals are hooked up + // again once the service appears; without this the desktop would keep the + // background it read at startup for the rest of the session. + QDBusServiceWatcher *watcher = new QDBusServiceWatcher(this); + watcher->setConnection(QDBusConnection::sessionBus()); + watcher->addWatchedService("com.cutefish.Settings"); + connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, &Wallpaper::init); + + init(); +} + +void Wallpaper::init() +{ + delete m_interface; + m_interface = new QDBusInterface("com.cutefish.Settings", + "/Theme", "com.cutefish.Theme", + QDBusConnection::sessionBus(), this); + + if (!m_interface->isValid()) + return; + + connect(m_interface, SIGNAL(wallpaperChanged(QString)), this, SLOT(onPathChanged(QString))); + connect(m_interface, SIGNAL(darkModeDimsWallpaerChanged()), this, SIGNAL(dimsWallpaperChanged())); + connect(m_interface, SIGNAL(backgroundTypeChanged()), this, SIGNAL(typeChanged())); + connect(m_interface, SIGNAL(backgroundColorChanged()), this, SIGNAL(colorChanged())); + connect(m_interface, SIGNAL(backgroundVisibleChanged()), this, SIGNAL(backgroundVisibleChanged())); + + emit typeChanged(); + emit pathChanged(); + emit colorChanged(); + emit dimsWallpaperChanged(); + emit backgroundVisibleChanged(); +} + +QVariant Wallpaper::themeProperty(const char *name) const +{ + return m_interface ? m_interface->property(name) : QVariant(); } int Wallpaper::type() const { - return m_interface.property("backgroundType").toInt(); + return themeProperty("backgroundType").toInt(); } QString Wallpaper::path() const { - return m_interface.property("wallpaper").toString(); + return themeProperty("wallpaper").toString(); } bool Wallpaper::dimsWallpaper() const { - return m_interface.property("darkModeDimsWallpaer").toBool(); + return themeProperty("darkModeDimsWallpaer").toBool(); } QString Wallpaper::color() const { - return m_interface.property("backgroundColor").toString(); + return themeProperty("backgroundColor").toString(); +} + +bool Wallpaper::backgroundVisible() const +{ + return themeProperty("backgroundVisible").toBool(); } void Wallpaper::onPathChanged(QString path) diff --git a/system/wallpaper.h b/system/wallpaper.h index c48502f..5902e77 100644 --- a/system/wallpaper.h +++ b/system/wallpaper.h @@ -4,6 +4,10 @@ #include #include +/** + * The desktop background as cutefish-settings publishes it over + * com.cutefish.Theme: an image (type 0) or a plain colour (type 1). + */ class Wallpaper : public QObject { Q_OBJECT @@ -11,6 +15,7 @@ class Wallpaper : public QObject Q_PROPERTY(QString path READ path NOTIFY pathChanged) Q_PROPERTY(bool dimsWallpaper READ dimsWallpaper NOTIFY dimsWallpaperChanged) Q_PROPERTY(QString color READ color NOTIFY colorChanged) + Q_PROPERTY(bool backgroundVisible READ backgroundVisible NOTIFY backgroundVisibleChanged) public: explicit Wallpaper(QObject *parent = nullptr); @@ -21,19 +26,26 @@ public: bool dimsWallpaper() const; QString color() const; + bool backgroundVisible() const; signals: void pathChanged(); void dimsWallpaperChanged(); void typeChanged(); void colorChanged(); + void backgroundVisibleChanged(); + +private: + QVariant themeProperty(const char *name) const; private slots: + void init(); void onPathChanged(QString path); private: - QDBusInterface m_interface; - QString m_wallpaper; + // Recreated in init(): a QDBusInterface built while cutefish-settings is + // not on the bus caches a failed introspection and never recovers. + QDBusInterface *m_interface; }; #endif // WALLPAPER_H