Logo Search packages:      
Sourcecode: kdetoys version File versions

weatherservice.cpp

/* $Id: weatherservice.cpp,v 1.38 2004/06/07 00:04:03 jratke Exp $ */
#include <kdebug.h>
#include <kapplication.h>
#include <kconfig.h>
#include <kglobal.h>
#include <klocale.h>
#include <kstandarddirs.h>

#include "weatherlib.h"
#include "weatherservice.h"
#include "stationdatabase.h"
#include "sun.h"

WeatherService::WeatherService(QObject *parent, const char *name) : QObject (parent, name),  DCOPObject("WeatherService")
{
      kdDebug(12006) << "Starting new service... " << endl;

      stationDB = new StationDatabase();

      m_weatherLib = new WeatherLib(stationDB, this, "WeatherLib");
      connect(m_weatherLib, SIGNAL(fileUpdating( const QString&)),
                  SLOT(updating( const QString&)));
      connect(m_weatherLib, SIGNAL(fileUpdate( const QString&)),
                  SLOT(updated( const QString&)));
      connect(m_weatherLib, SIGNAL(stationRemoved(const QString&)),
                  SLOT(slotStationRemoved(const QString&)));

      KConfig *conf = kapp->config();

      conf->setGroup("WEATHERSTATIONS");
      QStringList stations =conf->readListEntry("stations");
      for ( QStringList::Iterator it = stations.begin (); it != stations.end(); ++it )
            m_weatherLib->update(*it);
}

WeatherService::~WeatherService()
{
      kdDebug(12006) << "Going away... " << endl;
      // Don't need to call saveSettings() because WeatherService::exit already does that.
      // Plus it wouldn't work here anyway because we can't get the config after calling
      // kapp->quit();
      delete stationDB;
}

void WeatherService::updated(const QString &stationID)
{
      kdDebug(12006) << "Sending update for " << stationID << endl;
      emit fileUpdate( stationID );
}

void WeatherService::updateAll()
{
      kdDebug(12006) << "Sending for all" << endl;
      QStringList stations =  m_weatherLib->stations();
      for  ( QStringList::Iterator it = stations.begin(); it != stations.end(); ++it ) {
            update(*it);
      }

}

void WeatherService::updating(const QString &stationID)
{
      kdDebug(12006) << "Sending updating for " << stationID << endl;
      emit fileUpdating( stationID );
}

void WeatherService::slotStationRemoved(const QString &stationID)
{
  kdDebug(12006) << "Sending stationRemoved for " << stationID << endl;
  emit stationRemoved( stationID );
}

QString WeatherService::temperature(const QString &stationID)
{
      kdDebug (12006) << "Returning " << stationID << endl;
      return m_weatherLib->temperature(stationID);
}

QString WeatherService::dewPoint(const QString &stationID)
{
      return m_weatherLib->dewPoint(stationID);
}

QString WeatherService::relativeHumidity(const QString &stationID)
{
      return m_weatherLib->relHumidity(stationID);
}

QString WeatherService::heatIndex(const QString &stationID)
{
      return m_weatherLib->heatIndex(stationID);
}

QString WeatherService::windChill(const QString &stationID)
{
      return m_weatherLib->windChill(stationID);
}

QString WeatherService::wind(const QString &stationID)
{
      return m_weatherLib->wind(stationID);
}

QString WeatherService::pressure(const QString &stationID)
{
      return m_weatherLib->pressure(stationID);
}

QPixmap WeatherService::currentIcon(const QString &stationID)
{
    return icon( stationID );
}

QPixmap WeatherService::icon(const QString &stationID)
{
      kdDebug(12006) << "Get the current weather icon.." << endl;
      QString icon  = iconFileName(stationID);
      QPixmap theIcon = QPixmap(icon);
      return theIcon;
}

QString WeatherService::currentIconString(const QString &stationID)
{
      return m_weatherLib->iconName(stationID);
}

QString WeatherService::iconFileName(const QString &stationID)
{
      QString icon  = m_weatherLib->iconName(stationID);
      icon = locate( "data", "kweather/" + icon + ".png" );
      return icon;
}

QString WeatherService::date(const QString &stationID)
{
      return m_weatherLib->date(stationID);
}

