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