Revision: 4112
          http://skim-app.svn.sourceforge.net/skim-app/?rev=4112&view=rev
Author:   hofman
Date:     2008-06-30 13:09:01 -0700 (Mon, 30 Jun 2008)

Log Message:
-----------
Use single document method to find file extension for type, define it on Tiger. 
Change return types of some script command handlers.

Modified Paths:
--------------
    trunk/SKApplication.m
    trunk/SKNotesDocument.h
    trunk/SKNotesDocument.m
    trunk/SKPDFDocument.h
    trunk/SKPDFDocument.m

Modified: trunk/SKApplication.m
===================================================================
--- trunk/SKApplication.m       2008-06-30 17:57:01 UTC (rev 4111)
+++ trunk/SKApplication.m       2008-06-30 20:09:01 UTC (rev 4112)
@@ -83,7 +83,7 @@
     [super terminate:sender];
 }
 
-- (void)handleOpenScriptCommand:(NSScriptCommand *)command {
+- (id)handleOpenScriptCommand:(NSScriptCommand *)command {
        NSDictionary *args = [command evaluatedArguments];
     id file = [command directParameter];
        id lineNumber = [args objectForKey:@"line"];
@@ -92,7 +92,7 @@
     if (lineNumber == nil || ([file isKindOfClass:[NSArray class]] && [file 
count] != 1)) {
         if ([[SKApplication superclass] instancesRespondToSelector:_cmd])
             [super handleOpenScriptCommand:command];
-        return;
+        return nil;
     }
        
     if ([file isKindOfClass:[NSArray class]])
@@ -128,7 +128,7 @@
         [command setScriptErrorString:@"File argument is not a file."];
     }
     
-    return;
+    return nil;
 }
 
 - (void)reorganizeWindowsItem:(NSWindow *)aWindow {

Modified: trunk/SKNotesDocument.h
===================================================================
--- trunk/SKNotesDocument.h     2008-06-30 17:57:01 UTC (rev 4111)
+++ trunk/SKNotesDocument.h     2008-06-30 20:09:01 UTC (rev 4112)
@@ -45,6 +45,7 @@
     IBOutlet NSArrayController *arrayController;
     IBOutlet SKStatusBar *statusBar;
     NSMutableArray *notes;
+    BOOL exportUsingPanel;
 }
 
 - (IBAction)openPDF:(id)sender;

Modified: trunk/SKNotesDocument.m
===================================================================
--- trunk/SKNotesDocument.m     2008-06-30 17:57:01 UTC (rev 4111)
+++ trunk/SKNotesDocument.m     2008-06-30 20:09:01 UTC (rev 4112)
@@ -54,9 +54,12 @@
 #import "NSWindowController_SKExtensions.h"
 #import "NSDocument_SKExtensions.h"
 #import "NSMenu_SKExtensions.h"
+#import "NSView_SKExtensions.h"
 
 static NSString *SKNotesDocumentWindowFrameAutosaveName = 
@"SKNotesDocumentWindow";
 
+static NSString *SKLastExportedNotesTypeKey = @"SKLastExportedNotesType";
+
 static NSString *SKNotesDocumentRowHeightKey = @"rowHeight";
 static NSString *SKNotesDocumentChildKey = @"child";
 
@@ -115,15 +118,52 @@
 }
 
 - (NSString *)fileNameExtensionForType:(NSString *)typeName 
saveOperation:(NSSaveOperationType)saveOperation {
-    // this will never be called on 10.4, so we can safely call super
-    NSString *fileExtension = [super fileNameExtensionForType:typeName 
saveOperation:saveOperation];
-    if (fileExtension == nil) {
-        if ([[[NSDocumentController sharedDocumentController] 
customExportTemplateFiles] containsObject:typeName])
+    NSString *fileExtension = nil;
+    if ([[SKNotesDocument superclass] instancesRespondToSelector:_cmd]) {
+        fileExtension = [super fileNameExtensionForType:typeName 
saveOperation:saveOperation];
+        if (fileExtension == nil && [[[NSDocumentController 
sharedDocumentController] customExportTemplateFiles] containsObject:typeName])
             fileExtension = [typeName pathExtension];
-       }
+    } else {
+        NSArray *fileExtensions = [[NSDocumentController 
sharedDocumentController] fileExtensionsFromType:typeName];
+        if ([fileExtensions count])
+            fileExtension = [fileExtensions objectAtIndex:0];
+    }
     return fileExtension;
 }
 
