<commit_before>

#include <boost/algorithm/string/predicate.hpp>

#include <core/FilePath.hpp>
#include <core/system/System.hpp>
#include <core/system/Environment.hpp>

#import "GwtCallbacks.h"
#import "Options.hpp"

#import <Foundation/NSString.h>

#include "SessionLauncher.hpp"
#include "Utils.hpp"

#import "MainFrameController.h"

using namespace core;
using namespace desktop;

namespace {
   
FilePath userHomePath()
{
   return core::system::userHomePath("R_USER|HOME");
}

NSString* resolveAliasedPath(NSString* path)
{
   FilePath resolved = FilePath::resolveAliasedPath([path UTF8String],
                                                    userHomePath());
   return [NSString stringWithUTF8String: resolved.absolutePath().c_str()];
}
   
} // anonymous namespace

@implementation GwtCallbacks


- (id)init
{
   if (self = [super init])
   {
   }
   return self;
}

// sentinel function for gwt deferred binding
- (Boolean) isCocoa
{
   return true;
}

- (NSString*) proportionalFont
{
   return [NSString stringWithUTF8String: options().proportionalFont().c_str()];
}

- (NSString*) fixedWidthFont
{
   return [NSString stringWithUTF8String: options().fixedWidthFont().c_str()];
}

- (void) browseUrl: (NSString*) url
{
   NSURL* nsurl = [NSURL URLWithString: url];
   desktop::utils::browseURL(nsurl);
}

- (NSString*) runSheetFileDialog: (NSSavePanel*) panel
{
   NSString* path = @"";
   [panel beginSheetModalForWindow: [[MainFrameController instance] window]
                completionHandler: nil];
   long int result = [panel runModal];
   @try
   {
      if (result == NSOKButton)
      {
         path = [[panel URL] path];
      }
   }
   @catch (NSException* e)
   {
      throw e;
   }
   @finally
   {
      [NSApp endSheet:panel];
   }
   return path;
}

- (NSString*) getOpenFileName: (NSString*) caption
              dir: (NSString*) dir
              filter: (NSString*) filter
{
   NSOpenPanel *open = [NSOpenPanel openPanel];
   [open setTitle: caption];
   [open setDirectoryURL: [NSURL fileURLWithPath:
                           [dir stringByStandardizingPath]]];
   // If the filter was specified and looks like a filter string
   // (i.e. "R Projects (*.RProj)"), extract just the extension ("RProj") to
   // pass to the open dialog.
   if ([filter length] > 0 &&
       [filter rangeOfString: @"*."].location != NSNotFound)
   {
      NSString* toExt = [filter substringFromIndex:
                         [filter rangeOfString: @"*."].location + 2];
      NSString* fromExt = [toExt substringToIndex:
                           [toExt rangeOfString: @")"].location];
      [open setAllowedFileTypes: [NSArray arrayWithObject: fromExt]];
   }
   return [self runSheetFileDialog: open];
}

- (NSString*) getSaveFileName: (NSString*) caption
              dir: (NSString* ) dir
              defaultExtension: (NSString*) defaultExtension
              forceDefaultExtension: (Boolean) forceDefaultExtension
{
   // The method is invoked with an extension like ".R", but NSSavePanel
   // expects extensions to look like "R" (i.e. no leading period).
   NSArray *extensions = [NSArray arrayWithObject:
                          [defaultExtension substringFromIndex: 1]];
   NSURL *pathAndFile = [NSURL fileURLWithPath:
                         [dir stringByStandardizingPath]];
   NSSavePanel *save = [NSSavePanel savePanel];
   [save setAllowedFileTypes: extensions];
   [save setAllowsOtherFileTypes: !forceDefaultExtension];
   [save setTitle: caption];
   [save setDirectoryURL: pathAndFile];
   [save setNameFieldStringValue: [pathAndFile lastPathComponent]];
   return [self runSheetFileDialog: save];
}

- (NSString*) getExistingDirectory: (NSString*) caption dir: (NSString*) dir
{
   NSOpenPanel *open = [NSOpenPanel openPanel];
   [open setTitle: caption];
   [open setDirectoryURL: [NSURL fileURLWithPath:
                           [dir stringByStandardizingPath]]];
   [open setCanChooseFiles: false];
   [open setCanChooseDirectories: true];
   return [self runSheetFileDialog: open];
}