QString WeatherService::visibility(const QString &stationID)
{
      return m_weatherLib->visibility(stationID);
}

QStringList WeatherService::cover(const QString &stationID)
{
      return m_weatherLib->cover(stationID);
}

QStringList WeatherService::weather(const QString &stationID)
{
      return m_weatherLib->weather(stationID);
}

bool WeatherService::stationNeedsMaintenance(const QString &stationID)
{
      return m_weatherLib->stationNeedsMaintenance(stationID);
}

void WeatherService::update(const QString &stationID)
{
      m_weatherLib->update(stationID);
}

void WeatherService::forceUpdate(const QString &stationID)
{
      m_weatherLib->forceUpdate(stationID);
}

void WeatherService::removeStation(const QString &stationID)
{
      m_weatherLib->remove(stationID);
      saveSettings();
}

void WeatherService::addStation(const QString &stationID)
{
      m_weatherLib->update(stationID);
      saveSettings();
}

void WeatherService::exit()
{
      saveSettings();
      kapp->quit();
}

QStringList WeatherService::listStations()
{
      return m_weatherLib->stations();
}

void WeatherService::saveSettings()
{
      KConfig *conf = kapp->config();
      conf->setGroup("WEATHERSTATIONS");
      conf->writeEntry( "stations", m_weatherLib->stations());
      conf->sync();
}

QString WeatherService::stationName(const QString &stationID)
{
      if ( stationDB )
      {
            QString upperStationID = stationID.upper();
            return stationDB->stationNameFromID(upperStationID);
      }
      else
            return stationID;
}
QString WeatherService::stationCountry(const QString &stationID)
{

      if ( stationDB )
      {
            QString upperStationID = stationID.upper();
            return stationDB->stationCountryFromID(upperStationID);
      }
      else
            return stationID;
}
QString WeatherService::longitude(const QString &stationID)
{
      if ( stationDB )
      {
            QString upperStationID = stationID.upper();
            return stationDB->stationLongitudeFromID(upperStationID);
      }
      else
            return "None";
}
QString WeatherService::latitude(const QString &stationID)
{
      if ( stationDB )
      {
            QString upperStationID = stationID.upper();
            return stationDB->stationLatitudeFromID(upperStationID);
      }
      else
            return "None";
}

QStringList WeatherService::findStations(float lon, float lat)
{
      QStringList stationList;
      stationList << "KMKE" << "KPNE" << "KTPW";
      return stationList;
}

QString WeatherService::getTime(const QString &stationID, TimeType timeType)
{
      QString upperStationID = stationID.upper();

      QString latitude  = stationDB->stationLatitudeFromID(upperStationID);
      QString longitude = stationDB->stationLongitudeFromID(upperStationID);

      if ( latitude.compare(  i18n("Unknown Station" ) ) == 0  ||
           longitude.compare( i18n("Unknown Station" ) ) == 0 )
      {
            return i18n( "Unknown Station" );
      }
      else
      {
            Sun theSun( latitude, longitude );
            
            QTime time;
            switch ( timeType )
            {
                  case RISE:
                        time = theSun.computeRiseTime();
                        break;
                  case SET:
                        time = theSun.computeSetTime();
                        break;
                  case CIVIL_START:
                        time = theSun.computeCivilTwilightStart();
                        break;
                  case CIVIL_END:
                        time = theSun.computeCivilTwilightEnd();
                        break;
            }

            kdDebug(12006) << "station, lat, lon, time: " << upperStationID << " " <<
                  latitude << " " << longitude << " " << time << endl;

            return KGlobal::locale()->formatTime(time);
      }
}

QString WeatherService::sunRiseTime(const QString &stationID)
{
      return getTime(stationID, RISE);
}

QString WeatherService::sunSetTime(const QString &stationID)
{
      return getTime(stationID, SET);
}

QString WeatherService::civilTwilightStart(const QString &stationID)
{
      return getTime(stationID, CIVIL_START);
}

QString WeatherService::civilTwilightEnd(const QString &stationID)
{
      return getTime(stationID, CIVIL_END);
}



#include "weatherservice.moc"

Generated by  Doxygen 1.6.0   Back to index