batteryplugin.cpp 2.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * Copyright 2013 Albert Vaca <albertvaka@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy
 * defined in Section 14 of version 3 of the license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

21
#include "batteryplugin.h"
22 23

#include <QDebug>
24 25
#include <KNotification>
#include <KIcon>
26

27
#include "batterydbusinterface.h"
28

29 30
K_PLUGIN_FACTORY( KdeConnectPluginFactory, registerPlugin< BatteryPlugin >(); )
K_EXPORT_PLUGIN( KdeConnectPluginFactory("kdeconnect_battery", "kdeconnect_battery") )
31

32 33
BatteryPlugin::BatteryPlugin(QObject *parent, const QVariantList &args)
    : KdeConnectPlugin(parent, args)
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
34
    , batteryDbusInterface(new BatteryDbusInterface(parent))
35
{
36

37 38 39 40
}

void BatteryPlugin::connected()
{
41 42 43
    NetworkPackage np(PACKAGE_TYPE_BATTERY);
    np.set("request",true);
    device()->sendPackage(np);
44 45
}

46
BatteryPlugin::~BatteryPlugin()
47
{
48
    //FIXME: Qt dbus does not allow to remove an adaptor! (it causes a crash in
49
    // the next dbus access to its parent). The implication of not deleting this
50 51 52 53 54 55
    // is that disabling the plugin does not remove the interface (that will
    // return outdated values) and that enabling it again instantiates a second
    // adaptor. This is a partial memory leak (the memory will be freed when the
    // device is destroyed anyway)

    //batteryDbusInterface->deleteLater();
56
}
57

58 59
bool BatteryPlugin::receivePackage(const NetworkPackage& np)
{
60

61
    if (np.type() != PACKAGE_TYPE_BATTERY) return false;
62

63 64
    bool isCharging = np.get<bool>("isCharging");
    int currentCharge = np.get<int>("currentCharge");
65

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
66 67
    if (batteryDbusInterface->isCharging() != currentCharge
        || batteryDbusInterface->isCharging() != isCharging) {
68

69
        batteryDbusInterface->updateValues(isCharging, currentCharge);
70

71
        if (currentCharge == 14 && !isCharging) {
72
            KNotification* notification = new KNotification("batteryLow");
73 74 75 76
            notification->setPixmap(KIcon("battery-040").pixmap(48, 48));
            notification->setComponentData(KComponentData("kdeconnect", "kdeconnect"));
            notification->setTitle(device()->name() + ": low battery");
            notification->setText("Battery at 14%");
77
            notification->sendEvent();
78
        }
79 80 81 82 83 84

    }

    return true;

}
85