[Test] Added tests for CUtil::SplitParams
[xbmc.git] / xbmc / windows / GUIWindowLoginScreen.cpp
blob04eb5f90415978548aa32363b82fb2c7b88f7e18
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "GUIWindowLoginScreen.h"
11 #include "FileItem.h"
12 #include "GUIPassword.h"
13 #include "ServiceBroker.h"
14 #include "addons/Skin.h"
15 #include "dialogs/GUIDialogContextMenu.h"
16 #include "guilib/GUIComponent.h"
17 #include "guilib/GUIMessage.h"
18 #include "guilib/GUIWindowManager.h"
19 #include "guilib/LocalizeStrings.h"
20 #include "input/actions/Action.h"
21 #include "input/actions/ActionIDs.h"
22 #include "interfaces/builtins/Builtins.h"
23 #include "messaging/ApplicationMessenger.h"
24 #include "messaging/helpers/DialogOKHelper.h"
25 #include "profiles/Profile.h"
26 #include "profiles/ProfileManager.h"
27 #include "profiles/dialogs/GUIDialogProfileSettings.h"
28 #include "pvr/PVRManager.h"
29 #include "pvr/guilib/PVRGUIActionsPowerManagement.h"
30 #include "settings/Settings.h"
31 #include "settings/SettingsComponent.h"
32 #include "utils/StringUtils.h"
33 #include "utils/Variant.h"
34 #include "view/ViewState.h"
36 using namespace KODI::MESSAGING;
38 #define CONTROL_BIG_LIST 52
39 #define CONTROL_LABEL_HEADER 2
40 #define CONTROL_LABEL_SELECTED_PROFILE 3
42 CGUIWindowLoginScreen::CGUIWindowLoginScreen(void)
43 : CGUIWindow(WINDOW_LOGIN_SCREEN, "LoginScreen.xml")
45 watch.StartZero();
46 m_vecItems = new CFileItemList;
47 m_iSelectedItem = -1;
48 m_loadType = KEEP_IN_MEMORY;
51 CGUIWindowLoginScreen::~CGUIWindowLoginScreen(void)
53 delete m_vecItems;
56 bool CGUIWindowLoginScreen::OnMessage(CGUIMessage& message)
58 switch ( message.GetMessage() )
60 case GUI_MSG_WINDOW_DEINIT:
62 m_vecItems->Clear();
64 break;
66 case GUI_MSG_CLICKED:
68 int iControl = message.GetSenderId();
69 if (iControl == CONTROL_BIG_LIST)
71 int iAction = message.GetParam1();
73 // iItem is checked for validity inside these routines
74 if (iAction == ACTION_CONTEXT_MENU || iAction == ACTION_MOUSE_RIGHT_CLICK)
76 int iItem = m_viewControl.GetSelectedItem();
77 bool bResult = OnPopupMenu(m_viewControl.GetSelectedItem());
78 if (bResult)
80 Update();
81 CGUIMessage msg(GUI_MSG_ITEM_SELECT,GetID(),CONTROL_BIG_LIST,iItem);
82 OnMessage(msg);
85 return bResult;
87 else if (iAction == ACTION_SELECT_ITEM || iAction == ACTION_MOUSE_LEFT_CLICK)
89 int iItem = m_viewControl.GetSelectedItem();
90 bool bCanceled;
91 bool bOkay = g_passwordManager.IsProfileLockUnlocked(iItem, bCanceled);
93 if (bOkay)
95 if (iItem >= 0)
96 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_LOADPROFILE, iItem);
98 else
100 if (!bCanceled && iItem != 0)
101 HELPERS::ShowOKDialogText(CVariant{20068}, CVariant{20117});
106 break;
107 case GUI_MSG_SETFOCUS:
109 if (m_viewControl.HasControl(message.GetControlId()) && m_viewControl.GetCurrentControl() != message.GetControlId())
111 m_viewControl.SetFocused();
112 return true;
115 default:
116 break;
120 return CGUIWindow::OnMessage(message);
123 bool CGUIWindowLoginScreen::OnAction(const CAction &action)
125 // don't allow built in actions to act here except shutdown related ones.
126 // this forces only navigation type actions to be performed.
127 if (action.GetID() == ACTION_BUILT_IN_FUNCTION)
129 std::string actionName = action.GetName();
130 StringUtils::ToLower(actionName);
131 if ((actionName.find("shutdown") != std::string::npos) &&
132 CServiceBroker::GetPVRManager().Get<PVR::GUI::PowerManagement>().CanSystemPowerdown())
133 CBuiltins::GetInstance().Execute(action.GetName());
134 return true;
136 return CGUIWindow::OnAction(action);
139 bool CGUIWindowLoginScreen::OnBack(int actionID)
141 // no escape from the login window
142 return false;
145 void CGUIWindowLoginScreen::FrameMove()
147 if (GetFocusedControlID() == CONTROL_BIG_LIST && !CServiceBroker::GetGUI()->GetWindowManager().HasModalDialog(true))
149 if (m_viewControl.HasControl(CONTROL_BIG_LIST))
150 m_iSelectedItem = m_viewControl.GetSelectedItem();
153 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
155 std::string strLabel = StringUtils::Format(g_localizeStrings.Get(20114), m_iSelectedItem + 1,
156 profileManager->GetNumberOfProfiles());
157 SET_CONTROL_LABEL(CONTROL_LABEL_SELECTED_PROFILE,strLabel);
158 CGUIWindow::FrameMove();
161 void CGUIWindowLoginScreen::OnInitWindow()
163 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
165 m_iSelectedItem = static_cast<int>(profileManager->GetLastUsedProfileIndex());
167 // Update list/thumb control
168 m_viewControl.SetCurrentView(DEFAULT_VIEW_LIST);
169 Update();
170 m_viewControl.SetFocused();
171 SET_CONTROL_LABEL(CONTROL_LABEL_HEADER,g_localizeStrings.Get(20115));
172 SET_CONTROL_VISIBLE(CONTROL_BIG_LIST);
174 CGUIWindow::OnInitWindow();
177 void CGUIWindowLoginScreen::OnWindowLoaded()
179 CGUIWindow::OnWindowLoaded();
180 m_viewControl.Reset();
181 m_viewControl.SetParentWindow(GetID());
182 m_viewControl.AddView(GetControl(CONTROL_BIG_LIST));
185 void CGUIWindowLoginScreen::OnWindowUnload()
187 CGUIWindow::OnWindowUnload();
188 m_viewControl.Reset();
191 void CGUIWindowLoginScreen::Update()
193 m_vecItems->Clear();
195 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
197 for (unsigned int i = 0; i < profileManager->GetNumberOfProfiles(); ++i)
199 const CProfile *profile = profileManager->GetProfile(i);
201 CFileItemPtr item(new CFileItem(profile->getName()));
203 std::string strLabel;
204 if (profile->getDate().empty())
205 strLabel = g_localizeStrings.Get(20113);
206 else
207 strLabel = StringUtils::Format(g_localizeStrings.Get(20112), profile->getDate());
209 item->SetLabel2(strLabel);
210 item->SetArt("thumb", profile->getThumb());
211 if (profile->getThumb().empty())
212 item->SetArt("thumb", "DefaultUser.png");
213 item->SetLabelPreformatted(true);
215 m_vecItems->Add(item);
217 m_viewControl.SetItems(*m_vecItems);
218 m_viewControl.SetSelectedItem(m_iSelectedItem);
221 bool CGUIWindowLoginScreen::OnPopupMenu(int iItem)
223 if (iItem < 0 || iItem >= m_vecItems->Size())
224 return false;
226 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
228 CFileItemPtr pItem = m_vecItems->Get(iItem);
229 bool bSelect = pItem->IsSelected();
231 // mark the item
232 pItem->Select(true);
234 CContextButtons choices;
235 choices.Add(1, 20067);
237 if (iItem == 0 && g_passwordManager.iMasterLockRetriesLeft == 0)
238 choices.Add(2, 12334);
240 int choice = CGUIDialogContextMenu::ShowAndGetChoice(choices);
241 if (choice == 2)
243 if (g_passwordManager.CheckLock(profileManager->GetMasterProfile().getLockMode(), profileManager->GetMasterProfile().getLockCode(), 20075))
244 g_passwordManager.iMasterLockRetriesLeft = CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(CSettings::SETTING_MASTERLOCK_MAXRETRIES);
245 else // be inconvenient
246 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_SHUTDOWN);
248 return true;
251 // Edit the profile after checking if the correct master lock password was given.
252 if (choice == 1 && g_passwordManager.IsMasterLockUnlocked(true))
253 CGUIDialogProfileSettings::ShowForProfile(m_viewControl.GetSelectedItem());
255 //NOTE: this can potentially (de)select the wrong item if the filelisting has changed because of an action above.
256 if (iItem < static_cast<int>(profileManager->GetNumberOfProfiles()))
257 m_vecItems->Get(iItem)->Select(bSelect);
259 return false;
262 CFileItemPtr CGUIWindowLoginScreen::GetCurrentListItem(int offset)
264 int item = m_viewControl.GetSelectedItem();
265 if (item < 0 || !m_vecItems->Size()) return CFileItemPtr();
267 item = (item + offset) % m_vecItems->Size();
268 if (item < 0) item += m_vecItems->Size();
269 return m_vecItems->Get(item);