Add displayNameWithExtension: method to OakDocument

This will also ensure the display name does not contain any slashes (which it can when a custom name is set) so that it is safe to use this as a filename.
This commit is contained in:
Allan Odgaard
2016-07-08 09:42:10 +02:00
parent 9e06e35225
commit a00a4c2d86
2 changed files with 33 additions and 0 deletions

View File

@@ -25,6 +25,8 @@ PUBLIC @interface OakDocument : NSObject
@property (nonatomic) NSString* diskEncoding;
@property (nonatomic) NSString* diskNewlines;
- (NSString*)displayNameWithExtension:(BOOL)flag;
- (void)loadModalForWindow:(NSWindow*)aWindow completionHandler:(void(^)(BOOL success, NSString* errorMessage, oak::uuid_t const& filterUUID))block;
- (void)close;

View File

@@ -401,6 +401,37 @@ private:
return _untitledCount == 1 ? @"untitled" : [NSString stringWithFormat:@"untitled %lu", _untitledCount];
}
- (NSString*)displayNameWithExtension:(BOOL)flag
{
NSString* res = [self.displayName stringByReplacingOccurrencesOfString:@"/" withString:@":"];
if(!flag)
return res;
if(_customName && OakNotEmptyString([res pathExtension]))
return res;
if(_path)
return [_path lastPathComponent];
if(_buffer && _buffer->grammar())
{
if(bundles::item_ptr item = bundles::lookup(_buffer->grammar()->uuid()))
{
if(NSString* ext = to_ns(item->value_for_field(bundles::kFieldGrammarExtension)))
return [res stringByAppendingPathExtension:ext];
}
}
else if(_fileType)
{
for(auto const& item : bundles::query(bundles::kFieldGrammarScope, to_s(_fileType)))
{
if(NSString* ext = to_ns(item->value_for_field(bundles::kFieldGrammarExtension)))
return [res stringByAppendingPathExtension:ext];
}
}
return res;
}
- (void)setPath:(NSString*)newPath
{
NSString* path = to_ns(path::resolve(to_s(newPath)));