From: Benjamin R. <be...@us...> - 2004-03-16 03:06:06
|
Update of /cvsroot/ljkit/LJKit In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10668 Modified Files: LJAccount.h LJAccount.m Log Message: Added default account tracking. Index: LJAccount.h =================================================================== RCS file: /cvsroot/ljkit/LJKit/LJAccount.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** LJAccount.h 10 Jan 2004 08:34:06 -0000 1.2 --- LJAccount.h 16 Mar 2004 02:56:49 -0000 1.3 *************** *** 19,22 **** --- 19,25 ---- You may contact the author via email at be...@li.... */ + /* + 2004-03-13 [BPR] Added default account methods. + */ #import <Cocoa/Cocoa.h> *************** *** 112,116 **** /*! ! @const LJAccountDidLoginNotification @discussion Posted after an account object fails to log in. --- 115,119 ---- /*! ! @const LJAccountDidNotLoginNotification @discussion Posted after an account object fails to log in. *************** *** 184,187 **** --- 187,242 ---- + (LJAccount *)accountWithIdentifier:(NSString *)identifier; + + /*! + @method defaultAccount + @abstract Returns the default account. + @result An account reference if any exists. + @discussion + The LJAccount keeps an internal list of all known account objects. + This method will return the one designated as default. If you have + never set a default account, and arbitrary account will be returned. + This method is guaranteed not to return nil unless there are no + LJAccount objects in memory. + */ + + (LJAccount *)defaultAccount; + + /*! + @method setDefaultAccount: + @abstract Sets the default account. + @param newDefault The account to be set as the new default. + @discussion + Use this method to designate the account object to be returned by + defaultAccount. + The identifier of the selected account will be stored in the user defaults + database. As account objects are unarchived, they check their identifiers + against the defaults database. If a match is found, the unarchived object + will be registered as the default account. Thus, default account status + is maintained across executions of your application if you archive account + object instances. If you modify the defaults key directly default account + status will not be synchronized and the results are undefined. + */ + + (void)setDefaultAccount:(LJAccount *)newDefault; + + /*! + @method setDefault: + @abstract Makes the receiver the default. + @param flag YES to make the receiver the default. + @discussion + Use this method to designate the receiver to be the object returned by + defaultAccount. If flag is NO, no action is taken. + */ + - (void)setDefault:(BOOL)flag; + + /*! + @method isDefault + @abstract Determines if the receiver is the default account. + @returns YES if the receiver is the default account, NO otherwise. + @discussion + Use this method to determine if the receiver is the object returned by + defaultAccount. + */ + - (BOOL)isDefault; + + /*! @method initWithUsername: Index: LJAccount.m =================================================================== RCS file: /cvsroot/ljkit/LJKit/LJAccount.m,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** LJAccount.m 11 Feb 2004 08:52:57 -0000 1.4 --- LJAccount.m 16 Mar 2004 02:56:49 -0000 1.5 *************** *** 23,26 **** --- 23,30 ---- 2004-01-10 [BPR] Changed exception handling (gets strings from main bundle, not the LJKit bundle). + 2004-02-23 [BPR] Updated initWithUsername: to call LJServer's designated initializer. + (The account field wasn't being set.) + Added method: description + 2004-03-13 [BPR] Added default account methods. */ *************** *** 98,101 **** --- 102,106 ---- } + + (NSArray *)allAccounts { *************** *** 110,124 **** } + (LJAccount *)accountWithIdentifier:(NSString *)identifier { ! LJAccount *account = gAccountListHead; ! while (account) { ! if ([[account identifier] isEqualToString:identifier]) ! return account; ! account = account->_nextAccount; } - return nil; } - (id)init --- 115,184 ---- } + + /* In Objective-C, [nil anyMessage] == nil, which makes traversing list links a breeze! */ + - (LJAccount *)_accountWithIdentifier:(NSString *)identifier + { + if ([[self identifier] isEqualToString:identifier]) { + return self; + } else { + return [_nextAccount _accountWithIdentifier:identifier]; + } + } + + + (LJAccount *)accountWithIdentifier:(NSString *)identifier { ! return [gAccountListHead _accountWithIdentifier:identifier]; ! } ! ! + (LJAccount *)defaultAccount ! { ! return gAccountListHead; ! } ! ! ! + (void)setDefaultAccount:(LJAccount *)newDefault ! { ! NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; ! ! // Must save here in case newDefault is already the list head ! [defaults setObject:[newDefault identifier] forKey:@"LJDefaultAccountIdentifier"]; ! if (newDefault != gAccountListHead) { ! LJAccount *previous = gAccountListHead; ! LJAccount *current = gAccountListHead->_nextAccount; ! ! while (current) { ! if (current == newDefault) { ! [gAccountListHead setDefault:NO]; ! // remove newDefault from the list ! previous->_nextAccount = newDefault->_nextAccount; ! // put newDefault at the front ! newDefault->_nextAccount = gAccountListHead; ! gAccountListHead = newDefault; ! return; ! } ! previous = current; ! current = current->_nextAccount; ! } ! [NSException raise:NSInternalInconsistencyException ! format:@"New proposed default account not in the list."]; ! } ! } ! ! ! - (BOOL)isDefault ! { ! return (gAccountListHead == self); ! } ! ! ! - (void)setDefault:(BOOL)flag ! { ! if (flag) { ! [LJAccount setDefaultAccount:self]; } } + - (id)init *************** *** 126,132 **** self = [super init]; if (self) { ! // add self to global linked list of accounts ! _nextAccount = gAccountListHead; ! gAccountListHead = self; } return self; --- 186,197 ---- self = [super init]; if (self) { ! // add self to global linked list of accounts. ! // can't overwrite the head because it is the default now ! if (nil == gAccountListHead) { ! gAccountListHead = self; ! } else { ! _nextAccount = gAccountListHead->_nextAccount; ! gAccountListHead->_nextAccount = self; ! } } return self; *************** *** 135,143 **** - (id)initWithUsername:(NSString *)username { if ([self init]) { NSParameterAssert(username); _username = [[username lowercaseString] retain]; _fullname = [_username copy]; ! _server = [[LJServer alloc] init]; } return self; --- 200,211 ---- - (id)initWithUsername:(NSString *)username { + NSURL *defaultURL; + if ([self init]) { NSParameterAssert(username); _username = [[username lowercaseString] retain]; _fullname = [_username copy]; ! defaultURL = [NSURL URLWithString:@"http://www.livejournal.com"]; ! _server = [[LJServer alloc] initWithURL:defaultURL account:self]; } return self; *************** *** 153,156 **** --- 221,227 ---- { if ([self init]) { + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + NSString *theIdentifier; + _username = [[decoder decodeObjectForKey:@"LJAccountUsername"] retain]; _fullname = [[decoder decodeObjectForKey:@"LJAccountFullname"] retain]; *************** *** 168,171 **** --- 239,247 ---- // custom info _customInfo = [[decoder decodeObjectForKey:@"LJAccountCustomInfo"] retain]; + // check defaults to see if this is supposed to be the default account + theIdentifier = [defaults stringForKey:@"LJDefaultAccountIdentifier"]; + if ([theIdentifier isEqualToString:[self identifier]]) { + [LJAccount setDefaultAccount:self]; + } } return self; *************** *** 570,574 **** - (NSString *)identifier { ! NSURL *serverURL = [_server url]; int p = [[serverURL port] intValue]; return [NSString stringWithFormat:@"%@@%@:%u", --- 646,650 ---- - (NSString *)identifier { ! NSURL *serverURL = [_server URL]; int p = [[serverURL port] intValue]; return [NSString stringWithFormat:@"%@@%@:%u", *************** *** 619,621 **** --- 695,702 ---- } + - (NSString *)description + { + return [self identifier]; + } + @end |