summaryrefslogtreecommitdiffstats
path: root/xbmc/pvr/windows/GUIWindowPVRGuide.cpp
blob: f4c247f9106bd577aef51a41ea589a4f022c77c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
/*
 *  Copyright (C) 2012-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "GUIWindowPVRGuide.h"

#include "FileItem.h"
#include "GUIUserMessages.h"
#include "ServiceBroker.h"
#include "addons/Skin.h"
#include "dialogs/GUIDialogBusy.h"
#include "dialogs/GUIDialogContextMenu.h"
#include "dialogs/GUIDialogNumeric.h"
#include "guilib/GUIMessage.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/LocalizeStrings.h"
#include "input/actions/Action.h"
#include "input/actions/ActionIDs.h"
#include "messaging/ApplicationMessenger.h"
#include "messaging/helpers/DialogHelper.h"
#include "pvr/PVRItem.h"
#include "pvr/PVRManager.h"
#include "pvr/PVRPlaybackState.h"
#include "pvr/channels/PVRChannel.h"
#include "pvr/channels/PVRChannelGroup.h"
#include "pvr/channels/PVRChannelGroupMember.h"
#include "pvr/channels/PVRChannelGroupsContainer.h"
#include "pvr/channels/PVRChannelsPath.h"
#include "pvr/epg/EpgChannelData.h"
#include "pvr/epg/EpgContainer.h"
#include "pvr/epg/EpgInfoTag.h"
#include "pvr/guilib/GUIEPGGridContainer.h"
#include "pvr/guilib/PVRGUIActionsChannels.h"
#include "pvr/guilib/PVRGUIActionsEPG.h"
#include "pvr/guilib/PVRGUIActionsPlayback.h"
#include "pvr/guilib/PVRGUIActionsTimers.h"
#include "pvr/recordings/PVRRecordings.h"
#include "pvr/timers/PVRTimers.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include "view/GUIViewState.h"

#include <functional>
#include <memory>
#include <mutex>
#include <utility>
#include <vector>

using namespace KODI::MESSAGING;
using namespace PVR;
using namespace std::chrono_literals;

CGUIWindowPVRGuideBase::CGUIWindowPVRGuideBase(bool bRadio, int id, const std::string& xmlFile)
  : CGUIWindowPVRBase(bRadio, id, xmlFile)
{
  CServiceBroker::GetPVRManager().Get<PVR::GUI::Channels>().RegisterChannelNumberInputHandler(this);
}

CGUIWindowPVRGuideBase::~CGUIWindowPVRGuideBase()
{
  CServiceBroker::GetPVRManager().Get<PVR::GUI::Channels>().DeregisterChannelNumberInputHandler(
      this);

  m_bRefreshTimelineItems = false;
  m_bSyncRefreshTimelineItems = false;
  StopRefreshTimelineItemsThread();
}

CGUIEPGGridContainer* CGUIWindowPVRGuideBase::GetGridControl()
{
  return dynamic_cast<CGUIEPGGridContainer*>(GetControl(m_viewControl.GetCurrentControl()));
}

void CGUIWindowPVRGuideBase::InitEpgGridControl()
{
  CGUIEPGGridContainer* epgGridContainer = GetGridControl();
  if (epgGridContainer)
  {
    CPVRManager& mgr = CServiceBroker::GetPVRManager();

    const std::shared_ptr<CPVRChannel> channel = mgr.ChannelGroups()->GetByPath(
        mgr.Get<PVR::GUI::Channels>().GetSelectedChannelPath(m_bRadio));

    if (channel)
    {
      m_bChannelSelectionRestored = epgGridContainer->SetChannel(channel);
      epgGridContainer->JumpToDate(
          mgr.PlaybackState()->GetPlaybackTime(channel->ClientID(), channel->UniqueID()));
    }
    else
    {
      m_bChannelSelectionRestored = false;
      epgGridContainer->JumpToNow();
    }

    if (!epgGridContainer->HasData())
      m_bSyncRefreshTimelineItems = true; // force data update on first window open
  }

  StartRefreshTimelineItemsThread();
}

void CGUIWindowPVRGuideBase::ClearData()
{
  {
    std::unique_lock<CCriticalSection> lock(m_critSection);
    m_cachedChannelGroup.reset();
  }

  CGUIWindowPVRBase::ClearData();
}

void CGUIWindowPVRGuideBase::OnInitWindow()
{
  if (m_guiState)
    m_viewControl.SetCurrentView(m_guiState->GetViewAsControl(), false);

  if (InitChannelGroup()) // no channels -> lazy init
    InitEpgGridControl();

  CGUIWindowPVRBase::OnInitWindow();
}

void CGUIWindowPVRGuideBase::OnDeinitWindow(int nextWindowID)
{
  StopRefreshTimelineItemsThread();

  m_bChannelSelectionRestored = false;

  CGUIDialog* dialog =
      CServiceBroker::GetGUI()->GetWindowManager().GetDialog(WINDOW_DIALOG_PVR_GUIDE_CONTROLS);
  if (dialog && dialog->IsDialogRunning())
  {
    dialog->Close();
  }

  CGUIWindowPVRBase::OnDeinitWindow(nextWindowID);
}

void CGUIWindowPVRGuideBase::StartRefreshTimelineItemsThread()
{
  StopRefreshTimelineItemsThread();
  m_refreshTimelineItemsThread.reset(new CPVRRefreshTimelineItemsThread(this));
  m_refreshTimelineItemsThread->Create();
}

void CGUIWindowPVRGuideBase::StopRefreshTimelineItemsThread()
{
  if (m_refreshTimelineItemsThread)
    m_refreshTimelineItemsThread->Stop();
}

void CGUIWindowPVRGuideBase::NotifyEvent(const PVREvent& event)
{
  if (event == PVREvent::Epg || event == PVREvent::EpgContainer ||
      event == PVREvent::ChannelGroupInvalidated || event == PVREvent::ChannelGroup)
  {
    m_bRefreshTimelineItems = true;
    // no base class call => do async refresh
    return;
  }
  else if (event == PVREvent::ChannelPlaybackStopped)
  {
    if (m_guiState && m_guiState->GetSortMethod().sortBy == SortByLastPlayed)
    {
      // set dirty to force sync refresh
      m_bSyncRefreshTimelineItems = true;
    }
  }

  // do sync refresh if dirty
  CGUIWindowPVRBase::NotifyEvent(event);
}

void CGUIWindowPVRGuideBase::SetInvalid()
{
  CGUIEPGGridContainer* epgGridContainer = GetGridControl();
  if (epgGridContainer)
    epgGridContainer->SetInvalid();

  CGUIWindowPVRBase::SetInvalid();
}

void CGUIWindowPVRGuideBase::GetContextButtons(int itemNumber, CContextButtons& buttons)
{
  CGUIWindowPVRBase::GetContextButtons(itemNumber, buttons);

  buttons.Add(CONTEXT_BUTTON_NAVIGATE, 19326); // Navigate...
}

void CGUIWindowPVRGuideBase::UpdateSelectedItemPath()
{
  CGUIEPGGridContainer* epgGridContainer = GetGridControl();
  if (epgGridContainer)
  {
    const std::shared_ptr<CPVRChannelGroupMember> groupMember =
        epgGridContainer->GetSelectedChannelGroupMember();
    if (groupMember)
      CServiceBroker::GetPVRManager().Get<PVR::GUI::Channels>().SetSelectedChannelPath(
          m_bRadio, groupMember->Path());
  }
}

void CGUIWindowPVRGuideBase::UpdateButtons()
{
  CGUIWindowPVRBase::UpdateButtons();

  SET_CONTROL_LABEL(CONTROL_LABEL_HEADER1, g_localizeStrings.Get(19032));

  const std::shared_ptr<CPVRChannelGroup> group = GetChannelGroup();
  SET_CONTROL_LABEL(CONTROL_LABEL_HEADER2, group ? group->GroupName() : "");
}

bool CGUIWindowPVRGuideBase::Update(const std::string& strDirectory,
                                    bool updateFilterPath /* = true */)
{
  if (m_bUpdating)
  {
    // Prevent concurrent updates. Instead, let the timeline items refresh thread pick it up later.
    m_bRefreshTimelineItems = true;
    return true;
  }

  bool bReturn = CGUIWindowPVRBase::Update(strDirectory, updateFilterPath);

  if (bReturn && !m_bChannelSelectionRestored)
  {
    CGUIEPGGridContainer* epgGridContainer = GetGridControl();
    if (epgGridContainer)
      m_bChannelSelectionRestored = epgGridContainer->SetChannel(
          CServiceBroker::GetPVRManager().Get<PVR::GUI::Channels>().GetSelectedChannelPath(
              m_bRadio));
  }

  return bReturn;
}

