[release] version bump to 13.0 beta1
[vuplus_xbmc] / xbmc / profiles / windows / GUIWindowSettingsProfile.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
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"
36 #include "FileItem.h"
37 #include "guilib/Key.h"
38 #include "guilib/LocalizeStrings.h"
39
40 using namespace XFILE;
41
42 #define CONTROL_PROFILES 2
43 #define CONTROL_LASTLOADED_PROFILE 3
44 #define CONTROL_LOGINSCREEN 4
45 #define CONTROL_AUTOLOGIN 5
46
47 CGUIWindowSettingsProfile::CGUIWindowSettingsProfile(void)
48     : CGUIWindow(WINDOW_SETTINGS_PROFILES, "SettingsProfile.xml")
49 {
50   m_listItems = new CFileItemList;
51   m_loadType = KEEP_IN_MEMORY;
52 }
53
54 CGUIWindowSettingsProfile::~CGUIWindowSettingsProfile(void)
55 {
56   delete m_listItems;
57 }
58
59 int CGUIWindowSettingsProfile::GetSelectedItem()
60 {
61   CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), CONTROL_PROFILES);
62   g_windowManager.SendMessage(msg);
63
64   return msg.GetParam1();
65 }
66
67 void CGUIWindowSettingsProfile::OnPopupMenu(int iItem)
68 {
69   if (iItem == (int)CProfilesManager::Get().GetNumberOfProfiles())
70     return;
71
72   // popup the context menu
73   CContextButtons choices;
74   choices.Add(1, 20092); // Load profile
75   if (iItem > 0)
76     choices.Add(2, 117); // Delete
77
78   int choice = CGUIDialogContextMenu::ShowAndGetChoice(choices);
79   if (choice == 1)
80   {
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);
88     return;
89   }
90
91   if (choice == 2)
92   {
93     if (CProfilesManager::Get().DeleteProfile(iItem))
94       iItem--;
95   }
96
97   LoadList();
98   CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(),CONTROL_PROFILES,iItem);
99   OnMessage(msg);
100 }
101
102 bool CGUIWindowSettingsProfile::OnMessage(CGUIMessage& message)
103 {
104   switch ( message.GetMessage() )
105   {
106   case GUI_MSG_WINDOW_DEINIT:
107     {
108       CGUIWindow::OnMessage(message);
109       ClearListItems();
110       return true;
111     }
112     break;
113
114   case GUI_MSG_CLICKED:
115     {
116       int iControl = message.GetSenderId();
117       if (iControl == CONTROL_PROFILES)
118       {
119         int iAction = message.GetParam1();
120         if (
121           iAction == ACTION_SELECT_ITEM ||
122           iAction == ACTION_MOUSE_LEFT_CLICK ||
123           iAction == ACTION_CONTEXT_MENU ||
124           iAction == ACTION_MOUSE_RIGHT_CLICK
125         )
126         {
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)
131           {
132             //contextmenu
133             if (iItem <= (int)CProfilesManager::Get().GetNumberOfProfiles() - 1)
134             {
135               OnPopupMenu(iItem);
136             }
137             return true;
138           }
139           else if (iItem < (int)CProfilesManager::Get().GetNumberOfProfiles())
140           {
141             if (CGUIDialogProfileSettings::ShowForProfile(iItem))
142             {
143               LoadList();
144               CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(), 2,iItem);
145               g_windowManager.SendMessage(msg);
146
147               return true;
148             }
149
150             return false;
151           }
152           else if (iItem > (int)CProfilesManager::Get().GetNumberOfProfiles() - 1)
153           {
154             CDirectory::Create(URIUtils::AddFileToFolder(CProfilesManager::Get().GetUserDataFolder(),"profiles"));
155             if (CGUIDialogProfileSettings::ShowForProfile(CProfilesManager::Get().GetNumberOfProfiles()))
156             {
157               LoadList();
158               CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(), 2,iItem);
159               g_windowManager.SendMessage(msg);
160               return true;
161             }
162
163             return false;
164           }
165         }
166       }
167       else if (iControl == CONTROL_LOGINSCREEN)
168       {
169         CProfilesManager::Get().ToggleLoginScreen();
170         CProfilesManager::Get().Save();
171         return true;
172       }
173       else if (iControl == CONTROL_AUTOLOGIN)
174       {
175         int currentId = CProfilesManager::Get().GetAutoLoginProfileId();
176         int profileId;
177         if (GetAutoLoginProfileChoice(profileId) && (currentId != profileId))
178         {
179           CProfilesManager::Get().SetAutoLoginProfileId(profileId);
180           CProfilesManager::Get().Save();
181         }
182         return true;
183       }
184     }
185     break;
186   }
187
188   return CGUIWindow::OnMessage(message);
189 }
190
191 void CGUIWindowSettingsProfile::LoadList()
192 {
193   ClearListItems();
194
195   for (unsigned int i = 0; i < CProfilesManager::Get().GetNumberOfProfiles(); i++)
196   {
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);
203   }
204   {
205     CFileItemPtr item(new CFileItem(g_localizeStrings.Get(20058)));
206     m_listItems->Add(item);
207   }
208   CGUIMessage msg(GUI_MSG_LABEL_BIND, GetID(), CONTROL_PROFILES, 0, 0, m_listItems);
209   OnMessage(msg);
210
211   if (CProfilesManager::Get().UsingLoginScreen())
212   {
213     CONTROL_SELECT(CONTROL_LOGINSCREEN);
214   }
215   else
216   {
217     CONTROL_DESELECT(CONTROL_LOGINSCREEN);
218   }
219 }
220
221 void CGUIWindowSettingsProfile::ClearListItems()
222 {
223   CGUIMessage msg(GUI_MSG_LABEL_RESET, GetID(), CONTROL_PROFILES);
224   g_windowManager.SendMessage(msg);
225
226   m_listItems->Clear();
227 }
228
229 void CGUIWindowSettingsProfile::OnInitWindow()
230 {
231   LoadList();
232   CGUIWindow::OnInitWindow();
233 }
234
235 bool CGUIWindowSettingsProfile::GetAutoLoginProfileChoice(int &iProfile)
236 {
237   CGUIDialogSelect *dialog = (CGUIDialogSelect*)g_windowManager.GetWindow(WINDOW_DIALOG_SELECT);
238   if (!dialog) return false;
239
240   // add items
241   // "Last used profile" option comes first, so up indices by 1
242   int autoLoginProfileId = CProfilesManager::Get().GetAutoLoginProfileId() + 1;
243   CFileItemList items;
244   CFileItemPtr item(new CFileItem());
245   item->SetLabel(g_localizeStrings.Get(37014)); // Last used profile
246   item->SetIconImage("unknown-user.png");
247   items.Add(item);
248
249   for (unsigned int i = 0; i < CProfilesManager::Get().GetNumberOfProfiles(); i++)
250   {
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();
256     if (thumb.empty())
257       thumb = "unknown-user.png";
258     item->SetIconImage(thumb);
259     items.Add(item);
260   }
261
262   dialog->SetHeading(20093); // Profile name
263   dialog->Reset();
264   dialog->SetUseDetails(true);
265   dialog->EnableButton(true, 222); // Cancel
266   dialog->SetItems(&items);
267   dialog->SetSelected(autoLoginProfileId);
268   dialog->DoModal();
269
270   if (dialog->IsButtonPressed() || dialog->GetSelectedLabel() < 0)
271     return false; // user cancelled
272   iProfile = dialog->GetSelectedLabel() - 1;
273
274   return true;
275 }