Debian Codegraph @

xbmc/profiles/windows/GUIWindowSettingsProfile.cpp

raw
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 "GUIWindowSettingsProfile.h"
10
11 #include "FileItem.h"
12 #include "ServiceBroker.h"
13 #include "dialogs/GUIDialogContextMenu.h"
14 #include "dialogs/GUIDialogSelect.h"
15 #include "filesystem/Directory.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/ActionIDs.h"
21 #include "messaging/ApplicationMessenger.h"
22 #include "profiles/Profile.h"
23 #include "profiles/ProfileManager.h"
24 #include "profiles/dialogs/GUIDialogProfileSettings.h"
25 #include "settings/SettingsComponent.h"
26 #include "utils/URIUtils.h"
27 #include "utils/Variant.h"
28 #include "windows/GUIWindowFileManager.h"
29
30 using namespace XFILE;
31
32 #define CONTROL_PROFILES 2
33 #define CONTROL_LOGINSCREEN 4
34 #define CONTROL_AUTOLOGIN 5
35
36 CGUIWindowSettingsProfile::CGUIWindowSettingsProfile(void)
37 : CGUIWindow(WINDOW_SETTINGS_PROFILES, "SettingsProfile.xml")
38 {
39 m_listItems = new CFileItemList;
40 m_loadType = KEEP_IN_MEMORY;
41 }
42
43 CGUIWindowSettingsProfile::~CGUIWindowSettingsProfile(void)
44 {
45 delete m_listItems;
46 }
47
48 int CGUIWindowSettingsProfile::GetSelectedItem()
49 {
50 CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), CONTROL_PROFILES);
51 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
52
53 return msg.GetParam1();
54 }
55
56 void CGUIWindowSettingsProfile::OnPopupMenu(int iItem)
57 {
58 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
59
60 if (iItem == (int)profileManager->GetNumberOfProfiles())
61 return;
62
63 // popup the context menu
64 CContextButtons choices;
65 choices.Add(1, 20092); // Load profile
66 if (iItem > 0)
67 choices.Add(2, 117); // Delete
68
69 int choice = CGUIDialogContextMenu::ShowAndGetChoice(choices);
70 if (choice == 1)
71 {
72 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_LOADPROFILE, iItem);
73 return;
74 }
75
76 if (choice == 2)
77 {
78 if (profileManager->DeleteProfile(iItem))
79 iItem--;
80 }
81
82 LoadList();
83 CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(),CONTROL_PROFILES,iItem);
84 OnMessage(msg);
85 }
86
87 bool CGUIWindowSettingsProfile::OnMessage(CGUIMessage& message)
88 {
89 switch ( message.GetMessage() )
90 {
91 case GUI_MSG_WINDOW_DEINIT:
92 {
93 CGUIWindow::OnMessage(message);
94 ClearListItems();
95 return true;
96 }
97 break;
98
99 case GUI_MSG_CLICKED:
100 {
101 int iControl = message.GetSenderId();
102 if (iControl == CONTROL_PROFILES)
103 {
104 int iAction = message.GetParam1();
105 if (
106 iAction == ACTION_SELECT_ITEM ||
107 iAction == ACTION_MOUSE_LEFT_CLICK ||
108 iAction == ACTION_CONTEXT_MENU ||
109 iAction == ACTION_MOUSE_RIGHT_CLICK
110 )
111 {
112 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
113
114 CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), CONTROL_PROFILES);
115 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
116 int iItem = msg.GetParam1();
117 if (iAction == ACTION_CONTEXT_MENU || iAction == ACTION_MOUSE_RIGHT_CLICK)
118 {
119 //contextmenu
120 if (iItem <= static_cast<int>(profileManager->GetNumberOfProfiles()) - 1)
121 {
122 OnPopupMenu(iItem);
123 }
124 return true;
125 }
126 else if (iItem < static_cast<int>(profileManager->GetNumberOfProfiles()))
127 {
128 if (CGUIDialogProfileSettings::ShowForProfile(iItem))
129 {
130 LoadList();
131 CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(), 2,iItem);
132 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
133
134 return true;
135 }
136
137 return false;
138 }
139 else if (iItem > static_cast<int>(profileManager->GetNumberOfProfiles()) - 1)
140 {
141 CDirectory::Create(URIUtils::AddFileToFolder(profileManager->GetUserDataFolder(),"profiles"));
142 if (CGUIDialogProfileSettings::ShowForProfile(profileManager->GetNumberOfProfiles()))
143 {
144 LoadList();
145 CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(), 2,iItem);
146 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
147 return true;
148 }
149
150 return false;
151 }
152 }
153 }
154 else if (iControl == CONTROL_LOGINSCREEN)
155 {
156 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
157
158 profileManager->ToggleLoginScreen();
159 profileManager->Save();
160 return true;
161 }
162 else if (iControl == CONTROL_AUTOLOGIN)
163 {
164 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
165
166 int currentId = profileManager->GetAutoLoginProfileId();
167 int profileId;
168 if (GetAutoLoginProfileChoice(profileId) && (currentId != profileId))
169 {
170 profileManager->SetAutoLoginProfileId(profileId);
171 profileManager->Save();
172 }
173 return true;
174 }
175 }
176 break;
177 }
178
179 return CGUIWindow::OnMessage(message);
180 }
181
182 void CGUIWindowSettingsProfile::LoadList()
183 {
184 ClearListItems();
185
186 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
187
188 for (unsigned int i = 0; i < profileManager->GetNumberOfProfiles(); i++)
189 {
190 const CProfile *profile = profileManager->GetProfile(i);
191 CFileItemPtr item(new CFileItem(profile->getName()));
192 item->SetLabel2(profile->getDate());
193 item->SetArt("thumb", profile->getThumb());
194 item->SetOverlayImage(profile->getLockMode() == LOCK_MODE_EVERYONE ? CGUIListItem::ICON_OVERLAY_NONE : CGUIListItem::ICON_OVERLAY_LOCKED);
195 m_listItems->Add(item);
196 }
197 {
198 CFileItemPtr item(new CFileItem(g_localizeStrings.Get(20058)));
199 m_listItems->Add(item);
200 }
201 CGUIMessage msg(GUI_MSG_LABEL_BIND, GetID(), CONTROL_PROFILES, 0, 0, m_listItems);
202 OnMessage(msg);
203
204 if (profileManager->UsingLoginScreen())
205 {
206 CONTROL_SELECT(CONTROL_LOGINSCREEN);
207 }
208 else
209 {
210 CONTROL_DESELECT(CONTROL_LOGINSCREEN);
211 }
212 }
213
214 void CGUIWindowSettingsProfile::ClearListItems()
215 {
216 CGUIMessage msg(GUI_MSG_LABEL_RESET, GetID(), CONTROL_PROFILES);
217 CServiceBroker::GetGUI()->GetWindowManager().SendMessage(msg);
218
219 m_listItems->Clear();
220 }
221
222 void CGUIWindowSettingsProfile::OnInitWindow()
223 {
224 LoadList();
225 CGUIWindow::OnInitWindow();
226 }
227
228 bool CGUIWindowSettingsProfile::GetAutoLoginProfileChoice(int &iProfile)
229 {
230 CGUIDialogSelect *dialog = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogSelect>(WINDOW_DIALOG_SELECT);
231 if (!dialog) return false;
232
233 const std::shared_ptr<CProfileManager> profileManager = CServiceBroker::GetSettingsComponent()->GetProfileManager();
234
235 // add items
236 // "Last used profile" option comes first, so up indices by 1
237 int autoLoginProfileId = profileManager->GetAutoLoginProfileId() + 1;
238 CFileItemList items;
239 CFileItemPtr item(new CFileItem());
240 item->SetLabel(g_localizeStrings.Get(37014)); // Last used profile
241 item->SetArt("icon", "DefaultUser.png");
242 items.Add(item);
243
244 for (unsigned int i = 0; i < profileManager->GetNumberOfProfiles(); i++)
245 {
246 const CProfile *profile = profileManager->GetProfile(i);
247 const std::string& locked = g_localizeStrings.Get(profile->getLockMode() > 0 ? 20166 : 20165);
248 CFileItemPtr item(new CFileItem(profile->getName()));
249 item->SetLabel2(locked); // lock setting
250 std::string thumb = profile->getThumb();
251 if (thumb.empty())
252 thumb = "DefaultUser.png";
253 item->SetArt("icon", thumb);
254 items.Add(item);
255 }
256
257 dialog->SetHeading(CVariant{20093}); // Profile name
258 dialog->Reset();
259 dialog->SetUseDetails(true);
260 dialog->SetItems(items);
261 dialog->SetSelected(autoLoginProfileId);
262 dialog->Open();
263
264 if (dialog->IsButtonPressed() || dialog->GetSelectedItem() < 0)
265 return false; // user cancelled
266 iProfile = dialog->GetSelectedItem() - 1;
267
268 return true;
269 }