Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sungaila committed Mar 15, 2019
1 parent a6d319a commit c9e2437
Show file tree
Hide file tree
Showing 55 changed files with 2,385 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
*.swp
*.*~
project.lock.json
.DS_Store
*.pyc
nupkg/

# Visual Studio Code
.vscode

# Rider
.idea

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
msbuild.log
msbuild.err
msbuild.wrn

# Visual Studio 2015
.vs/
6 changes: 6 additions & 0 deletions App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Application x:Class="Sungaila.SUBSTitute.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DispatcherUnhandledException="Application_DispatcherUnhandledException"
StartupUri="Views/MainWindow.xaml" />

40 changes: 40 additions & 0 deletions App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Sungaila.SUBSTitute.ViewModels;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace Sungaila.SUBSTitute
{
public partial class App : Application
{
private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
if (MessageBox.Show(MainWindow,
$"The following unexpected error has occured:{Environment.NewLine}{Environment.NewLine}{e.Exception.Message}{Environment.NewLine}{Environment.NewLine}[OK] will terminate this application.{Environment.NewLine}[Cancel] will keep the application running but it might be unstable from now on.",
"Unexpected error",
MessageBoxButton.OKCancel,
MessageBoxImage.Error) != MessageBoxResult.OK)
e.Handled = true;
}

private void Application_Exit(object sender, ExitEventArgs e)
{
MainWindowViewModel? viewModel = MainWindow.DataContext as MainWindowViewModel;

if (viewModel == null)
return;

UserSettings.Save(new UserSettings
{
LastSelectedDriveLetter = viewModel.SelectedMapping?.DriveLetter,
LastBrowserRootDirectory = viewModel.BrowserRootDirectory
});
}
}
}
37 changes: 37 additions & 0 deletions Commands/BrowserGotoParentDirectoryCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.WindowsAPICodePack.Dialogs;
using Sungaila.SUBSTitute.Core;
using Sungaila.SUBSTitute.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Sungaila.SUBSTitute.Command
{
public class BrowserGotoParentDirectoryCommand
: ViewModelCommand<MainWindowViewModel>
{
public override void Execute(MainWindowViewModel parameter)
{
if (String.IsNullOrEmpty(parameter.BrowserRootDirectory))
return;

DirectoryInfo dirInfo = new DirectoryInfo(parameter.BrowserRootDirectory);

if (!dirInfo.Exists || dirInfo.Parent == null)
return;

parameter.BrowserRootDirectory = dirInfo.Parent.FullName;
}

public override bool CanExecute(MainWindowViewModel parameter)
{
if (!base.CanExecute(parameter) || String.IsNullOrEmpty(parameter.BrowserRootDirectory))
return false;

DirectoryInfo dirInfo = new DirectoryInfo(parameter.BrowserRootDirectory);

return dirInfo.Exists && dirInfo.Parent != null;
}
}
}
40 changes: 40 additions & 0 deletions Commands/MapDriveCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Microsoft.WindowsAPICodePack.Dialogs;
using Sungaila.SUBSTitute.Core;
using Sungaila.SUBSTitute.ViewModels;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace Sungaila.SUBSTitute.Command
{
public class MapDriveCommand
: ViewModelCommand<MainWindowViewModel>
{
public override void Execute(MainWindowViewModel parameter)
{
if (parameter.SelectedMapping == null || parameter.SelectedMapping.Directory == null)
return;

char driveLetter = parameter.SelectedMapping.DriveLetter;

try
{
Win32.DosDevice.MapDrive(driveLetter, parameter.SelectedMapping.Directory);
parameter.UpdateAvailableMappings(driveLetter);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to map drive letter {driveLetter}.{Environment.NewLine}{ex.Message}", "Failed to map", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

public override bool CanExecute(MainWindowViewModel parameter)
{
return base.CanExecute(parameter) &&
parameter.SelectedMapping != null &&
!String.IsNullOrEmpty(parameter.SelectedMapping.Directory) &&
(parameter.SelectedMapping.InitialDirectory == null || parameter.SelectedMapping.Directory != parameter.SelectedMapping.InitialDirectory);
}
}
}
25 changes: 25 additions & 0 deletions Commands/SelectBrowserDirectoryCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.WindowsAPICodePack.Dialogs;
using Sungaila.SUBSTitute.Core;
using Sungaila.SUBSTitute.ViewModels;
using System;
using System.Collections.Generic;
using System.Text;

namespace Sungaila.SUBSTitute.Command
{
public class SelectBrowserDirectoryCommand
: ViewModelCommand<MainWindowViewModel>
{
public override void Execute(MainWindowViewModel parameter)
{
var openDirectoryDialog = new CommonOpenFileDialog
{
IsFolderPicker = true,
InitialDirectory = parameter.BrowserRootDirectory
};

if (openDirectoryDialog.ShowDialog() == CommonFileDialogResult.Ok)
parameter.BrowserRootDirectory = openDirectoryDialog.FileName;
}
}
}
38 changes: 38 additions & 0 deletions Commands/SelectMappedDirectoryCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.WindowsAPICodePack.Dialogs;
using Sungaila.SUBSTitute.Core;
using Sungaila.SUBSTitute.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Sungaila.SUBSTitute.Command
{
public class SelectMappedDirectoryCommand
: ViewModelCommand<MainWindowViewModel>
{
public override void Execute(MainWindowViewModel parameter)
{
if (parameter.SelectedMapping == null)
return;

DirectoryInfo? dirInfo = parameter.SelectedMapping.Directory != null
? new DirectoryInfo(parameter.SelectedMapping.Directory)
: null;

var openDirectoryDialog = new CommonOpenFileDialog
{
IsFolderPicker = true,
InitialDirectory = dirInfo != null && dirInfo.Exists && dirInfo.Parent != null ? dirInfo.Parent.FullName : null
};

if (openDirectoryDialog.ShowDialog() == CommonFileDialogResult.Ok)
parameter.SelectedMapping.Directory = openDirectoryDialog.FileName;
}

public override bool CanExecute(MainWindowViewModel parameter)
{
return base.CanExecute(parameter) && parameter.SelectedMapping != null;
}
}
}
48 changes: 48 additions & 0 deletions Commands/UnmapAllDrivesCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.WindowsAPICodePack.Dialogs;
using Sungaila.SUBSTitute.Core;
using Sungaila.SUBSTitute.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Sungaila.SUBSTitute.Command
{
public class UnmapAllDrivesCommand
: ViewModelCommand<MainWindowViewModel>
{
public override void Execute(MainWindowViewModel parameter)
{
bool failed = false;
char? selectedDrive = parameter.SelectedMapping?.DriveLetter;

StringBuilder errorMessage = new StringBuilder("Failed to unmap the following drive letters:");

foreach (MappingViewModel mapping in parameter.Mappings.Where(mapping => mapping.IsDirectoryMapped))
{
char driveLetter = mapping.DriveLetter;

try
{
Win32.DosDevice.UnmapDrive(driveLetter);
}
catch (Exception ex)
{
failed = true;
errorMessage.Append($"{Environment.NewLine}{driveLetter} - {ex.Message}");
}
}

parameter.UpdateAvailableMappings(selectedDrive);

if (failed)
MessageBox.Show(errorMessage.ToString(), "Failed to unmap all drives", MessageBoxButton.OK, MessageBoxImage.Error);
}

public override bool CanExecute(MainWindowViewModel parameter)
{
return base.CanExecute(parameter) && parameter.Mappings.Any(mapping => mapping.IsDirectoryMapped);
}
}
}
37 changes: 37 additions & 0 deletions Commands/UnmapDriveCommand .cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.WindowsAPICodePack.Dialogs;
using Sungaila.SUBSTitute.Core;
using Sungaila.SUBSTitute.ViewModels;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace Sungaila.SUBSTitute.Command
{
public class UnmapDriveCommand
: ViewModelCommand<MainWindowViewModel>
{
public override void Execute(MainWindowViewModel parameter)
{
if (parameter.SelectedMapping == null)
return;

char driveLetter = parameter.SelectedMapping.DriveLetter;

try
{
Win32.DosDevice.UnmapDrive(driveLetter);
parameter.UpdateAvailableMappings(driveLetter);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to unmap drive letter {driveLetter}.{Environment.NewLine}{ex.Message}", "Failed to unmap", MessageBoxButton.OK, MessageBoxImage.Error);
}
}

public override bool CanExecute(MainWindowViewModel parameter)
{
return base.CanExecute(parameter) && parameter.SelectedMapping != null && parameter.SelectedMapping.IsDirectoryMapped;
}
}
}
29 changes: 29 additions & 0 deletions Converters/BoolToFontWeightConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace Sungaila.SUBSTitute.Converters
{
[ValueConversion(typeof(bool), typeof(FontWeight))]
public class BoolToFontWeightConverter
: IValueConverter
{
public static readonly BoolToFontWeightConverter Instance = new BoolToFontWeightConverter();

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is bool))
return Binding.DoNothing;

return ((bool)value) ? FontWeights.ExtraBold : FontWeights.Normal;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
29 changes: 29 additions & 0 deletions Converters/BoolToGridLengthConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace Sungaila.SUBSTitute.Converters
{
[ValueConversion(typeof(bool), typeof(GridLength))]
public class BoolToGridLengthConverter
: IValueConverter
{
public static readonly BoolToGridLengthConverter Instance = new BoolToGridLengthConverter();

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is bool))
return Binding.DoNothing;

return (bool)value ? new GridLength(1.0, GridUnitType.Star) : GridLength.Auto;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Loading

0 comments on commit c9e2437

Please sign in to comment.