You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
438 lines
11 KiB
438 lines
11 KiB
15 years ago
|
/*
|
||
|
KSysGuard, the KDE System Guard
|
||
|
|
||
|
Copyright (c) 2001 Tobias Koenig <tokoe@kde.org>
|
||
|
|
||
|
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.
|
||
|
|
||
|
*/
|
||
|
|
||
12 years ago
|
#include <tdeapplication.h>
|
||
15 years ago
|
#include <kiconloader.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
15 years ago
|
|
||
|
#include <ksgrd/SensorManager.h>
|
||
|
#include <ksgrd/StyleEngine.h>
|
||
|
|
||
15 years ago
|
#include <tqfile.h>
|
||
15 years ago
|
|
||
|
#include "SensorLogger.moc"
|
||
|
#include "SensorLoggerSettings.h"
|
||
|
|
||
15 years ago
|
SLListViewItem::SLListViewItem(TQListView *parent)
|
||
|
: TQListViewItem(parent)
|
||
15 years ago
|
{
|
||
|
}
|
||
|
|
||
15 years ago
|
LogSensor::LogSensor(TQListView *parent)
|
||
15 years ago
|
: timerID( NONE ), lowerLimitActive( 0 ), upperLimitActive( 0 ),
|
||
|
lowerLimit( 0 ), upperLimit( 0 )
|
||
|
{
|
||
13 years ago
|
TQ_CHECK_PTR(parent);
|
||
15 years ago
|
|
||
|
monitor = parent;
|
||
|
|
||
|
lvi = new SLListViewItem(monitor);
|
||
13 years ago
|
TQ_CHECK_PTR(lvi);
|
||
15 years ago
|
|
||
|
pixmap_running = UserIcon( "running" );
|
||
|
pixmap_waiting = UserIcon( "waiting" );
|
||
|
|
||
|
lvi->setPixmap(0, pixmap_waiting);
|
||
13 years ago
|
lvi->setTextColor(monitor->colorGroup().text());
|
||
15 years ago
|
|
||
|
monitor->insertItem(lvi);
|
||
|
}
|
||
|
|
||
|
LogSensor::~LogSensor(void)
|
||
|
{
|
||
|
if ((lvi) && (monitor))
|
||
|
delete lvi;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
LogSensor::startLogging(void)
|
||
|
{
|
||
|
lvi->setPixmap(0, pixmap_running);
|
||
|
timerOn();
|
||
|
}
|
||
|
|
||
|
void
|
||
|
LogSensor::stopLogging(void)
|
||
|
{
|
||
|
lvi->setPixmap(0, pixmap_waiting);
|
||
13 years ago
|
lvi->setTextColor(monitor->colorGroup().text());
|
||
13 years ago
|
lvi->repaint();
|
||
15 years ago
|
timerOff();
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
LogSensor::timerEvent(TQTimerEvent*)
|
||
15 years ago
|
{
|
||
|
KSGRD::SensorMgr->sendRequest(hostName, sensorName, (KSGRD::SensorClient*) this, 42);
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
LogSensor::answerReceived(int id, const TQString& answer)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQFile logFile(fileName);
|
||
15 years ago
|
|
||
|
if (!logFile.open(IO_ReadWrite | IO_Append))
|
||
|
{
|
||
|
stopLogging();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
switch (id)
|
||
|
{
|
||
|
case 42: {
|
||
15 years ago
|
TQTextStream stream(&logFile);
|
||
15 years ago
|
double value = answer.toDouble();
|
||
|
|
||
|
if (lowerLimitActive && value < lowerLimit)
|
||
|
{
|
||
|
timerOff();
|
||
|
lowerLimitActive = false;
|
||
13 years ago
|
lvi->setTextColor(monitor->colorGroup().foreground());
|
||
13 years ago
|
lvi->repaint();
|
||
15 years ago
|
KNotifyClient::event(monitor->winId(), "sensor_alarm", TQString("sensor '%1' at '%2' reached lower limit").arg(sensorName).arg(hostName));
|
||
15 years ago
|
timerOn();
|
||
|
} else if (upperLimitActive && value > upperLimit)
|
||
|
{
|
||
|
timerOff();
|
||
|
upperLimitActive = false;
|
||
13 years ago
|
lvi->setTextColor(monitor->colorGroup().foreground());
|
||
13 years ago
|
lvi->repaint();
|
||
15 years ago
|
KNotifyClient::event(monitor->winId(), "sensor_alarm", TQString("sensor '%1' at '%2' reached upper limit").arg(sensorName).arg(hostName));
|
||
15 years ago
|
timerOn();
|
||
|
}
|
||
13 years ago
|
TQDate date = TQT_TQDATE_OBJECT(TQDateTime::currentDateTime().date());
|
||
|
TQTime time = TQT_TQTIME_OBJECT(TQDateTime::currentDateTime().time());
|
||
15 years ago
|
|
||
15 years ago
|
stream << TQString("%1 %2 %3 %4 %5: %6\n").arg(date.shortMonthName(date.month())).arg(date.day()).arg(time.toString()).arg(hostName).arg(sensorName).arg(value);
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
logFile.close();
|
||
|
}
|
||
|
|
||
15 years ago
|
SensorLogger::SensorLogger(TQWidget *parent, const char *name, const TQString& title)
|
||
15 years ago
|
: KSGRD::SensorDisplay(parent, name, title)
|
||
|
{
|
||
15 years ago
|
monitor = new TQListView(this, "monitor");
|
||
13 years ago
|
TQ_CHECK_PTR(monitor);
|
||
15 years ago
|
|
||
|
monitor->addColumn(i18n("Logging"));
|
||
|
monitor->addColumn(i18n("Timer Interval"));
|
||
|
monitor->addColumn(i18n("Sensor Name"));
|
||
|
monitor->addColumn(i18n("Host Name"));
|
||
|
monitor->addColumn(i18n("Log File"));
|
||
|
|
||
13 years ago
|
TQColorGroup cgroup = monitor->colorGroup();
|
||
15 years ago
|
cgroup.setColor(TQColorGroup::Text, KSGRD::Style->firstForegroundColor());
|
||
|
cgroup.setColor(TQColorGroup::Base, KSGRD::Style->backgroundColor());
|
||
|
cgroup.setColor(TQColorGroup::Foreground, KSGRD::Style->alarmColor());
|
||
|
monitor->setPalette(TQPalette(cgroup, cgroup, cgroup));
|
||
|
monitor->setSelectionMode(TQListView::NoSelection);
|
||
15 years ago
|
|
||
15 years ago
|
connect(monitor, TQT_SIGNAL(rightButtonClicked(TQListViewItem*, const TQPoint&, int)), this, TQT_SLOT(RMBClicked(TQListViewItem*, const TQPoint&, int)));
|
||
15 years ago
|
|
||
|
setTitle(i18n("Sensor Logger"));
|
||
|
|
||
|
logSensors.setAutoDelete(true);
|
||
|
|
||
|
setPlotterWidget(monitor);
|
||
|
|
||
|
setMinimumSize(50, 25);
|
||
|
setModified(false);
|
||
|
}
|
||
|
|
||
|
SensorLogger::~SensorLogger(void)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
bool
|
||
15 years ago
|
SensorLogger::addSensor(const TQString& hostName, const TQString& sensorName, const TQString& sensorType, const TQString&)
|
||
15 years ago
|
{
|
||
|
if (sensorType != "integer" && sensorType != "float")
|
||
|
return (false);
|
||
|
|
||
|
sld = new SensorLoggerDlg(this, "SensorLoggerDlg");
|
||
13 years ago
|
TQ_CHECK_PTR(sld);
|
||
15 years ago
|
|
||
|
if (sld->exec()) {
|
||
|
if (!sld->fileName().isEmpty()) {
|
||
|
LogSensor *sensor = new LogSensor(monitor);
|
||
13 years ago
|
TQ_CHECK_PTR(sensor);
|
||
15 years ago
|
|
||
|
sensor->setHostName(hostName);
|
||
|
sensor->setSensorName(sensorName);
|
||
|
sensor->setFileName(sld->fileName());
|
||
|
sensor->setTimerInterval(sld->timerInterval());
|
||
|
sensor->setLowerLimitActive(sld->lowerLimitActive());
|
||
|
sensor->setUpperLimitActive(sld->upperLimitActive());
|
||
|
sensor->setLowerLimit(sld->lowerLimit());
|
||
|
sensor->setUpperLimit(sld->upperLimit());
|
||
|
|
||
|
logSensors.append(sensor);
|
||
|
|
||
|
setModified(true);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
delete sld;
|
||
|
sld = 0;
|
||
|
|
||
|
return (true);
|
||
|
}
|
||
|
|
||
|
bool
|
||
|
SensorLogger::editSensor(LogSensor* sensor)
|
||
|
{
|
||
|
sld = new SensorLoggerDlg(this, "SensorLoggerDlg");
|
||
13 years ago
|
TQ_CHECK_PTR(sld);
|
||
15 years ago
|
|
||
|
sld->setFileName(sensor->getFileName());
|
||
|
sld->setTimerInterval(sensor->getTimerInterval());
|
||
|
sld->setLowerLimitActive(sensor->getLowerLimitActive());
|
||
|
sld->setLowerLimit(sensor->getLowerLimit());
|
||
|
sld->setUpperLimitActive(sensor->getUpperLimitActive());
|
||
|
sld->setUpperLimit(sensor->getUpperLimit());
|
||
|
|
||
|
if (sld->exec()) {
|
||
|
if (!sld->fileName().isEmpty()) {
|
||
|
sensor->setFileName(sld->fileName());
|
||
|
sensor->setTimerInterval(sld->timerInterval());
|
||
|
sensor->setLowerLimitActive(sld->lowerLimitActive());
|
||
|
sensor->setUpperLimitActive(sld->upperLimitActive());
|
||
|
sensor->setLowerLimit(sld->lowerLimit());
|
||
|
sensor->setUpperLimit(sld->upperLimit());
|
||
|
|
||
|
setModified(true);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
delete sld;
|
||
|
sld = 0;
|
||
|
|
||
|
return (true);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
SensorLogger::configureSettings()
|
||
|
{
|
||
13 years ago
|
TQColorGroup cgroup = monitor->colorGroup();
|
||
15 years ago
|
|
||
|
sls = new SensorLoggerSettings(this, "SensorLoggerSettings");
|
||
13 years ago
|
TQ_CHECK_PTR(sls);
|
||
15 years ago
|
|
||
15 years ago
|
connect( sls, TQT_SIGNAL( applyClicked() ), TQT_SLOT( applySettings() ) );
|
||
15 years ago
|
|
||
|
sls->setTitle(title());
|
||
|
sls->setForegroundColor(cgroup.text());
|
||
|
sls->setBackgroundColor(cgroup.base());
|
||
|
sls->setAlarmColor(cgroup.foreground());
|
||
|
|
||
|
if (sls->exec())
|
||
|
applySettings();
|
||
|
|
||
|
delete sls;
|
||
|
sls = 0;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
SensorLogger::applySettings()
|
||
|
{
|
||
13 years ago
|
TQColorGroup cgroup = monitor->colorGroup();
|
||
15 years ago
|
|
||
|
setTitle(sls->title());
|
||
|
|
||
15 years ago
|
cgroup.setColor(TQColorGroup::Text, sls->foregroundColor());
|
||
|
cgroup.setColor(TQColorGroup::Base, sls->backgroundColor());
|
||
|
cgroup.setColor(TQColorGroup::Foreground, sls->alarmColor());
|
||
|
monitor->setPalette(TQPalette(cgroup, cgroup, cgroup));
|
||
15 years ago
|
|
||
|
setModified(true);
|
||
|
}
|
||
|
|
||
|
void
|
||
|
SensorLogger::applyStyle(void)
|
||
|
{
|
||
13 years ago
|
TQColorGroup cgroup = monitor->colorGroup();
|
||
15 years ago
|
|
||
15 years ago
|
cgroup.setColor(TQColorGroup::Text, KSGRD::Style->firstForegroundColor());
|
||
|
cgroup.setColor(TQColorGroup::Base, KSGRD::Style->backgroundColor());
|
||
|
cgroup.setColor(TQColorGroup::Foreground, KSGRD::Style->alarmColor());
|
||
|
monitor->setPalette(TQPalette(cgroup, cgroup, cgroup));
|
||
15 years ago
|
|
||
|
setModified(true);
|
||
|
}
|
||
|
|
||
|
bool
|
||
15 years ago
|
SensorLogger::restoreSettings(TQDomElement& element)
|
||
15 years ago
|
{
|
||
13 years ago
|
TQColorGroup cgroup = monitor->colorGroup();
|
||
15 years ago
|
|
||
15 years ago
|
cgroup.setColor(TQColorGroup::Text, restoreColor(element, "textColor", Qt::green));
|
||
|
cgroup.setColor(TQColorGroup::Base, restoreColor(element, "backgroundColor", Qt::black));
|
||
|
cgroup.setColor(TQColorGroup::Foreground, restoreColor(element, "alarmColor", Qt::red));
|
||
|
monitor->setPalette(TQPalette(cgroup, cgroup, cgroup));
|
||
15 years ago
|
|
||
|
logSensors.clear();
|
||
|
|
||
15 years ago
|
TQDomNodeList dnList = element.elementsByTagName("logsensors");
|
||
15 years ago
|
for (uint i = 0; i < dnList.count(); i++) {
|
||
15 years ago
|
TQDomElement element = dnList.item(i).toElement();
|
||
15 years ago
|
LogSensor* sensor = new LogSensor(monitor);
|
||
13 years ago
|
TQ_CHECK_PTR(sensor);
|
||
15 years ago
|
|
||
|
sensor->setHostName(element.attribute("hostName"));
|
||
|
sensor->setSensorName(element.attribute("sensorName"));
|
||
|
sensor->setFileName(element.attribute("fileName"));
|
||
|
sensor->setTimerInterval(element.attribute("timerInterval").toInt());
|
||
|
sensor->setLowerLimitActive(element.attribute("lowerLimitActive").toInt());
|
||
|
sensor->setLowerLimit(element.attribute("lowerLimit").toDouble());
|
||
|
sensor->setUpperLimitActive(element.attribute("upperLimitActive").toInt());
|
||
|
sensor->setUpperLimit(element.attribute("upperLimit").toDouble());
|
||
|
|
||
|
logSensors.append(sensor);
|
||
|
}
|
||
|
|
||
|
SensorDisplay::restoreSettings(element);
|
||
|
|
||
|
setModified(false);
|
||
|
|
||
|
return (true);
|
||
|
}
|
||
|
|
||
|
bool
|
||
15 years ago
|
SensorLogger::saveSettings(TQDomDocument& doc, TQDomElement& element, bool save)
|
||
15 years ago
|
{
|
||
13 years ago
|
saveColor(element, "textColor", monitor->colorGroup().text());
|
||
|
saveColor(element, "backgroundColor", monitor->colorGroup().base());
|
||
|
saveColor(element, "alarmColor", monitor->colorGroup().foreground());
|
||
15 years ago
|
|
||
|
for (LogSensor* sensor = logSensors.first(); sensor != 0; sensor = logSensors.next())
|
||
|
{
|
||
15 years ago
|
TQDomElement log = doc.createElement("logsensors");
|
||
15 years ago
|
log.setAttribute("sensorName", sensor->getSensorName());
|
||
|
log.setAttribute("hostName", sensor->getHostName());
|
||
|
log.setAttribute("fileName", sensor->getFileName());
|
||
|
log.setAttribute("timerInterval", sensor->getTimerInterval());
|
||
15 years ago
|
log.setAttribute("lowerLimitActive", TQString("%1").arg(sensor->getLowerLimitActive()));
|
||
|
log.setAttribute("lowerLimit", TQString("%1").arg(sensor->getLowerLimit()));
|
||
|
log.setAttribute("upperLimitActive", TQString("%1").arg(sensor->getUpperLimitActive()));
|
||
|
log.setAttribute("upperLimit", TQString("%1").arg(sensor->getUpperLimit()));
|
||
15 years ago
|
|
||
|
element.appendChild(log);
|
||
|
}
|
||
|
|
||
|
SensorDisplay::saveSettings(doc, element);
|
||
|
|
||
|
if (save)
|
||
|
setModified(false);
|
||
|
|
||
|
return (true);
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
SensorLogger::answerReceived(int, const TQString&)
|
||
15 years ago
|
{
|
||
|
// we do not use this, since all answers are received by the LogSensors
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
SensorLogger::resizeEvent(TQResizeEvent*)
|
||
15 years ago
|
{
|
||
|
frame()->setGeometry(0, 0, this->width(), this->height());
|
||
|
monitor->setGeometry(10, 20, this->width() - 20, this->height() - 30);
|
||
|
}
|
||
|
|
||
|
LogSensor*
|
||
15 years ago
|
SensorLogger::getLogSensor(TQListViewItem* item)
|
||
15 years ago
|
{
|
||
|
for (LogSensor* sensor = logSensors.first(); sensor != 0; sensor = logSensors.next())
|
||
|
{
|
||
|
if (item == sensor->getListViewItem()) {
|
||
|
return sensor;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
void
|
||
15 years ago
|
SensorLogger::RMBClicked(TQListViewItem* item, const TQPoint& point, int)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQPopupMenu pm;
|
||
15 years ago
|
if (hasSettingsDialog())
|
||
|
pm.insertItem(i18n("&Properties"), 1);
|
||
|
pm.insertItem(i18n("&Remove Display"), 2);
|
||
|
pm.insertSeparator(-1);
|
||
|
pm.insertItem(i18n("&Remove Sensor"), 3);
|
||
|
pm.insertItem(i18n("&Edit Sensor..."), 4);
|
||
|
|
||
|
if ( !item )
|
||
|
{
|
||
|
pm.setItemEnabled( 3, false );
|
||
|
pm.setItemEnabled( 4, false );
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
LogSensor* sensor = getLogSensor(item);
|
||
|
|
||
|
if ( sensor->isLogging() )
|
||
|
pm.insertItem(i18n("St&op Logging"), 6);
|
||
|
else
|
||
|
pm.insertItem(i18n("S&tart Logging"), 5);
|
||
|
}
|
||
|
|
||
|
switch (pm.exec(point))
|
||
|
{
|
||
|
case 1:
|
||
|
configureSettings();
|
||
|
break;
|
||
|
case 2: {
|
||
15 years ago
|
TQCustomEvent* ev = new TQCustomEvent(TQEvent::User);
|
||
15 years ago
|
ev->setData(this);
|
||
|
kapp->postEvent(parent(), ev);
|
||
|
break;
|
||
|
}
|
||
|
case 3: {
|
||
|
LogSensor* sensor = getLogSensor(item);
|
||
|
if (sensor)
|
||
|
logSensors.remove(sensor);
|
||
|
break;
|
||
|
}
|
||
|
case 4: {
|
||
|
LogSensor* sensor = getLogSensor(item);
|
||
|
if (sensor)
|
||
|
editSensor(sensor);
|
||
|
break;
|
||
|
}
|
||
|
case 5: {
|
||
|
LogSensor* sensor = getLogSensor(item);
|
||
|
if (sensor)
|
||
|
sensor->startLogging();
|
||
|
break;
|
||
|
}
|
||
|
case 6: {
|
||
|
LogSensor* sensor = getLogSensor(item);
|
||
|
if (sensor)
|
||
|
sensor->stopLogging();
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|