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 "GUIDialogProfileSettings.h"
22 #include "dialogs/GUIDialogFileBrowser.h"
23 #include "guilib/GUIKeyboardFactory.h"
24 #include "profiles/ProfilesManager.h"
25 #include "profiles/dialogs/GUIDialogLockSettings.h"
26 #include "guilib/GUIImage.h"
27 #include "guilib/GUIWindowManager.h"
28 #include "storage/MediaManager.h"
30 #include "utils/URIUtils.h"
31 #include "GUIPassword.h"
32 #include "dialogs/GUIDialogYesNo.h"
33 #include "filesystem/Directory.h"
34 #include "filesystem/File.h"
36 #include "settings/Settings.h"
37 #include "guilib/LocalizeStrings.h"
38 #include "TextureCache.h"
40 using namespace XFILE;
42 #define CONTROL_PROFILE_IMAGE 2
43 #define CONTROL_START 30
45 CGUIDialogProfileSettings::CGUIDialogProfileSettings(void)
46 : CGUIDialogSettings(WINDOW_DIALOG_PROFILE_SETTINGS, "ProfileSettings.xml")
51 CGUIDialogProfileSettings::~CGUIDialogProfileSettings(void)
55 bool CGUIDialogProfileSettings::OnMessage(CGUIMessage &message)
57 if (message.GetMessage() == GUI_MSG_CLICKED)
59 int iControl = message.GetSenderId();
68 return CGUIDialogSettings::OnMessage(message);
71 void CGUIDialogProfileSettings::OnWindowLoaded()
73 CGUIDialogSettings::OnWindowLoaded();
74 CGUIImage *pImage = (CGUIImage*)GetControl(2);
75 m_strDefaultImage = pImage ? pImage->GetFileName() : "";
78 void CGUIDialogProfileSettings::SetupPage()
80 CGUIDialogSettings::SetupPage();
81 SET_CONTROL_LABEL(1000,m_strName);
82 SET_CONTROL_LABEL(1001,m_strDirectory);
83 CGUIImage *pImage = (CGUIImage*)GetControl(2);
85 pImage->SetFileName(!m_strThumb.empty() ? m_strThumb : m_strDefaultImage);
88 void CGUIDialogProfileSettings::CreateSettings()
90 // clear out any old settings
95 if (!m_bIsDefault && m_bShowDetails)
100 if (!m_bShowDetails && m_locks.mode == LOCK_MODE_EVERYONE && CProfilesManager::Get().GetMasterProfile().getLockMode() != LOCK_MODE_EVERYONE)
103 if (!m_bIsDefault && m_bShowDetails)
107 setting.name = g_localizeStrings.Get(20060);
108 setting.data = &m_iDbMode;
109 setting.type = SettingInfo::SPIN;
110 setting.entry.push_back(make_pair(setting.entry.size(), g_localizeStrings.Get(20062)));
111 setting.entry.push_back(make_pair(setting.entry.size(), g_localizeStrings.Get(20063)));
112 setting.entry.push_back(make_pair(setting.entry.size(), g_localizeStrings.Get(20061)));
113 if (CProfilesManager::Get().GetMasterProfile().getLockMode() != LOCK_MODE_EVERYONE)
114 setting.entry.push_back(make_pair(setting.entry.size(), g_localizeStrings.Get(20107)));
116 m_settings.push_back(setting);
118 SettingInfo setting2;
120 setting2.name = g_localizeStrings.Get(20094);
121 setting2.data = &m_iSourcesMode;
122 setting2.type = SettingInfo::SPIN;
123 setting2.entry.push_back(make_pair(setting2.entry.size(), g_localizeStrings.Get(20062)));
124 setting2.entry.push_back(make_pair(setting2.entry.size(), g_localizeStrings.Get(20063)));
125 setting2.entry.push_back(make_pair(setting2.entry.size(), g_localizeStrings.Get(20061)));
126 if (CProfilesManager::Get().GetMasterProfile().getLockMode() != LOCK_MODE_EVERYONE)
127 setting2.entry.push_back(make_pair(setting2.entry.size(), g_localizeStrings.Get(20107)));
129 m_settings.push_back(setting2);
133 void CGUIDialogProfileSettings::OnSettingChanged(unsigned int num)
135 // setting has changed - update anything that needs it
136 if (num >= m_settings.size()) return;
137 OnSettingChanged(m_settings.at(num));
140 void CGUIDialogProfileSettings::OnSettingChanged(SettingInfo &setting)
142 // check and update anything that needs it
145 if (CGUIKeyboardFactory::ShowAndGetInput(m_strName,g_localizeStrings.Get(20093),false))
148 SET_CONTROL_LABEL(1000,m_strName);
155 g_mediaManager.GetLocalDrives(shares);
157 if (!m_strThumb.empty())
159 CFileItemPtr item(new CFileItem("thumb://Current", false));
160 item->SetArt("thumb", m_strThumb);
161 item->SetLabel(g_localizeStrings.Get(20016));
164 CFileItemPtr item(new CFileItem("thumb://None", false));
165 item->SetArt("thumb", m_strDefaultImage);
166 item->SetLabel(g_localizeStrings.Get(20018));
168 if (CGUIDialogFileBrowser::ShowAndGetImage(items,shares,g_localizeStrings.Get(1030),strThumb) &&
169 !strThumb.Equals("thumb://Current"))
172 m_strThumb = strThumb.Equals("thumb://None") ? "" : strThumb;
174 CGUIImage *pImage = (CGUIImage*)GetControl(2);
177 pImage->SetFileName("");
178 pImage->SetInvalid();
179 pImage->SetFileName(!m_strThumb.empty() ? m_strThumb : m_strDefaultImage);
185 if (OnProfilePath(m_strDirectory, m_bIsDefault))
188 SET_CONTROL_LABEL(1001,m_strDirectory);
196 if (CProfilesManager::Get().GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE && !m_bIsDefault)
198 if (CGUIDialogYesNo::ShowAndGetInput(20066,20118,20119,20022))
199 g_passwordManager.SetMasterLockMode(false);
200 if (CProfilesManager::Get().GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE)
203 if (CGUIDialogLockSettings::ShowAndGetLock(m_locks, m_bIsDefault ? 12360 : 20068, CProfilesManager::Get().GetMasterProfile().getLockMode() == LOCK_MODE_EVERYONE || m_bIsDefault))
208 if (CGUIDialogLockSettings::ShowAndGetLock(m_locks, m_bIsDefault ? 12360 : 20068, false, false))
216 void CGUIDialogProfileSettings::OnCancel()
221 bool CGUIDialogProfileSettings::OnProfilePath(CStdString &dir, bool isDefault)
225 share.strName = "Profiles";
226 share.strPath = "special://masterprofile/profiles/";
227 shares.push_back(share);
228 CStdString strDirectory;
230 strDirectory = share.strPath;
232 strDirectory = URIUtils::AddFileToFolder("special://masterprofile/", dir);
233 if (CGUIDialogFileBrowser::ShowAndGetDirectory(shares,g_localizeStrings.Get(657),strDirectory,true))
243 bool CGUIDialogProfileSettings::ShowForProfile(unsigned int iProfile, bool firstLogin)
245 CGUIDialogProfileSettings *dialog = (CGUIDialogProfileSettings *)g_windowManager.GetWindow(WINDOW_DIALOG_PROFILE_SETTINGS);
246 if (!dialog) return false;
248 dialog->m_bIsDefault = true;
250 dialog->m_bIsDefault = false;
251 if (firstLogin && iProfile > CProfilesManager::Get().GetNumberOfProfiles())
254 dialog->m_bNeedSave = false;
255 dialog->m_bShowDetails = !firstLogin;
256 dialog->SetProperty("heading", g_localizeStrings.Get(firstLogin ? 20255 : 20067));
258 const CProfile *profile = CProfilesManager::Get().GetProfile(iProfile);
262 dialog->m_strName.clear();
263 dialog->m_iDbMode = 2;
264 dialog->m_iSourcesMode = 2;
265 dialog->m_locks = CProfile::CLock();
267 bool bLock = CProfilesManager::Get().GetMasterProfile().getLockMode() != LOCK_MODE_EVERYONE && !g_passwordManager.bMasterUser;
268 dialog->m_locks.addonManager = bLock;
269 dialog->m_locks.settings = (bLock) ? LOCK_LEVEL::ALL : LOCK_LEVEL::NONE;
270 dialog->m_locks.files = bLock;
272 dialog->m_strDirectory.clear();
273 dialog->m_strThumb.clear();
275 if (!CGUIKeyboardFactory::ShowAndGetInput(dialog->m_strName,g_localizeStrings.Get(20093),false) || dialog->m_strName.empty())
277 // create a default path
278 CStdString defaultDir = URIUtils::AddFileToFolder("profiles",CUtil::MakeLegalFileName(dialog->m_strName));
279 URIUtils::AddSlashAtEnd(defaultDir);
280 CDirectory::Create(URIUtils::AddFileToFolder("special://masterprofile/", defaultDir));
281 // prompt for the user to change it if they want
282 CStdString userDir = defaultDir;
283 if (dialog->OnProfilePath(userDir, false)) // can't be the master user
285 if (!StringUtils::StartsWith(userDir, defaultDir)) // user chose a different folder
286 CDirectory::Remove(URIUtils::AddFileToFolder("special://masterprofile/", defaultDir));
288 dialog->m_strDirectory = userDir;
289 dialog->m_bNeedSave = true;
293 dialog->m_strName = profile->getName();
294 dialog->m_strThumb = profile->getThumb();
295 dialog->m_strDirectory = profile->getDirectory();
296 dialog->m_iDbMode = profile->canWriteDatabases()?0:1;
297 dialog->m_iSourcesMode = profile->canWriteSources()?0:1;
298 if (profile->hasDatabases())
299 dialog->m_iDbMode += 2;
300 if (profile->hasSources())
301 dialog->m_iSourcesMode += 2;
303 dialog->m_locks = profile->GetLocks();
306 if (dialog->m_bNeedSave)
308 if (iProfile >= CProfilesManager::Get().GetNumberOfProfiles())
310 if (dialog->m_strName.empty() || dialog->m_strDirectory.empty())
312 /*CStdString strLabel;
313 strLabel.Format(g_localizeStrings.Get(20047),dialog->m_strName);
314 if (!CGUIDialogYesNo::ShowAndGetInput(g_localizeStrings.Get(20058),strLabel,dialog->m_strDirectory,""))
316 CDirectory::Remove(URIUtils::AddFileToFolder(CProfilesManager::Get().GetUserDataFolder(), dialog->m_strDirectory));
320 // check for old profile settings
321 CProfile profile(dialog->m_strDirectory, dialog->m_strName, CProfilesManager::Get().GetNextProfileId());
322 CProfilesManager::Get().AddProfile(profile);
323 bool bExists = CFile::Exists(URIUtils::AddFileToFolder("special://masterprofile/",
324 dialog->m_strDirectory+"/guisettings.xml"));
327 if (!CGUIDialogYesNo::ShowAndGetInput(20058,20104,20105,20022))
332 // copy masterprofile guisettings to new profile guisettings
333 // If the user selects 'start fresh', do nothing as a fresh
334 // guisettings.xml will be created on first profile use.
335 if (CGUIDialogYesNo::ShowAndGetInput(20058,20048,20102,20022,20044,20064))
337 CFile::Cache(URIUtils::AddFileToFolder("special://masterprofile/","guisettings.xml"),
338 URIUtils::AddFileToFolder("special://masterprofile/",
339 dialog->m_strDirectory+"/guisettings.xml"));
343 bExists = CFile::Exists(URIUtils::AddFileToFolder("special://masterprofile/",
344 dialog->m_strDirectory+"/sources.xml"));
346 if (!CGUIDialogYesNo::ShowAndGetInput(20058,20106,20105,20022))
351 if ((dialog->m_iSourcesMode & 2) == 2)
352 // prompt user to copy masterprofile's sources.xml file
353 // If 'start fresh' (no) is selected, do nothing.
354 if (CGUIDialogYesNo::ShowAndGetInput(20058,20071,20102,20022,20044,20064))
356 CFile::Cache(URIUtils::AddFileToFolder("special://masterprofile/","sources.xml"),
357 URIUtils::AddFileToFolder("special://masterprofile/",
358 dialog->m_strDirectory+"/sources.xml"));
363 /*if (!dialog->m_bIsNewUser)
364 if (!CGUIDialogYesNo::ShowAndGetInput(20067,20103,20022,20022))
367 CProfile *profile = CProfilesManager::Get().GetProfile(iProfile);
369 profile->setName(dialog->m_strName);
370 profile->setDirectory(dialog->m_strDirectory);
371 profile->setThumb(dialog->m_strThumb);
372 profile->setWriteDatabases(!((dialog->m_iDbMode & 1) == 1));
373 profile->setWriteSources(!((dialog->m_iSourcesMode & 1) == 1));
374 profile->setDatabases((dialog->m_iDbMode & 2) == 2);
375 profile->setSources((dialog->m_iSourcesMode & 2) == 2);
376 profile->SetLocks(dialog->m_locks);
378 CProfilesManager::Get().Save();
382 return !dialog->m_bNeedSave;