update dev300-m58
[ooovba.git] / framework / source / application / login.cxx
blob951e48f5d6ccac6b65df608f1f787ddba5c263db
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: login.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 //_________________________________________________________________________________________________________________
35 // my own includes
36 //_________________________________________________________________________________________________________________
38 #ifndef __FRAMEWORK_SERVICES_LOGINDIALOG_HXX_
39 #include <services/logindialog.hxx>
40 #endif
41 #include <classes/servicemanager.hxx>
42 #include <macros/generic.hxx>
43 #include <macros/debug.hxx>
44 #include <services.h>
46 //_________________________________________________________________________________________________________________
47 // interface includes
48 //_________________________________________________________________________________________________________________
49 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
50 #include <com/sun/star/awt/XDialog.hpp>
51 #include <com/sun/star/beans/XPropertySet.hpp>
53 //_________________________________________________________________________________________________________________
54 // other includes
55 //_________________________________________________________________________________________________________________
56 #include <comphelper/processfactory.hxx>
57 #include <com/sun/star/uno/Reference.hxx>
58 #include <vos/process.hxx>
59 #include <rtl/ustring.hxx>
60 #include <rtl/ustrbuf.hxx>
61 #include <vcl/event.hxx>
62 #include <vcl/svapp.hxx>
63 #include <vcl/wrkwin.hxx>
64 #include <vcl/msgbox.hxx>
66 #include <stdio.h>
68 //_________________________________________________________________________________________________________________
69 // const
70 //_________________________________________________________________________________________________________________
72 #define TEMPFILE_ENCODING RTL_TEXTENCODING_UTF8 // encoding of written temp. ascii file
73 #define LOGIN_RDB DECLARE_ASCII("login.rdb") // name of our own registry file - neccessary to create own servicemanager
74 #define SEPERATOR "\n" // used to seperate parts in temp. file
76 #define MINARGUMENTCOUNT 1 // count of min. required arguments
77 #define ARGUMENTFOUND 0 // OUString::compareTo returns 0 if searched string match given one
78 #define ARGUMENTLENGTH 3 // length of fixed part of any argument to detect it easier!
80 #define ARGUMENT_TEMPFILE DECLARE_ASCII("-f=") // we support "-f=c:\temp\test.txt" to write dialog data in temp. file
81 #define ARGUMENT_DIALOGPARENT DECLARE_ASCII("-p=") // we support "-p=36748322" as window handle of parent for vcl dialog
83 //_________________________________________________________________________________________________________________
84 // namespace
85 //_________________________________________________________________________________________________________________
87 using namespace ::rtl ;
88 using namespace ::vos ;
89 using namespace ::comphelper ;
90 using namespace ::framework ;
91 using namespace ::com::sun::star::uno ;
92 using namespace ::com::sun::star::lang ;
93 using namespace ::com::sun::star::awt ;
94 using namespace ::com::sun::star::beans ;
96 //_________________________________________________________________________________________________________________
97 // defines
98 //_________________________________________________________________________________________________________________
100 //_________________________________________________________________________________________________________________
101 // declarations
102 //_________________________________________________________________________________________________________________
104 /*-************************************************************************************************************//**
105 @short implement command application to show login dialog and save his information in temp. file!
106 @descr We need this temp. file to share informations between our dialog and different processes, which
107 can't use vcl directly. Caller of this executable give us the file name as an argument - we save
108 all informations in it - caller can read it and MUST delete temp. file.
109 This is neccessary for example; to hide the password!
111 @implements -
113 @base Application
114 *//*-*************************************************************************************************************/
115 class LoginApplication : public Application
117 //*************************************************************************************************************
118 // public methods
119 //*************************************************************************************************************
120 public:
121 void Main();
123 //*************************************************************************************************************
124 // private methods
125 //*************************************************************************************************************
126 private:
127 void impl_parseCommandline(); // search supported arguments on command line
129 //*************************************************************************************************************
130 // private variables
131 //*************************************************************************************************************
132 private:
133 OString m_sTempFile ; // name of temp. file in system notation
134 sal_Int32 m_nParentHandle ; // a parent window handle for used vcl dialog
136 }; // class LoginApplication
138 //_________________________________________________________________________________________________________________
139 // global variables
140 //_________________________________________________________________________________________________________________
142 LoginApplication gLoginApplication;
144 //_________________________________________________________________________________________________________________
145 // main
146 //_________________________________________________________________________________________________________________
148 void LoginApplication::Main()
150 // Init global uno servicemanager.
151 ServiceManager aManager;
152 Reference< XMultiServiceFactory > xServiceManager = aManager.getSharedUNOServiceManager( DECLARE_ASCII("login.rdb") );
153 LOG_ASSERT( !(xServiceManager.is()==sal_False), "LoginApplication::Main()\nCould not create uno service manager!\n" )
155 // Parse command line and set found arguments on application member.
156 impl_parseCommandline();
157 LOG_ASSERT( !(m_sTempFile.getLength()<1), "LoginApplication::Main()\nWrong or missing argument for temp. file detected!\n" )
159 // Try to get neccessary dialog service.
160 // By the way - cast it to interface XPropertySet too - we need it later.
161 // (define SERVICENAME... comes from defines.hxx!)
162 Reference< XDialog > xLoginDialog( xServiceManager->createInstance( SERVICENAME_LOGINDIALOG ), UNO_QUERY );
163 Reference< XPropertySet > xPropertySet( xLoginDialog , UNO_QUERY );
165 // Work with valid ressources only!
166 // Otherwise do nothing ...
167 if (
168 ( xLoginDialog.is() == sal_True ) &&
169 ( xPropertySet.is() == sal_True ) &&
170 ( m_sTempFile.getLength() > 0 )
173 // Exist a parent window? YES => set right property.
174 if( m_nParentHandle != 0 )
176 Any aParentWindow;
177 aParentWindow <<= m_nParentHandle;
178 xPropertySet->setPropertyValue( PROPERTYNAME_PARENTWINDOW, aParentWindow );
181 Any aConnectionType;
182 aConnectionType <<= PROPERTYNAME_HTTPS;
183 xPropertySet->setPropertyValue( PROPERTYNAME_CONNECTIONTYPE, aConnectionType );
185 // Show login dialog and get decision of user.
186 sal_Bool bDecision = (sal_Bool)(xLoginDialog->execute());
188 OUString sUserName ;
189 OUString sPassword ;
190 OUString sServer ;
191 OUString sConnectionType ;
192 sal_Int32 nPort=0 ; // We need this default if follow "if"-statement "failed"!
194 // If user say "OK" ... get values from dialog.
195 // If user say "NO" ... leave it. Then we save empty informations later ...
196 if( bDecision == sal_True )
198 // defines PROPERTYNAME... comes from logindialog.hxx!
199 xPropertySet->getPropertyValue( PROPERTYNAME_USERNAME ) >>= sUserName ;
200 xPropertySet->getPropertyValue( PROPERTYNAME_PASSWORD ) >>= sPassword ;
201 xPropertySet->getPropertyValue( PROPERTYNAME_SERVER ) >>= sServer ;
202 xPropertySet->getPropertyValue( PROPERTYNAME_CONNECTIONTYPE ) >>= sConnectionType ;
203 if( sConnectionType.getLength() > 0 )
205 xPropertySet->getPropertyValue( sConnectionType ) >>= nPort;
209 // Build string for output.
210 // At this point it doesnt matter if information exist or not!
211 // Format of output: "<decision> [0|1] SEPERATOR
212 // <username> [string] SEPERATOR
213 // <password> [string] SEPERATOR
214 // <servername> [string] SEPERATOR
215 // <port> [int] SEPERATOR"
216 OUStringBuffer sBuffer( 1000 );
218 if( bDecision == sal_True )
220 sBuffer.appendAscii( "1" );
222 else
224 sBuffer.appendAscii( "0" );
226 sBuffer.appendAscii ( SEPERATOR );
227 sBuffer.append ( sUserName );
228 sBuffer.appendAscii ( SEPERATOR );
229 sBuffer.append ( sPassword );
230 sBuffer.appendAscii ( SEPERATOR );
231 sBuffer.append ( sServer );
232 sBuffer.appendAscii ( SEPERATOR );
233 sBuffer.append ( sConnectionType );
234 sBuffer.appendAscii ( SEPERATOR );
235 sBuffer.append ( nPort );
236 sBuffer.appendAscii ( SEPERATOR );
238 // Write informations in temp. file.
239 // If given file name isnt valid ... caller will have a problem!!!
240 // If fil already exist (That's out of specification!!!) we overwrite it everytime.
241 FILE* pFile = fopen( m_sTempFile.getStr(), "w" );
242 LOG_ASSERT( !(pFile==NULL), "LoginApplication::Main()\nCould not open file!\n" );
243 if( pFile != NULL )
245 OString sEncodedOut = U2B_ENC( sBuffer.makeStringAndClear(), TEMPFILE_ENCODING );
246 fprintf( pFile, sEncodedOut.getStr() );
247 fclose ( pFile );
252 //*****************************************************************************************************************
253 // private method
254 //*****************************************************************************************************************
255 void LoginApplication::impl_parseCommandline()
257 // Use vos::OStartupInfo for access to command line.
258 // Step over all arguments, search for supported ones and try to get his values.
259 // Set it on our member. Caller of this method must control setted values.
260 OStartupInfo aInfo;
262 sal_uInt32 nCount = aInfo.getCommandArgCount() ;
263 sal_uInt32 nArgument = 0 ;
264 OUString sArgument ;
265 OUString sValue ;
267 // Warn programmer if argument count isnt ok!
268 LOG_ASSERT( !(nCount!=MINARGUMENTCOUNT), "LoginApplication::impl_parseCommandline()\nWrong argument count detected!\n" )
270 // Reset all possible argument variables to defaults if someone is missing.
271 m_sTempFile = OString();
272 m_nParentHandle = 0 ;
274 // Step over all arguments ...
275 for( nArgument=0; nArgument<nCount; ++nArgument )
277 // .. but work with valid ones only!
278 // Don't check values here. Caller of this method must decide between wrong and allowed values!
279 aInfo.getCommandArg( nArgument, sArgument );
281 //_____________________________________________________________________________________________________
282 // Look for "-f=<temp. file name>"
283 if( sArgument.compareTo( ARGUMENT_TEMPFILE, ARGUMENTLENGTH ) == ARGUMENTFOUND )
285 sValue = sArgument.copy( ARGUMENTLENGTH );
286 m_sTempFile = U2B(sValue);
288 else
289 //_____________________________________________________________________________________________________
290 // Look for "-p=<parent window handle>"
291 if( sArgument.compareTo( ARGUMENT_DIALOGPARENT, ARGUMENTLENGTH ) == ARGUMENTFOUND )
293 sValue = sArgument.copy( ARGUMENTLENGTH );
294 m_nParentHandle = sValue.toInt32();
298 // Parent window handle is an optional argument ... but should be used mostly!
299 // Warn programmer.
300 LOG_ASSERT( !(m_nParentHandle==0), "Login.exe\nYou should give me a parent window handle!\n" )