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  MythConfirmationDialog *confirmdialog =
188  new MythConfirmationDialog(m_popupStack, message);
189 
190  if (confirmdialog->Create())
191  {
192  m_popupStack->AddScreen(confirmdialog);
193  connect(confirmdialog, SIGNAL(haveResult(bool)),
194  SLOT(DoDeleteVideo(bool)));
195  }
196  else
197  delete confirmdialog;
198 }
199 
200 void NetBase::DoDeleteVideo(bool remove)
201 {
202  if (!remove)
203  return;
204 
205  ResultItem *item = GetStreamItem();
206 
207  if (!item)
208  return;
209 
210  QString filename = GetDownloadFilename(item->GetTitle(),
211  item->GetMediaURL());
212 
213  if (filename.startsWith("myth://"))
214  RemoteFile::DeleteFile(filename);
215  else
216  {
217  QFile file(filename);
218  file.remove();
219  }
220 }
221 
222 void NetBase::customEvent(QEvent *event)
223 {
224  if (event->type() == MythEvent::MythEventMessage)
225  {
226  MythEvent *me = static_cast<MythEvent *>(event);
227  QStringList tokens = me->Message().split(" ", QString::SkipEmptyParts);
228 
229  if (tokens.isEmpty())
230  return;
231 
232  if (tokens[0] == "DOWNLOAD_FILE")
233  {
234  QStringList args = me->ExtraDataList();
235  if ((tokens.size() != 2) ||
236  (args[1] != m_downloadFile))
237  return;
238 
239  if (tokens[1] == "UPDATE")
240  {
241  QString message = tr("Downloading Video...\n"
242  "(%1 of %2 MB)")
243  .arg(QString::number(args[2].toInt() / 1024.0 / 1024.0,
244  'f', 2))
245  .arg(QString::number(args[3].toInt() / 1024.0 / 1024.0,
246  'f', 2));
247  m_progressDialog->SetMessage(message);
248  m_progressDialog->SetTotal(args[3].toInt());
249  m_progressDialog->SetProgress(args[2].toInt());
250  }
251  else if (tokens[1] == "FINISHED")
252  {
253  int fileSize = args[2].toInt();
254  int errorCode = args[4].toInt();
255 
256  if (m_progressDialog)
257  {
259  m_progressDialog = nullptr;
260  }
261 
262  if ((m_downloadFile.startsWith("myth://")))
263  {
264  if ((errorCode == 0) &&
265  (fileSize > 0))
266  {
268  }
269  else
270  {
271  ShowOkPopup(tr("Error downloading video to backend."));
272  }
273  }
274  }
275  }
276  }
277 }
278 
280 {
281  ResultItem *item = GetStreamItem();
282  if (!item)
283  return;
284 
285  QString baseFilename = GetDownloadFilename(item->GetTitle(),
286  item->GetMediaURL());
287 
288  QString finalFilename = generate_file_url("Default",
290  baseFilename);
291 
292  LOG(VB_GENERAL, LOG_INFO, QString("Downloading %1 to %2")
293  .arg(item->GetMediaURL()).arg(finalFilename));
294 
295  // Does the file already exist?
296  bool exists = RemoteFile::Exists(finalFilename);
297 
298  if (exists)
299  {
300  DoPlayVideo(finalFilename);
301  return;
302  }
303  DownloadVideo(item->GetMediaURL(), baseFilename);
304 }
305 
306 void NetBase::DoPlayVideo(const QString &filename)
307 {
308  ResultItem *item = GetStreamItem();
309  if (!item)
310  return;
311 
312  GetMythMainWindow()->HandleMedia("Internal", filename);
313 }
QString m_downloadFile
Definition: netbase.h:51
const QString & GetSubtitle() const
Definition: rssparse.h:136
void DownloadVideo(const QString &url, const QString &dest)
Definition: netbase.cpp:47
static bool DeleteFile(const QString &url)
Definition: remotefile.cpp:418
avoid disabling UI drawing
Definition: mythsystem.h:35
void StreamWebVideo(void)
Definition: netbase.cpp:87
void RunCmdWithoutScreensaver(const QString &cmd)
Definition: netbase.cpp:172
const uint & GetSeason() const
Definition: rssparse.h:156
void DoDownloadAndPlay(void)
Definition: netbase.cpp:279
void SetMessage(const QString &message)
Dialog asking for user confirmation.
void SetProgress(uint count)
MythConfirmationDialog * ShowOkPopup(const QString &message, QObject *parent, const char *slot, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
bool Create(void) override
void DisableScreensaver(void)
static Type MythEventMessage
Definition: mythevent.h:66
const QString & GetURL() const
Definition: rssparse.h:139
void removeListener(QObject *listener)
Remove a listener to the observable.
const QString & GetDescription() const
Definition: rssparse.h:138
const QStringList & GetPlayerArguments() const
Definition: rssparse.h:148
void AllowInput(bool allow)
const uint & GetEpisode() const
Definition: rssparse.h:157
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
void cleanThumbnailCacheDir()
void addListener(QObject *listener)
Add a listener to the observable.
void ShowWebVideo(void)
Definition: netbase.cpp:106
void DoPlayVideo(const QString &filename)
Definition: netbase.cpp:306
MythUIProgressDialog * m_progressDialog
Definition: netbase.h:48
GrabberScript::scriptList m_grabberList
Definition: netbase.h:52
QString GetConfDir(void)
Definition: mythdirs.cpp:224
virtual void Close()
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
const QString & GetMediaURL() const
Definition: rssparse.h:141
This class is used as a container for messages.
Definition: mythevent.h:16
QString GetDownloadFilename(const QString &title, const QString &url)
Definition: netutils.cpp:851
void SlotDeleteVideo(void)
Definition: netbase.cpp:183
MythScreenStack * m_popupStack
Definition: netbase.h:47
const bool & GetDownloadable() const
Definition: rssparse.h:154
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
QString GetSetting(const QString &key, const QString &defaultval="")
void InitProgressDialog()
Definition: netbase.cpp:53
QString GetMasterHostName(void)
virtual void LoadData(void)=0
const QString & GetPlayer() const
Definition: rssparse.h:147
const char * name
Definition: ParseText.cpp:328
void Init() override
Used after calling Load() to assign data to widgets and other UI initilisation which is prohibited in...
Definition: netbase.cpp:42
uint myth_system(const QString &command, uint flags, uint timeout)
MythUIHelper * GetMythUI()
MythMainWindow * GetMythMainWindow(void)
QString RemoteDownloadFile(const QString &url, const QString &storageGroup, const QString &filename)
void PauseIdleTimer(bool pause)
void RestoreScreensaver(void)
static bool Exists(const QString &url, struct stat *fileinfo)
Definition: remotefile.cpp:461
virtual ResultItem * GetStreamItem()=0
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
void CleanCacheDir()
Definition: netbase.cpp:68
void customEvent(QEvent *event) override
Definition: netbase.cpp:222
bool Create(void) override
NetBase(MythScreenStack *parent, const char *name=nullptr)
Definition: netbase.cpp:19
const QString & GetTitle() const
Definition: rssparse.h:134
void DoDeleteVideo(bool remove)
Definition: netbase.cpp:200
MetadataImageDownload * m_imageDownload
Definition: netbase.h:49
QString generate_file_url(const QString &storage_group, const QString &host, const QString &path)
Definition: videoutils.h:65
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)
Screen in which all other widgets are contained and rendered.
const QString & Message() const
Definition: mythevent.h:58
virtual ~NetBase()
Definition: netbase.cpp:27
const QStringList & ExtraDataList() const
Definition: mythevent.h:60
const QString & GetTime() const
Definition: rssparse.h:144
const QDateTime & GetDate() const
Definition: rssparse.h:143