Skip to content

Commit

Permalink
Verification logic moves to GPGServices: now cancelable and growled
Browse files Browse the repository at this point in the history
- decryptFiles now growls any signatures if possible, instead of
  opening a window
- adjust parameter so growl windows show GPGServices icon

- additional thread-safety in DummyVerificationController
- FileVerificationController now thread-safe for calls from
  workers (though actually now deprecated and unused)
  • Loading branch information
idodeclare committed May 19, 2012
1 parent a2d628d commit 53db2fd
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 126 deletions.
7 changes: 3 additions & 4 deletions Resources/en.lproj/InProgressWindow.xib
Expand Up @@ -216,7 +216,7 @@
<object class="NSButton" id="152201074"> <object class="NSButton" id="152201074">
<reference key="NSNextResponder" ref="292058056"/> <reference key="NSNextResponder" ref="292058056"/>
<int key="NSvFlags">268</int> <int key="NSvFlags">268</int>
<string key="NSFrame">{{434, 8}, {26, 32}}</string> <string key="NSFrame">{{434, 13}, {26, 20}}</string>
<reference key="NSSuperview" ref="292058056"/> <reference key="NSSuperview" ref="292058056"/>
<reference key="NSWindow"/> <reference key="NSWindow"/>
<reference key="NSNextKeyView"/> <reference key="NSNextKeyView"/>
Expand All @@ -233,13 +233,12 @@
</object> </object>
<string key="NSCellIdentifier">_NS:9</string> <string key="NSCellIdentifier">_NS:9</string>
<reference key="NSControlView" ref="152201074"/> <reference key="NSControlView" ref="152201074"/>
<int key="NSButtonFlags">-933478145</int> <int key="NSButtonFlags">106709247</int>
<int key="NSButtonFlags2">34</int> <int key="NSButtonFlags2">34</int>
<object class="NSCustomResource" key="NSNormalImage" id="499478659"> <object class="NSCustomResource" key="NSNormalImage">
<string key="NSClassName">NSImage</string> <string key="NSClassName">NSImage</string>
<string key="NSResourceName">NSStopProgressTemplate</string> <string key="NSResourceName">NSStopProgressTemplate</string>
</object> </object>
<reference key="NSAlternateImage" ref="499478659"/>
<string key="NSAlternateContents"/> <string key="NSAlternateContents"/>
<string key="NSKeyEquivalent"/> <string key="NSKeyEquivalent"/>
<int key="NSPeriodicDelay">400</int> <int key="NSPeriodicDelay">400</int>
Expand Down
5 changes: 3 additions & 2 deletions Source/DummyVerificationController.h
Expand Up @@ -17,10 +17,11 @@
IBOutlet NSProgressIndicator* indicator; IBOutlet NSProgressIndicator* indicator;
IBOutlet FileVerificationDataSource* dataSource; IBOutlet FileVerificationDataSource* dataSource;


BOOL isActive; BOOL _isActive;
} }


@property(assign) BOOL isActive; // thread-safe
@property (assign, nonatomic) BOOL isActive;


// thread-safe // thread-safe
- (void)showWindow:(id)sender; - (void)showWindow:(id)sender;
Expand Down
15 changes: 14 additions & 1 deletion Source/DummyVerificationController.m
Expand Up @@ -11,6 +11,7 @@


@interface DummyVerificationController (ThreadSafety) @interface DummyVerificationController (ThreadSafety)


- (void)setIsActiveOnMain:(NSNumber *)isActive;
- (void)showWindowOnMain:(id)sender; - (void)showWindowOnMain:(id)sender;
- (void)addResultsOnMain:(NSDictionary *)results; - (void)addResultsOnMain:(NSDictionary *)results;
- (void)addResultFromSigOnMain:(NSArray *)args; - (void)addResultFromSigOnMain:(NSArray *)args;
Expand All @@ -20,14 +21,26 @@ - (void)runModalOnMain:(NSMutableArray *)resHolder;


@implementation DummyVerificationController @implementation DummyVerificationController


@synthesize isActive; @synthesize isActive = _isActive;


- (id)initWithWindowNibName:(NSString *)windowNibName { - (id)initWithWindowNibName:(NSString *)windowNibName {
self = [super initWithWindowNibName:windowNibName]; self = [super initWithWindowNibName:windowNibName];


return self; return self;
} }


