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 // myth
8 #include "mythlogging.h"
9 #include "mythcontext.h"
10 #include "mythmainwindow.h"
11 #include "mythuihelper.h"
12 
13 // mythbrowser
14 #include "webpage.h"
15 #include "bookmarkeditor.h"
16 #include "mythbrowser.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 
275 {
276  (void) OK;
277 
278  if (m_progressBar)
280 
281  if (m_backButton)
283 
284  if (m_forwardButton)
286 }
287 
289 {
290  if (m_progressBar)
292 }
293 
295 {
297  if (item)
298  item->SetText(title);
299 }
300 
301 void MythBrowser::slotStatusBarMessage(const QString &text)
302 {
303  if (m_statusText)
304  m_statusText->SetText(text);
305 }
306 
308 {
309  if (!item)
310  return;
311 
313  slotStatusBarMessage(item->GetText());
314 }
315 
317 {
319 }
320 
321 bool MythBrowser::keyPressEvent(QKeyEvent *event)
322 {
323  // Always send keypress events to the currently focused widget first
324  if (GetFocusWidget()->keyPressEvent(event))
325  return true;
326 
327  QStringList actions;
328  bool handled = GetMythMainWindow()->TranslateKeyPress("Browser", event, actions);
329 
330  for (int i = 0; i < actions.size() && !handled; i++)
331  {
332 
333  QString action = actions[i];
334  handled = true;
335 
336  if (action == "MENU")
337  {
339 
340  QString label = tr("Actions");
341 
342  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
343 
344  m_menuPopup = new MythDialogBox(label, popupStack, "actionmenu");
345 
346  if (m_menuPopup->Create())
347  popupStack->AddScreen(m_menuPopup);
348 
349  m_menuPopup->SetReturnEvent(this, "action");
350 
351  m_menuPopup->AddButton(tr("Enter URL"), &MythBrowser::slotEnterURL);
352 
353  if (activeBrowser()->CanGoBack())
355 
356  if (activeBrowser()->CanGoForward())
358 
361  m_menuPopup->AddButton(tr("New Tab"), qOverload<>(&MythBrowser::slotAddTab));
362 
363  if (m_browserList.size() > 1)
364  m_menuPopup->AddButton(tr("Delete Tab"), &MythBrowser::slotDeleteTab);
365 
366  m_menuPopup->AddButton(tr("Add Bookmark"), &MythBrowser::slotAddBookmark);
367  }
368  else if (action == "INFO")
369  {
370  if (GetFocusWidget() == m_pageList)
372  else
374  }
375  else if (action == "ESCAPE")
376  {
378  }
379  else if (action == "PREVTAB")
380  {
381  int pos = m_pageList->GetCurrentPos();
382  if (pos > 0)
383  m_pageList->SetItemCurrent(--pos);
384  }
385  else if (action == "NEXTTAB")
386  {
387  int pos = m_pageList->GetCurrentPos();
388  if (pos < m_pageList->GetCount() - 1)
389  m_pageList->SetItemCurrent(++pos);
390  }
391  else if (action == "DELETE")
392  {
393  slotDeleteTab();
394  }
395  else
396  handled = false;
397  }
398 
399  if (!handled && MythScreenType::keyPressEvent(event))
400  handled = true;
401 
402  return handled;
403 }
404 
405 
MythUIButton::Clicked
void Clicked()
MythMainWindow::GetMainStack
MythScreenStack * GetMainStack()
Definition: mythmainwindow.cpp:324
MythUIWebBrowser::CanGoForward
bool CanGoForward(void)
Can go forward in page history.
Definition: mythuiwebbrowser.cpp:1229
MythBrowser::slotLoadProgress
void slotLoadProgress(int progress)
Definition: mythbrowser.cpp:288
MythDialogBox::SetReturnEvent
void SetReturnEvent(QObject *retobject, const QString &resultid)
Definition: mythdialogbox.cpp:300
MythUIWebBrowser::Forward
void Forward(void)
Got forward in page history.
Definition: mythuiwebbrowser.cpp:1264
MythUIButtonList::GetItemCurrent
MythUIButtonListItem * GetItemCurrent() const
Definition: mythuibuttonlist.cpp:1590
MythBrowser::m_browserList
QList< WebPage * > m_browserList
Definition: mythbrowser.h:72
MythBrowser::slotZoomOut
void slotZoomOut()
Definition: mythbrowser.cpp:231
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:402
MythBrowser::activeBrowser
MythUIWebBrowser * activeBrowser(void)
Definition: mythbrowser.cpp:128
MythBrowser::slotTitleChanged
void slotTitleChanged(const QString &title)
Definition: mythbrowser.cpp:294
title
QString title
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:636
MythMainWindow::TranslateKeyPress
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
Definition: mythmainwindow.cpp:1142
MythUIWebBrowser::CanGoBack
bool CanGoBack(void)
Can we go backward in page history.
Definition: mythuiwebbrowser.cpp:1242
MythPainter::GetFormatImage
MythImage * GetFormatImage()
Returns a blank reference counted image in the format required for the Draw functions for this painte...
Definition: mythpainter.cpp:540
progress
bool progress
Definition: mythtv/programs/mythcommflag/main.cpp:73
MythUIButtonList::itemSelected
void itemSelected(MythUIButtonListItem *item)
MythUIProgressBar::SetUsed
void SetUsed(int value)
Definition: mythuiprogressbar.cpp:59
MythBrowser::m_exitButton
MythUIButton * m_exitButton
Definition: mythbrowser.h:78
MythScreenStack
Definition: mythscreenstack.h:16
Bookmark::m_url
QString m_url
Definition: bookmarkmanager.h:21
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:45
MythBrowser::slotTabSelected
void slotTabSelected(MythUIButtonListItem *item)
Definition: mythbrowser.cpp:307
MythBrowser::m_progressBar
MythUIProgressBar * m_progressBar
Definition: mythbrowser.h:73
MythTextInputDialog::haveResult
void haveResult(QString)
MythMainWindow::GetStack
MythScreenStack * GetStack(const QString &stackname)
Definition: mythmainwindow.cpp:329
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:112
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:870
MythUIButtonListItem::SetText
void SetText(const QString &text, const QString &name="", const QString &state="")
Definition: mythuibuttonlist.cpp:3264
MythBrowser::m_menuPopup
MythDialogBox * m_menuPopup
Definition: mythbrowser.h:87
MythUIButtonListItem
Definition: mythuibuttonlist.h:28
MythUIWebBrowser::LoadPage
void LoadPage(const QUrl &url)
Loads the specified url and displays it.
Definition: mythuiwebbrowser.cpp:1051
bookmarkeditor.h
MythBrowser::WebPage
friend class WebPage
Definition: mythbrowser.h:91
MythUIProgressBar::SetTotal
void SetTotal(int value)
Definition: mythuiprogressbar.cpp:71
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:1169
BookmarkEditor
Site category, name and URL edit screen.
Definition: bookmarkeditor.h:19
MythBrowser::~MythBrowser
~MythBrowser() override
Definition: mythbrowser.cpp:25
MythUIButtonList::GetCurrentPos
int GetCurrentPos() const
Definition: mythuibuttonlist.h:198
MythScreenType::SetFocusWidget
bool SetFocusWidget(MythUIType *widget=nullptr)
Definition: mythscreentype.cpp:117
MythUIWebBrowser::ZoomIn
void ZoomIn(void)
Increase the text size.
Definition: mythuiwebbrowser.cpp:1161
MythDialogBox::AddButton
void AddButton(const QString &title)
Definition: mythdialogbox.h:197
MythDialogBox
Basic menu dialog, message and a list of options.
Definition: mythdialogbox.h:167
MythDialogBox::Create
bool Create(void) override
Definition: mythdialogbox.cpp:127
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:222
MythBrowser::slotLoadFinished
void slotLoadFinished(bool OK)
Definition: mythbrowser.cpp:274
Bookmark::m_category
QString m_category
Definition: bookmarkmanager.h:18
MythBrowser::m_editBookmark
Bookmark m_editBookmark
Definition: mythbrowser.h:85
MythBrowser::m_forwardButton
MythUIButton * m_forwardButton
Definition: mythbrowser.h:77
MythImage::DecrRef
int DecrRef(void) override
Decrements reference count and deletes on 0.
Definition: mythimage.cpp:54
MythUIWebBrowser::GetUrl
QUrl GetUrl(void)
Gets the current page's url.
Definition: mythuiwebbrowser.cpp:1289
MythBrowser::MythBrowser
MythBrowser(MythScreenStack *parent, QStringList &urlList)
Definition: mythbrowser.cpp:18
MythUIType::SetEnabled
void SetEnabled(bool enable)
Definition: mythuitype.cpp:1109
MythBrowser::m_urlList
QStringList m_urlList
Definition: mythbrowser.h:69
MythScreenType::GetScreenStack
MythScreenStack * GetScreenStack() const
Definition: mythscreentype.cpp:233
MythBrowser::slotStatusBarMessage
void slotStatusBarMessage(const QString &text)
Definition: mythbrowser.cpp:301
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:3305
mythuihelper.h
MythBrowser::m_backButton
MythUIButton * m_backButton
Definition: mythbrowser.h:76
MythBrowser::slotForward
void slotForward()
Definition: mythbrowser.cpp:246
MythBrowser::m_statusText
MythUIText * m_statusText
Definition: mythbrowser.h:75
MythBrowser::Create
bool Create(void) override
Definition: mythbrowser.cpp:37
MythScreenType::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythscreentype.cpp:414
MythBrowser::slotZoomIn
void slotZoomIn()
Definition: mythbrowser.cpp:236
MythBrowser::m_titleText
MythUIText * m_titleText
Definition: mythbrowser.h:74
XMLParseBase::LoadWindowFromXML
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
Definition: xmlparsebase.cpp:692
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:1253
MythBrowser::slotEnterURL
void slotEnterURL(void) const
Definition: mythbrowser.cpp:135
MythBrowser::m_currentBrowser
int m_currentBrowser
Definition: mythbrowser.h:80
MythBrowser::slotAddTab
void slotAddTab()
Definition: mythbrowser.h:51
MythUIButtonList::GetValue
virtual QString GetValue() const
Definition: mythuibuttonlist.cpp:1609
MythUIText::SetText
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:134
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:316
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:108
MythUIButtonList::SetItemCurrent
void SetItemCurrent(MythUIButtonListItem *item)
Definition: mythuibuttonlist.cpp:1557
build_compdb.action
action
Definition: build_compdb.py:9
MythBrowser::m_pageList
MythUIButtonList * m_pageList
Definition: mythbrowser.h:71
WebPage
Definition: webpage.h:17
MythUIWebBrowser
Web browsing widget.
Definition: mythuiwebbrowser.h:133
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:200
MythImage::Assign
void Assign(const QImage &img)
Definition: mythimage.cpp:79
GetMythPainter
MythPainter * GetMythPainter(void)
Definition: mythmainwindow.cpp:123
Bookmark::m_name
QString m_name
Definition: bookmarkmanager.h:19
MythBrowser::m_defaultSaveFilename
QString m_defaultSaveFilename
Definition: mythbrowser.h:83
GetMythUI
MythUIHelper * GetMythUI()
Definition: mythuihelper.cpp:66
MythBrowser::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythbrowser.cpp:321
mythmainwindow.h
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:52
OK
#define OK
Definition: dvbci.cpp:69
MythBrowser::m_defaultFavIcon
MythImage * m_defaultFavIcon
Definition: mythbrowser.h:89
WebPage::loadFinished
void loadFinished(bool OK)
MythBrowser::m_defaultSaveDir
QString m_defaultSaveDir
Definition: mythbrowser.h:82
MythMainWindow::PauseIdleTimer
void PauseIdleTimer(bool Pause)
Pause the idle timeout timer.
Definition: mythmainwindow.cpp:2198
mythbrowser.h