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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
/* -*- 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_connectivity.hxx"
#include "MQueryHelper.hxx"
#include "MTypeConverter.hxx"
#include "MConnection.hxx"
#include "MNSDeclares.hxx"
#include "MLdapAttributeMap.hxx"
#include <connectivity/dbexception.hxx>
#include "resource/mozab_res.hrc"
using namespace connectivity::mozab;
NS_IMPL_THREADSAFE_ISUPPORTS1(connectivity::mozab::MQueryHelper,nsIAbDirectoryQueryResultListener)
//
// class MQueryHelperResultEntry
//
MQueryHelperResultEntry::MQueryHelperResultEntry()
{
m_Card = NULL;
m_RowStates = 0;
}
MQueryHelperResultEntry::~MQueryHelperResultEntry()
{
OSL_TRACE("IN MQueryHelperResultEntry::~MQueryHelperResultEntry()");
OSL_TRACE("OUT MQueryHelperResultEntry::~MQueryHelperResultEntry()\n");
}
void
MQueryHelperResultEntry::setCard(nsIAbCard *card)
{
m_Card = card;
}
nsIAbCard *
MQueryHelperResultEntry::getCard()
{
return m_Card;
}
void MQueryHelperResultEntry::insert( const rtl::OString &key, rtl::OUString &value )
{
m_Fields[ key ] = value;
}
rtl::OUString MQueryHelperResultEntry::getValue( const rtl::OString &key ) const
{
FieldMap::const_iterator iter = m_Fields.find( key );
if ( iter == m_Fields.end() )
{
return rtl::OUString();
}
else
{
return iter->second;
}
}
void MQueryHelperResultEntry::setValue( const rtl::OString &key, const rtl::OUString & rValue)
{
m_Fields[ key ] = rValue;
}
//
// class MQueryHelper
//
MQueryHelper::MQueryHelper()
:m_nIndex( 0 )
,m_bHasMore( sal_True )
,m_bAtEnd( sal_False )
,m_bErrorCondition( sal_False )
,m_bQueryComplete( sal_False )
,mRefCnt( 0 ) // NSISUPPORTS - Initialize RefCnt to 0
{
m_aResults.clear();
#if OSL_DEBUG_LEVEL > 0
m_oThreadID = osl_getThreadIdentifier(NULL);
#endif
}
MQueryHelper::~MQueryHelper()
{
OSL_TRACE("IN MQueryHelper::~MQueryHelper()");
clear_results();
OSL_TRACE("OUT MQueryHelper::~MQueryHelper()");
}
void
MQueryHelper::append(MQueryHelperResultEntry* resEnt )
{
if ( resEnt != NULL ) {
m_aResults.push_back( resEnt );
m_bAtEnd = sal_False;
}
}
void
MQueryHelper::clear_results()
{
resultsArray::iterator iter = m_aResults.begin();
while ( iter != m_aResults.end() ) {
delete (*iter);
++iter;
}
m_aResults.clear();
}
void
MQueryHelper::rewind()
{
m_nIndex = 0;
}
void
MQueryHelper::reset()
{
m_nIndex = 0;
m_bHasMore = sal_True;
m_bQueryComplete = sal_False;
m_bAtEnd = sal_False;
m_bErrorCondition = sal_False;
clear_results();
}
void
MQueryHelper::clearResultOrComplete()
{
// Don't use a Mutex, it should be called by a method already holding it.
OSL_TRACE("In/Out : clearResultOrComplete()");
m_aCondition.reset();
}
void
MQueryHelper::notifyResultOrComplete()
{
OSL_TRACE("In/Out : notifyResultOrComplete()");
m_aCondition.set();
}
sal_Bool
MQueryHelper::waitForResultOrComplete( )
{
TimeValue timeValue = { 1, 0 }; // 20 Seconds 0 NanoSecond timeout
sal_Int32 times=0;
osl::Condition::Result rv = ::osl::Condition::result_ok;
OSL_TRACE("In : waitForResultOrComplete()");
// Can't hold mutex or condition would never get set...
while( (m_aCondition.check() == sal_False || rv == ::osl::Condition::result_error) && times < 20) {
rv = m_aCondition.wait( &timeValue );
times ++;
}
if (times >= 20 && rv == ::osl::Condition::result_timeout ) {
OSL_TRACE("waitForResultOrComplete() : Timeout!");
m_aError.setResId( STR_TIMEOUT_WAITING );
return sal_False;
}
if ( isError() ) {
OSL_TRACE("waitForResultOrComplete() : Error returned!");
m_aError.setResId( STR_ERR_EXECUTING_QUERY );
return sal_False;
}
m_aError.reset();
OSL_TRACE(" Out : waitForResultOrComplete()");
return sal_True;
}
MQueryHelperResultEntry*
MQueryHelper::next( )
{
MQueryHelperResultEntry* result;
sal_Int32 index;
m_aMutex.acquire();
index = m_nIndex;
m_aMutex.release();
result = getByIndex( index + 1) ; // Add 1 as Row is numbered from 1 to N
if ( result ) {
m_aMutex.acquire();
m_nIndex++;
m_aMutex.release();
}
return( result );
}
MQueryHelperResultEntry*
MQueryHelper::getByIndex( sal_uInt32 nRow )
{
// Row numbers are from 1 to N, need to ensure this, and then
// substract 1
if ( nRow < 1 ) {
return( NULL );
}
do {
// Obtain the Mutex - don't use a guard as we want to be able to release
// and acquire again...
m_aMutex.acquire();
if ( nRow > m_aResults.size() )
{
if ( m_bQueryComplete )
{
m_bAtEnd = sal_True;
m_aMutex.release();
return( NULL );
}
else
{
clearResultOrComplete();
m_aMutex.release();
if ( !waitForResultOrComplete( ) )
return( NULL );
}
}
else
{
m_aMutex.release();
return( m_aResults[ nRow -1 ] );
}
} while ( sal_True );
}
sal_Bool
MQueryHelper::hasMore() const
{
return m_bHasMore;
}
sal_Bool
MQueryHelper::atEnd() const
{
return m_bAtEnd;
}
sal_Bool
MQueryHelper::isError() const
{
return m_bErrorCondition;
}
sal_Bool
MQueryHelper::queryComplete() const
{
return m_bQueryComplete;
}
sal_Bool
MQueryHelper::waitForQueryComplete( )
{
m_aMutex.acquire();
OSL_TRACE("In : waitForQueryComplete()");
if ( ! m_bQueryComplete ) {
do
{
m_aMutex.release();
clearResultOrComplete();
if ( !waitForResultOrComplete( ) )
return( sal_False );
m_aMutex.acquire();
}
while ( !m_bQueryComplete );
}
m_aMutex.release();
OSL_TRACE("Out : waitForQueryComplete()");
return( sal_True );
}
sal_Bool
MQueryHelper::waitForRow( sal_Int32 rowNum )
{
m_aMutex.acquire();
do
{
m_aMutex.release();
clearResultOrComplete();
if ( !waitForResultOrComplete() )
return( sal_False );
m_aMutex.acquire();
}
while ( !m_bQueryComplete && m_aResults.size() < (size_t)rowNum );
m_aMutex.release();
return( sal_True );
}
// -------------------------------------------------------------------------
sal_Int32
MQueryHelper::getResultCount() const
{
OSL_TRACE( "IN MQueryHelper::getResultCount()" );
if ( !m_bQueryComplete )
{
OSL_TRACE( "\tOUT MQueryHelper::getResultCount() = -1");
return -1;
}
else
{
OSL_TRACE( "\tOUT MQueryHelper::getResultCount() = %d", m_aResults.size() );
return static_cast<sal_Int32>(m_aResults.size());
}
}
// -------------------------------------------------------------------------
sal_uInt32
MQueryHelper::getRealCount() const
{
OSL_TRACE( "IN/OUT MQueryHelper::getRealCount() = %d", m_aResults.size() );
return static_cast<sal_Int32>(m_aResults.size());
}
// -------------------------------------------------------------------------
NS_IMETHODIMP MQueryHelper::OnQueryItem(nsIAbDirectoryQueryResult *result)
{
::osl::MutexGuard aGuard( m_aMutex );
#if OSL_DEBUG_LEVEL > 0
OSL_TRACE( "IN MQueryHelper::OnQueryItem() Caller thread: %4d",m_oThreadID );
#endif
nsresult rv;
PRInt32 resultType;
if ( result == NULL ) {
OSL_TRACE("\tresult ptr is NULL");
return NS_OK;
}
// Get return status of executeQuery() call.
rv = result -> GetType(&resultType);
NS_ENSURE_SUCCESS(rv, rv);
// Check for errors of the executeQuery() call.
switch ( resultType ) {
case nsIAbDirectoryQueryResult::queryResultError:
OSL_TRACE("\tresultType == nsIAbDirectoryQueryResult::queryResultError");
m_bQueryComplete = sal_True;
m_bErrorCondition = sal_True;
notifyResultOrComplete();
return NS_OK;
case nsIAbDirectoryQueryResult::queryResultStopped:
OSL_TRACE("\tresultType == nsIAbDirectoryQueryResult::queryResultStopped");
m_bQueryComplete = sal_True;
notifyResultOrComplete();
return NS_OK;
case nsIAbDirectoryQueryResult::queryResultComplete:
OSL_TRACE("\tresultType == nsIAbDirectoryQueryResult::queryResultComplete");
m_bQueryComplete = sal_True;
notifyResultOrComplete();
return NS_OK;
case nsIAbDirectoryQueryResult::queryResultMatch:
OSL_TRACE("IN MQueryHelper::OnQueryItem --> queryResultMatch");
// Don't return, continues onto rest of method.
break;
default:
OSL_TRACE("\t******** Unexpected : resultType");
m_bQueryComplete = sal_True;
return NS_OK;
}
// Initialise an array that holds the resultset of the query.
nsCOMPtr<nsISupportsArray> properties;
rv = result -> GetResult(getter_AddRefs (properties));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsISupports> item;
rv = properties -> GetElementAt(0, getter_AddRefs(item));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbDirectoryQueryPropertyValue> property(do_QueryInterface(item, &rv));
NS_ENSURE_SUCCESS(rv, rv);
char *name;
rv = property -> GetName(&name);
NS_ENSURE_SUCCESS(rv, rv);
if ( !strcmp(name,"card:nsIAbCard") )
{
nsCOMPtr<nsISupports> cardSupports;
property->GetValueISupports (getter_AddRefs (cardSupports));
nsCOMPtr<nsIAbCard> card(do_QueryInterface(cardSupports, &rv));
NS_ENSURE_SUCCESS(rv, rv);
getCardValues(card);
}
nsMemory::Free(name);
OSL_TRACE( "\tOUT MQueryHelper::OnQueryItem()" );
notifyResultOrComplete();
return(NS_OK);
}
// -----------------------------------------------------------------------------
void MQueryHelper::notifyQueryError()
{
m_bQueryComplete = sal_True ;
notifyResultOrComplete() ;
}
const char * getAddrURI(const nsIAbDirectory* directory)
{
nsresult retCode;
nsCOMPtr<nsIRDFResource> rdfResource = do_QueryInterface((nsISupports *)directory, &retCode) ;
if (NS_FAILED(retCode)) { return NULL; }
const char * uri;
retCode=rdfResource->GetValueConst(&uri);
if (NS_FAILED(retCode)) { return NULL; }
return uri;
}
#define ENSURE_GETUPDATECARD(x) \
if (NS_FAILED(retCode)) \
{ \
OSL_TRACE( x ); \
return card; \
}
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
//Some address book does not support query uri on card
//In this case, we can't resync the cards, we just return the old cards
nsIAbCard * getUpdatedCard( nsIAbCard* card)
{
OSL_ENSURE(card != NULL, "getUpdatedCard for NULL");
nsresult retCode;
nsCOMPtr<nsIRDFResource> rdfResource = do_QueryInterface((nsISupports *)card, &retCode) ;
ENSURE_GETUPDATECARD( "IN getUpdatedCard: Card does not support nsIRDFResource\n" );
const char * uri;
retCode=rdfResource->GetValueConst(&uri);
ENSURE_GETUPDATECARD( "IN getUpdatedCard: Card does not has a uri\n" );
nsCOMPtr<nsIRDFService> rdfService (do_GetService(kRDFServiceCID, &retCode)) ;
ENSURE_GETUPDATECARD( "IN getUpdatedCard: Card does not has a uri\n" );
nsCOMPtr<nsIRDFResource> rdfCard;
retCode = rdfService->GetResource(nsDependentCString(uri), getter_AddRefs(rdfCard)) ;
ENSURE_GETUPDATECARD( "IN getUpdatedCard: Can not get the updated card\n" );
nsCOMPtr<nsIAbCard> aNewCard=do_QueryInterface((nsISupports *)rdfCard, &retCode);
ENSURE_GETUPDATECARD( "IN getUpdatedCard: Error in get new card\n" );
return aNewCard;
}
#define ENSURE_MOZAB_PROFILE_NOT_LOOKED(directory) \
if (getDirectoryType(directory) == SDBCAddress::Mozilla && isProfileLocked(NULL)) \
{ \
m_aError.setResId( STR_MOZILLA_IS_RUNNIG_NO_CHANGES ); \
return sal_False; \
}
sal_Int32 MQueryHelper::commitCard(const sal_Int32 rowIndex,nsIAbDirectory * directory)
{
ENSURE_MOZAB_PROFILE_NOT_LOOKED(directory);
MQueryHelperResultEntry *resEntry = getByIndex(rowIndex);
if (!resEntry)
{
return sal_False;
}
nsIAbCard *card=resEntry->getCard();
if (!card)
{
return sal_False;
}
nsresult rv;
PRBool hasCard;
rv = directory->HasCard(card,&hasCard);
if (setCardValues(rowIndex) != sal_True)
return sal_False;
if (!NS_FAILED(rv) && hasCard)
{
rv = card->EditCardToDatabase(getAddrURI(directory));
}
else
{
nsIAbCard *addedCard=NULL;
rv = directory->AddCard(card,&addedCard);
if (!NS_FAILED(rv))
resEntry->setCard(addedCard);
}
//We return NS_ERROR_FILE_ACCESS_DENIED in the case the mozillaAB has been changed out side of our process
if (rv == NS_ERROR_FILE_ACCESS_DENIED )
m_aError.setResId( STR_FOREIGN_PROCESS_CHANGED_AB );
return !(NS_FAILED(rv));
}
sal_Int32 MQueryHelper::deleteCard(const sal_Int32 rowIndex,nsIAbDirectory * directory)
{
ENSURE_MOZAB_PROFILE_NOT_LOOKED(directory);
MQueryHelperResultEntry *resEntry = getByIndex(rowIndex);
if (!resEntry)
{
return sal_False;
}
nsIAbCard *card=resEntry->getCard();
if (!card)
{
return sal_False;
}
nsresult rv;
PRBool hasCard;
if (resEntry->getRowStates() == RowStates_Inserted)
{
rv = 0;
hasCard = sal_True;
}
else
rv = directory->HasCard(card,&hasCard);
if (!NS_FAILED(rv) && hasCard)
{
nsCOMPtr <nsISupportsArray> cardsToDelete;
rv = NS_NewISupportsArray(getter_AddRefs(cardsToDelete));
if (NS_SUCCEEDED(rv))
{
nsCOMPtr<nsISupports> supports = do_QueryInterface(card, &rv);
if (NS_SUCCEEDED(rv))
{
rv = cardsToDelete->AppendElement(supports);
if (NS_SUCCEEDED(rv))
rv = directory->DeleteCards(cardsToDelete);
}
}
}
if (NS_SUCCEEDED(rv))
resEntry->setRowStates(RowStates_Deleted);
//We return NS_ERROR_FILE_ACCESS_DENIED in the case the mozillaAB has been changed out side of our process
if (rv == NS_ERROR_FILE_ACCESS_DENIED )
m_aError.setResId( STR_FOREIGN_PROCESS_CHANGED_AB );
return !(NS_FAILED(rv));
}
sal_Bool MQueryHelper::setCardValues(const sal_Int32 rowIndex)
{
MQueryHelperResultEntry *resEntry = getByIndex(rowIndex);
if (!resEntry)
{
m_aError.setResId( STR_CANT_FIND_ROW );
return sal_False;
}
nsIAbCard *card=resEntry->getCard();
if (!card)
{
m_aError.setResId( STR_CANT_FIND_CARD_FOR_ROW );
return sal_False;
}
MLdapAttributeMap::fillCardFromResult( *card, *resEntry );
return sal_True;
}
void MQueryHelper::getCardValues(nsIAbCard *card,sal_Int32 rowIndex)
{
MQueryHelperResultEntry *resEntry;
if (rowIndex>0)
{
resEntry = getByIndex(rowIndex);
}
else
resEntry = new MQueryHelperResultEntry();
MLdapAttributeMap::fillResultFromCard( *resEntry, *card );
resEntry->setCard(card);
if (!rowIndex)
append( resEntry );
}
sal_Bool MQueryHelper::resyncRow(sal_Int32 rowIndex)
{
MQueryHelperResultEntry *resEntry = getByIndex(rowIndex);
if (!resEntry)
{
m_aError.setResId( STR_CANT_FIND_ROW );
return sal_False;
}
nsIAbCard *card=resEntry->getCard();
card = getUpdatedCard(card);
getCardValues(card,rowIndex);
return sal_True;
}
// -------------------------------------------------------------------------
sal_Int32 MQueryHelper::createNewCard()
{
::osl::MutexGuard aGuard( m_aMutex );
nsresult rv;
nsCOMPtr <nsIAbCard> card = do_CreateInstance(NS_ABCARDPROPERTY_CONTRACTID, &rv);
//set default values
getCardValues(card);
return static_cast<sal_Int32>(m_aResults.size());
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|