xref: /core/editeng/source/uno/unofield.cxx (revision 966f40ee)
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 <com/sun/star/util/DateTime.hpp>
21 #include <com/sun/star/text/FilenameDisplayFormat.hpp>
22 #include <com/sun/star/lang/NoSupportException.hpp>
23 #include <com/sun/star/beans/PropertyAttribute.hpp>
24 #include <vcl/svapp.hxx>
25 #include <tools/debug.hxx>
26 #include <svl/itemprop.hxx>
27 
28 #include <editeng/eeitem.hxx>
29 #include <editeng/flditem.hxx>
30 #include <editeng/CustomPropertyField.hxx>
31 #include <editeng/measfld.hxx>
32 #include <editeng/unofield.hxx>
33 #include <editeng/unotext.hxx>
34 #include <comphelper/sequence.hxx>
35 #include <comphelper/servicehelper.hxx>
36 #include <cppuhelper/supportsservice.hxx>
37 #include <sal/log.hxx>
38 
39 #include <editeng/unonames.hxx>
40 
41 using namespace ::cppu;
42 using namespace ::com::sun::star;
43 
44 #define QUERYINT( xint ) \
45     if( rType == cppu::UnoType<xint>::get() ) \
46         aAny <<= uno::Reference< xint >(this)
47 
48 
49 #define WID_DATE    0
50 #define WID_BOOL1   1
51 #define WID_BOOL2   2
52 #define WID_INT32   3
53 #define WID_INT16   4
54 #define WID_STRING1 5
55 #define WID_STRING2 6
56 #define WID_STRING3 7
57 
58 class SvxUnoFieldData_Impl
59 {
60 public:
61     bool    mbBoolean1;
62     bool    mbBoolean2;
63     sal_Int32   mnInt32;
64     sal_Int16   mnInt16;
65     OUString    msString1;
66     OUString    msString2;
67     OUString    msString3;
68     util::DateTime maDateTime;
69 
70     OUString    msPresentation;
71 };
72 
73 static const SfxItemPropertySet* ImplGetFieldItemPropertySet( sal_Int32 mnId )
74 {
75     static const SfxItemPropertyMapEntry aExDateTimeFieldPropertyMap_Impl[] =
76     {
77         { OUString(UNO_TC_PROP_DATE_TIME), WID_DATE,  ::cppu::UnoType<util::DateTime>::get(), 0, 0 },
78         { OUString(UNO_TC_PROP_IS_FIXED),  WID_BOOL1, cppu::UnoType<bool>::get(),                  0, 0 },
79         { OUString(UNO_TC_PROP_IS_DATE),   WID_BOOL2, cppu::UnoType<bool>::get(),                  0, 0 },
80         { OUString(UNO_TC_PROP_NUMFORMAT), WID_INT32, ::cppu::UnoType<sal_Int32>::get(),      0, 0 },
81         { OUString(), 0, css::uno::Type(), 0, 0 }
82     };
83     static const SfxItemPropertySet aExDateTimeFieldPropertySet_Impl(aExDateTimeFieldPropertyMap_Impl);
84 
85     static const SfxItemPropertyMapEntry aDateTimeFieldPropertyMap_Impl[] =
86     {
87         { OUString(UNO_TC_PROP_IS_DATE), WID_BOOL2, cppu::UnoType<bool>::get(), 0, 0 },
88         { OUString(), 0, css::uno::Type(), 0, 0 }
89     };
90     static const SfxItemPropertySet aDateTimeFieldPropertySet_Impl(aDateTimeFieldPropertyMap_Impl);
91 
92     static const SfxItemPropertyMapEntry aUrlFieldPropertyMap_Impl[] =
93     {
94 
95         { OUString(UNO_TC_PROP_URL_FORMAT),         WID_INT16,   ::cppu::UnoType<sal_Int16>::get(), 0, 0 },
96         { OUString(UNO_TC_PROP_URL_REPRESENTATION), WID_STRING1, ::cppu::UnoType<OUString>::get(),  0, 0 },
97         { OUString(UNO_TC_PROP_URL_TARGET),         WID_STRING2, ::cppu::UnoType<OUString>::get(),  0, 0 },
98         { OUString(UNO_TC_PROP_URL),                WID_STRING3, ::cppu::UnoType<OUString>::get(),  0, 0 },
99         { OUString(), 0, css::uno::Type(), 0, 0 }
100     };
101     static const SfxItemPropertySet aUrlFieldPropertySet_Impl(aUrlFieldPropertyMap_Impl);
102 
103     static const SfxItemPropertyMapEntry aEmptyPropertyMap_Impl[] =
104     {
105         { OUString(), 0, css::uno::Type(), 0, 0 }
106     };
107     static const SfxItemPropertySet aEmptyPropertySet_Impl(aEmptyPropertyMap_Impl);
108 
109     static const SfxItemPropertyMapEntry aExtFileFieldPropertyMap_Impl[] =
110     {
111         { OUString(UNO_TC_PROP_IS_FIXED),             WID_BOOL1,   cppu::UnoType<bool>::get(),             0, 0 },
112         { OUString(UNO_TC_PROP_FILE_FORMAT),          WID_INT16,   ::cppu::UnoType<sal_Int16>::get(), 0, 0 },
113         { OUString(UNO_TC_PROP_CURRENT_PRESENTATION), WID_STRING1, ::cppu::UnoType<OUString>::get(),  0, 0 },
114         { OUString(), 0, css::uno::Type(), 0, 0 }
115     };
116     static const SfxItemPropertySet aExtFileFieldPropertySet_Impl(aExtFileFieldPropertyMap_Impl);
117 
118     static const SfxItemPropertyMapEntry aAuthorFieldPropertyMap_Impl[] =
119     {
120         { OUString(UNO_TC_PROP_IS_FIXED),             WID_BOOL1,  cppu::UnoType<bool>::get(),             0, 0 },
121         { OUString(UNO_TC_PROP_CURRENT_PRESENTATION), WID_STRING1,::cppu::UnoType<OUString>::get(),  0, 0 },
122         { OUString(UNO_TC_PROP_AUTHOR_CONTENT),       WID_STRING2,::cppu::UnoType<OUString>::get(),  0, 0 },
123         { OUString(UNO_TC_PROP_AUTHOR_FORMAT),        WID_INT16,  ::cppu::UnoType<sal_Int16>::get(), 0, 0 },
124         { OUString(UNO_TC_PROP_AUTHOR_FULLNAME),      WID_BOOL2,  cppu::UnoType<bool>::get(),             0, 0 },
125         { OUString(), 0, css::uno::Type(), 0, 0 }
126     };
127     static const SfxItemPropertySet aAuthorFieldPropertySet_Impl(aAuthorFieldPropertyMap_Impl);
128 
129     static const SfxItemPropertyMapEntry aMeasureFieldPropertyMap_Impl[] =
130     {
131         { OUString(UNO_TC_PROP_MEASURE_KIND), WID_INT16,  ::cppu::UnoType<sal_Int16>::get(), 0, 0 },
132         { OUString(), 0, css::uno::Type(), 0, 0 }
133     };
134     static const SfxItemPropertySet aMeasureFieldPropertySet_Impl(aMeasureFieldPropertyMap_Impl);
135 
136     static const SfxItemPropertyMapEntry aDocInfoCustomFieldPropertyMap_Impl[] =
137     {
138         { OUString(UNO_TC_PROP_NAME),                 WID_STRING1, cppu::UnoType<OUString>::get(),   0, 0 },
139         { OUString(UNO_TC_PROP_CURRENT_PRESENTATION), WID_STRING2, cppu::UnoType<OUString>::get(),   0, 0 },
140         { OUString(UNO_TC_PROP_IS_FIXED),             WID_BOOL1,   cppu::UnoType<bool>::get(),       0, 0 },
141         { OUString(UNO_TC_PROP_NUMFORMAT),            WID_INT32,   cppu::UnoType<sal_Int32>::get(),  0, 0 },
142         { OUString(UNO_TC_PROP_IS_FIXED_LANGUAGE),    WID_BOOL2,   cppu::UnoType<bool>::get(),       0, 0 },
143         { OUString(), 0, css::uno::Type(), 0, 0 }
144     };
145     static const SfxItemPropertySet aDocInfoCustomFieldPropertySet_Impl(aDocInfoCustomFieldPropertyMap_Impl);
146 
147     switch( mnId )
148     {
149     case text::textfield::Type::EXTENDED_TIME:
150     case text::textfield::Type::DATE:
151         return &aExDateTimeFieldPropertySet_Impl;
152     case text::textfield::Type::URL:
153         return &aUrlFieldPropertySet_Impl;
154     case text::textfield::Type::TIME:
155         return &aDateTimeFieldPropertySet_Impl;
156     case text::textfield::Type::EXTENDED_FILE:
157         return &aExtFileFieldPropertySet_Impl;
158     case text::textfield::Type::AUTHOR:
159         return &aAuthorFieldPropertySet_Impl;
160     case text::textfield::Type::MEASURE:
161         return &aMeasureFieldPropertySet_Impl;
162     case text::textfield::Type::DOCINFO_CUSTOM:
163         return &aDocInfoCustomFieldPropertySet_Impl;
164     default:
165         return &aEmptyPropertySet_Impl;
166     }
167 }
168 
169 /* conversion routines */
170 
171 static sal_Int16 getFileNameDisplayFormat( SvxFileFormat nFormat )
172 {
173     switch( nFormat )
174     {
175     case SvxFileFormat::NameAndExt:    return text::FilenameDisplayFormat::NAME_AND_EXT;
176     case SvxFileFormat::PathFull:    return text::FilenameDisplayFormat::FULL;
177     case SvxFileFormat::PathOnly:    return text::FilenameDisplayFormat::PATH;
178 //  case SvxFileFormat::NameOnly:
179     default: return text::FilenameDisplayFormat::NAME;
180     }
181 }
182 
183 static SvxFileFormat setFileNameDisplayFormat( sal_Int16 nFormat )
184 {
185     switch( nFormat )
186     {
187     case text::FilenameDisplayFormat::FULL: return SvxFileFormat::PathFull;
188     case text::FilenameDisplayFormat::PATH: return SvxFileFormat::PathOnly;
189     case text::FilenameDisplayFormat::NAME: return SvxFileFormat::NameOnly;
190 //  case text::FilenameDisplayFormat::NAME_AND_EXT:
191     default:
192         return SvxFileFormat::NameAndExt;
193     }
194 }
195 
196 static util::DateTime getDate( sal_Int32 nDate )
197 {
198     util::DateTime aDate;
199 
200     Date aTempDate( nDate );
201 
202     aDate.Day = aTempDate.GetDay();
203     aDate.Month = aTempDate.GetMonth();
204     aDate.Year = aTempDate.GetYear();
205 
206     return aDate;
207 }
208 
209 static Date setDate( util::DateTime const & rDate )
210 {
211     return Date( rDate.Day, rDate.Month, rDate.Year );
212 }
213 
214 static util::DateTime getTime(sal_Int64 const nTime)
215 {
216     util::DateTime aTime;
217 
218     tools::Time aTempTime( nTime );
219 
220     aTime.NanoSeconds = aTempTime.GetNanoSec();
221     aTime.Seconds = aTempTime.GetSec();
222     aTime.Minutes = aTempTime.GetMin();
223     aTime.Hours = aTempTime.GetHour();
224 
225     return aTime;
226 }
227 
228 static tools::Time setTime( util::DateTime const & rDate )
229 {
230     return tools::Time( rDate  );
231 }
232 
233 
234 // class SvxUnoTextField
235 
236 namespace
237 {
238     class theSvxUnoTextFieldUnoTunnelId : public rtl::Static< UnoTunnelIdInit, theSvxUnoTextFieldUnoTunnelId> {};
239 }
240 
241 const css::uno::Sequence< sal_Int8 > & SvxUnoTextField::getUnoTunnelId() throw()
242 {
243     return theSvxUnoTextFieldUnoTunnelId::get().getSeq();
244 }
245 
246 sal_Int64 SAL_CALL SvxUnoTextField::getSomething( const css::uno::Sequence< sal_Int8 >& rId )
247 {
248     if( rId.getLength() == 16 && 0 == memcmp( getUnoTunnelId().getConstArray(),
249                                                          rId.getConstArray(), 16 ) )
250     {
251         return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this));
252     }
253     return 0;
254 }
255 
256 SvxUnoTextField::SvxUnoTextField( sal_Int32 nServiceId ) throw()
257 :   OComponentHelper( getMutex() )
258 ,   mpPropSet(nullptr)
259 ,   mnServiceId(nServiceId)
260 ,   mpImpl( new SvxUnoFieldData_Impl )
261 {
262     mpPropSet = ImplGetFieldItemPropertySet(mnServiceId);
263 
264     mpImpl->maDateTime.NanoSeconds = 0;
265     mpImpl->maDateTime.Seconds = 0;
266     mpImpl->maDateTime.Minutes = 0;
267     mpImpl->maDateTime.Hours = 0;
268     mpImpl->maDateTime.Day = 0;
269     mpImpl->maDateTime.Month = 0;
270     mpImpl->maDateTime.Year = 0;
271     mpImpl->maDateTime.IsUTC = false;
272 
273     switch( nServiceId )
274     {
275     case text::textfield::Type::DATE:
276         mpImpl->mbBoolean2 = true;
277         mpImpl->mnInt32 = static_cast<sal_Int32>(SvxDateFormat::StdSmall);
278         mpImpl->mbBoolean1 = false;
279         break;
280 
281     case text::textfield::Type::EXTENDED_TIME:
282     case text::textfield::Type::TIME:
283         mpImpl->mbBoolean2 = false;
284         mpImpl->mbBoolean1 = false;
285         mpImpl->mnInt32 = static_cast<sal_Int32>(SvxTimeFormat::Standard);
286         break;
287 
288     case text::textfield::Type::URL:
289         mpImpl->mnInt16 = static_cast<sal_uInt16>(SvxURLFormat::Repr);
290         break;
291 
292     case text::textfield::Type::EXTENDED_FILE:
293         mpImpl->mbBoolean1 = false;
294         mpImpl->mnInt16 = text::FilenameDisplayFormat::FULL;
295         break;
296 
297     case text::textfield::Type::AUTHOR:
298         mpImpl->mnInt16 = static_cast<sal_uInt16>(SvxAuthorFormat::FullName);
299         mpImpl->mbBoolean1 = false;
300         mpImpl->mbBoolean2 = true;
301         break;
302 
303     case text::textfield::Type::MEASURE:
304         mpImpl->mnInt16 = static_cast<sal_uInt16>(SdrMeasureFieldKind::Value);
305         break;
306 
307     case text::textfield::Type::DOCINFO_CUSTOM:
308         mpImpl->mbBoolean1 = true;
309         mpImpl->mbBoolean2 = true;
310         mpImpl->mnInt32 = 0;
311         break;
312 
313     default:
314         mpImpl->mbBoolean1 = false;
315         mpImpl->mbBoolean2 = false;
316         mpImpl->mnInt32 = 0;
317         mpImpl->mnInt16 = 0;
318 
319     }
320 }
321 
322 SvxUnoTextField::SvxUnoTextField( uno::Reference< text::XTextRange > const & xAnchor, const OUString& rPresentation, const SvxFieldData* pData ) throw()
323 :   OComponentHelper( getMutex() )
324 ,   mxAnchor( xAnchor )
325 ,   mpPropSet(nullptr)
326 ,   mnServiceId(text::textfield::Type::UNSPECIFIED)
327 ,   mpImpl( new SvxUnoFieldData_Impl )
328 {
329     DBG_ASSERT(pData, "pFieldData == NULL! [CL]" );
330 
331     mpImpl->msPresentation = rPresentation;
332 
333     if(pData)
334     {
335         mnServiceId = pData->GetClassId();
336         DBG_ASSERT(mnServiceId != text::textfield::Type::UNSPECIFIED, "unknown SvxFieldData! [CL]");
337         if (mnServiceId != text::textfield::Type::UNSPECIFIED)
338         {
339             // extract field properties from data class
340             switch( mnServiceId )
341             {
342             case text::textfield::Type::DATE:
343                 {
344                     mpImpl->mbBoolean2 = true;
345                     // #i35416# for variable date field, don't use invalid "0000-00-00" date,
346                     // use current date instead
347                     bool bFixed = static_cast<const SvxDateField*>(pData)->GetType() == SvxDateType::Fix;
348                     mpImpl->maDateTime = getDate( bFixed ?
349                                             static_cast<const SvxDateField*>(pData)->GetFixDate() :
350                                             Date( Date::SYSTEM ).GetDate() );
351                     mpImpl->mnInt32 = static_cast<sal_Int32>(static_cast<const SvxDateField*>(pData)->GetFormat());
352                     mpImpl->mbBoolean1 = bFixed;
353                 }
354                 break;
355 
356             case text::textfield::Type::TIME:
357                 mpImpl->mbBoolean2 = false;
358                 mpImpl->mbBoolean1 = false;
359                 mpImpl->mnInt32 = static_cast<sal_Int32>(SvxTimeFormat::Standard);
360                 break;
361 
362             case text::textfield::Type::EXTENDED_TIME:
363                 mpImpl->mbBoolean2 = false;
364                 mpImpl->maDateTime = getTime( static_cast<const SvxExtTimeField*>(pData)->GetFixTime() );
365                 mpImpl->mbBoolean1 = static_cast<const SvxExtTimeField*>(pData)->GetType() == SvxTimeType::Fix;
366                 mpImpl->mnInt32 = static_cast<sal_Int32>(static_cast<const SvxExtTimeField*>(pData)->GetFormat());
367                 break;
368 
369             case text::textfield::Type::URL:
370                 mpImpl->msString1 = static_cast<const SvxURLField*>(pData)->GetRepresentation();
371                 mpImpl->msString2 = static_cast<const SvxURLField*>(pData)->GetTargetFrame();
372                 mpImpl->msString3 = static_cast<const SvxURLField*>(pData)->GetURL();
373                 mpImpl->mnInt16 = sal::static_int_cast< sal_Int16 >(
374                     static_cast<const SvxURLField*>(pData)->GetFormat());
375                 break;
376 
377             case text::textfield::Type::EXTENDED_FILE:
378                 mpImpl->msString1 = static_cast<const SvxExtFileField*>(pData)->GetFile();
379                 mpImpl->mbBoolean1 = static_cast<const SvxExtFileField*>(pData)->GetType() == SvxFileType::Fix;
380                 mpImpl->mnInt16 = getFileNameDisplayFormat(static_cast<const SvxExtFileField*>(pData)->GetFormat());
381                 break;
382 
383             case text::textfield::Type::AUTHOR:
384                 mpImpl->msString1  = static_cast<const SvxAuthorField*>(pData)->GetFormatted();
385                 mpImpl->msString2  = static_cast<const SvxAuthorField*>(pData)->GetFormatted();
386                 mpImpl->mnInt16    = sal::static_int_cast< sal_Int16 >(
387                     static_cast<const SvxAuthorField*>(pData)->GetFormat());
388                 mpImpl->mbBoolean1 = static_cast<const SvxAuthorField*>(pData)->GetType() == SvxAuthorType::Fix;
389                 mpImpl->mbBoolean2 = static_cast<const SvxAuthorField*>(pData)->GetFormat() != SvxAuthorFormat::ShortName;
390                 break;
391 
392             case text::textfield::Type::MEASURE:
393                 mpImpl->mnInt16     = sal::static_int_cast< sal_Int16 >(static_cast<const SdrMeasureField*>(pData)->GetMeasureFieldKind());
394                 break;
395 
396             case text::textfield::Type::DOCINFO_CUSTOM:
397                 mpImpl->msString1 = static_cast<const editeng::CustomPropertyField*>(pData)->GetName();
398                 mpImpl->msString2 = static_cast<const editeng::CustomPropertyField*>(pData)->GetCurrentPresentation();
399                 mpImpl->mbBoolean1 = false;
400                 mpImpl->mbBoolean2 = false;
401                 mpImpl->mnInt32 = 0;
402                 break;
403 
404             default:
405                 SAL_WARN("editeng", "Id service unknown: " << mnServiceId);
406                 break;
407             }
408         }
409     }
410 
411     mpPropSet = ImplGetFieldItemPropertySet(mnServiceId);
412 }
413 
414 SvxUnoTextField::~SvxUnoTextField() throw()
415 {
416 }
417 
418 std::unique_ptr<SvxFieldData> SvxUnoTextField::CreateFieldData() const throw()
419 {
420     std::unique_ptr<SvxFieldData> pData;
421 
422     switch( mnServiceId )
423     {
424     case text::textfield::Type::TIME:
425     case text::textfield::Type::EXTENDED_TIME:
426     case text::textfield::Type::DATE:
427     {
428         if( mpImpl->mbBoolean2 ) // IsDate?
429         {
430             Date aDate( setDate( mpImpl->maDateTime ) );
431             pData.reset( new SvxDateField( aDate, mpImpl->mbBoolean1?SvxDateType::Fix:SvxDateType::Var ) );
432             if( mpImpl->mnInt32 >= static_cast<sal_Int32>(SvxDateFormat::AppDefault) &&
433                 mpImpl->mnInt32 <= static_cast<sal_Int32>(SvxDateFormat::F) )
434                 static_cast<SvxDateField*>(pData.get())->SetFormat( static_cast<SvxDateFormat>(mpImpl->mnInt32) );
435         }
436         else
437         {
438             if( mnServiceId != text::textfield::Type::TIME && mnServiceId != text::textfield::Type::DATE )
439             {
440                 tools::Time aTime( setTime( mpImpl->maDateTime ) );
441                 pData.reset( new SvxExtTimeField( aTime, mpImpl->mbBoolean1?SvxTimeType::Fix:SvxTimeType::Var ) );
442 
443                 if( static_cast<SvxTimeFormat>(mpImpl->mnInt32) >= SvxTimeFormat::AppDefault &&
444                     static_cast<SvxTimeFormat>(mpImpl->mnInt32) <= SvxTimeFormat::HH12_MM_SS_00_AMPM )
445                     static_cast<SvxExtTimeField*>(pData.get())->SetFormat( static_cast<SvxTimeFormat>(mpImpl->mnInt32) );
446             }
447             else
448             {
449                 pData.reset( new SvxTimeField() );
450             }
451         }
452 
453     }
454         break;
455 
456     case text::textfield::Type::URL:
457         pData.reset( new SvxURLField( mpImpl->msString3, mpImpl->msString1, !mpImpl->msString1.isEmpty() ? SvxURLFormat::Repr : SvxURLFormat::Url ) );
458         static_cast<SvxURLField*>(pData.get())->SetTargetFrame( mpImpl->msString2 );
459         if( static_cast<SvxURLFormat>(mpImpl->mnInt16) >= SvxURLFormat::AppDefault &&
460             static_cast<SvxURLFormat>(mpImpl->mnInt16) <= SvxURLFormat::Repr )
461             static_cast<SvxURLField*>(pData.get())->SetFormat( static_cast<SvxURLFormat>(mpImpl->mnInt16) );
462         break;
463 
464     case text::textfield::Type::PAGE:
465         pData.reset( new SvxPageField() );
466         break;
467 
468     case text::textfield::Type::PAGES:
469         pData.reset( new SvxPagesField() );
470         break;
471 
472     case text::textfield::Type::DOCINFO_TITLE:
473         pData.reset( new SvxFileField() );
474         break;
475 
476     case text::textfield::Type::TABLE:
477         pData.reset( new SvxTableField() );
478         break;
479 
480     case text::textfield::Type::EXTENDED_FILE:
481     {
482         // #92009# pass fixed attribute to constructor
483         pData.reset( new SvxExtFileField( mpImpl->msString1,
484                                      mpImpl->mbBoolean1 ? SvxFileType::Fix : SvxFileType::Var,
485                                      setFileNameDisplayFormat(mpImpl->mnInt16 ) ) );
486         break;
487     }
488 
489     case text::textfield::Type::AUTHOR:
490     {
491         OUString aContent;
492         OUString aFirstName;
493         OUString aLastName;
494 
495         // do we have CurrentPresentation given?
496         // mimic behaviour of writer, which means:
497         // prefer CurrentPresentation over Content
498         // if both are given.
499         if( !mpImpl->msString1.isEmpty() )
500             aContent = mpImpl->msString1;
501         else
502             aContent = mpImpl->msString2;
503 
504         sal_Int32 nPos = aContent.lastIndexOf( sal_Char(' '), 0 );
505         if( nPos > 0 )
506         {
507             aFirstName = aContent.copy( 0, nPos );
508             aLastName = aContent.copy( nPos + 1 );
509         }
510         else
511         {
512             aLastName = aContent;
513         }
514 
515         // #92009# pass fixed attribute to constructor
516         pData.reset( new SvxAuthorField( aFirstName, aLastName, "",
517                                     mpImpl->mbBoolean1 ? SvxAuthorType::Fix : SvxAuthorType::Var ) );
518 
519         if( !mpImpl->mbBoolean2 )
520         {
521             static_cast<SvxAuthorField*>(pData.get())->SetFormat( SvxAuthorFormat::ShortName );
522         }
523         else if( static_cast<SvxAuthorFormat>(mpImpl->mnInt16) >= SvxAuthorFormat::FullName &&
524                  static_cast<SvxAuthorFormat>(mpImpl->mnInt16) <= SvxAuthorFormat::ShortName )
525         {
526             static_cast<SvxAuthorField*>(pData.get())->SetFormat( static_cast<SvxAuthorFormat>(mpImpl->mnInt16) );
527         }
528 
529         break;
530     }
531 
532     case text::textfield::Type::MEASURE:
533     {
534         SdrMeasureFieldKind eKind = SdrMeasureFieldKind::Value;
535         if( mpImpl->mnInt16 == sal_Int16(SdrMeasureFieldKind::Unit) || mpImpl->mnInt16 == sal_Int16(SdrMeasureFieldKind::Rotate90Blanks) )
536             eKind = static_cast<SdrMeasureFieldKind>(mpImpl->mnInt16);
537         pData.reset( new SdrMeasureField( eKind) );
538         break;
539     }
540     case text::textfield::Type::PRESENTATION_HEADER:
541         pData.reset( new SvxHeaderField() );
542         break;
543     case text::textfield::Type::PRESENTATION_FOOTER:
544         pData.reset( new SvxFooterField() );
545         break;
546     case text::textfield::Type::PRESENTATION_DATE_TIME:
547         pData.reset( new SvxDateTimeField() );
548         break;
549     case text::textfield::Type::PAGE_NAME:
550         pData.reset( new SvxPageTitleField() );
551         break;
552     case text::textfield::Type::DOCINFO_CUSTOM:
553         pData.reset( new editeng::CustomPropertyField(mpImpl->msString1, mpImpl->msString2) );
554         break;
555     };
556 
557     return pData;
558 }
559 
560 // uno::XInterface
561 uno::Any SAL_CALL SvxUnoTextField::queryAggregation( const uno::Type & rType )
562 {
563     uno::Any aAny;
564 
565     QUERYINT( beans::XPropertySet );
566     else QUERYINT( text::XTextContent );
567     else QUERYINT( text::XTextField );
568     else QUERYINT( lang::XServiceInfo );
569     else QUERYINT( lang::XUnoTunnel );
570     else
571         return OComponentHelper::queryAggregation( rType );
572 
573     return aAny;
574 }
575 
576 // XTypeProvider
577 
578 uno::Sequence< uno::Type > SAL_CALL SvxUnoTextField::getTypes()
579 {
580     if( !maTypeSequence.hasElements() )
581     {
582         maTypeSequence = comphelper::concatSequences(
583             OComponentHelper::getTypes(),
584             uno::Sequence {
585                 cppu::UnoType<text::XTextField>::get(),
586                 cppu::UnoType<beans::XPropertySet>::get(),
587                 cppu::UnoType<lang::XServiceInfo>::get(),
588                 cppu::UnoType<lang::XUnoTunnel>::get() });
589     }
590     return maTypeSequence;
591 }
592 
593 uno::Sequence< sal_Int8 > SAL_CALL SvxUnoTextField::getImplementationId()
594 {
595     return css::uno::Sequence<sal_Int8>();
596 }
597 
598 uno::Any SAL_CALL SvxUnoTextField::queryInterface( const uno::Type & rType )
599 {
600     return OComponentHelper::queryInterface(rType);
601 }
602 
603 void SAL_CALL SvxUnoTextField::acquire() throw( )
604 {
605     OComponentHelper::acquire();
606 }
607 
608 void SAL_CALL SvxUnoTextField::release() throw( )
609 {
610     OComponentHelper::release();
611 }
612 
613 // Interface text::XTextField
614 OUString SAL_CALL SvxUnoTextField::getPresentation( sal_Bool bShowCommand )
615 {
616     SolarMutexGuard aGuard;
617     if (bShowCommand)
618     {
619         switch (mnServiceId)
620         {
621             case text::textfield::Type::DATE:
622                 return OUString("Date");
623             case text::textfield::Type::URL:
624                 return OUString("URL");
625             case text::textfield::Type::PAGE:
626                 return OUString("Page");
627             case text::textfield::Type::PAGES:
628                 return OUString("Pages");
629             case text::textfield::Type::TIME:
630                 return OUString("Time");
631             case text::textfield::Type::DOCINFO_TITLE:
632                 return OUString("File");
633             case text::textfield::Type::TABLE:
634                 return OUString("Table");
635             case text::textfield::Type::EXTENDED_TIME:
636                 return OUString("ExtTime");
637             case text::textfield::Type::EXTENDED_FILE:
638                 return OUString("ExtFile");
639             case text::textfield::Type::AUTHOR:
640                 return OUString("Author");
641             case text::textfield::Type::MEASURE:
642                 return OUString("Measure");
643             case text::textfield::Type::PRESENTATION_HEADER:
644                 return OUString("Header");
645             case text::textfield::Type::PRESENTATION_FOOTER:
646                 return OUString("Footer");
647             case text::textfield::Type::PRESENTATION_DATE_TIME:
648                 return OUString("DateTime");
649             case text::textfield::Type::PAGE_NAME:
650                 return OUString("PageName");
651             case text::textfield::Type::DOCINFO_CUSTOM:
652                 return OUString("Custom");
653             default:
654                 return OUString("Unknown");
655         }
656     }
657     else
658     {
659         return mpImpl->msPresentation;
660     }
661 }
662 
663 // Interface text::XTextContent
664 void SAL_CALL SvxUnoTextField::attach( const uno::Reference< text::XTextRange >& xTextRange )
665 {
666     SvxUnoTextRangeBase* pRange = SvxUnoTextRange::getImplementation( xTextRange );
667     if(pRange == nullptr)
668         throw lang::IllegalArgumentException();
669 
670     std::unique_ptr<SvxFieldData> pData = CreateFieldData();
671     if( pData )
672         pRange->attachField( std::move(pData) );
673 }
674 
675 uno::Reference< text::XTextRange > SAL_CALL SvxUnoTextField::getAnchor()
676 {
677     return mxAnchor;
678 }
679 
680 // lang::XComponent
681 void SAL_CALL SvxUnoTextField::dispose()
682 {
683     OComponentHelper::dispose();
684 }
685 
686 void SAL_CALL SvxUnoTextField::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
687 {
688     OComponentHelper::addEventListener(xListener);
689 }
690 
691 void SAL_CALL SvxUnoTextField::removeEventListener( const uno::Reference< lang::XEventListener >& aListener )
692 {
693     OComponentHelper::removeEventListener(aListener);
694 }
695 
696 
697 // Interface beans::XPropertySet
698 uno::Reference< beans::XPropertySetInfo > SAL_CALL SvxUnoTextField::getPropertySetInfo(  )
699 {
700     SolarMutexGuard aGuard;
701     return mpPropSet->getPropertySetInfo();
702 }
703 
704 void SAL_CALL SvxUnoTextField::setPropertyValue( const OUString& aPropertyName, const uno::Any& aValue )
705 {
706     SolarMutexGuard aGuard;
707 
708     if( mpImpl == nullptr )
709         throw uno::RuntimeException();
710 
711     if (aPropertyName == UNO_TC_PROP_ANCHOR)
712     {
713         aValue >>= mxAnchor;
714         return;
715     }
716 
717     const SfxItemPropertySimpleEntry* pMap = mpPropSet->getPropertyMap().getByName( aPropertyName );
718     if ( !pMap )
719         throw beans::UnknownPropertyException();
720 
721     switch( pMap->nWID )
722     {
723     case WID_DATE:
724         if(aValue >>= mpImpl->maDateTime)
725             return;
726         break;
727     case WID_BOOL1:
728         if(aValue >>= mpImpl->mbBoolean1)
729             return;
730         break;
731     case WID_BOOL2:
732         if(aValue >>= mpImpl->mbBoolean2)
733             return;
734         break;
735     case WID_INT16:
736         if(aValue >>= mpImpl->mnInt16)
737             return;
738         break;
739     case WID_INT32:
740         if(aValue >>= mpImpl->mnInt32)
741             return;
742         break;
743     case WID_STRING1:
744         if(aValue >>= mpImpl->msString1)
745             return;
746         break;
747     case WID_STRING2:
748         if(aValue >>= mpImpl->msString2)
749             return;
750         break;
751     case WID_STRING3:
752         if(aValue >>= mpImpl->msString3)
753             return;
754         break;
755     }
756 
757     throw lang::IllegalArgumentException();
758 }
759 
760 uno::Any SAL_CALL SvxUnoTextField::getPropertyValue( const OUString& PropertyName )
761 {
762     SolarMutexGuard aGuard;
763 
764     if (PropertyName == UNO_TC_PROP_ANCHOR)
765         return uno::makeAny(mxAnchor);
766 
767     if (PropertyName == UNO_TC_PROP_TEXTFIELD_TYPE)
768         return uno::makeAny(mnServiceId);
769 
770     uno::Any aValue;
771 
772     const SfxItemPropertySimpleEntry* pMap = mpPropSet->getPropertyMap().getByName( PropertyName );
773     if ( !pMap )
774         throw beans::UnknownPropertyException();
775 
776     switch( pMap->nWID )
777     {
778     case WID_DATE:
779         aValue <<= mpImpl->maDateTime;
780         break;
781     case WID_BOOL1:
782         aValue <<= mpImpl->mbBoolean1;
783         break;
784     case WID_BOOL2:
785         aValue <<= mpImpl->mbBoolean2;
786         break;
787     case WID_INT16:
788         aValue <<= mpImpl->mnInt16;
789         break;
790     case WID_INT32:
791         aValue <<= mpImpl->mnInt32;
792         break;
793     case WID_STRING1:
794         aValue <<= mpImpl->msString1;
795         break;
796     case WID_STRING2:
797         aValue <<= mpImpl->msString2;
798         break;
799     case WID_STRING3:
800         aValue <<= mpImpl->msString3;
801         break;
802     }
803 
804     return aValue;
805 }
806 
807 void SAL_CALL SvxUnoTextField::addPropertyChangeListener( const OUString&, const uno::Reference< beans::XPropertyChangeListener >& ) {}
808 void SAL_CALL SvxUnoTextField::removePropertyChangeListener( const OUString&, const uno::Reference< beans::XPropertyChangeListener >& ) {}
809 void SAL_CALL SvxUnoTextField::addVetoableChangeListener( const OUString&, const uno::Reference< beans::XVetoableChangeListener >& ) {}
810 void SAL_CALL SvxUnoTextField::removeVetoableChangeListener( const OUString&, const uno::Reference< beans::XVetoableChangeListener >& ) {}
811 
812 // OComponentHelper
813 void SvxUnoTextField::disposing()
814 {
815     // nothing to do
816 }
817 
818 // lang::XServiceInfo
819 OUString SAL_CALL SvxUnoTextField::getImplementationName()
820 {
821     return OUString("SvxUnoTextField");
822 }
823 
824 uno::Sequence< OUString > SAL_CALL SvxUnoTextField::getSupportedServiceNames()
825 {
826     uno::Sequence<OUString> aSeq(4);
827     OUString* pServices = aSeq.getArray();
828     pServices[0] = "com.sun.star.text.TextContent";
829     pServices[1] = "com.sun.star.text.TextField";
830 
831     switch (mnServiceId)
832     {
833         case text::textfield::Type::DATE:
834             pServices[2] = "com.sun.star.text.TextField.DateTime";
835             pServices[3] = "com.sun.star.text.textfield.DateTime";
836         break;
837         case text::textfield::Type::URL:
838             pServices[2] = "com.sun.star.text.TextField.URL";
839             pServices[3] = "com.sun.star.text.textfield.URL";
840         break;
841         case text::textfield::Type::PAGE:
842             pServices[2] = "com.sun.star.text.TextField.PageNumber";
843             pServices[3] = "com.sun.star.text.textfield.PageNumber";
844         break;
845         case text::textfield::Type::PAGES:
846             pServices[2] = "com.sun.star.text.TextField.PageCount";
847             pServices[3] = "com.sun.star.text.textfield.PageCount";
848         break;
849         case text::textfield::Type::TIME:
850             pServices[2] = "com.sun.star.text.TextField.DateTime";
851             pServices[3] = "com.sun.star.text.textfield.DateTime";
852         break;
853         case text::textfield::Type::DOCINFO_TITLE:
854             pServices[2] = "com.sun.star.text.TextField.docinfo.Title";
855             pServices[3] = "com.sun.star.text.textfield.docinfo.Title";
856         break;
857         case text::textfield::Type::TABLE:
858             pServices[2] = "com.sun.star.text.TextField.SheetName";
859             pServices[3] = "com.sun.star.text.textfield.SheetName";
860         break;
861         case text::textfield::Type::EXTENDED_TIME:
862             pServices[2] = "com.sun.star.text.TextField.DateTime";
863             pServices[3] = "com.sun.star.text.textfield.DateTime";
864         break;
865         case text::textfield::Type::EXTENDED_FILE:
866             pServices[2] = "com.sun.star.text.TextField.FileName";
867             pServices[3] = "com.sun.star.text.textfield.FileName";
868         break;
869         case text::textfield::Type::AUTHOR:
870             pServices[2] = "com.sun.star.text.TextField.Author";
871             pServices[3] = "com.sun.star.text.textfield.Author";
872         break;
873         case text::textfield::Type::MEASURE:
874             pServices[2] = "com.sun.star.text.TextField.Measure";
875             pServices[3] = "com.sun.star.text.textfield.Measure";
876         break;
877         case text::textfield::Type::PRESENTATION_HEADER:
878             pServices[2] = "com.sun.star.presentation.TextField.Header";
879             pServices[3] = "com.sun.star.presentation.textfield.Header";
880         break;
881         case text::textfield::Type::PRESENTATION_FOOTER:
882             pServices[2] = "com.sun.star.presentation.TextField.Footer";
883             pServices[3] = "com.sun.star.presentation.textfield.Footer";
884         break;
885         case text::textfield::Type::PRESENTATION_DATE_TIME:
886             pServices[2] = "com.sun.star.presentation.TextField.DateTime";
887             pServices[3] = "com.sun.star.presentation.textfield.DateTime";
888         break;
889         case text::textfield::Type::PAGE_NAME:
890             pServices[2] = "com.sun.star.text.TextField.PageName";
891             pServices[3] = "com.sun.star.text.textfield.PageName";
892         break;
893         case text::textfield::Type::DOCINFO_CUSTOM:
894             pServices[2] = "com.sun.star.text.TextField.DocInfo.Custom";
895             pServices[3] = "com.sun.star.text.textfield.DocInfo.Custom";
896         break;
897         default:
898             aSeq.realloc(0);
899     }
900 
901     return aSeq;
902 }
903 
904 sal_Bool SAL_CALL SvxUnoTextField::supportsService( const OUString& ServiceName )
905 {
906     return cppu::supportsService( this, ServiceName );
907 }
908 
909 uno::Reference< uno::XInterface > SvxUnoTextCreateTextField( const OUString& ServiceSpecifier )
910 {
911     uno::Reference< uno::XInterface > xRet;
912 
913     const OUString aTextFieldPrexit( "com.sun.star.text.textfield." );
914 
915     // #i93308# up to OOo 3.2 we used this wrong namespace name with the capital T & F. This is
916     // fixed since OOo 3.2 but for compatibility we will still provide support for the wrong notation.
917 
918     if( (ServiceSpecifier.startsWith( aTextFieldPrexit )) ||
919         (ServiceSpecifier.startsWith( "com.sun.star.text.TextField." )) )
920     {
921         OUString aFieldType( ServiceSpecifier.copy( aTextFieldPrexit.getLength() ) );
922 
923         sal_Int32 nId = text::textfield::Type::UNSPECIFIED;
924 
925         if ( aFieldType == "DateTime" )
926         {
927             nId = text::textfield::Type::DATE;
928         }
929         else if ( aFieldType == "URL" )
930         {
931             nId = text::textfield::Type::URL;
932         }
933         else if ( aFieldType == "PageNumber" )
934         {
935             nId = text::textfield::Type::PAGE;
936         }
937         else if ( aFieldType == "PageCount" )
938         {
939             nId = text::textfield::Type::PAGES;
940         }
941         else if ( aFieldType == "SheetName" )
942         {
943             nId = text::textfield::Type::TABLE;
944         }
945         else if ( aFieldType == "FileName" )
946         {
947             nId = text::textfield::Type::EXTENDED_FILE;
948         }
949         else if (aFieldType == "docinfo.Title" ||
950                  aFieldType == "DocInfo.Title" )
951         {
952             nId = text::textfield::Type::DOCINFO_TITLE;
953         }
954         else if ( aFieldType == "Author" )
955         {
956             nId = text::textfield::Type::AUTHOR;
957         }
958         else if ( aFieldType == "Measure" )
959         {
960             nId = text::textfield::Type::MEASURE;
961         }
962         else if (aFieldType == "DocInfo.Custom")
963         {
964             nId = text::textfield::Type::DOCINFO_CUSTOM;
965         }
966 
967         if (nId != text::textfield::Type::UNSPECIFIED)
968             xRet = static_cast<cppu::OWeakObject *>(new SvxUnoTextField( nId ));
969     }
970 
971     return xRet;
972 }
973 
974 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
975