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