/***************************************************************************
                          sensorslist.cpp  -  description
                             -------------------
    begin                : mié abr 24 2002
    copyright            : (C) 2002 by Miguel Novas
    email                : michaell@teleline.es
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "sensorslist.h"

#include <tdeglobal.h>

SensorsList::SensorsList(TQObject *parent, const char * name)
                                  : TQObject(parent,name)
{
 tempScale= Sensor::dgCelsius;
 updateInterval= 5000;
}

SensorsList::~SensorsList()
{
}


void SensorsList::setMonitorized(bool enable)
{
 if(monitorized!=enable) {
   monitorized= enable;
   if(enable) startTimer( updateInterval );
   else       killTimers();
 }
}

void SensorsList::setUpdateInterval(int seconds)
{
 seconds= seconds*1000;
 if(updateInterval!=seconds) {
    updateInterval= seconds;
    if(monitorized) {
       setMonitorized(false);
       setMonitorized(true);
    }
 }
}

void SensorsList::setTempScale(Sensor::TempScale scale)
{
 if(tempScale!=scale) {
     tempScale= scale;
     TQObjectList *list= getSensors();
     if(list)
       for(Sensor *sensor=(Sensor*)list->first(); sensor!=0; sensor= (Sensor*)list->next())
         if(sensor->getType()==Sensor::lmTemp)
           emit sensor->configChanged();
 }
}


void SensorsList::readConfig()
{
 TDEConfig *ksConfig= TDEGlobal::config();
 ksConfig->setGroup( name() );
 setUpdateInterval(ksConfig->readNumEntry("UpdateInterval",5));
 TQString str= ksConfig->readEntry("Scale" ,"C");
 Sensor::TempScale tempScale;
 if(str=="F") tempScale= Sensor::dgFahrenheit;
 else if(str=="K") tempScale= Sensor::dgKelvin;
      else             tempScale= Sensor::dgCelsius;
 setTempScale(tempScale);
}


void SensorsList::writeConfig()
{
 TDEConfig *ksConfig= TDEGlobal::config();
 ksConfig->setGroup( name() );
 ksConfig->writeEntry("UpdateInterval",updateInterval/1000);
 TQString str;
 switch(tempScale) {
  case Sensor::dgCelsius   : str='C'; break;
  case Sensor::dgKelvin    : str='K'; break;
  case Sensor::dgFahrenheit: str='F'; break;
 }
 ksConfig->writeEntry("Scale" ,str);
}


//****************************************************************************
// Protected methods
//****************************************************************************

void SensorsList::timerEvent( TQTimerEvent * )
{
 updateSensors();
}

//***************

void SensorsList::childEvent ( TQChildEvent *e )
{
 if(e->inserted()) {
    connect((Sensor *)e->child(),TQ_SIGNAL(configChanged()),this,TQ_SLOT(slotConfigChanged()));
    connect((Sensor *)e->child(),TQ_SIGNAL(valueChanged(double)) ,this,TQ_SLOT(slotValueChanged()));
 }
}

//****************************************************************************
// Protected slots
//****************************************************************************


void SensorsList::slotConfigChanged()
{
 emit configChanged( sender()->name() );
}

void SensorsList::slotValueChanged()
{
 emit valueChanged( (Sensor *)sender() );
}


/*********************************************************************************/

#include "sensorslist.moc"
