Revision: 4569
          http://skim-app.svn.sourceforge.net/skim-app/?rev=4569&view=rev
Author:   hofman
Date:     2008-09-06 18:36:52 +0000 (Sat, 06 Sep 2008)

Log Message:
-----------
Update type select helper and download buttons in download controller using KVO

Modified Paths:
--------------
    trunk/SKDownloadController.m

Modified: trunk/SKDownloadController.m
===================================================================
--- trunk/SKDownloadController.m        2008-09-05 22:52:36 UTC (rev 4568)
+++ trunk/SKDownloadController.m        2008-09-06 18:36:52 UTC (rev 4569)
@@ -52,11 +52,18 @@
 
 static NSString *SKDownloadsWindowFrameAutosaveName = @"SKDownloadsWindow";
 
+static NSString *SKDownloadPropertiesObservationContext = 
@"SKDownloadPropertiesObservationContext";
+
 static NSString *SKDownloadControllerDownloadsKey = @"downloads";
 
 static NSString *SKDownloadsWindowCancelColumnIdentifier = @"cancel";
 static NSString *SKDownloadsWindowResumeColumnIdentifier = @"resume";
 
[EMAIL PROTECTED] SKDownloadController (SKPrivate)
+- (void)startObservingDownloads:(NSArray *)newDownloads;
+- (void)endObservingDownloads:(NSArray *)oldDownloads;
[EMAIL PROTECTED]
+
 @implementation SKDownloadController
 
 static SKDownloadController *sharedDownloadController = nil;
@@ -77,6 +84,7 @@
 }
 
 - (void)dealloc {
+    [self endObservingDownloads:downloads];
     [downloads release];
     [super dealloc];
 }
@@ -126,12 +134,14 @@
 
 - (void)insertObject:(SKDownload *)download inDownloadsAtIndex:(unsigned 
int)anIndex {
     [downloads insertObject:download atIndex:anIndex];
+    [self startObservingDownloads:[NSArray arrayWithObject:download]];
     [downloads 
makeObjectsPerformSelector:@selector(removeProgressIndicatorFromSuperview)];
     [tableView setNeedsDisplayInRect:[tableView rectOfRow:PROGRESS_COLUMN]];
 }
 
 - (void)removeObjectFromDownloadsAtIndex:(unsigned int)anIndex {
     SKDownload *download = [downloads objectAtIndex:anIndex];
+    [self endObservingDownloads:[NSArray arrayWithObject:download]];
     [download setDelegate:nil];
     [download cancel];
     [downloads removeObjectAtIndex:anIndex];
@@ -247,19 +257,6 @@
 
 #pragma mark SKDownloadDelegate
 
-- (void)downloadDidUpdate:(SKDownload *)download {
-}
-
-- (void)downloadDidStart:(SKDownload *)download {
-    unsigned int row = [downloads indexOfObject:download];
-    if (row != NSNotFound)
-        [tableView setNeedsDisplayInRect:NSUnionRect([tableView 
frameOfCellAtColumn:RESUME_COLUMN row:row], [tableView 
frameOfCellAtColumn:CANCEL_COLUMN row:row])];
-}
-
-- (void)downloadDidBeginDownloading:(SKDownload *)download {
-    [self downloadDidStart:download];
-}
-
 - (void)downloadDidEnd:(SKDownload *)download {
     if ([download status] == SKDownloadStatusFinished) {
         NSURL *URL = [NSURL fileURLWithPath:[download filePath]];
@@ -275,11 +272,7 @@
             [document setFileURL:[NSURL fileURLWithPath:[download filePath]]];
             if ([self countOfDownloads] == 0 && [[NSUserDefaults 
standardUserDefaults] boolForKey:SKAutoCloseDownloadsWindowKey])
                 [[self window] close];
-        } else {
-            [self downloadDidStart:download];
         }
-    } else {
-        [self downloadDidStart:download];
     }
 }
 
@@ -422,4 +415,32 @@
     [tableView scrollRowToVisible:itemIndex];
 }
 
+#pragma mark KVO
+
+- (void)startObservingDownloads:(NSArray *)newDownloads {
+    NSIndexSet *indexes = [NSIndexSet 
indexSetWithIndexesInRange:NSMakeRange(0, [newDownloads count])];
+    [newDownloads addObserver:self toObjectsAtIndexes:indexes 
forKeyPath:SKDownloadFileNameKey options:0 
context:SKDownloadPropertiesObservationContext];
+    [newDownloads addObserver:self toObjectsAtIndexes:indexes 
forKeyPath:SKDownloadStatusKey options:0 
context:SKDownloadPropertiesObservationContext];
+}
+
+- (void)endObservingDownloads:(NSArray *)oldDownloads {
+    NSIndexSet *indexes = [NSIndexSet 
indexSetWithIndexesInRange:NSMakeRange(0, [oldDownloads count])];
+    [oldDownloads removeObserver:self fromObjectsAtIndexes:indexes 
forKeyPath:SKDownloadFileNameKey];
+    [oldDownloads removeObserver:self fromObjectsAtIndexes:indexes 
forKeyPath:SKDownloadStatusKey];
+}
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
change:(NSDictionary *)change context:(void *)context {
+    if (context == SKDownloadPropertiesObservationContext) {
+        if ([keyPath isEqualToString:SKDownloadFileNameKey]) {
+            [[tableView typeSelectHelper] rebuildTypeSelectSearchCache];
+        } else if ([keyPath isEqualToString:SKDownloadStatusKey]) {
+            unsigned int row = [downloads indexOfObject:object];
+            if (row != NSNotFound)
+                [tableView setNeedsDisplayInRect:NSUnionRect([tableView 
frameOfCellAtColumn:RESUME_COLUMN row:row], [tableView 
frameOfCellAtColumn:CANCEL_COLUMN row:row])];
+        }
+    } else {
+        [super observeValueForKeyPath:keyPath ofObject:object change:change 
context:context];
+    }
+}
+
 @end


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 the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to