William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | /*
* Adium is the legal property of its developers, whose names are listed in the copyright file included
* with this source distribution.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "AdiumAccounts.h"
#import <Adium/AIAccountControllerProtocol.h>
#import <Adium/AIAccount.h>
#import <Adium/AIService.h>
#import <AIUtilities/AIArrayAdditions.h>
#import <AIUtilities/AIAttributedStringAdditions.h>
//Preference keys
#define TOP_ACCOUNT_ID @"TopAccountID" //Highest account object ID
#define ACCOUNT_LIST @"Accounts" //Array of accounts
#define ACCOUNT_TYPE @"Type" //Account type
#define ACCOUNT_SERVICE @"Service" //Account service
#define ACCOUNT_UID @"UID" //Account UID
#define ACCOUNT_OBJECT_ID @"ObjectID" //Account object ID
@interface AdiumAccounts ()
- (void)_loadAccounts;
- (void)_saveAccounts;
- (NSString *)_generateUniqueInternalObjectID;
- (NSString *)_upgradeServiceID:(NSString *)serviceID forAccountDict:(NSDictionary *)accountDict;
- (void)upgradeAccounts;
@end
/*!
* @class AdiumAccounts
* @brief Class to handle AIAccount access and creation
*
* This is a private class used by AIAccountController, its public interface.
*/
@implementation AdiumAccounts
- (id)init {
if ((self = [super init])) {
accounts = [[NSMutableArray alloc] init];
unloadableAccounts = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc {
[accounts release];
[unloadableAccounts release];
[super dealloc];
}
/*!
* @brief Finish Initing
*
* Requires the all AIServices have registered
*/
- (void)controllerDidLoad
{
[self _loadAccounts];
[self upgradeAccounts];
}
//Accounts -------------------------------------------------------------------------------------------------------
#pragma mark Accounts
/*!
* @brief Returns an array of all available accounts
*
* @return NSArray of AIAccount instances
*/
- (NSArray *)accounts
{
return accounts;
}
/*!
* @brief Returns an array of accounts compatible with a service
*
* @param service AIService for compatible accounts
* @result NSArray of AIAccount instances
*/
- (NSArray *)accountsCompatibleWithService:(AIService *)service
{
NSMutableArray *matchingAccounts = [NSMutableArray array];
AIAccount *account;
NSString *serviceClass = [service serviceClass];
for (account in accounts) {
if (account.enabled &&
[[account.service serviceClass] isEqualToString:serviceClass]) {
[matchingAccounts addObject:account];
}
}
return matchingAccounts;
}
- (AIAccount *)accountWithInternalObjectID:(NSString *)objectID
{
AIAccount *account = nil;
//Some ancient preferences have NSNumbers instead of NSStrings. Work properly, silently.
if ([objectID isKindOfClass:[NSNumber class]]) objectID = [(NSNumber *)objectID stringValue];
for (account in accounts) {
if ([objectID isEqualToString:account.internalObjectID]) break;
}
return account;
}
//Editing --------------------------------------------------------------------------------------------------------------
#pragma mark Editing
/*!
* @brief Create an account
*
* The account is not added to Adium's list of accounts, this must be done separately with addAccount:
* @param service AIService for the account
* @param inUID NSString userID for the account
* @return AIAccount instance that was created
*/
- (AIAccount *)createAccountWithService:(AIService *)service UID:(NSString *)inUID
{
return [service accountWithUID:inUID internalObjectID:[self _generateUniqueInternalObjectID]];
}
/*!
* @brief Add an account
*
* @param inAccount AIAccount to add
*/
- (void)addAccount:(AIAccount *)inAccount
{
[accounts addObject:inAccount];
[self _saveAccounts];
}
/*!
* @brief Delete an account
*
* @param inAccount AIAccount to delete
*/
- (void)deleteAccount:(AIAccount *)inAccount
{
//Shut down the account in preparation for release
//XXX - Is this sufficient? Don't some accounts take a while to disconnect and all? -ai
[inAccount willBeDeleted];
[adium.accountController forgetPasswordForAccount:inAccount];
//Remove from our array
[accounts removeObject:inAccount];
[self _saveAccounts];
}
/*!
* @brief Move an account
*
* @param account AIAccount to move
* @param destIndex Index to place the account
* @return new index of the account
*/
- (NSUInteger)moveAccount:(AIAccount *)account toIndex:(NSUInteger)destIndex
{
[accounts moveObject:account toIndex:destIndex];
[self _saveAccounts];
return [accounts indexOfObject:account];
}
/*!
* @brief An account's UID changed
*
* Save our account array, which stores the account's UID permanently
*/
- (void)accountDidChangeUID:(AIAccount *)account
{
[self _saveAccounts];
}
/*!
* @brief Generate a unique account InternalObjectID
*
* @return NSString unique InternalObjectID
*/
//XXX - This setup leaves the possibility that mangled preferences files would create multiple accounts with the same ID -ai
- (NSString *)_generateUniqueInternalObjectID
{
NSInteger topAccountID = [[adium.preferenceController preferenceForKey:TOP_ACCOUNT_ID group:PREF_GROUP_ACCOUNTS] integerValue];
NSString *internalObjectID = [NSString stringWithFormat:@"%ld",topAccountID];
[adium.preferenceController setPreference:[NSNumber numberWithInteger:topAccountID + 1]
forKey:TOP_ACCOUNT_ID
group:PREF_GROUP_ACCOUNTS];
return internalObjectID;
}
//Storage --------------------------------------------------------------------------------------------------------------
#pragma mark Storage
/*!
* @brief Load accounts from disk
*/
- (void)_loadAccounts
{
NSArray *accountList = [adium.preferenceController preferenceForKey:ACCOUNT_LIST group:PREF_GROUP_ACCOUNTS];
NSDictionary *accountDict;
//Create an instance of every saved account
for (accountDict in accountList) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *serviceID = [self _upgradeServiceID:[accountDict objectForKey:ACCOUNT_TYPE] forAccountDict:accountDict];
AIAccount *newAccount;
//Fetch the account service, UID, and ID
AIService *service = [adium.accountController serviceWithUniqueID:serviceID];
NSString *accountUID = [accountDict objectForKey:ACCOUNT_UID];
NSString *internalObjectID = [accountDict objectForKey:ACCOUNT_OBJECT_ID];
//Create the account and add it to our array
if (service && accountUID && [accountUID length]) {
if ((newAccount = [service accountWithUID:accountUID internalObjectID:internalObjectID])) {
[accounts addObject:newAccount];
} else {
NSLog(@"Could not load account %@",accountDict);
[unloadableAccounts addObject:accountDict];
}
} else {
if ([accountUID length]) {
NSLog(@"Available services are %@: could not load account %@ on service %@ (service %@)",
adium.accountController.services, accountDict, serviceID, service);
[unloadableAccounts addObject:accountDict];
} else {
AILog(@"Ignored an account with a 0 length accountUID: %@", accountDict);
}
}
[pool release];
}
//Broadcast an account list changed notification
[[NSNotificationCenter defaultCenter] postNotificationName:Account_ListChanged object:nil userInfo:nil];
}
/*!
* @brief ServiceID upgrade code (v0.63 -> v0.70 for libpurple, v0.70 -> v0.80 for bonjour, v1.0 -> v1.1 for libpurple)
*
* The changed name will only be saved if some other account change, such as adding an account, occurs,
* so this code should remain indefinitely to provide an upgrade path to people whose service IDs are in an
* old style.
*
* @param serviceID NSString service ID (old or new)
* @param accountDict Dictionary of the saved account
* @return NSString service ID (new), or nil if unable to upgrade
*/
- (NSString *)_upgradeServiceID:(NSString *)serviceID forAccountDict:(NSDictionary *)accountDict
{
//Libgaim
if ([serviceID rangeOfString:@"libgaim" options:(NSLiteralSearch | NSAnchoredSearch)].location != NSNotFound) {
NSMutableString *newServiceID = [serviceID mutableCopy];
[newServiceID replaceOccurrencesOfString:@"libgaim"
withString:@"libpurple"
options:(NSLiteralSearch | NSAnchoredSearch)
range:NSMakeRange(0, [newServiceID length])];
serviceID = [newServiceID autorelease];
} else if ([serviceID rangeOfString:@"LIBGAIM" options:(NSLiteralSearch | NSAnchoredSearch | NSBackwardsSearch)].location != NSNotFound) {
if ([serviceID isEqualToString:@"AIM-LIBGAIM"]) {
NSString *uid = [accountDict objectForKey:ACCOUNT_UID];
if (uid && [uid length]) {
const char firstCharacter = [uid characterAtIndex:0];
if ([uid hasSuffix:@"@mac.com"]) {
serviceID = @"libpurple-oscar-Mac";
} else if (firstCharacter >= '0' && firstCharacter <= '9') {
serviceID = @"libpurple-oscar-ICQ";
} else {
serviceID = @"libpurple-oscar-AIM";
}
}
} else if ([serviceID isEqualToString:@"GaduGadu-LIBGAIM"]) {
serviceID = @"libpurple-Gadu-Gadu";
} else if ([serviceID isEqualToString:@"Jabber-LIBGAIM"]) {
serviceID = @"libpurple-Jabber";
} else if ([serviceID isEqualToString:@"MSN-LIBGAIM"]) {
serviceID = @"libpurple-MSN";
} else if ([serviceID isEqualToString:@"Napster-LIBGAIM"]) {
serviceID = @"libpurple-Napster";
} else if ([serviceID isEqualToString:@"Novell-LIBGAIM"]) {
serviceID = @"libpurple-GroupWise";
} else if ([serviceID isEqualToString:@"Sametime-LIBGAIM"]) {
serviceID = @"libpurple-Sametime";
} else if ([serviceID isEqualToString:@"Yahoo-LIBGAIM"]) {
serviceID = @"libpurple-Yahoo!";
} else if ([serviceID isEqualToString:@"Yahoo-Japan-LIBGAIM"]) {
serviceID = @"libpurple-Yahoo!-Japan";
}
} else if ([serviceID isEqualToString:@"rvous-libezv"])
serviceID = @"bonjour-libezv";
else if ([serviceID isEqualToString:@"joscar-OSCAR-AIM"])
serviceID = @"libpurple-oscar-AIM";
else if ([serviceID isEqualToString:@"joscar-OSCAR-dotMac"])
serviceID = @"libpurple-oscar-Mac";
return serviceID;
}
/*!
* @brief Save accounts to disk
*/
- (void)_saveAccounts
{
NSMutableArray *flatAccounts = [NSMutableArray array];
AIAccount *account;
//Build a flattened array of the accounts
for (account in accounts) {
if (![account isTemporary]) {
NSMutableDictionary *flatAccount = [NSMutableDictionary dictionary];
AIService *service = account.service;
[flatAccount setObject:service.serviceCodeUniqueID forKey:ACCOUNT_TYPE]; //Unique plugin ID
[flatAccount setObject:service.serviceID forKey:ACCOUNT_SERVICE]; //Shared service ID
[flatAccount setObject:account.UID forKey:ACCOUNT_UID]; //Account UID
[flatAccount setObject:account.internalObjectID forKey:ACCOUNT_OBJECT_ID]; //Account Object ID
[flatAccounts addObject:flatAccount];
}
}
//Add any unloadable accounts so they're not lost
[flatAccounts addObjectsFromArray:unloadableAccounts];
//Save and broadcast an account list changed notification
[adium.preferenceController setPreference:flatAccounts forKey:ACCOUNT_LIST group:PREF_GROUP_ACCOUNTS];
[[NSNotificationCenter defaultCenter] postNotificationName:Account_ListChanged object:nil userInfo:nil];
}
/*!
* @brief Perform upgrades for a new version
*
* 1.0: KEY_ACCOUNT_DISPLAY_NAME and @"TextProfile" cleared if @"" and moved to global if identical on all accounts
*/
- (void)upgradeAccounts
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSNumber *upgradedAccounts = [userDefaults objectForKey:@"Adium:Account Prefs Upgraded for 1.0"];
if (!upgradedAccounts || ![upgradedAccounts boolValue]) {
[userDefaults setObject:[NSNumber numberWithBool:YES] forKey:@"Adium:Account Prefs Upgraded for 1.0"];
AIAccount *account;
NSEnumerator *enumerator, *keyEnumerator;
NSString *key;
//Adium 0.8x would store @"" in preferences which we now want to be able to inherit global values if they don't have a value.
NSSet *keysWeNowUseGlobally = [NSSet setWithObjects:KEY_ACCOUNT_DISPLAY_NAME, @"TextProfile", nil];
NSCharacterSet *whitespaceAndNewlineCharacterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
keyEnumerator = [keysWeNowUseGlobally objectEnumerator];
while ((key = [keyEnumerator nextObject])) {
NSAttributedString *firstAttributedString = nil;
BOOL allOnThisKeyAreTheSame = YES;
enumerator = [[self accounts] objectEnumerator];
while ((account = [enumerator nextObject])) {
NSAttributedString *attributedString = [[account preferenceForKey:key
group:GROUP_ACCOUNT_STATUS] attributedString];
if (attributedString && ![attributedString length]) {
[account setPreference:nil
forKey:key
group:GROUP_ACCOUNT_STATUS];
attributedString = nil;
}
if (attributedString) {
if (firstAttributedString) {
/* If this string is not the same as the first one we found, all are not the same.
* Only need to check if thus far they all have been the same
*/
if (allOnThisKeyAreTheSame &&
![[[attributedString string] stringByTrimmingCharactersInSet:whitespaceAndNewlineCharacterSet] isEqualToString:
[[firstAttributedString string] stringByTrimmingCharactersInSet:whitespaceAndNewlineCharacterSet]]) {
allOnThisKeyAreTheSame = NO;
}
} else {
//Note the first one we find, which will be our reference
firstAttributedString = attributedString;
}
}
}
if (allOnThisKeyAreTheSame && firstAttributedString) {
//All strings on this key are the same. Set the preference globally...
[adium.preferenceController setPreference:[firstAttributedString dataRepresentation]
forKey:key
group:GROUP_ACCOUNT_STATUS];
//And remove it from all accounts
enumerator = [[self accounts] objectEnumerator];
while ((account = [enumerator nextObject])) {
[account setPreference:nil
forKey:key
group:GROUP_ACCOUNT_STATUS];
}
}
}
[userDefaults synchronize];
}
}
@end
|