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