using FastReport.Design; using FastReport.Design.ToolWindows; using System; using System.Drawing; using System.IO; using System.Reflection; using System.Web; using System.Windows.Forms; using FastReport.Cloud.FastReport; using System.Threading.Tasks; using System.Globalization; using System.Collections.Generic; namespace FastReport.Utils { partial class Config { #region Private Fields private const UIStyle DEFAULT_UISTYLE = UIStyleUtils.DefaultStyle; private static readonly CultureInfo engCultureInfo = new CultureInfo("en-US"); private static Export.Email.EmailSettings FEmailSettings = new Export.Email.EmailSettings(); private static DesignerSettings FDesignerSettings = new DesignerSettings(); private static bool FSplashScreenEnabled = false; private static UIStyle FUIStyle = DEFAULT_UISTYLE; private static bool FUseRibbon = false; private static bool processEvents = false; private static int iconPack = 0; private static string saveFolder = ""; private static bool connectionStringVisible = true; private static bool disableHotkeys = false; private static bool applyLastFormat = true; private static Dictionary shortcuts = CreateDefaultShortcuts(); #if COMMUNITY private static Image splashScreen; private static Image welcomeScreen; #endif private static StorageService welcomeStorage = new StorageService("Designer,Welcome"); #endregion Private Fields #region Public Properties /// /// Gets an english culture information for localization purposes /// public static CultureInfo EngCultureInfo { get { return engCultureInfo; } } /// /// Gets dictionary of shortcut key. /// public static Dictionary Shortcuts { get { return shortcuts; } internal set { shortcuts = value; } } /// /// Gets or sets a value indicating whether hotkeys should be disabled. /// public static bool DisableHotkeys { get { return disableHotkeys; } set { disableHotkeys = value; } } /// /// Gets or sets a value indicating saving last formatting should be disabled. /// public static bool ApplyLastFormat { get { return applyLastFormat; } set { applyLastFormat = value; } } /// /// Gets or sets a value indicating that UI library must use high dpi compatible rendering. /// /// This flag is false by default. Turn it on at the application start if you need /// better appearance of custom drawn UI items in high dpi mode. This however may result in /// wrong appearance on multi-monitor setup. /// /// public static bool EnableBarHighDpi { #if MONO get; set; #else get { return DevComponents.DpiHelper.HighDpiEnabled; } set { if (!DevComponents.DpiHelper.HighDpiEnabled) DevComponents.DpiHelper.EnableHighDpi(); } #endif } /// /// Gets or sets the settings for the report designer window. /// public static DesignerSettings DesignerSettings { get { return FDesignerSettings; } set { FDesignerSettings = value; } } /// /// Gets or sets the UI style. /// /// /// This property affects both designer and preview windows. /// public static UIStyle UIStyle { get { return FUIStyle; } set { FUIStyle = value; } } /// /// Gets or sets a value indicating whether the Ribbon UI should be used /// public static bool UseRibbon { get { #if COMMUNITY return false; #else return FUseRibbon; #endif } set { FUseRibbon = value; } } /// /// Gets or sets a value indicating whether the designer window should use the compact menu (Visual Studio - alike). /// /// /// The compact menu is currently supported in WPF only. /// public static bool UseCompactMenu { get; set; } /// /// Gets or sets a value indicating of displaying the connection string. /// If false, the connection string is hidden from the interface. /// public static bool ConnectionStringVisible { get { return connectionStringVisible; } set { connectionStringVisible = value; } } /// /// Gets or set the current icon pack index. Default is 0 (classic). /// /// Set this property at the application start. public static int IconPack { get { return iconPack; } set { if (value < 0) value = 0; // increase the number when you add new packs. int number_of_packs = 2; if (value >= number_of_packs) value = number_of_packs - 1; iconPack = value; } } /// /// Gets or sets a value indicating whether SplashScreen should be displayed while loading designer /// public static bool SplashScreenEnabled { get { return FSplashScreenEnabled; } set { FSplashScreenEnabled = value; } } /// /// Gets or sets a value indicating whether Welcome window feature enabled. /// If false, interface elements associated with the Welcome window will not be visible. /// public static bool WelcomeEnabled { get => welcomeStorage.GetBool("Enabled", true); set => welcomeStorage.SetBool("Enabled", value); } /// /// Gets or sets a value indicating whether Welcome window shoud be displayed on startup /// public static bool WelcomeShowOnStartup { get => welcomeStorage.GetBool("ShowOnStartup", true); set => welcomeStorage.SetBool("ShowOnStartup", value); } /// /// Gets the folder to store auto save files /// public static string AutoSaveFolder { get { return Path.Combine(GetTempFolder(), "FastReport"); } } /// /// Gets or sets the default folder for SaveFileDialog. /// public static string SaveFolder { get { return saveFolder; } set { saveFolder = value; } } /// /// Gets the autosaved report /// public static string AutoSaveFile { get { return Path.Combine(AutoSaveFolder, "autosave.frx"); } } /// /// Gets the autosaved report path /// public static string AutoSaveFileName { get { return Path.Combine(AutoSaveFolder, "autosave.txt"); } } /// /// Is necessary to process abort and some other events in parallel /// public static bool ProcessEvents { get { return processEvents; } set { processEvents = value; } } /// /// Gets a value indicating that the ASP.NET hosting permission level is set to full trust. /// public static bool FullTrust { get { #if AVALONIA return true; #else return GetCurrentTrustLevel() == AspNetHostingPermissionLevel.Unrestricted; #endif } } /// /// Gets or sets a value that determines whether to disable some functionality to run in web mode. /// /// /// Use this property if you use FastReport in ASP.Net. Set this property to true before /// you access any FastReport .NET objects. /// public static bool WebMode { get { return FWebMode; } set { FWebMode = value; if (value) ReportSettings.ShowProgress = false; } } /// /// Gets or sets the settings for the "Send Email" window. /// public static Export.Email.EmailSettings EmailSettings { get { return FEmailSettings; } set { FEmailSettings = value; } } /// /// Gets or sets a value that determines whether to paint designer surface on a separate (render) thread. /// /// /// Default value is false. /// public static bool AvaloniaPaintOnRenderThread { get; set; } = false; #endregion Public Properties #region Public Methods /// /// Restores the form state from the configuration file. /// /// The form to restore. /// Obsolete. Use either BaseForm.Storage or . [Obsolete] public static void RestoreFormState(Form form) { new FormStorageService(form).RestoreFormState(); } /// /// Saves the form state to the configuration file. /// /// The form to save. /// Obsolete. Use either BaseForm.Storage or . [Obsolete] public static void SaveFormState(Form form) { new FormStorageService(form).SaveFormState(); } static partial void SaveConnectionStringVisible() { new StorageService("Designer,PropertiesWindow").SetBool("ConnectionStringVisible", ConnectionStringVisible); } static partial void RestoreConnectionStringVisible() { ConnectionStringVisible = new StorageService("Designer,PropertiesWindow").GetBool("ConnectionStringVisible", true); } /// /// Create a dictionary of default keyboard shortcuts. /// /// public static Dictionary CreateDefaultShortcuts() { Dictionary result = new Dictionary { { "File,Open", Keys.Control | Keys.O }, { "File,Save", Keys.Control | Keys.S }, { "File,SaveAs", Keys.Control | Keys.Alt | Keys.S }, { "File,SaveAll", Keys.Control | Keys.Shift | Keys.S }, #if !COMMUNITY { "File,Preview", Keys.Control | Keys.P }, #endif { "Editing,Undo", Keys.Control | Keys.Z }, { "Editing,Redo", Keys.Control | Keys.Y }, { "Editing,Cut", Keys.Control | Keys.X }, { "Editing,Copy", Keys.Control | Keys.C }, { "Editing,Paste", Keys.Control | Keys.V }, { "Editing,NewPage", Keys.Control | Keys.N }, { "Editing,Find", Keys.Control | Keys.F }, { "Editing,Replace", Keys.Control | Keys.H }, { "Editing,SelectAll", Keys.Control | Keys.A }, { "Help,Contents", Keys.F1 }, { "Editing,Bold", Keys.Control | Keys.B }, { "Editing,Italic", Keys.Control | Keys.I }, { "Editing,Underline", Keys.Control | Keys.U }, }; return result; } #endregion Public Methods #region Internal Methods internal static void DoEvent() { if (ProcessEvents && !WebMode) { System.Windows.Forms.Application.DoEvents(); } } #endregion Internal Methods #region Private Methods private static void LoadPluginsInCurrentFolder() { var appFolder = ApplicationFolder; if (!string.IsNullOrEmpty(appFolder)) { // find all plugin-connector in current directory var plugins = Directory.GetFiles(appFolder, "FastReport.Data.*.dll"); // initialize foreach (var pluginName in plugins) { try { var assembly = Assembly.LoadFrom(pluginName); ProcessAssembly(assembly); } catch { } } } } /// /// Save default settings and user tokens. /// Is user have to consent to save data, clear the data in the config file. /// private static void SaveAuthServiceUser() { XmlItem xi = FDoc.Root.FindItem("Auth").FindItem("User"); Auth.AuthService auth = Auth.AuthService.Instance; if (auth.IsEnable && auth.CanRefresh) { xi.ClearProps(); xi.Clear(); if (auth.User.RefreshToken != null) xi.SetProp("refresh_token", auth.User.RefreshToken); if (auth.User.IdToken != null) xi.SetProp("id_token", auth.User.IdToken); if (auth.User.Token != null) xi.SetProp("access_token", auth.User.Token); } else { xi.ClearProps(); xi.Clear(); } } /// /// Save default settings and user tokens. /// By default internal use only, it is able to be a public /// private static async void RestoreAuthServiceUser() { XmlItem xi = FDoc.Root.FindItem("Auth").FindItem("User"); Auth.AuthService auth = Auth.AuthService.Instance; if (auth.IsEnable) { auth.User.Reset(); auth.User.IdToken = xi.GetProp("id_token"); auth.User.Token = xi.GetProp("access_token"); auth.User.RefreshToken = xi.GetProp("refresh_token"); FRCloudOptions.Instance.ResetBackendHostAndApiKey(); bool isProgramStart = true; try { await Task.Factory.StartNew(() => auth.ParseTokens(isProgramStart)); } catch #if DEBUG (Exception ex) #endif { auth.User.Reset(); } } } #if !AVALONIA private static AspNetHostingPermissionLevel GetCurrentTrustLevel() { foreach (AspNetHostingPermissionLevel trustLevel in new AspNetHostingPermissionLevel[] { AspNetHostingPermissionLevel.Unrestricted, AspNetHostingPermissionLevel.High, AspNetHostingPermissionLevel.Medium, AspNetHostingPermissionLevel.Low, AspNetHostingPermissionLevel.Minimal }) { try { new AspNetHostingPermission(trustLevel).Demand(); } catch (System.Security.SecurityException) { continue; } return trustLevel; } return AspNetHostingPermissionLevel.None; } #endif private static void SaveUIStyle() { var storage = new StorageService("UIStyleNew"); storage.SetEnum("Style", UIStyle); storage.SetBoolStr("Ribbon", UseRibbon); // compatible with prev versions storage.SetInt("IconPack", IconPack); storage.SetBool("UseCompactMenu", UseCompactMenu); } private static void RestoreUIStyle() { var storage = new StorageService("UIStyleNew"); UIStyle = storage.GetEnum("Style", DEFAULT_UISTYLE); UseRibbon = storage.GetBool("Ribbon", true); IconPack = storage.GetInt("IconPack", 1); UseCompactMenu = storage.GetBool("UseCompactMenu", true); #if AVALONIA Application.InitControls(); Application.InitThemeColors(); #endif } private static void SaveUIOptions() { var storage = new StorageService("UIOptions"); storage.SetBoolStr("DisableHotkeys", DisableHotkeys); // compatible with prev versions storage.SetBool("ApplyLastFormat", ApplyLastFormat); Dictionary defaultShortcut = CreateDefaultShortcuts(); foreach (var item in shortcuts) { if (!defaultShortcut.ContainsKey(item.Key) || item.Value != defaultShortcut[item.Key] || FDoc.Root.FindItem("Shortcuts," + item.Key) != null) { StorageService shortCut = new StorageService("Shortcuts," + item.Key); shortCut.SetEnum("Value", item.Value); } } } private static void RestoreUIOptions() { RestoreRightToLeft(); var storage = new StorageService("UIOptions"); DisableHotkeys = storage.GetBool("DisableHotkeys", false); ApplyLastFormat = storage.GetBool("ApplyLastFormat", true); if (Root.Find("Shortcuts") != -1) foreach (var item in Root.FindItem("Shortcuts").Items) { foreach (var subItem in item.Items) { var shortCut = new StorageService("Shortcuts," + item.Name + "," + subItem.Name); shortcuts[item.Name + "," + subItem.Name] = shortCut.GetEnum("Value"); } } } #if !COMMUNITY private static void RestoreExportOptions() { ExportsOptions.GetInstance().RestoreState(); } private static void SaveExportOptions() { ExportsOptions.GetInstance().SaveState(); } #endif #if COMMUNITY public static Image SplashScreen { get { return splashScreen; } set { splashScreen = value; } } public static Image WelcomeScreen { get { return welcomeScreen; } set { welcomeScreen = value; } } #endif #endregion Private Methods } }