Update ooo320-m1
[ooovba.git] / configmgr / workben / apitest / cfgupdate.cxx
blob54722c06179164a28520b784e0ec6aded65e7a02
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: cfgupdate.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>
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>
57 #include <osl/time.h>
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;
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 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"),
237 aArgs), UNO_QUERY);
239 return xTree;
242 template <class Type>
243 // -----------------------------------------------------------------------------
244 void update(Reference< XInterface > xIFace, OUString sRelPath, Type sValue)
246 Reference< XHierarchicalPropertySet > xTree(xIFace, UNO_QUERY);
247 Any aValue;
248 aValue <<= sValue;
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;
265 Any aTree;
266 aTree <<= xNewElement;
267 Reference< XNameContainer >(xFactory, UNO_QUERY)->insertByName(aName, aTree);
269 return xNewElement;
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;
305 cout << endl;
306 for (int j = 0; j < nLevel; j++)
307 cout << " ";
309 if (xAccess.is())
311 OUString sType;
312 if (xFactory.is())
313 sType = OUString::createFromAscii(" type = 'set' " );
315 cout << "<" << aNames[i] << sType << ">";
316 displayTree(xAccess, nLevel + 1);
317 cout << endl << "</" << aNames[i] << ">";
319 else
320 cout << "<" << aNames[i] << "/>";
323 ::flush(cout);
326 // -----------------------------------------------------------------------------
327 // ---------------------------------- M A I N ----------------------------------
328 // -----------------------------------------------------------------------------
330 #if (defined UNX) || (defined OS2)
331 int main( int argc, char * argv[] )
332 #else
333 int _cdecl main( int argc, char * argv[] )
334 #endif
336 TimeValue aTimeout;
337 aTimeout.Seconds = 5;
338 aTimeout.Nanosec = 0;
340 // cout << " Please insert Text: ";
341 // cout.flush();
342 // OString aTxt = input("Der Text", 0);
343 // cout << endl << "You inserted: " << aTxt.getStr() << endl;
345 // cout << "Please insert Password: ";
346 // cout.flush();
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(
354 sServiceRegistry,
355 ::rtl::OUString()
357 if (!xORB.is())
359 ::flush(cout);
360 cerr << "Could not create the service factory !\n\n";
361 return 1;
363 cout << "Service factory created !\n---------------------------------------------------------------" << endl;
365 Sequence< Any > aCPArgs;
367 /*OUString sServerType = enterValue("Enter Servertype: ", "remote", false);
368 cout << endl;*/
371 rtl::OUString sFilePath;
372 rtl::OUString sPort;
373 rtl::OUString sUser;
375 sUser = enterValue("Enter User: ", "user1", false);
376 cout << endl;
378 OUString sPasswd;// = enterValue("Enter Password: ", "", true);
379 cout << endl;
381 aCPArgs = createSequence(sUser, sPasswd);
383 Reference< XMultiServiceFactory > xCfgProvider(
384 xORB->createInstanceWithArguments(
385 ::rtl::OUString::createFromAscii("com.sun.star.configuration.ConfigurationProvider"),
386 aCPArgs),
387 UNO_QUERY);
388 if (!xCfgProvider.is())
390 ::flush(cout);
391 cerr << "Could not create the configuration provider !\n\n";
392 return 3;
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);
421 catch (Exception& e)
423 ::flush(cout);
424 cerr << "Caught exception: " << e.Message << endl;
427 catch (...)
429 ::flush(cout);
430 cerr << "BUG: Caught UNKNOWN exception (?) " << endl;
433 return 0;