summaryrefslogtreecommitdiffstats
path: root/src/hardware_battery.cpp
blob: b9809e8f8210f3d92e3f2a50c858a50d8c7f53b1 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
 /**************************************************************************
 *   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_battery.cpp
 * \brief 	In this file can be found the Battery related code.
 * \author 	Danny Kukawka, <dkukawka@suse.de>, <danny.kukawka@web.de>
 * \date    	2006-2007
 */

#include "hardware_battery.h"

/*! The default constructor of the class Battery. */
Battery::Battery(dbusHAL* _dbus_HAL) : dbus_HAL(_dbus_HAL) {
	kdDebugFuncIn(trace);

	udi = TQString();
	initialized = false;
	
	initDefault();
	init(NULL);

	kdDebugFuncOut(trace);
}	

/*! The default constructor of the class Battery. */ 
Battery::Battery( dbusHAL* _dbus_HAL, TQString _udi ) : dbus_HAL(_dbus_HAL), udi(_udi) {
	if (trace) kdDebug() << funcinfo << "IN , udi: " << udi << endl;
	
	initialized = false;

	initDefault();
	init(NULL);

	kdDebugFuncOut(trace);
}

/*! The default constructor of the class Battery. */
Battery::Battery() {
	kdDebugFuncIn(trace);
	initialized = false;

	initDefault();
	udi = TQString();

	kdDebugFuncOut(trace);
}

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

}

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

	present = false;
	type = BAT_UNKNOWN;
	state = BAT_NORM;
	capacity_state = "ok";
	charging_state = UNKNOWN_STATE;
	charge_level_unit = "mWh";
	charge_level_current = 0;
	charge_level_lastfull = 0;
	charge_level_percentage = 0;
	design_capacity = 0;
	present_rate = 0;
	remaining_minutes = 0;
	serial = "";
	
	warn_level = 12;
	low_level = 7;
	crit_level = 2;
	
	kdDebugFuncOut(trace);
}

//! initialize this battery object with values from HAL
void Battery::init(dbusHAL* _dbus_HAL) {
	kdDebugFuncIn(trace);

	if (_dbus_HAL != NULL)
		dbus_HAL = _dbus_HAL;

	// read battery information from HAL
	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		//couldnt connect to HAL
		state=BAT_HAL_ERROR;
	} else {
		if (resetUdi(udi)) {
			//udi is valid
			recheck(); //fills all values
			//ready here for now
		} else {
			//udi is invalid or is no battery
			state=BAT_HAL_ERROR;
			kdWarning() << "Warning: Battery::init cannot make use of udi " << udi << endl;
		}
	}

	initialized = true;
	kdDebugFuncOut(trace);
}

//! rechecks only minimalistic set properties
/*!
* Will only recheck properties regarding current battery states/levels.
*/
void Battery::minRecheck() {
	kdDebugFuncIn(trace);

	//first check whether HAL is available
	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		//grr... no luck
		kdError() << "Battery::recheck couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return;
	}

	checkBatteryPresent();
	if (!present) {
		kdDebug() << "No need to update other properties, battery not present." << endl;
		kdDebugFuncOut(trace);
		return;
	} else {
		checkCapacityState();
		checkChargeLevelCurrent();
		checkRemainingPercentage();	
		checkChargingState();
		checkChargeLevelRate();
		checkRemainingTime();
	}

	kdDebugFuncOut(trace);
}

//! recheck all properties of the battery
/*!
 * Check and set the properties of a battery or collect the information for all
 * primary batteries in one object.
 */
void Battery::recheck() {
	kdDebugFuncIn(trace);

	//first check whether HAL is available
	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		//grr... no luck
		kdError() << "Battery::recheck couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return;
	}

	checkBatteryPresent();
	checkBatteryType();
	if (!present) {
		kdDebug() << "Warning: No need to update properties, battery not present." << endl;
		kdDebugFuncOut(trace);
		return;
	} else {
		checkBatteryTechnology();
		checkCapacityState();
		checkChargeLevelCurrent();
		checkChargeLevelLastfull();
		checkRemainingPercentage();	
		checkChargingState();
		checkChargeLevelUnit();
		checkChargeLevelDesign();
		checkChargeLevelRate();
		checkRemainingTime();
	}

	kdDebugFuncOut(trace);
}

// ---> query HAL for properties SECTION : START <----

//! to check battery.present
/*!
 * function to update \ref present from property battery.present
 * \return boolean with the result of the operation
 * \retval true 	if the update was successfull
 * \retval false 	if the update couldn't be applied
 */
bool Battery::checkBatteryPresent () {
	kdDebugFuncIn(trace);

	bool _present = false;
	
	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		kdError() << "Couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (dbus_HAL->halGetPropertyBool(udi,"battery.present", &_present)) {
		if (_present != present) {
			present = _present;
			
			// force depending changes
			if (_present) {
				// should be now present
				recheck();
			}
			if (!_present) {
				//Uhhh, battery is not present, so we need to reset battery to default.
				initDefault();
				checkBatteryType();
				state = BAT_NONE;
			}

			if (initialized) {
				emit changedBatteryPresent();
				emit changedBattery();
			}
		}

		// also query the serial ...  no need to do this in a extra place
		dbus_HAL->halGetPropertyString(udi, "battery.serial", &serial);

	} else {
		//query was not successfull
		kdDebug() << "Query of battery.present of " << udi << " wasn't successfull." << endl;
		//..but we assume its there try on
		present = true;
	}
	
	kdDebugFuncOut(trace);
	return true;
}

//! to check battery.type
/*!
 * function to update \ref type from property battery.type
 * \return boolean with the result of the operation
 * \retval true 	if the update was successfull
 * \retval false 	if the update couldn't be applied
 */
bool Battery::checkBatteryType () {
	kdDebugFuncIn(trace);
	
	TQString tmp_qstring;

	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		kdError() << "Couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (dbus_HAL->halGetPropertyString(udi,"battery.type", &tmp_qstring)) {
		if (tmp_qstring.compare("primary") == 0) {
			type = BAT_PRIMARY;
		} else if (tmp_qstring.compare("mouse") == 0) {
			type = BAT_MOUSE;
		} else if (tmp_qstring.compare("keyboard") == 0) {
			type = BAT_KEYBOARD;
		} else if (tmp_qstring.compare("keyboard_mouse") == 0) {
			type = BAT_KEY_MOUSE;
		} else if (tmp_qstring.compare("camera") == 0) {
			type = BAT_CAMERA;
		} else if (tmp_qstring.compare("ups") == 0) {
			type = BAT_UPS;
		} else {
			//anything else will currently be "UNKNOWN"
			type = BAT_UNKNOWN;
		}
		kdDebugFuncOut(trace);
		return true;
	} else {
		//query was not successfull
		kdWarning() << "Query of battery.type of " << udi << " was not successfull." << endl;
		type = BAT_UNKNOWN;
		kdDebugFuncOut(trace);
		return false;
	}
}

//! to check battery.technology
/*!
 * function to update \ref technology from property battery.technology
 * \return boolean with the result of the operation
 * \retval true 	if the update was successfull
 * \retval false 	if the update couldn't be applied
 */
bool Battery::checkBatteryTechnology () {
	kdDebugFuncIn(trace);
	
	TQString tmp_qstring;
	
	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		kdError() << "Couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (!present) {
		kdWarning() << "No need to update property, battery not present." << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (dbus_HAL->halGetPropertyString(udi,"battery.technology", &tmp_qstring)) {
		if (!tmp_qstring.isEmpty()) {
			technology = TQString(tmp_qstring);
		} else {
			technology = TQString("UNKNOWN");
		}
		kdDebugFuncOut(trace);
		return true;
	} else {
		//query was not successfull, but this property is optional
		technology = TQString("UNKNOWN");
		kdDebugFuncOut(trace);
		return false;
	}
}


//! to check battery.charge_level.capacity_state
/*!
 * function to update \ref capacity_state from battery.charge_level.capacity_state
 * \return boolean with the result of the operation
 * \retval true 	if the update was successfull
 * \retval false 	if the update couldn't be applied
 */
bool Battery::checkCapacityState () {
	kdDebugFuncIn(trace);

	TQString tmp_qstring;
	
	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		kdError() << "Couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (!present) {
		kdWarning() << "No need to update property, battery not present." << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (dbus_HAL->halGetPropertyString(udi, "battery.charge_level.capacity_state", &tmp_qstring)) {
		capacity_state = TQString(tmp_qstring);
		kdDebugFuncOut(trace);
		return true;
	} else {
		//query was not successfull, but this property is optional
		capacity_state = TQString();
		kdDebugFuncOut(trace);
		return false;
	}
}


//! to check battery.charge_level.current
/*!
 * function to update \ref charge_level_current from property battery.charge_level.current
 * \return boolean with the result of the operation
 * \retval true 	if the update was successfull
 * \retval false 	if the update couldn't be applied
 */
bool Battery::checkChargeLevelCurrent () {
	kdDebugFuncIn(trace);
	
	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		kdError() << "Couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (!present) {
		kdWarning() << "No need to update property, battery not present." << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (dbus_HAL->halGetPropertyInt(udi, "battery.charge_level.current", &charge_level_current)) {
		if (charge_level_current < 0) {
			//overflow?
			charge_level_current = 0;
		}
		kdDebugFuncOut(trace);
		return true;
	} else {
		//query was not successfull (is mandatory)
		kdError() << "Couldn't request charge_level.current for udi: " << udi << endl;
		state = BAT_NONE;
		kdDebugFuncOut(trace);
		return false;
	}
}


//! to check battery.charge_level.last_full
/*!
 * function to update \ref charge_level_lastfull from property battery.charge_level.last_full
 * \return boolean with the result of the operation
 * \retval true 	if the update was successfull
 * \retval false 	if the update couldn't be applied
 */
bool Battery::checkChargeLevelLastfull () {
	kdDebugFuncIn(trace);
	
	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		kdError() << "Couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (!present) {
		kdWarning() << "No need to update property, battery not present." << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (dbus_HAL->halGetPropertyInt(udi, "battery.charge_level.last_full", &charge_level_lastfull)) {
		if (charge_level_lastfull < charge_level_current ) {
			//possible overflow?
			charge_level_lastfull = charge_level_current;
		}
		kdDebugFuncOut(trace);
		return true;
	} else {
		//query was not successfull (is mandatory)
		kdError() << "couldn't query last_full of udi: " << udi << endl;
		charge_level_lastfull = 0; // set back to 0
		kdDebugFuncOut(trace);
		return false;
	}
}

//! to check battery.charge_level.rate
/*!
 * function to update \ref present_rate from property battery.charge_level.rate
 * \return boolean with the result of the operation
 * \retval true 	if the update was successfull
 * \retval false 	if the update couldn't be applied
 */
bool Battery::checkChargeLevelRate () {
	kdDebugFuncIn(trace);
	
	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		kdError()  << "Couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (!present) {
		kdWarning() << "No need to update property, battery not present." << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	int _rate = present_rate;

	if (dbus_HAL->halGetPropertyInt(udi, "battery.charge_level.rate", &present_rate)) {
		if (present_rate < 0 )
			present_rate = 0;

		if (present_rate != _rate)
			emit changedBattery();

		kdDebugFuncOut(trace);
		return true;
	} else {
		//query was not successfull but this is optional
		kdError() << "Couldn't request charge_level.rate for udi: " <<  udi << endl;
		present_rate = 0;
		kdDebugFuncOut(trace);
		return false;
	}
}


//! to check battery.charge_level.unit
/*!
 * function to update \ref charge_level_unit from property battery.charge_level.unit
 * \return boolean with the result of the operation
 * \retval true 	if the update was successfull
 * \retval false 	if the update couldn't be applied
 */
bool Battery::checkChargeLevelUnit () {
	kdDebugFuncOut(trace);
	
	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		kdError() << "Couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (!present) {
		kdWarning() << "No need to update property, battery not present." << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (!dbus_HAL->halGetPropertyString(udi, "battery.charge_level.unit", &charge_level_unit)) {
		//query was not successfull but this is optional
		kdWarning() << "Couldn't request charge_level.unit for udi: " << udi << endl;
		kdDebugFuncOut(trace);
		return false;
	} else {
		kdDebugFuncOut(trace);
		return true;
	}
}

//! to check battery.charge_level.design
/*!
 * function to update \ref design_capacity from property battery.charge_level.design
 * \return boolean with the result of the operation
 * \retval true 	if the update was successfull
 * \retval false 	if the update couldn't be applied
 */
