MythTV  0.28pre
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
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  ScriptInfo *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 == NULL)
96  popupStack = GetMythMainWindow()->GetStack("popup stack");
97 
98  MythUIBusyDialog *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 = NULL;
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 = NULL;
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 = 0;
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 NULL;
214 }
215 
216 QStringList SourceManager::getLocationList(ScriptInfo *si, const QString &str)
217 {
218  if (!m_scripts.contains(si))
219  return QStringList();
220  WeatherSource *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?
234  for (int x = 0; x < m_sources.size(); x++)
235  {
236  src = m_sources.at(x);
237  if (src->getId() == id && src->getLocale() == loc &&
238  src->getUnits() == units)
239  {
240  return src;
241  }
242  }
243 
244  // no matching source, make one
245  ScriptInfo *si;
246  for (int x = 0; x < m_scripts.size(); x++)
247  {
248  si = m_scripts.at(x);
249  if (si->id == id)
250  {
251  WeatherSource *ws = new WeatherSource(si);
252  ws->setLocale(loc);
253  ws->setUnits(units);
254  m_sources.append(ws);
255  return ws;
256  }
257  }
258 
259  LOG(VB_GENERAL, LOG_ERR, LOC +
260  QString("NeedSourceFor: Unable to find source for %1, %2, %3")
261  .arg(id).arg(loc).arg(units));
262  return NULL;
263 }
264 
266 {
268  for (int x = 0; x < m_sources.size(); x++)
269  {
270  src = m_sources.at(x);
271  src->startUpdateTimer();
272  }
273 }
274 
276 {
278  for (int x = 0; x < m_sources.size(); x++)
279  {
280  src = m_sources.at(x);
281  src->stopUpdateTimer();
282  }
283 }
284 
285 void SourceManager::doUpdate(bool forceUpdate)
286 {
288  for (int x = 0; x < m_sources.size(); x++)
289  {
290  src = m_sources.at(x);
291  if (src->inUse())
292  src->startUpdate(forceUpdate);
293  }
294 }
295 
297  QList<ScriptInfo *> &sources)
298 {
299  ScriptInfo *si;
300  bool handled;
301  for (int x = 0; x < m_scripts.size(); x++)
302  {
303  si = m_scripts.at(x);
304  QStringList stypes = si->types;
305  handled = true;
306  int i;
307  for (i = 0; i < types.count() && handled; ++i)
308  {
309  handled = stypes.contains(types[i]);
310  }
311  if (handled)
312  sources.append(si);
313  }
314 
315  if (sources.count())
316  return true;
317 
318  return false;
319 }
320 
322 {
323  if (!screen)
324  {
325  LOG(VB_GENERAL, LOG_ERR, LOC +
326  QString("Cannot connect nonexistent screen 0x%1")
327  .arg((uint64_t)screen,0,16));
328 
329  return false;
330  }
331 
332  SourceMap::iterator it = m_sourcemap.find(id);
333  if (it == m_sourcemap.end())
334  {
335  LOG(VB_GENERAL, LOG_ERR, LOC +
336  QString("Cannot connect nonexistent source '%1'").arg(id));
337 
338  return false;
339  }
340 
341  (const_cast<WeatherSource*>(*it))->connectScreen(screen);
342 
343  return true;
344 }
345 
347 {
348  if (!screen)
349  {
350  LOG(VB_GENERAL, LOG_ERR, LOC +
351  QString("Cannot disconnect nonexistent screen 0x%1")
352  .arg((uint64_t)screen,0,16));
353 
354  return false;
355  }
356 
357  SourceMap::iterator it = m_sourcemap.find(screen->getId());
358  if (it == m_sourcemap.end())
359  {
360  LOG(VB_GENERAL, LOG_ERR, LOC +
361  QString("Cannot disconnect nonexistent source %1")
362  .arg(screen->getId()));
363 
364  return false;
365  }
366 
367  (const_cast<WeatherSource*>(*it))->disconnectScreen(screen);
368 
369  return true;
370 }
371 
372 // Recurses dir for script files
374 {
375  if (!dir.exists())
376  return;
377 
378  dir.setFilter(QDir::Executable | QDir::Files | QDir::Dirs |
379  QDir::NoDotAndDotDot);
380  QFileInfoList files = dir.entryInfoList();
381  QFileInfo file;
382 
383  for (int x = 0; x < files.size(); x++)
384  {
385  qApp->processEvents();
386  file = files.at(x);
387  if (file.isDir())
388  {
389  QDir recurseTo(file.filePath());
390  recurseDirs(recurseTo);
391  }
392 
393  if (file.isExecutable() && !(file.isDir()))
394  {
396  if (info)
397  {
398  m_scripts.append(info);
399  LOG(VB_FILE, LOG_INFO, QString("Found Script '%1'")
400  .arg(file.absoluteFilePath()));
401  }
402  }
403  }
404 
405  return;
406 }