MythTV  master
bookmarkmanager.cpp
Go to the documentation of this file.
1 // C++
2 #include <iostream>
3 using namespace std;
4 
5 // Qt
6 #include <QString>
7 
8 // MythTV
9 #include <mythmainwindow.h>
10 #include <mythcontext.h>
11 #include <mythdbcon.h>
12 #include <mythdirs.h>
13 #include <mythuicheckbox.h>
14 #include <mythuibuttonlist.h>
15 #include <mythsystemlegacy.h>
16 
17 // mythbrowser
18 #include "bookmarkmanager.h"
19 #include "bookmarkeditor.h"
20 #include "browserdbutil.h"
21 #include "mythbrowser.h"
22 #include "mythflashplayer.h"
23 
24 // ---------------------------------------------------
25 
27  MythScreenType(parent, name),
28  m_commandEdit(nullptr), m_zoomEdit(nullptr),
29  m_descriptionText(nullptr), m_titleText(nullptr),
30  m_enablePluginsCheck(nullptr),
31  m_okButton(nullptr), m_cancelButton(nullptr)
32 {
33 }
34 
36 {
37  // Load the theme for this screen
38  bool foundtheme = LoadWindowFromXML("browser-ui.xml", "browserconfig", this);
39  if (!foundtheme)
40  return false;
41 
42  m_titleText = dynamic_cast<MythUIText *> (GetChild("title"));
43 
44  if (m_titleText)
45  m_titleText->SetText(tr("MythBrowser Settings"));
46 
47  m_commandEdit = dynamic_cast<MythUITextEdit *> (GetChild("command"));
48  m_zoomEdit = dynamic_cast<MythUITextEdit *> (GetChild("zoom"));
49  m_enablePluginsCheck = dynamic_cast<MythUICheckBox *> (GetChild("enablepluginscheck"));
50 
51  m_descriptionText = dynamic_cast<MythUIText *> (GetChild("description"));
52 
53  m_okButton = dynamic_cast<MythUIButton *> (GetChild("ok"));
54  m_cancelButton = dynamic_cast<MythUIButton *> (GetChild("cancel"));
55 
57  {
58  LOG(VB_GENERAL, LOG_ERR, "Theme is missing critical theme elements.");
59  return false;
60  }
61 
62  m_commandEdit->SetText(gCoreContext->GetSetting("WebBrowserCommand",
63  "Internal"));
64 
65  m_zoomEdit->SetText(gCoreContext->GetSetting("WebBrowserZoomLevel", "1.0"));
66 
67  int setting = gCoreContext->GetNumSetting("WebBrowserEnablePlugins", 1);
68  if (setting == 1)
70 
71  connect(m_okButton, SIGNAL(Clicked()), this, SLOT(slotSave()));
72  connect(m_cancelButton, SIGNAL(Clicked()), this, SLOT(Close()));
73 
74  connect(m_commandEdit, SIGNAL(TakingFocus()), SLOT(slotFocusChanged()));
75  connect(m_zoomEdit , SIGNAL(TakingFocus()), SLOT(slotFocusChanged()));
76  connect(m_enablePluginsCheck, SIGNAL(TakingFocus()), SLOT(slotFocusChanged()));
77  connect(m_okButton, SIGNAL(TakingFocus()), SLOT(slotFocusChanged()));
78  connect(m_cancelButton, SIGNAL(TakingFocus()), SLOT(slotFocusChanged()));
79 
81 
83 
84  return true;
85 }
86 
88 {
89  float zoom = m_zoomEdit->GetText().toFloat();
90  if (zoom > 5.0f)
91  zoom = 5.0f;
92  if (zoom < 0.3f)
93  zoom = 0.3f;
94  gCoreContext->SaveSetting("WebBrowserZoomLevel", QString("%1").arg(zoom));
95  gCoreContext->SaveSetting("WebBrowserCommand", m_commandEdit->GetText());
96  int checkstate = 0;
98  checkstate = 1;
99  gCoreContext->SaveSetting("WebBrowserEnablePlugins", checkstate);
100 
101  Close();
102 }
103 
104 bool BrowserConfig::keyPressEvent(QKeyEvent *event)
105 {
106  if (GetFocusWidget()->keyPressEvent(event))
107  return true;
108 
109  return MythScreenType::keyPressEvent(event);
110 }
111 
113 {
114  if (!m_descriptionText)
115  return;
116 
117  QString msg = "";
118  if (GetFocusWidget() == m_commandEdit)
119  msg = tr("This is the command that will be used to show the web browser. "
120  "Use 'Internal' to use the built in web browser'. "
121  "%ZOOM% and %URL% will be replaced with the zoom level and URL list.");
122  else if (GetFocusWidget() == m_zoomEdit)
123  msg = tr("This is the default text size that will be used. Valid values "
124  "for the Internal browser are from 0.3 to 5.0 with 1.0 being "
125  "normal size less than 1 is smaller and greater than 1 is "
126  "larger than normal size.");
127  else if (GetFocusWidget() == m_enablePluginsCheck)
128  msg = tr("If checked this will enable browser plugins if the 'Internal' "
129  "browser is being used.");
130  else if (GetFocusWidget() == m_cancelButton)
131  msg = tr("Exit without saving settings");
132  else if (GetFocusWidget() == m_okButton)
133  msg = tr("Save settings and Exit");
134 
136 }
137 
138 // ---------------------------------------------------
139 
141  : MythScreenType(parent, name)
142 {
143  m_bookmarkList = nullptr;
144  m_groupList = nullptr;
145  m_messageText = nullptr;
146  m_menuPopup = nullptr;
147 }
148 
150 {
151  // Load the theme for this screen
152  bool foundtheme = LoadWindowFromXML("browser-ui.xml", "bookmarkmanager", this);
153  if (!foundtheme)
154  return false;
155 
156  m_groupList = dynamic_cast<MythUIButtonList *>(GetChild("grouplist"));
157  m_bookmarkList = dynamic_cast<MythUIButtonList *>(GetChild("bookmarklist"));
158 
159  // optional text area warning user hasn't set any bookmarks yet
160  m_messageText = dynamic_cast<MythUIText *>(GetChild("messagetext"));
161  if (m_messageText)
162  m_messageText->SetText(tr("No bookmarks defined.\n\n"
163  "Use the 'Add Bookmark' menu option to add new bookmarks"));
164 
165  if (!m_groupList || !m_bookmarkList)
166  {
167  LOG(VB_GENERAL, LOG_ERR, "Theme is missing critical theme elements.");
168  return false;
169  }
170 
172  UpdateGroupList();
173  UpdateURLList();
174 
175  connect(m_groupList, SIGNAL(itemSelected(MythUIButtonListItem*)),
177 
178  connect(m_bookmarkList, SIGNAL(itemClicked(MythUIButtonListItem*)),
180 
181  BuildFocusList();
182 
184 
185  return true;
186 }
187 
189 {
190  while (!m_siteList.isEmpty())
191  delete m_siteList.takeFirst();
192 }
193 
195 {
196  m_groupList->Reset();
197  QStringList groups;
198  for (int x = 0; x < m_siteList.count(); x++)
199  {
200  Bookmark *site = m_siteList.at(x);
201 
202  if (groups.indexOf(site->category) == -1)
203  {
204  groups.append(site->category);
206  }
207  }
208 }
209 
211 {
213 
214  if (m_messageText)
215  m_messageText->SetVisible((m_siteList.count() == 0));
216 
218  if (!item)
219  return;
220 
221  QString group = item->GetText();
222 
223  for (int x = 0; x < m_siteList.count(); x++)
224  {
225  Bookmark *site = m_siteList.at(x);
226 
227  if (group == site->category)
228  {
231  item2->SetText(site->name, "name");
232  item2->SetText(site->url, "url");
233  if (site->isHomepage)
234  item2->DisplayState("yes", "homepage");
235  item2->SetData(qVariantFromValue(site));
236  item2->setChecked(site->selected ?
238  }
239  }
240 }
241 
243 {
244  uint count = 0;
245 
246  for (int x = 0; x < m_siteList.size(); x++)
247  {
248  Bookmark *site = m_siteList.at(x);
249  if (site && site->selected)
250  count++;
251  }
252 
253  return count;
254 }
255 
256 bool BookmarkManager::keyPressEvent(QKeyEvent *event)
257 {
258  if (GetFocusWidget()->keyPressEvent(event))
259  return true;
260 
261  QStringList actions;
262  bool handled = GetMythMainWindow()->TranslateKeyPress("qt", event, actions);
263 
264  for (int i = 0; i < actions.size() && !handled; i++)
265  {
266 
267  QString action = actions[i];
268  handled = true;
269 
270  if (action == "MENU")
271  {
272  QString label = tr("Actions");
273 
274  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
275 
276  m_menuPopup = new MythDialogBox(label, popupStack, "actionmenu");
277 
278  if (!m_menuPopup->Create())
279  {
280  delete m_menuPopup;
281  m_menuPopup = nullptr;
282  return true;
283  }
284 
285  m_menuPopup->SetReturnEvent(this, "action");
286 
287  m_menuPopup->AddButton(tr("Set Homepage"), SLOT(slotSetHomepage()));
288  m_menuPopup->AddButton(tr("Add Bookmark"), SLOT(slotAddBookmark()));
289 
291  {
292  m_menuPopup->AddButton(tr("Edit Bookmark"), SLOT(slotEditBookmark()));
293  m_menuPopup->AddButton(tr("Delete Bookmark"), SLOT(slotDeleteCurrent()));
294  m_menuPopup->AddButton(tr("Show Bookmark"), SLOT(slotShowCurrent()));
295  }
296 
297  if (GetMarkedCount() > 0)
298  {
299  m_menuPopup->AddButton(tr("Delete Marked"), SLOT(slotDeleteMarked()));
300  m_menuPopup->AddButton(tr("Show Marked"), SLOT(slotShowMarked()));
301  m_menuPopup->AddButton(tr("Clear Marked"), SLOT(slotClearMarked()));
302  }
303 
304  m_menuPopup->AddButton(tr("Settings"), SLOT(slotSettings()));
305 
306  popupStack->AddScreen(m_menuPopup);
307  }
308  else if (action == "INFO")
309  {
311 
312  if (item)
313  {
314  Bookmark *site = item->GetData().value<Bookmark*>();
315 
317  {
319  if (site)
320  site->selected = true;
321  }
322  else
323  {
325  if (site)
326  site->selected = false;
327  }
328  }
329  }
330  else if (action == "DELETE")
332  else if (action == "EDIT")
334  else
335  handled = false;
336  }
337 
338  if (!handled && MythScreenType::keyPressEvent(event))
339  handled = true;
340 
341  return handled;
342 }
343 
345 {
346  (void) item;
347 
348  UpdateURLList();
350 }
351 
353 {
354  if (!item)
355  return;
356 
357  Bookmark *site = item->GetData().value<Bookmark*>();
358  if (!site)
359  return;
360 
361  m_savedBookmark = *site;
362 
363  QString cmd = gCoreContext->GetSetting("WebBrowserCommand", "Internal");
364  QString zoom = gCoreContext->GetSetting("WebBrowserZoomLevel", "1.0");
365  QStringList urls;
366 
367  urls.append(site->url);
368 
369  if (cmd.toLower() == "internal")
370  {
372 
373  MythScreenType *mythbrowser;
374  if (urls[0].startsWith("mythflash://"))
375  mythbrowser = new MythFlashPlayer(mainStack, urls);
376  else
377  mythbrowser = new MythBrowser(mainStack, urls);
378 
379  if (mythbrowser->Create())
380  {
381  connect(mythbrowser, SIGNAL(Exiting()), SLOT(slotBrowserClosed()));
382  mainStack->AddScreen(mythbrowser);
383  }
384  else
385  delete mythbrowser;
386  }
387  else
388  {
389  cmd.replace("%ZOOM%", zoom);
390  cmd.replace("%URL%", urls.join(" "));
391 
392  cmd.replace("&","\\&");
393  cmd.replace(";","\\;");
394 
395  GetMythMainWindow()->AllowInput(false);
397  GetMythMainWindow()->AllowInput(true);
398 
399  // we need to reload the bookmarks incase the user added/deleted
400  // any while in MythBrowser
401  ReloadBookmarks();
402  }
403 }
404 
406 {
407  Bookmark *site = nullptr;
408 
409  if (edit)
410  {
412 
413  if (item && item->GetData().isValid())
414  {
415  site = item->GetData().value<Bookmark*>();
416  m_savedBookmark = *site;
417  }
418  else
419  {
420  LOG(VB_GENERAL, LOG_ERR, "BookmarkManager: Something is wrong. "
421  "Asked to edit a non existent bookmark!");
422  return;
423  }
424  }
425 
426 
428 
429  BookmarkEditor *editor = new BookmarkEditor(&m_savedBookmark, edit, mainStack,
430  "bookmarkeditor");
431 
432  connect(editor, SIGNAL(Exiting()), this, SLOT(slotEditDialogExited()));
433 
434  if (editor->Create())
435  mainStack->AddScreen(editor);
436 }
437 
439 {
440  ReloadBookmarks();
441 }
442 
444 {
446  UpdateGroupList();
447 
449  UpdateURLList();
450 
451  // try to set the current item to name
452  MythUIButtonListItem *item;
453  for (int x = 0; x < m_bookmarkList->GetCount(); x++)
454  {
455  item = m_bookmarkList->GetItemAt(x);
456  if (item && item->GetData().isValid())
457  {
458  Bookmark *site = item->GetData().value<Bookmark*>();
459  if (site && (*site == m_savedBookmark))
460  {
462  break;
463  }
464  }
465  }
466 }
467 
469 {
471  BrowserConfig *config = new BrowserConfig(mainStack, "browserconfig");
472 
473  if (config->Create())
474  mainStack->AddScreen(config);
475 }
476 
478 {
479  // Clear all homepage information
481 
482  // Set the homepage information for selected bookmark
484  if (item && item->GetData().isValid())
485  {
486  Bookmark *site = item->GetData().value<Bookmark*>();
487  if (site)
488  UpdateHomepageInDB(site);
489  }
490  ReloadBookmarks();
491 }
492 
494 {
495  ShowEditDialog(false);
496 }
497 
499 {
500  ShowEditDialog(true);
501 }
502 
504 {
506  return;
507 
508  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
509 
510  QString message = tr("Are you sure you want to delete the selected bookmark?");
511 
512  MythConfirmationDialog *dialog = new MythConfirmationDialog(popupStack, message, true);
513 
514  if (dialog->Create())
515  popupStack->AddScreen(dialog);
516 
517  connect(dialog, SIGNAL(haveResult(bool)),
518  this, SLOT(slotDoDeleteCurrent(bool)));
519 }
520 
522 {
523  if (!doDelete)
524  return;
525 
527  if (item)
528  {
529  QString category = "";
530  Bookmark *site = item->GetData().value<Bookmark*>();
531  if (site)
532  {
533  category = site->category;
534  RemoveFromDB(site);
535  }
536 
538  UpdateGroupList();
539 
540  if (category != "")
541  m_groupList->MoveToNamedPosition(category);
542 
543  UpdateURLList();
544  }
545 }
546 
548 {
549  if (GetMarkedCount() == 0)
550  return;
551 
552  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
553 
554  QString message = tr("Are you sure you want to delete the marked bookmarks?");
555 
556  MythConfirmationDialog *dialog = new MythConfirmationDialog(popupStack, message, true);
557 
558  if (dialog->Create())
559  popupStack->AddScreen(dialog);
560 
561  connect(dialog, SIGNAL(haveResult(bool)),
562  this, SLOT(slotDoDeleteMarked(bool)));
563 }
564 
566 {
567  if (!doDelete)
568  return;
569 
570  QString category = m_groupList->GetValue();
571 
572  for (int x = 0; x < m_siteList.size(); x++)
573  {
574  Bookmark *site = m_siteList.at(x);
575  if (site && site->selected)
576  RemoveFromDB(site);
577  }
578 
580  UpdateGroupList();
581 
582  if (category != "")
583  m_groupList->MoveToNamedPosition(category);
584 
585  UpdateURLList();
586 }
587 
589 {
591  if (item)
592  slotBookmarkClicked(item);
593 }
594 
596 {
597  if (GetMarkedCount() == 0)
598  return;
599 
601  if (item && item->GetData().isValid())
602  {
603  Bookmark *site = item->GetData().value<Bookmark*>();
604  m_savedBookmark = *site;
605  }
606 
607  QString cmd = gCoreContext->GetSetting("WebBrowserCommand", "Internal");
608  QString zoom = gCoreContext->GetSetting("WebBrowserZoomLevel", "1.0");
609  QStringList urls;
610 
611  for (int x = 0; x < m_siteList.size(); x++)
612  {
613  Bookmark *site = m_siteList.at(x);
614  if (site && site->selected)
615  urls.append(site->url);
616  }
617 
618  if (cmd.toLower() == "internal")
619  {
621 
622  MythBrowser *mythbrowser = new MythBrowser(mainStack, urls);
623 
624  if (mythbrowser->Create())
625  {
626  connect(mythbrowser, SIGNAL(Exiting()), SLOT(slotBrowserClosed()));
627  mainStack->AddScreen(mythbrowser);
628  }
629  else
630  delete mythbrowser;
631  }
632  else
633  {
634  cmd.replace("%ZOOM%", zoom);
635  cmd.replace("%URL%", urls.join(" "));
636 
637  cmd.replace("&","\\&");
638  cmd.replace(";","\\;");
639 
640  GetMythMainWindow()->AllowInput(false);
642  GetMythMainWindow()->AllowInput(true);
643 
644  // we need to reload the bookmarks incase the user added/deleted
645  // any while in MythBrowser
646  ReloadBookmarks();
647  }
648 }
649 
651 {
652  // we need to reload the bookmarks incase the user added/deleted
653  // any while in MythBrowser
654  ReloadBookmarks();
655 }
656 
658 {
659  for (int x = 0; x < m_bookmarkList->GetCount(); x++)
660  {
662  if (item)
663  {
665 
666  Bookmark *site = item->GetData().value<Bookmark*>();
667  if (site)
668  site->selected = false;
669  }
670  }
671 }
avoid disabling UI drawing
Definition: mythsystem.h:35
void slotBrowserClosed(void)
MythUIStateType::StateType GetCheckState() const
bool RemoveFromDB(Bookmark *site)
MythUIButton * m_okButton
Dialog asking for user confirmation.
QList< Bookmark * > m_siteList
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
MythUIText * m_messageText
void SetData(QVariant data)
void SaveSetting(const QString &key, int newValue)
QString category
virtual bool Create(void)
Basic menu dialog, message and a list of options.
void slotAddBookmark(void)
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:168
MythUICheckBox * m_enablePluginsCheck
void AllowInput(bool allow)
MythUIText * m_descriptionText
MythScreenStack * GetStack(const QString &stackname)
unsigned int uint
Definition: compat.h:140
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythScreenStack * GetMainStack()
MythUIButtonListItem * GetItemAt(int pos) const
QString name
void BuildFocusList(void)
void slotBookmarkClicked(MythUIButtonListItem *item)
void UpdateGroupList(void)
MythDialogBox * m_menuPopup
BrowserConfig(MythScreenStack *parent, const char *name=nullptr)
MythUITextEdit * m_commandEdit
bool MoveToNamedPosition(const QString &position_name)
void ShowEditDialog(bool edit)
void slotDoDeleteCurrent(bool doDelete)
MythUIButtonList * m_groupList
void slotSetHomepage(void)
virtual void Close()
void AddButton(const QString &title, QVariant data=0, bool newMenu=false, bool setCurrent=false)
virtual void SetVisible(bool visible)
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
bool Create(void) override
Definition: mythbrowser.cpp:45
void TakingFocus()
bool Create(void) override
void Refresh(void)
QString GetSetting(const QString &key, const QString &defaultval="")
void slotShowMarked(void)
bool ResetHomepageFromDB()
bool keyPressEvent(QKeyEvent *) override
Key event handler.
void SetText(const QString &text, const QString &name="", const QString &state="")
bool keyPressEvent(QKeyEvent *) override
Key event handler.
void slotDoDeleteMarked(bool doDelete)
MythUIButtonList * m_bookmarkList
void slotShowCurrent(void)
const char * name
Definition: ParseText.cpp:339
uint myth_system(const QString &command, uint flags, uint timeout)
void ReloadBookmarks(void)
CheckState state() const
void slotGroupSelected(MythUIButtonListItem *item)
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
void slotFocusChanged(void)
void slotSave(void)
void SetText(const QString &text, bool moveCursor=true)
MythUIType * GetFocusWidget(void) const
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
MythMainWindow * GetMythMainWindow(void)
QString GetText(const QString &name="") const
virtual QString GetValue() const
int GetNumSetting(const QString &key, int defaultval=0)
void slotClearMarked(void)
Bookmark m_savedBookmark
bool keyPressEvent(QKeyEvent *) override
Key event handler.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
QString url
bool Create(void) override
MythUIText * m_titleText
void SetReturnEvent(QObject *retobject, const QString &resultid)
void slotEditDialogExited(void)
void UpdateURLList(void)
void SetCheckState(MythUIStateType::StateType state)
bool Create(void) override
void SetItemCurrent(MythUIButtonListItem *item)
void slotDeleteMarked(void)
uint GetMarkedCount(void)
bool isHomepage
void slotDeleteCurrent(void)
bool SetFocusWidget(MythUIType *widget=nullptr)
bool UpdateHomepageInDB(Bookmark *site)
Site category, name and URL edit screen.
Screen in which all other widgets are contained and rendered.
MythUITextEdit * m_zoomEdit
void slotEditBookmark(void)
bool Create(void) override
MythUIButton * m_cancelButton
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:155
void DisplayState(const QString &state, const QString &name)
void slotSettings(void)
BookmarkManager(MythScreenStack *parent, const char *name)
int GetSiteList(QList< Bookmark * > &siteList)
void setChecked(CheckState state)
QString GetText(void) const
MythUIButtonListItem * GetItemCurrent() const
bool Create(void) override