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