MythTV  0.28pre
 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  : MythScreenType (parent, "mythbrowser"),
23  m_urlList(urlList), m_pageList(NULL),
24  m_progressBar(NULL), m_titleText(NULL),
25  m_statusText(NULL), m_backButton(NULL),
26  m_forwardButton(NULL), m_exitButton(NULL),
27  m_currentBrowser(-1), 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  if (GetMythUI()->FindThemeFile(favIcon))
75  {
76  if (QFile::exists(favIcon))
77  {
78  QImage image(favIcon);
80  m_defaultFavIcon->Assign(image);
81  }
82  }
83 
84  // this is the template for all other browser tabs
85  WebPage *page = new WebPage(this, browser);
86 
87  m_browserList.append(page);
90 
91  page->SetActive(true);
92 
93  connect(page, SIGNAL(loadProgress(int)),
94  this, SLOT(slotLoadProgress(int)));
95  connect(page, SIGNAL(statusBarMessage(const QString&)),
96  this, SLOT(slotStatusBarMessage(const QString&)));
97  connect(page, SIGNAL(loadFinished(bool)),
98  this, SLOT(slotLoadFinished(bool)));
99 
100  if (m_progressBar)
101  m_progressBar->SetTotal(100);
102 
103  if (m_exitButton)
104  {
105  m_exitButton->SetEnabled(false);
106  m_exitButton->SetEnabled(true);
107  connect(m_exitButton, SIGNAL(Clicked()), this, SLOT(Close()));
108  }
109 
110  if (m_backButton)
111  {
112  m_backButton->SetEnabled(false);
113  connect(m_backButton, SIGNAL(Clicked()), this, SLOT(slotBack()));
114  }
115 
116  if (m_forwardButton)
117  {
118  m_forwardButton->SetEnabled(false);
119  connect(m_forwardButton, SIGNAL(Clicked()), this, SLOT(slotForward()));
120  }
121 
122  BuildFocusList();
123 
124  SetFocusWidget(browser);
125 
127 
128  for (int x = 1; x < m_urlList.size(); x++)
129  slotAddTab(m_urlList[x], false);
130 
131  switchTab(0);
132 
133  return true;
134 }
135 
137 {
138  if (m_currentBrowser >=0 && m_currentBrowser < m_browserList.size())
139  return m_browserList[m_currentBrowser]->getBrowser();
140  else
141  return m_browserList[0]->getBrowser();
142 }
143 
145 {
146  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
147 
148  QString message = tr("Enter URL");
149 
150 
151  MythTextInputDialog *dialog = new MythTextInputDialog(popupStack, message);
152 
153  if (dialog->Create())
154  popupStack->AddScreen(dialog);
155 
156  connect(dialog, SIGNAL(haveResult(QString)),
157  SLOT(slotOpenURL(QString)), Qt::QueuedConnection);
158 }
159 
160 void MythBrowser::slotAddTab(const QString &url, bool doSwitch)
161 {
162  QString name = QString("browser%1").arg(m_browserList.size() + 1);
163  WebPage *page = new WebPage(this, m_browserList[0]->getBrowser()->GetArea(),
164  name.toAscii().constData());
165  m_browserList.append(page);
166 
167  QString newUrl = url;
168 
169  if (newUrl.isEmpty())
170  newUrl = "http://www.google.com"; // TODO: add a user definable home page
171 
172  if (!newUrl.startsWith("http://") && !newUrl.startsWith("https://") &&
173  !newUrl.startsWith("file:/") )
174  newUrl.prepend("http://");
175  page->getBrowser()->LoadPage(QUrl::fromEncoded(newUrl.toLocal8Bit()));
176 
177  page->SetActive(false);
178 
179  connect(page, SIGNAL(loadProgress(int)),
180  this, SLOT(slotLoadProgress(int)));
181  connect(page, SIGNAL(statusBarMessage(const QString&)),
182  this, SLOT(slotStatusBarMessage(const QString&)));
183  connect(page, SIGNAL(loadFinished(bool)),
184  this, SLOT(slotLoadFinished(bool)));
185 
186  if (doSwitch)
188 }
189 
191 {
192  if (m_browserList.size() < 2)
193  return;
194 
195  if (m_currentBrowser >= 0 && m_currentBrowser < m_browserList.size())
196  {
197  int tab = m_currentBrowser;
198  m_currentBrowser = -1;
199  WebPage *page = m_browserList.takeAt(tab);
200  delete page;
201 
202  if (tab >= m_browserList.size())
203  tab = m_browserList.size() - 1;
204 
205  switchTab(tab);
206  }
207 }
208 
209 void MythBrowser::switchTab(int newTab)
210 {
211  if (newTab == m_currentBrowser)
212  return;
213 
214  if (newTab < 0 || newTab >= m_browserList.size())
215  return;
216 
217  if (m_currentBrowser >= 0 && m_currentBrowser < m_browserList.size())
218  m_browserList[m_currentBrowser]->SetActive(false);
219 
220  BuildFocusList();
221 
222  m_browserList[newTab]->SetActive(true);
223 
224  m_currentBrowser = newTab;
225 
226  if (GetFocusWidget() != m_pageList)
228 }
229 
230 void MythBrowser::slotOpenURL(const QString &url)
231 {
232  QString sUrl = url;
233  if (!sUrl.startsWith("http://") && !sUrl.startsWith("https://") &&
234  !sUrl.startsWith("file:/") )
235  sUrl.prepend("http://");
236 
237  activeBrowser()->LoadPage(QUrl::fromEncoded(sUrl.toLocal8Bit()));
238 }
239 
241 {
242  activeBrowser()->ZoomOut();
243 }
244 
246 {
247  activeBrowser()->ZoomIn();
248 }
249 
251 {
252  activeBrowser()->Back();
253 }
254 
256 {
257  activeBrowser()->Forward();
258 }
259 
261 {
264  m_editBookmark.url = activeBrowser()->GetUrl().toString();
265 
267 
269  true, mainStack, "bookmarkeditor");
270 
271 
272  if (editor->Create())
273  mainStack->AddScreen(editor);
274 }
275 
277 {
279  if (item)
280  item->SetText(tr("Loading..."));
281 }
282 
284 {
285  (void) OK;
286 
287  if (m_progressBar)
289 
290  if (m_backButton)
291  m_backButton->SetEnabled(activeBrowser()->CanGoBack());
292 
293  if (m_forwardButton)
294  m_forwardButton->SetEnabled(activeBrowser()->CanGoForward());
295 }
296 
298 {
299  if (m_progressBar)
300  m_progressBar->SetUsed(progress);
301 }
302 
303 void MythBrowser::slotTitleChanged(const QString &title)
304 {
306  if (item)
307  item->SetText(title);
308 }
309 
310 void MythBrowser::slotStatusBarMessage(const QString &text)
311 {
312  if (m_statusText)
313  m_statusText->SetText(text);
314 }
315 
317 {
318  if (!item)
319  return;
320 
322  slotStatusBarMessage(item->GetText());
323 }
324 
326 {
328 }
329 
330 bool MythBrowser::keyPressEvent(QKeyEvent *event)
331 {
332  // Always send keypress events to the currently focused widget first
333  if (GetFocusWidget()->keyPressEvent(event))
334  return true;
335 
336  bool handled = false;
337  QStringList actions;
338  handled = GetMythMainWindow()->TranslateKeyPress("Browser", event, actions);
339 
340  for (int i = 0; i < actions.size() && !handled; i++)
341  {
342 
343  QString action = actions[i];
344  handled = true;
345 
346  if (action == "MENU")
347  {
349 
350  QString label = tr("Actions");
351 
352  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
353 
354  m_menuPopup = new MythDialogBox(label, popupStack, "actionmenu");
355 
356  if (m_menuPopup->Create())
357  popupStack->AddScreen(m_menuPopup);
358 
359  m_menuPopup->SetReturnEvent(this, "action");
360 
361  m_menuPopup->AddButton(tr("Enter URL"), SLOT(slotEnterURL()));
362 
363  if (activeBrowser()->CanGoBack())
364  m_menuPopup->AddButton(tr("Back"), SLOT(slotBack()));
365 
366  if (activeBrowser()->CanGoForward())
367  m_menuPopup->AddButton(tr("Forward"), SLOT(slotForward()));
368 
369  m_menuPopup->AddButton(tr("Zoom In"), SLOT(slotZoomIn()));
370  m_menuPopup->AddButton(tr("Zoom Out"), SLOT(slotZoomOut()));
371  m_menuPopup->AddButton(tr("New Tab"), SLOT(slotAddTab()));
372 
373  if (m_browserList.size() > 1)
374  m_menuPopup->AddButton(tr("Delete Tab"), SLOT(slotDeleteTab()));
375 
376  m_menuPopup->AddButton(tr("Add Bookmark"), SLOT(slotAddBookmark()));
377  }
378  else if (action == "INFO")
379  {
380  if (GetFocusWidget() == m_pageList)
382  else
384  }
385  else if (action == "ESCAPE")
386  {
388  }
389  else if (action == "PREVTAB")
390  {
391  int pos = m_pageList->GetCurrentPos();
392  if (pos > 0)
393  m_pageList->SetItemCurrent(--pos);
394  }
395  else if (action == "NEXTTAB")
396  {
397  int pos = m_pageList->GetCurrentPos();
398  if (pos < m_pageList->GetCount() - 1)
399  m_pageList->SetItemCurrent(++pos);
400  }
401  else if (action == "DELETE")
402  {
403  slotDeleteTab();
404  }
405  else
406  handled = false;
407  }
408 
409  if (!handled && MythScreenType::keyPressEvent(event))
410  handled = true;
411 
412  return handled;
413 }
414 
415 
void slotAddTab(const QString &url="", bool doSwitch=true)
void SetDefaultSaveFilename(const QString &)
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=NULL)
Definition: mythuiutils.h:26
void switchTab(int newTab)
void ZoomOut(void)
Decrease the text size.
MythDialogBox * m_menuPopup
Definition: iconview.h:130
void slotBack()
typedef void(__LZO_CDECL *lzo_free_func_t)(lzo_callback_p self
void SetEnabled(bool enable)
bool Create(void)
Definition: mythbrowser.cpp:45
QString GetText(const QString &name="") const
QUrl GetUrl(void)
Gets the current page's url.
QString category
Basic menu dialog, message and a list of options.
void SetDefaultSaveDirectory(const QString &)
MythDialogBox * m_menuPopup
Definition: mythbrowser.h:86
virtual bool Create(void)
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:166
MythPainter * GetMythPainter(void)
AllMusic * parent
void SetActive(bool active)
Definition: webpage.cpp:81
QString m_defaultSaveDir
Definition: mythbrowser.h:81
MythScreenStack * GetStack(const QString &stackname)
void slotZoomOut()
MythScreenStack * GetMainStack()
QString name
void Forward(void)
Got forward in page history.
MythBrowser(MythScreenStack *parent, QStringList &urlList)
Definition: mythbrowser.cpp:21
MythUIButtonListItem * GetItemCurrent() const
void slotLoadProgress(int progress)
virtual int DecrRef(void)
Decrements reference count and deletes on 0.
Definition: mythimage.cpp:72
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
MythUIWebBrowser * getBrowser()
Definition: webpage.h:28
void Back(void)
Got backward in page history.
MythUIButton * m_exitButton
Definition: mythbrowser.h:77
void slotEnterURL(void)
virtual QString GetValue() const
virtual MythRect GetArea(void) const
If the object has a minimum area defined, return it, other wise return the default area...
Definition: mythuitype.cpp:871
void slotDeleteTab(void)
void slotAddBookmark(void)
int GetCurrentPos() const
bool Create(void)
friend class WebPage
Definition: mythbrowser.h:90
MythUIProgressBar * m_progressBar
Definition: mythbrowser.h:72
MythUIButtonList * m_pageList
Definition: mythbrowser.h:70
void slotOpenURL(const QString &url)
void SetText(const QString &text, const QString &name="", const QString &state="")
void slotLoadStarted(void)
const char * name
Definition: ParseText.cpp:338
MythScreenStack * GetScreenStack() const
MythUIType * GetFocusWidget(void) const
MythUIHelper * GetMythUI()
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
int m_currentBrowser
Definition: mythbrowser.h:79
QString m_defaultSaveFilename
Definition: mythbrowser.h:82
void slotTabLosingFocus(void)
MythMainWindow * GetMythMainWindow(void)
void PauseIdleTimer(bool pause)
Dialog prompting the user to enter a text string.
QString url
MythImage * m_defaultFavIcon
Definition: mythbrowser.h:88
virtual bool keyPressEvent(QKeyEvent *)
Key event handler.
void slotLoadFinished(bool OK)
void SetReturnEvent(QObject *retobject, const QString &resultid)
bool SetFocusWidget(MythUIType *widget=NULL)
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true) MUNUSED_RESULT
Get a list of actions for a keypress in the given context.
GLuint GLfloat x
void ZoomIn(void)
Increase the text size.
void SetItemCurrent(MythUIButtonListItem *item)
void slotForward()
QList< WebPage * > m_browserList
Definition: mythbrowser.h:71
virtual void PopScreen(MythScreenType *screen=NULL, bool allowFade=true, bool deleteScreen=true)
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:97
Web browsing widget.
void LoadPage(QUrl)
Loads the specified url and displays it.
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
void slotZoomIn()
MythUIWebBrowser * activeBrowser(void)
bool keyPressEvent(QKeyEvent *)
Key event handler.