MythTV  master
netbase.cpp
Go to the documentation of this file.
1 #include <QDir>
2 
3 #include <libmyth/mythcontext.h>
4 #include <libmythbase/mythdate.h>
5 #include <libmythbase/mythdirs.h>
14 #include <libmythui/mythuihelper.h>
15 
16 #include "netbase.h"
17 
18 NetBase::NetBase(MythScreenStack *parent, const char *name)
19  : MythScreenType(parent, name),
20  m_popupStack(GetMythMainWindow()->GetStack("popup stack")),
21  m_imageDownload(new MetadataImageDownload(this))
22 {
24 }
25 
27 {
28  CleanCacheDir();
29 
30  qDeleteAll(m_grabberList);
31  m_grabberList.clear();
32 
34 
35  delete m_imageDownload;
36  m_imageDownload = nullptr;
37 
39 }
40 
42 {
43  LoadData();
44 }
45 
46 void NetBase::DownloadVideo(const QString &url, const QString &dest)
47 {
49  m_downloadFile = RemoteDownloadFile(url, "Default", dest);
50 }
51 
53 {
54  QString message = tr("Downloading Video...");
56  m_popupStack, "videodownloadprogressdialog");
57 
58  if (m_progressDialog->Create())
60  else
61  {
62  delete m_progressDialog;
63  m_progressDialog = nullptr;
64  }
65 }
66 
68 {
69  QString cache = QString("%1/cache/netvision-thumbcache")
70  .arg(GetConfDir());
71  QDir cacheDir(cache);
72  QStringList thumbs = cacheDir.entryList(QDir::Files);
73 
74  for (auto i = thumbs.crbegin(); i != thumbs.crend(); ++i)
75  {
76  QString filename = QString("%1/%2").arg(cache, *i);
77  LOG(VB_GENERAL, LOG_DEBUG, QString("Deleting file %1").arg(filename));
78  QFileInfo fi(filename);
79  QDateTime lastmod = fi.lastModified();
80  if (lastmod.addDays(7) < MythDate::current())
81  QFile::remove(filename);
82  }
83 }
84 
86 {
87  ResultItem *item = GetStreamItem();
88 
89  if (!item)
90  return;
91 
92  if (!item->GetDownloadable())
93  {
94  ShowWebVideo();
95  return;
96  }
97 
98  auto seconds = std::chrono::seconds(item->GetTime().toInt());
99  GetMythMainWindow()->HandleMedia("Internal", item->GetMediaURL(),
100  item->GetDescription(), item->GetTitle(), item->GetSubtitle(),
101  QString(), item->GetSeason(), item->GetEpisode(), QString(),
102  duration_cast<std::chrono::minutes>(seconds), item->GetDate().toString("yyyy"));
103 }
104 
106 {
107  ResultItem *item = GetStreamItem();
108 
109  if (!item)
110  return;
111 
112  if (!item->GetPlayer().isEmpty())
113  {
114  const QString& cmd = item->GetPlayer();
115  QStringList args = item->GetPlayerArguments();
116  if (args.empty())
117  {
118  args += item->GetMediaURL();
119  if (args.empty())
120  args += item->GetURL();
121  }
122  else
123  {
124  args.replaceInStrings("%DIR%", GetConfDir() + "/MythNetvision");
125  args.replaceInStrings("%MEDIAURL%", item->GetMediaURL());
126  args.replaceInStrings("%URL%", item->GetURL());
127  args.replaceInStrings("%TITLE%", item->GetTitle());
128  }
129 
130  QString playerCommand = cmd + " " + args.join(" ");
131  RunCmdWithoutScreensaver(playerCommand);
132  }
133  else
134  {
135  QString url = item->GetURL();
136 
137  LOG(VB_GENERAL, LOG_DEBUG, QString("Web URL = %1").arg(url));
138 
139  if (url.isEmpty())
140  return;
141 
142  QString browser = gCoreContext->GetSetting("WebBrowserCommand", "");
143  QString zoom = gCoreContext->GetSetting("WebBrowserZoomLevel", "1.0");
144 
145  if (browser.isEmpty())
146  {
147  ShowOkPopup(tr("No browser command set! MythNetTree needs "
148  "MythBrowser installed to display the video."));
149  return;
150  }
151 
152  if (browser.toLower() == "internal")
153  {
154  GetMythMainWindow()->HandleMedia("WebBrowser", url);
155  }
156  else
157  {
158  url.replace("mythflash://", "http://");
159  QString cmd = browser;
160  cmd.replace("%ZOOM%", zoom);
161  cmd.replace("%URL%", url);
162  cmd.replace('\'', "%27");
163  cmd.replace("&","\\&");
164  cmd.replace(";","\\;");
165 
167  }
168  }
169 }
170 
171 void NetBase::RunCmdWithoutScreensaver(const QString &cmd)
172 {
175  GetMythMainWindow()->AllowInput(false);
177  GetMythMainWindow()->AllowInput(true);
180 }
181 
183 {
184  QString message = tr("Are you sure you want to delete this file?");
185 
186  auto *confirmdialog = new MythConfirmationDialog(m_popupStack, message);
187 
188  if (confirmdialog->Create())
189  {
190  m_popupStack->AddScreen(confirmdialog);
191  connect(confirmdialog, &MythConfirmationDialog::haveResult,
192  this, &NetBase::DoDeleteVideo);
193  }
194  else
195  delete confirmdialog;
196 }
197 
198 void NetBase::DoDeleteVideo(bool remove)
199 {
200  if (!remove)
201  return;
202 
203  ResultItem *item = GetStreamItem();
204 
205  if (!item)
206  return;
207 
208  QString filename = GetDownloadFilename(item->GetTitle(),
209  item->GetMediaURL());
210 
211  if (filename.startsWith("myth://"))
213  else
214  {
215  QFile file(filename);
216  file.remove();
217  }
218 }
219 
220 void NetBase::customEvent(QEvent *event)
221 {
222  if (event->type() == MythEvent::kMythEventMessage)
223  {
224  auto *me = dynamic_cast<MythEvent *>(event);
225  if (me == nullptr)
226  return;
227 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
228  QStringList tokens = me->Message().split(" ", QString::SkipEmptyParts);
229 #else
230  QStringList tokens = me->Message().split(" ", Qt::SkipEmptyParts);
231 #endif
232 
233  if (tokens.isEmpty())
234  return;
235 
236  if (tokens[0] == "DOWNLOAD_FILE")
237  {
238  QStringList args = me->ExtraDataList();
239  if ((tokens.size() != 2) ||
240  (args[1] != m_downloadFile))
241  return;
242 
243  if (tokens[1] == "UPDATE")
244  {
245  QString message = tr("Downloading Video...\n"
246  "(%1 of %2 MB)")
247  .arg(QString::number(args[2].toInt() / 1024.0 / 1024.0,
248  'f', 2),
249  QString::number(args[3].toInt() / 1024.0 / 1024.0,
250  'f', 2));
251  m_progressDialog->SetMessage(message);
252  m_progressDialog->SetTotal(args[3].toInt());
253  m_progressDialog->SetProgress(args[2].toInt());
254  }
255  else if (tokens[1] == "FINISHED")
256  {
257  int fileSize = args[2].toInt();
258  int errorCode = args[4].toInt();
259 
260  if (m_progressDialog)
261  {
263  m_progressDialog = nullptr;
264  }
265 
266  if ((m_downloadFile.startsWith("myth://")))
267  {
268  if ((errorCode == 0) &&
269  (fileSize > 0))
270  {
272  }
273  else
274  {
275  ShowOkPopup(tr("Error downloading video to backend."));
276  }
277  }
278  }
279  }
280  }
281 }
282 
284 {
285  ResultItem *item = GetStreamItem();
286  if (!item)
287  return;
288 
289  QString baseFilename = GetDownloadFilename(item->GetTitle(),
290  item->GetMediaURL());
291 
292  QString finalFilename = generate_file_url("Default",
294  baseFilename);
295 
296  LOG(VB_GENERAL, LOG_INFO, QString("Downloading %1 to %2")
297  .arg(item->GetMediaURL(), finalFilename));
298 
299  // Does the file already exist?
300  bool exists = RemoteFile::Exists(finalFilename);
301 
302  if (exists)
303  {
304  DoPlayVideo(finalFilename);
305  return;
306  }
307  DownloadVideo(item->GetMediaURL(), baseFilename);
308 }
309 
310 void NetBase::DoPlayVideo(const QString &filename)
311 {
312  ResultItem *item = GetStreamItem();
313  if (!item)
314  return;
315 
316  GetMythMainWindow()->HandleMedia("Internal", filename);
317 }
318 
320 {
321  ResultItem *item = GetStreamItem();
322  if (!item)
323  return;
324 
325  QString filename = GetDownloadFilename(item->GetTitle(),
326  item->GetMediaURL());
327 
329 }
build_compdb.args
args
Definition: build_compdb.py:11
MythUIProgressDialog::Create
bool Create(void) override
Definition: mythprogressdialog.cpp:130
generate_file_url
QString generate_file_url(const QString &storage_group, const QString &host, const QString &path)
Definition: videoutils.h:65
build_compdb.dest
dest
Definition: build_compdb.py:9
MythCoreContext::GetMasterHostName
QString GetMasterHostName(void)
Definition: mythcorecontext.cpp:806
MythEvent::kMythEventMessage
static const Type kMythEventMessage
Definition: mythevent.h:79
GetDownloadFilename
QString GetDownloadFilename(const QString &title, const QString &url)
Definition: netutils.cpp:854
MetadataImageDownload
Definition: metadataimagedownload.h:87
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:386
ResultItem::GetPlayer
const QString & GetPlayer() const
Definition: rssparse.h:147
RemoteFile::Exists
static bool Exists(const QString &url, struct stat *fileinfo)
Definition: remotefile.cpp:454
mythscreenstack.h
NetBase::~NetBase
~NetBase() override
Definition: netbase.cpp:26
NetBase::SlotDeleteVideo
void SlotDeleteVideo(void)
Definition: netbase.cpp:182
MythEvent
This class is used as a container for messages.
Definition: mythevent.h:16
ResultItem::GetTitle
const QString & GetTitle() const
Definition: rssparse.h:134
MythConfirmationDialog::haveResult
void haveResult(bool)
mythdialogbox.h
MythScreenStack
Definition: mythscreenstack.h:16
MythMainWindow::RestoreScreensaver
static void RestoreScreensaver()
Definition: mythmainwindow.cpp:577
NetBase::InitProgressDialog
void InitProgressDialog()
Definition: netbase.cpp:52
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:45
NetBase::customEvent
void customEvent(QEvent *event) override
Definition: netbase.cpp:220
build_compdb.file
file
Definition: build_compdb.py:55
mythdirs.h
MythUIProgressDialog::SetMessage
void SetMessage(const QString &message)
Definition: mythprogressdialog.cpp:205
myth_system
uint myth_system(const QString &command, uint flags, std::chrono::seconds timeout)
Definition: mythsystemlegacy.cpp:506
remoteutil.h
NetBase::m_downloadFile
QString m_downloadFile
Definition: netbase.h:52
NetBase::Init
void Init() override
Used after calling Load() to assign data to widgets and other UI initilisation which is prohibited in...
Definition: netbase.cpp:41
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:14
mythprogressdialog.h
MythEvent::Message
const QString & Message() const
Definition: mythevent.h:65
MythMainWindow::HandleMedia
bool HandleMedia(const QString &Handler, const QString &Mrl, const QString &Plot="", const QString &Title="", const QString &Subtitle="", const QString &Director="", int Season=0, int Episode=0, const QString &Inetref="", std::chrono::minutes LenMins=2h, const QString &Year="1895", const QString &Id="", bool UseBookmarks=false)
Definition: mythmainwindow.cpp:1496
NetBase::DownloadVideo
void DownloadVideo(const QString &url, const QString &dest)
Definition: netbase.cpp:46
NetBase::DoPlayVideo
void DoPlayVideo()
Definition: netbase.cpp:319
MythObservable::addListener
void addListener(QObject *listener)
Add a listener to the observable.
Definition: mythobservable.cpp:38
MythMainWindow::DisableScreensaver
static void DisableScreensaver()
Definition: mythmainwindow.cpp:583
NetBase::m_popupStack
MythScreenStack * m_popupStack
Definition: netbase.h:48
mythdate.h
GetConfDir
QString GetConfDir(void)
Definition: mythdirs.cpp:256
ResultItem::GetDate
const QDateTime & GetDate() const
Definition: rssparse.h:143
remotefile.h
NetBase::GetStreamItem
virtual ResultItem * GetStreamItem()=0
NetBase::CleanCacheDir
static void CleanCacheDir()
Definition: netbase.cpp:67
metadataimagedownload.h
NetBase::m_grabberList
GrabberScript::scriptList m_grabberList
Definition: netbase.h:53
MythUIProgressDialog
Definition: mythprogressdialog.h:59
MythUIProgressDialog::SetProgress
void SetProgress(uint count)
Definition: mythprogressdialog.cpp:199
NetBase::m_imageDownload
MetadataImageDownload * m_imageDownload
Definition: netbase.h:50
ResultItem::GetTime
const QString & GetTime() const
Definition: rssparse.h:144
ResultItem::GetSeason
const uint & GetSeason() const
Definition: rssparse.h:156
NetBase::StreamWebVideo
void StreamWebVideo(void)
Definition: netbase.cpp:85
RemoteFile::DeleteFile
static bool DeleteFile(const QString &url)
Definition: remotefile.cpp:411
ResultItem::GetSubtitle
const QString & GetSubtitle() const
Definition: rssparse.h:136
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:55
ResultItem::GetDescription
const QString & GetDescription() const
Definition: rssparse.h:138
NetBase::DoDeleteVideo
void DoDeleteVideo(bool remove)
Definition: netbase.cpp:198
mythuihelper.h
ResultItem::GetPlayerArguments
const QStringList & GetPlayerArguments() const
Definition: rssparse.h:148
NetBase::LoadData
virtual void LoadData(void)=0
ResultItem::GetEpisode
const uint & GetEpisode() const
Definition: rssparse.h:157
NetBase::NetBase
NetBase(MythScreenStack *parent, const char *name=nullptr)
Definition: netbase.cpp:18
ResultItem::GetURL
const QString & GetURL() const
Definition: rssparse.h:139
MythConfirmationDialog
Dialog asking for user confirmation. Ok and optional Cancel button.
Definition: mythdialogbox.h:272
ResultItem
Definition: rssparse.h:109
MythUIProgressDialog::SetTotal
void SetTotal(uint total)
Definition: mythprogressdialog.cpp:193
mythcontext.h
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:104
NetBase::ShowWebVideo
void ShowWebVideo(void)
Definition: netbase.cpp:105
RemoteDownloadFile
QString RemoteDownloadFile(const QString &url, const QString &storageGroup, const QString &filename)
Definition: remoteutil.cpp:605
NetBase::m_progressDialog
MythUIProgressDialog * m_progressDialog
Definition: netbase.h:49
netbase.h
cleanThumbnailCacheDir
void cleanThumbnailCacheDir()
Definition: metadataimagedownload.cpp:496
kMSDontDisableDrawing
@ kMSDontDisableDrawing
avoid disabling UI drawing
Definition: mythsystem.h:37
MythMainWindow::AllowInput
void AllowInput(bool Allow)
Definition: mythmainwindow.cpp:1526
build_compdb.filename
filename
Definition: build_compdb.py:21
mythmainwindow.h
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:52
ResultItem::GetDownloadable
const bool & GetDownloadable() const
Definition: rssparse.h:154
ShowOkPopup
MythConfirmationDialog * ShowOkPopup(const QString &message, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
Definition: mythdialogbox.cpp:562
MythObservable::removeListener
void removeListener(QObject *listener)
Remove a listener to the observable.
Definition: mythobservable.cpp:55
videoutils.h
NetBase::DoDownloadAndPlay
void DoDownloadAndPlay(void)
Definition: netbase.cpp:283
ResultItem::GetMediaURL
const QString & GetMediaURL() const
Definition: rssparse.h:141
NetBase::RunCmdWithoutScreensaver
static void RunCmdWithoutScreensaver(const QString &cmd)
Definition: netbase.cpp:171
MythMainWindow::PauseIdleTimer
void PauseIdleTimer(bool Pause)
Pause the idle timeout timer.
Definition: mythmainwindow.cpp:2153
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:897