Bump version to 4.3-4
[LibreOffice.git] / sc / source / filter / xml / XMLCodeNameProvider.cxx
blobe86f5349cac5eff8705c9e230259b4be7116cdf9
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 "XMLCodeNameProvider.hxx"
21 #include "document.hxx"
23 using namespace com::sun::star;
26 bool XMLCodeNameProvider::_getCodeName( const uno::Any& aAny, OUString& rCodeName )
28 uno::Sequence<beans::PropertyValue> aProps;
29 if( !(aAny >>= aProps) )
30 return false;
32 OUString sCodeNameProp("CodeName");
33 sal_Int32 nPropCount = aProps.getLength();
34 for( sal_Int32 i=0; i<nPropCount; i++ )
36 if( aProps[i].Name == sCodeNameProp )
38 OUString sCodeName;
39 if( aProps[i].Value >>= sCodeName )
41 rCodeName = sCodeName;
42 return true;
47 return false;
51 XMLCodeNameProvider::XMLCodeNameProvider( ScDocument* pDoc ) :
52 mpDoc( pDoc ),
53 msDocName( "*doc*" ),
54 msCodeNameProp( "CodeName" )
58 XMLCodeNameProvider::~XMLCodeNameProvider()
62 sal_Bool SAL_CALL XMLCodeNameProvider::hasByName( const OUString& aName )
63 throw (uno::RuntimeException, std::exception )
65 if( aName == msDocName )
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.equals(aName) )
74 mpDoc->GetCodeName( i, sCodeName );
75 return !sCodeName.isEmpty();
79 return false;
82 uno::Any SAL_CALL XMLCodeNameProvider::getByName( const OUString& aName )
83 throw (container::NoSuchElementException,
84 lang::WrappedTargetException, uno::RuntimeException, std::exception)
86 uno::Any aRet;
87 uno::Sequence<beans::PropertyValue> aProps(1);
88 aProps[0].Name = msCodeNameProp;
89 if( aName == msDocName )
91 OUString sUCodeName( mpDoc->GetCodeName() );
92 aProps[0].Value <<= sUCodeName;
93 aRet <<= aProps;
94 return aRet;
97 SCTAB nCount = mpDoc->GetTableCount();
98 OUString sSheetName, sCodeName;
99 for( SCTAB i = 0; i < nCount; i++ )
101 if( mpDoc->GetName( i, sSheetName ) && sSheetName.equals(aName) )
103 mpDoc->GetCodeName( i, sCodeName );
104 aProps[0].Value <<= sCodeName;
105 aRet <<= aProps;
106 return aRet;
110 return aRet;
113 uno::Sequence< OUString > SAL_CALL XMLCodeNameProvider::getElementNames( )
114 throw (uno::RuntimeException, std::exception)
116 SCTAB nCount = mpDoc->GetTableCount() + 1;
117 uno::Sequence< OUString > aNames( nCount );
118 sal_Int32 nRealCount = 0;
120 if( !mpDoc->GetCodeName().isEmpty() )
121 aNames[nRealCount++] = msDocName;
123 OUString sSheetName, sCodeName;
124 for( SCTAB i = 0; i < nCount; i++ )
126 mpDoc->GetCodeName( i, sCodeName );
127 if (!sCodeName.isEmpty())
129 if( mpDoc->GetName( i, sSheetName ) )
130 aNames[nRealCount++] = sSheetName;
134 if( nCount != nRealCount )
135 aNames.realloc( nRealCount );
137 return aNames;
140 uno::Type SAL_CALL XMLCodeNameProvider::getElementType( )
141 throw (uno::RuntimeException, std::exception)
143 return getCppuType(static_cast<uno::Sequence<beans::PropertyValue>*>(0));
146 sal_Bool SAL_CALL XMLCodeNameProvider::hasElements()
147 throw (uno::RuntimeException, std::exception )
149 if( !mpDoc->GetCodeName().isEmpty() )
150 return sal_True;
152 SCTAB nCount = mpDoc->GetTableCount();
153 OUString sSheetName, sCodeName;
154 for( SCTAB i = 0; i < nCount; i++ )
156 mpDoc->GetCodeName( i, sCodeName );
157 if (!sCodeName.isEmpty() && mpDoc->GetName(i, sSheetName))
158 return sal_True;
161 return false;
164 void XMLCodeNameProvider::set( const uno::Reference< container::XNameAccess>& xNameAccess, ScDocument *pDoc )
166 uno::Any aAny;
167 OUString sDocName("*doc*");
168 OUString sCodeName;
169 if( xNameAccess->hasByName( sDocName ) )
171 aAny = xNameAccess->getByName( sDocName );
172 if( _getCodeName( aAny, sCodeName ) )
173 pDoc->SetCodeName( sCodeName );
176 SCTAB nCount = pDoc->GetTableCount();
177 OUString sSheetName;
178 for( SCTAB i = 0; i < nCount; i++ )
180 if( pDoc->GetName( i, sSheetName ) &&
181 xNameAccess->hasByName( sSheetName ) )
183 aAny = xNameAccess->getByName( sSheetName );
184 if( _getCodeName( aAny, sCodeName ) )
185 pDoc->SetCodeName( i, sCodeName );
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */