-
Notifications
You must be signed in to change notification settings - Fork 106
/
SNWhitelistViewController.m
656 lines (568 loc) · 26.3 KB
/
SNWhitelistViewController.m
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
#import "SNWhitelistViewController.h"
#import "SNNumberViewController.h"
#import "SNContentViewController.h"
/*
#import "SNSystemMessageHistoryViewController.h"
#import "SNSystemCallHistoryViewController.h"
*/
#import "SNMainViewController.h"
#import <objc/runtime.h>
#import <notify.h>
#import <sqlite3.h>
#ifndef SMSNinjaDebug
#define SETTINGS @"/var/mobile/Library/SMSNinja/smsninja.plist"
#define DATABASE @"/var/mobile/Library/SMSNinja/smsninja.db"
#else
#define SETTINGS @"/Users/snakeninny/Library/Application Support/iPhone Simulator/7.0.3/Applications/0C9D35FB-B626-42B7-AAE9-45F6F537890B/Documents/var/mobile/Library/SMSNinja/smsninja.plist"
#define DATABASE @"/Users/snakeninny/Library/Application Support/iPhone Simulator/7.0.3/Applications/0C9D35FB-B626-42B7-AAE9-45F6F537890B/Documents/var/mobile/Library/SMSNinja/smsninja.db"
#endif
static int amount;
@implementation SNWhitelistViewController
@synthesize chosenName;
@synthesize chosenKeyword;
- (void)dealloc
{
[keywordArray release];
keywordArray = nil;
[typeArray release];
typeArray = nil;
[nameArray release];
nameArray = nil;
[chosenName release];
chosenName = nil;
[chosenKeyword release];
chosenKeyword = nil;
[super dealloc];
}
- (void)loadDatabaseSegment
{
sqlite3 *database;
sqlite3_stmt *statement;
int openResult = sqlite3_open([DATABASE UTF8String], &database);
if (openResult == SQLITE_OK)
{
NSString *sql = [NSString stringWithFormat:@"select keyword, type, name from whitelist limit %lu, 50", (unsigned long)[keywordArray count]];
int prepareResult = sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL);
if (prepareResult == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
char *keyword = (char *)sqlite3_column_text(statement, 0);
[keywordArray addObject:keyword ? @(keyword) : @""];
char *type = (char *)sqlite3_column_text(statement, 1);
[typeArray addObject:type ? @(type) : @""];
char *name = (char *)sqlite3_column_text(statement, 2);
[nameArray addObject:name ? @(name) : @""];
}
sqlite3_finalize(statement);
}
else NSLog(@"SMSNinja: Failed to prepare %@, error %d", sql, prepareResult);
sql = @"select count(*) from whitelist";
prepareResult = sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL);
if (prepareResult == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
char *count = (char *)sqlite3_column_text(statement, 0);
amount = atoi(count);
}
sqlite3_finalize(statement);
}
else NSLog(@"SMSNinja: Failed to prepare %@, error %d", sql, prepareResult);
sqlite3_close(database);
}
else NSLog(@"SMSNinja: Failed to open %@, error %d", DATABASE, openResult);
}
- (instancetype)init
{
if ((self = [super initWithStyle:UITableViewStylePlain]))
{
self.navigationItem.rightBarButtonItem = self.editButtonItem;
keywordArray = [[NSMutableArray alloc] initWithCapacity:600];
typeArray = [[NSMutableArray alloc] initWithCapacity:600];
nameArray = [[NSMutableArray alloc] initWithCapacity:600];
chosenName = [[NSString alloc] initWithFormat:@""];
chosenKeyword = [[NSString alloc] initWithFormat:@""];
[self loadDatabaseSegment];
}
return self;
}
- (void)segmentAction:(UISegmentedControl *)sender
{
if (sender.selectedSegmentIndex == 1)
{
SNBlacklistViewController *blacklistViewController = [[SNBlacklistViewController alloc] init];
UINavigationController *navigationController = self.navigationController;
[navigationController popViewControllerAnimated:NO];
[navigationController pushViewController:blacklistViewController animated:NO];
[blacklistViewController release];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"White", @"White"), NSLocalizedString(@"Black", @"Black"), nil]];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0.0f, 0.0f, 100.0f, 30.0f);
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[typeArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // NumberViewController
{
SNNumberViewController *numberViewController = [[SNNumberViewController alloc] init];
numberViewController.flag = @"white";
numberViewController.nameString = [nameArray objectAtIndex:indexPath.row];
numberViewController.keywordString = [keywordArray objectAtIndex:indexPath.row];
numberViewController.originalKeyword = numberViewController.keywordString;
numberViewController.phoneAction = @"1";
numberViewController.messageAction = @"1";
numberViewController.replyString = @"0";
numberViewController.messageString = @"";
numberViewController.forwardString = @"0";
numberViewController.numberString = @"";
numberViewController.soundString = @"0";
[self.navigationController pushViewController:numberViewController animated:YES];
[numberViewController release];
}
else if ([[typeArray objectAtIndex:indexPath.row] isEqualToString:@"1"]) // ContentViewController
{
SNContentViewController *contentViewController = [[SNContentViewController alloc] init];
contentViewController.flag = @"white";
contentViewController.nameString = [nameArray objectAtIndex:indexPath.row];
contentViewController.keywordString = [keywordArray objectAtIndex:indexPath.row];
contentViewController.originalKeyword = contentViewController.keywordString;
contentViewController.replyString = @"0";
contentViewController.messageString = @"";
contentViewController.forwardString = @"0";
contentViewController.numberString = @"";
contentViewController.soundString = @"0";
[self.navigationController pushViewController:contentViewController animated:YES];
[contentViewController release];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [keywordArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"any-cell"];
if (cell == nil) cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"any-cell"] autorelease];
for (UIView *subview in [cell.contentView subviews]) [subview removeFromSuperview];
cell.textLabel.text = nil;
cell.accessoryView = nil;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 2.0f, cell.contentView.bounds.size.width - 50.0f, (cell.contentView.bounds.size.height - 4.0f) / 2.0f)];
nameLabel.text = [nameArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:nameLabel];
[nameLabel release];
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(nameLabel.frame.origin.x, nameLabel.frame.origin.y + nameLabel.frame.size.height, nameLabel.bounds.size.width, nameLabel.bounds.size.height)];
contentLabel.text = [keywordArray objectAtIndex:indexPath.row];
[cell.contentView addSubview:contentLabel];
[contentLabel release];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return NSLocalizedString(@"Whitelist", @"Whitelist");
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSSet *deleteSet = [NSSet setWithObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
sqlite3 *database;
int openResult = sqlite3_open([DATABASE UTF8String], &database);
if (openResult == SQLITE_OK)
{
for (NSIndexPath *chosenRowIndexPath in deleteSet)
{
NSString *sql = [NSString stringWithFormat:@"delete from whitelist where keyword = '%@' and type = '%@' and name = '%@'", [keywordArray objectAtIndex:chosenRowIndexPath.row], [typeArray objectAtIndex:chosenRowIndexPath.row], [[nameArray objectAtIndex:chosenRowIndexPath.row] stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];
int execResult = sqlite3_exec(database, [sql UTF8String], NULL, NULL, NULL);
if (execResult != SQLITE_OK) NSLog(@"SMSNinja: Failed to exec %@, error %d", sql, execResult);
}
sqlite3_close(database);
notify_post("com.naken.smsninja.whitelistchanged");
}
else NSLog(@"SMSNinja: Failed to open %@, error %d", DATABASE, openResult);
NSMutableIndexSet *discardedItems = [NSMutableIndexSet indexSet];
for (NSIndexPath *chosenRowIndexPath in deleteSet) [discardedItems addIndex:chosenRowIndexPath.row];
[keywordArray removeObjectsAtIndexes:discardedItems];
[typeArray removeObjectsAtIndexes:discardedItems];
[nameArray removeObjectsAtIndexes:discardedItems];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[deleteSet allObjects] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
- (void)gotoNumberView
{
SNNumberViewController *numberViewController = [[SNNumberViewController alloc] init];
numberViewController.flag = @"white";
numberViewController.nameString = @"";
numberViewController.keywordString = @"";
numberViewController.originalKeyword = numberViewController.keywordString;
numberViewController.phoneAction = @"1";
numberViewController.messageAction = @"1";
numberViewController.replyString = @"0";
numberViewController.messageString = @"";
numberViewController.forwardString = @"0";
numberViewController.numberString = @"";
numberViewController.soundString = @"0";
[self.navigationController pushViewController:numberViewController animated:YES];
[numberViewController release];
}
- (void)gotoContentView
{
SNContentViewController *contentViewController = [[SNContentViewController alloc] init];
contentViewController.flag = @"white";
contentViewController.nameString = @"";
contentViewController.keywordString = @"";
contentViewController.originalKeyword = contentViewController.keywordString;
contentViewController.replyString = @"0";
contentViewController.messageString = @"";
contentViewController.forwardString = @"0";
contentViewController.numberString = @"";
contentViewController.soundString = @"0";
[self.navigationController pushViewController:contentViewController animated:YES];
[contentViewController release];
}
- (void)gotoSystemCallHistoryView
{
CPDistributedMessagingCenter *messagingCenter = [objc_getClass("CPDistributedMessagingCenter") centerNamed:@"com.naken.smsninja.springboard"];
[messagingCenter sendMessageName:@"LaunchMobilePhone" userInfo:nil];
/*
SNSystemCallHistoryViewController *systemCallHistoryViewController = [[SNSystemCallHistoryViewController alloc] init];
systemCallHistoryViewController.flag = @"white";
[self.navigationController pushViewController:systemCallHistoryViewController animated:YES];
[systemCallHistoryViewController release];
*/
}
- (void)gotoSystemMessageHistoryView
{
CPDistributedMessagingCenter *messagingCenter = [objc_getClass("CPDistributedMessagingCenter") centerNamed:@"com.naken.smsninja.springboard"];
[messagingCenter sendMessageName:@"LaunchMobileSMS" userInfo:nil];
/*
SNSystemMessageHistoryViewController *systemMessageHistoryViewController = [[SNSystemMessageHistoryViewController alloc] init];
systemMessageHistoryViewController.flag = @"white";
[self.navigationController pushViewController:systemMessageHistoryViewController animated:YES];
[systemMessageHistoryViewController release];
*/
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case 0:
{
[self gotoNumberView];
break;
}
case 1:
{
[self gotoContentView];
break;
}
case 2:
{
[self gotoAddressbook];
break;
}
case 3:
{
[self gotoSystemCallHistoryView];
break;
}
case 4:
{
[self gotoSystemMessageHistoryView];
break;
}
}
}
- (void)addRecord
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel") destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Enter numbers", @"Enter numbers"), NSLocalizedString(@"Enter keywords", @"Enter keywords"), NSLocalizedString(@"From addressbook", @"From addressbook"), NSLocalizedString(@"From call history", @"From call history"), NSLocalizedString(@"From message history", @"From message history"), nil];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet release];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
if (editing) [self.navigationItem setLeftBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Add", @"Add") style:UIBarButtonItemStyleBordered target:self action:@selector(addRecord)] autorelease] animated:YES];
else [self.navigationItem setLeftBarButtonItem:nil animated:animated];
[super setEditing:editing animated:animated];
}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
CFIndex emailCounts = 0;
ABPropertyID property = kABPersonEmailProperty;
ABMultiValueRef emails = ABRecordCopyValue(person, property);
if (emails)
{
emailCounts = ABMultiValueGetCount(emails);
CFRelease(emails);
}
if (emailCounts == 1) property = kABPersonEmailProperty;
else property = kABPersonPhoneProperty;
CFMutableStringRef firstName = (CFMutableStringRef)ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFMutableStringRef lastName = (CFMutableStringRef)ABRecordCopyValue(person, kABPersonLastNameProperty);
self.chosenName = nil;
self.chosenName = [[firstName ? (NSString *)firstName : @"" stringByAppendingString:@" "] stringByAppendingString:lastName ? (NSString *)lastName : @""];
if (firstName) CFRelease(firstName);
if (lastName) CFRelease(lastName);
ABMultiValueRef values = ABRecordCopyValue(person, property);
if (!values) return;
CFArrayRef valueArray = ABMultiValueCopyArrayOfAllValues(values);
if (!valueArray)
{
CFRelease(values);
return;
}
CFStringRef value = (CFStringRef)CFArrayGetValueAtIndex(valueArray, 0);
if (!value)
{
CFRelease(values);
CFRelease(valueArray);
return;
}
NSString *tempString = (NSString *)value;
tempString = [tempString stringByReplacingOccurrencesOfString:@" " withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@"-" withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@"(" withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@")" withString:@""];
self.chosenKeyword = nil;
self.chosenKeyword = tempString;
CFRelease(values);
CFRelease(valueArray);
__block SNWhitelistViewController *weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
sqlite3 *database;
int openResult = sqlite3_open([DATABASE UTF8String], &database);
if (openResult == SQLITE_OK)
{
NSString *sql = [NSString stringWithFormat:@"insert or replace into whitelist (keyword, type, name, phone, sms, reply, message, forward, number, sound) values ('%@', '0', '%@', '1', '1', '0', '', '0', '', '0')", weakSelf.chosenKeyword, [weakSelf.chosenName stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];
int execResult = sqlite3_exec(database, [sql UTF8String], NULL, NULL, NULL);
if (execResult != SQLITE_OK) NSLog(@"SMSNinja: Failed to exec %@, error %d", sql, execResult);
sqlite3_close(database);
notify_post("com.naken.smsninja.whitelistchanged");
}
else NSLog(@"SMSNinja: Failed to open %@, error %d", DATABASE, openResult);
});
[keywordArray addObject:self.chosenKeyword];
[typeArray addObject:@"0"];
[nameArray addObject:self.chosenName];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:([keywordArray count] - 1) inSection:0]] withRowAnimation:YES];
[self.tableView endUpdates];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
CFIndex emailCounts = 0, phoneCounts = 0;
ABPropertyID property = kABPersonEmailProperty;
ABMultiValueRef emails = ABRecordCopyValue(person, property);
if (emails)
{
emailCounts = ABMultiValueGetCount(emails);
CFRelease(emails);
}
property = kABPersonPhoneProperty;
ABMultiValueRef phones = ABRecordCopyValue(person, property);
if (phones)
{
phoneCounts = ABMultiValueGetCount(phones);
CFRelease(phones);
}
if (emailCounts + phoneCounts == 1)
{
if (emailCounts == 1) property = kABPersonEmailProperty;
else property = kABPersonPhoneProperty;
}
else return YES;
CFMutableStringRef firstName = (CFMutableStringRef)ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFMutableStringRef lastName = (CFMutableStringRef)ABRecordCopyValue(person, kABPersonLastNameProperty);
self.chosenName = nil;
self.chosenName = [[firstName ? (NSString *)firstName : @"" stringByAppendingString:@" "] stringByAppendingString:lastName ? (NSString *)lastName : @""];
if (firstName) CFRelease(firstName);
if (lastName) CFRelease(lastName);
ABMultiValueRef values = ABRecordCopyValue(person, property);
if (!values) return NO;
CFArrayRef valueArray = ABMultiValueCopyArrayOfAllValues(values);
if (!valueArray)
{
CFRelease(values);
return NO;
}
CFStringRef value = (CFStringRef)CFArrayGetValueAtIndex(valueArray, 0);
if (!value)
{
CFRelease(values);
CFRelease(valueArray);
return NO;
}
NSString *tempString = (NSString *)value;
tempString = [tempString stringByReplacingOccurrencesOfString:@" " withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@"-" withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@"(" withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@")" withString:@""];
self.chosenKeyword = nil;
self.chosenKeyword = tempString;
CFRelease(values);
CFRelease(valueArray);
__block SNWhitelistViewController *weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
sqlite3 *database;
int openResult = sqlite3_open([DATABASE UTF8String], &database);
if (openResult == SQLITE_OK)
{
NSString *sql = [NSString stringWithFormat:@"insert or replace into whitelist (keyword, type, name, phone, sms, reply, message, forward, number, sound) values ('%@', '0', '%@', '1', '1', '0', '', '0', '', '0')", weakSelf.chosenKeyword, [weakSelf.chosenName stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];
int execResult = sqlite3_exec(database, [sql UTF8String], NULL, NULL, NULL);
if (execResult != SQLITE_OK) NSLog(@"SMSNinja: Failed to exec %@, error %d", sql, execResult);
sqlite3_close(database);
notify_post("com.naken.smsninja.whitelistchanged");
}
else NSLog(@"SMSNinja: Failed to open %@, error %d", DATABASE, openResult);
});
[keywordArray addObject:self.chosenKeyword];
[typeArray addObject:@"0"];
[nameArray addObject:self.chosenName];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:([keywordArray count] - 1) inSection:0]] withRowAnimation:YES];
[self.tableView endUpdates];
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
if (property == kABPersonEmailProperty || property == kABPersonPhoneProperty)
{
CFMutableStringRef firstName = (CFMutableStringRef)ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFMutableStringRef lastName = (CFMutableStringRef)ABRecordCopyValue(person, kABPersonLastNameProperty);
self.chosenName = nil;
self.chosenName = [[firstName ? (NSString *)firstName : @"" stringByAppendingString:@" "] stringByAppendingString:lastName ? (NSString *)lastName : @""];
if (firstName) CFRelease(firstName);
if (lastName) CFRelease(lastName);
ABMultiValueRef keywords = ABRecordCopyValue(person, property);
if (!keywords) return;
CFStringRef keyword = (CFStringRef)ABMultiValueCopyValueAtIndex(keywords, ABMultiValueGetIndexForIdentifier(keywords, identifier));
if (!keyword)
{
CFRelease(keywords);
return;
}
NSString *tempString = (NSString *)keyword;
tempString = [tempString stringByReplacingOccurrencesOfString:@" " withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@"-" withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@"(" withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@")" withString:@""];
self.chosenKeyword = nil;
self.chosenKeyword = tempString;
CFRelease(keyword);
CFRelease(keywords);
__block SNWhitelistViewController *weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
sqlite3 *database;
int openResult = sqlite3_open([DATABASE UTF8String], &database);
if (openResult == SQLITE_OK)
{
NSString *sql = [NSString stringWithFormat:@"insert or replace into whitelist (keyword, type, name, phone, sms, reply, message, forward, number, sound) values ('%@', '0', '%@', '1', '1', '0', '', '0', '', '0')", weakSelf.chosenKeyword, [weakSelf.chosenName stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];
int execResult = sqlite3_exec(database, [sql UTF8String], NULL, NULL, NULL);
if (execResult != SQLITE_OK) NSLog(@"SMSNinja: Failed to exec %@, error %d", sql, execResult);
sqlite3_close(database);
notify_post("com.naken.smsninja.whitelistchanged");
}
else NSLog(@"SMSNinja: Failed to open %@, error %d", DATABASE, openResult);
});
[keywordArray addObject:self.chosenKeyword];
[typeArray addObject:@"0"];
[nameArray addObject:self.chosenName];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:([keywordArray count] - 1) inSection:0]] withRowAnimation:YES];
[self.tableView endUpdates];
}
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
if (property == kABPersonEmailProperty || property == kABPersonPhoneProperty)
{
CFMutableStringRef firstName = (CFMutableStringRef)ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFMutableStringRef lastName = (CFMutableStringRef)ABRecordCopyValue(person, kABPersonLastNameProperty);
self.chosenName = nil;
self.chosenName = [[firstName ? (NSString *)firstName : @"" stringByAppendingString:@" "] stringByAppendingString:lastName ? (NSString *)lastName : @""];
if (firstName) CFRelease(firstName);
if (lastName) CFRelease(lastName);
ABMultiValueRef keywords = ABRecordCopyValue(person, property);
if (!keywords) return NO;
CFStringRef keyword = (CFStringRef)ABMultiValueCopyValueAtIndex(keywords, ABMultiValueGetIndexForIdentifier(keywords, identifier));
if (!keyword)
{
CFRelease(keywords);
return NO;
}
NSString *tempString = (NSString *)keyword;
tempString = [tempString stringByReplacingOccurrencesOfString:@" " withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@"-" withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@"(" withString:@""];
tempString = [tempString stringByReplacingOccurrencesOfString:@")" withString:@""];
self.chosenKeyword = nil;
self.chosenKeyword = tempString;
CFRelease(keyword);
CFRelease(keywords);
__block SNWhitelistViewController *weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
sqlite3 *database;
int openResult = sqlite3_open([DATABASE UTF8String], &database);
if (openResult == SQLITE_OK)
{
NSString *sql = [NSString stringWithFormat:@"insert or replace into whitelist (keyword, type, name, phone, sms, reply, message, forward, number, sound) values ('%@', '0', '%@', '1', '1', '0', '', '0', '', '0')", weakSelf.chosenKeyword, [weakSelf.chosenName stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];
int execResult = sqlite3_exec(database, [sql UTF8String], NULL, NULL, NULL);
if (execResult != SQLITE_OK) NSLog(@"SMSNinja: Failed to exec %@, error %d", sql, execResult);
sqlite3_close(database);
notify_post("com.naken.smsninja.whitelistchanged");
}
else NSLog(@"SMSNinja: Failed to open %@, error %d", DATABASE, openResult);
});
[keywordArray addObject:self.chosenKeyword];
[typeArray addObject:@"0"];
[nameArray addObject:self.chosenName];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:([keywordArray count] - 1) inSection:0]] withRowAnimation:YES];
[self.tableView endUpdates];
[self dismissModalViewControllerAnimated:YES];
}
return NO;
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)gotoAddressbook
{
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
if ([picker respondsToSelector:@selector(setPredicateForSelectionOfPerson:)]) picker.predicateForSelectionOfPerson = [NSPredicate predicateWithFormat:@"%K.@count + %K.@count == 1", ABPersonEmailAddressesProperty, ABPersonPhoneNumbersProperty];
[self presentModalViewController:picker animated:YES];
[picker release];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (amount != [keywordArray count] && scrollView.contentOffset.y + 1000.0f > scrollView.contentSize.height - scrollView.frame.size.height && scrollView.contentOffset.y != -64.0f && scrollView.contentOffset.y != 0.0f)
{
[self.tableView beginUpdates];
NSUInteger count = [keywordArray count];
[self loadDatabaseSegment];
NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:50];
for (NSUInteger i = count; i < [keywordArray count]; i++)
{
NSIndexPath *newPath = [NSIndexPath indexPathForRow:i inSection:0];
[insertIndexPaths insertObject:newPath atIndex:(i - count)];
}
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
}
@end