2014-03-17 14:41:26 -07:00
/ *
2015-12-10 15:56:25 -08:00
Copyright ( c ) 2015 , Lars Brubaker
2014-03-17 14:41:26 -07:00
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-03-17 14:41:26 -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-03-17 14:41:26 -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-03-17 14:41:26 -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-03-17 14:41:26 -07:00
either expressed or implied , of the FreeBSD Project .
* /
2015-12-07 16:33:29 -08:00
using MatterHackers.Agg ;
2016-07-21 15:13:10 -07:00
using MatterHackers.Agg.PlatformAbstract ;
2014-03-17 14:41:26 -07:00
using MatterHackers.Agg.UI ;
2016-07-06 16:40:23 -07:00
using MatterHackers.Localizations ;
2016-07-21 15:13:10 -07:00
using MatterHackers.MatterControl.DataStorage ;
2016-07-06 16:40:23 -07:00
using MatterHackers.MatterControl.PrinterCommunication ;
2015-07-13 12:49:01 -07:00
using MatterHackers.MatterControl.PrintLibrary ;
using MatterHackers.MatterControl.PrintLibrary.Provider ;
2014-03-17 14:41:26 -07:00
using MatterHackers.MatterControl.PrintQueue ;
using MatterHackers.MatterControl.SlicerConfiguration ;
2015-04-08 15:20:10 -07:00
using MatterHackers.VectorMath ;
2016-07-21 15:13:10 -07:00
using Newtonsoft.Json ;
2015-04-08 15:20:10 -07:00
using System ;
2016-07-12 17:23:45 -07:00
using System.Collections.Generic ;
2015-02-20 12:05:44 -08:00
using System.Diagnostics ;
2016-07-21 15:13:10 -07:00
using System.IO ;
2016-04-18 11:31:31 -07:00
using System.Linq ;
2015-12-08 12:02:44 -08:00
using System.Threading.Tasks ;
2014-03-17 14:41:26 -07:00
namespace MatterHackers.MatterControl
{
2016-08-10 15:40:09 -07:00
using Agg.Font ;
2016-08-29 15:29:43 -07:00
using System.Reflection ;
2016-09-01 17:24:09 -07:00
public class OemProfileDictionary : Dictionary < string , Dictionary < string , PublicDevice > >
{
}
public class PublicDevice
{
public string DeviceToken { get ; set ; }
public string ProfileToken { get ; set ; }
public string ShortProfileID { get ; set ; }
public string CacheKey = > this . ShortProfileID + ProfileManager . ProfileExtension ;
}
2016-07-26 12:54:16 -07:00
2015-12-22 16:44:54 -08:00
public abstract class ApplicationView : GuiWidget
2015-04-08 15:20:10 -07:00
{
2014-10-17 14:11:56 -07:00
public abstract void AddElements ( ) ;
2015-04-08 15:20:10 -07:00
}
2016-05-03 18:22:59 -07:00
public class TouchscreenView : ApplicationView
2015-04-08 15:20:10 -07:00
{
2016-05-03 18:22:59 -07:00
private FlowLayoutWidget TopContainer ;
2016-05-07 20:49:46 -07:00
private TouchscreenTabView touchscreenTabView ;
2015-04-08 15:20:10 -07:00
private QueueDataView queueDataView ;
private GuiWidget menuSeparator ;
private PrintProgressBar progressBar ;
2016-04-18 11:31:31 -07:00
private bool topIsHidden = false ;
2015-04-08 15:20:10 -07:00
2016-05-03 18:22:59 -07:00
public TouchscreenView ( )
2015-04-08 15:20:10 -07:00
{
AddElements ( ) ;
2016-04-18 11:31:31 -07:00
this . AnchorAll ( ) ;
}
2015-04-08 15:20:10 -07:00
2016-05-03 18:22:59 -07:00
public void ToggleTopContainer ( )
2014-10-17 14:11:56 -07:00
{
2014-10-27 19:54:01 -07:00
topIsHidden = ! topIsHidden ;
progressBar . WidgetIsExtended = ! progressBar . WidgetIsExtended ;
//To do - Animate this (KP)
2014-10-17 14:11:56 -07:00
this . menuSeparator . Visible = this . TopContainer . Visible ;
this . TopContainer . Visible = ! this . TopContainer . Visible ;
}
2015-04-08 15:20:10 -07:00
public override void AddElements ( )
{
2014-11-03 10:46:09 -08:00
topIsHidden = false ;
this . BackgroundColor = ActiveTheme . Instance . PrimaryBackgroundColor ;
2014-04-09 18:45:29 -07:00
2015-04-08 15:20:10 -07:00
FlowLayoutWidget container = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
container . AnchorAll ( ) ;
2014-04-09 18:45:29 -07:00
2016-04-18 11:31:31 -07:00
TopContainer = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
2014-10-17 14:11:56 -07:00
TopContainer . HAnchor = HAnchor . ParentLeftRight ;
2015-04-08 15:20:10 -07:00
#if ! __ANDROID__
2016-05-03 18:22:59 -07:00
// The application menu bar, which is suppressed on Android
ApplicationMenuRow menuRow = new ApplicationMenuRow ( ) ;
2014-10-17 14:11:56 -07:00
TopContainer . AddChild ( menuRow ) ;
2015-04-08 15:20:10 -07:00
#endif
2014-03-20 18:10:23 -07:00
2015-04-08 15:20:10 -07:00
menuSeparator = new GuiWidget ( ) ;
menuSeparator . Height = 12 ;
menuSeparator . HAnchor = HAnchor . ParentLeftRight ;
2014-10-17 14:11:56 -07:00
menuSeparator . MinimumSize = new Vector2 ( 0 , 12 ) ;
2015-04-08 15:20:10 -07:00
menuSeparator . Visible = false ;
2014-09-19 19:17:12 -07:00
2015-04-08 15:20:10 -07:00
queueDataView = new QueueDataView ( ) ;
2014-10-17 14:11:56 -07:00
TopContainer . AddChild ( new ActionBarPlus ( queueDataView ) ) ;
container . AddChild ( TopContainer ) ;
2014-10-27 19:54:01 -07:00
progressBar = new PrintProgressBar ( ) ;
2015-04-08 15:20:10 -07:00
container . AddChild ( progressBar ) ;
2014-10-17 14:11:56 -07:00
container . AddChild ( menuSeparator ) ;
2016-05-07 20:49:46 -07:00
touchscreenTabView = new TouchscreenTabView ( queueDataView ) ;
2014-10-27 19:54:01 -07:00
2016-05-07 20:49:46 -07:00
container . AddChild ( touchscreenTabView ) ;
2016-05-03 18:22:59 -07:00
this . AddChild ( container ) ;
2014-10-27 19:54:01 -07:00
}
}
2016-05-03 18:22:59 -07:00
public class DesktopView : ApplicationView
2015-04-08 15:20:10 -07:00
{
private WidescreenPanel widescreenPanel ;
2014-10-17 14:11:56 -07:00
2016-05-03 18:22:59 -07:00
public DesktopView ( )
2014-10-17 14:11:56 -07:00
{
2015-04-08 15:20:10 -07:00
AddElements ( ) ;
2016-05-03 18:22:59 -07:00
this . AnchorAll ( ) ;
2014-10-27 19:54:01 -07:00
}
2015-04-08 15:20:10 -07:00
2015-12-22 16:44:54 -08:00
public override void AddElements ( )
2015-04-08 15:20:10 -07:00
{
2016-04-18 11:31:31 -07:00
this . BackgroundColor = ActiveTheme . Instance . PrimaryBackgroundColor ;
2015-11-16 18:01:25 -08:00
2016-04-18 11:31:31 -07:00
var container = new FlowLayoutWidget ( FlowDirection . TopToBottom ) ;
2015-04-08 15:20:10 -07:00
container . AnchorAll ( ) ;
2014-04-27 20:24:58 -07:00
2016-05-03 18:22:59 -07:00
#if ! __ANDROID__
// The application menu bar, which is suppressed on Android
2016-04-18 11:31:31 -07:00
var menuRow = new ApplicationMenuRow ( ) ;
2015-04-08 15:20:10 -07:00
container . AddChild ( menuRow ) ;
2016-05-03 18:22:59 -07:00
#endif
2014-04-27 20:24:58 -07:00
2016-04-18 11:31:31 -07:00
var menuSeparator = new GuiWidget ( )
{
BackgroundColor = new RGBA_Bytes ( 200 , 200 , 200 ) ,
Height = 2 ,
HAnchor = HAnchor . ParentLeftRight ,
Margin = new BorderDouble ( 3 , 6 , 3 , 3 )
} ;
2015-04-08 15:20:10 -07:00
container . AddChild ( menuSeparator ) ;
2016-04-18 11:31:31 -07:00
2015-04-08 15:20:10 -07:00
widescreenPanel = new WidescreenPanel ( ) ;
container . AddChild ( widescreenPanel ) ;
2015-11-16 18:01:25 -08:00
using ( new PerformanceTimer ( "ReloadAll" , "AddChild" ) )
{
this . AddChild ( container ) ;
}
2015-02-20 12:05:44 -08:00
}
2015-04-08 15:20:10 -07:00
}
public class ApplicationController
{
private static ApplicationController globalInstance ;
2016-07-14 11:19:13 -07:00
public RootedObjectEventHandler AdvancedControlsPanelReloading = new RootedObjectEventHandler ( ) ;
2015-04-08 15:20:10 -07:00
public RootedObjectEventHandler CloudSyncStatusChanged = new RootedObjectEventHandler ( ) ;
2015-01-06 19:04:45 -08:00
public RootedObjectEventHandler DoneReloadingAll = new RootedObjectEventHandler ( ) ;
2015-07-15 12:28:52 -07:00
public RootedObjectEventHandler PluginsLoaded = new RootedObjectEventHandler ( ) ;
2015-01-06 19:04:45 -08:00
2016-08-29 17:13:45 -07:00
public static Action SignInAction ;
public static Action SignOutAction ;
2016-07-07 17:05:50 -07:00
public static Func < string > GetSessionInfo ;
2016-09-15 12:03:39 -07:00
public static Action < bool > OutboundRequest ;
2015-07-16 17:16:28 -07:00
2016-07-25 15:21:25 -07:00
/// <summary>
/// Allows application components to hook initial SystemWindow Load event without an existing Widget instance
/// </summary>
public static event EventHandler Load ;
2016-07-12 17:23:45 -07:00
public static Func < string , Task < Dictionary < string , string > > > GetProfileHistory ;
2016-07-28 09:38:03 -07:00
public static Func < PrinterInfo , string , Task < PrinterSettings > > GetPrinterProfileAsync ;
2016-08-22 13:46:14 -07:00
public static Func < IProgress < SyncReportType > , Task > SyncPrinterProfiles ;
2016-07-26 12:54:16 -07:00
public static Func < Task < OemProfileDictionary > > GetPublicProfileList ;
public static Func < string , Task < PrinterSettings > > DownloadPublicProfileAsync ;
2016-07-12 17:23:45 -07:00
2015-04-08 15:20:10 -07:00
public SlicePresetsWindow EditMaterialPresetsWindow { get ; set ; }
public SlicePresetsWindow EditQualityPresetsWindow { get ; set ; }
public ApplicationView MainView ;
public event EventHandler ApplicationClosed ;
2014-09-19 19:17:12 -07:00
2015-04-08 15:20:10 -07:00
private event EventHandler unregisterEvents ;
2014-09-19 19:17:12 -07:00
2015-04-08 15:20:10 -07:00
public bool WidescreenMode { get ; set ; }
2014-09-19 19:17:12 -07:00
2016-08-29 15:29:43 -07:00
static int applicationInstanceCount = 0 ;
public static int ApplicationInstanceCount
{
get
{
if ( applicationInstanceCount = = 0 )
{
2016-08-29 17:13:45 -07:00
Assembly mcAssembly = Assembly . GetEntryAssembly ( ) ;
2016-08-31 11:42:02 -07:00
if ( mcAssembly ! = null )
2016-08-29 15:29:43 -07:00
{
2016-08-29 17:13:45 -07:00
string applicationName = Path . GetFileNameWithoutExtension ( mcAssembly . Location ) . ToUpper ( ) ;
Process [ ] p1 = Process . GetProcesses ( ) ;
foreach ( System . Diagnostics . Process pro in p1 )
2016-08-29 15:29:43 -07:00
{
2016-08-31 11:42:02 -07:00
try
{
if ( pro ? . ProcessName ! = null
& & pro . ProcessName . ToUpper ( ) . Contains ( applicationName ) )
{
applicationInstanceCount + + ;
}
}
catch
2016-08-29 17:13:45 -07:00
{
}
2016-08-29 15:29:43 -07:00
}
}
}
return applicationInstanceCount ;
}
}
2015-04-08 15:20:10 -07:00
public ApplicationController ( )
{
//Name = "MainSlidePanel";
2016-07-14 11:19:13 -07:00
ActiveTheme . ThemeChanged . RegisterEvent ( ReloadAll , ref unregisterEvents ) ;
2015-04-08 15:20:10 -07:00
}
2015-01-06 19:04:45 -08:00
2016-08-29 17:13:45 -07:00
public void StartSignIn ( )
2015-07-16 17:16:28 -07:00
{
2016-07-06 16:40:23 -07:00
if ( PrinterConnectionAndCommunication . Instance . PrinterIsPrinting
| | PrinterConnectionAndCommunication . Instance . PrinterIsPaused )
{
2016-08-29 17:13:45 -07:00
// can't sign in while printing
2016-07-06 16:40:23 -07:00
UiThread . RunOnIdle ( ( ) = >
2016-08-29 17:13:45 -07:00
StyledMessageBox . ShowMessageBox ( null , "Please wait until the print has finished and try again." . Localize ( ) , "Can't sign in while printing" . Localize ( ) )
2016-07-06 16:40:23 -07:00
) ;
}
2016-08-29 17:13:45 -07:00
else // do the regular sign in
2016-07-06 16:40:23 -07:00
{
2016-08-29 17:13:45 -07:00
SignInAction ? . Invoke ( ) ;
2016-07-06 16:40:23 -07:00
}
2015-07-16 17:16:28 -07:00
}
2016-08-10 15:40:09 -07:00
private static TypeFace monoSpacedTypeFace = null ;
public static TypeFace MonoSpacedTypeFace
{
get
{
if ( monoSpacedTypeFace = = null )
{
monoSpacedTypeFace = TypeFace . LoadFrom ( StaticData . Instance . ReadAllText ( Path . Combine ( "Fonts" , "LiberationMono.svg" ) ) ) ;
}
return monoSpacedTypeFace ;
}
private set { }
}
2016-07-21 15:13:10 -07:00
/// <summary>
/// Requests fresh content from online services, falling back to cached content if offline
/// </summary>
/// <param name="collector">The custom collector function to load the content</param>
/// <returns></returns>
2016-07-26 12:54:16 -07:00
public async static Task < T > LoadCacheableAsync < T > ( string cacheKey , string cacheScope , Func < Task < T > > collector , string staticDataFallbackPath = null ) where T : class
2016-07-21 15:13:10 -07:00
{
2016-09-01 17:24:09 -07:00
string cachePath = CacheablePath ( cacheScope , cacheKey ) ;
2016-07-21 15:13:10 -07:00
try
{
// Try to update the document
2016-07-26 12:54:16 -07:00
T item = await collector ( ) ;
if ( item ! = null )
2016-07-21 15:13:10 -07:00
{
// update cache on success
2016-09-01 17:24:09 -07:00
File . WriteAllText ( cachePath , JsonConvert . SerializeObject ( item , Formatting . Indented ) ) ;
2016-07-26 12:54:16 -07:00
return item ;
2016-07-21 15:13:10 -07:00
}
}
catch
{
2016-09-01 17:24:09 -07:00
// Fall back to preexisting cache if failed
2016-07-21 15:13:10 -07:00
}
try
{
2016-07-21 17:48:19 -07:00
if ( File . Exists ( cachePath ) )
{
// Load from cache and deserialize
return JsonConvert . DeserializeObject < T > ( File . ReadAllText ( cachePath ) ) ;
}
2016-07-21 15:13:10 -07:00
}
catch
{
2016-09-01 17:24:09 -07:00
// Fall back to StaticData
2016-07-21 15:13:10 -07:00
}
try
{
2016-07-21 17:48:19 -07:00
if ( staticDataFallbackPath ! = null
2016-09-01 12:12:03 -07:00
& & StaticData . Instance . FileExists ( staticDataFallbackPath ) )
2016-07-21 17:48:19 -07:00
{
return JsonConvert . DeserializeObject < T > ( StaticData . Instance . ReadAllText ( staticDataFallbackPath ) ) ;
}
2016-07-21 15:13:10 -07:00
}
catch
{
return default ( T ) ;
}
2016-07-21 17:48:19 -07:00
return default ( T ) ;
2016-07-21 15:13:10 -07:00
}
2016-09-01 17:24:09 -07:00
private static string cacheDirectory = Path . Combine ( ApplicationDataStorage . ApplicationUserDataPath , "data" , "temp" , "cache" ) ;
internal static string CacheablePath ( string cacheScope , string cacheKey )
{
string scopeDirectory = Path . Combine ( cacheDirectory , cacheScope ) ;
// Ensure directory exists
Directory . CreateDirectory ( scopeDirectory ) ;
string cachePath = Path . Combine ( scopeDirectory , cacheKey ) ;
return cachePath ;
}
2016-08-29 17:13:45 -07:00
public void StartSignOut ( )
2015-07-16 17:16:28 -07:00
{
2016-07-06 16:40:23 -07:00
if ( PrinterConnectionAndCommunication . Instance . PrinterIsPrinting
| | PrinterConnectionAndCommunication . Instance . PrinterIsPaused )
{
// can't log out while printing
UiThread . RunOnIdle ( ( ) = >
StyledMessageBox . ShowMessageBox ( null , "Please wait until the print has finished and try again." . Localize ( ) , "Can't log out while printing" . Localize ( ) )
) ;
}
else // do the regular log out
{
2016-08-29 17:13:45 -07:00
bool allowShowingSignOutWarning = true ;
if ( allowShowingSignOutWarning )
2016-07-06 16:40:23 -07:00
{
2016-09-02 16:26:56 -07:00
// Warn on sign out that no access to user printers and cloud library put a 'Don't remind me again' check box
2016-08-29 17:13:45 -07:00
StyledMessageBox . ShowMessageBox ( ( clickedSignOut ) = >
2016-07-06 16:40:23 -07:00
{
2016-08-29 17:13:45 -07:00
if ( clickedSignOut )
2016-07-06 16:40:23 -07:00
{
2016-08-29 17:13:45 -07:00
SignOutAction ? . Invoke ( ) ;
2016-07-06 16:40:23 -07:00
}
2016-08-29 17:13:45 -07:00
} , "Are you sure you want to sign out? You will not have access to your printer profiles or cloud library." . Localize ( ) , "Sign Out?" . Localize ( ) , StyledMessageBox . MessageType . YES_NO , "Sign Out" . Localize ( ) , "Cancel" . Localize ( ) ) ;
2016-07-06 16:40:23 -07:00
}
2016-08-29 17:13:45 -07:00
else // just run the sign out event
2016-07-06 16:40:23 -07:00
{
2016-08-29 17:13:45 -07:00
SignOutAction ? . Invoke ( ) ;
2016-07-06 16:40:23 -07:00
}
}
2015-07-16 17:16:28 -07:00
}
public string GetSessionUsername ( )
{
2016-07-07 17:05:50 -07:00
if ( GetSessionInfo ! = null )
2015-07-16 17:16:28 -07:00
{
2016-07-07 17:05:50 -07:00
return GetSessionInfo ( ) ;
2015-07-16 17:16:28 -07:00
}
else
{
return null ;
}
}
2016-07-06 17:45:53 -07:00
private static string MakeValidFileName ( string name )
{
if ( string . IsNullOrEmpty ( name ) )
{
return name ;
}
string invalidChars = System . Text . RegularExpressions . Regex . Escape ( new string ( System . IO . Path . GetInvalidFileNameChars ( ) ) ) ;
string invalidRegStr = string . Format ( @"([{0}]*\.+$)|([{0}]+)" , invalidChars ) ;
return System . Text . RegularExpressions . Regex . Replace ( name , invalidRegStr , "_" ) ;
}
public string GetSessionUsernameForFileSystem ( )
{
return MakeValidFileName ( GetSessionUsername ( ) ) ;
}
2015-04-08 15:20:10 -07:00
public void ReloadAll ( object sender , EventArgs e )
{
2015-06-11 12:06:40 -07:00
UiThread . RunOnIdle ( ( ) = >
2015-04-08 15:20:10 -07:00
{
2015-11-16 18:01:25 -08:00
using ( new PerformanceTimer ( "ReloadAll" , "Total" ) )
2015-01-06 19:04:45 -08:00
{
2016-01-15 12:28:52 -08:00
// give the widget a chance to hear about the close before they are actually closed.
2016-01-24 20:06:04 -08:00
PopOutManager . SaveIfClosed = false ;
2016-04-18 11:31:31 -07:00
WidescreenPanel . PreChangePanels . CallEvents ( this , null ) ;
2016-01-12 11:20:27 -08:00
MainView . CloseAllChildren ( ) ;
2015-11-16 18:01:25 -08:00
using ( new PerformanceTimer ( "ReloadAll" , "AddElements" ) )
{
MainView . AddElements ( ) ;
}
2016-01-24 20:06:04 -08:00
PopOutManager . SaveIfClosed = true ;
2016-07-06 14:04:23 -07:00
DoneReloadingAll ? . CallEvents ( null , null ) ;
2016-01-24 20:06:04 -08:00
}
2015-04-08 15:20:10 -07:00
} ) ;
}
public void OnApplicationClosed ( )
{
2016-04-18 11:31:31 -07:00
ApplicationClosed ? . Invoke ( null , null ) ;
2015-04-08 15:20:10 -07:00
}
public static ApplicationController Instance
{
get
{
if ( globalInstance = = null )
{
2015-10-30 12:35:54 -07:00
//using (new PerformanceTimer("Startup", "AppController Instance"))
2015-04-08 15:20:10 -07:00
{
2015-10-29 11:31:09 -07:00
globalInstance = new ApplicationController ( ) ;
2016-04-29 07:45:15 -07:00
if ( UserSettings . Instance . DisplayMode = = ApplicationDisplayType . Touchscreen )
2015-10-29 11:31:09 -07:00
{
2016-05-03 18:22:59 -07:00
globalInstance . MainView = new TouchscreenView ( ) ;
2015-10-29 11:31:09 -07:00
}
else
{
2016-05-03 18:22:59 -07:00
globalInstance . MainView = new DesktopView ( ) ;
2015-10-29 11:31:09 -07:00
}
2016-06-08 11:46:46 -07:00
ActiveSliceSettings . ActivePrinterChanged . RegisterEvent ( ( s , e ) = > ApplicationController . Instance . ReloadAll ( null , null ) , ref globalInstance . unregisterEvents ) ;
2015-04-08 15:20:10 -07:00
}
}
return globalInstance ;
}
}
public void ReloadAdvancedControlsPanel ( )
{
2016-07-14 11:19:13 -07:00
AdvancedControlsPanelReloading . CallEvents ( this , null ) ;
2015-04-08 15:20:10 -07:00
}
2015-08-27 11:56:01 -07:00
public LibraryDataView CurrentLibraryDataView = null ;
2015-07-13 12:49:01 -07:00
public void SwitchToPurchasedLibrary ( )
{
// Switch to the purchased library
2015-08-27 11:56:01 -07:00
LibraryProviderSelector libraryProviderSelector = CurrentLibraryDataView . CurrentLibraryProvider . GetRootProvider ( ) as LibraryProviderSelector ;
2015-12-22 16:44:54 -08:00
if ( libraryProviderSelector ! = null )
2015-07-29 12:03:51 -07:00
{
2015-09-08 18:50:35 -07:00
LibraryProvider purchaseProvider = libraryProviderSelector . GetPurchasedLibrary ( ) ;
2015-12-22 16:44:54 -08:00
UiThread . RunOnIdle ( ( ) = >
2015-09-08 18:50:35 -07:00
{
CurrentLibraryDataView . CurrentLibraryProvider = purchaseProvider ;
} ) ;
2015-07-29 12:03:51 -07:00
}
2015-07-13 12:49:01 -07:00
}
2015-12-22 16:44:54 -08:00
public void SwitchToSharedLibrary ( )
{
// Switch to the shared library
LibraryProviderSelector libraryProviderSelector = CurrentLibraryDataView . CurrentLibraryProvider . GetRootProvider ( ) as LibraryProviderSelector ;
if ( libraryProviderSelector ! = null )
{
LibraryProvider sharedProvider = libraryProviderSelector . GetSharedLibrary ( ) ;
UiThread . RunOnIdle ( ( ) = >
{
CurrentLibraryDataView . CurrentLibraryProvider = sharedProvider ;
} ) ;
}
}
2015-09-17 19:19:15 -07:00
2016-09-06 18:15:06 -07:00
public void ChangeCloudSyncStatus ( bool userAuthenticated , string reason = "" )
2015-04-08 15:20:10 -07:00
{
2016-09-06 18:15:06 -07:00
UserSettings . Instance . set ( UserSettingsKey . CredentialsInvalid , userAuthenticated ? "false" : "true" ) ;
UserSettings . Instance . set ( UserSettingsKey . CredentialsInvalidReason , userAuthenticated ? "" : reason ) ;
2015-07-12 19:02:42 -07:00
CloudSyncStatusChanged . CallEvents ( this , new CloudSyncEventArgs ( ) { IsAuthenticated = userAuthenticated } ) ;
2016-07-11 15:29:02 -07:00
string activeUserName = ApplicationController . Instance . GetSessionUsernameForFileSystem ( ) ;
2016-08-22 12:39:17 -07:00
string currentUserName = UserSettings . Instance . get ( "ActiveUserName" ) ;
2016-07-11 15:29:02 -07:00
UserSettings . Instance . set ( "ActiveUserName" , activeUserName ) ;
2016-07-14 11:19:13 -07:00
2016-08-22 12:39:17 -07:00
// Only fire UserChanged if it actually happened - prevents runaway positive feedback loop
if ( currentUserName ! = activeUserName )
{
UserChanged ( ) ;
}
2016-07-14 11:19:13 -07:00
}
// Called after every startup and at the completion of every authentication change
public void UserChanged ( )
{
2016-07-11 15:29:02 -07:00
ProfileManager . Reload ( ) ;
2016-07-14 11:19:13 -07:00
var profileManager = ProfileManager . Instance ;
// Ensure SQLite printers are imported
profileManager . EnsurePrintersImported ( ) ;
2016-07-14 13:46:33 -07:00
var guestDB = ProfileManager . LoadGuestDB ( ) ;
2016-07-14 11:19:13 -07:00
// If profiles.json was created, run the import wizard to pull in any SQLite printers
2016-07-14 13:46:33 -07:00
if ( guestDB ? . Profiles ! = null & & guestDB . Profiles . Any ( ) & & ! profileManager . IsGuestProfile & & ! profileManager . PrintersImported )
2016-07-14 11:19:13 -07:00
{
var wizardPage = new CopyGuestProfilesToUser ( ( ) = >
{
// On success, set state indicating import has been run and update ProfileManager state
profileManager . PrintersImported = true ;
profileManager . Save ( ) ;
} ) ;
// Show the import printers wizard
2016-07-14 11:39:19 -07:00
WizardWindow . Show ( "/CopyGuestProfiles" , "Upload Printers" , wizardPage ) ;
2016-07-14 11:19:13 -07:00
}
2015-07-12 19:02:42 -07:00
}
public class CloudSyncEventArgs : EventArgs
{
public bool IsAuthenticated { get ; set ; }
2015-04-08 15:20:10 -07:00
}
2016-07-21 13:49:22 -07:00
public void OnLoadActions ( )
{
2016-07-25 15:21:25 -07:00
Load ? . Invoke ( this , null ) ;
2016-09-12 14:06:42 -07:00
// Pushing this after load fixes that empty printer list
2016-09-16 13:11:48 -07:00
ApplicationController . Instance . UserChanged ( ) ;
2016-07-21 13:49:22 -07:00
2016-07-26 12:42:03 -07:00
if ( ! System . IO . File . Exists ( @"/storage/sdcard0/Download/LaunchTestPrint.stl" ) )
2016-07-21 13:49:22 -07:00
{
2016-07-26 12:42:03 -07:00
bool showAuthWindow = WizardWindow . ShouldShowAuthPanel ? . Invoke ( ) ? ? false ;
2016-08-12 11:27:40 -07:00
if ( showAuthWindow )
{
2016-08-30 10:30:55 -07:00
if ( ApplicationSettings . Instance . get ( ApplicationSettingsKey . SuppressAuthPanel ) ! = "True" )
2016-08-29 22:04:09 -07:00
{
//Launch window to prompt user to sign in
UiThread . RunOnIdle ( ( ) = > WizardWindow . ShowPrinterSetup ( ) ) ;
}
2016-08-12 11:27:40 -07:00
}
else
{
//If user in logged in sync before checking to prompt to create printer
if ( ApplicationController . SyncPrinterProfiles = = null )
{
RunSetupIfRequired ( ) ;
}
else
{
2016-08-22 13:46:14 -07:00
ApplicationController . SyncPrinterProfiles . Invoke ( null ) . ContinueWith ( ( task ) = >
2016-08-12 11:27:40 -07:00
{
RunSetupIfRequired ( ) ;
} ) ;
}
}
2016-07-26 16:50:07 -07:00
if ( OsInformation . OperatingSystem ! = OSType . Windows )
{
// show this last so it is on top
if ( UserSettings . Instance . get ( "SoftwareLicenseAccepted" ) ! = "true" )
{
UiThread . RunOnIdle ( ( ) = > WizardWindow . Show < LicenseAgreementPage > ( "SoftwareLicense" , "Software License Agreement" ) ) ;
}
}
2016-07-26 12:42:03 -07:00
}
else
{
StartPrintingTest ( ) ;
2016-07-21 13:49:22 -07:00
}
if ( ActiveSliceSettings . Instance . PrinterSelected
& & ActiveSliceSettings . Instance . GetValue < bool > ( SettingsKey . auto_connect ) )
{
UiThread . RunOnIdle ( ( ) = >
{
//PrinterConnectionAndCommunication.Instance.HaltConnectionThread();
PrinterConnectionAndCommunication . Instance . ConnectToActivePrinter ( ) ;
} , 2 ) ;
}
}
2016-08-12 11:27:40 -07:00
private static void RunSetupIfRequired ( )
{
ApplicationController . Instance . ReloadAdvancedControlsPanel ( ) ;
if ( ! ProfileManager . Instance . ActiveProfiles . Any ( ) )
{
// Start the setup wizard if no profiles exist
UiThread . RunOnIdle ( ( ) = > WizardWindow . ShowPrinterSetup ( ) ) ;
}
}
private EventHandler unregisterEvent ;
2016-07-26 12:42:03 -07:00
public void StartPrintingTest ( )
{
QueueData . Instance . RemoveAll ( ) ;
QueueData . Instance . AddItem ( new PrintItemWrapper ( new PrintItem ( "LaunchTestPrint" , @"/storage/sdcard0/Download/LaunchTestPrint.stl" ) ) ) ;
PrinterConnectionAndCommunication . Instance . ConnectToActivePrinter ( ) ;
PrinterConnectionAndCommunication . Instance . CommunicationStateChanged . RegisterEvent ( ( sender , e ) = >
{
if ( PrinterConnectionAndCommunication . Instance . CommunicationState = = PrinterConnectionAndCommunication . CommunicationStates . Connected )
{
PrinterConnectionAndCommunication . Instance . PrintActivePartIfPossible ( ) ;
}
} , ref unregisterEvent ) ;
}
2016-08-08 17:17:59 -07:00
2016-09-06 14:42:14 -07:00
public void ReloadLibrarySelectorUI ( )
{
LibraryProviderSelector . Reload ( ) ;
}
2016-08-08 17:17:59 -07:00
public void ReloadLibraryUI ( )
{
PrintLibraryWidget . Reload ( ) ;
}
2016-09-06 14:04:44 -07:00
public static void ClearCachedCredentials ( )
{
string sessionFilePath = Path . Combine ( ApplicationDataStorage . ApplicationUserDataPath , "cache" , "session.bin" ) ;
if ( File . Exists ( sessionFilePath ) )
{
File . Delete ( sessionFilePath ) ;
}
}
2015-04-08 15:20:10 -07:00
}
2016-08-22 13:46:14 -07:00
public class SyncReportType
{
public string actionLabel ;
public double percComplete ;
}
2015-04-08 15:20:10 -07:00
}