summaryrefslogtreecommitdiffstats
path: root/xbmc/pvr/guilib/PVRGUIActionsChannels.cpp
blob: 80fb90eed26b02ab69af996b3dbf241382df0eb5 (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
/*
 *  Copyright (C) 2016-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 "PVRGUIActionsChannels.h"

#include "FileItem.h"
#include "ServiceBroker.h"
#include "dialogs/GUIDialogSelect.h"
#include "dialogs/GUIDialogYesNo.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/WindowIDs.h"
#include "input/actions/Action.h"
#include "input/actions/ActionIDs.h"
#include "messaging/ApplicationMessenger.h"
#include "messaging/helpers/DialogOKHelper.h"
#include "pvr/PVRItem.h"
#include "pvr/PVRManager.h"
#include "pvr/PVRPlaybackState.h"
#include "pvr/addons/PVRClient.h"
#include "pvr/addons/PVRClients.h"
#include "pvr/channels/PVRChannel.h"
#include "pvr/channels/PVRChannelGroup.h"
#include "pvr/channels/PVRChannelGroupMember.h"
#include "pvr/channels/PVRChannelGroups.h"
#include "pvr/channels/PVRChannelGroupsContainer.h"
#include "pvr/epg/EpgInfoTag.h"
#include "pvr/windows/GUIWindowPVRBase.h"
#include "settings/Settings.h"
#include "utils/Variant.h"
#include "utils/log.h"

#include <algorithm>
#include <chrono>
#include <memory>
#include <mutex>
#include <string>
#include <vector>

using namespace PVR;
using namespace KODI::MESSAGING;

void CPVRChannelSwitchingInputHandler::AppendChannelNumberCharacter(char cCharacter)
{
  // special case. if only a single zero was typed in, switch to previously played channel.
  if (GetCurrentDigitCount() == 0 && cCharacter == '0')
  {
    SwitchToPreviousChannel();
    return;
  }

  CPVRChannelNumberInputHandler::AppendChannelNumberCharacter(cCharacter);
}

void CPVRChannelSwitchingInputHandler::GetChannelNumbers(std::vector<std::string>& channelNumbers)
{
  const CPVRManager& pvrMgr = CServiceBroker::GetPVRManager();
  const std::shared_ptr<CPVRChannel> playingChannel = pvrMgr.PlaybackState()->GetPlayingChannel();
  if (playingChannel)
  {
    const std::shared_ptr<CPVRChannelGroup> group =
        pvrMgr.ChannelGroups()->GetGroupAll(playingChannel->IsRadio());
    if (group)
      group->GetChannelNumbers(channelNumbers);
  }
}

void CPVRChannelSwitchingInputHandler::OnInputDone()
{
  CPVRChannelNumber channelNumber = GetChannelNumber();
  if (channelNumber.GetChannelNumber())
    SwitchToChannel(channelNumber);
}

void CPVRChannelSwitchingInputHandler::SwitchToChannel(const CPVRChannelNumber& channelNumber)
{
  if (channelNumber.IsValid() && CServiceBroker::GetPVRManager().PlaybackState()->IsPlaying())
  {
    const std::shared_ptr<CPVRChannel> playingChannel =
        CServiceBroker::GetPVRManager().PlaybackState()->GetPlayingChannel();
    if (playingChannel)
    {
      bool bRadio = playingChannel->IsRadio();
      const std::shared_ptr<CPVRChannelGroup> group =
          CServiceBroker::GetPVRManager().PlaybackState()->GetActiveChannelGroup(bRadio);

      if (channelNumber != group->GetChannelNumber(playingChannel))
      {
        // channel number present in active group?
        std::shared_ptr<CPVRChannelGroupMember> groupMember =
            group->GetByChannelNumber(channelNumber);

        if (!groupMember)
        {
          // channel number present in any group?
          const CPVRChannelGroups* groupAccess =
              CServiceBroker::GetPVRManager().ChannelGroups()->Get(bRadio);
          const std::vector<std::shared_ptr<CPVRChannelGroup>> groups =
              groupAccess->GetMembers(true);
          for (const auto& currentGroup : groups)
          {
            if (currentGroup == group) // we have already checked this group
              continue;

            groupMember = currentGroup->GetByChannelNumber(channelNumber);
            if (groupMember)
              break;
          }
        }

        if (groupMember)
        {
          CServiceBroker::GetAppMessenger()->PostMsg(
              TMSG_GUI_ACTION, WINDOW_INVALID, -1,
              static_cast<void*>(new CAction(
                  ACTION_CHANNEL_SWITCH, static_cast<float>(channelNumber.GetChannelNumber()),
                  static_cast<float>(channelNumber.GetSubChannelNumber()))));
        }
      }
    }
  }
}

void CPVRChannelSwitchingInputHandler::SwitchToPreviousChannel()
{
  const std::shared_ptr<CPVRPlaybackState> playbackState =
      CServiceBroker::GetPVRManager().PlaybackState();
  if (playbackState->IsPlaying())
  {
    const std::shared_ptr<CPVRChannel> playingChannel = playbackState->GetPlayingChannel();
    if (playingChannel)
    {
      const std::shared_ptr<CPVRChannelGroupMember> groupMember =
          playbackState->GetPreviousToLastPlayedChannelGroupMember(playingChannel->IsRadio());
      if (groupMember)
      {
        const CPVRChannelNumber channelNumber = groupMember->ChannelNumber();
        CServiceBroker::GetAppMessenger()->SendMsg(
            TMSG_GUI_ACTION, WINDOW_INVALID, -1,
            static_cast<void*>(new CAction(
                ACTION_CHANNEL_SWITCH, static_cast<float>(channelNumber.GetChannelNumber()),
                static_cast<float>(channelNumber.GetSubChannelNumber()))));
      }
    }
  }
}

CPVRGUIActionsChannels::CPVRGUIActionsChannels()
  : m_settings({CSettings::SETTING_PVRMANAGER_PRESELECTPLAYINGCHANNEL})
{
  RegisterChannelNumberInputHandler(&m_channelNumberInputHandler);
}

CPVRGUIActionsChannels::~CPVRGUIActionsChannels()
{
  DeregisterChannelNumberInputHandler(&m_channelNumberInputHandler);
}

void CPVRGUIActionsChannels::RegisterChannelNumberInputHandler(
    CPVRChannelNumberInputHandler* handler)
{
  if (handler)
    handler->Events().Subscribe(this, &CPVRGUIActionsChannels::Notify);
}

void CPVRGUIActionsChannels::DeregisterChannelNumberInputHandler(
    CPVRChannelNumberInputHandler* handler)
{
  if (handler)
    handler->Events().Unsubscribe(this);
}

void CPVRGUIActionsChannels::Notify(const PVRChannelNumberInputChangedEvent& event)
{
  m_events.Publish(event);
}

bool CPVRGUIActionsChannels::HideChannel(const CFileItem& item) const
{
  const std::shared_ptr<CPVRChannel> channel = item.GetPVRChannelInfoTag();

  if (!channel)
    return false;

  if (!CGUIDialogYesNo::ShowAndGetInput(
          CVariant{19054}, // "Hide channel"
          CVariant{19039}, // "Are you sure you want to hide this channel?"
          CVariant{""}, CVariant{channel->ChannelName()}))
    return false;

  if (!CServiceBroker::GetPVRManager()
           .ChannelGroups()
           ->GetGroupAll(channel->IsRadio())
           ->RemoveFromGroup(channel))
    return false;

  CGUIWindowPVRBase* pvrWindow =
      dynamic_cast<CGUIWindowPVRBase*>(CServiceBroker::GetGUI()->GetWindowManager().GetWindow(
          CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow()));
  if (pvrWindow)
    pvrWindow->DoRefresh();
  else
    CLog::LogF(LOGERROR, "Called on non-pvr window. No refresh possible.");

  return true;
}

bool CPVRGUIActionsChannels::StartChannelScan()
{
  return StartChannelScan(PVR_INVALID_CLIENT_ID);
}

bool CPVRGUIActionsChannels::StartChannelScan(int clientId)
{
  if (!CServiceBroker::GetPVRManager().IsStarted() || IsRunningChannelScan())
    return false;

  std::shared_ptr<CPVRClient> scanClient;
  std::vector<std::shared_ptr<CPVRClient>> possibleScanClients =
      CServiceBroker::GetPVRManager().Clients()->GetClientsSupportingChannelScan();
  m_bChannelScanRunning = true;

  if (clientId != PVR_INVALID_CLIENT_ID)
  {
    const auto it =
        std::find_if(possibleScanClients.cbegin(), possibleScanClients.cend(),
                     [clientId](const auto& client) { return client->GetID() == clientId; });

    if (it != possibleScanClients.cend())
      scanClient = (*it);

    if (!scanClient)
    {
      CLog::LogF(LOGERROR,
                 "Provided client id '{}' could not be found in list of possible scan clients!",
                 clientId);
      m_bChannelScanRunning = false;
      return false;
    }
  }
  /* multiple clients found */
  else if (possibleScanClients.size() > 1)
  {
    CGUIDialogSelect* pDialog =
        CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogSelect>(
            WINDOW_DIALOG_SELECT);
    if (!pDialog)
    {
      CLog::LogF(LOGERROR, "Unable to get WINDOW_DIALOG_SELECT!");
      m_bChannelScanRunning = false;
      return false;
    }

    pDialog->Reset();
    pDialog->SetHeading(CVariant{19119}); // "On which backend do you want to search?"

    for (const auto& client : possibleScanClients)
      pDialog->Add(client->GetFriendlyName());

    pDialog->Open();

    int selection = pDialog->GetSelectedItem();
    if (selection >= 0)
      scanClient = possibleScanClients[selection];
  }
  /* one client found */
  else if (possibleScanClients.size() == 1)
  {
    scanClient = possibleScanClients[0];
  }
  /* no clients found */
  else if (!scanClient)
  {
    HELPERS::ShowOKDialogText(
        CVariant{19033}, // "Information"
        CVariant{19192}); // "None of the connected PVR backends supports scanning for channels."
    m_bChannelScanRunning = false;
    return false;
  }

  /* start the channel scan */
  CLog::LogFC(LOGDEBUG, LOGPVR, "Starting to scan for channels on client {}",
              scanClient->GetFriendlyName());
  auto start = std::chrono::steady_clock::now();

  /* do the scan */
  if (scanClient->StartChannelScan() != PVR_ERROR_NO_ERROR)
    HELPERS::ShowOKDialogText(
        CVariant{257}, // "Error"
        CVariant{
            19193}); // "The channel scan can't be started. Check the log for more information about this message."

  auto end = std::chrono::steady_clock::now();
  auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);

  CLog::LogFC(LOGDEBUG, LOGPVR, "Channel scan finished after {} ms", duration.count());

  m_bChannelScanRunning = false;
  return true;
}

std::shared_ptr<CPVRChannelGroupMember> CPVRGUIActionsChannels::GetChannelGroupMember(
    const std::shared_ptr<CPVRChannel>& channel) const
{
  if (!channel)
    return {};

  std::shared_ptr<CPVRChannelGroupMember> groupMember;

  // first, try whether the channel is contained in the active channel group, except
  // if a window is active which never uses the active channel group, e.g. Timers window
  const int activeWindowID = CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow();

  static std::vector<int> windowIDs = {
      WINDOW_TV_RECORDINGS,    WINDOW_TV_TIMERS,    WINDOW_TV_TIMER_RULES,    WINDOW_TV_SEARCH,
      WINDOW_RADIO_RECORDINGS, WINDOW_RADIO_TIMERS, WINDOW_RADIO_TIMER_RULES, WINDOW_RADIO_SEARCH,
  };

  if (std::find(windowIDs.cbegin(), windowIDs.cend(), activeWindowID) == windowIDs.cend())
  {
    const std::shared_ptr<CPVRChannelGroup> group =
        CServiceBroker::GetPVRManager().PlaybackState()->GetActiveChannelGroup(channel->IsRadio());
    if (group)
      groupMember = group->GetByUniqueID(channel->StorageId());
  }

  // as fallback, obtain the member from the 'all channels' group
  if (!groupMember)
  {
    const std::shared_ptr<CPVRChannelGroup> group =
        CServiceBroker::GetPVRManager().ChannelGroups()->GetGroupAll(channel->IsRadio());
    if (group)
      groupMember = group->GetByUniqueID(channel->StorageId());
  }

  return groupMember;
}

