Skip to content

Commit

Permalink
Move formatting of results to dataSource, correctly colorize text
Browse files Browse the repository at this point in the history
  • Loading branch information
the-kenny committed Apr 22, 2011
1 parent 94e1c2a commit 70401aa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 68 deletions.
31 changes: 23 additions & 8 deletions FileVerificationDataSource.m
Expand Up @@ -43,23 +43,39 @@ - (void)addResultFromSig:(GPGSignature*)sig forFile:(NSString*)file {
if(GPGErrorCodeFromError([sig status]) == GPGErrorNoError) {
GPGContext* ctx = [[[GPGContext alloc] init] autorelease];
NSString* userID = [[ctx keyFromFingerprint:[sig fingerprint] secretKey:NO] userID];
NSString* validity = [sig validityDescription];
GPGValidity validity = [sig validity];
NSString* validityDesc = [sig validityDescription];

verificationResult = [NSString stringWithFormat:@"Signed by: %@ (%@ trust)", userID, validity];
switch(validity) {
case GPGValidityNever:
bgColor = [NSColor colorWithCalibratedRed:0.8 green:0.0 blue:0.0 alpha:0.7];
break;
case GPGValidityMarginal:
bgColor = [NSColor colorWithCalibratedRed:0.9 green:0.8 blue:0.0 alpha:1.0];
break;
case GPGValidityFull:
case GPGValidityUltimate:
bgColor = [NSColor colorWithCalibratedRed:0.0 green:0.8 blue:0.0 alpha:1.0];
break;
default:
bgColor = [NSColor whiteColor];
}

verificationResult = [NSString stringWithFormat:@"Signed by: %@ (%@ trust)", userID, validityDesc];
NSMutableAttributedString* tmp = [[[NSMutableAttributedString alloc] initWithString:verificationResult
attributes:nil] autorelease];
NSRange range = [verificationResult rangeOfString:[NSString stringWithFormat:@"(%@ trust)", validity]];
NSRange range = [verificationResult rangeOfString:[NSString stringWithFormat:@"(%@ trust)", validityDesc]];
[tmp addAttribute:NSFontAttributeName
value:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]]
range:range];
[tmp addAttribute:NSBackgroundColorAttributeName
value:[NSColor colorWithCalibratedRed:0.0 green:0.8 blue:0.0 alpha:1.0]
value:bgColor
range:range];

verificationResult = (NSString*)tmp;

bgColor = [NSColor greenColor];
} else {
bgColor = [NSColor colorWithCalibratedRed:0.8 green:0.0 blue:0.0 alpha:0.7];

verificationResult = [NSString stringWithFormat:@"Verification FAILED: %@", GPGErrorDescription([sig status])];
NSMutableAttributedString* tmp = [[[NSMutableAttributedString alloc] initWithString:verificationResult
attributes:nil] autorelease];
Expand All @@ -68,11 +84,10 @@ - (void)addResultFromSig:(GPGSignature*)sig forFile:(NSString*)file {
value:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]]
range:range];
[tmp addAttribute:NSBackgroundColorAttributeName
value:[NSColor colorWithCalibratedRed:0.8 green:0.0 blue:0.0 alpha:0.7]
value:bgColor
range:range];

verificationResult = (NSString*)tmp;
bgColor = [NSColor redColor];
}


Expand Down
79 changes: 19 additions & 60 deletions Source/FileVerificationController.m
Expand Up @@ -8,6 +8,7 @@

#import "FileVerificationController.h"
#import "MacGPGME/MacGPGME.h"
#import "FileVerificationDataSource.h"

@implementation FileVerificationController

