1 /*
2  *  Copyright (C) 2017-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 "GUIDialogPVRClientPriorities.h"
10 
11 #include "ServiceBroker.h"
12 #include "guilib/GUIMessage.h"
13 #include "pvr/PVRManager.h"
14 #include "pvr/addons/PVRClient.h"
15 #include "settings/lib/Setting.h"
16 #include "utils/StringUtils.h"
17 #include "utils/log.h"
18 
19 #include <cstdlib>
20 #include <memory>
21 
22 using namespace PVR;
23 
CGUIDialogPVRClientPriorities()24 CGUIDialogPVRClientPriorities::CGUIDialogPVRClientPriorities() :
25   CGUIDialogSettingsManualBase(WINDOW_DIALOG_PVR_CLIENT_PRIORITIES, "DialogSettings.xml")
26 {
27   m_loadType = LOAD_EVERY_TIME;
28 }
29 
SetupView()30 void CGUIDialogPVRClientPriorities::SetupView()
31 {
32   CGUIDialogSettingsManualBase::SetupView();
33 
34   SetHeading(19240); // Client priorities
35   SET_CONTROL_HIDDEN(CONTROL_SETTINGS_CUSTOM_BUTTON);
36   SET_CONTROL_LABEL(CONTROL_SETTINGS_OKAY_BUTTON, 186); // OK
37   SET_CONTROL_LABEL(CONTROL_SETTINGS_CANCEL_BUTTON, 222); // Cancel
38 }
39 
GetSettingsLabel(const std::shared_ptr<ISetting> & pSetting)40 std::string CGUIDialogPVRClientPriorities::GetSettingsLabel(
41     const std::shared_ptr<ISetting>& pSetting)
42 {
43   int iClientId = std::atoi(pSetting->GetId().c_str());
44   auto clientEntry = m_clients.find(iClientId);
45   if (clientEntry != m_clients.end())
46     return clientEntry->second->GetFriendlyName();
47 
48   CLog::LogF(LOGERROR, "Unable to obtain pvr client with id '{}'", iClientId);
49   return CGUIDialogSettingsManualBase::GetLocalizedString(13205); // Unknown
50 }
51 
InitializeSettings()52 void CGUIDialogPVRClientPriorities::InitializeSettings()
53 {
54   CGUIDialogSettingsManualBase::InitializeSettings();
55 
56   const std::shared_ptr<CSettingCategory> category = AddCategory("pvrclientpriorities", -1);
57   if (category == nullptr)
58   {
59     CLog::LogF(LOGERROR, "Unable to add settings category");
60     return;
61   }
62 
63   const std::shared_ptr<CSettingGroup> group = AddGroup(category);
64   if (group == nullptr)
65   {
66     CLog::LogF(LOGERROR, "Unable to add settings group");
67     return;
68   }
69 
70   std::shared_ptr<CSetting> setting = nullptr;
71 
72   CServiceBroker::GetPVRManager().Clients()->GetCreatedClients(m_clients);
73   for (const auto& client : m_clients)
74   {
75     setting = AddEdit(group, StringUtils::Format("%i", client.second->GetID()), 13205 /* Unknown */, SettingLevel::Basic, client.second->GetPriority());
76   }
77 }
78 
OnSettingChanged(const std::shared_ptr<const CSetting> & setting)79 void CGUIDialogPVRClientPriorities::OnSettingChanged(const std::shared_ptr<const CSetting>& setting)
80 {
81   if (setting == nullptr)
82   {
83     CLog::LogF(LOGERROR, "No setting");
84     return;
85   }
86 
87   CGUIDialogSettingsManualBase::OnSettingChanged(setting);
88 
89   m_changedValues[setting->GetId()] = std::static_pointer_cast<const CSettingInt>(setting)->GetValue();
90 }
91 
Save()92 bool CGUIDialogPVRClientPriorities::Save()
93 {
94   for (const auto& changedClient : m_changedValues)
95   {
96     int iClientId = std::atoi(changedClient.first.c_str());
97     auto clientEntry = m_clients.find(iClientId);
98     if (clientEntry != m_clients.end())
99       clientEntry->second->SetPriority(changedClient.second);
100   }
101 
102   return true;
103 }
104