/// AppOnStartup #region AppOnStartup /// <summary>The event occurs on startup.</summary> /// <param name="sender">The sender.</param> /// <param name="e">The event args</param> private void AppOnStartup(object sender, StartupEventArgs e) { mutex = new Mutex(true, UniqueMutexName, out isOwned); GC.KeepAlive(mutex); // check if mutex is owned if (isOwned) { // if mutex is owned then no other instances are active // create all the necessary data and run the app LogHelper.Init(ConfigData.FilePath_AppUsageLog); LogHelper.Log("Started " + System.Windows.Forms.Application.ProductVersion); ConfigData.Init(); AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; System.Windows.Forms.Application.ThreadException += Application_ThreadException; ProcessArgs(e.Args); MainController.Singleton.Run(); return; } // Application already run WPFNotifier.Warning("Application already run!"); // Terminate this instance. Shutdown(); }
/// timerAllowedToPrint #region timerAllowedToPrint /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void timerAllowedToPrint_Tick(object sender, EventArgs e) { timerAllowedToPrint.Stop(); if (!ConfigData.Config_DontCheckUserBalance) { AllowedToPrintResponse res = APIWrapper.CheckIfCanPrint(Environment.MachineName, Environment.UserName, PrintWithColor, NumberOfPrints, ChosenPrinter.Name); if (res == null) { WPFNotifier.Warning(string.Format("No response from server but still printing.")); Print(PDFFileName, PostScriptMetaData.Title, ChosenPrinter.Settings, PostScriptMetaData); } else { if (res.AllowedToPrint) { Print(PDFFileName, PostScriptMetaData.Title, ChosenPrinter.Settings, PostScriptMetaData); } else { timerAllowedToPrint.Start(); } } } timerAllowedToPrint.Start(); }
/// <summary> /// Changes list of printers /// </summary> public void ChangePrinters() { LogHelper.LogDebug(); try { bool nocolored = false; CanPrintWithColor = true; List <string> printers = null; if (PrintWithColor) { printers = MainController.Singleton.AvailablePrinters.ColoredNames; if (printers == null || printers.Count < 1) { nocolored = true; } } else { printers = MainController.Singleton.AvailablePrinters.AllNames; } if (nocolored) { LogHelper.LogDebug("No available color printers found. You are not allowed to print colored this time."); WPFNotifier.Warning("No available color printers found. You are not allowed to print colored this time."); CanPrintWithColor = false; return; } if (printers == null || printers.Count < 1) { LogHelper.LogDebug("No available printers found. You are not allowed to print this time."); WPFNotifier.Warning("No available printers found. You are not allowed to print this time."); return; } LogHelper.LogDebug("Add printers to combo : " + printers.Count); MainController.Singleton.Dispatcher.Invoke(new Action(() => { Printers.Clear(); foreach (string name in printers.ToArray()) { Printers.Add(name); } }), DispatcherPriority.Background); Thread.Sleep(100); SetDefaultPrinter(); } catch (Exception ex) { WPFNotifier.Error("This printer is not available now. Try to use one of the other printers.", null, ex); Close(); } }
/// <summary> /// Tries to print first time /// </summary> /// <param name="fileName"></param> /// <param name="printerName"></param> /// <param name="printerSettings"></param> void PrintFirstTry(string fileName, string documentName, PrinterSettings printerSettings) { // check if it's Demo mode if (IsTestMode) { return; } if (ConfigData.Config_DontCheckUserBalance) { Print(fileName, documentName, printerSettings, PostScriptMetaData); } else { SetStatus("Checking user balance"); var res = APIWrapper.CheckIfCanPrint(Environment.MachineName, Environment.UserName, printerSettings.DefaultPageSettings.Color, NumberOfPrints, ChosenPrinter.Name); if (res == null) { WPFNotifier.Warning(string.Format("No response from server but still printing.")); Print(fileName, documentName, printerSettings, PostScriptMetaData); } else { if (res.Result) { if (res.AllowedToPrint) { Print(fileName, documentName, printerSettings, PostScriptMetaData); } else { //WPFNotifier.Warning(string.Format("You are not allowed to print in the current time.{0}Reason: {1}", Environment.NewLine, res.Reason)); SetStatus("Not allowed to print"); if (WPFNotifier.Question(string.Format("You are not allowed to print at the current time.{0}Reason: {1}{0}{2}", Environment.NewLine, res.Reason, "Do you want to leave the printing (Yes) or continue waiting (No)?")) == MessageBoxResult.Yes) { Close(); } else { StartTimerAllowedToPrint(); } } } else { WPFNotifier.Warning(string.Format("Not successful request CheckIfCanPrint.")); } } } }
/// <summary> /// Loads list of printers /// </summary> private bool UpdatePrinters() { LogHelper.LogDebug(); MainController.Singleton.UpdateAvailablePrinters(); if (!MainController.Singleton.HasAvailablePrinters) { WPFNotifier.Warning("This printer is not available now. Try to use one of the other printers."); Close(); return(false); } return(true); }
/// <summary> /// Tries to resume the print job first time /// </summary> /// <param name="fileName"></param> /// <param name="printJob"></param> void ResumeJobFirstTry(PrintJobData printJob) { if (ConfigData.Config_DontCheckUserBalance) { ResumeJob(printJob); } else { SetStatus("Checking user balance"); var res = APIWrapper.CheckIfCanPrint(Environment.MachineName, Environment.UserName, printJob.Color, NumberOfPrints, printJob.PrintJobTitle.PrinterName); if (res == null) { WPFNotifier.Warning(string.Format("No response from server but still printing.")); ResumeJob(printJob); } else { if (res.Result) { if (res.AllowedToPrint) { ResumeJob(printJob); } else { SetStatus("Not allowed to print"); if (WPFNotifier.Question(string.Format("You are not allowed to print at the current time.{0}Reason: {1}{0}{2}", Environment.NewLine, res.Reason, "Do you want to leave the printing (Yes) or continue waiting (No)?")) == MessageBoxResult.Yes) { CancelJob(printJob); } else { StartTimerAllowedToPrint(); } } } else { WPFNotifier.Warning(string.Format("Not successful request CheckIfCanPrint.")); } } } }
/// <summary> /// Update data by DEVMODE structure. /// </summary> private void UpdateDataDevMode() { try { // get job info var jobInfo2 = CubiclesPrinterLib.Win32.Print.PrintJob.GetJobInfo(PrintJobData.RemotePrinterName, PrintJobData.PrintJobTitle.JobID); if (jobInfo2 != null) { NumberOfCopies = (short)jobInfo2.dmOut.dmCopies; PrintDuplex = (Duplex)jobInfo2.dmOut.dmDuplex; } else { WPFNotifier.Warning(PrintJobData.RemotePrinterName + Environment.NewLine + " Can't access printer, data corrupted, default values set."); } } catch (Exception ex) { WPFNotifier.Error(ex); } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void LocalPrintEventWatcherPrintJobCancelled(object sender, PrintJobDataEventArgs e) { // check the event args if (e != null && e.Data != null) { LogHelper.LogDebug("Cancelled WPF! " + e.Data.PrintJobTitle.MegaToString()); // check the watchers and remove the watcher for the specified data if (_watchers != null && _watchers.Count > 0) { _watchers.RemoveWatcher(e.Data); } // check if able to show job cancelled message if (!cancelledShowing) { // show only one message about cancelling the job cancelledShowing = true; WPFNotifier.Warning(string.Format("You are not allowed to choose this printer.{0}You have to use {1} or {2} instead.{0}Printing cancelled.", Environment.NewLine, ConfigData.PrinterName, ConfigData.PrinterName2)); cancelledShowing = false; } } }