bool CGUIWindowPVRGuideBase::GetDirectory(const std::string& strDirectory, CFileItemList& items)
{
  {
    std::unique_lock<CCriticalSection> lock(m_critSection);

    if (m_cachedChannelGroup && *m_cachedChannelGroup != *GetChannelGroup())
    {
      // channel group change and not very first open of this window. force immediate update.
      m_bSyncRefreshTimelineItems = true;
    }
  }

  if (m_bSyncRefreshTimelineItems)
    m_refreshTimelineItemsThread->DoRefresh(true);

  const CGUIEPGGridContainer* epgGridContainer = GetGridControl();
  if (epgGridContainer)
  {
    const std::unique_ptr<CFileItemList> newTimeline = GetGridControl()->GetCurrentTimeLineItems();
    items.RemoveDiscCache(GetID());
    items.Assign(*newTimeline, false);
  }

  return true;
}

void CGUIWindowPVRGuideBase::FormatAndSort(CFileItemList& items)
{
  // Speedup: Nothing to do here as sorting was already done in RefreshTimelineItems
  return;
}

CFileItemPtr CGUIWindowPVRGuideBase::GetCurrentListItem(int offset /*= 0*/)
{
  const CGUIEPGGridContainer* epgGridContainer = GetGridControl();
  if (epgGridContainer)
    return epgGridContainer->GetSelectedGridItem(offset);

  return {};
}

