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 "XMLCodeNameProvider.hxx"
21 #include <document.hxx>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <comphelper/sequence.hxx>
25 using namespace com::sun::star
;
27 bool XMLCodeNameProvider::_getCodeName( const uno::Any
& aAny
, OUString
& rCodeName
)
29 uno::Sequence
<beans::PropertyValue
> aProps
;
30 if( !(aAny
>>= aProps
) )
33 for( const auto& rProp
: std::as_const(aProps
) )
35 if( rProp
.Name
== "CodeName" )
38 if( rProp
.Value
>>= sCodeName
)
40 rCodeName
= sCodeName
;
49 static constexpr OUStringLiteral
gsDocName( "*doc*" );
50 static constexpr OUStringLiteral
gsCodeNameProp( "CodeName" );
52 XMLCodeNameProvider::XMLCodeNameProvider( ScDocument
* pDoc
) :
57 XMLCodeNameProvider::~XMLCodeNameProvider()
61 sal_Bool SAL_CALL
XMLCodeNameProvider::hasByName( const OUString
& aName
)
63 if( aName
== gsDocName
)
64 return !mpDoc
->GetCodeName().isEmpty();
66 SCTAB nCount
= mpDoc
->GetTableCount();
67 OUString sSheetName
, sCodeName
;
68 for( SCTAB i
= 0; i
< nCount
; i
++ )
70 if( mpDoc
->GetName( i
, sSheetName
) && sSheetName
== aName
)
72 mpDoc
->GetCodeName( i
, sCodeName
);
73 return !sCodeName
.isEmpty();
80 uno::Any SAL_CALL
XMLCodeNameProvider::getByName( const OUString
& aName
)
83 uno::Sequence
<beans::PropertyValue
> aProps(1);
84 aProps
[0].Name
= gsCodeNameProp
;
85 if( aName
== gsDocName
)
87 OUString
sUCodeName( mpDoc
->GetCodeName() );
88 aProps
[0].Value
<<= sUCodeName
;
93 SCTAB nCount
= mpDoc
->GetTableCount();
94 OUString sSheetName
, sCodeName
;
95 for( SCTAB i
= 0; i
< nCount
; i
++ )
97 if( mpDoc
->GetName( i
, sSheetName
) && sSheetName
== aName
)
99 mpDoc
->GetCodeName( i
, sCodeName
);
100 aProps
[0].Value
<<= sCodeName
;
109 uno::Sequence
< OUString
> SAL_CALL
XMLCodeNameProvider::getElementNames( )
111 SCTAB nCount
= mpDoc
->GetTableCount() + 1;
112 std::vector
< OUString
> aNames
;
113 aNames
.reserve(nCount
);
115 if( !mpDoc
->GetCodeName().isEmpty() )
116 aNames
.push_back(gsDocName
);
118 OUString sSheetName
, sCodeName
;
119 for( SCTAB i
= 0; i
< nCount
; i
++ )
121 mpDoc
->GetCodeName( i
, sCodeName
);
122 if (!sCodeName
.isEmpty())
124 if( mpDoc
->GetName( i
, sSheetName
) )
125 aNames
.push_back(sSheetName
);
129 return comphelper::containerToSequence(aNames
);
132 uno::Type SAL_CALL
XMLCodeNameProvider::getElementType( )
134 return cppu::UnoType
<uno::Sequence
<beans::PropertyValue
>>::get();
137 sal_Bool SAL_CALL
XMLCodeNameProvider::hasElements()
139 if( !mpDoc
->GetCodeName().isEmpty() )
142 SCTAB nCount
= mpDoc
->GetTableCount();
143 OUString sSheetName
, sCodeName
;
144 for( SCTAB i
= 0; i
< nCount
; i
++ )
146 mpDoc
->GetCodeName( i
, sCodeName
);
147 if (!sCodeName
.isEmpty() && mpDoc
->GetName(i
, sSheetName
))
154 void XMLCodeNameProvider::set( const uno::Reference
< container::XNameAccess
>& xNameAccess
, ScDocument
*pDoc
)
157 OUString
sDocName("*doc*");
159 if( xNameAccess
->hasByName( sDocName
) )
161 aAny
= xNameAccess
->getByName( sDocName
);
162 if( _getCodeName( aAny
, sCodeName
) )
163 pDoc
->SetCodeName( sCodeName
);
166 SCTAB nCount
= pDoc
->GetTableCount();
168 for( SCTAB i
= 0; i
< nCount
; i
++ )
170 if( pDoc
->GetName( i
, sSheetName
) &&
171 xNameAccess
->hasByName( sSheetName
) )
173 aAny
= xNameAccess
->getByName( sSheetName
);
174 if( _getCodeName( aAny
, sCodeName
) )
175 pDoc
->SetCodeName( i
, sCodeName
);
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */