Skip to content

Commit

Permalink
Catching errors in logging. Fixing other crashes/issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
daiplusplus committed Jun 18, 2018
1 parent 2ab11ee commit f32868e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 22 deletions.
16 changes: 16 additions & 0 deletions TeslaTags.Gui/ViewModel/BaseViewModel.cs
Expand Up @@ -11,6 +11,22 @@ namespace TeslaTags.Gui
{
public abstract class BaseViewModel : ViewModelBase
{
/* TODO: I don't understand why this doesn't work.
protected RelayCommand CreateBusyCommand( Action action, Func<Boolean> additionalCanExecute, Boolean enabledWhenBusy = false )
{
RelayCommand cmd;
if( enabledWhenBusy )
{
cmd = new RelayCommand( action, canExecute: () => this.CanExecuteWhenBusy() && additionalCanExecute() );
}
else
{
cmd = new RelayCommand( action, canExecute: () => this.CanExecuteWhenNotBusy() && additionalCanExecute() );
}
this.busyCommands.Add( cmd );
return cmd;
}*/

protected RelayCommand CreateBusyCommand( Action action, Boolean enabledWhenBusy = false )
{
RelayCommand cmd;
Expand Down
64 changes: 46 additions & 18 deletions TeslaTags.Gui/ViewModel/MainViewModel.cs
Expand Up @@ -18,15 +18,16 @@ public class MainViewModel : BaseViewModel, ITeslaTagEventsListener

public MainViewModel(ITeslaTagsService teslaTagsService, ITeslaTagUtilityService utilityService, IConfigurationService configurationService, IWindowService windowService)
{
this.teslaTagsService = teslaTagsService;
this.teslaTagsService = teslaTagsService;
this.teslaTagsService.EventsListener = new DispatchTeslaTagEventsListener( this );
this.utilityService = utilityService;
this.utilityService = utilityService;
this.configurationService = configurationService;
this.windowService = windowService;
this.windowService = windowService;

this.WindowLoadedCommand = new RelayCommand( this.WindowLoaded );
this.StartCommand = this.CreateBusyCommand( this.Start );
this.StopCommand = this.CreateBusyCommand( this.Stop, enabledWhenBusy: true );
this.WindowLoadedCommand = new RelayCommand( this.WindowLoaded );
this.WindowClosingCommand = new RelayCommand( this.WindowClosing );
this.StartCommand = this.CreateBusyCommand( this.Start ); // , additionalCanExecute: this.DirectoryPathIsValid ); // weird, why doesn't this work? my `DirectoryPathIsValid` function isn't invoked after editing the textbox.
this.StopCommand = this.CreateBusyCommand( this.Stop, enabledWhenBusy: true );

///////////

Expand Down Expand Up @@ -60,7 +61,16 @@ public MainViewModel(ITeslaTagsService teslaTagsService, ITeslaTagUtilityService
public String DirectoryPath
{
get { return this.directoryPath; }
set { this.Set( nameof(this.DirectoryPath), ref this.directoryPath, value ); }
set
{
Boolean diff = this.Set( nameof(this.DirectoryPath), ref this.directoryPath, value );
//if( diff ) this.StartCommand.RaiseCanExecuteChanged();
}
}

private Boolean DirectoryPathIsValid()
{
return !String.IsNullOrWhiteSpace( this.DirectoryPath ) && Directory.Exists( this.DirectoryPath );
}

private Boolean onlyValidate;
Expand Down Expand Up @@ -164,22 +174,34 @@ private void LoadConfig()
{
Config config = this.configurationService.Config;

this.DirectoryPath = config.RootDirectory;
if( !String.IsNullOrWhiteSpace( config.RootDirectory ) && Directory.Exists( config.RootDirectory ) )
{
this.DirectoryPath = config.RootDirectory;
}

this.HideEmptyDirectories = config.HideEmptyDirectories;
this.ExcludeLines = String.Join( "\r\n", config.ExcludeList );
this.GenreRules.LoadFrom( config.GenreRules );
this.ExcludeLines = String.Join( "\r\n", config.ExcludeList ?? new String[0] );

if( config.GenreRules != null )
{
this.GenreRules.LoadFrom( config.GenreRules );
}

var pos = config.RestoredWindowPosition;
var window = this.windowService.GetWindowByDataContext( this );
if( pos.Width > 0 && pos.Height > 0 )
if( window != null )
{
window.Top = pos.Y;
window.Left = pos.X;
window.Width = pos.Width;
window.Height = pos.Height;
}
var pos = config.RestoredWindowPosition;

if( pos.Width > 0 && pos.Height > 0 )
{
window.Top = pos.Y;
window.Left = pos.X;
window.Width = pos.Width;
window.Height = pos.Height;
}

if( config.IsMaximized ) window.WindowState = System.Windows.WindowState.Maximized;
if( config.IsMaximized ) window.WindowState = System.Windows.WindowState.Maximized;
}
}

private void SaveConfig()
Expand Down Expand Up @@ -208,6 +230,12 @@ private void SaveConfig()

private void Start()
{
if( !this.DirectoryPathIsValid() )
{
// TODO: Show error message?
return;
}

this.IsBusy = this.teslaTagsService.IsBusy;
this.ProgressPerc = -1;

Expand Down
23 changes: 19 additions & 4 deletions TeslaTags/Program.cs
Expand Up @@ -3,7 +3,6 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

namespace TeslaTags
{
Expand Down Expand Up @@ -183,9 +182,25 @@ public DirectoryResult ProcessDirectory(String directory, Boolean readOnly, Bool

(FolderType folderType, Int32 modifiedCount, Int32 totalCount) = Folder.Process( directory, readOnly, undo, genreRules, messages );

File.AppendAllLines( this.generalLog, messages.Select( msg => msg.ToString() ) ); // AppendAllLines writes a terminal `\r\n`

File.AppendAllText( this.directoryLog, directory + "\r\n" );
try
{
File.AppendAllLines( this.generalLog, messages.Select( msg => msg.ToString() ) ); // AppendAllLines writes a terminal `\r\n`
}
catch( Exception ex )
{
String text = String.Format( CultureInfo.InvariantCulture, @"Couldn't write to log file ""{0}"". Exception: {1}, Message: ""{2}"".", this.generalLog, ex.GetType().Name, ex.Message );
messages.Add( new Message( MessageSeverity.Error, directory, directory, text ) );
}

try
{
File.AppendAllText( this.directoryLog, directory + "\r\n" );
}
catch( Exception ex )
{
String text = String.Format( CultureInfo.InvariantCulture, @"Couldn't write to log file ""{0}"". Exception: {1}, Message: ""{2}"".", this.directoryLog, ex.GetType().Name, ex.Message );
messages.Add( new Message( MessageSeverity.Error, directory, directory, text ) );
}

return new DirectoryResult( folderType, totalCount, modifiedCount, messages );
}
Expand Down

0 comments on commit f32868e

Please sign in to comment.