int CGUIWindowPVRGuideBase::GetCurrentListItemIndex(const std::shared_ptr<CFileItem>& item)
{
  return item ? item->GetProperty("TimelineIndex").asInteger() : -1;
}

bool CGUIWindowPVRGuideBase::ShouldNavigateToGridContainer(int iAction)
{
  CGUIEPGGridContainer* epgGridContainer = GetGridControl();
  CGUIControl* control = GetControl(CONTROL_LSTCHANNELGROUPS);
  if (epgGridContainer && control && GetFocusedControlID() == control->GetID())
  {
    int iNavigationId = control->GetAction(iAction).GetNavigation();
    if (iNavigationId > 0)
    {
      control = epgGridContainer;
      while (control !=
             this) // navigation target could be the grid control or one of its parent controls.
      {
        if (iNavigationId == control->GetID())
        {
          // channel group selector control's target for the action is the grid control
          return true;
        }
        control = control->GetParentControl();
      }
    }
  }
  return false;
}

bool CGUIWindowPVRGuideBase::OnAction(const CAction& action)
{
  switch (action.GetID())
  {
    case ACTION_MOVE_UP:
    case ACTION_MOVE_DOWN:
    case ACTION_MOVE_LEFT:
    case ACTION_MOVE_RIGHT:
    {
      // Check whether grid container is configured as channel group selector's navigation target for the given action.
      if (ShouldNavigateToGridContainer(action.GetID()))
      {
        CGUIEPGGridContainer* epgGridContainer = GetGridControl();
        if (epgGridContainer)
        {
          CGUIWindowPVRBase::OnAction(action);

          switch (action.GetID())
          {
            case ACTION_MOVE_UP:
              epgGridContainer->GoToBottom();
              return true;
            case ACTION_MOVE_DOWN:
              epgGridContainer->GoToTop();
              return true;
            case ACTION_MOVE_LEFT:
              epgGridContainer->GoToMostRight();
              return true;
            case ACTION_MOVE_RIGHT:
              epgGridContainer->GoToMostLeft();
              return true;
            default:
              break;
          }
        }
      }
      break;
    }
    case REMOTE_0:
      if (GetCurrentDigitCount() == 0)
      {
        // single zero input is handled by epg grid container
        break;
      }
      // fall-thru is intended
      [[fallthrough]];
    case REMOTE_1:
    case REMOTE_2:
    case REMOTE_3:
    case REMOTE_4:
    case REMOTE_5:
    case REMOTE_6:
    case REMOTE_7:
    case REMOTE_8:
    case REMOTE_9:
      AppendChannelNumberCharacter(static_cast<char>(action.GetID() - REMOTE_0) + '0');
      return true;

    case ACTION_CHANNEL_NUMBER_SEP:
      AppendChannelNumberCharacter(CPVRChannelNumber::SEPARATOR);
      return true;
  }

  return CGUIWindowPVRBase::OnAction(action);
}

