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 "ProfileBuiltins.h"
10 
11 #include "GUIPassword.h"
12 #include "GUIUserMessages.h"
13 #include "ServiceBroker.h"
14 #include "Util.h"
15 #include "addons/AddonManager.h"
16 #include "dialogs/GUIDialogKaiToast.h"
17 #include "guilib/GUIComponent.h"
18 #include "guilib/GUIWindowManager.h"
19 #include "guilib/LocalizeStrings.h"
20 #include "messaging/ApplicationMessenger.h"
21 #include "profiles/ProfileManager.h"
22 #include "settings/SettingsComponent.h"
23 #include "utils/StringUtils.h"
24 
25 using namespace KODI::MESSAGING;
26 
27 /*! \brief Load a profile.
28  *  \param params The parameters.
29  *  \details params[0] = The profile name.
30  *           params[1] = "prompt" to allow unlocking dialogs (optional)
31  */
LoadProfile(const std::vector<std::string> & params)32 static int LoadProfile(const std::vector<std::string>& params)
33 {
34   const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
35 
36   int index = profileManager->GetProfileIndex(params[0]);
37   bool prompt = (params.size() == 2 && StringUtils::EqualsNoCase(params[1], "prompt"));
38   bool bCanceled;
39   if (index >= 0
40       && (profileManager->GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE
41         || g_passwordManager.IsProfileLockUnlocked(index,bCanceled,prompt)))
42   {
43     CApplicationMessenger::GetInstance().PostMsg(TMSG_LOADPROFILE, index);
44   }
45 
46   return 0;
47 }
48 
49 /*! \brief Log off currently signed in profile.
50  *  \param params (ignored)
51  */
LogOff(const std::vector<std::string> & params)52 static int LogOff(const std::vector<std::string>& params)
53 {
54   const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
55   profileManager->LogOff();
56 
57   return 0;
58 }
59 
60 /*! \brief Toggle master mode.
61  *  \param params (ignored)
62  */
MasterMode(const std::vector<std::string> & params)63 static int MasterMode(const std::vector<std::string>& params)
64 {
65   if (g_passwordManager.bMasterUser)
66   {
67     g_passwordManager.bMasterUser = false;
68     g_passwordManager.LockSources(true);
69     CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Warning, g_localizeStrings.Get(20052),g_localizeStrings.Get(20053));
70   }
71   else if (g_passwordManager.IsMasterLockUnlocked(true))
72   {
73     g_passwordManager.LockSources(false);
74     g_passwordManager.bMasterUser = true;
75     CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Warning, g_localizeStrings.Get(20052),g_localizeStrings.Get(20054));
76   }
77 
78   CUtil::DeleteVideoDatabaseDirectoryCache();
79   CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE);
80   CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
81 
82   return 0;
83 }
84 
85 
86 // Note: For new Texts with comma add a "\" before!!! Is used for table text.
87 //
88 /// \page page_List_of_built_in_functions
89 /// \section built_in_functions_13 Profile built-in's
90 ///
91 /// -----------------------------------------------------------------------------
92 ///
93 /// \table_start
94 ///   \table_h2_l{
95 ///     Function,
96 ///     Description }
97 ///   \table_row2_l{
98 ///     <b>`LoadProfile(profilename\,[prompt])`</b>
99 ///     ,
100 ///     Load the specified profile. If prompt is not specified\, and a password
101 ///     would be required for the requested profile\, this command will silently
102 ///     fail. If prompt is specified and a password is required\, a password
103 ///     dialog will be shown.
104 ///     @param[in] profilename           The profile name.
105 ///     @param[in] prompt                Add "prompt" to allow unlocking dialogs (optional)
106 ///   }
107 ///   \table_row2_l{
108 ///     <b>`Mastermode`</b>
109 ///     ,
110 ///     Runs Kodi in master mode
111 ///   }
112 ///   \table_row2_l{
113 ///     <b>`System.LogOff`</b>
114 ///     ,
115 ///     Log off current user.
116 ///   }
117 /// \table_end
118 ///
119 
GetOperations() const120 CBuiltins::CommandMap CProfileBuiltins::GetOperations() const
121 {
122   return {
123            {"loadprofile",   {"Load the specified profile (note; if locks are active it won't work)", 1, LoadProfile}},
124            {"mastermode",    {"Control master mode", 0, MasterMode}},
125            {"system.logoff", {"Log off current user", 0, LogOff}}
126          };
127 }
128