2 * Copyright (C) 2005-2008 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, 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"
27 #include "utils/log.h"
28 #include "FileSystem/File.h"
32 CPasswordManager
&CPasswordManager::GetInstance()
34 static CPasswordManager sPasswordManager
;
35 return sPasswordManager
;
38 CPasswordManager::CPasswordManager()
43 bool CPasswordManager::AuthenticateURL(CURL
&url
)
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());
63 bool CPasswordManager::PromptToAuthenticateURL(CURL
&url
)
66 CStdString username
= url
.GetUserName();
67 CStdString path
= GetLookupPath(url
);
69 bool saveDetails
= false;
70 if (!CGUIDialogLockSettings::ShowAndGetUserAndPassword(username
, passcode
, url
.GetWithoutUserDetails(), &saveDetails
))
73 url
.SetPassword(passcode
);
74 url
.SetUserName(username
);
76 // save the information for later
77 CStdString authenticatedPath
= url
.Get();
83 { // write to some random XML file...
84 m_permanentCache
[path
] = authenticatedPath
;
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
;
94 void CPasswordManager::Clear()
96 m_temporaryCache
.clear();
97 m_permanentCache
.clear();
101 void CPasswordManager::Load()
104 CStdString passwordsFile
= g_settings
.GetUserDataItem("passwords.xml");
105 if (XFILE::CFile::Exists(passwordsFile
))
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());
114 const TiXmlElement
*root
= doc
.RootElement();
115 if (root
->ValueStr() != "passwords")
117 // read in our passwords
118 const TiXmlElement
*path
= root
->FirstChildElement("path");
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");
134 void CPasswordManager::Save() const
136 if (!m_permanentCache
.size())
140 TiXmlElement
rootElement("passwords");
141 TiXmlNode
*root
= doc
.InsertEndChild(rootElement
);
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
164 return "smb://" + url
.GetHostName() + "/";