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
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
),
43 uno::Sequence
< ::rtl::OUString
> aNode( 1 );
44 aNode
[ 0 ] = rtl::OUString::createFromAscii(
45 "Office.Common/Passwords/AuthenticateUsingSystemCredentials" );
46 EnableNotification( aNode
);
50 void SysCredentialsConfigItem::Notify(
51 const uno::Sequence
< rtl::OUString
> & /*seqPropertyNames*/ )
54 ::osl::MutexGuard
aGuard( m_aMutex
);
57 getSystemCredentialsURLs();
59 m_pOwner
->persistentConfigChanged();
62 uno::Sequence
< rtl::OUString
>
63 SysCredentialsConfigItem::getSystemCredentialsURLs()
65 ::osl::MutexGuard
aGuard( m_aMutex
);
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
) );
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() ) )
91 void SysCredentialsConfigItem::setSystemCredentialsURLs(
92 const uno::Sequence
< rtl::OUString
> & seqURLList
)
94 ::osl::MutexGuard
aGuard( m_aMutex
);
97 uno::Sequence
< rtl::OUString
> aPropNames( 1 );
98 uno::Sequence
< uno::Any
> aPropValues( 1 );
100 = ::rtl::OUString::createFromAscii(
101 "AuthenticateUsingSystemCredentials" );
102 aPropValues
[ 0 ] <<= seqURLList
;
104 utl::ConfigItem::SetModified();
105 utl::ConfigItem::PutProperties( aPropNames
, aPropValues
);
107 m_seqURLs
= seqURLList
;
111 //============================================================================
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( '/' ) );
122 sal_Int32 aPrevInd
= aURL
.lastIndexOf( sal_Unicode( '/' ), aInd
);
123 if ( aURL
.indexOf( ::rtl::OUString::createFromAscii( "://" ) )
125 aInd
!= aURL
.getLength() - 1 )
127 aURL
= aURL
.copy( 0, aInd
);
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() )
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
) )
167 while( removeLastSegment( aUrl
) && aUrl
.getLength() );
169 aResult
= rtl::OUString();
175 SysCredentialsConfig::SysCredentialsConfig()
176 : m_aConfigItem( this ),
177 m_bCfgInited( false )
181 void SysCredentialsConfig::initCfg()
183 osl::MutexGuard
aGuard( m_aMutex
);
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
] );
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();
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
) )
224 if ( findURL( m_aCfgContainer
, aURL
, aResult
) )
227 return rtl::OUString();
230 void SysCredentialsConfig::add( rtl::OUString
const & rURL
, bool bPersistent
)
232 ::osl::MutexGuard
aGuard( m_aMutex
);
236 m_aMemContainer
.erase( rURL
);
239 m_aCfgContainer
.insert( rURL
);
245 if ( m_aCfgContainer
.erase( rURL
) > 0 )
248 m_aMemContainer
.insert( rURL
);
252 void SysCredentialsConfig::remove( rtl::OUString
const & rURL
)
254 m_aMemContainer
.erase( rURL
);
257 if ( m_aCfgContainer
.erase( rURL
) > 0 )
261 uno::Sequence
< rtl::OUString
> SysCredentialsConfig::list( bool bOnlyPersistent
)
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();
279 if ( !bOnlyPersistent
)
281 it
= m_aMemContainer
.begin();
282 end
= m_aMemContainer
.end();
294 void SysCredentialsConfig::persistentConfigChanged()
296 ::osl::MutexGuard
aGuard( m_aMutex
);
297 m_bCfgInited
= false; // re-init on demand.