1 /*
2  *  Copyright (C) 2012-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #include "GUIWindowPVRTimersBase.h"
10 
11 #include "FileItem.h"
12 #include "GUIInfoManager.h"
13 #include "ServiceBroker.h"
14 #include "guilib/GUIComponent.h"
15 #include "guilib/GUIMessage.h"
16 #include "input/actions/Action.h"
17 #include "input/actions/ActionIDs.h"
18 #include "pvr/PVRManager.h"
19 #include "pvr/guilib/PVRGUIActions.h"
20 #include "pvr/timers/PVRTimerInfoTag.h"
21 #include "pvr/timers/PVRTimersPath.h"
22 #include "settings/Settings.h"
23 #include "settings/SettingsComponent.h"
24 #include "utils/URIUtils.h"
25 
26 #include <memory>
27 #include <string>
28 
29 using namespace PVR;
30 
CGUIWindowPVRTimersBase(bool bRadio,int id,const std::string & xmlFile)31 CGUIWindowPVRTimersBase::CGUIWindowPVRTimersBase(bool bRadio, int id, const std::string& xmlFile) :
32   CGUIWindowPVRBase(bRadio, id, xmlFile)
33 {
34 }
35 
36 CGUIWindowPVRTimersBase::~CGUIWindowPVRTimersBase() = default;
37 
OnAction(const CAction & action)38 bool CGUIWindowPVRTimersBase::OnAction(const CAction& action)
39 {
40   if (action.GetID() == ACTION_PARENT_DIR ||
41       action.GetID() == ACTION_NAV_BACK)
42   {
43     CPVRTimersPath path(m_vecItems->GetPath());
44     if (path.IsValid() && path.IsTimerRule())
45     {
46       m_currentFileItem.reset();
47       GoParentFolder();
48       return true;
49     }
50   }
51   return CGUIWindowPVRBase::OnAction(action);
52 }
53 
Update(const std::string & strDirectory,bool updateFilterPath)54 bool CGUIWindowPVRTimersBase::Update(const std::string& strDirectory, bool updateFilterPath /* = true */)
55 {
56   int iOldCount = m_vecItems->GetObjectCount();
57   const std::string oldPath = m_vecItems->GetPath();
58 
59   bool bReturn = CGUIWindowPVRBase::Update(strDirectory);
60 
61   if (bReturn && iOldCount > 0 && m_vecItems->GetObjectCount() == 0 && oldPath == m_vecItems->GetPath())
62   {
63     /* go to the parent folder if we're in a subdirectory and for instance just deleted the last item */
64     const CPVRTimersPath path(m_vecItems->GetPath());
65     if (path.IsValid() && path.IsTimerRule())
66     {
67       m_currentFileItem.reset();
68       GoParentFolder();
69     }
70   }
71 
72   return bReturn;
73 }
74 
UpdateButtons()75 void CGUIWindowPVRTimersBase::UpdateButtons()
76 {
77   SET_CONTROL_SELECTED(GetID(), CONTROL_BTNHIDEDISABLEDTIMERS, CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_PVRTIMERS_HIDEDISABLEDTIMERS));
78 
79   CGUIWindowPVRBase::UpdateButtons();
80 
81   std::string strHeaderTitle;
82   if (m_currentFileItem && m_currentFileItem->HasPVRTimerInfoTag())
83   {
84     std::shared_ptr<CPVRTimerInfoTag> timer = m_currentFileItem->GetPVRTimerInfoTag();
85     strHeaderTitle = timer->Title();
86   }
87 
88   SET_CONTROL_LABEL(CONTROL_LABEL_HEADER1, strHeaderTitle);
89 }
90 
OnMessage(CGUIMessage & message)91 bool CGUIWindowPVRTimersBase::OnMessage(CGUIMessage& message)
92 {
93   bool bReturn = false;
94   switch (message.GetMessage())
95   {
96     case GUI_MSG_CLICKED:
97       if (message.GetSenderId() == m_viewControl.GetCurrentControl())
98       {
99         int iItem = m_viewControl.GetSelectedItem();
100         if (iItem >= 0 && iItem < m_vecItems->Size())
101         {
102           bReturn = true;
103           switch (message.GetParam1())
104           {
105             case ACTION_SHOW_INFO:
106             case ACTION_SELECT_ITEM:
107             case ACTION_MOUSE_LEFT_CLICK:
108             {
109               CFileItemPtr item(m_vecItems->Get(iItem));
110               if (item->m_bIsFolder && (message.GetParam1() != ACTION_SHOW_INFO))
111               {
112                 m_currentFileItem = item;
113                 bReturn = false; // folders are handled by base class
114               }
115               else
116               {
117                 m_currentFileItem.reset();
118                 ActionShowTimer(item);
119               }
120               break;
121             }
122             case ACTION_CONTEXT_MENU:
123             case ACTION_MOUSE_RIGHT_CLICK:
124               OnPopupMenu(iItem);
125               break;
126             case ACTION_DELETE_ITEM:
127               CServiceBroker::GetPVRManager().GUIActions()->DeleteTimer(m_vecItems->Get(iItem));
128               break;
129             default:
130               bReturn = false;
131               break;
132           }
133         }
134       }
135       else if (message.GetSenderId() == CONTROL_BTNHIDEDISABLEDTIMERS)
136       {
137         const std::shared_ptr<CSettings> settings = CServiceBroker::GetSettingsComponent()->GetSettings();
138         settings->ToggleBool(CSettings::SETTING_PVRTIMERS_HIDEDISABLEDTIMERS);
139         settings->Save();
140         Update(GetDirectoryPath());
141         bReturn = true;
142       }
143       break;
144     case GUI_MSG_REFRESH_LIST:
145     {
146       switch (static_cast<PVREvent>(message.GetParam1()))
147       {
148         case PVREvent::CurrentItem:
149         case PVREvent::Epg:
150         case PVREvent::EpgActiveItem:
151         case PVREvent::EpgContainer:
152         case PVREvent::Timers:
153           SetInvalid();
154           break;
155 
156         case PVREvent::TimersInvalidated:
157           Refresh(true);
158           break;
159 
160         default:
161           break;
162       }
163       break;
164     }
165   }
166 
167   return bReturn || CGUIWindowPVRBase::OnMessage(message);
168 }
169 
ActionShowTimer(const CFileItemPtr & item)170 bool CGUIWindowPVRTimersBase::ActionShowTimer(const CFileItemPtr& item)
171 {
172   bool bReturn = false;
173 
174   /* Check if "Add timer..." entry is selected, if yes
175      create a new timer and open settings dialog, otherwise
176      open settings for selected timer entry */
177   if (URIUtils::PathEquals(item->GetPath(), CPVRTimersPath::PATH_ADDTIMER))
178     bReturn = CServiceBroker::GetPVRManager().GUIActions()->AddTimer(m_bRadio);
179   else
180     bReturn = CServiceBroker::GetPVRManager().GUIActions()->EditTimer(item);
181 
182   return bReturn;
183 }
184