summaryrefslogtreecommitdiff
path: root/framework/source/application/login.cxx
blob: ebaae7982189f533b5b182d1625cc87f78c67c98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_framework.hxx"

//_________________________________________________________________________________________________________________
//	my own includes
//_________________________________________________________________________________________________________________

#include <services/logindialog.hxx>
#include <classes/servicemanager.hxx>
#include <macros/generic.hxx>
#include <macros/debug.hxx>
#include <services.h>

//_________________________________________________________________________________________________________________
//	interface includes
//_________________________________________________________________________________________________________________
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/awt/XDialog.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>

//_________________________________________________________________________________________________________________
//	other includes
//_________________________________________________________________________________________________________________
#include <comphelper/processfactory.hxx>
#include <com/sun/star/uno/Reference.hxx>
#include <osl/process.h>
#include <rtl/ustring.hxx>
#include <rtl/ustrbuf.hxx>
#include <vcl/event.hxx>
#include <vcl/svapp.hxx>
#include <vcl/wrkwin.hxx>
#include <vcl/msgbox.hxx>

#include <stdio.h>

//_________________________________________________________________________________________________________________
//	const
//_________________________________________________________________________________________________________________

#define	TEMPFILE_ENCODING			RTL_TEXTENCODING_UTF8			// encoding of written temp. ascii file
#define	LOGIN_RDB					DECLARE_ASCII("login.rdb")		// name of our own registry file - neccessary to create own servicemanager
#define SEPERATOR                   "\n"                            // used to seperate parts in temp. file

#define MINARGUMENTCOUNT            1                               // count of min. required arguments
#define ARGUMENTFOUND               0                               // OUString::compareTo returns 0 if searched string match given one
#define ARGUMENTLENGTH              3                               // length of fixed part of any argument to detect it easier!

#define ARGUMENT_TEMPFILE           DECLARE_ASCII("-f=")            // we support "-f=c:\temp\test.txt" to write dialog data in temp. file
#define ARGUMENT_DIALOGPARENT       DECLARE_ASCII("-p=")            // we support "-p=36748322" as window handle of parent for vcl dialog

//_________________________________________________________________________________________________________________
//	namespace
//_________________________________________________________________________________________________________________

using namespace ::rtl						;
using namespace ::comphelper				;
using namespace ::framework					;
using namespace ::com::sun::star::uno		;
using namespace ::com::sun::star::lang		;
using namespace ::com::sun::star::awt		;
using namespace ::com::sun::star::beans		;

//_________________________________________________________________________________________________________________
//	defines
//_________________________________________________________________________________________________________________

//_________________________________________________________________________________________________________________
//	declarations
//_________________________________________________________________________________________________________________

/*-************************************************************************************************************//**
    @short		implement command application to show login dialog and save his information in temp. file!
    @descr		We need this temp. file to share informations between our dialog and different processes, which
                can't use vcl directly. Caller of this executable give us the file name as an argument - we save
                all informations in it - caller can read it and MUST delete temp. file.
                This is neccessary for example; to hide the password!

    @implements	-

    @base		Application
*//*-*************************************************************************************************************/
class LoginApplication : public Application
{
    //*************************************************************************************************************
    //	public methods
    //*************************************************************************************************************
    public:
        void Main();

    //*************************************************************************************************************
    //	private methods
    //*************************************************************************************************************
    private:
        void impl_parseCommandline();		// search supported arguments on command line

    //*************************************************************************************************************
    //	private variables
    //*************************************************************************************************************
    private:
        OString     m_sTempFile     ;   // name of temp. file in system notation
        sal_Int32   m_nParentHandle ;   // a parent window handle for used vcl dialog

};	//	class LoginApplication

//_________________________________________________________________________________________________________________
//	global variables
//_________________________________________________________________________________________________________________

LoginApplication	gLoginApplication;

//_________________________________________________________________________________________________________________
//	main
//_________________________________________________________________________________________________________________

