2014-07-02 11:43:06 -07:00
/ *
Copyright ( c ) 2014 , Kevin Pope
All rights reserved .
Redistribution and use in source and binary forms , with or without
2015-04-08 15:20:10 -07:00
modification , are permitted provided that the following conditions are met :
2014-07-02 11:43:06 -07:00
1. Redistributions of source code must retain the above copyright notice , this
2015-04-08 15:20:10 -07:00
list of conditions and the following disclaimer .
2014-07-02 11:43:06 -07:00
2. Redistributions in binary form must reproduce the above copyright notice ,
this list of conditions and the following disclaimer in the documentation
2015-04-08 15:20:10 -07:00
and / or other materials provided with the distribution .
2014-07-02 11:43:06 -07:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES
( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ;
LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
The views and conclusions contained in the software and documentation are those
2015-04-08 15:20:10 -07:00
of the authors and should not be interpreted as representing official policies ,
2014-07-02 11:43:06 -07:00
either expressed or implied , of the FreeBSD Project .
* /
using MatterHackers.Agg ;
2016-04-14 18:01:45 -07:00
using MatterHackers.Agg.UI ;
using MatterHackers.Localizations ;
2016-02-23 11:15:03 -08:00
using MatterHackers.MatterControl.DataStorage ;
2016-04-14 14:34:30 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
2016-02-23 09:32:26 -08:00
using MatterHackers.MatterControl.PrintQueue ;
2016-04-20 11:53:08 -07:00
using MatterHackers.MatterControl.SlicerConfiguration ;
2016-04-14 14:34:30 -07:00
using System ;
2015-04-08 15:20:10 -07:00
using System.Collections.Generic ;
using System.IO ;
2014-07-02 11:43:06 -07:00
namespace MatterHackers.MatterControl.PrintHistory
{
2016-04-14 18:01:45 -07:00
public static class ResumePrinting
{
static string resumePrint = "Resume Print" . Localize ( ) ;
static string cancelResume = "Cancel" . Localize ( ) ;
static string resumeFailedPrintMessage = "It appears your last print failed to complete.\n\nWould your like to attempt to resume from the last know position?" . Localize ( ) ;
static string resumeFailedPrintTitle = "Resume Last Print" . Localize ( ) ;
static PrintTask lastPrintTask ;
public static void CheckIfNeedToResumePrint ( 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
2016-04-20 11:53:08 -07:00
& & File . Exists ( lastPrint . PrintingGCodeFileName ) // PrintingGCodeFileName is still on disk
& & lastPrint . PercentDone > 0 // we are actually part way into the print
2016-07-12 17:46:48 -07:00
& & ActiveSliceSettings . Instance . GetValue ( SettingsKey . has_hardware_leveling ) = = "0" )
2016-04-20 11:53:08 -07:00
{
2016-04-14 18:01:45 -07:00
lastPrintTask = lastPrint ;
StyledMessageBox . ShowMessageBox ( ResumeFailedPrintProcessDialogResponse , resumeFailedPrintMessage , resumeFailedPrintTitle , StyledMessageBox . MessageType . YES_NO , resumePrint , cancelResume ) ;
}
}
}
private static void ResumeFailedPrintProcessDialogResponse ( bool messageBoxResponse )
{
if ( messageBoxResponse )
{
UiThread . RunOnIdle ( ( ) = >
{
if ( PrinterConnectionAndCommunication . Instance . CommunicationState = = PrinterConnectionAndCommunication . CommunicationStates . Connected )
{
PrinterConnectionAndCommunication . Instance . CommunicationState = PrinterConnectionAndCommunication . CommunicationStates . PreparingToPrint ;
PrinterConnectionAndCommunication . Instance . StartPrint ( lastPrintTask . PrintingGCodeFileName , lastPrintTask ) ;
}
} ) ;
}
else // the resume has been canceled
{
lastPrintTask . PrintingGCodeFileName = null ;
lastPrintTask . Commit ( ) ;
}
}
}
2015-04-08 15:20:10 -07:00
public class PrintHistoryData
{
2016-02-23 09:32:26 -08:00
public static readonly int RecordLimit = 20 ;
2015-02-20 12:05:44 -08:00
public RootedObjectEventHandler HistoryCleared = new RootedObjectEventHandler ( ) ;
2016-02-23 09:32:26 -08:00
public bool ShowTimestamp ;
private static PrintHistoryData instance ;
2015-02-20 12:05:44 -08:00
2016-04-14 14:34:30 -07:00
private static event EventHandler unregisterEvents ;
2015-04-08 15:20:10 -07:00
public static PrintHistoryData Instance
{
get
{
if ( instance = = null )
{
instance = new PrintHistoryData ( ) ;
2016-04-14 18:01:45 -07:00
PrinterConnectionAndCommunication . Instance . ConnectionSucceeded . RegisterEvent ( ResumePrinting . CheckIfNeedToResumePrint , ref unregisterEvents ) ;
2016-04-14 14:34:30 -07:00
}
2015-04-08 15:20:10 -07:00
return instance ;
}
}
2014-07-02 11:43:06 -07:00
2016-02-23 09:32:26 -08:00
public IEnumerable < DataStorage . PrintTask > GetHistoryItems ( int recordCount )
2015-04-08 15:20:10 -07:00
{
string query ;
if ( UserSettings . Instance . get ( "PrintHistoryFilterShowCompleted" ) = = "true" )
{
query = string . Format ( "SELECT * FROM PrintTask WHERE PrintComplete = 1 ORDER BY PrintStart DESC LIMIT {0};" , recordCount ) ;
}
else
{
query = string . Format ( "SELECT * FROM PrintTask ORDER BY PrintStart DESC LIMIT {0};" , recordCount ) ;
}
2016-02-23 11:15:03 -08:00
return Datastore . Instance . dbSQLite . Query < PrintTask > ( query ) ;
2015-04-08 15:20:10 -07:00
}
2015-02-20 12:05:44 -08:00
internal void ClearHistory ( )
{
2016-02-23 11:15:03 -08:00
Datastore . Instance . dbSQLite . ExecuteScalar < PrintTask > ( "DELETE FROM PrintTask;" ) ;
2015-02-20 12:05:44 -08:00
HistoryCleared . CallEvents ( this , null ) ;
}
}
2014-07-02 11:43:06 -07:00
}