Skip to content
This repository
tag: 2.3.0
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 98 lines (84 sloc) 2.865 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
//
// AddDBController.m
// MongoHub
//
// Created by Syd on 10-4-28.
// Copyright 2010 ThePeppersStudio.COM. All rights reserved.
//

#import "Configure.h"
#import "AddDBController.h"
#import "DatabasesArrayController.h"
#import "Database.h"
#import "Connection.h"
#import "NSString+Extras.h"

@implementation AddDBController

@synthesize dbname;
@synthesize user;
@synthesize password;
@synthesize dbInfo;
@synthesize conn;
@synthesize managedObjectContext;
@synthesize databasesArrayController;

- (id)init {
    if (![super initWithWindowNibName:@"NewDB"]) return nil;
    return self;
}

- (void)dealloc {
    [dbname release];
    [user release];
    [password release];
    [dbInfo release];
    [managedObjectContext release];
    [databasesArrayController release];
    [conn release];
    [super dealloc];
}

- (void)windowWillClose:(NSNotification *)notification {
    [[NSNotificationCenter defaultCenter] postNotificationName:kNewDBWindowWillClose object:dbInfo];
    dbInfo = nil;
}

- (IBAction)cancel:(id)sender {
    dbInfo = nil;
    [self close];
}

- (IBAction)add:(id)sender {
    if ([ [dbname stringValue] length] == 0) {
        NSRunAlertPanel(@"Error", @"Database name could not be empty", @"OK", nil, nil);
        return;
    }
    NSArray *keys = [[NSArray alloc] initWithObjects:@"dbname", @"user", @"password", nil];
    NSString *dbstr = [[NSString alloc] initWithString:[dbname stringValue]];
    NSString *userStr = [[NSString alloc] initWithString:[user stringValue]];
    NSString *passStr = [[NSString alloc] initWithString:[password stringValue]];
    NSArray *objs = [[NSArray alloc] initWithObjects:dbstr, userStr, passStr, nil];
    [dbstr release];
    [userStr release];
    [passStr release];
    if (!dbInfo) {
        dbInfo = [[NSMutableDictionary alloc] initWithCapacity:3];
    }
    dbInfo = [NSMutableDictionary dictionaryWithObjects:objs forKeys:keys];
    [objs release];
    [keys release];
    if ([[dbInfo objectForKey:@"user"] isPresent] || [[dbInfo objectForKey:@"password"] isPresent]) {
        Database *dbobj = [databasesArrayController dbInfo:conn name:[dbname stringValue]];
        if (dbobj==nil) {
            //[dbobj release];
            dbobj = [databasesArrayController newObjectWithConn:conn name:[dbname stringValue] user:[dbInfo objectForKey:@"user"] password:[dbInfo objectForKey:@"password"]];
            [databasesArrayController addObject:dbobj];
            [dbobj release];
        }
        [self saveAction];
    }
    [self close];
}

- (void) saveAction {
    
    NSError *error = nil;
    
    if (![[self managedObjectContext] commitEditing]) {
        NSLog(@"%@:%s unable to commit editing before saving", [self class], _cmd);
    }
    
    if (![[self managedObjectContext] save:&error]) {
        [[NSApplication sharedApplication] presentError:error];
    }
}

@end
Something went wrong with that request. Please try again.