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.
9 #include "GUIWindowLoginScreen.h"
12 #include "FileItemList.h"
13 #include "GUIPassword.h"
14 #include "ServiceBroker.h"
15 #include "addons/Skin.h"
16 #include "dialogs/GUIDialogContextMenu.h"
17 #include "guilib/GUIComponent.h"
18 #include "guilib/GUIMessage.h"
19 #include "guilib/GUIWindowManager.h"
20 #include "guilib/LocalizeStrings.h"
21 #include "input/actions/Action.h"
22 #include "input/actions/ActionIDs.h"
23 #include "interfaces/builtins/Builtins.h"
24 #include "messaging/ApplicationMessenger.h"
25 #include "messaging/helpers/DialogOKHelper.h"
26 #include "profiles/Profile.h"
27 #include "profiles/ProfileManager.h"
28 #include "profiles/dialogs/GUIDialogProfileSettings.h"
29 #include "pvr/PVRManager.h"
30 #include "pvr/guilib/PVRGUIActionsPowerManagement.h"
31 #include "settings/Settings.h"
32 #include "settings/SettingsComponent.h"
33 #include "utils/StringUtils.h"
34 #include "utils/Variant.h"
35 #include "view/ViewState.h"
37 using namespace KODI::MESSAGING
;
39 #define CONTROL_BIG_LIST 52
40 #define CONTROL_LABEL_HEADER 2
41 #define CONTROL_LABEL_SELECTED_PROFILE 3
43 CGUIWindowLoginScreen::CGUIWindowLoginScreen(void)
44 : CGUIWindow(WINDOW_LOGIN_SCREEN
, "LoginScreen.xml")
47 m_vecItems
= new CFileItemList
;
49 m_loadType
= KEEP_IN_MEMORY
;
52 CGUIWindowLoginScreen::~CGUIWindowLoginScreen(void)
57 bool CGUIWindowLoginScreen::OnMessage(CGUIMessage
& message
)
59 switch ( message
.GetMessage() )
61 case GUI_MSG_WINDOW_DEINIT
:
69 int iControl
= message
.GetSenderId();
70 if (iControl
== CONTROL_BIG_LIST
)
72 int iAction
= message
.GetParam1();
74 // iItem is checked for validity inside these routines
75 if (iAction
== ACTION_CONTEXT_MENU
|| iAction
== ACTION_MOUSE_RIGHT_CLICK
)
77 int iItem
= m_viewControl
.GetSelectedItem();
78 bool bResult
= OnPopupMenu(m_viewControl
.GetSelectedItem());
82 CGUIMessage
msg(GUI_MSG_ITEM_SELECT
,GetID(),CONTROL_BIG_LIST
,iItem
);
88 else if (iAction
== ACTION_SELECT_ITEM
|| iAction
== ACTION_MOUSE_LEFT_CLICK
)
90 int iItem
= m_viewControl
.GetSelectedItem();
92 bool bOkay
= g_passwordManager
.IsProfileLockUnlocked(iItem
, bCanceled
);
97 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_LOADPROFILE
, iItem
);
101 if (!bCanceled
&& iItem
!= 0)
102 HELPERS::ShowOKDialogText(CVariant
{20068}, CVariant
{20117});
108 case GUI_MSG_SETFOCUS
:
110 if (m_viewControl
.HasControl(message
.GetControlId()) && m_viewControl
.GetCurrentControl() != message
.GetControlId())
112 m_viewControl
.SetFocused();
121 return CGUIWindow::OnMessage(message
);
124 bool CGUIWindowLoginScreen::OnAction(const CAction
&action
)
126 // don't allow built in actions to act here except shutdown related ones.
127 // this forces only navigation type actions to be performed.
128 if (action
.GetID() == ACTION_BUILT_IN_FUNCTION
)
130 std::string actionName
= action
.GetName();
131 StringUtils::ToLower(actionName
);
132 if ((actionName
.find("shutdown") != std::string::npos
) &&
133 CServiceBroker::GetPVRManager().Get
<PVR::GUI::PowerManagement
>().CanSystemPowerdown())
134 CBuiltins::GetInstance().Execute(action
.GetName());
137 return CGUIWindow::OnAction(action
);
140 bool CGUIWindowLoginScreen::OnBack(int actionID
)
142 // no escape from the login window
146 void CGUIWindowLoginScreen::FrameMove()
148 if (GetFocusedControlID() == CONTROL_BIG_LIST
&& !CServiceBroker::GetGUI()->GetWindowManager().HasModalDialog(true))
150 if (m_viewControl
.HasControl(CONTROL_BIG_LIST
))
151 m_iSelectedItem
= m_viewControl
.GetSelectedItem();
154 const std::shared_ptr
<CProfileManager
> profileManager
= CServiceBroker::GetSettingsComponent()->GetProfileManager();
156 std::string strLabel
= StringUtils::Format(g_localizeStrings
.Get(20114), m_iSelectedItem
+ 1,
157 profileManager
->GetNumberOfProfiles());
158 SET_CONTROL_LABEL(CONTROL_LABEL_SELECTED_PROFILE
,strLabel
);
159 CGUIWindow::FrameMove();
162 void CGUIWindowLoginScreen::OnInitWindow()
164 const std::shared_ptr
<CProfileManager
> profileManager
= CServiceBroker::GetSettingsComponent()->GetProfileManager();
166 m_iSelectedItem
= static_cast<int>(profileManager
->GetLastUsedProfileIndex());
168 // Update list/thumb control
169 m_viewControl
.SetCurrentView(DEFAULT_VIEW_LIST
);
171 m_viewControl
.SetFocused();
172 SET_CONTROL_LABEL(CONTROL_LABEL_HEADER
,g_localizeStrings
.Get(20115));
173 SET_CONTROL_VISIBLE(CONTROL_BIG_LIST
);
175 CGUIWindow::OnInitWindow();
178 void CGUIWindowLoginScreen::OnWindowLoaded()
180 CGUIWindow::OnWindowLoaded();
181 m_viewControl
.Reset();
182 m_viewControl
.SetParentWindow(GetID());
183 m_viewControl
.AddView(GetControl(CONTROL_BIG_LIST
));
186 void CGUIWindowLoginScreen::OnWindowUnload()
188 CGUIWindow::OnWindowUnload();
189 m_viewControl
.Reset();
192 void CGUIWindowLoginScreen::Update()
196 const std::shared_ptr
<CProfileManager
> profileManager
= CServiceBroker::GetSettingsComponent()->GetProfileManager();
198 for (unsigned int i
= 0; i
< profileManager
->GetNumberOfProfiles(); ++i
)
200 const CProfile
*profile
= profileManager
->GetProfile(i
);
202 CFileItemPtr
item(new CFileItem(profile
->getName()));
204 std::string strLabel
;
205 if (profile
->getDate().empty())
206 strLabel
= g_localizeStrings
.Get(20113);
208 strLabel
= StringUtils::Format(g_localizeStrings
.Get(20112), profile
->getDate());
210 item
->SetLabel2(strLabel
);
211 item
->SetArt("thumb", profile
->getThumb());
212 if (profile
->getThumb().empty())
213 item
->SetArt("thumb", "DefaultUser.png");
214 item
->SetLabelPreformatted(true);
216 m_vecItems
->Add(item
);
218 m_viewControl
.SetItems(*m_vecItems
);
219 m_viewControl
.SetSelectedItem(m_iSelectedItem
);
222 bool CGUIWindowLoginScreen::OnPopupMenu(int iItem
)
224 if (iItem
< 0 || iItem
>= m_vecItems
->Size())
227 const std::shared_ptr
<CProfileManager
> profileManager
= CServiceBroker::GetSettingsComponent()->GetProfileManager();
229 CFileItemPtr pItem
= m_vecItems
->Get(iItem
);
230 bool bSelect
= pItem
->IsSelected();
235 CContextButtons choices
;
236 choices
.Add(1, 20067);
238 if (iItem
== 0 && g_passwordManager
.iMasterLockRetriesLeft
== 0)
239 choices
.Add(2, 12334);
241 int choice
= CGUIDialogContextMenu::ShowAndGetChoice(choices
);
244 if (g_passwordManager
.CheckLock(profileManager
->GetMasterProfile().getLockMode(), profileManager
->GetMasterProfile().getLockCode(), 20075))
245 g_passwordManager
.iMasterLockRetriesLeft
= CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(CSettings::SETTING_MASTERLOCK_MAXRETRIES
);
246 else // be inconvenient
247 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_SHUTDOWN
);
252 // Edit the profile after checking if the correct master lock password was given.
253 if (choice
== 1 && g_passwordManager
.IsMasterLockUnlocked(true))
254 CGUIDialogProfileSettings::ShowForProfile(m_viewControl
.GetSelectedItem());
256 //NOTE: this can potentially (de)select the wrong item if the filelisting has changed because of an action above.
257 if (iItem
< static_cast<int>(profileManager
->GetNumberOfProfiles()))
258 m_vecItems
->Get(iItem
)->Select(bSelect
);
263 CFileItemPtr
CGUIWindowLoginScreen::GetCurrentListItem(int offset
)
265 int item
= m_viewControl
.GetSelectedItem();
266 if (item
< 0 || !m_vecItems
->Size()) return CFileItemPtr();
268 item
= (item
+ offset
) % m_vecItems
->Size();
269 if (item
< 0) item
+= m_vecItems
->Size();
270 return m_vecItems
->Get(item
);