Update ooo320-m1
[ooovba.git] / configmgr / workben / apitest / cfgadmin.cxx
blob0be8d861a0a5b6ff18e8dabe38a8f9f575379baf
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cfgadmin.cxx,v $
10 * $Revision: 1.9 $
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_
35 #include <iostream>
36 using namespace std;
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"
64 #include <osl/time.h>
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;
76 using ::rtl::OString;
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
89 out << char(*p);
90 else
91 out << "[\\u" << hex << *p << "]";
92 return out;
95 void showSequence(const Sequence<OUString> &aSeq)
97 OUString aArray;
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
114 char const* myText;
115 prompt_and_wait(char const* text = "") : myText(text) {}
116 ~prompt_and_wait()
118 cout << myText << ">" << endl;
119 int const mx = int( (+0u - +1u) >> 1);
121 char c=0;
122 if (cin.get(c) && c != '\n')
123 cin.ignore(mx,'\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)
136 aCPArgs.realloc(1);
137 aCPArgs[0] <<= configmgr::createPropertyValue(ASCII("user"), sUser);
139 if (sPasswd.getLength() > 0)
141 aCPArgs.realloc(2);
142 aCPArgs[1] <<= configmgr::createPropertyValue(ASCII("password"), sPasswd);
144 return aCPArgs;
147 //=============================================================================
148 #include <string.h>
149 #if (defined UNX) || (defined OS2)
150 #else
151 #include <conio.h>
152 #endif
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);
163 #ifdef WNT
164 char ch = '\0';
166 cout << aBuffer;
167 cout.flush();
169 while(ch != 13)
171 ch = getch();
172 if (ch == 8)
174 if (nLen > 0)
176 cout << "\b \b";
177 cout.flush();
178 --nLen;
179 aBuffer[nLen] = '\0';
181 else
183 cout << "\a";
184 cout.flush();
187 else if (ch != 13)
189 if (nLen < MAX_INPUT_LEN)
191 if (cEcho == 0)
193 cout << ch;
195 else
197 cout << cEcho;
199 cout.flush();
200 aBuffer[nLen++] = ch;
201 aBuffer[nLen] = '\0';
203 else
205 cout << "\a";
206 cout.flush();
210 #else
211 if (!cin.getline(aBuffer,sizeof aBuffer))
212 return OString();
213 #endif
214 return OString(aBuffer);
217 // -----------------------------------------------------------------------------
218 rtl::OUString enterValue(const char* _aStr, const char* _aDefault, bool _bIsAPassword)
220 cout << _aStr;
221 cout.flush();
222 OString aTxt = input(_aDefault, _bIsAPassword ? '*' : 0);
224 OUString sValue = OUString::createFromAscii(aTxt);
225 return sValue;
228 ///////////////////////////////////////////////////////////////////////////////////////////
229 void write(Reference<XNameAccess >& xAccess)
231 if (xAccess.is())
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];
238 cout << endl;
240 else
241 cout << "BUG: XNameAccess not available";
242 cout << endl;
244 // -----------------------------------------------------------------------------
245 void write(Reference< XChild >& xChild)
247 if (xChild.is())
248 cout << "\n[ P ] -\tParent";
249 else
250 cout << "BUG: Parent not available (no XChild)";
251 cout << endl;
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;
266 write(xGroupAccess);
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;
278 write(xNewGroup);
279 Any aGroup;
280 aGroup <<= xNewGroup;
281 Reference< XNameContainer >(xGroupAccess, UNO_QUERY)->insertByName(sGroup, aGroup);
282 return sGroup;
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;
298 write(xUserAccess);
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;
312 write(xNewUser);
314 Any aValue;
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);
321 Any aUser;
322 aUser <<= xNewUser;
323 Reference< XNameContainer >(xUserAccess, UNO_QUERY)->insertByName(sUser, aUser);
325 return sUser;
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"),
350 aArgs), UNO_QUERY);
352 return xTree;
355 template <class Type>
356 // -----------------------------------------------------------------------------
357 void update(Reference< XInterface > xIFace, OUString sRelPath, Type sValue)
359 Reference< XHierarchicalPropertySet > xTree(xIFace, UNO_QUERY);
360 Any aValue;
361 aValue <<= sValue;
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;
378 Any aTree;
379 aTree <<= xNewElement;
380 Reference< XNameContainer >(xFactory, UNO_QUERY)->insertByName(aName, aTree);
382 return xNewElement;
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[] )
411 #else
412 int _cdecl main( int argc, char * argv[] )
413 #endif
415 TimeValue aTimeout;
416 aTimeout.Seconds = 5;
417 aTimeout.Nanosec = 0;
419 // cout << " Please insert Text: ";
420 // cout.flush();
421 // OString aTxt = input("Der Text", 0);
422 // cout << endl << "You inserted: " << aTxt.getStr() << endl;
424 // cout << "Please insert Password: ";
425 // cout.flush();
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(
433 sServiceRegistry,
434 ::rtl::OUString()
436 if (!xORB.is())
438 ::flush(cout);
439 cerr << "Could not create the service factory !\n\n";
440 return 1;
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"),
449 aCPArgs),
450 UNO_QUERY);
452 if (!xCfgProvider.is())
454 ::flush(cout);
455 cerr << "Could not create the configuration provider !\n\n";
456 return 3;
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);
468 // create a group
469 OUString sGroupName = insertGroup(xGroupAccess);
470 // create a user
471 OUString sUserName;
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);
502 catch (Exception& e)
504 ::flush(cout);
505 cerr << "Caught exception: " << e.Message << endl;
506 return 1;
509 catch (...)
511 flush(cout);
512 cerr << "BUG: Caught UNKNOWN exception (?) " << endl;
515 return 0;