| 1 |
/* $Id$ */ |
|---|
| 2 |
|
|---|
| 3 |
/* |
|---|
| 4 |
* Copyright (c) 2003-2006 Axel Andersson |
|---|
| 5 |
* All rights reserved. |
|---|
| 6 |
* |
|---|
| 7 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 8 |
* modification, are permitted provided that the following conditions |
|---|
| 9 |
* are met: |
|---|
| 10 |
* 1. Redistributions of source code must retain the above copyright |
|---|
| 11 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 13 |
* notice, this list of conditions and the following disclaimer in the |
|---|
| 14 |
* documentation and/or other materials provided with the distribution. |
|---|
| 15 |
* |
|---|
| 16 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|---|
| 17 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 18 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|---|
| 19 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
|---|
| 20 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|---|
| 21 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|---|
| 22 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 23 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|---|
| 24 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|---|
| 25 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|---|
| 26 |
* POSSIBILITY OF SUCH DAMAGE. |
|---|
| 27 |
*/ |
|---|
| 28 |
|
|---|
| 29 |
#import "WCAccounts.h" |
|---|
| 30 |
#import "WCAccountsController.h" |
|---|
| 31 |
#import "WCConfig.h" |
|---|
| 32 |
#import "WCConfigController.h" |
|---|
| 33 |
#import "WCDashboardController.h" |
|---|
| 34 |
#import "WCSettings.h" |
|---|
| 35 |
|
|---|
| 36 |
static WCAccountsController *sharedAccountsController; |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
@interface WCAccountsController(Private) |
|---|
| 40 |
|
|---|
| 41 |
- (WCAccount *)_selectedAccount; |
|---|
| 42 |
|
|---|
| 43 |
- (void)_selectAccount:(WCAccount *)account; |
|---|
| 44 |
- (void)_unselectAccount:(WCAccount *)account; |
|---|
| 45 |
- (void)_validateAccount:(WCAccount *)account; |
|---|
| 46 |
|
|---|
| 47 |
- (void)_readUsersFromFile:(NSString *)path; |
|---|
| 48 |
- (void)_readGroupsFromFile:(NSString *)path; |
|---|
| 49 |
- (BOOL)_writeUsersToFile:(NSString *)path; |
|---|
| 50 |
- (BOOL)_writeGroupsToFile:(NSString *)path; |
|---|
| 51 |
|
|---|
| 52 |
@end |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
@implementation WCAccountsController(Private) |
|---|
| 56 |
|
|---|
| 57 |
- (WCAccount *)_selectedAccount { |
|---|
| 58 |
NSInteger row; |
|---|
| 59 |
|
|---|
| 60 |
row = [_tableView selectedRow]; |
|---|
| 61 |
|
|---|
| 62 |
if(row < 0) |
|---|
| 63 |
return NULL; |
|---|
| 64 |
|
|---|
| 65 |
return [_accounts objectAtIndex:row]; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
#pragma mark - |
|---|
| 71 |
|
|---|
| 72 |
- (void)_selectAccount:(WCAccount *)account { |
|---|
| 73 |
NSEnumerator *enumerator; |
|---|
| 74 |
WCAccount *group; |
|---|
| 75 |
|
|---|
| 76 |
[_groupPopUpButton removeAllItems]; |
|---|
| 77 |
[_groupPopUpButton addItemWithTitle:WCLS(@"none", "Group menu item")]; |
|---|
| 78 |
enumerator = [[_groups accounts] objectEnumerator]; |
|---|
| 79 |
|
|---|
| 80 |
while((group = [enumerator nextObject])) |
|---|
| 81 |
[_groupPopUpButton addItemWithTitle:[group name]]; |
|---|
| 82 |
|
|---|
| 83 |
[self _validateAccount:account]; |
|---|
| 84 |
|
|---|
| 85 |
if(account) { |
|---|
| 86 |
[_typePopUpButton selectItemAtIndex: |
|---|
| 87 |
[_typePopUpButton indexOfItemWithTag:[account type]]]; |
|---|
| 88 |
[_nameTextField setStringValue:[account name]]; |
|---|
| 89 |
[_passwordTextField setStringValue:[account password]]; |
|---|
| 90 |
|
|---|
| 91 |
if([[account group] length] == 0) { |
|---|
| 92 |
[_groupPopUpButton selectItemAtIndex:0]; |
|---|
| 93 |
} else { |
|---|
| 94 |
[_groupPopUpButton selectItemWithTitle:[account group]]; |
|---|
| 95 |
account = [_groups accountWithName:[account group]]; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
[_getUserInfoButton setState:[account getUserInfo]]; |
|---|
| 99 |
[_broadcastButton setState:[account broadcast]]; |
|---|
| 100 |
[_postNewsButton setState:[account postNews]]; |
|---|
| 101 |
[_clearNewsButton setState:[account clearNews]]; |
|---|
| 102 |
[_downloadButton setState:[account download]]; |
|---|
| 103 |
[_uploadButton setState:[account upload]]; |
|---|
| 104 |
[_uploadAnywhereButton setState:[account uploadAnywhere]]; |
|---|
| 105 |
[_createFoldersButton setState:[account createFolders]]; |
|---|
| 106 |
[_moveFilesButton setState:[account moveFiles]]; |
|---|
| 107 |
[_deleteFilesButton setState:[account deleteFiles]]; |
|---|
| 108 |
[_viewDropBoxesButton setState:[account viewDropBoxes]]; |
|---|
| 109 |
[_createAccountsButton setState:[account createAccounts]]; |
|---|
| 110 |
[_editAccountsButton setState:[account editAccounts]]; |
|---|
| 111 |
[_deleteAccountsButton setState:[account deleteAccounts]]; |
|---|
| 112 |
[_elevatePrivilegesButton setState:[account elevatePrivileges]]; |
|---|
| 113 |
[_kickUsersButton setState:[account kickUsers]]; |
|---|
| 114 |
[_banUsersButton setState:[account banUsers]]; |
|---|
| 115 |
[_cannotBeKickedButton setState:[account cannotBeKicked]]; |
|---|
| 116 |
[_setTopicButton setState:[account setTopic]]; |
|---|
| 117 |
|
|---|
| 118 |
/* if([account downloads] > 0) |
|---|
| 119 |
[_downloadsTextField setIntValue:[account downloads]]; |
|---|
| 120 |
else |
|---|
| 121 |
[_downloadsTextField setStringValue:@""];*/ |
|---|
| 122 |
|
|---|
| 123 |
if([account downloadSpeed] > 0) |
|---|
| 124 |
[_downloadSpeedTextField setIntValue:[account downloadSpeed] / (float) 1024.0f]; |
|---|
| 125 |
else |
|---|
| 126 |
[_downloadSpeedTextField setStringValue:@""]; |
|---|
| 127 |
|
|---|
| 128 |
/* if([account uploads] > 0) |
|---|
| 129 |
[_uploadsTextField setIntValue:[account uploads]]; |
|---|
| 130 |
else |
|---|
| 131 |
[_uploadsTextField setStringValue:@""];*/ |
|---|
| 132 |
|
|---|
| 133 |
if([account uploadSpeed] > 0) |
|---|
| 134 |
[_uploadSpeedTextField setIntValue:[account uploadSpeed] / (float) 1024.0f]; |
|---|
| 135 |
else |
|---|
| 136 |
[_uploadSpeedTextField setStringValue:@""]; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
_selected = YES; |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
- (void)_unselectAccount:(WCAccount *)account { |
|---|
| 145 |
if(!_selected) |
|---|
| 146 |
return; |
|---|
| 147 |
|
|---|
| 148 |
[account setName:[_nameTextField stringValue]]; |
|---|
| 149 |
[account setPassword:[_passwordTextField stringValue]]; |
|---|
| 150 |
|
|---|
| 151 |
if([_groupPopUpButton indexOfSelectedItem] == 0) |
|---|
| 152 |
[account setGroup:@""]; |
|---|
| 153 |
else |
|---|
| 154 |
[account setGroup:[_groupPopUpButton titleOfSelectedItem]]; |
|---|
| 155 |
|
|---|
| 156 |
[account setGetUserInfo:[_getUserInfoButton state]]; |
|---|
| 157 |
[account setBroadcast:[_broadcastButton state]]; |
|---|
| 158 |
[account setPostNews:[_postNewsButton state]]; |
|---|
| 159 |
[account setClearNews:[_clearNewsButton state]]; |
|---|
| 160 |
[account setDownload:[_downloadButton state]]; |
|---|
| 161 |
[account setUpload:[_uploadButton state]]; |
|---|
| 162 |
[account setUploadAnywhere:[_uploadAnywhereButton state]]; |
|---|
| 163 |
[account setCreateFolders:[_createFoldersButton state]]; |
|---|
| 164 |
[account setMoveFiles:[_moveFilesButton state]]; |
|---|
| 165 |
[account setDeleteFiles:[_deleteFilesButton state]]; |
|---|
| 166 |
[account setViewDropBoxes:[_viewDropBoxesButton state]]; |
|---|
| 167 |
[account setCreateAccounts:[_createAccountsButton state]]; |
|---|
| 168 |
[account setEditAccounts:[_editAccountsButton state]]; |
|---|
| 169 |
[account setDeleteAccounts:[_deleteAccountsButton state]]; |
|---|
| 170 |
[account setElevatePrivileges:[_elevatePrivilegesButton state]]; |
|---|
| 171 |
[account setKickUsers:[_kickUsersButton state]]; |
|---|
| 172 |
[account setBanUsers:[_banUsersButton state]]; |
|---|
| 173 |
[account setCannotBeKicked:[_cannotBeKickedButton state]]; |
|---|
| 174 |
[account setSetTopic:[_setTopicButton state]]; |
|---|
| 175 |
|
|---|
| 176 |
// [account setDownloads:[_downloadsTextField intValue]]; |
|---|
| 177 |
[account setDownloadSpeed:[_downloadSpeedTextField intValue] * 1024]; |
|---|
| 178 |
// [account setUploads:[_uploadsTextField intValue]]; |
|---|
| 179 |
[account setUploadSpeed:[_uploadSpeedTextField intValue] * 1024]; |
|---|
| 180 |
|
|---|
| 181 |
_selected = NO; |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
- (void)_validateAccount:(WCAccount *)account { |
|---|
| 187 |
BOOL enabled; |
|---|
| 188 |
|
|---|
| 189 |
enabled = [[WCDashboardController dashboardController] isAuthorized] && (account != NULL); |
|---|
| 190 |
[_addButton setEnabled:enabled]; |
|---|
| 191 |
[_deleteButton setEnabled:enabled]; |
|---|
| 192 |
[_typePopUpButton setEnabled:enabled]; |
|---|
| 193 |
[_nameTextField setEnabled:enabled]; |
|---|
| 194 |
|
|---|
| 195 |
enabled = ([[WCDashboardController dashboardController] isAuthorized] && [account type] == WCAccountUser); |
|---|
| 196 |
[_passwordTextField setEnabled:enabled]; |
|---|
| 197 |
[_groupPopUpButton setEnabled:enabled]; |
|---|
| 198 |
|
|---|
| 199 |
enabled = ([[WCDashboardController dashboardController] isAuthorized] && [[account group] length] == 0); |
|---|
| 200 |
[_getUserInfoButton setEnabled:enabled]; |
|---|
| 201 |
[_broadcastButton setEnabled:enabled]; |
|---|
| 202 |
[_postNewsButton setEnabled:enabled]; |
|---|
| 203 |
[_clearNewsButton setEnabled:enabled]; |
|---|
| 204 |
[_downloadButton setEnabled:enabled]; |
|---|
| 205 |
[_uploadButton setEnabled:enabled]; |
|---|
| 206 |
[_uploadAnywhereButton setEnabled:enabled]; |
|---|
| 207 |
[_createFoldersButton setEnabled:enabled]; |
|---|
| 208 |
[_moveFilesButton setEnabled:enabled]; |
|---|
| 209 |
[_deleteFilesButton setEnabled:enabled]; |
|---|
| 210 |
[_viewDropBoxesButton setEnabled:enabled]; |
|---|
| 211 |
[_createAccountsButton setEnabled:enabled]; |
|---|
| 212 |
[_editAccountsButton setEnabled:enabled]; |
|---|
| 213 |
[_deleteAccountsButton setEnabled:enabled]; |
|---|
| 214 |
[_elevatePrivilegesButton setEnabled:enabled]; |
|---|
| 215 |
[_kickUsersButton setEnabled:enabled]; |
|---|
| 216 |
[_banUsersButton setEnabled:enabled]; |
|---|
| 217 |
[_cannotBeKickedButton setEnabled:enabled]; |
|---|
| 218 |
[_setTopicButton setEnabled:enabled]; |
|---|
| 219 |
// [_downloadsTextField setEnabled:enabled]; |
|---|
| 220 |
[_downloadSpeedTextField setEnabled:enabled]; |
|---|
| 221 |
// [_uploadsTextField setEnabled:enabled]; |
|---|
| 222 |
[_uploadSpeedTextField setEnabled:enabled]; |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
#pragma mark - |
|---|
| 228 |
|
|---|
| 229 |
- (void)_readUsersFromFile:(NSString *)path { |
|---|
| 230 |
[_accounts removeObjectsInArray:[_users accounts]]; |
|---|
| 231 |
[_users release]; |
|---|
| 232 |
_users = [[WCAccounts alloc] initWithContentsOfFile:path type:WCAccountUser]; |
|---|
| 233 |
|
|---|
| 234 |
[_accounts addObjectsFromArray:[_users accounts]]; |
|---|
| 235 |
[_accounts sortUsingSelector:@selector(compareName:)]; |
|---|
| 236 |
|
|---|
| 237 |
[_tableView reloadData]; |
|---|
| 238 |
|
|---|
| 239 |
[self _selectAccount:[self _selectedAccount]]; |
|---|
| 240 |
} |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
- (void)_readGroupsFromFile:(NSString *)path { |
|---|
| 245 |
[_accounts removeObjectsInArray:[_groups accounts]]; |
|---|
| 246 |
[_groups release]; |
|---|
| 247 |
_groups = [[WCAccounts alloc] initWithContentsOfFile:path type:WCAccountGroup]; |
|---|
| 248 |
|
|---|
| 249 |
[_accounts addObjectsFromArray:[_groups accounts]]; |
|---|
| 250 |
[_accounts sortUsingSelector:@selector(compareName:)]; |
|---|
| 251 |
|
|---|
| 252 |
[_tableView reloadData]; |
|---|
| 253 |
|
|---|
| 254 |
[self _selectAccount:[self _selectedAccount]]; |
|---|
| 255 |
} |
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
- (BOOL)_writeUsersToFile:(NSString *)path { |
|---|
| 260 |
NSString *temp; |
|---|
| 261 |
WCDashboardController *controller; |
|---|
| 262 |
WCConfig *config; |
|---|
| 263 |
|
|---|
| 264 |
[self _unselectAccount:[self _selectedAccount]]; |
|---|
| 265 |
|
|---|
| 266 |
controller = [WCDashboardController dashboardController]; |
|---|
| 267 |
config = [[WCConfigController configController] config]; |
|---|
| 268 |
temp = [NSFileManager temporaryPathWithPrefix:@"users" suffix:@"conf"]; |
|---|
| 269 |
|
|---|
| 270 |
if([_users writeToFile:temp]) { |
|---|
| 271 |
if([controller movePath:temp toPath:path]) { |
|---|
| 272 |
if([controller changeOwnerOfPath:path |
|---|
| 273 |
toUser:[config stringForKey:@"user"] |
|---|
| 274 |
group:[config stringForKey:@"group"]]) { |
|---|
| 275 |
_touched = NO; |
|---|
| 276 |
|
|---|
| 277 |
return YES; |
|---|
| 278 |
} |
|---|
| 279 |
} |
|---|
| 280 |
|
|---|
| 281 |
} |
|---|
| 282 |
|
|---|
| 283 |
return NO; |
|---|
| 284 |
} |
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
- (BOOL)_writeGroupsToFile:(NSString *)path { |
|---|
| 289 |
NSString *temp; |
|---|
| 290 |
WCDashboardController *controller; |
|---|
| 291 |
WCConfig *config; |
|---|
| 292 |
|
|---|
| 293 |
[self _unselectAccount:[self _selectedAccount]]; |
|---|
| 294 |
|
|---|
| 295 |
controller = [WCDashboardController dashboardController]; |
|---|
| 296 |
config = [[WCConfigController configController] config]; |
|---|
| 297 |
temp = [NSFileManager temporaryPathWithPrefix:@"groups" suffix:@"conf"]; |
|---|
| 298 |
|
|---|
| 299 |
if([_groups writeToFile:temp]) { |
|---|
| 300 |
if([controller movePath:temp toPath:path]) { |
|---|
| 301 |
if([controller changeOwnerOfPath:path |
|---|
| 302 |
toUser:[config stringForKey:@"user"] |
|---|
| 303 |
group:[config stringForKey:@"group"]]) { |
|---|
| 304 |
_touched = NO; |
|---|
| 305 |
|
|---|
| 306 |
return YES; |
|---|
| 307 |
} |
|---|
| 308 |
} |
|---|
| 309 |
|
|---|
| 310 |
} |
|---|
| 311 |
|
|---|
| 312 |
return NO; |
|---|
| 313 |
} |
|---|
| 314 |
|
|---|
| 315 |
@end |
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
@implementation WCAccountsController |
|---|
| 319 |
|
|---|
| 320 |
+ (WCAccountsController *)accountsController { |
|---|
| 321 |
return sharedAccountsController; |
|---|
| 322 |
} |
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
- (id)init { |
|---|
| 327 |
self = [super init]; |
|---|
| 328 |
|
|---|
| 329 |
sharedAccountsController = self; |
|---|
| 330 |
|
|---|
| 331 |
return self; |
|---|
| 332 |
} |
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
- (void)awakeFromNib { |
|---|
| 337 |
WIIconCell *cell; |
|---|
| 338 |
|
|---|
| 339 |
_accounts = [[NSMutableArray alloc] init]; |
|---|
| 340 |
|
|---|
| 341 |
cell = [[WIIconCell alloc] init]; |
|---|
| 342 |
[[_tableView tableColumnWithIdentifier:@"Name"] setDataCell:cell]; |
|---|
| 343 |
[cell release]; |
|---|
| 344 |
|
|---|
| 345 |
_userImage = [[NSImage alloc] initWithContentsOfFile: |
|---|
| 346 |
[[self bundle] pathForResource:@"User" ofType:@"tiff"]]; |
|---|
| 347 |
_groupImage = [[NSImage alloc] initWithContentsOfFile: |
|---|
| 348 |
[[self bundle] pathForResource:@"Group" ofType:@"tiff"]]; |
|---|
| 349 |
|
|---|
| 350 |
[[NSNotificationCenter defaultCenter] |
|---|
| 351 |
addObserver:self |
|---|
| 352 |
selector:@selector(authorizationStatusDidChange:) |
|---|
| 353 |
name:WCAuthorizationStatusDidChange |
|---|
| 354 |
object:NULL]; |
|---|
| 355 |
} |
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
- (void)awakeFromController { |
|---|
| 360 |
[self _readUsersFromFile:WCExpandWiredPath(@"users")]; |
|---|
| 361 |
[self _readGroupsFromFile:WCExpandWiredPath(@"groups")]; |
|---|
| 362 |
|
|---|
| 363 |
[self _selectAccount:[self _selectedAccount]]; |
|---|
| 364 |
} |
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
- (BOOL)saveFromController { |
|---|
| 369 |
if(_touched) { |
|---|
| 370 |
if([self _writeUsersToFile:WCExpandWiredPath(@"users")]) { |
|---|
| 371 |
if([self _writeGroupsToFile:WCExpandWiredPath(@"groups")]) { |
|---|
| 372 |
[[NSNotificationCenter defaultCenter] postNotificationName:WCAccountsDidChange]; |
|---|
| 373 |
|
|---|
| 374 |
return YES; |
|---|
| 375 |
} |
|---|
| 376 |
} |
|---|
| 377 |
|
|---|
| 378 |
return NO; |
|---|
| 379 |
} |
|---|
| 380 |
|
|---|
| 381 |
return YES; |
|---|
| 382 |
} |
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
- (void)dealloc { |
|---|
| 387 |
[[NSNotificationCenter defaultCenter] removeObserver:self]; |
|---|
| 388 |
|
|---|
| 389 |
[_accounts release]; |
|---|
| 390 |
[_users release]; |
|---|
| 391 |
[_groups release]; |
|---|
| 392 |
|
|---|
| 393 |
[_userImage release]; |
|---|
| 394 |
[_groupImage release]; |
|---|
| 395 |
|
|---|
| 396 |
[super dealloc]; |
|---|
| 397 |
} |
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
#pragma mark - |
|---|
| 402 |
|
|---|
| 403 |
- (void)authorizationStatusDidChange:(NSNotification *)notification { |
|---|
| 404 |
[self _validateAccount:[self _selectedAccount]]; |
|---|
| 405 |
} |
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 |
- (void)controlTextDidChange:(NSNotification *)notification { |
|---|
| 410 |
WCAccount *account; |
|---|
| 411 |
|
|---|
| 412 |
_touched = YES; |
|---|
| 413 |
|
|---|
| 414 |
if([notification object] == _nameTextField) { |
|---|
| 415 |
account = [self _selectedAccount]; |
|---|
| 416 |
[account setName:[_nameTextField stringValue]]; |
|---|
| 417 |
[_tableView reloadData]; |
|---|
| 418 |
} |
|---|
| 419 |
} |
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
#pragma mark - |
|---|
| 424 |
|
|---|
| 425 |
- (IBAction)touch:(id)sender { |
|---|
| 426 |
_touched = YES; |
|---|
| 427 |
} |
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
- (IBAction)add:(id)sender { |
|---|
| 432 |
WCAccount *account; |
|---|
| 433 |
NSString *untitled, *name; |
|---|
| 434 |
int i = 2; |
|---|
| 435 |
|
|---|
| 436 |
untitled = WCLS(@"Untitled", @"New user name"); |
|---|
| 437 |
name = untitled; |
|---|
| 438 |
|
|---|
| 439 |
while(([_users accountWithName:name])) |
|---|
| 440 |
name = [NSSWF:@"%@ %u", untitled, i++]; |
|---|
| 441 |
|
|---|
| 442 |
account = [[WCAccount alloc] initWithName:name type:WCAccountUser]; |
|---|
| 443 |
[_accounts addObject:account]; |
|---|
| 444 |
[_users addAccount:account]; |
|---|
| 445 |
[account release]; |
|---|
| 446 |
|
|---|
| 447 |
_touched = YES; |
|---|
| 448 |
|
|---|
| 449 |
[_tableView reloadData]; |
|---|
| 450 |
[_tableView selectRow:[_accounts count] - 1 byExtendingSelection:NO]; |
|---|
| 451 |
} |
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
- (IBAction)delete:(id)sender { |
|---|
| 456 |
NSString *name; |
|---|
| 457 |
|
|---|
| 458 |
name = [[self _selectedAccount] name]; |
|---|
| 459 |
|
|---|
| 460 |
NSBeginAlertSheet([NSSWF:WCLS(@"Are you sure you want to delete \"%@\"?", @"Delete account dialog title (account)"), name], |
|---|
| 461 |
WCLS(@"Delete", @"Delete account dialog button title"), |
|---|
| 462 |
WCLS(@"Cancel", @"Delete account dialog button title"), |
|---|
| 463 |
NULL, |
|---|
| 464 |
[_deleteButton window], |
|---|
| 465 |
self, |
|---|
| 466 |
@selector(deleteSheetDidEnd:returnCode:contextInfo:), |
|---|
| 467 |
NULL, |
|---|
| 468 |
NULL, |
|---|
| 469 |
WCLS(@"This cannot be undone.", @"Delete account dialog description")); |
|---|
| 470 |
} |
|---|
| 471 |
|
|---|
| 472 |
|
|---|
| 473 |
|
|---|
| 474 |
- (void)deleteSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo { |
|---|
| 475 |
NSEnumerator *enumerator; |
|---|
| 476 |
WCAccount *account, *user; |
|---|
| 477 |
NSInteger row; |
|---|
| 478 |
|
|---|
| 479 |
if(returnCode == NSAlertDefaultReturn) { |
|---|
| 480 |
account = [self _selectedAccount]; |
|---|
| 481 |
|
|---|
| 482 |
if([account type] == WCAccountGroup) { |
|---|
| 483 |
enumerator = [[_users accounts] objectEnumerator]; |
|---|
| 484 |
|
|---|
| 485 |
while((user = [enumerator nextObject])) { |
|---|
| 486 |
if([[user group] isEqualToString:[account name]]) |
|---|
| 487 |
[user setGroup:@""]; |
|---|
| 488 |
} |
|---|
| 489 |
} |
|---|
| 490 |
|
|---|
| 491 |
[account retain]; |
|---|
| 492 |
[_accounts removeObject:account]; |
|---|
| 493 |
[_users deleteAccount:account]; |
|---|
| 494 |
[_groups deleteAccount:account]; |
|---|
| 495 |
[account release]; |
|---|
| 496 |
|
|---|
| 497 |
_touched = YES; |
|---|
| 498 |
|
|---|
| 499 |
[_tableView reloadData]; |
|---|
| 500 |
|
|---|
| 501 |
row = [_tableView selectedRow]; |
|---|
| 502 |
row = row == 0 ? 0 : row - 1; |
|---|
| 503 |
|
|---|
| 504 |
if(row != [_tableView selectedRow]) |
|---|
| 505 |
[_tableView selectRow:row byExtendingSelection:NO]; |
|---|
| 506 |
else |
|---|
| 507 |
[self _selectAccount:[self _selectedAccount]]; |
|---|
| 508 |
} |
|---|
| 509 |
} |
|---|
| 510 |
|
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| 513 |
- (IBAction)selectType:(id)sender { |
|---|
| 514 |
WCAccount *account; |
|---|
| 515 |
|
|---|
| 516 |
account = [self _selectedAccount]; |
|---|
| 517 |
[account setType:[[_typePopUpButton selectedItem] tag]]; |
|---|
| 518 |
|
|---|
| 519 |
switch([account type]) { |
|---|
| 520 |
case WCAccountUser: |
|---|
| 521 |
[account retain]; |
|---|
| 522 |
[_groups deleteAccount:account]; |
|---|
| 523 |
[_users addAccount:account]; |
|---|
| 524 |
[account release]; |
|---|
| 525 |
break; |
|---|
| 526 |
|
|---|
| 527 |
case WCAccountGroup: |
|---|
| 528 |
[account retain]; |
|---|
| 529 |
[_users deleteAccount:account]; |
|---|
| 530 |
[_groups addAccount:account]; |
|---|
| 531 |
[account release]; |
|---|
| 532 |
break; |
|---|
| 533 |
} |
|---|
| 534 |
|
|---|
| 535 |
_touched = YES; |
|---|
| 536 |
|
|---|
| 537 |
[_tableView reloadData]; |
|---|
| 538 |
|
|---|
| 539 |
[self _selectAccount:account]; |
|---|
| 540 |
} |
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
- (IBAction)selectGroup:(id)sender { |
|---|
| 545 |
WCAccount *account; |
|---|
| 546 |
|
|---|
| 547 |
account = [self _selectedAccount]; |
|---|
| 548 |
|
|---|
| 549 |
if([_groupPopUpButton indexOfSelectedItem] == 0) |
|---|
| 550 |
[account setGroup:@""]; |
|---|
| 551 |
else |
|---|
| 552 |
[account setGroup:[_groupPopUpButton titleOfSelectedItem]]; |
|---|
| 553 |
|
|---|
| 554 |
_touched = YES; |
|---|
| 555 |
|
|---|
| 556 |
[self _selectAccount:account]; |
|---|
| 557 |
} |
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 |
|
|---|
| 561 |
#pragma mark - |
|---|
| 562 |
|
|---|
| 563 |
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { |
|---|
| 564 |
return [_accounts count]; |
|---|
| 565 |
} |
|---|
| 566 |
|
|---|
| 567 |
|
|---|
| 568 |
|
|---|
| 569 |
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { |
|---|
| 570 |
return [[_accounts objectAtIndex:row] name]; |
|---|
| 571 |
} |
|---|
| 572 |
|
|---|
| 573 |
|
|---|
| 574 |
|
|---|
| 575 |
- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { |
|---|
| 576 |
[cell setImage:([(WCAccount *) [_accounts objectAtIndex:row] type] == WCAccountUser) ? _userImage : _groupImage]; |
|---|
| 577 |
} |
|---|
| 578 |
|
|---|
| 579 |
|
|---|
| 580 |
- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row { |
|---|
| 581 |
[self _unselectAccount:[self _selectedAccount]]; |
|---|
| 582 |
|
|---|
| 583 |
return YES; |
|---|
| 584 |
} |
|---|
| 585 |
|
|---|
| 586 |
|
|---|
| 587 |
|
|---|
| 588 |
- (void)tableViewSelectionDidChange:(NSNotification *)notification { |
|---|
| 589 |
[self _selectAccount:[self _selectedAccount]]; |
|---|
| 590 |
} |
|---|
| 591 |
|
|---|
| 592 |
@end |
|---|