bool Battery::checkChargeLevelDesign () {
	kdDebugFuncIn(trace);
	
	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		kdError() << "Couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (!present) {
		kdWarning() << "No need to update property, battery not present." << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (dbus_HAL->halGetPropertyInt(udi, "battery.charge_level.design", &design_capacity)) {
		if (design_capacity < 0)
			design_capacity = 0;

		kdDebugFuncOut(trace);
		return true;
	} else {
		//query was not successfull (is mandatory but not critical)
		kdWarning() << "Couldn't request charge_level.design for udi: " << udi << endl;
		kdDebugFuncOut(trace);
		return false;
	}
}


//! to check battery.charge_level.percentage
/*!
 * function to update \ref charge_level_percentage from property battery.charge_level.percentage
 * \return boolean with the result of the operation
 * \retval true 	if the update was successfull
 * \retval false 	if the update couldn't be applied
 */
bool Battery::checkRemainingPercentage () {
	kdDebugFuncIn(trace);
	
	bool ret = false;
	int _val = 0;
	int _state = -1;

	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		kdError() << "Couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (!present) {
		kdWarning() << "No need to update property, battery not present." << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (dbus_HAL->halGetPropertyInt(udi, "battery.charge_level.percentage", &_val)) {
		if (_val > 100) {
			_val = 100;
		} else if (_val < 0) {
			_val = 0;
		}
		ret = true;
	} else {
		//query was not successfull, but this property is optional
		//so we calculate it from charge_level
		if (charge_level_current > 0) {
			_val = (int)((float)(charge_level_current * 100) / (float)charge_level_lastfull);
			ret = true;
		} else {
			kdError() << "Couldn't query percentage of udi: " << udi 
				  << ". and charge_level_current >= 0" << endl;
		}
	}

	if (charge_level_percentage != _val) {
		if (initialized) {
			emit changedBatteryPercentage();
			emit changedBattery();
		}
		charge_level_percentage = _val;
	}

	//battery state
	if (charge_level_percentage <= crit_level) {
		//we are now in critical level
		_state = BAT_CRIT;
	} else if (charge_level_percentage <= low_level) {
		//we are now in a low level
		_state = BAT_LOW;
	} else if (charge_level_percentage <= warn_level) {
		//we are now in warning state
		_state = BAT_WARN;
	} else if (state != BAT_NONE) {
		_state = BAT_NORM;
	} else {
		_state = BAT_NONE;
	}
	
	if (state != _state) {
		if (initialized) {
			if (_state == (BAT_CRIT || BAT_LOW || BAT_WARN))
				emit changedBatteryWarnState(_state);
			else if (state == (BAT_CRIT || BAT_LOW || BAT_WARN))
				emit changedBatteryWarnState(_state);
			else 
				emit changedBatteryState();

			emit changedBattery();
		}
		state = _state;
	}

	kdDebugFuncOut(trace);
	return ret;
}

//! to check battery.remaining_time
/*!
 * function to update \ref remaining_minutes from property battery.remaining_time
 * \return boolean with the result of the operation
 * \retval true 	if the update was successfull
 * \retval false 	if the update couldn't be applied
 */
bool Battery::checkRemainingTime () {
	kdDebugFuncIn(trace);
	
	int _min = 0;
	bool _ret = false;	

	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		kdError() << "Couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (!present) {
		kdWarning() << "No need to update property, battery not present." << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (dbus_HAL->halGetPropertyInt(udi, "battery.remaining_time", &_min)) {
		_min /= 60;
		_ret = true;
	} else {
		//query was not successfull, but this is optional 
		//and even very unlikely to be present

		//try to get it via charge_level_current and present_rate
		if (charge_level_current > 0 && present_rate > 0) {
			_min = ((charge_level_current * 60 )/ present_rate) ;
			_ret = true;
		} else  {
			//we don't know it better
			_min = 0;
			_ret = false;
		}
	}

	if (remaining_minutes != _min) {
		if (initialized) {
			emit changedBatteryTime();
			emit changedBattery();
		}

		remaining_minutes = _min;
	}

	kdDebugFuncOut(trace);
	return _ret;
}

//! to check battery.rechargeable.is_*
/*!
 * function to update \ref charging_state from property battery.rechargeable.is_*
 * \return boolean with the result of the operation
 * \retval true 	if the update was successfull
 * \retval false 	if the update couldn't be applied
 */
bool Battery::checkChargingState () {
	kdDebugFuncIn(trace);
	
	bool tmp_bool = false;
	bool tmp_bool2 = false;
	bool _ret = false;
	int _c_state = -1;

	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		kdError() << "Couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (!present) {
		kdWarning() << "No need to update property, battery not present." << endl;
		kdDebugFuncOut(trace);
		return false;
	}

	if (dbus_HAL->halGetPropertyBool(udi, "battery.rechargeable.is_charging", &tmp_bool) && 
	    dbus_HAL->halGetPropertyBool(udi, "battery.rechargeable.is_discharging", &tmp_bool2)) {
		if (tmp_bool && !tmp_bool2) {
			_c_state = CHARGING;
		} else if (tmp_bool2 && !tmp_bool) {
			_c_state = DISCHARGING;
		} else {
			_c_state = UNKNOWN_STATE;
		}
		
		_ret = true;
	} else {
		kdError() << "Couldn't get current charging state for udi: " << udi << endl;
		_c_state = UNKNOWN_STATE;
		_ret = false;
	}

	if (charging_state != _c_state) {
		if (initialized) {
			emit changedBatteryChargingState();
			emit changedBattery();
		}

		charging_state = _c_state;
	}

	kdDebugFuncOut(trace);
	return _ret;	
}

// ---> query HAL for properties SECTION : END <----

//! to recheck a special value for a HAL event
/*!
 * Check for the given property new values from HAL and set them to 
 * the battery variables.
 * \param _udi		TQString with the UDI of the device to recheck
 * \param _property	TQString with the property which was changed
 * \return boolean with the result of the operation
 * \retval true 	if the update was successfull
 * \retval false 	if the update couldn't be applied
 */
bool Battery::updateProperty(TQString _udi, TQString _property) {
	kdDebugFuncIn(trace);
	
	//first check whether HAL is available
	if (!dbus_HAL->isConnectedToHAL() && !dbus_HAL->reconnect()) {
		//grr... no luck
		kdError() << "Battery::updateProperty couldn't connect to HAL" << endl;
		kdDebugFuncOut(trace);
		return false;
	}
	
	bool ret = true;

	if (udi.startsWith(_udi)) {
		// handle different keys and update only the needed
		if (_property.startsWith("battery.present")) {
			if (!checkBatteryPresent()) ret = false;
		} else if (_property.startsWith("battery.type")) {
			if (!checkBatteryType()) ret = false;
		} else if (_property.startsWith("battery.charge_level.capacity_state")) {
			if (!checkCapacityState()) ret=  false;
		} else if (_property.startsWith("battery.charge_level.current")) {
			if (!checkChargeLevelCurrent()) ret = false;
		} else if (_property.startsWith("battery.charge_level.rate")) {
			if (!checkChargeLevelRate()) ret = false;
		} else if (_property.startsWith("battery.charge_level.percentage")) {
			if (!checkRemainingPercentage()) ret = false;
		} else if (_property.startsWith("battery.remaining_time")) {
			if (!checkRemainingTime()) ret = false;
		} else if (_property.startsWith("battery.rechargeable.is_")) {
			if (!checkChargingState()) ret = false;
		} else if (_property.startsWith("battery.charge_level.last_full")) {
			if (!checkChargeLevelLastfull()) ret = false;
	// --> changes on this keys should normally not happen except if battery was added /removed
		} else if (_property.startsWith("battery.technology")) {
			if (!checkBatteryTechnology()) ret = false;
		} else if (_property.startsWith("battery.charge_level.unit")) {
			if (!checkChargeLevelUnit()) ret = false;
		} else if (_property.startsWith("battery.charge_level.design")) {
			if (!checkChargeLevelDesign()) ret = false;
		} else {
			kdDebug() << "Unknown or unhandled device property: " << _property 
				  << " for device with udi: " << _udi << endl;
			ret = false;
		}
	} else {
		kdError() << "Given UDI doesn't match the UDI of this battery object." << endl;
		ret = false;
	}

	kdDebugFuncOut(trace);
	return ret;
}

