MythTV  master
weatherUtils.cpp
Go to the documentation of this file.
1 // QT headers
2 #include <QString>
3 #include <QStringList>
4 #include <QCoreApplication>
5 
6 #include <mythdirs.h>
7 #include "mythmainwindow.h"
8 #include "mythuihelper.h"
9 
10 // MythWeather headers
11 #include "weatherUtils.h"
12 
13 static QString getScreenTitle(const QString &screenName)
14 {
15  if (screenName == "Current Conditions")
16  return QCoreApplication::translate("(Weather Screens)",
17  "Current Conditions");
18  if (screenName == "Three Day Forecast")
19  return QCoreApplication::translate("(Weather Screens)",
20  "Three Day Forecast");
21  if (screenName == "18 Hour Forecast")
22  return QCoreApplication::translate("(Weather Screens)",
23  "18 Hour Forecast");
24  if (screenName == "Severe Weather Alerts")
25  return QCoreApplication::translate("(Weather Screens)",
26  "Severe Weather Alerts");
27  if (screenName == "Six Day Forecast")
28  return QCoreApplication::translate("(Weather Screens)",
29  "Six Day Forecast");
30  if (screenName == "Static Map")
31  return QCoreApplication::translate("(Weather Screens)",
32  "Static Map");
33  if (screenName == "Animated Map")
34  return QCoreApplication::translate("(Weather Screens)",
35  "Animated Map");
36 
37  return screenName;
38 }
39 
41 {
42  ScreenListMap screens;
43  QStringList searchpath = GetMythUI()->GetThemeSearchPath();
44 
45  // Check the theme first if it has its own weather-screens.xml
46 
47  QStringList::iterator it;
48  for (it = searchpath.begin(); it != searchpath.end(); ++it)
49  {
50  QString filename = (*it) + "weather-screens.xml";
51  if (doLoadScreens(filename, screens))
52  {
53  LOG(VB_GENERAL, LOG_INFO,
54  QString("Loading from: %1").arg(filename));
55  break;
56  }
57  }
58 
59  // Also load from the default file in case the theme file doesn't
60  // exist or the theme file doesn't define all the screens
61 
62  QString filename = GetShareDir() + "mythweather/weather-screens.xml";
63 
64  if (!doLoadScreens(filename, screens))
65  {
66  LOG(VB_GENERAL, LOG_ERR,
67  QString("Unable to parse weather-screens.xml"));
68  }
69 
70  return screens;
71 }
72 
73 bool doLoadScreens(const QString &filename, ScreenListMap &screens)
74 {
75  QFile f(filename);
76  QDomDocument doc;
77 
78  if (!f.open(QIODevice::ReadOnly))
79  {
80  return false;
81  }
82 
83  if ( !doc.setContent( &f ) )
84  {
85  f.close();
86  return false;
87  }
88  f.close();
89 
90  QDomElement docElem = doc.documentElement();
91 
92  for (QDomNode n = docElem.firstChild(); !n.isNull();
93  n = n.nextSibling())
94  {
95  QDomElement e = n.toElement();
96  if (!e.isNull())
97  {
98  if ( (e.tagName() == "screen") && !screens.contains(e.attribute("name")) )
99  {
100  screens[e.attribute("name")].m_multiLoc = false;
101  screens[e.attribute("name")].m_name = e.attribute("name");
102  screens[e.attribute("name")].m_title =
103  getScreenTitle(e.attribute("name"));
104  QString hasUnits = e.attribute("hasunits");
105  if (hasUnits.toLower() == "no")
106  screens[e.attribute("name")].m_hasUnits = false;
107  else
108  screens[e.attribute("name")].m_hasUnits = true;
109  screens[e.attribute("name")].m_dataTypes = loadScreen(e);
110  }
111  }
112  }
113  return true;
114 }
115 
116 QStringList loadScreen(const QDomElement& ScreenListInfo)
117 {
118 
119  QStringList typesList;
120 
121  for (QDomNode n = ScreenListInfo.firstChild(); !n.isNull();
122  n = n.nextSibling())
123  {
124  QDomElement e = n.toElement();
125  if (!e.isNull())
126  {
127  if (e.tagName() == "datum")
128  {
129  QString name = e.attribute("name");
130  typesList << name;
131  }
132  }
133  }
134 
135  return typesList;
136 }
QStringList loadScreen(const QDomElement &ScreenListInfo)
QMap< QString, ScreenListInfo > ScreenListMap
Definition: weatherUtils.h:79
QStringList GetThemeSearchPath(void)
bool doLoadScreens(const QString &filename, ScreenListMap &screens)
QString GetShareDir(void)
Definition: mythdirs.cpp:222
const char * name
Definition: ParseText.cpp:328
MythUIHelper * GetMythUI()
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
ScreenListMap loadScreens()
static QString getScreenTitle(const QString &screenName)