Update ooo320-m1
[ooovba.git] / svtools / source / passwordcontainer / syscreds.cxx
blobd4d875a20517ec5c6c89ee4f2d3803f68f71da2c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: $
10 * $Revision: $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "syscreds.hxx"
32 #include "com/sun/star/beans/PropertyValue.hpp"
34 using namespace com::sun::star;
36 SysCredentialsConfigItem::SysCredentialsConfigItem(
37 SysCredentialsConfig * pOwner )
38 : utl::ConfigItem( rtl::OUString::createFromAscii( "Office.Common/Passwords" ),
39 CONFIG_MODE_IMMEDIATE_UPDATE ),
40 m_bInited( false ),
41 m_pOwner( pOwner )
43 uno::Sequence< ::rtl::OUString > aNode( 1 );
44 aNode[ 0 ] = rtl::OUString::createFromAscii(
45 "Office.Common/Passwords/AuthenticateUsingSystemCredentials" );
46 EnableNotification( aNode );
49 //virtual
50 void SysCredentialsConfigItem::Notify(
51 const uno::Sequence< rtl::OUString > & /*seqPropertyNames*/ )
54 ::osl::MutexGuard aGuard( m_aMutex );
55 m_bInited = false;
56 // rebuild m_seqURLs
57 getSystemCredentialsURLs();
59 m_pOwner->persistentConfigChanged();
62 uno::Sequence< rtl::OUString >
63 SysCredentialsConfigItem::getSystemCredentialsURLs()
65 ::osl::MutexGuard aGuard( m_aMutex );
66 if ( !m_bInited )
68 // read config item
69 uno::Sequence< ::rtl::OUString > aPropNames( 1 );
70 aPropNames[ 0 ] = rtl::OUString::createFromAscii(
71 "AuthenticateUsingSystemCredentials" );
72 uno::Sequence< uno::Any > aAnyValues(
73 utl::ConfigItem::GetProperties( aPropNames ) );
75 OSL_ENSURE(
76 aAnyValues.getLength() == 1,
77 "SysCredentialsConfigItem::getSystemCredentialsURLs: "
78 "Error reading config item!" );
80 uno::Sequence< rtl::OUString > aValues;
81 if ( ( aAnyValues[ 0 ] >>= aValues ) ||
82 ( !aAnyValues[ 0 ].hasValue() ) )
84 m_seqURLs = aValues;
85 m_bInited = true;
88 return m_seqURLs;
91 void SysCredentialsConfigItem::setSystemCredentialsURLs(
92 const uno::Sequence< rtl::OUString > & seqURLList )
94 ::osl::MutexGuard aGuard( m_aMutex );
96 // write config item.
97 uno::Sequence< rtl::OUString > aPropNames( 1 );
98 uno::Sequence< uno::Any > aPropValues( 1 );
99 aPropNames[ 0 ]
100 = ::rtl::OUString::createFromAscii(
101 "AuthenticateUsingSystemCredentials" );
102 aPropValues[ 0 ] <<= seqURLList;
104 utl::ConfigItem::SetModified();
105 utl::ConfigItem::PutProperties( aPropNames, aPropValues );
107 m_seqURLs = seqURLList;
108 m_bInited = true;
111 //============================================================================
113 namespace
115 // TODO: This code is actually copied from svtools/source/passwordcontainer.cxx
116 bool removeLastSegment( ::rtl::OUString & aURL )
118 sal_Int32 aInd = aURL.lastIndexOf( sal_Unicode( '/' ) );
120 if( aInd > 0 )
122 sal_Int32 aPrevInd = aURL.lastIndexOf( sal_Unicode( '/' ), aInd );
123 if ( aURL.indexOf( ::rtl::OUString::createFromAscii( "://" ) )
124 != aPrevInd - 2 ||
125 aInd != aURL.getLength() - 1 )
127 aURL = aURL.copy( 0, aInd );
128 return true;
132 return false;
135 bool findURL( StringSet const & rContainer, rtl::OUString const & aURL, rtl::OUString & aResult )
137 // TODO: This code is actually copied from svtools/source/passwordcontainer.cxx
138 if( !rContainer.empty() && aURL.getLength() )
140 ::rtl::OUString aUrl( aURL );
142 // each iteration remove last '/...' section from the aUrl
143 // while it's possible, up to the most left '://'
146 // first look for <url>/somename and then look for <url>/somename/...
147 StringSet::const_iterator aIter = rContainer.find( aUrl );
148 if( aIter != rContainer.end() )
150 aResult = *aIter;
151 return true;
153 else
155 ::rtl::OUString tmpUrl( aUrl );
156 if ( tmpUrl.getStr()[tmpUrl.getLength() - 1] != (sal_Unicode)'/' )
157 tmpUrl += ::rtl::OUString::createFromAscii( "/" );
159 aIter = rContainer.lower_bound( tmpUrl );
160 if( aIter != rContainer.end() && aIter->match( tmpUrl ) )
162 aResult = *aIter;
163 return true;
167 while( removeLastSegment( aUrl ) && aUrl.getLength() );
169 aResult = rtl::OUString();
170 return false;
173 } // namespace
175 SysCredentialsConfig::SysCredentialsConfig()
176 : m_aConfigItem( this ),
177 m_bCfgInited( false )
181 void SysCredentialsConfig::initCfg()
183 osl::MutexGuard aGuard( m_aMutex );
184 if ( !m_bCfgInited )
186 uno::Sequence< rtl::OUString > aURLs(
187 m_aConfigItem.getSystemCredentialsURLs() );
188 for ( sal_Int32 n = 0; n < aURLs.getLength(); ++n )
189 m_aCfgContainer.insert( aURLs[ n ] );
191 m_bCfgInited = true;
195 void SysCredentialsConfig::writeCfg()
197 osl::MutexGuard aGuard( m_aMutex );
199 OSL_ENSURE( m_bCfgInited, "SysCredentialsConfig::writeCfg : not initialized!" );
201 uno::Sequence< rtl::OUString > aURLs( m_aCfgContainer.size() );
202 StringSet::const_iterator it = m_aCfgContainer.begin();
203 const StringSet::const_iterator end = m_aCfgContainer.end();
204 sal_Int32 n = 0;
206 while ( it != end )
208 aURLs[ n ] = *it;
209 ++it;
210 ++n;
213 m_aConfigItem.setSystemCredentialsURLs( aURLs );
216 rtl::OUString SysCredentialsConfig::find( rtl::OUString const & aURL )
218 osl::MutexGuard aGuard( m_aMutex );
219 rtl::OUString aResult;
220 if ( findURL( m_aMemContainer, aURL, aResult ) )
221 return aResult;
223 initCfg();
224 if ( findURL( m_aCfgContainer, aURL, aResult ) )
225 return aResult;
227 return rtl::OUString();
230 void SysCredentialsConfig::add( rtl::OUString const & rURL, bool bPersistent )
232 ::osl::MutexGuard aGuard( m_aMutex );
234 if ( bPersistent )
236 m_aMemContainer.erase( rURL );
238 initCfg();
239 m_aCfgContainer.insert( rURL );
240 writeCfg();
242 else
244 initCfg();
245 if ( m_aCfgContainer.erase( rURL ) > 0 )
246 writeCfg();
248 m_aMemContainer.insert( rURL );
252 void SysCredentialsConfig::remove( rtl::OUString const & rURL )
254 m_aMemContainer.erase( rURL );
256 initCfg();
257 if ( m_aCfgContainer.erase( rURL ) > 0 )
258 writeCfg();
261 uno::Sequence< rtl::OUString > SysCredentialsConfig::list( bool bOnlyPersistent )
263 initCfg();
264 sal_Int32 nCount = m_aCfgContainer.size()
265 + ( bOnlyPersistent ? 0 : m_aMemContainer.size() );
266 uno::Sequence< rtl::OUString > aResult( nCount );
268 StringSet::const_iterator it = m_aCfgContainer.begin();
269 StringSet::const_iterator end = m_aCfgContainer.end();
270 sal_Int32 n = 0;
272 while ( it != end )
274 aResult[ n ] = *it;
275 ++it;
276 ++n;
279 if ( !bOnlyPersistent )
281 it = m_aMemContainer.begin();
282 end = m_aMemContainer.end();
284 while ( it != end )
286 aResult[ n ] = *it;
287 ++it;
288 ++n;
291 return aResult;
294 void SysCredentialsConfig::persistentConfigChanged()
296 ::osl::MutexGuard aGuard( m_aMutex );
297 m_bCfgInited = false; // re-init on demand.