Expand Down Expand Up @@ -81,10 +82,6 @@ - (void)startVerification:(void(^)(NSArray*))callback {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSFileManager* fmgr = [[[NSFileManager alloc] init] autorelease];

NSColor* bgColor = nil;
id verificationResult = nil; //NSString or NSAttributedString
BOOL verified = NO;

NSException* firstException = nil;
NSException* secondException = nil;

Expand Down Expand Up @@ -126,13 +123,13 @@ - (void)startVerification:(void(^)(NSArray*))callback {
}


NSDictionary* result = nil;
if(sigs != nil) {
verified = YES;

if(sigs.count == 0) {
id verificationResult = nil; //NSString or NSAttributedString
verificationResult = @"Verification FAILED: No signature data found.";

NSColor* bgColor = [NSColor colorWithCalibratedRed:0.8 green:0.0 blue:0.0 alpha:0.7];

NSRange range = [verificationResult rangeOfString:@"FAILED"];
verificationResult = [[NSMutableAttributedString alloc]
initWithString:verificationResult];
Expand All @@ -141,65 +138,27 @@ - (void)startVerification:(void(^)(NSArray*))callback {
value:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]]
range:range];
[verificationResult addAttribute:NSBackgroundColorAttributeName
value:[NSColor colorWithCalibratedRed:0.8 green:0.0 blue:0.0 alpha:0.7]
value:bgColor
range:range];

bgColor = [NSColor redColor];
NSDictionary* result = [NSDictionary dictionaryWithObjectsAndKeys:
[signedFile lastPathComponent], @"filename",
verificationResult, @"verificationResult",
bgColor, @"bgColor",
nil];

[[NSOperationQueue mainQueue] addOperationWithBlock:^(void) {
[dataSource addResults:result];
}];
} else if(sigs.count > 0) {
GPGSignature* sig=[sigs objectAtIndex:0];
if(GPGErrorCodeFromError([sig status]) == GPGErrorNoError) {
GPGContext* ctx = [[[GPGContext alloc] init] autorelease];
NSString* userID = [[ctx keyFromFingerprint:[sig fingerprint] secretKey:NO] userID];
NSString* validity = [sig validityDescription];

verificationResult = [NSString stringWithFormat:@"Signed by: %@ (%@ trust)", userID, validity];
NSMutableAttributedString* tmp = [[[NSMutableAttributedString alloc] initWithString:verificationResult
attributes:nil] autorelease];
NSRange range = [verificationResult rangeOfString:[NSString stringWithFormat:@"(%@ trust)", validity]];
[tmp addAttribute:NSFontAttributeName
value:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]]
range:range];
[tmp addAttribute:NSBackgroundColorAttributeName
value:[NSColor colorWithCalibratedRed:0.0 green:0.8 blue:0.0 alpha:1.0]
range:range];

verificationResult = (NSString*)tmp;

bgColor = [NSColor greenColor];
} else {
verificationResult = [NSString stringWithFormat:@"Verification FAILED: %@", GPGErrorDescription([sig status])];
NSMutableAttributedString* tmp = [[[NSMutableAttributedString alloc] initWithString:verificationResult
attributes:nil] autorelease];
NSRange range = [verificationResult rangeOfString:@"FAILED"];
[tmp addAttribute:NSFontAttributeName
value:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]]
range:range];
[tmp addAttribute:NSBackgroundColorAttributeName
value:[NSColor colorWithCalibratedRed:0.8 green:0.0 blue:0.0 alpha:0.7]
range:range];

verificationResult = (NSString*)tmp;
bgColor = [NSColor redColor];
for(GPGSignature* sig in sigs) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^(void) {
[dataSource addResultFromSig:sig forFile:signedFile];
}];
}
}

//Add to results
result = [NSDictionary dictionaryWithObjectsAndKeys:
[signedFile lastPathComponent], @"filename",
signatureFile, @"signaturePath",
verificationResult, @"verificationResult",
[NSNumber numberWithBool:verified], @"verificationSucceeded",
bgColor, @"bgColor",
nil];
}
}



if(result != nil)
[dataSource performSelectorOnMainThread:@selector(addResults:)
withObject:result
waitUntilDone:YES];

[pool release];
}];
}
Expand Down

0 comments on commit 70401aa

Please sign in to comment.