+- (BOOL)prepareSavePanel:(NSSavePanel *)savePanel {
+    BOOL success = [super prepareSavePanel:savePanel];
+    if (success && exportUsingPanel) {
+        NSPopUpButton *formatPopup = [[savePanel accessoryView] 
subviewOfClass:[NSPopUpButton class]];
+        if (formatPopup) {
+            NSString *lastExportedType = [[NSUserDefaults 
standardUserDefaults] stringForKey:SKLastExportedNotesTypeKey];
+            if (lastExportedType) {
+                int idx = [formatPopup 
indexOfItemWithRepresentedObject:lastExportedType];
+                if (idx != -1 && idx != [formatPopup indexOfSelectedItem]) {
+                    [formatPopup selectItemAtIndex:idx];
+                    [formatPopup sendAction:[formatPopup action] 
to:[formatPopup target]];
+                    [savePanel setAllowedFileTypes:[NSArray 
arrayWithObjects:[self fileNameExtensionForType:lastExportedType 
saveOperation:NSSaveToOperation], nil]];
+                }
+            }
+        }
+    }
+    return success;
+}
+
+- (void)runModalSavePanelForSaveOperation:(NSSaveOperationType)saveOperation 
delegate:(id)delegate didSaveSelector:(SEL)didSaveSelector contextInfo:(void 
*)contextInfo {
+    // Override so we can determine if this is a save, saveAs or export 
operation, so we can prepare the correct accessory view
+    if (saveOperation == NSSaveToOperation)
+        exportUsingPanel = YES;
+    [super runModalSavePanelForSaveOperation:saveOperation delegate:delegate 
didSaveSelector:didSaveSelector contextInfo:contextInfo];
+}
+
+- (BOOL)saveToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName 
forSaveOperation:(NSSaveOperationType)saveOperation error:(NSError **)outError{
+    if (saveOperation == NSSaveToOperation && exportUsingPanel)
+        [[NSUserDefaults standardUserDefaults] setObject:typeName 
forKey:SKLastExportedNotesTypeKey];
+    exportUsingPanel = NO;
+    return [super saveToURL:absoluteURL ofType:typeName 
forSaveOperation:saveOperation error:outError];
+}
+
 - (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
     NSData *data = nil;
     

Modified: trunk/SKPDFDocument.h
===================================================================
--- trunk/SKPDFDocument.h       2008-06-30 17:57:01 UTC (rev 4111)
+++ trunk/SKPDFDocument.h       2008-06-30 20:09:01 UTC (rev 4112)
@@ -136,16 +136,16 @@
 - (void)setSelectionQDRect:(NSData *)inQDBoundsAsData;
 - (id)selectionPage;
 - (void)setSelectionPage:(PDFPage *)page;
-- (id)handleRevertScriptCommand:(NSScriptCommand *)command;
-- (id)handleGoToScriptCommand:(NSScriptCommand *)command;
+- (void)handleRevertScriptCommand:(NSScriptCommand *)command;
+- (void)handleGoToScriptCommand:(NSScriptCommand *)command;
 - (id)handleFindScriptCommand:(NSScriptCommand *)command;
-- (id)handleShowTeXScriptCommand:(NSScriptCommand *)command;
+- (void)handleShowTeXScriptCommand:(NSScriptCommand *)command;
 
 @end
 
 
 @interface NSWindow (SKScriptingExtensions)
-- (id)handleRevertScriptCommand:(NSScriptCommand *)command;
+- (void)handleRevertScriptCommand:(NSScriptCommand *)command;
 @end
 
 

Modified: trunk/SKPDFDocument.m
===================================================================
--- trunk/SKPDFDocument.m       2008-06-30 17:57:01 UTC (rev 4111)
+++ trunk/SKPDFDocument.m       2008-06-30 20:09:01 UTC (rev 4112)
@@ -226,13 +226,10 @@
 
 - (NSString *)fileNameExtensionForType:(NSString *)typeName 
saveOperation:(NSSaveOperationType)saveOperation {
     NSString *fileExtension = nil;
-    // this should never be called on 10.4, but just make sure
     if ([[SKPDFDocument superclass] instancesRespondToSelector:_cmd]) {
         fileExtension = [super fileNameExtensionForType:typeName 
saveOperation:saveOperation];
-        if (fileExtension == nil) {
-            if ([[[NSDocumentController sharedDocumentController] 
customExportTemplateFiles] containsObject:typeName])
-                fileExtension = [typeName pathExtension];
-        }
+        if (fileExtension == nil && [[[NSDocumentController 
sharedDocumentController] customExportTemplateFiles] containsObject:typeName])
+            fileExtension = [typeName pathExtension];
     } else {
         NSArray *fileExtensions = [[NSDocumentController 
sharedDocumentController] fileExtensionsFromType:typeName];
         if ([fileExtensions count])
@@ -242,7 +239,8 @@
 }
 
 - (BOOL)prepareSavePanel:(NSSavePanel *)savePanel {
-    if (exportUsingPanel) {
+    BOOL success = [super prepareSavePanel:savePanel];
+    if (success && exportUsingPanel) {
         NSPopUpButton *formatPopup = [[savePanel accessoryView] 
subviewOfClass:[NSPopUpButton class]];
         if (formatPopup) {
             NSString *lastExportedType = [[NSUserDefaults 
standardUserDefaults] stringForKey:SKLastExportedTypeKey];
@@ -256,17 +254,12 @@
                 if (idx != -1 && idx != [formatPopup indexOfSelectedItem]) {
                     [formatPopup selectItemAtIndex:idx];
                     [formatPopup sendAction:[formatPopup action] 
to:[formatPopup target]];
-                    NSArray *fileTypes = nil;
-                    if (NSAppKitVersionNumber > NSAppKitVersionNumber10_4)
-                        fileTypes = [NSArray arrayWithObjects:[self 
fileNameExtensionForType:lastExportedType saveOperation:NSSaveToOperation], 
nil];
-                    else
-                        fileTypes = [[NSDocumentController 
sharedDocumentController] fileExtensionsFromType:lastExportedType];
-                    [savePanel setAllowedFileTypes:fileTypes];
+                    [savePanel setAllowedFileTypes:[NSArray 
arrayWithObjects:[self fileNameExtensionForType:lastExportedType 
saveOperation:NSSaveToOperation], nil]];
                 }
             }
         }
     }
-    return YES;
+    return success;
 }
 
 - (void)runModalSavePanelForSaveOperation:(NSSaveOperationType)saveOperation 
delegate:(id)delegate didSaveSelector:(SEL)didSaveSelector contextInfo:(void 
*)contextInfo {
@@ -1841,14 +1834,14 @@
     return info;
 }
 
-// fix a bug in Apple's implementation, which ignores the file type (for 
export)
+// fix a bug in Apple's implementation, which ignores the file type (for 
export), also make some more sensible choice of save operation
 - (id)handleSaveScriptCommand:(NSScriptCommand *)command {
        NSDictionary *args = [command evaluatedArguments];
     id fileURL = [args objectForKey:@"File"];
     id fileType = [args objectForKey:@"FileType"];
-    // we don't want to expose the value of NSPDFPboardType to the user, we 
advertise this type as "PDF".
+    // we don't want to expose the value of SKPDFDocumentType to the user, we 
advertise this type as "PDF".
     if ([fileType isEqualToString:@"PDF"]) {
-        fileType = NSPDFPboardType;
+        fileType = SKPDFDocumentType;
         NSMutableDictionary *arguments = [[command arguments] mutableCopy];
         [arguments setObject:fileType forKey:@"FileType"];
         [command setArguments:arguments];
@@ -1859,28 +1852,22 @@
             [command setScriptErrorNumber:NSArgumentsWrongScriptError];
             [command setScriptErrorString:@"The file is not a file or alias."];
         } else {
-            NSArray *fileExtensions = nil;
+            NSSaveOperationType saveOperation = fileType ? NSSaveToOperation : 
NSSaveAsOperation;
+            fileType = fileType ? SKNormalizedDocumentType(fileType) : 
SKPDFDocumentType;
+            
+            NSString *requiredExtension = [self 
fileNameExtensionForType:fileType saveOperation:saveOperation];
             NSString *extension = [[fileURL path] pathExtension];
-            if (NSAppKitVersionNumber > NSAppKitVersionNumber10_4)
-                fileExtensions = [NSArray arrayWithObjects:[self 
fileNameExtensionForType:SKNormalizedDocumentType(fileType) 
saveOperation:fileType ? NSSaveToOperation : NSSaveAsOperation], nil];
-            else
-                fileExtensions = [[NSDocumentController 
sharedDocumentController] 
fileExtensionsFromType:SKNormalizedDocumentType(fileType)];
             
-            if (extension == nil && [fileExtensions count]) {
-                extension = [fileExtensions objectAtIndex:0];
+            if (extension == nil && requiredExtension) {
+                extension = requiredExtension;
                 fileURL = [NSURL fileURLWithPath:[[fileURL path] 
stringByAppendingPathExtension:extension]];
             }
-            if ([fileExtensions containsObject:[extension lowercaseString]] == 
NO) {
+            if (requiredExtension && [requiredExtension 
caseInsensitiveCompare:extension] != NSOrderedSame) {
                 [command setScriptErrorNumber:NSArgumentsWrongScriptError];
                 [command setScriptErrorString:[NSString 
stringWithFormat:@"Invalid file extension for this file type."]];
-            } else if (fileType) {
-                if ([self saveToURL:fileURL 
ofType:SKNormalizedDocumentType(fileType) forSaveOperation:NSSaveToOperation 
error:NULL] == NO) {
-                    [command setScriptErrorNumber:NSInternalScriptError];
-                    [command setScriptErrorString:@"Unable to export."];
-                }
-            } else if ([self saveToURL:fileURL ofType:NSPDFPboardType 
forSaveOperation:NSSaveAsOperation error:NULL] == NO) {
+            } else if ([self saveToURL:fileURL ofType:fileType 
forSaveOperation:saveOperation error:NULL] == NO) {
                 [command setScriptErrorNumber:NSInternalScriptError];
-                [command setScriptErrorString:@"Unable to save."];
+                [command setScriptErrorString:saveOperation == 
NSSaveToOperation ? @"Unable to export." : @"Unable to save."];
             }
         }
     } else if (fileType) {
@@ -1924,7 +1911,7 @@
     return nil;
 }
 
-- (id)handleRevertScriptCommand:(NSScriptCommand *)command {
+- (void)handleRevertScriptCommand:(NSScriptCommand *)command {
     if ([self fileURL] && [[NSFileManager defaultManager] 
fileExistsAtPath:[self fileName]]) {
         if ([self revertToContentsOfURL:[self fileURL] ofType:[self fileType] 
error:NULL] == NO) {
             [command setScriptErrorNumber:NSInternalScriptError];
@@ -1934,10 +1921,9 @@
         [command setScriptErrorNumber:NSArgumentsWrongScriptError];
         [command setScriptErrorString:@"File does not exist."];
     }
-    return nil;
 }
 
-- (id)handleGoToScriptCommand:(NSScriptCommand *)command {
+- (void)handleGoToScriptCommand:(NSScriptCommand *)command {
        NSDictionary *args = [command evaluatedArguments];
     id location = [args objectForKey:@"To"];
     
@@ -1960,7 +1946,6 @@
             [[self pdfView] scrollRect:bounds inPageToVisible:page];
         }
     }
-    return nil;
 }
 
 - (id)handleFindScriptCommand:(NSScriptCommand *)command {
@@ -1994,7 +1979,7 @@
     return specifier ? specifier : [NSArray array];
 }
 
-- (id)handleShowTeXScriptCommand:(NSScriptCommand *)command {
+- (void)handleShowTeXScriptCommand:(NSScriptCommand *)command {
        NSDictionary *args = [command evaluatedArguments];
     id page = [args objectForKey:@"Page"];
     id pointData = [args objectForKey:@"Point"];
@@ -2016,8 +2001,6 @@
         
         [[self synchronizer] findLineForLocation:point inRect:rect 
atPageIndex:pageIndex];
     }
-    
-    return nil;
 }
 
 @end
@@ -2025,14 +2008,13 @@
 
 @implementation NSWindow (SKScriptingExtensions)
 
-- (id)handleRevertScriptCommand:(NSScriptCommand *)command {
+- (void)handleRevertScriptCommand:(NSScriptCommand *)command {
     id document = [[self windowController] document];
     if (document == nil) {
         [command setScriptErrorNumber:NSArgumentsWrongScriptError];
         [command setScriptErrorString:@"Window does not have a document."];
-        return nil;
     } else
-        return [document handleRevertScriptCommand:command];
+        [document handleRevertScriptCommand:command];
 }
 
 @end


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to