Revision: 2844
          http://skim-app.svn.sourceforge.net/skim-app/?rev=2844&view=rev
Author:   hofman
Date:     2007-09-07 12:57:23 -0700 (Fri, 07 Sep 2007)

Log Message:
-----------
Download external URLs to default download location. Don't remove file after 
finishing. Add a contextual menu to the download window. Don't remove finished 
downloads by default. 

Modified Paths:
--------------
    trunk/Files_SKExtensions.h
    trunk/Files_SKExtensions.m
    trunk/InitialUserDefaults.plist
    trunk/SKDownload.m
    trunk/SKDownloadController.m

Modified: trunk/Files_SKExtensions.h
===================================================================
--- trunk/Files_SKExtensions.h  2007-09-07 18:18:54 UTC (rev 2843)
+++ trunk/Files_SKExtensions.h  2007-09-07 19:57:23 UTC (rev 2844)
@@ -38,6 +38,10 @@
 
 #import <Cocoa/Cocoa.h>
 
+// This function is not thread safe
+
+extern NSURL *SKDownloadFolderURL();
+
 // These functions are thread safe
 
 extern BOOL SKFileIsInTrash(NSURL *fileURL);

Modified: trunk/Files_SKExtensions.m
===================================================================
--- trunk/Files_SKExtensions.m  2007-09-07 18:18:54 UTC (rev 2843)
+++ trunk/Files_SKExtensions.m  2007-09-07 19:57:23 UTC (rev 2844)
@@ -39,6 +39,36 @@
 #import "Files_SKExtensions.h"
 #import <Carbon/Carbon.h>
 
+NSURL *SKDownloadFolderURL() {
+    OSStatus err;
+       ICInstance inst;
+       ICAttr junk = 0;
+       ICFileSpec spec;
+    
+       static CFURLRef pathURL = NULL;
+    
+    if (NULL == pathURL) {
+        long size = sizeof(ICFileSpec);
+        FSRef pathRef;
+        
+        err = ICStart(&inst, 'SKim');
+        if (noErr == err)
+            err = ICBegin(inst, icReadOnlyPerm);
+        
+        if (err == noErr) {
+            err = ICGetPref(inst, kICDownloadFolder, &junk, &spec, &size);
+            if (noErr == err) {
+                ICEnd(inst);
+                ICStop(inst);
+            }
+            
+            err = FSpMakeFSRef(&(spec.fss), &pathRef);
+            if(err == noErr)
+                pathURL = CFURLCreateFromFSRef(CFAllocatorGetDefault(), 
&pathRef);
+        }
+    }
+    return (NSURL *)pathURL;
+}
 
 BOOL SKFileIsInTrash(NSURL *fileURL) {
     NSCParameterAssert([fileURL isFileURL]);    

Modified: trunk/InitialUserDefaults.plist
===================================================================
--- trunk/InitialUserDefaults.plist     2007-09-07 18:18:54 UTC (rev 2843)
+++ trunk/InitialUserDefaults.plist     2007-09-07 19:57:23 UTC (rev 2844)
@@ -235,7 +235,7 @@
         <key>SKAutoOpenDownloadsWindow</key>
         <true/>
         <key>SKAutoRemoveFinishedDownloads</key>
-        <true/>
+        <false/>
         <key>SKAutoCloseDownloadsWindow</key>
         <false/>
         <key>SUScheduledCheckInterval</key>

Modified: trunk/SKDownload.m
===================================================================
--- trunk/SKDownload.m  2007-09-07 18:18:54 UTC (rev 2843)
+++ trunk/SKDownload.m  2007-09-07 19:57:23 UTC (rev 2844)
@@ -38,8 +38,8 @@
 
 #import "SKDownload.h"
 #import <ApplicationServices/ApplicationServices.h>
+#import "Files_SKExtensions.h"
 
-
 @interface SKDownload (Private)
 - (void)setStatus:(int)newStatus;
 - (void)setFilePath:(NSString *)newFilePath;
@@ -78,7 +78,7 @@
 
 - (void)dealloc {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
-    [self cleanupDownload];
+    [self cancelDownload];
     [URL release];
     [URLDownload release];
     [filePath release];
@@ -88,7 +88,7 @@
 }
 
 - (void)handleApplicationWillTerminateNotification:(NSNotification 
*)notification {
-    [self cleanupDownload];
+    [self cancelDownload];
 }
 
 #pragma mark Accessors
@@ -306,9 +306,7 @@
 }
 
 - (void)download:(NSURLDownload *)download 
decideDestinationWithSuggestedFilename:(NSString *)filename {
-       NSString *tmpDir = [NSTemporaryDirectory() 
stringByAppendingPathComponent:[[NSProcessInfo processInfo] 
globallyUniqueString]];
-    [[NSFileManager defaultManager] createDirectoryAtPath:tmpDir 
attributes:nil];
-    [URLDownload setDestination:[tmpDir 
stringByAppendingPathComponent:filename] allowOverwrite:YES];
+    [URLDownload setDestination:[[SKDownloadFolderURL() path] 
stringByAppendingPathComponent:filename] allowOverwrite:NO];
 }
 
 - (void)download:(NSURLDownload *)download didCreateDestination:(NSString 
*)path {

Modified: trunk/SKDownloadController.m
===================================================================
--- trunk/SKDownloadController.m        2007-09-07 18:18:54 UTC (rev 2843)
+++ trunk/SKDownloadController.m        2007-09-07 19:57:23 UTC (rev 2844)
@@ -43,6 +43,7 @@
 #import "SKStringConstants.h"
 #import "SKTableView.h"
 #import "SKTypeSelectHelper.h"
+#import "NSString_SKExtensions.h"
 
 
 @implementation SKDownloadController
@@ -127,36 +128,45 @@
 }
 
 - (IBAction)cancelDownload:(id)sender {
-       int row = [tableView clickedRow];
+    SKDownload *download = [sender 
respondsToSelector:@selector(representedObject)] ? [sender representedObject] : 
nil;
     
-    if (row != -1) {
-        SKDownload *download = [downloads objectAtIndex:row];
-        if ([download status] == SKDownloadStatusDownloading) {
-            [download cancelDownload];
-        }
+    if (download == nil) {
+        int row = [tableView clickedRow];
+        if (row != -1)
+            download = [downloads objectAtIndex:row];
     }
+    if (download && [download status] == SKDownloadStatusDownloading) {
+        [download cancelDownload];
+    }
 }
 
 - (IBAction)resumeDownload:(id)sender {
-       int row = [tableView clickedRow];
+    SKDownload *download = [sender 
respondsToSelector:@selector(representedObject)] ? [sender representedObject] : 
nil;
     
-    if (row != -1) {
-        SKDownload *download = [downloads objectAtIndex:row];
-        if ([download status] == SKDownloadStatusCanceled) {
-            [download resumeDownload];
-            [self reloadTableView];
-            [self updateButtons];
-        }
+    if (download == nil) {
+        int row = [tableView clickedRow];
+        if (row != -1)
+            download = [downloads objectAtIndex:row];
     }
+    if (download && [download status] == SKDownloadStatusCanceled) {
+        [download resumeDownload];
+        [self reloadTableView];
+        [self updateButtons];
+    }
 }
 
 - (IBAction)removeDownload:(id)sender {
-       int row = [tableView clickedRow];
+    SKDownload *download = [sender 
respondsToSelector:@selector(representedObject)] ? [sender representedObject] : 
nil;
     
-    if (row != -1) {
-        SKDownload *download = [downloads objectAtIndex:row];
-        [download cleanupDownload];
-        [downloads removeObjectAtIndex:row];
+    if (download == nil) {
+        int row = [tableView clickedRow];
+        if (row != -1)
+            download = [downloads objectAtIndex:row];
+    }
+    
+    if (download) {
+        [download cancelDownload];
+        [downloads removeObject:download];
         [self reloadTableView];
         [self updateButtons];
     }
@@ -181,6 +191,47 @@
     [preferencesSheet orderOut:self];
 }
 
+- (void)openDownloadedFile:(id)sender {
+    SKDownload *download = [sender representedObject];
+    
+    if (download && [download status] != SKDownloadStatusFinished) {
+        NSBeep();
+        return;
+    }
+    
+    NSURL *fileURL = [NSURL fileURLWithPath:[download filePath]];
+    NSError *error;
+    if (nil == [[NSDocumentController sharedDocumentController] 
openDocumentWithContentsOfURL:fileURL display:YES error:&error])
+        [NSApp presentError:error];
+}
+
+- (void)revealDownloadedFile:(id)sender {
+    SKDownload *download = [sender representedObject];
+    
+    if (download && [download status] != SKDownloadStatusFinished) {
+        NSBeep();
+        return;
+    }
+    
+    [[NSWorkspace sharedWorkspace] selectFile:[download filePath] 
inFileViewerRootedAtPath:nil];
+}
+
+- (void)trashDownloadedFile:(id)sender {
+    SKDownload *download = [sender representedObject];
+    
+    if (download && [download status] != SKDownloadStatusFinished) {
+        NSBeep();
+        return;
+    }
+    
+    NSString *filePath = [download filePath];
+    NSString *folderPath = [filePath stringByDeletingLastPathComponent];
+    NSString *fileName = [filePath lastPathComponent];
+    int tag = 0;
+    
+    [[NSWorkspace sharedWorkspace] 
performFileOperation:NSWorkspaceRecycleOperation source:folderPath 
destination:nil files:[NSArray arrayWithObjects:fileName, nil] tag:&tag];
+}
+
 #pragma mark SKDownloadDelegate
 
 - (void)downloadDidStart:(SKDownload *)download {
@@ -202,7 +253,7 @@
             [NSApp presentError:error];
         
         if ([[NSUserDefaults standardUserDefaults] 
boolForKey:SKAutoRemoveFinishedDownloadsKey]) {
-            [download cleanupDownload];
+            [download cancelDownload];
             [downloads removeObject:download];
             // for the document to note that the file has been deleted
             [document setFileURL:[NSURL fileURLWithPath:[download filePath]]];
@@ -313,7 +364,7 @@
     if ([download canCancel]) {
         [download cancelDownload];
     } else {
-        [download cleanupDownload];
+        [download cancelDownload];
         [downloads removeObjectAtIndex:row];
         [self reloadTableView];
         [self updateButtons];
@@ -324,6 +375,44 @@
     return YES;
 }
 
+- (NSMenu *)tableView:(NSTableView *)aTableView 
menuForTableColumn:(NSTableColumn *)tableColumn row:(int)row {
+    NSMenu *menu = [[[NSMenu allocWithZone:[NSMenu menuZone]] init] 
autorelease];
+    NSMenuItem *menuItem;
+    SKDownload *download = [downloads objectAtIndex:row];
+    
+    [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] 
byExtendingSelection:NO];
+    
+    if ([download canCancel]) {
+        menuItem = [menu addItemWithTitle:NSLocalizedString(@"Cancel", @"Menu 
item title") action:@selector(cancelDownload:) keyEquivalent:@""];
+        [menuItem setTarget:self];
+        [menuItem setRepresentedObject:download];
+    } else {
+        menuItem = [menu addItemWithTitle:NSLocalizedString(@"Remove", @"Menu 
item title") action:@selector(removeDownload:) keyEquivalent:@""];
+        [menuItem setTarget:self];
+        [menuItem setRepresentedObject:download];
+    }
+    if ([download canResume]) {
+        menuItem = [menu addItemWithTitle:NSLocalizedString(@"Resume", @"Menu 
item title") action:@selector(resumeDownload:) keyEquivalent:@""];
+        [menuItem setTarget:self];
+        [menuItem setRepresentedObject:download];
+    }
+    if ([download status] == SKDownloadStatusFinished) {
+        menuItem = [menu addItemWithTitle:[NSLocalizedString(@"Open", @"Menu 
item title") stringByAppendingEllipsis] action:@selector(openDownloadedFile:) 
keyEquivalent:@""];
+        [menuItem setTarget:self];
+        [menuItem setRepresentedObject:download];
+        
+        menuItem = [menu addItemWithTitle:[NSLocalizedString(@"Reveal", @"Menu 
item title") stringByAppendingEllipsis] action:@selector(revealDownloadedFile:) 
keyEquivalent:@""];
+        [menuItem setTarget:self];
+        [menuItem setRepresentedObject:download];
+        
+        menuItem = [menu addItemWithTitle:NSLocalizedString(@"Move to Trash", 
@"Menu item title") action:@selector(trashDownloadedFile:) keyEquivalent:@""];
+        [menuItem setTarget:self];
+        [menuItem setRepresentedObject:download];
+    }
+    
+    return menu;
+}
+
 #pragma mark SKTypeSelectHelper datasource protocol
 
 - (NSArray *)typeSelectHelperSelectionItems:(SKTypeSelectHelper 
*)typeSelectHelper {


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

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to