LibreOffice Module sw (master) 1
vbadocumentproperties.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 */
21#include <sal/log.hxx>
22#include <com/sun/star/document/XDocumentProperties.hpp>
23#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
24#include <com/sun/star/beans/NamedValue.hpp>
25#include <com/sun/star/beans/XPropertyContainer.hpp>
26#include <ooo/vba/word/WdBuiltInProperty.hpp>
27#include <ooo/vba/office/MsoDocProperties.hpp>
29#include <memory>
30#include "wordvbahelper.hxx"
31#include <fesh.hxx>
32#include <docsh.hxx>
33#include <utility>
34using namespace ::ooo::vba;
35using namespace css;
36
38static sal_Int8 lcl_toMSOPropType( const uno::Type& aType )
39{
40 sal_Int16 msoType = office::MsoDocProperties::msoPropertyTypeString;
41
42 switch ( aType.getTypeClass() )
43 {
44 case uno::TypeClass_BOOLEAN:
45 msoType = office::MsoDocProperties::msoPropertyTypeBoolean;
46 break;
47 case uno::TypeClass_FLOAT:
48 msoType = office::MsoDocProperties::msoPropertyTypeFloat;
49 break;
50 case uno::TypeClass_STRUCT: // Assume date
51 msoType = office::MsoDocProperties::msoPropertyTypeDate;
52 break;
53 case uno::TypeClass_BYTE:
54 case uno::TypeClass_SHORT:
55 case uno::TypeClass_LONG:
56 case uno::TypeClass_HYPER:
57 msoType = office::MsoDocProperties::msoPropertyTypeNumber;
58 break;
59 default:
60 throw lang::IllegalArgumentException();
61 }
62 return msoType;
63}
64
65namespace {
66
67class PropertGetSetHelper
68{
69protected:
70 uno::Reference< frame::XModel > m_xModel;
71 uno::Reference<document::XDocumentProperties> m_xDocProps;
72public:
73 explicit PropertGetSetHelper( uno::Reference< frame::XModel > xModel ):m_xModel(std::move( xModel ))
74 {
75 uno::Reference<document::XDocumentPropertiesSupplier> const
76 xDocPropSupp(m_xModel, uno::UNO_QUERY_THROW);
77 m_xDocProps.set(xDocPropSupp->getDocumentProperties(),
78 uno::UNO_SET_THROW);
79 }
80 virtual ~PropertGetSetHelper() {}
81 virtual uno::Any getPropertyValue( const OUString& rPropName ) = 0;
82 virtual void setPropertyValue( const OUString& rPropName, const uno::Any& aValue ) = 0;
83 uno::Reference< beans::XPropertySet > getUserDefinedProperties() {
84 return uno::Reference<beans::XPropertySet>(
85 m_xDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
86 }
87
88};
89
90class BuiltinPropertyGetSetHelper : public PropertGetSetHelper
91{
92public:
93 explicit BuiltinPropertyGetSetHelper( const uno::Reference< frame::XModel >& xModel ) :PropertGetSetHelper( xModel )
94 {
95 }
96 virtual uno::Any getPropertyValue( const OUString& rPropName ) override
97 {
98 if ( rPropName == "EditingDuration" )
99 {
100 sal_Int32 const nSecs = m_xDocProps->getEditingDuration();
101 return uno::Any( nSecs/60 ); // minutes
102 }
103 else if ("Title" == rPropName)
104 {
105 return uno::Any(m_xDocProps->getTitle());
106 }
107 else if ("Subject" == rPropName)
108 {
109 return uno::Any(m_xDocProps->getSubject());
110 }
111 else if ("Author" == rPropName)
112 {
113 return uno::Any(m_xDocProps->getAuthor());
114 }
115 else if ("Keywords" == rPropName)
116 {
117 return uno::Any(m_xDocProps->getKeywords());
118 }
119 else if ("Description" == rPropName)
120 {
121 return uno::Any(m_xDocProps->getDescription());
122 }
123 else if ("Template" == rPropName)
124 {
125 return uno::Any(m_xDocProps->getTemplateName());
126 }
127 else if ("ModifiedBy" == rPropName)
128 {
129 return uno::Any(m_xDocProps->getModifiedBy());
130 }
131 else if ("Generator" == rPropName)
132 {
133 return uno::Any(m_xDocProps->getGenerator());
134 }
135 else if ("PrintDate" == rPropName)
136 {
137 return uno::Any(m_xDocProps->getPrintDate());
138 }
139 else if ("CreationDate" == rPropName)
140 {
141 return uno::Any(m_xDocProps->getCreationDate());
142 }
143 else if ("ModifyDate" == rPropName)
144 {
145 return uno::Any(m_xDocProps->getModificationDate());
146 }
147 else if ("AutoloadURL" == rPropName)
148 {
149 return uno::Any(m_xDocProps->getAutoloadURL());
150 }
151 else
152 {
153 // fall back to user-defined properties
154 return getUserDefinedProperties()->getPropertyValue(rPropName);
155 }
156 }
157 virtual void setPropertyValue( const OUString& rPropName, const uno::Any& aValue ) override
158 {
159 if ("EditingDuration" == rPropName)
160 {
161 sal_Int32 nMins = 0;
162 if (aValue >>= nMins)
163 {
164 m_xDocProps->setEditingDuration(nMins * 60); // convert minutes
165 }
166 }
167 else if ("Title" == rPropName)
168 {
169 OUString str;
170 if (aValue >>= str)
171 {
172 m_xDocProps->setTitle(str);
173 }
174 }
175 else if ("Subject" == rPropName)
176 {
177 OUString str;
178 if (aValue >>= str)
179 {
180 m_xDocProps->setSubject(str);
181 }
182 }
183 else if ("Author" == rPropName)
184 {
185 OUString str;
186 if (aValue >>= str)
187 {
188 m_xDocProps->setAuthor(str);
189 }
190 }
191 else if ("Keywords" == rPropName)
192 {
193 uno::Sequence<OUString> keywords;
194 if (aValue >>= keywords)
195 {
196 m_xDocProps->setKeywords(keywords);
197 }
198 }
199 else if ("Description" == rPropName)
200 {
201 OUString str;
202 if (aValue >>= str)
203 {
204 m_xDocProps->setDescription(str);
205 }
206 }
207 else if ("Template" == rPropName)
208 {
209 OUString str;
210 if (aValue >>= str)
211 {
212 m_xDocProps->setTemplateName(str);
213 }
214 }
215 else if ("ModifiedBy" == rPropName)
216 {
217 OUString str;
218 if (aValue >>= str)
219 {
220 m_xDocProps->setModifiedBy(str);
221 }
222 }
223 else if ("Generator" == rPropName)
224 {
225 OUString str;
226 if (aValue >>= str)
227 {
228 return m_xDocProps->setGenerator(str);
229 }
230 }
231 else if ("PrintDate" == rPropName)
232 {
233 util::DateTime dt;
234 if (aValue >>= dt)
235 {
236 m_xDocProps->setPrintDate(dt);
237 }
238 }
239 else if ("CreationDate" == rPropName)
240 {
241 util::DateTime dt;
242 if (aValue >>= dt)
243 {
244 m_xDocProps->setCreationDate(dt);
245 }
246 }
247 else if ("ModifyDate" == rPropName)
248 {
249 util::DateTime dt;
250 if (aValue >>= dt)
251 {
252 m_xDocProps->setModificationDate(dt);
253 }
254 }
255 else if ("AutoloadURL" == rPropName)
256 {
257 OUString str;
258 if (aValue >>= str)
259 {
260 m_xDocProps->setAutoloadURL(str);
261 }
262 }
263 else
264 {
265 // fall back to user-defined properties
266 getUserDefinedProperties()->setPropertyValue(rPropName, aValue);
267 }
268 }
269};
270
271class CustomPropertyGetSetHelper : public BuiltinPropertyGetSetHelper
272{
273public:
274 explicit CustomPropertyGetSetHelper( const uno::Reference< frame::XModel >& xModel ) :BuiltinPropertyGetSetHelper( xModel )
275 {
276 }
277 virtual uno::Any getPropertyValue( const OUString& rPropName ) override
278 {
279 return getUserDefinedProperties()->getPropertyValue(rPropName);
280 }
281 virtual void setPropertyValue(
282 const OUString& rPropName, const uno::Any& rValue) override
283 {
284 return getUserDefinedProperties()->setPropertyValue(rPropName, rValue);
285 }
286};
287
288class StatisticPropertyGetSetHelper : public PropertGetSetHelper
289{
290 SwDocShell* mpDocShell;
291 uno::Reference< beans::XPropertySet > mxModelProps;
292public:
293 explicit StatisticPropertyGetSetHelper( const uno::Reference< frame::XModel >& xModel ) :PropertGetSetHelper( xModel ) , mpDocShell( nullptr )
294 {
295 mxModelProps.set( m_xModel, uno::UNO_QUERY_THROW );
296 mpDocShell = word::getDocShell( xModel );
297 }
298 virtual uno::Any getPropertyValue( const OUString& rPropName ) override
299 {
300 try
301 {
302 // Characters, ParagraphCount & WordCount are available from
303 // the model ( and additionally these also update the statics object )
304 return mxModelProps->getPropertyValue( rPropName );
305 }
306 catch (const uno::Exception&)
307 {
308 TOOLS_WARN_EXCEPTION("sw.vba", "");
309 }
310 uno::Any aReturn;
311 if ( rPropName == "LineCount" ) // special processing needed
312 {
313 if ( mpDocShell )
314 {
315 if (SwFEShell* pFEShell = mpDocShell->GetFEShell())
316 aReturn <<= pFEShell->GetLineCount();
317 }
318 }
319 else
320 {
321 uno::Sequence< beans::NamedValue > const stats(
322 m_xDocProps->getDocumentStatistics());
323
324 auto pStat = std::find_if(stats.begin(), stats.end(),
325 [&rPropName](const beans::NamedValue& rStat) { return rPropName == rStat.Name; });
326 if (pStat == stats.end())
327 throw uno::RuntimeException(); // bad Property
328
329 aReturn = pStat->Value;
330 }
331 return aReturn;
332 }
333
334 virtual void setPropertyValue( const OUString& rPropName, const uno::Any& aValue ) override
335 {
336 uno::Sequence< beans::NamedValue > stats(
337 m_xDocProps->getDocumentStatistics());
338
339 auto [begin, end] = asNonConstRange(stats);
340 auto pStat = std::find_if(begin, end,
341 [&rPropName](const beans::NamedValue& rStat) { return rPropName == rStat.Name; });
342 if (pStat != end)
343 {
344 pStat->Value = aValue;
345 m_xDocProps->setDocumentStatistics(stats);
346 }
347 }
348};
349
350class DocPropInfo
351{
352public:
353 OUString msMSODesc;
354 OUString msOOOPropName;
355 std::shared_ptr< PropertGetSetHelper > mpPropGetSetHelper;
356
357 static DocPropInfo createDocPropInfo( const OUString& sDesc, const OUString& sPropName, std::shared_ptr< PropertGetSetHelper > const & rHelper )
358 {
359 DocPropInfo aItem;
360 aItem.msMSODesc = sDesc;
361 aItem.msOOOPropName = sPropName;
362 aItem.mpPropGetSetHelper = rHelper;
363 return aItem;
364 }
365
366 static DocPropInfo createDocPropInfo( const char* sDesc, const char* sPropName, std::shared_ptr< PropertGetSetHelper > const & rHelper )
367 {
368 return createDocPropInfo( OUString::createFromAscii( sDesc ), OUString::createFromAscii( sPropName ), rHelper );
369 }
371 {
372 if ( mpPropGetSetHelper )
373 return mpPropGetSetHelper->getPropertyValue( msOOOPropName );
374 return uno::Any();
375 }
376 void setValue( const uno::Any& rValue )
377 {
378 if ( mpPropGetSetHelper )
379 mpPropGetSetHelper->setPropertyValue( msOOOPropName, rValue );
380 }
381 uno::Reference< beans::XPropertySet > getUserDefinedProperties()
382 {
383 uno::Reference< beans::XPropertySet > xProps;
384 if ( mpPropGetSetHelper )
385 return mpPropGetSetHelper->getUserDefinedProperties();
386 return xProps;
387 }
388};
389
390}
391
392typedef std::unordered_map< sal_Int32, DocPropInfo > MSOIndexToOODocPropInfo;
393
394namespace {
395
396class BuiltInIndexHelper
397{
398 MSOIndexToOODocPropInfo m_docPropInfoMap;
399
400public:
401 explicit BuiltInIndexHelper( const uno::Reference< frame::XModel >& xModel )
402 {
403 auto aStandardHelper = std::make_shared<BuiltinPropertyGetSetHelper>( xModel );
404 auto aUsingStatsHelper = std::make_shared<StatisticPropertyGetSetHelper>( xModel );
405
406 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyTitle ] = DocPropInfo::createDocPropInfo( "Title", "Title", aStandardHelper );
407 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertySubject ] = DocPropInfo::createDocPropInfo( "Subject", "Subject", aStandardHelper );
408 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyAuthor ] = DocPropInfo::createDocPropInfo( "Author", "Author", aStandardHelper );
409 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyKeywords ] = DocPropInfo::createDocPropInfo( "Keywords", "Keywords", aStandardHelper );
410 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyComments ] = DocPropInfo::createDocPropInfo( "Comments", "Description", aStandardHelper );
411 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyTemplate ] = DocPropInfo::createDocPropInfo( "Template", "Template", aStandardHelper );
412 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyLastAuthor ] = DocPropInfo::createDocPropInfo( "Last author", "ModifiedBy", aStandardHelper ); // doesn't seem to exist - throw or return nothing ?
413 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyRevision ] = DocPropInfo::createDocPropInfo( "Revision number", "EditingCycles", aStandardHelper ); // doesn't seem to exist - throw or return nothing ?
414 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyAppName ] = DocPropInfo::createDocPropInfo( "Application name", "Generator", aStandardHelper ); // doesn't seem to exist - throw or return nothing ?
415 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyTimeLastPrinted ] = DocPropInfo::createDocPropInfo( "Last print date", "PrintDate", aStandardHelper ); // doesn't seem to exist - throw or return nothing ?
416 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyTimeCreated ] = DocPropInfo::createDocPropInfo( "Creation date", "CreationDate", aStandardHelper );
417 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyTimeLastSaved ] = DocPropInfo::createDocPropInfo( "Last save time", "ModifyDate", aStandardHelper );
418 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyVBATotalEdit ] = DocPropInfo::createDocPropInfo( "Total editing time", "EditingDuration", aStandardHelper ); // Not sure if this is correct
419 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyPages ] = DocPropInfo::createDocPropInfo( "Number of pages", "PageCount", aUsingStatsHelper ); // special handling required ?
420 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyWords ] = DocPropInfo::createDocPropInfo( "Number of words", "WordCount", aUsingStatsHelper ); // special handling require ?
421 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyCharacters ] = DocPropInfo::createDocPropInfo( "Number of characters", "CharacterCount", aUsingStatsHelper ); // special handling required ?
422 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertySecurity ] = DocPropInfo::createDocPropInfo( "Security", "", aStandardHelper ); // doesn't seem to exist
423 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyCategory ] = DocPropInfo::createDocPropInfo( "Category", "Category", aStandardHelper ); // hacked in
424 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyFormat ] = DocPropInfo::createDocPropInfo( "Format", "", aStandardHelper ); // doesn't seem to exist
425 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyManager ] = DocPropInfo::createDocPropInfo( "Manager", "Manager", aStandardHelper ); // hacked in
426 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyCompany ] = DocPropInfo::createDocPropInfo( "Company", "Company", aStandardHelper ); // hacked in
427 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyBytes ] = DocPropInfo::createDocPropInfo( "Number of bytes", "", aStandardHelper ); // doesn't seem to exist - size on disk exists ( for an already saved document ) perhaps it will do ( or we need something else )
428 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyLines ] = DocPropInfo::createDocPropInfo( "Number of lines", "LineCount", aUsingStatsHelper ); // special handling
429 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyParas ] = DocPropInfo::createDocPropInfo( "Number of paragraphs", "ParagraphCount", aUsingStatsHelper ); // special handling
430 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertySlides ] = DocPropInfo::createDocPropInfo( "Number of slides", "" , aStandardHelper ); // doesn't seem to exist
431 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyNotes ] = DocPropInfo::createDocPropInfo( "Number of notes", "", aStandardHelper ); // doesn't seem to exist
432 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyHiddenSlides ] = DocPropInfo::createDocPropInfo("Number of hidden Slides", "", aStandardHelper ); // doesn't seem to exist
433 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyMMClips ] = DocPropInfo::createDocPropInfo( "Number of multimedia clips", "", aStandardHelper ); // doesn't seem to exist
434 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyHyperlinkBase ] = DocPropInfo::createDocPropInfo( "Hyperlink base", "AutoloadURL", aStandardHelper );
435 m_docPropInfoMap[ word::WdBuiltInProperty::wdPropertyCharsWSpaces ] = DocPropInfo::createDocPropInfo( "Number of characters (with spaces)", "", aStandardHelper ); // doesn't seem to be supported
436 }
437
438 MSOIndexToOODocPropInfo& getDocPropInfoMap() { return m_docPropInfoMap; }
439};
440
441}
442
444
445namespace {
446
447class SwVbaBuiltInDocumentProperty : public SwVbaDocumentProperty_BASE
448{
449protected:
450 DocPropInfo mPropInfo;
451public:
452 SwVbaBuiltInDocumentProperty( const uno::Reference< ov::XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, DocPropInfo rInfo );
453 // XDocumentProperty
454 virtual void SAL_CALL Delete( ) override;
455 virtual OUString SAL_CALL getName( ) override;
456 virtual void SAL_CALL setName( const OUString& Name ) override;
457 virtual ::sal_Int8 SAL_CALL getType( ) override;
458 virtual void SAL_CALL setType( ::sal_Int8 Type ) override;
459 virtual sal_Bool SAL_CALL getLinkToContent( ) override;
460 virtual void SAL_CALL setLinkToContent( sal_Bool LinkToContent ) override;
461 virtual uno::Any SAL_CALL getValue( ) override;
462 virtual void SAL_CALL setValue( const uno::Any& Value ) override;
463 virtual OUString SAL_CALL getLinkSource( ) override;
464 virtual void SAL_CALL setLinkSource( const OUString& LinkSource ) override;
465 //XDefaultProperty
466 virtual OUString SAL_CALL getDefaultPropertyName( ) override { return "Value"; }
467 // XHelperInterface
468 virtual OUString getServiceImplName() override;
469 virtual uno::Sequence<OUString> getServiceNames() override;
470};
471
472class SwVbaCustomDocumentProperty : public SwVbaBuiltInDocumentProperty
473{
474public:
475
476 SwVbaCustomDocumentProperty( const uno::Reference< ov::XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const DocPropInfo& rInfo );
477
478 virtual sal_Bool SAL_CALL getLinkToContent( ) override;
479 virtual void SAL_CALL setLinkToContent( sal_Bool LinkToContent ) override;
480
481 virtual OUString SAL_CALL getLinkSource( ) override;
482 virtual void SAL_CALL setLinkSource( const OUString& LinkSource ) override;
483 virtual void SAL_CALL Delete( ) override;
484 virtual void SAL_CALL setName( const OUString& Name ) override;
485 virtual void SAL_CALL setType( ::sal_Int8 Type ) override;
486
487};
488
489}
490
491SwVbaCustomDocumentProperty::SwVbaCustomDocumentProperty( const uno::Reference< ov::XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const DocPropInfo& rInfo ) : SwVbaBuiltInDocumentProperty( xParent, xContext, rInfo )
492{
493}
494
496SwVbaCustomDocumentProperty::getLinkToContent( )
497{
498 // #FIXME we need to store the link content somewhere
499 return false;
500}
501
502void
503SwVbaCustomDocumentProperty::setLinkToContent( sal_Bool /*bLinkContent*/ )
504{
505}
506
507OUString
508SwVbaCustomDocumentProperty::getLinkSource( )
509{
510 // #FIXME we need to store the link content somewhere
511 return OUString();
512}
513
514void
515SwVbaCustomDocumentProperty::setLinkSource( const OUString& /*rsLinkContent*/ )
516{
517 // #FIXME we need to store the link source somewhere
518}
519
520void SAL_CALL
521SwVbaCustomDocumentProperty::setName( const OUString& /*Name*/ )
522{
523 // setName on existing property ?
524 // #FIXME
525 // do we need to delete existing property and create a new one?
526}
527
528void SAL_CALL
529SwVbaCustomDocumentProperty::setType( ::sal_Int8 /*Type*/ )
530{
531 // setType, do we need to do a conversion?
532 // #FIXME the underlying value needs to be changed to the new type
533}
534
535void SAL_CALL
536SwVbaCustomDocumentProperty::Delete( )
537{
538 uno::Reference< beans::XPropertyContainer > xContainer(
539 mPropInfo.getUserDefinedProperties(), uno::UNO_QUERY_THROW);
540 xContainer->removeProperty( getName() );
541}
542
543SwVbaBuiltInDocumentProperty::SwVbaBuiltInDocumentProperty( const uno::Reference< ov::XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, DocPropInfo rInfo ) : SwVbaDocumentProperty_BASE( xParent, xContext ), mPropInfo(std::move( rInfo ))
544{
545}
546
547void SAL_CALL
548SwVbaBuiltInDocumentProperty::Delete( )
549{
550 // not valid for Builtin
551 throw uno::RuntimeException();
552}
553
554OUString SAL_CALL
555SwVbaBuiltInDocumentProperty::getName( )
556{
557 return mPropInfo.msMSODesc;
558}
559
560void SAL_CALL
561SwVbaBuiltInDocumentProperty::setName( const OUString& )
562{
563 // not valid for Builtin
564 throw uno::RuntimeException();
565}
566
567::sal_Int8 SAL_CALL
568SwVbaBuiltInDocumentProperty::getType( )
569{
570 return lcl_toMSOPropType( getValue().getValueType() );
571}
572
573void SAL_CALL
574SwVbaBuiltInDocumentProperty::setType( ::sal_Int8 /*Type*/ )
575{
576 // not valid for Builtin
577 throw uno::RuntimeException();
578}
579
580sal_Bool SAL_CALL
581SwVbaBuiltInDocumentProperty::getLinkToContent( )
582{
583 return false; // built-in always false
584}
585
586void SAL_CALL
587SwVbaBuiltInDocumentProperty::setLinkToContent( sal_Bool /*LinkToContent*/ )
588{
589 // not valid for Builtin
590 throw uno::RuntimeException();
591}
592
593uno::Any SAL_CALL
594SwVbaBuiltInDocumentProperty::getValue( )
595{
596 uno::Any aRet = mPropInfo.getValue();
597 if ( !aRet.hasValue() )
598 throw uno::RuntimeException();
599 return aRet;
600}
601
602void SAL_CALL
603SwVbaBuiltInDocumentProperty::setValue( const uno::Any& Value )
604{
605 mPropInfo.setValue( Value );
606}
607
608OUString SAL_CALL
609SwVbaBuiltInDocumentProperty::getLinkSource( )
610{
611 // not valid for Builtin
612 throw uno::RuntimeException();
613}
614
615void SAL_CALL
616SwVbaBuiltInDocumentProperty::setLinkSource( const OUString& /*LinkSource*/ )
617{
618 // not valid for Builtin
619 throw uno::RuntimeException();
620}
621
622OUString
623SwVbaBuiltInDocumentProperty::getServiceImplName()
624{
625 return "SwVbaBuiltinDocumentProperty";
626}
627
628uno::Sequence<OUString>
629SwVbaBuiltInDocumentProperty::getServiceNames()
630{
631 static uno::Sequence< OUString > const aServiceNames
632 {
633 "ooo.vba.word.DocumentProperty"
634 };
635 return aServiceNames;
636}
637typedef ::cppu::WeakImplHelper< css::container::XIndexAccess
638 ,css::container::XNameAccess
639 ,css::container::XEnumerationAccess
641
642typedef std::unordered_map< sal_Int32, uno::Reference< XDocumentProperty > > DocProps;
643
644namespace {
645
646class DocPropEnumeration : public ::cppu::WeakImplHelper< css::container::XEnumeration >
647{
648 DocProps mDocProps;
649 DocProps::iterator mIt;
650public:
651
652 explicit DocPropEnumeration( DocProps&& rProps ) : mDocProps( std::move(rProps) ), mIt( mDocProps.begin() ) {}
653 virtual sal_Bool SAL_CALL hasMoreElements( ) override
654 {
655 return mIt != mDocProps.end();
656 }
657 virtual uno::Any SAL_CALL nextElement( ) override
658 {
659 if ( !hasMoreElements() )
660 throw container::NoSuchElementException();
661 return uno::Any( mIt++->second );
662 }
663};
664
665}
666
667typedef std::unordered_map< OUString, uno::Reference< XDocumentProperty > > DocPropsByName;
668
669namespace {
670
671class BuiltInPropertiesImpl : public PropertiesImpl_BASE
672{
673protected:
674
675 uno::Reference< frame::XModel > m_xModel;
676
677 DocProps mDocProps;
678 DocPropsByName mNamedDocProps;
679
680 public:
681 BuiltInPropertiesImpl( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, uno::Reference< frame::XModel > xModel ) : m_xModel(std::move( xModel ))
682 {
683 BuiltInIndexHelper builtIns( m_xModel );
684 for ( sal_Int32 index = word::WdBuiltInProperty::wdPropertyTitle; index <= word::WdBuiltInProperty::wdPropertyCharsWSpaces; ++index )
685 {
686 mDocProps[ index ] = new SwVbaBuiltInDocumentProperty( xParent, xContext, builtIns.getDocPropInfoMap()[ index ] );
687 mNamedDocProps[ mDocProps[ index ]->getName() ] = mDocProps[ index ];
688 }
689 }
690// XIndexAccess
691 virtual ::sal_Int32 SAL_CALL getCount( ) override
692 {
693 return mDocProps.size();
694 }
695 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
696 {
697 // correct the correct by the base class for 1 based indices
698 DocProps::iterator it = mDocProps.find( ++Index );
699 if ( it == mDocProps.end() )
700 throw lang::IndexOutOfBoundsException();
701 return uno::Any( it->second );
702 }
703 virtual uno::Any SAL_CALL getByName( const OUString& aName ) override
704 {
705 if ( !hasByName( aName ) )
706 throw container::NoSuchElementException();
707 DocPropsByName::iterator it = mNamedDocProps.find( aName );
708 return uno::Any( it->second );
709
710 }
711 virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) override
712 {
713 uno::Sequence< OUString > aNames( getCount() );
714 OUString* pName = aNames.getArray();
715 for (const auto& rEntry : mNamedDocProps)
716 {
717 *pName = rEntry.first;
718 ++pName;
719 }
720 return aNames;
721 }
722
723 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override
724 {
725 DocPropsByName::iterator it = mNamedDocProps.find( aName );
726 if ( it == mNamedDocProps.end() )
727 return false;
728 return true;
729 }
730// XElementAccess
731 virtual uno::Type SAL_CALL getElementType( ) override
732 {
734 }
735 virtual sal_Bool SAL_CALL hasElements( ) override
736 {
737 return !mDocProps.empty();
738 }
739 virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) override
740 {
741 return new DocPropEnumeration( std::unordered_map(mDocProps) );
742 }
743};
744
745}
746
747SwVbaBuiltinDocumentProperties::SwVbaBuiltinDocumentProperties( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel ) : SwVbaDocumentproperties_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( new BuiltInPropertiesImpl( xParent, xContext, xModel ) ) )
748{
749}
750
751uno::Reference< XDocumentProperty > SAL_CALL
752SwVbaBuiltinDocumentProperties::Add( const OUString& /*Name*/, sal_Bool /*LinkToContent*/, ::sal_Int8 /*Type*/, const uno::Any& /*value*/, const uno::Any& /*LinkSource*/ )
753{
754 throw uno::RuntimeException( "not supported for Builtin properties" );
755}
756
757// XEnumerationAccess
758uno::Type SAL_CALL
760{
762}
763
764uno::Reference< container::XEnumeration > SAL_CALL
766{
767 uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
768 return xEnumAccess->createEnumeration();
769}
770
771// ScVbaCollectionBaseImpl
774{
775 // pass through
776 return aSource;
777}
778
779// XHelperInterface
780OUString
782{
783 return "SwVbaBuiltinDocumentProperties";
784}
785
786uno::Sequence<OUString>
788{
789 static uno::Sequence< OUString > const aServiceNames
790 {
791 "ooo.vba.word.DocumentProperties"
792 };
793 return aServiceNames;
794}
795
796namespace {
797
798class CustomPropertiesImpl : public PropertiesImpl_BASE
799{
800 uno::Reference< XHelperInterface > m_xParent;
801 uno::Reference< uno::XComponentContext > m_xContext;
802 uno::Reference< frame::XModel > m_xModel;
803 uno::Reference< beans::XPropertySet > mxUserDefinedProp;
804 std::shared_ptr< PropertGetSetHelper > mpPropGetSetHelper;
805public:
806 CustomPropertiesImpl( uno::Reference< XHelperInterface > xParent, uno::Reference< uno::XComponentContext > xContext, uno::Reference< frame::XModel > xModel ) : m_xParent(std::move( xParent )), m_xContext(std::move( xContext )), m_xModel(std::move( xModel ))
807 {
808 // suck in the document( custom ) properties
809 mpPropGetSetHelper = std::make_shared<CustomPropertyGetSetHelper>( m_xModel );
810 mxUserDefinedProp.set(mpPropGetSetHelper->getUserDefinedProperties(),
811 uno::UNO_SET_THROW);
812 };
813 // XIndexAccess
814 virtual ::sal_Int32 SAL_CALL getCount( ) override
815 {
816 return mxUserDefinedProp->getPropertySetInfo()->getProperties().getLength();
817 }
818
819 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) override
820 {
821 uno::Sequence< beans::Property > aProps = mxUserDefinedProp->getPropertySetInfo()->getProperties();
822 if ( Index >= aProps.getLength() )
823 throw lang::IndexOutOfBoundsException();
824 // How to determine type e.g Date? ( com.sun.star.util.DateTime )
825 DocPropInfo aPropInfo = DocPropInfo::createDocPropInfo( aProps[ Index ].Name, aProps[ Index ].Name, mpPropGetSetHelper );
826 return uno::Any( uno::Reference< XDocumentProperty >( new SwVbaCustomDocumentProperty( m_xParent, m_xContext, aPropInfo ) ) );
827 }
828
829 virtual uno::Any SAL_CALL getByName( const OUString& aName ) override
830 {
831 if ( !hasByName( aName ) )
832 throw container::NoSuchElementException();
833
834 DocPropInfo aPropInfo = DocPropInfo::createDocPropInfo( aName, aName, mpPropGetSetHelper );
835 return uno::Any( uno::Reference< XDocumentProperty >( new SwVbaCustomDocumentProperty( m_xParent, m_xContext, aPropInfo ) ) );
836 }
837
838 virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) override
839 {
840 const uno::Sequence< beans::Property > aProps = mxUserDefinedProp->getPropertySetInfo()->getProperties();
841 uno::Sequence< OUString > aNames( aProps.getLength() );
842 std::transform(aProps.begin(), aProps.end(), aNames.getArray(),
843 [](const beans::Property& rProp) -> OUString { return rProp.Name; });
844 return aNames;
845 }
846
847 virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) override
848 {
849 SAL_INFO("sw.vba", "hasByName(" << aName << ") returns " << mxUserDefinedProp->getPropertySetInfo()->hasPropertyByName( aName ) );
850 return mxUserDefinedProp->getPropertySetInfo()->hasPropertyByName( aName );
851 }
852
853 // XElementAccess
854 virtual uno::Type SAL_CALL getElementType( ) override
855 {
857 }
858
859 virtual sal_Bool SAL_CALL hasElements( ) override
860 {
861 return getCount() > 0;
862 }
863
864 virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) override
865 {
866 // create a map of properties ( the key doesn't matter )
867 SAL_INFO("sw.vba", "Creating an enumeration");
868 sal_Int32 key = 0;
869 sal_Int32 nElem = getCount();
870 DocProps simpleDocPropSnapShot;
871 for ( ; key < nElem; ++key )
872 simpleDocPropSnapShot[ key ].set( getByIndex( key ), uno::UNO_QUERY_THROW );
873 SAL_INFO("sw.vba", "After creating the enumeration");
874 return new DocPropEnumeration( std::move(simpleDocPropSnapShot) );
875 }
876
877 void addProp( const OUString& Name, const uno::Any& Value )
878 {
879 uno::Reference< beans::XPropertyContainer > xContainer( mxUserDefinedProp, uno::UNO_QUERY_THROW );
880 // TODO fixme, perform the necessary Type Value conversions
881 xContainer->addProperty( Name, sal_Int16(128), Value );
882 }
883
884};
885
886}
887
888SwVbaCustomDocumentProperties::SwVbaCustomDocumentProperties( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel ) : SwVbaBuiltinDocumentProperties( xParent, xContext, xModel )
889{
890 // replace the m_xIndexAccess implementation ( we need a virtual init )
891 m_xIndexAccess.set( new CustomPropertiesImpl( xParent, xContext, xModel ) );
892 m_xNameAccess.set( m_xIndexAccess, uno::UNO_QUERY_THROW );
893}
894
895uno::Reference< XDocumentProperty > SAL_CALL
896SwVbaCustomDocumentProperties::Add( const OUString& Name, sal_Bool LinkToContent, ::sal_Int8 /*Type*/, const uno::Any& Value, const uno::Any& LinkSource )
897{
898 CustomPropertiesImpl* pCustomProps = dynamic_cast< CustomPropertiesImpl* > ( m_xIndexAccess.get() );
899 uno::Reference< XDocumentProperty > xDocProp;
900 if ( pCustomProps )
901 {
902 OUString sLinkSource;
903 pCustomProps->addProp( Name, Value );
904
905 xDocProp.set( m_xNameAccess->getByName( Name ), uno::UNO_QUERY_THROW );
906 xDocProp->setLinkToContent( LinkToContent );
907
908 if ( LinkSource >>= sLinkSource )
909 xDocProp->setLinkSource( sLinkSource );
910 }
911 return xDocProp;
912}
913
914// XHelperInterface
915OUString
917{
918 return "SwVbaCustomDocumentProperties";
919}
920
921/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
Reference< XComponentContext > m_xContext
const char * pName
virtual css::uno::Sequence< OUString > getServiceNames()=0
css::uno::Reference< css::container::XNameAccess > m_xNameAccess
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
SwFEShell * GetFEShell()
For Core - it knows the DocShell but not the WrtShell!
Definition: docsh.cxx:1223
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
virtual OUString getServiceImplName() override
virtual css::uno::Type SAL_CALL getElementType() override
SwVbaBuiltinDocumentProperties(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::frame::XModel > &xDocument)
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual css::uno::Reference< ::ooo::vba::XDocumentProperty > SAL_CALL Add(const OUString &Name, sal_Bool LinkToContent, ::sal_Int8 Type, const css::uno::Any &Value, const css::uno::Any &LinkSource) override
virtual css::uno::Sequence< OUString > getServiceNames() override
SwVbaCustomDocumentProperties(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::frame::XModel > &xDocument)
virtual css::uno::Reference< ::ooo::vba::XDocumentProperty > SAL_CALL Add(const OUString &Name, sal_Bool LinkToContent, ::sal_Int8 Type, const css::uno::Any &Value, const css::uno::Any &LinkSource) override
virtual OUString getServiceImplName() override
css::uno::Type const & get()
#define TOOLS_WARN_EXCEPTION(area, stream)
Reference< frame::XModel > m_xModel
Sequence< OUString > aServiceNames
OUString aName
#define SAL_INFO(area, stream)
void set(css::uno::UnoInterfaceReference const &value)
Type
Value
Reference
index
css::beans::Optional< css::uno::Any > getValue(std::u16string_view id)
enumrange< T >::Iterator begin(enumrange< T >)
SwDocShell * getDocShell(const uno::Reference< frame::XModel > &xModel)
VBAHELPER_DLLPUBLIC bool setPropertyValue(css::uno::Sequence< css::beans::PropertyValue > &aProp, const OUString &aName, const css::uno::Any &aValue)
end
bool getPropertyValue(ValueType &rValue, css::uno::Reference< css::beans::XPropertySet > const &xPropSet, OUString const &propName)
bool getType(BSTR name, Type &type)
RegError REGISTRY_CALLTYPE setValue(RegKeyHandle hKey, rtl_uString *keyName, RegValueType valueType, RegValue pData, sal_uInt32 valueSize)
void setValue(Type type, object value)
bool hasValue()
Object Value
Reference< XModel > xModel
OUString Name
unsigned char sal_Bool
signed char sal_Int8
InheritedHelperInterfaceWeakImpl< ooo::vba::XDocumentProperty > SwVbaDocumentProperty_BASE
std::unordered_map< sal_Int32, DocPropInfo > MSOIndexToOODocPropInfo
::cppu::WeakImplHelper< css::container::XIndexAccess,css::container::XNameAccess,css::container::XEnumerationAccess > PropertiesImpl_BASE
static sal_Int8 lcl_toMSOPropType(const uno::Type &aType)
std::unordered_map< sal_Int32, uno::Reference< XDocumentProperty > > DocProps
std::unordered_map< OUString, uno::Reference< XDocumentProperty > > DocPropsByName