Implement support for document types with multiple allowed file

extensions in NSSavePanel.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@27228 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Wolfgang Lux 2008-12-06 17:06:05 +00:00
parent 51ae4e18a6
commit 02bce68800
4 changed files with 106 additions and 73 deletions

View file

@ -1,3 +1,16 @@
2008-12-06 Wolfgang Lux <wlux@uni-muenster.de>
* Headers/AppKit/NSSavePanel.h:
* Source/NSSavePanel.m (-requiredFileType, -setRequiredFileType:,
-allowedFileTypes, -setAllowedFileTypes:, -filename, -_resetDefaults,
-_shouldShowExtension:isDir:):
* Source/NSDocument.m (-changeSaveType:,
-fileNameFromRunningSavePanelForSaveOperation:): Implement support
for document types with multiple allowed file extensions.
* Source/NSDocument.m (-writeWithBackuptoFile:ofType:saveOperation:):
Bug fix: Do not modify the file name argument.
2008-12-06 Wolfgang Lux <wolfgang.lux@gmail.com>
* Source/NSSavePanel.m (-ok, -cancel:, -_setDefaultDirectory:,

View file

@ -70,7 +70,7 @@ enum {
NSSize _originalMinSize;
NSSize _originalSize;
NSString *_requiredFileType;
NSArray *_allowedFileTypes;
NSString *_directory;
NSString *_fullFileName;

View file

@ -771,47 +771,37 @@ withContentsOfURL: (NSURL *)url
if (fileName && isNativeType)
{
NSArray *extensions = [[NSDocumentController sharedDocumentController]
fileExtensionsFromType: fileType];
if ([extensions count] > 0)
if ([fileManager fileExistsAtPath: fileName])
{
NSString *extension = [extensions objectAtIndex: 0];
NSString *newFileName = [[fileName stringByDeletingPathExtension]
stringByAppendingPathExtension: extension];
backupFilename = [self _backupFileNameFor: fileName];
if ([fileManager fileExistsAtPath: newFileName])
if (![self _writeBackupForFile: fileName
toFile: backupFilename])
{
backupFilename = [self _backupFileNameFor: newFileName];
if (![self _writeBackupForFile: newFileName
toFile: backupFilename])
{
return NO;
}
return NO;
}
}
if ([self writeToFile: fileName
ofType: fileType
originalFile: backupFilename
saveOperation: saveOp])
if ([self writeToFile: fileName
ofType: fileType
originalFile: backupFilename
saveOperation: saveOp])
{
// FIXME: Should set the file attributes
if (saveOp != NSSaveToOperation)
{
// FIXME: Should set the file attributes
if (saveOp != NSSaveToOperation)
{
[self setFileName: newFileName];
[self setFileType: fileType];
[self updateChangeCount: NSChangeCleared];
}
if (backupFilename && ![self keepBackupFile])
{
[fileManager removeFileAtPath: backupFilename handler: nil];
}
return YES;
[self setFileName: fileName];
[self setFileType: fileType];
[self updateChangeCount: NSChangeCleared];
}
if (backupFilename && ![self keepBackupFile])
{
[fileManager removeFileAtPath: backupFilename handler: nil];
}
return YES;
}
}
@ -853,14 +843,14 @@ withContentsOfURL: (NSURL *)url
{
if ([url isFileURL])
{
NSString *newFileName;
NSString *fileName;
newFileName = [url path];
if ([fileManager fileExistsAtPath: newFileName])
fileName = [url path];
if ([fileManager fileExistsAtPath: fileName])
{
backupFilename = [self _backupFileNameFor: newFileName];
backupFilename = [self _backupFileNameFor: fileName];
if (![self _writeBackupForFile: newFileName
if (![self _writeBackupForFile: fileName
toFile: backupFilename])
{
// FIXME: Set error.
@ -961,10 +951,7 @@ originalContentsURL: (NSURL *)orig
ASSIGN(_save_type, [controller _nameForHumanReadableType:
[sender titleOfSelectedItem]]);
extensions = [controller fileExtensionsFromType: _save_type];
if ([extensions count] > 0)
{
[(NSSavePanel *)[sender window] setRequiredFileType: [extensions objectAtIndex:0]];
}
[(NSSavePanel *)[sender window] setAllowedFileTypes: extensions];
}
- (int)runModalSavePanel: (NSSavePanel *)savePanel
@ -1088,10 +1075,7 @@ originalContentsURL: (NSURL *)orig
{
NSArray *extensions = [[NSDocumentController sharedDocumentController]
fileExtensionsFromType: [self fileType]];
if ([extensions count] > 0)
{
[savePanel setRequiredFileType:[extensions objectAtIndex:0]];
}
[savePanel setAllowedFileTypes: extensions];
}
switch (saveOperation)

View file

@ -412,7 +412,7 @@ setPath(NSBrowser *browser, NSString *path)
[self _setDefaultDirectory];
[self setPrompt: @"Name:"];
[self setTitle: @"Save"];
[self setRequiredFileType: @""];
[self setAllowedFileTypes: nil];
[self setTreatsFilePackagesAsDirectories: NO];
[self setDelegate: nil];
[self setAccessoryView: nil];
@ -588,8 +588,9 @@ selectCellWithString: (NSString*)title
{
if (*isDir == NO)
{
if (_requiredFileType != nil && [_requiredFileType length] != 0
&& [extension isEqualToString: _requiredFileType] == NO)
if (_allowedFileTypes != nil
&& [_allowedFileTypes indexOfObject: extension] == NSNotFound
&& [_allowedFileTypes indexOfObject: @""] != NSNotFound)
return NO;
}
else if ([extension length] == 0)
@ -598,9 +599,9 @@ selectCellWithString: (NSString*)title
}
else if (_treatsFilePackagesAsDirectories == NO)
{
if (_requiredFileType == nil || [_requiredFileType length] == 0
|| [extension isEqualToString: _requiredFileType] == YES)
*isDir = NO;
if (_allowedFileTypes == nil
|| [_allowedFileTypes indexOfObject: extension] != NSNotFound)
*isDir = NO;
}
return YES;
@ -687,7 +688,7 @@ selectCellWithString: (NSString*)title
[[NSNotificationCenter defaultCenter] removeObserver: self];
TEST_RELEASE (_fullFileName);
TEST_RELEASE (_directory);
TEST_RELEASE (_requiredFileType);
TEST_RELEASE (_allowedFileTypes);
[super dealloc];
}
@ -703,7 +704,7 @@ selectCellWithString: (NSString*)title
* All these are set automatically
_directory = nil;
_fullFileName = nil;
_requiredFileType = nil;
_allowedFileTypes = nil;
_delegate = nil;
_treatsFilePackagesAsDirectories = NO;
@ -928,24 +929,55 @@ selectCellWithString: (NSString*)title
is used for another file type within the application. If
you do not invoke it, or set it to empty string or nil, no
extension will be appended, indicated by an empty string
returned from -requiredFileType .</p><p>See Also: -requiredFileType</p>
returned from -requiredFileType.</p><p>This method is equivalent
to calling -setAllowedFileTypes: with an array containing only
fileType.</p><p>See Also: -requiredFileType</p>
*/
- (void) setRequiredFileType: (NSString*)fileType
{
ASSIGN(_requiredFileType, fileType);
if ([fileType length] == 0)
DESTROY(_allowedFileTypes);
else
ASSIGN(_allowedFileTypes, [NSArray arrayWithObject: fileType]);
}
/**<p>Returns the required file type. The default, indicated by empty string,
* is no required file type.</p><p>See Also: -setRequiredFileType:</p>
/**<p>Returns the required file type. The default, indicated by an empty
* string, is no required file type.</p><p>This method is equivalent to
* calling -allowedFileTypes and returning the first element of the list
* of allowed types, or the empty string if there are none.</p>
* <p>See Also: -setRequiredFileType:</p>
*/
- (NSString*) requiredFileType
{
return _requiredFileType;
if ([_allowedFileTypes count] > 0)
return [_allowedFileTypes objectAtIndex: 0];
else
return @"";
}
/**<p> Specifies the allowed types, i.e., file name extensions to
be appended to any selected files that don't already have one
of those extensions. The elements of the array should be strings
that do not include the period that begins the extension. Invoke
this method each time the Save panel is used for another file type
within the application. If you do not invoke it, or set it to an
empty array or nil, no extension will be appended, indicated by nil
returned from -allowedFileTypes.</p><p>See Also: -allowedFileTypes</p>
*/
- (void) setAllowedFileTypes: (NSArray *)types
{
// FIXME
if ([types count] == 0)
DESTROY(_allowedFileTypes);
else
ASSIGN(_allowedFileTypes, types);
}
/**<p>Returns an array of the allowed file types. The default, indicated by
* nil, is any file type is allowed.</p><p>See Also: -setAllowedFileTypes:</p>
*/
- (NSArray *) allowedFileTypes
{
return _allowedFileTypes;
}
- (void) setAllowsOtherFileTypes: (BOOL)flag
@ -953,12 +985,6 @@ selectCellWithString: (NSString*)title
_allowsOtherFileTypes = flag;
}
- (NSArray *) allowedFileTypes
{
// FIXME
return nil;
}
- (BOOL) allowsOtherFileTypes
{
return _allowsOtherFileTypes;
@ -1076,17 +1102,27 @@ selectCellWithString: (NSString*)title
*/
- (NSString*) filename
{
NSString *fileType;
if (_fullFileName == nil)
return @"";
if (_requiredFileType == nil || [_requiredFileType isEqual: @""] == YES)
if (_allowedFileTypes == nil ||
[_allowedFileTypes indexOfObject: @""] != NSNotFound)
return _fullFileName;
// add filetype extension only if the filename does not include it already
if ([[_fullFileName pathExtension] isEqual: _requiredFileType] == YES)
return _fullFileName;
/* add filetype extension only if the filename does not include an
allowed one already */
fileType = [_fullFileName pathExtension];
if ([_allowedFileTypes indexOfObject: fileType] != NSNotFound)
{
return _fullFileName;
}
else
return [_fullFileName stringByAppendingPathExtension: _requiredFileType];
{
fileType = [_allowedFileTypes objectAtIndex: 0];
return [_fullFileName stringByAppendingPathExtension: fileType];
}
}
- (NSURL *) URL