// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// Defines the WhenDoneService type.
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.Services
{
using System.Diagnostics;
using System.Windows.Forms;
using Caliburn.Micro;
using HandBrake.ApplicationServices.EventArgs;
using HandBrake.ApplicationServices.Services.Interfaces;
using HandBrake.ApplicationServices.Utilities;
using HandBrakeWPF.Services.Interfaces;
using HandBrakeWPF.ViewModels.Interfaces;
using Application = System.Windows.Application;
///
/// The when done service.
///
public class PrePostActionService : IPrePostActionService
{
///
/// The queue processor.
///
private readonly IQueueProcessor queueProcessor;
///
/// The user setting service.
///
private readonly IUserSettingService userSettingService;
///
/// The window manager.
///
private readonly IWindowManager windowManager;
///
/// Initializes a new instance of the class.
///
///
/// The queue processor.
///
///
/// The user Setting Service.
///
///
/// The window Manager.
///
public PrePostActionService(IQueueProcessor queueProcessor, IUserSettingService userSettingService, IWindowManager windowManager)
{
this.queueProcessor = queueProcessor;
this.userSettingService = userSettingService;
this.windowManager = windowManager;
this.queueProcessor.QueueCompleted += QueueProcessorQueueCompleted;
this.queueProcessor.EncodeService.EncodeCompleted += EncodeService_EncodeCompleted;
this.queueProcessor.EncodeService.EncodeStarted += EncodeService_EncodeStarted;
}
///
/// The encode service_ encode started.
///
///
/// The sender.
///
///
/// The e.
///
private void EncodeService_EncodeStarted(object sender, System.EventArgs e)
{
if (this.userSettingService.GetUserSetting(UserSettingConstants.PreventSleep))
{
Win32.PreventSleep();
}
}
///
/// The encode service_ encode completed.
///
///
/// The sender.
///
///
/// The EncodeCompletedEventArgs.
///
private void EncodeService_EncodeCompleted(object sender, HandBrake.ApplicationServices.EventArgs.EncodeCompletedEventArgs e)
{
// Send the file to the users requested applicaiton
if (e.Successful)
{
this.SendToApplication(e.FileName);
}
// Allow the system to sleep again.
Execute.OnUIThread(() =>
{
if (this.userSettingService.GetUserSetting(UserSettingConstants.PreventSleep))
{
Win32.AllowSleep();
}
});
}
///
/// The queue processor queue completed event handler.
///
///
/// The sender.
///
///
/// The e.
///
private void QueueProcessorQueueCompleted(object sender, QueueCompletedEventArgs e)
{
if (e.WasManuallyStopped)
{
return;
}
if (this.userSettingService.GetUserSetting(UserSettingConstants.WhenCompleteAction) == "Do nothing")
{
return;
}
// Give the user the ability to cancel the shutdown. Default 60 second timer.
ICountdownAlertViewModel titleSpecificView = IoC.Get();
Caliburn.Micro.Execute.OnUIThread(
() =>
{
titleSpecificView.SetAction(this.userSettingService.GetUserSetting(UserSettingConstants.WhenCompleteAction));
this.windowManager.ShowDialog(titleSpecificView);
});
if (!titleSpecificView.IsCancelled)
{
// Do something whent he encode ends.
switch (this.userSettingService.GetUserSetting(UserSettingConstants.WhenCompleteAction))
{
case "Shutdown":
Process.Start("Shutdown", "-s -t 60");
break;
case "Log off":
Win32.ExitWindowsEx(0, 0);
break;
case "Suspend":
System.Windows.Forms.Application.SetSuspendState(PowerState.Suspend, true, true);
break;
case "Hibernate":
System.Windows.Forms.Application.SetSuspendState(PowerState.Hibernate, true, true);
break;
case "Lock System":
Win32.LockWorkStation();
break;
case "Quit HandBrake":
Execute.OnUIThread(() => Application.Current.Shutdown());
break;
}
}
}
///
/// Send a file to a 3rd party application after encoding has completed.
///
///
/// The file path
///
private void SendToApplication(string file)
{
if (this.userSettingService.GetUserSetting(UserSettingConstants.SendFile) &&
!string.IsNullOrEmpty(this.userSettingService.GetUserSetting(UserSettingConstants.SendFileTo)))
{
string args = string.Format(
"{0} \"{1}\"",
this.userSettingService.GetUserSetting(UserSettingConstants.SendFileToArgs),
file);
var vlc =
new ProcessStartInfo(
this.userSettingService.GetUserSetting(UserSettingConstants.SendFileTo), args);
Process.Start(vlc);
}
}
}
}