2010-09-20 19:44:17 -05:00
//
// MGMContacts . m
// VoiceBase
//
// Created by Mr . Gecko on 8 / 18 / 10.
2011-03-07 19:02:53 -06:00
// Copyright ( c ) 2011 Mr . Gecko ' s Media ( James Coleman ) . http : // mrgeckosmedia . com /
//
// Permission to use , copy , modify , and / or distribute this software for any purpose
// with or without fee is hereby granted , provided that the above copyright notice
// and this permission notice appear in all copies .
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS . IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL , DIRECT , INDIRECT ,
// OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE ,
// DATA OR PROFITS , WHETHER IN AN ACTION OF CONTRACT , NEGLIGENCE OR OTHER TORTIOUS
// ACTION , ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE .
2010-09-20 19:44:17 -05:00
//
# import "MGMContacts.h"
# import "MGMContactsProtocol.h"
# import "MGMAddressBook.h"
# import "MGMGoogleContacts.h"
# import "MGMInstance.h"
# import "MGMAddons.h"
# import < MGMUsers / MGMUsers . h >
NSString * const MGMCContactsDB = @ "contacts.db" ;
NSString * const MGMCUpdateDB = @ "update.db" ;
NSString * const MGMCWordSA = @ "%@*" ;
NSString * const MGMCWordSSA = @ " %@*" ;
NSString * const MGMCWordS = @ " %@" ;
NSString * const MGMCQoute = @ "\" " ;
NSString * const MGMCNum = @ "%%%@%%" ;
NSString * const MGMCNEAR = @ "NEAR" ;
NSString * const MGMCAND = @ "AND" ;
NSString * const MGMCOR = @ "OR" ;
NSString * const MGMCNOT = @ "NOT" ;
NSString * const MGMCCNumber = @ "%@ (%@)" ;
NSString * const MGMTiffExt = @ "tif" ;
const int MGMCMaxResults = 10 ;
@ interface MGMContacts ( MGMPrivate )
- ( void ) updated ;
@ end
@ implementation MGMContacts
+ ( id ) contactsWithClass : ( Class ) theClass delegate : ( id ) theDelegate {
return [ [ [ self alloc ] initWithClass : theClass delegate : theDelegate ] autorelease ] ;
}
- ( id ) initWithClass : ( Class ) theClass delegate : ( id ) theDelegate {
2011-02-22 12:00:36 -06:00
if ( ( self = [ super init ] ) ) {
2010-09-20 19:44:17 -05:00
maxResults = MGMCMaxResults ;
delegate = theDelegate ;
user = [ delegate user ] ;
contactsLock = [ NSLock new ] ;
isUpdating = NO ;
stopingUpdate = NO ;
updateLock = [ NSLock new ] ;
if ( theClass = = NULL ) {
[ self release ] ;
self = nil ;
} else {
contacts = [ [ theClass alloc ] initWithDelegate : delegate ] ;
if ( contacts = = nil ) {
[ self release ] ;
self = nil ;
} else {
if ( [ [ NSFileManager defaultManager ] fileExistsAtPath : [ [ user supportPath ] stringByAppendingPathComponent : MGMCContactsDB ] ] ) {
[ self setContactsConnection : [ MGMLiteConnection connectionWithPath : [ [ user supportPath ] stringByAppendingPathComponent : MGMCContactsDB ] ] ] ;
}
[ [ NSNotificationCenter defaultCenter ] addObserver : self selector : @ selector ( userUpdated : ) name : MGMUserUpdatedNotification object : nil ] ;
}
}
}
return self ;
}
- ( void ) dealloc {
[ [ NSNotificationCenter defaultCenter ] removeObserver : self ] ;
2011-02-22 12:00:36 -06:00
[ updateLock lock ] ;
[ updateLock unlock ] ;
[ updateLock release ] ;
[ contactsLock lock ] ;
[ contactsLock unlock ] ;
[ contactsLock release ] ;
[ contactsConnection release ] ;
[ updateConnection release ] ;
[ contacts release ] ;
2010-09-20 19:44:17 -05:00
[ super dealloc ] ;
}
2010-09-24 15:35:47 -05:00
- ( void ) setDelegate : ( id ) theDelegate {
delegate = theDelegate ;
}
- ( id < MGMContactsOwnerDelegate > ) delegate {
return delegate ;
}
2010-09-20 19:44:17 -05:00
- ( void ) stop {
if ( isUpdating ) {
stopingUpdate = YES ;
[ contacts stop ] ;
[ updateLock lock ] ;
[ updateLock unlock ] ;
2011-02-22 12:00:36 -06:00
[ updateConnection release ] ;
updateConnection = nil ;
2010-09-20 19:44:17 -05:00
isUpdating = NO ;
stopingUpdate = NO ;
}
}
- ( void ) userUpdated : ( NSNotification * ) theNotification {
MGMUser * theUser = [ theNotification object ] ;
if ( [ theUser isEqual : user ] && ( ! [ [ theUser settingForKey : MGMSContactsSourceKey ] isEqual : NSStringFromClass ( [ contacts class ] ) ] || ( [ contacts isKindOfClass : [ MGMGoogleContacts class ] ] && ! [ [ [ ( MGMGoogleContacts * ) contacts user ] settingForKey : MGMUserID ] isEqual : [ theUser settingForKey : MGMCGoogleContactsUser ] ] ) ) ) {
if ( stopingUpdate ) return ;
[ self stop ] ;
2011-02-22 12:00:36 -06:00
[ contacts release ] ;
2010-09-20 19:44:17 -05:00
contacts = [ [ NSClassFromString ( [ theUser settingForKey : MGMSContactsSourceKey ] ) alloc ] initWithDelegate : delegate ] ;
[ self updateContacts ] ;
}
}
- ( void ) setMaxResults : ( int ) theMaxResults {
maxResults = theMaxResults ;
}
- ( int ) maxResults {
return maxResults ;
}
- ( MGMLiteConnection * ) contactsConnection {
return contactsConnection ;
}
- ( void ) setContactsConnection : ( MGMLiteConnection * ) theConnection {
[ contactsLock lock ] ;
2011-02-22 12:00:36 -06:00
[ contactsConnection release ] ;
2010-09-20 19:44:17 -05:00
contactsConnection = [ theConnection retain ] ;
[ contactsLock unlock ] ;
}
- ( void ) updateContacts {
if ( isUpdating )
return ;
isUpdating = YES ;
[ updateLock lock ] ;
2011-02-24 11:24:19 -06:00
NSFileManager * manager = [ NSFileManager defaultManager ] ;
if ( [ manager fileExistsAtPath : [ [ user supportPath ] stringByAppendingPathComponent : MGMCUpdateDB ] ] )
[ manager removeItemAtPath : [ [ user supportPath ] stringByAppendingPathComponent : MGMCUpdateDB ] ] ;
2010-09-20 19:44:17 -05:00
updateConnection = [ [ MGMLiteConnection connectionWithPath : [ [ user supportPath ] stringByAppendingPathComponent : MGMCUpdateDB ] ] retain ] ;
if ( updateConnection = = nil ) {
[ self contactsError : nil ] ;
}
# if MGMContactsDebug
NSLog ( @ "Getting Contacts" ) ;
[ updateConnection setLogQuery : YES ] ;
# endif
2013-07-20 06:56:17 -05:00
[ updateConnection query : @ "CREATE VIRTUAL TABLE contacts USING fts3(name, company, number INTEGER, label, photo)" ] ;
2010-09-20 19:44:17 -05:00
[ updateConnection query : @ "CREATE TABLE groups (docid INTEGER PRIMARY KEY AUTOINCREMENT, name)" ] ;
[ updateConnection query : @ "CREATE TABLE groupMembers (docid INTEGER PRIMARY KEY AUTOINCREMENT, groupid INTEGER, contactid INTEGER)" ] ;
[ updateLock unlock ] ;
[ contacts getContacts : self ] ;
}
- ( void ) gotContact : ( NSDictionary * ) theContact {
NSAutoreleasePool * pool = [ NSAutoreleasePool new ] ;
[ updateLock lock ] ;
if ( updateConnection ! = nil ) {
[ updateConnection query : @ "INSERT INTO contacts (name, company, number, label, photo) VALUES (%@, %@, %@, %@, %@)" , [ theContact objectForKey : MGMCName ] , [ theContact objectForKey : MGMCCompany ] , [ theContact objectForKey : MGMCNumber ] , [ theContact objectForKey : MGMCLabel ] , [ theContact objectForKey : MGMCPhoto ] ] ;
# if MGMContactsDebug
if ( [ updateConnection errorID ] ! = 0 )
NSLog ( @ "%@ %@" , [ updateConnection errorMessage ] , theContact ) ;
# endif
}
[ updateLock unlock ] ;
[ pool drain ] ;
}
- ( void ) doneGettingContacts {
NSAutoreleasePool * pool = [ NSAutoreleasePool new ] ;
[ contacts getGroups : self ] ;
# if MGMContactsDebug
NSLog ( @ "Finished Adding Contacts" ) ;
# endif
[ pool drain ] ;
}
- ( void ) contactsError : ( NSError * ) theError {
NSAutoreleasePool * pool = [ NSAutoreleasePool new ] ;
NSLog ( @ "MGMContacts Error: %@" , theError ) ;
isUpdating = NO ;
[ pool drain ] ;
}
- ( void ) gotGroup : ( NSString * ) theName withMembers : ( NSArray * ) theMembers {
NSAutoreleasePool * pool = [ NSAutoreleasePool new ] ;
[ updateLock lock ] ;
if ( updateConnection ! = nil ) {
[ updateConnection query : @ "INSERT INTO groups (name) VALUES (%@)" , theName ] ;
long long int groupID = [ updateConnection insertId ] ;
for ( unsigned int i = 0 ; i < [ theMembers count ] ; i + + ) {
NSDictionary * result = [ [ updateConnection query : @ "SELECT docid, * FROM contacts WHERE number = %@" , [ theMembers objectAtIndex : i ] ] nextRow ] ;
2011-02-22 12:00:36 -06:00
if ( result ! = nil )
2010-09-20 19:44:17 -05:00
[ updateConnection query : @ "INSERT INTO groupMembers (groupid, contactid) VALUES (%qi, %@)" , groupID , [ result objectForKey : MGMCDocID ] ] ;
}
}
[ updateLock unlock ] ;
[ pool drain ] ;
}
- ( void ) doneGettingGroups {
NSAutoreleasePool * pool = [ NSAutoreleasePool new ] ;
[ self updated ] ;
# if MGMContactsDebug
NSLog ( @ "Finished Adding Groups" ) ;
# endif
[ pool drain ] ;
}
- ( void ) groupsError : ( NSError * ) theError {
NSAutoreleasePool * pool = [ NSAutoreleasePool new ] ;
NSLog ( @ "MGMContacts Group Error: %@" , theError ) ;
[ self updated ] ;
[ pool drain ] ;
}
- ( void ) updated {
[ updateLock lock ] ;
[ self setContactsConnection : nil ] ;
2011-02-24 11:24:19 -06:00
NSFileManager * manager = [ NSFileManager defaultManager ] ;
if ( [ manager fileExistsAtPath : [ [ user supportPath ] stringByAppendingPathComponent : MGMCContactsDB ] ] )
[ manager removeItemAtPath : [ [ user supportPath ] stringByAppendingPathComponent : MGMCContactsDB ] ] ;
[ manager moveItemAtPath : [ [ user supportPath ] stringByAppendingPathComponent : MGMCUpdateDB ] toPath : [ [ user supportPath ] stringByAppendingPathComponent : MGMCContactsDB ] ] ;
2010-09-20 19:44:17 -05:00
[ self setContactsConnection : [ MGMLiteConnection connectionWithPath : [ [ user supportPath ] stringByAppendingPathComponent : MGMCContactsDB ] ] ] ;
[ updateLock unlock ] ;
isUpdating = NO ;
2010-09-24 15:35:47 -05:00
if ( delegate ! = nil && [ delegate respondsToSelector : @ selector ( updatedContacts ) ] ) [ delegate updatedContacts ] ;
2010-09-20 19:44:17 -05:00
}
- ( NSNumber * ) countContactsMatching : ( NSString * ) theString {
if ( contactsConnection = = nil )
return [ NSNumber numberWithInt : 0 ] ;
[ contactsLock lock ] ;
NSAutoreleasePool * pool = [ NSAutoreleasePool new ] ;
NSString * string = [ theString stringByTrimmingCharactersInSet : [ NSCharacterSet whitespaceCharacterSet ] ] ;
MGMLiteResult * result ;
if ( theString = = nil || [ string isEqual : @ "" ] ) {
result = [ contactsConnection query : @ "SELECT COUNT(docid) AS count FROM contacts" ] ;
} else {
if ( [ string isPhone ] ) {
NSString * search = [ NSString stringWithFormat : MGMCNum , [ [ string removePhoneWhiteSpace ] littersToNumbers ] ] ;
result = [ contactsConnection query : @ "SELECT COUNT(docid) AS count FROM contacts WHERE number LIKE %@" , search ] ;
} else {
NSArray * words = [ string componentsSeparatedByString : @ " " ] ;
NSMutableString * search = [ NSMutableString string ] ;
BOOL quote = NO ;
for ( int i = 0 ; i < [ words count ] ; i + + ) {
NSString * word = [ words objectAtIndex : i ] ;
if ( quote ) {
quote = ! [ word hasSuffix : MGMCQoute ] ;
if ( i = = 0 )
[ search appendString : word ] ;
else
[ search appendFormat : MGMCWordS , word ] ;
} else {
quote = [ word hasPrefix : MGMCQoute ] ;
if ( quote ) {
i - - ;
continue ;
}
if ( i = = 0 )
[ search appendFormat : MGMCWordSA , word ] ;
else {
if ( [ word hasPrefix : MGMCNEAR ] || [ word isEqual : MGMCAND ] || [ word isEqual : MGMCOR ] || [ word isEqual : MGMCNOT ] ) {
[ search appendFormat : MGMCWordS , word ] ;
} else {
[ search appendFormat : MGMCWordSSA , word ] ;
}
}
}
}
result = [ contactsConnection query : @ "SELECT COUNT(docid) AS count FROM contacts WHERE contacts MATCH %@" , search ] ;
}
}
NSNumber * count = [ [ [ result nextRow ] objectForKey : @ "count" ] copy ] ;
[ contactsLock unlock ] ;
[ pool drain ] ;
return [ count autorelease ] ;
}
- ( NSArray * ) contactsMatching : ( NSString * ) theString page : ( int ) thePage {
2011-09-11 14:54:29 -05:00
return [ self contactsMatching : theString page : thePage includePhoto : YES ] ;
}
- ( NSArray * ) contactsMatching : ( NSString * ) theString page : ( int ) thePage includePhoto : ( BOOL ) shouldIncludePhoto {
2010-09-20 19:44:17 -05:00
if ( contactsConnection = = nil )
return nil ;
[ contactsLock lock ] ;
2011-09-11 14:54:29 -05:00
NSString * select = nil ;
if ( shouldIncludePhoto )
select = @ "SELECT docid, name, company, number, label, photo" ;
else
select = @ "SELECT docid, name, company, number, label" ;
2010-09-20 19:44:17 -05:00
NSMutableArray * contactsArray = [ NSMutableArray array ] ;
NSAutoreleasePool * pool = [ NSAutoreleasePool new ] ;
NSString * string = [ theString stringByTrimmingCharactersInSet : [ NSCharacterSet whitespaceCharacterSet ] ] ;
MGMLiteResult * result ;
if ( thePage = = 0 ) thePage = 1 ;
long long int page = ( thePage * maxResults ) - maxResults ;
if ( theString = = nil || [ string isEqual : @ "" ] ) {
2011-09-11 14:54:29 -05:00
result = [ contactsConnection query : [ select stringByAppendingString : @ " FROM contacts ORDER BY company, name LIMIT %qi, %d" ] , page , maxResults ] ;
2010-09-20 19:44:17 -05:00
} else {
if ( [ string isPhone ] ) {
NSString * search = [ NSString stringWithFormat : MGMCNum , [ [ string removePhoneWhiteSpace ] littersToNumbers ] ] ;
2011-09-11 14:54:29 -05:00
result = [ contactsConnection query : [ select stringByAppendingString : @ " FROM contacts WHERE number LIKE %@ ORDER BY company, name LIMIT %qi, %d" ] , search , page , maxResults ] ;
2010-09-20 19:44:17 -05:00
} else {
NSArray * words = [ string componentsSeparatedByString : @ " " ] ;
NSMutableString * search = [ NSMutableString string ] ;
BOOL quote = NO ;
for ( int i = 0 ; i < [ words count ] ; i + + ) {
NSString * word = [ words objectAtIndex : i ] ;
if ( quote ) {
quote = ! [ word hasSuffix : MGMCQoute ] ;
if ( i = = 0 )
[ search appendString : word ] ;
else
[ search appendFormat : MGMCWordS , word ] ;
} else {
quote = [ word hasPrefix : MGMCQoute ] ;
if ( quote ) {
i - - ;
continue ;
}
if ( i = = 0 )
[ search appendFormat : MGMCWordSA , word ] ;
else {
if ( [ word hasPrefix : MGMCNEAR ] || [ word isEqual : MGMCAND ] || [ word isEqual : MGMCOR ] || [ word isEqual : MGMCNOT ] ) {
[ search appendFormat : MGMCWordS , word ] ;
} else {
[ search appendFormat : MGMCWordSSA , word ] ;
}
}
}
}
2011-09-11 14:54:29 -05:00
result = [ contactsConnection query : [ select stringByAppendingString : @ ", offsets(contacts) AS offset FROM contacts WHERE contacts MATCH %@ ORDER BY offset LIMIT %qi, %d" ] , search , page , maxResults ] ;
2010-09-20 19:44:17 -05:00
}
}
NSDictionary * contact = nil ;
while ( ( contact = [ result nextRow ] ) ! = nil ) {
[ contactsArray addObject : contact ] ;
}
[ contactsLock unlock ] ;
[ pool drain ] ;
return contactsArray ;
}
- ( NSArray * ) contactCompletionsMatching : ( NSString * ) theString {
if ( contactsConnection = = nil )
return nil ;
[ contactsLock lock ] ;
NSMutableArray * completions = [ NSMutableArray array ] ;
NSAutoreleasePool * pool = [ NSAutoreleasePool new ] ;
NSString * string = [ theString stringByTrimmingCharactersInSet : [ NSCharacterSet whitespaceCharacterSet ] ] ;
if ( theString = = nil || [ string isEqual : @ "" ] ) {
[ pool drain ] ;
return completions ;
} else {
if ( [ string isPhone ] ) {
for ( int i = 0 ; i < 2 ; i + + ) {
NSString * search = [ NSString stringWithFormat : @ "%@%%" , ( i = = 0 ? [ string phoneFormat ] : [ string phoneFormatAreaCode : [ delegate areaCode ] ] ) ] ;
2011-09-11 14:54:29 -05:00
MGMLiteResult * result = [ contactsConnection query : @ "SELECT name, company, number FROM contacts WHERE number LIKE %@ ORDER BY company, name LIMIT %d" , search , maxResults ] ;
2010-09-20 19:44:17 -05:00
NSDictionary * contact = nil ;
while ( ( contact = [ result nextRow ] ) ! = nil ) {
NSString * completion = nil ;
NSString * number = [ contact objectForKey : MGMCNumber ] ;
if ( i = = 0 ) {
if ( ! [ string hasPrefix : @ "+1" ] && ! [ string hasPrefix : @ "011" ] ) {
if ( [ number hasPrefix : @ "+1" ] )
number = [ number substringFromIndex : 2 ] ;
}
} else {
number = [ number substringFromIndex : 5 ] ;
}
if ( [ contact objectForKey : MGMCName ] ! = nil && ! [ [ contact objectForKey : MGMCName ] isEqual : @ "" ] ) {
completion = [ NSString stringWithFormat : MGMCCNumber , number , [ contact objectForKey : MGMCName ] ] ;
} else if ( [ contact objectForKey : MGMCCompany ] ! = nil && ! [ [ contact objectForKey : MGMCCompany ] isEqual : @ "" ] ) {
completion = [ NSString stringWithFormat : MGMCCNumber , number , [ contact objectForKey : MGMCCompany ] ] ;
} else {
completion = number ;
}
[ completions addObject : completion ] ;
}
}
} else {
NSArray * words = [ string componentsSeparatedByString : @ " " ] ;
NSMutableString * search = [ NSMutableString string ] ;
for ( int i = 0 ; i < [ words count ] ; i + + ) {
NSString * word = [ words objectAtIndex : i ] ;
if ( i = = 0 )
[ search appendFormat : MGMCWordSA , word ] ;
else {
[ search appendFormat : MGMCWordSSA , word ] ;
}
}
2011-09-16 17:04:03 -05:00
MGMLiteResult * result = [ contactsConnection query : @ "SELECT name, company, label, number, offsets(contacts) AS offset FROM contacts WHERE contacts MATCH %@ ORDER BY offset LIMIT %d" , search , maxResults ] ;
2010-09-20 19:44:17 -05:00
NSDictionary * contact = nil ;
while ( ( contact = [ result nextRow ] ) ! = nil ) {
NSString * completion = nil ;
if ( ( [ contact objectForKey : MGMCName ] ! = nil && ! [ [ contact objectForKey : MGMCName ] isEqual : @ "" ] ) || ( [ contact objectForKey : MGMCCompany ] ! = nil && ! [ [ contact objectForKey : MGMCCompany ] isEqual : @ "" ] ) ) {
NSArray * words = [ string componentsSeparatedByString : @ " " ] ;
NSString * name = nil ;
if ( [ contact objectForKey : MGMCName ] ! = nil && ! [ [ contact objectForKey : MGMCName ] isEqual : @ "" ] )
name = [ contact objectForKey : MGMCName ] ;
else
name = [ contact objectForKey : MGMCCompany ] ;
if ( ! [ name hasPrefix : [ words objectAtIndex : 0 ] ] ) {
NSMutableArray * nameArray = [ NSMutableArray arrayWithArray : [ name componentsSeparatedByString : @ " " ] ] ;
for ( int i = 0 ; i < [ nameArray count ] ; i + + ) {
if ( [ [ nameArray objectAtIndex : i ] hasPrefix : [ words objectAtIndex : 0 ] ] ) {
name = [ nameArray objectAtIndex : i ] ;
[ nameArray removeObjectAtIndex : i ] ;
break ;
}
}
for ( int i = 0 ; i < [ nameArray count ] ; i + + ) {
name = [ name stringByAppendingFormat : MGMCWordS , [ nameArray objectAtIndex : i ] ] ;
}
}
2011-09-16 17:04:03 -05:00
if ( [ [ contact objectForKey : MGMCLabel ] isEqual : @ "" ] )
completion = [ NSString stringWithFormat : @ "%@ <%@>" , name , [ [ contact objectForKey : MGMCNumber ] readableNumber ] ] ;
else
completion = [ NSString stringWithFormat : @ "%@ <%@> (%@)" , name , [ [ contact objectForKey : MGMCNumber ] readableNumber ] , [ contact objectForKey : MGMCLabel ] ] ;
2010-09-20 19:44:17 -05:00
} else {
completion = [ [ contact objectForKey : MGMCNumber ] readableNumber ] ;
}
[ completions addObject : completion ] ;
}
}
}
[ contactsLock unlock ] ;
[ pool drain ] ;
return completions ;
}
2011-09-11 14:54:29 -05:00
2010-09-20 19:44:17 -05:00
- ( NSDictionary * ) contactWithID : ( NSNumber * ) theID {
if ( contactsConnection = = nil )
return nil ;
return [ [ contactsConnection query : @ "SELECT docid, * FROM contacts WHERE docid = %@" , theID ] nextRow ] ;
}
- ( NSData * ) photoDataForNumber : ( NSString * ) theNumber {
NSDictionary * contact = [ [ contactsConnection query : @ "SELECT photo FROM contacts WHERE number = %@ AND photo NOT NULL" , theNumber ] nextRow ] ;
if ( contact ! = nil )
return [ contact objectForKey : MGMCPhoto ] ;
return nil ;
}
- ( NSString * ) cachedPhotoForNumber : ( NSString * ) theNumber {
NSFileManager * manager = [ NSFileManager defaultManager ] ;
# if ! TARGET_OS _IPHONE
if ( [ delegate respondsToSelector : @ selector ( userNumber ) ] && [ [ delegate userNumber ] isEqual : theNumber ] ) {
if ( ! [ manager fileExistsAtPath : [ [ MGMUser cachePath ] stringByAppendingPathComponent : [ theNumber stringByAppendingPathExtension : MGMTiffExt ] ] ] ) {
NSData * photo = [ MGMAddressBook userPhotoData ] ;
if ( photo ! = nil ) {
[ photo writeToFile : [ [ MGMUser cachePath ] stringByAppendingPathComponent : [ theNumber stringByAppendingPathExtension : MGMTiffExt ] ] atomically : YES ] ;
return [ [ MGMUser cachePath ] stringByAppendingPathComponent : [ theNumber stringByAppendingPathExtension : MGMTiffExt ] ] ;
}
} else {
return [ [ MGMUser cachePath ] stringByAppendingPathComponent : [ theNumber stringByAppendingPathExtension : MGMTiffExt ] ] ;
}
}
# endif
if ( ! [ manager fileExistsAtPath : [ [ MGMUser cachePath ] stringByAppendingPathComponent : [ theNumber stringByAppendingPathExtension : MGMTiffExt ] ] ] ) {
NSData * photo = [ self photoDataForNumber : theNumber ] ;
if ( photo ! = nil ) {
[ photo writeToFile : [ [ MGMUser cachePath ] stringByAppendingPathComponent : [ theNumber stringByAppendingPathExtension : MGMTiffExt ] ] atomically : YES ] ;
return [ [ MGMUser cachePath ] stringByAppendingPathComponent : [ theNumber stringByAppendingPathExtension : MGMTiffExt ] ] ;
}
} else {
return [ [ MGMUser cachePath ] stringByAppendingPathComponent : [ theNumber stringByAppendingPathExtension : MGMTiffExt ] ] ;
}
return nil ;
}
- ( NSString * ) nameForNumber : ( NSString * ) theNumber {
NSDictionary * contact = [ [ contactsConnection query : @ "SELECT name, company FROM contacts WHERE number = %@ AND (name != '' OR company != '')" , theNumber ] nextRow ] ;
if ( contact ! = nil ) {
if ( ! [ [ contact objectForKey : MGMCName ] isEqual : @ "" ] )
return [ contact objectForKey : MGMCName ] ;
else if ( ! [ [ contact objectForKey : MGMCCompany ] isEqual : @ "" ] )
return [ contact objectForKey : MGMCCompany ] ;
}
2011-02-22 12:00:36 -06:00
if ( [ theNumber isPhoneComplete ] )
2010-10-16 20:32:55 -05:00
return [ theNumber readableNumber ] ;
return theNumber ;
2010-09-20 19:44:17 -05:00
}
- ( NSArray * ) groups {
if ( contactsConnection = = nil )
return nil ;
[ contactsLock lock ] ;
NSMutableArray * groupsArray = [ NSMutableArray array ] ;
NSAutoreleasePool * pool = [ NSAutoreleasePool new ] ;
MGMLiteResult * result = [ contactsConnection query : @ "SELECT * FROM groups ORDER BY name" ] ;
NSDictionary * group = nil ;
while ( ( group = [ result nextRow ] ) ! = nil ) {
[ groupsArray addObject : group ] ;
}
[ contactsLock unlock ] ;
[ pool drain ] ;
return groupsArray ;
}
- ( NSDictionary * ) groupWithID : ( NSNumber * ) theID {
if ( contactsConnection = = nil )
return nil ;
return [ [ contactsConnection query : @ "SELECT * FROM groups WHERE docid = %@" , theID ] nextRow ] ;
}
- ( NSNumber * ) membersCountOfGroup : ( NSDictionary * ) theGroup {
return [ self membersCountOfGroupID : [ theGroup objectForKey : MGMCDocID ] ] ;
}
- ( NSNumber * ) membersCountOfGroupID : ( NSNumber * ) theGroup {
if ( contactsConnection = = nil )
return nil ;
[ contactsLock lock ] ;
NSAutoreleasePool * pool = [ NSAutoreleasePool new ] ;
MGMLiteResult * result = [ contactsConnection query : @ "SELECT COUNT(docid) AS count FROM groupMembers WHERE groupid = %@" , theGroup ] ;
NSNumber * count = [ [ [ result nextRow ] objectForKey : @ "count" ] copy ] ;
[ contactsLock unlock ] ;
[ pool drain ] ;
return [ count autorelease ] ;
}
- ( NSArray * ) membersOfGroup : ( NSDictionary * ) theGroup {
return [ self membersOfGroupID : [ theGroup objectForKey : MGMCDocID ] ] ;
}
- ( NSArray * ) membersOfGroupID : ( NSNumber * ) theGroup {
if ( contactsConnection = = nil )
return nil ;
[ contactsLock lock ] ;
NSMutableArray * memebersArray = [ NSMutableArray array ] ;
NSAutoreleasePool * pool = [ NSAutoreleasePool new ] ;
MGMLiteResult * result = [ contactsConnection query : @ "SELECT * FROM groupMembers WHERE groupid = %@" , theGroup ] ;
NSDictionary * member = nil ;
while ( ( member = [ result nextRow ] ) ! = nil ) {
NSDictionary * contact = [ self contactWithID : [ member objectForKey : MGMCContactID ] ] ;
2011-02-22 12:00:36 -06:00
if ( contact ! = nil )
2010-09-20 19:44:17 -05:00
[ memebersArray addObject : contact ] ;
}
[ contactsLock unlock ] ;
[ pool drain ] ;
return memebersArray ;
}
- ( NSArray * ) groupsOfContact : ( NSDictionary * ) theContact {
if ( contactsConnection = = nil )
return nil ;
[ contactsLock lock ] ;
NSMutableArray * groupsArray = [ NSMutableArray array ] ;
NSAutoreleasePool * pool = [ NSAutoreleasePool new ] ;
MGMLiteResult * result = [ contactsConnection query : @ "SELECT * FROM groupMembers WHERE contactid = %@" , [ theContact objectForKey : MGMCDocID ] ] ;
NSDictionary * groupMember = nil ;
while ( ( groupMember = [ result nextRow ] ) ! = nil ) {
NSDictionary * group = [ self groupWithID : [ groupMember objectForKey : MGMCGroupID ] ] ;
if ( group ! = nil )
[ groupsArray addObject : group ] ;
}
[ contactsLock unlock ] ;
[ pool drain ] ;
return groupsArray ;
}
@ end