[video] fix selection after changing video or extra art
[xbmc.git] / xbmc / pvr / guilib / PVRGUIActionsEPG.cpp
blob15d3aedd081acc9a41f3be7791f855d4632042ca
1 /*
2 * Copyright (C) 2016-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "PVRGUIActionsEPG.h"
11 #include "FileItem.h"
12 #include "ServiceBroker.h"
13 #include "dialogs/GUIDialogYesNo.h"
14 #include "guilib/GUIComponent.h"
15 #include "guilib/GUIKeyboardFactory.h"
16 #include "guilib/GUIWindowManager.h"
17 #include "guilib/LocalizeStrings.h"
18 #include "guilib/WindowIDs.h"
19 #include "pvr/PVRItem.h"
20 #include "pvr/PVRManager.h"
21 #include "pvr/dialogs/GUIDialogPVRChannelGuide.h"
22 #include "pvr/dialogs/GUIDialogPVRGuideInfo.h"
23 #include "pvr/epg/EpgContainer.h"
24 #include "pvr/epg/EpgSearchFilter.h"
25 #include "pvr/guilib/PVRGUIActionsParentalControl.h"
26 #include "pvr/windows/GUIWindowPVRSearch.h"
27 #include "utils/Variant.h"
28 #include "utils/log.h"
30 #include <memory>
31 #include <string>
33 using namespace PVR;
35 namespace
37 PVR::CGUIWindowPVRSearchBase* GetSearchWindow(bool bRadio)
39 const int windowSearchId = bRadio ? WINDOW_RADIO_SEARCH : WINDOW_TV_SEARCH;
41 PVR::CGUIWindowPVRSearchBase* windowSearch;
43 CGUIWindowManager& windowMgr = CServiceBroker::GetGUI()->GetWindowManager();
44 if (bRadio)
45 windowSearch = windowMgr.GetWindow<PVR::CGUIWindowPVRRadioSearch>(windowSearchId);
46 else
47 windowSearch = windowMgr.GetWindow<PVR::CGUIWindowPVRTVSearch>(windowSearchId);
49 if (!windowSearch)
50 CLog::LogF(LOGERROR, "Unable to get {}!", bRadio ? "WINDOW_RADIO_SEARCH" : "WINDOW_TV_SEARCH");
52 return windowSearch;
54 } // unnamed namespace
56 bool CPVRGUIActionsEPG::ShowEPGInfo(const CFileItem& item) const
58 const std::shared_ptr<const CPVRChannel> channel(CPVRItem(item).GetChannel());
59 if (channel && CServiceBroker::GetPVRManager().Get<PVR::GUI::Parental>().CheckParentalLock(
60 channel) != ParentalCheckResult::SUCCESS)
61 return false;
63 const std::shared_ptr<CPVREpgInfoTag> epgTag(CPVRItem(item).GetEpgInfoTag());
64 if (!epgTag)
66 CLog::LogF(LOGERROR, "No epg tag!");
67 return false;
70 CGUIDialogPVRGuideInfo* pDlgInfo =
71 CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogPVRGuideInfo>(
72 WINDOW_DIALOG_PVR_GUIDE_INFO);
73 if (!pDlgInfo)
75 CLog::LogF(LOGERROR, "Unable to get WINDOW_DIALOG_PVR_GUIDE_INFO!");
76 return false;
79 pDlgInfo->SetProgInfo(std::make_shared<CFileItem>(epgTag));
80 pDlgInfo->Open();
81 return true;
84 bool CPVRGUIActionsEPG::ShowChannelEPG(const CFileItem& item) const
86 const std::shared_ptr<const CPVRChannel> channel(CPVRItem(item).GetChannel());
87 if (channel && CServiceBroker::GetPVRManager().Get<PVR::GUI::Parental>().CheckParentalLock(
88 channel) != ParentalCheckResult::SUCCESS)
89 return false;
91 CGUIDialogPVRChannelGuide* pDlgInfo =
92 CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogPVRChannelGuide>(
93 WINDOW_DIALOG_PVR_CHANNEL_GUIDE);
94 if (!pDlgInfo)
96 CLog::LogF(LOGERROR, "Unable to get WINDOW_DIALOG_PVR_CHANNEL_GUIDE!");
97 return false;
100 pDlgInfo->Open(channel);
101 return true;
104 bool CPVRGUIActionsEPG::FindSimilar(const CFileItem& item) const
106 CGUIWindowPVRSearchBase* windowSearch = GetSearchWindow(CPVRItem(item).IsRadio());
107 if (!windowSearch)
108 return false;
110 //! @todo If we want dialogs to spawn program search in a clean way - without having to force-close any
111 // other dialogs - we must introduce a search dialog with functionality similar to the search window.
113 for (int iId = CServiceBroker::GetGUI()->GetWindowManager().GetTopmostModalDialog(
114 true /* ignoreClosing */);
115 iId != WINDOW_INVALID;
116 iId = CServiceBroker::GetGUI()->GetWindowManager().GetTopmostModalDialog(
117 true /* ignoreClosing */))
119 CLog::LogF(LOGWARNING,
120 "Have to close modal dialog with id {} before search window can be opened.", iId);
122 CGUIWindow* window = CServiceBroker::GetGUI()->GetWindowManager().GetWindow(iId);
123 if (window)
125 window->Close();
127 else
129 CLog::LogF(LOGERROR, "Unable to get window instance {}! Cannot open search window.", iId);
130 return false; // return, otherwise we run into an endless loop
134 windowSearch->SetItemToSearch(item);
135 CServiceBroker::GetGUI()->GetWindowManager().ActivateWindow(windowSearch->GetID());
136 return true;
139 bool CPVRGUIActionsEPG::ExecuteSavedSearch(const CFileItem& item)
141 const auto searchFilter = item.GetEPGSearchFilter();
143 if (!searchFilter)
145 CLog::LogF(LOGERROR, "Wrong item type. No EPG search filter present.");
146 return false;
149 CGUIWindowPVRSearchBase* windowSearch = GetSearchWindow(searchFilter->IsRadio());
150 if (!windowSearch)
151 return false;
153 windowSearch->SetItemToSearch(item);
154 CServiceBroker::GetGUI()->GetWindowManager().ActivateWindow(windowSearch->GetID());
155 return true;
158 bool CPVRGUIActionsEPG::EditSavedSearch(const CFileItem& item)
160 const auto searchFilter = item.GetEPGSearchFilter();
162 if (!searchFilter)
164 CLog::LogF(LOGERROR, "Wrong item type. No EPG search filter present.");
165 return false;
168 CGUIWindowPVRSearchBase* windowSearch = GetSearchWindow(searchFilter->IsRadio());
169 if (!windowSearch)
170 return false;
172 if (windowSearch->OpenDialogSearch(item) == CGUIDialogPVRGuideSearch::Result::SEARCH)
173 CServiceBroker::GetGUI()->GetWindowManager().ActivateWindow(windowSearch->GetID());
175 return true;
178 bool CPVRGUIActionsEPG::RenameSavedSearch(const CFileItem& item)
180 const auto searchFilter = item.GetEPGSearchFilter();
182 if (!searchFilter)
184 CLog::LogF(LOGERROR, "Wrong item type. No EPG search filter present.");
185 return false;
188 std::string title = searchFilter->GetTitle();
189 if (CGUIKeyboardFactory::ShowAndGetInput(title,
190 CVariant{g_localizeStrings.Get(528)}, // "Enter title"
191 false))
193 searchFilter->SetTitle(title);
194 CServiceBroker::GetPVRManager().EpgContainer().PersistSavedSearch(*searchFilter);
195 return true;
197 return false;
200 bool CPVRGUIActionsEPG::DeleteSavedSearch(const CFileItem& item)
202 const auto searchFilter = item.GetEPGSearchFilter();
204 if (!searchFilter)
206 CLog::LogF(LOGERROR, "Wrong item type. No EPG search filter present.");
207 return false;
210 if (CGUIDialogYesNo::ShowAndGetInput(CVariant{122}, // "Confirm delete"
211 CVariant{19338}, // "Delete this saved search?"
212 CVariant{""}, CVariant{item.GetLabel()}))
214 return CServiceBroker::GetPVRManager().EpgContainer().DeleteSavedSearch(*searchFilter);
216 return false;