Permalink
Browse files

Store account credentials using new CPUserDefaults

  • Loading branch information...
1 parent 7790372 commit f2bc5b90bb962338eec377eae0071df426ea439e @benlangfeld committed Nov 5, 2010
Showing with 57 additions and 32 deletions.
  1. +47 −14 Controllers/AccountsController.j
  2. +1 −1 Controllers/ManageAccountsController.j
  3. +9 −9 Models/Account.j
  4. +0 −8 main.j
@@ -27,10 +27,6 @@
var SharedController = nil;
-AccountWasCreatedNotification = @"AccountWasCreatedNotification";
-AccountWasEditedNotification = @"AccountWasEditedNotification";
-AccountWasDeletedNotification = @"AccountWasDeletedNotification";
-
@implementation AccountsController : CPViewController
{
CPArray accounts @accessors;
@@ -59,6 +55,21 @@ AccountWasDeletedNotification = @"AccountWasDeletedNotification";
selector:@selector(expandAccount:)
name:TNStropheRosterRetrievedNotification
object:nil];
+ [[CPNotificationCenter defaultCenter]
+ addObserver:self
+ selector:@selector(storeAccounts)
+ name:AccountWasAddedNotification
+ object:nil];
+ [[CPNotificationCenter defaultCenter]
+ addObserver:self
+ selector:@selector(storeAccounts)
+ name:AccountWasEditedNotification
+ object:nil];
+ [[CPNotificationCenter defaultCenter]
+ addObserver:self
+ selector:@selector(storeAccounts)
+ name:AccountWasDeletedNotification
+ object:nil];
[self setupStoredAccounts];
}
return self;
@@ -110,12 +121,31 @@ AccountWasDeletedNotification = @"AccountWasDeletedNotification";
- (void)setupStoredAccounts
{
- for (var i = 0; i < localStorage.length; i++)
+ var storedAccounts = [[CPUserDefaults standardUserDefaults] objectForKey:@"accounts"];
+ for (var i = 0; i < [storedAccounts count]; i++)
+ {
+ var account = [storedAccounts objectAtIndex:i];
+ [self addAccountWithJID:[account valueForKey:@"jid"] andPassword:[account valueForKey:@"password"] enabled:[account valueForKey:@"enabled"]];
+ }
+}
+
+- (CPArray)accountCredentials
+{
+ var credentials = [CPArray array];
+ for (var i = 0; i < [accounts count]; i++)
{
- var JID = localStorage.key(i),
- object = localStorage.getObject(JID);
- [self addAccountWithJID:JID andPassword:object["password"] enabled:object["enabled"]];
+ var account = [accounts objectAtIndex:i],
+ creds = [CPDictionary dictionaryWithObjectsAndKeys:[account JID], @"jid",
+ [account password], @"password",
+ [account isEnabled], @"enabled"];
+ [credentials addObject:creds];
}
+ return credentials;
+}
+
+- (void)storeAccounts
+{
+ [[CPUserDefaults standardUserDefaults] setObject:[self accountCredentials] forKey:@"accounts"];
}
- (void)awakeFromCib
@@ -223,11 +253,15 @@ AccountWasDeletedNotification = @"AccountWasDeletedNotification";
- (void)addAccountWithJID:(CPString)aJID andPassword:(CPString)aPassword enabled:(BOOL)isEnabled
{
CPLog.debug("Adding account with JID " + aJID + " and password " + aPassword);
- var account = [Account accountWithJID:aJID andPassword:aPassword enabled:isEnabled];
- [accounts addObject:account];
- [[CPNotificationCenter defaultCenter] postNotificationName:AccountWasCreatedNotification object:self];
- if (isEnabled)
- [account connect];
+ [self addAccount:[Account accountWithJID:aJID andPassword:aPassword enabled:isEnabled]];
+}
+
+- (void)addAccount:(Account)anAccount
+{
+ [accounts addObject:anAccount];
+ [[CPNotificationCenter defaultCenter] postNotificationName:AccountWasAddedNotification object:self];
+ if ([anAccount isEnabled])
+ [anAccount connect];
}
- (void)deleteAccountWithJID:(CPString)aJID
@@ -240,7 +274,6 @@ AccountWasDeletedNotification = @"AccountWasDeletedNotification";
{
[anAccount disconnect];
[accounts removeObject:anAccount];
- localStorage.removeItem([anAccount JID]);
[[CPNotificationCenter defaultCenter] postNotificationName:AccountWasDeletedNotification object:self];
[self reload];
}
@@ -54,7 +54,7 @@ var SharedController = nil;
[[CPNotificationCenter defaultCenter]
addObserver:self
selector:@selector(accountsWereUpdated:)
- name:AccountWasCreatedNotification
+ name:AccountWasAddedNotification
object:nil];
[[CPNotificationCenter defaultCenter]
addObserver:self
View
@@ -29,6 +29,11 @@ ConnectionStatusConnected = @"Connected";
ConnectionStatusConnecting = @"Connecting";
ConnectionStatusDisconnected = @"Disconnected";
+AccountWasCreatedNotification = @"AccountWasCreatedNotification";
+AccountWasAddedNotification = @"AccountWasAddedNotification";
+AccountWasEditedNotification = @"AccountWasEditedNotification";
+AccountWasDeletedNotification = @"AccountWasDeletedNotification";
+
@implementation Account : TNStropheConnection
{
TNStropheRoster roster @accessors;
@@ -51,16 +56,11 @@ ConnectionStatusDisconnected = @"Disconnected";
roster = [TNStropheRoster rosterWithConnection:self];
[self setDelegate:self];
[roster setDelegate:self];
- [self store];
+ [[CPNotificationCenter defaultCenter] postNotificationName:AccountWasCreatedNotification object:self];
}
return self;
}
-- (void)store
-{
- localStorage.setObject([self JID], {"password": [self password], "enabled": enabled});
-}
-
- (CPString)description
{
return [self JID];
@@ -125,15 +125,15 @@ ConnectionStatusDisconnected = @"Disconnected";
- (void)enable
{
enabled = YES;
- localStorage.setObject([self JID], {"password": [self password], "enabled": true});
+ [[CPNotificationCenter defaultCenter] postNotificationName:AccountWasEditedNotification object:self];
[self connect];
}
- (void)disable
{
[self disconnect];
enabled = NO;
- localStorage.setObject([self JID], {"password": [self password], "enabled": false});
+ [[CPNotificationCenter defaultCenter] postNotificationName:AccountWasEditedNotification object:self];
}
- (EditAccountController)editAccountController
@@ -149,7 +149,7 @@ ConnectionStatusDisconnected = @"Disconnected";
[self disconnect];
[self setJID:aJID];
[self setPassword:aPassword];
- [self store];
+ [[CPNotificationCenter defaultCenter] postNotificationName:AccountWasEditedNotification object:self];
[[CPNotificationCenter defaultCenter]
addObserver:self
selector:@selector(connect)
View
8 main.j
@@ -25,13 +25,5 @@
function main(args, namedArgs)
{
- Storage.prototype.setObject = function(key, value) {
- this.setItem(key, JSON.stringify(value));
- }
-
- Storage.prototype.getObject = function(key) {
- return this.getItem(key) && JSON.parse(this.getItem(key));
- }
-
CPApplicationMain(args, namedArgs);
}

0 comments on commit f2bc5b9

Please sign in to comment.