<commit_before>﻿using System;
using System.Collections.Generic;
using System.Composition;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using BCad.Extensions;
using BCad.FileHandlers;
using Microsoft.Win32;

namespace BCad.Services
{
    [ExportWorkspaceService, Shared]
    internal class FileSystemService : IFileSystemService
    {
        [ImportMany]
        public IEnumerable<Lazy<IFileHandler, FileHandlerMetadata>> FileHandlers { get; set; }

        [Import]
        public IWorkspace Workspace { get; set; }

        public Task<string> GetFileNameFromUserForSave()
        {
            var x = FileHandlers.Where(fw => fw.Metadata.CanWrite).Select(fw => new FileSpecification(fw.Metadata.DisplayName, fw.Metadata.FileExtensions));
            return GetFileNameFromUserForWrite(x);
        }

        public Task<string> GetFileNameFromUserForWrite(IEnumerable<FileSpecification> fileSpecifications)
        {
            var filter = string.Join("|",
                from fs in fileSpecifications.OrderBy(f => f.DisplayName)
                let exts = string.Join(";", fs.FileExtensions.Select(x => "*" + x))
                select string.Format("{0}|{1}", fs.DisplayName, exts));

            var dialog = new SaveFileDialog();
            dialog.DefaultExt = fileSpecifications.First().FileExtensions.First();
            dialog.Filter = filter;
            var result = dialog.ShowDialog();
            if (result != true)
                return Task.FromResult<string>(null);

            return Task.FromResult(dialog.FileName);
        }

        public Task<string> GetFileNameFromUserForOpen()
        {
            var fileSpecifications = FileHandlers.Where(fr => fr.Metadata.CanRead).Select(fr => new FileSpecification(fr.Metadata.DisplayName, fr.Metadata.FileExtensions));
            var filter = string.Join("|",
                    from r in fileSpecifications.OrderBy(f => f.DisplayName)
                    let exts = string.Join(";", r.FileExtensions.Select(x => "*" + x))
                    select string.Format("{0}|{1}", r.DisplayName, exts));

            var all = string.Format("{0}|{1}",
                "All supported types",
                string.Join(";", fileSpecifications.SelectMany(f => f.FileExtensions).Select(x => "*" + x).OrderBy(x => x)));

            filter = string.Join("|", all, filter);

            var dialog = new OpenFileDialog();
            dialog.InitialDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); // TODO: this is just for debugging
            dialog.DefaultExt = fileSpecifications.First().FileExtensions.First();
            dialog.Filter = filter;
            var result = dialog.ShowDialog();
            if (result != true)
                return Task.FromResult<string>(null);
            return Task.FromResult(dialog.FileName);
        }

        public Task<bool> TryWriteDrawing(string fileName, Drawing drawing, ViewPort viewPort)
        {
            if (fileName == null)
                throw new ArgumentNullException("fileName");

            var extension = Path.GetExtension(fileName);<commit_msg>don't auto-open in binary directory anymore<commit_after>﻿using System;
using System.Collections.Generic;
using System.Composition;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using BCad.Extensions;
using BCad.FileHandlers;
using Microsoft.Win32;

namespace BCad.Services
{
    [ExportWorkspaceService, Shared]
    internal class FileSystemService : IFileSystemService
    {
        [ImportMany]
        public IEnumerable<Lazy<IFileHandler, FileHandlerMetadata>> FileHandlers { get; set; }

        [Import]
        public IWorkspace Workspace { get; set; }

        public Task<string> GetFileNameFromUserForSave()
        {
            var x = FileHandlers.Where(fw => fw.Metadata.CanWrite).Select(fw => new FileSpecification(fw.Metadata.DisplayName, fw.Metadata.FileExtensions));
            return GetFileNameFromUserForWrite(x);
        }

        public Task<string> GetFileNameFromUserForWrite(IEnumerable<FileSpecification> fileSpecifications)
        {
            var filter = string.Join("|",
                from fs in fileSpecifications.OrderBy(f => f.DisplayName)
                let exts = string.Join(";", fs.FileExtensions.Select(x => "*" + x))
                select string.Format("{0}|{1}", fs.DisplayName, exts));

            var dialog = new SaveFileDialog();
            dialog.DefaultExt = fileSpecifications.First().FileExtensions.First();
            dialog.Filter = filter;
            var result = dialog.ShowDialog();
            if (result != true)
                return Task.FromResult<string>(null);

            return Task.FromResult(dialog.FileName);
        }

        public Task<string> GetFileNameFromUserForOpen()
        {
            var fileSpecifications = FileHandlers.Where(fr => fr.Metadata.CanRead).Select(fr => new FileSpecification(fr.Metadata.DisplayName, fr.Metadata.FileExtensions));
            var filter = string.Join("|",
                    from r in fileSpecifications.OrderBy(f => f.DisplayName)
                    let exts = string.Join(";", r.FileExtensions.Select(x => "*" + x))
                    select string.Format("{0}|{1}", r.DisplayName, exts));

            var all = string.Format("{0}|{1}",
                "All supported types",
                string.Join(";", fileSpecifications.SelectMany(f => f.FileExtensions).Select(x => "*" + x).OrderBy(x => x)));

            filter = string.Join("|", all, filter);

            var dialog = new OpenFileDialog();
            dialog.DefaultExt = fileSpecifications.First().FileExtensions.First();
            dialog.Filter = filter;
            var result = dialog.ShowDialog();
            if (result != true)
                return Task.FromResult<string>(null);
            return Task.FromResult(dialog.FileName);
        }

        public Task<bool> TryWriteDrawing(string fileName, Drawing drawing, ViewPort viewPort)
        {
            if (fileName == null)
                throw new ArgumentNullException("fileName");

            var extension = Path.GetExtension(fileName);
{'fraction_non_alphanumeric': 0.11633145615446501, 'fraction_numerical': 0.002574416733708769, 'mean_word_length': 2.490174059517125, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2867285', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1598, 'n_words': 350}