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 <osl/diagnose.h>
22 #include <comphelper/sequence.hxx>
24 using namespace com::sun::star
;
26 SysCredentialsConfigItem::SysCredentialsConfigItem(
27 SysCredentialsConfig
* pOwner
)
28 : utl::ConfigItem( "Office.Common/Passwords", ConfigItemMode::NONE
),
32 uno::Sequence
<OUString
> aNode
{ "Office.Common/Passwords/AuthenticateUsingSystemCredentials" };
33 EnableNotification( aNode
);
37 void SysCredentialsConfigItem::Notify(
38 const uno::Sequence
< OUString
> & /*seqPropertyNames*/ )
41 ::osl::MutexGuard
aGuard( m_aMutex
);
44 getSystemCredentialsURLs();
46 m_pOwner
->persistentConfigChanged();
49 void SysCredentialsConfigItem::ImplCommit()
54 uno::Sequence
< OUString
>
55 SysCredentialsConfigItem::getSystemCredentialsURLs()
57 ::osl::MutexGuard
aGuard( m_aMutex
);
61 uno::Sequence
<OUString
> aPropNames
{ "AuthenticateUsingSystemCredentials" };
62 uno::Sequence
< uno::Any
> aAnyValues(
63 utl::ConfigItem::GetProperties( aPropNames
) );
66 aAnyValues
.getLength() == 1,
67 "SysCredentialsConfigItem::getSystemCredentialsURLs: "
68 "Error reading config item!" );
70 uno::Sequence
< OUString
> aValues
;
71 if ( ( aAnyValues
[ 0 ] >>= aValues
) ||
72 ( !aAnyValues
[ 0 ].hasValue() ) )
81 void SysCredentialsConfigItem::setSystemCredentialsURLs(
82 const uno::Sequence
< OUString
> & seqURLList
)
84 ::osl::MutexGuard
aGuard( m_aMutex
);
87 uno::Sequence
< OUString
> aPropNames
{ "AuthenticateUsingSystemCredentials" };
88 uno::Sequence
< uno::Any
> aPropValues
{ uno::Any(seqURLList
) };
90 utl::ConfigItem::SetModified();
91 utl::ConfigItem::PutProperties( aPropNames
, aPropValues
);
93 m_seqURLs
= seqURLList
;
100 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
101 bool removeLastSegment( OUString
& aURL
)
103 sal_Int32 aInd
= aURL
.lastIndexOf( '/' );
107 sal_Int32 aPrevInd
= aURL
.lastIndexOf( '/', aInd
);
108 if ( aURL
.indexOf( "://" ) != aPrevInd
- 2 ||
109 aInd
!= aURL
.getLength() - 1 )
111 aURL
= aURL
.copy( 0, aInd
);
119 bool findURL( std::set
<OUString
> const & rContainer
, OUString
const & aURL
, OUString
& aResult
)
121 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
122 if( !rContainer
.empty() && !aURL
.isEmpty() )
124 OUString
aUrl( aURL
);
126 // each iteration remove last '/...' section from the aUrl
127 // while it's possible, up to the most left '://'
130 // first look for <url>/somename and then look for <url>/somename/...
131 auto aIter
= rContainer
.find( aUrl
);
132 if( aIter
!= rContainer
.end() )
139 OUString
tmpUrl( aUrl
);
140 if ( !tmpUrl
.endsWith("/") )
143 aIter
= rContainer
.lower_bound( tmpUrl
);
144 if( aIter
!= rContainer
.end() && aIter
->match( tmpUrl
) )
151 while( removeLastSegment( aUrl
) && !aUrl
.isEmpty() );
159 SysCredentialsConfig::SysCredentialsConfig()
160 : m_aConfigItem( this ),
161 m_bCfgInited( false )
165 void SysCredentialsConfig::initCfg()
167 osl::MutexGuard
aGuard( m_aMutex
);
170 const uno::Sequence
< OUString
> aURLs(
171 m_aConfigItem
.getSystemCredentialsURLs() );
172 m_aCfgContainer
.insert( aURLs
.begin(), aURLs
.end() );
177 void SysCredentialsConfig::writeCfg()
179 osl::MutexGuard
aGuard( m_aMutex
);
181 OSL_ENSURE( m_bCfgInited
, "SysCredentialsConfig::writeCfg : not initialized!" );
183 m_aConfigItem
.setSystemCredentialsURLs( comphelper::containerToSequence(m_aCfgContainer
) );
186 OUString
SysCredentialsConfig::find( OUString
const & aURL
)
188 osl::MutexGuard
aGuard( m_aMutex
);
190 if ( findURL( m_aMemContainer
, aURL
, aResult
) )
194 if ( findURL( m_aCfgContainer
, aURL
, aResult
) )
200 void SysCredentialsConfig::add( OUString
const & rURL
, bool bPersistent
)
202 ::osl::MutexGuard
aGuard( m_aMutex
);
206 m_aMemContainer
.erase( rURL
);
209 m_aCfgContainer
.insert( rURL
);
215 if ( m_aCfgContainer
.erase( rURL
) > 0 )
218 m_aMemContainer
.insert( rURL
);
222 void SysCredentialsConfig::remove( OUString
const & rURL
)
224 m_aMemContainer
.erase( rURL
);
227 if ( m_aCfgContainer
.erase( rURL
) > 0 )
231 uno::Sequence
< OUString
> SysCredentialsConfig::list( bool bOnlyPersistent
)
234 sal_Int32 nCount
= m_aCfgContainer
.size()
235 + ( bOnlyPersistent
? 0 : m_aMemContainer
.size() );
236 uno::Sequence
< OUString
> aResult( nCount
);
237 auto aResultRange
= asNonConstRange(aResult
);
240 for ( const auto& rItem
: m_aCfgContainer
)
242 aResultRange
[ n
] = rItem
;
246 if ( !bOnlyPersistent
)
248 for ( const auto& rItem
: m_aMemContainer
)
250 aResultRange
[ n
] = rItem
;
257 void SysCredentialsConfig::persistentConfigChanged()
259 ::osl::MutexGuard
aGuard( m_aMutex
);
260 m_bCfgInited
= false; // re-init on demand.
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */