1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include <stdio.h>
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "sal/main.h"
31*cdf0e10cSrcweir #include <osl/diagnose.h>
32*cdf0e10cSrcweir #include <osl/file.h>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <com/sun/star/registry/XSimpleRegistry.hpp>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using namespace ::rtl;
42*cdf0e10cSrcweir using namespace ::com::sun::star;
43*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir static void print_options() SAL_THROW( () )
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir     printf(
48*cdf0e10cSrcweir         "\nusage: regsingleton [-r|-ra] registry_file singleton_name[=service_name] ...\n\n"
49*cdf0e10cSrcweir         "Inserts a singleton entry into rdb.\n"
50*cdf0e10cSrcweir         "Option -r revokes given entries, -ra revokes all entries.\n" );
51*cdf0e10cSrcweir }
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir //==================================================================================================
54*cdf0e10cSrcweir SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir     if (argc < 3)
57*cdf0e10cSrcweir     {
58*cdf0e10cSrcweir         print_options();
59*cdf0e10cSrcweir         return 1;
60*cdf0e10cSrcweir     }
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     bool insert_entry = true;
63*cdf0e10cSrcweir     bool remove_all = false;
64*cdf0e10cSrcweir     int nPos = 1;
65*cdf0e10cSrcweir     if ('-' == argv[ nPos ][ 0 ] && 'r' == argv[ nPos ][ 1 ])
66*cdf0e10cSrcweir     {
67*cdf0e10cSrcweir         if ('a' == argv[ nPos ][ 2 ] && '\0' == argv[ nPos ][ 3 ])
68*cdf0e10cSrcweir         {
69*cdf0e10cSrcweir             remove_all = true;
70*cdf0e10cSrcweir         }
71*cdf0e10cSrcweir         else if ('\0' != argv[ nPos ][ 2 ])
72*cdf0e10cSrcweir         {
73*cdf0e10cSrcweir             print_options();
74*cdf0e10cSrcweir             return 1;
75*cdf0e10cSrcweir         }
76*cdf0e10cSrcweir         insert_entry = false;
77*cdf0e10cSrcweir         ++nPos;
78*cdf0e10cSrcweir     }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     OUString sys_path( OUString::createFromAscii( argv[ nPos ] ) );
81*cdf0e10cSrcweir     OUString file_url;
82*cdf0e10cSrcweir     oslFileError rc = osl_getFileURLFromSystemPath( sys_path.pData, &file_url.pData );
83*cdf0e10cSrcweir     if (osl_File_E_None != rc)
84*cdf0e10cSrcweir     {
85*cdf0e10cSrcweir         fprintf( stderr, "\nerror: cannot make file url out of %s\n", argv[ nPos ] );
86*cdf0e10cSrcweir         return 1;
87*cdf0e10cSrcweir     }
88*cdf0e10cSrcweir     ++nPos;
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 	try
91*cdf0e10cSrcweir 	{
92*cdf0e10cSrcweir         Reference< registry::XSimpleRegistry > xSimReg( ::cppu::createSimpleRegistry() );
93*cdf0e10cSrcweir         xSimReg->open( file_url, sal_False, sal_True );
94*cdf0e10cSrcweir         Reference< registry::XRegistryKey > xRoot( xSimReg->getRootKey() );
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir         if (remove_all)
97*cdf0e10cSrcweir         {
98*cdf0e10cSrcweir             try
99*cdf0e10cSrcweir             {
100*cdf0e10cSrcweir                 xRoot->deleteKey( OUSTR("SINGLETONS") );
101*cdf0e10cSrcweir             }
102*cdf0e10cSrcweir             catch (registry::InvalidRegistryException & exc)
103*cdf0e10cSrcweir             {
104*cdf0e10cSrcweir                 OString cstr_msg(
105*cdf0e10cSrcweir                     OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
106*cdf0e10cSrcweir                 fprintf(
107*cdf0e10cSrcweir                     stderr, "\nwarning: removing all singletons failed: %s\n",
108*cdf0e10cSrcweir                     cstr_msg.getStr() );
109*cdf0e10cSrcweir             }
110*cdf0e10cSrcweir         }
111*cdf0e10cSrcweir         else
112*cdf0e10cSrcweir         {
113*cdf0e10cSrcweir             Reference< registry::XRegistryKey > xKey( xRoot->openKey( OUSTR("SINGLETONS") ) );
114*cdf0e10cSrcweir             if (! xKey.is())
115*cdf0e10cSrcweir                 xKey = xRoot->createKey( OUSTR("SINGLETONS") );
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir             for ( ; nPos < argc; ++nPos )
118*cdf0e10cSrcweir             {
119*cdf0e10cSrcweir                 OUString singleton( OUString::createFromAscii( argv[ nPos ] ) );
120*cdf0e10cSrcweir                 OUString service;
121*cdf0e10cSrcweir                 sal_Int32 eq = singleton.indexOf( '=' );
122*cdf0e10cSrcweir                 if (eq >= 0)
123*cdf0e10cSrcweir                 {
124*cdf0e10cSrcweir                     service = singleton.copy( eq +1 );
125*cdf0e10cSrcweir                     singleton = singleton.copy( 0, eq );
126*cdf0e10cSrcweir                 }
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir                 if (insert_entry)
129*cdf0e10cSrcweir                 {
130*cdf0e10cSrcweir                     if (service.getLength())
131*cdf0e10cSrcweir                     {
132*cdf0e10cSrcweir                         Reference< registry::XRegistryKey > xEntry( xKey->openKey( singleton ) );
133*cdf0e10cSrcweir                         if (! xEntry.is())
134*cdf0e10cSrcweir                             xEntry = xKey->createKey( singleton );
135*cdf0e10cSrcweir                         xEntry->setStringValue( service );
136*cdf0e10cSrcweir                     }
137*cdf0e10cSrcweir                     else
138*cdf0e10cSrcweir                     {
139*cdf0e10cSrcweir                         OString entry( OUStringToOString( singleton, RTL_TEXTENCODING_ASCII_US ) );
140*cdf0e10cSrcweir                         fprintf(
141*cdf0e10cSrcweir                             stderr, "\nwarning: no service name given for singleton %s!\n",
142*cdf0e10cSrcweir                             entry.getStr() );
143*cdf0e10cSrcweir                     }
144*cdf0e10cSrcweir                 }
145*cdf0e10cSrcweir                 else
146*cdf0e10cSrcweir                 {
147*cdf0e10cSrcweir                     try
148*cdf0e10cSrcweir                     {
149*cdf0e10cSrcweir                         xKey->deleteKey( singleton );
150*cdf0e10cSrcweir                     }
151*cdf0e10cSrcweir                     catch (registry::InvalidRegistryException & exc)
152*cdf0e10cSrcweir                     {
153*cdf0e10cSrcweir                         OString cstr_singleton(
154*cdf0e10cSrcweir                             OUStringToOString( singleton, RTL_TEXTENCODING_ASCII_US ) );
155*cdf0e10cSrcweir                         OString cstr_msg(
156*cdf0e10cSrcweir                             OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
157*cdf0e10cSrcweir                         fprintf(
158*cdf0e10cSrcweir                             stderr, "\nwarning: singleton %s is not registered: %s\n",
159*cdf0e10cSrcweir                             cstr_singleton.getStr(), cstr_msg.getStr() );
160*cdf0e10cSrcweir                     }
161*cdf0e10cSrcweir                 }
162*cdf0e10cSrcweir             }
163*cdf0e10cSrcweir         }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir         return 0;
166*cdf0e10cSrcweir 	}
167*cdf0e10cSrcweir 	catch (Exception & rExc)
168*cdf0e10cSrcweir 	{
169*cdf0e10cSrcweir 		OString msg( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
170*cdf0e10cSrcweir         fprintf( stderr, "\nerror: %s\n", msg.getStr() );
171*cdf0e10cSrcweir         return 1;
172*cdf0e10cSrcweir 	}
173*cdf0e10cSrcweir }
174