conversionjob.m

来自「处理WORD文档的」· M 代码 · 共 276 行

M
276
字号
////  ConversionJob.m//  ////  Created by andrew on Thu Apr 12 2001.// Andrew C. Stone and Stone Design Corp//#include <stdio.h>#include <stdlib.h>#import <Carbon/Carbon.h>#import <Cocoa/Cocoa.h>#import "NSFileManager-Extensions.h"#import "ConversionJob.h"#import "DRController.h"#import "SDLogTask.h"@implementation ConversionJobNSString *JobStatusDidChangeNotification = @"JobStatusDidChange";/*        Usage: antiword [switches] wordfile1 [wordfile2 ...]        Switches: [-t|-p papersize][-m mapping][-w #][-i #][-X #][-Ls]                -t text output (default)                -p <paper size name> PostScript output                   like: a4, letter or legal                -w <width> in characters of text output    -i <level> image level (PostScript only)    -m <mapping> character mapping file    -X <encoding> character set (Postscript only)                -L use landscape mode (PostScript only)                -s Show hidden (by Word) text*/// We live in a world of both file extensions AND traditional Mac types - so let's look for both:+ (NSArray *)convertibleFileTypes {    return [NSArray arrayWithObjects:@"doc",@"DOC",NSFileTypeForHFSTypeCode('WDBN'),nil];}+ (BOOL)canConvertFile:(NSString *)path {    NSString *extension = [path pathExtension];        // IF there is no extension, let's see if there is a Mac Type:    if (IS_NULL(extension)) extension = NSHFSTypeOfFile(path);        if ([[self convertibleFileTypes] containsObject:extension]) return YES;    return NO;}- (id)init {    self = [super init];    if (self) {        inputPath = @"";        outputPath = @"";        statusString = @"";        conversionStatus = ConversionNeedsInfo;    }    return self;}// the designated initializer- (id)initWithInputFile:(NSString *)docFile outputFile:(NSString *)outpath landscape:(BOOL)landscape paperName:(NSString *)paper; {    self = [self init];        inputPath = [docFile copyWithZone:[self zone]];    outputPath = [outpath copyWithZone:[self zone]];    paperName = [paper copyWithZone:[self zone]];    _isLandscape = landscape;    return self;}// Don't Litter!- (void)dealloc {    [inputPath release];    [outputPath release];    [statusString release];    [paperName release];    [super dealloc];}// note that we do not copy status or status string!- (id)copyWithZone:(NSZone *)zone {    ConversionJob *newJob = [[ConversionJob allocWithZone:zone] init];    [newJob setInputPath:inputPath];    [newJob setOutputPath:outputPath];    [newJob setPaperName:paperName];    [newJob setLandscape:_isLandscape];    return newJob;}// sets and gets- (NSString *)paperName {    return paperName;}- (void)setPaperName:(NSString *)path {    if (![path isEqualToString:paperName]) {        [paperName release];        paperName = [path copyWithZone:[self zone]];        [self setConversionStatus:ConversionReadyToBegin];    }}- (void)setLandscape:(BOOL)bin {   _isLandscape = bin;}- (BOOL)landscape {    return _isLandscape;}- (NSString *)inputPath {    return inputPath;}- (void)setInputPath:(NSString *)path {    if (![path isEqualToString:inputPath]) {        [inputPath release];        inputPath = [path copyWithZone:[self zone]];        [self setConversionStatus:ConversionReadyToBegin];    }}- (NSString *)outputPath {    return outputPath;}- (void)setOutputPath:(NSString *)path {    if (![path isEqualToString:outputPath]) {        [outputPath release];        outputPath = [path copyWithZone:[self zone]];    }}+ (BOOL)pathValid:(NSString *)path {    BOOL isDir;    return (NOT_NULL(path) && [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir] && isDir && [[NSFileManager defaultManager] isWritableFileAtPath:path]);}- (NSString *)defaultOutputFolder:(NSString *)nextToInput {   return [[NSFileManager defaultManager] temporaryDirectory];}- (BOOL)canWriteTo:(NSString *)file {    return [[NSFileManager defaultManager]isWritableFileAtPath:[file stringByDeletingLastPathComponent]];}// This method will create a valid output path:- (NSString *)validOutputPath {    static int unique = 0;    if (NOT_NULL(outputPath) && [self canWriteTo:outputPath]) return outputPath;        else {        [self setOutputPath:[[[NSFileManager defaultManager] temporaryDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%d.%@",[[inputPath lastPathComponent] stringByDeletingPathExtension],++unique,[inputPath pathExtension]]]];	return outputPath;    }}- (NSString *)statusString {    if (NOT_NULL(statusString))         return statusString;    else {        switch(conversionStatus) {            case ConversionNeedsInfo:                return NSLocalizedStringFromTable(@"Awaiting Input Font or Folder - drag one on!",@"DOC",@"status string when info is still needed");            case ConversionReadyToBegin:                return NSLocalizedStringFromTable(@"Initializing Job",@"DOC",@"status string when job starts");            case ConversionBusy:                return [NSString stringWithFormat:@"%@ %@...",NSLocalizedStringFromTable(@"Converting",@"DOC",@"status string when conversion is happening"),[inputPath lastPathComponent]];            case ConversionDone:                return NSLocalizedStringFromTable(@"Conversion complete - drag it out!",@"DOC",@"status string when conversion is done");            case ConversionAborted:                return NSLocalizedStringFromTable(@"Conversion was stopped",@"DOC",@"status string when conversion is stopped");	    case ConversionError:	    default:	                return NSLocalizedStringFromTable(@"ERROR! See Log...",@"DOC",@"status string when error converting");        }    }}- (void)abort {// PENDING: kill the job!    [self setConversionStatus:ConversionAborted];}- (void)setStatusString:(NSString *)status {    if (![status isEqualToString:statusString]) {	[statusString release];        statusString = [status copyWithZone:[self zone]];    }}- (ConversionStatus)conversionStatus {   return conversionStatus;}- (void)setConversionStatus:(ConversionStatus)status {    conversionStatus = status;    [[NSNotificationCenter defaultCenter] postNotificationName:JobStatusDidChangeNotification object:self];}- (NSArray *)arguments {    NSMutableArray *args = [NSMutableArray array];    [args addObject:@"-p"];    [args addObject:paperName];        if (_isLandscape)    [args addObject:@"-L"];    [args addObject:inputPath];    // future options can be added here...    return args;  }//// Here is the callback from SDLogText// We'll pass it on up to DRController//- (void)taskTerminated:(BOOL)success{    [[NSApp delegate] taskTerminated:success outputFile:outputPath];}- (int)doExecution:(id)delegate {    volatile int statusAtCompletion = -1;        // we place each execution inside of it's own NSAutoreleasePool    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];        // the actual executable is inside the folder 'antiword'    NSString *antiword = [[NSBundle mainBundle]pathForResource:@"antiword" ofType:@""];  // the folder!    [self setConversionStatus:ConversionBusy];    [self validOutputPath];// Here's my groovy Task wrapper which logs errors:// the one interesting variable is the dictionary passed into the environment:// We need to set the HOME (where the resources will be sought by antiword)//         [[SDLogTask alloc]initAndLaunchWithArgs:[self arguments] executable:[antiword stringByAppendingPathComponent:@"antiword"]                directory:antiword                     logToText:[[NSApp delegate] logText] includeStandardOutput:NO outFile:outputPath owner:self environment:[NSDictionary dictionaryWithObjectsAndKeys:antiword, @"HOME",nil]];                                  [pool release];    return 1;}@end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?