MythTV  master
themechooser.cpp
Go to the documentation of this file.
1 
2 // Theme Chooser headers
3 #include "themechooser.h"
4 
5 // Qt headers
6 #include <QCoreApplication>
7 #include <QRunnable>
8 #include <QRegExp>
9 
10 // MythTV headers
11 #include "mythcorecontext.h"
12 #include "mythcoreutil.h"
13 #include "mthreadpool.h"
14 #include "remotefile.h"
15 #include "mythdownloadmanager.h"
16 #include "programtypes.h"
17 #include "mythsystemevent.h"
18 #include "mythdate.h"
19 #include "mythversion.h"
20 #include "mythlogging.h"
21 #include "storagegroup.h"
22 
23 // LibMythUI headers
24 #include "mythmainwindow.h"
25 #include "mythuihelper.h"
26 #include "mythuiprogressbar.h"
27 #include "mythdialogbox.h"
28 #include "mythuibuttonlist.h"
29 #include "mythscreenstack.h"
30 #include "mythuistatetype.h"
31 #include "mythuigroup.h"
32 #include "mythuiimage.h"
33 #include "mythuitext.h"
34 
35 #define LOC QString("ThemeChooser: ")
36 #define LOC_WARN QString("ThemeChooser, Warning: ")
37 #define LOC_ERR QString("ThemeChooser, Error: ")
38 
42 class ThemeExtractThread : public QRunnable
43 {
44  public:
46  const QString &srcFile, const QString &destDir) :
47  m_parent(parent),
48  m_srcFile(srcFile),
49  m_destDir(destDir)
50  {
51  m_srcFile.detach();
52  m_destDir.detach();
53  }
54 
55  void run()
56  {
58 
59  MythEvent *me =
60  new MythEvent("THEME_INSTALLED", QStringList(m_srcFile));
61  QCoreApplication::postEvent(m_parent, me);
62  }
63 
64  private:
66  QString m_srcFile;
67  QString m_destDir;
68 };
69 
70 
76  const QString &name) :
77  MythScreenType(parent, name),
78  m_themes(NULL),
79  m_preview(NULL),
80  m_fullPreviewShowing(false),
81  m_fullPreviewStateType(NULL),
82  m_fullScreenName(NULL),
83  m_fullScreenPreview(NULL),
84  m_refreshDownloadableThemes(false),
85  m_downloadTheme(NULL),
86  m_downloadState(dsIdle),
87  m_popupMenu(NULL)
88 {
90 
91  StorageGroup sgroup("Themes", gCoreContext->GetHostName());
92  m_userThemeDir = sgroup.GetFirstDir(true);
93 }
94 
96 {
98 }
99 
100 static bool sortThemeNames(const QFileInfo &s1, const QFileInfo &s2)
101 {
102  return s1.fileName().toLower() < s2.fileName().toLower();
103 }
104 
105 
107 {
108  // Load the theme for this screen
109  if (!LoadWindowFromXML("settings-ui.xml", "themechooser", this))
110  return false;
111 
112  bool err = false;
113  UIUtilE::Assign(this, m_themes, "themes", &err);
114 
115  UIUtilW::Assign(this, m_preview, "preview");
116  UIUtilW::Assign(this, m_fullPreviewStateType, "fullpreviewstate");
117 
119  {
120  MythUIGroup *state =
121  dynamic_cast<MythUIGroup*>
122  (m_fullPreviewStateType->GetChild("fullscreen"));
123  if (state)
124  {
126  dynamic_cast<MythUIText*>(state->GetChild("fullscreenname"));
128  dynamic_cast<MythUIImage*>(state->GetChild("fullscreenpreview"));
129  }
130  }
131 
132  if (err)
133  {
134  LOG(VB_GENERAL, LOG_ERR, LOC + "Cannot load screen 'themechooser'");
135  return false;
136  }
137 
138  connect(m_themes, SIGNAL(itemClicked(MythUIButtonListItem*)),
139  this, SLOT(saveAndReload(MythUIButtonListItem*)));
140  connect(m_themes, SIGNAL(itemSelected(MythUIButtonListItem*)),
141  this, SLOT(itemChanged(MythUIButtonListItem*)));
142 
143  BuildFocusList();
144 
146 
147  return true;
148 }
149 
151 {
152  SetBusyPopupMessage(tr("Loading Installed Themes"));
153 
154  QStringList themesSeen;
155  QDir themes(m_userThemeDir);
156  themes.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
157  themes.setSorting(QDir::Name | QDir::IgnoreCase);
158 
159  m_infoList = themes.entryInfoList();
160 
161  for( QFileInfoList::iterator it = m_infoList.begin();
162  it != m_infoList.end();
163  ++it )
164  {
165  if (loadThemeInfo(*it))
166  {
167  themesSeen << (*it).fileName();
168  m_themeStatuses[(*it).fileName()] = "default";
169  }
170  }
171 
172  themes.setPath(GetThemesParentDir());
173  QFileInfoList sharedThemes = themes.entryInfoList();
174  for( QFileInfoList::iterator it = sharedThemes.begin();
175  it != sharedThemes.end();
176  ++it )
177  {
178  if ((!themesSeen.contains((*it).fileName())) &&
179  (loadThemeInfo(*it)))
180  {
181  m_infoList << *it;
182  themesSeen << (*it).fileName();
183  m_themeStatuses[(*it).fileName()] = "default";
184  }
185  }
186 
187  // MYTH_SOURCE_VERSION - examples v29-pre-574-g92517f5, v29-Pre, v29.1-21-ge26a33c
188  QString MythVersion(MYTH_SOURCE_VERSION);
189  QRegExp trunkver("v[0-9]+-pre.*",Qt::CaseInsensitive);
190  QRegExp validver("v[0-9]+.*",Qt::CaseInsensitive);
191 
192  if (!validver.exactMatch(MythVersion))
193  {
194  LOG(VB_GENERAL, LOG_ERR, QString("Invalid MythTV version %1, will use themes from trunk").arg(MythVersion));
195  MythVersion = "trunk";
196  }
197  if (trunkver.exactMatch(MythVersion))
198  MythVersion = "trunk";
199 
200  if (MythVersion == "trunk")
201  {
202  LoadVersion(MythVersion, themesSeen, true);
203  LOG(VB_GUI, LOG_INFO, QString("Loading themes for %1").arg(MythVersion));
204  }
205  else
206  {
207 
208  MythVersion = MYTH_BINARY_VERSION; // Example: 29.20161017-1
209  // Remove the date part and the rest, eg 29.20161017-1 -> 29
210  MythVersion.replace(QRegExp("\\.[0-9]{8,}.*"), "");
211  LOG(VB_GUI, LOG_INFO, QString("Loading themes for %1").arg(MythVersion));
212  LoadVersion(MythVersion, themesSeen, true);
213 
214  // If a version of the theme for this tag exists, use it...
215  // MYTH_SOURCE_VERSION - examples v29-pre-574-g92517f5, v29-Pre, v29.1-21-ge26a33c
216  QRegExp subexp("v[0-9]+\\.([0-9]+)-*");
217  // This captures the subversion, i.e. the number after a dot
218  int pos = subexp.indexIn(MYTH_SOURCE_VERSION);
219  if (pos > -1)
220  {
221  QString subversion;
222  int idx = subexp.cap(1).toInt();
223  for ( ; idx > 0; --idx)
224  {
225  subversion = MythVersion + "." + QString::number(idx);
226  LOG(VB_GUI, LOG_INFO, QString("Loading themes for %1").arg(subversion));
227  LoadVersion(subversion, themesSeen, false);
228  }
229  }
230  }
231 
232  ResetBusyPopup();
233 
234  qSort(m_infoList.begin(), m_infoList.end(), sortThemeNames);
235 }
236 
237 void ThemeChooser::LoadVersion(const QString &version,
238  QStringList &themesSeen, bool alert_user)
239 {
240  QString remoteThemesFile = GetConfDir();
241  remoteThemesFile.append("/tmp/themes.zip");
242  QString themeSite = QString("%1/%2")
243  .arg(gCoreContext->GetSetting("ThemeRepositoryURL",
244  "http://themes.mythtv.org/themes/repository")).arg(version);
245  QDir remoteThemesDir(GetMythUI()->GetThemeCacheDir()
246  .append("/themechooser/").append(version));
247 
248  int downloadFailures =
249  gCoreContext->GetNumSetting("ThemeInfoDownloadFailures", 0);
250  if (QFile::exists(remoteThemesFile))
251  {
252  QFileInfo finfo(remoteThemesFile);
253  if (finfo.lastModified().toUTC() <
254  MythDate::current().addSecs(-600))
255  {
256  LOG(VB_GUI, LOG_INFO, LOC +
257  QString("%1 is over 10 minutes old, forcing "
258  "remote theme list download").arg(remoteThemesFile));
260  }
261 
262  if (!remoteThemesDir.exists())
264  }
265  else if (downloadFailures < 2) // (and themes.zip does not exist)
266  {
267  LOG(VB_GUI, LOG_INFO, LOC +
268  QString("%1 does not exist, forcing remote theme "
269  "list download").arg(remoteThemesFile));
271  }
272 
274  {
275  QFile test(remoteThemesFile);
276  if (test.open(QIODevice::WriteOnly))
277  test.remove();
278  else
279  {
280  ShowOkPopup(tr("Unable to create '%1'").arg(remoteThemesFile));
281  return;
282  }
283 
284  SetBusyPopupMessage(tr("Refreshing Downloadable Themes Information"));
285 
286  QString url = themeSite;
287  url.append("/themes.zip");
288  QString destdir = GetMythUI()->GetThemeCacheDir();
289  destdir.append("/themechooser");
290  QString versiondir = QString("%1/%2").arg(destdir).arg(version);
291  if (!removeThemeDir(versiondir))
292  ShowOkPopup(tr("Unable to remove '%1'").arg(versiondir));
293  QDir dir;
294  if (!dir.mkpath(destdir))
295  ShowOkPopup(tr("Unable to create '%1'").arg(destdir));
296  bool result = GetMythDownloadManager()->download(url, remoteThemesFile);
297 
298  LOG(VB_GUI, LOG_INFO, LOC +
299  QString("Downloading '%1' to '%2'").arg(url).arg(remoteThemesFile));
300 
301  SetBusyPopupMessage(tr("Extracting Downloadable Themes Information"));
302 
303  if (!result || !extractZIP(remoteThemesFile, destdir))
304  {
305  QFile::remove(remoteThemesFile);
306 
307  downloadFailures++;
308  gCoreContext->SaveSetting("ThemeInfoDownloadFailures",
309  downloadFailures);
310 
311  if (!result)
312  {
313  LOG(VB_GUI, LOG_ERR, LOC +
314  QString("Failed to download '%1'").arg(url));
315  if (alert_user)
316  ShowOkPopup(tr("Failed to download '%1'").arg(url));
317  }
318  else
319  {
320  LOG(VB_GUI, LOG_ERR, LOC +
321  QString("Failed to unzip '%1' to '%2'")
322  .arg(remoteThemesFile).arg(destdir));
323  if (alert_user)
324  ShowOkPopup(tr("Failed to unzip '%1' to '%2'")
325  .arg(remoteThemesFile).arg(destdir));
326  }
327  }
328  else
329  LOG(VB_GUI, LOG_INFO, LOC +
330  QString("Unzipped '%1' to '%2'")
331  .arg(remoteThemesFile)
332  .arg(destdir));
333  }
334 
335  QDir themes;
336  themes.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
337  themes.setSorting(QDir::Name | QDir::IgnoreCase);
338 
339  if ((QFile::exists(remoteThemesFile)) &&
340  (remoteThemesDir.exists()))
341  {
342  SetBusyPopupMessage(tr("Loading Downloadable Themes"));
343 
344  LOG(VB_GUI, LOG_INFO, LOC +
345  QString("%1 and %2 exist, using cached remote themes list")
346  .arg(remoteThemesFile).arg(remoteThemesDir.absolutePath()));
347 
348  QString themesPath = remoteThemesDir.absolutePath();
349  themes.setPath(themesPath);
350 
351  QFileInfoList downloadableThemes = themes.entryInfoList();
352  for( QFileInfoList::iterator it = downloadableThemes.begin();
353  it != downloadableThemes.end();
354  ++it )
355  {
356  QString dirName = (*it).fileName();
357  QString themeName = dirName;
358  QString remoteDir = themeSite;
359  remoteDir.append("/").append(dirName);
360  QString localDir = themes.absolutePath();
361  localDir.append("/").append(dirName);
362 
363  ThemeInfo remoteTheme((*it).absoluteFilePath());
364 
365  if (themesSeen.contains(dirName))
366  {
367  ThemeInfo *localTheme = m_themeNameInfos[dirName];
368 
369  themeName = remoteTheme.GetName();
370 
371  int rmtMaj = remoteTheme.GetMajorVersion();
372  int rmtMin = remoteTheme.GetMinorVersion();
373  int locMaj = localTheme->GetMajorVersion();
374  int locMin = localTheme->GetMinorVersion();
375 
376  if ((rmtMaj > locMaj) ||
377  ((rmtMaj == locMaj) &&
378  (rmtMin > locMin)))
379  {
380  if (loadThemeInfo(*it))
381  {
382  LOG(VB_GUI, LOG_DEBUG, LOC +
383  QString("'%1' old version %2.%3, new version %4.%5")
384  .arg(themeName).arg(locMaj).arg(locMin)
385  .arg(rmtMaj).arg(rmtMin));
386 
387  m_infoList << *it;
388  m_themeStatuses[themeName] = "updateavailable";
389 
390  QFileInfo finfo(remoteTheme.GetPreviewPath());
392  remoteDir.append("/").append(finfo.fileName()),
393  localDir.append("/").append(finfo.fileName()),
394  NULL);
395  }
396  }
397  else if ((rmtMaj == locMaj) &&
398  (rmtMin == locMin))
399  {
400  LOG(VB_GUI, LOG_DEBUG, LOC +
401  QString("'%1' up to date (%2.%3)")
402  .arg(themeName).arg(locMaj).arg(locMin));
403 
404  m_themeStatuses[themeName] = "uptodate";
405  }
406  }
407  else
408  {
409  LOG(VB_GUI, LOG_DEBUG, LOC +
410  QString("'%1' (%2.%3) available")
411  .arg(themeName)
412  .arg(remoteTheme.GetMajorVersion())
413  .arg(remoteTheme.GetMinorVersion()));
414 
415  ThemeInfo *remoteTheme = loadThemeInfo(*it);
416  if (remoteTheme)
417  {
418  themeName = remoteTheme->GetName();
419  themesSeen << dirName;
420  m_infoList << *it;
421  m_themeStatuses[themeName] = "updateavailable";
422 
423  QFileInfo finfo(remoteTheme->GetPreviewPath());
425  remoteDir.append("/").append(finfo.fileName()),
426  localDir.append("/").append(finfo.fileName()),
427  NULL);
428  }
429  }
430  }
431  }
432 
433 }
434 
436 {
437  QString curTheme = gCoreContext->GetSetting("Theme");
438  ThemeInfo *themeinfo = NULL;
439  ThemeInfo *curThemeInfo = NULL;
440  MythUIButtonListItem *item = NULL;
441 
442  m_themes->Reset();
443  for( QFileInfoList::iterator it = m_infoList.begin();
444  it != m_infoList.end();
445  ++it )
446  {
447  QFileInfo &theme = *it;
448 
449  if (!m_themeFileNameInfos.contains(theme.filePath()))
450  continue;
451 
452  themeinfo = m_themeFileNameInfos[theme.filePath()];
453  if (!themeinfo)
454  continue;
455 
456  QString buttonText = QString("%1 %2.%3")
457  .arg(themeinfo->GetName())
458  .arg(themeinfo->GetMajorVersion())
459  .arg(themeinfo->GetMinorVersion());
460 
461  item = new MythUIButtonListItem(m_themes, buttonText);
462  if (item)
463  {
464  if (themeinfo->GetDownloadURL().isEmpty())
465  item->DisplayState("local", "themelocation");
466  else
467  item->DisplayState("remote", "themelocation");
468 
469  item->DisplayState(themeinfo->GetAspect(), "aspectstate");
470 
471  item->DisplayState(m_themeStatuses[themeinfo->GetName()],
472  "themestatus");
473  InfoMap infomap;
474  themeinfo->ToMap(infomap);
475  item->SetTextFromMap(infomap);
476  item->SetData(qVariantFromValue(themeinfo));
477 
478  QString thumbnail = themeinfo->GetPreviewPath();
479  QFileInfo fInfo(thumbnail);
480  // Downloadable themeinfos have thumbnail copies of their preview images
481  if (!themeinfo->GetDownloadURL().isEmpty())
482  thumbnail = thumbnail.append(".thumb.jpg");
483  item->SetImage(thumbnail);
484 
485  if (curTheme == themeinfo->GetDirectoryName())
486  curThemeInfo = themeinfo;
487  }
488  }
489 
491 
492  if (curThemeInfo)
493  m_themes->SetValueByData(qVariantFromValue(curThemeInfo));
494 
496  if (current)
497  itemChanged(current);
498 
499  QString testFile = m_userThemeDir + "/.test";
500  QFile test(testFile);
501  if (test.open(QIODevice::WriteOnly))
502  test.remove();
503  else
504  {
505  ShowOkPopup(tr("Error creating test file, %1 themes directory is "
506  "not writable.").arg(m_userThemeDir));
507  }
508 }
509 
511 {
512  if (theme.fileName() == "default" || theme.fileName() == "default-wide")
513  return NULL;
514 
515  ThemeInfo *themeinfo = NULL;
516  if (theme.exists()) // local directory vs http:// or remote URL
517  themeinfo = new ThemeInfo(theme.absoluteFilePath());
518  else
519  themeinfo = new ThemeInfo(theme.filePath());
520 
521  if (!themeinfo)
522  return NULL;
523 
524  if (themeinfo->GetName().isEmpty() || !(themeinfo->GetType() & THEME_UI))
525  {
526  delete themeinfo;
527  return NULL;
528  }
529 
530  m_themeFileNameInfos[theme.filePath()] = themeinfo;
531  m_themeNameInfos[theme.fileName()] = themeinfo;
532 
533  return themeinfo;
534 }
535 
537 {
538  if (m_popupMenu)
539  return;
540 
541  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
542  QString label = tr("Theme Chooser Menu");
543 
544  m_popupMenu =
545  new MythDialogBox(label, popupStack, "themechoosermenupopup");
546 
547  connect(m_popupMenu, SIGNAL(Closed(QString, int)), SLOT(popupClosed(QString, int)));
548 
549  if (m_popupMenu->Create())
550  popupStack->AddScreen(m_popupMenu);
551  else
552  {
553  delete m_popupMenu;
554  m_popupMenu = NULL;
555  return;
556  }
557 
558  m_popupMenu->SetReturnEvent(this, "popupmenu");
559 
561  {
563  m_popupMenu->AddButton(tr("Hide Fullscreen Preview"),
564  SLOT(toggleFullscreenPreview()));
565  else
566  m_popupMenu->AddButton(tr("Show Fullscreen Preview"),
567  SLOT(toggleFullscreenPreview()));
568  }
569 
570  m_popupMenu->AddButton(tr("Refresh Downloadable Themes"),
571  SLOT(refreshDownloadableThemes()));
572 
574  if (current)
575  {
576  ThemeInfo *info = current->GetData().value<ThemeInfo *>();
577 
578  if (info)
579  {
580  m_popupMenu->AddButton(tr("Select Theme"),
581  SLOT(saveAndReload()));
582 
583  if (info->GetPreviewPath().startsWith(m_userThemeDir))
584  m_popupMenu->AddButton(tr("Delete Theme"),
585  SLOT(removeTheme()));
586  }
587  }
588 
589  if (gCoreContext->GetNumSetting("ThemeUpdateNofications", 1))
590  m_popupMenu->AddButton(tr("Disable Theme Update Notifications"),
592  else
593  m_popupMenu->AddButton(tr("Enable Theme Update Notifications"),
595 }
596 
597 void ThemeChooser::popupClosed(QString which, int result)
598 {
599  (void)which;
600  (void)result;
601 
602  m_popupMenu = NULL;
603 }
604 
605 bool ThemeChooser::keyPressEvent(QKeyEvent *event)
606 {
607  if (GetFocusWidget()->keyPressEvent(event))
608  return true;
609 
610  bool handled = false;
611  QStringList actions;
612  handled = GetMythMainWindow()->TranslateKeyPress("Theme Chooser", event, actions);
613 
614  for (int i = 0; i < actions.size() && !handled; ++i)
615  {
616  QString action = actions[i];
617  handled = true;
618 
619  if (action == "MENU")
620  showPopupMenu();
621  else if (action == "DELETE")
622  removeTheme();
623  else if ((action == "ESCAPE") &&
625  {
627  }
628  else
629  handled = false;
630  }
631 
632  if (!handled && MythScreenType::keyPressEvent(event))
633  handled = true;
634 
635  return handled;
636 }
637 
639 {
641  {
643  {
646 
647  if (m_fullScreenName)
649 
651  m_fullPreviewShowing = false;
652  }
653  else
654  {
656  ThemeInfo *info = item->GetData().value<ThemeInfo*>();
657  if (info)
658  {
660  {
663  }
664 
665  if (m_fullScreenName)
666  m_fullScreenName->SetText(info->GetName());
667 
668  m_fullPreviewStateType->DisplayState("fullscreen");
669  m_fullPreviewShowing = true;
670  }
671  }
672  }
673 }
674 
676 {
677  if (gCoreContext->GetNumSetting("ThemeUpdateNofications", 1))
678  gCoreContext->SaveSettingOnHost("ThemeUpdateNofications", "0", "");
679  else
680  gCoreContext->SaveSettingOnHost("ThemeUpdateNofications", "1", "");
681 }
682 
684 {
685  LOG(VB_GUI, LOG_INFO, LOC + "Forcing remote theme list refresh");
687  gCoreContext->SaveSetting("ThemeInfoDownloadFailures", 0);
689 }
690 
692 {
694  if (current)
695  saveAndReload(current);
696 }
697 
699 {
700  ThemeInfo *info = item->GetData().value<ThemeInfo *>();
701 
702  if (!info)
703  return;
704 
705  if (!info->GetDownloadURL().isEmpty())
706  {
707  QString testFile = m_userThemeDir + "/.test";
708  QFile test(testFile);
709  if (test.open(QIODevice::WriteOnly))
710  test.remove();
711  else
712  {
713  ShowOkPopup(tr("Unable to install theme, %1 themes directory is "
714  "not writable.").arg(m_userThemeDir));
715  return;
716  }
717 
718  QString downloadURL = info->GetDownloadURL();
719  QFileInfo qfile(downloadURL);
720  QString baseName = qfile.fileName();
721 
722  if (!gCoreContext->GetSetting("ThemeDownloadURL").isEmpty())
723  {
724  QStringList tokens =
725  gCoreContext->GetSetting("ThemeDownloadURL")
726  .split(";", QString::SkipEmptyParts);
727  QString origURL = downloadURL;
728  downloadURL.replace(tokens[0], tokens[1]);
729  LOG(VB_FILE, LOG_WARNING, LOC +
730  QString("Theme download URL overridden from %1 to %2.")
731  .arg(origURL).arg(downloadURL));
732  }
733 
734  OpenBusyPopup(tr("Downloading %1 Theme").arg(info->GetName()));
735  m_downloadTheme = info;
736 #if 0
737  m_downloadFile = RemoteDownloadFile(downloadURL,
738  "Temp", baseName);
740 #else
741  QString localFile = GetConfDir() + "/tmp/" + baseName;
742  GetMythDownloadManager()->queueDownload(downloadURL, localFile, this);
743  m_downloadFile = localFile;
745 #endif
746  }
747  else
748  {
749  gCoreContext->SaveSetting("Theme", info->GetDirectoryName());
750  GetMythMainWindow()->JumpTo("Reload Theme");
751  }
752 }
753 
755 {
756  ThemeInfo *info = item->GetData().value<ThemeInfo*>();
757 
758  if (!info)
759  return;
760 
761  QFileInfo preview(info->GetPreviewPath());
762  InfoMap infomap;
763  info->ToMap(infomap);
764  SetTextFromMap(infomap);
765  if (m_preview)
766  {
767  if (preview.exists())
768  {
770  m_preview->Load();
771  }
772  else
773  m_preview->Reset();
774  }
776  {
778  {
779  if (preview.exists())
780  {
783  }
784  else
786  }
787 
788  if (m_fullScreenName)
789  m_fullScreenName->SetText(info->GetName());
790  }
791 
792  MythUIStateType *themeLocation =
793  dynamic_cast<MythUIStateType*>(GetChild("themelocation"));
794  if (themeLocation)
795  {
796  if (info->GetDownloadURL().isEmpty())
797  themeLocation->DisplayState("local");
798  else
799  themeLocation->DisplayState("remote");
800  }
801 
802  MythUIStateType *aspectState =
803  dynamic_cast<MythUIStateType*>(GetChild("aspectstate"));
804  if (aspectState)
805  aspectState->DisplayState(info->GetAspect());
806 }
807 
808 void ThemeChooser::updateProgressBar(int bytesReceived,
809  int bytesTotal)
810 {
811  MythUIProgressBar *progressBar =
812  dynamic_cast<MythUIProgressBar *>(GetChild("downloadprogressbar"));
813 
814  if (!progressBar)
815  return;
816 
817  progressBar->SetUsed(bytesReceived);
818  progressBar->SetTotal(bytesTotal);
819 }
820 
822 {
823  if ((MythEvent::Type)(e->type()) == MythEvent::MythEventMessage)
824  {
825  MythEvent *me = (MythEvent *)e;
826  QStringList tokens = me->Message().split(" ", QString::SkipEmptyParts);
827 
828  if (tokens.isEmpty())
829  return;
830 
831  if (tokens[0] == "DOWNLOAD_FILE")
832  {
833  QStringList args = me->ExtraDataList();
834  if ((m_downloadState == dsIdle) ||
835  (tokens.size() != 2) ||
836  (!m_downloadTheme) ||
837  (args[1] != m_downloadFile))
838  return;
839 
840  if (tokens[1] == "UPDATE")
841  {
842  updateProgressBar(args[2].toInt(), args[3].toInt());
843  }
844  else if (tokens[1] == "FINISHED")
845  {
846  bool remoteFileIsLocal = false;
847  int fileSize = args[2].toInt();
848  int errorCode = args[4].toInt();
849 
850  CloseBusyPopup();
851 
852  QFileInfo file(m_downloadFile);
854  (m_downloadFile.startsWith("myth://")))
855  {
856  // The backend download is finished so start the
857  // frontend download
858  if ((errorCode == 0) &&
859  (fileSize > 0))
860  {
862  QString localFile = GetConfDir() + "/tmp/" +
863  file.fileName();
864  file.setFile(localFile);
865 
866  if (file.exists())
867  {
868  remoteFileIsLocal = true;
869  m_downloadFile = localFile;
870  }
871  else
872  {
874  m_downloadFile, localFile, this);
875  OpenBusyPopup(tr("Copying %1 Theme Package")
876  .arg(m_downloadTheme->GetName()));
877  m_downloadFile = localFile;
878  return;
879  }
880  }
881  else
882  {
884  ShowOkPopup(tr("ERROR downloading theme package on master backend."));
885  }
886  }
887 
889  (file.exists()))
890  {
891  // The frontend download is finished
892  if ((errorCode == 0) &&
893  (fileSize > 0))
894  {
896  ThemeExtractThread *extractThread =
900  extractThread, "ThemeExtract");
901 
902  if (!remoteFileIsLocal)
903  RemoteFile::DeleteFile(args[0]);
904 
905  OpenBusyPopup(tr("Installing %1 Theme")
906  .arg(m_downloadTheme->GetName()));
907  }
908  else
909  {
911  ShowOkPopup(tr("ERROR downloading theme package from master backend."));
912  }
913  }
914  }
915  }
916  else if ((me->Message() == "THEME_INSTALLED") &&
917  (m_downloadTheme) &&
919  {
921  CloseBusyPopup();
922  QStringList args = me->ExtraDataList();
923  QFile::remove(args[0]);
924 
925  QString event = QString("THEME_INSTALLED PATH %1")
926  .arg(m_userThemeDir +
929 
931 
932  // Send a message to ourself so we trigger a reload our next chance
933  MythEvent *me = new MythEvent("THEME_RELOAD");
934  qApp->postEvent(this, me);
935  }
936  else if ((me->Message() == "THEME_RELOAD") &&
937  (m_downloadState == dsIdle))
938  {
939  GetMythMainWindow()->JumpTo("Reload Theme");
940  }
941  }
942 }
943 
945 {
947  if (!current)
948  {
949  ShowOkPopup(tr("Error, no theme selected."));
950  return;
951  }
952 
953  ThemeInfo *info = current->GetData().value<ThemeInfo *>();
954  if (!info)
955  {
956  ShowOkPopup(tr("Error, unable to find current theme."));
957  return;
958  }
959 
960  if (!info->GetPreviewPath().startsWith(m_userThemeDir))
961  {
962  ShowOkPopup(tr("%1 is not a user-installed theme and can not "
963  "be deleted.").arg(info->GetName()));
964  return;
965  }
966 
968 
970 }
971 
972 bool ThemeChooser::removeThemeDir(const QString &dirname)
973 {
974  if ((!dirname.startsWith(m_userThemeDir)) &&
975  (!dirname.startsWith(GetMythUI()->GetThemeCacheDir())))
976  return true;
977 
978  QDir dir(dirname);
979 
980  if (!dir.exists())
981  return true;
982 
983  dir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
984  QFileInfoList list = dir.entryInfoList();
985  QFileInfoList::const_iterator it = list.begin();
986  const QFileInfo *fi;
987 
988  while (it != list.end())
989  {
990  fi = &(*it++);
991  if (fi->isFile() && !fi->isSymLink())
992  {
993  if (!QFile::remove(fi->absoluteFilePath()))
994  return false;
995  }
996  else if (fi->isDir() && !fi->isSymLink())
997  {
998  if (!removeThemeDir(fi->absoluteFilePath()))
999  return false;
1000  }
1001  }
1002 
1003  return dir.rmdir(dirname);
1004 }
1005 
1007 
1009  m_updateTimer(new QTimer(this))
1010 {
1011  QString version = MYTH_SOURCE_PATH;
1012 
1013  if (!version.isEmpty() && !version.startsWith("fixes/"))
1014  // Treat devel branches as master
1015  m_mythVersions << "trunk";
1016  else
1017  {
1018  version = MYTH_BINARY_VERSION; // Example: 0.25.20101017-1
1019  version.replace(QRegExp("\\.[0-9]{8,}.*"), "");
1020 
1021  // If a version of the theme for this tag exists, use it...
1022  QRegExp subexp("v[0-9]+.[0-9]+.([0-9]+)-*");
1023  int pos = subexp.indexIn(MYTH_SOURCE_VERSION);
1024  if (pos > -1)
1025  {
1026  QString subversion;
1027  int idx = subexp.cap(1).toInt();
1028  for ( ; idx > 0; --idx)
1029  m_mythVersions << version + "." + QString::number(idx);
1030  }
1032  }
1033 
1036  "remotethemes/themes.zip",
1037  "Temp");
1038 
1039  gCoreContext->SaveSetting("ThemeUpdateStatus", "");
1040 
1041  connect(m_updateTimer, SIGNAL(timeout()), SLOT(checkForUpdate()));
1042 
1043  if (getenv("MYTHTV_DEBUGMDM"))
1044  {
1045  LOG(VB_GENERAL, LOG_INFO, "Checking for theme updates every minute");
1046  m_updateTimer->start(60 * 1000); // Run once a minute
1047  }
1048  else
1049  {
1050  LOG(VB_GENERAL, LOG_INFO, "Checking for theme updates every hour");
1051  m_updateTimer->start(60 * 60 * 1000); // Run once an hour
1052  }
1053 
1054  // Run once 15 seconds from now
1055  QTimer::singleShot(15 * 1000, this, SLOT(checkForUpdate()));
1056 }
1057 
1059 {
1060  if (m_updateTimer)
1061  {
1062  m_updateTimer->stop();
1063  delete m_updateTimer;
1064  m_updateTimer = NULL;
1065  }
1066 }
1067 
1069 {
1070  if (GetMythUI()->GetCurrentLocation(false, true) != "mainmenu")
1071  return;
1072 
1073  ThemeInfo *localTheme = NULL;
1074  int locMaj = 0;
1075  int locMin = 0;
1076 
1078  {
1079  QStringList::iterator Iversion;
1080 
1081  for (Iversion = m_mythVersions.begin();
1082  Iversion != m_mythVersions.end(); ++Iversion)
1083  {
1084 
1085  QString remoteThemeDir =
1088  QString("remotethemes/%1/%2")
1089  .arg(*Iversion)
1090  .arg(GetMythUI()->GetThemeName()),
1091  "Temp");
1092 
1093  QString infoXML = remoteThemeDir;
1094  infoXML.append("/themeinfo.xml");
1095 
1096  LOG(VB_GUI, LOG_INFO, QString("ThemeUpdateChecker Loading '%1'")
1097  .arg(infoXML));
1098 
1099  if (RemoteFile::Exists(infoXML))
1100  {
1101  ThemeInfo *remoteTheme = new ThemeInfo(remoteThemeDir);
1102  if (!remoteTheme || remoteTheme->GetType() & THEME_UNKN)
1103  {
1104  LOG(VB_GENERAL, LOG_ERR,
1105  QString("ThemeUpdateChecker::checkForUpdate(): "
1106  "Unable to create ThemeInfo for %1")
1107  .arg(infoXML));
1108  delete remoteTheme;
1109  remoteTheme = NULL;
1110  return;
1111  }
1112 
1113  if (!localTheme)
1114  {
1115  localTheme = new ThemeInfo(GetMythUI()->GetThemeDir());
1116  if (!localTheme || localTheme->GetType() & THEME_UNKN)
1117  {
1118  LOG(VB_GENERAL, LOG_ERR,
1119  "ThemeUpdateChecker::checkForUpdate(): "
1120  "Unable to create ThemeInfo for current theme");
1121  delete localTheme;
1122  localTheme = NULL;
1123  return;
1124  }
1125  locMaj = localTheme->GetMajorVersion();
1126  locMin = localTheme->GetMinorVersion();
1127  }
1128 
1129  int rmtMaj = remoteTheme->GetMajorVersion();
1130  int rmtMin = remoteTheme->GetMinorVersion();
1131 
1132  delete remoteTheme;
1133  remoteTheme = NULL;
1134 
1135  if ((rmtMaj > locMaj) ||
1136  ((rmtMaj == locMaj) &&
1137  (rmtMin > locMin)))
1138  {
1140  QString("%1-%2.%3").arg(GetMythUI()->GetThemeName())
1141  .arg(rmtMaj).arg(rmtMin);
1142 
1143  QString status = gCoreContext->GetSetting
1144  ("ThemeUpdateStatus");
1145  QString currentLocation = GetMythUI()->GetCurrentLocation
1146  (false, true);
1147 
1148  if ((!status.startsWith(m_lastKnownThemeVersion)) &&
1149  (currentLocation == "mainmenu"))
1150  {
1151  m_currentVersion = QString("%1.%2")
1152  .arg(locMaj).arg(locMin);
1153  m_newVersion = QString("%1.%2").arg(rmtMaj).arg(rmtMin);
1154 
1155  gCoreContext->SaveSetting("ThemeUpdateStatus",
1157  + " notified");
1158 
1159  QString message = tr("Version %1 of the %2 theme is now "
1160  "available in the Theme Chooser. "
1161  "The currently installed version "
1162  "is %3.")
1163  .arg(m_newVersion)
1164  .arg(GetMythUI()->GetThemeName())
1165  .arg(m_currentVersion);
1166 
1167  ShowOkPopup(message);
1168  break;
1169  }
1170  }
1171  }
1172  }
1173  }
1174 
1175  delete localTheme;
1176 }
1177 
1178 /* vim: set expandtab tabstop=4 shiftwidth=4: */
This widget is used for grouping other widgets for display when a particular named state is called...
void ToMap(InfoMap &infoMap) const
Definition: themeinfo.cpp:258
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=NULL)
Definition: mythuiutils.h:27
static bool DeleteFile(const QString &url)
Definition: remotefile.cpp:430
bool removeThemeDir(const QString &dirname)
ThemeInfo * loadThemeInfo(QFileInfo &theme)
int GetMajorVersion() const
Definition: themeinfo.h:36
void Reset(void)
Reset the widget to it&#39;s original state, should not reset changes made by the theme.
void updateProgressBar(int bytesReceived, int bytesTotal)
ThemeChooser(MythScreenStack *parent, const QString &name="ThemeChooser")
Creates a new ThemeChooser Screen.
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:83
QString GenMythURL(QString host=QString(), QString port=QString(), QString path=QString(), QString storageGroup=QString())
void start(QRunnable *runnable, QString debugName, int priority=0)
MythConfirmationDialog * ShowOkPopup(const QString &message, QObject *parent, const char *slot, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
All purpose text widget, displays a text string.
Definition: mythuitext.h:28
ThemeChooser * m_parent
void queueDownload(const QString &url, const QString &dest, QObject *caller, const bool reload=false)
Adds a url to the download queue.
void SetData(QVariant data)
QString GetAspect() const
Definition: themeinfo.h:28
void SaveSetting(const QString &key, int newValue)
static Type MythEventMessage
Definition: mythevent.h:65
void removeListener(QObject *listener)
Remove a listener to the observable.
int GetMasterServerPort(void)
Image widget, displays a single image or multiple images in sequence.
Definition: mythuiimage.h:97
Basic menu dialog, message and a list of options.
QString m_userThemeDir
Definition: themechooser.h:84
void Reset(void)
Reset the widget to it&#39;s original state, should not reset changes made by the theme.
Definition: mythuitext.cpp:114
static bool downloadURL(const QString &url, QByteArray *buffer)
static bool sortThemeNames(const QFileInfo &s1, const QFileInfo &s2)
virtual bool Create(void)
void removeTheme(void)
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:166
MythScreenStack * GetStack(const QString &stackname)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QString m_lastKnownThemeVersion
Definition: themechooser.h:113
QString GetCurrentLocation(bool fullPath=false, bool mainStackOnly=true)
void addListener(QObject *listener)
Add a listener to the observable.
QMap< QString, ThemeInfo * > m_themeFileNameInfos
Definition: themechooser.h:87
DownloadState m_downloadState
Definition: themechooser.h:91
void itemChanged(MythUIButtonListItem *item)
bool extractZIP(const QString &zipFile, const QString &outDir)
bool Load(bool allowLoadInBackground=true, bool forceStat=false)
Load the image(s), wraps ImageLoader::LoadImage()
void BuildFocusList(void)
QString m_currentVersion
Definition: themechooser.h:114
bool m_fullPreviewShowing
Definition: themechooser.h:77
void SetImage(MythImage *image, const QString &name="")
Sets an image directly, should only be used in special circumstances since it bypasses the cache...
QString GetName() const
Definition: themeinfo.h:30
void checkForUpdate(void)
QString GetThemeCacheDir(void)
QString GetConfDir(void)
Definition: mythdirs.cpp:212
void showPopupMenu(void)
void AddButton(const QString &title, QVariant data=0, bool newMenu=false, bool setCurrent=false)
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
void SetValueByData(QVariant data)
This class is used as a container for messages.
Definition: mythevent.h:15
void Reset()
Reset the widget to it&#39;s original state, should not reset changes made by the theme.
const char * name
Definition: ParseText.cpp:338
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
void customEvent(QEvent *e)
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
QString GetSetting(const QString &key, const QString &defaultval="")
void Load(void)
Load data which will ultimately be displayed on-screen or used to determine what appears on-screen (S...
View and select installed themes.
Definition: themechooser.h:27
Create a group of widgets.
Definition: mythuigroup.h:11
MythDownloadManager * GetMythDownloadManager(void)
Gets the pointer to the MythDownloadManager singleton.
MythUIImage * m_fullScreenPreview
Definition: themechooser.h:80
bool download(const QString &url, const QString &dest, const bool reload=false)
Downloads a URL to a file in blocking mode.
void ReloadInBackground(void)
void refreshDownloadableThemes(void)
virtual void SetTextFromMap(const InfoMap &infoMap)
QString GetMasterHostName(void)
int GetMinorVersion() const
Definition: themeinfo.h:37
bool m_refreshDownloadableThemes
Definition: themechooser.h:83
void LoadVersion(const QString &version, QStringList &themesSeen, bool alert_user)
void JumpTo(const QString &destination, bool pop=true)
void SetBusyPopupMessage(const QString &message)
MythDialogBox * m_popupMenu
Definition: themechooser.h:93
void Init(void)
Used after calling Load() to assign data to widgets and other UI initilisation which is prohibited in...
MythUIHelper * GetMythUI()
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
MythUIType * GetFocusWidget(void) const
MythMainWindow * GetMythMainWindow(void)
MythUIImage * m_preview
Definition: themechooser.h:75
QString RemoteDownloadFile(const QString &url, const QString &storageGroup, const QString &filename)
static MThreadPool * globalInstance(void)
bool keyPressEvent(QKeyEvent *)
Key event handler.
static bool Exists(const QString &url, struct stat *fileinfo)
Definition: remotefile.cpp:473
int GetNumSetting(const QString &key, int defaultval=0)
void toggleFullscreenPreview(void)
QMap< QString, ThemeInfo * > m_themeNameInfos
Definition: themechooser.h:86
int GetType() const
Definition: themeinfo.h:35
void CloseBusyPopup(void)
QMap< QString, QString > m_themeStatuses
Definition: themechooser.h:88
bool Create(void)
ThemeInfo * m_downloadTheme
Definition: themechooser.h:89
virtual bool keyPressEvent(QKeyEvent *)
Key event handler.
QString GetDownloadURL() const
Definition: themeinfo.h:39
void SetFilename(const QString &filename)
Must be followed by a call to Load() to load the image.
void SetReturnEvent(QObject *retobject, const QString &resultid)
QFileInfoList m_infoList
Definition: themechooser.h:82
bool SetFocusWidget(MythUIType *widget=NULL)
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true) MUNUSED_RESULT
Get a list of actions for a keypress in the given context.
QString m_downloadFile
Definition: themechooser.h:90
bool DisplayState(const QString &name)
string themeName
Definition: mythburn.py:189
void toggleThemeUpdateNotifications(void)
void saveAndReload(void)
QString GetThemesParentDir(void)
Definition: mythdirs.cpp:213
static const QString LOC
QTimer * m_updateTimer
Definition: themechooser.h:110
void OpenBusyPopup(QString message="")
MythUIButtonList * m_themes
Definition: themechooser.h:74
void popupClosed(QString which, int result)
bool SaveSettingOnHost(const QString &key, const QString &newValue, const QString &host)
Screen in which all other widgets are contained and rendered.
QString GetDirectoryName() const
Definition: themeinfo.h:43
Progress bar widget.
QStringList m_mythVersions
Definition: themechooser.h:111
const QString & Message() const
Definition: mythevent.h:57
void Reset(void)
Reset the image back to the default defined in the theme.
ThemeExtractThread(ThemeChooser *parent, const QString &srcFile, const QString &destDir)
QString GetHostName(void)
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:150
void DisplayState(const QString &state, const QString &name)
void SetTextFromMap(const InfoMap &infoMap, const QString &state="")
void SendSystemEvent(const QString &msg)
MythUIText * m_fullScreenName
Definition: themechooser.h:79
const QStringList & ExtraDataList() const
Definition: mythevent.h:59
void ResetBusyPopup(void)
QString GetPreviewPath() const
Definition: themeinfo.h:34
MythUIStateType * m_fullPreviewStateType
Definition: themechooser.h:78
MythUIButtonListItem * GetItemCurrent() const
void LoadInBackground(QString message="")