| 1 |
/* |
| 2 |
* Copyright (C) 2005-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 "GUIWindowLoginScreen.h" |
| 10 |
|
| 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" |
| 35 |
|
| 36 |
using namespace KODI::MESSAGING; |
| 37 |
|
| 38 |
#define CONTROL_BIG_LIST 52 |
| 39 |
#define CONTROL_LABEL_HEADER 2 |
| 40 |
#define CONTROL_LABEL_SELECTED_PROFILE 3 |
| 41 |
|
| 42 |
CGUIWindowLoginScreen::CGUIWindowLoginScreen(void) |
| 43 |
: CGUIWindow(WINDOW_LOGIN_SCREEN, "LoginScreen.xml") |
| 44 |
{ |
| 45 |
watch.StartZero(); |
| 46 |
m_vecItems = new CFileItemList; |
| 47 |
m_iSelectedItem = -1; |
| 48 |
m_loadType = KEEP_IN_MEMORY; |
| 49 |
} |
| 50 |
|
| 51 |
CGUIWindowLoginScreen::~CGUIWindowLoginScreen(void) |
| 52 |
{ |
| 53 |
delete m_vecItems; |
| 54 |
} |
| 55 |
|
| 56 |
bool CGUIWindowLoginScreen::OnMessage(CGUIMessage& message) |
| 57 |
{ |
| 58 |
switch ( message.GetMessage() ) |
| 59 |
{ |
| 60 |
case GUI_MSG_WINDOW_DEINIT: |
| 61 |
{ |
| 62 |
m_vecItems->Clear(); |
| 63 |
} |
| 64 |
break; |
| 65 |
|
| 66 |
case GUI_MSG_CLICKED: |
| 67 |
{ |
| 68 |
int iControl = message.GetSenderId(); |
| 69 |
if (iControl == CONTROL_BIG_LIST) |
| 70 |
{ |
| 71 |
int iAction = message.GetParam1(); |
| 72 |
|
| 73 |
// iItem is checked for validity inside these routines |
| 74 |
if (iAction == ACTION_CONTEXT_MENU || iAction == ACTION_MOUSE_RIGHT_CLICK) |
| 75 |
{ |
| 76 |
int iItem = m_viewControl.GetSelectedItem(); |
| 77 |
bool bResult = OnPopupMenu(m_viewControl.GetSelectedItem()); |
| 78 |
if (bResult) |
| 79 |
{ |
| 80 |
Update(); |
| 81 |
CGUIMessage msg(GUI_MSG_ITEM_SELECT,GetID(),CONTROL_BIG_LIST,iItem); |
| 82 |
OnMessage(msg); |
| 83 |
} |
| 84 |
|
| 85 |
return bResult; |
| 86 |
} |
| 87 |
else if (iAction == ACTION_SELECT_ITEM || iAction == ACTION_MOUSE_LEFT_CLICK) |
| 88 |
{ |
| 89 |
int iItem = m_viewControl.GetSelectedItem(); |
| 90 |
bool bCanceled; |
| 91 |
bool bOkay = g_passwordManager.IsProfileLockUnlocked(iItem, bCanceled); |
| 92 |
|
| 93 |
if (bOkay) |
| 94 |
{ |
| 95 |
if (iItem >= 0) |
| 96 |
CServiceBroker::GetAppMessenger()->PostMsg(TMSG_LOADPROFILE, iItem); |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
if (!bCanceled && iItem != 0) |
| 101 |
HELPERS::ShowOKDialogText(CVariant{20068}, CVariant{20117}); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
break; |
| 107 |
case GUI_MSG_SETFOCUS: |
| 108 |
{ |
| 109 |
if (m_viewControl.HasControl(message.GetControlId()) && m_viewControl.GetCurrentControl() != message.GetControlId()) |
| 110 |
{ |
| 111 |
m_viewControl.SetFocused(); |
| 112 |
return true; |
| 113 |
} |
| 114 |
} |
| 115 |
default: |
| 116 |
break; |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
return CGUIWindow::OnMessage(message); |
| 121 |
} |
| 122 |
|
| 123 |
bool CGUIWindowLoginScreen::OnAction(const CAction &action) |
| 124 |
{ |
| 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) |
| 128 |
{ |
| 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; |
| 135 |
} |
| 136 |
return CGUIWindow::OnAction(action); |
| 137 |
} |
| 138 |
|
| 139 |
bool CGUIWindowLoginScreen::OnBack(int actionID) |
| 140 |
{ |
| 141 |
// no escape from the login window |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
void CGUIWindowLoginScreen::FrameMove() |
| 146 |
{ |
| 147 |
if (GetFocusedControlID() == CONTROL_BIG_LIST && !CServiceBroker::GetGUI()->GetWindowManager().HasModalDialog(true)) |
| 148 |
{ |
| 149 |
if (m_viewControl.HasControl(CONTROL_BIG_LIST)) |
| 150 |
m_iSelectedItem = m_viewControl.GetSelectedItem(); |
| 151 |
} |
| 152 |
|
| 153 |
const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager(); |
| 154 |
|
| 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(); |
| 159 |
} |
| 160 |
|
| 161 |
void CGUIWindowLoginScreen::OnInitWindow() |
| 162 |
{ |
| 163 |
const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager(); |
| 164 |
|
| 165 |
m_iSelectedItem = static_cast<int>(profileManager->GetLastUsedProfileIndex()); |
| 166 |
|
| 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); |
| 173 |
|
| 174 |
CGUIWindow::OnInitWindow(); |
| 175 |
} |
| 176 |
|
| 177 |
void CGUIWindowLoginScreen::OnWindowLoaded() |
| 178 |
{ |
| 179 |
CGUIWindow::OnWindowLoaded(); |
| 180 |
m_viewControl.Reset(); |
| 181 |
m_viewControl.SetParentWindow(GetID()); |
| 182 |
m_viewControl.AddView(GetControl(CONTROL_BIG_LIST)); |
| 183 |
} |
| 184 |
|
| 185 |
void CGUIWindowLoginScreen::OnWindowUnload() |
| 186 |
{ |
| 187 |
CGUIWindow::OnWindowUnload(); |
| 188 |
m_viewControl.Reset(); |
| 189 |
} |
| 190 |
|
| 191 |
void CGUIWindowLoginScreen::Update() |
| 192 |
{ |
| 193 |
m_vecItems->Clear(); |
| 194 |
|
| 195 |
const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager(); |
| 196 |
|
| 197 |
for (unsigned int i = 0; i < profileManager->GetNumberOfProfiles(); ++i) |
| 198 |
{ |
| 199 |
const CProfile *profile = profileManager->GetProfile(i); |
| 200 |
|
| 201 |
CFileItemPtr item(new CFileItem(profile->getName())); |
| 202 |
|
| 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()); |
| 208 |
|
| 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); |
| 214 |
|
| 215 |
m_vecItems->Add(item); |
| 216 |
} |
| 217 |
m_viewControl.SetItems(*m_vecItems); |
| 218 |
m_viewControl.SetSelectedItem(m_iSelectedItem); |
| 219 |
} |
| 220 |
|
| 221 |
bool CGUIWindowLoginScreen::OnPopupMenu(int iItem) |
| 222 |
{ |
| 223 |
if (iItem < 0 || iItem >= m_vecItems->Size()) |
| 224 |
return false; |
| 225 |
|
| 226 |
const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager(); |
| 227 |
|
| 228 |
CFileItemPtr pItem = m_vecItems->Get(iItem); |
| 229 |
bool bSelect = pItem->IsSelected(); |
| 230 |
|
| 231 |
// mark the item |
| 232 |
pItem->Select(true); |
| 233 |
|
| 234 |
CContextButtons choices; |
| 235 |
choices.Add(1, 20067); |
| 236 |
|
| 237 |
if (iItem == 0 && g_passwordManager.iMasterLockRetriesLeft == 0) |
| 238 |
choices.Add(2, 12334); |
| 239 |
|
| 240 |
int choice = CGUIDialogContextMenu::ShowAndGetChoice(choices); |
| 241 |
if (choice == 2) |
| 242 |
{ |
| 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); |
| 247 |
|
| 248 |
return true; |
| 249 |
} |
| 250 |
|
| 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()); |
| 254 |
|
| 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); |
| 258 |
|
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
CFileItemPtr CGUIWindowLoginScreen::GetCurrentListItem(int offset) |
| 263 |
{ |
| 264 |
int item = m_viewControl.GetSelectedItem(); |
| 265 |
if (item < 0 || !m_vecItems->Size()) return CFileItemPtr(); |
| 266 |
|
| 267 |
item = (item + offset) % m_vecItems->Size(); |
| 268 |
if (item < 0) item += m_vecItems->Size(); |
| 269 |
return m_vecItems->Get(item); |
| 270 |
} |