Commit fe0f31fe authored by A. Wilcox's avatar A. Wilcox

XUI: Remove X-specific account/account mgr. Unnecessary cruft.

parent 5af1d237
//
// AuXAccountManager.h
// AuctionsX
//
// Created by Andrew Wilcox on 07/02/2012.
// Copyright (c) 2012 Wilcox Technologies. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "AuXAccount.h"
@interface AuXAccountManager : NSObject {
NSMutableArray *accounts;
NSMutableDictionary *plistContents;
}
+(AuXAccountManager *)sharedAccountManager;
-(void)addAccount:(AuXAccount *)account;
-(AuXAccount *)accountAtIndex:(unsigned int)index;
-(NSArray *)accountsForSite:(AuSiteCode)site;
@end
//
// AuXAccountManager.mm
// AuctionsX
//
// Created by Andrew Wilcox on 07/02/2012.
// Copyright (c) 2012 Wilcox Technologies. All rights reserved.
//
#import <libkern/OSAtomic.h>
#import "AuXAccountManager.h"
#include <Models/eBaySite.h>
#include <Models/EtsySite.h>
#import <TargetConditionals.h>
#if 1
inline bool OSAtomicCompareAndSwapPtrBarrier(void *predicate,
void *replacement,
void * volatile *theValue) {
#if defined(__LP64__) && __LP64__
return OSAtomicCompareAndSwap64Barrier((int64_t)predicate,
(int64_t)replacement,
(int64_t *)theValue);
#else // defined(__LP64__) && __LP64__
return OSAtomicCompareAndSwap32Barrier((int32_t)predicate,
(int32_t)replacement,
(int32_t *)theValue);
#endif // defined(__LP64__) && __LP64__
}
#endif // MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
static void * volatile sharedAccountInstance = nil;
@interface AuXAccountManager(Internal)
-(void)accountChangedFriendly:(AuXAccount *)account;
-(void)loadFromPlist;
-(void)synchPlist;
@end
@implementation AuXAccountManager
#pragma mark - Singleton method
+(AuXAccountManager *)sharedAccountManager
{
while (!sharedAccountInstance) {
AuXAccountManager *temp = [[self alloc] init];
if(!OSAtomicCompareAndSwapPtrBarrier(0x0, temp, &sharedAccountInstance)) {
[temp release];
}
}
return static_cast<AuXAccountManager *>(sharedAccountInstance);
}
#pragma mark - Plist handling methods
inline NSString *getPlistPath()
{
NSString *path;
NSString *root = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
#if !TARGET_OS_IPHONE
path = [root stringByAppendingPathComponent:@"Application Support"];
path = [path stringByAppendingPathComponent:@"Auctions"];
#else
path = root;
#endif
path = [path stringByAppendingPathComponent:@"Accounts.plist"];
return path;
}
-(void)loadFromPlist
{
NSString *error;
NSPropertyListFormat format;
NSString *path = getPlistPath();
NSArray *account_dict;
if(![[NSFileManager defaultManager] fileExistsAtPath:path])
{
return; // no accounts
};
NSData *plist = [[NSFileManager defaultManager] contentsAtPath:path];
plistContents = [NSPropertyListSerialization propertyListFromData:plist mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&error];
if(!plistContents)
NSLog(@"%@", [NSString stringWithFormat:@"Couldn't read accounts plist (%@)!",error]);
account_dict = [plistContents objectForKey:@"Accounts"];
for(unsigned int i = 0; i < [account_dict count]; i++)
{
NSDictionary *this_account = [account_dict objectAtIndex:i];
if([[this_account objectForKey:@"Disabled"] boolValue]) continue;
AuXAccount *account = [[AuXAccount alloc] init];
[account setUpdating:YES];
[account setFriendly:[this_account objectForKey:@"Friendly"]];
[account setToken:[this_account objectForKey:@"Token"]];
[account setUsername:[this_account objectForKey:@"Username"]];
unsigned int site_code = [((NSNumber *)[this_account objectForKey:@"Site"]) intValue];
switch (site_code)
{
case SITE_EBAY:
[account setSite:new eBaySite([[account username] UTF8String], [[account token] UTF8String])];
break;
case SITE_ETSY:
[account setSite:new EtsySite([[account username] UTF8String], [[account token] UTF8String])];
break;
default:
NSLog(@"Unknown site code");
[account release];
continue;
};
[accounts addObject:account];
[account setUpdating:NO];
[account release];
};
}
-(void)synchPlist
{
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistContents format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
[[NSFileManager defaultManager] createFileAtPath:getPlistPath() contents:plistData attributes:nil];
}
#pragma mark - Initialiser
-(id)init
{
accounts = [[NSMutableArray alloc] initWithCapacity:0];
[self loadFromPlist];
return self;
}
-(void)dealloc
{
[accounts release];
[plistContents release];
[super dealloc];
}
#pragma mark - Account handling methods
-(void)addAccount:(AuXAccount *)account
{
@synchronized(plistContents)
{
NSMutableArray *arrayDict = [plistContents objectForKey:@"Accounts"];
[arrayDict addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[account username],@"Username",[account token],@"Token",[NSNumber numberWithInt:[account site]->siteCode()],@"Site",[account friendly],@"Friendly",nil]];
[self synchPlist];
[accounts addObject:account];
}
}
-(void)accountChangedFriendly:(AuXAccount *)account
{
@synchronized(plistContents)
{
NSArray *arrayDict = [plistContents objectForKey:@"Accounts"];
unsigned int accountIndex = [accounts indexOfObject:account];
[[arrayDict objectAtIndex:accountIndex] setObject:[account friendly] forKey:@"Friendly"];
[self synchPlist];
}
}
#pragma mark - Account retrieval methods
-(AuXAccount *)accountAtIndex:(unsigned int)index
{
return [accounts objectAtIndex:index];
}
-(NSArray *)accountsForSite:(AuSiteCode)site
{
NSMutableArray *siteAccounts = [NSMutableArray arrayWithCapacity:0];
for(unsigned int nextAccount = 0; nextAccount < [accounts count]; nextAccount++)
{
AuXAccount *acct = [accounts objectAtIndex:nextAccount];
if([acct site]->siteCode() == site)
[siteAccounts addObject:acct];
}
return siteAccounts;
}
@end
//
// AuXAccount.h
// AuctionsX
//
// Created by Andrew Wilcox on 07/02/2012.
// Copyright (c) 2012 Wilcox Technologies. All rights reserved.
//
#import <Foundation/Foundation.h>
#include <Models/AuSite.h>
@interface AuXAccount : NSObject {
AuSite *site;
NSString *username;
NSString *token;
NSString *friendly;
BOOL isUpdating;
}
-(NSString *)friendly;
-(void)setFriendly:(NSString *)_friendly;
-(BOOL)isUpdating;
-(void)setUpdating:(BOOL)updating;
-(AuSite *)site;
-(void)setSite:(AuSite *)_site;
-(NSString *)token;
-(void)setToken:(NSString *)_token;
-(NSString *)username;
-(void)setUsername:(NSString *)_username;
@end
//
// AuXAccount.mm
// AuctionsX
//
// Created by Andrew Wilcox on 07/02/2012.
// Copyright (c) 2012 Wilcox Technologies. All rights reserved.
//
#import "AuXAccount.h"
#import "AuXAccountManager.h"
@implementation AuXAccount
-(BOOL)isUpdating
{
return isUpdating;
}
-(void)setUpdating:(BOOL)nowUpdating
{
isUpdating = nowUpdating;
}
-(AuSite *)site
{
return site;
}
-(void)setSite:(AuSite *)_site
{
site = _site;
}
-(NSString *)token
{
return token;
}
-(void)setToken:(NSString *)_token
{
[token release];
token = [_token retain];
}
-(NSString *)username
{
return username;
}
-(void)setUsername:(NSString *)_username
{
[username release];
username = [_username retain];
}
-(NSString *)description
{
return [NSString stringWithFormat:@"Account %@ on %p (%@)",[self username],[self site],[self friendly]];
}
-(NSString *)friendly
{
return friendly;
}
-(void)setFriendly:(NSString *)_friendly
{
[friendly release];
friendly = [_friendly retain];
if(!isUpdating) [[AuXAccountManager sharedAccountManager] accountChangedFriendly:self];
}
@end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment