LibreOffice Module sc (master) 1
XMLCodeNameProvider.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21#include <document.hxx>
22#include <com/sun/star/beans/PropertyValue.hpp>
23
26
27using namespace com::sun::star;
28
29bool XMLCodeNameProvider::_getCodeName( const uno::Any& aAny, OUString& rCodeName )
30{
31 uno::Sequence<beans::PropertyValue> aProps;
32 if( !(aAny >>= aProps) )
33 return false;
34
35 for( const auto& rProp : std::as_const(aProps) )
36 {
37 if( rProp.Name == "CodeName" )
38 {
39 OUString sCodeName;
40 if( rProp.Value >>= sCodeName )
41 {
42 rCodeName = sCodeName;
43 return true;
44 }
45 }
46 }
47
48 return false;
49}
50
51constexpr OUStringLiteral gsDocName( u"*doc*" );
52constexpr OUStringLiteral gsCodeNameProp( u"CodeName" );
53
55 mpDoc( pDoc )
56{
57}
58
60{
61}
62
63sal_Bool SAL_CALL XMLCodeNameProvider::hasByName( const OUString& aName )
64{
65 if( aName == gsDocName )
66 return !mpDoc->GetCodeName().isEmpty();
67
69 OUString sSheetName, sCodeName;
70 for( SCTAB i = 0; i < nCount; i++ )
71 {
72 if( mpDoc->GetName( i, sSheetName ) && sSheetName == aName )
73 {
74 mpDoc->GetCodeName( i, sCodeName );
75 return !sCodeName.isEmpty();
76 }
77 }
78
79 return false;
80}
81
82uno::Any SAL_CALL XMLCodeNameProvider::getByName( const OUString& aName )
83{
84 uno::Any aRet;
85 if( aName == gsDocName )
86 {
87 OUString sUCodeName( mpDoc->GetCodeName() );
88 aRet <<= uno::Sequence{ comphelper::makePropertyValue(gsCodeNameProp, sUCodeName) };
89 return aRet;
90 }
91
93 OUString sSheetName, sCodeName;
94 for( SCTAB i = 0; i < nCount; i++ )
95 {
96 if( mpDoc->GetName( i, sSheetName ) && sSheetName == aName )
97 {
98 mpDoc->GetCodeName( i, sCodeName );
99 aRet <<= uno::Sequence{ comphelper::makePropertyValue(gsCodeNameProp, sCodeName) };
100 return aRet;
101 }
102 }
103
104 return aRet;
105}
106
107uno::Sequence< OUString > SAL_CALL XMLCodeNameProvider::getElementNames( )
108{
110 std::vector< OUString > aNames;
111 aNames.reserve(nCount);
112
113 if( !mpDoc->GetCodeName().isEmpty() )
114 aNames.push_back(gsDocName);
115
116 OUString sSheetName, sCodeName;
117 for( SCTAB i = 0; i < nCount; i++ )
118 {
119 mpDoc->GetCodeName( i, sCodeName );
120 if (!sCodeName.isEmpty())
121 {
122 if( mpDoc->GetName( i, sSheetName ) )
123 aNames.push_back(sSheetName);
124 }
125 }
126
127 return comphelper::containerToSequence(aNames);
128}
129
131{
133}
134
136{
137 if( !mpDoc->GetCodeName().isEmpty() )
138 return true;
139
141 OUString sSheetName, sCodeName;
142 for( SCTAB i = 0; i < nCount; i++ )
143 {
144 mpDoc->GetCodeName( i, sCodeName );
145 if (!sCodeName.isEmpty() && mpDoc->GetName(i, sSheetName))
146 return true;
147 }
148
149 return false;
150}
151
152void XMLCodeNameProvider::set( const uno::Reference< container::XNameAccess>& xNameAccess, ScDocument *pDoc )
153{
154 uno::Any aAny;
155 OUString sDocName("*doc*");
156 OUString sCodeName;
157 if( xNameAccess->hasByName( sDocName ) )
158 {
159 aAny = xNameAccess->getByName( sDocName );
160 if( _getCodeName( aAny, sCodeName ) )
161 pDoc->SetCodeName( sCodeName );
162 }
163
164 SCTAB nCount = pDoc->GetTableCount();
165 OUString sSheetName;
166 for( SCTAB i = 0; i < nCount; i++ )
167 {
168 if( pDoc->GetName( i, sSheetName ) &&
169 xNameAccess->hasByName( sSheetName ) )
170 {
171 aAny = xNameAccess->getByName( sSheetName );
172 if( _getCodeName( aAny, sCodeName ) )
173 pDoc->SetCodeName( i, sCodeName );
174 }
175 }
176}
177
178/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral gsCodeNameProp(u"CodeName")
constexpr OUStringLiteral gsDocName(u"*doc*")
void SetCodeName(const OUString &r)
Definition: document.hxx:610
const OUString & GetCodeName() const
Definition: document.hxx:609
SC_DLLPUBLIC bool GetName(SCTAB nTab, OUString &rName) const
Definition: document.cxx:204
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:297
virtual sal_Bool SAL_CALL hasElements() override
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
static bool _getCodeName(const css::uno::Any &aAny, OUString &rCodeName)
XMLCodeNameProvider(ScDocument *pDoc)
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
virtual css::uno::Type SAL_CALL getElementType() override
static void set(const css::uno::Reference< css::container::XNameAccess > &xNameAccess, ScDocument *pDoc)
virtual ~XMLCodeNameProvider() override
int nCount
OUString aName
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
int i
unsigned char sal_Bool
sal_Int16 SCTAB
Definition: types.hxx:22