Permalink
Browse files

alpha release

  • Loading branch information...
1 parent c320ba3 commit d6c470775c4306e4d33e49064320159809a29363 @jwang committed Apr 3, 2011
@@ -7,36 +7,51 @@
objects = {
/* Begin PBXBuildFile section */
+ 5224089713480B34005E3503 /* started.png in Resources */ = {isa = PBXBuildFile; fileRef = 5224089513480B34005E3503 /* started.png */; };
+ 5224089813480B34005E3503 /* stopped.png in Resources */ = {isa = PBXBuildFile; fileRef = 5224089613480B34005E3503 /* stopped.png */; };
+ 522408A11348179B005E3503 /* postgres.tiff in Resources */ = {isa = PBXBuildFile; fileRef = 522408A01348179B005E3503 /* postgres.tiff */; };
+ 522408AA13482CD4005E3503 /* elephant.png in Resources */ = {isa = PBXBuildFile; fileRef = 522408A913482CD4005E3503 /* elephant.png */; };
+ 529E2A2A133DDED80040D93C /* postgresql.png in Resources */ = {isa = PBXBuildFile; fileRef = 529E2A29133DDED80040D93C /* postgresql.png */; };
+ 52C9C612133DEF2F008DB7F5 /* logo.png in Resources */ = {isa = PBXBuildFile; fileRef = 52C9C60C133DEF2F008DB7F5 /* logo.png */; };
52D78CC3133DC8460044F52C /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52D78CC2133DC8460044F52C /* Cocoa.framework */; };
52D78CC5133DC8460044F52C /* PreferencePanes.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52D78CC4133DC8460044F52C /* PreferencePanes.framework */; };
52D78CCF133DC8460044F52C /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 52D78CCD133DC8460044F52C /* InfoPlist.strings */; };
52D78CD2133DC8460044F52C /* Postgres.h in Headers */ = {isa = PBXBuildFile; fileRef = 52D78CD1133DC8460044F52C /* Postgres.h */; };
52D78CD4133DC8460044F52C /* Postgres.m in Sources */ = {isa = PBXBuildFile; fileRef = 52D78CD3133DC8460044F52C /* Postgres.m */; };
52D78CD6133DC8460044F52C /* Postgres.tiff in Resources */ = {isa = PBXBuildFile; fileRef = 52D78CD5133DC8460044F52C /* Postgres.tiff */; };
52D78CD9133DC8460044F52C /* Postgres.xib in Resources */ = {isa = PBXBuildFile; fileRef = 52D78CD7133DC8460044F52C /* Postgres.xib */; };
+ 52D78CE8133DD3490044F52C /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52D78CE7133DD3490044F52C /* AppKit.framework */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ 5224089513480B34005E3503 /* started.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = started.png; sourceTree = "<group>"; };
+ 5224089613480B34005E3503 /* stopped.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = stopped.png; sourceTree = "<group>"; };
+ 522408A01348179B005E3503 /* postgres.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = postgres.tiff; sourceTree = "<group>"; };
+ 522408A913482CD4005E3503 /* elephant.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = elephant.png; sourceTree = "<group>"; };
+ 529E2A29133DDED80040D93C /* postgresql.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = postgresql.png; sourceTree = "<group>"; };
+ 52C9C60C133DEF2F008DB7F5 /* logo.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = logo.png; sourceTree = "<group>"; };
52D78CBF133DC8460044F52C /* Postgres.prefPane */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Postgres.prefPane; sourceTree = BUILT_PRODUCTS_DIR; };
52D78CC2133DC8460044F52C /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
52D78CC4133DC8460044F52C /* PreferencePanes.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = PreferencePanes.framework; path = System/Library/Frameworks/PreferencePanes.framework; sourceTree = SDKROOT; };
52D78CC7133DC8460044F52C /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
52D78CC8133DC8460044F52C /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; };
52D78CC9133DC8460044F52C /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
- 52D78CCC133DC8460044F52C /* Postgres-Info.plist */ = {isa = PBXFileReference; path = "Postgres-Info.plist"; sourceTree = "<group>"; };
+ 52D78CCC133DC8460044F52C /* Postgres-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Postgres-Info.plist"; sourceTree = "<group>"; };
52D78CCE133DC8460044F52C /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
- 52D78CD0133DC8460044F52C /* Postgres-Prefix.pch */ = {isa = PBXFileReference; path = "Postgres-Prefix.pch"; sourceTree = "<group>"; };
+ 52D78CD0133DC8460044F52C /* Postgres-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Postgres-Prefix.pch"; sourceTree = "<group>"; };
52D78CD1133DC8460044F52C /* Postgres.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Postgres.h; sourceTree = "<group>"; };
52D78CD3133DC8460044F52C /* Postgres.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Postgres.m; sourceTree = "<group>"; };
52D78CD5133DC8460044F52C /* Postgres.tiff */ = {isa = PBXFileReference; lastKnownFileType = image.tiff; path = Postgres.tiff; sourceTree = "<group>"; };
52D78CD8133DC8460044F52C /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/Postgres.xib; sourceTree = "<group>"; };
+ 52D78CE7133DD3490044F52C /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
52D78CBA133DC8460044F52C /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
+ 52D78CE8133DD3490044F52C /* AppKit.framework in Frameworks */,
52D78CC3133DC8460044F52C /* Cocoa.framework in Frameworks */,
52D78CC5133DC8460044F52C /* PreferencePanes.framework in Frameworks */,
);
@@ -48,6 +63,7 @@
52D78CB2133DC8460044F52C = {
isa = PBXGroup;
children = (
+ 52D78CE7133DD3490044F52C /* AppKit.framework */,
52D78CCA133DC8460044F52C /* Postgres */,
52D78CC1133DC8460044F52C /* Frameworks */,
52D78CC0133DC8460044F52C /* Products */,
@@ -88,6 +104,7 @@
52D78CD1133DC8460044F52C /* Postgres.h */,
52D78CD3133DC8460044F52C /* Postgres.m */,
52D78CD5133DC8460044F52C /* Postgres.tiff */,
+ 529E2A29133DDED80040D93C /* postgresql.png */,
52D78CD7133DC8460044F52C /* Postgres.xib */,
52D78CCB133DC8460044F52C /* Supporting Files */,
);
@@ -97,6 +114,11 @@
52D78CCB133DC8460044F52C /* Supporting Files */ = {
isa = PBXGroup;
children = (
+ 5224089513480B34005E3503 /* started.png */,
+ 5224089613480B34005E3503 /* stopped.png */,
+ 522408A01348179B005E3503 /* postgres.tiff */,
+ 522408A913482CD4005E3503 /* elephant.png */,
+ 52C9C60C133DEF2F008DB7F5 /* logo.png */,
52D78CCC133DC8460044F52C /* Postgres-Info.plist */,
52D78CCD133DC8460044F52C /* InfoPlist.strings */,
52D78CD0133DC8460044F52C /* Postgres-Prefix.pch */,
@@ -170,6 +192,12 @@
52D78CCF133DC8460044F52C /* InfoPlist.strings in Resources */,
52D78CD6133DC8460044F52C /* Postgres.tiff in Resources */,
52D78CD9133DC8460044F52C /* Postgres.xib in Resources */,
+ 529E2A2A133DDED80040D93C /* postgresql.png in Resources */,
+ 52C9C612133DEF2F008DB7F5 /* logo.png in Resources */,
+ 5224089713480B34005E3503 /* started.png in Resources */,
+ 5224089813480B34005E3503 /* stopped.png in Resources */,
+ 522408A11348179B005E3503 /* postgres.tiff in Resources */,
+ 522408AA13482CD4005E3503 /* elephant.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -301,6 +329,7 @@
52D78CDE133DC8460044F52C /* Release */,
);
defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
@@ -27,7 +27,7 @@
<key>NSMainNibFile</key>
<string>Postgres</string>
<key>NSPrefPaneIconFile</key>
- <string>Postgres.tiff</string>
+ <string>postgres.tiff</string>
<key>NSPrefPaneIconLabel</key>
<string>Postgres</string>
<key>NSPrincipalClass</key>
View
@@ -7,13 +7,40 @@
//
#import <PreferencePanes/PreferencePanes.h>
-
+#import <AppKit/AppKit.h>
@interface Postgres : NSPreferencePane {
@private
+ NSString *_path;
+ NSButton *_startButton;
+ NSTextField *_statusLabel;
+ NSButton *_autoStartCheckBox;
+ NSTextField *_detailInformationText;
+ NSProgressIndicator *_progressIndicator;
+
+ NSString *_serverLog;
+ NSString *_postgres;
+ NSString *_pgctl;
+ NSTextField *_startedSubtext;
+ NSImageView *_statusImage;
+
+ BOOL _isRunning;
}
+@property(nonatomic, retain) NSString *path;
+@property (assign) IBOutlet NSButton *startButton;
+@property (assign) IBOutlet NSTextField *statusLabel;
+@property (assign) IBOutlet NSButton *autoStartCheckBox;
+@property (assign) IBOutlet NSTextField *detailInformationText;
+@property (assign) IBOutlet NSProgressIndicator *progressIndicator;
+@property (nonatomic, retain) NSString *serverLog;
+@property (nonatomic, retain) NSString *postgres;
+@property (nonatomic, retain) NSString *pgctl;
+@property (assign) IBOutlet NSTextField *startedSubtext;
+@property (assign) IBOutlet NSImageView *statusImage;
- (void)mainViewDidLoad;
+- (IBAction)startStopServer:(id)sender;
+- (IBAction)autoStartChanged:(id)sender;
@end
View
@@ -8,11 +8,224 @@
#import "Postgres.h"
+#define LAUNCHCTL @"/bin/launchctl"
+#define WHICH @"/usr/bin/which"
+#define ECHO @"/bin/echo"
+#define FIND @"/usr/bin/find"
+#define PG_CTL @"pg_ctl"
+#define USR_LOCAL @"/usr/local"
+#define LAUNCH_AGENTS @"~/Library/LaunchAgents/"
+#define PLIST @"~/Library/LaunchAgents/org.postgresql.postgres.plist"
+#define COPY @"/bin/cp"
+#define MKDIR @"/bin/mkdir"
@implementation Postgres
+@synthesize path = _path;
+@synthesize startButton = _startButton;
+@synthesize statusLabel = _statusLabel;
+@synthesize autoStartCheckBox = _autoStartCheckBox;
+@synthesize detailInformationText = _detailInformationText;
+@synthesize progressIndicator = _progressIndicator;
+@synthesize postgres = _postgres;
+@synthesize serverLog = _serverLog;
+@synthesize pgctl = _pgctl;
+@synthesize startedSubtext = _startedSubtext;
+@synthesize statusImage = _statusImage;
+
+- (void)updateChrome {
+ NSString *startedPath = [[self bundle] pathForResource:@"started" ofType:@"png"];
+ NSImage *started = [[NSImage alloc] initWithContentsOfFile:startedPath];
+
+ NSString *stoppedPath = [[self bundle] pathForResource:@"stopped" ofType:@"png"];
+ NSImage *stopped = [[NSImage alloc] initWithContentsOfFile:stoppedPath];
+
+ if (_isRunning) {
+ [self.startButton setTitle:@"Stop Postgresql Server"];
+ [self.detailInformationText setTitleWithMnemonic:@"The Postgresql Database Server is started and ready for client connections. To shut the Server down, use the \"Stop Postgresql Server\" button."];
+ [self.statusLabel setTextColor:[NSColor greenColor]];
+ [self.statusLabel setTitleWithMnemonic:@"Running"];
+ [self.startedSubtext setHidden:NO];
+ [self.statusImage setImage:started];
+
+ }
+ else {
+ [self.startButton setTitle:@"Start Postgresql Server"];
+ [self.detailInformationText setTitleWithMnemonic:@"The Postgresql Database Server is currently stopped. To start it, use the \"Start PostgreSQL Server\" button."];
+ [self.statusLabel setTitleWithMnemonic:@"Stopped"];
+ [self.statusLabel setTextColor:[NSColor redColor]];
+ [self.startedSubtext setHidden:YES];
+ [self.statusImage setImage:stopped];
+ }
+ [started release];
+ [stopped release];
+}
+
+- (NSString *)runCLICommand:(NSString *)command arguments:(NSArray *)args waitUntilExit:(BOOL)wait {
+
+ NSTask *task;
+ task = [[NSTask alloc] init];
+ [task setLaunchPath:command];
+ [task setArguments: args];
+
+ NSPipe *pipe;
+ pipe = [NSPipe pipe];
+ [task setStandardOutput: pipe];
+ //The magic line that keeps your log where it belongs
+ [task setStandardInput:[NSPipe pipe]];
+
+ NSFileHandle *file;
+ file = [pipe fileHandleForReading];
+
+ [task launch];
+
+ if (wait) {
+ [task waitUntilExit];
+ }
+
+ NSData *data;
+ data = [file readDataToEndOfFile];
+
+ NSString *string;
+ string = [[NSString alloc] initWithData: data
+ encoding: NSUTF8StringEncoding];
+ [task release];
+ return string;
+}
+
+- (NSString *)runCLICommand:(NSString *)command arguments:(NSArray *)args {
+ return [self runCLICommand:command arguments:args waitUntilExit:NO];
+}
+
+- (void)checkServerStatus {
+ // Find out if Postgres server is running
+ NSArray *args = [NSArray arrayWithObjects: @"-D", self.postgres, @"status", nil];
+
+ // clean the pg_ctl from whitespaces
+ self.pgctl = [self.pgctl stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
+
+ NSString *result = [self runCLICommand:self.pgctl arguments:args];
+ //pg_ctl: server is running
+ if ([result rangeOfString:@"pg_ctl: server is running"].location != NSNotFound) {
+ _isRunning = YES;
+ }
+ else {
+ _isRunning = NO;
+ }
+
+ // Set the running/stopped label and set the button text to correct
+ // Check if Auto-start is enabled and set checkbox accordingly
+ args = [NSArray arrayWithObjects:@"list",@"org.postgresql.postgres", nil];
+ NSString *autoS = [self runCLICommand:LAUNCHCTL arguments:args waitUntilExit:NO];
+
+ if ([autoS length] == 0 || [autoS isEqualToString:@"launchctl list returned unknown response"]) {
+ [self.autoStartCheckBox setState:0];
+ }
+ else {
+ [self.autoStartCheckBox setState:1];
+ }
+
+ [self performSelectorOnMainThread:@selector(updateChrome) withObject:nil waitUntilDone:NO];
+
+}
+
+- (void)startup {
+ NSArray *args = nil;
+
+ // Find the Postgres Database
+ // first cheat and look for PGDATA
+ args = [NSArray arrayWithObjects:@"$PGDATA", nil];
+ NSString *PGDATA = [self runCLICommand:ECHO arguments:args];
+ // if that's not found, look for postgresql.conf in /usr/local
+ if ([PGDATA rangeOfString:@"PGDATA"].location != NSNotFound) {
+ args = [NSArray arrayWithObjects:USR_LOCAL,@"-type", @"f", @"-name", @"postgresql.conf", nil];
+ PGDATA = [self runCLICommand:FIND arguments:args];
+ }
+
+ // remove the /postgresql.conf from the path along with whitespaces and newline characters
+ self.postgres = [PGDATA stringByDeletingLastPathComponent];
+ self.postgres = [self.postgres stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
+
+ // Find the pg_ctl
+ args = [NSArray arrayWithObjects: PG_CTL, nil];
+ self.pgctl = [self runCLICommand:WHICH arguments:args];
+
+ // Don't proceed. Disable everything.
+ if ([self.pgctl length] == 0) {
+ [self.startButton setEnabled:NO];
+ [self.autoStartCheckBox setEnabled:NO];
+ }
+ else {
+ [self checkServerStatus];
+ }
+}
+
+// For checking after user closes or choses Show All option.
+- (void)didSelect {
+ [self startup];
+}
- (void)mainViewDidLoad
{
+ _isRunning = NO;
+ [self startup];
+
+}
+
+- (IBAction)startStopServer:(id)sender {
+
+ // pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
+ // pg_ctl -D /usr/local/var/postgres stop -s -m fast
+ self.serverLog = @"/usr/local/var/postgres/server.log";
+
+ NSArray *args = nil;
+ if(_isRunning) {
+ [self.startButton setTitle:@"Stop Postgresql Server"];
+ args = [NSArray arrayWithObjects: @"-D", self.postgres, @"stop", @"-s", @"-m", @"fast", nil];
+ }
+ else {
+ [self.startButton setTitle:@"Start Postgresql Server"];
+ args = [NSArray arrayWithObjects: @"-D", self.postgres, @"-l", self.serverLog, @"start", nil];
+ }
+ [self runCLICommand:self.pgctl arguments:args waitUntilExit:YES];
+
+ // update the chrome after done
+ [self performSelector:@selector(checkServerStatus) withObject:nil afterDelay:3.0];
+}
+
+- (IBAction)autoStartChanged:(id)sender {
+ NSString *result = nil;
+ NSArray *args = nil;
+ if ([self.autoStartCheckBox state] == 0) {
+ // launchctl unload -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
+ args = [NSArray arrayWithObjects:@"unload", @"-w", PLIST, nil];
+ result = [self runCLICommand:LAUNCHCTL arguments:args];
+
+ } else {
+ // mkdir -p ~/Library/LaunchAgents
+ args = [NSArray arrayWithObjects:@"-p", LAUNCH_AGENTS, nil];
+ result = [self runCLICommand:MKDIR arguments:args waitUntilExit:YES];
+
+ // find the postgres.plist in the /usr/local
+ args = [NSArray arrayWithObjects:USR_LOCAL,@"-type", @"f", @"-name", @"org.postgresql.postgres.plist", nil];
+ result = [self runCLICommand:FIND arguments:args waitUntilExit:YES];
+
+ // cp /usr/local/Cellar/postgresql/9.0.3/org.postgresql.postgres.plist ~/Library/LaunchAgents/
+ args = [NSArray arrayWithObjects:result, LAUNCH_AGENTS, nil];
+ result = [self runCLICommand:COPY arguments:args waitUntilExit:YES];
+
+ // launchctl load -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
+ args = [NSArray arrayWithObjects:@"load", @"-w", PLIST, nil];
+ result = [self runCLICommand:LAUNCHCTL arguments:args];
+ }
+
+}
+
+- (void)dealloc {
+ [_pgctl release];
+ [_serverLog release];
+ [_postgres release];
+ [_path release];
+ [super dealloc];
}
@end
View
Binary file not shown.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.

0 comments on commit d6c4707

Please sign in to comment.