LibreOffice Module sw (master)  1
vbafield.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 #include "vbafield.hxx"
20 #include "vbarange.hxx"
21 #include <com/sun/star/beans/XPropertySet.hpp>
22 #include <com/sun/star/frame/XModel.hpp>
23 #include <com/sun/star/text/XTextFieldsSupplier.hpp>
24 #include <com/sun/star/text/FilenameDisplayFormat.hpp>
25 #include <com/sun/star/util/XRefreshable.hpp>
26 #include <com/sun/star/util/XUpdatable.hpp>
27 #include <ooo/vba/word/WdFieldType.hpp>
28 #include <basic/sberrors.hxx>
29 #include <cppuhelper/implbase.hxx>
30 #include <sal/log.hxx>
31 
32 using namespace ::ooo::vba;
33 using namespace ::com::sun::star;
34 
35 SwVbaField::SwVbaField( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, const uno::Reference< css::text::XTextField >& xTextField) : SwVbaField_BASE( rParent, rContext )
36 {
37  mxTextField.set( xTextField, uno::UNO_SET_THROW );
38 }
39 
41 {
42  uno::Reference< util::XUpdatable > xUpdatable( mxTextField, uno::UNO_QUERY );
43  if( xUpdatable.is() )
44  {
45  xUpdatable->update();
46  return true;
47  }
48  return false;
49 }
50 
51 // XHelperInterface
52 OUString
54 {
55  return "SwVbaField";
56 }
57 
58 uno::Sequence<OUString>
60 {
61  static uno::Sequence< OUString > const aServiceNames
62  {
63  "ooo.vba.word.Field"
64  };
65  return aServiceNames;
66 }
67 
68 namespace {
69 
70 // FIXME? copy and paste code
71 // the codes are copied from ww8par5.cxx
72 class SwVbaReadFieldParams
73 {
74 private:
75  OUString aData;
76  sal_Int32 nLen, nFnd, nNext, nSavPtr;
77  OUString aFieldName;
78 public:
79  explicit SwVbaReadFieldParams( const OUString& rData );
80 
81  long SkipToNextToken();
82 
83  sal_Int32 FindNextStringPiece( sal_Int32 _nStart );
84 
85  OUString GetResult() const;
86  const OUString& GetFieldName()const { return aFieldName; }
87 };
88 
89 }
90 
91 SwVbaReadFieldParams::SwVbaReadFieldParams( const OUString& _rData )
92  : aData( _rData ), nLen( _rData.getLength() ), nNext( 0 )
93 {
94  // First search for an opening parenthesis or a space or a quotation mark
95  // or a backslash, so that the field command
96  // (thus INCLUDEPICTURE or ...) is ignored.
97  while( (nLen > nNext) && (aData[ nNext ] == ' ') )
98  ++nNext;
99 
100  sal_Unicode c;
101  while( nLen > nNext
102  && (c = aData[ nNext ]) != ' '
103  && c != '"'
104  && c != '\\'
105  && c != 132
106  && c != 0x201c )
107  ++nNext;
108 
109  nFnd = nNext;
110  nSavPtr = nNext;
111  aFieldName = aData.copy( 0, nFnd );
112 }
113 
114 OUString SwVbaReadFieldParams::GetResult() const
115 {
116  return (-1 == nFnd)
117  ? OUString()
118  : aData.copy( nFnd, (nSavPtr - nFnd) );
119 }
120 
121 // ret: -2: NOT a '\' parameter but normal Text
122 long SwVbaReadFieldParams::SkipToNextToken()
123 {
124  long nRet = -1; // end
125  if (
126  (-1 != nNext) && (nLen > nNext) &&
127  -1 != (nFnd = FindNextStringPiece(nNext))
128  )
129  {
130  nSavPtr = nNext;
131 
132  if ('\\' == aData[nFnd] && '\\' != aData[nFnd + 1])
133  {
134  nRet = aData[++nFnd];
135  nNext = ++nFnd; // and set behind
136  }
137  else
138  {
139  nRet = -2;
140  if (
141  (-1 != nSavPtr ) &&
142  (
143  ('"' == aData[nSavPtr - 1]) ||
144  (0x201d == aData[nSavPtr - 1])
145  )
146  )
147  {
148  --nSavPtr;
149  }
150  }
151  }
152  return nRet;
153 }
154 
155 // FindNextPara is searching for the next Backslash-Parameter or the next string
156 // until blank or the next "\" or until the closing quotation mark
157 // or until the string end of pStr.
158 
159 // Output ppNext (if ppNext != 0) beginning of the search for the next parameter or 0
160 
161 // Return value: 0 if String-End reached, otherwise begin of the parameter or the string
162 
163 sal_Int32 SwVbaReadFieldParams::FindNextStringPiece(const sal_Int32 nStart)
164 {
165  sal_Int32 n = ( -1 == nStart ) ? nFnd : nStart; // Start
166  sal_Int32 n2; // End
167 
168  nNext = -1; // Default for not found
169 
170  while( (nLen > n) && (aData[ n ] == ' ') )
171  ++n;
172 
173  if( nLen == n )
174  return -1; // String End reached!
175 
176  if( (aData[ n ] == '"') // quotation marks are in front of parenthesis?
177  || (aData[ n ] == 0x201c)
178  || (aData[ n ] == 132) )
179  {
180  n++; // ignore quotation marks
181  n2 = n; // From here search for the end
182  while( (nLen > n2)
183  && (aData[ n2 ] != '"')
184  && (aData[ n2 ] != 0x201d)
185  && (aData[ n2 ] != 147) )
186  n2++; // Search for the end of the parenthesis
187  }
188  else // no quotation marks
189  {
190  n2 = n; // from here search for the end
191  while( (nLen > n2) && (aData[ n2 ] != ' ') ) // Search for the end of the parenthesis
192  {
193  if( aData[ n2 ] == '\\' )
194  {
195  if( aData[ n2+1 ] == '\\' )
196  n2 += 2; // double-backslash -> OK
197  else
198  {
199  if( n2 > n )
200  n2--;
201  break; // single-backslash -> End
202  }
203  }
204  else
205  n2++; // no backslash -> OK
206  }
207  }
208  if( nLen > n2 )
209  {
210  if(aData[ n2 ] != ' ') n2++;
211  nNext = n2;
212  }
213  return n;
214 }
215 
216 // SwVbaFields
217 
218 static uno::Any lcl_createField( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel, const uno::Any& aSource )
219 {
220  uno::Reference< text::XTextField > xTextField( aSource, uno::UNO_QUERY_THROW );
221  uno::Reference< text::XTextDocument > xTextDocument( xModel, uno::UNO_QUERY_THROW );
222  uno::Reference< word::XField > xField( new SwVbaField( xParent, xContext, xTextField ) );
223  return uno::makeAny( xField );
224 }
225 
226 namespace {
227 
228 class FieldEnumeration : public ::cppu::WeakImplHelper< css::container::XEnumeration >
229 {
230  uno::Reference< XHelperInterface > mxParent;
231  uno::Reference< uno::XComponentContext > mxContext;
232  uno::Reference< frame::XModel > mxModel;
233  uno::Reference< container::XEnumeration > mxEnumeration;
234 public:
235  FieldEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xModel, const uno::Reference< container::XEnumeration >& xEnumeration ) : mxParent( xParent ), mxContext( xContext ), mxModel( xModel ), mxEnumeration( xEnumeration )
236  {
237  }
238  virtual sal_Bool SAL_CALL hasMoreElements( ) override
239  {
240  return mxEnumeration->hasMoreElements();
241  }
242  virtual uno::Any SAL_CALL nextElement( ) override
243  {
244  if ( !hasMoreElements() )
245  throw container::NoSuchElementException();
246  return lcl_createField( mxParent, mxContext, mxModel, mxEnumeration->nextElement() );
247  }
248 };
249 
250 class FieldCollectionHelper : public ::cppu::WeakImplHelper< container::XIndexAccess,
251  container::XEnumerationAccess >
252 {
253  uno::Reference< XHelperInterface > mxParent;
254  uno::Reference< uno::XComponentContext > mxContext;
255  uno::Reference< frame::XModel > mxModel;
256  uno::Reference< container::XEnumerationAccess > mxEnumerationAccess;
257 public:
259  FieldCollectionHelper( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel ) : mxParent( xParent ), mxContext( xContext ), mxModel( xModel )
260  {
261  uno::Reference< text::XTextFieldsSupplier > xSupp( xModel, uno::UNO_QUERY_THROW );
262  mxEnumerationAccess.set( xSupp->getTextFields(), uno::UNO_SET_THROW );
263  }
264  // XElementAccess
265  virtual uno::Type SAL_CALL getElementType( ) override { return mxEnumerationAccess->getElementType(); }
266  virtual sal_Bool SAL_CALL hasElements( ) override { return mxEnumerationAccess->hasElements(); }
267  // XIndexAccess
268  virtual ::sal_Int32 SAL_CALL getCount( ) override
269  {
270  uno::Reference< container::XEnumeration > xEnumeration = mxEnumerationAccess->createEnumeration();
271  sal_Int32 nCount = 0;
272  while( xEnumeration->hasMoreElements() )
273  {
274  ++nCount;
275  xEnumeration->nextElement();
276  }
277  return nCount;
278  }
279  virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
280  {
281  if( Index < 0 || Index >= getCount() )
282  throw lang::IndexOutOfBoundsException();
283 
284  uno::Reference< container::XEnumeration > xEnumeration = mxEnumerationAccess->createEnumeration();
285  sal_Int32 nCount = 0;
286  while( xEnumeration->hasMoreElements() )
287  {
288  if( nCount == Index )
289  {
290  return xEnumeration->nextElement();
291  }
292  ++nCount;
293  }
294  throw lang::IndexOutOfBoundsException();
295  }
296  // XEnumerationAccess
297  virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) override
298  {
299  uno::Reference< container::XEnumeration > xEnumeration = mxEnumerationAccess->createEnumeration();
300  return uno::Reference< container::XEnumeration >( new FieldEnumeration( mxParent, mxContext, mxModel, xEnumeration ) );
301  }
302 };
303 
304 }
305 
306 SwVbaFields::SwVbaFields( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xModel ) : SwVbaFields_BASE( xParent, xContext , uno::Reference< container::XIndexAccess >( new FieldCollectionHelper( xParent, xContext, xModel ) ) ), mxModel( xModel )
307 {
308  mxMSF.set( mxModel, uno::UNO_QUERY_THROW );
309 }
310 
311 uno::Reference< word::XField > SAL_CALL
312 SwVbaFields::Add( const css::uno::Reference< ::ooo::vba::word::XRange >& Range, const css::uno::Any& Type, const css::uno::Any& Text, const css::uno::Any& /*PreserveFormatting*/ )
313 {
314  sal_Int32 nType = word::WdFieldType::wdFieldEmpty;
315  Type >>= nType;
316  OUString sText;
317  Text >>= sText;
318 
319  OUString sFieldName;
320  if( ( nType == word::WdFieldType::wdFieldEmpty ) && !sText.isEmpty() )
321  {
322  SwVbaReadFieldParams aReadParam(sText);
323  sFieldName = aReadParam.GetFieldName();
324  SAL_INFO("sw.vba", "the field name is " << sFieldName );
325  }
326 
327  uno::Reference< text::XTextContent > xTextField;
328  if( nType == word::WdFieldType::wdFieldFileName || sFieldName.equalsIgnoreAsciiCase("FILENAME") )
329  {
330  xTextField.set( Create_Field_FileName( sText ), uno::UNO_QUERY_THROW );
331  }
332  else if( nType == word::WdFieldType::wdFieldDocProperty || sFieldName.equalsIgnoreAsciiCase("DOCPROPERTY") )
333  {
334  xTextField.set( Create_Field_DocProperty( sText ), uno::UNO_QUERY_THROW );
335  }
336  else
337  {
338  throw uno::RuntimeException("Not implemented" );
339  }
340 
341  SwVbaRange& rVbaRange = dynamic_cast<SwVbaRange&>(*Range);
342  uno::Reference< text::XTextRange > xTextRange = rVbaRange.getXTextRange();
343  uno::Reference< text::XText > xText = xTextRange->getText();
344  xText->insertTextContent( xTextRange, xTextField, true );
345  return uno::Reference< word::XField >( new SwVbaField( mxParent, mxContext, uno::Reference< text::XTextField >( xTextField, uno::UNO_QUERY_THROW ) ) );
346 }
347 
348 uno::Reference< text::XTextField > SwVbaFields::Create_Field_FileName( const OUString& _text )
349 {
350  uno::Reference< text::XTextField > xTextField( mxMSF->createInstance("com.sun.star.text.TextField.FileName"), uno::UNO_QUERY_THROW );
351  sal_Int16 nFileFormat = text::FilenameDisplayFormat::NAME_AND_EXT;
352  if( !_text.isEmpty() )
353  {
354  long nRet;
355  SwVbaReadFieldParams aReadParam( _text );
356  while (-1 != (nRet = aReadParam.SkipToNextToken()))
357  {
358  switch (nRet)
359  {
360  case 'p':
361  nFileFormat = text::FilenameDisplayFormat::FULL;
362  break;
363  case '*':
364  //Skip over MERGEFORMAT
365  aReadParam.SkipToNextToken();
366  break;
367  default:
368  DebugHelper::basicexception(ERRCODE_BASIC_BAD_ARGUMENT, OUString());
369  break;
370  }
371  }
372  }
373 
374  uno::Reference< beans::XPropertySet > xProps( xTextField, uno::UNO_QUERY_THROW );
375  xProps->setPropertyValue("FileFormat", uno::makeAny( nFileFormat ) );
376 
377  return xTextField;
378 }
379 
380 namespace {
381 
382 struct DocPropertyTable
383 {
384  const char* sDocPropertyName;
385  const char* sFieldService;
386 };
387 
388 }
389 
390 static const DocPropertyTable aDocPropertyTables[] =
391 {
392  { "Author", "com.sun.star.text.textfield.docinfo.CreateAuthor" },
393  { "Bytes", nullptr },
394  { "Category", nullptr },
395  { "Characters",nullptr },
396  { "CharactersWithSpaces", nullptr },
397  { "Comments", "com.sun.star.text.textfield.docinfo.Description" },
398  { "Company", nullptr },
399  { "CreateTime", "com.sun.star.text.textfield.docinfo.CreateDateTime" },
400  { "HyperlinkBase", nullptr },
401  { "Keywords", "com.sun.star.text.textfield.docinfo.Keywords" },
402  { "LastPrinted", "com.sun.star.text.textfield.docinfo.PrintDateTime" },
403  { "LastSavedBy", "com.sun.star.text.textfield.docinfo.ChangeAuthor" },
404  { "LastSavedTime", "com.sun.star.text.textfield.docinfo.ChangeDateTime" },
405  { "Lines", nullptr },
406  { "Manager", nullptr },
407  { "NameofApplication", nullptr },
408  { "ODMADocID", nullptr },
409  { "Pages", "com.sun.star.text.textfield.PageCount" },
410  { "Paragraphs", "com.sun.star.text.textfield.ParagraphCount" },
411  { "RevisionNumber", "com.sun.star.text.textfield.docinfo.Revision" },
412  { "Security", nullptr },
413  { "Subject", "com.sun.star.text.textfield.docinfo.Subject" },
414  { "Template", "com.sun.star.text.textfield.TemplateName" },
415  { "Title", "com.sun.star.text.textfield.docinfo.Title" },
416  { "TotalEditingTime", "com.sun.star.text.textfield.docinfo.EditTime" },
417  { "Words", "com.sun.star.text.textfield.WordCount" },
418  { nullptr, nullptr }
419 };
420 
421 uno::Reference< text::XTextField > SwVbaFields::Create_Field_DocProperty( const OUString& _text )
422 {
423  OUString aDocProperty;
424  SwVbaReadFieldParams aReadParam( _text );
425  long nRet;
426  while( -1 != ( nRet = aReadParam.SkipToNextToken() ))
427  {
428  switch( nRet )
429  {
430  case -2:
431  if( aDocProperty.isEmpty() )
432  aDocProperty = aReadParam.GetResult();
433  break;
434  case '*':
435  //Skip over MERGEFORMAT
436  aReadParam.SkipToNextToken();
437  break;
438  }
439  }
440  aDocProperty = aDocProperty.replaceAll("\"", "");
441  SAL_INFO("sw.vba", "SwVbaFields::Create_Field_DocProperty, the document property name is " << aDocProperty );
442  if( aDocProperty.isEmpty() )
443  {
444  throw uno::RuntimeException();
445  }
446 
447  bool bCustom = true;
448  OUString sFieldService;
449  // find the build in document properties
450  for( const DocPropertyTable* pTable = aDocPropertyTables; pTable->sDocPropertyName != nullptr; pTable++ )
451  {
452  if( aDocProperty.equalsIgnoreAsciiCaseAscii( pTable->sDocPropertyName ) )
453  {
454  if( pTable->sFieldService != nullptr )
455  sFieldService = OUString::createFromAscii(pTable->sFieldService);
456  bCustom = false;
457  break;
458  }
459  }
460 
461  if( bCustom )
462  {
463  sFieldService = "com.sun.star.text.textfield.docinfo.Custom";
464  }
465  else if( sFieldService.isEmpty() )
466  {
467  throw uno::RuntimeException("Not implemented" );
468  }
469 
470  uno::Reference< text::XTextField > xTextField( mxMSF->createInstance( sFieldService ), uno::UNO_QUERY_THROW );
471 
472  if( bCustom )
473  {
474  uno::Reference< beans::XPropertySet > xProps( xTextField, uno::UNO_QUERY_THROW );
475  xProps->setPropertyValue("Name", uno::makeAny( aDocProperty ) );
476  }
477 
478  return xTextField;
479 }
480 
481 uno::Reference< container::XEnumeration > SAL_CALL
483 {
484  uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
485  return xEnumerationAccess->createEnumeration();
486 }
487 
488 // ScVbaCollectionBaseImpl
489 uno::Any
491 {
492  return lcl_createField( mxParent, mxContext, mxModel, aSource );
493 }
494 
495 sal_Int32 SAL_CALL SwVbaFields::Update()
496 {
497  sal_Int32 nUpdate = 1;
498  try
499  {
500  uno::Reference< text::XTextFieldsSupplier > xSupp( mxModel, uno::UNO_QUERY_THROW );
501  uno::Reference< util::XRefreshable > xRef( xSupp->getTextFields(), uno::UNO_QUERY_THROW );
502  xRef->refresh();
503  nUpdate = 0;
504  }
505  catch(const uno::Exception&)
506  {
507  nUpdate = 1;
508  }
509  return nUpdate;
510 }
511 
512 // XHelperInterface
513 OUString
515 {
516  return "SwVbaFields";
517 }
518 
519 // XEnumerationAccess
520 uno::Type SAL_CALL
522 {
524 }
525 
526 uno::Sequence<OUString>
528 {
529  static uno::Sequence< OUString > const aServiceNames
530  {
531  "ooo.vba.word.Fields"
532  };
533  return aServiceNames;
534 }
535 
536 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Type
#define ERRCODE_BASIC_BAD_ARGUMENT
const char aData[]
Definition: ww8scan.hxx:47
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
uno::Reference< uno::XComponentContext > mxContext
WeakReference< XInterface > mxParent
sal_Int64 n
Reference
css::uno::Reference< css::frame::XModel2 > mxModel
Sequence< OUString > aServiceNames
virtual OUString getServiceImplName() override
Definition: vbafield.cxx:53
SwVbaFields(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::frame::XModel > &xModel)
Definition: vbafield.cxx:306
static const DocPropertyTable aDocPropertyTables[]
Definition: vbafield.cxx:390
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
Definition: vbafield.cxx:482
sal_uInt16 sal_Unicode
int n2
css::uno::Reference< css::text::XTextField > Create_Field_FileName(const OUString &rText)
Definition: vbafield.cxx:348
int nCount
virtual css::uno::Reference< css::text::XTextRange > SAL_CALL getXTextRange() override
Definition: vbarange.cxx:83
css::uno::Reference< css::text::XTextField > Create_Field_DocProperty(const OUString &_text)
Definition: vbafield.cxx:421
virtual css::uno::Type SAL_CALL getElementType() override
Definition: vbafield.cxx:521
css::uno::Reference< css::text::XTextField > mxTextField
Definition: vbafield.hxx:32
SwVbaField(const css::uno::Reference< ooo::vba::XHelperInterface > &rParent, const css::uno::Reference< css::uno::XComponentContext > &rContext, const css::uno::Reference< css::text::XTextField > &xTextField)
Definition: vbafield.cxx:35
virtual OUString getServiceImplName() override
Definition: vbafield.cxx:514
unsigned char sal_Bool
css::uno::Reference< css::lang::XMultiServiceFactory > mxMSF
Definition: vbafield.hxx:48
virtual sal_Int32 SAL_CALL Update() override
Definition: vbafield.cxx:495
css::uno::Type const & get()
virtual css::uno::Reference< ::ooo::vba::word::XField > SAL_CALL Add(const css::uno::Reference< ::ooo::vba::word::XRange > &Range, const css::uno::Any &Type, const css::uno::Any &Text, const css::uno::Any &PreserveFormatting) override
Definition: vbafield.cxx:312
static uno::Any lcl_createField(const uno::Reference< XHelperInterface > &xParent, const uno::Reference< uno::XComponentContext > &xContext, const uno::Reference< frame::XModel > &xModel, const uno::Any &aSource)
Definition: vbafield.cxx:218
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbafield.cxx:59
#define SAL_INFO(area, stream)
void copy(const fs::path &src, const fs::path &dest)
QPRO_FUNC_TYPE nType
double getLength(const B2DPolygon &rCandidate)
virtual sal_Bool SAL_CALL Update() override
Definition: vbafield.cxx:40
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
Definition: vbafield.cxx:490
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbafield.cxx:527
struct _ADOIndex Index