bump to Gotham 13.1 beta2
[vuplus_xbmc] / xbmc / pvr / windows / GUIWindowPVRRecordings.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 "GUIWindowPVRRecordings.h"
22
23 #include "guilib/GUIKeyboardFactory.h"
24 #include "dialogs/GUIDialogYesNo.h"
25 #include "guilib/GUIWindowManager.h"
26 #include "guilib/Key.h"
27 #include "guilib/LocalizeStrings.h"
28 #include "GUIInfoManager.h"
29 #include "pvr/PVRManager.h"
30 #include "pvr/recordings/PVRRecordings.h"
31 #include "pvr/timers/PVRTimers.h"
32 #include "pvr/windows/GUIWindowPVR.h"
33 #include "utils/log.h"
34 #include "utils/StringUtils.h"
35 #include "threads/SingleLock.h"
36 #include "pvr/addons/PVRClients.h"
37 #include "video/windows/GUIWindowVideoNav.h"
38
39 using namespace PVR;
40
41 CGUIWindowPVRRecordings::CGUIWindowPVRRecordings(CGUIWindowPVR *parent) :
42   CGUIWindowPVRCommon(parent, PVR_WINDOW_RECORDINGS, CONTROL_BTNRECORDINGS, CONTROL_LIST_RECORDINGS)
43 {
44   m_strSelectedPath = "pvr://recordings/";
45 }
46
47 void CGUIWindowPVRRecordings::UnregisterObservers(void)
48 {
49   CSingleLock lock(m_critSection);
50   if(g_PVRRecordings)
51     g_PVRRecordings->UnregisterObserver(this);
52   if(g_PVRTimers)
53     g_PVRTimers->UnregisterObserver(this);
54   g_infoManager.UnregisterObserver(this);
55 }
56
57 void CGUIWindowPVRRecordings::ResetObservers(void)
58 {
59   CSingleLock lock(m_critSection);
60   g_PVRRecordings->RegisterObserver(this);
61   g_PVRTimers->RegisterObserver(this);
62   g_infoManager.RegisterObserver(this);
63 }
64
65 CStdString CGUIWindowPVRRecordings::GetResumeString(const CFileItem& item)
66 {
67   CStdString resumeString;
68   if (item.IsPVRRecording())
69   {
70
71     // First try to find the resume position on the back-end, if that fails use video database
72     int positionInSeconds = item.GetPVRRecordingInfoTag()->GetLastPlayedPosition();
73     // If the back-end does report a saved position then make sure there is a corresponding resume bookmark
74     if (positionInSeconds > 0)
75     {
76       CBookmark bookmark;
77       bookmark.timeInSeconds = positionInSeconds;
78       bookmark.totalTimeInSeconds = (double)item.GetPVRRecordingInfoTag()->GetDuration();
79       CVideoDatabase db;
80       if (db.Open())
81       {
82         CStdString itemPath(item.GetPVRRecordingInfoTag()->m_strFileNameAndPath);
83         db.AddBookMarkToFile(itemPath, bookmark, CBookmark::RESUME);
84         db.Close();
85       }
86     }
87     else if (positionInSeconds < 0)
88     {
89       CVideoDatabase db;
90       if (db.Open())
91       {
92         CBookmark bookmark;
93         CStdString itemPath(item.GetPVRRecordingInfoTag()->m_strFileNameAndPath);
94         if (db.GetResumeBookMark(itemPath, bookmark) )
95           positionInSeconds = lrint(bookmark.timeInSeconds);
96         db.Close();
97       }
98     }
99
100     // Suppress resume from 0
101     if (positionInSeconds > 0)
102       resumeString = StringUtils::Format(g_localizeStrings.Get(12022).c_str(), StringUtils::SecondsToTimeString(positionInSeconds).c_str());
103   }
104   return resumeString;
105 }
106
107 void CGUIWindowPVRRecordings::GetContextButtons(int itemNumber, CContextButtons &buttons) const
108 {
109   if (itemNumber < 0 || itemNumber >= m_parent->m_vecItems->Size())
110     return;
111   CFileItemPtr pItem = m_parent->m_vecItems->Get(itemNumber);
112
113   if (pItem->HasPVRRecordingInfoTag())
114   {
115     buttons.Add(CONTEXT_BUTTON_INFO, 19053);      /* Get Information of this recording */
116     buttons.Add(CONTEXT_BUTTON_FIND, 19003);      /* Find similar program */
117     buttons.Add(CONTEXT_BUTTON_PLAY_ITEM, 12021); /* Play this recording */
118     CStdString resumeString = GetResumeString(*pItem);
119     if (!resumeString.empty())
120     {
121       buttons.Add(CONTEXT_BUTTON_RESUME_ITEM, resumeString);
122     }
123   }
124   if (pItem->m_bIsFolder)
125   {
126     // Have both options for folders since we don't know whether all childs are watched/unwatched
127     buttons.Add(CONTEXT_BUTTON_MARK_UNWATCHED, 16104); /* Mark as UnWatched */
128     buttons.Add(CONTEXT_BUTTON_MARK_WATCHED, 16103);   /* Mark as Watched */
129   }
130   if (pItem->HasPVRRecordingInfoTag())
131   {
132     if (pItem->GetPVRRecordingInfoTag()->m_playCount > 0)
133       buttons.Add(CONTEXT_BUTTON_MARK_UNWATCHED, 16104); /* Mark as UnWatched */
134     else
135       buttons.Add(CONTEXT_BUTTON_MARK_WATCHED, 16103);   /* Mark as Watched */
136
137     buttons.Add(CONTEXT_BUTTON_RENAME, 118);      /* Rename this recording */
138     buttons.Add(CONTEXT_BUTTON_DELETE, 117);      /* Delete this recording */
139   }
140   buttons.Add(CONTEXT_BUTTON_SORTBY_NAME, 103);       /* sort by name */
141   buttons.Add(CONTEXT_BUTTON_SORTBY_DATE, 104);       /* sort by date */
142
143   if (pItem->HasPVRRecordingInfoTag() &&
144       g_PVRClients->HasMenuHooks(pItem->GetPVRRecordingInfoTag()->m_iClientId, PVR_MENUHOOK_RECORDING))
145     buttons.Add(CONTEXT_BUTTON_MENU_HOOKS, 19195);      /* PVR client specific action */
146
147   // Update sort by button
148 //if (m_guiState->GetSortMethod()!=SortByNone)
149 //{
150 //  CStdString sortLabel;
151 //  sortLabel.Format(g_localizeStrings.Get(550).c_str(), g_localizeStrings.Get(m_guiState->GetSortMethodLabel()).c_str());
152 //  buttons.Add(CONTEXT_BUTTON_SORTBY, sortLabel);   /* Sort method */
153 //
154 //  if (m_guiState->GetDisplaySortOrder()==SortOrderAscending)
155 //    buttons.Add(CONTEXT_BUTTON_SORTASC, 584);        /* Sort up or down */
156 //  else
157 //    buttons.Add(CONTEXT_BUTTON_SORTASC, 585);        /* Sort up or down */
158 //}
159 }
160
161 bool CGUIWindowPVRRecordings::OnAction(const CAction &action)
162 {
163   if (action.GetID() == ACTION_PARENT_DIR ||
164       action.GetID() == ACTION_NAV_BACK)
165   {
166     if (m_parent->m_vecItems->GetPath() != "pvr://recordings/")
167     {
168       m_parent->GoParentFolder();
169       return true;
170     }
171   }
172   return CGUIWindowPVRCommon::OnAction(action);
173 }
174
175 bool CGUIWindowPVRRecordings::OnContextButton(int itemNumber, CONTEXT_BUTTON button)
176 {
177   if (itemNumber < 0 || itemNumber >= m_parent->m_vecItems->Size())
178     return false;
179   CFileItemPtr pItem = m_parent->m_vecItems->Get(itemNumber);
180
181   return OnContextButtonPlay(pItem.get(), button) ||
182       OnContextButtonRename(pItem.get(), button) ||
183       OnContextButtonDelete(pItem.get(), button) ||
184       OnContextButtonInfo(pItem.get(), button) ||
185       OnContextButtonMarkWatched(pItem, button) ||
186       CGUIWindowPVRCommon::OnContextButton(itemNumber, button);
187 }
188
189 void CGUIWindowPVRRecordings::OnWindowUnload(void)
190 {
191   m_strSelectedPath = m_parent->m_vecItems->GetPath();
192   CGUIWindowPVRCommon::OnWindowUnload();
193 }
194
195 void CGUIWindowPVRRecordings::UpdateData(bool bUpdateSelectedFile /* = true */)
196 {
197   CSingleLock lock(m_critSection);
198   CLog::Log(LOGDEBUG, "CGUIWindowPVRRecordings - %s - update window '%s'. set view to %d", __FUNCTION__, GetName(), m_iControlList);
199   m_bUpdateRequired = false;
200
201   /* lock the graphics context while updating */
202   CSingleLock graphicsLock(g_graphicsContext);
203
204   m_iSelected = m_parent->m_viewControl.GetSelectedItem();
205   if (!StringUtils::StartsWith(m_parent->m_vecItems->GetPath(), "pvr://recordings/"))
206     m_strSelectedPath = "pvr://recordings/";
207   else
208     m_strSelectedPath = m_parent->m_vecItems->GetPath();
209
210   m_parent->m_viewControl.SetCurrentView(m_iControlList);
211   ShowBusyItem();
212   m_parent->m_vecItems->Clear();
213   m_parent->m_vecItems->SetPath(m_strSelectedPath);
214   m_parent->Update(m_strSelectedPath);
215   m_parent->m_viewControl.SetItems(*m_parent->m_vecItems);
216
217   if (bUpdateSelectedFile)
218   {
219     if (!SelectPlayingFile())
220       m_parent->m_viewControl.SetSelectedItem(m_iSelected);
221   }
222
223   m_parent->SetLabel(CONTROL_LABELHEADER, g_localizeStrings.Get(19017));
224   m_parent->SetLabel(CONTROL_LABELGROUP, "");
225 }
226
227 void CGUIWindowPVRRecordings::Notify(const Observable &obs, const ObservableMessage msg)
228 {
229   if (msg == ObservableMessageTimers || msg == ObservableMessageCurrentItem)
230   {
231     if (IsVisible())
232       SetInvalid();
233     else
234       m_bUpdateRequired = true;
235   }
236   else if (msg == ObservableMessageRecordings || msg == ObservableMessageTimersReset)
237   {
238     if (IsVisible())
239       UpdateData();
240     else
241       m_bUpdateRequired = true;
242   }
243 }
244
245 bool CGUIWindowPVRRecordings::OnClickButton(CGUIMessage &message)
246 {
247   bool bReturn = false;
248
249   if (IsSelectedButton(message))
250   {
251     bReturn = true;
252     g_PVRManager.TriggerRecordingsUpdate();
253   }
254
255   return bReturn;
256 }
257
258 bool CGUIWindowPVRRecordings::OnClickList(CGUIMessage &message)
259 {
260   bool bReturn = false;
261
262   if (IsSelectedList(message))
263   {
264     bReturn = true;
265     int iAction = message.GetParam1();
266     int iItem = m_parent->m_viewControl.GetSelectedItem();
267
268     /* get the fileitem pointer */
269     if (iItem < 0 || iItem >= (int) m_parent->m_vecItems->Size())
270       return bReturn;
271     CFileItemPtr pItem = m_parent->m_vecItems->Get(iItem);
272
273     /* process actions */
274     if (iAction == ACTION_SELECT_ITEM || iAction == ACTION_MOUSE_LEFT_CLICK || iAction == ACTION_PLAY)
275     {
276       int choice = CONTEXT_BUTTON_PLAY_ITEM;
277       CStdString resumeString = GetResumeString(*pItem);
278       if (!resumeString.empty())
279       {
280         CContextButtons choices;
281         choices.Add(CONTEXT_BUTTON_RESUME_ITEM, resumeString);
282         choices.Add(CONTEXT_BUTTON_PLAY_ITEM, 12021);
283         choice = CGUIDialogContextMenu::ShowAndGetChoice(choices);
284       }
285       if (choice < 0)
286         bReturn = true;
287       else
288         bReturn = OnContextButtonPlay(pItem.get(), (CONTEXT_BUTTON)choice);
289     }
290     else if (iAction == ACTION_CONTEXT_MENU || iAction == ACTION_MOUSE_RIGHT_CLICK)
291       m_parent->OnPopupMenu(iItem);
292     else if (iAction == ACTION_SHOW_INFO)
293       ShowRecordingInfo(pItem.get());
294     else if (iAction == ACTION_DELETE_ITEM)
295       bReturn = ActionDeleteRecording(pItem.get());
296     else
297       bReturn = false;
298   }
299
300   return bReturn;
301 }
302
303 bool CGUIWindowPVRRecordings::OnContextButtonDelete(CFileItem *item, CONTEXT_BUTTON button)
304 {
305   bool bReturn = false;
306
307   if (button == CONTEXT_BUTTON_DELETE)
308   {
309     bReturn = false;
310
311     CGUIDialogYesNo* pDialog = (CGUIDialogYesNo*)g_windowManager.GetWindow(WINDOW_DIALOG_YES_NO);
312     if (!pDialog)
313       return bReturn;
314     pDialog->SetHeading(122);
315     pDialog->SetLine(0, 19043);
316     pDialog->SetLine(1, "");
317     pDialog->SetLine(2, item->GetPVRRecordingInfoTag()->m_strTitle);
318     pDialog->DoModal();
319
320     if (!pDialog->IsConfirmed())
321       return bReturn;
322
323     bReturn = g_PVRRecordings->DeleteRecording(*item);
324   }
325
326   return bReturn;
327 }
328
329 bool CGUIWindowPVRRecordings::OnContextButtonInfo(CFileItem *item, CONTEXT_BUTTON button)
330 {
331   bool bReturn = false;
332
333   if (button == CONTEXT_BUTTON_INFO)
334   {
335     bReturn = true;
336     ShowRecordingInfo(item);
337   }
338
339   return bReturn;
340 }
341
342 bool CGUIWindowPVRRecordings::OnContextButtonPlay(CFileItem *item, CONTEXT_BUTTON button)
343 {
344   bool bReturn = false;
345
346   if ((button == CONTEXT_BUTTON_PLAY_ITEM) ||
347       (button == CONTEXT_BUTTON_RESUME_ITEM))
348   {
349     item->m_lStartOffset = button == CONTEXT_BUTTON_RESUME_ITEM ? STARTOFFSET_RESUME : 0;
350     bReturn = PlayFile(item, false); /* play recording */
351   }
352
353   return bReturn;
354 }
355
356 bool CGUIWindowPVRRecordings::OnContextButtonRename(CFileItem *item, CONTEXT_BUTTON button)
357 {
358   bool bReturn = false;
359
360   if (button == CONTEXT_BUTTON_RENAME)
361   {
362     bReturn = true;
363
364     CPVRRecording *recording = item->GetPVRRecordingInfoTag();
365     CStdString strNewName = recording->m_strTitle;
366     if (CGUIKeyboardFactory::ShowAndGetInput(strNewName, g_localizeStrings.Get(19041), false))
367     {
368       if (g_PVRRecordings->RenameRecording(*item, strNewName))
369         UpdateData();
370     }
371   }
372
373   return bReturn;
374 }
375
376 bool CGUIWindowPVRRecordings::OnContextButtonMarkWatched(const CFileItemPtr &item, CONTEXT_BUTTON button)
377 {
378   bool bReturn = false;
379
380   if (button == CONTEXT_BUTTON_MARK_WATCHED)
381   {
382     bReturn = true;
383
384     int newSelection = m_parent->m_viewControl.GetSelectedItem();
385     g_PVRRecordings->SetRecordingsPlayCount(item, 1);
386     m_parent->m_viewControl.SetSelectedItem(newSelection);
387
388     UpdateData();
389   }
390
391   if (button == CONTEXT_BUTTON_MARK_UNWATCHED)
392   {
393     bReturn = true;
394
395     g_PVRRecordings->SetRecordingsPlayCount(item, 0);
396
397     UpdateData();
398   }
399
400   return bReturn;
401 }
402
403 void CGUIWindowPVRRecordings::BeforeUpdate(const CStdString &strDirectory)
404 {
405   // set items path to current directory
406   m_parent->m_vecItems->SetPath(strDirectory);
407
408   if (m_thumbLoader.IsLoading())
409     m_thumbLoader.StopThread();
410 }
411
412 void CGUIWindowPVRRecordings::AfterUpdate(CFileItemList& items)
413 {
414   if (!items.IsEmpty())
415   {
416     CFileItemList files;
417     for (int i = 0; i < items.Size(); i++)
418     {
419       CFileItemPtr pItem = items[i];
420       if (!pItem->m_bIsFolder)
421         files.Add(pItem);
422     }
423
424     if (!files.IsEmpty())
425     {
426       files.SetPath(items.GetPath());
427       if(m_database.Open())
428       {
429         if (g_PVRRecordings->HasAllRecordingsPathExtension(files.GetPath()))
430         {
431           // Build a map of all files belonging to common subdirectories and call
432           // LoadVideoInfo for each item list
433           typedef boost::shared_ptr<CFileItemList> CFileItemListPtr;
434           typedef std::map<CStdString, CFileItemListPtr> DirectoryMap;
435
436           DirectoryMap directory_map;
437           for (int i = 0; i < files.Size(); i++)
438           {
439             CStdString strDirectory = URIUtils::GetDirectory(files[i]->GetPath());
440             DirectoryMap::iterator it = directory_map.find(strDirectory);
441             if (it == directory_map.end())
442               it = directory_map.insert(std::make_pair(
443                   strDirectory, CFileItemListPtr(new CFileItemList(strDirectory)))).first;
444             it->second->Add(files[i]);
445           }
446
447           for (DirectoryMap::iterator it = directory_map.begin(); it != directory_map.end(); it++)
448             CGUIWindowVideoNav::LoadVideoInfo(*it->second, m_database, false);
449         }
450         else
451           CGUIWindowVideoNav::LoadVideoInfo(files, m_database, false);
452         m_database.Close();
453       }
454       m_thumbLoader.Load(files);
455     }
456   }
457 }