KDE/kdebase/workspace/plasma

Alex Merry kde at randomguy3.me.uk
Fri Aug 22 03:16:57 CEST 2008


SVN commit 850683 by alexmerry:

Update when batteries appear and disappear.

These really could do with some love (it was one of the earliest complex engine/applet pairs, and it shows), but working on them is a pain for me, as my laptop is old and decrepit.
Any laptop-weilding volunteers?
CCMAIL: plasma-devel at kde.org

BUG: 164660



 M  +25 -0     applets/battery/battery.cpp  
 M  +2 -0      applets/battery/battery.h  
 M  +58 -7     dataengines/powermanagement/powermanagementengine.cpp  
 M  +3 -1      dataengines/powermanagement/powermanagementengine.h  


--- trunk/KDE/kdebase/workspace/plasma/applets/battery/battery.cpp #850682:850683
@@ -580,6 +580,11 @@
     }
 
     dataEngine("powermanagement")->connectSource(I18N_NOOP("AC Adapter"), this);
+
+    connect(dataEngine("powermanagement"), SIGNAL(sourceAdded(QString)),
+            this,                          SLOT(sourceAdded(QString)));
+    connect(dataEngine("powermanagement"), SIGNAL(sourceRemoved(QString)),
+            this,                          SLOT(sourceRemoved(QString)));
 }
 
 void Battery::disconnectSources()
@@ -591,6 +596,26 @@
     }
 
     dataEngine("powermanagement")->disconnectSource(I18N_NOOP("AC Adapter"), this);
+
+    disconnect(SLOT(sourceAdded(QString)));
+    disconnect(SLOT(sourceRemoved(QString)));
 }
 
+void Battery::sourceAdded(const QString& source)
+{
+    if (source.startsWith("Battery") && source != "Battery") {
+        dataEngine("powermanagement")->connectSource(source, this);
+        m_numOfBattery++;
+    }
+}
+
+void Battery::sourceRemoved(const QString& source)
+{
+    if (m_batteries_data.contains(source)) {
+        m_batteries_data.remove(source);
+        m_numOfBattery--;
+        update();
+    }
+}
+
 #include "battery.moc"
--- trunk/KDE/kdebase/workspace/plasma/applets/battery/battery.h #850682:850683
@@ -67,6 +67,8 @@
         void animationUpdate(qreal progress);
         void acAnimationUpdate(qreal progress);
         void batteryAnimationUpdate(qreal progress);
+        void sourceAdded(const QString &source);
+        void sourceRemoved(const QString &source);
 
     private:
         Q_ENUMS( m_batteryStyle )
--- trunk/KDE/kdebase/workspace/plasma/dataengines/powermanagement/powermanagementengine.cpp #850682:850683
@@ -38,9 +38,9 @@
         , m_sources(0)
 {
     Q_UNUSED(args)
-        
+
     m_sources << I18N_NOOP("Battery") << I18N_NOOP("AC Adapter") << I18N_NOOP("Sleepstates");
-    
+
     // This following call can be removed, but if we do, the
     // data is not shown in the plasmaengineexplorer.
     // sourceRequestEvent("Battery");
@@ -51,11 +51,15 @@
 
 void PowermanagementEngine::init()
 {
+    connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceRemoved(QString)),
+            this,                              SLOT(deviceRemoved(QString)));
+    connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceAdded(QString)),
+            this,                              SLOT(deviceAdded(QString)));
 }
 
 QStringList PowermanagementEngine::sources() const 
 {
-    return m_sources;
+    return m_sources + m_batterySources.values();
 }
 
 bool PowermanagementEngine::sourceRequestEvent(const QString &name)
@@ -67,10 +71,10 @@
             setData(I18N_NOOP("Battery"), I18N_NOOP("has Battery"), false);
             return true;
         }
-        
+
         uint index = 0;
         QStringList battery_sources;
-        
+
         foreach (const Solid::Device &device_battery, list_battery) {
             const Solid::Battery* battery = device_battery.as<Solid::Battery>();
 
@@ -94,8 +98,8 @@
                 updateBatteryPlugState(battery->isPlugged(), device_battery.udi());
             }
         }
-        
-        if(battery_sources.count() > 0) {
+
+        if (battery_sources.count() > 0) {
             setData(I18N_NOOP("Battery"), I18N_NOOP("has Battery"), true);
             setData(I18N_NOOP("Battery"), I18N_NOOP("sources"), battery_sources);
         }
@@ -170,4 +174,51 @@
     scheduleSourcesUpdated();
 }
 
+void PowermanagementEngine::deviceRemoved(const QString& udi)
+{
+    if (m_batterySources.contains(udi)) {
+        const QString& source = m_batterySources[udi];
+        m_batterySources.remove(udi);
+        removeSource(source);
+
+        QStringList sourceNames(m_batterySources.values());
+        sourceNames.removeAll(source);
+        setData("Battery", "sources", sourceNames);
+    }
+}
+
+void PowermanagementEngine::deviceAdded(const QString& udi)
+{
+    Solid::Device device(udi);
+    if (device.isValid()) {
+        const Solid::Battery* battery = device.as<Solid::Battery>();
+
+        if (battery != 0) {
+            int index = 0;
+            QStringList sourceNames(m_batterySources.values());
+            while (sourceNames.contains(QString("Battery%1").arg(index))) {
+                index++;
+            }
+
+            QString source = QString("Battery%1").arg(index);
+            sourceNames << source;
+            m_batterySources[device.udi()] = source;
+
+            connect(battery, SIGNAL(chargeStateChanged(int, const QString &)), this,
+                    SLOT(updateBatteryChargeState(int, const QString &)));
+            connect(battery, SIGNAL(chargePercentChanged(int, const QString &)), this,
+                    SLOT(updateBatteryChargePercent(int, const QString &)));
+            connect(battery, SIGNAL(plugStateChanged(bool, const QString &)), this,
+                    SLOT(updateBatteryPlugState(bool, const QString &)));
+
+            // Set initial values
+            updateBatteryChargeState(battery->chargeState(), device.udi());
+            updateBatteryChargePercent(battery->chargePercent(), device.udi());
+            updateBatteryPlugState(battery->isPlugged(), device.udi());
+
+            setData("Battery", "sources", sourceNames);
+        }
+    }
+}
+
 #include "powermanagementengine.moc"
--- trunk/KDE/kdebase/workspace/plasma/dataengines/powermanagement/powermanagementengine.h #850682:850683
@@ -44,11 +44,13 @@
     bool sourceRequestEvent(const QString &name);
     void init();
 
-protected slots:
+private slots:
     void updateBatteryChargeState(int newState, const QString& udi);
     void updateBatteryPlugState(bool newState, const QString& udi);
     void updateBatteryChargePercent(int newValue, const QString& udi);
     void updateAcPlugState(bool newState);
+    void deviceRemoved(const QString& udi);
+    void deviceAdded(const QString& udi);
 
 private:
     Solid::AcAdapter* m_acadapter;


More information about the Plasma-devel mailing list