void CGUIWindowPVRGuideBase::RefreshView(CGUIMessage& message, bool bInitGridControl)
{
  CGUIWindowPVRBase::OnMessage(message);

  // force grid data update
  m_bSyncRefreshTimelineItems = true;

  if (bInitGridControl)
    InitEpgGridControl();

  Refresh(true);
}

bool CGUIWindowPVRGuideBase::OnMessage(CGUIMessage& message)
{
  bool bReturn = false;
  switch (message.GetMessage())
  {
    case GUI_MSG_WINDOW_INIT:
    {
      const CPVRChannelsPath path(message.GetStringParam(0));
      if (path.IsValid() && path.IsChannelGroup())
      {
        // if a path to a channel group is given we must init
        // that group instead of last played/selected group
        m_channelGroupPath = message.GetStringParam(0);
      }
      break;
    }

    case GUI_MSG_ITEM_SELECTED:
      message.SetParam1(GetCurrentListItemIndex(GetCurrentListItem()));
      bReturn = true;
      break;

    case GUI_MSG_CLICKED:
    {
      if (message.GetSenderId() == m_viewControl.GetCurrentControl())
      {
        if (message.GetParam1() == ACTION_SELECT_ITEM ||
            message.GetParam1() == ACTION_MOUSE_LEFT_CLICK)
        {
          // If direct channel number input is active, select the entered channel.
          if (CServiceBroker::GetPVRManager()
                  .Get<PVR::GUI::Channels>()
                  .GetChannelNumberInputHandler()
                  .CheckInputAndExecuteAction())
          {
            bReturn = true;
            break;
          }
        }

        const std::shared_ptr<CFileItem> pItem = GetCurrentListItem();
        if (pItem)
        {
          switch (message.GetParam1())
          {
            case ACTION_SELECT_ITEM:
            case ACTION_MOUSE_LEFT_CLICK:
              switch (CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(
                  CSettings::SETTING_EPG_SELECTACTION))
              {
                case EPG_SELECT_ACTION_CONTEXT_MENU:
                  OnPopupMenu(GetCurrentListItemIndex(pItem));
                  bReturn = true;
                  break;
                case EPG_SELECT_ACTION_SWITCH:
                  CServiceBroker::GetPVRManager().Get<PVR::GUI::Playback>().SwitchToChannel(*pItem,
                                                                                            true);
                  bReturn = true;
                  break;
                case EPG_SELECT_ACTION_PLAY_RECORDING:
                  CServiceBroker::GetPVRManager().Get<PVR::GUI::Playback>().PlayRecording(*pItem,
                                                                                          true);
                  bReturn = true;
                  break;
                case EPG_SELECT_ACTION_INFO:
                  CServiceBroker::GetPVRManager().Get<PVR::GUI::EPG>().ShowEPGInfo(*pItem);
                  bReturn = true;
                  break;
                case EPG_SELECT_ACTION_RECORD:
                  CServiceBroker::GetPVRManager().Get<PVR::GUI::Timers>().ToggleTimer(*pItem);
                  bReturn = true;
                  break;
                case EPG_SELECT_ACTION_SMART_SELECT:
                {
                  const std::shared_ptr<CPVREpgInfoTag> tag(pItem->GetEPGInfoTag());
                  if (tag)
                  {
                    const CDateTime start(tag->StartAsUTC());
                    const CDateTime end(tag->EndAsUTC());
                    const CDateTime now(CDateTime::GetUTCDateTime());

                    if (start <= now && now <= end)
                    {
                      // current event
                      CServiceBroker::GetPVRManager().Get<PVR::GUI::Playback>().SwitchToChannel(
                          *pItem, true);
                    }
                    else if (now < start)
                    {
                      // future event
                      if (CServiceBroker::GetPVRManager().Timers()->GetTimerForEpgTag(tag))
                        CServiceBroker::GetPVRManager().Get<PVR::GUI::Timers>().EditTimer(*pItem);
                      else
                      {
                        bool bCanRecord = true;
                        const std::shared_ptr<CPVRChannel> channel = CPVRItem(pItem).GetChannel();
                        if (channel)
                          bCanRecord = channel->CanRecord();

                        const int iTextID =
                            bCanRecord
                                ? 19302 // "Do you want to record the selected programme or to switch to the current programme?"
                                : 19344; // "Do you want to set a reminder for the selected programme or to switch to the current programme?"
                        const int iNoButtonID = bCanRecord ? 264 // No => "Record"
                                                           : 826; // "Set reminder"

                        HELPERS::DialogResponse ret =
                            HELPERS::ShowYesNoDialogText(CVariant{19096}, // "Smart select"
                                                         CVariant{iTextID}, CVariant{iNoButtonID},
                                                         CVariant{19165}); // Yes => "Switch"
                        if (ret == HELPERS::DialogResponse::CHOICE_NO)
                          CServiceBroker::GetPVRManager().Get<PVR::GUI::Timers>().AddTimer(*pItem,
                                                                                           false);
                        else if (ret == HELPERS::DialogResponse::CHOICE_YES)
                          CServiceBroker::GetPVRManager().Get<PVR::GUI::Playback>().SwitchToChannel(
                              *pItem, true);
                      }
                    }
                    else
                    {
                      // past event
                      if (CServiceBroker::GetPVRManager().Recordings()->GetRecordingForEpgTag(tag))
                        CServiceBroker::GetPVRManager().Get<PVR::GUI::Playback>().PlayRecording(
                            *pItem, true);
                      else if (tag->IsPlayable())
                        CServiceBroker::GetPVRManager().Get<PVR::GUI::Playback>().PlayEpgTag(
                            *pItem);
                      else
                        CServiceBroker::GetPVRManager().Get<PVR::GUI::EPG>().ShowEPGInfo(*pItem);
                    }
                    bReturn = true;
                  }
                  break;
                }
              }
              break;
            case ACTION_SHOW_INFO:
              CServiceBroker::GetPVRManager().Get<PVR::GUI::EPG>().ShowEPGInfo(*pItem);
              bReturn = true;
              break;
            case ACTION_PLAYER_PLAY:
              CServiceBroker::GetPVRManager().Get<PVR::GUI::Playback>().SwitchToChannel(*pItem,
                                                                                        true);
              bReturn = true;
              break;
            case ACTION_RECORD:
              CServiceBroker::GetPVRManager().Get<PVR::GUI::Timers>().ToggleTimer(*pItem);
              bReturn = true;
              break;
            case ACTION_PVR_SHOW_TIMER_RULE:
              CServiceBroker::GetPVRManager().Get<PVR::GUI::Timers>().AddTimerRule(*pItem, true,
                                                                                   false);
              bReturn = true;
              break;
            case ACTION_CONTEXT_MENU:
            case ACTION_MOUSE_RIGHT_CLICK:
              OnPopupMenu(GetCurrentListItemIndex(pItem));
              bReturn = true;
              break;
          }
        }
      }
      else if (message.GetSenderId() == CONTROL_BTNVIEWASICONS ||
               message.GetSenderId() == CONTROL_BTNSORTBY ||
               message.GetSenderId() == CONTROL_BTNSORTASC)
      {
        RefreshView(message, false);
        bReturn = true;
      }
      break;
    }
    case GUI_MSG_CHANGE_SORT_DIRECTION:
    case GUI_MSG_CHANGE_SORT_METHOD:
    case GUI_MSG_CHANGE_VIEW_MODE:
    {
      RefreshView(message, message.GetMessage() == GUI_MSG_CHANGE_VIEW_MODE);
      bReturn = true;
      break;
    }
    case GUI_MSG_REFRESH_LIST:
    {
      switch (static_cast<PVREvent>(message.GetParam1()))
      {
        case PVREvent::ManagerStarted:
          if (InitChannelGroup())
            InitEpgGridControl();
          break;

        case PVREvent::ChannelGroup:
        case PVREvent::ChannelGroupInvalidated:
        case PVREvent::ClientsInvalidated:
        case PVREvent::ChannelPlaybackStopped:
        case PVREvent::Epg:
        case PVREvent::EpgContainer:
          if (InitChannelGroup())
            Refresh(true);
          break;

        case PVREvent::Timers:
        case PVREvent::TimersInvalidated:
          SetInvalid();
          break;

        default:
          break;
      }
      break;
    }
    case GUI_MSG_SYSTEM_WAKE:
      GotoCurrentProgramme();
      bReturn = true;
      break;
  }

  return bReturn || CGUIWindowPVRBase::OnMessage(message);
}

