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 
20 #include "XMLCodeNameProvider.hxx"
21 #include <document.hxx>
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <comphelper/sequence.hxx>
24 
25 using namespace com::sun::star;
26 
27 bool XMLCodeNameProvider::_getCodeName( const uno::Any& aAny, OUString& rCodeName )
28 {
29  uno::Sequence<beans::PropertyValue> aProps;
30  if( !(aAny >>= aProps) )
31  return false;
32 
33  for( const auto& rProp : std::as_const(aProps) )
34  {
35  if( rProp.Name == "CodeName" )
36  {
37  OUString sCodeName;
38  if( rProp.Value >>= sCodeName )
39  {
40  rCodeName = sCodeName;
41  return true;
42  }
43  }
44  }
45 
46  return false;
47 }
48 
49 constexpr OUStringLiteral gsDocName( u"*doc*" );
50 constexpr OUStringLiteral gsCodeNameProp( u"CodeName" );
51 
53  mpDoc( pDoc )
54 {
55 }
56 
58 {
59 }
60 
61 sal_Bool SAL_CALL XMLCodeNameProvider::hasByName( const OUString& aName )
62 {
63  if( aName == gsDocName )
64  return !mpDoc->GetCodeName().isEmpty();
65 
67  OUString sSheetName, sCodeName;
68  for( SCTAB i = 0; i < nCount; i++ )
69  {
70  if( mpDoc->GetName( i, sSheetName ) && sSheetName == aName )
71  {
72  mpDoc->GetCodeName( i, sCodeName );
73  return !sCodeName.isEmpty();
74  }
75  }
76 
77  return false;
78 }
79 
80 uno::Any SAL_CALL XMLCodeNameProvider::getByName( const OUString& aName )
81 {
82  uno::Any aRet;
83  uno::Sequence<beans::PropertyValue> aProps(1);
84  aProps[0].Name = gsCodeNameProp;
85  if( aName == gsDocName )
86  {
87  OUString sUCodeName( mpDoc->GetCodeName() );
88  aProps[0].Value <<= sUCodeName;
89  aRet <<= aProps;
90  return aRet;
91  }
92 
94  OUString sSheetName, sCodeName;
95  for( SCTAB i = 0; i < nCount; i++ )
96  {
97  if( mpDoc->GetName( i, sSheetName ) && sSheetName == aName )
98  {
99  mpDoc->GetCodeName( i, sCodeName );
100  aProps[0].Value <<= sCodeName;
101  aRet <<= aProps;
102  return aRet;
103  }
104  }
105 
106  return aRet;
107 }
108 
109 uno::Sequence< OUString > SAL_CALL XMLCodeNameProvider::getElementNames( )
110 {
111  SCTAB nCount = mpDoc->GetTableCount() + 1;
112  std::vector< OUString > aNames;
113  aNames.reserve(nCount);
114 
115  if( !mpDoc->GetCodeName().isEmpty() )
116  aNames.push_back(gsDocName);
117 
118  OUString sSheetName, sCodeName;
119  for( SCTAB i = 0; i < nCount; i++ )
120  {
121  mpDoc->GetCodeName( i, sCodeName );
122  if (!sCodeName.isEmpty())
123  {
124  if( mpDoc->GetName( i, sSheetName ) )
125  aNames.push_back(sSheetName);
126  }
127  }
128 
129  return comphelper::containerToSequence(aNames);
130 }
131 
133 {
135 }
136 
138 {
139  if( !mpDoc->GetCodeName().isEmpty() )
140  return true;
141 
143  OUString sSheetName, sCodeName;
144  for( SCTAB i = 0; i < nCount; i++ )
145  {
146  mpDoc->GetCodeName( i, sCodeName );
147  if (!sCodeName.isEmpty() && mpDoc->GetName(i, sSheetName))
148  return true;
149  }
150 
151  return false;
152 }
153 
154 void XMLCodeNameProvider::set( const uno::Reference< container::XNameAccess>& xNameAccess, ScDocument *pDoc )
155 {
156  uno::Any aAny;
157  OUString sDocName("*doc*");
158  OUString sCodeName;
159  if( xNameAccess->hasByName( sDocName ) )
160  {
161  aAny = xNameAccess->getByName( sDocName );
162  if( _getCodeName( aAny, sCodeName ) )
163  pDoc->SetCodeName( sCodeName );
164  }
165 
166  SCTAB nCount = pDoc->GetTableCount();
167  OUString sSheetName;
168  for( SCTAB i = 0; i < nCount; i++ )
169  {
170  if( pDoc->GetName( i, sSheetName ) &&
171  xNameAccess->hasByName( sSheetName ) )
172  {
173  aAny = xNameAccess->getByName( sSheetName );
174  if( _getCodeName( aAny, sCodeName ) )
175  pDoc->SetCodeName( i, sCodeName );
176  }
177  }
178 }
179 
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Type SAL_CALL getElementType() override
virtual ~XMLCodeNameProvider() override
virtual sal_Bool SAL_CALL hasElements() override
virtual css::uno::Any SAL_CALL getByName(const OUString &aName) override
void SetCodeName(const OUString &r)
Definition: document.hxx:596
SC_DLLPUBLIC SCTAB GetTableCount() const
Definition: document.cxx:312
int nCount
static void set(const css::uno::Reference< css::container::XNameAccess > &xNameAccess, ScDocument *pDoc)
constexpr OUStringLiteral gsDocName(u"*doc*")
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
int i
unsigned char sal_Bool
XMLCodeNameProvider(ScDocument *pDoc)
constexpr OUStringLiteral gsCodeNameProp(u"CodeName")
static bool _getCodeName(const css::uno::Any &aAny, OUString &rCodeName)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
const OUString & GetCodeName() const
Definition: document.hxx:595
SC_DLLPUBLIC bool GetName(SCTAB nTab, OUString &rName) const
Definition: document.cxx:211
sal_Int16 SCTAB
Definition: types.hxx:23