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