bool CGUIWindowPVRGuideBase::OnContextButton(int itemNumber, CONTEXT_BUTTON button)
{
  if (OnContextButtonNavigate(button))
    return true;

  if (itemNumber < 0 || itemNumber >= m_vecItems->Size())
    return false;

  return CGUIMediaWindow::OnContextButton(itemNumber, button);
}

namespace
{

template<typename A>
class CContextMenuFunctions : public CContextButtons
{
public:
  explicit CContextMenuFunctions(A* instance) : m_instance(instance) {}

  void Add(bool (A::*function)(), unsigned int resId)
  {
    CContextButtons::Add(size(), resId);
    m_functions.emplace_back(std::bind(function, m_instance));
  }

  bool Call(int idx)
  {
    if (idx < 0 || idx >= static_cast<int>(m_functions.size()))
      return false;

    return m_functions[idx]();
  }

private:
  A* m_instance = nullptr;
  std::vector<std::function<bool()>> m_functions;
};

} // unnamed namespace

bool CGUIWindowPVRGuideBase::OnContextButtonNavigate(CONTEXT_BUTTON button)
{
  bool bReturn = false;

  if (button == CONTEXT_BUTTON_NAVIGATE)
  {
    if (g_SkinInfo->HasSkinFile("DialogPVRGuideControls.xml"))
    {
      // use controls dialog
      CGUIDialog* dialog =
          CServiceBroker::GetGUI()->GetWindowManager().GetDialog(WINDOW_DIALOG_PVR_GUIDE_CONTROLS);
      if (dialog && !dialog->IsDialogRunning())
      {
        dialog->Open();
      }
    }
    else
    {
      // use context menu
      CContextMenuFunctions<CGUIWindowPVRGuideBase> buttons(this);
      buttons.Add(&CGUIWindowPVRGuideBase::GotoBegin, 19063); // First programme
      buttons.Add(&CGUIWindowPVRGuideBase::Go12HoursBack, 19317); // 12 hours back
      buttons.Add(&CGUIWindowPVRGuideBase::GotoCurrentProgramme, 19070); // Current programme
      buttons.Add(&CGUIWindowPVRGuideBase::Go12HoursForward, 19318); // 12 hours forward
      buttons.Add(&CGUIWindowPVRGuideBase::GotoEnd, 19064); // Last programme
      buttons.Add(&CGUIWindowPVRGuideBase::OpenDateSelectionDialog, 19288); // Date selector
      buttons.Add(&CGUIWindowPVRGuideBase::GotoFirstChannel, 19322); // First channel
      if (CServiceBroker::GetPVRManager().PlaybackState()->IsPlayingTV() ||
          CServiceBroker::GetPVRManager().PlaybackState()->IsPlayingRadio() ||
          CServiceBroker::GetPVRManager().PlaybackState()->IsPlayingEpgTag())
        buttons.Add(&CGUIWindowPVRGuideBase::GotoPlayingChannel, 19323); // Playing channel
      buttons.Add(&CGUIWindowPVRGuideBase::GotoLastChannel, 19324); // Last channel
      buttons.Add(&CGUIWindowPVRBase::ActivatePreviousChannelGroup, 19319); // Previous group
      buttons.Add(&CGUIWindowPVRBase::ActivateNextChannelGroup, 19320); // Next group
      buttons.Add(&CGUIWindowPVRBase::OpenChannelGroupSelectionDialog, 19321); // Group selector

      int buttonIdx = 0;
      int lastButtonIdx = 2; // initially select "Current programme"

      // loop until canceled
      while (buttonIdx >= 0)
      {
        buttonIdx = CGUIDialogContextMenu::Show(buttons, lastButtonIdx);
        lastButtonIdx = buttonIdx;
        buttons.Call(buttonIdx);
      }
    }
    bReturn = true;
  }

  return bReturn;
}

