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
10 #include <libmythui/mythuihelper.h>
11 
12 // mythbrowser
13 #include "bookmarkeditor.h"
14 #include "mythbrowser.h"
15 #include "webpage.h"
16 
17 MythBrowser::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();
29  if (m_defaultFavIcon)
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);
71  m_defaultFavIcon->Assign(image);
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)
92  m_progressBar->SetTotal(100);
93 
94  if (m_exitButton)
95  {
96  m_exitButton->SetEnabled(false);
97  m_exitButton->SetEnabled(true);
99  }
100 
101  if (m_backButton)
102  {
103  m_backButton->SetEnabled(false);
105  }
106 
107  if (m_forwardButton)
108  {
109  m_forwardButton->SetEnabled(false);
111  }
112 
113  BuildFocusList();
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 {
129  if (m_currentBrowser >=0 && m_currentBrowser < m_browserList.size())
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 
150 void 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 
185  if (m_currentBrowser >= 0 && m_currentBrowser < m_browserList.size())
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 
199 void MythBrowser::switchTab(int newTab)
200 {
201  if (newTab == m_currentBrowser)
202  return;
203 
204  if (newTab < 0 || newTab >= m_browserList.size())
205  return;
206 
207  if (m_currentBrowser >= 0 && m_currentBrowser < m_browserList.size())
208  m_browserList[m_currentBrowser]->SetActive(false);
209 
210  BuildFocusList();
211 
212  m_browserList[newTab]->SetActive(true);
213 
214  m_currentBrowser = newTab;
215 
216  if (GetFocusWidget() != m_pageList)
218 }
219 
220 void 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 {
232  activeBrowser()->ZoomOut();
233 }
234 
236 {
237  activeBrowser()->ZoomIn();
238 }
239 
241 {
242  activeBrowser()->Back();
243 }
244 
246 {
247  activeBrowser()->Forward();
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 
273 void 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 
291 void MythBrowser::slotTitleChanged(const QString &title)
292 {
294  if (item)
295  item->SetText(title);
296 }
297 
298 void MythBrowser::slotStatusBarMessage(const QString &text)
299 {
300  if (m_statusText)
301  m_statusText->SetText(text);
302 }
303 
305 {
306  if (!item)
307  return;
308 
310  slotStatusBarMessage(item->GetText());
311 }
312 
314 {
316 }
317 
318 bool 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 
348  m_menuPopup->AddButton(tr("Enter URL"), &MythBrowser::slotEnterURL);
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)
361  m_menuPopup->AddButton(tr("Delete Tab"), &MythBrowser::slotDeleteTab);
362 
363  m_menuPopup->AddButton(tr("Add Bookmark"), &MythBrowser::slotAddBookmark);
364  }
365  else if (action == "INFO")
366  {
367  if (GetFocusWidget() == m_pageList)
369  else
371  }
372  else if (action == "ESCAPE")
373  {
375  }
376  else if (action == "PREVTAB")
377  {
378  int pos = m_pageList->GetCurrentPos();
379  if (pos > 0)
380  m_pageList->SetItemCurrent(--pos);
381  }
382  else if (action == "NEXTTAB")
383  {
384  int pos = m_pageList->GetCurrentPos();
385  if (pos < m_pageList->GetCount() - 1)
386  m_pageList->SetItemCurrent(++pos);
387  }
388  else if (action == "DELETE")
389  {
390  slotDeleteTab();
391  }
392  else
393  {
394  handled = false;
395  }
396  }
397 
398  if (!handled && MythScreenType::keyPressEvent(event))
399  handled = true;
400 
401  return handled;
402 }
403 
404 
MythUIButton::Clicked
void Clicked()
MythMainWindow::GetMainStack
MythScreenStack * GetMainStack()
Definition: mythmainwindow.cpp:317
MythBrowser::slotLoadProgress
void slotLoadProgress(int progress)
Definition: mythbrowser.cpp:285
MythDialogBox::SetReturnEvent
void SetReturnEvent(QObject *retobject, const QString &resultid)
Definition: mythdialogbox.cpp:303
MythUIWebBrowser::Forward
void Forward(void)
Got forward in page history.
Definition: mythuiwebbrowser.cpp:1251
MythUIButtonList::GetItemCurrent
MythUIButtonListItem * GetItemCurrent() const
Definition: mythuibuttonlist.cpp:1614
MythBrowser::m_browserList
QList< WebPage * > m_browserList
Definition: mythbrowser.h:73
MythBrowser::slotZoomOut
void slotZoomOut()
Definition: mythbrowser.cpp:230
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:384
MythBrowser::activeBrowser
MythUIWebBrowser * activeBrowser(void)
Definition: mythbrowser.cpp:127
MythBrowser::slotTitleChanged
void slotTitleChanged(const QString &title)
Definition: mythbrowser.cpp:291
progress
bool progress
Definition: mythcommflag.cpp:60
MythPainter::GetFormatImage
MythImage * GetFormatImage()
Returns a blank reference counted image in the format required for the Draw functions for this painte...
Definition: mythpainter.cpp:524
MythUIButtonList::itemSelected
void itemSelected(MythUIButtonListItem *item)
MythUIProgressBar::SetUsed
void SetUsed(int value)
Definition: mythuiprogressbar.cpp:72
MythBrowser::m_exitButton
MythUIButton * m_exitButton
Definition: mythbrowser.h:79
xbmcvfs.exists
bool exists(str path)
Definition: xbmcvfs.py:51
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:304
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:266
MythBrowser::slotBack
void slotBack()
Definition: mythbrowser.cpp:240
MythScreenType::GetFocusWidget
MythUIType * GetFocusWidget(void) const
Definition: mythscreentype.cpp:111
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:885
MythUIButtonListItem::SetText
void SetText(const QString &text, const QString &name="", const QString &state="")
Definition: mythuibuttonlist.cpp:3319
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:1044
bookmarkeditor.h
MythBrowser::WebPage
friend class WebPage
Definition: mythbrowser.h:92
MythUIProgressBar::SetTotal
void SetTotal(int value)
Definition: mythuiprogressbar.cpp:78
MythBrowser::slotOpenURL
void slotOpenURL(const QString &url)
Definition: mythbrowser.cpp:220
mythlogging.h
MythUIWebBrowser::ZoomOut
void ZoomOut(void)
Decrease the text size.
Definition: mythuiwebbrowser.cpp:1162
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:1111
MythBrowser::~MythBrowser
~MythBrowser() override
Definition: mythbrowser.cpp:24
MythUIButtonList::GetCurrentPos
int GetCurrentPos() const
Definition: mythuibuttonlist.h:240
MythScreenType::SetFocusWidget
bool SetFocusWidget(MythUIType *widget=nullptr)
Definition: mythscreentype.cpp:116
MythUIWebBrowser::ZoomIn
void ZoomIn(void)
Increase the text size.
Definition: mythuiwebbrowser.cpp:1154
MythDialogBox::AddButton
void AddButton(const QString &title)
Definition: mythdialogbox.h:197
MythDialogBox
Basic menu dialog, message and a list of options.
Definition: mythdialogbox.h:165
MythDialogBox::Create
bool Create(void) override
Definition: mythdialogbox.cpp:127
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:204
MythBrowser::slotLoadFinished
void slotLoadFinished(bool OK)
Definition: mythbrowser.cpp:273
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:1276
MythBrowser::MythBrowser
MythBrowser(MythScreenStack *parent, QStringList &urlList)
Definition: mythbrowser.cpp:17
MythUIType::SetEnabled
void SetEnabled(bool enable)
Definition: mythuitype.cpp:1128
MythBrowser::m_urlList
QStringList m_urlList
Definition: mythbrowser.h:70
MythScreenType::GetScreenStack
MythScreenStack * GetScreenStack() const
Definition: mythscreentype.cpp:215
MythBrowser::slotStatusBarMessage
void slotStatusBarMessage(const QString &text)
Definition: mythbrowser.cpp:298
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:3368
mythuihelper.h
MythBrowser::m_backButton
MythUIButton * m_backButton
Definition: mythbrowser.h:77
MythBrowser::slotForward
void slotForward()
Definition: mythbrowser.cpp:245
MythBrowser::m_statusText
MythUIText * m_statusText
Definition: mythbrowser.h:76
MythBrowser::Create
bool Create(void) override
Definition: mythbrowser.cpp:36
MythScreenType::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythscreentype.cpp:402
MythBrowser::slotZoomIn
void slotZoomIn()
Definition: mythbrowser.cpp:235
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:701
MythBrowser::slotAddBookmark
void slotAddBookmark(void)
Definition: mythbrowser.cpp:250
MythBrowser::slotDeleteTab
void slotDeleteTab(void)
Definition: mythbrowser.cpp:180
MythUIWebBrowser::Back
void Back(void)
Got backward in page history.
Definition: mythuiwebbrowser.cpp:1240
MythBrowser::slotEnterURL
void slotEnterURL(void) const
Definition: mythbrowser.cpp:134
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:1633
MythUIText::SetText
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:115
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:313
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:1581
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:322
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:313
MythBrowser::switchTab
void switchTab(int newTab)
Definition: mythbrowser.cpp:199
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:64
MythBrowser::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythbrowser.cpp:318
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:2154
mythbrowser.h