summaryrefslogtreecommitdiffstats
path: root/src/kima.cpp
blob: ff4218507299ed20d15a03d4195d7058ea93b6d7 (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
/***************************************************************************
 *   Copyright (C) 2006 by Ken Werner                                      *
 *   ken.werner@web.de                                                     *
 *                                                                         *
 *   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) any later version.                                   *
 *                                                                         *
 *   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.             *
 ***************************************************************************/

#include <list>

// TQt
#include <tqlabel.h>
#include <tqfile.h>
#include <tqspinbox.h>
#include <tqcombobox.h>
#include <tqtextedit.h>
#include <tqtimer.h>
#include <tqcursor.h>
#include <tdelistview.h>
#include <tqlistview.h> 
#include <tqwidgetstack.h> 

// KDE
#include <tdemessagebox.h>
#include <tdeaboutapplication.h>
#include <tdepopupmenu.h>
#include <kstdaction.h>
#include <tdeaction.h>
#include <tdeactioncollection.h>
#include <tdeactionclasses.h>
#include <tdeapplication.h>
#include <kdialogbase.h>
#include <tdeconfig.h>
//#include <tdeglobal.h>
#include <kiconloader.h>
#include <tdelocale.h>

// Kima
#include "kima.h"
#include "flowlayout.h"
#include "sourcelistitem.h"
#include "prefs.h"

// Kima Soures
#include "sources/acpithermalsrc.h"
#include "sources/ibmacpithermalsrc.h"
#include "sources/ibmacpifansrc.h"
#include "sources/ibmhdaps.h"
#include "sources/omnibookthermalsrc.h"
#include "sources/ibookg4thermalsrc.h"
#include "sources/hwmonthermalsrc.h"
#include "sources/hwmonfansrc.h"
#include "sources/sysfreqsrc.h"
#include "sources/cpuinfofreqsrc.h"
#include "sources/i8ksrc.h"
#include "sources/nvidiathermalsrc.h"
#include "sources/hddtempsrc.h"
#include "sources/uptimesrc.h"
#include "sources/batterysrc.h"

//#include "kdebug.h"

extern "C" {
KDE_EXPORT	KPanelApplet* init( TQWidget* inParent, const TQString& inConfigFile) {
		TDEGlobal::locale()->insertCatalogue("kima");
		return new Kima(inConfigFile, KPanelApplet::Normal,
		                      //KPanelApplet::About | KPanelApplet::Help | KPanelApplet::Preferences,
		                      KPanelApplet::About | KPanelApplet::Preferences,
		                      inParent, "kima");
	}
}

Kima::Kima(const TQString& inConfigFile, Type inType, int inActions, TQWidget* inParent, const char* inName):
		KPanelApplet(inConfigFile, inType, inActions, inParent, inName),
		TQToolTip(this),
		mCachedWFH(0),
		mCachedHeight(0),
		mDraggedSourceItem(NULL){
	mAboutDialog = 0; // aboutData is created in the about() method
	mPrefsDlg = 0; // the prefs dialog is created in the preferences() method
	mPrefs = 0; // the prefs ui is created in the preferences() method

	// Get the current application configuration handle
	mTDEConfig = config();
	mTDEConfig->setGroup("Kima");

	// automatically delete pointers
	mSources.setAutoDelete(TRUE);

	// create layout
	mLayout = new FlowLayout(this, orientation() );
	mLayout->setSpacing(8);


	// add the sources into the sources list
	addSources(NVidiaThermalSrc::createInstances(this));
    addSources(ACPIThermalSrc::createInstances(this));
	addSources(IBMACPIThermalSrc::createInstances(this));
	addSources(IBMACPIFanSrc::createInstances(this));
	addSources(IBMHDAPSSrc::createInstances(this));
	addSources(OmnibookThermalSrc::createInstances(this));
	addSources(IbookG4ThermalSrc::createInstances(this));
	addSources(HwMonThermalSrc::createInstances(this));
	addSources(SysFreqSrc::createInstances(this));
	addSources(CpuinfoFreqSrc::createInstances(this));
	addSources(I8kSrc::createInstances(this));
	addSources(HDDTempSrc::createInstances(this));
	addSources(HwMonFanSrc::createInstances(this));
	addSources(UptimeSrc::createInstances(this));
	addSources(BatterySrc::createInstances(this));
	

	TQPtrListIterator<Source> itSync(mSources);
	Source* source;
	while((source = itSync.current()) != 0){
		++itSync;
        registerSource(source);
	}
	mLayout->updatePositions(mTDEConfig);

	// create the menu
	mMenu = new TDEPopupMenu(this);
	mMenu->insertTitle(SmallIcon("hwinfo"), i18n("Kima"));

	if (mCpufreqd.enabled()) {
		mMenu->insertItem(i18n("&Performance Profiles"), mCpufreqd.menu());
	}

	TDEActionCollection* actionCollection = new TDEActionCollection(this, "actionCollection", kapp);

	TDEAction* action = KStdAction::preferences(TQT_TQOBJECT(this), TQT_SLOT(preferences()), actionCollection);
	action->setText(i18n("&Preferences"));
	action->plug(mMenu);

	action = KStdAction::aboutApp(TQT_TQOBJECT(this), TQT_SLOT(about()), actionCollection);
	action->setText(i18n("&About Kima"));
	action->plug(mMenu);

	//menu->insertSeparator();
	//KStdAction::quit( kapp, TQT_SLOT( quit() ), actionCollection )->plug( menu );
}

Kima::~Kima() {
	// super class deletes the widgets
}

void Kima::addSources(const std::list<Source*>& inList){
	for(std::list<Source*>::const_iterator it = inList.begin(); it != inList.end(); ++it){
		mSources.append(*it);
	}
}

void Kima::displaySource(bool inDisplay, Source* inSource){
	if(inDisplay){
		//kdDebug() << "Kima::displaySource mLayout->add: " << inSource->getName() << endl;
		mLayout->addSource(inSource); // add the source
		//kdDebug() << "Pos of " << inSource->getID() << ": " << inSource->getPosition() << endl;
	}else{
		//kdDebug() << "Kima::displaySource mLayout->remove: " << inSource->getName() << endl;
		mLayout->remove(inSource->getWidget());
	}
}

void Kima::about() {
	if(!mAboutDialog){
		TDEAboutData* aboutData = new TDEAboutData("libkima",
			"Kima",
			"0.7.3.2",
			I18N_NOOP("Kicker monitoring applet.\n\n"
				"This applet monitors various temperature, frequency and fan sources.\n"
				"Make sure you have enabled a supported kernel module.\n"),
			TDEAboutData::License_GPL,
			"(c) 2004-2007, Ken Werner");
		aboutData->addAuthor("Ken Werner", 0, "ken.werner@web.de");
		aboutData->addAuthor("Johannes Schaub", 0, "schaub-johannes@web.de");
		aboutData->addAuthor("Valentine Sinitsyn", I18N_NOOP("cpufreqd control module"), "e_val@inbox.ru");
		aboutData->addCredit("Franziska Simmank");
		aboutData->addCredit("Manfred Paul");
		aboutData->addCredit("Rainer Dorsch");
		aboutData->addCredit("Flavio Castelli");
		aboutData->addCredit("Jocke Andersson");
		aboutData->addCredit("Raoul Martin");
		aboutData->addCredit("Donnie Bhayangkara");
		aboutData->addCredit("Gonzalo Nemmi");
		aboutData->addCredit("Mariusz Ciesla");
		aboutData->addCredit("Mark Rosenstand");
		aboutData->addCredit("Santiago Bruno");
		aboutData->addCredit("Dark Apostrophe");
		aboutData->addCredit("Sascha Hoogen");
		aboutData->addCredit("Peter Avramucz");
		aboutData->addCredit("Oskar Ellström");
		aboutData->addCredit("Uğur Çetin");
		mAboutDialog = new TDEAboutApplication(aboutData, this, "aboutApp", false);
	}
	mAboutDialog->show();
}

void Kima::help() {
	KMessageBox::information(0, i18n("This is a help box"));
}

void Kima::preferences() {
	//KMessageBox::information(0, i18n("This is a preferences box"));
	if(!mPrefsDlg){
		//create the prefs dialog
		mPrefsDlg = new KDialogBase( this, "prefsDlg", false, i18n("Configuration"), KDialogBase::Ok|KDialogBase::Apply|KDialogBase::Cancel, KDialogBase::Ok, false );
		//create the prefs ui
		mPrefs = new Prefs( mPrefsDlg, "prefsui" ); //the uic generated class

		//setup the dialog
		//mPrefsDlg->setFixedSize(mPrefs->size());
		mPrefsDlg->setMainWidget(mPrefs); //embed the uic generated class into the kde dialog
		connect(mPrefsDlg, TQT_SIGNAL(applyClicked()), TQT_TQOBJECT(this), TQT_SLOT(savePreferences()));
		connect(mPrefsDlg, TQT_SIGNAL(okClicked()), TQT_TQOBJECT(this), TQT_SLOT(savePreferences()));
		connect(mPrefsDlg, TQT_SIGNAL(cancelClicked()), TQT_TQOBJECT(this), TQT_SLOT(cancelPreferences()));

		// create new list items and the widgetStack widgets
		TQPtrListIterator<Source> it(mSources);
		Source* source;
		while((source = it.current()) != 0){
			++it;
			// create the specific source prefs and add them to the widgetStack
			mPrefs->widgetStack->addWidget(source->createPrefs(mPrefs->widgetStack));
			
            // create the SourceListItem to switch and to enable/disable the widgetstack item (the source)
            // and connect the apply/ok signals to the item, so it can change it's text to a potentially
            // new source name.
			SourceListItem * item = new SourceListItem(source, mPrefs->sourceListView, TQCheckListItem::CheckBox);
            connect(mPrefsDlg, TQT_SIGNAL(applyClicked()), item, TQT_SLOT(updateText()));
            connect(mPrefsDlg, TQT_SIGNAL(okClicked()), item, TQT_SLOT(updateText()));
		}		
		
		if(mPrefs->sourceListView->firstChild()){
			mPrefs->sourceListView->setSelected(mPrefs->sourceListView->firstChild(), true);  // select the first list item
			raiseSourcePrefsWidget(mPrefs->sourceListView->firstChild()); // raise that item
		}

		connect(mPrefs->sourceListView, TQT_SIGNAL(selectionChanged(TQListViewItem*)), TQT_TQOBJECT(this), TQT_SLOT(raiseSourcePrefsWidget(TQListViewItem*)));
	}

	// update prefs gui
	TQPtrListIterator<Source> srcIt(mSources);
	Source* source;
	while((source = srcIt.current()) != 0){
		++srcIt;
		source->updatePrefsGUI();
	}

	// update the sourcelist checkbox
	TQPtrList<TQListViewItem> lst;
	TQListViewItemIterator lvIt(mPrefs->sourceListView);
	while(lvIt.current()){
		SourceListItem* sli = static_cast<SourceListItem*>(lvIt.current());
		sli->setOn(sli->getSource()->isEnabled());
		++lvIt;
	}

	//show the dialog
	mPrefsDlg->show();
}

void Kima::raiseSourcePrefsWidget(TQListViewItem* inListViewItem){
	mPrefs->widgetStack->raiseWidget(static_cast<SourceListItem*>(inListViewItem)->getSource()->getPrefs());
}

void Kima::cancelPreferences(){
	// update prefs gui
	TQPtrListIterator<Source> it(mSources);
	Source* source;
	while((source = it.current()) != 0){
		++it;
		source->updatePrefsGUI();
	}
}

void Kima::savePreferences(){
	// save prefs
	TQPtrListIterator<Source> it(mSources);
	Source* source;
	while((source = it.current()) != 0){
		++it;
		source->applyPrefs();
		source->savePrefs(mTDEConfig);
	}
	// save the position of all sources because they might have changed.
	mLayout->updatePositions(mTDEConfig);
	// update the source widgets
	updateSourceWidgets();
	// write config
	mTDEConfig->sync();
	// reset cached witdh for height to force recalculation of the applets width
	mCachedWFH = 0;
}

int Kima::widthForHeight(int inHeight) const{
	//kdDebug() << "widthForHeight: " << height << endl;
	mLayout->setOrientation(TQt::Horizontal);
	if(mCachedHeight != inHeight){
		mCachedHeight = inHeight;
		mCachedWFH = 0;
	}
 	mCachedWFH = TQMAX(mLayout->widthForHeight(inHeight), mCachedWFH);
	return mCachedWFH;
}

int Kima::heightForWidth(int inWidth) const{
	//kdDebug() << "heightForWidth: " << width << endl;
	mLayout->setOrientation(TQt::Vertical);
	return mLayout->heightForWidth(inWidth);
	//return sizeHint().height();
}

void Kima::mousePressEvent(TQMouseEvent* inEvent ){
	if(inEvent->button() == TQt::RightButton){
		mMenu->popup( inEvent->globalPos() );
		if(mDraggedSourceItem)
			mDraggedSourceItem->widget()->unsetCursor(); // unset drag cursor
	}
	else if(inEvent->button() == TQt::LeftButton){
		TQLayoutIterator it = mLayout->iterator();
		while(it.current()){
			TQWidget * c = it.current()->widget();
			if(c->rect().contains(c->mapFromGlobal(inEvent->globalPos()))){
				mDraggedSourceItem = it.current(); // remenber the clicked source
				c->setCursor(TQt::SizeAllCursor);   // set drag cursor
				mDragFactor = (c->rect().height()/2.0) / (double)c->mapFromGlobal(inEvent->globalPos()).y();
				break;
			}
			++it;
		}
	}
}

void Kima::mouseReleaseEvent(TQMouseEvent* inEvent){
	//if(inEvent->button() == TQMouseEvent::LeftButton && mDraggedSourceItem){
	if(mDraggedSourceItem){
		mCachedWFH = 0; // reset cached witdh for height to force recalculation of the applets width
		mDraggedSourceItem->widget()->unsetCursor(); // unset drag cursor
		mDraggedSourceItem = NULL;
	}
}

void Kima::mouseMoveEvent( TQMouseEvent* inEvent ) {
	if(!mDraggedSourceItem)
		return;
	TQLayoutIterator it = mLayout->iterator();
	while(it.current()){
		TQWidget * c = it.current()->widget();
		if(c->rect().contains(c->mapFromGlobal(inEvent->globalPos()))){
			if(it.current() == mDraggedSourceItem)
                		break;
			// we are over a source-widget.
			// move the dragged widget above / below that 
			// widget, depending where the cursor is.
			TQRect crect = c->rect();
			double relPos = c->mapFromGlobal(inEvent->globalPos()).y() * mDragFactor;
			if(mLayout->moveItem(mDraggedSourceItem, it.current(), crect.height()/2.0 > relPos ? FlowLayout::ABOVE : FlowLayout::BELOW)){
				mLayout->updatePositions(mTDEConfig);
				updateGeometry();
				mTDEConfig->sync(); // write config
				updateSourceWidgets();
			}
			break;
		}
		++it;
	}
	// outside kima?
	if(!rect().contains(mapToParent(inEvent->pos())))
		mDraggedSourceItem->widget()->setCursor(TQt::ForbiddenCursor);
	else
		mDraggedSourceItem->widget()->setCursor(TQt::SizeAllCursor);
}

void Kima::paintEvent(TQPaintEvent* inEvent){
	KPanelApplet::paintEvent(inEvent);
	updateSourceWidgets();
}

void Kima::updateSourceWidgets(){
	// repaint the source widgets
	for(Source* source = mSources.first(); source; source = mSources.next())
		if(source->showOnApplet())
			source->getWidget()->update();
}

void Kima::registerSource(Source* source) {
	// then, tell the source to set up its widget
	source->realizeWidget();
	
	// enable transparency
	source->getWidget()->setBackgroundMode(X11ParentRelative);
	
	// load prefs from the configuration
	// this call also emits enabledChanged
	source->loadPrefs(mTDEConfig);
	
	// add the source to the layout if necessary
	displaySource(source->isEnabled() && source->showOnApplet(), source);
	
	// connection to add and remove sources from mLayout
	connect(source, TQT_SIGNAL(displaySource(bool, Source*)), TQT_TQOBJECT(this), TQT_SLOT(displaySource(bool, Source*)));
}

void Kima::maybeTip(const TQPoint& inPos){
	if(!rect().contains(inPos))
		return;
	TQString text = "<b>" + i18n("Sources:") + "</b><br><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";

	TQPtrListIterator<Source> it(mSources);
	Source* source;
	while((source = it.current()) != 0){
		++it;
		if(source->isEnabled() && source->isToolTipEnabled())
			text.append("<tr><td>" + 
				source->getName() + "</td><td>" +
				source->getValue() + "</td><td>");
	}
	text.append("</table>");
	tip(rect(), text);
}
#include "kima.moc"