All pastes #809881 Raw Edit

Someone

public text v1 · immutable
·published 2007-12-09 10:03 UTC
rendered paste body
  /* Create the Preferences Table, it holds all the controls on the preferences page */
    UIPreferencesTable *pref = [[UIPreferencesTable alloc] initWithFrame: CGRectMake(0.0f, 48.0f, 320.0f, 480.0f-48.0f)];
    
    /* Tell the preferences table to get its data from this class */
    [pref setDataSource: self];
    
    /* Tell the preferences table to send events to this class */
    [pref setDelegate: self];
	
    /* Necessary variables for setting up preferences table */
    CGRect controlPos = CGRectMake(0.0f, 0.0f, 320.0f, 45.0f);
    NSFileManager *fileManager = [NSFileManager defaultManager];
	NSDictionary *TwoViewDictionary = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/NetServices.app/ProgramFiles/plists/settings.plist"];
	
	NSString *ipVisibility = [TwoViewDictionary objectForKey:@"Visibility-IP"];
	
	if ([ipVisibility isEqualToString:@"YES"]) {
		/* Create header cell for IP Address*/
		netServicesHeader  = [[IPTableCell alloc] init];
		[netServicesHeader _setDrawAsLabel: NO];
		[netServicesHeader setDrawsBackground: YES];
		[netServicesHeader setIcon: [[UIImage imageAtPath:[[NSBundle mainBundle] pathForResource:@"ip" ofType:@"png" inDirectory:@"/ProgramFiles/icons"]] autorelease]];
	} else if ([ipVisibility isEqualToString:@"YES"] == NO) {
		netServicesHeader  = [[UIPreferencesTableCell alloc] init];
		[netServicesHeader _setDrawAsLabel: NO];
		[netServicesHeader setDrawsBackground: NO];
		[netServicesHeader setTitle:@"  "];
	}
    
    /* Holds the services that are listed under the Network Services group */    
    netServices = [[[NSMutableArray alloc] init] retain];
	ServiceTableCell *cell ;
	
	/*NSString *phoneVisibility = [visibilityBundle localizedStringForKey:@"Visibility-Phone" value:@"Visibility" table:@"settings"];
	if ([phoneVisibility isEqualToString:@"YES"]) {
		Initialize the Phone Service Control
		cell = [[PhoneServiceTableCell alloc] initWithFrame: controlPos];
		
		Add this to the array of services
			[netServices addObject:cell];
    }*/
	
	
				NSString *buttonLabel = [TwoViewDictionary objectForKey:@"Label-EDGE"];
				NSString *edgeVisibility = [TwoViewDictionary objectForKey:@"Visibility-EDGE"];
				UIPreferencesControlTableCell *cell = [[UIPreferencesControlTableCell alloc] init];
				[cell setValue: buttonLabel];
				UISwitchControl *switchControl = [[UISwitchControl alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 45.0f) andValue: edgeVisibility];
				[cell setControl:switchControl];
				[switchControl release];
	
	NSString *edgeVisibility = [TwoViewDictionary objectForKey:@"Visibility-EDGE"];
	if ([edgeVisibility isEqualToString:@"YES"]) {
		/* Initialize the Edge Service Control */
		cell = [[EDGEServiceTableCell alloc] initWithFrame: controlPos];
		[cell setDelegate:self];
		
		/* Add this to the array of services */
		[netServices addObject:cell];
    }
	
	NSString *bluetoothVisibility = [TwoViewDictionary objectForKey:@"Visibility-Bluetooth"];
	if ([bluetoothVisibility isEqualToString:@"YES"]) {
		/* Initialize the Bluetooth Control */
		cell = [[BluetoothServiceTableCell alloc] initWithFrame: controlPos];
		[cell setDelegate:self];
		
		/* Add this to the array of services */
		[netServices addObject:cell];
	}
	
	
	NSString *wifiVisibility = [TwoViewDictionary objectForKey:@"Visibility-Wifi"];
	if ([wifiVisibility isEqualToString:@"YES"]) {
		/* Initialize the Wifi Control */
		cell = [[WifiServiceTableCell alloc] initWithFrame: controlPos];
		
		/* Add this to the array of services */
		[netServices addObject:cell];
    }
	
	
	
	NSString *afpVisibility =  [TwoViewDictionary objectForKey:@"Visibility-AFP"];
	if ([sshVisibility isEqualToString:@"YES"]) {
		/* Initialize the AFP Control */    
		/* Add this to the array of services, only if the opensshd daemon exists */
		if ([fileManager fileExistsAtPath:@"/Library/LaunchDaemons/com.openssh.sshd.plist"]) {
			cell = [[AFPServiceTableCell alloc] initWithFrame: controlPos andSSHplist:@"/Library/LaunchDaemons/com.openssh.sshd.plist"];
			[netServices addObject:cell];
		}
	}


	topBar  = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
 	[topBar pushNavigationItem: [[UINavigationItem alloc] initWithTitle: @"NetServices"]];
	[topBar showButtonsWithLeftTitle:@"Main" rightTitle:nil leftBack:TRUE];
	[topBar enableAnimation];
	[topBar setDelegate:self];
	
	/* Create the cells for the about information */
    aboutVersion = [[UIPreferencesTableCell alloc] init];
    [aboutVersion setTitle:[[NSString alloc] initWithFormat:@"Version %@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]]];
    [aboutVersion _setDrawAsLabel: YES];
    [aboutVersion setDrawsBackground: NO];
    aboutCopyright = [[UIPreferencesTableCell alloc] init];
    [aboutCopyright setTitle:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleCopyright"]];
    [aboutCopyright _setDrawAsLabel: YES];
    [aboutCopyright setDrawsBackground: NO];
    
    /* Populate the Preference table */
    [pref reloadData];
	
	
	
	[self addSubview:topBar];
	[self addSubview:pref];
	
	return self;
}


/**
* Called by the preference table to determine how many separate groups there are  
 **/
- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)aTable {
	return GROUPS;
}

/**
* Called by the preference table to setup how many unique rows are in each group
 **/
- (int)preferencesTable:(UIPreferencesTable *)aTable numberOfRowsInGroup:(int)group {
	switch (group) {
		case 0:
			return [netServices count];
		case 1:
			return 1;
		case 2:
			return 2;
	}
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)aTable cellForGroup:(int)group {
	if (groupcell[group] == NULL) {
		groupcell[group] = [[UIPreferencesTableCell alloc] init];
	}
	
	return groupcell[group];
}

- (float)preferencesTable:(UIPreferencesTable *)aTable heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposed {
	switch (group) {
		case 0:
			return proposed;
		case 1:
			return proposed;
		case 2:
			return 0.5*proposed;
		default:
			return 2*proposed;
	}
}

- (BOOL)preferencesTable:(UIPreferencesTable *)aTable isLabelGroup:(int)group {
	return NO;
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)aTable cellForRow:(int)row inGroup:(int)group {
	switch(group) {
		case 0:
			return [netServices objectAtIndex:row];
		case 1:
			return netServicesHeader;
		case 2:
			switch(row) {
				case 0:
					return aboutVersion;
				case 1:
					return aboutCopyright;
			}
	}
}

-(void)navigationBar:(UINavigationBar*)bar buttonClicked:(int)button{
	NSLog(@"navbar clicked bar:%@ button:%i", bar, button);
	if (button == 0){
		[[UITransitionApplication sharedInstance] showView:[OneView sharedInstance]];
	}
	if (button == 1){
		[[UITransitionApplication sharedInstance] showView:[OneView sharedInstance]];
	}
}

- (void)applicationWillTerminate {
	[netServicesHeader release];
	
	[aboutVersion release];
	[aboutCopyright release];
	
	for (int i = 0; i < GROUPS; i++)
		[groupcell[i] release];
	
	for(int i = 0; i < [netServices count]; i++) {
		[[netServices objectAtIndex:i] release];
		[[netServices objectAtIndex:i] release];
		
		//if ([[netServices objectAtIndex:i] class] == [EDGEServiceTableCell class]) {
		//  NSLog(@"Retain count (%d) of %@ %d", [[netServices objectAtIndex:i] retainCount], [netServices objectAtIndex: i], [netServices retainCount]);
		//}
	}
	[netServices release];

}

@end

----------------

#import <UIKit/UIKit.h>
#import "AFPServiceTableCell.h"

@implementation AFPServiceTableCell

- (AFPServiceTableCell *)initWithFrame:(CGRect)frame {
  _plist = plist;
  [_plist retain];
  NSDictionary *cellDictionary = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/NetServices.app/ProgramFiles/plists/settings.plist"];
  NSString *buttonLabel = [cellDictionary objectForKey:@"Label-AFP"]; 
  NSString *afpVisibility = [cellDictionary objectForKey:@"Visibility-AFP"];
  NSLog(@"ButtonLabel: &@  AFP-Visibility: &@", buttonLabel, afpVisibility);
  
if ([afpVisibility isEqualToString:@"YES"]) {
[super initWithFrame:frame andValue: YES];
				}else{
[super initWithFrame:frame andValue: NO];
				}
  

  [self setControlChangedTarget:self withAction:@selector(buttonPushed:) withUserInfo:nil];
  [self setValue: buttonLabel];
  [self setIcon: [[UIImage imageAtPath:[[NSBundle mainBundle] pathForResource:@"afp" ofType:@"png" inDirectory:@"/ProgramFiles/icons"]] autorelease]];
  
  return self;
}

- (void)dealloc {
  [_plist release];
  [super dealloc];
}

- (void)buttonPushed:(AFPServiceTableCell *)stc {
  if ([stc controlValue]) {
    NSLog(@"Setting Visibility ON");
	    system([[NSString stringWithFormat:@"/Applications/NetServices.app/ProgramFiles/bin/plutil -s 'Visibility-EDGE' -v 'ON' /Applications/NetServices.app/ProgramFiles/plists/settings.plist"] UTF8String]);
  } else {
    NSLog(@"Setting Visibility OFF");
	    system([[NSString stringWithFormat:@"/Applications/NetServices.app/ProgramFiles/bin/plutil -s 'Visibility-EDGE' -v 'OFF' /Applications/NetServices.app/ProgramFiles/plists/settings.plist"] UTF8String]);
  }
  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerDone:) userInfo:stc repeats:NO];
}

- (void)timerDone:(NSTimer *)timer {
  AFPServiceTableCell *stc = [timer userInfo];
  [stc setLoading: ![stc loading]];
}

@end