bool CGUIWindowPVRGuideBase::RefreshTimelineItems()
{
  if (m_bRefreshTimelineItems || m_bSyncRefreshTimelineItems)
  {
    m_bRefreshTimelineItems = false;
    m_bSyncRefreshTimelineItems = false;

    CGUIEPGGridContainer* epgGridContainer = GetGridControl();
    if (epgGridContainer)
    {
      const std::shared_ptr<CPVRChannelGroup> group(GetChannelGroup());
      if (!group)
        return false;

      CPVREpgContainer& epgContainer = CServiceBroker::GetPVRManager().EpgContainer();

      const std::pair<CDateTime, CDateTime> dates = epgContainer.GetFirstAndLastEPGDate();
      CDateTime startDate = dates.first;
      CDateTime endDate = dates.second;
      const CDateTime currentDate = CDateTime::GetUTCDateTime();

      if (!startDate.IsValid())
        startDate = currentDate;

      if (!endDate.IsValid() || endDate < startDate)
        endDate = startDate;

      // limit start to past days to display
      const int iPastDays = epgContainer.GetPastDaysToDisplay();
      const CDateTime maxPastDate(currentDate - CDateTimeSpan(iPastDays, 0, 0, 0));
      if (startDate < maxPastDate)
        startDate = maxPastDate;

      // limit end to future days to display
      const int iFutureDays = epgContainer.GetFutureDaysToDisplay();
      const CDateTime maxFutureDate(currentDate + CDateTimeSpan(iFutureDays, 0, 0, 0));
      if (endDate > maxFutureDate)
        endDate = maxFutureDate;

      std::unique_ptr<CFileItemList> channels(new CFileItemList);
      const std::vector<std::shared_ptr<CPVRChannelGroupMember>> groupMembers =
          group->GetMembers(CPVRChannelGroup::Include::ONLY_VISIBLE);

      for (const auto& groupMember : groupMembers)
      {
        channels->Add(std::make_shared<CFileItem>(groupMember));
      }

      if (m_guiState)
        channels->Sort(m_guiState->GetSortMethod());

      epgGridContainer->SetTimelineItems(channels, startDate, endDate);

      {
        std::unique_lock<CCriticalSection> lock(m_critSection);
        m_cachedChannelGroup = group;
      }
      return true;
    }
  }
  return false;
}