- (void)setIsActive:(BOOL)isActive {
[self performSelectorOnMainThread:@selector(setIsActiveOnMain:)
withObject:[NSNumber numberWithBool:isActive] waitUntilDone:NO];
}

// called by setIsActive
- (void)setIsActiveOnMain:(NSNumber *)isActive {
[self willChangeValueForKey:@"isActive"];
_isActive = [isActive boolValue];
[self didChangeValueForKey:@"isActive"];
}

- (void)windowDidLoad { - (void)windowDidLoad {
[self bind:@"isActive" toObject:dataSource withKeyPath:@"isActive" options:nil]; [self bind:@"isActive" toObject:dataSource withKeyPath:@"isActive" options:nil];
} }
Expand Down
9 changes: 4 additions & 5 deletions Source/FileVerificationController.h
Expand Up @@ -25,14 +25,13 @@
@property(retain) NSArray* filesToVerify; @property(retain) NSArray* filesToVerify;
@property(readonly) NSOperationQueue* verificationQueue; @property(readonly) NSOperationQueue* verificationQueue;


// threadSafe
- (NSInteger)runModal; - (NSInteger)runModal;
- (IBAction)okClicked:(id)sender;


//Callback contains all successfully checked files - (IBAction)okClicked:(id)sender;
- (void)startVerification:(void(^)(NSArray*))callback;


#pragma mark - Helper Methods #pragma mark - Helper Methods
- (NSString*)searchFileForSignatureFile:(NSString*)file; + (NSString*)searchFileForSignatureFile:(NSString*)file;
- (NSString*)searchSignatureFileForFile:(NSString*)sigFile; + (NSString*)searchSignatureFileForFile:(NSString*)sigFile;


@end @end
124 changes: 20 additions & 104 deletions Source/FileVerificationController.m
Expand Up @@ -12,6 +12,12 @@


#import "FileVerificationDataSource.h" #import "FileVerificationDataSource.h"


@interface FileVerificationController ()

- (void)runModalOnMain:(NSMutableArray *)resHolder;

@end

@implementation FileVerificationController @implementation FileVerificationController


@synthesize filesToVerify, verificationQueue; @synthesize filesToVerify, verificationQueue;
Expand Down Expand Up @@ -44,123 +50,33 @@ - (void)windowDidLoad {
} }


- (NSInteger)runModal { - (NSInteger)runModal {
NSMutableArray *resHolder = [NSMutableArray arrayWithCapacity:1];
[self performSelectorOnMainThread:@selector(runModalOnMain:) withObject:resHolder waitUntilDone:YES];
return [[resHolder lastObject] integerValue];
}

- (void)runModalOnMain:(NSMutableArray *)resHolder {
[NSApp activateIgnoringOtherApps:YES]; [NSApp activateIgnoringOtherApps:YES];
[self showWindow:self]; [self showWindow:nil];
NSInteger ret = [NSApp runModalForWindow:self.window]; NSInteger ret = [NSApp runModalForWindow:self.window];
[self.window close]; [self.window close];
return ret; [resHolder addObject:[NSNumber numberWithInteger:ret]];
} }


- (IBAction)okClicked:(id)sender { - (IBAction)okClicked:(id)sender {
[NSApp stopModalWithCode:0]; [NSApp stopModalWithCode:0];
} }


