update emoji autocorrect entries from po-files
[LibreOffice.git] / connectivity / source / drivers / mozab / mozillasrc / MNSMozabProxy.cxx
blob5afbc95bdb51efbb839217cc83c479424594d696
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_LDAPURL_CONTRACTID "@mozilla.org/network/ldap-url;1"
200 namespace connectivity {
201 namespace mozab {
202 class MLDAPMessageListener : public nsILDAPMessageListener
204 NS_DECL_ISUPPORTS
205 NS_DECL_NSILDAPMESSAGELISTENER
207 MLDAPMessageListener();
208 virtual ~MLDAPMessageListener();
210 sal_Bool initialized() const;
211 sal_Bool goodConnection() const { return initialized() && m_GoodConnection; }
213 protected:
215 ::osl::Mutex m_aMutex;
216 ::osl::Condition m_aCondition;
218 sal_Bool m_IsComplete;
219 sal_Bool m_GoodConnection;
221 void setConnectionStatus( sal_Bool _good );
226 NS_IMPL_THREADSAFE_ISUPPORTS1(MLDAPMessageListener, nsILDAPMessageListener)
228 MLDAPMessageListener::MLDAPMessageListener()
229 : mRefCnt( 0 )
230 , m_IsComplete( sal_False )
231 , m_GoodConnection( sal_False )
233 m_aCondition.reset();
236 MLDAPMessageListener::~MLDAPMessageListener()
240 sal_Bool MLDAPMessageListener::initialized() const
242 return const_cast< MLDAPMessageListener* >( this )->m_aCondition.check();
245 void MLDAPMessageListener::setConnectionStatus( sal_Bool _good )
247 ::osl::MutexGuard aGuard( m_aMutex );
248 m_IsComplete = sal_True;
249 m_GoodConnection = _good;
251 m_aCondition.set();
254 NS_IMETHODIMP MLDAPMessageListener::OnLDAPInit(nsILDAPConnection* /*aConn*/, nsresult aStatus )
256 setConnectionStatus( NS_SUCCEEDED( aStatus ) ? sal_True : sal_False );
257 return aStatus;
260 NS_IMETHODIMP MLDAPMessageListener::OnLDAPMessage( nsILDAPMessage* aMessage )
262 nsresult rv;
264 PRInt32 messageType;
265 rv = aMessage->GetType(&messageType);
266 NS_ENSURE_SUCCESS(rv, rv);
267 PRInt32 errCode;
268 switch (messageType)
270 case nsILDAPMessage::RES_BIND:
271 rv = aMessage->GetErrorCode(&errCode);
272 // if the login failed
273 if (errCode != (PRInt32)nsILDAPErrors::SUCCESS) {
274 setConnectionStatus( sal_False );
276 else
277 setConnectionStatus( sal_True );
278 break;
279 case nsILDAPMessage::RES_SEARCH_RESULT:
280 setConnectionStatus( sal_True );
281 break;
282 default:
283 break;
286 return NS_OK;
291 nsresult
292 MNSMozabProxy::testLDAPConnection( )
294 nsresult rv=NS_ERROR_INVALID_ARG;
295 switch(m_Args->funcIndex)
297 case ProxiedFunc::FUNC_TESTLDAP_INIT_LDAP:
298 if (m_Args->arg1 && m_Args->arg4 )
300 rv = InitLDAP((sal_Char*)m_Args->arg1,(sal_Unicode*)m_Args->arg2,(sal_Unicode*)m_Args->arg3,(sal_Bool*)m_Args->arg4);
302 break;
303 case ProxiedFunc::FUNC_TESTLDAP_IS_LDAP_CONNECTED:
304 if (m_Args->arg5)
306 const MLDAPMessageListener* pListener( static_cast< const MLDAPMessageListener* >( m_Args->arg5 ) );
307 if ( pListener->initialized() )
308 rv = pListener->goodConnection() ? 0 : (nsresult)PR_NOT_CONNECTED_ERROR;
309 else
310 rv = (nsresult)PR_CONNECT_TIMEOUT_ERROR;
312 break;
313 case ProxiedFunc::FUNC_TESTLDAP_RELEASE_RESOURCE:
314 if (m_Args->arg5)
316 ((MLDAPMessageListener*)m_Args->arg5)->Release();
317 delete (MLDAPMessageListener*)m_Args->arg5;
318 m_Args->arg5 = NULL;
319 rv = 0;
321 break;
322 default:
323 return NS_ERROR_INVALID_ARG;
325 return rv;
327 nsresult
328 MNSMozabProxy::InitLDAP(sal_Char* sUri, sal_Unicode* sBindDN, sal_Unicode* pPasswd,sal_Bool * nUseSSL)
330 sal_Bool useSSL = *nUseSSL;
331 nsresult rv;
333 nsCOMPtr<nsILDAPURL> url;
334 url = do_CreateInstance(NS_LDAPURL_CONTRACTID, &rv);
335 if ( NS_FAILED(rv) )
336 return NS_ERROR_INVALID_ARG;
338 rv = url->SetSpec( nsDependentCString(sUri) );
339 NS_ENSURE_SUCCESS(rv, rv);
341 nsCAutoString host;
342 rv = url->GetAsciiHost(host);
343 NS_ENSURE_SUCCESS(rv, rv);
345 PRInt32 port;
346 rv = url->GetPort(&port);
347 NS_ENSURE_SUCCESS(rv, rv);
349 nsCString dn;
350 rv = url->GetDn(dn);
351 NS_ENSURE_SUCCESS(rv, rv);
355 // Get the ldap connection
356 nsCOMPtr<nsILDAPConnection> ldapConnection;
357 ldapConnection = do_CreateInstance(NS_LDAPCONNECTION_CONTRACTID, &rv);
358 NS_ENSURE_SUCCESS(rv, rv);
360 MLDAPMessageListener* messageListener =
361 new MLDAPMessageListener ( );
362 if (messageListener == NULL)
363 return NS_ERROR_INVALID_ARG;
365 messageListener->AddRef();
367 nsCAutoString nsBind;
368 // PRUnichar != sal_Unicode in mingw
369 nsBind.AssignWithConversion(reinterpret_cast_mingw_only<const PRUnichar *>(sBindDN));
371 // Now lets initialize the LDAP connection properly.
372 rv = ldapConnection->Init(host.get(), port, useSSL, nsBind,
373 messageListener,NULL,nsILDAPConnection::VERSION3);
374 // Initiate the LDAP operation
375 nsCOMPtr<nsILDAPOperation> ldapOperation =
376 do_CreateInstance(NS_LDAPOPERATION_CONTRACTID, &rv);
378 rv = ldapOperation->Init(ldapConnection, messageListener, nsnull);
379 if (NS_FAILED(rv))
380 return NS_ERROR_UNEXPECTED; // this should never happen
382 if ( pPasswd && *pPasswd )
384 nsCAutoString nsPassword;
385 // PRUnichar != sal_Unicode in mingw
386 nsPassword.AssignWithConversion(reinterpret_cast_mingw_only<const PRUnichar *>(pPasswd));
387 rv = ldapOperation->SimpleBind(nsPassword);
390 if (NS_SUCCEEDED(rv))
391 m_Args->arg5 = messageListener;
392 return rv;
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */