1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "MNSMozabProxy.hxx"
22 #include "resource/mozab_res.hrc"
23 #include "MDatabaseMetaDataHelper.hxx"
25 #include <osl/mutex.hxx>
26 #include <osl/conditn.hxx>
27 #include <osl/thread.hxx>
29 #include "pre_include_mozilla.h"
30 #include <nsIProxyObjectManager.h>
31 // More Mozilla includes for LDAP Connection Test
33 #include "nsILDAPURL.h"
34 #include "nsILDAPMessage.h"
35 #include "nsILDAPMessageListener.h"
36 #include "nsILDAPErrors.h"
37 #include "nsILDAPConnection.h"
38 #include "nsILDAPOperation.h"
39 #include "post_include_mozilla.h"
40 #include <MQueryHelper.hxx>
41 #include <com/sun/star/uno/Reference.hxx>
42 #include <comphelper/processfactory.hxx>
43 #include "com/sun/star/mozilla/XProxyRunner.hpp"
45 using namespace com::sun::star::uno
;
46 using namespace com::sun::star::lang
;
47 using namespace com::sun::star::mozilla
;
50 #define TYPEASSERT(value,type) if (value != type) return !NS_OK;
52 using namespace connectivity::mozab
;
54 /* Implementation file */
56 static ::osl::Mutex m_aThreadMutex
;
57 extern nsresult
NewAddressBook(const OUString
* aName
);
60 MNSMozabProxy::MNSMozabProxy()
63 #if OSL_DEBUG_LEVEL > 0
64 m_oThreadID
= osl::Thread::getCurrentIdentifier();
69 MNSMozabProxy::~MNSMozabProxy()
73 sal_Int32
MNSMozabProxy::StartProxy(RunArgs
* args
,::com::sun::star::mozilla::MozillaProductType aProduct
,const OUString
&aProfile
)
75 OSL_TRACE( "IN : MNSMozabProxy::StartProxy()" );
76 ::osl::MutexGuard
aGuard(m_aThreadMutex
);
82 Reference
<XMultiServiceFactory
> xFactory
= ::comphelper::getProcessServiceFactory();
83 OSL_ENSURE( xFactory
.is(), "can't get service factory" );
84 ::com::sun::star::uno::Reference
<XInterface
> xInstance
= xFactory
->createInstance("com.sun.star.mozilla.MozillaBootstrap");
85 OSL_ENSURE( xInstance
.is(), "failed to create instance" );
86 xRunner
= ::com::sun::star::uno::Reference
< ::com::sun::star::mozilla::XProxyRunner
>(xInstance
,UNO_QUERY
);
88 const ::com::sun::star::uno::Reference
< ::com::sun::star::mozilla::XCodeProxy
> aCode(this);
89 return xRunner
->Run(aCode
);
92 extern nsresult
getTableStringsProxied(const sal_Char
* sAbURI
, sal_Int32
*nDirectoryType
,MNameMapper
*nmap
,
93 ::std::vector
< OUString
>* _rStrings
,
94 ::std::vector
< OUString
>* _rTypes
,
95 sal_Int32
* pErrorId
);
97 ::com::sun::star::mozilla::MozillaProductType SAL_CALL
MNSMozabProxy::getProductType( ) throw (::com::sun::star::uno::RuntimeException
)
101 OUString SAL_CALL
MNSMozabProxy::getProfileName( ) throw (::com::sun::star::uno::RuntimeException
)
106 sal_Int32 SAL_CALL
MNSMozabProxy::run( ) throw (::com::sun::star::uno::RuntimeException
)
108 #if OSL_DEBUG_LEVEL > 0
109 OSL_TRACE( "IN : MNSMozabProxy::Run() Caller thread :%4d" , m_oThreadID
);
111 OSL_TRACE( "IN : MNSMozabProxy::Run()" );
113 nsresult rv
= NS_ERROR_INVALID_ARG
;
115 return NS_ERROR_INVALID_ARG
;
116 switch(m_Args
->funcIndex
)
118 case ProxiedFunc::FUNC_TESTLDAP_INIT_LDAP
:
119 case ProxiedFunc::FUNC_TESTLDAP_IS_LDAP_CONNECTED
:
120 case ProxiedFunc::FUNC_TESTLDAP_RELEASE_RESOURCE
:
121 rv
= testLDAPConnection();
123 case ProxiedFunc::FUNC_GET_TABLE_STRINGS
:
124 rv
= getTableStringsProxied((const sal_Char
*)m_Args
->arg1
,
125 (sal_Int32
*)m_Args
->arg2
,
126 (MNameMapper
*)m_Args
->arg3
,
127 (::std::vector
< OUString
>*)m_Args
->arg4
,
128 (::std::vector
< OUString
>*)m_Args
->arg5
,
129 (sal_Int32
*)m_Args
->arg6
);
131 case ProxiedFunc::FUNC_EXECUTE_QUERY
:
132 if (m_Args
->arg1
&& m_Args
->arg2
)
134 rv
= ((MQuery
*)m_Args
->arg1
)->executeQueryProxied((OConnection
*)m_Args
->arg2
);
137 case ProxiedFunc::FUNC_QUERYHELPER_CREATE_NEW_CARD
:
138 case ProxiedFunc::FUNC_QUERYHELPER_DELETE_CARD
:
139 case ProxiedFunc::FUNC_QUERYHELPER_COMMIT_CARD
:
140 case ProxiedFunc::FUNC_QUERYHELPER_RESYNC_CARD
:
143 rv
= QueryHelperStub();
146 case ProxiedFunc::FUNC_NEW_ADDRESS_BOOK
:
149 rv
= NewAddressBook((const OUString
*)m_Args
->arg1
);
153 return NS_ERROR_INVALID_ARG
;
158 nsresult
MNSMozabProxy::QueryHelperStub()
160 nsresult rv
= NS_ERROR_INVALID_ARG
;
161 MQueryHelper
* mHelper
=(MQueryHelper
*) m_Args
->arg1
;
162 switch(m_Args
->funcIndex
)
164 case ProxiedFunc::FUNC_QUERYHELPER_CREATE_NEW_CARD
:
165 if (m_Args
->arg2
) //m_Args->arg2 used to return cord number
167 *((sal_Int32
*)m_Args
->arg2
) = mHelper
->createNewCard();
171 case ProxiedFunc::FUNC_QUERYHELPER_DELETE_CARD
:
172 if (m_Args
->arg2
&& m_Args
->arg3
) //m_Args->arg2 used to get the cord number
174 rv
= mHelper
->deleteCard(*((sal_uInt32
*)m_Args
->arg2
),(nsIAbDirectory
*)m_Args
->arg3
);
177 case ProxiedFunc::FUNC_QUERYHELPER_COMMIT_CARD
:
178 if (m_Args
->arg2
&& m_Args
->arg3
) //m_Args->arg2 used to get the cord number
180 rv
= mHelper
->commitCard(*((sal_uInt32
*)m_Args
->arg2
),(nsIAbDirectory
*)m_Args
->arg3
);
183 case ProxiedFunc::FUNC_QUERYHELPER_RESYNC_CARD
:
184 if (m_Args
->arg2
) //m_Args->arg2 used to get the cord number
186 rv
= mHelper
->resyncRow(*((sal_uInt32
*)m_Args
->arg2
));
196 #define NS_LDAPCONNECTION_CONTRACTID "@mozilla.org/network/ldap-connection;1"
197 #define NS_LDAPOPERATION_CONTRACTID "@mozilla.org/network/ldap-operation;1"
198 #define NS_LDAPMESSAGE_CONTRACTID "@mozilla.org/network/ldap-message;1"
199 #define NS_LDAPURL_CONTRACTID "@mozilla.org/network/ldap-url;1"
201 namespace connectivity
{
203 class MLDAPMessageListener
: public nsILDAPMessageListener
206 NS_DECL_NSILDAPMESSAGELISTENER
208 MLDAPMessageListener();
209 virtual ~MLDAPMessageListener();
211 sal_Bool
initialized() const;
212 sal_Bool
goodConnection() const { return initialized() && m_GoodConnection
; }
216 ::osl::Mutex m_aMutex
;
217 ::osl::Condition m_aCondition
;
219 sal_Bool m_IsComplete
;
220 sal_Bool m_GoodConnection
;
222 void setConnectionStatus( sal_Bool _good
);
227 NS_IMPL_THREADSAFE_ISUPPORTS1(MLDAPMessageListener
, nsILDAPMessageListener
)
229 MLDAPMessageListener::MLDAPMessageListener()
231 , m_IsComplete( sal_False
)
232 , m_GoodConnection( sal_False
)
234 m_aCondition
.reset();
237 MLDAPMessageListener::~MLDAPMessageListener()
241 sal_Bool
MLDAPMessageListener::initialized() const
243 return const_cast< MLDAPMessageListener
* >( this )->m_aCondition
.check();
246 void MLDAPMessageListener::setConnectionStatus( sal_Bool _good
)
248 ::osl::MutexGuard
aGuard( m_aMutex
);
249 m_IsComplete
= sal_True
;
250 m_GoodConnection
= _good
;
255 NS_IMETHODIMP
MLDAPMessageListener::OnLDAPInit(nsILDAPConnection
* /*aConn*/, nsresult aStatus
)
257 setConnectionStatus( NS_SUCCEEDED( aStatus
) ? sal_True
: sal_False
);
261 NS_IMETHODIMP
MLDAPMessageListener::OnLDAPMessage( nsILDAPMessage
* aMessage
)
266 rv
= aMessage
->GetType(&messageType
);
267 NS_ENSURE_SUCCESS(rv
, rv
);
271 case nsILDAPMessage::RES_BIND
:
272 rv
= aMessage
->GetErrorCode(&errCode
);
273 // if the login failed
274 if (errCode
!= (PRInt32
)nsILDAPErrors::SUCCESS
) {
275 setConnectionStatus( sal_False
);
278 setConnectionStatus( sal_True
);
280 case nsILDAPMessage::RES_SEARCH_RESULT
:
281 setConnectionStatus( sal_True
);
293 MNSMozabProxy::testLDAPConnection( )
295 nsresult rv
=NS_ERROR_INVALID_ARG
;
296 switch(m_Args
->funcIndex
)
298 case ProxiedFunc::FUNC_TESTLDAP_INIT_LDAP
:
299 if (m_Args
->arg1
&& m_Args
->arg4
)
301 rv
= InitLDAP((sal_Char
*)m_Args
->arg1
,(sal_Unicode
*)m_Args
->arg2
,(sal_Unicode
*)m_Args
->arg3
,(sal_Bool
*)m_Args
->arg4
);
304 case ProxiedFunc::FUNC_TESTLDAP_IS_LDAP_CONNECTED
:
307 const MLDAPMessageListener
* pListener( static_cast< const MLDAPMessageListener
* >( m_Args
->arg5
) );
308 if ( pListener
->initialized() )
309 rv
= pListener
->goodConnection() ? 0 : (nsresult
)PR_NOT_CONNECTED_ERROR
;
311 rv
= (nsresult
)PR_CONNECT_TIMEOUT_ERROR
;
314 case ProxiedFunc::FUNC_TESTLDAP_RELEASE_RESOURCE
:
317 ((MLDAPMessageListener
*)m_Args
->arg5
)->Release();
318 delete (MLDAPMessageListener
*)m_Args
->arg5
;
324 return NS_ERROR_INVALID_ARG
;
329 MNSMozabProxy::InitLDAP(sal_Char
* sUri
, sal_Unicode
* sBindDN
, sal_Unicode
* pPasswd
,sal_Bool
* nUseSSL
)
331 sal_Bool useSSL
= *nUseSSL
;
334 nsCOMPtr
<nsILDAPURL
> url
;
335 url
= do_CreateInstance(NS_LDAPURL_CONTRACTID
, &rv
);
337 return NS_ERROR_INVALID_ARG
;
339 rv
= url
->SetSpec( nsDependentCString(sUri
) );
340 NS_ENSURE_SUCCESS(rv
, rv
);
343 rv
= url
->GetAsciiHost(host
);
344 NS_ENSURE_SUCCESS(rv
, rv
);
347 rv
= url
->GetPort(&port
);
348 NS_ENSURE_SUCCESS(rv
, rv
);
352 NS_ENSURE_SUCCESS(rv
, rv
);
356 // Get the ldap connection
357 nsCOMPtr
<nsILDAPConnection
> ldapConnection
;
358 ldapConnection
= do_CreateInstance(NS_LDAPCONNECTION_CONTRACTID
, &rv
);
359 NS_ENSURE_SUCCESS(rv
, rv
);
361 MLDAPMessageListener
* messageListener
=
362 new MLDAPMessageListener ( );
363 if (messageListener
== NULL
)
364 return NS_ERROR_INVALID_ARG
;
366 messageListener
->AddRef();
368 nsCAutoString nsBind
;
369 // PRUnichar != sal_Unicode in mingw
370 nsBind
.AssignWithConversion(reinterpret_cast_mingw_only
<const PRUnichar
*>(sBindDN
));
372 // Now lets initialize the LDAP connection properly.
373 rv
= ldapConnection
->Init(host
.get(), port
, useSSL
, nsBind
,
374 messageListener
,NULL
,nsILDAPConnection::VERSION3
);
375 // Initiate the LDAP operation
376 nsCOMPtr
<nsILDAPOperation
> ldapOperation
=
377 do_CreateInstance(NS_LDAPOPERATION_CONTRACTID
, &rv
);
379 rv
= ldapOperation
->Init(ldapConnection
, messageListener
, nsnull
);
381 return NS_ERROR_UNEXPECTED
; // this should never happen
383 if ( pPasswd
&& *pPasswd
)
385 nsCAutoString nsPassword
;
386 // PRUnichar != sal_Unicode in mingw
387 nsPassword
.AssignWithConversion(reinterpret_cast_mingw_only
<const PRUnichar
*>(pPasswd
));
388 rv
= ldapOperation
->SimpleBind(nsPassword
);
391 if (NS_SUCCEEDED(rv
))
392 m_Args
->arg5
= messageListener
;
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */