// // ClickableImageView.m // cunnyfinder // // Created by James Shiffer on 12/20/22. // Copyright © 2022 FemboyFinancial. All rights reserved. // #import "ClickableImageView.h" @implementation ClickableImageView -(id)initWithFrame:(NSRect)frame imageUrl:(NSURL *)fileUrl { self = [super initWithFrame:frame]; self.fileUrl = fileUrl; return self; } -(void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; // Drawing code here. } -(void)downloadImage:(NSString *)filePath { NSOperationQueue* queue = [NSOperationQueue new]; NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadAndSaveImage:) object:filePath]; [queue addOperation:operation]; } -(void)downloadAndSaveImage:(NSString *)filePath { NSData* imageData = [[NSData alloc] initWithContentsOfURL:self.fileUrl]; NSFileManager* fs = [[NSFileManager alloc] init]; BOOL success = [fs createFileAtPath:filePath contents:imageData attributes:nil]; if (!success) { [self performSelectorOnMainThread:@selector(showWarning:) withObject:nil waitUntilDone:YES]; } } -(void)showWarning:(id)sender { NSAlert *alert = [[NSAlert alloc] init]; [alert addButtonWithTitle:@"OK"]; [alert setMessageText:@"Uohhh!"]; [alert setIcon:[NSImage imageNamed:NSImageNameCaution]]; [alert setInformativeText:@"Damn bratty file failed to save... correction is needed!"]; [alert setAlertStyle:NSWarningAlertStyle]; [alert beginSheetModalForWindow:self.window completionHandler:nil]; } -(void)mouseDown:(NSEvent *)event { NSSavePanel* panel = [NSSavePanel savePanel]; panel.allowedFileTypes = @[[self.fileUrl pathExtension]]; panel.nameFieldStringValue = [self.fileUrl lastPathComponent]; [panel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) { if (result == NSFileHandlingPanelOKButton) { [self downloadImage:[panel filename]]; } }]; } @end