00001
00002 #include <iostream>
00003 using namespace std;
00004
00005
00006 #include <QString>
00007
00008
00009 #include <mythmainwindow.h>
00010 #include <mythcontext.h>
00011 #include <mythdbcon.h>
00012 #include <mythdirs.h>
00013 #include <mythuicheckbox.h>
00014 #include <mythuibuttonlist.h>
00015 #include <mythsystem.h>
00016
00017
00018 #include "bookmarkmanager.h"
00019 #include "bookmarkeditor.h"
00020 #include "browserdbutil.h"
00021 #include "mythbrowser.h"
00022 #include "mythflashplayer.h"
00023
00024
00025
00026 BrowserConfig::BrowserConfig(MythScreenStack *parent, const char *name) :
00027 MythScreenType(parent, name),
00028 m_commandEdit(NULL), m_zoomEdit(NULL),
00029 m_descriptionText(NULL), m_titleText(NULL),
00030 m_enablePluginsCheck(NULL),
00031 m_okButton(NULL), m_cancelButton(NULL)
00032 {
00033 }
00034
00035 bool BrowserConfig::Create()
00036 {
00037 bool foundtheme = false;
00038
00039
00040 foundtheme = LoadWindowFromXML("browser-ui.xml", "browserconfig", this);
00041
00042 if (!foundtheme)
00043 return false;
00044
00045 m_titleText = dynamic_cast<MythUIText *> (GetChild("title"));
00046
00047 if (m_titleText)
00048 m_titleText->SetText(tr("MythBrowser Settings"));
00049
00050 m_commandEdit = dynamic_cast<MythUITextEdit *> (GetChild("command"));
00051 m_zoomEdit = dynamic_cast<MythUITextEdit *> (GetChild("zoom"));
00052 m_enablePluginsCheck = dynamic_cast<MythUICheckBox *> (GetChild("enablepluginscheck"));
00053
00054 m_descriptionText = dynamic_cast<MythUIText *> (GetChild("description"));
00055
00056 m_okButton = dynamic_cast<MythUIButton *> (GetChild("ok"));
00057 m_cancelButton = dynamic_cast<MythUIButton *> (GetChild("cancel"));
00058
00059 if (!m_commandEdit || !m_zoomEdit || !m_enablePluginsCheck || !m_okButton || !m_cancelButton)
00060 {
00061 LOG(VB_GENERAL, LOG_ERR, "Theme is missing critical theme elements.");
00062 return false;
00063 }
00064
00065 m_commandEdit->SetText(gCoreContext->GetSetting("WebBrowserCommand",
00066 "Internal"));
00067
00068 m_zoomEdit->SetText(gCoreContext->GetSetting("WebBrowserZoomLevel", "1.4"));
00069
00070 int setting = gCoreContext->GetNumSetting("WebBrowserEnablePlugins", 1);
00071 if (setting == 1)
00072 m_enablePluginsCheck->SetCheckState(MythUIStateType::Full);
00073
00074 connect(m_okButton, SIGNAL(Clicked()), this, SLOT(slotSave()));
00075 connect(m_cancelButton, SIGNAL(Clicked()), this, SLOT(Close()));
00076
00077 connect(m_commandEdit, SIGNAL(TakingFocus()), SLOT(slotFocusChanged()));
00078 connect(m_zoomEdit , SIGNAL(TakingFocus()), SLOT(slotFocusChanged()));
00079 connect(m_enablePluginsCheck, SIGNAL(TakingFocus()), SLOT(slotFocusChanged()));
00080 connect(m_okButton, SIGNAL(TakingFocus()), SLOT(slotFocusChanged()));
00081 connect(m_cancelButton, SIGNAL(TakingFocus()), SLOT(slotFocusChanged()));
00082
00083 BuildFocusList();
00084
00085 SetFocusWidget(m_commandEdit);
00086
00087 return true;
00088 }
00089
00090 BrowserConfig::~BrowserConfig()
00091 {
00092 }
00093
00094 void BrowserConfig::slotSave(void)
00095 {
00096 float zoom = m_zoomEdit->GetText().toFloat();
00097 if (zoom > 5.0)
00098 zoom = 5.0;
00099 if (zoom < 0.3)
00100 zoom = 0.3;
00101 gCoreContext->SaveSetting("WebBrowserZoomLevel", QString("%1").arg(zoom));
00102 gCoreContext->SaveSetting("WebBrowserCommand", m_commandEdit->GetText());
00103 int checkstate = 0;
00104 if (m_enablePluginsCheck->GetCheckState() == MythUIStateType::Full)
00105 checkstate = 1;
00106 gCoreContext->SaveSetting("WebBrowserEnablePlugins", checkstate);
00107
00108 Close();
00109 }
00110
00111 bool BrowserConfig::keyPressEvent(QKeyEvent *event)
00112 {
00113 if (GetFocusWidget()->keyPressEvent(event))
00114 return true;
00115
00116 bool handled = false;
00117
00118 if (!handled && MythScreenType::keyPressEvent(event))
00119 handled = true;
00120
00121 return handled;
00122 }
00123
00124 void BrowserConfig::slotFocusChanged(void)
00125 {
00126 if (!m_descriptionText)
00127 return;
00128
00129 QString msg = "";
00130 if (GetFocusWidget() == m_commandEdit)
00131 msg = tr("This is the command that will be used to show the web browser. "
00132 "Use 'Internal' to use the built in web browser'. "
00133 "%ZOOM% and %URL% will be replaced with the zoom level and URL list.");
00134 else if (GetFocusWidget() == m_zoomEdit)
00135 msg = tr("This is the default text size that will be used. Valid values "
00136 "for the Internal browser are from 0.3 to 5.0 with 1.0 being "
00137 "normal size less than 1 is smaller and greater than 1 is "
00138 "larger than normal size.");
00139 else if (GetFocusWidget() == m_enablePluginsCheck)
00140 msg = tr("If checked this will enable browser plugins if the 'Internal' "
00141 "browser is being used.");
00142 else if (GetFocusWidget() == m_cancelButton)
00143 msg = tr("Exit without saving settings");
00144 else if (GetFocusWidget() == m_okButton)
00145 msg = tr("Save settings and Exit");
00146
00147 m_descriptionText->SetText(msg);
00148 }
00149
00150
00151
00152 BookmarkManager::BookmarkManager(MythScreenStack *parent, const char *name)
00153 : MythScreenType(parent, name)
00154 {
00155 m_bookmarkList = NULL;
00156 m_groupList = NULL;
00157 m_messageText = NULL;
00158 m_menuPopup = NULL;
00159 }
00160
00161 bool BookmarkManager::Create(void)
00162 {
00163 bool foundtheme = false;
00164
00165
00166 foundtheme = LoadWindowFromXML("browser-ui.xml", "bookmarkmanager", this);
00167
00168 if (!foundtheme)
00169 return false;
00170
00171 m_groupList = dynamic_cast<MythUIButtonList *>(GetChild("grouplist"));
00172 m_bookmarkList = dynamic_cast<MythUIButtonList *>(GetChild("bookmarklist"));
00173
00174
00175 m_messageText = dynamic_cast<MythUIText *>(GetChild("messagetext"));
00176 if (m_messageText)
00177 m_messageText->SetText(tr("No bookmarks defined.\n\n"
00178 "Use the 'Add Bookmark' menu option to add new bookmarks"));
00179
00180 if (!m_groupList || !m_bookmarkList)
00181 {
00182 LOG(VB_GENERAL, LOG_ERR, "Theme is missing critical theme elements.");
00183 return false;
00184 }
00185
00186 GetSiteList(m_siteList);
00187 UpdateGroupList();
00188 UpdateURLList();
00189
00190 connect(m_groupList, SIGNAL(itemSelected(MythUIButtonListItem*)),
00191 this, SLOT(slotGroupSelected(MythUIButtonListItem*)));
00192
00193 connect(m_bookmarkList, SIGNAL(itemClicked(MythUIButtonListItem*)),
00194 this, SLOT(slotBookmarkClicked(MythUIButtonListItem*)));
00195
00196 BuildFocusList();
00197
00198 SetFocusWidget(m_groupList);
00199
00200 return true;
00201 }
00202
00203 BookmarkManager::~BookmarkManager()
00204 {
00205 while (!m_siteList.isEmpty())
00206 delete m_siteList.takeFirst();
00207 }
00208
00209 void BookmarkManager::UpdateGroupList(void)
00210 {
00211 m_groupList->Reset();
00212 QStringList groups;
00213 for (int x = 0; x < m_siteList.count(); x++)
00214 {
00215 Bookmark *site = m_siteList.at(x);
00216
00217 if (groups.indexOf(site->category) == -1)
00218 {
00219 groups.append(site->category);
00220 new MythUIButtonListItem(m_groupList, site->category);
00221 }
00222 }
00223 }
00224
00225 void BookmarkManager::UpdateURLList(void)
00226 {
00227 m_bookmarkList->Reset();
00228
00229 if (m_messageText)
00230 m_messageText->SetVisible((m_siteList.count() == 0));
00231
00232 MythUIButtonListItem *item = m_groupList->GetItemCurrent();
00233 if (!item)
00234 return;
00235
00236 QString group = item->GetText();
00237
00238 for (int x = 0; x < m_siteList.count(); x++)
00239 {
00240 Bookmark *site = m_siteList.at(x);
00241
00242 if (group == site->category)
00243 {
00244 MythUIButtonListItem *item = new MythUIButtonListItem(
00245 m_bookmarkList, "", "", true, MythUIButtonListItem::NotChecked);
00246 item->SetText(site->name, "name");
00247 item->SetText(site->url, "url");
00248 item->SetData(qVariantFromValue(site));
00249 item->setChecked(site->selected ?
00250 MythUIButtonListItem::FullChecked : MythUIButtonListItem::NotChecked);
00251 }
00252 }
00253 }
00254
00255 uint BookmarkManager::GetMarkedCount(void)
00256 {
00257 uint count = 0;
00258
00259 for (int x = 0; x < m_siteList.size(); x++)
00260 {
00261 Bookmark *site = m_siteList.at(x);
00262 if (site && site->selected)
00263 count++;
00264 }
00265
00266 return count;
00267 }
00268
00269 bool BookmarkManager::keyPressEvent(QKeyEvent *event)
00270 {
00271 if (GetFocusWidget()->keyPressEvent(event))
00272 return true;
00273
00274 bool handled = false;
00275 QStringList actions;
00276 handled = GetMythMainWindow()->TranslateKeyPress("qt", event, actions);
00277
00278 for (int i = 0; i < actions.size() && !handled; i++)
00279 {
00280
00281 QString action = actions[i];
00282 handled = true;
00283
00284 if (action == "MENU")
00285 {
00286 QString label = tr("Actions");
00287
00288 MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
00289
00290 m_menuPopup = new MythDialogBox(label, popupStack, "actionmenu");
00291
00292 if (!m_menuPopup->Create())
00293 {
00294 delete m_menuPopup;
00295 m_menuPopup = NULL;
00296 return true;
00297 }
00298
00299 m_menuPopup->SetReturnEvent(this, "action");
00300
00301 m_menuPopup->AddButton(tr("Add Bookmark"), SLOT(slotAddBookmark()));
00302
00303 if (m_bookmarkList->GetItemCurrent())
00304 {
00305 m_menuPopup->AddButton(tr("Edit Bookmark"), SLOT(slotEditBookmark()));
00306 m_menuPopup->AddButton(tr("Delete Bookmark"), SLOT(slotDeleteCurrent()));
00307 m_menuPopup->AddButton(tr("Show Bookmark"), SLOT(slotShowCurrent()));
00308 }
00309
00310 if (GetMarkedCount() > 0)
00311 {
00312 m_menuPopup->AddButton(tr("Delete Marked"), SLOT(slotDeleteMarked()));
00313 m_menuPopup->AddButton(tr("Show Marked"), SLOT(slotShowMarked()));
00314 m_menuPopup->AddButton(tr("Clear Marked"), SLOT(slotClearMarked()));
00315 }
00316
00317 popupStack->AddScreen(m_menuPopup);
00318 }
00319 else if (action == "INFO")
00320 {
00321 MythUIButtonListItem *item = m_bookmarkList->GetItemCurrent();
00322
00323 if (item)
00324 {
00325 Bookmark *site = qVariantValue<Bookmark*>(item->GetData());
00326
00327 if (item->state() == MythUIButtonListItem::NotChecked)
00328 {
00329 item->setChecked(MythUIButtonListItem::FullChecked);
00330 if (site)
00331 site->selected = true;
00332 }
00333 else
00334 {
00335 item->setChecked(MythUIButtonListItem::NotChecked);
00336 if (site)
00337 site->selected = false;
00338 }
00339 }
00340 }
00341 else if (action == "DELETE")
00342 slotDeleteCurrent();
00343 else if (action == "EDIT")
00344 slotEditBookmark();
00345 else
00346 handled = false;
00347 }
00348
00349 if (!handled && MythScreenType::keyPressEvent(event))
00350 handled = true;
00351
00352 return handled;
00353 }
00354
00355 void BookmarkManager::slotGroupSelected(MythUIButtonListItem *item)
00356 {
00357 (void) item;
00358
00359 UpdateURLList();
00360 m_bookmarkList->Refresh();
00361 }
00362
00363 void BookmarkManager::slotBookmarkClicked(MythUIButtonListItem *item)
00364 {
00365 if (!item)
00366 return;
00367
00368 Bookmark *site = qVariantValue<Bookmark*>(item->GetData());
00369 if (!site)
00370 return;
00371
00372 m_savedBookmark = *site;
00373
00374 QString cmd = gCoreContext->GetSetting("WebBrowserCommand", "Internal");
00375 QString zoom = gCoreContext->GetSetting("WebBrowserZoomLevel", "1.4");
00376 QStringList urls;
00377
00378 urls.append(site->url);
00379
00380 if (cmd.toLower() == "internal")
00381 {
00382 MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
00383
00384 MythScreenType *mythbrowser;
00385 if (urls[0].startsWith("mythflash://"))
00386 mythbrowser = new MythFlashPlayer(mainStack, urls);
00387 else
00388 mythbrowser = new MythBrowser(mainStack, urls, zoom.toFloat());
00389
00390 if (mythbrowser->Create())
00391 {
00392 connect(mythbrowser, SIGNAL(Exiting()), SLOT(slotBrowserClosed()));
00393 mainStack->AddScreen(mythbrowser);
00394 }
00395 else
00396 delete mythbrowser;
00397 }
00398 else
00399 {
00400 cmd.replace("%ZOOM%", zoom);
00401 cmd.replace("%URL%", urls.join(" "));
00402
00403 cmd.replace("&","\\&");
00404 cmd.replace(";","\\;");
00405
00406 GetMythMainWindow()->AllowInput(false);
00407 myth_system(cmd, kMSDontDisableDrawing);
00408 GetMythMainWindow()->AllowInput(true);
00409
00410
00411
00412 ReloadBookmarks();
00413 }
00414 }
00415
00416 void BookmarkManager::ShowEditDialog(bool edit)
00417 {
00418 Bookmark *site = NULL;
00419
00420 if (edit)
00421 {
00422 MythUIButtonListItem *item = m_bookmarkList->GetItemCurrent();
00423
00424 if (item && item->GetData().isValid())
00425 {
00426 site = qVariantValue<Bookmark*>(item->GetData());
00427 m_savedBookmark = *site;
00428 }
00429 else
00430 {
00431 LOG(VB_GENERAL, LOG_ERR, "BookmarkManager: Something is wrong. "
00432 "Asked to edit a non existent bookmark!");
00433 return;
00434 }
00435 }
00436
00437
00438 MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
00439
00440 BookmarkEditor *editor = new BookmarkEditor(&m_savedBookmark, edit, mainStack,
00441 "bookmarkeditor");
00442
00443 connect(editor, SIGNAL(Exiting()), this, SLOT(slotEditDialogExited()));
00444
00445 if (editor->Create())
00446 mainStack->AddScreen(editor);
00447 }
00448
00449 void BookmarkManager::slotEditDialogExited(void)
00450 {
00451 ReloadBookmarks();
00452 }
00453
00454 void BookmarkManager::ReloadBookmarks(void)
00455 {
00456 GetSiteList(m_siteList);
00457 UpdateGroupList();
00458
00459 m_groupList->MoveToNamedPosition(m_savedBookmark.category);
00460 UpdateURLList();
00461
00462
00463 MythUIButtonListItem *item;
00464 for (int x = 0; x < m_bookmarkList->GetCount(); x++)
00465 {
00466 item = m_bookmarkList->GetItemAt(x);
00467 if (item && item->GetData().isValid())
00468 {
00469 Bookmark *site = qVariantValue<Bookmark*>(item->GetData());
00470 if (site && (*site == m_savedBookmark))
00471 {
00472 m_bookmarkList->SetItemCurrent(item);
00473 break;
00474 }
00475 }
00476 }
00477 }
00478
00479 void BookmarkManager::slotAddBookmark(void)
00480 {
00481 ShowEditDialog(false);
00482 }
00483
00484 void BookmarkManager::slotEditBookmark(void)
00485 {
00486 ShowEditDialog(true);
00487 }
00488
00489 void BookmarkManager::slotDeleteCurrent(void)
00490 {
00491 if (!m_bookmarkList->GetItemCurrent())
00492 return;
00493
00494 MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
00495
00496 QString message = tr("Are you sure you want to delete the selected bookmark?");
00497
00498 MythConfirmationDialog *dialog = new MythConfirmationDialog(popupStack, message, true);
00499
00500 if (dialog->Create())
00501 popupStack->AddScreen(dialog);
00502
00503 connect(dialog, SIGNAL(haveResult(bool)),
00504 this, SLOT(slotDoDeleteCurrent(bool)));
00505 }
00506
00507 void BookmarkManager::slotDoDeleteCurrent(bool doDelete)
00508 {
00509 if (!doDelete)
00510 return;
00511
00512 MythUIButtonListItem *item = m_bookmarkList->GetItemCurrent();
00513 if (item)
00514 {
00515 QString category = "";
00516 Bookmark *site = qVariantValue<Bookmark*>(item->GetData());
00517 if (site)
00518 {
00519 category = site->category;
00520 RemoveFromDB(site);
00521 }
00522
00523 GetSiteList(m_siteList);
00524 UpdateGroupList();
00525
00526 if (category != "")
00527 m_groupList->MoveToNamedPosition(category);
00528
00529 UpdateURLList();
00530 }
00531 }
00532
00533 void BookmarkManager::slotDeleteMarked(void)
00534 {
00535 if (GetMarkedCount() == 0)
00536 return;
00537
00538 MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
00539
00540 QString message = tr("Are you sure you want to delete the marked bookmarks?");
00541
00542 MythConfirmationDialog *dialog = new MythConfirmationDialog(popupStack, message, true);
00543
00544 if (dialog->Create())
00545 popupStack->AddScreen(dialog);
00546
00547 connect(dialog, SIGNAL(haveResult(bool)),
00548 this, SLOT(slotDoDeleteMarked(bool)));
00549 }
00550
00551 void BookmarkManager::slotDoDeleteMarked(bool doDelete)
00552 {
00553 if (!doDelete)
00554 return;
00555
00556 QString category = m_groupList->GetValue();
00557
00558 for (int x = 0; x < m_siteList.size(); x++)
00559 {
00560 Bookmark *site = m_siteList.at(x);
00561 if (site && site->selected)
00562 RemoveFromDB(site);
00563 }
00564
00565 GetSiteList(m_siteList);
00566 UpdateGroupList();
00567
00568 if (category != "")
00569 m_groupList->MoveToNamedPosition(category);
00570
00571 UpdateURLList();
00572 }
00573
00574 void BookmarkManager::slotShowCurrent(void)
00575 {
00576 MythUIButtonListItem *item = m_bookmarkList->GetItemCurrent();
00577 if (item)
00578 slotBookmarkClicked(item);
00579 }
00580
00581 void BookmarkManager::slotShowMarked(void)
00582 {
00583 if (GetMarkedCount() == 0)
00584 return;
00585
00586 MythUIButtonListItem *item = m_bookmarkList->GetItemCurrent();
00587 if (item && item->GetData().isValid())
00588 {
00589 Bookmark *site = qVariantValue<Bookmark*>(item->GetData());
00590 m_savedBookmark = *site;
00591 }
00592
00593 QString cmd = gCoreContext->GetSetting("WebBrowserCommand", "Internal");
00594 QString zoom = gCoreContext->GetSetting("WebBrowserZoomLevel", "1.4");
00595 QStringList urls;
00596
00597 for (int x = 0; x < m_siteList.size(); x++)
00598 {
00599 Bookmark *site = m_siteList.at(x);
00600 if (site && site->selected)
00601 urls.append(site->url);
00602 }
00603
00604 if (cmd.toLower() == "internal")
00605 {
00606 MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
00607
00608 MythBrowser *mythbrowser = new MythBrowser(mainStack, urls, zoom.toFloat());
00609
00610 if (mythbrowser->Create())
00611 {
00612 connect(mythbrowser, SIGNAL(Exiting()), SLOT(slotBrowserClosed()));
00613 mainStack->AddScreen(mythbrowser);
00614 }
00615 else
00616 delete mythbrowser;
00617 }
00618 else
00619 {
00620 cmd.replace("%ZOOM%", zoom);
00621 cmd.replace("%URL%", urls.join(" "));
00622
00623 cmd.replace("&","\\&");
00624 cmd.replace(";","\\;");
00625
00626 GetMythMainWindow()->AllowInput(false);
00627 myth_system(cmd, kMSDontDisableDrawing);
00628 GetMythMainWindow()->AllowInput(true);
00629
00630
00631
00632 ReloadBookmarks();
00633 }
00634 }
00635
00636 void BookmarkManager::slotBrowserClosed(void)
00637 {
00638
00639
00640 ReloadBookmarks();
00641 }
00642
00643 void BookmarkManager::slotClearMarked(void)
00644 {
00645 for (int x = 0; x < m_bookmarkList->GetCount(); x++)
00646 {
00647 MythUIButtonListItem *item = m_bookmarkList->GetItemAt(x);
00648 if (item)
00649 {
00650 item->setChecked(MythUIButtonListItem::NotChecked);
00651
00652 Bookmark *site = qVariantValue<Bookmark*>(item->GetData());
00653 if (site)
00654 site->selected = false;
00655 }
00656 }
00657 }