tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / svl / source / passwordcontainer / syscreds.cxx
blobe3c7561479d29ae2e0228089074423c52e18e4d1
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( u"Office.Common/Passwords"_ustr, ConfigItemMode::NONE ),
29 m_bInited( false ),
30 m_pOwner( pOwner )
32 uno::Sequence<OUString> aNode { u"Office.Common/Passwords/AuthenticateUsingSystemCredentials"_ustr };
33 EnableNotification( aNode );
36 //virtual
37 void SysCredentialsConfigItem::Notify(
38 const uno::Sequence< OUString > & /*seqPropertyNames*/ )
41 std::unique_lock aGuard( m_aMutex );
42 m_bInited = false;
43 // rebuild m_seqURLs
44 getSystemCredentialsURLs(aGuard);
46 m_pOwner->persistentConfigChanged();
49 void SysCredentialsConfigItem::ImplCommit()
51 // does nothing
54 uno::Sequence< OUString >
55 SysCredentialsConfigItem::getSystemCredentialsURLs()
57 std::unique_lock aGuard(m_aMutex);
58 return getSystemCredentialsURLs(aGuard);
61 const uno::Sequence< OUString > &
62 SysCredentialsConfigItem::getSystemCredentialsURLs(std::unique_lock<std::mutex>& /*rGuard*/)
64 if ( !m_bInited )
66 // read config item
67 uno::Sequence<OUString> aPropNames { u"AuthenticateUsingSystemCredentials"_ustr };
68 uno::Sequence< uno::Any > aAnyValues(
69 utl::ConfigItem::GetProperties( aPropNames ) );
71 OSL_ENSURE(
72 aAnyValues.getLength() == 1,
73 "SysCredentialsConfigItem::getSystemCredentialsURLs: "
74 "Error reading config item!" );
76 uno::Sequence< OUString > aValues;
77 if ( ( aAnyValues[ 0 ] >>= aValues ) ||
78 ( !aAnyValues[ 0 ].hasValue() ) )
80 m_seqURLs = std::move(aValues);
81 m_bInited = true;
84 return m_seqURLs;
87 void SysCredentialsConfigItem::setSystemCredentialsURLs(
88 const uno::Sequence< OUString > & seqURLList )
90 // write config item.
91 uno::Sequence< OUString > aPropNames{ u"AuthenticateUsingSystemCredentials"_ustr };
92 uno::Sequence< uno::Any > aPropValues{ uno::Any(seqURLList) };
94 utl::ConfigItem::SetModified();
95 utl::ConfigItem::PutProperties( aPropNames, aPropValues );
97 std::unique_lock aGuard( m_aMutex );
99 m_seqURLs = seqURLList;
100 m_bInited = true;
104 namespace
106 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
107 bool removeLastSegment( OUString & aURL )
109 sal_Int32 aInd = aURL.lastIndexOf( '/' );
111 if( aInd > 0 )
113 sal_Int32 aPrevInd = aURL.lastIndexOf( '/', aInd );
114 if ( aURL.indexOf( "://" ) != aPrevInd - 2 ||
115 aInd != aURL.getLength() - 1 )
117 aURL = aURL.copy( 0, aInd );
118 return true;
122 return false;
125 bool findURL( std::set<OUString> const & rContainer, OUString const & aURL, OUString & aResult )
127 // TODO: This code is actually copied from svl/source/passwordcontainer.cxx
128 if( !rContainer.empty() && !aURL.isEmpty() )
130 OUString aUrl( aURL );
132 // each iteration remove last '/...' section from the aUrl
133 // while it's possible, up to the most left '://'
136 // first look for <url>/somename and then look for <url>/somename/...
137 auto aIter = rContainer.find( aUrl );
138 if( aIter != rContainer.end() )
140 aResult = *aIter;
141 return true;
143 else
145 OUString tmpUrl( aUrl );
146 if ( !tmpUrl.endsWith("/") )
147 tmpUrl += "/";
149 aIter = rContainer.lower_bound( tmpUrl );
150 if( aIter != rContainer.end() && aIter->match( tmpUrl ) )
152 aResult = *aIter;
153 return true;
157 while( removeLastSegment( aUrl ) && !aUrl.isEmpty() );
159 aResult.clear();
160 return false;
163 } // namespace
165 SysCredentialsConfig::SysCredentialsConfig()
166 : m_aConfigItem( this ),
167 m_bCfgInited( false )
171 void SysCredentialsConfig::initCfg(std::unique_lock<std::mutex>& /*rGuard*/)
173 if ( !m_bCfgInited )
175 const uno::Sequence< OUString > aURLs(
176 m_aConfigItem.getSystemCredentialsURLs() );
177 m_aCfgContainer.insert( aURLs.begin(), aURLs.end() );
178 m_bCfgInited = true;
182 void SysCredentialsConfig::writeCfg(std::unique_lock<std::mutex>& /*rGuard*/)
184 OSL_ENSURE( m_bCfgInited, "SysCredentialsConfig::writeCfg : not initialized!" );
186 m_aConfigItem.setSystemCredentialsURLs( comphelper::containerToSequence(m_aCfgContainer) );
189 OUString SysCredentialsConfig::find( OUString const & aURL )
191 std::unique_lock aGuard( m_aMutex );
192 OUString aResult;
193 if ( findURL( m_aMemContainer, aURL, aResult ) )
194 return aResult;
196 initCfg(aGuard);
197 if ( findURL( m_aCfgContainer, aURL, aResult ) )
198 return aResult;
200 return OUString();
203 void SysCredentialsConfig::add( OUString const & rURL, bool bPersistent )
205 std::unique_lock aGuard( m_aMutex );
207 if ( bPersistent )
209 m_aMemContainer.erase( rURL );
211 initCfg(aGuard);
212 m_aCfgContainer.insert( rURL );
213 writeCfg(aGuard);
215 else
217 initCfg(aGuard);
218 if ( m_aCfgContainer.erase( rURL ) > 0 )
219 writeCfg(aGuard);
221 m_aMemContainer.insert( rURL );
225 void SysCredentialsConfig::remove( OUString const & rURL )
227 std::unique_lock aGuard(m_aMutex);
229 m_aMemContainer.erase( rURL );
231 initCfg(aGuard);
232 if ( m_aCfgContainer.erase( rURL ) > 0 )
233 writeCfg(aGuard);
236 uno::Sequence< OUString > SysCredentialsConfig::list( bool bOnlyPersistent )
238 std::unique_lock aGuard(m_aMutex);
239 initCfg(aGuard);
240 sal_Int32 nCount = m_aCfgContainer.size()
241 + ( bOnlyPersistent ? 0 : m_aMemContainer.size() );
242 uno::Sequence< OUString > aResult( nCount );
243 auto aResultRange = asNonConstRange(aResult);
244 sal_Int32 n = 0;
246 for ( const auto& rItem : m_aCfgContainer )
248 aResultRange[ n ] = rItem;
249 ++n;
252 if ( !bOnlyPersistent )
254 for ( const auto& rItem : m_aMemContainer )
256 aResultRange[ n ] = rItem;
257 ++n;
260 return aResult;
263 void SysCredentialsConfig::persistentConfigChanged()
265 std::unique_lock aGuard( m_aMutex );
266 m_bCfgInited = false; // re-init on demand.
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */