summaryrefslogtreecommitdiffstats
path: root/src/detaileddialog.cpp
blob: 76340b534700e9c67c84735cb99e3907b48bd3ee (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
/***************************************************************************
 *   Copyright (C) 2006 by   Daniel Gollub                                 *
 *                            <[email protected]>                            *
 *                 2006-2007 Danny Kukawka                                 *
 *                            <[email protected]>, <[email protected]>   *
 *                                                                         *
 *   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	detaileddialog.cpp
 * \brief	In this file can be found the detailed dialog related code.
 * \author	Daniel Gollub <[email protected]>
 * \author	Danny Kukawka, <[email protected]>, <[email protected]>
 * \date	2006-2007
 */

// KDE headers:
#include <kled.h>
#include <kiconloader.h>
#include <tdelocale.h>

// QT headers:
#include <tqpushbutton.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqtimer.h>
#include <tqgroupbox.h>

// own headers
#include "detaileddialog.h"

/*! 
 * This is the default constructor of the class detaileddialog. 
 */
detaileddialog::detaileddialog( HardwareInfo *_hwinfo, TQPixmap *_pixmap, Settings *_set, 
				TQWidget* parent, const char* name )
    			      : detailed_Dialog( parent, name, false, WDestructiveClose ) {
	kdDebugFuncIn(trace);
	hwinfo = _hwinfo;
	config = _set;
	pixmap = _pixmap;
	primaryBatteries = hwinfo->getPrimaryBatteries();
	cpuInfo = new CPUInfo();

	int batteries = primaryBatteries->getNumBatteries();
	numOfCPUs = cpuInfo->getCPUNum();

	this->setCaption(i18n("TDEPowersave Information Dialog"));
	
	// use this as compromise with current translation process
	// TODO: remove them in the next translation round
	GeneralGroup->setTitle(i18n("Miscellaneous"));
	ProcessorGroup->setTitle(i18n("CPUs"));

	ProcessorGridLayout = new TQGridLayout(ProcessorFrame, numOfCPUs, 2, 0, 5, "ProcessorGridLayout");
	
	if (batteries > 0) {
		if (batteries > 1) batteries++;

		BatteryGroup->setTitle(i18n("Battery state:").remove(":"));
		BatteryGridLayout = new TQGridLayout(BatteryFrame, batteries, 2, 0, 5, "BatteryGridLayout");

		for (int i = 0;  i < batteries; i++) {
			TQLabel *Label = new TQLabel(BatteryFrame, "BatteryLabel");
			if ((primaryBatteries->getNumBatteries() > 1) && (i == 0))
				Label->setText( i18n( "Total:" ));
			else if ((primaryBatteries->getNumBatteries() > 1) && (i > 0))
				Label->setText( i18n( "Battery %1" ).arg(i));
			else 
				Label->setText( i18n( "Battery %1" ).arg(i + 1));
	
			BatteryGridLayout->addWidget( Label, i , 0);
	
			KProgress *PBar = new KProgress(BatteryFrame, "BatteryPBar");
			PBar->setTextEnabled(true);
	
			BatteryPBar.append( PBar );
			BatteryGridLayout->addWidget( PBar, i , 1);
		}
		BatteryFrame->adjustSize();
		tl_powerConsDesc->hide();
		tl_powerConsValue->hide();
		connect(hwinfo, TQT_SIGNAL(generalDataChanged()), this, TQT_SLOT(setBattery()));
		connect(primaryBatteries, TQT_SIGNAL(batteryChanged()), this, TQT_SLOT(setBattery()));
		connect(primaryBatteries, TQT_SIGNAL(batteryChargingStateChanged(int)), this,
			TQT_SLOT(setPowerConsumption()));
		connect(primaryBatteries, TQT_SIGNAL(batteryRateChanged()), this, 
			TQT_SLOT(setPowerConsumption()));
		setBattery();
		setPowerConsumption();
	} else {
		BatteryGroup->hide();
	}

	cpuInfo->checkCPUSpeed();

	ProcessorPictogram->setPixmap(SmallIcon("processor", 22));

	for (int i = 0; i < numOfCPUs; i++) {
		TQLabel *Label = new TQLabel(ProcessorFrame, "ProcessorLabel");
		Label->setText( i18n( "Processor %1" ).arg(i + 1));
		ProcessorGridLayout->addWidget( Label, i , 0);

		KProgress *CPUPBar = new KProgress(ProcessorFrame, "ProcessorPBar");
		CPUPBar->setTextEnabled(true);

		ProcessorPBar.append( CPUPBar );
		ProcessorGridLayout->addWidget( CPUPBar, i , 1);
	}
	ProcessorFrame->adjustSize();
	
	connect(OkButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(closeDetailedDlg()));
	connect(hwinfo, TQT_SIGNAL(ACStatus(bool)), this, TQT_SLOT(setAC()));
	// TODO: replace event
	//connect(pd, TQT_SIGNAL(schemeDataChanged()), this, TQT_SLOT(setInfos()));
	connect(hwinfo, TQT_SIGNAL(generalDataChanged()), this, TQT_SLOT(setInfos()));

	if (hwinfo->supportCPUFreq() || cpuInfo->cpuFreqHW) {
		// Check if cpufreq is available
		cpuInfo->getCPUMaxSpeed();
		setProcessor();
		connect(hwinfo, TQT_SIGNAL(currentCPUFreqPolicyChanged()), this, TQT_SLOT(setInfos()));
	} else {
		// .. if not, use cpu throttling
		if (!cpuInfo->getCPUThrottlingState() || numOfCPUs <= 1) {
			connect(hwinfo, TQT_SIGNAL(generalDataChanged()), this, TQT_SLOT(setProcessorThrottling()));
		}
		setProcessorThrottling();
	}

	setAC();
	setInfos();

	kdDebugFuncOut(trace);
}

/*! This is the default destructor of class detaileddialog. */
detaileddialog::~detaileddialog() {	
	kdDebugFuncIn(trace);
	// no need to delete child widgets, TQt does it all for us
}

/*!
 * \b TQT_SLOT called if the dialog is closed by the user.
 * We do some cleanups here.
 */
void detaileddialog::closeDetailedDlg() {
	kdDebugFuncIn(trace);

	this->close();
	delete(this);
}

/*!
 * \b TQT_SLOT to set up the battery progress widgets.
 */
void detaileddialog::setBattery() {
	kdDebugFuncIn(trace);

	TQString minutes;
	int batteries = 0;

	
	// refresh battery collection
	primaryBatteries = hwinfo->getPrimaryBatteries();
	TQPtrList<Battery> allBatteries = hwinfo->getAllBatteries();

	batteries = primaryBatteries->getNumBatteries();

	if (batteries > 1) batteries++;

	for (int i=0; i < batteries ; i++) {
		int _r_min = 0;
		int _r_per = 0;
		int _c_state = UNKNOWN_STATE;
		bool _present = false;

		BatteryPBar[i]->setTextEnabled(true);
		BatteryPBar[i]->reset();

		if ( (primaryBatteries->getNumBatteries() > 1) && (i == 0) ) {
			// first progressbar with overall infos
			
			_r_min = primaryBatteries->getRemainingMinutes();
			_r_per = primaryBatteries->getRemainingPercent();
			_c_state = primaryBatteries->getChargingState();
			if (primaryBatteries->getNumPresentBatteries() > 0)
				_present = true;
		}
		else {
			// find the related primary battery
			int _current = 0;
			Battery *bat;
			for (bat = allBatteries.first(); bat; bat = allBatteries.next() ) {
				if (bat->getType() ==  primaryBatteries->getBatteryType()) {
					_current++;
					
					if (!bat->isPresent()) {
						_present = false;
					}
					else {
						_r_min = bat->getRemainingMinutes();
						_r_per = bat->getPercentage();
						_c_state = bat->getChargingState();
						_present = true;
					}

					if (_current == i) {
 						break;
					}
					
				}
			}
		}

		if (!_present) {
			BatteryPBar[i]->setFormat(i18n("not present"));
			BatteryPBar[i]->setProgress(0);
			BatteryPBar[i]->setEnabled(false);
		} else {
			int hours = _r_min / 60;
			minutes.setNum(_r_min % 60);
			minutes = minutes.rightJustify(2, '0');	
	
			// CHARG_STATE_CHARG_DISCHARG --> display only the percentage
			if (_c_state == UNKNOWN_STATE || _r_min < 0 ) {
				BatteryPBar[i]->setFormat("%p%");
			} else if (_c_state == CHARGING && hwinfo->hasAPM() ) {
				// this should fix apm, where we have no time info if charging
				BatteryPBar[i]->setFormat("%p% " + i18n("charged"));
			} else if (_c_state == CHARGING) {
				TQString temp = i18n("%1:%2 h until charged").arg(hours).arg(minutes);
				BatteryPBar[i]->setFormat(temp);
			} else if (_c_state == DISCHARGING) {
				TQString temp = i18n("%1:%2 h remaining").arg(hours).arg(minutes);
				BatteryPBar[i]->setFormat(temp);
			
			} else {
				//fallback 
				BatteryPBar[i]->setFormat(i18n("unknown"));
			}
	
			if (_r_per < 0)
				BatteryPBar[i]->setProgress(0);
			else	
				BatteryPBar[i]->setProgress(_r_per);
			BatteryPBar[i]->setEnabled(true);
		}
	}

	BatteryPictogram->setPixmap(*pixmap);
	kdDebugFuncOut(trace);
}

/*!
 * \b TQT_SLOT to set up the Processor/CPU progress bar widgets.
 */
void detaileddialog::setPowerConsumption() {
	kdDebugFuncIn(trace);

	// refresh battery collection
	primaryBatteries = hwinfo->getPrimaryBatteries();
	int rate = primaryBatteries->getCurrentRate();

	if (rate > 0 && !primaryBatteries->getChargeLevelUnit().isEmpty()) {	

		TQString _val;
		_val.setNum(rate);
		_val += " " + primaryBatteries->getChargeLevelUnit().remove('h');

		tl_powerConsValue->setText(_val);

		if (!tl_powerConsDesc->isShown()) {
			tl_powerConsDesc->show();
			tl_powerConsValue->show();
		}
	} else {
		if (tl_powerConsDesc->isShown()) {
			tl_powerConsDesc->hide();
			tl_powerConsValue->hide();
		}
	}

	kdDebugFuncOut(trace);
}

/*!
 * \b TQT_SLOT to set up the Processor/CPU progress bar widgets.
 */
void detaileddialog::setProcessor() {
	kdDebugFuncIn(trace);

	cpuInfo->checkCPUSpeed();

	for (int i=0; i < numOfCPUs; i++) {
		kdDebug() << "ID: " << i << "(" << cpuInfo->cpufreq_speed.count()
			  << ") cur_freq: " << cpuInfo->cpufreq_speed[i] << " max_freq: "
			  << cpuInfo->cpufreq_max_speed[i] << endl; 

		//ProcessorPBar[i]->setTextEnabled(true);
		if (cpuInfo->cpufreq_speed[i] > 0) {
			// CPU/Core is back from offline
			if(ProcessorPBar[i]->progress() == 0)
				cpuInfo->getCPUMaxSpeed();

			if(ProcessorPBar[i]->progress() != cpuInfo->cpufreq_speed[i]) {
				// get max cpu freq and set it to the max of the progressbar
				int maxfreq = cpuInfo->cpufreq_max_speed[i];
				ProcessorPBar[i]->setTotalSteps(maxfreq);
	
				// display  1400 MHz instead of 1400%
				ProcessorPBar[i]->setFormat(i18n("%v MHz"));
				ProcessorPBar[i]->setProgress(cpuInfo->cpufreq_speed[i]);
				ProcessorPBar[i]->setEnabled(true);
			}
		} else {
			ProcessorPBar[i]->setFormat(i18n("deactivated"));
			ProcessorPBar[i]->setProgress(0);
			ProcessorPBar[i]->setEnabled(false);
		}
	}
	TQTimer::singleShot(333, this, TQT_SLOT(setProcessor()));	
	kdDebugFuncOut(trace);
}

/*!
 * \b TQT_SLOT to set up the Processor/CPU bar widgets for cpu throttling machines.
 */
void detaileddialog::setProcessorThrottling() {
	kdDebugFuncOut(trace);

	bool throttling = cpuInfo->getCPUThrottlingState();
	cpuInfo->checkCPUSpeedThrottling();

	for (int i=0; i < numOfCPUs; i++) { 
		if (throttling)
			kdDebug() << "Throttling CPU : " << i << " - freq: " << cpuInfo->cpufreq_speed[i]
				  << " - throttling state: " << cpuInfo->cpu_throttling[i] << "%" << endl;
		else
			kdDebug() << "CPU - freq: " << cpuInfo->cpufreq_speed[i] << endl;
	
		if (throttling && cpuInfo->cpufreq_speed[i] > 0 && cpuInfo->cpu_throttling[i] >= 0) {
			// get max cpu freq and set it to the max of the progressbar
			ProcessorPBar[i]->setTotalSteps(100);
			TQString ProgressString = TQString("%1% (%2 MHz)").arg(100 - cpuInfo->cpu_throttling[i]).arg(cpuInfo->cpufreq_speed[i]);
			ProcessorPBar[i]->setFormat(i18n(ProgressString.utf8()));
			ProcessorPBar[i]->setProgress(100 - cpuInfo->cpu_throttling[i]);
			ProcessorPBar[i]->setEnabled(true);
		} else if (cpuInfo->cpufreq_speed[i] < 0) {
			ProcessorPBar[i]->setFormat(i18n("deactivated"));
			ProcessorPBar[i]->setProgress(0);
			ProcessorPBar[i]->setEnabled(false);
		} else {
			ProcessorPBar[i]->setTotalSteps(cpuInfo->cpufreq_speed[i]);
			ProcessorPBar[i]->setFormat(i18n("%v MHz"));
			ProcessorPBar[i]->setProgress(cpuInfo->cpufreq_speed[i]);
			ProcessorPBar[i]->setEnabled(true);
		}
	}

	if (throttling || numOfCPUs > 1) {
		// currently there are no events we can use to get actual data
		// so we recheck data ever 2 secs to register changes in the 
		// throttling state and if a CPU/core online state change
		TQTimer::singleShot(2000, this, TQT_SLOT(setProcessorThrottling()));
	}

	kdDebugFuncOut(trace);
}

/*!
 * \b TQT_SLOT to set up the AC status within the Led widget.
 */
void detaileddialog::setAC() {
	kdDebugFuncIn(trace);

	if (hwinfo->getAcAdapter()) {
		LabelACStatus->setText( i18n("plugged in") );
		LedAC->on();
	} else {
		LedAC->off();
		LabelACStatus->setText( i18n("unplugged") );
	}

	setInfos();
	kdDebugFuncOut(trace);
}

/*!
 * \b TQT_SLOT to set all additional informtation as e.g. CPUFrequency policy
 * or current scheme
 */
void detaileddialog::setInfos() {
	kdDebugFuncOut(trace);

	TQString display;
	TQString displayValue;

	if(!config->currentScheme.isEmpty())
		display += i18n("Current Scheme: ") + "\n";
		displayValue += i18n(config->currentScheme.utf8()) + "\n";
		if(config->currentScheme == config->ac_scheme)
			InfoPictogram->setPixmap(SmallIcon("scheme_power", 22));
		else if(config->currentScheme == config->battery_scheme)
			InfoPictogram->setPixmap(SmallIcon("scheme_powersave", 22));
		else if(config->currentScheme == "Acoustic")
			InfoPictogram->setPixmap(SmallIcon("scheme_acoustic", 22));
		else if(config->currentScheme == "Presentation")
			InfoPictogram->setPixmap(SmallIcon("scheme_presentation", 22));
		else if(config->currentScheme == "AdvancedPowersave")
			InfoPictogram->setPixmap(SmallIcon("scheme_advanced_powersave", 22));
		else
			InfoPictogram->setPixmap(SmallIcon("tdepowersave", 22));

	if(hwinfo->isOnline()) {
		if (hwinfo->supportCPUFreq()) {
			display += i18n("Current CPU Frequency Policy:") + "\n";
			switch (hwinfo->getCurrentCPUFreqPolicy()){
				case PERFORMANCE:
					displayValue += i18n("Performance")  + "\n";
					break;
				case DYNAMIC:
					displayValue += i18n("Dynamic") + "\n";
					break;
				case POWERSAVE:
					displayValue += i18n("Powersave") + "\n";
					break;
				default:
					displayValue += i18n("unknown") + "\n";
					break;
			}
		}

		// refresh battery collection
		primaryBatteries = hwinfo->getPrimaryBatteries();
		int batteries = primaryBatteries->getNumBatteries();
		TQPtrList<Battery> allBatteries = hwinfo->getAllBatteries();

		if (batteries > 0 && primaryBatteries->getNumPresentBatteries() > 0) {

			display += i18n("Battery state:") + "\n";
			switch (primaryBatteries->getBatteryState()){
				case BAT_CRIT:
					displayValue += i18n("Critical") + "\n";
					break;
				case BAT_LOW:
					displayValue += i18n("Low") + "\n";
					break;
				case BAT_WARN:
					displayValue += i18n("Warning") + "\n";
					break;
				case BAT_NORM:
					displayValue += i18n("ok") + "\n";
					break;
				default:
					displayValue += i18n("unknown") + "\n";
					break;
			}
		}
		
		if(hwinfo->supportBrightness()) {
			display += i18n("Set brightness supported:") + "\n";
			displayValue += i18n("yes") + "\n";
		} else {
			display += i18n("Set brightness supported:") + "\n";
			displayValue += i18n("no") + "\n";
		}

		display += i18n("TDE hardware subsystem:");
		displayValue += i18n("active");
	}
	else {
		display += i18n("TDE hardware subsystem:");
		displayValue += i18n("not available");
	}

	if(!display.isEmpty()) 
		InfoLabel->setText(display);
	
	InfoLabelValue->setText(displayValue);
	kdDebugFuncOut(trace);
}

#include "detaileddialog.moc"