openfirePrefPane.m 5.48 KB
Newer Older
1
//
David Smith's avatar
David Smith committed
2 3
//  openfirePrefPane.m
//  Preference panel for Openfire
4 5 6 7 8 9 10
//
//  Created by Daniel Henninger on 7/7/06.
//  Copyright (c) 2006 Jive Software. All rights reserved.
//
//  Concept taken from MySQL preference panel, as well as some borrowed code.
//

11
#import <Security/Security.h>
12
#import <CoreFoundation/CoreFoundation.h>
David Smith's avatar
David Smith committed
13
#import "openfirePrefPane.h"
14

David Smith's avatar
David Smith committed
15
@implementation openfirePrefPane
16 17 18

- (void)mainViewDidLoad
{
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	AuthorizationItem authItems[1]; 	// we only want to get authorization for one command

    authItems[0].name = kAuthorizationRightExecute;	// we want the right to execute
	char *cmd = [[[NSBundle bundleForClass:[self class]] pathForAuxiliaryExecutable:@"HelperTool"] fileSystemRepresentation];
    authItems[0].value = cmd;		// the path to the helper tool
    authItems[0].valueLength = strlen(cmd);	// length of the command
    authItems[0].flags = 0;				// no extra flags
    
	AuthorizationRights authRights;
    authRights.count = 1;		// we have one item
    authRights.items = authItems;	// here is the values for our item
	[authView setAuthorizationRights:&authRights];
	[authView setAutoupdate:YES];
	[authView setDelegate:self];
	[authView updateStatus:self];

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    [statusProgress setStyle:NSProgressIndicatorSpinningStyle];
    [statusProgress setDisplayedWhenStopped:NO];

	[self updateStatus];
}

- (BOOL)isRunning
{
    FILE *ps;
    char buff[1024];
    
    if((ps=popen(pscmd, "r")) == NULL)
    {
        // There was an error opening the pipe. Alert the user.
        NSBeginAlertSheet(
            @"Error!",
            @"OK",
            nil,
            nil,
            [NSApp mainWindow],
            self,
            nil,
            nil,
            self,
David Smith's avatar
David Smith committed
59
            @"An error occured while detecting a running Openfire process.",
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
            nil);
        
        return NO;
    }
    else
    {
		BOOL running = NO;
        if(fgets(buff, 1024, ps)) {
			running = YES;
			printf(buff);
		}
        pclose(ps);
        return running;
    }
}

- (IBAction)openAdminInterface:(id)sender
{
	NSString *stringURL = @"http://localhost:9090/";
	[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:stringURL]];
}

- (IBAction)toggleAutoStart:(id)sender
{
84 85 86
	char *args[2];
	args[0] = "boot"; 
	args[1] = NULL;
87
	
88 89 90
	OSStatus ourStatus = AuthorizationExecuteWithPrivileges([[authView authorization] authorizationRef],
															[authView authorizationRights]->items[0].value,
															kAuthorizationFlagDefaults, args, NULL);
91
        
92 93 94 95 96 97 98 99 100 101 102 103 104
	if(ourStatus != errAuthorizationSuccess)
	{
		// alert user the startup has failed
		NSBeginAlertSheet(
						  @"Error!",
						  @"OK",
						  nil,
						  nil,
						  [NSApp mainWindow],
						  self,
						  nil,
						  nil,
						  self,
David Smith's avatar
David Smith committed
105
						  @"Could not toggle Openfire startup.",
106 107
						  nil);
		[statusTimer invalidate];
108 109
		[self checkStatus];
	}
110 111 112 113 114 115
	
	[self updateStatus];
}

- (IBAction)toggleServer:(id)sender
{
116
	[statusMessage setHidden:YES];
117 118
	[statusProgress startAnimation:self];
    [startButton setEnabled:NO];
119
	
120 121 122
    if(![self isRunning])
    {
        [self startServer];
123
		statusTimer = [NSTimer scheduledTimerWithTimeInterval:4 target:self 
124 125 126 127 128
			selector:@selector(checkStatus) userInfo:nil repeats:NO];
    }
    else
    {
        [self stopServer];
129
        statusTimer = [NSTimer scheduledTimerWithTimeInterval:4 target:self 
130 131 132 133 134 135 136 137
            selector:@selector(checkStatus) userInfo:nil repeats:NO];
    }
    [self updateStatus];
}

- (void)checkStatus
{
	[statusProgress stopAnimation:self];
138
	[statusMessage setHidden:NO];
139 140 141 142
    [startButton setEnabled:YES];
    [self updateStatus];
}

143 144 145 146 147 148 149 150

- (void)updateStatus
{
	if ([self isRunning] == NO)
	{
		[statusMessage setStringValue:@"Stopped"];
		[statusMessage setTextColor:[NSColor redColor]];
		[statusDescription setStringValue:@"The server may take a few seconds to start up."];
David Smith's avatar
David Smith committed
151
		[startButton setTitle:@"Start Openfire"];
152 153 154 155 156 157 158
		[viewAdminButton setEnabled:NO];
	}
	else 
	{
		[statusMessage setStringValue:@"Running"];
		[statusMessage setTextColor:[NSColor greenColor]];
		[statusDescription setStringValue:@"The server may take a few seconds to stop."];
David Smith's avatar
David Smith committed
159
		[startButton setTitle:@"Stop Openfire"];
160 161 162 163 164 165
		[viewAdminButton setEnabled:YES];
	}
	BOOL isStartingAtBoot = [[[NSMutableDictionary dictionaryWithContentsOfFile:plistPath] objectForKey:@"RunAtLoad"] boolValue];
	[autoStartCheckbox setState:(isStartingAtBoot ? NSOnState : NSOffState)];
}

166 167
- (void)startServer
{
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	char *args[0];
	args[1] = NULL;
			
	OSStatus ourStatus = AuthorizationExecuteWithPrivileges([[authView authorization] authorizationRef],
															[authView authorizationRights]->items[0].value,
															kAuthorizationFlagDefaults, args, NULL);
	// wait for the server to start

	if(ourStatus != errAuthorizationSuccess)
	{
		// alert user the startup has failed
		NSBeginAlertSheet(
			@"Error!",
			@"OK",
			nil,
			nil,
			[NSApp mainWindow],
			self,
			nil,
			nil,
			self,
David Smith's avatar
David Smith committed
189
			@"Could not start the Openfire server.",
190
			nil);
191
		[statusTimer invalidate];
192 193
		[self checkStatus];
	}
194 195 196 197
}

- (void) stopServer
{
198 199 200 201 202 203
	char *args[1];
	args[0] = NULL;
		
	OSStatus ourStatus = AuthorizationExecuteWithPrivileges([[authView authorization] authorizationRef],
															[authView authorizationRights]->items[0].value,
															kAuthorizationFlagDefaults, args, NULL);
204
	
205 206 207 208 209 210 211 212 213 214 215 216 217
	if(ourStatus != errAuthorizationSuccess)
	{
		// alert user the startup has failed
		NSBeginAlertSheet(
			@"Error!",
			@"OK",
			nil,
			nil,
			[NSApp mainWindow],
			self,
			nil,
			nil,
			self,
David Smith's avatar
David Smith committed
218
			@"Could not stop the Openfire server.",
219
			nil);
220
		[statusTimer invalidate];
221 222
		[self checkStatus];
	}
223 224 225
}

@end