using Microsoft.Win32; using Neo.LegitimateLicences.ActiveX.Printing; using Neo.LegitimateLicences.ActiveX.Printing.OperatingLicence; //using Neo.LegitimateLicences.ActiveX.Printing; //using Neo.LegitimateLicences.ActiveX.Printing.OperatingLicence; using Neo.LegitimateLicences.Desktop.Helpers; using Neo.LegitimateLicences.Desktop.Scan; using Neo.LegitimateLicences.Helpers; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using System.Net.Http; using System.Reflection; using System.Diagnostics; using System.IO; namespace Neo.LegitimateLicences.Desktop { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { Assembly assembly = Assembly.GetExecutingAssembly(); FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location); Version myVersion = new Version(fvi.ProductVersion); if (args.Length == 0) { MessageBox.Show("No argument specified." + Environment.NewLine + Environment.NewLine + "Current Version: " + myVersion); return; } args[0] = args[0].ToLower(); if (args[0] == "registerprotocol") { #region Register custom protocol RegisterMyProtocol(AppDomain.CurrentDomain.BaseDirectory + @"\Neo.LegitimateLicences.Desktop.exe"); return; #endregion } else if (args[0].Contains("legitimatedesktop")) { var argumentString = args[0].Replace("legitimatedesktop:", ""); var arguments = argumentString.Split('_'); #region Validate the arguments if (arguments.Length != 3) { MessageBox.Show("Invalid argument format." + args[0]); return; } if (arguments[0] != "print" && arguments[0] != "scan") { MessageBox.Show("Invalid argument specified: " + args[0]); return; } #endregion var dataToken = arguments[2]; if (arguments[0] == "print") { #region Handle Printing var apiHelper = new LegitimateLicencesApiHelper(ConfigurationManager.AppSettings["ApiKey"], arguments[1], ConfigurationManager.AppSettings["Environment"]); var response = apiHelper.GetPrintoutData(dataToken); Version latestVersion = new Version(response.desktopAppVersion.ToString()); bool islatestVersion = true; islatestVersion = CheckVersion(latestVersion); if (islatestVersion) { if (response.printout.PrintoutTypeID == 1 /*Receipt*/) { var receipt = response.data; var receiptPrinter = new AppReceiptPrinter( JsonHelper.GetValidString(receipt.Division), JsonHelper.GetValidString(receipt.ReceiptNumber), JsonHelper.GetValidString(receipt.ReceiptHeading), JsonHelper.GetValidString(receipt.CreatedBy), JsonHelper.GetValidString(receipt.PaymentType), JsonHelper.GetValidString(receipt.ApplicantData), JsonHelper.GetValidString(receipt.ReceiptItems), JsonHelper.GetValidString(receipt.Province), JsonHelper.GetValidLong(receipt.ReceiptCost), "", JsonHelper.GetValidString(response.printout.PrintedByName), 1, JsonHelper.GetValidString(response.printout.PrinterName), 297, 210, Convert.ToBoolean(response.printout.EnablePrintPreview), Convert.ToBoolean(response.printout.IsReprint) ); receiptPrinter.Print(); } else if (response.printout.PrintoutTypeID == 2 /*Operating Licence*/) { var ol = response.data; var passengers = JsonHelper.GetValidString(ol.SeatedCapacity); if (JsonHelper.GetValidString(ol.StandingCapacity) != "") { passengers = passengers + ", " + JsonHelper.GetValidString(ol.StandingCapacity); } //#region Create printer object var olPrinter = new OperatingLicencePrinter( JsonHelper.GetValidString(ol.AssociationName), JsonHelper.GetValidString(ol.AccreditationNumber), "", JsonHelper.GetValidString(ol.OperatingLicenceNumber), JsonHelper.GetValidString(ol.ApplicationNumber), JsonHelper.GetValidString(ol.IDNumberOrBusinessRegistrationNo), JsonHelper.GetValidString(ol.NameOnLicence), JsonHelper.GetValidString(ol.PostalAddress), JsonHelper.GetValidString(ol.RegistrationNumber), JsonHelper.GetValidString(ol.GrossMass), JsonHelper.GetValidString(ol.EngineNumber), JsonHelper.GetValidString(ol.VIN), JsonHelper.GetValidString(ol.VehicleMake), passengers, JsonHelper.GetValidString(ol.VehicleType), JsonHelper.GetValidString(ol.CrossBorder), JsonHelper.GetValidString(ol.DateOfIssue), JsonHelper.GetValidString(ol.DateOfExpiry), JsonHelper.GetValidString(ol.LicensingBoard), JsonHelper.GetValidString(ol.CertificateNumber), JsonHelper.GetValidString(ol.YearOfFirstReg), JsonHelper.GetValidString(response.printout.PrintedByName), JsonHelper.GetValidString(response.printout.DatePrinted), 1, JsonHelper.GetValidString(response.printout.PrinterName), 297, 210, Convert.ToBoolean(response.printout.EnablePrintPreview), Convert.ToInt16(ol.ApplicationRequestTypeID), Convert.ToBoolean(response.printout.IsTestPrint) ); olPrinter.Print(); } else if (response.printout.PrintoutTypeID == 4 /*Accreditation Certificate*/) { var acc = response.data; #region Create printer object #region Issue Date DateTime? issueDate = JsonHelper.GetValidDate(acc.DateOfIssue); var issueDateString = ""; if (issueDate.HasValue) { issueDateString = issueDate.Value.ToString("dd MMMM yyyy"); } #endregion #region Expiry Date DateTime? expiryDate = JsonHelper.GetValidDate(acc.DateOfExpiry); var expiryDateString = ""; if (expiryDate.HasValue) { expiryDateString = expiryDate.Value.ToString("dd MMMM yyyy"); } #endregion var acPrinter = new AccreditationCertificatePrinter( JsonHelper.GetValidString(acc.AccreditationNumber), JsonHelper.GetValidString(acc.Member.FullName), JsonHelper.GetValidString(acc.Member.IDNumberOrBusinessRegistrationNo), issueDateString, expiryDateString, JsonHelper.GetValidString(acc.CertificateNumber), JsonHelper.GetValidString(response.printout.PrintedByName), JsonHelper.GetValidString(response.printout.DatePrinted), 1, 297, 210, JsonHelper.GetValidString(response.printout.PrinterName), Convert.ToBoolean(response.printout.EnablePrintPreview), Convert.ToBoolean(response.printout.IsTestPrint) ); #endregion acPrinter.Print(); } else if (response.printout.PrintoutTypeID == 5 /*Accreditated Licence Decal*/) { var ol = response.data; //#region Create printer object var decalPrinter = new AccreditationDecalPrinter( JsonHelper.GetValidString(ol.AccreditationNumber), JsonHelper.GetValidString(ol.OperatingLicenceNumber), JsonHelper.GetValidString(ol.VIN), JsonHelper.GetValidString(ol.EngineNumber), JsonHelper.GetValidString(ol.RegistrationNumber), JsonHelper.GetValidString(ol.GrossMass), "", //tare JsonHelper.GetValidString(ol.VehicleMake), JsonHelper.GetValidString(ol.VehicleType), JsonHelper.GetValidString(ol.SeatedCapacity), JsonHelper.GetValidString(ol.StandingCapacity), dataToken, Convert.ToDateTime(ol.DateOfExpiry), JsonHelper.GetValidString(response.printout.PrinterName), "", "", 1, JsonHelper.GetValidString(response.printout.PrinterName), Convert.ToDateTime(response.printout.DatePrinted), Convert.ToBoolean(response.printout.EnablePrintPreview), Convert.ToBoolean(response.printout.IsTestPrint) ); decalPrinter.Print(); } else if (response.printout.PrintoutTypeID == 6 /*Temporary Replacement Licence*/) { var tl = response.data; //#region Create printer object var decalPrinter = new TemporaryReplacementLicencePrinter( JsonHelper.GetValidString(tl.ExistingLicenceNumber).ToString(), JsonHelper.GetValidString(tl.ExistingRegistration), JsonHelper.GetValidString(tl.IDNumberOrBusinessRegistrationNo), JsonHelper.GetValidString(tl.Member), JsonHelper.GetValidString(tl.Mode), JsonHelper.GetValidString(tl.PostalAddress), JsonHelper.GetValidString(tl.PostalAddressCode), JsonHelper.GetValidString(tl.ReplacementDateFrom), JsonHelper.GetValidString(tl.ReplacementDateTo), JsonHelper.GetValidString(tl.TransactionType), JsonHelper.GetValidString(tl.ReplacementRegistrationNumber), 1, JsonHelper.GetValidString(response.printout.PrinterName), JsonHelper.GetValidString(response.printout.PrintedByName), Convert.ToDateTime(response.printout.DatePrinted).ToString(), 297, 210, Convert.ToBoolean(response.printout.EnablePrintPreview), Convert.ToBoolean(response.printout.IsTestPrint) ); decalPrinter.Print(); } } else { try { string filename = Path.Combine(Application.StartupPath, "Neo.LegitimateLicences.DesktopUpdater.exe"); var proc = System.Diagnostics.Process.Start(filename, args[0]+"_"+ latestVersion.ToString()); return; } catch(Exception ax) { MessageBox.Show("An error occured : " + Environment.NewLine + Environment.NewLine + ax.Message); return; } } #endregion } else if (arguments[0] == "scan") { var apiHelper = new LegitimateLicencesApiHelper(ConfigurationManager.AppSettings["ApiKey"], arguments[1], ConfigurationManager.AppSettings["Environment"]); var response = apiHelper.GetScanData(arguments[2]); var documents = response.scanSession.Documents; Version latestVersion = new Version(response.desktopAppVersion.ToString()); bool islatestVersion = true; islatestVersion = CheckVersion(latestVersion); if(islatestVersion) { if (documents.Count == 1) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var singleScanPage = new SingleScan(); singleScanPage.Document = documents[0]; singleScanPage.UserEmail = arguments[1]; Application.Run(singleScanPage); } } else { try { string filename = Path.Combine(Application.StartupPath, "Neo.LegitimateLicences.DesktopUpdater.exe"); var proc = System.Diagnostics.Process.Start(filename, args[0] + "_" + latestVersion.ToString()); return; } catch (Exception ax) { MessageBox.Show("An error occured : " + Environment.NewLine + Environment.NewLine + ax.Message); return; } } } } else { MessageBox.Show("Invalid argument specified: " + args[0]); return; } return; } #region RegisterMyProtocol static void RegisterMyProtocol(string myAppPath) //myAppPath = full path to your application { RegistryKey key = Registry.ClassesRoot.OpenSubKey("LegitimateDesktop"); //open myApp protocol's subkey if (key == null) //if the protocol is not registered yet...we register it { key = Registry.ClassesRoot.CreateSubKey("LegitimateDesktop"); key.SetValue(string.Empty, "URL: LegitimateDesktop Protocol"); key.SetValue("URL Protocol", string.Empty); key = key.CreateSubKey(@"shell\open\command"); key.SetValue(string.Empty, myAppPath + " " + "%1"); //%1 represents the argument - this tells windows to open this program with an argument / parameter } key.Close(); } #endregion #region CheckVersion static bool CheckVersion(Version latestVersion) { bool islatestVersion = true; //get my own version to compare against latest. Assembly assembly = Assembly.GetExecutingAssembly(); FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location); Version myVersion = new Version(fvi.ProductVersion); if (latestVersion > myVersion) { islatestVersion = false; } return islatestVersion; } #endregion } }