mattercontrol/PrinterControls/PrinterConnections/SetupStepInstallDriver.cs

180 lines
5.7 KiB
C#
Raw Normal View History

2015-04-08 15:20:10 -07:00
using MatterHackers.Agg;
2014-06-19 16:09:38 -07:00
using MatterHackers.Agg.PlatformAbstract;
2014-01-29 19:09:30 -08:00
using MatterHackers.Agg.UI;
using MatterHackers.Localizations;
using MatterHackers.MatterControl.CustomWidgets;
2016-04-18 11:31:31 -07:00
using MatterHackers.MatterControl.SlicerConfiguration;
2015-04-08 15:20:10 -07:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
2014-01-29 19:09:30 -08:00
namespace MatterHackers.MatterControl.PrinterControls.PrinterConnections
{
2015-04-08 15:20:10 -07:00
public class SetupStepInstallDriver : SetupConnectionWidgetBase
{
private FlowLayoutWidget printerDriverContainer;
private TextWidget printerDriverMessage;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
private Button installButton;
private Button skipButton;
2014-01-29 19:09:30 -08:00
2016-04-18 11:31:31 -07:00
public SetupStepInstallDriver(ConnectionWizard windowController)
: base(windowController)
2015-04-08 15:20:10 -07:00
{
2014-03-11 15:24:47 -07:00
headerLabel.Text = string.Format(LocalizedString.Get("Install Communication Driver"));
2015-04-08 15:20:10 -07:00
printerDriverContainer = createPrinterDriverContainer();
contentRow.AddChild(printerDriverContainer);
{
//Construct buttons
2014-03-11 15:24:47 -07:00
installButton = textImageButtonFactory.Generate(LocalizedString.Get("Install Driver"));
2015-04-08 15:20:10 -07:00
installButton.Click += (sender, e) =>
{
2016-04-18 11:31:31 -07:00
UiThread.RunOnIdle(() =>
{
bool canContinue = this.InstallDriver();
if (canContinue)
{
connectionWizard.ChangeToSetupBaudOrComPortOne();
}
});
2015-04-08 15:20:10 -07:00
};
2014-01-29 19:09:30 -08:00
2014-03-11 15:24:47 -07:00
skipButton = textImageButtonFactory.Generate(LocalizedString.Get("Skip"));
2016-04-18 11:31:31 -07:00
skipButton.Click += (s, e) => connectionWizard.ChangeToSetupBaudOrComPortOne();
2015-04-08 15:20:10 -07:00
//Add buttons to buttonContainer
footerRow.AddChild(installButton);
footerRow.AddChild(skipButton);
footerRow.AddChild(new HorizontalSpacer());
2015-04-08 15:20:10 -07:00
footerRow.AddChild(cancelButton);
}
}
private FlowLayoutWidget createPrinterDriverContainer()
{
FlowLayoutWidget container = new FlowLayoutWidget(FlowDirection.TopToBottom);
container.Margin = new BorderDouble(0, 5);
BorderDouble elementMargin = new BorderDouble(top: 3);
2014-01-29 19:09:30 -08:00
2014-03-11 15:24:47 -07:00
printerDriverMessage = new TextWidget(LocalizedString.Get("This printer requires a driver for communication."), 0, 0, 10);
printerDriverMessage.TextColor = ActiveTheme.Instance.PrimaryTextColor;
2015-04-08 15:20:10 -07:00
printerDriverMessage.HAnchor = HAnchor.ParentLeftRight;
printerDriverMessage.Margin = elementMargin;
2014-01-29 19:09:30 -08:00
2014-03-11 15:24:47 -07:00
TextWidget printerDriverMessageTwo = new TextWidget(LocalizedString.Get("Driver located. Would you like to install?"), 0, 0, 10);
printerDriverMessageTwo.TextColor = ActiveTheme.Instance.PrimaryTextColor;
2014-01-29 19:09:30 -08:00
printerDriverMessageTwo.HAnchor = HAnchor.ParentLeftRight;
printerDriverMessageTwo.Margin = elementMargin;
2015-04-08 15:20:10 -07:00
container.AddChild(printerDriverMessage);
container.AddChild(printerDriverMessageTwo);
container.HAnchor = HAnchor.ParentLeftRight;
return container;
}
private void InstallDriver(string fileName)
{
switch (OsInformation.OperatingSystem)
{
case OSType.Windows:
if (File.Exists(fileName))
{
2015-04-08 15:20:10 -07:00
if (Path.GetExtension(fileName).ToUpper() == ".INF")
{
Process driverInstallerProcess = new Process();
// Prepare the process to run
2016-04-18 11:31:31 -07:00
2015-04-08 15:20:10 -07:00
// Enter in the command line arguments, everything you would enter after the executable name itself
driverInstallerProcess.StartInfo.Arguments = Path.GetFullPath(fileName);
2016-04-18 11:31:31 -07:00
2015-04-08 15:20:10 -07:00
// Enter the executable to run, including the complete path
string printerDriverInstallerExePathAndFileName = Path.GetFullPath(Path.Combine(".", "InfInstaller.exe"));
driverInstallerProcess.StartInfo.CreateNoWindow = true;
driverInstallerProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
driverInstallerProcess.StartInfo.FileName = Path.GetFullPath(printerDriverInstallerExePathAndFileName);
driverInstallerProcess.StartInfo.Verb = "runas";
driverInstallerProcess.StartInfo.UseShellExecute = true;
driverInstallerProcess.Start();
driverInstallerProcess.WaitForExit();
}
else
{
Process.Start(fileName);
}
}
else
{
2016-04-18 11:31:31 -07:00
throw new Exception(string.Format("Can't find driver {0}.", fileName));
}
2015-04-08 15:20:10 -07:00
break;
2015-04-08 15:20:10 -07:00
case OSType.Mac:
break;
2014-01-29 19:09:30 -08:00
2015-04-08 15:20:10 -07:00
case OSType.X11:
if (File.Exists(fileName))
{
if (Path.GetExtension(fileName).ToUpper() == ".INF")
{
var driverInstallerProcess = new Process();
// Prepare the process to run
2016-04-18 11:31:31 -07:00
2015-04-08 15:20:10 -07:00
// Enter in the command line arguments, everything you would enter after the executable name itself
driverInstallerProcess.StartInfo.Arguments = Path.GetFullPath(fileName);
2016-04-18 11:31:31 -07:00
2015-04-08 15:20:10 -07:00
// Enter the executable to run, including the complete path
string printerDriverInstallerExePathAndFileName = Path.Combine(".", "InfInstaller.exe");
driverInstallerProcess.StartInfo.CreateNoWindow = true;
driverInstallerProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
driverInstallerProcess.StartInfo.FileName = Path.GetFullPath(printerDriverInstallerExePathAndFileName);
driverInstallerProcess.StartInfo.Verb = "runas";
driverInstallerProcess.StartInfo.UseShellExecute = true;
driverInstallerProcess.Start();
driverInstallerProcess.WaitForExit();
// Retrieve the app's exit code
var exitCode = driverInstallerProcess.ExitCode;
}
else
{
Process.Start(fileName);
}
}
else
{
2016-04-18 11:31:31 -07:00
throw new Exception("Can't find driver: " + fileName);
2015-04-08 15:20:10 -07:00
}
break;
}
}
2016-04-18 11:31:31 -07:00
private bool InstallDriver()
2015-04-08 15:20:10 -07:00
{
try
{
2016-04-18 11:31:31 -07:00
printerDriverMessage.Text = "Installing".Localize() + "...";
foreach (string driverPath in ActiveSliceSettings.Instance.PrinterDrivers())
2015-04-08 15:20:10 -07:00
{
InstallDriver(driverPath);
}
2016-04-18 11:31:31 -07:00
2015-04-08 15:20:10 -07:00
return true;
}
catch (Exception)
{
2016-04-18 11:31:31 -07:00
printerDriverMessage.Text = "Sorry, we were unable to install the driver.".Localize();
2015-04-08 15:20:10 -07:00
return false;
}
}
}
}