2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "GUIPassword.h"
22 #include "GUIUserMessages.h"
23 #include "ApplicationMessenger.h"
24 #include "dialogs/GUIDialogGamepad.h"
25 #include "guilib/GUIKeyboardFactory.h"
26 #include "dialogs/GUIDialogNumeric.h"
27 #include "dialogs/GUIDialogOK.h"
28 #include "profiles/ProfilesManager.h"
29 #include "profiles/dialogs/GUIDialogLockSettings.h"
30 #include "profiles/dialogs/GUIDialogProfileSettings.h"
32 #include "settings/MediaSourceSettings.h"
33 #include "settings/Settings.h"
34 #include "guilib/GUIWindowManager.h"
36 #include "guilib/LocalizeStrings.h"
37 #include "utils/log.h"
38 #include "view/ViewStateSettings.h"
40 CGUIPassword::CGUIPassword(void)
42 iMasterLockRetriesLeft = -1;
45 CGUIPassword::~CGUIPassword(void)
48 bool CGUIPassword::IsItemUnlocked(CFileItem* pItem, const CStdString &strType)
50 // \brief Tests if the user is allowed to access the share folder
51 // \param pItem The share folder item to access
52 // \param strType The type of share being accessed, e.g. "music", "video", etc. See CSettings::UpdateSources()
53 // \return If access is granted, returns \e true
54 if (CProfilesManager::Get().GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE)
57 while (pItem->m_iHasLock > 1)
59 CStdString strLockCode = pItem->m_strLockCode;
60 CStdString strLabel = pItem->GetLabel();
61 int iResult = 0; // init to user succeeded state, doing this to optimize switch statement below
62 char buffer[33]; // holds 32 places plus sign character
63 if(g_passwordManager.bMasterUser)// Check if we are the MasterUser!
69 if (0 != CSettings::Get().GetInt("masterlock.maxretries") && pItem->m_iBadPwdCount >= CSettings::Get().GetInt("masterlock.maxretries"))
70 { // user previously exhausted all retries, show access denied error
71 CGUIDialogOK::ShowAndGetInput(12345, 12346, 0, 0);
74 // show the appropriate lock dialog
75 CStdString strHeading = "";
76 if (pItem->m_bIsFolder)
77 strHeading = g_localizeStrings.Get(12325);
79 strHeading = g_localizeStrings.Get(12348);
81 iResult = VerifyPassword(pItem->m_iLockMode, strLockCode, strHeading);
86 { // user canceled out
92 // password entry succeeded
93 pItem->m_iBadPwdCount = 0;
94 pItem->m_iHasLock = 1;
95 g_passwordManager.LockSource(strType,strLabel,false);
96 sprintf(buffer,"%i",pItem->m_iBadPwdCount);
97 CMediaSourceSettings::Get().UpdateSource(strType, strLabel, "badpwdcount", buffer);
98 CMediaSourceSettings::Get().Save();
103 // password entry failed
104 if (0 != CSettings::Get().GetInt("masterlock.maxretries"))
105 pItem->m_iBadPwdCount++;
106 sprintf(buffer,"%i",pItem->m_iBadPwdCount);
107 CMediaSourceSettings::Get().UpdateSource(strType, strLabel, "badpwdcount", buffer);
108 CMediaSourceSettings::Get().Save();
113 // this should never happen, but if it does, do nothing
122 bool CGUIPassword::CheckStartUpLock()
124 // prompt user for mastercode if the mastercode was set b4 or by xml
125 int iVerifyPasswordResult = -1;
126 CStdString strHeader = g_localizeStrings.Get(20075);
127 if (iMasterLockRetriesLeft == -1)
128 iMasterLockRetriesLeft = CSettings::Get().GetInt("masterlock.maxretries");
129 if (g_passwordManager.iMasterLockRetriesLeft == 0) g_passwordManager.iMasterLockRetriesLeft = 1;
130 CStdString strPassword = CProfilesManager::Get().GetMasterProfile().getLockCode();
131 if (CProfilesManager::Get().GetMasterProfile().getLockMode() == 0)
132 iVerifyPasswordResult = 0;
135 for (int i=1; i <= g_passwordManager.iMasterLockRetriesLeft; i++)
137 iVerifyPasswordResult = VerifyPassword(CProfilesManager::Get().GetMasterProfile().getLockMode(), strPassword, strHeader);
138 if (iVerifyPasswordResult != 0 )
140 CStdString strLabel,strLabel1;
141 strLabel1 = g_localizeStrings.Get(12343);
142 int iLeft = g_passwordManager.iMasterLockRetriesLeft-i;
143 strLabel.Format("%i %s",iLeft,strLabel1.c_str());
145 // PopUp OK and Display: MasterLock mode has changed but no no Mastercode has been set!
146 CGUIDialogOK::ShowAndGetInput(20076, 12367, 12368, strLabel);
149 i=g_passwordManager.iMasterLockRetriesLeft;
153 if (iVerifyPasswordResult == 0)
155 g_passwordManager.iMasterLockRetriesLeft = CSettings::Get().GetInt("masterlock.maxretries");
156 return true; // OK The MasterCode Accepted! XBMC Can Run!
160 CApplicationMessenger::Get().Shutdown(); // Turn off the box
165 bool CGUIPassword::SetMasterLockMode(bool bDetails)
167 CProfile* profile = CProfilesManager::Get().GetProfile(0);
170 CProfile::CLock locks = profile->GetLocks();
171 if (CGUIDialogLockSettings::ShowAndGetLock(locks, 12360, true, bDetails))
173 profile->SetLocks(locks);
180 bool CGUIPassword::IsProfileLockUnlocked(int iProfile)
183 return IsProfileLockUnlocked(iProfile,bDummy,true);
186 bool CGUIPassword::IsProfileLockUnlocked(int iProfile, bool& bCanceled, bool prompt)
188 if (g_passwordManager.bMasterUser)
190 int iProfileToCheck=iProfile;
192 iProfileToCheck = CProfilesManager::Get().GetCurrentProfileIndex();
193 if (iProfileToCheck == 0)
194 return IsMasterLockUnlocked(prompt,bCanceled);
197 CProfile *profile = CProfilesManager::Get().GetProfile(iProfileToCheck);
202 return (profile->getLockMode() == LOCK_MODE_EVERYONE);
204 if (profile->getDate().IsEmpty() &&
205 (CProfilesManager::Get().GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE ||
206 profile->getLockMode() == LOCK_MODE_EVERYONE))
208 // user hasn't set a password and this is the first time they've used this account
209 // so prompt for password/settings
210 if (CGUIDialogProfileSettings::ShowForProfile(iProfileToCheck, true))
214 if (CProfilesManager::Get().GetMasterProfile().getLockMode() != LOCK_MODE_EVERYONE)
215 return CheckLock(profile->getLockMode(),profile->getLockCode(),20095,bCanceled);
221 bool CGUIPassword::IsMasterLockUnlocked(bool bPromptUser)
224 return IsMasterLockUnlocked(bPromptUser,bDummy);
227 bool CGUIPassword::IsMasterLockUnlocked(bool bPromptUser, bool& bCanceled)
230 if (iMasterLockRetriesLeft == -1)
231 iMasterLockRetriesLeft = CSettings::Get().GetInt("masterlock.maxretries");
232 if ((LOCK_MODE_EVERYONE < CProfilesManager::Get().GetMasterProfile().getLockMode() && !bMasterUser) && !bPromptUser)
233 // not unlocked, but calling code doesn't want to prompt user
236 if (g_passwordManager.bMasterUser || CProfilesManager::Get().GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE)
239 if (iMasterLockRetriesLeft == 0)
241 UpdateMasterLockRetryCount(false);
245 // no, unlock since we are allowed to prompt
246 int iVerifyPasswordResult = -1;
247 CStdString strHeading = g_localizeStrings.Get(20075);
248 CStdString strPassword = CProfilesManager::Get().GetMasterProfile().getLockCode();
249 iVerifyPasswordResult = VerifyPassword(CProfilesManager::Get().GetMasterProfile().getLockMode(), strPassword, strHeading);
250 if (1 == iVerifyPasswordResult)
251 UpdateMasterLockRetryCount(false);
253 if (0 != iVerifyPasswordResult)
259 // user successfully entered mastercode
260 UpdateMasterLockRetryCount(true);
264 void CGUIPassword::UpdateMasterLockRetryCount(bool bResetCount)
266 // \brief Updates Master Lock status.
267 // \param bResetCount masterlock retry counter is zeroed if true, or incremented and displays an Access Denied dialog if false.
270 // Bad mastercode entered
271 if (0 < CSettings::Get().GetInt("masterlock.maxretries"))
273 // We're keeping track of how many bad passwords are entered
274 if (1 < g_passwordManager.iMasterLockRetriesLeft)
276 // user still has at least one retry after decrementing
277 g_passwordManager.iMasterLockRetriesLeft--;
281 // user has run out of retry attempts
282 g_passwordManager.iMasterLockRetriesLeft = 0;
283 // Tell the user they ran out of retry attempts
284 CGUIDialogOK::ShowAndGetInput(12345, 12346, 0, 0);
288 CStdString dlgLine1 = "";
289 if (0 < g_passwordManager.iMasterLockRetriesLeft)
290 dlgLine1.Format("%d %s", g_passwordManager.iMasterLockRetriesLeft, g_localizeStrings.Get(12343));
291 CGUIDialogOK::ShowAndGetInput(20075, 12345, dlgLine1, 0);
294 g_passwordManager.iMasterLockRetriesLeft = CSettings::Get().GetInt("masterlock.maxretries"); // user entered correct mastercode, reset retries to max allowed
297 bool CGUIPassword::CheckLock(LockType btnType, const CStdString& strPassword, int iHeading)
300 return CheckLock(btnType,strPassword,iHeading,bDummy);
303 bool CGUIPassword::CheckLock(LockType btnType, const CStdString& strPassword, int iHeading, bool& bCanceled)
306 if (btnType == LOCK_MODE_EVERYONE || strPassword.Equals("-") ||
307 CProfilesManager::Get().GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE || g_passwordManager.bMasterUser)
310 int iVerifyPasswordResult = -1;
311 CStdString strHeading = g_localizeStrings.Get(iHeading);
312 iVerifyPasswordResult = VerifyPassword(btnType, strPassword, strHeading);
314 if (iVerifyPasswordResult == -1)
317 return (iVerifyPasswordResult==0);
320 bool CGUIPassword::CheckSettingLevelLock(const SettingLevel& level, bool enforce /*=false*/)
322 LOCK_LEVEL::SETTINGS_LOCK lockLevel = CProfilesManager::Get().GetCurrentProfile().settingsLockLevel();
324 if (lockLevel == LOCK_LEVEL::NONE)
327 //check if we are already in settings and in an level that needs unlocking
328 int windowID = g_windowManager.GetActiveWindow();
329 if ((int)lockLevel-1 <= (short)CViewStateSettings::Get().GetSettingLevel() &&
330 (windowID == WINDOW_SETTINGS_MENU ||
331 (windowID >= WINDOW_SCREEN_CALIBRATION &&
332 windowID <= WINDOW_SETTINGS_MYPVR)))
333 return true; //Already unlocked
335 else if (lockLevel == LOCK_LEVEL::ALL)
336 return IsMasterLockUnlocked(true);
337 else if ((int)lockLevel-1 <= (short)level)
340 return IsMasterLockUnlocked(true);
341 else if (!IsMasterLockUnlocked(false))
343 //Current Setting level is higher than our permission... so lower the viewing level
344 SettingLevel newLevel = (SettingLevel)(short)(lockLevel-2);
345 CViewStateSettings::Get().SetSettingLevel(newLevel);
352 bool CGUIPassword::CheckMenuLock(int iWindowID)
354 bool bCheckPW = false;
355 int iSwitch = iWindowID;
357 // check if a settings subcategory was called from other than settings window
358 if (iWindowID >= WINDOW_SCREEN_CALIBRATION && iWindowID <= WINDOW_SETTINGS_MYPVR)
360 int iCWindowID = g_windowManager.GetActiveWindow();
361 if (iCWindowID != WINDOW_SETTINGS_MENU && (iCWindowID < WINDOW_SCREEN_CALIBRATION || iCWindowID > WINDOW_SETTINGS_MYPVR))
362 iSwitch = WINDOW_SETTINGS_MENU;
365 if (iWindowID == WINDOW_MUSIC_FILES)
366 if (g_windowManager.GetActiveWindow() == WINDOW_MUSIC_NAV)
367 iSwitch = WINDOW_HOME;
369 if (iWindowID == WINDOW_MUSIC_NAV)
370 if (g_windowManager.GetActiveWindow() == WINDOW_HOME)
371 iSwitch = WINDOW_MUSIC_FILES;
373 if (iWindowID == WINDOW_VIDEO_NAV)
374 if (g_windowManager.GetActiveWindow() == WINDOW_HOME)
375 iSwitch = WINDOW_VIDEO_FILES;
377 if (iWindowID == WINDOW_VIDEO_FILES)
378 if (g_windowManager.GetActiveWindow() == WINDOW_VIDEO_NAV)
379 iSwitch = WINDOW_HOME;
383 case WINDOW_SETTINGS_MENU: // Settings
384 return CheckSettingLevelLock(CViewStateSettings::Get().GetSettingLevel());
386 case WINDOW_ADDON_BROWSER: // Addons
387 bCheckPW = CProfilesManager::Get().GetCurrentProfile().addonmanagerLocked();
389 case WINDOW_FILES: // Files
390 bCheckPW = CProfilesManager::Get().GetCurrentProfile().filesLocked();
392 case WINDOW_PROGRAMS: // Programs
393 bCheckPW = CProfilesManager::Get().GetCurrentProfile().programsLocked();
395 case WINDOW_MUSIC_FILES: // Music
396 bCheckPW = CProfilesManager::Get().GetCurrentProfile().musicLocked();
398 case WINDOW_VIDEO_FILES: // Video
399 bCheckPW = CProfilesManager::Get().GetCurrentProfile().videoLocked();
401 case WINDOW_PICTURES: // Pictures
402 bCheckPW = CProfilesManager::Get().GetCurrentProfile().picturesLocked();
404 case WINDOW_SETTINGS_PROFILES:
412 return IsMasterLockUnlocked(true); //Now let's check the PW if we need!
417 bool CGUIPassword::LockSource(const CStdString& strType, const CStdString& strName, bool bState)
419 VECSOURCES* pShares = CMediaSourceSettings::Get().GetSources(strType);
420 bool bResult = false;
421 for (IVECSOURCES it=pShares->begin();it != pShares->end();++it)
423 if (it->strName == strName)
425 if (it->m_iHasLock > 0)
427 it->m_iHasLock = bState?2:1;
433 CGUIMessage msg(GUI_MSG_NOTIFY_ALL,0,0,GUI_MSG_UPDATE_SOURCES);
434 g_windowManager.SendThreadMessage(msg);
439 void CGUIPassword::LockSources(bool lock)
441 // lock or unlock all sources (those with locks)
442 const char* strType[5] = {"programs","music","video","pictures","files"};
443 for (int i=0;i<5;++i)
445 VECSOURCES *shares = CMediaSourceSettings::Get().GetSources(strType[i]);
446 for (IVECSOURCES it=shares->begin();it != shares->end();++it)
447 if (it->m_iLockMode != LOCK_MODE_EVERYONE)
448 it->m_iHasLock = lock ? 2 : 1;
450 CGUIMessage msg(GUI_MSG_NOTIFY_ALL,0,0,GUI_MSG_UPDATE_SOURCES);
451 g_windowManager.SendThreadMessage(msg);
454 void CGUIPassword::RemoveSourceLocks()
456 // remove lock from all sources
457 const char* strType[5] = {"programs","music","video","pictures","files"};
458 for (int i=0;i<5;++i)
460 VECSOURCES *shares = CMediaSourceSettings::Get().GetSources(strType[i]);
461 for (IVECSOURCES it=shares->begin();it != shares->end();++it)
462 if (it->m_iLockMode != LOCK_MODE_EVERYONE) // remove old info
465 it->m_iLockMode = LOCK_MODE_EVERYONE;
466 CMediaSourceSettings::Get().UpdateSource(strType[i], it->strName, "lockmode", "0"); // removes locks from xml
469 CMediaSourceSettings::Get().Save();
470 CGUIMessage msg(GUI_MSG_NOTIFY_ALL,0,0, GUI_MSG_UPDATE_SOURCES);
471 g_windowManager.SendThreadMessage(msg);
474 bool CGUIPassword::IsDatabasePathUnlocked(const CStdString& strPath, VECSOURCES& vecSources)
476 if (g_passwordManager.bMasterUser || CProfilesManager::Get().GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE)
479 // try to find the best matching source
481 int iIndex = CUtil::GetMatchingSource(strPath, vecSources, bName);
483 if (iIndex > -1 && iIndex < (int)vecSources.size())
484 if (vecSources[iIndex].m_iHasLock < 2)
490 void CGUIPassword::OnSettingAction(const CSetting *setting)
495 const std::string &settingId = setting->GetId();
496 if (settingId == "masterlock.lockcode")
500 int CGUIPassword::VerifyPassword(LockType btnType, const CStdString& strPassword, const CStdString& strHeading)
502 int iVerifyPasswordResult;
505 case LOCK_MODE_NUMERIC:
506 iVerifyPasswordResult = CGUIDialogNumeric::ShowAndVerifyPassword(const_cast<CStdString&>(strPassword), strHeading, 0);
508 case LOCK_MODE_GAMEPAD:
509 iVerifyPasswordResult = CGUIDialogGamepad::ShowAndVerifyPassword(const_cast<CStdString&>(strPassword), strHeading, 0);
511 case LOCK_MODE_QWERTY:
512 iVerifyPasswordResult = CGUIKeyboardFactory::ShowAndVerifyPassword(const_cast<CStdString&>(strPassword), strHeading, 0);
514 default: // must not be supported, treat as unlocked
515 iVerifyPasswordResult = 0;
519 return iVerifyPasswordResult;