Back to index

lightning-sunbird  0.9+nobinonly
Classes | Functions | Variables
nsAbOSXDirectory.mm File Reference
#include "nsAbOSXDirectory.h"
#include "nsAbOSXCard.h"
#include "nsAbOSXUtils.h"
#include "nsAbQueryStringToExpression.h"
#include "nsArrayEnumerator.h"
#include "nsAutoPtr.h"
#include "nsCOMArray.h"
#include "nsEnumeratorUtils.h"
#include "nsIAbDirectoryQueryProxy.h"
#include "nsIAddrBookSession.h"
#include "nsIRDFService.h"
#include "nsServiceManagerUtils.h"
#include <AddressBook/AddressBook.h>

Go to the source code of this file.

Classes

class  ABChangedMonitor
struct  nsEnumeratorData

Functions

static nsresult ConvertToGroupResource (nsIRDFService *aRDFService, NSString *aUid, nsIAbDirectory **aResult)
static nsresult ConvertToCard (nsIRDFService *aRDFService, ABRecord *aRecord, nsIAbCard **aResult)
static nsresult Update (nsIRDFService *aRDFService, NSString *aUid)
static nsresult MapConditionString (nsIAbBooleanConditionString *aCondition, PRBool aNegate, PRBool &aCanHandle, ABSearchElement **aResult)
static nsresult BuildSearchElements (nsIAbBooleanExpression *aExpression, PRBool &aCanHandle, ABSearchElement **aResult)
static PRBool Search (nsIAbBooleanExpression *aExpression, NSArray **aResult)
 NS_IMPL_ISUPPORTS_INHERITED2 (nsAbOSXDirectory, nsAbDirectoryRDFResource, nsIAbDirectory, nsIAbOSXDirectory) NS_IMETHODIMP nsAbOSXDirectory
PLDHashOperator Enumerator (nsIAbCardHashKey *aKey, void *aUserArg)

Variables

static PRUint32 sObserverCount = 0
static ABChangedMonitorsObserver = nsnull

Function Documentation

static nsresult BuildSearchElements ( nsIAbBooleanExpression aExpression,
PRBool aCanHandle,
ABSearchElement **  aResult 
) [static]

Definition at line 354 of file nsAbOSXDirectory.mm.

{
    aCanHandle = PR_TRUE;

    nsCOMPtr<nsISupportsArray> expressions;
    nsresult rv = aExpression->GetExpressions(getter_AddRefs(expressions));
    NS_ENSURE_SUCCESS(rv, rv);

    nsAbBooleanOperationType operation;
    rv = aExpression->GetOperation(&operation);
    NS_ENSURE_SUCCESS(rv, rv);

    PRUint32 count;
    rv = expressions->Count(&count);
    NS_ENSURE_SUCCESS(rv, rv);

    NS_ASSERTION(count > 1 && operation != nsIAbBooleanOperationTypes::NOT,
                 "This doesn't make sense!");

    NSMutableArray *array = nsnull;
    if (count > 1) {
        array = [[NSMutableArray alloc] init];
    }

    PRUint32 i;
    nsCOMPtr<nsIAbBooleanConditionString> condition;
    nsCOMPtr<nsIAbBooleanExpression> subExpression;
    for (i = 0; i < count; ++i) {
        ABSearchElement *element = nsnull;

        condition = do_QueryElementAt(expressions, i);
        if (condition) {
            rv = MapConditionString(condition, operation == nsIAbBooleanOperationTypes::NOT, aCanHandle, &element);
            NS_ENSURE_SUCCESS(rv, rv);
        }
        else {
            subExpression = do_QueryElementAt(expressions, i);
            if (subExpression) {
                rv = BuildSearchElements(subExpression, aCanHandle, &element);
                NS_ENSURE_SUCCESS(rv, rv);
            }
        }

        if (!aCanHandle) {
            return NS_OK;
        }

        if (element) {
            if (array) {
                [array addObject:element];
            }
            else {
                *aResult = element;
            }
        }
    }

    if (array) {
        ABSearchConjunction conjunction = operation == nsIAbBooleanOperationTypes::AND ? kABSearchAnd : kABSearchOr;
        *aResult = [ABSearchElement searchElementForConjunction:conjunction children:array];
    }

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static nsresult ConvertToCard ( nsIRDFService aRDFService,
ABRecord *  aRecord,
nsIAbCard **  aResult 
) [static]

Definition at line 74 of file nsAbOSXDirectory.mm.

{
    *aResult = nsnull;

    NSString *uid = [aRecord uniqueId];
    NS_ASSERTION(uid, "No UID for card!.");
    if (!uid) {
        return NS_ERROR_FAILURE;
    }

    nsCAutoString uri(NS_ABOSXCARD_URI_PREFIX);
    AppendToCString(uid, uri);

    nsCOMPtr<nsIRDFResource> resource;
    nsresult rv = aRDFService->GetResource(uri, getter_AddRefs(resource));
    NS_ENSURE_SUCCESS(rv, rv);

    return CallQueryInterface(resource, aResult);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static nsresult ConvertToGroupResource ( nsIRDFService aRDFService,
NSString aUid,
nsIAbDirectory **  aResult 
) [static]

Definition at line 56 of file nsAbOSXDirectory.mm.

{
    NS_ASSERTION(aUid, "No UID for group!.");

    *aResult = nsnull;

    nsCAutoString uri(NS_ABOSXDIRECTORY_URI_PREFIX);
    AppendToCString(aUid, uri);

    nsCOMPtr<nsIRDFResource> resource;
    nsresult rv = aRDFService->GetResource(uri, getter_AddRefs(resource));
    NS_ENSURE_SUCCESS(rv, rv);

    return CallQueryInterface(resource, aResult);
}

Here is the call graph for this function:

Here is the caller graph for this function:

PLDHashOperator Enumerator ( nsIAbCardHashKey aKey,
void aUserArg 
)

Definition at line 497 of file nsAbOSXDirectory.mm.

{
    nsEnumeratorData *data = NS_STATIC_CAST(nsEnumeratorData*, aUserArg);

    nsIAbCard *abCard = aKey->GetCard();

    nsCOMPtr<nsIRDFResource> resource = do_QueryInterface(abCard);

    const char* uri;
    resource->GetValueConst(&uri);
    NSString *uid = [NSString stringWithUTF8String:(uri + 21)];

    unsigned int i, count = [data->mCards count];
    for (i = 0; i < count; ++i) {
        if ([[[data->mCards objectAtIndex:i] uniqueId] isEqualToString:uid]) {
            [data->mCards removeObjectAtIndex:i];
            break;
        }
    }

    if (i == count) {
        data->mSession->NotifyDirectoryItemDeleted(data->mDirectory, abCard);

        return PL_DHASH_REMOVE;
    }

    return PL_DHASH_NEXT;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static nsresult MapConditionString ( nsIAbBooleanConditionString aCondition,
PRBool  aNegate,
PRBool aCanHandle,
ABSearchElement **  aResult 
) [static]

Definition at line 229 of file nsAbOSXDirectory.mm.

{
    aCanHandle = PR_FALSE;

    nsAbBooleanConditionType conditionType = 0;
    nsresult rv = aCondition->GetCondition(&conditionType);
    NS_ENSURE_SUCCESS(rv, rv);

    ABSearchComparison comparison;
    switch (conditionType) {
        case nsIAbBooleanConditionTypes::Contains:
        {
            if (!aNegate) {
                comparison = kABContainsSubString;
                aCanHandle = PR_TRUE;
            }
            break;
        }
        case nsIAbBooleanConditionTypes::DoesNotContain:
        {
            if (aNegate) {
                comparison = kABContainsSubString;
                aCanHandle = PR_TRUE;
            }
            break;
        }
        case nsIAbBooleanConditionTypes::Is:
        {
            comparison = aNegate ? kABNotEqual : kABEqual;
            aCanHandle = PR_TRUE;
            break;
        }
        case nsIAbBooleanConditionTypes::IsNot:
        {
            comparison = aNegate ? kABEqual : kABNotEqual;
            aCanHandle = PR_TRUE;
            break;
        }
        case nsIAbBooleanConditionTypes::BeginsWith:
        {
            if (!aNegate) {
                comparison = kABPrefixMatch;
                aCanHandle = PR_TRUE;
            }
            break;
        }
        case nsIAbBooleanConditionTypes::EndsWith:
        {
            //comparison = kABSuffixMatch;
            break;
        }
        case nsIAbBooleanConditionTypes::LessThan:
        {
            comparison = aNegate ? kABGreaterThanOrEqual : kABLessThan;
            aCanHandle = PR_TRUE;
            break;
        }
        case nsIAbBooleanConditionTypes::GreaterThan:
        {
            comparison = aNegate ? kABLessThanOrEqual : kABGreaterThan;
            aCanHandle = PR_TRUE;
            break;
        }
    }

    if (!aCanHandle) {
        return NS_OK;
    }

    nsXPIDLCString name;
    rv = aCondition->GetName(getter_Copies(name));
    NS_ENSURE_SUCCESS(rv, rv);

    nsXPIDLString value;
    rv = aCondition->GetValue(getter_Copies(value));
    NS_ENSURE_SUCCESS(rv, rv);

    PRUint32 length = value.Length();

    PRUint32 i;
    for (i = 0; i < nsAbOSXUtils::kPropertyMapSize; ++i) {
        if (name.EqualsASCII(nsAbOSXUtils::kPropertyMap[i].mPropertyName)) {
            *aResult =
                [ABPerson searchElementForProperty:nsAbOSXUtils::kPropertyMap[i].mOSXProperty
                                             label:nsAbOSXUtils::kPropertyMap[i].mOSXLabel
                                               key:nsAbOSXUtils::kPropertyMap[i].mOSXKey
                                             value:[NSString stringWithCharacters:value.get() length:length]
                                        comparison:comparison];

            return NS_OK;
        }
    }

    if (name.EqualsLiteral("DisplayName") && comparison == kABContainsSubString) {
        ABSearchElement *first =
                [ABPerson searchElementForProperty:kABFirstNameProperty
                                             label:nil
                                               key:nil
                                             value:[NSString stringWithCharacters:value.get() length:length]
                                        comparison:comparison];
        ABSearchElement *second =
                [ABPerson searchElementForProperty:kABLastNameProperty
                                             label:nil
                                               key:nil
                                             value:[NSString stringWithCharacters:value.get() length:length]
                                        comparison:comparison];
        ABSearchElement *third =
                [ABGroup searchElementForProperty:kABGroupNameProperty
                                             label:nil
                                               key:nil
                                             value:[NSString stringWithCharacters:value.get() length:length]
                                        comparison:comparison];

        *aResult = [ABSearchElement searchElementForConjunction:kABSearchOr children:[NSArray arrayWithObjects:first, second, third, nil]];

        return NS_OK;
    }

    aCanHandle = PR_FALSE;

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 447 of file nsAbOSXDirectory.mm.

{
    ABAddressBook *addressBook = [ABAddressBook sharedAddressBook];
    if (sObserverCount == 0) {
        sObserver = [[ABChangedMonitor alloc] init];
        [[NSNotificationCenter defaultCenter] addObserver:(ABChangedMonitor*)sObserver
           selector:@selector(ABChanged:)
           name:kABDatabaseChangedExternallyNotification
           object:nil];
    }
    ++sObserverCount;

    nsresult rv = nsAbDirectoryRDFResource::Init(aUri);
    NS_ENSURE_SUCCESS(rv, rv);

    if (mURINoQuery.Length() > 22) {
        nsCAutoString uid(Substring(mURINoQuery, 21));
        ABRecord *card = [addressBook recordForUniqueId:[NSString stringWithUTF8String:uid.get()]];
        NS_ASSERTION([card isKindOfClass:[ABGroup class]], "Huh.");

        m_IsMailList = PR_TRUE;
        AppendToString([card valueForProperty:kABGroupNameProperty], m_DirName);
    }

    return NS_OK;
}

Here is the call graph for this function:

static PRBool Search ( nsIAbBooleanExpression aExpression,
NSArray **  aResult 
) [static]

Definition at line 422 of file nsAbOSXDirectory.mm.

{
    PRBool canHandle = PR_FALSE;
    ABSearchElement *searchElement;
    nsresult rv = BuildSearchElements(aExpression, canHandle, &searchElement);
    NS_ENSURE_SUCCESS(rv, PR_FALSE);

    if (canHandle) {
        *aResult = [[ABAddressBook sharedAddressBook] recordsMatchingSearchElement:searchElement];
    }

    return canHandle;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static nsresult Update ( nsIRDFService aRDFService,
NSString aUid 
) [static]

Definition at line 96 of file nsAbOSXDirectory.mm.

{
    ABAddressBook *addressBook = [ABAddressBook sharedAddressBook];
    ABRecord *card = [addressBook recordForUniqueId:aUid];
    if ([card isKindOfClass:[ABGroup class]]) {
        nsCOMPtr<nsIAbDirectory> directory;
        ConvertToGroupResource(aRDFService, aUid, getter_AddRefs(directory));
        nsCOMPtr<nsIAbOSXDirectory> osxDirectory =
            do_QueryInterface(directory);

        osxDirectory->Update();
    }
    else {
        nsCOMPtr<nsIAbCard> abCard;
        ConvertToCard(aRDFService, card, getter_AddRefs(abCard));
        nsCOMPtr<nsIAbOSXCard> osxCard = do_QueryInterface(abCard);

        osxCard->Update(PR_TRUE);
    }
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 437 of file nsAbOSXDirectory.mm.

PRUint32 sObserverCount = 0 [static]

Definition at line 436 of file nsAbOSXDirectory.mm.