std::shared_ptr<CPVRChannelGroupMember> CPVRGUIActionsChannels::GetChannelGroupMember(
    const CFileItem& item) const
{
  std::shared_ptr<CPVRChannelGroupMember> groupMember = item.GetPVRChannelGroupMemberInfoTag();

  if (!groupMember)
    groupMember = GetChannelGroupMember(CPVRItem(std::make_shared<CFileItem>(item)).GetChannel());

  return groupMember;
}

CPVRChannelNumberInputHandler& CPVRGUIActionsChannels::GetChannelNumberInputHandler()
{
  // window/dialog specific input handler
  CPVRChannelNumberInputHandler* windowInputHandler = dynamic_cast<CPVRChannelNumberInputHandler*>(
      CServiceBroker::GetGUI()->GetWindowManager().GetWindow(
          CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindowOrDialog()));
  if (windowInputHandler)
    return *windowInputHandler;

  // default
  return m_channelNumberInputHandler;
}

CPVRGUIChannelNavigator& CPVRGUIActionsChannels::GetChannelNavigator()
{
  return m_channelNavigator;
}

void CPVRGUIActionsChannels::OnPlaybackStarted(const CFileItem& item)
{
  const std::shared_ptr<CPVRChannelGroupMember> groupMember = GetChannelGroupMember(item);
  if (groupMember)
  {
    m_channelNavigator.SetPlayingChannel(groupMember);
    SetSelectedChannelPath(groupMember->Channel()->IsRadio(), groupMember->Path());
  }
}

void CPVRGUIActionsChannels::OnPlaybackStopped(const CFileItem& item)
{
  if (item.HasPVRChannelInfoTag() || item.HasEPGInfoTag())
  {
    m_channelNavigator.ClearPlayingChannel();
  }
}

void CPVRGUIActionsChannels::SetSelectedChannelPath(bool bRadio, const std::string& path)
{
  std::unique_lock<CCriticalSection> lock(m_critSection);
  if (bRadio)
    m_selectedChannelPathRadio = path;
  else
    m_selectedChannelPathTV = path;
}

std::string CPVRGUIActionsChannels::GetSelectedChannelPath(bool bRadio) const
{
  if (m_settings.GetBoolValue(CSettings::SETTING_PVRMANAGER_PRESELECTPLAYINGCHANNEL))
  {
    CPVRManager& mgr = CServiceBroker::GetPVRManager();

    // if preselect playing channel is activated, return the path of the playing channel, if any.
    const std::shared_ptr<CPVRChannelGroupMember> playingChannel =
        mgr.PlaybackState()->GetPlayingChannelGroupMember();
    if (playingChannel && playingChannel->IsRadio() == bRadio)
      return playingChannel->Path();

    const std::shared_ptr<CPVREpgInfoTag> playingTag = mgr.PlaybackState()->GetPlayingEpgTag();
    if (playingTag && playingTag->IsRadio() == bRadio)
    {
      const std::shared_ptr<CPVRChannel> channel =
          mgr.ChannelGroups()->GetChannelForEpgTag(playingTag);
      if (channel)
        return GetChannelGroupMember(channel)->Path();
    }
  }

  std::unique_lock<CCriticalSection> lock(m_critSection);
  return bRadio ? m_selectedChannelPathRadio : m_selectedChannelPathTV;
}