1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cfgupdate.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
33 #define _PRIVATE_TEST_
38 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
39 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40 #include <com/sun/star/uno/Type.hxx>
41 #include <com/sun/star/uno/TypeClass.hpp>
43 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
44 #include <com/sun/star/container/XNameAccess.hpp>
45 #include <com/sun/star/container/XNameContainer.hpp>
46 #include <com/sun/star/container/XHierarchicalName.hpp>
47 #include <com/sun/star/container/XNamed.hpp>
48 #include <com/sun/star/container/XNameReplace.hpp>
49 #include <com/sun/star/container/XChild.hpp>
50 #include <com/sun/star/beans/XExactName.hpp>
51 #include <com/sun/star/util/XChangesBatch.hpp>
52 #include <com/sun/star/beans/XHierarchicalPropertySet.hpp>
55 #include <rtl/ustring.hxx>
56 #include <rtl/string.hxx>
58 #include <cppuhelper/servicefactory.hxx>
59 #include <com/sun/star/uno/Sequence.h>
60 #include <com/sun/star/uno/Any.h>
62 #include "createpropertyvalue.hxx"
64 #include "typeconverter.hxx"
66 // #include <com/sun/star/configuration/XConfigurationSync.hpp>
68 using namespace ::com::sun::star::uno
;
69 using namespace ::com::sun::star::lang
;
70 using namespace ::com::sun::star::container
;
71 using namespace ::com::sun::star::beans
;
72 //using namespace ::com::sun::star::util;
73 using namespace ::com::sun::star::util
;
75 using ::rtl::OUString
;
77 //using namespace ::configmgr;
79 using namespace ::cppu
;
81 #define ASCII(x) ::rtl::OUString::createFromAscii(x)
83 ostream
& operator << (ostream
& out
, rtl::OUString
const& aStr
)
85 sal_Unicode
const* const pStr
= aStr
.getStr();
86 sal_Unicode
const* const pEnd
= pStr
+ aStr
.getLength();
87 for (sal_Unicode
const* p
= pStr
; p
< pEnd
; ++p
)
88 if (0 < *p
&& *p
< 127) // ASCII
91 out
<< "[\\u" << hex
<< *p
<< "]";
95 void showSequence(const Sequence
<OUString
> &aSeq
)
98 const OUString
*pStr
= aSeq
.getConstArray();
99 for (int i
=0;i
<aSeq
.getLength();i
++)
101 OUString aStr
= pStr
[i
];
102 // aArray += aStr + ASCII(", ");
103 cout
<< aStr
<< endl
;
105 volatile int dummy
= 0;
108 //=============================================================================
109 //=============================================================================
110 void test_read_access(Reference
< XInterface
>& xIface
, Reference
< XMultiServiceFactory
> &xMSF
);
111 //=============================================================================
112 struct prompt_and_wait
115 prompt_and_wait(char const* text
= "") : myText(text
) {}
118 cout
<< myText
<< ">" << endl
;
119 int const mx
= int( (+0u - +1u) >> 1);
122 if (cin
.get(c
) && c
!= '\n')
126 static prompt_and_wait
exit_prompt("Quitting\nQ");
129 // -----------------------------------------------------------------------------
130 Sequence
<Any
> createSequence(const OUString
&sUser
, const OUString
&sPasswd
)
132 Sequence
< Any
> aCPArgs
;
134 if (sUser
.getLength() > 0)
137 aCPArgs
[0] <<= configmgr::createPropertyValue(ASCII("user"), sUser
);
139 if (sPasswd
.getLength() > 0)
142 aCPArgs
[1] <<= configmgr::createPropertyValue(ASCII("password"), sPasswd
);
147 //=============================================================================
149 #if (defined UNX) || (defined OS2)
154 OString
input(const char* pDefaultText
, char cEcho
)
156 // PRE: a Default Text would be shown, cEcho is a Value which will show if a key is pressed.
157 const int MAX_INPUT_LEN
= 500;
158 char aBuffer
[MAX_INPUT_LEN
];
160 strcpy(aBuffer
, pDefaultText
);
161 int nLen
= strlen(aBuffer
);
179 aBuffer
[nLen
] = '\0';
189 if (nLen
< MAX_INPUT_LEN
)
200 aBuffer
[nLen
++] = ch
;
201 aBuffer
[nLen
] = '\0';
211 if (!cin
.getline(aBuffer
,sizeof aBuffer
))
214 return OString(aBuffer
);
217 // -----------------------------------------------------------------------------
218 rtl::OUString
enterValue(const char* _aStr
, const char* _aDefault
, bool _bIsAPassword
)
222 OString aTxt
= input(_aDefault
, _bIsAPassword
? '*' : 0);
224 OUString sValue
= OUString::createFromAscii(aTxt
);
228 // -----------------------------------------------------------------------------
229 Reference
< XNameAccess
> beginChanges(Reference
< XMultiServiceFactory
> xFactory
, OUString sPath
)
231 Sequence
< Any
> aArgs(1);
232 aArgs
[0] <<= configmgr::createPropertyValue(ASCII("nodepath"),sPath
);
234 cout
<< "starting update for node:" << sPath
<< endl
;
236 Reference
< XNameAccess
> xTree(xFactory
->createInstanceWithArguments(OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
242 template <class Type
>
243 // -----------------------------------------------------------------------------
244 void update(Reference
< XInterface
> xIFace
, OUString sRelPath
, Type sValue
)
246 Reference
< XHierarchicalPropertySet
> xTree(xIFace
, UNO_QUERY
);
250 cout
<< "updating node:" << sRelPath
<< endl
;
251 xTree
->setHierarchicalPropertyValue(sRelPath
, aValue
);
254 // -----------------------------------------------------------------------------
255 Reference
< XHierarchicalPropertySet
> insertTree(Reference
< XInterface
> xIFace
, OUString aName
)
257 if (!aName
.getLength())
258 aName
= enterValue("/nEnter a Tree to insert: ", "", false);
260 Reference
< XSingleServiceFactory
> xFactory(xIFace
, UNO_QUERY
);
261 Reference
< XHierarchicalPropertySet
> xNewElement(xFactory
->createInstance(), UNO_QUERY
);
263 cout
<< "inserting new tree element:" << aName
<< endl
;
266 aTree
<<= xNewElement
;
267 Reference
< XNameContainer
>(xFactory
, UNO_QUERY
)->insertByName(aName
, aTree
);
272 // -----------------------------------------------------------------------------
273 void removeTree(Reference
< XInterface
> xIFace
, OUString aName
)
275 if (!aName
.getLength())
276 aName
= enterValue("/nEnter a Tree to remove: ", "", false);
278 cout
<< "removing new tree element:" << aName
<< endl
;
280 Reference
< XNameContainer
>(xIFace
, UNO_QUERY
)->removeByName(aName
);
283 // -----------------------------------------------------------------------------
284 void commitChanges(Reference
< XInterface
> xIFace
)
286 cout
<< "committing changes:" << endl
;
288 Reference
< XChangesBatch
> xChangesBatch(xIFace
, UNO_QUERY
);
289 xChangesBatch
->commitChanges();
292 // -----------------------------------------------------------------------------
293 void displayTree(Reference
< XNameAccess
> xIFace
, sal_Int32 nLevel
)
295 const char* pTab
= " ";
296 Sequence
<OUString
> aNames( xIFace
->getElementNames() );
297 for (int i
= 0; i
< aNames
.getLength(); ++i
)
299 Any aElement
= xIFace
->getByName(aNames
[i
]);
300 Reference
< XNameAccess
> xAccess
;
301 Reference
< XSingleServiceFactory
> xFactory
;
302 aElement
>>= xFactory
;
303 aElement
>>= xAccess
;
306 for (int j
= 0; j
< nLevel
; j
++)
313 sType
= OUString::createFromAscii(" type = 'set' " );
315 cout
<< "<" << aNames
[i
] << sType
<< ">";
316 displayTree(xAccess
, nLevel
+ 1);
317 cout
<< endl
<< "</" << aNames
[i
] << ">";
320 cout
<< "<" << aNames
[i
] << "/>";
326 // -----------------------------------------------------------------------------
327 // ---------------------------------- M A I N ----------------------------------
328 // -----------------------------------------------------------------------------
330 #if (defined UNX) || (defined OS2)
331 int main( int argc
, char * argv
[] )
333 int _cdecl
main( int argc
, char * argv
[] )
337 aTimeout
.Seconds
= 5;
338 aTimeout
.Nanosec
= 0;
340 // cout << " Please insert Text: ";
342 // OString aTxt = input("Der Text", 0);
343 // cout << endl << "You inserted: " << aTxt.getStr() << endl;
345 // cout << "Please insert Password: ";
347 // OString aPasswd = input("", '*');
348 // cout << endl << "You inserted: " << aPasswd.getStr() << endl;
352 OUString
const sServiceRegistry
= OUString::createFromAscii( argc
> 1 ? argv
[1] : "applicat.rdb" );
353 Reference
< XMultiServiceFactory
> xORB
= createRegistryServiceFactory(
360 cerr
<< "Could not create the service factory !\n\n";
363 cout
<< "Service factory created !\n---------------------------------------------------------------" << endl
;
365 Sequence
< Any
> aCPArgs
;
367 /*OUString sServerType = enterValue("Enter Servertype: ", "remote", false);
371 rtl::OUString sFilePath
;
375 sUser
= enterValue("Enter User: ", "user1", false);
378 OUString sPasswd
;// = enterValue("Enter Password: ", "", true);
381 aCPArgs
= createSequence(sUser
, sPasswd
);
383 Reference
< XMultiServiceFactory
> xCfgProvider(
384 xORB
->createInstanceWithArguments(
385 ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider"),
388 if (!xCfgProvider
.is())
391 cerr
<< "Could not create the configuration provider !\n\n";
395 Reference
< XNameAccess
> xUpdateAccess
;
397 // now do updates for the user
398 xUpdateAccess
= beginChanges(xCfgProvider
, OUString::createFromAscii("org.openoffice.Security"));
399 displayTree(xUpdateAccess
, 0);
401 update(xUpdateAccess
, OUString::createFromAscii("_3D_Engine/Dithering"), sal_Bool(sal_False
));
403 /* xUpdateAccess = beginChanges(xCfgProvider, OUString::createFromAscii("org.openoffice.Inet"));
405 update(xUpdateAccess, OUString::createFromAscii("Proxy/FTP/Port"), sal_Int32(11));
406 update(xUpdateAccess, OUString::createFromAscii("Proxy/FTP/Name"), OUString::createFromAscii("Test3"));
407 update(xUpdateAccess, OUString::createFromAscii("DNS/IP_Address"), OUString::createFromAscii("Test4"));
409 commitChanges(xUpdateAccess
);
411 // now do updates with inserting and removing of nodes
413 xUpdateAccess
= beginChanges(xCfgProvider
, OUString::createFromAscii("org.openoffice.Security/MountPoints"));
414 displayTree(xUpdateAccess
, 0);
416 Reference
< XHierarchicalPropertySet
> xTree
= insertTree(xUpdateAccess
, OUString());
417 update(xUpdateAccess
, OUString::createFromAscii("InstallationDirectory/Directory"), OUString::createFromAscii("Test1"));
418 removeTree(xUpdateAccess
, OUString());
419 commitChanges(xUpdateAccess
);
424 cerr
<< "Caught exception: " << e
.Message
<< endl
;
430 cerr << "BUG: Caught UNKNOWN exception (?) " << endl;