MythTV master
netbase.cpp
Go to the documentation of this file.
1// C++
2#include <ranges>
3
4// Qt
5#include <QDir>
6
7// MythTV
20
21#include "netbase.h"
22
23NetBase::NetBase(MythScreenStack *parent, const char *name)
24 : MythScreenType(parent, name),
25 m_popupStack(GetMythMainWindow()->GetStack("popup stack")),
26 m_imageDownload(new MetadataImageDownload(this))
27{
29}
30
32{
34
35 qDeleteAll(m_grabberList);
36 m_grabberList.clear();
37
39
40 delete m_imageDownload;
41 m_imageDownload = nullptr;
42
44}
45
47{
48 LoadData();
49}
50
51void NetBase::DownloadVideo(const QString &url, const QString &dest)
52{
54 m_downloadFile = RemoteDownloadFile(url, "Default", dest);
55}
56
58{
59 QString message = tr("Downloading Video...");
61 m_popupStack, "videodownloadprogressdialog");
62
64 {
66 }
67 else
68 {
69 delete m_progressDialog;
70 m_progressDialog = nullptr;
71 }
72}
73
75{
76 QString cache = QString("%1/cache/netvision-thumbcache")
77 .arg(GetConfDir());
78 QDir cacheDir(cache);
79 QStringList thumbs = cacheDir.entryList(QDir::Files);
80
81 for (const auto & thumb : std::ranges::reverse_view(thumbs))
82 {
83 QString filename = QString("%1/%2").arg(cache, thumb);
84 LOG(VB_GENERAL, LOG_DEBUG, QString("Deleting file %1").arg(filename));
85 QFileInfo fi(filename);
86 QDateTime lastmod = fi.lastModified();
87 if (lastmod.addDays(7) < MythDate::current())
88 QFile::remove(filename);
89 }
90}
91
93{
94 ResultItem *item = GetStreamItem();
95
96 if (!item)
97 return;
98
99 if (!item->GetDownloadable())
100 {
101 ShowWebVideo();
102 return;
103 }
104
105 auto seconds = std::chrono::seconds(item->GetTime().toInt());
106 GetMythMainWindow()->HandleMedia("Internal", item->GetMediaURL(),
107 item->GetDescription(), item->GetTitle(), item->GetSubtitle(),
108 QString(), item->GetSeason(), item->GetEpisode(), QString(),
109 duration_cast<std::chrono::minutes>(seconds), item->GetDate().toString("yyyy"));
110}
111
113{
114 ResultItem *item = GetStreamItem();
115
116 if (!item)
117 return;
118
119 if (!item->GetPlayer().isEmpty())
120 {
121 const QString& cmd = item->GetPlayer();
122 QStringList args = item->GetPlayerArguments();
123 if (args.empty())
124 {
125 args += item->GetMediaURL();
126 if (args.empty())
127 args += item->GetURL();
128 }
129 else
130 {
131 args.replaceInStrings("%DIR%", GetConfDir() + "/MythNetvision");
132 args.replaceInStrings("%MEDIAURL%", item->GetMediaURL());
133 args.replaceInStrings("%URL%", item->GetURL());
134 args.replaceInStrings("%TITLE%", item->GetTitle());
135 }
136
137 QString playerCommand = cmd + " " + args.join(" ");
138 RunCmdWithoutScreensaver(playerCommand);
139 }
140 else
141 {
142 QString url = item->GetURL();
143
144 LOG(VB_GENERAL, LOG_DEBUG, QString("Web URL = %1").arg(url));
145
146 if (url.isEmpty())
147 return;
148
149 QString browser = gCoreContext->GetSetting("WebBrowserCommand", "");
150 QString zoom = gCoreContext->GetSetting("WebBrowserZoomLevel", "1.0");
151
152 if (browser.isEmpty())
153 {
154 ShowOkPopup(tr("No browser command set! MythNetTree needs "
155 "MythBrowser installed to display the video."));
156 return;
157 }
158
159 if (browser.toLower() == "internal")
160 {
161 GetMythMainWindow()->HandleMedia("WebBrowser", url);
162 }
163 else
164 {
165 url.replace("mythflash://", "http://");
166 QString cmd = browser;
167 cmd.replace("%ZOOM%", zoom);
168 cmd.replace("%URL%", url);
169 cmd.replace('\'', "%27");
170 cmd.replace("&","\\&");
171 cmd.replace(";","\\;");
172
174 }
175 }
176}
177
178void NetBase::RunCmdWithoutScreensaver(const QString &cmd)
179{
187}
188
190{
191 QString message = tr("Are you sure you want to delete this file?");
192
193 auto *confirmdialog = new MythConfirmationDialog(m_popupStack, message);
194
195 if (confirmdialog->Create())
196 {
197 m_popupStack->AddScreen(confirmdialog);
198 connect(confirmdialog, &MythConfirmationDialog::haveResult,
200 }
201 else
202 {
203 delete confirmdialog;
204 }
205}
206
207void NetBase::DoDeleteVideo(bool remove)
208{
209 if (!remove)
210 return;
211
212 ResultItem *item = GetStreamItem();
213
214 if (!item)
215 return;
216
217 QString filename = GetDownloadFilename(item->GetTitle(),
218 item->GetMediaURL());
219
220 if (filename.startsWith("myth://"))
221 {
223 }
224 else
225 {
226 QFile file(filename);
227 file.remove();
228 }
229}
230
231void NetBase::customEvent(QEvent *event)
232{
233 if (event->type() == MythEvent::kMythEventMessage)
234 {
235 auto *me = dynamic_cast<MythEvent *>(event);
236 if (me == nullptr)
237 return;
238 QStringList tokens = me->Message().split(" ", Qt::SkipEmptyParts);
239
240 if (tokens.isEmpty())
241 return;
242
243 if (tokens[0] == "DOWNLOAD_FILE")
244 {
245 QStringList args = me->ExtraDataList();
246 if ((tokens.size() != 2) ||
247 (args[1] != m_downloadFile))
248 return;
249
250 if (tokens[1] == "UPDATE")
251 {
252 QString message = tr("Downloading Video...\n"
253 "(%1 of %2 MB)")
254 .arg(QString::number(args[2].toInt() / 1024.0 / 1024.0,
255 'f', 2),
256 QString::number(args[3].toInt() / 1024.0 / 1024.0,
257 'f', 2));
259 m_progressDialog->SetTotal(args[3].toInt());
260 m_progressDialog->SetProgress(args[2].toInt());
261 }
262 else if (tokens[1] == "FINISHED")
263 {
264 int fileSize = args[2].toInt();
265 int errorCode = args[4].toInt();
266
268 {
270 m_progressDialog = nullptr;
271 }
272
273 if ((m_downloadFile.startsWith("myth://")))
274 {
275 if ((errorCode == 0) &&
276 (fileSize > 0))
277 {
279 }
280 else
281 {
282 ShowOkPopup(tr("Error downloading video to backend."));
283 }
284 }
285 }
286 }
287 }
288}
289
291{
292 ResultItem *item = GetStreamItem();
293 if (!item)
294 return;
295
296 QString baseFilename = GetDownloadFilename(item->GetTitle(),
297 item->GetMediaURL());
298
299 QString finalFilename = StorageGroup::generate_file_url("Default",
301 baseFilename);
302
303 LOG(VB_GENERAL, LOG_INFO, QString("Downloading %1 to %2")
304 .arg(item->GetMediaURL(), finalFilename));
305
306 // Does the file already exist?
307 bool exists = RemoteFile::Exists(finalFilename);
308
309 if (exists)
310 {
311 DoPlayVideo(finalFilename);
312 return;
313 }
314 DownloadVideo(item->GetMediaURL(), baseFilename);
315}
316
317void NetBase::DoPlayVideo(const QString &filename)
318{
319 ResultItem *item = GetStreamItem();
320 if (!item)
321 return;
322
323 GetMythMainWindow()->HandleMedia("Internal", filename);
324}
325
327{
328 ResultItem *item = GetStreamItem();
329 if (!item)
330 return;
331
332 QString filename = GetDownloadFilename(item->GetTitle(),
333 item->GetMediaURL());
334
336}
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:51
void DoPlayVideo()
Definition: netbase.cpp:326
NetBase(MythScreenStack *parent, const char *name=nullptr)
Definition: netbase.cpp:23
void ShowWebVideo(void)
Definition: netbase.cpp:112
static void CleanCacheDir()
Definition: netbase.cpp:74
MythUIProgressDialog * m_progressDialog
Definition: netbase.h:49
static void RunCmdWithoutScreensaver(const QString &cmd)
Definition: netbase.cpp:178
void DoDeleteVideo(bool remove)
Definition: netbase.cpp:207
void customEvent(QEvent *event) override
Definition: netbase.cpp:231
void SlotDeleteVideo(void)
Definition: netbase.cpp:189
MetadataImageDownload * m_imageDownload
Definition: netbase.h:50
void StreamWebVideo(void)
Definition: netbase.cpp:92
MythScreenStack * m_popupStack
Definition: netbase.h:48
~NetBase() override
Definition: netbase.cpp:31
virtual ResultItem * GetStreamItem()=0
void InitProgressDialog()
Definition: netbase.cpp:57
void Init() override
Used after calling Load() to assign data to widgets and other UI initilisation which is prohibited in...
Definition: netbase.cpp:46
QString m_downloadFile
Definition: netbase.h:52
virtual void LoadData(void)=0
void DoDownloadAndPlay(void)
Definition: netbase.cpp:290
static bool DeleteFile(const QString &url)
Definition: remotefile.cpp:420
static bool Exists(const QString &url, struct stat *fileinfo)
Definition: remotefile.cpp:463
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:285
#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)