summaryrefslogtreecommitdiffstats
path: root/src/hardware_batteryCollection.cpp
blob: 523ca6f58606159f098a957cbb32fe487ac7b222 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
 /**************************************************************************
 *   Copyright (C) 2006-2007 by Danny Kukawka                              *
 *                           <dkukawka@suse.de>, <danny.kukawka@web.de>    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License     *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

/*! 
 * \file 	hardware_batteryCollection.cpp
 * \brief 	In this file can be found the Battery Collection related code.
 * \author 	Danny Kukawka, <dkukawka@suse.de>, <danny.kukawka@web.de>
 * \date    	2006-2007
 */

// own headers
#include "hardware_batteryCollection.h"

/*! The default constructor of the class BatteryCollection. */
BatteryCollection::BatteryCollection( int _type ) {
	kdDebugFuncIn(trace);

	initDefault();
	type = _type;

	kdDebugFuncOut(trace);
}


/*! The default destructor of the class BatteryCollection. */
BatteryCollection::~BatteryCollection( ) {
	kdDebugFuncIn(trace);

}

//! init a battery with default values
void BatteryCollection::initDefault() {
	kdDebugFuncIn(trace);

	udis.clear();
	
	present_rate_unit = "mWh";

	charging_state = UNKNOWN_STATE;
	state = BAT_NORM;
	remaining_percent = -1;
	remaining_minutes = -1;
	present_rate = 0;
	
	warn_level = 12;
	low_level = 7;
	crit_level = 2;

	kdDebugFuncOut(trace);
}

/*!
 * This function refresh the information of the collection from the given 
 * batterylist.
 * \param BatteryList		TQPtrList with battery objects
 * \param force_level_recheck	boolean with info if the the check for the current 
 *				battery warning level should get forced
 */ 
bool BatteryCollection::refreshInfo(TQPtrList<Battery> BatteryList, bool force_level_recheck) {
	kdDebugFuncIn(trace);

	int _charging_state = UNKNOWN_STATE;
	int _percent = 0;
	int _minutes = 0;
	int _present_batteries = 0;
	int _present_rate = 0;

	// for now: clean list before run update process!
	udis.clear();

	if (!BatteryList.isEmpty()) {
		Battery *bat;
		for (bat = BatteryList.first(); bat; bat = BatteryList.next() ) {
			if (type == bat->getType()) {
				udis.append(bat->getUdi());

				if (bat->isPresent()) {
					// count present batteries
					_present_batteries++;
				
					// handle charging state
					if (bat->getChargingState() != _charging_state) {
						if (_charging_state == UNKNOWN_STATE) {
							_charging_state = bat->getChargingState();
						} else if ( bat->getChargingState() == UNKNOWN_STATE) {
							kdWarning()  << "found battery with unknown state,"
								     << " do nothing" << endl;
						} else {
							
							if (_charging_state != bat->getChargingState()) {
								// This should only happen if one is in 
								// state CHARGING and the other in DISCHARGING
								kdWarning() << "Unexpected chargingstates" << endl;
								_charging_state = UNKNOWN_STATE;
							}
						}
					} 

					// handle remaining percentage
					if (bat->getPercentage() >= 0) {
						_percent = (_percent + bat->getPercentage()) / _present_batteries;
					}
					
					if (bat->getRemainingMinutes() >= 0) {
						_minutes += bat->getRemainingMinutes();
					}
					
					if (bat->getPresentRate() >= 0) {
						_present_rate += bat->getPresentRate();
					}
					
					if (!bat->getChargelevelUnit().isEmpty()) {
						present_rate_unit = bat->getChargelevelUnit();
					}
				}
			}
		}

		bool _changed = false;

		if (_charging_state != charging_state) {
			charging_state = _charging_state;
			_changed = true;
			emit batteryChargingStateChanged (charging_state);
		}
		if (_percent != remaining_percent || force_level_recheck) {
			remaining_percent = _percent;

			if (_present_batteries < 1) {
				/* there are no batteries present, we don't need to emit
				   a event, there is nothing ... */
				state = BAT_NONE;
			}else if (remaining_percent <= crit_level) {
				if (state != BAT_CRIT) {
					state = BAT_CRIT;
					emit batteryWarnState( type, BAT_CRIT );
				}
			} else if (remaining_percent <= low_level) {
				if (state != BAT_LOW) {
					state = BAT_LOW;
					emit batteryWarnState( type, BAT_LOW );
				}
			} else if (remaining_percent <= warn_level) {
				if (state != BAT_WARN) {
					state = BAT_WARN;
					emit batteryWarnState( type, BAT_WARN );
				}
			} else if (state != BAT_NONE) {
				if (state != BAT_NORM) {
					state = BAT_NORM;
					emit batteryWarnState( type, BAT_NORM );
				}
			} else {
				state = BAT_NONE;
			}

			_changed = true;
			emit batteryPercentageChanged (remaining_percent );
		}
		if (_minutes != remaining_minutes) {
			remaining_minutes = _minutes;
			_changed = true;
			emit batteryMinutesChanged( remaining_minutes );
		}
		if (_present_batteries != present_batteries) {
			present_batteries = _present_batteries;
			_changed = true;
			emit batteryPresentChanged ( present_batteries );
		}
		if (_present_rate != present_rate ) {
			present_rate = _present_rate;
			// don't set to changed, this avoid useless calls
			emit batteryRateChanged ();
		}

		if (_changed) 
			emit batteryChanged();

		kdDebugFuncOut(trace);
		return true;
	} else {
		kdError() << "Could not refresh battery information, BatteryList was empty" << endl;
		initDefault();
		kdDebugFuncOut(trace);
		return false;
	}
}