//! Resets the current HAL udi used by the one given
/*!
* The given TQString will be (checked and) used as new HAL udi for the battery.
* But don't forget to do a recheck of the battery afterwards.
* \param _udi 		TQString with the UDI to reset
* \return boolean with the result of the operation
* \retval true 		if reset was successfull
* \retval false 	if reset couldn't be applied
*/
bool Battery::resetUdi(TQString _udi) {
	kdDebugFuncIn(trace);

	bool tmp_result=false;

	//trivial pre-check to eliminate totally dumb _udi strings
	if (!_udi.isNull() && !_udi.isEmpty() && _udi.startsWith("/org/freedesktop/Hal/devices/")) {
		
		//ok, now lets ask HAL if its really a battery
		if (dbus_HAL->isConnectedToHAL() || dbus_HAL->reconnect()) {

			dbus_HAL->halQueryCapability(_udi,"battery",&tmp_result);

		} else {
			kdError() << "Battery::resetUdi couldn't connect to HAL" << endl;
		}

	} else {
		kdError() << "Battery::resetUdi received empty or invalid udi" << endl;
	}

	kdDebugFuncOut(trace);
	return tmp_result;
}

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

//! sets the chargelevel in percent when battery should go into state warning
void Battery::setWarnLevel(int _warn_level) {
	kdDebugFuncIn(trace);

	if (_warn_level < low_level) {
		kdError() << "Refuse requested level: " << _warn_level 
			  << " as it is smaller than the LowLevel: " << low_level << endl;
	} else {
		warn_level = _warn_level;
	}

	kdDebugFuncOut(trace);
}

//! sets the chargelevel in percent when battery should go into state low
void Battery::setLowLevel(int _low_level) {
	kdDebugFuncIn(trace);

	if (_low_level < crit_level || _low_level > warn_level) {
		kdError() << "Refuse requested level: " << _low_level 
			  << " as it is not between WarnLevel: " << warn_level
			  << " and CritLevel: " << crit_level << endl; 
	} else {
		low_level = _low_level;
	}

	kdDebugFuncOut(trace);
}

//! sets the chargelevel in percent when battery should go into state critical
void Battery::setCritLevel(int _crit_level) {
	kdDebugFuncIn(trace);

	if (_crit_level > low_level) {
		kdError() << "Refuse requested level: " << _crit_level
			  << " as it is bigger than LowLevel: " << low_level << endl;
	} else {
		crit_level = _crit_level;
	}

	kdDebugFuncOut(trace);
}

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

//! reports the HAL udi of this battery
TQString Battery::getUdi() const {
	return TQString(udi);
}

//! reports the battery type
int Battery::getType() const {
	return type;
}

//! gives the name of this battery technology
TQString Battery::getTechnology() const {
	return TQString(technology);
}

//! tells the current batterystate as enum BAT_STATE_
int Battery::getState() const {
	return state;
}

//! estimates the remaining minutes until fully charged/discharged
int Battery::getRemainingMinutes() const {
	return remaining_minutes;
}

//! current charging/discharging rate 
int Battery::getPresentRate() const {
	return present_rate;
}

//! get availability of this battery
bool Battery::isPresent() {
	return present;
}

//! maximum capacity of this battery by design
int Battery::getDesignCapacity() const {
	return design_capacity;
}

//! current charging state as enum BAT_CHARG_STATE
int Battery::getChargingState() const {
	return charging_state;
}

//! reports the physical unit of values like DesignCapacity, PresentRate and Lastfull
TQString Battery::getChargelevelUnit() const {
	return TQString(charge_level_unit);
}

//! reports current chargelevel in percentage
int Battery::getPercentage() const {
	return charge_level_percentage;
}

//! reports last full capacity of this battery when fully charged
int Battery::getLastfull() const {
	return charge_level_lastfull;
}

//! reports current chargelevel in units reported by getChargelevelUnit()
int Battery::getCurrentLevel() const {
	return charge_level_current;
}

//! reports HAL capacity_state value
TQString Battery::getCapacityState() const {
	return TQString(capacity_state);
}

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

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

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

//some convenience methods

//! check if the battery is currently charged
bool Battery::isCharging() {
	return (charging_state == CHARGING);
}

//! check if the battery gets currently discharged
bool Battery::isDischarging() {
	return (charging_state == DISCHARGING);
}

//! check it this is a primary battery
bool Battery::isPrimary() const {
	return (type == BAT_PRIMARY);
}

//! check if the battery state is ok/normal
bool Battery::isOk() {
	return (state == BAT_NORM);
}

//! check if the battery is in warning level/state
bool Battery::isWarning() {
	return (state == BAT_WARN);
}

//! check if the battery is in a low chargingstate
bool Battery::isLow() {
	return (state == BAT_LOW);
}

//! check if the battery level is critical
bool Battery::isCritical() {
	return (state == BAT_CRIT);
}
 
// ---> get private members SECTION : START <----

#include "hardware_battery.moc"