- (void) undo
{
   if ([NSApp mainWindow] == [[MainFrameController instance] window])
   {<commit_msg>path aliasing for file dialogs<commit_after>

#include <boost/algorithm/string/predicate.hpp>

#include <core/FilePath.hpp>
#include <core/system/System.hpp>
#include <core/system/Environment.hpp>

#import "GwtCallbacks.h"
#import "Options.hpp"

#import <Foundation/NSString.h>

#include "SessionLauncher.hpp"
#include "Utils.hpp"

#import "MainFrameController.h"

using namespace core;
using namespace desktop;

namespace {
   
FilePath userHomePath()
{
   return core::system::userHomePath("R_USER|HOME");
}
   
NSString* createAliasedPath(NSString* path)
{
   if (path == nil || [path length] == 0)
      return @"";
   
   std::string aliased = FilePath::createAliasedPath(
                  FilePath([path UTF8String]), userHomePath());
   
   return [NSString stringWithUTF8String: aliased.c_str()];
}


NSString* resolveAliasedPath(NSString* path)
{
   if (path == nil)
      path = @"";
   
   FilePath resolved = FilePath::resolveAliasedPath([path UTF8String],
                                                    userHomePath());
   return [NSString stringWithUTF8String: resolved.absolutePath().c_str()];
}
   
} // anonymous namespace

@implementation GwtCallbacks


- (id)init
{
   if (self = [super init])
   {
   }
   return self;
}

// sentinel function for gwt deferred binding
- (Boolean) isCocoa
{
   return true;
}

- (NSString*) proportionalFont
{
   return [NSString stringWithUTF8String: options().proportionalFont().c_str()];
}

- (NSString*) fixedWidthFont
{
   return [NSString stringWithUTF8String: options().fixedWidthFont().c_str()];
}

- (void) browseUrl: (NSString*) url
{
   NSURL* nsurl = [NSURL URLWithString: url];
   desktop::utils::browseURL(nsurl);
}

- (NSString*) runSheetFileDialog: (NSSavePanel*) panel
{
   NSString* path = @"";
   [panel beginSheetModalForWindow: [[MainFrameController instance] window]
                completionHandler: nil];
   long int result = [panel runModal];
   @try
   {
      if (result == NSOKButton)
      {
         path = [[panel URL] path];
      }
   }
   @catch (NSException* e)
   {
      throw e;
   }
   @finally
   {
      [NSApp endSheet:panel];
   }
   return createAliasedPath(path);
}

- (NSString*) getOpenFileName: (NSString*) caption
              dir: (NSString*) dir
              filter: (NSString*) filter
{
   dir = resolveAliasedPath(dir);
   
   NSOpenPanel *open = [NSOpenPanel openPanel];
   [open setTitle: caption];
   [open setDirectoryURL: [NSURL fileURLWithPath:
                           [dir stringByStandardizingPath]]];
   // If the filter was specified and looks like a filter string
   // (i.e. "R Projects (*.RProj)"), extract just the extension ("RProj") to
   // pass to the open dialog.
   if ([filter length] > 0 &&
       [filter rangeOfString: @"*."].location != NSNotFound)
   {
      NSString* toExt = [filter substringFromIndex:
                         [filter rangeOfString: @"*."].location + 2];
      NSString* fromExt = [toExt substringToIndex:
                           [toExt rangeOfString: @")"].location];
      [open setAllowedFileTypes: [NSArray arrayWithObject: fromExt]];
   }
   return [self runSheetFileDialog: open];
}

- (NSString*) getSaveFileName: (NSString*) caption
              dir: (NSString* ) dir
              defaultExtension: (NSString*) defaultExtension
              forceDefaultExtension: (Boolean) forceDefaultExtension
{
   dir = resolveAliasedPath(dir);
   
   // The method is invoked with an extension like ".R", but NSSavePanel
   // expects extensions to look like "R" (i.e. no leading period).
   NSArray *extensions = [NSArray arrayWithObject:
                          [defaultExtension substringFromIndex: 1]];
   NSURL *pathAndFile = [NSURL fileURLWithPath:
                         [dir stringByStandardizingPath]];
   NSSavePanel *save = [NSSavePanel savePanel];
   [save setAllowedFileTypes: extensions];
   [save setAllowsOtherFileTypes: !forceDefaultExtension];
   [save setTitle: caption];
   [save setDirectoryURL: pathAndFile];
   [save setNameFieldStringValue: [pathAndFile lastPathComponent]];
   return [self runSheetFileDialog: save];
}

- (NSString*) getExistingDirectory: (NSString*) caption dir: (NSString*) dir
{
   dir = resolveAliasedPath(dir);
   NSOpenPanel *open = [NSOpenPanel openPanel];
   [open setTitle: caption];
   [open setDirectoryURL: [NSURL fileURLWithPath:
                           [dir stringByStandardizingPath]]];
   [open setCanChooseFiles: false];
   [open setCanChooseDirectories: true];
   return [self runSheetFileDialog: open];
}

- (void) undo
{
   if ([NSApp mainWindow] == [[MainFrameController instance] window])
   {
{'fraction_non_alphanumeric': 0.12165089499486946, 'fraction_numerical': 0.0019382054497776764, 'mean_word_length': 3.128, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1768349', 'n_tokens_mistral': 2703, 'n_tokens_neox': 2572, 'n_words': 660}