fd44a3608261167cf773e7ca1455be59cac0839d
[vuplus_xbmc] / xbmc / pvr / dialogs / GUIDialogPVRGroupManager.cpp
1 /*
2  *      Copyright (C) 2012-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "GUIDialogPVRGroupManager.h"
22 #include "FileItem.h"
23 #include "guilib/GUIKeyboardFactory.h"
24 #include "dialogs/GUIDialogOK.h"
25 #include "dialogs/GUIDialogYesNo.h"
26 #include "guilib/GUIWindowManager.h"
27 #include "guilib/Key.h"
28 #include "guilib/LocalizeStrings.h"
29
30 #include "pvr/PVRManager.h"
31 #include "pvr/channels/PVRChannelGroupsContainer.h"
32
33 using namespace std;
34 using namespace PVR;
35
36 #define CONTROL_LIST_CHANNELS_LEFT    11
37 #define CONTROL_LIST_CHANNELS_RIGHT   12
38 #define CONTROL_LIST_CHANNEL_GROUPS   13
39 #define CONTROL_CURRENT_GROUP_LABEL   20
40 #define CONTROL_UNGROUPED_LABEL       21
41 #define CONTROL_IN_GROUP_LABEL        22
42 #define BUTTON_NEWGROUP               26
43 #define BUTTON_RENAMEGROUP            27
44 #define BUTTON_DELGROUP               28
45 #define BUTTON_OK                     29
46
47 CGUIDialogPVRGroupManager::CGUIDialogPVRGroupManager() :
48     CGUIDialog(WINDOW_DIALOG_PVR_GROUP_MANAGER, "DialogPVRGroupManager.xml")
49 {
50   m_ungroupedChannels = new CFileItemList;
51   m_groupMembers      = new CFileItemList;
52   m_channelGroups     = new CFileItemList;
53 }
54
55 CGUIDialogPVRGroupManager::~CGUIDialogPVRGroupManager()
56 {
57   delete m_ungroupedChannels;
58   delete m_groupMembers;
59   delete m_channelGroups;
60 }
61
62 bool CGUIDialogPVRGroupManager::PersistChanges(void)
63 {
64   return g_PVRChannelGroups->Get(m_bIsRadio)->PersistAll();
65 }
66
67 bool CGUIDialogPVRGroupManager::CancelChanges(void)
68 {
69   // TODO
70   return false;
71 }
72
73 bool CGUIDialogPVRGroupManager::ActionButtonOk(CGUIMessage &message)
74 {
75   bool bReturn = false;
76   unsigned int iControl = message.GetSenderId();
77
78   if (iControl == BUTTON_OK)
79   {
80     PersistChanges();
81     Close();
82     bReturn = true;
83   }
84
85   return bReturn;
86 }
87
88 bool CGUIDialogPVRGroupManager::ActionButtonNewGroup(CGUIMessage &message)
89 {
90   bool bReturn = false;
91   unsigned int iControl = message.GetSenderId();
92
93   if (iControl == BUTTON_NEWGROUP)
94   {
95     CStdString strGroupName = "";
96     /* prompt for a group name */
97     if (CGUIKeyboardFactory::ShowAndGetInput(strGroupName, g_localizeStrings.Get(19139), false))
98     {
99       if (strGroupName != "")
100       {
101         /* add the group if it doesn't already exist */
102         CPVRChannelGroups *groups = ((CPVRChannelGroups *) g_PVRChannelGroups->Get(m_bIsRadio));
103         if (groups->AddGroup(strGroupName))
104         {
105           g_PVRChannelGroups->Get(m_bIsRadio)->GetByName(strGroupName)->SetGroupType(PVR_GROUP_TYPE_USER_DEFINED);
106           m_iSelectedChannelGroup = groups->Size() - 1;
107           Update();
108         }
109       }
110     }
111     bReturn = true;
112   }
113
114   return bReturn;
115 }
116
117 bool CGUIDialogPVRGroupManager::ActionButtonDeleteGroup(CGUIMessage &message)
118 {
119   bool bReturn = false;
120   unsigned int iControl = message.GetSenderId();
121
122   if (iControl == BUTTON_DELGROUP)
123   {
124     if (!m_selectedGroup)
125       return bReturn;
126
127     CGUIDialogYesNo* pDialog = (CGUIDialogYesNo*)g_windowManager.GetWindow(WINDOW_DIALOG_YES_NO);
128     if (!pDialog)
129       return bReturn;
130
131     pDialog->SetHeading(117);
132     pDialog->SetLine(0, "");
133     pDialog->SetLine(1, m_selectedGroup->GroupName());
134     pDialog->SetLine(2, "");
135     pDialog->DoModal();
136
137     if (pDialog->IsConfirmed())
138     {
139       if (((CPVRChannelGroups *) g_PVRChannelGroups->Get(m_bIsRadio))->DeleteGroup(*m_selectedGroup))
140         Update();
141     }
142
143     bReturn = true;
144   }
145
146   return bReturn;
147 }
148
149 bool CGUIDialogPVRGroupManager::ActionButtonRenameGroup(CGUIMessage &message)
150 {
151   bool bReturn = false;
152   unsigned int iControl = message.GetSenderId();
153
154   if (iControl == BUTTON_RENAMEGROUP)
155   {
156     if (!m_selectedGroup)
157       return bReturn;
158
159     CStdString strGroupName(m_selectedGroup->GroupName());
160     if (CGUIKeyboardFactory::ShowAndGetInput(strGroupName, g_localizeStrings.Get(19139), false))
161     {
162       if (strGroupName != "")
163       {
164         m_selectedGroup->SetGroupName(strGroupName, true);
165         Update();
166       }
167     }
168
169     bReturn = true;
170   }
171
172   return bReturn;
173 }
174
175 bool CGUIDialogPVRGroupManager::ActionButtonUngroupedChannels(CGUIMessage &message)
176 {
177   bool bReturn = false;
178   unsigned int iControl = message.GetSenderId();
179
180   if (m_viewUngroupedChannels.HasControl(iControl))   // list/thumb control
181   {
182     m_iSelectedUngroupedChannel = m_viewUngroupedChannels.GetSelectedItem();
183     int iAction     = message.GetParam1();
184
185     if (iAction == ACTION_SELECT_ITEM || iAction == ACTION_MOUSE_LEFT_CLICK)
186     {
187       if (m_channelGroups->GetFolderCount() == 0)
188       {
189         CGUIDialogOK::ShowAndGetInput(19033,19137,0,19138);
190       }
191       else if (m_ungroupedChannels->GetFileCount() > 0)
192       {
193         CFileItemPtr pItemChannel = m_ungroupedChannels->Get(m_iSelectedUngroupedChannel);
194         if (m_selectedGroup->AddToGroup(*pItemChannel->GetPVRChannelInfoTag()))
195           Update();
196       }
197     }
198     bReturn = true;
199   }
200
201   return bReturn;
202 }
203
204 bool CGUIDialogPVRGroupManager::ActionButtonGroupMembers(CGUIMessage &message)
205 {
206   bool bReturn = false;
207   unsigned int iControl = message.GetSenderId();
208
209   if (m_viewGroupMembers.HasControl(iControl))   // list/thumb control
210   {
211     m_iSelectedGroupMember = m_viewGroupMembers.GetSelectedItem();
212     int iAction      = message.GetParam1();
213
214     if (iAction == ACTION_SELECT_ITEM || iAction == ACTION_MOUSE_LEFT_CLICK)
215     {
216       if (m_selectedGroup && m_groupMembers->GetFileCount() > 0)
217       {
218         CFileItemPtr pItemChannel = m_groupMembers->Get(m_iSelectedGroupMember);
219         m_selectedGroup->RemoveFromGroup(*pItemChannel->GetPVRChannelInfoTag());
220         Update();
221       }
222     }
223     bReturn = true;
224   }
225
226   return bReturn;
227 }
228
229 bool CGUIDialogPVRGroupManager::ActionButtonChannelGroups(CGUIMessage &message)
230 {
231   bool bReturn = false;
232   unsigned int iControl = message.GetSenderId();
233
234   if (m_viewChannelGroups.HasControl(iControl))   // list/thumb control
235   {
236     int iAction = message.GetParam1();
237
238     if (iAction == ACTION_SELECT_ITEM || iAction == ACTION_MOUSE_LEFT_CLICK)
239     {
240       m_iSelectedChannelGroup = m_viewChannelGroups.GetSelectedItem();
241       Update();
242     }
243     bReturn = true;
244   }
245
246   return bReturn;
247 }
248
249 bool CGUIDialogPVRGroupManager::OnMessageClick(CGUIMessage &message)
250 {
251   return ActionButtonOk(message) ||
252       ActionButtonNewGroup(message) ||
253       ActionButtonDeleteGroup(message) ||
254       ActionButtonRenameGroup(message) ||
255       ActionButtonUngroupedChannels(message) ||
256       ActionButtonGroupMembers(message) ||
257       ActionButtonChannelGroups(message);
258 }
259
260 bool CGUIDialogPVRGroupManager::OnMessage(CGUIMessage& message)
261 {
262   unsigned int iMessage = message.GetMessage();
263
264   switch (iMessage)
265   {
266     case GUI_MSG_CLICKED:
267     {
268       OnMessageClick(message);
269     }
270     break;
271   }
272
273   return CGUIDialog::OnMessage(message);
274 }
275
276 void CGUIDialogPVRGroupManager::OnInitWindow()
277 {
278   CGUIDialog::OnInitWindow();
279   m_iSelectedUngroupedChannel  = 0;
280   m_iSelectedGroupMember = 0;
281   m_iSelectedChannelGroup = 0;
282   Update();
283 }
284
285 void CGUIDialogPVRGroupManager::OnDeinitWindow(int nextWindowID)
286 {
287   Clear();
288   CGUIDialog::OnDeinitWindow(nextWindowID);
289 }
290
291 void CGUIDialogPVRGroupManager::OnWindowLoaded()
292 {
293   CGUIDialog::OnWindowLoaded();
294
295   m_viewUngroupedChannels.Reset();
296   m_viewUngroupedChannels.SetParentWindow(GetID());
297   m_viewUngroupedChannels.AddView(GetControl(CONTROL_LIST_CHANNELS_LEFT));
298
299   m_viewGroupMembers.Reset();
300   m_viewGroupMembers.SetParentWindow(GetID());
301   m_viewGroupMembers.AddView(GetControl(CONTROL_LIST_CHANNELS_RIGHT));
302
303   m_viewChannelGroups.Reset();
304   m_viewChannelGroups.SetParentWindow(GetID());
305   m_viewChannelGroups.AddView(GetControl(CONTROL_LIST_CHANNEL_GROUPS));
306 }
307
308 void CGUIDialogPVRGroupManager::OnWindowUnload()
309 {
310   CGUIDialog::OnWindowUnload();
311   m_viewUngroupedChannels.Reset();
312   m_viewGroupMembers.Reset();
313   m_viewChannelGroups.Reset();
314 }
315
316 void CGUIDialogPVRGroupManager::Update()
317 {
318   /* lock our display, as this window is rendered from the player thread */
319   g_graphicsContext.Lock();
320   m_viewUngroupedChannels.SetCurrentView(CONTROL_LIST_CHANNELS_LEFT);
321   m_viewGroupMembers.SetCurrentView(CONTROL_LIST_CHANNELS_RIGHT);
322   m_viewChannelGroups.SetCurrentView(CONTROL_LIST_CHANNEL_GROUPS);
323
324   Clear();
325
326   /* get the groups list */
327   g_PVRChannelGroups->Get(m_bIsRadio)->GetGroupList(m_channelGroups);
328   m_viewChannelGroups.SetItems(*m_channelGroups);
329   m_viewChannelGroups.SetSelectedItem(m_iSelectedChannelGroup);
330
331   /* select a group or select the default group if no group was selected */
332   CFileItemPtr pItem = m_channelGroups->Get(m_viewChannelGroups.GetSelectedItem());
333   m_selectedGroup = g_PVRChannelGroups->Get(m_bIsRadio)->GetByName(pItem->m_strTitle);
334   if (m_selectedGroup)
335   {
336     /* set this group in the pvrmanager, so it becomes the selected group in other dialogs too */
337     g_PVRManager.SetPlayingGroup(m_selectedGroup);
338     SET_CONTROL_LABEL(CONTROL_CURRENT_GROUP_LABEL, m_selectedGroup->GroupName());
339
340     if (m_selectedGroup->IsInternalGroup())
341     {
342       CStdString strNewLabel;
343       strNewLabel.Format("%s %s", g_localizeStrings.Get(19022), m_bIsRadio ? g_localizeStrings.Get(19024) : g_localizeStrings.Get(19023));
344       SET_CONTROL_LABEL(CONTROL_UNGROUPED_LABEL, strNewLabel);
345
346       strNewLabel.Format("%s %s", g_localizeStrings.Get(19218), m_bIsRadio ? g_localizeStrings.Get(19024) : g_localizeStrings.Get(19023));
347       SET_CONTROL_LABEL(CONTROL_IN_GROUP_LABEL, strNewLabel);
348     }
349     else
350     {
351       CStdString strNewLabel;
352       strNewLabel.Format("%s", g_localizeStrings.Get(19219));
353       SET_CONTROL_LABEL(CONTROL_UNGROUPED_LABEL, strNewLabel);
354
355       strNewLabel.Format("%s %s", g_localizeStrings.Get(19220), m_selectedGroup->GroupName());
356       SET_CONTROL_LABEL(CONTROL_IN_GROUP_LABEL, strNewLabel);
357     }
358
359     /* get all channels that are not in this group for the center part */
360     m_selectedGroup->GetMembers(*m_ungroupedChannels, false);
361     m_viewUngroupedChannels.SetItems(*m_ungroupedChannels);
362     m_viewUngroupedChannels.SetSelectedItem(m_iSelectedUngroupedChannel);
363
364     /* get all channels in this group for the right side part */
365     m_selectedGroup->GetMembers(*m_groupMembers, true);
366     m_viewGroupMembers.SetItems(*m_groupMembers);
367     m_viewGroupMembers.SetSelectedItem(m_iSelectedGroupMember);
368   }
369
370   g_graphicsContext.Unlock();
371 }
372
373 void CGUIDialogPVRGroupManager::Clear()
374 {
375   m_viewUngroupedChannels.Clear();
376   m_viewGroupMembers.Clear();
377   m_viewChannelGroups.Clear();
378
379   m_ungroupedChannels->Clear();
380   m_groupMembers->Clear();
381   m_channelGroups->Clear();
382 }