From c3319820162d9983c4e3307a4eeb168546b6e1fc Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Mon, 8 Dec 2014 16:03:49 +0000 Subject: [PATCH] * Source/NSDocumentFrameworkPrivate.h, * Source/NSDocumentController.m: Open recent menu to clear items that have been deleted whenever it reloads. Based on patch by Paul Landers . git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@38242 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 7 ++ Source/NSDocumentController.m | 130 ++++++++++++++++++---------- Source/NSDocumentFrameworkPrivate.h | 2 + 3 files changed, 92 insertions(+), 47 deletions(-) diff --git a/ChangeLog b/ChangeLog index bb9ea16e4..e64e1880a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2014-12-08 Fred Kiefer + + * Source/NSDocumentFrameworkPrivate.h, + * Source/NSDocumentController.m: + Open recent menu to clear items that have been deleted whenever it reloads. + Based on patch by Paul Landers . + 2014-12-08 Fred Kiefer * Source/GSToolTips.m: Fixed two issues with tooltips. diff --git a/Source/NSDocumentController.m b/Source/NSDocumentController.m index 6ad88824c..52c21018e 100644 --- a/Source/NSDocumentController.m +++ b/Source/NSDocumentController.m @@ -277,42 +277,8 @@ TypeInfoForHumanReadableName (NSArray *types, NSString *typeName) _documents = [[NSMutableArray alloc] init]; /* Get list of recent documents */ - _recent_documents = [[NSUserDefaults standardUserDefaults] - objectForKey: NSRecentDocuments]; - if (_recent_documents) - { - int i, count, max; + [self _loadRecentDocuments]; - _recent_documents = [_recent_documents mutableCopy]; - count = [_recent_documents count]; - for (i = 0; i < count; i++) - { - NSString *str; - NSURL *url; - - str = [_recent_documents objectAtIndex: i]; - url = [NSURL URLWithString: str]; - if (url == nil) - { - NSLog(@"NSRecentItems value '%@' is not valid ... ignored", str); - [_recent_documents removeObjectAtIndex: i]; - i--; - count--; - } - else - { - [_recent_documents replaceObjectAtIndex: i withObject: url]; - } - } - - max = [self maximumRecentDocumentCount]; - if (count > max) - { - [_recent_documents removeObjectsInRange: NSMakeRange(0, count - max)]; - } - } - else - _recent_documents = RETAIN([NSMutableArray array]); [self setShouldCreateUI: YES]; [[[NSWorkspace sharedWorkspace] notificationCenter] @@ -1347,7 +1313,6 @@ static BOOL _shouldClose = YES; - (void) noteNewRecentDocumentURL: (NSURL *)anURL { NSUInteger index = [_recent_documents indexOfObject: anURL]; - NSMutableArray *a; if (index != NSNotFound) { @@ -1362,16 +1327,8 @@ static BOOL _shouldClose = YES; [_recent_documents addObject: anURL]; // Save the changed list - a = [_recent_documents mutableCopy]; - index = [a count]; - while (index-- > 0) - { - [a replaceObjectAtIndex: index withObject: - [[a objectAtIndex: index] absoluteString]]; - } - [[NSUserDefaults standardUserDefaults] - setObject: a forKey: NSRecentDocuments]; - RELEASE(a); + [self _saveRecentDocuments]; + [self _updateRecentDocumentsMenu]; } @@ -1658,6 +1615,7 @@ static NSString *processName = nil; - (void) _updateRecentDocumentsMenu { NSMenu *recentMenu; + BOOL listUpdated; int i; recentMenu = [self _recentDocumentsMenu]; @@ -1670,10 +1628,31 @@ static NSString *processName = nil; [recentMenu setAutoenablesItems: NO]; [recentMenu setMenuChangedMessagesEnabled: NO]; + // remove from list all deleted or otherwise inaccessable files + listUpdated = NO; + for (i = [_recent_documents count] - 1; i >= 0; i--) + { + NSURL *u = [_recent_documents objectAtIndex: i]; + NSError *error; + // Check if resource has been deleted + if (![u checkResourceIsReachableAndReturnError: &error]) + { + [_recent_documents removeObjectAtIndex: i]; + listUpdated = YES; + } + } + if (listUpdated) + { + // Save the changed list + [self _saveRecentDocuments]; + } + + // remove them all while ([recentMenu numberOfItems] > 0) { - [recentMenu removeItemAtIndex: 0]; // remove them all + [recentMenu removeItemAtIndex: 0]; } + for (i = [_recent_documents count] - 1; i >= -2; i--) { // add all items incl. a Clear List item if needed @@ -1751,4 +1730,61 @@ static NSString *processName = nil; [self presentError: err]; } +- (void) _saveRecentDocuments +{ + NSMutableArray *a = [_recent_documents mutableCopy]; + NSUInteger index = [a count]; + + while (index-- > 0) + { + [a replaceObjectAtIndex: index withObject: + [[a objectAtIndex: index] absoluteString]]; + } + [[NSUserDefaults standardUserDefaults] + setObject: a forKey: NSRecentDocuments]; + RELEASE(a); +} + +- (void) _loadRecentDocuments +{ + _recent_documents = [[NSUserDefaults standardUserDefaults] + objectForKey: NSRecentDocuments]; + if (_recent_documents) + { + int i, count, max; + + _recent_documents = [_recent_documents mutableCopy]; + count = [_recent_documents count]; + for (i = 0; i < count; i++) + { + NSString *str; + NSURL *url; + + str = [_recent_documents objectAtIndex: i]; + url = [NSURL URLWithString: str]; + if (url == nil) + { + NSLog(@"NSRecentItems value '%@' is not valid ... ignored", str); + [_recent_documents removeObjectAtIndex: i]; + i--; + count--; + } + else + { + [_recent_documents replaceObjectAtIndex: i withObject: url]; + } + } + + max = [self maximumRecentDocumentCount]; + if (count > max) + { + [_recent_documents removeObjectsInRange: NSMakeRange(0, count - max)]; + } + } + else + { + _recent_documents = RETAIN([NSMutableArray array]); + } +} + @end diff --git a/Source/NSDocumentFrameworkPrivate.h b/Source/NSDocumentFrameworkPrivate.h index 881f7f5d6..2fa2d43c4 100644 --- a/Source/NSDocumentFrameworkPrivate.h +++ b/Source/NSDocumentFrameworkPrivate.h @@ -50,6 +50,8 @@ - (void) _setRecentDocumentsMenu: (NSMenu *)menu; - (void) _updateRecentDocumentsMenu; - (IBAction) _openRecentDocument: (id)sender; +- (void) _saveRecentDocuments; +- (void) _loadRecentDocuments; @end @interface NSDocument (Private)