Skip to content

Commit

Permalink
adding connection duplication feature #75
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Lebel committed Nov 16, 2013
1 parent 30ff1d5 commit 6289134
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 29 deletions.
6 changes: 6 additions & 0 deletions Resources/English.lproj/MHMainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@
<action selector="performClick:" target="554" id="590"/>
</connections>
</menuItem>
<menuItem title="Duplicate Connection" keyEquivalent="d" id="7zg-uW-JzR">
<connections>
<action selector="duplicateConnection:" target="205" id="ua4-qW-kY0"/>
<binding destination="542" name="enabled" keyPath="selectedObjects.@count" id="7kl-d9-zUa"/>
</connections>
</menuItem>
<menuItem title="Delete Connection" id="587">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

@protocol MHConnectionEditorWindowControllerDelegate <NSObject>
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) ConnectionsArrayController *connectionsArrayController;
- (void)connectionWindowControllerDidCancel:(MHConnectionEditorWindowController *)controller;
- (void)connectionWindowControllerDidValidate:(MHConnectionEditorWindowController *)controller;
@end
Expand All @@ -34,17 +35,19 @@
IBOutlet NSTextField *_sshuserTextField;
IBOutlet NSSecureTextField *_sshpasswordTextField;
IBOutlet NSTextField *_sshkeyfileTextField;
IBOutlet ConnectionsArrayController *_connectionsArrayController;
IBOutlet NSButton *_selectKeyFileButton;
IBOutlet NSButton *_addSaveButton;

MHConnectionStore *_editedConnectionStore;
MHConnectionStore *_connectionStoreDefaultValue;
BOOL _newConnection;
id<MHConnectionEditorWindowControllerDelegate> _delegate;
}
@property(nonatomic, retain, readwrite) MHConnectionStore *editedConnectionStore;
@property(nonatomic, retain, readwrite) MHConnectionStore *connectionStoreDefaultValue;
@property(nonatomic, assign, readwrite) id<MHConnectionEditorWindowControllerDelegate> delegate;
@property(nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, assign, readonly) ConnectionsArrayController *connectionsArrayController;
@property(nonatomic, assign, readonly, getter=isNewConnetion) BOOL newConnection;

- (IBAction)cancelAction:(id)sender;
Expand Down
100 changes: 72 additions & 28 deletions Sources/ConnectionControllers/MHConnectionEditorWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#import "DatabasesArrayController.h"
#import "mongo.h"

#define COPY_ALIAS_SUFFIX @" - Copy"

@interface MHConnectionEditorWindowController ()
- (void)_updateSSHFields;
- (void)_updateReplFields;
Expand All @@ -21,6 +23,7 @@ @implementation MHConnectionEditorWindowController
@synthesize editedConnectionStore = _editedConnectionStore;
@synthesize delegate = _delegate;
@synthesize newConnection = _newConnection;
@synthesize connectionStoreDefaultValue = _connectionStoreDefaultValue;

