From ef0b67bc70d6529dd8da40bfd82fcb105a7aa465 Mon Sep 17 00:00:00 2001 From: wlux Date: Sun, 29 Nov 2009 14:07:29 +0000 Subject: [PATCH] Present a warning when the user enters a filename with a wrong extension in NSSavePanel. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@29080 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 5 +++ Source/NSSavePanel.m | 80 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 72 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index 28a55d8e8..f6031b6ef 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2009-11-29 Wolfgang Lux + + * Source/NSSavePanel.m (-ok:): Present a warning when the user has + entered a filename with a wrong extension. + 2009-11-29 Richard Frith-Macdonald * Source/NSOutlineView.m: Improve display of insertion point for DnD diff --git a/Source/NSSavePanel.m b/Source/NSSavePanel.m index c58ff2d8a..33523e5e9 100644 --- a/Source/NSSavePanel.m +++ b/Source/NSSavePanel.m @@ -117,6 +117,7 @@ setPath(NSBrowser *browser, NSString *path) - (void) _unmountMedia; - (void) _selectTextInColumn: (int)column; - (void) _selectCellName: (NSString *)title; +- (void) _setFileName: (NSString *)name; - (void) _setupForDirectory: (NSString *)path file: (NSString *)name; - (BOOL) _shouldShowExtension: (NSString *)extension; - (void) _windowResized: (NSNotification*)n; @@ -559,6 +560,14 @@ selectCellWithString: (NSString*)title return YES; } +- (void) _setFileName: (NSString *)filename +{ + [self _selectCellName: filename]; + [[_form cellAtIndex: 0] setStringValue: filename]; + [_form selectTextAtIndex: 0]; + [_form setNeedsDisplay: YES]; +} + - (void) _setupForDirectory: (NSString *)path file: (NSString *)filename { if (path == nil) @@ -576,13 +585,7 @@ selectCellWithString: (NSString*)title filename = @""; ASSIGN(_fullFileName, [_directory stringByAppendingPathComponent: filename]); setPath(_browser, _fullFileName); - - [self _selectCellName: filename]; - [_form abortEditing]; // FIXME This is necessary because we cannot set a - // cell's value while it is being edited. - [[_form cellAtIndex: 0] setStringValue: filename]; - [_form selectTextAtIndex: 0]; - [_form setNeedsDisplay: YES]; + [self _setFileName: filename]; [self _browser: _browser selectCellWithString: [[_browser selectedCell] stringValue] @@ -1172,7 +1175,7 @@ selectCellWithString: (NSString*)title { NSMatrix *matrix; NSBrowserCell *selectedCell; - NSString *filename; + NSString *filename, *ext, *req; BOOL isDir = NO; matrix = [_browser matrixInColumn: [_browser lastColumn]]; @@ -1208,13 +1211,64 @@ selectCellWithString: (NSString*)title ASSIGN (_directory, [filename stringByDeletingLastPathComponent]); ASSIGN (_fullFileName, filename); setPath(_browser, _fullFileName); + [self _setFileName: [_fullFileName lastPathComponent]]; + } + } - filename = [_fullFileName lastPathComponent]; + /* Warn user if a wrong extension was entered */ + req = [self requiredFileType]; + ext = [_fullFileName pathExtension]; + if ([req length] > 0 && ![ext isEqualToString: req]) + { + int result; + NSString *msgFormat, *butFormat; + NSString *altExt; - [self _selectCellName: filename]; - [[_form cellAtIndex: 0] setStringValue: filename]; - [_form selectTextAtIndex: 0]; - [_form setNeedsDisplay: YES]; + if ([self allowsOtherFileTypes]) + { + msgFormat = + _(@"You have used the extension '.%@'.\n" + @"The standard extension is '.%@'.'"); + butFormat = _(@"Use .%@"); + altExt = ext; + } + else + { + msgFormat = + _(@"You cannot save this document with extension '.%@'.\n" + @"The required extension is '.%@'."); + butFormat = _(@"Use .%@"); + altExt = [ext stringByAppendingPathExtension: req]; + } + + result = NSRunAlertPanel(_(@"Save"), + msgFormat, + [NSString stringWithFormat: butFormat, req], + _(@"Cancel"), + [NSString stringWithFormat: butFormat, altExt], + ext, req); + switch (result) + { + case NSAlertDefaultReturn: + filename = [_fullFileName stringByDeletingPathExtension]; + filename = [filename stringByAppendingPathExtension: req]; + + ASSIGN (_fullFileName, filename); + setPath(_browser, _fullFileName); + [self _setFileName: [_fullFileName lastPathComponent]]; + break; + case NSAlertOtherReturn: + if (altExt != ext) + { + filename = [_fullFileName stringByAppendingPathExtension: req]; + + ASSIGN (_fullFileName, filename); + setPath(_browser, _fullFileName); + [self _setFileName: [_fullFileName lastPathComponent]]; + } + break; + default: + return; } }