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>
22 #include <osl/diagnose.h>
23 #include <comphelper/sequence.hxx>
25 using namespace com::sun::star
;
27 SysCredentialsConfigItem::SysCredentialsConfigItem(
28 SysCredentialsConfig
* pOwner
)
29 : utl::ConfigItem( "Office.Common/Passwords", ConfigItemMode::NONE
),
33 uno::Sequence
<OUString
> aNode
{ "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::ImplCommit()
55 uno::Sequence
< OUString
>
56 SysCredentialsConfigItem::getSystemCredentialsURLs()
58 ::osl::MutexGuard
aGuard( m_aMutex
);
62 uno::Sequence
<OUString
> aPropNames
{ "AuthenticateUsingSystemCredentials" };
63 uno::Sequence
< uno::Any
> aAnyValues(
64 utl::ConfigItem::GetProperties( aPropNames
) );
67 aAnyValues
.getLength() == 1,
68 "SysCredentialsConfigItem::getSystemCredentialsURLs: "
69 "Error reading config item!" );
71 uno::Sequence
< OUString
> aValues
;
72 if ( ( aAnyValues
[ 0 ] >>= aValues
) ||
73 ( !aAnyValues
[ 0 ].hasValue() ) )
82 void SysCredentialsConfigItem::setSystemCredentialsURLs(
83 const uno::Sequence
< OUString
> & seqURLList
)
85 ::osl::MutexGuard
aGuard( m_aMutex
);
88 uno::Sequence
< OUString
> aPropNames( 1 );
89 uno::Sequence
< uno::Any
> aPropValues( 1 );
90 aPropNames
[ 0 ] = "AuthenticateUsingSystemCredentials";
91 aPropValues
[ 0 ] <<= seqURLList
;
93 utl::ConfigItem::SetModified();
94 utl::ConfigItem::PutProperties( aPropNames
, aPropValues
);
96 m_seqURLs
= seqURLList
;
103 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
104 bool removeLastSegment( OUString
& aURL
)
106 sal_Int32 aInd
= aURL
.lastIndexOf( '/' );
110 sal_Int32 aPrevInd
= aURL
.lastIndexOf( '/', aInd
);
111 if ( aURL
.indexOf( "://" ) != aPrevInd
- 2 ||
112 aInd
!= aURL
.getLength() - 1 )
114 aURL
= aURL
.copy( 0, aInd
);
122 bool findURL( StringSet
const & rContainer
, OUString
const & aURL
, OUString
& aResult
)
124 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
125 if( !rContainer
.empty() && !aURL
.isEmpty() )
127 OUString
aUrl( aURL
);
129 // each iteration remove last '/...' section from the aUrl
130 // while it's possible, up to the most left '://'
133 // first look for <url>/somename and then look for <url>/somename/...
134 StringSet::const_iterator aIter
= rContainer
.find( aUrl
);
135 if( aIter
!= rContainer
.end() )
142 OUString
tmpUrl( aUrl
);
143 if ( !tmpUrl
.endsWith("/") )
146 aIter
= rContainer
.lower_bound( tmpUrl
);
147 if( aIter
!= rContainer
.end() && aIter
->match( tmpUrl
) )
154 while( removeLastSegment( aUrl
) && !aUrl
.isEmpty() );
162 SysCredentialsConfig::SysCredentialsConfig()
163 : m_aConfigItem( this ),
164 m_bCfgInited( false )
168 void SysCredentialsConfig::initCfg()
170 osl::MutexGuard
aGuard( m_aMutex
);
173 uno::Sequence
< OUString
> aURLs(
174 m_aConfigItem
.getSystemCredentialsURLs() );
175 for ( sal_Int32 n
= 0; n
< aURLs
.getLength(); ++n
)
176 m_aCfgContainer
.insert( aURLs
[ n
] );
182 void SysCredentialsConfig::writeCfg()
184 osl::MutexGuard
aGuard( m_aMutex
);
186 OSL_ENSURE( m_bCfgInited
, "SysCredentialsConfig::writeCfg : not initialized!" );
188 m_aConfigItem
.setSystemCredentialsURLs( comphelper::containerToSequence(m_aCfgContainer
) );
191 OUString
SysCredentialsConfig::find( OUString
const & aURL
)
193 osl::MutexGuard
aGuard( m_aMutex
);
195 if ( findURL( m_aMemContainer
, aURL
, aResult
) )
199 if ( findURL( m_aCfgContainer
, aURL
, aResult
) )
205 void SysCredentialsConfig::add( OUString
const & rURL
, bool bPersistent
)
207 ::osl::MutexGuard
aGuard( m_aMutex
);
211 m_aMemContainer
.erase( rURL
);
214 m_aCfgContainer
.insert( rURL
);
220 if ( m_aCfgContainer
.erase( rURL
) > 0 )
223 m_aMemContainer
.insert( rURL
);
227 void SysCredentialsConfig::remove( OUString
const & rURL
)
229 m_aMemContainer
.erase( rURL
);
232 if ( m_aCfgContainer
.erase( rURL
) > 0 )
236 uno::Sequence
< OUString
> SysCredentialsConfig::list( bool bOnlyPersistent
)
239 sal_Int32 nCount
= m_aCfgContainer
.size()
240 + ( bOnlyPersistent
? 0 : m_aMemContainer
.size() );
241 uno::Sequence
< OUString
> aResult( nCount
);
245 for ( const auto& rItem
: m_aCfgContainer
)
247 aResult
[ n
] = rItem
;
251 if ( !bOnlyPersistent
)
253 for ( const auto& rItem
: m_aMemContainer
)
255 aResult
[ n
] = rItem
;
262 void SysCredentialsConfig::persistentConfigChanged()
264 ::osl::MutexGuard
aGuard( m_aMutex
);
265 m_bCfgInited
= false; // re-init on demand.
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */