From 3ceefce2e4333db6f13f8249bd14ef797f065f9e Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Tue, 19 Mar 2013 22:09:40 +0100 Subject: [PATCH] Improve renaming files with hidden extension MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now clear the “hidden extension” flag if the user manually adds it. We also ensure the hidden extension is not ignored by comparing base name with display name. The flag is ignored under certain circumstances. Comparing base name with display name alone may give a false positive for localized file names. --- .../OakFileBrowser/src/io/FSDataSource.mm | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Frameworks/OakFileBrowser/src/io/FSDataSource.mm b/Frameworks/OakFileBrowser/src/io/FSDataSource.mm index 839e8aff..f0e37faa 100644 --- a/Frameworks/OakFileBrowser/src/io/FSDataSource.mm +++ b/Frameworks/OakFileBrowser/src/io/FSDataSource.mm @@ -10,6 +10,7 @@ #import #import #import +#import #import #import #import @@ -102,8 +103,24 @@ static ino_t inode (std::string const& path) std::string src = [[item.url path] fileSystemRepresentation]; std::string dst = path::join(path::parent(src), [[objectValue stringByReplacingOccurrencesOfString:@"/" withString:@":"] fileSystemRepresentation]); - if(path::info(src) & path::flag::hidden_extension) // FIXME files with multiple extenions have the “hidden_extension” flag ignored - dst += path::extension(src); // TODO replicate Finder’s heuristic for toggling “extension hidden” flag + + // “hidden extension” is ignored if Finder is set to show all file extensions, if there are multiple extensions, or if no application is assigned to the extension. + std::string const baseName = path::name(src); + std::string const displayName = to_s(item.name); + bool hiddenExtension = baseName != displayName && (path::info(src) & path::flag::hidden_extension); + + if(src == dst && hiddenExtension) + { + NSURL* dstURL = [NSURL fileURLWithPath:[NSString stringWithCxxString:dst]]; + NSError* error; + if(![dstURL setResourceValue:@NO forKey:NSURLHasHiddenExtensionKey error:&error]) + NSLog(@"%s %@", sel_getName(_cmd), error); + + return; + } + + if(hiddenExtension) + dst += path::extension(src); if(src != dst) {