void LoginApplication::Main()
{
    // Init global uno servicemanager.
    ServiceManager aManager;
    Reference< XMultiServiceFactory > xServiceManager = aManager.getSharedUNOServiceManager( DECLARE_ASCII("login.rdb") );
    LOG_ASSERT( !(xServiceManager.is()==sal_False), "LoginApplication::Main()\nCould not create uno service manager!\n" )

    // Parse command line and set found arguments on application member.
    impl_parseCommandline();
    LOG_ASSERT( !(m_sTempFile.getLength()<1), "LoginApplication::Main()\nWrong or missing argument for temp. file detected!\n" )

    // Try to get neccessary dialog service.
    // By the way - cast it to interface XPropertySet too - we need it later.
    // (define SERVICENAME... comes from defines.hxx!)
    Reference< XDialog >		xLoginDialog( xServiceManager->createInstance( SERVICENAME_LOGINDIALOG ), UNO_QUERY );
    Reference< XPropertySet >	xPropertySet( xLoginDialog												, UNO_QUERY );

    // Work with valid ressources only!
    // Otherwise do nothing ...
    if	(
            ( xLoginDialog.is()			==	sal_True	)	&&
            ( xPropertySet.is()			==	sal_True	)	&&
            ( m_sTempFile.getLength()   >   0           )
        )
    {
        // Exist a parent window? YES => set right property.
        if( m_nParentHandle != 0 )
        {
            Any aParentWindow;
            aParentWindow <<= m_nParentHandle;
            xPropertySet->setPropertyValue( PROPERTYNAME_PARENTWINDOW, aParentWindow );
        }

        Any aConnectionType;
        aConnectionType <<= PROPERTYNAME_HTTPS;
        xPropertySet->setPropertyValue( PROPERTYNAME_CONNECTIONTYPE, aConnectionType );

        // Show login dialog and get decision of user.
        sal_Bool bDecision = (sal_Bool)(xLoginDialog->execute());

        OUString	sUserName		;
        OUString	sPassword		;
        OUString	sServer  		;
        OUString	sConnectionType	;
        sal_Int32	nPort=0			;	// We need this default if follow "if"-statement "failed"!

        // If user say "OK" ... get values from dialog.
        // If user say "NO" ... leave it. Then we save empty informations later ...
        if( bDecision == sal_True )
        {
            // defines PROPERTYNAME... comes from logindialog.hxx!
            xPropertySet->getPropertyValue( PROPERTYNAME_USERNAME		) >>= sUserName 		;
            xPropertySet->getPropertyValue( PROPERTYNAME_PASSWORD		) >>= sPassword 		;
            xPropertySet->getPropertyValue( PROPERTYNAME_SERVER			) >>= sServer   		;
            xPropertySet->getPropertyValue( PROPERTYNAME_CONNECTIONTYPE	) >>= sConnectionType	;
            if( sConnectionType.getLength() > 0 )
            {
                xPropertySet->getPropertyValue( sConnectionType ) >>= nPort;
            }
        }

        // Build string for output.
        // At this point it doesnt matter if information exist or not!
        // Format of output: "<decision>    [0|1]       SEPERATOR
        //                    <username>    [string]    SEPERATOR
        //                    <password>    [string]    SEPERATOR
        //                    <servername>  [string]    SEPERATOR
        //                    <port>        [int]       SEPERATOR"
        OUStringBuffer sBuffer( 1000 );

        if( bDecision == sal_True )
        {
            sBuffer.appendAscii( "1" );
        }
        else
        {
            sBuffer.appendAscii( "0" );
        }
        sBuffer.appendAscii ( SEPERATOR         );
        sBuffer.append		( sUserName			);
        sBuffer.appendAscii ( SEPERATOR         );
        sBuffer.append		( sPassword			);
        sBuffer.appendAscii ( SEPERATOR         );
        sBuffer.append		( sServer			);
        sBuffer.appendAscii ( SEPERATOR         );
        sBuffer.append		( sConnectionType	);
        sBuffer.appendAscii ( SEPERATOR         );
        sBuffer.append		( nPort				);
        sBuffer.appendAscii ( SEPERATOR         );

        // Write informations in temp. file.
        // If given file name isnt valid ... caller will have a problem!!!
        // If fil already exist (That's out of specification!!!) we overwrite it everytime.
        FILE* pFile = fopen( m_sTempFile.getStr(), "w" );
        LOG_ASSERT( !(pFile==NULL), "LoginApplication::Main()\nCould not open file!\n" );
        if( pFile != NULL )
        {
            OString sEncodedOut = U2B_ENC( sBuffer.makeStringAndClear(), TEMPFILE_ENCODING );
            fprintf( pFile, sEncodedOut.getStr()	);
            fclose ( pFile							);
        }
    }
}

//*****************************************************************************************************************
//	private method
//*****************************************************************************************************************
void LoginApplication::impl_parseCommandline()
{
    // Step over all arguments, search for supported ones and try to get his values.
    // Set it on our member. Caller of this method must control setted values.

    sal_uInt32	nCount = osl_getCommandArgCount();
    sal_uInt32	nArgument	=	0							;
    OUString	sArgument									;
    OUString    sValue                                      ;

    // Warn programmer if argument count isnt ok!
    LOG_ASSERT( !(nCount!=MINARGUMENTCOUNT), "LoginApplication::impl_parseCommandline()\nWrong argument count detected!\n" )

    // Reset all possible argument variables to defaults if someone is missing.
    m_sTempFile     = OString();
    m_nParentHandle = 0        ;

    // Step over all arguments ...
    for( nArgument=0; nArgument<nCount; ++nArgument )
    {
        // .. but work with valid ones only!
        // Don't check values here. Caller of this method must decide between wrong and allowed values!
        osl_getCommandArg( nArgument, &sArgument.pData );

        //_____________________________________________________________________________________________________
        // Look for "-f=<temp. file name>"
        if( sArgument.compareTo( ARGUMENT_TEMPFILE, ARGUMENTLENGTH ) == ARGUMENTFOUND )
        {
            sValue      = sArgument.copy( ARGUMENTLENGTH );
            m_sTempFile = U2B(sValue);
        }
        else
        //_____________________________________________________________________________________________________
        // Look for "-p=<parent window handle>"
        if( sArgument.compareTo( ARGUMENT_DIALOGPARENT, ARGUMENTLENGTH ) == ARGUMENTFOUND )
        {
            sValue          = sArgument.copy( ARGUMENTLENGTH );
            m_nParentHandle = sValue.toInt32();
        }
    }

    // Parent window handle is an optional argument ... but should be used mostly!
    // Warn programmer.
    LOG_ASSERT( !(m_nParentHandle==0), "Login.exe\nYou should give me a parent window handle!\n" )
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */