1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <sal/log.hxx>
#include <unotools/configitem.hxx>
#include <unotools/configmgr.hxx>
#include <unotools/configpaths.hxx>
#include <comphelper/processfactory.hxx>
#include <com/sun/star/beans/XMultiPropertySet.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/util/XChangesListener.hpp>
#include <com/sun/star/util/XChangesNotifier.hpp>
#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
#include <com/sun/star/container/XHierarchicalName.hpp>
#include <com/sun/star/configuration/XTemplateContainer.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/awt/XRequestCallback.hpp>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <com/sun/star/beans/PropertyAttribute.hpp>
#include <com/sun/star/util/XStringEscape.hpp>
#include <com/sun/star/util/XChangesBatch.hpp>
#include <osl/diagnose.h>
#include <comphelper/solarmutex.hxx>
#include <rtl/ref.hxx>
#include <rtl/ustrbuf.hxx>
using namespace utl;
using namespace com::sun::star::uno;
using namespace com::sun::star::util;
using namespace com::sun::star::lang;
using namespace com::sun::star::beans;
using namespace com::sun::star::container;
using namespace com::sun::star::configuration;
#include <cppuhelper/implbase.hxx>
#ifdef DBG_UTIL
static inline void lcl_CFG_DBG_EXCEPTION(const sal_Char* cText, const Exception& rEx)
{
OString sMsg(cText);
sMsg += OString(rEx.Message.getStr(), rEx.Message.getLength(), RTL_TEXTENCODING_ASCII_US);
OSL_FAIL(sMsg.getStr());
}
#define CATCH_INFO(a) \
catch(const Exception& rEx) \
{ \
lcl_CFG_DBG_EXCEPTION(a, rEx);\
}
#else
#define CATCH_INFO(a) catch(const Exception&){}
#endif
/*
The ConfigChangeListener_Impl receives notifications from the configuration about changes that
have happened. It forwards this notification to the ConfigItem it knows a pParent by calling its
"CallNotify" method. As ConfigItems are most probably not thread safe, the SolarMutex is acquired
before doing so.
*/
namespace utl{
class ConfigChangeListener_Impl : public cppu::WeakImplHelper
<
com::sun::star::util::XChangesListener
>
{
public:
ConfigItem* pParent;
const Sequence< OUString > aPropertyNames;
ConfigChangeListener_Impl(ConfigItem& rItem, const Sequence< OUString >& rNames);
virtual ~ConfigChangeListener_Impl();
//XChangesListener
virtual void SAL_CALL changesOccurred( const ChangesEvent& Event ) throw(RuntimeException, std::exception) override;
//XEventListener
virtual void SAL_CALL disposing( const EventObject& Source ) throw(RuntimeException, std::exception) override;
};
}
class ValueCounter_Impl
{
sal_Int16& rCnt;
public:
explicit ValueCounter_Impl(sal_Int16& rCounter)
: rCnt(rCounter)
{
rCnt++;
}
~ValueCounter_Impl()
{
OSL_ENSURE(rCnt>0, "RefCount < 0 ??");
rCnt--;
}
};
ConfigChangeListener_Impl::ConfigChangeListener_Impl(
ConfigItem& rItem, const Sequence< OUString >& rNames) :
pParent(&rItem),
aPropertyNames(rNames)
{
}
ConfigChangeListener_Impl::~ConfigChangeListener_Impl()
{
}
static bool lcl_Find(
const OUString& rTemp,
const OUString* pCheckPropertyNames,
sal_Int32 nLength)
{
//return true if the path is completely correct or if it is longer
//i.e ...Print/Content/Graphic and .../Print
for(sal_Int32 nIndex = 0; nIndex < nLength; nIndex++)
if( isPrefixOfConfigurationPath(rTemp, pCheckPropertyNames[nIndex]) )
return true;
return false;
}
void ConfigChangeListener_Impl::changesOccurred( const ChangesEvent& rEvent ) throw(RuntimeException, std::exception)
{
const ElementChange* pElementChanges = rEvent.Changes.getConstArray();
Sequence<OUString> aChangedNames(rEvent.Changes.getLength());
OUString* pNames = aChangedNames.getArray();
const OUString* pCheckPropertyNames = aPropertyNames.getConstArray();
sal_Int32 nNotify = 0;
for(int i = 0; i < aChangedNames.getLength(); i++)
{
OUString sTemp;
pElementChanges[i].Accessor >>= sTemp;
if(lcl_Find(sTemp, pCheckPropertyNames, aPropertyNames.getLength()))
pNames[nNotify++] = sTemp;
}
if( nNotify )
{
::comphelper::SolarMutex *pMutex = ::comphelper::SolarMutex::get();
if ( pMutex )
{
rtl::Reference< comphelper::SolarMutex > aGuard( pMutex );
aChangedNames.realloc(nNotify);
pParent->CallNotify(aChangedNames);
}
}
}
void ConfigChangeListener_Impl::disposing( const EventObject& /*rSource*/ ) throw(RuntimeException, std::exception)
{
pParent->RemoveChangesListener();
}
ConfigItem::ConfigItem(const OUString &rSubTree, ConfigItemMode nSetMode ) :
sSubTree(rSubTree),
m_nMode(nSetMode),
m_bIsModified(false),
m_bEnableInternalNotification(false),
m_nInValueChange(0)
{
if (utl::ConfigManager::IsAvoidConfig())
return;
if (nSetMode & ConfigItemMode::ReleaseTree)
ConfigManager::getConfigManager().addConfigItem(*this);
else
m_xHierarchyAccess = ConfigManager::getConfigManager().addConfigItem(*this);
}
ConfigItem::~ConfigItem()
{
RemoveChangesListener();
ConfigManager::getConfigManager().removeConfigItem(*this);
}
void ConfigItem::CallNotify( const com::sun::star::uno::Sequence<OUString>& rPropertyNames )
{
// the call is forwarded to the virtual Notify() method
// it is pure virtual, so all classes deriving from ConfigItem have to decide how they
// want to notify listeners
if(!IsInValueChange() || m_bEnableInternalNotification)
Notify(rPropertyNames);
}
void ConfigItem::impl_packLocalizedProperties( const Sequence< OUString >& lInNames ,
const Sequence< Any >& lInValues ,
Sequence< Any >& lOutValues )
{
// Safe impossible cases.
// This method should be called for special ConfigItem-mode only!
OSL_ENSURE( ((m_nMode & ConfigItemMode::AllLocales ) == ConfigItemMode::AllLocales), "ConfigItem::impl_packLocalizedProperties()\nWrong call of this method detected!\n" );
sal_Int32 nSourceCounter; // used to step during input lists
sal_Int32 nSourceSize; // marks end of loop over input lists
sal_Int32 nDestinationCounter; // actual position in output lists
sal_Int32 nPropertyCounter; // counter of inner loop for Sequence< PropertyValue >
sal_Int32 nPropertiesSize; // marks end of inner loop
Sequence< OUString > lPropertyNames; // list of all locales for localized entry
Sequence< PropertyValue > lProperties; // localized values of an configuration entry packed for return
Reference< XInterface > xLocalizedNode; // if cfg entry is localized ... lInValues contains an XInterface!
// Optimise follow algorithm ... A LITTLE BIT :-)
// There exist two different possibilities:
// i ) There exist no localized entries ... => size of lOutValues will be the same like lInNames/lInValues!
// ii) There exist some (mostly one or two) localized entries ... => size of lOutValues will be the same like lInNames/lInValues!
// ... Why? If a localized value exist - the any is filled with an XInterface object (is a SetNode-service).
// We read all his child nodes and pack it into Sequence< PropertyValue >.
// The result list we pack into the return any. We never change size of lists!
nSourceSize = lInNames.getLength();
lOutValues.realloc( nSourceSize );
// Algorithm:
// Copy all names and values from in to out lists.
// Look for special localized entries ... You can detect it as "XInterface" packed into an Any.
// Use this XInterface-object to read all localized values and pack it into Sequence< PropertValue >.
// Add this list to out lists then.
nDestinationCounter = 0;
for( nSourceCounter=0; nSourceCounter<nSourceSize; ++nSourceCounter )
{
// If item a special localized one ... convert and pack it ...
if( lInValues[nSourceCounter].getValueTypeName() == "com.sun.star.uno.XInterface" )
{
lInValues[nSourceCounter] >>= xLocalizedNode;
Reference< XNameContainer > xSetAccess( xLocalizedNode, UNO_QUERY );
if( xSetAccess.is() )
{
lPropertyNames = xSetAccess->getElementNames();
nPropertiesSize = lPropertyNames.getLength();
lProperties.realloc( nPropertiesSize );
for( nPropertyCounter=0; nPropertyCounter<nPropertiesSize; ++nPropertyCounter )
{
lProperties[nPropertyCounter].Name = lPropertyNames[nPropertyCounter];
OUString sLocaleValue;
xSetAccess->getByName( lPropertyNames[nPropertyCounter] ) >>= sLocaleValue;
lProperties[nPropertyCounter].Value <<= sLocaleValue;
}
lOutValues[nDestinationCounter] <<= lProperties;
}
}
// ... or copy normal items to return lists directly.
else
{
lOutValues[nDestinationCounter] = lInValues[nSourceCounter];
}
++nDestinationCounter;
}
}
void ConfigItem::impl_unpackLocalizedProperties( const Sequence< OUString >& lInNames ,
const Sequence< Any >& lInValues ,
Sequence< OUString >& lOutNames ,
Sequence< Any >& lOutValues )
{
// Safe impossible cases.
// This method should be called for special ConfigItem-mode only!
OSL_ENSURE( ((m_nMode & ConfigItemMode::AllLocales ) == ConfigItemMode::AllLocales), "ConfigItem::impl_unpackLocalizedProperties()\nWrong call of this method detected!\n" );
sal_Int32 nSourceCounter; // used to step during input lists
sal_Int32 nSourceSize; // marks end of loop over input lists
sal_Int32 nDestinationCounter; // actual position in output lists
sal_Int32 nPropertyCounter; // counter of inner loop for Sequence< PropertyValue >
sal_Int32 nPropertiesSize; // marks end of inner loop
OUString sNodeName; // base name of node ( e.g. "UIName/" ) ... expand to locale ( e.g. "UIName/de" )
Sequence< PropertyValue > lProperties; // localized values of an configuration entry gotten from lInValues-Any
// Optimise follow algorithm ... A LITTLE BIT :-)
// There exist two different possibilities:
// i ) There exist no localized entries ... => size of lOutNames/lOutValues will be the same like lInNames/lInValues!
// ii) There exist some (mostly one or two) localized entries ... => size of lOutNames/lOutValues will be some bytes greater then lInNames/lInValues.
// => I think we should make it fast for i). ii) is a special case and mustn't be SOOOO... fast.
// We should reserve same space for output list like input ones first.
// Follow algorithm looks for these borders and change it for ii) only!
// It will be faster then a "realloc()" call in every loop ...
nSourceSize = lInNames.getLength();
lOutNames.realloc ( nSourceSize );
lOutValues.realloc ( nSourceSize );
// Algorithm:
// Copy all names and values from const to return lists.
// Look for special localized entries ... You can detect it as Sequence< PropertyValue > packed into an Any.
// Split it ... insert PropertyValue.Name to lOutNames and PropertyValue.Value to lOutValues.
nDestinationCounter = 0;
for( nSourceCounter=0; nSourceCounter<nSourceSize; ++nSourceCounter )
{
// If item a special localized one ... split it and insert his parts to output lists ...
if( lInValues[nSourceCounter].getValueType() == cppu::UnoType<Sequence<PropertyValue>>::get() )
{
lInValues[nSourceCounter] >>= lProperties;
nPropertiesSize = lProperties.getLength();
sNodeName = lInNames[nSourceCounter] + "/";
if( (nDestinationCounter+nPropertiesSize) > lOutNames.getLength() )
{
lOutNames.realloc ( nDestinationCounter+nPropertiesSize );
lOutValues.realloc ( nDestinationCounter+nPropertiesSize );
}
for( nPropertyCounter=0; nPropertyCounter<nPropertiesSize; ++nPropertyCounter )
{
lOutNames [nDestinationCounter] = sNodeName + lProperties[nPropertyCounter].Name;
lOutValues[nDestinationCounter] = lProperties[nPropertyCounter].Value;
++nDestinationCounter;
}
}
// ... or copy normal items to return lists directly.
else
{
if( (nDestinationCounter+1) > lOutNames.getLength() )
{
lOutNames.realloc ( nDestinationCounter+1 );
lOutValues.realloc ( nDestinationCounter+1 );
}
lOutNames [nDestinationCounter] = lInNames [nSourceCounter];
lOutValues[nDestinationCounter] = lInValues[nSourceCounter];
++nDestinationCounter;
}
}
}
Sequence< sal_Bool > ConfigItem::GetReadOnlyStates(const com::sun::star::uno::Sequence< OUString >& rNames)
{
sal_Int32 i;
// size of return list is fix!
// Every item must match to length of incoming name list.
sal_Int32 nCount = rNames.getLength();
Sequence< sal_Bool > lStates(nCount);
// We must be sure to return a valid information every time!
// Set default to non readonly... similar to the configuration handling of this property.
for ( i=0; i<nCount; ++i)
lStates[i] = sal_False;
// no access - no information...
Reference< XHierarchicalNameAccess > xHierarchyAccess = GetTree();
if (!xHierarchyAccess.is())
return lStates;
for (i=0; i<nCount; ++i)
{
try
{
OUString sName = rNames[i];
OUString sPath;
OUString sProperty;
(void)::utl::splitLastFromConfigurationPath(sName,sPath,sProperty);
if (sPath.isEmpty() && sProperty.isEmpty())
{
OSL_FAIL("ConfigItem::IsReadonly()\nsplitt failed\n");
continue;
}
Reference< XInterface > xNode;
Reference< XPropertySet > xSet;
Reference< XPropertySetInfo > xInfo;
if (!sPath.isEmpty())
{
Any aNode = xHierarchyAccess->getByHierarchicalName(sPath);
if (!(aNode >>= xNode) || !xNode.is())
{
OSL_FAIL("ConfigItem::IsReadonly()\nno set available\n");
continue;
}
}
else
{
xNode.set( xHierarchyAccess, UNO_QUERY );
}
xSet.set(xNode, UNO_QUERY);
if (xSet.is())
{
xInfo = xSet->getPropertySetInfo();
OSL_ENSURE(xInfo.is(), "ConfigItem::IsReadonly()\ngetPropertySetInfo failed ...\n");
}
else
{
xInfo.set(xNode, UNO_QUERY);
OSL_ENSURE(xInfo.is(), "ConfigItem::IsReadonly()\nUNO_QUERY failed ...\n");
}
if (!xInfo.is())
{
OSL_FAIL("ConfigItem::IsReadonly()\nno prop info available\n");
continue;
}
Property aProp = xInfo->getPropertyByName(sProperty);
lStates[i] = ((aProp.Attributes & PropertyAttribute::READONLY) == PropertyAttribute::READONLY);
}
catch (const Exception&)
{
}
}
return lStates;
}
Sequence< Any > ConfigItem::GetProperties(const Sequence< OUString >& rNames)
{
Sequence< Any > aRet(rNames.getLength());
const OUString* pNames = rNames.getConstArray();
Any* pRet = aRet.getArray();
Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree();
if(xHierarchyAccess.is())
{
for(int i = 0; i < rNames.getLength(); i++)
{
try
{
pRet[i] = xHierarchyAccess->getByHierarchicalName(pNames[i]);
}
catch (const Exception& rEx)
{
SAL_WARN(
"unotools.config",
"ignoring XHierarchicalNameAccess to /org.openoffice."
<< sSubTree << "/" << pNames[i] << " Exception: "
<< rEx.Message);
}
}
// In special mode "ALL_LOCALES" we must convert localized values to Sequence< PropertyValue >.
if((m_nMode & ConfigItemMode::AllLocales ) == ConfigItemMode::AllLocales)
{
Sequence< Any > lValues;
impl_packLocalizedProperties( rNames, aRet, lValues );
aRet = lValues;
}
}
return aRet;
}
bool ConfigItem::PutProperties( const Sequence< OUString >& rNames,
const Sequence< Any>& rValues)
{
ValueCounter_Impl aCounter(m_nInValueChange);
Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree();
Reference<XNameReplace> xTopNodeReplace(xHierarchyAccess, UNO_QUERY);
bool bRet = xHierarchyAccess.is() && xTopNodeReplace.is();
if(bRet)
{
Sequence< OUString > lNames;
Sequence< Any > lValues;
const OUString* pNames = NULL;
const Any* pValues = NULL;
sal_Int32 nNameCount;
if(( m_nMode & ConfigItemMode::AllLocales ) == ConfigItemMode::AllLocales )
{
// If ConfigItem works in "ALL_LOCALES"-mode ... we must support a Sequence< PropertyValue >
// as value of an localized configuration entry!
// How we can do that?
// We must split all PropertyValues to "Sequence< OUString >" AND "Sequence< Any >"!
impl_unpackLocalizedProperties( rNames, rValues, lNames, lValues );
pNames = lNames.getConstArray ();
pValues = lValues.getConstArray ();
nNameCount = lNames.getLength ();
}
else
{
// This is the normal mode ...
// Use given input lists directly.
pNames = rNames.getConstArray ();
pValues = rValues.getConstArray ();
nNameCount = rNames.getLength ();
}
for(int i = 0; i < nNameCount; i++)
{
try
{
OUString sNode, sProperty;
if (splitLastFromConfigurationPath(pNames[i],sNode, sProperty))
{
Any aNode = xHierarchyAccess->getByHierarchicalName(sNode);
Reference<XNameAccess> xNodeAcc;
aNode >>= xNodeAcc;
Reference<XNameReplace> xNodeReplace(xNodeAcc, UNO_QUERY);
Reference<XNameContainer> xNodeCont (xNodeAcc, UNO_QUERY);
bool bExist = (xNodeAcc.is() && xNodeAcc->hasByName(sProperty));
if (bExist && xNodeReplace.is())
xNodeReplace->replaceByName(sProperty, pValues[i]);
else
if (!bExist && xNodeCont.is())
xNodeCont->insertByName(sProperty, pValues[i]);
else
bRet = false;
}
else //direct value
{
xTopNodeReplace->replaceByName(sProperty, pValues[i]);
}
}
CATCH_INFO("Exception from PutProperties: ");
}
try
{
Reference<XChangesBatch> xBatch(xHierarchyAccess, UNO_QUERY);
xBatch->commitChanges();
}
CATCH_INFO("Exception from commitChanges(): ")
}
return bRet;
}
void ConfigItem::DisableNotification()
{
OSL_ENSURE( xChangeLstnr.is(), "ConfigItem::DisableNotification: notifications not enabled currently!" );
RemoveChangesListener();
}
bool ConfigItem::EnableNotification(const Sequence< OUString >& rNames,
bool bEnableInternalNotification )
{
OSL_ENSURE(!(m_nMode & ConfigItemMode::ReleaseTree), "notification in ConfigItemMode::ReleaseTree mode not possible");
m_bEnableInternalNotification = bEnableInternalNotification;
Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree();
Reference<XChangesNotifier> xChgNot(xHierarchyAccess, UNO_QUERY);
if(!xChgNot.is())
return false;
OSL_ENSURE(!xChangeLstnr.is(), "EnableNotification already called");
if(xChangeLstnr.is())
xChgNot->removeChangesListener( xChangeLstnr );
bool bRet = true;
try
{
xChangeLstnr = new ConfigChangeListener_Impl(*this, rNames);
xChgNot->addChangesListener( xChangeLstnr );
}
catch (const RuntimeException&)
{
bRet = false;
}
return bRet;
}
void ConfigItem::RemoveChangesListener()
{
Reference<XChangesNotifier> xChgNot(m_xHierarchyAccess, UNO_QUERY);
if(xChgNot.is() && xChangeLstnr.is())
{
try
{
xChgNot->removeChangesListener( xChangeLstnr );
xChangeLstnr = 0;
}
catch (const Exception&)
{
}
}
}
static void lcl_normalizeLocalNames(Sequence< OUString >& _rNames, ConfigNameFormat _eFormat, Reference<XInterface> const& _xParentNode)
{
switch (_eFormat)
{
case CONFIG_NAME_LOCAL_NAME:
// unaltered - this is our input format
break;
case CONFIG_NAME_FULL_PATH:
{
Reference<XHierarchicalName> xFormatter(_xParentNode, UNO_QUERY);
if (xFormatter.is())
{
OUString * pNames = _rNames.getArray();
for(int i = 0; i<_rNames.getLength(); ++i)
{
try
{
pNames[i] = xFormatter->composeHierarchicalName(pNames[i]);
}
CATCH_INFO("Exception from composeHierarchicalName(): ")
}
break;
}
}
OSL_FAIL("Cannot create absolute paths: missing interface");
// make local paths instaed
case CONFIG_NAME_LOCAL_PATH:
{
Reference<XTemplateContainer> xTypeContainer(_xParentNode, UNO_QUERY);
if (xTypeContainer.is())
{
OUString sTypeName = xTypeContainer->getElementTemplateName();
sTypeName = sTypeName.copy(sTypeName.lastIndexOf('/')+1);
OUString * pNames = _rNames.getArray();
for(int i = 0; i<_rNames.getLength(); ++i)
{
pNames[i] = wrapConfigurationElementName(pNames[i],sTypeName);
}
}
else
{
Reference<XServiceInfo> xSVI(_xParentNode, UNO_QUERY);
if (xSVI.is() && xSVI->supportsService("com.sun.star.configuration.SetAccess"))
{
OUString * pNames = _rNames.getArray();
for(int i = 0; i<_rNames.getLength(); ++i)
{
pNames[i] = wrapConfigurationElementName(pNames[i]);
}
}
}
}
break;
case CONFIG_NAME_PLAINTEXT_NAME:
{
Reference<XStringEscape> xEscaper(_xParentNode, UNO_QUERY);
if (xEscaper.is())
{
OUString * pNames = _rNames.getArray();
for(int i = 0; i<_rNames.getLength(); ++i)
try
{
pNames[i] = xEscaper->unescapeString(pNames[i]);
}
CATCH_INFO("Exception from unescapeString(): ")
}
}
break;
}
}
Sequence< OUString > ConfigItem::GetNodeNames(const OUString& rNode)
{
ConfigNameFormat const eDefaultFormat = CONFIG_NAME_LOCAL_NAME; // CONFIG_NAME_DEFAULT;
return GetNodeNames(rNode, eDefaultFormat);
}
Sequence< OUString > ConfigItem::GetNodeNames(const OUString& rNode, ConfigNameFormat eFormat)
{
Sequence< OUString > aRet;
Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree();
if(xHierarchyAccess.is())
{
try
{
Reference<XNameAccess> xCont;
if(!rNode.isEmpty())
{
Any aNode = xHierarchyAccess->getByHierarchicalName(rNode);
aNode >>= xCont;
}
else
xCont.set(xHierarchyAccess, UNO_QUERY);
if(xCont.is())
{
aRet = xCont->getElementNames();
lcl_normalizeLocalNames(aRet,eFormat,xCont);
}
}
CATCH_INFO("Exception from GetNodeNames: ");
}
return aRet;
}
bool ConfigItem::ClearNodeSet(const OUString& rNode)
{
ValueCounter_Impl aCounter(m_nInValueChange);
bool bRet = false;
Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree();
if(xHierarchyAccess.is())
{
try
{
Reference<XNameContainer> xCont;
if(!rNode.isEmpty())
{
Any aNode = xHierarchyAccess->getByHierarchicalName(rNode);
aNode >>= xCont;
}
else
xCont.set(xHierarchyAccess, UNO_QUERY);
if(!xCont.is())
return false;
Sequence< OUString > aNames = xCont->getElementNames();
const OUString* pNames = aNames.getConstArray();
Reference<XChangesBatch> xBatch(xHierarchyAccess, UNO_QUERY);
for(sal_Int32 i = 0; i < aNames.getLength(); i++)
{
try
{
xCont->removeByName(pNames[i]);
}
CATCH_INFO("Exception from removeByName(): ")
}
xBatch->commitChanges();
bRet = true;
}
CATCH_INFO("Exception from ClearNodeSet")
}
return bRet;
}
bool ConfigItem::ClearNodeElements(const OUString& rNode, Sequence< OUString >& rElements)
{
ValueCounter_Impl aCounter(m_nInValueChange);
bool bRet = false;
Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree();
if(xHierarchyAccess.is())
{
const OUString* pElements = rElements.getConstArray();
try
{
Reference<XNameContainer> xCont;
if(!rNode.isEmpty())
{
Any aNode = xHierarchyAccess->getByHierarchicalName(rNode);
aNode >>= xCont;
}
else
xCont.set(xHierarchyAccess, UNO_QUERY);
if(!xCont.is())
return false;
try
{
for(sal_Int32 nElement = 0; nElement < rElements.getLength(); nElement++)
{
xCont->removeByName(pElements[nElement]);
}
Reference<XChangesBatch> xBatch(xHierarchyAccess, UNO_QUERY);
xBatch->commitChanges();
}
CATCH_INFO("Exception from commitChanges(): ")
bRet = true;
}
CATCH_INFO("Exception from GetNodeNames: ")
}
return bRet;
}
static inline
OUString lcl_extractSetPropertyName( const OUString& rInPath, const OUString& rPrefix )
{
OUString const sSubPath = dropPrefixFromConfigurationPath( rInPath, rPrefix);
return extractFirstFromConfigurationPath( sSubPath );
}
static
Sequence< OUString > lcl_extractSetPropertyNames( const Sequence< PropertyValue >& rValues, const OUString& rPrefix )
{
const PropertyValue* pProperties = rValues.getConstArray();
Sequence< OUString > aSubNodeNames(rValues.getLength());
OUString* pSubNodeNames = aSubNodeNames.getArray();
OUString sLastSubNode;
sal_Int32 nSubIndex = 0;
for(sal_Int32 i = 0; i < rValues.getLength(); i++)
{
OUString const sSubPath = dropPrefixFromConfigurationPath( pProperties[i].Name, rPrefix);
OUString const sSubNode = extractFirstFromConfigurationPath( sSubPath );
if(sLastSubNode != sSubNode)
{
pSubNodeNames[nSubIndex++] = sSubNode;
}
sLastSubNode = sSubNode;
}
aSubNodeNames.realloc(nSubIndex);
return aSubNodeNames;
}
// Add or change properties
bool ConfigItem::SetSetProperties(
const OUString& rNode, const Sequence< PropertyValue >& rValues)
{
ValueCounter_Impl aCounter(m_nInValueChange);
bool bRet = true;
Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree();
if(xHierarchyAccess.is())
{
Reference<XChangesBatch> xBatch(xHierarchyAccess, UNO_QUERY);
try
{
Reference<XNameContainer> xCont;
if(!rNode.isEmpty())
{
Any aNode = xHierarchyAccess->getByHierarchicalName(rNode);
aNode >>= xCont;
}
else
xCont.set(xHierarchyAccess, UNO_QUERY);
if(!xCont.is())
return false;
Reference<XSingleServiceFactory> xFac(xCont, UNO_QUERY);
if(xFac.is())
{
const Sequence< OUString > aSubNodeNames = lcl_extractSetPropertyNames(rValues, rNode);
const sal_Int32 nSubNodeCount = aSubNodeNames.getLength();
for(sal_Int32 j = 0; j <nSubNodeCount; j++)
{
if(!xCont->hasByName(aSubNodeNames[j]))
{
Reference<XInterface> xInst = xFac->createInstance();
Any aVal; aVal <<= xInst;
xCont->insertByName(aSubNodeNames[j], aVal);
}
//set values
}
try
{
xBatch->commitChanges();
}
CATCH_INFO("Exception from commitChanges(): ")
const PropertyValue* pProperties = rValues.getConstArray();
Sequence< OUString > aSetNames(rValues.getLength());
OUString* pSetNames = aSetNames.getArray();
Sequence< Any> aSetValues(rValues.getLength());
Any* pSetValues = aSetValues.getArray();
bool bEmptyNode = rNode.isEmpty();
for(sal_Int32 k = 0; k < rValues.getLength(); k++)
{
pSetNames[k] = pProperties[k].Name.copy( bEmptyNode ? 1 : 0);
pSetValues[k] = pProperties[k].Value;
}
bRet = PutProperties(aSetNames, aSetValues);
}
else
{
//if no factory is available then the node contains basic data elements
const PropertyValue* pValues = rValues.getConstArray();
for(int nValue = 0; nValue < rValues.getLength();nValue++)
{
try
{
OUString sSubNode = lcl_extractSetPropertyName( pValues[nValue].Name, rNode );
if(xCont->hasByName(sSubNode))
xCont->replaceByName(sSubNode, pValues[nValue].Value);
else
xCont->insertByName(sSubNode, pValues[nValue].Value);
OSL_ENSURE( xHierarchyAccess->hasByHierarchicalName(pValues[nValue].Name),
"Invalid config path" );
}
CATCH_INFO("Exception form insert/replaceByName(): ")
}
xBatch->commitChanges();
}
}
#ifdef DBG_UTIL
catch (const Exception& rEx)
{
lcl_CFG_DBG_EXCEPTION("Exception from SetSetProperties: ", rEx);
#else
catch (const Exception&)
{
#endif
bRet = false;
}
}
return bRet;
}
bool ConfigItem::ReplaceSetProperties(
const OUString& rNode, const Sequence< PropertyValue >& rValues)
{
ValueCounter_Impl aCounter(m_nInValueChange);
bool bRet = true;
Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree();
if(xHierarchyAccess.is())
{
Reference<XChangesBatch> xBatch(xHierarchyAccess, UNO_QUERY);
try
{
Reference<XNameContainer> xCont;
if(!rNode.isEmpty())
{
Any aNode = xHierarchyAccess->getByHierarchicalName(rNode);
aNode >>= xCont;
}
else
xCont.set(xHierarchyAccess, UNO_QUERY);
if(!xCont.is())
return false;
// JB: Change: now the same name handling for sets of simple values
const Sequence< OUString > aSubNodeNames = lcl_extractSetPropertyNames(rValues, rNode);
const OUString* pSubNodeNames = aSubNodeNames.getConstArray();
const sal_Int32 nSubNodeCount = aSubNodeNames.getLength();
Reference<XSingleServiceFactory> xFac(xCont, UNO_QUERY);
const bool isSimpleValueSet = !xFac.is();
//remove unknown members first
{
const Sequence<OUString> aContainerSubNodes = xCont->getElementNames();
const OUString* pContainerSubNodes = aContainerSubNodes.getConstArray();
for(sal_Int32 nContSub = 0; nContSub < aContainerSubNodes.getLength(); nContSub++)
{
bool bFound = false;
for(sal_Int32 j = 0; j < nSubNodeCount; j++)
{
if(pSubNodeNames[j] == pContainerSubNodes[nContSub])
{
bFound = true;
break;
}
}
if(!bFound)
try
{
xCont->removeByName(pContainerSubNodes[nContSub]);
}
catch (const Exception&)
{
if (isSimpleValueSet)
{
try
{
// #i37322#: fallback action: replace with <void/>
xCont->replaceByName(pContainerSubNodes[nContSub], Any());
// fallback successful: continue looping
continue;
}
catch (Exception &)
{} // propagate original exception, if fallback fails
}
throw;
}
}
try { xBatch->commitChanges(); }
CATCH_INFO("Exception from commitChanges(): ")
}
if(xFac.is()) // !isSimpleValueSet
{
for(sal_Int32 j = 0; j < nSubNodeCount; j++)
{
if(!xCont->hasByName(pSubNodeNames[j]))
{
//create if not available
Reference<XInterface> xInst = xFac->createInstance();
Any aVal; aVal <<= xInst;
xCont->insertByName(pSubNodeNames[j], aVal);
}
}
try { xBatch->commitChanges(); }
CATCH_INFO("Exception from commitChanges(): ")
const PropertyValue* pProperties = rValues.getConstArray();
Sequence< OUString > aSetNames(rValues.getLength());
OUString* pSetNames = aSetNames.getArray();
Sequence< Any> aSetValues(rValues.getLength());
Any* pSetValues = aSetValues.getArray();
bool bEmptyNode = rNode.isEmpty();
for(sal_Int32 k = 0; k < rValues.getLength(); k++)
{
pSetNames[k] = pProperties[k].Name.copy( bEmptyNode ? 1 : 0);
pSetValues[k] = pProperties[k].Value;
}
bRet = PutProperties(aSetNames, aSetValues);
}
else
{
const PropertyValue* pValues = rValues.getConstArray();
//if no factory is available then the node contains basic data elements
for(int nValue = 0; nValue < rValues.getLength();nValue++)
{
try
{
OUString sSubNode = lcl_extractSetPropertyName( pValues[nValue].Name, rNode );
if(xCont->hasByName(sSubNode))
xCont->replaceByName(sSubNode, pValues[nValue].Value);
else
xCont->insertByName(sSubNode, pValues[nValue].Value);
}
CATCH_INFO("Exception from insert/replaceByName(): ");
}
xBatch->commitChanges();
}
}
#ifdef DBG_UTIL
catch (const Exception& rEx)
{
lcl_CFG_DBG_EXCEPTION("Exception from ReplaceSetProperties: ", rEx);
#else
catch (const Exception&)
{
#endif
bRet = false;
}
}
return bRet;
}
bool ConfigItem::AddNode(const OUString& rNode, const OUString& rNewNode)
{
ValueCounter_Impl aCounter(m_nInValueChange);
bool bRet = true;
Reference<XHierarchicalNameAccess> xHierarchyAccess = GetTree();
if(xHierarchyAccess.is())
{
Reference<XChangesBatch> xBatch(xHierarchyAccess, UNO_QUERY);
try
{
Reference<XNameContainer> xCont;
if(!rNode.isEmpty())
{
Any aNode = xHierarchyAccess->getByHierarchicalName(rNode);
aNode >>= xCont;
}
else
xCont.set(xHierarchyAccess, UNO_QUERY);
if(!xCont.is())
return false;
Reference<XSingleServiceFactory> xFac(xCont, UNO_QUERY);
if(xFac.is())
{
if(!xCont->hasByName(rNewNode))
{
Reference<XInterface> xInst = xFac->createInstance();
Any aVal; aVal <<= xInst;
xCont->insertByName(rNewNode, aVal);
}
try
{
xBatch->commitChanges();
}
CATCH_INFO("Exception from commitChanges(): ")
}
else
{
//if no factory is available then the node contains basic data elements
try
{
if(!xCont->hasByName(rNewNode))
xCont->insertByName(rNewNode, Any());
}
CATCH_INFO("Exception from AddNode(): ")
}
xBatch->commitChanges();
}
#ifdef DBG_UTIL
catch (const Exception& rEx)
{
lcl_CFG_DBG_EXCEPTION("Exception from AddNode(): ", rEx);
#else
catch (const Exception&)
{
#endif
bRet = false;
}
}
return bRet;
}
void ConfigItem::SetModified()
{
m_bIsModified = true;
}
void ConfigItem::ClearModified()
{
m_bIsModified = false;
}
Reference< XHierarchicalNameAccess> ConfigItem::GetTree()
{
Reference< XHierarchicalNameAccess> xRet;
if (utl::ConfigManager::IsAvoidConfig())
return xRet;
if(!m_xHierarchyAccess.is())
xRet = ConfigManager::acquireTree(*this);
else
xRet = m_xHierarchyAccess;
return xRet;
}
void ConfigItem::Commit()
{
ImplCommit();
ClearModified();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|