Config.DesignExt.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. using FastReport.Design;
  2. using FastReport.Design.ToolWindows;
  3. using System;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Web;
  8. using System.Windows.Forms;
  9. using FastReport.Cloud.FastReport;
  10. using System.Threading.Tasks;
  11. using System.Globalization;
  12. using System.Collections.Generic;
  13. namespace FastReport.Utils
  14. {
  15. partial class Config
  16. {
  17. #region Private Fields
  18. private const UIStyle DEFAULT_UISTYLE = UIStyleUtils.DefaultStyle;
  19. private static readonly CultureInfo engCultureInfo = new CultureInfo("en-US");
  20. private static Export.Email.EmailSettings FEmailSettings = new Export.Email.EmailSettings();
  21. private static DesignerSettings FDesignerSettings = new DesignerSettings();
  22. private static bool FSplashScreenEnabled = false;
  23. private static UIStyle FUIStyle = DEFAULT_UISTYLE;
  24. private static bool FUseRibbon = false;
  25. private static bool processEvents = false;
  26. private static int iconPack = 0;
  27. private static string saveFolder = "";
  28. private static bool connectionStringVisible = true;
  29. private static bool disableHotkeys = false;
  30. private static bool applyLastFormat = true;
  31. private static Dictionary<string, Keys> shortcuts = CreateDefaultShortcuts();
  32. #if COMMUNITY
  33. private static Image splashScreen;
  34. private static Image welcomeScreen;
  35. #endif
  36. private static StorageService welcomeStorage = new StorageService("Designer,Welcome");
  37. #endregion Private Fields
  38. #region Public Properties
  39. /// <summary>
  40. /// Gets an english culture information for localization purposes
  41. /// </summary>
  42. public static CultureInfo EngCultureInfo
  43. {
  44. get { return engCultureInfo; }
  45. }
  46. /// <summary>
  47. /// Gets dictionary of shortcut key.
  48. /// </summary>
  49. public static Dictionary<string, Keys> Shortcuts
  50. {
  51. get { return shortcuts; }
  52. internal set { shortcuts = value; }
  53. }
  54. /// <summary>
  55. /// Gets or sets a value indicating whether hotkeys should be disabled.
  56. /// </summary>
  57. public static bool DisableHotkeys
  58. {
  59. get { return disableHotkeys; }
  60. set { disableHotkeys = value; }
  61. }
  62. /// <summary>
  63. /// Gets or sets a value indicating saving last formatting should be disabled.
  64. /// </summary>
  65. public static bool ApplyLastFormat
  66. {
  67. get { return applyLastFormat; }
  68. set { applyLastFormat = value; }
  69. }
  70. /// <summary>
  71. /// Gets or sets a value indicating that UI library must use high dpi compatible rendering.
  72. /// </summary>
  73. /// <remarks>This flag is false by default. Turn it on at the application start if you need
  74. /// better appearance of custom drawn UI items in high dpi mode. This however may result in
  75. /// wrong appearance on multi-monitor setup.
  76. /// /// </remarks>
  77. public static bool EnableBarHighDpi
  78. {
  79. #if MONO
  80. get; set;
  81. #else
  82. get
  83. {
  84. return DevComponents.DpiHelper.HighDpiEnabled;
  85. }
  86. set
  87. {
  88. if (!DevComponents.DpiHelper.HighDpiEnabled)
  89. DevComponents.DpiHelper.EnableHighDpi();
  90. }
  91. #endif
  92. }
  93. /// <summary>
  94. /// Gets or sets the settings for the report designer window.
  95. /// </summary>
  96. public static DesignerSettings DesignerSettings
  97. {
  98. get { return FDesignerSettings; }
  99. set { FDesignerSettings = value; }
  100. }
  101. /// <summary>
  102. /// Gets or sets the UI style.
  103. /// </summary>
  104. /// <remarks>
  105. /// This property affects both designer and preview windows.
  106. /// </remarks>
  107. public static UIStyle UIStyle
  108. {
  109. get { return FUIStyle; }
  110. set { FUIStyle = value; }
  111. }
  112. /// <summary>
  113. /// Gets or sets a value indicating whether the Ribbon UI should be used
  114. /// </summary>
  115. public static bool UseRibbon
  116. {
  117. get
  118. {
  119. #if COMMUNITY
  120. return false;
  121. #else
  122. return FUseRibbon;
  123. #endif
  124. }
  125. set { FUseRibbon = value; }
  126. }
  127. /// <summary>
  128. /// Gets or sets a value indicating whether the designer window should use the compact menu (Visual Studio - alike).
  129. /// </summary>
  130. /// <remarks>
  131. /// The compact menu is currently supported in WPF only.
  132. /// </remarks>
  133. public static bool UseCompactMenu { get; set; }
  134. /// <summary>
  135. /// Gets or sets a value indicating of displaying the connection string.
  136. /// If false, the connection string is hidden from the interface.
  137. /// </summary>
  138. public static bool ConnectionStringVisible
  139. {
  140. get { return connectionStringVisible; }
  141. set { connectionStringVisible = value; }
  142. }
  143. /// <summary>
  144. /// Gets or set the current icon pack index. Default is 0 (classic).
  145. /// </summary>
  146. /// <remarks>Set this property at the application start.</remarks>
  147. public static int IconPack
  148. {
  149. get { return iconPack; }
  150. set
  151. {
  152. if (value < 0)
  153. value = 0;
  154. // increase the number when you add new packs.
  155. int number_of_packs = 2;
  156. if (value >= number_of_packs)
  157. value = number_of_packs - 1;
  158. iconPack = value;
  159. }
  160. }
  161. /// <summary>
  162. /// Gets or sets a value indicating whether SplashScreen should be displayed while loading designer
  163. /// </summary>
  164. public static bool SplashScreenEnabled
  165. {
  166. get { return FSplashScreenEnabled; }
  167. set { FSplashScreenEnabled = value; }
  168. }
  169. /// <summary>
  170. /// Gets or sets a value indicating whether Welcome window feature enabled.
  171. /// If false, interface elements associated with the Welcome window will not be visible.
  172. /// </summary>
  173. public static bool WelcomeEnabled
  174. {
  175. get => welcomeStorage.GetBool("Enabled", true);
  176. set => welcomeStorage.SetBool("Enabled", value);
  177. }
  178. /// <summary>
  179. /// Gets or sets a value indicating whether Welcome window shoud be displayed on startup
  180. /// </summary>
  181. public static bool WelcomeShowOnStartup
  182. {
  183. get => welcomeStorage.GetBool("ShowOnStartup", true);
  184. set => welcomeStorage.SetBool("ShowOnStartup", value);
  185. }
  186. /// <summary>
  187. /// Gets the folder to store auto save files
  188. /// </summary>
  189. public static string AutoSaveFolder
  190. {
  191. get { return Path.Combine(GetTempFolder(), "FastReport"); }
  192. }
  193. /// <summary>
  194. /// Gets or sets the default folder for SaveFileDialog.
  195. /// </summary>
  196. public static string SaveFolder
  197. {
  198. get { return saveFolder; }
  199. set { saveFolder = value; }
  200. }
  201. /// <summary>
  202. /// Gets the autosaved report
  203. /// </summary>
  204. public static string AutoSaveFile
  205. {
  206. get { return Path.Combine(AutoSaveFolder, "autosave.frx"); }
  207. }
  208. /// <summary>
  209. /// Gets the autosaved report path
  210. /// </summary>
  211. public static string AutoSaveFileName
  212. {
  213. get { return Path.Combine(AutoSaveFolder, "autosave.txt"); }
  214. }
  215. /// <summary>
  216. /// Is necessary to process abort and some other events in parallel
  217. /// </summary>
  218. public static bool ProcessEvents
  219. {
  220. get { return processEvents; }
  221. set { processEvents = value; }
  222. }
  223. /// <summary>
  224. /// Gets a value indicating that the ASP.NET hosting permission level is set to full trust.
  225. /// </summary>
  226. public static bool FullTrust
  227. {
  228. get
  229. {
  230. #if AVALONIA
  231. return true;
  232. #else
  233. return GetCurrentTrustLevel() == AspNetHostingPermissionLevel.Unrestricted;
  234. #endif
  235. }
  236. }
  237. /// <summary>
  238. /// Gets or sets a value that determines whether to disable some functionality to run in web mode.
  239. /// </summary>
  240. /// <remarks>
  241. /// Use this property if you use FastReport in ASP.Net. Set this property to <b>true</b> <b>before</b>
  242. /// you access any FastReport .NET objects.
  243. /// </remarks>
  244. public static bool WebMode
  245. {
  246. get
  247. {
  248. return FWebMode;
  249. }
  250. set
  251. {
  252. FWebMode = value;
  253. if (value)
  254. ReportSettings.ShowProgress = false;
  255. }
  256. }
  257. /// <summary>
  258. /// Gets or sets the settings for the "Send Email" window.
  259. /// </summary>
  260. public static Export.Email.EmailSettings EmailSettings
  261. {
  262. get { return FEmailSettings; }
  263. set { FEmailSettings = value; }
  264. }
  265. /// <summary>
  266. /// Gets or sets a value that determines whether to paint designer surface on a separate (render) thread.
  267. /// </summary>
  268. /// <remarks>
  269. /// Default value is false.
  270. /// </remarks>
  271. public static bool AvaloniaPaintOnRenderThread { get; set; } = false;
  272. #endregion Public Properties
  273. #region Public Methods
  274. /// <summary>
  275. /// Restores the form state from the configuration file.
  276. /// </summary>
  277. /// <param name="form">The form to restore.</param>
  278. /// <remarks>Obsolete. Use either BaseForm.Storage or <see cref="FormStorageService"/>.</remarks>
  279. [Obsolete]
  280. public static void RestoreFormState(Form form)
  281. {
  282. new FormStorageService(form).RestoreFormState();
  283. }
  284. /// <summary>
  285. /// Saves the form state to the configuration file.
  286. /// </summary>
  287. /// <param name="form">The form to save.</param>
  288. /// <remarks>Obsolete. Use either BaseForm.Storage or <see cref="FormStorageService"/>.</remarks>
  289. [Obsolete]
  290. public static void SaveFormState(Form form)
  291. {
  292. new FormStorageService(form).SaveFormState();
  293. }
  294. static partial void SaveConnectionStringVisible()
  295. {
  296. new StorageService("Designer,PropertiesWindow").SetBool("ConnectionStringVisible", ConnectionStringVisible);
  297. }
  298. static partial void RestoreConnectionStringVisible()
  299. {
  300. ConnectionStringVisible = new StorageService("Designer,PropertiesWindow").GetBool("ConnectionStringVisible", true);
  301. }
  302. /// <summary>
  303. /// Create a dictionary of default keyboard shortcuts.
  304. /// </summary>
  305. /// <returns></returns>
  306. public static Dictionary<string, Keys> CreateDefaultShortcuts()
  307. {
  308. Dictionary<string, Keys> result = new Dictionary<string, Keys>
  309. {
  310. { "File,Open", Keys.Control | Keys.O },
  311. { "File,Save", Keys.Control | Keys.S },
  312. { "File,SaveAs", Keys.Control | Keys.Alt | Keys.S },
  313. { "File,SaveAll", Keys.Control | Keys.Shift | Keys.S },
  314. #if !COMMUNITY
  315. { "File,Preview", Keys.Control | Keys.P },
  316. #endif
  317. { "Editing,Undo", Keys.Control | Keys.Z },
  318. { "Editing,Redo", Keys.Control | Keys.Y },
  319. { "Editing,Cut", Keys.Control | Keys.X },
  320. { "Editing,Copy", Keys.Control | Keys.C },
  321. { "Editing,Paste", Keys.Control | Keys.V },
  322. { "Editing,NewPage", Keys.Control | Keys.N },
  323. { "Editing,Find", Keys.Control | Keys.F },
  324. { "Editing,Replace", Keys.Control | Keys.H },
  325. { "Editing,SelectAll", Keys.Control | Keys.A },
  326. { "Help,Contents", Keys.F1 },
  327. { "Editing,Bold", Keys.Control | Keys.B },
  328. { "Editing,Italic", Keys.Control | Keys.I },
  329. { "Editing,Underline", Keys.Control | Keys.U },
  330. };
  331. return result;
  332. }
  333. #endregion Public Methods
  334. #region Internal Methods
  335. internal static void DoEvent()
  336. {
  337. if (ProcessEvents && !WebMode)
  338. {
  339. System.Windows.Forms.Application.DoEvents();
  340. }
  341. }
  342. #endregion Internal Methods
  343. #region Private Methods
  344. private static void LoadPluginsInCurrentFolder()
  345. {
  346. var appFolder = ApplicationFolder;
  347. if (!string.IsNullOrEmpty(appFolder))
  348. {
  349. // find all plugin-connector in current directory
  350. var plugins = Directory.GetFiles(appFolder, "FastReport.Data.*.dll");
  351. // initialize
  352. foreach (var pluginName in plugins)
  353. {
  354. try
  355. {
  356. var assembly = Assembly.LoadFrom(pluginName);
  357. ProcessAssembly(assembly);
  358. }
  359. catch
  360. {
  361. }
  362. }
  363. }
  364. }
  365. /// <summary>
  366. /// Save default <see cref="Auth.AuthService"/> settings and user tokens.
  367. /// Is user have to consent to save data, clear the data in the config file.
  368. /// </summary>
  369. private static void SaveAuthServiceUser()
  370. {
  371. XmlItem xi = FDoc.Root.FindItem("Auth").FindItem("User");
  372. Auth.AuthService auth = Auth.AuthService.Instance;
  373. if (auth.IsEnable && auth.CanRefresh)
  374. {
  375. xi.ClearProps();
  376. xi.Clear();
  377. if (auth.User.RefreshToken != null)
  378. xi.SetProp("refresh_token", auth.User.RefreshToken);
  379. if (auth.User.IdToken != null)
  380. xi.SetProp("id_token", auth.User.IdToken);
  381. if (auth.User.Token != null)
  382. xi.SetProp("access_token", auth.User.Token);
  383. }
  384. else
  385. {
  386. xi.ClearProps();
  387. xi.Clear();
  388. }
  389. }
  390. /// <summary>
  391. /// Save default <see cref="Auth.AuthService"/> settings and user tokens.
  392. /// By default internal use only, it is able to be a public
  393. /// </summary>
  394. private static async void RestoreAuthServiceUser()
  395. {
  396. XmlItem xi = FDoc.Root.FindItem("Auth").FindItem("User");
  397. Auth.AuthService auth = Auth.AuthService.Instance;
  398. if (auth.IsEnable)
  399. {
  400. auth.User.Reset();
  401. auth.User.IdToken = xi.GetProp("id_token");
  402. auth.User.Token = xi.GetProp("access_token");
  403. auth.User.RefreshToken = xi.GetProp("refresh_token");
  404. FRCloudOptions.Instance.ResetBackendHostAndApiKey();
  405. bool isProgramStart = true;
  406. try
  407. {
  408. await Task.Factory.StartNew(() => auth.ParseTokens(isProgramStart));
  409. }
  410. catch
  411. #if DEBUG
  412. (Exception ex)
  413. #endif
  414. {
  415. auth.User.Reset();
  416. }
  417. }
  418. }
  419. #if !AVALONIA
  420. private static AspNetHostingPermissionLevel GetCurrentTrustLevel()
  421. {
  422. foreach (AspNetHostingPermissionLevel trustLevel in
  423. new AspNetHostingPermissionLevel[] {
  424. AspNetHostingPermissionLevel.Unrestricted,
  425. AspNetHostingPermissionLevel.High,
  426. AspNetHostingPermissionLevel.Medium,
  427. AspNetHostingPermissionLevel.Low,
  428. AspNetHostingPermissionLevel.Minimal
  429. })
  430. {
  431. try
  432. {
  433. new AspNetHostingPermission(trustLevel).Demand();
  434. }
  435. catch (System.Security.SecurityException)
  436. {
  437. continue;
  438. }
  439. return trustLevel;
  440. }
  441. return AspNetHostingPermissionLevel.None;
  442. }
  443. #endif
  444. private static void SaveUIStyle()
  445. {
  446. var storage = new StorageService("UIStyleNew");
  447. storage.SetEnum("Style", UIStyle);
  448. storage.SetBoolStr("Ribbon", UseRibbon); // compatible with prev versions
  449. storage.SetInt("IconPack", IconPack);
  450. storage.SetBool("UseCompactMenu", UseCompactMenu);
  451. }
  452. private static void RestoreUIStyle()
  453. {
  454. var storage = new StorageService("UIStyleNew");
  455. UIStyle = storage.GetEnum("Style", DEFAULT_UISTYLE);
  456. UseRibbon = storage.GetBool("Ribbon", true);
  457. IconPack = storage.GetInt("IconPack", 1);
  458. UseCompactMenu = storage.GetBool("UseCompactMenu", true);
  459. #if AVALONIA
  460. Application.InitControls();
  461. Application.InitThemeColors();
  462. #endif
  463. }
  464. private static void SaveUIOptions()
  465. {
  466. var storage = new StorageService("UIOptions");
  467. storage.SetBoolStr("DisableHotkeys", DisableHotkeys); // compatible with prev versions
  468. storage.SetBool("ApplyLastFormat", ApplyLastFormat);
  469. Dictionary<string, Keys> defaultShortcut = CreateDefaultShortcuts();
  470. foreach (var item in shortcuts)
  471. {
  472. if (!defaultShortcut.ContainsKey(item.Key) || item.Value != defaultShortcut[item.Key] || FDoc.Root.FindItem("Shortcuts," + item.Key) != null)
  473. {
  474. StorageService shortCut = new StorageService("Shortcuts," + item.Key);
  475. shortCut.SetEnum("Value", item.Value);
  476. }
  477. }
  478. }
  479. private static void RestoreUIOptions()
  480. {
  481. RestoreRightToLeft();
  482. var storage = new StorageService("UIOptions");
  483. DisableHotkeys = storage.GetBool("DisableHotkeys", false);
  484. ApplyLastFormat = storage.GetBool("ApplyLastFormat", true);
  485. if (Root.Find("Shortcuts") != -1)
  486. foreach (var item in Root.FindItem("Shortcuts").Items)
  487. {
  488. foreach (var subItem in item.Items)
  489. {
  490. var shortCut = new StorageService("Shortcuts," + item.Name + "," + subItem.Name);
  491. shortcuts[item.Name + "," + subItem.Name] = shortCut.GetEnum<Keys>("Value");
  492. }
  493. }
  494. }
  495. #if !COMMUNITY
  496. private static void RestoreExportOptions()
  497. {
  498. ExportsOptions.GetInstance().RestoreState();
  499. }
  500. private static void SaveExportOptions()
  501. {
  502. ExportsOptions.GetInstance().SaveState();
  503. }
  504. #endif
  505. #if COMMUNITY
  506. public static Image SplashScreen
  507. {
  508. get
  509. {
  510. return splashScreen;
  511. }
  512. set
  513. {
  514. splashScreen = value;
  515. }
  516. }
  517. public static Image WelcomeScreen
  518. {
  519. get
  520. {
  521. return welcomeScreen;
  522. }
  523. set
  524. {
  525. welcomeScreen = value;
  526. }
  527. }
  528. #endif
  529. #endregion Private Methods
  530. }
  531. }