//! check if the given udi is already handled by this collection
bool BatteryCollection::isBatteryHandled( TQString udi ) {
	return udis.contains( udi );
}

// ---> write private members SECTION : START <----
//! get the unit for charge level stuff
TQString BatteryCollection::getChargeLevelUnit() const {
	return present_rate_unit;
}

//! get the current reported battery rate
int BatteryCollection::getCurrentRate() const {
	return present_rate;
}

//! get the cumulative remaining time
int BatteryCollection::getRemainingMinutes() const {
	return remaining_minutes;
}

//! get the cumulative remaining percentage of the battery capacity
int BatteryCollection::getRemainingPercent() const {
	return remaining_percent;
}

//! get the current Charging state of the machine
int BatteryCollection::getChargingState() const {
	return charging_state;
}

//! get the current battery state for this collection
int BatteryCollection::getBatteryState() const {
	return state;
}

//! get the number of available batteries 
int BatteryCollection::getNumBatteries() const {
	return udis.count();
}

//! get the number of present batteries, represent \ref present_batteries
int BatteryCollection::getNumPresentBatteries() const {
	return present_batteries;
}

//! get the battery Type from enum \ref BAT_TYPE 
int BatteryCollection::getBatteryType() const {
	return type;
}

//! sets the chargelevel in percent when battery should go into state warning
/*!
 * \return boolean with result of the operation
 * \retval true  	if successful
 * \retval false 	else, if a error occurs
 */
bool BatteryCollection::setWarnLevel(int _warn_level) {
	kdDebugFuncIn(trace);

	if (_warn_level < low_level) {
		kdError() << "Refuse: " << _warn_level 
			  << " as it is smaller than the LowLevel: " << low_level << endl;
		kdDebugFuncOut(trace);
		return false;
	} else {
		warn_level = _warn_level;
		kdDebugFuncOut(trace);
		return true;
	}
}

//! sets the chargelevel in percent when battery should go into state low
/*!
 * \return boolean with result of the operation
 * \retval true  	if successful
 * \retval false 	else, if a error occurs
 */
bool BatteryCollection::setLowLevel(int _low_level) {
	kdDebugFuncIn(trace);

	if (_low_level < crit_level || _low_level > warn_level) {
		kdError() << "Refuses: " << _low_level
 			  << " as it is not between WarnLevel: " << warn_level
			  << " and CritLevel: " << crit_level << endl; 
		kdDebugFuncOut(trace);
		return false;
	} else {
		low_level = _low_level;
		kdDebugFuncOut(trace);
		return true;
	}
}

//! sets the chargelevel in percent when battery should go into state critical
/*!
 * \return boolean with result of the operation
 * \retval true  	if successful
 * \retval false 	else, if a error occurs
 */
bool BatteryCollection::setCritLevel(int _crit_level) {
	kdDebugFuncIn(trace);

	if (_crit_level > low_level) {
		kdError() << "Refuses " << _crit_level 
			  << " as it is bigger than LowLevel: " << low_level << endl;
		kdDebugFuncOut(trace);
		return false;
	} else {
		crit_level = _crit_level;
		kdDebugFuncOut(trace);
		return true;
	}
}

// ---> write private members SECTION : END <----
// ---> get private members SECTION : START <----

//! reports the chargelevel in percent when battery goes to state warning
int BatteryCollection::getWarnLevel() const {
	return warn_level;
}

//! reports the chargelevel in percent when battery goes to state low
int BatteryCollection::getLowLevel() const {
	return low_level;
}

//! reports the chargelevel in percent when battery goes to state critical
int BatteryCollection::getCritLevel() const {
	return crit_level;
}

// ---> get private members SECTION : END <----

#include "hardware_batteryCollection.moc"