MythTV master
netbase.cpp
Go to the documentation of this file.
1#include <QDir>
2
16
17#include "netbase.h"
18
19NetBase::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{
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
47void 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
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 (auto i = thumbs.crbegin(); i != thumbs.crend(); ++i)
76 {
77 QString filename = QString("%1/%2").arg(cache, *i);
78 LOG(VB_GENERAL, LOG_DEBUG, QString("Deleting file %1").arg(filename));
79 QFileInfo fi(filename);
80 QDateTime lastmod = fi.lastModified();
81 if (lastmod.addDays(7) < MythDate::current())
82 QFile::remove(filename);
83 }
84}
85
87{
88 ResultItem *item = GetStreamItem();
89
90 if (!item)
91 return;
92
93 if (!item->GetDownloadable())
94 {
96 return;
97 }
98
99 auto seconds = std::chrono::seconds(item->GetTime().toInt());
100 GetMythMainWindow()->HandleMedia("Internal", item->GetMediaURL(),
101 item->GetDescription(), item->GetTitle(), item->GetSubtitle(),
102 QString(), item->GetSeason(), item->GetEpisode(), QString(),
103 duration_cast<std::chrono::minutes>(seconds), 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
172void NetBase::RunCmdWithoutScreensaver(const QString &cmd)
173{
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, &MythConfirmationDialog::haveResult,
194 }
195 else
196 {
197 delete confirmdialog;
198 }
199}
200
201void NetBase::DoDeleteVideo(bool remove)
202{
203 if (!remove)
204 return;
205
206 ResultItem *item = GetStreamItem();
207
208 if (!item)
209 return;
210
211 QString filename = GetDownloadFilename(item->GetTitle(),
212 item->GetMediaURL());
213
214 if (filename.startsWith("myth://"))
216 else
217 {
218 QFile file(filename);
219 file.remove();
220 }
221}
222
223void NetBase::customEvent(QEvent *event)
224{
225 if (event->type() == MythEvent::kMythEventMessage)
226 {
227 auto *me = dynamic_cast<MythEvent *>(event);
228 if (me == nullptr)
229 return;
230 QStringList tokens = me->Message().split(" ", Qt::SkipEmptyParts);
231
232 if (tokens.isEmpty())
233 return;
234
235 if (tokens[0] == "DOWNLOAD_FILE")
236 {
237 QStringList args = me->ExtraDataList();
238 if ((tokens.size() != 2) ||
239 (args[1] != m_downloadFile))
240 return;
241
242 if (tokens[1] == "UPDATE")
243 {
244 QString message = tr("Downloading Video...\n"
245 "(%1 of %2 MB)")
246 .arg(QString::number(args[2].toInt() / 1024.0 / 1024.0,
247 'f', 2),
248 QString::number(args[3].toInt() / 1024.0 / 1024.0,
249 'f', 2));
251 m_progressDialog->SetTotal(args[3].toInt());
252 m_progressDialog->SetProgress(args[2].toInt());
253 }
254 else if (tokens[1] == "FINISHED")
255 {
256 int fileSize = args[2].toInt();
257 int errorCode = args[4].toInt();
258
260 {
262 m_progressDialog = nullptr;
263 }
264
265 if ((m_downloadFile.startsWith("myth://")))
266 {
267 if ((errorCode == 0) &&
268 (fileSize > 0))
269 {
271 }
272 else
273 {
274 ShowOkPopup(tr("Error downloading video to backend."));
275 }
276 }
277 }
278 }
279 }
280}
281
283{
284 ResultItem *item = GetStreamItem();
285 if (!item)
286 return;
287
288 QString baseFilename = GetDownloadFilename(item->GetTitle(),
289 item->GetMediaURL());
290
291 QString finalFilename = StorageGroup::generate_file_url("Default",
293 baseFilename);
294
295 LOG(VB_GENERAL, LOG_INFO, QString("Downloading %1 to %2")
296 .arg(item->GetMediaURL(), finalFilename));
297
298 // Does the file already exist?
299 bool exists = RemoteFile::Exists(finalFilename);
300
301 if (exists)
302 {
303 DoPlayVideo(finalFilename);
304 return;
305 }
306 DownloadVideo(item->GetMediaURL(), baseFilename);
307}
308
309void NetBase::DoPlayVideo(const QString &filename)
310{
311 ResultItem *item = GetStreamItem();
312 if (!item)
313 return;
314
315 GetMythMainWindow()->HandleMedia("Internal", filename);
316}
317
319{
320 ResultItem *item = GetStreamItem();
321 if (!item)
322 return;
323
324 QString filename = GetDownloadFilename(item->GetTitle(),
325 item->GetMediaURL());
326
328}
Dialog asking for user confirmation.
QString GetSetting(const QString &key, const QString &defaultval="")
QString GetMasterHostName(void)
This class is used as a container for messages.
Definition: mythevent.h:17
const QString & Message() const
Definition: mythevent.h:65
static const Type kMythEventMessage
Definition: mythevent.h:79
static void DisableScreensaver()
void PauseIdleTimer(bool Pause)
Pause the idle timeout timer.
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)
static void RestoreScreensaver()
void AllowInput(bool Allow)
void addListener(QObject *listener)
Add a listener to the observable.
void removeListener(QObject *listener)
Remove a listener to the observable.
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Screen in which all other widgets are contained and rendered.
virtual void Close()
bool Create(void) override
void SetProgress(uint count)
void SetMessage(const QString &message)
GrabberScript::scriptList m_grabberList
Definition: netbase.h:53
void DownloadVideo(const QString &url, const QString &dest)
Definition: netbase.cpp:47
void DoPlayVideo()
Definition: netbase.cpp:318
NetBase(MythScreenStack *parent, const char *name=nullptr)
Definition: netbase.cpp:19
void ShowWebVideo(void)
Definition: netbase.cpp:106
static void CleanCacheDir()
Definition: netbase.cpp:68
MythUIProgressDialog * m_progressDialog
Definition: netbase.h:49
static void RunCmdWithoutScreensaver(const QString &cmd)
Definition: netbase.cpp:172
void DoDeleteVideo(bool remove)
Definition: netbase.cpp:201
void customEvent(QEvent *event) override
Definition: netbase.cpp:223
void SlotDeleteVideo(void)
Definition: netbase.cpp:183
MetadataImageDownload * m_imageDownload
Definition: netbase.h:50
void StreamWebVideo(void)
Definition: netbase.cpp:86
MythScreenStack * m_popupStack
Definition: netbase.h:48
~NetBase() override
Definition: netbase.cpp:27
virtual ResultItem * GetStreamItem()=0
void InitProgressDialog()
Definition: netbase.cpp:53
void Init() override
Used after calling Load() to assign data to widgets and other UI initilisation which is prohibited in...
Definition: netbase.cpp:42
QString m_downloadFile
Definition: netbase.h:52
virtual void LoadData(void)=0
void DoDownloadAndPlay(void)
Definition: netbase.cpp:282
static bool DeleteFile(const QString &url)
Definition: remotefile.cpp:418
static bool Exists(const QString &url, struct stat *fileinfo)
Definition: remotefile.cpp:461
const QString & GetPlayer() const
Definition: rssparse.h:147
const QString & GetURL() const
Definition: rssparse.h:139
const QString & GetDescription() const
Definition: rssparse.h:138
const QString & GetMediaURL() const
Definition: rssparse.h:141
const QDateTime & GetDate() const
Definition: rssparse.h:143
const QStringList & GetPlayerArguments() const
Definition: rssparse.h:148
const QString & GetTime() const
Definition: rssparse.h:144
const uint & GetEpisode() const
Definition: rssparse.h:157
const QString & GetTitle() const
Definition: rssparse.h:134
const bool & GetDownloadable() const
Definition: rssparse.h:154
const QString & GetSubtitle() const
Definition: rssparse.h:136
const uint & GetSeason() const
Definition: rssparse.h:156
static QString generate_file_url(const QString &storage_group, const QString &host, const QString &path)
void cleanThumbnailCacheDir()
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythConfirmationDialog * ShowOkPopup(const QString &message, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
QString GetConfDir(void)
Definition: mythdirs.cpp:263
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythMainWindow * GetMythMainWindow(void)
@ kMSDontDisableDrawing
avoid disabling UI drawing
Definition: mythsystem.h:37
uint myth_system(const QString &command, uint flags, std::chrono::seconds timeout)
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:15
bool exists(str path)
Definition: xbmcvfs.py:51
QString GetDownloadFilename(const QString &title, const QString &url)
Definition: netutils.cpp:854
QString RemoteDownloadFile(const QString &url, const QString &storageGroup, const QString &filename)
Definition: remoteutil.cpp:609