MythTV  master
sourceManager.cpp
Go to the documentation of this file.
1 
2 // QT headers
3 #include <QDir>
4 #include <QFileInfo>
5 #include <QString>
6 #include <QStringList>
7 #include <QApplication>
8 
9 // MythTV headers
10 #include <mythcontext.h>
11 #include <mythdbcon.h>
12 #include <mythdb.h>
13 #include <mythprogressdialog.h>
14 #include <mythdirs.h>
15 
16 // MythWeather headers
17 #include "weatherScreen.h"
18 #include "weatherSource.h"
19 #include "sourceManager.h"
20 
21 #define LOC QString("SourceManager: ")
22 #define LOC_ERR QString("SourceManager Error: ")
23 
25 {
26  findScriptsDB();
27  setupSources();
28 }
29 
31 {
32  clearSources();
33 }
34 
36 {
38  QString query =
39  "SELECT DISTINCT wss.sourceid, source_name, update_timeout, "
40  "retrieve_timeout, path, author, version, email, types "
41  "FROM weathersourcesettings wss "
42  "LEFT JOIN weatherdatalayout wdl "
43  "ON wss.sourceid = wdl.weathersourcesettings_sourceid "
44  "WHERE hostname = :HOST;";
45 
46  db.prepare(query);
47  db.bindValue(":HOST", gCoreContext->GetHostName());
48  if (!db.exec())
49  {
50  MythDB::DBError("Finding weather source scripts for host", db);
51  return false;
52  }
53 
54  while (db.next())
55  {
56  QFileInfo fi(db.value(4).toString());
57 
58  if (!fi.isExecutable())
59  {
60  // scripts will be deleted from db in the more robust (i.e. slower)
61  // findScripts() -- run when entering setup
62  continue;
63  }
64  auto *si = new ScriptInfo;
65  si->id = db.value(0).toInt();
66  si->name = db.value(1).toString();
67  si->updateTimeout = db.value(2).toUInt() * 1000;
68  si->scriptTimeout = db.value(3).toUInt();
69  si->path = fi.absolutePath();
70  si->program = fi.absoluteFilePath();
71  si->author = db.value(5).toString();
72  si->version = db.value(6).toString();
73  si->email = db.value(7).toString();
74  si->types = db.value(8).toString().split(",");
75  m_scripts.append(si);
76  }
77 
78  return true;
79 }
80 
82 {
83  QString path = GetShareDir() + "mythweather/scripts/";
84  QDir dir(path);
85  dir.setFilter(QDir::Executable | QDir::Files | QDir::Dirs);
86 
87  if (!dir.exists())
88  {
89  LOG(VB_GENERAL, LOG_ERR, "MythWeather: Scripts directory not found");
90  return false;
91  }
92  QString busymessage = tr("Searching for scripts");
93 
94  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("weather stack");
95  if (popupStack == nullptr)
96  popupStack = GetMythMainWindow()->GetStack("popup stack");
97 
98  auto *busyPopup = new MythUIBusyDialog(busymessage, popupStack,
99  "mythweatherbusydialog");
100 
101  if (busyPopup->Create())
102  {
103  popupStack->AddScreen(busyPopup, false);
104  }
105  else
106  {
107  delete busyPopup;
108  busyPopup = nullptr;
109  }
110 
111  qApp->processEvents();
112 
113  recurseDirs(dir);
114 
115  // run through and see if any scripts have been deleted
117 
118  db.prepare("SELECT sourceid, path FROM weathersourcesettings "
119  "WHERE hostname = :HOST;");
120  db.bindValue(":HOST", gCoreContext->GetHostName());
121  if (!db.exec())
122  MythDB::DBError("SourceManager::findScripts - select", db);
123  QStringList toRemove;
124  while (db.next())
125  {
126  QFileInfo fi(db.value(1).toString());
127  if (!fi.isExecutable())
128  {
129  toRemove << db.value(0).toString();
130  LOG(VB_GENERAL, LOG_ERR, QString("'%1' no longer exists")
131  .arg(fi.absoluteFilePath()));
132  }
133  }
134 
135  db.prepare("DELETE FROM weathersourcesettings WHERE sourceid = :ID;");
136  for (int i = 0; i < toRemove.count(); ++i)
137  {
138  db.bindValue(":ID", toRemove[i]);
139  if (!db.exec())
140  {
141  // MythDB::DBError("Deleting weather source settings", db);
142  }
143  }
144 
145  if (busyPopup)
146  {
147  busyPopup->Close();
148  busyPopup = nullptr;
149  }
150 
151  return m_scripts.count() > 0;
152 }
153 
155 {
156  while (!m_scripts.isEmpty())
157  delete m_scripts.takeFirst();
158  m_scripts.clear();
159 
160  while (!m_sources.isEmpty())
161  delete m_sources.takeFirst();
162  m_sources.clear();
163 }
164 
166 {
168 
169  db.prepare(
170  "SELECT DISTINCT location, weathersourcesettings_sourceid, "
171  " weatherscreens.units, weatherscreens.screen_id "
172  "FROM weatherdatalayout,weatherscreens "
173  "WHERE weatherscreens.screen_id = weatherscreens_screen_id AND "
174  " weatherscreens.hostname = :HOST");
175  db.bindValue(":HOST", gCoreContext->GetHostName());
176  if (!db.exec())
177  {
178  MythDB::DBError("Finding weather sources for this host", db);
179  return;
180  }
181 
182  m_sourcemap.clear();
183 
184  while (db.next())
185  {
186  QString loc = db.value(0).toString();
187  uint sourceid = db.value(1).toUInt();
188  units_t units = db.value(2).toUInt();
189  uint screen = db.value(3).toUInt();
190  const WeatherSource *src = needSourceFor(sourceid, loc, units);
191  if (src)
192  m_sourcemap.insert((long)screen, src);
193  }
194 }
195 
197 {
198  ScriptInfo *src = nullptr;
199  for (int x = 0; x < m_scripts.size(); x++)
200  {
201  src = m_scripts.at(x);
202  if (src->name == name)
203  {
204  return src;
205  }
206  }
207 
208  if (!src)
209  {
210  LOG(VB_GENERAL, LOG_ERR, "No Source found for " + name);
211  }
212 
213  return nullptr;
214 }
215 
216 QStringList SourceManager::getLocationList(ScriptInfo *si, const QString &str)
217 {
218  if (!m_scripts.contains(si))
219  return QStringList();
220  auto *ws = new WeatherSource(si);
221 
222  QStringList locationList(ws->getLocationList(str));
223 
224  delete ws;
225 
226  return locationList;
227 }
228 
229 WeatherSource *SourceManager::needSourceFor(int id, const QString &loc,
230  units_t units)
231 {
232  // matching source exists?
233  for (int x = 0; x < m_sources.size(); x++)
234  {
235  WeatherSource *src = m_sources.at(x);
236  if (src->getId() == id && src->getLocale() == loc &&
237  src->getUnits() == units)
238  {
239  return src;
240  }
241  }
242 
243  // no matching source, make one
244  for (int x = 0; x < m_scripts.size(); x++)
245  {
246  ScriptInfo *si = m_scripts.at(x);
247  if (si->id == id)
248  {
249  auto *ws = new WeatherSource(si);
250  ws->setLocale(loc);
251  ws->setUnits(units);
252  m_sources.append(ws);
253  return ws;
254  }
255  }
256 
257  LOG(VB_GENERAL, LOG_ERR, LOC +
258  QString("NeedSourceFor: Unable to find source for %1, %2, %3")
259  .arg(id).arg(loc).arg(units));
260  return nullptr;
261 }
262 
264 {
265  for (int x = 0; x < m_sources.size(); x++)
266  {
267  WeatherSource *src = m_sources.at(x);
268  src->startUpdateTimer();
269  }
270 }
271 
273 {
274  for (int x = 0; x < m_sources.size(); x++)
275  {
276  WeatherSource *src = m_sources.at(x);
277  src->stopUpdateTimer();
278  }
279 }
280 
281 void SourceManager::doUpdate(bool forceUpdate)
282 {
283  for (int x = 0; x < m_sources.size(); x++)
284  {
285  WeatherSource *src = m_sources.at(x);
286  if (src->inUse())
287  src->startUpdate(forceUpdate);
288  }
289 }
290 
291 bool SourceManager::findPossibleSources(QStringList types,
292  QList<ScriptInfo *> &sources)
293 {
294  for (int x = 0; x < m_scripts.size(); x++)
295  {
296  ScriptInfo *si = m_scripts.at(x);
297  QStringList stypes = si->types;
298  bool handled = true;
299  for (int i = 0; i < types.count() && handled; ++i)
300  {
301  handled = stypes.contains(types[i]);
302  }
303  if (handled)
304  sources.append(si);
305  }
306 
307  return sources.count() != 0;
308 }
309 
311 {
312  if (!screen)
313  {
314  LOG(VB_GENERAL, LOG_ERR, LOC +
315  QString("Cannot connect nonexistent screen 0x%1")
316  .arg((uint64_t)screen,0,16));
317 
318  return false;
319  }
320 
321  SourceMap::iterator it = m_sourcemap.find(id);
322  if (it == m_sourcemap.end())
323  {
324  LOG(VB_GENERAL, LOG_ERR, LOC +
325  QString("Cannot connect nonexistent source '%1'").arg(id));
326 
327  return false;
328  }
329 
330  (const_cast<WeatherSource*>(*it))->connectScreen(screen);
331 
332  return true;
333 }
334 
336 {
337  if (!screen)
338  {
339  LOG(VB_GENERAL, LOG_ERR, LOC +
340  QString("Cannot disconnect nonexistent screen 0x%1")
341  .arg((uint64_t)screen,0,16));
342 
343  return false;
344  }
345 
346  SourceMap::iterator it = m_sourcemap.find(screen->getId());
347  if (it == m_sourcemap.end())
348  {
349  LOG(VB_GENERAL, LOG_ERR, LOC +
350  QString("Cannot disconnect nonexistent source %1")
351  .arg(screen->getId()));
352 
353  return false;
354  }
355 
356  (const_cast<WeatherSource*>(*it))->disconnectScreen(screen);
357 
358  return true;
359 }
360 
361 // Recurses dir for script files
363 {
364  if (!dir.exists())
365  return;
366 
367  dir.setFilter(QDir::Executable | QDir::Files | QDir::Dirs |
368  QDir::NoDotAndDotDot);
369  QFileInfoList files = dir.entryInfoList();
370  QFileInfo file;
371 
372  for (int x = 0; x < files.size(); x++)
373  {
374  qApp->processEvents();
375  file = files.at(x);
376  if (file.isDir())
377  {
378  QDir recurseTo(file.filePath());
379  recurseDirs(recurseTo);
380  }
381 
382  if (file.isExecutable() && !(file.isDir()))
383  {
385  if (info)
386  {
387  m_scripts.append(info);
388  LOG(VB_FILE, LOG_INFO, QString("Found Script '%1'")
389  .arg(file.absoluteFilePath()));
390  }
391  }
392  }
393 }
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:782
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:863
WeatherSource * needSourceFor(int id, const QString &loc, units_t units)
static ScriptInfo * ProbeScript(const QFileInfo &fi)
QList< WeatherSource * > m_sources
Definition: sourceManager.h:44
QString name
Definition: weatherSource.h:21
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
MythScreenStack * GetStack(const QString &stackname)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QStringList types
Definition: weatherSource.h:25
QString getLocale()
Definition: weatherSource.h:59
QVariant value(int i) const
Definition: mythdbcon.h:198
bool findScriptsDB()
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
#define LOC
QString GetShareDir(void)
Definition: mythdirs.cpp:222
bool findPossibleSources(QStringList types, QList< ScriptInfo * > &sources)
unsigned int uint
Definition: compat.h:140
static MSqlQueryInfo InitCon(ConnectionReuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
bool disconnectScreen(WeatherScreen *screen)
void recurseDirs(QDir dir)
MythMainWindow * GetMythMainWindow(void)
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:807
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
SourceMap m_sourcemap
Definition: sourceManager.h:45
unsigned char units_t
Definition: weatherUtils.h:22
void startUpdate(bool forceUpdate=false)
units_t getUnits()
Definition: weatherSource.h:55
ScriptInfo * getSourceByName(const QString &name)
void doUpdate(bool forceUpdate=false)
bool connectScreen(uint id, WeatherScreen *screen)
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:179
Weather screen.
Definition: weatherScreen.h:26
void startUpdateTimer()
Definition: weatherSource.h:69
QString GetHostName(void)
QStringList getLocationList(ScriptInfo *si, const QString &str)
QList< ScriptInfo * > m_scripts
Definition: sourceManager.h:43
void stopUpdateTimer()
Definition: weatherSource.h:70