MythTV master
bookmarkmanager.cpp
Go to the documentation of this file.
1// C++
2#include <algorithm>
3#include <iostream>
4
5// Qt
6#include <QString>
7
8// MythTV
17
18// mythbrowser
19#include "bookmarkmanager.h"
20#include "bookmarkeditor.h"
21#include "browserdbutil.h"
22#include "mythbrowser.h"
23#include "mythflashplayer.h"
24
25// ---------------------------------------------------
26
28{
29 // Load the theme for this screen
30 bool foundtheme = LoadWindowFromXML("browser-ui.xml", "browserconfig", this);
31 if (!foundtheme)
32 return false;
33
34 m_titleText = dynamic_cast<MythUIText *> (GetChild("title"));
35
36 if (m_titleText)
37 m_titleText->SetText(tr("MythBrowser Settings"));
38
39 m_commandEdit = dynamic_cast<MythUITextEdit *> (GetChild("command"));
40 m_zoomEdit = dynamic_cast<MythUITextEdit *> (GetChild("zoom"));
41 m_enablePluginsCheck = dynamic_cast<MythUICheckBox *> (GetChild("enablepluginscheck"));
42
43 m_descriptionText = dynamic_cast<MythUIText *> (GetChild("description"));
44
45 m_okButton = dynamic_cast<MythUIButton *> (GetChild("ok"));
46 m_cancelButton = dynamic_cast<MythUIButton *> (GetChild("cancel"));
47
49 {
50 LOG(VB_GENERAL, LOG_ERR, "Theme is missing critical theme elements.");
51 return false;
52 }
53
54 m_commandEdit->SetText(gCoreContext->GetSetting("WebBrowserCommand",
55 "Internal"));
56
57 m_zoomEdit->SetText(gCoreContext->GetSetting("WebBrowserZoomLevel", "1.0"));
58
59 int setting = gCoreContext->GetNumSetting("WebBrowserEnablePlugins", 1);
60 if (setting == 1)
62
65
71
73
75
76 return true;
77}
78
80{
81 float zoom = m_zoomEdit->GetText().toFloat();
82 zoom = std::clamp(zoom, 0.3F, 5.0F);
83 gCoreContext->SaveSetting("WebBrowserZoomLevel", QString("%1").arg(zoom));
84 gCoreContext->SaveSetting("WebBrowserCommand", m_commandEdit->GetText());
85 int checkstate = 0;
87 checkstate = 1;
88 gCoreContext->SaveSetting("WebBrowserEnablePlugins", checkstate);
89
90 Close();
91}
92
93bool BrowserConfig::keyPressEvent(QKeyEvent *event)
94{
95 if (GetFocusWidget()->keyPressEvent(event))
96 return true;
97
99}
100
102{
104 return;
105
106 QString msg = "";
108 {
109 msg = tr("This is the command that will be used to show the web browser. "
110 "Use 'Internal' to use the built in web browser'. "
111 "%ZOOM% and %URL% will be replaced with the zoom level and URL list.");
112 }
113 else if (GetFocusWidget() == m_zoomEdit)
114 {
115 msg = tr("This is the default text size that will be used. Valid values "
116 "for the Internal browser are from 0.3 to 5.0 with 1.0 being "
117 "normal size less than 1 is smaller and greater than 1 is "
118 "larger than normal size.");
119 }
121 {
122 msg = tr("If checked this will enable browser plugins if the 'Internal' "
123 "browser is being used.");
124 }
125 else if (GetFocusWidget() == m_cancelButton)
126 {
127 msg = tr("Exit without saving settings");
128 }
129 else if (GetFocusWidget() == m_okButton)
130 {
131 msg = tr("Save settings and Exit");
132 }
133
135}
136
137// ---------------------------------------------------
138
140{
141 // Load the theme for this screen
142 bool foundtheme = LoadWindowFromXML("browser-ui.xml", "bookmarkmanager", this);
143 if (!foundtheme)
144 return false;
145
146 m_groupList = dynamic_cast<MythUIButtonList *>(GetChild("grouplist"));
147 m_bookmarkList = dynamic_cast<MythUIButtonList *>(GetChild("bookmarklist"));
148
149 // optional text area warning user hasn't set any bookmarks yet
150 m_messageText = dynamic_cast<MythUIText *>(GetChild("messagetext"));
151 if (m_messageText)
152 m_messageText->SetText(tr("No bookmarks defined.\n\n"
153 "Use the 'Add Bookmark' menu option to add new bookmarks"));
154
156 {
157 LOG(VB_GENERAL, LOG_ERR, "Theme is missing critical theme elements.");
158 return false;
159 }
160
164
167
170
172
174
175 return true;
176}
177
179{
180 while (!m_siteList.isEmpty())
181 delete m_siteList.takeFirst();
182}
183
185{
187 QStringList groups;
188 groups.reserve(m_siteList.count());
189 for (int x = 0; x < m_siteList.count(); x++)
190 {
191 Bookmark *site = m_siteList.at(x);
192
193 if (groups.indexOf(site->m_category) == -1)
194 {
195 groups.append(site->m_category);
197 }
198 }
199}
200
202{
204
205 if (m_messageText)
206 m_messageText->SetVisible((m_siteList.count() == 0));
207
209 if (!item)
210 return;
211
212 QString group = item->GetText();
213
214 for (int x = 0; x < m_siteList.count(); x++)
215 {
216 Bookmark *site = m_siteList.at(x);
217
218 if (group == site->m_category)
219 {
220 auto *item2 = new MythUIButtonListItem(m_bookmarkList,
222 item2->SetText(site->m_name, "name");
223 item2->SetText(site->m_url, "url");
224 if (site->m_isHomepage)
225 item2->DisplayState("yes", "homepage");
226 item2->SetData(QVariant::fromValue(site));
227 item2->setChecked(site->m_selected ?
229 }
230 }
231}
232
234{
235 auto selected = [](auto *site){ return site && site->m_selected; };
236 return std::count_if(m_siteList.cbegin(), m_siteList.cend(), selected);
237}
238
239bool BookmarkManager::keyPressEvent(QKeyEvent *event)
240{
241 if (GetFocusWidget()->keyPressEvent(event))
242 return true;
243
244 QStringList actions;
245 bool handled = GetMythMainWindow()->TranslateKeyPress("qt", event, actions);
246
247 for (int i = 0; i < actions.size() && !handled; i++)
248 {
249
250 const QString& action = actions[i];
251 handled = true;
252
253 if (action == "MENU")
254 {
255 QString label = tr("Actions");
256
257 MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
258
259 m_menuPopup = new MythDialogBox(label, popupStack, "actionmenu");
260
261 if (!m_menuPopup->Create())
262 {
263 delete m_menuPopup;
264 m_menuPopup = nullptr;
265 return true;
266 }
267
268 m_menuPopup->SetReturnEvent(this, "action");
269
272
274 {
278 }
279
280 if (GetMarkedCount() > 0)
281 {
285 }
286
288
289 popupStack->AddScreen(m_menuPopup);
290 }
291 else if (action == "INFO")
292 {
294
295 if (item)
296 {
297 auto *site = item->GetData().value<Bookmark*>();
298
300 {
302 if (site)
303 site->m_selected = true;
304 }
305 else
306 {
308 if (site)
309 site->m_selected = false;
310 }
311 }
312 }
313 else if (action == "DELETE")
314 {
316 }
317 else if (action == "EDIT")
318 {
320 }
321 else
322 {
323 handled = false;
324 }
325 }
326
327 if (!handled && MythScreenType::keyPressEvent(event))
328 handled = true;
329
330 return handled;
331}
332
334{
337}
338
340{
341 if (!item)
342 return;
343
344 auto *site = item->GetData().value<Bookmark*>();
345 if (!site)
346 return;
347
348 m_savedBookmark = *site;
349
350 QString cmd = gCoreContext->GetSetting("WebBrowserCommand", "Internal");
351 QString zoom = gCoreContext->GetSetting("WebBrowserZoomLevel", "1.0");
352 QStringList urls;
353
354 urls.append(site->m_url);
355
356 if (cmd.toLower() == "internal")
357 {
359
360 MythScreenType *mythbrowser = nullptr;
361 if (urls[0].startsWith("mythflash://"))
362 mythbrowser = new MythFlashPlayer(mainStack, urls);
363 else
364 mythbrowser = new MythBrowser(mainStack, urls);
365
366 if (mythbrowser->Create())
367 {
368 connect(mythbrowser, &MythScreenType::Exiting, this, &BookmarkManager::slotBrowserClosed);
369 mainStack->AddScreen(mythbrowser);
370 }
371 else
372 {
373 delete mythbrowser;
374 }
375 }
376 else
377 {
378 cmd.replace("%ZOOM%", zoom);
379 cmd.replace("%URL%", urls.join(" "));
380
381 cmd.replace("&","\\&");
382 cmd.replace(";","\\;");
383
387
388 // we need to reload the bookmarks incase the user added/deleted
389 // any while in MythBrowser
391 }
392}
393
395{
396 if (edit)
397 {
399
400 if (!item || !item->GetData().isValid())
401 {
402 LOG(VB_GENERAL, LOG_ERR, "BookmarkManager: Something is wrong. "
403 "Asked to edit a non existent bookmark!");
404 return;
405 }
406 auto *site = item->GetData().value<Bookmark*>();
407 if (!site)
408 {
409 LOG(VB_GENERAL, LOG_ERR, "BookmarkManager: Something is wrong. "
410 "Existing bookmark is invalid!");
411 return;
412 }
413
414 m_savedBookmark = *site;
415 }
416
417
419
420 auto *editor = new BookmarkEditor(&m_savedBookmark, edit, mainStack,
421 "bookmarkeditor");
422
424
425 if (editor->Create())
426 mainStack->AddScreen(editor);
427}
428
430{
432}
433
435{
438
441
442 // try to set the current item to name
443 for (int x = 0; x < m_bookmarkList->GetCount(); x++)
444 {
446 if (item && item->GetData().isValid())
447 {
448 auto *site = item->GetData().value<Bookmark*>();
449 if (site && (*site == m_savedBookmark))
450 {
452 break;
453 }
454 }
455 }
456}
457
459{
461 auto *config = new BrowserConfig(mainStack, "browserconfig");
462
463 if (config->Create())
464 mainStack->AddScreen(config);
465}
466
468{
469 // Clear all homepage information
471
472 // Set the homepage information for selected bookmark
474 if (item && item->GetData().isValid())
475 {
476 auto *site = item->GetData().value<Bookmark*>();
477 if (site)
478 UpdateHomepageInDB(site);
479 }
481}
482
484{
485 ShowEditDialog(false);
486}
487
489{
490 ShowEditDialog(true);
491}
492
494{
496 return;
497
498 MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
499
500 QString message = tr("Are you sure you want to delete the selected bookmark?");
501
502 auto *dialog = new MythConfirmationDialog(popupStack, message, true);
503
504 if (dialog->Create())
505 popupStack->AddScreen(dialog);
506
507 connect(dialog, &MythConfirmationDialog::haveResult,
509}
510
512{
513 if (!doDelete)
514 return;
515
517 if (item)
518 {
519 QString category = "";
520 auto *site = item->GetData().value<Bookmark*>();
521 if (site)
522 {
523 category = site->m_category;
524 RemoveFromDB(site);
525 }
526
529
530 if (category != "")
532
534 }
535}
536
538{
539 if (GetMarkedCount() == 0)
540 return;
541
542 MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
543
544 QString message = tr("Are you sure you want to delete the marked bookmarks?");
545
546 auto *dialog = new MythConfirmationDialog(popupStack, message, true);
547
548 if (dialog->Create())
549 popupStack->AddScreen(dialog);
550
551 connect(dialog, &MythConfirmationDialog::haveResult,
553}
554
556{
557 if (!doDelete)
558 return;
559
560 QString category = m_groupList->GetValue();
561
562 for (auto *site : std::as_const(m_siteList))
563 {
564 if (site && site->m_selected)
565 RemoveFromDB(site);
566 }
567
570
571 if (category != "")
573
575}
576
578{
580 if (item)
582}
583
585{
586 if (GetMarkedCount() == 0)
587 return;
588
590 if (item && item->GetData().isValid())
591 {
592 auto *site = item->GetData().value<Bookmark*>();
593 if (site)
594 m_savedBookmark = *site;
595 }
596
597 QString cmd = gCoreContext->GetSetting("WebBrowserCommand", "Internal");
598 QString zoom = gCoreContext->GetSetting("WebBrowserZoomLevel", "1.0");
599 QStringList urls;
600
601 for (const auto *site : std::as_const(m_siteList))
602 {
603 if (site && site->m_selected)
604 urls.append(site->m_url);
605 }
606
607 if (cmd.toLower() == "internal")
608 {
610
611 auto *mythbrowser = new MythBrowser(mainStack, urls);
612
613 if (mythbrowser->Create())
614 {
615 connect(mythbrowser, &MythScreenType::Exiting, this, &BookmarkManager::slotBrowserClosed);
616 mainStack->AddScreen(mythbrowser);
617 }
618 else
619 {
620 delete mythbrowser;
621 }
622 }
623 else
624 {
625 cmd.replace("%ZOOM%", zoom);
626 cmd.replace("%URL%", urls.join(" "));
627
628 cmd.replace("&","\\&");
629 cmd.replace(";","\\;");
630
634
635 // we need to reload the bookmarks incase the user added/deleted
636 // any while in MythBrowser
638 }
639}
640
642{
643 // we need to reload the bookmarks incase the user added/deleted
644 // any while in MythBrowser
646}
647
649{
650 for (int x = 0; x < m_bookmarkList->GetCount(); x++)
651 {
653 if (item)
654 {
656
657 auto *site = item->GetData().value<Bookmark*>();
658 if (site)
659 site->m_selected = false;
660 }
661 }
662}
663
664#include "moc_bookmarkmanager.cpp"
int GetSiteList(QList< Bookmark * > &siteList)
bool RemoveFromDB(Bookmark *site)
bool UpdateHomepageInDB(Bookmark *site)
bool ResetHomepageFromDB()
Site category, name and URL edit screen.
void slotDeleteCurrent(void)
bool Create(void) override
void ShowEditDialog(bool edit)
MythUIButtonList * m_bookmarkList
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
void slotClearMarked(void)
void ReloadBookmarks(void)
void slotShowMarked(void)
void slotGroupSelected(MythUIButtonListItem *item)
Bookmark m_savedBookmark
void slotAddBookmark(void)
void slotDoDeleteCurrent(bool doDelete)
MythUIButtonList * m_groupList
void slotEditBookmark(void)
void slotDeleteMarked(void)
void slotShowCurrent(void)
MythDialogBox * m_menuPopup
static void slotSettings(void)
void slotDoDeleteMarked(bool doDelete)
void slotBookmarkClicked(MythUIButtonListItem *item)
void UpdateURLList(void)
~BookmarkManager() override
void slotBrowserClosed(void)
void UpdateGroupList(void)
void slotEditDialogExited(void)
MythUIText * m_messageText
QList< Bookmark * > m_siteList
void slotSetHomepage(void)
uint GetMarkedCount(void)
bool m_selected
QString m_category
QString m_name
QString m_url
bool m_isHomepage
MythUIButton * m_cancelButton
MythUIText * m_titleText
void slotFocusChanged(void)
bool Create(void) override
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
MythUIButton * m_okButton
MythUITextEdit * m_zoomEdit
MythUIText * m_descriptionText
MythUICheckBox * m_enablePluginsCheck
MythUITextEdit * m_commandEdit
void slotSave(void)
Dialog asking for user confirmation.
void SaveSetting(const QString &key, int newValue)
QString GetSetting(const QString &key, const QString &defaultval="")
int GetNumSetting(const QString &key, int defaultval=0)
Basic menu dialog, message and a list of options.
void AddButton(const QString &title)
void SetReturnEvent(QObject *retobject, const QString &resultid)
bool Create(void) override
MythScreenStack * GetMainStack()
bool TranslateKeyPress(const QString &Context, QKeyEvent *Event, QStringList &Actions, bool AllowJumps=true)
Get a list of actions for a keypress in the given context.
MythScreenStack * GetStack(const QString &Stackname)
void AllowInput(bool Allow)
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Screen in which all other widgets are contained and rendered.
virtual bool Create(void)
void BuildFocusList(void)
MythUIType * GetFocusWidget(void) const
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
bool SetFocusWidget(MythUIType *widget=nullptr)
virtual void Close()
CheckState state() const
void setChecked(CheckState state)
QString GetText(const QString &name="") const
List widget, displays list items in a variety of themeable arrangements and can trigger signals when ...
virtual QString GetValue() const
MythUIButtonListItem * GetItemCurrent() const
void SetItemCurrent(MythUIButtonListItem *item)
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
void itemClicked(MythUIButtonListItem *item)
MythUIButtonListItem * GetItemAt(int pos) const
bool MoveToNamedPosition(const QString &position_name)
void itemSelected(MythUIButtonListItem *item)
A single button widget.
Definition: mythuibutton.h:22
void Clicked()
A checkbox widget supporting three check states - on,off,half and two conditions - selected and unsel...
void SetCheckState(MythUIStateType::StateType state)
MythUIStateType::StateType GetCheckState() const
A text entry and edit widget.
QString GetText(void) const
void SetText(const QString &text, bool moveCursor=true)
All purpose text widget, displays a text string.
Definition: mythuitext.h:29
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:115
void TakingFocus(void)
virtual void SetVisible(bool visible)
void Refresh(void)
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:130
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
unsigned int uint
Definition: compat.h:60
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythMainWindow * GetMythMainWindow(void)
@ kMSDontDisableDrawing
avoid disabling UI drawing
Definition: mythsystem.h:37
uint myth_system(const QString &command, uint flags, std::chrono::seconds timeout)
static eu8 clamp(eu8 value, eu8 low, eu8 high)
Definition: pxsup2dast.c:204