Skip to content

Commit

Permalink
Changed support for password protected archives. It seems to work now…
Browse files Browse the repository at this point in the history
…. Untested and the dialog does not re-appear on failure.
  • Loading branch information
Alexander Rauchfuss committed Apr 26, 2011
1 parent a36c12f commit 4af13e0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
100 changes: 51 additions & 49 deletions Managed Objects/TSSTManagedGroup.m
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,18 @@ - (id)instance
{
if (!instance)
{
NSFileManager * manager = [NSFileManager defaultManager];
if([manager fileExistsAtPath: [self valueForKey: @"path"]])
{
instance = [[XADArchive alloc] initWithFile: [self valueForKey: @"path"]];
// Set the archive delegate so that password and encoding queries can have a modal pop up.
[instance setDelegate: [NSApp delegate]];
NSFileManager * manager = [NSFileManager defaultManager];
if([manager fileExistsAtPath: [self valueForKey: @"path"]])
{
instance = [[XADArchive alloc] initWithFile: [self valueForKey: @"path"] delegate: self error:NULL];

// Set the archive delegate so that password and encoding queries can have a modal pop up.

if([self valueForKey: @"password"])
{
[instance setPassword: [self valueForKey: @"password"]];
}
}
if([self valueForKey: @"password"])
{
[instance setPassword: [self valueForKey: @"password"]];
}
}
}

return instance;
Expand Down Expand Up @@ -315,25 +315,6 @@ - (NSManagedObject *)topLevelGroup
- (void)nestedArchiveContents
{
XADArchive * imageArchive = [self valueForKey: @"instance"];

if([imageArchive isEncrypted])
{
NSString * password = nil;
NSData * testData = nil;
do
{
password = [[NSApp delegate] passwordForArchiveWithPath: [self valueForKey: @"path"]];
[imageArchive setPassword: password];
testData = [self dataForPageIndex: 1];
} while(password && !testData);

if(!testData)
{
return;
}

[self setValue: password forKey: @"password"];
}

NSFileManager * fileManager = [NSFileManager defaultManager];
NSData * fileData;
Expand Down Expand Up @@ -369,26 +350,26 @@ - (void)nestedArchiveContents
}
else if([[[NSUserDefaults standardUserDefaults] valueForKey: TSSTNestedArchives] boolValue] && [[TSSTManagedArchive archiveExtensions] containsObject: extension])
{
fileData = [imageArchive contentsOfEntry: counter];
nestedDescription = [NSEntityDescription insertNewObjectForEntityForName: @"Archive" inManagedObjectContext: [self managedObjectContext]];
[nestedDescription setValue: fileName forKey: @"name"];
[nestedDescription setValue: [NSNumber numberWithBool: YES] forKey: @"nested"];

collision = 0;
do {
archivePath = [NSString stringWithFormat: @"%i-%@", collision, fileName];
archivePath = [NSTemporaryDirectory() stringByAppendingPathComponent: archivePath];
++collision;
} while ([fileManager fileExistsAtPath: archivePath]);

[[NSFileManager defaultManager] createDirectoryAtPath: [archivePath stringByDeletingLastPathComponent]
withIntermediateDirectories: YES
attributes: nil
error: NULL];
[[NSFileManager defaultManager] createFileAtPath: archivePath contents: fileData attributes: nil];
fileData = [imageArchive contentsOfEntry: counter];
nestedDescription = [NSEntityDescription insertNewObjectForEntityForName: @"Archive" inManagedObjectContext: [self managedObjectContext]];
[nestedDescription setValue: fileName forKey: @"name"];
[nestedDescription setValue: [NSNumber numberWithBool: YES] forKey: @"nested"];

[nestedDescription setValue: archivePath forKey: @"path"];
[(TSSTManagedArchive *)nestedDescription nestedArchiveContents];
collision = 0;
do {
archivePath = [NSString stringWithFormat: @"%i-%@", collision, fileName];
archivePath = [NSTemporaryDirectory() stringByAppendingPathComponent: archivePath];
++collision;
} while ([fileManager fileExistsAtPath: archivePath]);

[[NSFileManager defaultManager] createDirectoryAtPath: [archivePath stringByDeletingLastPathComponent]
withIntermediateDirectories: YES
attributes: nil
error: NULL];
[[NSFileManager defaultManager] createFileAtPath: archivePath contents: fileData attributes: nil];

[nestedDescription setValue: archivePath forKey: @"path"];
[(TSSTManagedArchive *)nestedDescription nestedArchiveContents];
}
else if([[TSSTPage textExtensions] containsObject: extension])
{
Expand Down Expand Up @@ -432,6 +413,27 @@ - (BOOL)quicklookCompatible
}


/** Delegates **/

/* Called when Simple Comic encounters a password protected
archive. Brings a password dialog forward. */
-(void)archiveNeedsPassword:(XADArchive *)archive
{
NSString * password = [self valueForKey: @"password"];

if(password)
{
[archive setPassword: password];
return;
}

password = [[NSApp delegate] passwordForArchiveWithPath: [self valueForKey: @"path"]];
[archive setPassword: password];

[self setValue: password forKey: @"password"];
}


@end


Expand Down
3 changes: 1 addition & 2 deletions SimpleComicAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,7 @@ - (void)updateEncodingMenuTestedAgainst:(NSData *)data
}


/* Called when Simple Comic encounters a password protected
archive. Brings a password dialog forward. */

- (NSString*)passwordForArchiveWithPath:(NSString*)filename
{
NSString* password = nil;
Expand Down

0 comments on commit 4af13e0

Please sign in to comment.