[Windows] Remove redundant DirectSound error codes
[xbmc.git] / xbmc / GUIPassword.cpp
blob07b536756822a43ea66b578be79194caa9321312
1 /*
2 * Copyright (C) 2005-2020 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 "GUIPassword.h"
11 #include "FileItem.h"
12 #include "GUIUserMessages.h"
13 #include "ServiceBroker.h"
14 #include "URL.h"
15 #include "Util.h"
16 #include "dialogs/GUIDialogGamepad.h"
17 #include "dialogs/GUIDialogNumeric.h"
18 #include "favourites/FavouritesService.h"
19 #include "guilib/GUIComponent.h"
20 #include "guilib/GUIKeyboardFactory.h"
21 #include "guilib/GUIWindowManager.h"
22 #include "guilib/LocalizeStrings.h"
23 #include "media/MediaLockState.h"
24 #include "messaging/ApplicationMessenger.h"
25 #include "messaging/helpers/DialogOKHelper.h"
26 #include "profiles/ProfileManager.h"
27 #include "profiles/dialogs/GUIDialogLockSettings.h"
28 #include "profiles/dialogs/GUIDialogProfileSettings.h"
29 #include "settings/MediaSourceSettings.h"
30 #include "settings/Settings.h"
31 #include "settings/SettingsComponent.h"
32 #include "utils/StringUtils.h"
33 #include "utils/URIUtils.h"
34 #include "utils/Variant.h"
35 #include "utils/log.h"
36 #include "view/ViewStateSettings.h"
38 #include <utility>
40 using namespace KODI::MESSAGING;
42 CGUIPassword::CGUIPassword(void)
44 iMasterLockRetriesLeft = -1;
45 bMasterUser = false;
47 CGUIPassword::~CGUIPassword(void) = default;
49 template<typename T>
50 bool CGUIPassword::IsItemUnlocked(T pItem,
51 const std::string& strType,
52 const std::string& strLabel,
53 const std::string& strHeading)
55 const std::shared_ptr<CProfileManager> profileManager =
56 CServiceBroker::GetSettingsComponent()->GetProfileManager();
57 if (profileManager->GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE)
58 return true;
60 while (pItem->m_iHasLock > LOCK_STATE_LOCK_BUT_UNLOCKED)
62 const std::string strLockCode = pItem->m_strLockCode;
63 int iResult = 0; // init to user succeeded state, doing this to optimize switch statement below
64 if (!g_passwordManager.bMasterUser) // Check if we are the MasterUser!
66 if (0 != CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(
67 CSettings::SETTING_MASTERLOCK_MAXRETRIES) &&
68 pItem->m_iBadPwdCount >= CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(
69 CSettings::SETTING_MASTERLOCK_MAXRETRIES))
71 // user previously exhausted all retries, show access denied error
72 HELPERS::ShowOKDialogText(CVariant{12345}, CVariant{12346});
73 return false;
75 // show the appropriate lock dialog
76 iResult = VerifyPassword(pItem->m_iLockMode, strLockCode, strHeading);
78 switch (iResult)
80 case -1:
81 { // user canceled out
82 return false;
83 break;
85 case 0:
87 // password entry succeeded
88 pItem->m_iBadPwdCount = 0;
89 pItem->m_iHasLock = LOCK_STATE_LOCK_BUT_UNLOCKED;
90 g_passwordManager.LockSource(strType, strLabel, false);
91 CMediaSourceSettings::GetInstance().UpdateSource(strType, strLabel, "badpwdcount",
92 std::to_string(pItem->m_iBadPwdCount));
93 CMediaSourceSettings::GetInstance().Save();
95 // a mediasource has been unlocked successfully
96 // => refresh favourites due to possible visibility changes
97 CServiceBroker::GetFavouritesService().RefreshFavourites();
98 break;
100 case 1:
102 // password entry failed
103 if (0 != CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(
104 CSettings::SETTING_MASTERLOCK_MAXRETRIES))
105 pItem->m_iBadPwdCount++;
106 CMediaSourceSettings::GetInstance().UpdateSource(strType, strLabel, "badpwdcount",
107 std::to_string(pItem->m_iBadPwdCount));
108 CMediaSourceSettings::GetInstance().Save();
109 break;
111 default:
113 // this should never happen, but if it does, do nothing
114 return false;
115 break;
119 return true;
122 bool CGUIPassword::IsItemUnlocked(CFileItem* pItem, const std::string& strType)
124 const std::string strLabel = pItem->GetLabel();
125 std::string strHeading;
126 if (pItem->m_bIsFolder)
127 strHeading = g_localizeStrings.Get(12325); // "Locked! Enter code..."
128 else
129 strHeading = g_localizeStrings.Get(12348); // "Item locked"
131 return IsItemUnlocked<CFileItem*>(pItem, strType, strLabel, strHeading);
134 bool CGUIPassword::IsItemUnlocked(CMediaSource* pItem, const std::string& strType)
136 const std::string strLabel = pItem->strName;
137 const std::string& strHeading = g_localizeStrings.Get(12325); // "Locked! Enter code..."
139 return IsItemUnlocked<CMediaSource*>(pItem, strType, strLabel, strHeading);
142 bool CGUIPassword::CheckStartUpLock()
144 // prompt user for mastercode if the mastercode was set b4 or by xml
145 int iVerifyPasswordResult = -1;
147 const std::string& strHeader = g_localizeStrings.Get(20075); // "Enter master lock code"
149 if (iMasterLockRetriesLeft == -1)
150 iMasterLockRetriesLeft = CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(CSettings::SETTING_MASTERLOCK_MAXRETRIES);
152 if (g_passwordManager.iMasterLockRetriesLeft == 0)
153 g_passwordManager.iMasterLockRetriesLeft = 1;
155 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
157 std::string strPassword = profileManager->GetMasterProfile().getLockCode();
159 if (profileManager->GetMasterProfile().getLockMode() == 0)
160 iVerifyPasswordResult = 0;
161 else
163 for (int i=1; i <= g_passwordManager.iMasterLockRetriesLeft; i++)
165 iVerifyPasswordResult = VerifyPassword(profileManager->GetMasterProfile().getLockMode(), strPassword, strHeader);
166 if (iVerifyPasswordResult != 0 )
168 std::string strLabel1;
169 strLabel1 = g_localizeStrings.Get(12343); // "retries left"
170 int iLeft = g_passwordManager.iMasterLockRetriesLeft-i;
171 std::string strLabel = StringUtils::Format("{} {}", iLeft, strLabel1);
173 // PopUp OK and Display: MasterLock mode has changed but no new Mastercode has been set!
174 HELPERS::ShowOKDialogLines(CVariant{12360}, CVariant{12367}, CVariant{strLabel}, CVariant{""});
176 else
177 i=g_passwordManager.iMasterLockRetriesLeft;
181 if (iVerifyPasswordResult == 0)
183 g_passwordManager.iMasterLockRetriesLeft = CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(CSettings::SETTING_MASTERLOCK_MAXRETRIES);
184 return true; // OK The MasterCode Accepted! XBMC Can Run!
186 else
188 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_SHUTDOWN); // Turn off the box
189 return false;
193 bool CGUIPassword::SetMasterLockMode(bool bDetails)
195 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
197 CProfile* profile = profileManager->GetProfile(0);
198 if (profile)
200 CProfile::CLock locks = profile->GetLocks();
201 // prompt user for master lock
202 if (CGUIDialogLockSettings::ShowAndGetLock(locks, 12360, true, bDetails))
204 profile->SetLocks(locks);
205 return true;
208 return false;
211 bool CGUIPassword::IsProfileLockUnlocked(int iProfile)
213 bool bDummy;
214 return IsProfileLockUnlocked(iProfile,bDummy,true);
217 bool CGUIPassword::IsProfileLockUnlocked(int iProfile, bool& bCanceled, bool prompt)
219 if (g_passwordManager.bMasterUser)
220 return true;
222 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
224 int iProfileToCheck = iProfile;
225 if (iProfile == -1)
226 iProfileToCheck = profileManager->GetCurrentProfileIndex();
228 if (iProfileToCheck == 0)
229 return IsMasterLockUnlocked(prompt,bCanceled);
230 else
232 const CProfile *profile = profileManager->GetProfile(iProfileToCheck);
233 if (!profile)
234 return false;
236 if (!prompt)
237 return (profile->getLockMode() == LOCK_MODE_EVERYONE);
239 if (profile->getDate().empty() &&
240 (profileManager->GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE ||
241 profile->getLockMode() == LOCK_MODE_EVERYONE))
243 // user hasn't set a password and this is the first time they've used this account
244 // so prompt for password/settings
245 if (CGUIDialogProfileSettings::ShowForProfile(iProfileToCheck, true))
246 return true;
248 else
250 if (profileManager->GetMasterProfile().getLockMode() != LOCK_MODE_EVERYONE)
251 // prompt user for profile lock code
252 return CheckLock(profile->getLockMode(),profile->getLockCode(),20095,bCanceled);
256 return true;
259 bool CGUIPassword::IsMasterLockUnlocked(bool bPromptUser)
261 bool bDummy;
262 return IsMasterLockUnlocked(bPromptUser,bDummy);
265 bool CGUIPassword::IsMasterLockUnlocked(bool bPromptUser, bool& bCanceled)
267 bCanceled = false;
269 if (iMasterLockRetriesLeft == -1)
270 iMasterLockRetriesLeft = CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(CSettings::SETTING_MASTERLOCK_MAXRETRIES);
272 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
274 if ((LOCK_MODE_EVERYONE < profileManager->GetMasterProfile().getLockMode() && !bMasterUser) && !bPromptUser)
276 // not unlocked, but calling code doesn't want to prompt user
277 return false;
280 if (g_passwordManager.bMasterUser || profileManager->GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE)
281 return true;
283 if (iMasterLockRetriesLeft == 0)
285 UpdateMasterLockRetryCount(false);
286 return false;
289 // no, unlock since we are allowed to prompt
290 const std::string& strHeading = g_localizeStrings.Get(20075);
291 std::string strPassword = profileManager->GetMasterProfile().getLockCode();
293 int iVerifyPasswordResult = VerifyPassword(profileManager->GetMasterProfile().getLockMode(), strPassword, strHeading);
294 if (1 == iVerifyPasswordResult)
295 UpdateMasterLockRetryCount(false);
297 if (0 != iVerifyPasswordResult)
299 bCanceled = true;
300 return false;
303 // user successfully entered mastercode
304 UpdateMasterLockRetryCount(true);
305 return true;
308 void CGUIPassword::UpdateMasterLockRetryCount(bool bResetCount)
310 // \brief Updates Master Lock status.
311 // \param bResetCount masterlock retry counter is zeroed if true, or incremented and displays an Access Denied dialog if false.
312 if (!bResetCount)
314 // Bad mastercode entered
315 if (0 < CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(CSettings::SETTING_MASTERLOCK_MAXRETRIES))
317 // We're keeping track of how many bad passwords are entered
318 if (1 < g_passwordManager.iMasterLockRetriesLeft)
320 // user still has at least one retry after decrementing
321 g_passwordManager.iMasterLockRetriesLeft--;
323 else
325 // user has run out of retry attempts
326 g_passwordManager.iMasterLockRetriesLeft = 0;
327 // Tell the user they ran out of retry attempts
328 HELPERS::ShowOKDialogText(CVariant{12345}, CVariant{12346});
329 return;
332 std::string dlgLine1 = "";
333 if (0 < g_passwordManager.iMasterLockRetriesLeft)
334 dlgLine1 = StringUtils::Format("{} {}", g_passwordManager.iMasterLockRetriesLeft,
335 g_localizeStrings.Get(12343)); // "retries left"
336 // prompt user for master lock code
337 HELPERS::ShowOKDialogLines(CVariant{20075}, CVariant{12345}, CVariant{std::move(dlgLine1)}, CVariant{0});
339 else
340 g_passwordManager.iMasterLockRetriesLeft = CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt(CSettings::SETTING_MASTERLOCK_MAXRETRIES); // user entered correct mastercode, reset retries to max allowed
343 bool CGUIPassword::CheckLock(LockType btnType, const std::string& strPassword, int iHeading)
345 bool bDummy;
346 return CheckLock(btnType,strPassword,iHeading,bDummy);
349 bool CGUIPassword::CheckLock(LockType btnType, const std::string& strPassword, int iHeading, bool& bCanceled)
351 bCanceled = false;
353 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
355 if (btnType == LOCK_MODE_EVERYONE ||
356 strPassword == "-" ||
357 profileManager->GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE ||
358 g_passwordManager.bMasterUser)
360 return true;
363 const std::string& strHeading = g_localizeStrings.Get(iHeading);
364 int iVerifyPasswordResult = VerifyPassword(btnType, strPassword, strHeading);
366 if (iVerifyPasswordResult == -1)
367 bCanceled = true;
369 return (iVerifyPasswordResult==0);
372 bool CGUIPassword::CheckSettingLevelLock(const SettingLevel& level, bool enforce /*=false*/)
374 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
376 LOCK_LEVEL::SETTINGS_LOCK lockLevel = profileManager->GetCurrentProfile().settingsLockLevel();
378 if (lockLevel == LOCK_LEVEL::NONE)
379 return true;
381 //check if we are already in settings and in an level that needs unlocking
382 int windowID = CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow();
383 if ((int)lockLevel-1 <= (short)CViewStateSettings::GetInstance().GetSettingLevel() &&
384 (windowID == WINDOW_SETTINGS_MENU ||
385 (windowID >= WINDOW_SCREEN_CALIBRATION &&
386 windowID <= WINDOW_SETTINGS_MYPVR)))
387 return true; //Already unlocked
389 else if (lockLevel == LOCK_LEVEL::ALL)
390 return IsMasterLockUnlocked(true);
391 else if ((int)lockLevel-1 <= (short)level)
393 if (enforce)
394 return IsMasterLockUnlocked(true);
395 else if (!IsMasterLockUnlocked(false))
397 //Current Setting level is higher than our permission... so lower the viewing level
398 SettingLevel newLevel = (SettingLevel)(short)(lockLevel-2);
399 CViewStateSettings::GetInstance().SetSettingLevel(newLevel);
402 return true;
406 bool IsSettingsWindow(int iWindowID)
408 return (iWindowID >= WINDOW_SCREEN_CALIBRATION && iWindowID <= WINDOW_SETTINGS_MYPVR)
409 || iWindowID == WINDOW_SKIN_SETTINGS;
412 bool CGUIPassword::CheckMenuLock(int iWindowID)
414 bool bCheckPW = false;
415 int iSwitch = iWindowID;
417 // check if a settings subcategory was called from other than settings window
418 if (IsSettingsWindow(iWindowID))
420 int iCWindowID = CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow();
421 if (iCWindowID != WINDOW_SETTINGS_MENU && !IsSettingsWindow(iCWindowID))
422 iSwitch = WINDOW_SETTINGS_MENU;
425 if (iWindowID == WINDOW_MUSIC_NAV)
427 if (CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow() == WINDOW_HOME)
428 iSwitch = WINDOW_MUSIC_NAV;
431 if (iWindowID == WINDOW_VIDEO_NAV)
433 if (CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow() == WINDOW_HOME)
434 iSwitch = WINDOW_VIDEO_NAV;
437 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
439 switch (iSwitch)
441 case WINDOW_SETTINGS_MENU: // Settings
442 return CheckSettingLevelLock(CViewStateSettings::GetInstance().GetSettingLevel());
443 break;
444 case WINDOW_ADDON_BROWSER: // Addons
445 bCheckPW = profileManager->GetCurrentProfile().addonmanagerLocked();
446 break;
447 case WINDOW_FILES: // Files
448 bCheckPW = profileManager->GetCurrentProfile().filesLocked();
449 break;
450 case WINDOW_PROGRAMS: // Programs
451 bCheckPW = profileManager->GetCurrentProfile().programsLocked();
452 break;
453 case WINDOW_MUSIC_NAV: // Music
454 bCheckPW = profileManager->GetCurrentProfile().musicLocked();
455 if (!bCheckPW && !m_strMediaSourcePath.empty()) // check mediasource by path
456 return g_passwordManager.IsMediaPathUnlocked(profileManager, "music");
457 break;
458 case WINDOW_VIDEO_NAV: // Video
459 bCheckPW = profileManager->GetCurrentProfile().videoLocked();
460 if (!bCheckPW && !m_strMediaSourcePath.empty()) // check mediasource by path
461 return g_passwordManager.IsMediaPathUnlocked(profileManager, "video");
462 break;
463 case WINDOW_PICTURES: // Pictures
464 bCheckPW = profileManager->GetCurrentProfile().picturesLocked();
465 break;
466 case WINDOW_GAMES: // Games
467 bCheckPW = profileManager->GetCurrentProfile().gamesLocked();
468 break;
469 case WINDOW_SETTINGS_PROFILES:
470 bCheckPW = true;
471 break;
472 default:
473 bCheckPW = false;
474 break;
476 if (bCheckPW)
477 return IsMasterLockUnlocked(true); //Now let's check the PW if we need!
478 else
479 return true;
482 bool CGUIPassword::IsVideoUnlocked()
484 const auto profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
486 const bool isLocked{profileManager->GetCurrentProfile().videoLocked()};
487 if (!isLocked && !m_strMediaSourcePath.empty()) // check mediasource by path
488 return g_passwordManager.IsMediaPathUnlocked(profileManager, "video");
490 if (isLocked)
491 return IsMasterLockUnlocked(true); //Now let's check the PW if we need!
492 return true;
495 bool CGUIPassword::IsMusicUnlocked()
497 const auto profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
499 const bool isLocked{profileManager->GetCurrentProfile().musicLocked()};
500 if (!isLocked && !m_strMediaSourcePath.empty()) // check mediasource by path
501 return g_passwordManager.IsMediaPathUnlocked(profileManager, "music");
503 if (isLocked)
504 return IsMasterLockUnlocked(true); //Now let's check the PW if we need!
505 return true;
508 bool CGUIPassword::LockSource(const std::string& strType, const std::string& strName, bool bState)
510 VECSOURCES* pShares = CMediaSourceSettings::GetInstance().GetSources(strType);
511 bool bResult = false;
512 for (IVECSOURCES it=pShares->begin();it != pShares->end();++it)
514 if (it->strName == strName)
516 if (it->m_iHasLock > LOCK_STATE_NO_LOCK)
518 it->m_iHasLock = bState ? LOCK_STATE_LOCKED : LOCK_STATE_LOCK_BUT_UNLOCKED;
519 bResult = true;
521 break;
524 CGUIMessage msg(GUI_MSG_NOTIFY_ALL,0,0,GUI_MSG_UPDATE_SOURCES);
525 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
527 return bResult;
530 void CGUIPassword::LockSources(bool lock)
532 // lock or unlock all sources (those with locks)
533 const char* strTypes[] = {"programs", "music", "video", "pictures", "files", "games"};
534 for (const char* const strType : strTypes)
536 VECSOURCES *shares = CMediaSourceSettings::GetInstance().GetSources(strType);
537 for (IVECSOURCES it=shares->begin();it != shares->end();++it)
538 if (it->m_iLockMode != LOCK_MODE_EVERYONE)
539 it->m_iHasLock = lock ? LOCK_STATE_LOCKED : LOCK_STATE_LOCK_BUT_UNLOCKED;
541 CGUIMessage msg(GUI_MSG_NOTIFY_ALL,0,0,GUI_MSG_UPDATE_SOURCES);
542 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
545 void CGUIPassword::RemoveSourceLocks()
547 // remove lock from all sources
548 const char* strTypes[] = {"programs", "music", "video", "pictures", "files", "games"};
549 for (const char* const strType : strTypes)
551 VECSOURCES *shares = CMediaSourceSettings::GetInstance().GetSources(strType);
552 for (IVECSOURCES it=shares->begin();it != shares->end();++it)
553 if (it->m_iLockMode != LOCK_MODE_EVERYONE) // remove old info
555 it->m_iHasLock = LOCK_STATE_NO_LOCK;
556 it->m_iLockMode = LOCK_MODE_EVERYONE;
558 // remove locks from xml
559 CMediaSourceSettings::GetInstance().UpdateSource(strType, it->strName, "lockmode", "0");
562 CMediaSourceSettings::GetInstance().Save();
563 CGUIMessage msg(GUI_MSG_NOTIFY_ALL,0,0, GUI_MSG_UPDATE_SOURCES);
564 CServiceBroker::GetGUI()->GetWindowManager().SendThreadMessage(msg);
567 bool CGUIPassword::IsDatabasePathUnlocked(const std::string& strPath, VECSOURCES& vecSources)
569 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
571 if (g_passwordManager.bMasterUser || profileManager->GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE)
572 return true;
574 // Don't check plugin paths as they don't have an entry in sources.xml
575 // Base locked status on videos being locked and being in the video
576 // navigation screen (must have passed lock by then)
577 CURL url{strPath};
578 if (url.IsProtocol("plugin"))
579 return !(profileManager->GetCurrentProfile().videoLocked() &&
580 CServiceBroker::GetGUI()->GetWindowManager().GetActiveWindow() != WINDOW_VIDEO_NAV);
582 // try to find the best matching source
583 bool bName = false;
584 int iIndex = CUtil::GetMatchingSource(strPath, vecSources, bName);
586 if (iIndex > -1 && iIndex < static_cast<int>(vecSources.size()))
587 if (vecSources[iIndex].m_iHasLock < LOCK_STATE_LOCKED)
588 return true;
590 return false;
593 bool CGUIPassword::IsMediaPathUnlocked(const std::shared_ptr<CProfileManager>& profileManager,
594 const std::string& strType) const
596 if (!StringUtils::StartsWithNoCase(m_strMediaSourcePath, "root") &&
597 !StringUtils::StartsWithNoCase(m_strMediaSourcePath, "library://"))
599 if (!g_passwordManager.bMasterUser &&
600 profileManager->GetMasterProfile().getLockMode() != LOCK_MODE_EVERYONE)
602 VECSOURCES& vecSources = *CMediaSourceSettings::GetInstance().GetSources(strType);
603 bool bName = false;
604 int iIndex = CUtil::GetMatchingSource(m_strMediaSourcePath, vecSources, bName);
605 if (iIndex > -1 && iIndex < static_cast<int>(vecSources.size()))
607 return g_passwordManager.IsItemUnlocked(&vecSources[iIndex], strType);
612 return true;
615 bool CGUIPassword::IsMediaFileUnlocked(const std::string& type, const std::string& file) const
617 std::vector<CMediaSource>* vecSources = CMediaSourceSettings::GetInstance().GetSources(type);
619 if (!vecSources)
621 CLog::Log(LOGERROR,
622 "{}: CMediaSourceSettings::GetInstance().GetSources(\"{}\") returned nullptr.",
623 __func__, type);
624 return true;
627 // try to find the best matching source for this file
629 bool isSourceName{false};
630 const std::string fileBasePath = URIUtils::GetBasePath(file);
632 int iIndex = CUtil::GetMatchingSource(fileBasePath, *vecSources, isSourceName);
634 if (iIndex > -1 && iIndex < static_cast<int>(vecSources->size()))
635 return (*vecSources)[iIndex].m_iHasLock < LOCK_STATE_LOCKED;
637 return true;
640 void CGUIPassword::OnSettingAction(const std::shared_ptr<const CSetting>& setting)
642 if (setting == NULL)
643 return;
645 const std::string &settingId = setting->GetId();
646 if (settingId == CSettings::SETTING_MASTERLOCK_LOCKCODE)
647 SetMasterLockMode();
650 int CGUIPassword::VerifyPassword(LockType btnType, const std::string& strPassword, const std::string& strHeading)
652 int iVerifyPasswordResult;
653 switch (btnType)
655 case LOCK_MODE_NUMERIC:
656 iVerifyPasswordResult = CGUIDialogNumeric::ShowAndVerifyPassword(const_cast<std::string&>(strPassword), strHeading, 0);
657 break;
658 case LOCK_MODE_GAMEPAD:
659 iVerifyPasswordResult = CGUIDialogGamepad::ShowAndVerifyPassword(const_cast<std::string&>(strPassword), strHeading, 0);
660 break;
661 case LOCK_MODE_QWERTY:
662 iVerifyPasswordResult = CGUIKeyboardFactory::ShowAndVerifyPassword(const_cast<std::string&>(strPassword), strHeading, 0);
663 break;
664 default: // must not be supported, treat as unlocked
665 iVerifyPasswordResult = 0;
666 break;
669 return iVerifyPasswordResult;