- (id)init
{
Expand All @@ -30,42 +33,75 @@ - (id)init

- (void)dealloc
{
self.connectionStoreDefaultValue = nil;
self.editedConnectionStore = nil;
[super dealloc];
}

- (void)windowDidLoad
{
MHConnectionStore *defaultValue = (self.editedConnectionStore == nil)?self.connectionStoreDefaultValue:self.editedConnectionStore;

[_hostportTextField.cell setPlaceholderString:[NSString stringWithFormat:@"%d", MONGO_DEFAULT_PORT]];
[_sshuserTextField.cell setPlaceholderString:[NSProcessInfo.processInfo.environment objectForKey:@"USER" ]];
if (self.editedConnectionStore) {
[_hostTextField setStringValue:self.editedConnectionStore.host];
if (self.editedConnectionStore.hostport.stringValue.longLongValue == 0) {
_addSaveButton.title = NSLocalizedString(@"Save", @"Save connection (after updating)");
_newConnection = NO;
} else {
_newConnection = YES;
_addSaveButton.title = NSLocalizedString(@"Add", @"Add connection");
}
if (defaultValue) {
if (_newConnection) {
NSCharacterSet *numberOrWhiteSpace = [NSCharacterSet characterSetWithCharactersInString:@"1234567890 "];
NSString *baseAlias = defaultValue.alias;
NSString *alias;
NSUInteger index = 1;

while ([numberOrWhiteSpace characterIsMember:[baseAlias characterAtIndex:baseAlias.length - 1]]) {
baseAlias = [baseAlias substringToIndex:baseAlias.length - 1];
}
if ([baseAlias hasSuffix:COPY_ALIAS_SUFFIX]) {
baseAlias = [baseAlias substringToIndex:baseAlias.length - COPY_ALIAS_SUFFIX.length];
alias = [NSString stringWithFormat:@"%@%@ %lu", baseAlias, COPY_ALIAS_SUFFIX, (unsigned long)index];
index++;
} else {
baseAlias = defaultValue.alias;
alias = [NSString stringWithFormat:@"%@%@", defaultValue.alias, COPY_ALIAS_SUFFIX];
}
while ([self connectionStoreWithAlias:alias] != nil) {
alias = [NSString stringWithFormat:@"%@%@ %lu", baseAlias, COPY_ALIAS_SUFFIX, (unsigned long)index];
index++;
}
[_aliasTextField setStringValue:alias];
} else {
[_aliasTextField setStringValue:defaultValue.alias];
}
self.window.title = _aliasTextField.stringValue;
[_hostTextField setStringValue:defaultValue.host];
if (defaultValue.hostport.stringValue.longLongValue == 0) {
[_hostportTextField setStringValue:@""];
} else {
[_hostportTextField setStringValue:self.editedConnectionStore.hostport.stringValue];
[_hostportTextField setStringValue:defaultValue.hostport.stringValue];
}
if (self.editedConnectionStore.servers) [_serversTextField setStringValue:self.editedConnectionStore.servers];
if (self.editedConnectionStore.repl_name) [_replnameTextField setStringValue:self.editedConnectionStore.repl_name];
[_usereplCheckBox setState:self.editedConnectionStore.userepl.boolValue?NSOnState:NSOffState];
[_aliasTextField setStringValue:self.editedConnectionStore.alias];
if (self.editedConnectionStore.adminuser) [_adminuserTextField setStringValue:self.editedConnectionStore.adminuser];
if (self.editedConnectionStore.adminpass) [_adminpassTextField setStringValue:self.editedConnectionStore.adminpass];
if (self.editedConnectionStore.defaultdb) [_defaultdbTextField setStringValue:self.editedConnectionStore.defaultdb];
if (self.editedConnectionStore.sshhost) [_sshhostTextField setStringValue:self.editedConnectionStore.sshhost];
if (self.editedConnectionStore.sshport.stringValue.longLongValue == 0) {
if (defaultValue.servers) [_serversTextField setStringValue:defaultValue.servers];
if (defaultValue.repl_name) [_replnameTextField setStringValue:defaultValue.repl_name];
[_usereplCheckBox setState:defaultValue.userepl.boolValue?NSOnState:NSOffState];
if (defaultValue.adminuser) [_adminuserTextField setStringValue:defaultValue.adminuser];
if (defaultValue.adminpass) [_adminpassTextField setStringValue:defaultValue.adminpass];
if (defaultValue.defaultdb) [_defaultdbTextField setStringValue:defaultValue.defaultdb];
if (defaultValue.sshhost) [_sshhostTextField setStringValue:defaultValue.sshhost];
if (defaultValue.sshport.stringValue.longLongValue == 0) {
[_sshportTextField setStringValue:@""];
} else {
[_sshportTextField setStringValue:self.editedConnectionStore.sshport.stringValue];
[_sshportTextField setStringValue:defaultValue.sshport.stringValue];
}
if (self.editedConnectionStore.sshuser) [_sshuserTextField setStringValue:self.editedConnectionStore.sshuser];
if (self.editedConnectionStore.sshpassword) [_sshpasswordTextField setStringValue:self.editedConnectionStore.sshpassword];
if (self.editedConnectionStore.sshkeyfile) [_sshkeyfileTextField setStringValue:self.editedConnectionStore.sshkeyfile];
[_usesshCheckBox setState:self.editedConnectionStore.usessh.boolValue?NSOnState:NSOffState];
_addSaveButton.title = NSLocalizedString(@"Save", @"Save connection (after updating)");
_newConnection = NO;
self.window.title = self.editedConnectionStore.alias;
if (defaultValue.sshuser) [_sshuserTextField setStringValue:defaultValue.sshuser];
if (defaultValue.sshpassword) [_sshpasswordTextField setStringValue:defaultValue.sshpassword];
if (defaultValue.sshkeyfile) [_sshkeyfileTextField setStringValue:defaultValue.sshkeyfile];
[_usesshCheckBox setState:defaultValue.usessh.boolValue?NSOnState:NSOffState];
} else {
self.window.title = NSLocalizedString(@"New Connection", @"New Connection");
[_hostTextField setStringValue:@""];
[_hostportTextField setStringValue:@""];
[_serversTextField setStringValue:@""];
Expand All @@ -81,9 +117,6 @@ - (void)windowDidLoad
[_sshpasswordTextField setStringValue:@""];
[_sshkeyfileTextField setStringValue:@""];
[_usesshCheckBox setState:NSOffState];
_newConnection = YES;
_addSaveButton.title = NSLocalizedString(@"Add", @"Add connection");
self.window.title = NSLocalizedString(@"New Connection", @"New Connection");
}
[_sshhostTextField setEnabled:_usereplCheckBox.state == NSOnState];
[_sshuserTextField setEnabled:_usereplCheckBox.state == NSOnState];
Expand Down Expand Up @@ -113,12 +146,24 @@ - (NSManagedObjectContext *)managedObjectContext
return _delegate.managedObjectContext;
}

- (ConnectionsArrayController *)connectionsArrayController
{
return _delegate.connectionsArrayController;
}

- (IBAction)cancelAction:(id)sender
{
[_delegate connectionWindowControllerDidCancel:self];
[NSApp endSheet:self.window];
}

- (MHConnectionStore *)connectionStoreWithAlias:(NSString *)alias
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"alias=%@", alias];
NSArray *items = [self.connectionsArrayController itemsUsingFetchPredicate:predicate];
return (items.count == 1)?[items objectAtIndex:0]:nil;
}

- (IBAction)addSaveAction:(id)sender
{
NSString *hostName;
Expand Down Expand Up @@ -167,14 +212,13 @@ - (IBAction)addSaveAction:(id)sender
NSBeginAlertSheet(NSLocalizedString(@"Error", @"Error"), NSLocalizedString(@"OK", @"OK"), nil, nil, self.window, nil, nil, nil, nil, NSLocalizedString(@"Name already in use!", @""));
return;
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"alias=%@", alias];
NSArray *items = [_connectionsArrayController itemsUsingFetchPredicate:predicate];
if (items.count == 1 && [items objectAtIndex:0] != self.editedConnectionStore) {
MHConnectionStore *sameAliasConnection = [self connectionStoreWithAlias:alias];
if (sameAliasConnection && sameAliasConnection != self.editedConnectionStore) {
NSBeginAlertSheet(NSLocalizedString(@"Error", @"Error"), NSLocalizedString(@"OK", @"OK"), nil, nil, self.window, nil, nil, nil, nil, NSLocalizedString(@"Name already in use!", @""));
return;
}
if (!self.editedConnectionStore) {
self.editedConnectionStore = [[_connectionsArrayController newObject] retain];
self.editedConnectionStore = [[self.connectionsArrayController newObject] retain];
}
self.editedConnectionStore.host = hostName;
self.editedConnectionStore.hostport = [NSNumber numberWithLongLong:hostPort];
Expand All @@ -192,7 +236,7 @@ - (IBAction)addSaveAction:(id)sender
self.editedConnectionStore.sshkeyfile = _sshkeyfileTextField.stringValue;
self.editedConnectionStore.usessh = [NSNumber numberWithBool:useSSH];
if (_newConnection) {
[_connectionsArrayController addObject:self.editedConnectionStore];
[self.connectionsArrayController addObject:self.editedConnectionStore];
}
[_delegate connectionWindowControllerDidValidate:self];
[NSApp endSheet:self.window];
Expand Down
1 change: 1 addition & 0 deletions Sources/MHApplicationDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ typedef enum {
- (void)saveConnections;
- (IBAction)showAddConnectionPanel:(id)sender;
- (IBAction)showEditConnectionPanel:(id)sender;
- (IBAction)duplicateConnection:(id)sender;
- (IBAction)deleteConnection:(id)sender;
- (IBAction)resizeConnectionItemView:(id)sender;
- (IBAction)showConnectionWindow:(id)sender;
Expand Down
11 changes: 11 additions & 0 deletions Sources/MHApplicationDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@ - (IBAction)showAddConnectionPanel:(id)sender
}
}

- (IBAction)duplicateConnection:(id)sender
{
if ([connectionsArrayController selectedObjects] && !self.connectionEditorWindowController) {
self.connectionEditorWindowController = [[MHConnectionEditorWindowController alloc] init];
self.connectionEditorWindowController.delegate = self;
self.connectionEditorWindowController.connectionStoreDefaultValue = [[connectionsArrayController selectedObjects] objectAtIndex:0];
[self.connectionEditorWindowController modalForWindow:self.window];
}
}

- (IBAction)deleteConnection:(id)sender
{
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
Expand Down Expand Up @@ -521,6 +531,7 @@ - (void)connectionWindowControllerDidCancel:(MHConnectionEditorWindowController
- (void)connectionWindowControllerDidValidate:(MHConnectionEditorWindowController *)controller
{
[self saveConnections];
[connectionsCollectionView setNeedsDisplay:YES];
if (self.connectionEditorWindowController == controller) {
self.connectionEditorWindowController = nil;
}
Expand Down

0 comments on commit 6289134

Please sign in to comment.