1 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 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
|
/*
Project: FTP
Copyright (C) 2005-2008 Riccardo Mottola
Author: Riccardo Mottola
Created: 2005-03-30
Application Controller
This application 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 application 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
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#import "AppController.h"
#import "fileElement.h"
@implementation fileTransmitParms
@end
@implementation AppController
+ (void)initialize
{
NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
/*
* Register your app's defaults here by adding objects to the
* dictionary, eg
*
* [defaults setObject:anObject forKey:keyForThatObject];
*
*/
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (id)init
{
NSFont *font;
if ((self = [super init]))
{
connMode = defaultMode;
threadRunning = NO;
font = [NSFont userFixedPitchFontOfSize: 0];
textAttributes = [NSMutableDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
[textAttributes retain];
}
return self;
}
- (void)dealloc
{
[textAttributes release];
[super dealloc];
}
- (void)awakeFromNib
{
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotif
{
NSArray *dirList;
NSUserDefaults *defaults;
NSString *readValue;
NSPort *port1;
NSPort *port2;
NSArray *portArray;
NSConnection *kitConnection;
/* read the user preferences */
defaults = [NSUserDefaults standardUserDefaults];
readValue = [defaults stringForKey:connectionModeKey];
/* if no value was set for the key we set port as mode */
if ([readValue isEqualToString:@"default"])
connMode = defaultMode;
else if ([readValue isEqualToString:@"port"] || readValue == nil)
connMode = portMode;
else if ([readValue isEqualToString:@"passive"])
connMode = passiveMode;
else
NSLog(@"Unrecognized value in user preferences for %@: %@", connectionModeKey, readValue);
/* set double actions for tables */
[localView setTarget:self];
[localView setDoubleAction:@selector(listDoubleClick:)];
[remoteView setTarget:self];
[remoteView setDoubleAction:@selector(listDoubleClick:)];
/* startup code */
local = [[LocalClient alloc] init];
[local setWorkingDir:[local homeDir]];
dirList = [local dirContents];
[progBar setDoubleValue:0.0]; // reset the progress bar
/* we create a data source and set the tableviews */
localTableData = [[fileTable alloc] init];
[localTableData initData:dirList];
[localView setDataSource:localTableData];
remoteTableData = [[fileTable alloc] init];
/* we update the path menu */
[self updatePath :localPath :[local workDirSplit]];
// #### and a release of this array ?!?
// we set up distributed objects
port1 = [NSPort port];
port2 = [NSPort port];
kitConnection = [[NSConnection alloc] initWithReceivePort:port1
sendPort:port2];
[kitConnection setRootObject:self];
/* Ports switched here. */
portArray = [NSArray arrayWithObjects:port2, port1, nil];
[NSThread detachNewThreadSelector: @selector(connectWithPorts:)
toTarget: [FtpClient class]
withObject: portArray];
return;
}
- (BOOL)applicationShouldTerminate:(id)sender
{
return YES;
}
- (void)applicationWillTerminate:(NSNotification *)aNotif
{
}
- (BOOL)application:(NSApplication *)application openFile:(NSString *)fileName
{
return NO;
}
/* update the pop-up menu with a new path */
- (void)updatePath :(NSPopUpButton *)path :(NSArray *)pathArray
{
[path removeAllItems];
[path addItemsWithTitles:pathArray];
}
/* performs the action of the path pull-down menu
it navigates upwards the tree
and works for both the local and remote path */
- (IBAction)changePathFromMenu:(id)sender
{
Client *theClient;
NSTableView *theView;
fileTable *theTable;
NSString *thePath;
NSArray *items;
int selectedIndex;
int i;
NSArray *dirList;
NSLog(@"%@", [sender class]);
if (sender == localPath)
{
theClient = local;
theView = localView;
theTable = localTableData;
} else
{
theClient = ftp;
theView = remoteView;
theTable = remoteTableData;
}
thePath = [NSString string];
selectedIndex = [sender indexOfItem:[sender selectedItem]];
items = [sender itemTitles];
for (i = [items count] - 1; i >= selectedIndex; i--)
thePath = [thePath stringByAppendingPathComponent: [items objectAtIndex:i]];
NSLog(@"selected path: %@", thePath);
[theClient changeWorkingDir:thePath];
if ((dirList = [theClient dirContents]) == nil)
return;
[theTable initData:dirList];
[theView reloadData];
[self updatePath :sender :[theClient workDirSplit]];
}
/* perform the action of a double click in a table element
a directory should be opened, a file down or uploaded
The same method works for local and remote, detecting them */
- (IBAction)listDoubleClick:(id)sender
{
Client *theClient;
NSTableView *theView;
fileTable *theTable;
int elementIndex;
fileElement *fileEl;
NSString *thePath;
NSArray *dirList;
NSPopUpButton *thePathMenu;
if (threadRunning)
{
NSLog(@"thread was still running");
return;
}
theView = sender;
NSLog(@"%@", [theView class]);
if (theView == localView)
{
theClient = local;
theTable = localTableData;
thePathMenu = localPath;
} else
{
theClient = ftp;
theTable = remoteTableData;
thePathMenu = remotePath;
}
elementIndex = [sender selectedRow];
if (elementIndex < 0)
{
NSLog(@"error: double click with nothing selected");
return;
}
fileEl = [theTable elementAtIndex:elementIndex];
NSLog(@"element: %@ %d", [fileEl filename], [fileEl isDir]);
thePath = [NSString stringWithString:[theClient workingDir]];
thePath = [thePath stringByAppendingPathComponent: [fileEl filename]];
if ([fileEl isDir])
{
NSLog(@"should cd to %@", thePath);
[theClient changeWorkingDir:thePath];
if ((dirList = [theClient dirContents]) == nil)
return;
[theTable initData:dirList];
[theView reloadData];
[self updatePath :thePathMenu :[theClient workDirSplit]];
} else
{
if (theView == localView)
{
NSLog(@"should upload %@", thePath);
[self performStoreFile];
} else
{
NSLog(@"should download %@", thePath);
[self performRetrieveFile];
}
}
}
- (void)setInterfaceEnabled:(BOOL)flag
{
[localView setEnabled:flag];
[remoteView setEnabled:flag];
[localPath setEnabled:flag];
[remotePath setEnabled:flag];
[buttUpload setEnabled:flag];
[buttDownload setEnabled:flag];
}
- (void)setThreadRunningState:(BOOL)flag
{
threadRunning = flag;
[self setInterfaceEnabled:!flag];
}
- (void)performRetrieveFile
{
NSEnumerator *elemEnum;
fileElement *fileEl;
id currEl;
[self setThreadRunningState:YES];
// We should actually do a copy of the selection
elemEnum = [remoteView selectedRowEnumerator];
while ((currEl = [elemEnum nextObject]) != nil)
{
fileEl = [remoteTableData elementAtIndex:[currEl intValue]];
NSLog(@"should download: %@", [fileEl filename]);
[ftp retrieveFile:fileEl to:local beingAt:0];
}
}
- (void)performStoreFile
{
NSEnumerator *elemEnum;
fileElement *fileEl;
id currEl;
[self setThreadRunningState:YES];
// We should actually do a copy of the selection
elemEnum = [localView selectedRowEnumerator];
while ((currEl = [elemEnum nextObject]) != nil)
{
fileEl = [localTableData elementAtIndex:[currEl intValue]];
NSLog(@"should upload: %@", [fileEl filename]);
[ftp storeFile:fileEl from:local beingAt:0];
}
}
- (IBAction)downloadButton:(id)sender
{
if (threadRunning)
{
NSLog(@"thread was still running");
return;
}
[self performRetrieveFile];
}
- (IBAction)uploadButton:(id)sender
{
if (threadRunning)
{
NSLog(@"thread was still running");
return;
}
[self performStoreFile];
}
- (IBAction)localDelete:(id)sender
{
NSEnumerator *elemEnum;
fileElement *fileEl;
id currEl;
elemEnum = [localView selectedRowEnumerator];
while ((currEl = [elemEnum nextObject]) != nil)
{
fileEl = [localTableData elementAtIndex:[currEl intValue]];
[local deleteFile:fileEl beingAt:0];
}
}
- (IBAction)remoteDelete:(id)sender
{
NSEnumerator *elemEnum;
fileElement *fileEl;
id currEl;
elemEnum = [remoteView selectedRowEnumerator];
while ((currEl = [elemEnum nextObject]) != nil)
{
fileEl = [remoteTableData elementAtIndex:[currEl intValue]];
[ftp deleteFile:fileEl beingAt:0];
}
}
- (void)setTransferBegin:(NSString *)name :(unsigned long long)size
{
[infoMessage setStringValue:name];
[progBar setDoubleValue:0];
#ifdef WIN32
DWORD msecs = timeGetTime();
beginTimeVal.tv_sec=msecs/1000;
beginTimeVal.tv_usec=(msecs - beginTimeVal.tv_sec*1000) * 1000;
#else
gettimeofday(&beginTimeVal, NULL);
#endif
transferSize = size;
[mainWin displayIfNeeded];
}
- (void)setTransferProgress:(NSNumber *)bytesTransferred
{
struct timeval currTimeVal;
float speed;
NSString *speedStr;
NSString *sizeStr;
double percent;
unsigned long long bytes;
bytes = [bytesTransferred unsignedLongLongValue];
#ifdef WIN32
DWORD msecs = timeGetTime();
currTimeVal.tv_sec=msecs/1000;
currTimeVal.tv_usec=(msecs - currTimeVal.tv_sec*1000) * 1000;
#else
gettimeofday(&currTimeVal, NULL);
#endif
speed = (float)((double)bytes / (double)(currTimeVal.tv_sec - beginTimeVal.tv_sec));
percent = ((double)bytes / (double)transferSize) * 100;
[progBar setDoubleValue:percent];
speedStr = [NSString alloc];
if (speed < 1024)
speedStr = [speedStr initWithFormat:@"%3.2fB/s", speed];
else if (speed < 1024*1024)
speedStr = [speedStr initWithFormat:@"%3.2fKB/s", speed/1024];
else
speedStr = [speedStr initWithFormat:@"%3.2fMB/s", speed/(1024*1024)];
[infoSpeed setStringValue:speedStr];
[speedStr release];
sizeStr = [NSString alloc];
if (transferSize < 1024)
sizeStr = [sizeStr initWithFormat:@"%3.2f : %3.2f B", (float)bytes, (float)transferSize];
else if (transferSize < 1024*1024)
sizeStr = [sizeStr initWithFormat:@"%3.2f : %3.2f KB", (double)bytes/1024, (double)transferSize/1024];
else
sizeStr = [sizeStr initWithFormat:@"%3.2f : %3.2f MB", (double)bytes/(1024*1024), (double)transferSize/(1024*1024)];
[infoSize setStringValue:sizeStr];
[sizeStr release];
}
- (void)setTransferEnd:(NSNumber *)bytesTransferred
{
struct timeval currTimeVal;
double deltaT;
float speed;
NSString *speedStr;
NSString *sizeStr;
double percent;
unsigned long long bytes;
bytes = [bytesTransferred unsignedLongLongValue];
#ifdef WIN32
DWORD msecs = timeGetTime();
currTimeVal.tv_sec=msecs/1000;
currTimeVal.tv_usec=(msecs - currTimeVal.tv_sec*1000) * 1000;
#else
gettimeofday(&currTimeVal, NULL);
#endif
deltaT = (currTimeVal.tv_sec - beginTimeVal.tv_sec)+((double)(currTimeVal.tv_usec - beginTimeVal.tv_usec)/1000000);
speed = (float)((double)bytes / deltaT);
NSLog(@"Elapsed time: %f", (float)deltaT);
percent = ((double)bytes / (double)transferSize) * 100;
speedStr = [NSString alloc];
if (speed < 1024)
speedStr = [speedStr initWithFormat:@"%3.2fB/s", speed];
else if (speed < 1024*1024)
speedStr = [speedStr initWithFormat:@"%3.2fKB/s", speed/1024];
else
speedStr = [speedStr initWithFormat:@"%3.2fMB/s", speed/(1024*1024)];
[infoSpeed setStringValue:speedStr];
[speedStr release];
sizeStr = [NSString alloc];
if (transferSize < 1024)
sizeStr = [sizeStr initWithFormat:@"%3.2f : %3.2f B", (float)bytes, (float)transferSize];
else if (transferSize < 1024*1024)
sizeStr = [sizeStr initWithFormat:@"%3.2f : %3.2f KB", (double)bytes/1024, (double)transferSize/1024];
else
sizeStr = [sizeStr initWithFormat:@"%3.2f : %3.2f MB", (double)bytes/(1024*1024), (double)transferSize/(1024*1024)];
[infoSize setStringValue:sizeStr];
[sizeStr release];
[progBar setDoubleValue:percent];
[mainWin displayIfNeeded];
}
- (IBAction)disconnect:(id)sender
{
[ftp disconnect];
[mainWin setTitle:@"FTP"];
}
- (IBAction)showPrefPanel:(id)sender
{
[prefPanel makeKeyAndOrderFront:self];
switch (connMode)
{
case defaultMode:
[portType selectCellWithTag:0];
break;
case portMode:
[portType selectCellWithTag:1];
break;
case passiveMode:
[portType selectCellWithTag:2];
break;
default:
NSLog(@"Unexpected mode on pref pane setup.");
}
}
- (IBAction)prefSave:(id)sender
{
NSUserDefaults *defaults;
defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"tag... %d", [[portType selectedCell] tag]);
switch ([[portType selectedCell] tag])
{
case 0:
//default
NSLog(@"default");
connMode = defaultMode;
[ftp setPortDefault];
[defaults setObject:@"default" forKey:connectionModeKey];
break;
case 1:
//port
NSLog(@"port");
connMode = portMode;
[ftp setPortPort];
[defaults setObject:@"port" forKey:connectionModeKey];
break;
case 2:
// passive
NSLog(@"passive");
connMode = passiveMode;
[ftp setPortPassive];
[defaults setObject:@"passive" forKey:connectionModeKey];
break;
default:
NSLog(@"unexpected selection");
}
[prefPanel performClose:nil];
}
- (IBAction)prefCancel:(id)sender
{
[prefPanel performClose:nil];
}
- (IBAction)showFtpLog:(id)sender
{
[logWin makeKeyAndOrderFront:self];
}
/**
Called by the server object to register itself.
*/
- (void)setServer:(id)anObject
{
ftp = (FtpClient*)[anObject retain];
NSLog(@"FTP server object set");
return;
}
/*
This routine is called after adding new results to the text view's backing store.
We now need to scroll the NSScrollView in which the NSTextView sits to the part
that we just added at the end
*/
- (void)scrollToVisible:(id)ignore
{
[logTextField scrollRangeToVisible:NSMakeRange([[logTextField string] length], 0)];
}
- (IBAction)appendTextToLog:(NSString *)textChunk
{
NSAttributedString *attrStr;
attrStr = [[NSAttributedString alloc] initWithString: textChunk
attributes: textAttributes];
/* add the textChunk to the NSTextView's backing store as an attributed string */
[[logTextField textStorage] appendAttributedString: attrStr];
/* setup a selector to be called the next time through the event loop to scroll
the view to the just pasted text. We don't want to scroll right now,
because of a bug in Mac OS X version 10.1 that causes scrolling in the context
of a text storage update to starve the app of events */
[self performSelector:@selector(scrollToVisible:) withObject:nil afterDelay:0.0];
[attrStr autorelease];
}
/* --- connection panel methods --- */
- (IBAction)showConnPanel:(id)sender
{
[connectPanel makeKeyAndOrderFront:self];
}
- (IBAction)connectConn:(id)sender
{
NSArray *dirList;
char tempStr[1024];
char tempStr2[1024];
[connectPanel performClose:nil];
[mainWin makeKeyAndOrderFront:self];
ftp = [ftp initWithController:self :connMode];
[[connAddress stringValue] getCString:tempStr];
if ([ftp connect:[connPort intValue] :tempStr] < 0)
{
NSRunAlertPanel(@"Error", @"Connection failed.\nCheck that you typed the host name correctly.", @"Ok", nil, nil);
NSLog(@"connection failed in connectConn");
return;
}
if ([connAnon state] == NSOnState)
{
strcpy(tempStr, "anonymous");
strcpy(tempStr2, "user@myhost.com");
} else
{
[[connUser stringValue] getCString:tempStr];
[[connPass stringValue] getCString:tempStr2];
}
if ([ftp authenticate:tempStr :tempStr2] < 0)
{
NSRunAlertPanel(@"Error", @"Authentication failed.\nCheck that your username and password are correct.", @"Ok", nil, nil);
NSLog(@"authentication failed.");
return;
} else
{
[ftp setWorkingDir:[ftp homeDir]];
if ((dirList = [ftp dirContents]) == nil)
return;
[remoteTableData initData:dirList];
[remoteView setDataSource:remoteTableData];
/* update the path menu */
[self updatePath :remotePath :[ftp workDirSplit]];
/* set the window title */
[mainWin setTitle:[connAddress stringValue]];
}
}
- (IBAction)cancelConn:(id)sender
{
[connectPanel performClose:nil];
}
- (IBAction)anonymousConn:(id)sender
{
if ([connAnon state] == NSOnState)
{
[connUser setEnabled:NO];
[connPass setEnabled:NO];
} else
{
[connUser setEnabled:YES];
[connPass setEnabled:YES];
}
}
- (void)showAlertDialog:(NSString *)message
{
[message retain];
NSRunAlertPanel(@"Attention", message, @"Ok", nil, nil);
[message release];
}
- (connectionModes)connectionMode
{
return connMode;
}
@end
|