1/* ABQuery
2 *
3 *    Copyright 2003 Brendan Cully <brendan@kublai.com>
4 *
5 *    This program is free software; you can redistribute it and/or modify
6 *    it under the terms of the GNU General Public License as published by
7 *     the Free Software Foundation; either version 2 of the License, or
8 *     (at your option) any later version.
9 *
10 *     This program is distributed in the hope that it will be useful,
11 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 *     GNU General Public License for more details.
14 *
15 *     You should have received a copy of the GNU General Public License
16 *     along with this program; if not, write to the Free Software Foundation,
17 *     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,, USA.
18 */
19
20#import <Foundation/Foundation.h>
21#import <AddressBook/AddressBook.h>
22
23int main (int argc, const char *argv[]) {
24    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
25    ABAddressBook *book = [ABAddressBook sharedAddressBook];
26    ABSearchElement *firstNameSearch, *lastNameSearch, *emailSearch, *search;
27    NSArray *searchTerms;
28    NSArray *results;
29    NSEnumerator *addressEnum;
30    ABPerson *person;
31    NSString *key = [NSString stringWithCString:argv[1]];
32
33    firstNameSearch = [ABPerson searchElementForProperty:kABFirstNameProperty
34		                  label:nil
35		                  key:nil
36				  value:key
37                                  comparison:kABContainsSubStringCaseInsensitive];
38    lastNameSearch = [ABPerson searchElementForProperty:kABLastNameProperty
39		                  label:nil
40		                  key:nil
41				  value:key
42                                  comparison:kABContainsSubStringCaseInsensitive];
43    emailSearch = [ABPerson searchElementForProperty:kABEmailProperty
44                              label:nil
45			      key:nil
46			      value:key
47			      comparison:kABContainsSubStringCaseInsensitive];
48    searchTerms = [NSArray arrayWithObjects:firstNameSearch, lastNameSearch, emailSearch, nil];
49    search = [ABSearchElement searchElementForConjunction:kABSearchOr
50                                children:searchTerms];
51    results = [book recordsMatchingSearchElement:search];
52
53    addressEnum = [results objectEnumerator];
54
55    while (person = (ABPerson*)[addressEnum nextObject]) {
56        NSString *fullName = [NSString stringWithFormat:@"%@ %@", [[person valueForProperty:kABFirstNameProperty] description], [[person valueForProperty:kABLastNameProperty] description]];
57
58        ABMultiValue *emails = [person valueForProperty:kABEmailProperty];
59        int count = [emails count];
60        int i;
61        for (i = 0; i < count; i++) {
62            NSString *email = [emails valueAtIndex:i];
63            printf("%s\t%s\t(AddressBook)\n", [email cString], [fullName UTF8String]);
64      }
65    }
66
67    [pool release];
68
69    return 0;
70}
71