changed: update version strings for beta4
[xbmc.git] / xbmc / utils / PasswordManager.cpp
blob536db2f82f2a16dfd3935736e2227373c97733d6
1 /*
2 * Copyright (C) 2005-2008 Team XBMC
3 * http://www.xbmc.org
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.
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, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 #include "PasswordManager.h"
23 #include "GUIDialogLockSettings.h"
24 #include "URL.h"
25 #include "Settings.h"
26 #include "XMLUtils.h"
27 #include "utils/log.h"
28 #include "FileSystem/File.h"
30 using namespace std;
32 CPasswordManager &CPasswordManager::GetInstance()
34 static CPasswordManager sPasswordManager;
35 return sPasswordManager;
38 CPasswordManager::CPasswordManager()
40 m_loaded = false;
43 bool CPasswordManager::AuthenticateURL(CURL &url)
45 if (!m_loaded)
46 Load();
47 CStdString lookup(GetLookupPath(url));
48 map<CStdString, CStdString>::const_iterator it = m_temporaryCache.find(lookup);
49 if (it == m_temporaryCache.end())
50 { // second step, try something that doesn't quite match
51 it = m_temporaryCache.find(GetServerLookup(lookup));
53 if (it != m_temporaryCache.end())
55 CURL auth(it->second);
56 url.SetPassword(auth.GetPassWord());
57 url.SetUserName(auth.GetUserName());
58 return true;
60 return false;
63 bool CPasswordManager::PromptToAuthenticateURL(CURL &url)
65 CStdString passcode;
66 CStdString username = url.GetUserName();
67 CStdString path = GetLookupPath(url);
69 bool saveDetails = false;
70 if (!CGUIDialogLockSettings::ShowAndGetUserAndPassword(username, passcode, url.GetWithoutUserDetails(), &saveDetails))
71 return false;
73 url.SetPassword(passcode);
74 url.SetUserName(username);
76 // save the information for later
77 CStdString authenticatedPath = url.Get();
79 if (!m_loaded)
80 Load();
82 if (saveDetails)
83 { // write to some random XML file...
84 m_permanentCache[path] = authenticatedPath;
85 Save();
88 // save for both this path and more generally the server as a whole.
89 m_temporaryCache[path] = authenticatedPath;
90 m_temporaryCache[GetServerLookup(path)] = authenticatedPath;
91 return true;
94 void CPasswordManager::Clear()
96 m_temporaryCache.clear();
97 m_permanentCache.clear();
98 m_loaded = false;
101 void CPasswordManager::Load()
103 Clear();
104 CStdString passwordsFile = g_settings.GetUserDataItem("passwords.xml");
105 if (XFILE::CFile::Exists(passwordsFile))
107 TiXmlDocument doc;
108 if (!doc.LoadFile(passwordsFile))
110 CLog::Log(LOGERROR, "%s - Unable to load: %s, Line %d\n%s",
111 __FUNCTION__, passwordsFile.c_str(), doc.ErrorRow(), doc.ErrorDesc());
112 return;
114 const TiXmlElement *root = doc.RootElement();
115 if (root->ValueStr() != "passwords")
116 return;
117 // read in our passwords
118 const TiXmlElement *path = root->FirstChildElement("path");
119 while (path)
121 CStdString from, to;
122 if (XMLUtils::GetPath(path, "from", from) && XMLUtils::GetPath(path, "to", to))
124 m_permanentCache[from] = to;
125 m_temporaryCache[from] = to;
126 m_temporaryCache[GetServerLookup(from)] = to;
128 path = path->NextSiblingElement("path");
131 m_loaded = true;
134 void CPasswordManager::Save() const
136 if (!m_permanentCache.size())
137 return;
139 TiXmlDocument doc;
140 TiXmlElement rootElement("passwords");
141 TiXmlNode *root = doc.InsertEndChild(rootElement);
142 if (!root)
143 return;
145 for (map<CStdString, CStdString>::const_iterator i = m_permanentCache.begin(); i != m_permanentCache.end(); ++i)
147 TiXmlElement pathElement("path");
148 TiXmlNode *path = root->InsertEndChild(pathElement);
149 XMLUtils::SetPath(path, "from", i->first);
150 XMLUtils::SetPath(path, "to", i->second);
153 doc.SaveFile(g_settings.GetUserDataItem("passwords.xml"));
156 CStdString CPasswordManager::GetLookupPath(const CURL &url) const
158 return "smb://" + url.GetHostName() + "/" + url.GetShareName();
161 CStdString CPasswordManager::GetServerLookup(const CStdString &path) const
163 CURL url(path);
164 return "smb://" + url.GetHostName() + "/";