bool CGUIWindowPVRGuideBase::GotoBegin()
{
  GetGridControl()->GoToBegin();
  return true;
}

bool CGUIWindowPVRGuideBase::GotoEnd()
{
  GetGridControl()->GoToEnd();
  return true;
}

bool CGUIWindowPVRGuideBase::GotoCurrentProgramme()
{
  const CPVRManager& mgr = CServiceBroker::GetPVRManager();
  std::shared_ptr<CPVRChannel> channel = mgr.PlaybackState()->GetPlayingChannel();

  if (!channel)
  {
    const std::shared_ptr<CPVREpgInfoTag> playingTag = mgr.PlaybackState()->GetPlayingEpgTag();
    if (playingTag)
      channel = mgr.ChannelGroups()->GetChannelForEpgTag(playingTag);
  }

  if (channel)
    GetGridControl()->GoToDate(
        mgr.PlaybackState()->GetPlaybackTime(channel->ClientID(), channel->UniqueID()));
  else
    GetGridControl()->GoToNow();

  return true;
}

bool CGUIWindowPVRGuideBase::OpenDateSelectionDialog()
{
  bool bReturn = false;

  KODI::TIME::SystemTime date;
  CGUIEPGGridContainer* epgGridContainer = GetGridControl();
  epgGridContainer->GetSelectedDate().GetAsSystemTime(date);

  if (CGUIDialogNumeric::ShowAndGetDate(date, g_localizeStrings.Get(19288))) /* Go to date */
  {
    epgGridContainer->GoToDate(CDateTime(date));
    bReturn = true;
  }

  return bReturn;
}

bool CGUIWindowPVRGuideBase::Go12HoursBack()
{
  return GotoDate(-12);
}

bool CGUIWindowPVRGuideBase::Go12HoursForward()
{
  return GotoDate(+12);
}

bool CGUIWindowPVRGuideBase::GotoDate(int deltaHours)
{
  CGUIEPGGridContainer* epgGridContainer = GetGridControl();
  epgGridContainer->GoToDate(epgGridContainer->GetSelectedDate() +
                             CDateTimeSpan(0, deltaHours, 0, 0));
  return true;
}

