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