public PrintHistoryListItem(PrintTask printTask, bool showTimestamp)
 {            
     this.printTask = printTask;
     this.showTimestamp = showTimestamp;
     SetDisplayAttributes();
     AddChildElements();
     AddHandlers();
 }
        public static void CheckIfNeedToRecoverPrint(object sender, EventArgs e)
		{
			foreach (PrintTask lastPrint in PrintHistoryData.Instance.GetHistoryItems(1))
			{
				if (!lastPrint.PrintComplete // Top Print History Item is not complete
				&& !string.IsNullOrEmpty(lastPrint.PrintingGCodeFileName) // PrintingGCodeFileName is set
				&& File.Exists(lastPrint.PrintingGCodeFileName) // PrintingGCodeFileName is still on disk
				&& lastPrint.PercentDone > 0 // we are actually part way into the print
				&& ActiveSliceSettings.Instance.GetValue(SettingsKey.has_hardware_leveling) == "0")
                {
					lastPrintTask = lastPrint;
                    StyledMessageBox.ShowMessageBox(RecoverPrintProcessDialogResponse, printRecoveryPrintMessage, recoverPrintTitle, StyledMessageBox.MessageType.YES_NO, recoverPrint, cancelRecovery);
				}
			}
		}
		private void loadGCodeWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
			switch (communicationState)
			{
				case CommunicationStates.Connected:
					// This can happen if the printer is reset during the slicing of the part.
					break;

				case CommunicationStates.PreparingToPrint:
					if (ActivePrintItem.PrintItem.Id == 0)
					{
						ActivePrintItem.PrintItem.Commit();
					}

					activePrintTask = new PrintTask();
					activePrintTask.PrintStart = DateTime.Now;
					activePrintTask.PrinterId = ActivePrinterProfile.Instance.ActivePrinter.Id;
					activePrintTask.PrintName = ActivePrintItem.PrintItem.Name;
					activePrintTask.PrintItemId = ActivePrintItem.PrintItem.Id;
					activePrintTask.PrintComplete = false;
					activePrintTask.Commit();

					CommunicationState = CommunicationStates.Printing;
					break;

				default:
#if DEBUG
					throw new Exception("We are not preparing to print so we should not be starting to print");
					//#else
					CommunicationState = CommunicationStates.Connected;
#endif
					break;
			}
		}
		private void DoneLoadingGCodeToPrint()
		{
			switch (communicationState)
			{
				case CommunicationStates.Connected:
					// This can happen if the printer is reset during the slicing of the part.
					break;

				case CommunicationStates.PreparingToPrint:
					if (ActivePrintItem.PrintItem.Id == 0)
					{
						ActivePrintItem.PrintItem.Commit();
					}

					if (activePrintTask == null)
					{
						// TODO: Fix printerItemID int requirement
						activePrintTask = new PrintTask();
						activePrintTask.PrintStart = DateTime.Now;
						activePrintTask.PrinterId = this.ActivePrinter.ID.GetHashCode();
						activePrintTask.PrintName = ActivePrintItem.PrintItem.Name;
						activePrintTask.PrintItemId = ActivePrintItem.PrintItem.Id;
						activePrintTask.PrintingGCodeFileName = ActivePrintItem.GetGCodePathAndFileName();
						activePrintTask.PrintComplete = false;

						activePrintTask.Commit();
					}

					CommunicationState = CommunicationStates.Printing;
					break;

				default:
#if DEBUG
					throw new Exception("We are not preparing to print so we should not be starting to print");
					//#else
					CommunicationState = CommunicationStates.Connected;
#endif
					break;
			}
		}
		private void CancelPrint(bool markPrintCanceled)
		{
			lock (locker)
			{
				// get rid of all the gcode we have left to print
				ClearQueuedGCode();
				string cancelGCode = ActiveSliceSettings.Instance.GetValue(SettingsKey.cancel_gcode);
				if (cancelGCode.Trim() != "")
				{
					// add any gcode we want to print while canceling
					InjectGCode(cancelGCode);
				}
				// let the process know we canceled not ended normally.
				printWasCanceled = true;
				if (markPrintCanceled
					&& activePrintTask != null)
				{
					TimeSpan printTimeSpan = DateTime.Now.Subtract(activePrintTask.PrintStart);

					activePrintTask.PrintEnd = DateTime.Now;
					activePrintTask.PrintComplete = false;
					activePrintTask.PrintingGCodeFileName = "";
					activePrintTask.Commit();
				}

				// no matter what we no longer have a print task
				activePrintTask = null;
			}
		}
		public async void StartPrint(string gcodeFilename, PrintTask printTaskToUse = null)
		{
			if (!PrinterIsConnected || PrinterIsPrinting)
			{
				return;
			}

			haveReportedError = false;
			printWasCanceled = false;
			ExtrusionRatio = 1;
			FeedRateRatio = 1;
			waitingForPosition.Stop();
			waitingForPosition.Reset();

			ClearQueuedGCode();
			activePrintTask = printTaskToUse;

			await Task.Run(() =>
			{
				LoadGCodeToPrint(gcodeFilename);
			});
			DoneLoadingGCodeToPrint();
		}
        public bool StartPrint(string gcodeFileContents)
        {
            if (!PrinterIsConnected || PrinterIsPrinting)
            {
                return false;
            }

            gcodeFileContents = gcodeFileContents.Replace("\r\n", "\n");
            gcodeFileContents = gcodeFileContents.Replace('\r', '\n');
            string[] gcodeLines = gcodeFileContents.Split('\n');
            List<string> printableGCode = new List<string>(gcodeLines.Length);
            foreach (string line in gcodeLines)
            {
                printableGCode.Add(line);
            }

            ExtrusionRatio = 1;
            FeedRateRatio = 1;

            switch(communicationState)
            {
                case CommunicationStates.PreparingToPrintToSd:
                    activePrintTask = null;
                    CommunicationState = CommunicationStates.PrintingToSd;
                    break;

                case CommunicationStates.PreparingToPrint:
                    if (ActivePrintItem.PrintItem.Id == 0)
                    {
                        ActivePrintItem.PrintItem.Commit();
                    }

                    activePrintTask = new PrintTask();
                    activePrintTask.PrintStart = DateTime.Now;
                    activePrintTask.PrinterId = ActivePrinterProfile.Instance.ActivePrinter.Id;
                    activePrintTask.PrintName = ActivePrintItem.PrintItem.Name;
                    activePrintTask.PrintItemId = ActivePrintItem.PrintItem.Id;
                    activePrintTask.PrintComplete = false;
                    activePrintTask.Commit();

                    CommunicationState = CommunicationStates.Printing;
                    break;

                default:
                    throw new NotFiniteNumberException();
            }

            ClearQueuedGCode();
            loadedGCode = GCodeFile.ParseGCodeString(string.Join("\n", printableGCode.ToArray()));

            if (printableGCode.Count == 0)
            {
                return true;
            }

            sendGCodeToPrinterThread = new Thread(SendCurrentGCodeFileToPrinter);
            sendGCodeToPrinterThread.Name = "sendGCodeToPrinterThread - StartPrint";
            sendGCodeToPrinterThread.IsBackground = true;
            sendGCodeToPrinterThread.Start();

            return true;
        }