Permalink
Browse files

Adding alert dialog prompting migration, with the option to quit instead

  • Loading branch information...
1 parent 2885a2f commit 5b9c501eff8b6e6cc45cbc6ce6cf0aa1e3f49e70 @mattt mattt committed Sep 11, 2012
Showing with 50 additions and 11 deletions.
  1. +18 −3 Postgres/AppDelegate.m
  2. +13 −0 Postgres/PostgresServer.h
  3. +19 −8 Postgres/PostgresServer.m
View
@@ -47,10 +47,12 @@ static BOOL PostgresIsHelperApplicationSetAsLoginItem() {
return flag;
}
+@interface AppDelegate () <PostgresServerMigrationDelegate>
+@end
@implementation AppDelegate {
- __strong NSStatusItem *_statusBarItem;
- __strong WelcomeWindowController *_welcomeWindowController;
+ NSStatusItem *_statusBarItem;
+ WelcomeWindowController *_welcomeWindowController;
}
@synthesize postgresStatusMenuItemViewController = _postgresStatusMenuItemViewController;
@synthesize statusBarMenu = _statusBarMenu;
@@ -66,7 +68,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification {
[self.checkForUpdatesMenuItem setEnabled:YES];
[self.checkForUpdatesMenuItem setHidden:NO];
#endif
-
+
_statusBarItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
_statusBarItem.highlightMode = YES;
_statusBarItem.menu = self.statusBarMenu;
@@ -77,6 +79,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification {
[self.automaticallyOpenDocumentationMenuItem setState:[[NSUserDefaults standardUserDefaults] boolForKey:kPostgresAutomaticallyOpenDocumentationPreferenceKey]];
[self.automaticallyStartMenuItem setState:PostgresIsHelperApplicationSetAsLoginItem() ? NSOnState : NSOffState];
+ [[PostgresServer sharedServer] setMigrationDelegate:self];
[[PostgresServer sharedServer] startOnPort:kPostgresAppDefaultPort terminationHandler:^(NSUInteger status) {
if (status == 0) {
[self.postgresStatusMenuItemViewController stopAnimatingWithTitle:[NSString stringWithFormat:NSLocalizedString(@"Running on Port %u", nil), kPostgresAppDefaultPort] wasSuccessful:YES];
@@ -161,4 +164,16 @@ - (IBAction)checkForUpdates:(id)sender {
#endif
}
+#pragma mark - PostgresServerMigrationDelegate
+
+- (BOOL)postgresServer:(PostgresServer *)server
+shouldMigrateFromVersion:(NSString *)fromVersion
+ toVersion:(NSString *)toVersion
+{
+ NSAlert *alert = [NSAlert alertWithMessageText:[NSString stringWithFormat:NSLocalizedString(@"Upgrade to Postgres Version %@?", nil), toVersion] defaultButton:NSLocalizedString(@"OK", nil) alternateButton:NSLocalizedString(@"Quit", nil) otherButton:nil informativeTextWithFormat:NSLocalizedString(@"Your current database, configured for Postgres %@, will have its data moved to `var-%@`.\n\nA new data directory at `var`, configured for Postgres %@ will be initialized in its place.", nil), fromVersion, fromVersion, toVersion];
+ NSInteger result = [alert runModal];
+
+ return result == NSAlertDefaultReturn;
+}
+
@end
View
@@ -25,8 +25,11 @@
#import <Foundation/Foundation.h>
+@protocol PostgresServerMigrationDelegate;
+
@interface PostgresServer : NSObject
+@property (weak) id <PostgresServerMigrationDelegate> migrationDelegate;
@property (readonly) BOOL isRunning;
@property (readonly) NSUInteger port;
@@ -45,3 +48,13 @@
terminationHandler:(void (^)(NSUInteger status))terminationHandler;
@end
+
+#pragma mark -
+
+@protocol PostgresServerMigrationDelegate <NSObject>
+
+- (BOOL)postgresServer:(PostgresServer *)server
+shouldMigrateFromVersion:(NSString *)fromVersion
+ toVersion:(NSString *)toVersion;
+
+@end
View
@@ -31,6 +31,10 @@
#define str(a) #a
static NSString * PGNormalizedVersionStringFromString(NSString *version) {
+ if (!version) {
+ return nil;
+ }
+
NSScanner *scanner = [NSScanner scannerWithString:version];
[scanner setCharactersToBeSkipped:[NSCharacterSet punctuationCharacterSet]];
@@ -50,6 +54,7 @@ @implementation PostgresServer {
xpc_connection_t _xpc_connection;
}
+@synthesize migrationDelegate = _migrationDelegate;
+ (PostgresServer *)sharedServer {
static PostgresServer *_sharedServer = nil;
@@ -105,16 +110,22 @@ - (BOOL)startOnPort:(NSUInteger)port
NSLog(@"Existing PGVersion: %@", existingPGVersion);
NSLog(@"Installed PGVersion: %@", installedPGVersion);
- if ([installedPGVersion compare:existingPGVersion options:NSNumericSearch] == NSOrderedDescending) {
- if ([[NSFileManager defaultManager] fileExistsAtPath:_varPath]) {
- NSError *error = nil;
- [[NSFileManager defaultManager] moveItemAtPath:_varPath toPath:[_varPath stringByAppendingFormat:@"-%@", existingPGVersion] error:&error];
- if (error) {
- NSLog(@"Error: %@", error);
+ if ([[NSFileManager defaultManager] fileExistsAtPath:_varPath] && existingPGVersion) {
+ if ([installedPGVersion compare:existingPGVersion options:NSNumericSearch] == NSOrderedDescending) {
+ if ([[NSFileManager defaultManager] fileExistsAtPath:_varPath]) {
+ if (![self.migrationDelegate postgresServer:self shouldMigrateFromVersion:existingPGVersion toVersion:installedPGVersion]) {
+ [NSApp terminate:self];
+ }
+
+ NSError *error = nil;
+ [[NSFileManager defaultManager] moveItemAtPath:_varPath toPath:[_varPath stringByAppendingFormat:@"-%@", existingPGVersion] error:&error];
+ if (error) {
+ NSLog(@"Error: %@", error);
+ }
}
+
+ existingPGVersion = nil;
}
-
- existingPGVersion = nil;
}
if (!existingPGVersion) {

0 comments on commit 5b9c501

Please sign in to comment.