LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / svl / source / passwordcontainer / syscreds.cxx
bloba3354b853a8236651a7fa3ab73fd7b36e27660b8
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 <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 ),
29 m_bInited( false ),
30 m_pOwner( pOwner )
32 uno::Sequence<OUString> aNode { "Office.Common/Passwords/AuthenticateUsingSystemCredentials" };
33 EnableNotification( aNode );
36 //virtual
37 void SysCredentialsConfigItem::Notify(
38 const uno::Sequence< OUString > & /*seqPropertyNames*/ )
41 ::osl::MutexGuard aGuard( m_aMutex );
42 m_bInited = false;
43 // rebuild m_seqURLs
44 getSystemCredentialsURLs();
46 m_pOwner->persistentConfigChanged();
49 void SysCredentialsConfigItem::ImplCommit()
51 // does nothing
54 uno::Sequence< OUString >
55 SysCredentialsConfigItem::getSystemCredentialsURLs()
57 ::osl::MutexGuard aGuard( m_aMutex );
58 if ( !m_bInited )
60 // read config item
61 uno::Sequence<OUString> aPropNames { "AuthenticateUsingSystemCredentials" };
62 uno::Sequence< uno::Any > aAnyValues(
63 utl::ConfigItem::GetProperties( aPropNames ) );
65 OSL_ENSURE(
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() ) )
74 m_seqURLs = aValues;
75 m_bInited = true;
78 return m_seqURLs;
81 void SysCredentialsConfigItem::setSystemCredentialsURLs(
82 const uno::Sequence< OUString > & seqURLList )
84 ::osl::MutexGuard aGuard( m_aMutex );
86 // write config item.
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;
94 m_bInited = true;
98 namespace
100 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
101 bool removeLastSegment( OUString & aURL )
103 sal_Int32 aInd = aURL.lastIndexOf( '/' );
105 if( aInd > 0 )
107 sal_Int32 aPrevInd = aURL.lastIndexOf( '/', aInd );
108 if ( aURL.indexOf( "://" ) != aPrevInd - 2 ||
109 aInd != aURL.getLength() - 1 )
111 aURL = aURL.copy( 0, aInd );
112 return true;
116 return false;
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() )
134 aResult = *aIter;
135 return true;
137 else
139 OUString tmpUrl( aUrl );
140 if ( !tmpUrl.endsWith("/") )
141 tmpUrl += "/";
143 aIter = rContainer.lower_bound( tmpUrl );
144 if( aIter != rContainer.end() && aIter->match( tmpUrl ) )
146 aResult = *aIter;
147 return true;
151 while( removeLastSegment( aUrl ) && !aUrl.isEmpty() );
153 aResult.clear();
154 return false;
157 } // namespace
159 SysCredentialsConfig::SysCredentialsConfig()
160 : m_aConfigItem( this ),
161 m_bCfgInited( false )
165 void SysCredentialsConfig::initCfg()
167 osl::MutexGuard aGuard( m_aMutex );
168 if ( !m_bCfgInited )
170 const uno::Sequence< OUString > aURLs(
171 m_aConfigItem.getSystemCredentialsURLs() );
172 m_aCfgContainer.insert( aURLs.begin(), aURLs.end() );
173 m_bCfgInited = true;
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 );
189 OUString aResult;
190 if ( findURL( m_aMemContainer, aURL, aResult ) )
191 return aResult;
193 initCfg();
194 if ( findURL( m_aCfgContainer, aURL, aResult ) )
195 return aResult;
197 return OUString();
200 void SysCredentialsConfig::add( OUString const & rURL, bool bPersistent )
202 ::osl::MutexGuard aGuard( m_aMutex );
204 if ( bPersistent )
206 m_aMemContainer.erase( rURL );
208 initCfg();
209 m_aCfgContainer.insert( rURL );
210 writeCfg();
212 else
214 initCfg();
215 if ( m_aCfgContainer.erase( rURL ) > 0 )
216 writeCfg();
218 m_aMemContainer.insert( rURL );
222 void SysCredentialsConfig::remove( OUString const & rURL )
224 m_aMemContainer.erase( rURL );
226 initCfg();
227 if ( m_aCfgContainer.erase( rURL ) > 0 )
228 writeCfg();
231 uno::Sequence< OUString > SysCredentialsConfig::list( bool bOnlyPersistent )
233 initCfg();
234 sal_Int32 nCount = m_aCfgContainer.size()
235 + ( bOnlyPersistent ? 0 : m_aMemContainer.size() );
236 uno::Sequence< OUString > aResult( nCount );
237 auto aResultRange = asNonConstRange(aResult);
238 sal_Int32 n = 0;
240 for ( const auto& rItem : m_aCfgContainer )
242 aResultRange[ n ] = rItem;
243 ++n;
246 if ( !bOnlyPersistent )
248 for ( const auto& rItem : m_aMemContainer )
250 aResultRange[ n ] = rItem;
251 ++n;
254 return aResult;
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: */