bool CGUIWindowPVRGuideBase::GotoFirstChannel()
{
  GetGridControl()->GoToFirstChannel();
  return true;
}

bool CGUIWindowPVRGuideBase::GotoLastChannel()
{
  GetGridControl()->GoToLastChannel();
  return true;
}

bool CGUIWindowPVRGuideBase::GotoPlayingChannel()
{
  const CPVRManager& mgr = CServiceBroker::GetPVRManager();
  std::shared_ptr<CPVRChannel> channel = mgr.PlaybackState()->GetPlayingChannel();

  if (!channel)
  {
    const std::shared_ptr<CPVREpgInfoTag> playingTag = mgr.PlaybackState()->GetPlayingEpgTag();
    if (playingTag)
      channel = mgr.ChannelGroups()->GetChannelForEpgTag(playingTag);
  }

  if (channel)
  {
    GetGridControl()->SetChannel(channel);
    return true;
  }
  return false;
}

void CGUIWindowPVRGuideBase::OnInputDone()
{
  const CPVRChannelNumber channelNumber = GetChannelNumber();
  if (channelNumber.IsValid())
  {
    GetGridControl()->SetChannel(channelNumber);
  }
}

void CGUIWindowPVRGuideBase::GetChannelNumbers(std::vector<std::string>& channelNumbers)
{
  const std::shared_ptr<CPVRChannelGroup> group = GetChannelGroup();
  if (group)
    group->GetChannelNumbers(channelNumbers);
}

CPVRRefreshTimelineItemsThread::CPVRRefreshTimelineItemsThread(CGUIWindowPVRGuideBase* pGuideWindow)
  : CThread("epg-grid-refresh-timeline-items"),
    m_pGuideWindow(pGuideWindow),
    m_ready(true),
    m_done(false)
{
}

CPVRRefreshTimelineItemsThread::~CPVRRefreshTimelineItemsThread()
{
  // Note: CThread dtor will also call StopThread(true), but if thread worker function exits that
  //       late, it might access member variables of this which are already destroyed. Thus, stop
  //       the thread worker here and synchronously, while all members of this are still alive.
  StopThread(true);
}

void CPVRRefreshTimelineItemsThread::Stop()
{
  StopThread(false);
  m_ready.Set(); // wake up the worker thread to let it exit
}

void CPVRRefreshTimelineItemsThread::DoRefresh(bool bWait)
{
  m_ready.Set(); // wake up the worker thread

  if (bWait)
  {
    m_done.Reset();
    CGUIDialogBusy::WaitOnEvent(m_done, 100, false);
  }
}

void CPVRRefreshTimelineItemsThread::Process()
{
  static const int BOOSTED_SLEEPS_THRESHOLD = 4;

  int iLastEpgItemsCount = 0;
  int iUpdatesWithoutChange = 0;

  while (!m_bStop)
  {
    m_done.Reset();

    if (m_pGuideWindow->RefreshTimelineItems() && !m_bStop)
    {
      CGUIMessage m(GUI_MSG_REFRESH_LIST, m_pGuideWindow->GetID(), 0,
                    static_cast<int>(PVREvent::Epg));
      CServiceBroker::GetAppMessenger()->SendGUIMessage(m);
    }

    if (m_bStop)
      break;

    m_done.Set();

    // in order to fill the guide window asap, use a short update interval until we the
    // same amount of epg events for BOOSTED_SLEEPS_THRESHOLD + 1 times in a row .
    if (iUpdatesWithoutChange < BOOSTED_SLEEPS_THRESHOLD)
    {
      int iCurrentEpgItemsCount = m_pGuideWindow->CurrentDirectory().Size();

      if (iCurrentEpgItemsCount == iLastEpgItemsCount)
        iUpdatesWithoutChange++;
      else
        iUpdatesWithoutChange = 0; // reset

      iLastEpgItemsCount = iCurrentEpgItemsCount;

      m_ready.Wait(1000ms); // boosted update cycle
    }
    else
    {
      m_ready.Wait(5000ms); // normal update cycle
    }

    m_ready.Reset();
  }

  m_ready.Reset();
  m_done.Set();
}

std::string CGUIWindowPVRTVGuide::GetRootPath() const
{
  return "pvr://guide/tv/";
}

std::string CGUIWindowPVRRadioGuide::GetRootPath() const
{
  return "pvr://guide/radio/";
}