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>
24 #include <comphelper/propertyvalue.hxx>
25 #include <comphelper/sequence.hxx>
27 using namespace com::sun::star
;
29 bool XMLCodeNameProvider::_getCodeName( const uno::Any
& aAny
, OUString
& rCodeName
)
31 uno::Sequence
<beans::PropertyValue
> aProps
;
32 if( !(aAny
>>= aProps
) )
35 for (const auto& rProp
: aProps
)
37 if( rProp
.Name
== "CodeName" )
40 if( rProp
.Value
>>= sCodeName
)
42 rCodeName
= sCodeName
;
51 constexpr OUString
gsDocName( u
"*doc*"_ustr
);
52 constexpr OUString
gsCodeNameProp( u
"CodeName"_ustr
);
54 XMLCodeNameProvider::XMLCodeNameProvider( ScDocument
* pDoc
) :
59 XMLCodeNameProvider::~XMLCodeNameProvider()
63 sal_Bool SAL_CALL
XMLCodeNameProvider::hasByName( const OUString
& aName
)
65 if( aName
== gsDocName
)
66 return !mpDoc
->GetCodeName().isEmpty();
68 SCTAB nCount
= mpDoc
->GetTableCount();
69 OUString sSheetName
, sCodeName
;
70 for( SCTAB i
= 0; i
< nCount
; i
++ )
72 if( mpDoc
->GetName( i
, sSheetName
) && sSheetName
== aName
)
74 mpDoc
->GetCodeName( i
, sCodeName
);
75 return !sCodeName
.isEmpty();
82 uno::Any SAL_CALL
XMLCodeNameProvider::getByName( const OUString
& aName
)
85 if( aName
== gsDocName
)
87 OUString
sUCodeName( mpDoc
->GetCodeName() );
88 aRet
<<= uno::Sequence
{ comphelper::makePropertyValue(gsCodeNameProp
, sUCodeName
) };
92 SCTAB nCount
= mpDoc
->GetTableCount();
93 OUString sSheetName
, sCodeName
;
94 for( SCTAB i
= 0; i
< nCount
; i
++ )
96 if( mpDoc
->GetName( i
, sSheetName
) && sSheetName
== aName
)
98 mpDoc
->GetCodeName( i
, sCodeName
);
99 aRet
<<= uno::Sequence
{ comphelper::makePropertyValue(gsCodeNameProp
, sCodeName
) };
107 uno::Sequence
< OUString
> SAL_CALL
XMLCodeNameProvider::getElementNames( )
109 SCTAB nCount
= mpDoc
->GetTableCount() + 1;
110 std::vector
< OUString
> aNames
;
111 aNames
.reserve(nCount
);
113 if( !mpDoc
->GetCodeName().isEmpty() )
114 aNames
.push_back(gsDocName
);
116 OUString sSheetName
, sCodeName
;
117 for( SCTAB i
= 0; i
< nCount
; i
++ )
119 mpDoc
->GetCodeName( i
, sCodeName
);
120 if (!sCodeName
.isEmpty())
122 if( mpDoc
->GetName( i
, sSheetName
) )
123 aNames
.push_back(sSheetName
);
127 return comphelper::containerToSequence(aNames
);
130 uno::Type SAL_CALL
XMLCodeNameProvider::getElementType( )
132 return cppu::UnoType
<uno::Sequence
<beans::PropertyValue
>>::get();
135 sal_Bool SAL_CALL
XMLCodeNameProvider::hasElements()
137 if( !mpDoc
->GetCodeName().isEmpty() )
140 SCTAB nCount
= mpDoc
->GetTableCount();
141 OUString sSheetName
, sCodeName
;
142 for( SCTAB i
= 0; i
< nCount
; i
++ )
144 mpDoc
->GetCodeName( i
, sCodeName
);
145 if (!sCodeName
.isEmpty() && mpDoc
->GetName(i
, sSheetName
))
152 void XMLCodeNameProvider::set( const uno::Reference
< container::XNameAccess
>& xNameAccess
, ScDocument
*pDoc
)
155 OUString
sDocName(u
"*doc*"_ustr
);
157 if( xNameAccess
->hasByName( sDocName
) )
159 aAny
= xNameAccess
->getByName( sDocName
);
160 if( _getCodeName( aAny
, sCodeName
) )
161 pDoc
->SetCodeName( sCodeName
);
164 SCTAB nCount
= pDoc
->GetTableCount();
166 for( SCTAB i
= 0; i
< nCount
; i
++ )
168 if( pDoc
->GetName( i
, sSheetName
) &&
169 xNameAccess
->hasByName( sSheetName
) )
171 aAny
= xNameAccess
->getByName( sSheetName
);
172 if( _getCodeName( aAny
, sCodeName
) )
173 pDoc
->SetCodeName( i
, sCodeName
);
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */