Revert "Implemented per device resolution setting and selection"
[harmattan/cameraplus] / src / sailfish / batteryinfo.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012-2013 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "batteryinfo.h"
22 #include <QBatteryInfo>
23 #if defined(QT4)
24 #include <QDeclarativeInfo>
25 #elif defined(QT5)
26 #include <QQmlInfo>
27 #endif
28
29 BatteryInfo::BatteryInfo(QObject *parent) :
30   QObject(parent),
31   m_battery(0) {
32
33 }
34
35 BatteryInfo::~BatteryInfo() {
36   setActive(false);
37 }
38
39 bool BatteryInfo::isCharging() const {
40   if (!m_battery) {
41     qmlInfo(this) << "BatteryInfo has to be activated first";
42     return false;
43   }
44
45   QBatteryInfo::ChargingState state = m_battery->chargingState(0);
46   if (state == QBatteryInfo::Charging) {
47     return true;
48   }
49
50   return false;
51 }
52
53 bool BatteryInfo::isCritical() const {
54   if (!m_battery) {
55     qmlInfo(this) << "BatteryInfo has to be activated first";
56     return true;
57   }
58
59   QBatteryInfo::BatteryStatus state = m_battery->batteryStatus(0);
60   if (state == QBatteryInfo::BatteryOk || state == QBatteryInfo::BatteryFull) {
61     return false;
62   }
63
64   return true;
65 }
66
67 bool BatteryInfo::isActive() const {
68   return m_battery != 0;
69 }
70
71 void BatteryInfo::setActive(bool active) {
72   if (isActive() == active) {
73     return;
74   }
75
76   if (!active) {
77     m_battery->deleteLater();
78     m_battery = 0;
79   }
80   else {
81     m_battery = new QBatteryInfo(this);
82
83     QObject::connect(m_battery, SIGNAL(batteryStatusChanged(int, QBatteryInfo::BatteryStatus)),
84                      this, SIGNAL(chargingChanged()));
85     QObject::connect(m_battery, SIGNAL(chargingStateChanged(int, QBatteryInfo::ChargingState)),
86                      this, SIGNAL(chargingChanged()));
87   }
88
89   emit activeChanged();
90 }