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"
23 using namespace com::sun::star
;
25 bool XMLCodeNameProvider::_getCodeName( const uno::Any
& aAny
, OUString
& rCodeName
)
27 uno::Sequence
<beans::PropertyValue
> aProps
;
28 if( !(aAny
>>= aProps
) )
31 OUString
sCodeNameProp("CodeName");
32 sal_Int32 nPropCount
= aProps
.getLength();
33 for( sal_Int32 i
=0; i
<nPropCount
; i
++ )
35 if( aProps
[i
].Name
== sCodeNameProp
)
38 if( aProps
[i
].Value
>>= sCodeName
)
40 rCodeName
= sCodeName
;
49 XMLCodeNameProvider::XMLCodeNameProvider( ScDocument
* pDoc
) :
52 msCodeNameProp( "CodeName" )
56 XMLCodeNameProvider::~XMLCodeNameProvider()
60 sal_Bool SAL_CALL
XMLCodeNameProvider::hasByName( const OUString
& aName
)
61 throw (uno::RuntimeException
, std::exception
)
63 if( aName
== msDocName
)
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
.equals(aName
) )
72 mpDoc
->GetCodeName( i
, sCodeName
);
73 return !sCodeName
.isEmpty();
80 uno::Any SAL_CALL
XMLCodeNameProvider::getByName( const OUString
& aName
)
81 throw (container::NoSuchElementException
,
82 lang::WrappedTargetException
, uno::RuntimeException
, std::exception
)
85 uno::Sequence
<beans::PropertyValue
> aProps(1);
86 aProps
[0].Name
= msCodeNameProp
;
87 if( aName
== msDocName
)
89 OUString
sUCodeName( mpDoc
->GetCodeName() );
90 aProps
[0].Value
<<= sUCodeName
;
95 SCTAB nCount
= mpDoc
->GetTableCount();
96 OUString sSheetName
, sCodeName
;
97 for( SCTAB i
= 0; i
< nCount
; i
++ )
99 if( mpDoc
->GetName( i
, sSheetName
) && sSheetName
.equals(aName
) )
101 mpDoc
->GetCodeName( i
, sCodeName
);
102 aProps
[0].Value
<<= sCodeName
;
111 uno::Sequence
< OUString
> SAL_CALL
XMLCodeNameProvider::getElementNames( )
112 throw (uno::RuntimeException
, std::exception
)
114 SCTAB nCount
= mpDoc
->GetTableCount() + 1;
115 uno::Sequence
< OUString
> aNames( nCount
);
116 sal_Int32 nRealCount
= 0;
118 if( !mpDoc
->GetCodeName().isEmpty() )
119 aNames
[nRealCount
++] = msDocName
;
121 OUString sSheetName
, sCodeName
;
122 for( SCTAB i
= 0; i
< nCount
; i
++ )
124 mpDoc
->GetCodeName( i
, sCodeName
);
125 if (!sCodeName
.isEmpty())
127 if( mpDoc
->GetName( i
, sSheetName
) )
128 aNames
[nRealCount
++] = sSheetName
;
132 if( nCount
!= nRealCount
)
133 aNames
.realloc( nRealCount
);
138 uno::Type SAL_CALL
XMLCodeNameProvider::getElementType( )
139 throw (uno::RuntimeException
, std::exception
)
141 return cppu::UnoType
<uno::Sequence
<beans::PropertyValue
>>::get();
144 sal_Bool SAL_CALL
XMLCodeNameProvider::hasElements()
145 throw (uno::RuntimeException
, std::exception
)
147 if( !mpDoc
->GetCodeName().isEmpty() )
150 SCTAB nCount
= mpDoc
->GetTableCount();
151 OUString sSheetName
, sCodeName
;
152 for( SCTAB i
= 0; i
< nCount
; i
++ )
154 mpDoc
->GetCodeName( i
, sCodeName
);
155 if (!sCodeName
.isEmpty() && mpDoc
->GetName(i
, sSheetName
))
162 void XMLCodeNameProvider::set( const uno::Reference
< container::XNameAccess
>& xNameAccess
, ScDocument
*pDoc
)
165 OUString
sDocName("*doc*");
167 if( xNameAccess
->hasByName( sDocName
) )
169 aAny
= xNameAccess
->getByName( sDocName
);
170 if( _getCodeName( aAny
, sCodeName
) )
171 pDoc
->SetCodeName( sCodeName
);
174 SCTAB nCount
= pDoc
->GetTableCount();
176 for( SCTAB i
= 0; i
< nCount
; i
++ )
178 if( pDoc
->GetName( i
, sSheetName
) &&
179 xNameAccess
->hasByName( sSheetName
) )
181 aAny
= xNameAccess
->getByName( sSheetName
);
182 if( _getCodeName( aAny
, sCodeName
) )
183 pDoc
->SetCodeName( i
, sCodeName
);
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */