@implementation AppDelegate
#pragma mark -
#pragma mark NSApplicationDelegate
-(void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// …
[self.pTextView setEditable:NO];
// …
[self addLogText:@"Zum Konfigurieren gewünschte Datei auf das Dock Icon ziehen."];
// …
}
- (BOOL)application:(NSApplication *)sender
openFile:(NSString *)filename
{
// …
if([filename length] < 1)
{
return NO;
}
// …
NSURL *URL = [NSURL fileURLWithPath:filename];
if(URL == nil)
{
return NO;
}
// …
self.pItemURL = URL;
// …
[self addLogText:[NSString stringWithFormat:@"Basisdatei gesetzt\n%@", filename]];
// …
[self startWorkspaceNotificationObserver:self];
// …
return YES;
// …
}
#pragma mark -
#pragma mark Notifications
-(BOOL)stopWorkspaceNotificationObserver:(id)aSender
{
// …
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
if(workspace == nil)
{
return NO;
}
// …
NSNotificationCenter *notificationCenter = [workspace notificationCenter];
if(notificationCenter == nil)
{
return NO;
}
// …
[notificationCenter removeObserver:self
name:NSWorkspaceDidMountNotification
object:nil];
// …
return NO;
// …
}
-(BOOL)startWorkspaceNotificationObserver:(id)aSender
{
// …
[self stopWorkspaceNotificationObserver:self];
// …
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
if(workspace == nil)
{
return NO;
}
// …
NSNotificationCenter *notificationCenter = [workspace notificationCenter];
if(notificationCenter == nil)
{
return NO;
}
// …
[notificationCenter addObserver:self
selector:@selector(receivedWorkspaceDidMountNotification:)
name:NSWorkspaceDidMountNotification
object:nil];
// …
return YES;
// …
}
-(BOOL)receivedWorkspaceDidMountNotification:(NSNotification*)aNotification
{
// …
if(aNotification == nil)
{
return NO;
}
// …
if([[aNotification name] isEqualToString:NSWorkspaceDidMountNotification] == NO)
{
return NO;
}
// …
NSDictionary *dictionary = [aNotification userInfo];
if([dictionary count] < 1)
{
return NO;
}
// …
NSURL *URL = [dictionary objectForKey:NSWorkspaceVolumeURLKey];
if(URL == nil)
{
return NO;
}
// …
[self addLogText:[NSString stringWithFormat:@"Laufwerk eingehängt\n%@", [URL path]]];
// …
if([self runActionForDeviceWithURL:URL] == NO)
{
[self addLogText:@"F E H L E R"];
}
// …
return YES;
// …
}
#pragma mark -
#pragma mark Action
-(NSURL*)itemURL
{
// …
NSURL *URL = self.pItemURL;
if(URL == nil)
{
return nil;
}
// …
return URL;
// …
}
-(NSString*)itemName
{
// …
NSURL *URL = [self itemURL];
if(URL == nil)
{
return nil;
}
// …
NSString *string = [URL lastPathComponent];
if([string length] < 1)
{
return nil;
}
// …
return string;
// …
}
-(BOOL)runActionForDeviceWithURL:(NSURL*)aDeviceURL
{
// …
if(aDeviceURL == nil)
{
return NO;
}
// …
// NSLog(@"%@", aDeviceURL);
// …
NSString *string = [self itemName];
if([string length] < 1)
{
return NO;
}
// …
NSURL *URL = [aDeviceURL URLByAppendingPathComponent:string];
if(URL == nil)
{
return NO;
}
// …
[self addLogText:[NSString stringWithFormat:@"Entferne Datei\n%@", [URL path]]];
// …
NSFileManager *fileManager = [NSFileManager defaultManager];
if(fileManager == nil)
{
return NO;
}
// …
NSError *error = nil;
// …
[fileManager removeItemAtURL:URL
error:&error];
if(error != nil)
{
return NO;
}
// …
NSURL *itemURL = [self itemURL];
if(itemURL == nil)
{
return NO;
}
// …
[self addLogText:[NSString stringWithFormat:@"Kopiere Datei\n%@", [itemURL path]]];
// …
[fileManager copyItemAtURL:itemURL
toURL:URL
error:&error];
if(error != nil)
{
return NO;
}
// …
[self addLogText:[NSString stringWithFormat:@"Laufwerk auswerfen\n%@", [aDeviceURL path]]];
// …
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
if(workspace == nil)
{
return NO;
}
// …
[workspace unmountAndEjectDeviceAtURL:aDeviceURL
error:&error];
if(error != nil)
{
return NO;
}
// …
[self addLogText:@"OK"];
// …
return YES;
// …
}
#pragma mark -
#pragma mark Text View
-(BOOL)addLogText:(NSString*)aLogText
{
// …
if([aLogText length] < 1)
{
return NO;
}
// …
NSTextView *textView = self.pTextView;
if(textView == nil)
{
return NO;
}
// …
NSString *string = [textView string];
if(string == nil)
{
string = [NSString string];
}
// …
NSMutableString *mutableString = [NSMutableString stringWithString:string];
if(mutableString == nil)
{
return NO;
}
// …
string = [NSString stringWithFormat:@"%@\n%@\n\n", [NSDate date], aLogText];
if([string length] < 1)
{
return NO;
}
// …
[mutableString insertString:string
atIndex:0];
// …
[textView setString:mutableString];
// …
return YES;
// …
}
@end