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