PageRenderTime 46ms CodeModel.GetById 13ms app.highlight 28ms RepoModel.GetById 2ms app.codeStats 0ms

/xbmc/utils/Weather.h

http://github.com/xbmc/xbmc
C++ Header | 173 lines | 122 code | 27 blank | 24 comment | 1 complexity | ad359203369a9114e8f1eb2d15a97b2f MD5 | raw file
  1#pragma once
  2
  3/*
  4 *      Copyright (C) 2005-2013 Team XBMC
  5 *      http://xbmc.org
  6 *
  7 *  This Program is free software; you can redistribute it and/or modify
  8 *  it under the terms of the GNU General Public License as published by
  9 *  the Free Software Foundation; either version 2, or (at your option)
 10 *  any later version.
 11 *
 12 *  This Program is distributed in the hope that it will be useful,
 13 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15 *  GNU General Public License for more details.
 16 *
 17 *  You should have received a copy of the GNU General Public License
 18 *  along with XBMC; see the file COPYING.  If not, see
 19 *  <http://www.gnu.org/licenses/>.
 20 *
 21 */
 22
 23#include "InfoLoader.h"
 24#include "settings/lib/ISettingCallback.h"
 25#include "utils/GlobalsHandling.h"
 26
 27#include <map>
 28#include <string>
 29
 30class TiXmlElement;
 31
 32#define WEATHER_LABEL_LOCATION   10
 33#define WEATHER_IMAGE_CURRENT_ICON 21
 34#define WEATHER_LABEL_CURRENT_COND 22
 35#define WEATHER_LABEL_CURRENT_TEMP 23
 36#define WEATHER_LABEL_CURRENT_FEEL 24
 37#define WEATHER_LABEL_CURRENT_UVID 25
 38#define WEATHER_LABEL_CURRENT_WIND 26
 39#define WEATHER_LABEL_CURRENT_DEWP 27
 40#define WEATHER_LABEL_CURRENT_HUMI 28
 41
 42struct day_forecast
 43{
 44  std::string m_icon;
 45  std::string m_overview;
 46  std::string m_day;
 47  std::string m_high;
 48  std::string m_low;
 49};
 50
 51#define NUM_DAYS 7
 52
 53class CWeatherInfo
 54{
 55public:
 56  day_forecast forecast[NUM_DAYS];
 57
 58  void Reset()
 59  {
 60    lastUpdateTime.clear();
 61    currentIcon.clear();
 62    currentConditions.clear();
 63    currentTemperature.clear();
 64    currentFeelsLike.clear();
 65    currentWind.clear();
 66    currentHumidity.clear();
 67    currentUVIndex.clear();
 68    currentDewPoint.clear();
 69
 70    for (int i = 0; i < NUM_DAYS; i++)
 71    {
 72      forecast[i].m_icon.clear();
 73      forecast[i].m_overview.clear();
 74      forecast[i].m_day.clear();
 75      forecast[i].m_high.clear();
 76      forecast[i].m_low.clear();
 77    }
 78  };
 79
 80  std::string lastUpdateTime;
 81  std::string location;
 82  std::string currentIcon;
 83  std::string currentConditions;
 84  std::string currentTemperature;
 85  std::string currentFeelsLike;
 86  std::string currentUVIndex;
 87  std::string currentWind;
 88  std::string currentDewPoint;
 89  std::string currentHumidity;
 90  std::string busyString;
 91  std::string naIcon;
 92};
 93
 94class CWeatherJob : public CJob
 95{
 96public:
 97  CWeatherJob(int location);
 98
 99  virtual bool DoWork();
100
101  const CWeatherInfo &GetInfo() const;
102private:
103  void LocalizeOverview(std::string &str);
104  void LocalizeOverviewToken(std::string &str);
105  void LoadLocalizedToken();
106  static int ConvertSpeed(int speed);
107
108  void SetFromProperties();
109
110  /*! \brief Formats a celcius temperature into a string based on the users locale
111   \param text the string to format
112   \param temp the temperature (in degrees celcius).
113   */
114  static void FormatTemperature(std::string &text, double temp);
115
116  struct ci_less : std::binary_function<std::string, std::string, bool>
117  {
118    // case-independent (ci) compare_less binary function
119    struct nocase_compare : public std::binary_function<unsigned char,unsigned char,bool>
120    {
121      bool operator() (const unsigned char& c1, const unsigned char& c2) const {
122          return tolower (c1) < tolower (c2);
123      }
124    };
125    bool operator() (const std::string & s1, const std::string & s2) const {
126      return std::lexicographical_compare
127        (s1.begin (), s1.end (),
128        s2.begin (), s2.end (),
129        nocase_compare ());
130    }
131  };
132
133  std::map<std::string, int, ci_less> m_localizedTokens;
134  typedef std::map<std::string, int, ci_less>::const_iterator ilocalizedTokens;
135
136  CWeatherInfo m_info;
137  int m_location;
138
139  static bool m_imagesOkay;
140};
141
142class CWeather : public CInfoLoader,
143                 public ISettingCallback
144{
145public:
146  CWeather(void);
147  virtual ~CWeather(void);
148  static bool GetSearchResults(const std::string &strSearch, std::string &strResult);
149
150  std::string GetLocation(int iLocation);
151  const std::string &GetLastUpdateTime() const { return m_info.lastUpdateTime; };
152  const day_forecast &GetForecast(int day) const;
153  bool IsFetched();
154  void Reset();
155
156  void SetArea(int iLocation);
157  int GetArea() const;
158protected:
159  virtual CJob *GetJob() const override;
160  virtual std::string TranslateInfo(int info) const override;
161  virtual std::string BusyInfo(int info) const override;
162  virtual void OnJobComplete(unsigned int jobID, bool success, CJob *job) override;
163
164  virtual void OnSettingChanged(const CSetting *setting) override;
165  virtual void OnSettingAction(const CSetting *setting) override;
166
167private:
168
169  CWeatherInfo m_info;
170};
171
172XBMC_GLOBAL_REF(CWeather, g_weatherManager);
173#define g_weatherManager XBMC_GLOBAL_USE(CWeather)