Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / mozab / mozillasrc / MNSMozabProxy.cxx
blob9cd96b40acdcd81674162a84ae6a5f953fcda785
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
24 #include "MQuery.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
32 #include "prprf.h"
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()
62 m_Args = NULL;
63 #if OSL_DEBUG_LEVEL > 0
64 m_oThreadID = osl::Thread::getCurrentIdentifier();
65 #endif
66 acquire();
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);
77 m_Product = aProduct;
78 m_Profile = aProfile;
79 m_Args = args;
80 if (!xRunner.is())
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)
99 return m_Product;
101 OUString SAL_CALL MNSMozabProxy::getProfileName( ) throw (::com::sun::star::uno::RuntimeException)
103 return m_Profile;
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 );
110 #else
111 OSL_TRACE( "IN : MNSMozabProxy::Run()" );
112 #endif
113 nsresult rv = NS_ERROR_INVALID_ARG;
114 if (m_Args == NULL)
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();
122 break;
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);
130 break;
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);
136 break;
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:
141 if (m_Args->arg1)
143 rv = QueryHelperStub();
145 break;
146 case ProxiedFunc::FUNC_NEW_ADDRESS_BOOK:
147 if (m_Args->arg1)
149 rv = NewAddressBook((const OUString*)m_Args->arg1 );
151 break;
152 default:
153 return NS_ERROR_INVALID_ARG;
155 return rv;
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();
168 rv = NS_OK;
170 break;
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);
176 break;
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);
182 break;
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));
188 break;
189 default:
190 break;
192 return rv;
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 {
202 namespace mozab {
203 class MLDAPMessageListener : public nsILDAPMessageListener
205 NS_DECL_ISUPPORTS
206 NS_DECL_NSILDAPMESSAGELISTENER
208 MLDAPMessageListener();
209 virtual ~MLDAPMessageListener();
211 sal_Bool initialized() const;
212 sal_Bool goodConnection() const { return initialized() && m_GoodConnection; }
214 protected:
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()
230 : mRefCnt( 0 )
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;
252 m_aCondition.set();
255 NS_IMETHODIMP MLDAPMessageListener::OnLDAPInit(nsILDAPConnection* /*aConn*/, nsresult aStatus )
257 setConnectionStatus( NS_SUCCEEDED( aStatus ) ? sal_True : sal_False );
258 return aStatus;
261 NS_IMETHODIMP MLDAPMessageListener::OnLDAPMessage( nsILDAPMessage* aMessage )
263 nsresult rv;
265 PRInt32 messageType;
266 rv = aMessage->GetType(&messageType);
267 NS_ENSURE_SUCCESS(rv, rv);
268 PRInt32 errCode;
269 switch (messageType)
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 );
277 else
278 setConnectionStatus( sal_True );
279 break;
280 case nsILDAPMessage::RES_SEARCH_RESULT:
281 setConnectionStatus( sal_True );
282 break;
283 default:
284 break;
287 return NS_OK;
292 nsresult
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);
303 break;
304 case ProxiedFunc::FUNC_TESTLDAP_IS_LDAP_CONNECTED:
305 if (m_Args->arg5)
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;
310 else
311 rv = (nsresult)PR_CONNECT_TIMEOUT_ERROR;
313 break;
314 case ProxiedFunc::FUNC_TESTLDAP_RELEASE_RESOURCE:
315 if (m_Args->arg5)
317 ((MLDAPMessageListener*)m_Args->arg5)->Release();
318 delete (MLDAPMessageListener*)m_Args->arg5;
319 m_Args->arg5 = NULL;
320 rv = 0;
322 break;
323 default:
324 return NS_ERROR_INVALID_ARG;
326 return rv;
328 nsresult
329 MNSMozabProxy::InitLDAP(sal_Char* sUri, sal_Unicode* sBindDN, sal_Unicode* pPasswd,sal_Bool * nUseSSL)
331 sal_Bool useSSL = *nUseSSL;
332 nsresult rv;
334 nsCOMPtr<nsILDAPURL> url;
335 url = do_CreateInstance(NS_LDAPURL_CONTRACTID, &rv);
336 if ( NS_FAILED(rv) )
337 return NS_ERROR_INVALID_ARG;
339 rv = url->SetSpec( nsDependentCString(sUri) );
340 NS_ENSURE_SUCCESS(rv, rv);
342 nsCAutoString host;
343 rv = url->GetAsciiHost(host);
344 NS_ENSURE_SUCCESS(rv, rv);
346 PRInt32 port;
347 rv = url->GetPort(&port);
348 NS_ENSURE_SUCCESS(rv, rv);
350 nsCString dn;
351 rv = url->GetDn(dn);
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);
380 if (NS_FAILED(rv))
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;
393 return rv;
396 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */