1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "syscreds.hxx"
21 #include "com/sun/star/beans/PropertyValue.hpp"
23 using namespace com::sun::star
;
25 SysCredentialsConfigItem::SysCredentialsConfigItem(
26 SysCredentialsConfig
* pOwner
)
27 : utl::ConfigItem( OUString("Office.Common/Passwords"),
28 CONFIG_MODE_IMMEDIATE_UPDATE
),
32 uno::Sequence
< OUString
> aNode( 1 );
33 aNode
[ 0 ] = OUString( "Office.Common/Passwords/AuthenticateUsingSystemCredentials" );
34 EnableNotification( aNode
);
38 void SysCredentialsConfigItem::Notify(
39 const uno::Sequence
< OUString
> & /*seqPropertyNames*/ )
42 ::osl::MutexGuard
aGuard( m_aMutex
);
45 getSystemCredentialsURLs();
47 m_pOwner
->persistentConfigChanged();
50 void SysCredentialsConfigItem::Commit()
55 uno::Sequence
< OUString
>
56 SysCredentialsConfigItem::getSystemCredentialsURLs()
58 ::osl::MutexGuard
aGuard( m_aMutex
);
62 uno::Sequence
< OUString
> aPropNames( 1 );
63 aPropNames
[ 0 ] = OUString( "AuthenticateUsingSystemCredentials" );
64 uno::Sequence
< uno::Any
> aAnyValues(
65 utl::ConfigItem::GetProperties( aPropNames
) );
68 aAnyValues
.getLength() == 1,
69 "SysCredentialsConfigItem::getSystemCredentialsURLs: "
70 "Error reading config item!" );
72 uno::Sequence
< OUString
> aValues
;
73 if ( ( aAnyValues
[ 0 ] >>= aValues
) ||
74 ( !aAnyValues
[ 0 ].hasValue() ) )
83 void SysCredentialsConfigItem::setSystemCredentialsURLs(
84 const uno::Sequence
< OUString
> & seqURLList
)
86 ::osl::MutexGuard
aGuard( m_aMutex
);
89 uno::Sequence
< OUString
> aPropNames( 1 );
90 uno::Sequence
< uno::Any
> aPropValues( 1 );
91 aPropNames
[ 0 ] = OUString( "AuthenticateUsingSystemCredentials" );
92 aPropValues
[ 0 ] <<= seqURLList
;
94 utl::ConfigItem::SetModified();
95 utl::ConfigItem::PutProperties( aPropNames
, aPropValues
);
97 m_seqURLs
= seqURLList
;
101 //============================================================================
105 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
106 bool removeLastSegment( OUString
& aURL
)
108 sal_Int32 aInd
= aURL
.lastIndexOf( sal_Unicode( '/' ) );
112 sal_Int32 aPrevInd
= aURL
.lastIndexOf( sal_Unicode( '/' ), aInd
);
113 if ( aURL
.indexOf( "://" ) != aPrevInd
- 2 ||
114 aInd
!= aURL
.getLength() - 1 )
116 aURL
= aURL
.copy( 0, aInd
);
124 bool findURL( StringSet
const & rContainer
, OUString
const & aURL
, OUString
& aResult
)
126 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
127 if( !rContainer
.empty() && !aURL
.isEmpty() )
129 OUString
aUrl( aURL
);
131 // each iteration remove last '/...' section from the aUrl
132 // while it's possible, up to the most left '://'
135 // first look for <url>/somename and then look for <url>/somename/...
136 StringSet::const_iterator aIter
= rContainer
.find( aUrl
);
137 if( aIter
!= rContainer
.end() )
144 OUString
tmpUrl( aUrl
);
145 if ( tmpUrl
.getStr()[tmpUrl
.getLength() - 1] != (sal_Unicode
)'/' )
146 tmpUrl
+= OUString("/");
148 aIter
= rContainer
.lower_bound( tmpUrl
);
149 if( aIter
!= rContainer
.end() && aIter
->match( tmpUrl
) )
156 while( removeLastSegment( aUrl
) && !aUrl
.isEmpty() );
158 aResult
= OUString();
164 SysCredentialsConfig::SysCredentialsConfig()
165 : m_aConfigItem( this ),
166 m_bCfgInited( false )
170 void SysCredentialsConfig::initCfg()
172 osl::MutexGuard
aGuard( m_aMutex
);
175 uno::Sequence
< OUString
> aURLs(
176 m_aConfigItem
.getSystemCredentialsURLs() );
177 for ( sal_Int32 n
= 0; n
< aURLs
.getLength(); ++n
)
178 m_aCfgContainer
.insert( aURLs
[ n
] );
184 void SysCredentialsConfig::writeCfg()
186 osl::MutexGuard
aGuard( m_aMutex
);
188 OSL_ENSURE( m_bCfgInited
, "SysCredentialsConfig::writeCfg : not initialized!" );
190 uno::Sequence
< OUString
> aURLs( m_aCfgContainer
.size() );
191 StringSet::const_iterator it
= m_aCfgContainer
.begin();
192 const StringSet::const_iterator end
= m_aCfgContainer
.end();
202 m_aConfigItem
.setSystemCredentialsURLs( aURLs
);
205 OUString
SysCredentialsConfig::find( OUString
const & aURL
)
207 osl::MutexGuard
aGuard( m_aMutex
);
209 if ( findURL( m_aMemContainer
, aURL
, aResult
) )
213 if ( findURL( m_aCfgContainer
, aURL
, aResult
) )
219 void SysCredentialsConfig::add( OUString
const & rURL
, bool bPersistent
)
221 ::osl::MutexGuard
aGuard( m_aMutex
);
225 m_aMemContainer
.erase( rURL
);
228 m_aCfgContainer
.insert( rURL
);
234 if ( m_aCfgContainer
.erase( rURL
) > 0 )
237 m_aMemContainer
.insert( rURL
);
241 void SysCredentialsConfig::remove( OUString
const & rURL
)
243 m_aMemContainer
.erase( rURL
);
246 if ( m_aCfgContainer
.erase( rURL
) > 0 )
250 uno::Sequence
< OUString
> SysCredentialsConfig::list( bool bOnlyPersistent
)
253 sal_Int32 nCount
= m_aCfgContainer
.size()
254 + ( bOnlyPersistent
? 0 : m_aMemContainer
.size() );
255 uno::Sequence
< OUString
> aResult( nCount
);
257 StringSet::const_iterator it
= m_aCfgContainer
.begin();
258 StringSet::const_iterator end
= m_aCfgContainer
.end();
268 if ( !bOnlyPersistent
)
270 it
= m_aMemContainer
.begin();
271 end
= m_aMemContainer
.end();
283 void SysCredentialsConfig::persistentConfigChanged()
285 ::osl::MutexGuard
aGuard( m_aMutex
);
286 m_bCfgInited
= false; // re-init on demand.
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */