Skip to content

Commit

Permalink
Improve code readability a little
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld committed Nov 2, 2010
1 parent 6109e0a commit 4a81511
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 138 deletions.
23 changes: 13 additions & 10 deletions AccountValidators/SCAccountValidator.j
Expand Up @@ -11,22 +11,25 @@
@import <Foundation/CPObject.j>

@implementation SCAccountValidator : CPObject
{
}

+ (BOOL)validateUsername:(CPString)username
{
return YES;
}

+ (CPString)validatePassword:(CPString)password
withConfirmPassword:(CPString)confirmPassword
+ (CPString)validatePassword:(CPString)password withConfirmPassword:(CPString)confirmPassword
{
var retVal = nil;
if (!password || password === "")
retVal = "Password can't be blank.";
else if ([password length] < 4)
retVal = "Password must be at least 4 characters long.";
else if (password !== confirmPassword)
retVal = "Passwords don't match.";
var retVal = nil;
if (!password || password === "")
retVal = "Password can't be blank.";
else if ([password length] < 4)
retVal = "Password must be at least 4 characters long.";
else if (password !== confirmPassword)
retVal = "Passwords don't match.";

return retVal;
return retVal;
}

@end
4 changes: 4 additions & 0 deletions AccountValidators/SCEmailAccountValidator.j
Expand Up @@ -12,9 +12,13 @@
@import "SCAccountValidator.j"

@implementation SCEmailAccountValidator : SCAccountValidator
{
}

+ (BOOL)validateUsername:(CPString)username
{
var reg = new RegExp("^[-a-zA-Z0-9+._]+@[-a-zA-Z0-9.]+\\.[a-zA-Z]{2,4}$");
return reg.test(username);
}

@end
6 changes: 5 additions & 1 deletion LoginProviders/SCEmailLoginDialogController.j
Expand Up @@ -13,7 +13,10 @@
@import "SCLoginDialogController.j"

var DefaultLoginDialogController = nil;
@implementation SCEmailLoginDialogController : SCLoginDialogController{ }

@implementation SCEmailLoginDialogController : SCLoginDialogController
{
}

- (void)awakeFromCib
{
Expand All @@ -40,4 +43,5 @@ var DefaultLoginDialogController = nil;
DefaultLoginDialogController = [self newLoginDialogController];
return DefaultLoginDialogController;
}

@end
189 changes: 90 additions & 99 deletions LoginProviders/SCLoginDialogController.j
Expand Up @@ -11,13 +11,13 @@
@import <AppKit/CPWindowController.j>
@import "../AccountValidators/SCAccountValidator.j"

var DefaultLoginDialogController = nil,
DefaultLoginTitle = @"Login/Register",
LoginTitle = @"Login",
RegisterTitle = @"Register",
UserCheckErrorMessage = @"There was an error trying to find your username. Check your internet connection.",
GenericErrorMessage = @"Something went wrong. Check your internet connection and try again.",
ConnectionStatusCode = -1;
var DefaultLoginDialogController = nil,
DefaultLoginTitle = @"Login/Register",
LoginTitle = @"Login",
RegisterTitle = @"Register",
UserCheckErrorMessage = @"There was an error trying to find your username. Check your internet connection.",
GenericErrorMessage = @"Something went wrong. Check your internet connection and try again.",
ConnectionStatusCode = -1;

SCLoginSucceeded = 0;
SCLoginFailed = 1;
Expand All @@ -27,38 +27,37 @@ SCLoginFailed = 1;
This is the controller for the default login dialog built-in to SCAuth.
*/

@implementation SCLoginDialogController : CPWindowController
{
unsigned _dialogReturnCode;
CPString _username @accessors(readonly, property=username);
id _delegate @accessors(property=delegate);
SEL _callback;
CPURLConnection _userCheckConnection;
CPURLConnection _loginConnection;
CPURLConnection _registrationConnection;
id _accountValidator @accessors(property=accountValidator);
// I use this for dependency injection in the tests
CPObject _connectionClass @accessors(property=connectionClass);

@outlet CPButton _tryAgainButton @accessors(property=tryAgainButton);
@outlet CPTextField _subheading @accessors(property=subheading);
@outlet CPTextField _userLabel @accessors(property=userLabel);
@outlet CPTextField _userField @accessors(property=userField);
@outlet CPTextField _passwordLabel @accessors(property=passwordLabel);
@outlet CPTextField _passwordField @accessors(property=passwordField);
@outlet CPTextField _passwordConfirmLabel @accessors(property=passwordConfirmLabel);
@outlet CPTextField _passwordConfirmField @accessors(property=passwordConfirmField);
@outlet CPTextField _errorMessage @accessors(property=errorMessage);
@outlet CPButton _loginButton @accessors(property=loginButton);
@outlet CPButton _cancelButton @accessors(property=cancelButton);
@outlet CPTextField _registeringProgressLabel @accessors(property=registeringProgressLabel);
@outlet CPTextField _loggingInProgressLabel @accessors(property=loggingInProgressLabel);
@outlet CPImageView _progressSpinner @accessors(property=progressSpinner);
@outlet CPImageView _userCheckSpinner @accessors(property=userCheckSpinner);
@outlet CPButton _forgotPasswordLink @accessors(property=forgotPasswordLink);
@outlet CPCheckBox _rememberMeButton @accessors(property=rememberMeButton);
@outlet CPView _formFieldContainer @accessors(property=formFieldContainer);
unsigned _dialogReturnCode;
CPString _username @accessors(readonly, property=username);
id _delegate @accessors(property=delegate);
SEL _callback;
CPURLConnection _userCheckConnection;
CPURLConnection _loginConnection;
CPURLConnection _registrationConnection;
id _accountValidator @accessors(property=accountValidator);
// I use this for dependency injection in the tests
CPObject _connectionClass @accessors(property=connectionClass);

@outlet CPButton _tryAgainButton @accessors(property=tryAgainButton);
@outlet CPTextField _subheading @accessors(property=subheading);
@outlet CPTextField _userLabel @accessors(property=userLabel);
@outlet CPTextField _userField @accessors(property=userField);
@outlet CPTextField _passwordLabel @accessors(property=passwordLabel);
@outlet CPTextField _passwordField @accessors(property=passwordField);
@outlet CPTextField _passwordConfirmLabel @accessors(property=passwordConfirmLabel);
@outlet CPTextField _passwordConfirmField @accessors(property=passwordConfirmField);
@outlet CPTextField _errorMessage @accessors(property=errorMessage);
@outlet CPButton _loginButton @accessors(property=loginButton);
@outlet CPButton _cancelButton @accessors(property=cancelButton);
@outlet CPTextField _registeringProgressLabel @accessors(property=registeringProgressLabel);
@outlet CPTextField _loggingInProgressLabel @accessors(property=loggingInProgressLabel);
@outlet CPImageView _progressSpinner @accessors(property=progressSpinner);
@outlet CPImageView _userCheckSpinner @accessors(property=userCheckSpinner);
@outlet CPButton _forgotPasswordLink @accessors(property=forgotPasswordLink);
@outlet CPCheckBox _rememberMeButton @accessors(property=rememberMeButton);
@outlet CPView _formFieldContainer @accessors(property=formFieldContainer);
}

- (void)awakeFromCib
Expand All @@ -80,7 +79,7 @@ SCLoginFailed = 1;
[_subheading setLineBreakMode:CPLineBreakByWordWrapping];
[_subheading setBackgroundColor:[CPColor colorWithCalibratedRed:103.0 / 255.0 green:154.0 / 255.0 blue:205.0 / 255.0 alpha:1.0]];
[_subheading setTextColor:[CPColor whiteColor]];
var border = [[CPView alloc] initWithFrame:CPRectMake(0,CPRectGetHeight([_subheading bounds])-1,CPRectGetWidth([_subheading bounds]),1)];
var border = [[CPView alloc] initWithFrame:CPRectMake(0, CPRectGetHeight([_subheading bounds]) - 1,CPRectGetWidth([_subheading bounds]), 1)];
[border setAutoresizingMask: CPViewWidthSizable | CPViewMinYMargin];
[border setBackgroundColor:[CPColor grayColor]];
[_subheading addSubview:border];
Expand All @@ -89,7 +88,7 @@ SCLoginFailed = 1;
[_errorMessage setLineBreakMode:CPLineBreakByWordWrapping];
[_errorMessage setBackgroundColor:[CPColor colorWithHexString:"993333"]];
[_errorMessage setTextColor:[CPColor whiteColor]];
var border = [[CPView alloc] initWithFrame:CPRectMake(0,CPRectGetHeight([_errorMessage bounds])-1,CPRectGetWidth([_errorMessage bounds]),1)];
var border = [[CPView alloc] initWithFrame:CPRectMake(0, CPRectGetHeight([_errorMessage bounds]) - 1, CPRectGetWidth([_errorMessage bounds]), 1)];
[border setAutoresizingMask: CPViewWidthSizable | CPViewMinYMargin];
[border setBackgroundColor:[CPColor grayColor]];
[_errorMessage addSubview:border];
Expand Down Expand Up @@ -169,16 +168,15 @@ SCLoginFailed = 1;
[self _setErrorMessageText:passwordError];
else
{
var userIsValid = [_accountValidator validateUsername:[_userField stringValue]];
if (!userIsValid)
[self _setErrorMessageText:@"Please enter a valid username."];
else
if ([_accountValidator validateUsername:[_userField stringValue]])
{
[self _registerUser:[_userField stringValue] password:[_passwordField stringValue]];
[_registeringProgressLabel setHidden:NO];
[_progressSpinner setHidden:NO];
[_loginButton setHidden:YES];
}
else
[self _setErrorMessageText:@"Please enter a valid username."];
}
}
else
Expand Down Expand Up @@ -209,28 +207,26 @@ SCLoginFailed = 1;
/* @ignore */
- (void)_loginUser:(CPString)username password:(CPString)password
{
var shouldRemember = ([_rememberMeButton state] === CPOnState),
loginObject = {'username' : username, 'password' : password, 'remember' : shouldRemember},
request = [CPURLRequest requestWithURL:[[CPBundle mainBundle] objectForInfoDictionaryKey:@"SCAuthLoginURL"] || @"/session/"];
var shouldRemember = ([_rememberMeButton state] === CPOnState),
loginObject = {'username' : username, 'password' : password, 'remember' : shouldRemember},
request = [CPURLRequest requestWithURL:[[CPBundle mainBundle] objectForInfoDictionaryKey:@"SCAuthLoginURL"] || @"/session/"];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:[CPString JSONFromObject:loginObject]];
_loginConnection = [_connectionClass connectionWithRequest:request
delegate:self];
_loginConnection = [_connectionClass connectionWithRequest:request delegate:self];
_loginConnection.username = username;
}

/* @ignore */
- (void)_registerUser:(CPString)username password:(CPString)password
{
var shouldRemember = ([_rememberMeButton state] === CPOnState),
registerObject = {'username' : username, 'password' : password, 'remember' : shouldRemember},
request = [CPURLRequest requestWithURL:[[CPBundle mainBundle] objectForInfoDictionaryKey:@"SCAuthRegistrationURL"] || @"/user/"];
var shouldRemember = ([_rememberMeButton state] === CPOnState),
registerObject = {'username' : username, 'password' : password, 'remember' : shouldRemember},
request = [CPURLRequest requestWithURL:[[CPBundle mainBundle] objectForInfoDictionaryKey:@"SCAuthRegistrationURL"] || @"/user/"];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:[CPString JSONFromObject:registerObject]];
_registrationConnection = [_connectionClass connectionWithRequest:request
delegate:self];
_registrationConnection = [_connectionClass connectionWithRequest:request delegate:self];
_registrationConnection.username = username;
}

Expand All @@ -250,8 +246,7 @@ SCLoginFailed = 1;
var loginFrame = [_loginButton frame];
[_cancelButton setFrameOrigin:CGPointMake(loginFrame.origin.x - [_cancelButton frame].size.width - 8.0,
loginFrame.origin.y)];
[_rememberMeButton setFrameOrigin:CGPointMake(0,
loginFrame.origin.y + loginFrame.size.height / 2 - [_rememberMeButton frame].size.height / 2)];
[_rememberMeButton setFrameOrigin:CGPointMake(0, loginFrame.origin.y + loginFrame.size.height / 2 - [_rememberMeButton frame].size.height / 2)];
[_progressSpinner setFrameOrigin:CGPointMake(loginFrame.origin.x,
loginFrame.origin.y + loginFrame.size.height / 2.0 - [_progressSpinner frame].size.height / 2.0)];
var spinnerFrame = [_progressSpinner frame];
Expand Down Expand Up @@ -309,26 +304,23 @@ SCLoginFailed = 1;
var request = [CPURLRequest requestWithURL:([[CPBundle mainBundle] objectForInfoDictionaryKey:@"SCAuthUserCheckURL"] || @"/user/") + [_userField stringValue]];

[request setHTTPMethod:@"GET"];
_userCheckConnection = [_connectionClass connectionWithRequest:request
delegate:self];
_userCheckConnection = [_connectionClass connectionWithRequest:request delegate:self];
}

- (void)_setMessage:(CPString)aMessage inTextBox:(CPTextField)textBox
{
if (!aMessage)
{
[textBox setStringValue:""];
[textBox setHidden:YES];
}
else
if (aMessage)
{
[textBox setStringValue:aMessage];
[textBox setHidden:NO];
var fieldFrame = [_formFieldContainer frame],
size = [aMessage sizeWithFont:[textBox font]
inWidth:fieldFrame.size.width + 16.0];
var size = [aMessage sizeWithFont:[textBox font] inWidth:[_formFieldContainer frame].size.width + 16.0];
[textBox setFrame:CGRectMake(0, 0, size.width, size.height + 18.0)];
}
else
{
[textBox setStringValue:""];
[textBox setHidden:YES];
}
}

/*!
Expand Down Expand Up @@ -403,7 +395,7 @@ SCLoginFailed = 1;
{
var currentErrorMessage = ([_errorMessage isHidden] ? nil : [_errorMessage stringValue]);
[self _setDefaultHiddenSettings];
[self setSubheadingText:"Welcome! Looks like you're a new user. Just choose a password to register."];
[self setSubheadingText:"Welcome! Looks like you're a new user. Just choose a password to register."];
if ([_loginButton title] === RegisterTitle && currentErrorMessage)
[self _setErrorMessageText:currentErrorMessage];
[_loginButton setTitle:RegisterTitle];
Expand Down Expand Up @@ -549,45 +541,44 @@ SCLoginFailed = 1;
var statusCode = [aResponse statusCode];
switch (aConnection)
{
case _userCheckConnection:
if (statusCode === 200)
return;
else if (statusCode == 404)
[self _setDialogModeToRegister];
else
[self _userCheckFailedWithStatusCode:statusCode];
break;
case _userCheckConnection:
if (statusCode === 200)
return;
else if (statusCode == 404)
[self _setDialogModeToRegister];
else
[self _userCheckFailedWithStatusCode:statusCode];
break;

case _loginConnection:
if (statusCode === 200)
return;
else
{
if (statusCode === 403)
case _loginConnection:
if (statusCode === 200)
return;
else
{
// Between clicking login and the response, the dialog switched to register mode
// so we no longer care about this login error
if ([_loginButton title] !== RegisterTitle)
[self _loginFailedWithError:@"Incorrect username or password." statusCode:statusCode];
if (statusCode === 403)
{
// Between clicking login and the response, the dialog switched to register mode
// so we no longer care about this login error
if ([_loginButton title] !== RegisterTitle)
[self _loginFailedWithError:@"Incorrect username or password." statusCode:statusCode];
}
else
[self _loginFailedWithError:GenericErrorMessage statusCode:statusCode];
}
else
[self _loginFailedWithError:GenericErrorMessage statusCode:statusCode];
}
break;
break;

case _registrationConnection:
if (statusCode === 200)
return;
else
{
if (statusCode === 409)
[self _setDialogModeToLogin];
case _registrationConnection:
if (statusCode === 200)
return;
else
[self _registrationFailedWithError:GenericErrorMessage statusCode:statusCode];
}
{
if (statusCode === 409)
[self _setDialogModeToLogin];
else
[self _registrationFailedWithError:GenericErrorMessage statusCode:statusCode];
}
}
[aConnection cancel];

}

- (void)connection:(CPURLConnection)aConnection didReceiveData:(CPString)data
Expand Down

0 comments on commit 4a81511

Please sign in to comment.