using Microsoft.Win32; using Neo.LegitimateLicences.ActiveX.Printing; using Neo.LegitimateLicences.Desktop.Helpers; using Neo.LegitimateLicences.Desktop.Scan; using Neo.LegitimateLicences.Helpers; using System; using System.Configuration; using System.Windows.Forms; using System.Reflection; using System.Diagnostics; using System.IO; namespace Neo.LegitimateLicences.Desktop { static class Program { /// /// The main entry point for the application. /// [STAThread] public 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(); //args = new string[] { "legitimatedesktop:print_gavin@softedgesolutions.co.za_6C0D491067424611" }; 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); if (response == null || response.printout == null) { MessageBox.Show("Unable to load print data for token. Please try again."); return; } Version latestVersion = new Version(response.desktopAppVersion.ToString()); bool 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.GetValidDouble(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.DatePrinted), 1, JsonHelper.GetValidString(response.printout.PrinterName), 297, 210, Convert.ToBoolean(response.printout.EnablePrintPreview), Convert.ToInt16(ol.ApplicationRequestTypeID), Convert.ToBoolean(response.printout.IsTestPrint), (short)ol.DivisionTypeID ); 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, "https://verify.nltis.transport.gov.za/", Convert.ToDateTime(ol.DateOfExpiry), JsonHelper.GetValidString(ol.NameOnLicence), "", "", 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 if (response.printout.PrintoutTypeID == 8 /*Provider Certificate*/) { var pc = response.data; // Safe converters for provider dynamic/dictionary values string S(object v) { return v == null ? "" : Convert.ToString(v); } DateTime? D(object v) { DateTime dt; return DateTime.TryParse(S(v), out dt) ? (DateTime?)dt : null; } long L(object v) { long x; return long.TryParse(S(v), out x) ? x : 0L; } //#region Create printer object var pcPrinter = new ProviderCertificatePrinter( S(pc.PlatformProviderID), S(pc.LegalName), S(pc.TradingName), S(pc.BusinessRegNumber), S(pc.RegisteredAddressLine1), S(pc.RegisteredAddressLine2), S(pc.RegisteredAddressSuburb), S(pc.RegisteredAddressCity), S(pc.RegisteredAddressProvinceID), S(pc.RegisteredAddressPostalCode), S(pc.CertificateNumber), S(pc.IssueNumber), S(pc.ReceiptNumber), S(pc.IssuedAt), S(pc.ExpiresAt), 1, JsonHelper.GetValidString(response.printout.PrinterName), 297, 210, Convert.ToBoolean(response.printout.EnablePrintPreview), Convert.ToBoolean(response.printout.IsTestPrint) ); pcPrinter.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) { Assembly assembly = Assembly.GetExecutingAssembly(); FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location); Version myVersion = new Version(fvi.ProductVersion); return myVersion >= latestVersion; } #endregion } }