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 "GUIWindowSettingsProfile.h"
22 #include "windows/GUIWindowFileManager.h"
23 #include "profiles/Profile.h"
24 #include "profiles/ProfilesManager.h"
25 #include "Application.h"
26 #include "dialogs/GUIDialogContextMenu.h"
27 #include "dialogs/GUIDialogSelect.h"
28 #include "profiles/dialogs/GUIDialogProfileSettings.h"
29 #include "network/Network.h"
30 #include "utils/URIUtils.h"
31 #include "utils/Weather.h"
32 #include "GUIPassword.h"
33 #include "windows/GUIWindowLoginScreen.h"
34 #include "guilib/GUIWindowManager.h"
35 #include "filesystem/Directory.h"
37 #include "guilib/Key.h"
38 #include "guilib/LocalizeStrings.h"
40 using namespace XFILE;
42 #define CONTROL_PROFILES 2
43 #define CONTROL_LASTLOADED_PROFILE 3
44 #define CONTROL_LOGINSCREEN 4
45 #define CONTROL_AUTOLOGIN 5
47 CGUIWindowSettingsProfile::CGUIWindowSettingsProfile(void)
48 : CGUIWindow(WINDOW_SETTINGS_PROFILES, "SettingsProfile.xml")
50 m_listItems = new CFileItemList;
51 m_loadType = KEEP_IN_MEMORY;
54 CGUIWindowSettingsProfile::~CGUIWindowSettingsProfile(void)
59 int CGUIWindowSettingsProfile::GetSelectedItem()
61 CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), CONTROL_PROFILES);
62 g_windowManager.SendMessage(msg);
64 return msg.GetParam1();
67 void CGUIWindowSettingsProfile::OnPopupMenu(int iItem)
69 if (iItem == (int)CProfilesManager::Get().GetNumberOfProfiles())
72 // popup the context menu
73 CContextButtons choices;
74 choices.Add(1, 20092); // Load profile
76 choices.Add(2, 117); // Delete
78 int choice = CGUIDialogContextMenu::ShowAndGetChoice(choices);
81 unsigned iCtrlID = GetFocusedControlID();
82 g_application.StopPlaying();
83 CGUIMessage msg2(GUI_MSG_ITEM_SELECTED, g_windowManager.GetActiveWindow(), iCtrlID);
84 g_windowManager.SendMessage(msg2);
85 g_application.getNetwork().NetworkMessage(CNetwork::SERVICES_DOWN,1);
86 CProfilesManager::Get().LoadMasterProfileForLogin();
87 CGUIWindowLoginScreen::LoadProfile(iItem);
93 if (CProfilesManager::Get().DeleteProfile(iItem))
98 CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(),CONTROL_PROFILES,iItem);
102 bool CGUIWindowSettingsProfile::OnMessage(CGUIMessage& message)
104 switch ( message.GetMessage() )
106 case GUI_MSG_WINDOW_DEINIT:
108 CGUIWindow::OnMessage(message);
114 case GUI_MSG_CLICKED:
116 int iControl = message.GetSenderId();
117 if (iControl == CONTROL_PROFILES)
119 int iAction = message.GetParam1();
121 iAction == ACTION_SELECT_ITEM ||
122 iAction == ACTION_MOUSE_LEFT_CLICK ||
123 iAction == ACTION_CONTEXT_MENU ||
124 iAction == ACTION_MOUSE_RIGHT_CLICK
127 CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), CONTROL_PROFILES);
128 g_windowManager.SendMessage(msg);
129 int iItem = msg.GetParam1();
130 if (iAction == ACTION_CONTEXT_MENU || iAction == ACTION_MOUSE_RIGHT_CLICK)
133 if (iItem <= (int)CProfilesManager::Get().GetNumberOfProfiles() - 1)
139 else if (iItem < (int)CProfilesManager::Get().GetNumberOfProfiles())
141 if (CGUIDialogProfileSettings::ShowForProfile(iItem))
144 CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(), 2,iItem);
145 g_windowManager.SendMessage(msg);
152 else if (iItem > (int)CProfilesManager::Get().GetNumberOfProfiles() - 1)
154 CDirectory::Create(URIUtils::AddFileToFolder(CProfilesManager::Get().GetUserDataFolder(),"profiles"));
155 if (CGUIDialogProfileSettings::ShowForProfile(CProfilesManager::Get().GetNumberOfProfiles()))
158 CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(), 2,iItem);
159 g_windowManager.SendMessage(msg);
167 else if (iControl == CONTROL_LOGINSCREEN)
169 CProfilesManager::Get().ToggleLoginScreen();
170 CProfilesManager::Get().Save();
173 else if (iControl == CONTROL_AUTOLOGIN)
175 int currentId = CProfilesManager::Get().GetAutoLoginProfileId();
177 if (GetAutoLoginProfileChoice(profileId) && (currentId != profileId))
179 CProfilesManager::Get().SetAutoLoginProfileId(profileId);
180 CProfilesManager::Get().Save();
188 return CGUIWindow::OnMessage(message);
191 void CGUIWindowSettingsProfile::LoadList()
195 for (unsigned int i = 0; i < CProfilesManager::Get().GetNumberOfProfiles(); i++)
197 const CProfile *profile = CProfilesManager::Get().GetProfile(i);
198 CFileItemPtr item(new CFileItem(profile->getName()));
199 item->SetLabel2(profile->getDate());
200 item->SetArt("thumb", profile->getThumb());
201 item->SetOverlayImage(profile->getLockMode() == LOCK_MODE_EVERYONE ? CGUIListItem::ICON_OVERLAY_NONE : CGUIListItem::ICON_OVERLAY_LOCKED);
202 m_listItems->Add(item);
205 CFileItemPtr item(new CFileItem(g_localizeStrings.Get(20058)));
206 m_listItems->Add(item);
208 CGUIMessage msg(GUI_MSG_LABEL_BIND, GetID(), CONTROL_PROFILES, 0, 0, m_listItems);
211 if (CProfilesManager::Get().UsingLoginScreen())
213 CONTROL_SELECT(CONTROL_LOGINSCREEN);
217 CONTROL_DESELECT(CONTROL_LOGINSCREEN);
221 void CGUIWindowSettingsProfile::ClearListItems()
223 CGUIMessage msg(GUI_MSG_LABEL_RESET, GetID(), CONTROL_PROFILES);
224 g_windowManager.SendMessage(msg);
226 m_listItems->Clear();
229 void CGUIWindowSettingsProfile::OnInitWindow()
232 CGUIWindow::OnInitWindow();
235 bool CGUIWindowSettingsProfile::GetAutoLoginProfileChoice(int &iProfile)
237 CGUIDialogSelect *dialog = (CGUIDialogSelect*)g_windowManager.GetWindow(WINDOW_DIALOG_SELECT);
238 if (!dialog) return false;
241 // "Last used profile" option comes first, so up indices by 1
242 int autoLoginProfileId = CProfilesManager::Get().GetAutoLoginProfileId() + 1;
244 CFileItemPtr item(new CFileItem());
245 item->SetLabel(g_localizeStrings.Get(37014)); // Last used profile
246 item->SetIconImage("unknown-user.png");
249 for (unsigned int i = 0; i < CProfilesManager::Get().GetNumberOfProfiles(); i++)
251 const CProfile *profile = CProfilesManager::Get().GetProfile(i);
252 CStdString locked = g_localizeStrings.Get(profile->getLockMode() > 0 ? 20166 : 20165);
253 CFileItemPtr item(new CFileItem(profile->getName()));
254 item->SetProperty("Addon.Summary", locked); // lock setting
255 CStdString thumb = profile->getThumb();
257 thumb = "unknown-user.png";
258 item->SetIconImage(thumb);
262 dialog->SetHeading(20093); // Profile name
264 dialog->SetUseDetails(true);
265 dialog->EnableButton(true, 222); // Cancel
266 dialog->SetItems(&items);
267 dialog->SetSelected(autoLoginProfileId);
270 if (dialog->IsButtonPressed() || dialog->GetSelectedLabel() < 0)
271 return false; // user cancelled
272 iProfile = dialog->GetSelectedLabel() - 1;