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