bump product version to 6.3.0.0.beta1
[LibreOffice.git] / svl / source / passwordcontainer / syscreds.cxx
blobf438b3db7338ab30ae06289d7e73452f77a18706
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 ),
30 m_bInited( false ),
31 m_pOwner( pOwner )
33 uno::Sequence<OUString> aNode { "Office.Common/Passwords/AuthenticateUsingSystemCredentials" };
34 EnableNotification( aNode );
37 //virtual
38 void SysCredentialsConfigItem::Notify(
39 const uno::Sequence< OUString > & /*seqPropertyNames*/ )
42 ::osl::MutexGuard aGuard( m_aMutex );
43 m_bInited = false;
44 // rebuild m_seqURLs
45 getSystemCredentialsURLs();
47 m_pOwner->persistentConfigChanged();
50 void SysCredentialsConfigItem::ImplCommit()
52 // does nothing
55 uno::Sequence< OUString >
56 SysCredentialsConfigItem::getSystemCredentialsURLs()
58 ::osl::MutexGuard aGuard( m_aMutex );
59 if ( !m_bInited )
61 // read config item
62 uno::Sequence<OUString> aPropNames { "AuthenticateUsingSystemCredentials" };
63 uno::Sequence< uno::Any > aAnyValues(
64 utl::ConfigItem::GetProperties( aPropNames ) );
66 OSL_ENSURE(
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() ) )
75 m_seqURLs = aValues;
76 m_bInited = true;
79 return m_seqURLs;
82 void SysCredentialsConfigItem::setSystemCredentialsURLs(
83 const uno::Sequence< OUString > & seqURLList )
85 ::osl::MutexGuard aGuard( m_aMutex );
87 // write config item.
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;
97 m_bInited = true;
101 namespace
103 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
104 bool removeLastSegment( OUString & aURL )
106 sal_Int32 aInd = aURL.lastIndexOf( '/' );
108 if( aInd > 0 )
110 sal_Int32 aPrevInd = aURL.lastIndexOf( '/', aInd );
111 if ( aURL.indexOf( "://" ) != aPrevInd - 2 ||
112 aInd != aURL.getLength() - 1 )
114 aURL = aURL.copy( 0, aInd );
115 return true;
119 return false;
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() )
137 aResult = *aIter;
138 return true;
140 else
142 OUString tmpUrl( aUrl );
143 if ( !tmpUrl.endsWith("/") )
144 tmpUrl += "/";
146 aIter = rContainer.lower_bound( tmpUrl );
147 if( aIter != rContainer.end() && aIter->match( tmpUrl ) )
149 aResult = *aIter;
150 return true;
154 while( removeLastSegment( aUrl ) && !aUrl.isEmpty() );
156 aResult.clear();
157 return false;
160 } // namespace
162 SysCredentialsConfig::SysCredentialsConfig()
163 : m_aConfigItem( this ),
164 m_bCfgInited( false )
168 void SysCredentialsConfig::initCfg()
170 osl::MutexGuard aGuard( m_aMutex );
171 if ( !m_bCfgInited )
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 ] );
178 m_bCfgInited = true;
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 );
194 OUString aResult;
195 if ( findURL( m_aMemContainer, aURL, aResult ) )
196 return aResult;
198 initCfg();
199 if ( findURL( m_aCfgContainer, aURL, aResult ) )
200 return aResult;
202 return OUString();
205 void SysCredentialsConfig::add( OUString const & rURL, bool bPersistent )
207 ::osl::MutexGuard aGuard( m_aMutex );
209 if ( bPersistent )
211 m_aMemContainer.erase( rURL );
213 initCfg();
214 m_aCfgContainer.insert( rURL );
215 writeCfg();
217 else
219 initCfg();
220 if ( m_aCfgContainer.erase( rURL ) > 0 )
221 writeCfg();
223 m_aMemContainer.insert( rURL );
227 void SysCredentialsConfig::remove( OUString const & rURL )
229 m_aMemContainer.erase( rURL );
231 initCfg();
232 if ( m_aCfgContainer.erase( rURL ) > 0 )
233 writeCfg();
236 uno::Sequence< OUString > SysCredentialsConfig::list( bool bOnlyPersistent )
238 initCfg();
239 sal_Int32 nCount = m_aCfgContainer.size()
240 + ( bOnlyPersistent ? 0 : m_aMemContainer.size() );
241 uno::Sequence< OUString > aResult( nCount );
243 sal_Int32 n = 0;
245 for ( const auto& rItem : m_aCfgContainer )
247 aResult[ n ] = rItem;
248 ++n;
251 if ( !bOnlyPersistent )
253 for ( const auto& rItem : m_aMemContainer )
255 aResult[ n ] = rItem;
256 ++n;
259 return aResult;
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: */