MythTV  0.27pre
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
mythbrowser.cpp
Go to the documentation of this file.
1 #include <stdlib.h>
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 
22  QStringList &urlList, float zoom)
23  : MythScreenType (parent, "mythbrowser"),
24  m_urlList(urlList), m_pageList(NULL),
25  m_progressBar(NULL), m_titleText(NULL),
26  m_statusText(NULL), m_currentBrowser(-1),
27  m_zoom(zoom), m_menuPopup(NULL),
28  m_defaultFavIcon(NULL)
29 {
31 }
32 
34 {
35  while (!m_browserList.isEmpty())
36  delete m_browserList.takeFirst();
38  if (m_defaultFavIcon)
39  {
41  m_defaultFavIcon = NULL;
42  }
43 }
44 
46 {
47  // Load the theme for this screen
48  if (!LoadWindowFromXML("browser-ui.xml", "browser", this))
49  return false;
50 
51  bool err = false;
52  MythUIWebBrowser *browser = NULL;
53 
54  UIUtilE::Assign(this, browser, "webbrowser", &err);
55  UIUtilE::Assign(this, m_pageList, "pagelist", &err);
56  UIUtilW::Assign(this, m_progressBar, "progressbar");
57  UIUtilW::Assign(this, m_statusText, "status");
58  UIUtilW::Assign(this, m_titleText, "title");
59  UIUtilW::Assign(this, m_backButton, "back");
60  UIUtilW::Assign(this, m_forwardButton, "forward");
61  UIUtilW::Assign(this, m_exitButton, "exit");
62 
63  if (err)
64  {
65  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'browser'");
66  return false;
67  }
68 
69  connect(m_pageList, SIGNAL(itemSelected(MythUIButtonListItem*)),
71 
72  // create the default favicon
73  QString favIcon = "mb_default_favicon.png";
74  GetMythUI()->FindThemeFile(favIcon);
75  if (QFile::exists(favIcon))
76  {
77  QImage image(favIcon);
79  m_defaultFavIcon->Assign(image);
80  }
81 
82  // this is the template for all other browser tabs
83  WebPage *page = new WebPage(this, browser);
84 
85  m_browserList.append(page);
86  page->getBrowser()->SetZoom(m_zoom);
89 
90  page->SetActive(true);
91 
92  connect(page, SIGNAL(loadProgress(int)),
93  this, SLOT(slotLoadProgress(int)));
94  connect(page, SIGNAL(statusBarMessage(const QString&)),
95  this, SLOT(slotStatusBarMessage(const QString&)));
96  connect(page, SIGNAL(loadFinished(bool)),
97  this, SLOT(slotLoadFinished(bool)));
98 
99  if (m_progressBar)
100  m_progressBar->SetTotal(100);
101 
102  if (m_exitButton)
103  {
104  m_exitButton->SetEnabled(false);
105  m_exitButton->SetEnabled(true);
106  connect(m_exitButton, SIGNAL(Clicked()), this, SLOT(Close()));
107  }
108 
109  if (m_backButton)
110  {
111  m_backButton->SetEnabled(false);
112  connect(m_backButton, SIGNAL(Clicked()), this, SLOT(slotBack()));
113  }
114 
115  if (m_forwardButton)
116  {
117  m_forwardButton->SetEnabled(false);
118  connect(m_forwardButton, SIGNAL(Clicked()), this, SLOT(slotForward()));
119  }
120 
121  BuildFocusList();
122 
123  SetFocusWidget(browser);
124 
126 
127  for (int x = 1; x < m_urlList.size(); x++)
128  slotAddTab(m_urlList[x], false);
129 
130  switchTab(0);
131 
132  return true;
133 }
134 
136 {
137  if (m_currentBrowser >=0 && m_currentBrowser < m_browserList.size())
138  return m_browserList[m_currentBrowser]->getBrowser();
139  else
140  return m_browserList[0]->getBrowser();
141 }
142 
144 {
145  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
146 
147  QString message = tr("Enter URL");
148 
149 
150  MythTextInputDialog *dialog = new MythTextInputDialog(popupStack, message);
151 
152  if (dialog->Create())
153  popupStack->AddScreen(dialog);
154 
155  connect(dialog, SIGNAL(haveResult(QString)),
156  SLOT(slotOpenURL(QString)), Qt::QueuedConnection);
157 }
158 
159 void MythBrowser::slotAddTab(const QString &url, bool doSwitch)
160 {
161  QString name = QString("browser%1").arg(m_browserList.size() + 1);
162  WebPage *page = new WebPage(this, m_browserList[0]->getBrowser()->GetArea(),
163  name.toAscii().constData());
164  page->getBrowser()->SetZoom(m_zoom);
165 
166  m_browserList.append(page);
167 
168  QString newUrl = url;
169 
170  if (newUrl.isEmpty())
171  newUrl = "http://www.google.com"; // TODO: add a user definable home page
172 
173  if (!newUrl.startsWith("http://") && !newUrl.startsWith("https://") &&
174  !newUrl.startsWith("file:/") )
175  newUrl.prepend("http://");
176  page->getBrowser()->LoadPage(QUrl::fromEncoded(newUrl.toLocal8Bit()));
177 
178  page->SetActive(false);
179 
180  connect(page, SIGNAL(loadProgress(int)),
181  this, SLOT(slotLoadProgress(int)));
182  connect(page, SIGNAL(statusBarMessage(const QString&)),
183  this, SLOT(slotStatusBarMessage(const QString&)));
184  connect(page, SIGNAL(loadFinished(bool)),
185  this, SLOT(slotLoadFinished(bool)));
186 
187  if (doSwitch)
189 }
190 
192 {
193  if (m_browserList.size() < 2)
194  return;
195 
196  if (m_currentBrowser >= 0 && m_currentBrowser < m_browserList.size())
197  {
198  int tab = m_currentBrowser;
199  m_currentBrowser = -1;
200  WebPage *page = m_browserList.takeAt(tab);
201  delete page;
202 
203  if (tab >= m_browserList.size())
204  tab = m_browserList.size() - 1;
205 
206  switchTab(tab);
207  }
208 }
209 
210 void MythBrowser::switchTab(int newTab)
211 {
212  if (newTab == m_currentBrowser)
213  return;
214 
215  if (newTab < 0 || newTab >= m_browserList.size())
216  return;
217 
218  if (m_currentBrowser >= 0 && m_currentBrowser < m_browserList.size())
219  m_browserList[m_currentBrowser]->SetActive(false);
220 
221  BuildFocusList();
222 
223  m_browserList[newTab]->SetActive(true);
224 
225  m_currentBrowser = newTab;
226 
227  if (GetFocusWidget() != m_pageList)
229 }
230 
231 void MythBrowser::slotOpenURL(const QString &url)
232 {
233  QString sUrl = url;
234  if (!sUrl.startsWith("http://") && !sUrl.startsWith("https://") &&
235  !sUrl.startsWith("file:/") )
236  sUrl.prepend("http://");
237 
238  activeBrowser()->LoadPage(QUrl::fromEncoded(sUrl.toLocal8Bit()));
239 }
240 
242 {
243  activeBrowser()->ZoomOut();
244 }
245 
247 {
248  activeBrowser()->ZoomIn();
249 }
250 
252 {
253  activeBrowser()->Back();
254 }
255 
257 {
258  activeBrowser()->Forward();
259 }
260 
262 {
265  m_editBookmark.url = activeBrowser()->GetUrl().toString();
266 
268 
270  true, mainStack, "bookmarkeditor");
271 
272 
273  if (editor->Create())
274  mainStack->AddScreen(editor);
275 }
276 
278 {
280  if (item)
281  item->SetText(tr("Loading..."));
282 }
283 
285 {
286  (void) OK;
287 
288  if (m_progressBar)
290 
291  if (m_backButton)
292  m_backButton->SetEnabled(activeBrowser()->CanGoBack());
293 
294  if (m_forwardButton)
295  m_forwardButton->SetEnabled(activeBrowser()->CanGoForward());
296 }
297 
299 {
300  if (m_progressBar)
301  m_progressBar->SetUsed(progress);
302 }
303 
304 void MythBrowser::slotTitleChanged(const QString &title)
305 {
307  if (item)
308  item->SetText(title);
309 }
310 
311 void MythBrowser::slotStatusBarMessage(const QString &text)
312 {
313  if (m_statusText)
314  m_statusText->SetText(text);
315 }
316 
318 {
319  if (!item)
320  return;
321 
323  slotStatusBarMessage(item->GetText());
324 }
325 
327 {
329 }
330 
331 bool MythBrowser::keyPressEvent(QKeyEvent *event)
332 {
333  // Always send keypress events to the currently focused widget first
334  if (GetFocusWidget()->keyPressEvent(event))
335  return true;
336 
337  bool handled = false;
338  QStringList actions;
339  handled = GetMythMainWindow()->TranslateKeyPress("Browser", event, actions);
340 
341  for (int i = 0; i < actions.size() && !handled; i++)
342  {
343 
344  QString action = actions[i];
345  handled = true;
346 
347  if (action == "MENU")
348  {
350 
351  QString label = tr("Actions");
352 
353  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
354 
355  m_menuPopup = new MythDialogBox(label, popupStack, "actionmenu");
356 
357  if (m_menuPopup->Create())
358  popupStack->AddScreen(m_menuPopup);
359 
360  m_menuPopup->SetReturnEvent(this, "action");
361 
362  m_menuPopup->AddButton(tr("Enter URL"), SLOT(slotEnterURL()));
363 
364  if (activeBrowser()->CanGoBack())
365  m_menuPopup->AddButton(tr("Back"), SLOT(slotBack()));
366 
367  if (activeBrowser()->CanGoForward())
368  m_menuPopup->AddButton(tr("Forward"), SLOT(slotForward()));
369 
370  m_menuPopup->AddButton(tr("Zoom In"), SLOT(slotZoomIn()));
371  m_menuPopup->AddButton(tr("Zoom Out"), SLOT(slotZoomOut()));
372  m_menuPopup->AddButton(tr("New Tab"), SLOT(slotAddTab()));
373 
374  if (m_browserList.size() > 1)
375  m_menuPopup->AddButton(tr("Delete Tab"), SLOT(slotDeleteTab()));
376 
377  m_menuPopup->AddButton(tr("Add Bookmark"), SLOT(slotAddBookmark()));
378  }
379  else if (action == "INFO")
380  {
381  if (GetFocusWidget() == m_pageList)
383  else
385  }
386  else if (action == "ESCAPE")
387  {
388  GetScreenStack()->PopScreen(true, true);
389  }
390  else if (action == "PREVTAB")
391  {
392  int pos = m_pageList->GetCurrentPos();
393  if (pos > 0)
394  m_pageList->SetItemCurrent(--pos);
395  }
396  else if (action == "NEXTTAB")
397  {
398  int pos = m_pageList->GetCurrentPos();
399  if (pos < m_pageList->GetCount() - 1)
400  m_pageList->SetItemCurrent(++pos);
401  }
402  else if (action == "DELETE")
403  {
404  slotDeleteTab();
405  }
406  else
407  handled = false;
408  }
409 
410  if (!handled && MythScreenType::keyPressEvent(event))
411  handled = true;
412 
413  return handled;
414 }
415 
416