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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_tools.hxx"
30 
31 #include "tools/testtoolloader.hxx"
32 #include <osl/module.h>
33 #include <rtl/logfile.hxx>
34 #include <vos/process.hxx>
35 #include "tools/solar.h"
36 #include "tools/string.hxx"
37 #include "tools/debug.hxx"
38 
39 #include <comphelper/uieventslogger.hxx>
40 
41 using namespace rtl;
42 
43 namespace tools
44 {
45     typedef void ( *pfunc_CreateRemoteControl)();
46     typedef void ( *pfunc_DestroyRemoteControl)();
47 
48     typedef void ( *pfunc_CreateEventLogger)();
49     typedef void ( *pfunc_DestroyEventLogger)();
50 
51 static oslModule	aTestToolModule	= 0;
52 // are we to be automated at all?
53 static bool bAutomate = false;
54 static bool bLoggerStarted = false;
55 
56 
57 sal_uInt32 GetCommandLineParamCount()
58 {
59 	vos:: OStartupInfo  aStartInfo;
60 	return aStartInfo.getCommandArgCount();
61 }
62 
63 String GetCommandLineParam( sal_uInt32 nParam )
64 {
65 	vos:: OStartupInfo  aStartInfo;
66 	::rtl::OUString aParam;
67 	vos:: OStartupInfo ::TStartupError eError = aStartInfo.getCommandArg( nParam, aParam );
68 	if ( eError == vos:: OStartupInfo ::E_None )
69 		return String( aParam );
70 	else
71 	{
72 		DBG_ERROR( "Unable to get CommandLineParam" );
73 		return String();
74 	}
75 }
76 
77 extern "C" { static void SAL_CALL thisModule() {} }
78 
79 void LoadLib()
80 {
81 	if ( !aTestToolModule )
82 	{
83 		aTestToolModule = osl_loadModuleRelative(
84 			&thisModule,
85 			rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SVLIBRARY("sts"))).pData,
86 			SAL_LOADMODULE_GLOBAL );
87 	}
88 }
89 
90 void InitTestToolLib()
91 {
92 	RTL_LOGFILE_CONTEXT( aLog, "desktop (cd100003) ::InitTestToolLib" );
93 
94     sal_uInt32 i;
95 
96     for ( i = 0 ; i < GetCommandLineParamCount() ; i++ )
97 	{
98 		if ( GetCommandLineParam( i ).EqualsIgnoreCaseAscii("/enableautomation")
99 			|| GetCommandLineParam( i ).EqualsIgnoreCaseAscii("-enableautomation"))
100         {
101             bAutomate = true;
102             break;
103         }
104     }
105 
106     if ( bAutomate )
107 	{
108 		OUString	aFuncName( RTL_CONSTASCII_USTRINGPARAM( "CreateRemoteControl" ));
109 
110 		LoadLib();
111 		if ( aTestToolModule )
112 		{
113 			oslGenericFunction pInitFunc = osl_getFunctionSymbol(
114 				aTestToolModule, aFuncName.pData );
115 			if ( pInitFunc )
116 				(reinterpret_cast< pfunc_CreateRemoteControl >(pInitFunc))();
117 			else
118 			{
119 				DBG_ERROR1( "Unable to get Symbol 'CreateRemoteControl' from library %s while loading testtool support.", SVLIBRARY( "sts" ) );
120 			}
121 		}
122 		else
123 		{
124 			DBG_ERROR1( "Unable to access library %s while loading testtool support.", SVLIBRARY( "sts" ) );
125 		}
126 	}
127 
128 	if ( ::comphelper::UiEventsLogger::isEnabled() )
129 	{
130 		OUString	aFuncName( RTL_CONSTASCII_USTRINGPARAM( "CreateEventLogger" ));
131 
132 		LoadLib();
133 		if ( aTestToolModule )
134 		{
135 			oslGenericFunction pInitFunc = osl_getFunctionSymbol(
136 				aTestToolModule, aFuncName.pData );
137 			if ( pInitFunc )
138             {
139 				(reinterpret_cast< pfunc_CreateEventLogger >(pInitFunc))();
140                 bLoggerStarted = sal_True;
141             }
142 			else
143 			{
144 				DBG_ERROR1( "Unable to get Symbol 'CreateEventLogger' from library %s while loading testtool support.", SVLIBRARY( "sts" ) );
145 			}
146 		}
147 		else
148 		{
149 			DBG_ERROR1( "Unable to access library %s while loading testtool support.", SVLIBRARY( "sts" ) );
150 		}
151 	}
152 }
153 
154 void DeInitTestToolLib()
155 {
156 	if ( aTestToolModule )
157 	{
158 		if ( bAutomate )
159 		{
160 			OUString	aFuncName( RTL_CONSTASCII_USTRINGPARAM( "DestroyRemoteControl" ));
161 
162 			oslGenericFunction pDeInitFunc = osl_getFunctionSymbol(
163 				aTestToolModule, aFuncName.pData );
164 			if ( pDeInitFunc )
165 				(reinterpret_cast< pfunc_DestroyRemoteControl >(pDeInitFunc))();
166 		}
167 
168 		if ( bLoggerStarted /*::comphelper::UiEventsLogger::isEnabled()*/ )
169 		{
170 			OUString	aFuncName( RTL_CONSTASCII_USTRINGPARAM( "DestroyEventLogger" ));
171 
172 			oslGenericFunction pDeInitFunc = osl_getFunctionSymbol(
173 				aTestToolModule, aFuncName.pData );
174 			if ( pDeInitFunc )
175             {
176 				(reinterpret_cast< pfunc_DestroyEventLogger >(pDeInitFunc))();
177                 bLoggerStarted = sal_False;
178             }
179 		}
180 
181 		osl_unloadModule( aTestToolModule );
182 	}
183 }
184 
185 } // namespace tools
186