Skip to content

Commit

Permalink
Trying to enable and disable filters properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
sammcd committed Jan 11, 2010
1 parent 2b404ee commit 4d83c8b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
10 changes: 5 additions & 5 deletions BTMainWindowController.m
Expand Up @@ -44,9 +44,9 @@ - (IBAction)toggleBusinessTime:(id)sender {
[statusWindowController startTimer];
[businessTimeButton setTitle:@"It's Business Time"];
businessTime = YES;
[blackListModel enableFilters];


// Disable the host filtering.
[blackListModel disableFilters];

} else {
if (statusWindowController != nil ) {
Expand All @@ -55,8 +55,8 @@ - (IBAction)toggleBusinessTime:(id)sender {
businessTime = NO;
[businessTimeButton setTitle:@"Take a break"];

// Disable the host filtering.
[blackListModel disableFilters];
[blackListModel enableFilters];

}


Expand Down
72 changes: 65 additions & 7 deletions FTHostsController.m
Expand Up @@ -30,7 +30,7 @@ - (void)dealloc {


- (NSString*)readHostsFile {
return [NSString stringWithContentsOfFile:@"/etc/hosts" encoding:NSASCIIStringEncoding error:NULL];
return [NSString stringWithContentsOfFile:@"/Users/sammcd/Desktop/hosts" encoding:NSASCIIStringEncoding error:NULL];
}


Expand Down Expand Up @@ -63,7 +63,7 @@ - (void)writeHostsToFile {

} else {
// We did find the section, so update it.

NSLog(@"Update");
NSRange rangeToDelete;
rangeToDelete.location = startRange.location + startRange.length; // end of first line

Expand All @@ -73,15 +73,23 @@ - (void)writeHostsToFile {
// Time for some major repair!!
// todo: handle gracefully, right now we just delete it all

rangeToDelete.length = [hostsFile length] - rangeToDelete.location;
rangeToDelete.length = [hostsFile length] - rangeToDelete.location - 1;

} else {
rangeToDelete.length = endRange.location - startRange.location;
rangeToDelete.length = endRange.location - startRange.location - 1;
NSLog(@"%d - %d = %d",endRange.location, startRange.location,rangeToDelete.length);

}

NSLog(@"%d %d %d",rangeToDelete.location, rangeToDelete.length, [hostsFile length]);

//[hostsFile replaceCharactersInRange:rangeToDelete withString:newHosts];
if ( rangeToDelete.length > [sectionStart length] )
[hostsFile deleteCharactersInRange:rangeToDelete];

[hostsFile insertString:@"\n" atIndex:rangeToDelete.location];
[hostsFile insertString:newHosts atIndex:rangeToDelete.location+1];
[hostsFile insertString:@"\n" atIndex:rangeToDelete.location ];
[hostsFile insertString:sectionEnd atIndex:rangeToDelete.location ];

}

Expand All @@ -97,10 +105,60 @@ - (void)writeHostsToFile {
}

- (void)removeHostsFromFile {

NSMutableString* hostsFile;
NSString* sectionStart;
NSString* sectionEnd;

sectionStart = [NSString stringWithFormat:@"# %@ START",uniqueName];
sectionEnd = [NSString stringWithFormat:@"# %@ END",uniqueName];

// Open file
hostsFile = [[NSMutableString alloc] initWithString:[self readHostsFile]];

// Find section
// Check if we have already talked to this file.
NSRange startRange = [hostsFile rangeOfString:sectionStart];

if ( startRange.location == NSNotFound ) {
// Did not find the section so do nothing.

} else {
// We did find the section, so update it.
NSLog(@"Update");
NSRange rangeToDelete;
rangeToDelete.location = startRange.location + startRange.length; // end of first line

NSRange endRange = [hostsFile rangeOfString:sectionEnd];
if ( endRange.location == NSNotFound ) {
// Okay so the stupid user messed up my pretty little file.
// Time for some major repair!!
// todo: handle gracefully, right now we just delete it all

rangeToDelete.length = [hostsFile length] - rangeToDelete.location -1;

} else {
rangeToDelete.length = endRange.location - startRange.location - 1;
NSLog(@"%d - %d = %d",endRange.location, startRange.location,rangeToDelete.length);

}

NSLog(@"%d %d %d",rangeToDelete.location, rangeToDelete.length, [hostsFile length]);

NSLog(@"%@",hostsFile);
if ( rangeToDelete.length > [sectionStart length] )
[hostsFile deleteCharactersInRange:rangeToDelete];
NSLog(@"===================================");
NSLog(@"%@",hostsFile);
//NSLog(@"length: %d index: %d",[hostsFile length];
[hostsFile insertString:@"\n" atIndex:rangeToDelete.location ];
[hostsFile insertString:sectionEnd atIndex:rangeToDelete.location+1];

}

NSLog(@"Tried to delete");

// Remove section
NSError* error;
[hostsFile writeToFile:@"/Users/sammcd/Desktop/hosts" atomically:YES encoding:NSASCIIStringEncoding error:&error];

// Flush DNS caches
}
Expand Down

0 comments on commit 4d83c8b

Please sign in to comment.