Skip to content
This repository
tree: 17d9056eb0
Fetching contributors…

Cannot retrieve contributors at this time

file 185 lines (135 sloc) 7.287 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/*
Fraise version 3.7 - Based on Smultron by Peter Borg
Written by Jean-François Moy - jeanfrancois.moy@gmail.com
Find the latest version at http://github.com/jfmoy/Fraise

Copyright 2010 Jean-François Moy
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

#import "FRAStandardHeader.h"

#import "FRAAuthenticationController.h"
#import "FRAVariousPerformer.h"
#import "FRAProjectsController.h"
#import "FRAOpenSavePerformer.h"



@implementation FRAAuthenticationController

static id sharedInstance = nil;

+ (FRAAuthenticationController *)sharedInstance
{
if (sharedInstance == nil) {
sharedInstance = [[self alloc] init];
}

return sharedInstance;
}


- (id)init
{
    if (sharedInstance == nil) {
        sharedInstance = [super init];
    }
    return sharedInstance;
}


- (void)authenticateOpenSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
[sheet close];
[FRAVarious stopModalLoop];
if (returnCode == NSAlertDefaultReturn) {
[self performAuthenticatedOpenOfPath:[(NSArray *)contextInfo objectAtIndex:0] withEncoding:[[(NSArray *)contextInfo objectAtIndex:1] unsignedIntegerValue]];
}
}


- (void)authenticateSaveSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
[sheet close];
[FRAVarious stopModalLoop];
if (returnCode == NSAlertDefaultReturn) {
[self performAuthenticatedSaveOfDocument:[(NSArray *)contextInfo objectAtIndex:0] data:[(NSArray *)contextInfo objectAtIndex:1] path:[(NSArray *)contextInfo objectAtIndex:2] fromSaveAs:[[(NSArray *)contextInfo objectAtIndex:3] boolValue] aCopy:[[(NSArray *)contextInfo objectAtIndex:4] boolValue]];
}
}


- (void)performAuthenticatedOpenOfPath:(NSString *)path withEncoding:(NSStringEncoding)encoding
{
NSTask *task = [[NSTask alloc] init];
    NSPipe *pipe = [[NSPipe alloc] init];
    NSFileHandle *fileHandle = [pipe fileHandleForReading];

    [task setLaunchPath:@"/usr/libexec/authopen"];
    [task setArguments:[NSArray arrayWithObjects:path, nil]];
    [task setStandardOutput:pipe];

    [task launch];

    NSData *data = [[NSData alloc] initWithData:[fileHandle readDataToEndOfFile]];;

[task waitUntilExit];
NSInteger status = [task terminationStatus];

if (status != 0) {
NSString *title = [NSString stringWithFormat:NSLocalizedString(@"There was a unknown error when trying to open the file %@ with authentication", @"Indicate that there was a unknown error when trying to open the file %@ with authentication in Unknown-error-when-opening-with-authentication sheet"), path];
[FRAVarious standardAlertSheetWithTitle:title message:NSLocalizedString(@"Please check the permissions for the file and the enclosing folder and try again", @"Indicate that they should please check the permissions for the file and the enclosing folder and try again in Unknown-error-when-opening-with-authentication sheet") window:FRACurrentWindow];
} else {
[FRAOpenSave shouldOpenPartTwo:path withEncoding:encoding data:data];
}
}


- (void)performAuthenticatedSaveOfDocument:(id)document data:(NSData *)data path:(NSString *)path fromSaveAs:(BOOL)fromSaveAs aCopy:(BOOL)aCopy
{
NSString *convertedPath = [NSString stringWithUTF8String:[path UTF8String]];
NSTask *task = [[NSTask alloc] init];
    NSPipe *pipe = [[NSPipe alloc] init];
    NSFileHandle *writeHandle = [pipe fileHandleForWriting];

    [task setLaunchPath:@"/usr/libexec/authopen"];
[task setArguments:[NSArray arrayWithObjects:@"-c", @"-w", convertedPath, nil]];
    [task setStandardInput:pipe];

[task launch];
[writeHandle writeData:data];

close([writeHandle fileDescriptor]); // Close it manually
[writeHandle setValue:[NSNumber numberWithUnsignedShort:1] forKey:@"_flags"];

[task waitUntilExit];

NSInteger status = [task terminationStatus];

if (status != 0) {
NSString *title = [NSString stringWithFormat:NSLocalizedString(@"There was a unknown error when trying to save the file %@ with authentication", @"Indicate that there was a unknown error when trying to save the file %@ with authentication in Unknown-error-when-saving-with-authentication sheet"), path];
[FRAVarious standardAlertSheetWithTitle:title message:NSLocalizedStringFromTable(@"Please check if the file is locked, on a media that is unwritable or if you can save it in another location", @"Localizable3", @"Please check if the file is locked, on a media that is unwritable or if you can save it in another location") window:FRACurrentWindow];
} else {
if (!aCopy) {
[FRAOpenSave updateAfterSaveForDocument:document path:path];
}
}
}


- (void)installCommandLineUtility
{
NSString *fraisePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"fraise"];
NSData *fraiseData = [[NSData alloc] initWithContentsOfFile:fraisePath];
NSString *fraiseManPagePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"fraise.1"];
NSData *fraiseManPageData = [[NSData alloc] initWithContentsOfFile:fraiseManPagePath];

NSTask *task = [[NSTask alloc] init];
    NSPipe *pipe = [[NSPipe alloc] init];
    NSFileHandle *writeHandle = [pipe fileHandleForWriting];

    [task setLaunchPath:@"/usr/libexec/authopen"];
    [task setArguments:[NSArray arrayWithObjects:@"-c", @"-m", @"0755", @"-w", @"/usr/bin/fraise", nil]];
    [task setStandardInput:pipe];

[task launch];

NSInteger status;
signal(SIGPIPE, SIG_IGN); // One seems to need this code if someone writes the wrong password three times, otherwise it crashes the application
@try {
[writeHandle writeData:fraiseData];

close([writeHandle fileDescriptor]); // Close it manually
[writeHandle setValue:[NSNumber numberWithUnsignedShort:1] forKey:@"_flags"];
}
@catch (NSException *exception) {
status = 1;
}
@finally {
}

[task waitUntilExit];

status = [task terminationStatus];

if (status == 0) {
task = [[NSTask alloc] init];
pipe = [[NSPipe alloc] init];
writeHandle = [pipe fileHandleForWriting];

[task setLaunchPath:@"/usr/libexec/authopen"];
[task setArguments:[NSArray arrayWithObjects:@"-c", @"-w", @"/usr/share/man/man1/fraise.1", nil]];
[task setStandardInput:pipe];

[task launch];
[writeHandle writeData:fraiseManPageData];

close([writeHandle fileDescriptor]); // Close it manually
[writeHandle setValue:[NSNumber numberWithUnsignedShort:1] forKey:@"_flags"];

[task waitUntilExit];

status = [task terminationStatus];
}

if (status != 0) {
[FRAVarious standardAlertSheetWithTitle:NSLocalizedString(@"There was a unknown error when trying to install the command-line utility", @"Indicate that there was a unknown error when trying to install the command-linbe utility in Unknown-error-when-installing-comman-line-utility sheet") message:@"" window:FRACurrentWindow];
}
}

@end
Something went wrong with that request. Please try again.