From 258db46f261ba5b910f5441d3469f5fb64d28800 Mon Sep 17 00:00:00 2001 From: Miltiadis Vasilakis Date: Thu, 14 Mar 2013 17:06:02 +0200 Subject: [PATCH] Improve code and fix bugs --- pithos-macos/ImageAndTextCell.m | 7 ++----- pithos-macos/PithosAccountNode.h | 1 - pithos-macos/PithosAccountNode.m | 9 ++++----- pithos-macos/PithosActivity.h | 4 ++-- pithos-macos/PithosActivity.m | 2 +- pithos-macos/PithosBrowserController.h | 32 ++++++++++-------------------- pithos-macos/PithosBrowserController.m | 20 ++++++++++++++++--- pithos-macos/PithosContainerNode.h | 1 - pithos-macos/PithosContainerNode.m | 9 ++++----- pithos-macos/PithosNode.m | 6 ++---- pithos-macos/PithosObjectNode.m | 2 +- pithos-macos/PithosSharingAccountsNode.h | 1 - pithos-macos/PithosSharingAccountsNode.m | 17 +++++++++------- pithos-macos/PithosSubdirNode.m | 2 +- pithos-macos/PithosUtilities.m | 4 ++-- pithos-macos/pithos_macosAppDelegate.m | 2 +- 16 files changed, 57 insertions(+), 62 deletions(-) diff --git a/pithos-macos/ImageAndTextCell.m b/pithos-macos/ImageAndTextCell.m index 52baa00..0bb3163 100644 --- a/pithos-macos/ImageAndTextCell.m +++ b/pithos-macos/ImageAndTextCell.m @@ -183,11 +183,8 @@ imageFrame.origin.y -= kImageOriginYOffset; imageFrame.size = imageSize; - if ([controlView isFlipped]) - imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2); - else - imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2); - [image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver]; + imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2); + [image drawInRect:imageFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil]; NSRect newFrame = cellFrame; newFrame.origin.x += kTextOriginXOffset; diff --git a/pithos-macos/PithosAccountNode.h b/pithos-macos/PithosAccountNode.h index ecda291..6be678c 100644 --- a/pithos-macos/PithosAccountNode.h +++ b/pithos-macos/PithosAccountNode.h @@ -44,7 +44,6 @@ ASIPithos *pithos; ASIPithosAccount *pithosAccount; - NSMutableArray *containers; ASIPithosAccountRequest *accountRequest; ASIPithosAccountRequest *applyMetadataAccountRequest; diff --git a/pithos-macos/PithosAccountNode.m b/pithos-macos/PithosAccountNode.m index f5768d4..42a210b 100644 --- a/pithos-macos/PithosAccountNode.m +++ b/pithos-macos/PithosAccountNode.m @@ -235,7 +235,6 @@ static NSImage *sharedIcon = nil; } else { newChildren = nil; accountRequest = nil; - containers = nil; forcedRefresh = NO; @synchronized(self) { freshness = PithosNodeStateRefreshNeeded; @@ -251,9 +250,10 @@ static NSImage *sharedIcon = nil; if (accountRequest.responseStatusCode == 200) { self.pithosAccount = [accountRequest account]; + NSMutableArray *containers = [accountRequest.userInfo objectForKey:@"containers"]; NSArray *someContainers = [accountRequest containers]; if (containers == nil) { - containers = [[NSMutableArray alloc] initWithArray:someContainers]; + containers = [NSMutableArray arrayWithArray:someContainers]; } else { [containers addObjectsFromArray:someContainers]; } @@ -291,7 +291,6 @@ static NSImage *sharedIcon = nil; } // Else cache was used and all results were fetched during this request, so existing children can be reused accountRequest = nil; - containers = nil; forcedRefresh = NO; @synchronized(self) { freshness = PithosNodeStateRefreshFinished; @@ -315,14 +314,14 @@ static NSImage *sharedIcon = nil; [NSNumber numberWithInteger:NSOperationQueuePriorityVeryHigh], @"priority", [NSNumber numberWithUnsignedInteger:10], @"retries", NSStringFromSelector(@selector(accountRequestFinished:)), @"didFinishSelector", - NSStringFromSelector(@selector(accountRequestFailed:)), @"didFailSelector", + NSStringFromSelector(@selector(accountRequestFailed:)), @"didFailSelector", + containers, @"containers", nil]; [[PithosUtilities prepareRequest:accountRequest priority:NSOperationQueuePriorityVeryHigh] startAsynchronous]; } } else if (accountRequest.responseStatusCode == 304) { // Account is not modified, so existing children can be reused accountRequest = nil; - containers = nil; forcedRefresh = NO; @synchronized(self) { freshness = PithosNodeStateRefreshFinished; diff --git a/pithos-macos/PithosActivity.h b/pithos-macos/PithosActivity.h index 0e4614e..bee9998 100644 --- a/pithos-macos/PithosActivity.h +++ b/pithos-macos/PithosActivity.h @@ -53,7 +53,7 @@ typedef enum { NSString *message; NSUInteger totalBytes; NSUInteger currentBytes; - PithosAccount *__unsafe_unretained pithosAccount; + PithosAccount *pithosAccount; } - (id)initWithType:(PithosActivityType)aType pithosAccount:(PithosAccount *)aPithosAccount; @@ -62,6 +62,6 @@ typedef enum { @property (nonatomic, copy) NSString *message; @property (nonatomic, assign) NSUInteger totalBytes; @property (nonatomic, assign) NSUInteger currentBytes; -@property (nonatomic, unsafe_unretained) PithosAccount *pithosAccount; +@property (nonatomic, strong) PithosAccount *pithosAccount; @end diff --git a/pithos-macos/PithosActivity.m b/pithos-macos/PithosActivity.m index 7ac67b8..b47404d 100644 --- a/pithos-macos/PithosActivity.m +++ b/pithos-macos/PithosActivity.m @@ -49,7 +49,7 @@ type = aType; totalBytes = 0; currentBytes = 0; - pithosAccount = aPithosAccount; + self.pithosAccount = aPithosAccount; } return self; } diff --git a/pithos-macos/PithosBrowserController.h b/pithos-macos/PithosBrowserController.h index 4473c55..2454f05 100644 --- a/pithos-macos/PithosBrowserController.h +++ b/pithos-macos/PithosBrowserController.h @@ -63,14 +63,14 @@ NSViewController *sharedPreviewController; - NSSplitView *__unsafe_unretained verticalSplitView; - NSSplitView *__unsafe_unretained horizontalSplitView; - NSView *__unsafe_unretained leftTopView; - NSView *__unsafe_unretained leftBottomView; - NSOutlineView *__unsafe_unretained outlineView; - NSBrowser *__unsafe_unretained browser; - NSMenu *__unsafe_unretained outlineViewMenu; - NSMenu *__unsafe_unretained browserMenu; + IBOutlet NSSplitView *verticalSplitView; + IBOutlet NSSplitView *horizontalSplitView; + IBOutlet NSView *leftTopView; + IBOutlet NSView *leftBottomView; + IBOutlet NSOutlineView *outlineView; + IBOutlet NSBrowser *browser; + IBOutlet NSMenu *outlineViewMenu; + IBOutlet NSMenu *browserMenu; BOOL editingItem; @@ -81,8 +81,8 @@ PithosNode *clipboardParentNode; BOOL clipboardCopy; - NSTextField *__unsafe_unretained activityTextField; - NSProgressIndicator *__unsafe_unretained activityProgressIndicator; + IBOutlet NSTextField *activityTextField; + IBOutlet NSProgressIndicator *activityProgressIndicator; PithosActivityFacility *activityFacility; NSTimer *refreshTimer; @@ -109,15 +109,6 @@ @property (nonatomic, unsafe_unretained) PithosAccount *pithosAccountManager; @property (nonatomic, strong) PithosAccountNode *accountNode; -@property (nonatomic, unsafe_unretained) IBOutlet NSSplitView *verticalSplitView; -@property (nonatomic, unsafe_unretained) IBOutlet NSSplitView *horizontalSplitView; -@property (nonatomic, unsafe_unretained) IBOutlet NSView *leftTopView; -@property (nonatomic, unsafe_unretained) IBOutlet NSView *leftBottomView; -@property (nonatomic, unsafe_unretained) IBOutlet NSOutlineView *outlineView; -@property (nonatomic, unsafe_unretained) IBOutlet NSBrowser *browser; -@property (nonatomic, unsafe_unretained) IBOutlet NSMenu *outlineViewMenu; -@property (nonatomic, unsafe_unretained) IBOutlet NSMenu *browserMenu; - @property (nonatomic, strong) NSArray *draggedNodes; @property (nonatomic, strong) PithosNode *draggedParentNode; @@ -125,9 +116,6 @@ @property (nonatomic, strong) PithosNode *clipboardParentNode; @property (nonatomic, assign) BOOL clipboardCopy; -@property (nonatomic, unsafe_unretained) IBOutlet NSTextField *activityTextField; -@property (nonatomic, unsafe_unretained) IBOutlet NSProgressIndicator *activityProgressIndicator; - - (IBAction)forceRefresh:(id)sender; - (IBAction)refresh:(id)sender; - (void)resetBrowser; diff --git a/pithos-macos/PithosBrowserController.m b/pithos-macos/PithosBrowserController.m index 205bfb8..17420db 100644 --- a/pithos-macos/PithosBrowserController.m +++ b/pithos-macos/PithosBrowserController.m @@ -110,10 +110,8 @@ @implementation PithosBrowserController @synthesize pithos; @synthesize pithosAccountManager, accountNode; -@synthesize verticalSplitView, horizontalSplitView, leftTopView, leftBottomView, outlineView, browser, outlineViewMenu, browserMenu; @synthesize draggedNodes, draggedParentNode; @synthesize clipboardNodes, clipboardParentNode, clipboardCopy; -@synthesize activityTextField, activityProgressIndicator; #pragma mark - #pragma Object Lifecycle @@ -242,7 +240,7 @@ // Request for browser refresh [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pithosBrowserRefreshNeeded:) - name:@"PithosBrowserRefreshNeeeded" + name:@"PithosBrowserRefreshNeeded" object:nil]; } @@ -404,6 +402,10 @@ #pragma mark Observers - (void)pithosNodeChildrenUpdated:(NSNotification *)notification { + if (![NSThread isMainThread]) { + [self performSelectorOnMainThread:@selector(pithosNodeChildrenUpdated:) withObject:notification waitUntilDone:NO]; + return; + } PithosNode *node = (PithosNode *)[notification object]; if ((node == accountNode) || ![node.pithos isEqualTo:pithos]) return; @@ -418,6 +420,10 @@ } - (void)pithosAccountNodeChildrenUpdated:(NSNotification *)notification { + if (![NSThread isMainThread]) { + [self performSelectorOnMainThread:@selector(pithosAccountNodeChildrenUpdated:) withObject:notification waitUntilDone:NO]; + return; + } BOOL containerPithosFound = NO; BOOL containerTrashFound = NO; NSMutableIndexSet *removedContainersNodeChildren = [NSMutableIndexSet indexSet]; @@ -491,6 +497,10 @@ #pragma mark Actions - (IBAction)forceRefresh:(id)sender { + if (![NSThread isMainThread]) { + [self performSelectorOnMainThread:@selector(forceRefresh:) withObject:sender waitUntilDone:NO]; + return; + } if (editingItem) return; if (sender) @@ -504,6 +514,10 @@ } - (IBAction)refresh:(id)sender { + if (![NSThread isMainThread]) { + [self performSelectorOnMainThread:@selector(refresh:) withObject:sender waitUntilDone:NO]; + return; + } if (editingItem) return; if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) { diff --git a/pithos-macos/PithosContainerNode.h b/pithos-macos/PithosContainerNode.h index a92c12e..5481d2b 100644 --- a/pithos-macos/PithosContainerNode.h +++ b/pithos-macos/PithosContainerNode.h @@ -44,7 +44,6 @@ ASIPithos *pithos; ASIPithosContainer *pithosContainer; - NSMutableArray *objects; ASIPithosContainerRequest *containerRequest; NSString *prefix; diff --git a/pithos-macos/PithosContainerNode.m b/pithos-macos/PithosContainerNode.m index 8336b31..8ec0954 100644 --- a/pithos-macos/PithosContainerNode.m +++ b/pithos-macos/PithosContainerNode.m @@ -227,7 +227,6 @@ static NSImage *sharedIcon = nil; } else { newChildren = nil; containerRequest = nil; - objects = nil; forcedRefresh = NO; @synchronized(self) { freshness = PithosNodeStateRefreshNeeded; @@ -246,9 +245,10 @@ static NSImage *sharedIcon = nil; pithosContainer.blockSize = [containerRequest blockSize]; } + NSMutableArray *objects = [containerRequest.userInfo objectForKey:@"objects"]; NSArray *someObjects = [containerRequest objects]; if (objects == nil) { - objects = [[NSMutableArray alloc] initWithArray:someObjects]; + objects = [NSMutableArray arrayWithArray:someObjects]; } else { [objects addObjectsFromArray:someObjects]; } @@ -346,7 +346,6 @@ static NSImage *sharedIcon = nil; } // Else cache was used and all results were fetched during this request, so existing children can be reused containerRequest = nil; - objects = nil; forcedRefresh = NO; @synchronized(self) { freshness = PithosNodeStateRefreshFinished; @@ -375,14 +374,14 @@ static NSImage *sharedIcon = nil; [NSNumber numberWithInteger:NSOperationQueuePriorityVeryHigh], @"priority", [NSNumber numberWithUnsignedInteger:10], @"retries", NSStringFromSelector(@selector(containerRequestFinished:)), @"didFinishSelector", - NSStringFromSelector(@selector(containerRequestFailed:)), @"didFailSelector", + NSStringFromSelector(@selector(containerRequestFailed:)), @"didFailSelector", + objects, @"objects", nil]; [[PithosUtilities prepareRequest:containerRequest priority:NSOperationQueuePriorityVeryHigh] startAsynchronous]; } } else if (containerRequest.responseStatusCode == 304) { // Container is not modified, so existing children can be reused containerRequest = nil; - objects = nil; forcedRefresh = NO; @synchronized(self) { freshness = PithosNodeStateRefreshFinished; diff --git a/pithos-macos/PithosNode.m b/pithos-macos/PithosNode.m index 99567cf..b7dbd99 100644 --- a/pithos-macos/PithosNode.m +++ b/pithos-macos/PithosNode.m @@ -105,7 +105,7 @@ - (void)refresh { [self invalidateChildren]; - self.children; + [self children]; } - (void)forceRefresh { @@ -143,9 +143,7 @@ pithosNodeInfoController.node = nil; [[pithosNodeInfoController window] close]; } - for (PithosNode *child in children) { - [child pithosNodeWillBeRemoved]; - } + [children makeObjectsPerformSelector:@selector(pithosNodeWillBeRemoved)]; } #pragma mark - diff --git a/pithos-macos/PithosObjectNode.m b/pithos-macos/PithosObjectNode.m index a0d0d97..fc797f1 100644 --- a/pithos-macos/PithosObjectNode.m +++ b/pithos-macos/PithosObjectNode.m @@ -168,7 +168,7 @@ self.isPublic = (pithosObject.publicURI != nil); // Refresh browser if the object is in my shared and is no longer shared if (shared && !pithosObject.sharing) - [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosBrowserRefreshNeeeded" object:self]; + [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosBrowserRefreshNeeded" object:self]; } - (void)setLimitedPithosObject:(ASIPithosObject *)aPithosObject { diff --git a/pithos-macos/PithosSharingAccountsNode.h b/pithos-macos/PithosSharingAccountsNode.h index 32add2c..c3d8691 100644 --- a/pithos-macos/PithosSharingAccountsNode.h +++ b/pithos-macos/PithosSharingAccountsNode.h @@ -42,7 +42,6 @@ @interface PithosSharingAccountsNode : PithosNode { ASIPithos *pithos; - NSMutableArray *sharingAccounts; ASIPithosRequest *sharingAccountsRequest; BOOL reset; diff --git a/pithos-macos/PithosSharingAccountsNode.m b/pithos-macos/PithosSharingAccountsNode.m index b03192d..9059d3f 100644 --- a/pithos-macos/PithosSharingAccountsNode.m +++ b/pithos-macos/PithosSharingAccountsNode.m @@ -159,7 +159,6 @@ } else { newChildren = nil; sharingAccountsRequest = nil; - sharingAccounts = nil; forcedRefresh = NO; @synchronized(self) { freshness = PithosNodeStateRefreshNeeded; @@ -173,9 +172,10 @@ DLog(@"List sharing accounts finished: %@", [sharingAccountsRequest url]); DLog(@"Cached: %d", [sharingAccountsRequest didUseCachedResponse]); if (sharingAccountsRequest.responseStatusCode == 200) { + NSMutableArray *sharingAccounts = [sharingAccountsRequest.userInfo objectForKey:@"sharingAccounts"]; NSArray *someSharingAccounts = [sharingAccountsRequest sharingAccounts]; if (sharingAccounts == nil) { - sharingAccounts = [[NSMutableArray alloc] initWithArray:someSharingAccounts]; + sharingAccounts = [NSMutableArray arrayWithArray:someSharingAccounts]; } else { [sharingAccounts addObjectsFromArray:someSharingAccounts]; } @@ -185,9 +185,7 @@ DLog(@"using newChildren"); newChildren = [[NSMutableArray alloc] init]; NSMutableIndexSet *keptNodes = [NSMutableIndexSet indexSet]; - NSMutableArray *sharingAccountsNames = [NSMutableArray arrayWithCapacity:sharingAccounts.count]; for (ASIPithosAccount *account in sharingAccounts) { - [sharingAccountsNames addObject:account.name]; if (![account.name isEqualToString:pithos.authUser]) { PithosAccountNode *node = [[PithosAccountNode alloc] initWithPithos:pithos]; node.parent = self; @@ -206,7 +204,6 @@ [newChildren addObject:node]; } } - [pithosAccountManager updateUserCatalogForForDisplaynames:nil UUIDs:sharingAccountsNames]; [[children objectsAtIndexes: [[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [children count])] indexesPassingTest:^(NSUInteger idx, BOOL *stop){ if ([keptNodes containsIndex:idx]) @@ -215,8 +212,13 @@ }]] makeObjectsPerformSelector:@selector(pithosNodeWillBeRemoved)]; } // Else cache was used and all results were fetched during this request, so existing children can be reused + // Update user catalog even if cache was used + NSMutableArray *sharingAccountsNames = [NSMutableArray arrayWithCapacity:sharingAccounts.count]; + for (ASIPithosAccount *account in sharingAccounts) { + [sharingAccountsNames addObject:account.name]; + } + [pithosAccountManager updateUserCatalogForForDisplaynames:nil UUIDs:sharingAccountsNames]; sharingAccountsRequest = nil; - sharingAccounts = nil; forcedRefresh = NO; @synchronized(self) { freshness = PithosNodeStateRefreshFinished; @@ -234,7 +236,8 @@ [NSNumber numberWithInteger:NSOperationQueuePriorityVeryHigh], @"priority", [NSNumber numberWithUnsignedInteger:10], @"retries", NSStringFromSelector(@selector(sharingAccountsRequestFinished:)), @"didFinishSelector", - NSStringFromSelector(@selector(sharingAccountsRequestFailed:)), @"didFailSelector", + NSStringFromSelector(@selector(sharingAccountsRequestFailed:)), @"didFailSelector", + sharingAccounts, @"sharingAccounts", nil]; // if (!forcedRefresh) // sharingAccountsRequest.downloadCache = [ASIDownloadCache sharedCache]; diff --git a/pithos-macos/PithosSubdirNode.m b/pithos-macos/PithosSubdirNode.m index 993d627..ba3e492 100644 --- a/pithos-macos/PithosSubdirNode.m +++ b/pithos-macos/PithosSubdirNode.m @@ -177,7 +177,7 @@ static NSImage *sharedIcon = nil; self.isPublic = (pithosObject.publicURI != nil); // Refresh browser if the object is in my shared and is no longer shared if (shared && !pithosObject.subdir && !pithosObject.sharing) { - [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosBrowserRefreshNeeeded" object:self]; + [[NSNotificationCenter defaultCenter] postNotificationName:@"PithosBrowserRefreshNeeded" object:self]; } } diff --git a/pithos-macos/PithosUtilities.m b/pithos-macos/PithosUtilities.m index d48928a..8b42206 100644 --- a/pithos-macos/PithosUtilities.m +++ b/pithos-macos/PithosUtilities.m @@ -1281,9 +1281,9 @@ #pragma mark Request Helper Methods + (ASIPithosRequest *)prepareRequest:(ASIPithosRequest *)request priority:(NSOperationQueuePriority)priority { - [request setTimeOutSeconds:60]; + request.timeOutSeconds = 60; request.numberOfTimesToRetryOnTimeout = 10; - [request setQueuePriority:priority]; + request.queuePriority = priority; return request; } diff --git a/pithos-macos/pithos_macosAppDelegate.m b/pithos-macos/pithos_macosAppDelegate.m index 3c78a73..01d4c87 100644 --- a/pithos-macos/pithos_macosAppDelegate.m +++ b/pithos-macos/pithos_macosAppDelegate.m @@ -134,7 +134,7 @@ currentPithosAccount, pithosAccounts, pithosAccountsDictionary, syncPithosAccoun [smallImage lockFocus]; [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh]; [sourceImage setSize:NSMakeSize(18, 18)]; - [sourceImage compositeToPoint:NSZeroPoint operation:NSCompositeCopy]; + [sourceImage drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; [smallImage unlockFocus]; statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength]; -- 1.7.10.4