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: cfgadmin.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>
42 #include <com/sun/star/beans/XHierarchicalPropertySet.hpp>
44 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
45 #include <com/sun/star/container/XNameAccess.hpp>
46 #include <com/sun/star/container/XNameContainer.hpp>
47 #include <com/sun/star/container/XHierarchicalName.hpp>
48 #include <com/sun/star/container/XNamed.hpp>
49 #include <com/sun/star/container/XNameReplace.hpp>
50 #include <com/sun/star/container/XChild.hpp>
51 #include <com/sun/star/beans/XExactName.hpp>
52 #include <com/sun/star/util/XChangesBatch.hpp>
55 #include <rtl/ustring.hxx>
56 #include <rtl/string.hxx>
57 #include <cppuhelper/servicefactory.hxx>
58 #include <com/sun/star/uno/Sequence.h>
59 #include <com/sun/star/uno/Any.h>
61 #include "createpropertyvalue.hxx"
63 #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 void write(Reference
<XNameAccess
>& xAccess
)
233 Sequence
<OUString
> aNames( xAccess
->getElementNames() );
235 cout
<< "Element Names: (" << aNames
.getLength() << ")";
236 for (int i
= 0; i
< aNames
.getLength(); ++i
)
237 cout
<< "\n[" << i
<< "] -\t" << aNames
[i
];
241 cout
<< "BUG: XNameAccess not available";
244 // -----------------------------------------------------------------------------
245 void write(Reference
< XChild
>& xChild
)
248 cout
<< "\n[ P ] -\tParent";
250 cout
<< "BUG: Parent not available (no XChild)";
254 // -----------------------------------------------------------------------------
255 void displayTree(Reference
< XNameAccess
> xIFace
){
257 write(Reference
< XNameAccess
>(xIFace
));
261 // -----------------------------------------------------------------------------
262 void displayGroups(Reference
< XNameAccess
> xGroupAccess
)
265 cout
<< "Currently available groups !\n---------------------------------------------------------------" << endl
;
269 // -----------------------------------------------------------------------------
270 OUString
insertGroup(Reference
< XNameAccess
> xGroupAccess
)
272 OUString sGroup
= enterValue(" Enter a new group to create: ", "", true);
274 Reference
< XSingleServiceFactory
> xFactory(xGroupAccess
, UNO_QUERY
);
275 Reference
< XNameAccess
> xNewGroup(xFactory
->createInstance(), UNO_QUERY
);
277 cout
<< "Group data: !\n---------------------------------------------------------------" << endl
;
280 aGroup
<<= xNewGroup
;
281 Reference
< XNameContainer
>(xGroupAccess
, UNO_QUERY
)->insertByName(sGroup
, aGroup
);
285 // -----------------------------------------------------------------------------
286 void deleteGroup(Reference
< XNameAccess
> xGroupAccess
, OUString sGroup
)
288 if (!sGroup
.getLength())
289 sGroup
= enterValue(" Enter a group to delete: ", "", true);
290 cout
<< "deleting group !\n---------------------------------------------------------------" << endl
;
291 Reference
< XNameContainer
>(xGroupAccess
, UNO_QUERY
)->removeByName(sGroup
);
294 // -----------------------------------------------------------------------------
295 void displayUsers(Reference
< XNameAccess
> xUserAccess
)
297 cout
<< "Currently available users !\n---------------------------------------------------------------" << endl
;
301 // -----------------------------------------------------------------------------
302 OUString
insertUser(Reference
< XNameAccess
> xUserAccess
, OUString aGroup
)
304 OUString sUser
= enterValue(" Enter a new User to create: ", "", true);
305 Reference
< XSingleServiceFactory
> xFactory(xUserAccess
, UNO_QUERY
);
307 Sequence
< Any
> aArgs(1);
308 aArgs
[0] <<= configmgr::createPropertyValue(ASCII("group"), aGroup
);
309 Reference
< XNameAccess
> xNewUser(xFactory
->createInstanceWithArguments(aArgs
), UNO_QUERY
);
311 cout
<< "User data: !\n---------------------------------------------------------------" << endl
;
315 aValue
<<= ASCII("MyCompany");
317 // now do some updates for the user
318 Reference
< XHierarchicalPropertySet
> xUpdate(xNewUser
, UNO_QUERY
);
319 xUpdate
->setHierarchicalPropertyValue(ASCII("Data/Company"),aValue
);
323 Reference
< XNameContainer
>(xUserAccess
, UNO_QUERY
)->insertByName(sUser
, aUser
);
328 // -----------------------------------------------------------------------------
329 void deleteUser(Reference
< XNameAccess
> xUserAccess
, OUString sUser
)
331 if (!sUser
.getLength())
332 sUser
= enterValue(" Enter a User to delete: ", "", true);
333 cout
<< "deleting User !\n---------------------------------------------------------------" << endl
;
334 Reference
< XNameContainer
>(xUserAccess
, UNO_QUERY
)->removeByName(sUser
);
337 // -----------------------------------------------------------------------------
338 Reference
< XNameAccess
> beginChanges(Reference
< XMultiServiceFactory
> xFactory
, OUString sPath
, OUString
& sUser
)
340 if (!sUser
.getLength())
341 sUser
= enterValue(" Enter a User: ", "", true);
343 Sequence
< Any
> aArgs(2);
344 aArgs
[0] <<= configmgr::createPropertyValue(ASCII("user"), sUser
);
345 aArgs
[1] <<= configmgr::createPropertyValue(ASCII("nodepath"),sPath
);
347 cout
<< "starting update for node:" << sPath
<< endl
;
349 Reference
< XNameAccess
> xTree(xFactory
->createInstanceWithArguments(OUString::createFromAscii("com.sun.star.configuration.ConfigurationUpdateAccess"),
355 template <class Type
>
356 // -----------------------------------------------------------------------------
357 void update(Reference
< XInterface
> xIFace
, OUString sRelPath
, Type sValue
)
359 Reference
< XHierarchicalPropertySet
> xTree(xIFace
, UNO_QUERY
);
363 cout
<< "updating node:" << sRelPath
<< endl
;
364 xTree
->setHierarchicalPropertyValue(sRelPath
, aValue
);
367 // -----------------------------------------------------------------------------
368 Reference
< XHierarchicalPropertySet
> insertTree(Reference
< XInterface
> xIFace
, OUString aName
)
370 if (!aName
.getLength())
371 aName
= enterValue("/nEnter a Tree to insert: ", "", false);
373 Reference
< XSingleServiceFactory
> xFactory(xIFace
, UNO_QUERY
);
374 Reference
< XHierarchicalPropertySet
> xNewElement(xFactory
->createInstance(), UNO_QUERY
);
376 cout
<< "inserting new tree element:" << aName
<< endl
;
379 aTree
<<= xNewElement
;
380 Reference
< XNameContainer
>(xFactory
, UNO_QUERY
)->insertByName(aName
, aTree
);
385 // -----------------------------------------------------------------------------
386 void removeTree(Reference
< XInterface
> xIFace
, OUString aName
)
388 if (!aName
.getLength())
389 aName
= enterValue("/nEnter a Tree to remove: ", "", false);
391 cout
<< "removing new tree element:" << aName
<< endl
;
393 Reference
< XNameContainer
>(xIFace
, UNO_QUERY
)->removeByName(aName
);
396 // -----------------------------------------------------------------------------
397 void commitChanges(Reference
< XInterface
> xIFace
)
399 cout
<< "committing changes:" << endl
;
401 Reference
< XChangesBatch
> xChangesBatch(xIFace
, UNO_QUERY
);
402 xChangesBatch
->commitChanges();
405 // -----------------------------------------------------------------------------
406 // ---------------------------------- M A I N ----------------------------------
407 // -----------------------------------------------------------------------------
409 #if (defined UNX) || (defined OS2)
410 int main( int argc
, char * argv
[] )
412 int _cdecl
main( int argc
, char * argv
[] )
416 aTimeout
.Seconds
= 5;
417 aTimeout
.Nanosec
= 0;
419 // cout << " Please insert Text: ";
421 // OString aTxt = input("Der Text", 0);
422 // cout << endl << "You inserted: " << aTxt.getStr() << endl;
424 // cout << "Please insert Password: ";
426 // OString aPasswd = input("", '*');
427 // cout << endl << "You inserted: " << aPasswd.getStr() << endl;
431 OUString
const sServiceRegistry
= OUString::createFromAscii( argc
> 1 ? argv
[1] : "applicat.rdb" );
432 Reference
< XMultiServiceFactory
> xORB
= createRegistryServiceFactory(
439 cerr
<< "Could not create the service factory !\n\n";
442 cout
<< "Service factory created !\n---------------------------------------------------------------" << endl
;
444 Sequence
< Any
> aCPArgs
= createSequence(OUString::createFromAscii("Administrator"), OUString());
446 Reference
< XMultiServiceFactory
> xCfgProvider(
447 xORB
->createInstanceWithArguments(
448 ::rtl::OUString::createFromAscii("com.sun.star.configuration.AdministrationProvider"),
452 if (!xCfgProvider
.is())
455 cerr
<< "Could not create the configuration provider !\n\n";
459 cout
<< "Configuration Provider created !\n---------------------------------------------------------------" << endl
;
461 Reference
< XNameAccess
> xUpdateAccess
;
462 Reference
< XNameAccess
> xGroupAccess(xCfgProvider
->createInstance(OUString::createFromAscii("com.sun.star.configuration.GroupAccess")),UNO_QUERY
);
463 Reference
< XNameAccess
> xUserAccess(xCfgProvider
->createInstance(OUString::createFromAscii("com.sun.star.configuration.UserAccess")),UNO_QUERY
);
465 displayGroups(xGroupAccess
);
466 displayUsers(xUserAccess
);
469 OUString sGroupName
= insertGroup(xGroupAccess
);
472 sUserName
= insertUser(xUserAccess
, sGroupName
);
474 // now do updates for the user
475 xUpdateAccess
= beginChanges(xCfgProvider
, OUString::createFromAscii("org.openoffice.Inet"), sUserName
);
477 update(xUpdateAccess
, OUString::createFromAscii("Proxy/FTP/Port"), sal_Int32(12));
478 update(xUpdateAccess
, OUString::createFromAscii("Proxy/FTP/Name"), OUString::createFromAscii("demo"));
479 update(xUpdateAccess
, OUString::createFromAscii("DNS/IP_Address"), OUString::createFromAscii("demo1"));
481 xUpdateAccess
= beginChanges(xCfgProvider
, OUString::createFromAscii("org.openoffice.Office.Common"), sUserName
);
482 update(xUpdateAccess
, OUString::createFromAscii("_3D_Engine/Dithering"), sal_Bool(sal_False
));
483 commitChanges(xUpdateAccess
);
485 // now do updates with inserting and removing of nodes
487 xUpdateAccess
= beginChanges(xCfgProvider
, OUString::createFromAscii("org.openoffice.Security/MountPoints"), sUserName
);
488 displayTree(xUpdateAccess
);
490 Reference
< XHierarchicalPropertySet
> xTree
= insertTree(xUpdateAccess
, OUString());
491 update(xUpdateAccess
, OUString::createFromAscii("InstallationDirectory/Directory"), OUString::createFromAscii("Test1"));
492 removeTree(xUpdateAccess
, OUString());
493 commitChanges(xUpdateAccess
);
496 /* deleteUser(xUserAccess, sUserName);
497 deleteGroup(xGroupAccess, sGroupName); */
499 displayGroups(xGroupAccess
);
500 displayUsers(xUserAccess
);
505 cerr
<< "Caught exception: " << e
.Message
<< endl
;
512 cerr << "BUG: Caught UNKNOWN exception (?) " << endl;