bump product version to 4.1.6.2
[LibreOffice.git] / svl / source / passwordcontainer / syscreds.cxx
blob83e3b11e02e2eec3dd7786743da6c5f25c84182c
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"
23 using namespace com::sun::star;
25 SysCredentialsConfigItem::SysCredentialsConfigItem(
26 SysCredentialsConfig * pOwner )
27 : utl::ConfigItem( OUString("Office.Common/Passwords"),
28 CONFIG_MODE_IMMEDIATE_UPDATE ),
29 m_bInited( false ),
30 m_pOwner( pOwner )
32 uno::Sequence< OUString > aNode( 1 );
33 aNode[ 0 ] = OUString( "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::Commit()
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( 1 );
63 aPropNames[ 0 ] = OUString( "AuthenticateUsingSystemCredentials" );
64 uno::Sequence< uno::Any > aAnyValues(
65 utl::ConfigItem::GetProperties( aPropNames ) );
67 OSL_ENSURE(
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() ) )
76 m_seqURLs = aValues;
77 m_bInited = true;
80 return m_seqURLs;
83 void SysCredentialsConfigItem::setSystemCredentialsURLs(
84 const uno::Sequence< OUString > & seqURLList )
86 ::osl::MutexGuard aGuard( m_aMutex );
88 // write config item.
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;
98 m_bInited = true;
101 //============================================================================
103 namespace
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( '/' ) );
110 if( aInd > 0 )
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 );
117 return true;
121 return false;
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() )
139 aResult = *aIter;
140 return true;
142 else
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 ) )
151 aResult = *aIter;
152 return true;
156 while( removeLastSegment( aUrl ) && !aUrl.isEmpty() );
158 aResult = OUString();
159 return false;
162 } // namespace
164 SysCredentialsConfig::SysCredentialsConfig()
165 : m_aConfigItem( this ),
166 m_bCfgInited( false )
170 void SysCredentialsConfig::initCfg()
172 osl::MutexGuard aGuard( m_aMutex );
173 if ( !m_bCfgInited )
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 ] );
180 m_bCfgInited = true;
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();
193 sal_Int32 n = 0;
195 while ( it != end )
197 aURLs[ n ] = *it;
198 ++it;
199 ++n;
202 m_aConfigItem.setSystemCredentialsURLs( aURLs );
205 OUString SysCredentialsConfig::find( OUString const & aURL )
207 osl::MutexGuard aGuard( m_aMutex );
208 OUString aResult;
209 if ( findURL( m_aMemContainer, aURL, aResult ) )
210 return aResult;
212 initCfg();
213 if ( findURL( m_aCfgContainer, aURL, aResult ) )
214 return aResult;
216 return OUString();
219 void SysCredentialsConfig::add( OUString const & rURL, bool bPersistent )
221 ::osl::MutexGuard aGuard( m_aMutex );
223 if ( bPersistent )
225 m_aMemContainer.erase( rURL );
227 initCfg();
228 m_aCfgContainer.insert( rURL );
229 writeCfg();
231 else
233 initCfg();
234 if ( m_aCfgContainer.erase( rURL ) > 0 )
235 writeCfg();
237 m_aMemContainer.insert( rURL );
241 void SysCredentialsConfig::remove( OUString const & rURL )
243 m_aMemContainer.erase( rURL );
245 initCfg();
246 if ( m_aCfgContainer.erase( rURL ) > 0 )
247 writeCfg();
250 uno::Sequence< OUString > SysCredentialsConfig::list( bool bOnlyPersistent )
252 initCfg();
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();
259 sal_Int32 n = 0;
261 while ( it != end )
263 aResult[ n ] = *it;
264 ++it;
265 ++n;
268 if ( !bOnlyPersistent )
270 it = m_aMemContainer.begin();
271 end = m_aMemContainer.end();
273 while ( it != end )
275 aResult[ n ] = *it;
276 ++it;
277 ++n;
280 return aResult;
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: */