MythTV master
mythbrowser.cpp
Go to the documentation of this file.
1#include <cstdlib>
2#include <iostream>
3
4// qt
5#include <QEvent>
6
7// MythTV
11
12// mythbrowser
13#include "bookmarkeditor.h"
14#include "mythbrowser.h"
15#include "webpage.h"
16
17MythBrowser::MythBrowser(MythScreenStack *parent, QStringList &urlList)
18 : MythScreenType (parent, "mythbrowser"),
19 m_urlList(urlList)
20{
22}
23
25{
26 while (!m_browserList.isEmpty())
27 delete m_browserList.takeFirst();
30 {
32 m_defaultFavIcon = nullptr;
33 }
34}
35
37{
38 // Load the theme for this screen
39 if (!LoadWindowFromXML("browser-ui.xml", "browser", this))
40 return false;
41
42 bool err = false;
43 MythUIWebBrowser *browser = nullptr;
44
45 UIUtilE::Assign(this, browser, "webbrowser", &err);
46 UIUtilE::Assign(this, m_pageList, "pagelist", &err);
47 UIUtilW::Assign(this, m_progressBar, "progressbar");
48 UIUtilW::Assign(this, m_statusText, "status");
49 UIUtilW::Assign(this, m_titleText, "title");
50 UIUtilW::Assign(this, m_backButton, "back");
51 UIUtilW::Assign(this, m_forwardButton, "forward");
52 UIUtilW::Assign(this, m_exitButton, "exit");
53
54 if (err)
55 {
56 LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'browser'");
57 return false;
58 }
59
62
63 // create the default favicon
64 QString favIcon = "mb_default_favicon.png";
65 if (GetMythUI()->FindThemeFile(favIcon))
66 {
67 if (QFile::exists(favIcon))
68 {
69 QImage image(favIcon);
72 }
73 }
74
75 // this is the template for all other browser tabs
76 auto *page = new WebPage(this, browser);
77
78 m_browserList.append(page);
79 page->getBrowser()->SetDefaultSaveDirectory(m_defaultSaveDir);
80 page->getBrowser()->SetDefaultSaveFilename(m_defaultSaveFilename);
81
82 page->SetActive(true);
83
84 connect(page, &WebPage::loadProgress,
86 connect(page, &WebPage::statusBarMessage,
88 connect(page, &WebPage::loadFinished,
90
91 if (m_progressBar)
93
94 if (m_exitButton)
95 {
99 }
100
101 if (m_backButton)
102 {
103 m_backButton->SetEnabled(false);
105 }
106
107 if (m_forwardButton)
108 {
111 }
112
114
115 SetFocusWidget(browser);
116
118
119 for (int x = 1; x < m_urlList.size(); x++)
120 slotAddTab(m_urlList[x], false);
121
122 switchTab(0);
123
124 return true;
125}
126
128{
130 return m_browserList[m_currentBrowser]->getBrowser();
131 return m_browserList[0]->getBrowser();
132}
133
135{
136 MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
137
138 QString message = tr("Enter URL");
139
140
141 auto *dialog = new MythTextInputDialog(popupStack, message);
142
143 if (dialog->Create())
144 popupStack->AddScreen(dialog);
145
146 connect(dialog, &MythTextInputDialog::haveResult,
147 this, &MythBrowser::slotOpenURL, Qt::QueuedConnection);
148}
149
150void MythBrowser::slotAddTab(const QString &url, bool doSwitch)
151{
152 QString name = QString("browser%1").arg(m_browserList.size() + 1);
153 auto *page = new WebPage(this, m_browserList[0]->getBrowser()->GetArea(),
154 name.toLatin1().constData());
155 m_browserList.append(page);
156
157 QString newUrl = url;
158
159 if (newUrl.isEmpty())
160 newUrl = "http://www.google.com"; // TODO: add a user definable home page
161
162 if (!newUrl.startsWith("http://") && !newUrl.startsWith("https://") &&
163 !newUrl.startsWith("file:/") )
164 newUrl.prepend("http://");
165 page->getBrowser()->LoadPage(QUrl::fromEncoded(newUrl.toLocal8Bit()));
166
167 page->SetActive(false);
168
169 connect(page, &WebPage::loadProgress,
171 connect(page, &WebPage::statusBarMessage,
173 connect(page, &WebPage::loadFinished,
175
176 if (doSwitch)
178}
179
181{
182 if (m_browserList.size() < 2)
183 return;
184
186 {
187 int tab = m_currentBrowser;
188 m_currentBrowser = -1;
189 WebPage *page = m_browserList.takeAt(tab);
190 delete page;
191
192 if (tab >= m_browserList.size())
193 tab = m_browserList.size() - 1;
194
195 switchTab(tab);
196 }
197}
198
200{
201 if (newTab == m_currentBrowser)
202 return;
203
204 if (newTab < 0 || newTab >= m_browserList.size())
205 return;
206
208 m_browserList[m_currentBrowser]->SetActive(false);
209
211
212 m_browserList[newTab]->SetActive(true);
213
214 m_currentBrowser = newTab;
215
216 if (GetFocusWidget() != m_pageList)
218}
219
220void MythBrowser::slotOpenURL(const QString &url)
221{
222 QString sUrl = url;
223 if (!sUrl.startsWith("http://") && !sUrl.startsWith("https://") &&
224 !sUrl.startsWith("file:/") )
225 sUrl.prepend("http://");
226
227 activeBrowser()->LoadPage(QUrl::fromEncoded(sUrl.toLocal8Bit()));
228}
229
231{
233}
234
236{
238}
239
241{
242 activeBrowser()->Back();
243}
244
246{
248}
249
251{
254 m_editBookmark.m_url = activeBrowser()->GetUrl().toString();
255
257
258 auto *editor = new BookmarkEditor(&m_editBookmark,
259 true, mainStack, "bookmarkeditor");
260
261
262 if (editor->Create())
263 mainStack->AddScreen(editor);
264}
265
267{
269 if (item)
270 item->SetText(tr("Loading..."));
271}
272
273void MythBrowser::slotLoadFinished([[maybe_unused]] bool OK)
274{
275 if (m_progressBar)
277
278 if (m_backButton)
279 m_backButton->SetEnabled(activeBrowser()->CanGoBack());
280
281 if (m_forwardButton)
282 m_forwardButton->SetEnabled(activeBrowser()->CanGoForward());
283}
284
286{
287 if (m_progressBar)
289}
290
291void MythBrowser::slotTitleChanged(const QString &title)
292{
294 if (item)
295 item->SetText(title);
296}
297
298void MythBrowser::slotStatusBarMessage(const QString &text)
299{
300 if (m_statusText)
301 m_statusText->SetText(text);
302}
303
305{
306 if (!item)
307 return;
308
311}
312
314{
316}
317
318bool MythBrowser::keyPressEvent(QKeyEvent *event)
319{
320 // Always send keypress events to the currently focused widget first
321 if (GetFocusWidget()->keyPressEvent(event))
322 return true;
323
324 QStringList actions;
325 bool handled = GetMythMainWindow()->TranslateKeyPress("Browser", event, actions);
326
327 for (int i = 0; i < actions.size() && !handled; i++)
328 {
329
330 const QString& action = actions[i];
331 handled = true;
332
333 if (action == "MENU")
334 {
336
337 QString label = tr("Actions");
338
339 MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
340
341 m_menuPopup = new MythDialogBox(label, popupStack, "actionmenu");
342
343 if (m_menuPopup->Create())
344 popupStack->AddScreen(m_menuPopup);
345
346 m_menuPopup->SetReturnEvent(this, "action");
347
349
350 if (activeBrowser()->CanGoBack())
352
353 if (activeBrowser()->CanGoForward())
355
358 m_menuPopup->AddButton(tr("New Tab"), qOverload<>(&MythBrowser::slotAddTab));
359
360 if (m_browserList.size() > 1)
362
364 }
365 else if (action == "INFO")
366 {
367 if (GetFocusWidget() == m_pageList)
369 else
371 }
372 else if (action == "ESCAPE")
373 {
374 if (activeBrowser()->CanGoBack())
375 activeBrowser()->Back();
376 else
378 }
379 else if (action == "PREVTAB")
380 {
381 int pos = m_pageList->GetCurrentPos();
382 if (pos > 0)
384 }
385 else if (action == "NEXTTAB")
386 {
387 int pos = m_pageList->GetCurrentPos();
388 if (pos < m_pageList->GetCount() - 1)
390 }
391 else if (action == "DELETE")
392 {
394 }
395 else
396 {
397 handled = false;
398 }
399 }
400
401 if (!handled && MythScreenType::keyPressEvent(event))
402 handled = true;
403
404 return handled;
405}
406
407
Site category, name and URL edit screen.
QString m_category
QString m_name
QString m_url
void slotLoadFinished(bool OK)
MythUIText * m_statusText
Definition: mythbrowser.h:76
void slotForward()
void slotLoadProgress(int progress)
~MythBrowser() override
Definition: mythbrowser.cpp:24
friend class WebPage
Definition: mythbrowser.h:92
void slotZoomIn()
QString m_defaultSaveDir
Definition: mythbrowser.h:83
MythBrowser(MythScreenStack *parent, QStringList &urlList)
Definition: mythbrowser.cpp:17
QString m_defaultSaveFilename
Definition: mythbrowser.h:84
MythImage * m_defaultFavIcon
Definition: mythbrowser.h:90
void slotStatusBarMessage(const QString &text)
Bookmark m_editBookmark
Definition: mythbrowser.h:86
MythUIButton * m_exitButton
Definition: mythbrowser.h:79
MythUIButton * m_forwardButton
Definition: mythbrowser.h:78
MythUIButton * m_backButton
Definition: mythbrowser.h:77
void slotTabLosingFocus(void)
int m_currentBrowser
Definition: mythbrowser.h:81
MythUIWebBrowser * activeBrowser(void)
MythUIButtonList * m_pageList
Definition: mythbrowser.h:72
void slotLoadStarted(void)
void slotTabSelected(MythUIButtonListItem *item)
QStringList m_urlList
Definition: mythbrowser.h:70
void slotZoomOut()
void slotOpenURL(const QString &url)
MythUIText * m_titleText
Definition: mythbrowser.h:75
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
void slotDeleteTab(void)
void slotAddBookmark(void)
MythUIProgressBar * m_progressBar
Definition: mythbrowser.h:74
void slotTitleChanged(const QString &title)
void switchTab(int newTab)
void slotAddTab()
Definition: mythbrowser.h:52
QList< WebPage * > m_browserList
Definition: mythbrowser.h:73
MythDialogBox * m_menuPopup
Definition: mythbrowser.h:88
bool Create(void) override
Definition: mythbrowser.cpp:36
void slotEnterURL(void) const
void slotBack()
Basic menu dialog, message and a list of options.
void AddButton(const QString &title)
void SetReturnEvent(QObject *retobject, const QString &resultid)
bool Create(void) override
void Assign(const QImage &img)
Definition: mythimage.cpp:77
int DecrRef(void) override
Decrements reference count and deletes on 0.
Definition: mythimage.cpp:52
void PauseIdleTimer(bool Pause)
Pause the idle timeout timer.
MythScreenStack * GetMainStack()
bool TranslateKeyPress(const QString &Context, QKeyEvent *Event, QStringList &Actions, bool AllowJumps=true)
Get a list of actions for a keypress in the given context.
MythScreenStack * GetStack(const QString &Stackname)
MythImage * GetFormatImage()
Returns a blank reference counted image in the format required for the Draw functions for this painte...
virtual void PopScreen(MythScreenType *screen=nullptr, bool allowFade=true, bool deleteScreen=true)
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Screen in which all other widgets are contained and rendered.
void BuildFocusList(void)
MythUIType * GetFocusWidget(void) const
MythScreenStack * GetScreenStack() const
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
bool SetFocusWidget(MythUIType *widget=nullptr)
virtual void Close()
Dialog prompting the user to enter a text string.
void haveResult(QString)
QString GetText(const QString &name="") const
void SetText(const QString &text, const QString &name="", const QString &state="")
virtual QString GetValue() const
MythUIButtonListItem * GetItemCurrent() const
void SetItemCurrent(MythUIButtonListItem *item)
int GetCurrentPos() const
void itemSelected(MythUIButtonListItem *item)
void Clicked()
void SetUsed(int value)
void SetTotal(int value)
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:115
void SetEnabled(bool enable)
virtual MythRect GetArea(void) const
If the object has a minimum area defined, return it, other wise return the default area.
Definition: mythuitype.cpp:885
Web browsing widget.
void LoadPage(const QUrl &url)
Loads the specified url and displays it.
void ZoomIn(void)
Increase the text size.
void Back(void)
Got backward in page history.
void ZoomOut(void)
Decrease the text size.
QUrl GetUrl(void)
Gets the current page's url.
void Forward(void)
Got forward in page history.
void loadProgress(int progress)
void loadFinished(bool OK)
void statusBarMessage(const QString &text)
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
static constexpr int OK
Definition: dvbci.cpp:69
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythPainter * GetMythPainter(void)
MythMainWindow * GetMythMainWindow(void)
MythUIHelper * GetMythUI()
bool exists(str path)
Definition: xbmcvfs.py:51
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27