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 // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_framework.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
32*cdf0e10cSrcweir //	my own includes
33*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #ifndef __FRAMEWORK_SERVICES_LOGINDIALOG_HXX_
36*cdf0e10cSrcweir #include <services/logindialog.hxx>
37*cdf0e10cSrcweir #endif
38*cdf0e10cSrcweir #include <classes/servicemanager.hxx>
39*cdf0e10cSrcweir #include <macros/generic.hxx>
40*cdf0e10cSrcweir #include <macros/debug.hxx>
41*cdf0e10cSrcweir #include <services.h>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
44*cdf0e10cSrcweir //	interface includes
45*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
46*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/awt/XDialog.hpp>
48*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
51*cdf0e10cSrcweir //	other includes
52*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
53*cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
54*cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
55*cdf0e10cSrcweir #include <vos/process.hxx>
56*cdf0e10cSrcweir #include <rtl/ustring.hxx>
57*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
58*cdf0e10cSrcweir #include <vcl/event.hxx>
59*cdf0e10cSrcweir #include <vcl/svapp.hxx>
60*cdf0e10cSrcweir #include <vcl/wrkwin.hxx>
61*cdf0e10cSrcweir #include <vcl/msgbox.hxx>
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir #include <stdio.h>
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
66*cdf0e10cSrcweir //	const
67*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir #define	TEMPFILE_ENCODING			RTL_TEXTENCODING_UTF8			// encoding of written temp. ascii file
70*cdf0e10cSrcweir #define	LOGIN_RDB					DECLARE_ASCII("login.rdb")		// name of our own registry file - neccessary to create own servicemanager
71*cdf0e10cSrcweir #define SEPERATOR                   "\n"                            // used to seperate parts in temp. file
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir #define MINARGUMENTCOUNT            1                               // count of min. required arguments
74*cdf0e10cSrcweir #define ARGUMENTFOUND               0                               // OUString::compareTo returns 0 if searched string match given one
75*cdf0e10cSrcweir #define ARGUMENTLENGTH              3                               // length of fixed part of any argument to detect it easier!
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir #define ARGUMENT_TEMPFILE           DECLARE_ASCII("-f=")            // we support "-f=c:\temp\test.txt" to write dialog data in temp. file
78*cdf0e10cSrcweir #define ARGUMENT_DIALOGPARENT       DECLARE_ASCII("-p=")            // we support "-p=36748322" as window handle of parent for vcl dialog
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
81*cdf0e10cSrcweir //	namespace
82*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir using namespace ::rtl						;
85*cdf0e10cSrcweir using namespace ::vos						;
86*cdf0e10cSrcweir using namespace ::comphelper				;
87*cdf0e10cSrcweir using namespace ::framework					;
88*cdf0e10cSrcweir using namespace ::com::sun::star::uno		;
89*cdf0e10cSrcweir using namespace ::com::sun::star::lang		;
90*cdf0e10cSrcweir using namespace ::com::sun::star::awt		;
91*cdf0e10cSrcweir using namespace ::com::sun::star::beans		;
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
94*cdf0e10cSrcweir //	defines
95*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
98*cdf0e10cSrcweir //	declarations
99*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir /*-************************************************************************************************************//**
102*cdf0e10cSrcweir 	@short		implement command application to show login dialog and save his information in temp. file!
103*cdf0e10cSrcweir 	@descr		We need this temp. file to share informations between our dialog and different processes, which
104*cdf0e10cSrcweir 				can't use vcl directly. Caller of this executable give us the file name as an argument - we save
105*cdf0e10cSrcweir 				all informations in it - caller can read it and MUST delete temp. file.
106*cdf0e10cSrcweir 				This is neccessary for example; to hide the password!
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 	@implements	-
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 	@base		Application
111*cdf0e10cSrcweir *//*-*************************************************************************************************************/
112*cdf0e10cSrcweir class LoginApplication : public Application
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir 	//*************************************************************************************************************
115*cdf0e10cSrcweir 	//	public methods
116*cdf0e10cSrcweir 	//*************************************************************************************************************
117*cdf0e10cSrcweir 	public:
118*cdf0e10cSrcweir 		void Main();
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 	//*************************************************************************************************************
121*cdf0e10cSrcweir 	//	private methods
122*cdf0e10cSrcweir 	//*************************************************************************************************************
123*cdf0e10cSrcweir 	private:
124*cdf0e10cSrcweir 		void impl_parseCommandline();		// search supported arguments on command line
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	//*************************************************************************************************************
127*cdf0e10cSrcweir 	//	private variables
128*cdf0e10cSrcweir 	//*************************************************************************************************************
129*cdf0e10cSrcweir 	private:
130*cdf0e10cSrcweir         OString     m_sTempFile     ;   // name of temp. file in system notation
131*cdf0e10cSrcweir         sal_Int32   m_nParentHandle ;   // a parent window handle for used vcl dialog
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir };	//	class LoginApplication
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
136*cdf0e10cSrcweir //	global variables
137*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir LoginApplication	gLoginApplication;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
142*cdf0e10cSrcweir //	main
143*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir void LoginApplication::Main()
146*cdf0e10cSrcweir {
147*cdf0e10cSrcweir 	// Init global uno servicemanager.
148*cdf0e10cSrcweir 	ServiceManager aManager;
149*cdf0e10cSrcweir 	Reference< XMultiServiceFactory > xServiceManager = aManager.getSharedUNOServiceManager( DECLARE_ASCII("login.rdb") );
150*cdf0e10cSrcweir 	LOG_ASSERT( !(xServiceManager.is()==sal_False), "LoginApplication::Main()\nCould not create uno service manager!\n" )
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	// Parse command line and set found arguments on application member.
153*cdf0e10cSrcweir 	impl_parseCommandline();
154*cdf0e10cSrcweir 	LOG_ASSERT( !(m_sTempFile.getLength()<1), "LoginApplication::Main()\nWrong or missing argument for temp. file detected!\n" )
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 	// Try to get neccessary dialog service.
157*cdf0e10cSrcweir 	// By the way - cast it to interface XPropertySet too - we need it later.
158*cdf0e10cSrcweir 	// (define SERVICENAME... comes from defines.hxx!)
159*cdf0e10cSrcweir 	Reference< XDialog >		xLoginDialog( xServiceManager->createInstance( SERVICENAME_LOGINDIALOG ), UNO_QUERY );
160*cdf0e10cSrcweir 	Reference< XPropertySet >	xPropertySet( xLoginDialog												, UNO_QUERY );
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 	// Work with valid ressources only!
163*cdf0e10cSrcweir 	// Otherwise do nothing ...
164*cdf0e10cSrcweir 	if	(
165*cdf0e10cSrcweir 			( xLoginDialog.is()			==	sal_True	)	&&
166*cdf0e10cSrcweir 			( xPropertySet.is()			==	sal_True	)	&&
167*cdf0e10cSrcweir             ( m_sTempFile.getLength()   >   0           )
168*cdf0e10cSrcweir 		)
169*cdf0e10cSrcweir 	{
170*cdf0e10cSrcweir         // Exist a parent window? YES => set right property.
171*cdf0e10cSrcweir         if( m_nParentHandle != 0 )
172*cdf0e10cSrcweir         {
173*cdf0e10cSrcweir             Any aParentWindow;
174*cdf0e10cSrcweir             aParentWindow <<= m_nParentHandle;
175*cdf0e10cSrcweir             xPropertySet->setPropertyValue( PROPERTYNAME_PARENTWINDOW, aParentWindow );
176*cdf0e10cSrcweir         }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir 		Any aConnectionType;
179*cdf0e10cSrcweir 		aConnectionType <<= PROPERTYNAME_HTTPS;
180*cdf0e10cSrcweir 		xPropertySet->setPropertyValue( PROPERTYNAME_CONNECTIONTYPE, aConnectionType );
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 		// Show login dialog and get decision of user.
183*cdf0e10cSrcweir 		sal_Bool bDecision = (sal_Bool)(xLoginDialog->execute());
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 		OUString	sUserName		;
186*cdf0e10cSrcweir 		OUString	sPassword		;
187*cdf0e10cSrcweir 		OUString	sServer  		;
188*cdf0e10cSrcweir 		OUString	sConnectionType	;
189*cdf0e10cSrcweir 		sal_Int32	nPort=0			;	// We need this default if follow "if"-statement "failed"!
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir 		// If user say "OK" ... get values from dialog.
192*cdf0e10cSrcweir 		// If user say "NO" ... leave it. Then we save empty informations later ...
193*cdf0e10cSrcweir 		if( bDecision == sal_True )
194*cdf0e10cSrcweir 		{
195*cdf0e10cSrcweir 			// defines PROPERTYNAME... comes from logindialog.hxx!
196*cdf0e10cSrcweir 			xPropertySet->getPropertyValue( PROPERTYNAME_USERNAME		) >>= sUserName 		;
197*cdf0e10cSrcweir 			xPropertySet->getPropertyValue( PROPERTYNAME_PASSWORD		) >>= sPassword 		;
198*cdf0e10cSrcweir 			xPropertySet->getPropertyValue( PROPERTYNAME_SERVER			) >>= sServer   		;
199*cdf0e10cSrcweir 			xPropertySet->getPropertyValue( PROPERTYNAME_CONNECTIONTYPE	) >>= sConnectionType	;
200*cdf0e10cSrcweir 			if( sConnectionType.getLength() > 0 )
201*cdf0e10cSrcweir 			{
202*cdf0e10cSrcweir 				xPropertySet->getPropertyValue( sConnectionType ) >>= nPort;
203*cdf0e10cSrcweir 			}
204*cdf0e10cSrcweir 		}
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 		// Build string for output.
207*cdf0e10cSrcweir 		// At this point it doesnt matter if information exist or not!
208*cdf0e10cSrcweir         // Format of output: "<decision>    [0|1]       SEPERATOR
209*cdf0e10cSrcweir         //                    <username>    [string]    SEPERATOR
210*cdf0e10cSrcweir         //                    <password>    [string]    SEPERATOR
211*cdf0e10cSrcweir         //                    <servername>  [string]    SEPERATOR
212*cdf0e10cSrcweir         //                    <port>        [int]       SEPERATOR"
213*cdf0e10cSrcweir 		OUStringBuffer sBuffer( 1000 );
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 		if( bDecision == sal_True )
216*cdf0e10cSrcweir 		{
217*cdf0e10cSrcweir 			sBuffer.appendAscii( "1" );
218*cdf0e10cSrcweir 		}
219*cdf0e10cSrcweir 		else
220*cdf0e10cSrcweir 		{
221*cdf0e10cSrcweir 			sBuffer.appendAscii( "0" );
222*cdf0e10cSrcweir 		}
223*cdf0e10cSrcweir         sBuffer.appendAscii ( SEPERATOR         );
224*cdf0e10cSrcweir 		sBuffer.append		( sUserName			);
225*cdf0e10cSrcweir         sBuffer.appendAscii ( SEPERATOR         );
226*cdf0e10cSrcweir 		sBuffer.append		( sPassword			);
227*cdf0e10cSrcweir         sBuffer.appendAscii ( SEPERATOR         );
228*cdf0e10cSrcweir 		sBuffer.append		( sServer			);
229*cdf0e10cSrcweir         sBuffer.appendAscii ( SEPERATOR         );
230*cdf0e10cSrcweir 		sBuffer.append		( sConnectionType	);
231*cdf0e10cSrcweir         sBuffer.appendAscii ( SEPERATOR         );
232*cdf0e10cSrcweir 		sBuffer.append		( nPort				);
233*cdf0e10cSrcweir         sBuffer.appendAscii ( SEPERATOR         );
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 		// Write informations in temp. file.
236*cdf0e10cSrcweir 		// If given file name isnt valid ... caller will have a problem!!!
237*cdf0e10cSrcweir 		// If fil already exist (That's out of specification!!!) we overwrite it everytime.
238*cdf0e10cSrcweir 		FILE* pFile = fopen( m_sTempFile.getStr(), "w" );
239*cdf0e10cSrcweir 		LOG_ASSERT( !(pFile==NULL), "LoginApplication::Main()\nCould not open file!\n" );
240*cdf0e10cSrcweir 		if( pFile != NULL )
241*cdf0e10cSrcweir 		{
242*cdf0e10cSrcweir 			OString sEncodedOut = U2B_ENC( sBuffer.makeStringAndClear(), TEMPFILE_ENCODING );
243*cdf0e10cSrcweir 			fprintf( pFile, sEncodedOut.getStr()	);
244*cdf0e10cSrcweir 			fclose ( pFile							);
245*cdf0e10cSrcweir 		}
246*cdf0e10cSrcweir 	}
247*cdf0e10cSrcweir }
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir //*****************************************************************************************************************
250*cdf0e10cSrcweir //	private method
251*cdf0e10cSrcweir //*****************************************************************************************************************
252*cdf0e10cSrcweir void LoginApplication::impl_parseCommandline()
253*cdf0e10cSrcweir {
254*cdf0e10cSrcweir 	// Use vos::OStartupInfo for access to command line.
255*cdf0e10cSrcweir 	// Step over all arguments, search for supported ones and try to get his values.
256*cdf0e10cSrcweir 	// Set it on our member. Caller of this method must control setted values.
257*cdf0e10cSrcweir 	OStartupInfo aInfo;
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 	sal_uInt32	nCount		=	aInfo.getCommandArgCount()	;
260*cdf0e10cSrcweir 	sal_uInt32	nArgument	=	0							;
261*cdf0e10cSrcweir 	OUString	sArgument									;
262*cdf0e10cSrcweir     OUString    sValue                                      ;
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir 	// Warn programmer if argument count isnt ok!
265*cdf0e10cSrcweir     LOG_ASSERT( !(nCount!=MINARGUMENTCOUNT), "LoginApplication::impl_parseCommandline()\nWrong argument count detected!\n" )
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir     // Reset all possible argument variables to defaults if someone is missing.
268*cdf0e10cSrcweir     m_sTempFile     = OString();
269*cdf0e10cSrcweir     m_nParentHandle = 0        ;
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir 	// Step over all arguments ...
272*cdf0e10cSrcweir 	for( nArgument=0; nArgument<nCount; ++nArgument )
273*cdf0e10cSrcweir 	{
274*cdf0e10cSrcweir 		// .. but work with valid ones only!
275*cdf0e10cSrcweir 		// Don't check values here. Caller of this method must decide between wrong and allowed values!
276*cdf0e10cSrcweir 		aInfo.getCommandArg( nArgument, sArgument );
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 		//_____________________________________________________________________________________________________
279*cdf0e10cSrcweir         // Look for "-f=<temp. file name>"
280*cdf0e10cSrcweir         if( sArgument.compareTo( ARGUMENT_TEMPFILE, ARGUMENTLENGTH ) == ARGUMENTFOUND )
281*cdf0e10cSrcweir 		{
282*cdf0e10cSrcweir             sValue      = sArgument.copy( ARGUMENTLENGTH );
283*cdf0e10cSrcweir             m_sTempFile = U2B(sValue);
284*cdf0e10cSrcweir 		}
285*cdf0e10cSrcweir         else
286*cdf0e10cSrcweir 		//_____________________________________________________________________________________________________
287*cdf0e10cSrcweir         // Look for "-p=<parent window handle>"
288*cdf0e10cSrcweir         if( sArgument.compareTo( ARGUMENT_DIALOGPARENT, ARGUMENTLENGTH ) == ARGUMENTFOUND )
289*cdf0e10cSrcweir 		{
290*cdf0e10cSrcweir             sValue          = sArgument.copy( ARGUMENTLENGTH );
291*cdf0e10cSrcweir             m_nParentHandle = sValue.toInt32();
292*cdf0e10cSrcweir 		}
293*cdf0e10cSrcweir 	}
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir     // Parent window handle is an optional argument ... but should be used mostly!
296*cdf0e10cSrcweir     // Warn programmer.
297*cdf0e10cSrcweir     LOG_ASSERT( !(m_nParentHandle==0), "Login.exe\nYou should give me a parent window handle!\n" )
298*cdf0e10cSrcweir }
299