[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / settings / GUIWindowSettingsProfile.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.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 "Profile.h"
24 #include "Application.h"
25 #include "dialogs/GUIDialogContextMenu.h"
26 #include "GUIDialogProfileSettings.h"
27 #include "network/Network.h"
28 #include "utils/URIUtils.h"
29 #include "utils/Weather.h"
30 #include "GUIPassword.h"
31 #include "windows/GUIWindowLoginScreen.h"
32 #include "guilib/GUIWindowManager.h"
33 #include "filesystem/Directory.h"
34 #include "FileItem.h"
35 #include "Settings.h"
36 #include "guilib/LocalizeStrings.h"
37
38 using namespace XFILE;
39
40 #define CONTROL_PROFILES 2
41 #define CONTROL_LASTLOADED_PROFILE 3
42 #define CONTROL_LOGINSCREEN 4
43
44 CGUIWindowSettingsProfile::CGUIWindowSettingsProfile(void)
45     : CGUIWindow(WINDOW_SETTINGS_PROFILES, "SettingsProfile.xml")
46 {
47   m_listItems = new CFileItemList;
48   m_loadType = KEEP_IN_MEMORY;
49 }
50
51 CGUIWindowSettingsProfile::~CGUIWindowSettingsProfile(void)
52 {
53   delete m_listItems;
54 }
55
56 int CGUIWindowSettingsProfile::GetSelectedItem()
57 {
58   CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), CONTROL_PROFILES);
59   g_windowManager.SendMessage(msg);
60
61   return msg.GetParam1();
62 }
63
64 void CGUIWindowSettingsProfile::OnPopupMenu(int iItem)
65 {
66   if (iItem == (int)g_settings.GetNumProfiles())
67     return;
68
69   // popup the context menu
70   CContextButtons choices;
71   choices.Add(1, 20092); // Load profile
72   if (iItem > 0)
73     choices.Add(2, 117); // Delete
74
75   int choice = CGUIDialogContextMenu::ShowAndGetChoice(choices);
76   if (choice == 1)
77   {
78     unsigned iCtrlID = GetFocusedControlID();
79     g_application.StopPlaying();
80     CGUIMessage msg2(GUI_MSG_ITEM_SELECTED, g_windowManager.GetActiveWindow(), iCtrlID);
81     g_windowManager.SendMessage(msg2);
82     g_application.getNetwork().NetworkMessage(CNetwork::SERVICES_DOWN,1);
83     g_settings.LoadMasterForLogin();
84     CGUIWindowLoginScreen::LoadProfile(iItem);
85     return;
86   }
87
88   if (choice == 2)
89   {
90     if (g_settings.DeleteProfile(iItem))
91       iItem--;
92   }
93
94   LoadList();
95   CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(),CONTROL_PROFILES,iItem);
96   OnMessage(msg);
97 }
98
99 bool CGUIWindowSettingsProfile::OnMessage(CGUIMessage& message)
100 {
101   switch ( message.GetMessage() )
102   {
103   case GUI_MSG_WINDOW_DEINIT:
104     {
105       CGUIWindow::OnMessage(message);
106       ClearListItems();
107       return true;
108     }
109     break;
110
111   case GUI_MSG_CLICKED:
112     {
113       int iControl = message.GetSenderId();
114       if (iControl == CONTROL_PROFILES)
115       {
116         int iAction = message.GetParam1();
117         if (
118           iAction == ACTION_SELECT_ITEM ||
119           iAction == ACTION_MOUSE_LEFT_CLICK ||
120           iAction == ACTION_CONTEXT_MENU ||
121           iAction == ACTION_MOUSE_RIGHT_CLICK
122         )
123         {
124           CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), CONTROL_PROFILES);
125           g_windowManager.SendMessage(msg);
126           int iItem = msg.GetParam1();
127           if (iAction == ACTION_CONTEXT_MENU || iAction == ACTION_MOUSE_RIGHT_CLICK)
128           {
129             //contextmenu
130             if (iItem <= (int)g_settings.GetNumProfiles() - 1)
131             {
132               OnPopupMenu(iItem);
133             }
134             return true;
135           }
136           else if (iItem < (int)g_settings.GetNumProfiles())
137           {
138             if (CGUIDialogProfileSettings::ShowForProfile(iItem))
139             {
140               LoadList();
141               CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(), 2,iItem);
142               g_windowManager.SendMessage(msg);
143
144               return true;
145             }
146
147             return false;
148           }
149           else if (iItem > (int)g_settings.GetNumProfiles() - 1)
150           {
151             CDirectory::Create(URIUtils::AddFileToFolder(g_settings.GetUserDataFolder(),"profiles"));
152             if (CGUIDialogProfileSettings::ShowForProfile(g_settings.GetNumProfiles()))
153             {
154               LoadList();
155               CGUIMessage msg(GUI_MSG_ITEM_SELECT, GetID(), 2,iItem);
156               g_windowManager.SendMessage(msg);
157               return true;
158             }
159
160             return false;
161           }
162         }
163       }
164       else if (iControl == CONTROL_LOGINSCREEN)
165       {
166         g_settings.ToggleLoginScreen();
167         g_settings.SaveProfiles(PROFILES_FILE);
168         return true;
169       }
170     }
171     break;
172   }
173
174   return CGUIWindow::OnMessage(message);
175 }
176
177 void CGUIWindowSettingsProfile::LoadList()
178 {
179   ClearListItems();
180
181   for (unsigned int i = 0; i < g_settings.GetNumProfiles(); i++)
182   {
183     const CProfile *profile = g_settings.GetProfile(i);
184     CFileItemPtr item(new CFileItem(profile->getName()));
185     item->SetLabel2(profile->getDate());
186     item->SetArt("thumb", profile->getThumb());
187     item->SetOverlayImage(profile->getLockMode() == LOCK_MODE_EVERYONE ? CGUIListItem::ICON_OVERLAY_NONE : CGUIListItem::ICON_OVERLAY_LOCKED);
188     m_listItems->Add(item);
189   }
190   {
191     CFileItemPtr item(new CFileItem(g_localizeStrings.Get(20058)));
192     m_listItems->Add(item);
193   }
194   CGUIMessage msg(GUI_MSG_LABEL_BIND, GetID(), CONTROL_PROFILES, 0, 0, m_listItems);
195   OnMessage(msg);
196
197   if (g_settings.UsingLoginScreen())
198   {
199     CONTROL_SELECT(CONTROL_LOGINSCREEN);
200   }
201   else
202   {
203     CONTROL_DESELECT(CONTROL_LOGINSCREEN);
204   }
205 }
206
207 void CGUIWindowSettingsProfile::ClearListItems()
208 {
209   CGUIMessage msg(GUI_MSG_LABEL_RESET, GetID(), CONTROL_PROFILES);
210   g_windowManager.SendMessage(msg);
211
212   m_listItems->Clear();
213 }
214
215 void CGUIWindowSettingsProfile::OnInitWindow()
216 {
217   LoadList();
218   CGUIWindow::OnInitWindow();
219 }
220