- (void)startVerification:(void(^)(NSArray*))callback {
//Load window to setup bindings
[self performSelectorOnMainThread:@selector(window) withObject:nil waitUntilDone:NO];
[indicator performSelectorOnMainThread:@selector(startAnimation:) withObject:self waitUntilDone:NO];

for(NSString* serviceFile in self.filesToVerify) {

//Do the file stuff here to be able to check if file is already in verification
NSString* signatureFile = serviceFile;
NSString* signedFile = [self searchFileForSignatureFile:signatureFile];
if(signedFile == nil) {
NSString* tmp = [self searchSignatureFileForFile:signatureFile];
signedFile = signatureFile;
signatureFile = tmp;
}

if(signatureFile != nil) {
if([filesInVerification containsObject:signatureFile]) {
continue;
} else {
//Probably a problem with restarting of validation when files are missing
[filesInVerification addObject:signatureFile];
}
}

[verificationQueue addOperationWithBlock:^(void) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSFileManager* fmgr = [[[NSFileManager alloc] init] autorelease];

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

NSArray* sigs = nil;

if([fmgr fileExistsAtPath:signedFile] && [fmgr fileExistsAtPath:signatureFile]) {
@try {
GPGController* ctx = [GPGController gpgController];
NSData* signatureFileData = [[[NSData alloc] initWithContentsOfFile:signatureFile] autorelease];
NSData* signedFileData = [[[NSData alloc] initWithContentsOfFile:signedFile] autorelease];
sigs = [ctx verifySignature:signatureFileData originalData:signedFileData];
} @catch (NSException *exception) {
firstException = exception;
sigs = nil;
}
}

//Try to verify the file itself without a detached sig
if(sigs == nil || sigs.count == 0) {
@try {
GPGController* ctx = [GPGController gpgController];
NSData* signedFileData = [[[NSData alloc] initWithContentsOfFile:serviceFile] autorelease];
sigs = [ctx verifySignedData:signedFileData];
} @catch (NSException *exception) {
secondException = exception;
sigs = nil;
}
}

if(sigs != nil) {
if(sigs.count == 0) {
id verificationResult = nil; //NSString or NSAttributedString
verificationResult = @"Verification FAILED: No signatures 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];

[verificationResult addAttribute:NSFontAttributeName
value:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]]
range:range];
[verificationResult addAttribute:NSBackgroundColorAttributeName
value:bgColor
range:range];

NSDictionary* result = [NSDictionary dictionaryWithObjectsAndKeys:
[signedFile lastPathComponent], @"filename",
verificationResult, @"verificationResult",
nil];
[dataSource addResults:result];
} else if(sigs.count > 0) {
for(GPGSignature* sig in sigs) {
[dataSource addResultFromSig:sig forFile:signedFile];
}
}
} else {
[dataSource addResults:[NSDictionary dictionaryWithObjectsAndKeys:
[signedFile lastPathComponent], @"filename",
@"No verifiable data found", @"verificationResult",
nil]];
}

[pool release];
}];
}
}

// Who invokes this and what does it do? // Who invokes this and what does it do?
- (void)observeValueForKeyPath:(NSString *)keyPath - (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object ofObject:(id)object
change:(NSDictionary *)change change:(NSDictionary *)change
context:(void *)context { context:(void *)context {
if([keyPath isEqualToString:@"operationCount"]) { if([keyPath isEqualToString:@"operationCount"]) {
if([object operationCount] == 0) if([object operationCount] == 0) {
[indicator stopAnimation:self]; [indicator performSelectorOnMainThread:@selector(stopAnimation:)
withObject:self waitUntilDone:NO];
}
} }
} }


Expand All @@ -172,7 +88,7 @@ - (void)doubleClickAction:(id)sender {


#pragma mark - Helper Methods #pragma mark - Helper Methods


- (NSString*)searchFileForSignatureFile:(NSString*)sigFile { + (NSString*)searchFileForSignatureFile:(NSString*)sigFile {
NSFileManager* fmgr = [[[NSFileManager alloc] init] autorelease]; NSFileManager* fmgr = [[[NSFileManager alloc] init] autorelease];


NSString* file = [sigFile stringByDeletingPathExtension]; NSString* file = [sigFile stringByDeletingPathExtension];
Expand All @@ -183,7 +99,7 @@ - (NSString*)searchFileForSignatureFile:(NSString*)sigFile {
return nil; return nil;
} }


- (NSString*)searchSignatureFileForFile:(NSString*)sigFile { + (NSString*)searchSignatureFileForFile:(NSString*)sigFile {
NSFileManager* fmgr = [[[NSFileManager alloc] init] autorelease]; NSFileManager* fmgr = [[[NSFileManager alloc] init] autorelease];


NSSet* exts = [NSSet setWithObjects:@".sig", @".asc", nil]; NSSet* exts = [NSSet setWithObjects:@".sig", @".asc", nil];
Expand Down

0 comments on commit 53db2fd

Please sign in to comment.