DataBaseConfiguration.xaml.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Text.RegularExpressions;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Forms;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using Comal.Classes;
  19. using InABox.Clients;
  20. using InABox.Configuration;
  21. using InABox.Core;
  22. using InABox.DynamicGrid;
  23. using InABox.Wpf;
  24. using InABox.WPF;
  25. using PRSServer;
  26. using WindowsShortcutFactory;
  27. using Button = System.Windows.Controls.Button;
  28. using Color = System.Windows.Media.Color;
  29. using ColorConverter = System.Windows.Media.ColorConverter;
  30. using Image = System.Windows.Controls.Image;
  31. using KeyEventArgs = System.Windows.Input.KeyEventArgs;
  32. using MessageBox = System.Windows.MessageBox;
  33. //using IWshRuntimeLibrary;
  34. namespace PRSDesktop
  35. {
  36. /// <summary>
  37. /// Interaction logic for DataBaseConfiguration.xaml
  38. /// </summary>
  39. public partial class DataBaseConfiguration : ThemableWindow
  40. {
  41. private string _current = "";
  42. private Dictionary<string, DatabaseSettings> _settings;
  43. private bool bLoading = true;
  44. private List<string> startscreens = new();
  45. private Dictionary<string, Server> _servers;
  46. public DataBaseConfiguration()
  47. {
  48. InitializeComponent();
  49. Help.Content = new Image { Source = PRSDesktop.Resources.help.AsBitmapImage() };
  50. AutoUpdate.Content = new Image { Source = PRSDesktop.Resources.autoupdate.AsBitmapImage() };
  51. ExportSettings.Content = new Image { Source = PRSDesktop.Resources.download.AsBitmapImage() };
  52. ImportSettings.Content = new Image { Source = PRSDesktop.Resources.upload.AsBitmapImage() };
  53. var version = CoreUtils.GetVersion();
  54. var debug = string.Equals(version, "???");
  55. var hasLocal = HasLocalServer();
  56. if (!debug)
  57. {
  58. StandaloneHeader.Height = new GridLength(0, GridUnitType.Pixel);
  59. StandalonePath.Height = new GridLength(0, GridUnitType.Pixel);
  60. JobPrefixHeader.Height = new GridLength(0, GridUnitType.Pixel);
  61. JobPrefixContent.Height = new GridLength(0, GridUnitType.Pixel);
  62. POPrefixHeader.Height = new GridLength(0, GridUnitType.Pixel);
  63. POPrefixContent.Height = new GridLength(0, GridUnitType.Pixel);
  64. }
  65. if (!hasLocal)
  66. {
  67. LocalHeader.Height = new GridLength(0, GridUnitType.Pixel);
  68. LocalSelectionRow.Height = new GridLength(0, GridUnitType.Pixel);
  69. }
  70. else
  71. {
  72. var servers = GetLocalServerConfiguration().LoadAll();
  73. LocalServerName.Items.Clear();
  74. _servers = servers
  75. .Select(x => x.Value.CreateServer(x.Key))
  76. .Where(x => x.Type == ServerType.Database)
  77. .ToDictionary(x => x.Key, x => x);
  78. LocalServerName.ItemsSource = _servers;
  79. LocalServerName.DisplayMemberPath = "Value";
  80. }
  81. if (!debug && !hasLocal)
  82. {
  83. NetworkHeader.Height = new GridLength(0, GridUnitType.Pixel);
  84. NetworkPath.SetValue(Grid.ColumnProperty, 0);
  85. NetworkPath.SetValue(Grid.ColumnSpanProperty, 2);
  86. }
  87. AddProfile.Background = new ImageBrush(PRSDesktop.Resources.add.AsBitmapImage());
  88. DeleteProfile.Background = new ImageBrush(PRSDesktop.Resources.delete.AsBitmapImage());
  89. _settings = new LocalConfiguration<DatabaseSettings>().LoadAll();
  90. }
  91. public override void OnApplyTemplate()
  92. {
  93. base.OnApplyTemplate();
  94. Setup();
  95. bLoading = false;
  96. }
  97. //private String GetVersion()
  98. //{
  99. // String curfile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "version.txt");
  100. // //MessageBox.Show(curfile);
  101. // if (System.IO.File.Exists(curfile))
  102. // return System.IO.File.ReadAllText(curfile);
  103. // return "???";
  104. //}
  105. private LocalConfiguration<ServerSettings> GetLocalServerConfiguration(string? section = null)
  106. {
  107. return new LocalConfiguration<ServerSettings>(CoreUtils.GetCommonAppData("PRSServer"), section ?? "");
  108. }
  109. private bool HasLocalServer()
  110. {
  111. var configuration = GetLocalServerConfiguration();
  112. return File.Exists(configuration.GetFileName());
  113. }
  114. private void Setup()
  115. {
  116. Profiles.ItemsSource = new ObservableCollection<string>(_settings.Keys);
  117. _current = _settings.Keys.FirstOrDefault();
  118. Profiles.SelectedItem = _current;
  119. ProfileName.Text = _current;
  120. LoadSettings();
  121. Profiles.Focus();
  122. }
  123. private void Profiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
  124. {
  125. _current = Profiles.SelectedItem as string;
  126. if (!bLoading)
  127. {
  128. bLoading = true;
  129. ProfileName.Text = _current;
  130. LoadSettings();
  131. bLoading = false;
  132. }
  133. Focus();
  134. }
  135. private void AddProfile_MouseUp(object sender, MouseButtonEventArgs e)
  136. {
  137. var name = "PRS";
  138. var i = 1;
  139. while (_settings.ContainsKey(name))
  140. name = string.Format("PRS ({0})", i++);
  141. _settings[name] = new DatabaseSettings();
  142. var options = Profiles.ItemsSource as ObservableCollection<string>;
  143. options.Add(name);
  144. Profiles.SelectedItem = name;
  145. }
  146. private void DeleteProfile_MouseUp(object sender, MouseButtonEventArgs e)
  147. {
  148. if (_settings.Keys.Count > 1)
  149. {
  150. if (ShortcutExists(_current))
  151. DeleteShortcut(_current);
  152. _settings.Remove(_current);
  153. var options = Profiles.ItemsSource as ObservableCollection<string>;
  154. var index = options.IndexOf(_current);
  155. options.RemoveAt(index);
  156. Profiles.SelectedItem = options[Math.Min(index, Profiles.Items.Count - 1)];
  157. }
  158. }
  159. private void LoadSettings()
  160. {
  161. if (_current == null || !_settings.ContainsKey(_current))
  162. return;
  163. bLoading = true;
  164. var settings = _settings[_current];
  165. if (ShortcutExists(_current))
  166. CreateLink.Content = "Delete Link";
  167. else
  168. CreateLink.Content = "Create Link";
  169. IsActiveCheck.IsChecked = settings.IsActive;
  170. SplashSelect.Tag = settings.Logo;
  171. try
  172. {
  173. ColorScheme.SelectedColor = (System.Windows.Media.Color)ColorConverter.ConvertFromString(settings.ColorScheme);
  174. }
  175. catch
  176. {
  177. ColorScheme.SelectedColor = Colors.CornflowerBlue;
  178. }
  179. DBLocation.Text = settings.FileName;
  180. ServerURLs.Text = String.Join(";",settings.URLs);
  181. var prot = Math.Max(0, Math.Min((int)settings.Protocol, Protocol.Items.Count - 1));
  182. Protocol.SelectedIndex = prot;
  183. var debug = string.Equals(CoreUtils.GetVersion(), "???");
  184. var hasLocal = HasLocalServer();
  185. var databaseType = settings.DatabaseType;
  186. if (!debug && databaseType == DatabaseType.Standalone)
  187. databaseType = DatabaseType.Networked;
  188. if (!hasLocal && databaseType == DatabaseType.Local)
  189. databaseType = DatabaseType.Local;
  190. if (hasLocal)
  191. {
  192. LocalServerName.SelectedItem = _servers.Where(x => x.Key == settings.LocalServerName).FirstOrDefault();
  193. }
  194. StandaloneOption.IsChecked = databaseType == DatabaseType.Standalone;
  195. NetworkOption.IsChecked = databaseType == DatabaseType.Networked;
  196. LocalOption.IsChecked = databaseType == DatabaseType.Local;
  197. SelectOption(databaseType);
  198. LoginType.SelectedIndex = (int)settings.LoginType;
  199. SelectLogin(settings.LoginType);
  200. UserID.Text = settings.LoginType == Comal.Classes.LoginType.UserID ? settings.UserID : "";
  201. Password.Password = settings.LoginType == Comal.Classes.LoginType.UserID ? settings.Password : "";
  202. AutoLogin.IsChecked = settings.LoginType == Comal.Classes.LoginType.UserID ? settings.Autologin : false;
  203. //UpdateType.SelectedIndex = settings.UpdateType == AutoUpdateType.Url ? 0 : 1;
  204. //UpdateLocation.Text = settings.UpdateLocation;
  205. //UpdateAdmin.SelectedIndex = settings.UpdateAdmin ? 0 : 1;
  206. //UpdateChannel.SelectedIndex = settings.UpdateChannel == Comal.Classes.AutoUpdateChannel.Stable ? 0 : 1;
  207. LibraryLocation.Text = settings.LibraryLocation;
  208. GoogleAPIKey.Text = settings.GoogleAPIKey;
  209. JobPrefix.Text = settings.JobPrefix;
  210. PurchaseorderPrefix.Text = settings.PurchaseOrderPrefix;
  211. bLoading = false;
  212. }
  213. private static string MakeValidFileName(string name)
  214. {
  215. var invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
  216. var invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars);
  217. return Regex.Replace(name, invalidRegStr, "_");
  218. }
  219. private bool ShortcutExists(string profile)
  220. {
  221. var filename = Path.Combine(
  222. Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
  223. string.Format("{0}.lnk", MakeValidFileName(profile))
  224. );
  225. return File.Exists(filename);
  226. }
  227. private void DeleteShortcut(string profile)
  228. {
  229. var filename = Path.Combine(
  230. Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
  231. string.Format("{0}.lnk", MakeValidFileName(profile))
  232. );
  233. if (File.Exists(filename))
  234. File.Delete(filename);
  235. }
  236. private void CreateShortcut(string profile)
  237. {
  238. string filename = Path.Combine(
  239. Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
  240. String.Format("{0}.lnk", MakeValidFileName(profile))
  241. );
  242. if (System.IO.File.Exists(filename))
  243. return;
  244. using var shortcut = new WindowsShortcut
  245. {
  246. Path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName,
  247. Arguments = String.Format("/profile=\"{0}\"", profile),
  248. Description = String.Format("PRS - {0}",profile)
  249. };
  250. shortcut.Save(filename);
  251. //MessageBox.Show("Not Yet Implemented in .NET6!");
  252. // string filename = Path.Combine(
  253. // Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
  254. // String.Format("{0}.lnk", MakeValidFileName(profile))
  255. // );
  256. //
  257. // if (System.IO.File.Exists(filename))
  258. // return;
  259. //
  260. // WshShell shell = new WshShell();
  261. //
  262. // IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(filename);
  263. // shortcut.Description = String.Format("PRS - {0}",profile);
  264. // shortcut.TargetPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
  265. // shortcut.Arguments = String.Format("/profile=\"{0}\"", profile);
  266. // shortcut.Save();
  267. }
  268. private void UnloadSettings()
  269. {
  270. if (bLoading)
  271. return;
  272. if (!_settings.ContainsKey(_current))
  273. return;
  274. var settings = _settings[_current];
  275. settings.IsActive = IsActiveCheck.IsChecked == true;
  276. settings.Logo = (byte[])SplashSelect.Tag;
  277. settings.ColorScheme = ColorScheme.SelectedColor.ToString();
  278. settings.DatabaseType = StandaloneOption.IsChecked == true ? DatabaseType.Standalone :
  279. NetworkOption.IsChecked == true ? DatabaseType.Networked :
  280. LocalOption.IsChecked == true ? DatabaseType.Local :
  281. DatabaseType.Networked;
  282. settings.FileName = DBLocation.Text;
  283. settings.URLs = ServerURLs.Text.Split(';');
  284. settings.Protocol = (SerializerProtocol)Protocol.SelectedIndex;
  285. settings.LocalServerName = LocalServerName.SelectedItem != null ? ((KeyValuePair<string, Server>)LocalServerName.SelectedItem).Key : "";
  286. settings.LoginType = (LoginType)LoginType.SelectedIndex;
  287. settings.Autologin = settings.LoginType == Comal.Classes.LoginType.UserID ? AutoLogin.IsChecked == true : false;
  288. settings.UserID = settings.LoginType == Comal.Classes.LoginType.UserID ? UserID.Text : "";
  289. settings.Password = settings.LoginType == Comal.Classes.LoginType.UserID ? Password.Password : "";
  290. //settings.UpdateType = UpdateType.SelectedIndex == 0 ? AutoUpdateType.Url : AutoUpdateType.File;
  291. //settings.UpdateLocation = UpdateLocation.Text;
  292. //settings.UpdateAdmin = UpdateAdmin.SelectedIndex < 1;
  293. //settings.UpdateChannel = UpdateChannel.SelectedIndex < 1 ? Comal.Classes.AutoUpdateChannel.Stable : Comal.Classes.AutoUpdateChannel.PreRelease;
  294. settings.LibraryLocation = LibraryLocation.Text;
  295. settings.Provider = DatabaseProvider.SQLite;
  296. settings.GoogleAPIKey = GoogleAPIKey.Text;
  297. settings.JobPrefix = JobPrefix.Text;
  298. settings.PurchaseOrderPrefix = PurchaseorderPrefix.Text;
  299. }
  300. private void ProfileName_TextChanged(object sender, TextChangedEventArgs e)
  301. {
  302. if (bLoading)
  303. return;
  304. bLoading = true;
  305. var defset = _settings[_current];
  306. _settings.Remove(_current);
  307. _settings[ProfileName.Text] = defset;
  308. var options = Profiles.ItemsSource as ObservableCollection<string>;
  309. var cur = options.IndexOf(_current);
  310. options[cur] = ProfileName.Text;
  311. if (_current != null && ShortcutExists(_current))
  312. {
  313. DeleteShortcut(_current);
  314. CreateShortcut(ProfileName.Text);
  315. }
  316. _current = ProfileName.Text;
  317. bLoading = false;
  318. }
  319. #region DatabaseSettings
  320. private void SelectOption(DatabaseType databaseType)
  321. {
  322. DBLocation.IsEnabled = databaseType == DatabaseType.Standalone; // && (DbProvider.SelectedIndex == 1);
  323. DBButton.IsEnabled = databaseType == DatabaseType.Standalone; // && (DbProvider.SelectedIndex != 1);
  324. ColorScheme.IsEnabled = databaseType == DatabaseType.Standalone;
  325. SplashView.IsEnabled = databaseType == DatabaseType.Standalone;
  326. SplashSelect.IsEnabled = databaseType == DatabaseType.Standalone;
  327. ServerURLs.IsEnabled = databaseType == DatabaseType.Networked;
  328. Protocol.IsEnabled = databaseType == DatabaseType.Networked;
  329. LocalServerName.IsEnabled = databaseType == DatabaseType.Local;
  330. }
  331. private void DBButton_Click(object sender, RoutedEventArgs e)
  332. {
  333. using (var sfd = new SaveFileDialog())
  334. {
  335. //var provider = (DatabaseProvider)DbProvider.SelectedIndex;
  336. //if (provider == DatabaseProvider.SQLite)
  337. //{
  338. sfd.Filter = "SQLite Database Files (*.dbs)|*.dbs";
  339. sfd.DefaultExt = "dbs";
  340. //}
  341. //else
  342. //{
  343. // ofd.Filter = "LiteDB Database Files (*.db; *.dbl)|*.db;*.dbl";
  344. // ofd.DefaultExt = "dbl";
  345. //}
  346. sfd.InitialDirectory = Path.GetDirectoryName(DBLocation.Text);
  347. sfd.FileName = Path.GetFileName(DBLocation.Text);
  348. sfd.CheckFileExists = false;
  349. var result = sfd.ShowDialog();
  350. if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(sfd.FileName))
  351. {
  352. DBLocation.Text = sfd.FileName;
  353. UnloadSettings();
  354. }
  355. }
  356. }
  357. private void Option_Checked(object sender, RoutedEventArgs e)
  358. {
  359. var databaseType = sender == StandaloneOption ? DatabaseType.Standalone :
  360. sender == NetworkOption ? DatabaseType.Networked :
  361. DatabaseType.Local;
  362. SelectOption(databaseType);
  363. UnloadSettings();
  364. }
  365. private void StandaloneLabel_Click(object sender, MouseButtonEventArgs e)
  366. {
  367. StandaloneOption.IsChecked = true;
  368. }
  369. private void NetworkLabel_Click(object sender, MouseButtonEventArgs e)
  370. {
  371. NetworkOption.IsChecked = true;
  372. }
  373. private void LocalLabel_Click(object sender, MouseButtonEventArgs e)
  374. {
  375. LocalOption.IsChecked = true;
  376. }
  377. private void LocalServerName_SelectionChanged(object sender, SelectionChangedEventArgs e)
  378. {
  379. UnloadSettings();
  380. }
  381. private void ServerPort_TextChanged(object sender, TextChangedEventArgs e)
  382. {
  383. UnloadSettings();
  384. }
  385. private void ServerURL_TextChanged(object sender, TextChangedEventArgs e)
  386. {
  387. UnloadSettings();
  388. }
  389. private void Protocol_SelectionChanged(object sender, SelectionChangedEventArgs e)
  390. {
  391. UnloadSettings();
  392. }
  393. #endregion
  394. private void SelectLogin(LoginType Login)
  395. {
  396. UserID.IsEnabled = Login == Comal.Classes.LoginType.UserID;
  397. Password.IsEnabled = Login == Comal.Classes.LoginType.UserID;
  398. AutoLogin.IsEnabled = Login == Comal.Classes.LoginType.UserID;
  399. }
  400. //private void UpdateButton_Click(object sender, RoutedEventArgs e)
  401. //{
  402. // if (UpdateType.SelectedIndex != 1)
  403. // return;
  404. // using (var ofd = new OpenFileDialog())
  405. // {
  406. // ofd.Filter = "Executable Files (*.exe)|*.exe";
  407. // ofd.InitialDirectory = !String.IsNullOrEmpty(UpdateLocation.Text) ? Path.GetDirectoryName(UpdateLocation.Text) : "";
  408. // ofd.FileName = UpdateLocation.Text;
  409. // ofd.Multiselect = false;
  410. // ofd.CheckFileExists = false;
  411. // DialogResult result = ofd.ShowDialog();
  412. // if ((result == System.Windows.Forms.DialogResult.OK) && (!string.IsNullOrWhiteSpace(ofd.FileName)))
  413. // {
  414. // UpdateLocation.Text = ofd.FileName;
  415. // UnloadSettings();
  416. // }
  417. // }
  418. //}
  419. private void AutoDiscover_Click(object sender, RoutedEventArgs e)
  420. {
  421. Progress.Show("Looking for available Servers...");
  422. try
  423. {
  424. var Client = new UdpClient();
  425. Client.Client.SendTimeout = 10000;
  426. Client.Client.ReceiveTimeout = 20000;
  427. var RequestData = Encoding.ASCII.GetBytes("");
  428. var ServerEp = new IPEndPoint(IPAddress.Any, 0);
  429. Client.EnableBroadcast = true;
  430. Client.Send(RequestData, RequestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888));
  431. Progress.SetMessage("Scanning local network...");
  432. var ServerResponseData = Client.Receive(ref ServerEp);
  433. var ServerResponse = Encoding.ASCII.GetString(ServerResponseData);
  434. var autodiscover = Serialization.Deserialize<AutoDiscoverySettings>(ServerResponse);
  435. ProfileName.Text = autodiscover.Name;
  436. IsActiveCheck.IsChecked = true;
  437. SplashSelect.Tag = autodiscover.Logo;
  438. StandaloneOption.IsChecked = false;
  439. DBLocation.Text = "";
  440. NetworkOption.IsChecked = true;
  441. Protocol.SelectedIndex = Math.Max(0, Math.Min((int)autodiscover.Protocol, Protocol.Items.Count - 1));
  442. ServerURLs.Text = String.Join(";",autodiscover.URLs);
  443. //UpdateType.SelectedIndex = autodiscover.UpdateType == AutoUpdateType.Url ? 0 : 1;
  444. //UpdateLocation.Text = autodiscover.UpdateLocation;
  445. //UpdateAdmin.SelectedIndex = autodiscover.UpdateAdmin ? 0 : 1;
  446. //UpdateChannel.SelectedIndex = autodiscover.UpdateChannel == Comal.Classes.AutoUpdateChannel.Stable ? 0 : 1;
  447. LibraryLocation.Text = autodiscover.LibraryLocation;
  448. GoogleAPIKey.Text = autodiscover.GoogleAPIKey;
  449. Client.Close();
  450. Progress.Close();
  451. MessageBox.Show(String.Format("Server found at {0}", String.Join(";",autodiscover.URLs)));
  452. UnloadSettings();
  453. }
  454. catch (Exception err)
  455. {
  456. Progress.Close();
  457. MessageBox.Show("No Server Found!");
  458. }
  459. }
  460. private void LoginType_SelectionChanged(object sender, SelectionChangedEventArgs e)
  461. {
  462. SelectLogin((LoginType)LoginType.SelectedIndex);
  463. UnloadSettings();
  464. }
  465. private void LibraryButton_Click(object sender, RoutedEventArgs e)
  466. {
  467. using (var ofd = new FolderBrowserDialog())
  468. {
  469. if (Directory.Exists(LibraryLocation.Text))
  470. ofd.SelectedPath = LibraryLocation.Text;
  471. var result = ofd.ShowDialog();
  472. if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(ofd.SelectedPath))
  473. {
  474. LibraryLocation.Text = ofd.SelectedPath;
  475. UnloadSettings();
  476. }
  477. }
  478. }
  479. //private void UpdateType_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  480. //{
  481. // UpdateButton.Visibility = UpdateType.SelectedIndex == 1 ? Visibility.Visible : Visibility.Collapsed;
  482. // UnloadSettings();
  483. //}
  484. //private void UpdateAdmin_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  485. //{
  486. // UnloadSettings();
  487. //}
  488. //private void UpdateChannel_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  489. //{
  490. // UnloadSettings();
  491. //}
  492. private void IsActive_Checked(object sender, RoutedEventArgs e)
  493. {
  494. UnloadSettings();
  495. }
  496. private void UserID_TextChanged(object sender, TextChangedEventArgs e)
  497. {
  498. UnloadSettings();
  499. }
  500. private void Password_KeyUp(object sender, KeyEventArgs e)
  501. {
  502. UnloadSettings();
  503. }
  504. private void AutoLogin_Checked(object sender, RoutedEventArgs e)
  505. {
  506. UnloadSettings();
  507. }
  508. //private void UpdateLocation_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
  509. //{
  510. // UnloadSettings();
  511. //}
  512. private void LibraryLocation_TextChanged(object sender, TextChangedEventArgs e)
  513. {
  514. UnloadSettings();
  515. }
  516. private void UpdateAdmin_Checked(object sender, RoutedEventArgs e)
  517. {
  518. UnloadSettings();
  519. }
  520. private void GoogleAPIKey_TextChanged(object sender, TextChangedEventArgs e)
  521. {
  522. UnloadSettings();
  523. }
  524. private void JobPrefix_TextChanged(object sender, TextChangedEventArgs e)
  525. {
  526. UnloadSettings();
  527. }
  528. private void PurchaseOrderPrefix_TextChanged(object sender, TextChangedEventArgs e)
  529. {
  530. UnloadSettings();
  531. }
  532. private void ColorScheme_OnSelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color?> e)
  533. {
  534. UnloadSettings();
  535. }
  536. private void SplashSelect_Click(object sender, RoutedEventArgs e)
  537. {
  538. using (var ofd = new OpenFileDialog())
  539. {
  540. ofd.Filter = "Image Files (*.bmp; *.jpg; *.jpeg; *.png)|*.bmp;*.jpg;*.jpeg;*.png";
  541. ofd.DefaultExt = "png";
  542. ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  543. ofd.Multiselect = false;
  544. ofd.CheckFileExists = true;
  545. var result = ofd.ShowDialog();
  546. if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(ofd.FileName))
  547. {
  548. var image = System.Drawing.Image.FromFile(ofd.FileName);
  549. var size = image.Size.Adjust(450, 450);
  550. image = new Bitmap(image, size);
  551. SplashSelect.Tag = new ImageConverter().ConvertTo(image, typeof(byte[]));
  552. UnloadSettings();
  553. }
  554. }
  555. }
  556. private void SplashView_Click(object sender, RoutedEventArgs e)
  557. {
  558. var data = SplashSelect.Tag != null ? (byte[])SplashSelect.Tag : null;
  559. if (data == null || !data.Any())
  560. return;
  561. var type = ImageUtils.GetImageType(data);
  562. var file = Path.ChangeExtension(Path.GetTempFileName(), type.ToString());
  563. File.WriteAllBytes(file, data);
  564. var gsProcessInfo = new ProcessStartInfo();
  565. gsProcessInfo.Verb = "open";
  566. gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  567. gsProcessInfo.FileName = file;
  568. gsProcessInfo.UseShellExecute = true;
  569. Process.Start(gsProcessInfo);
  570. }
  571. private void CreateLink_Click(object sender, RoutedEventArgs e)
  572. {
  573. var button = sender as Button;
  574. var action = button.Content as string;
  575. if (string.Equals("Delete Link", action))
  576. {
  577. DeleteShortcut(_current);
  578. button.Content = "Create Link";
  579. }
  580. else
  581. {
  582. CreateShortcut(_current);
  583. button.Content = "Delete Link";
  584. }
  585. }
  586. private void OKButton_Click(object sender, RoutedEventArgs e)
  587. {
  588. // Delete All
  589. var olds = new LocalConfiguration<DatabaseSettings>().Sections();
  590. foreach (var old in olds)
  591. new LocalConfiguration<DatabaseSettings>(old).Delete();
  592. foreach (var key in _settings.Keys)
  593. new LocalConfiguration<DatabaseSettings>(key).Save(_settings[key]);
  594. DialogResult = true;
  595. Close();
  596. }
  597. private void CancelButton_Click(object sender, RoutedEventArgs e)
  598. {
  599. DialogResult = false;
  600. Close();
  601. }
  602. private void Export_Click(object sender, RoutedEventArgs e)
  603. {
  604. using (var sfd = new SaveFileDialog())
  605. {
  606. sfd.Filter = "Settings Files (*.settings)|*.settings";
  607. sfd.DefaultExt = "settings";
  608. sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  609. var result = sfd.ShowDialog();
  610. if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(sfd.FileName))
  611. {
  612. var json = Serialization.Serialize(_settings);
  613. File.WriteAllText(sfd.FileName, json);
  614. MessageBox.Show("Settings exported!");
  615. }
  616. }
  617. }
  618. private void Import_Click(object sender, RoutedEventArgs e)
  619. {
  620. using (var ofd = new OpenFileDialog())
  621. {
  622. ofd.Filter = "Settings Files (*.settings)|*.settings";
  623. ofd.DefaultExt = "settings";
  624. ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  625. ofd.Multiselect = false;
  626. ofd.CheckFileExists = true;
  627. var result = ofd.ShowDialog();
  628. if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(ofd.FileName))
  629. {
  630. var json = File.ReadAllText(ofd.FileName);
  631. _settings = Serialization.Deserialize<Dictionary<string, DatabaseSettings>>(json);
  632. Setup();
  633. }
  634. }
  635. }
  636. private void AutoUpdate_Click(object sender, RoutedEventArgs e)
  637. {
  638. var editable = App.AutoUpdateSettings.ToEditable();
  639. var buttons = new DynamicEditorButtons()
  640. {
  641. new DynamicEditorButton(
  642. "",
  643. PRSDesktop.Resources.help.AsBitmapImage(),
  644. null,
  645. (o, e) => ShowHelp("Automatic_Updates"))
  646. };
  647. var propertyEditor = new DynamicEditorForm(typeof(EditableAutoUpdateSettings), null, buttons);
  648. propertyEditor.OnDefineLookups += editor =>
  649. {
  650. var values = (editor.EditorDefinition as ILookupEditor).Values(editor.ColumnName);
  651. editor.LoadLookups(values);
  652. };
  653. propertyEditor.OnDefineEditor += PropertyEditor_OnDefineEditor;
  654. propertyEditor.Items = new BaseObject[] { editable };
  655. if (propertyEditor.ShowDialog() == true)
  656. {
  657. App.AutoUpdateSettings.FromEditable(editable);
  658. new LocalConfiguration<AutoUpdateSettings>().Save(App.AutoUpdateSettings);
  659. }
  660. }
  661. private BaseEditor? PropertyEditor_OnDefineEditor(object item, DynamicGridColumn column)
  662. {
  663. if (!string.Equals(column.ColumnName, "Elevated"))
  664. return new NullEditor();
  665. return null;
  666. }
  667. private void ShowHelp(string slug)
  668. {
  669. Process.Start(new ProcessStartInfo("https://prsdigital.com.au/wiki/index.php/" + slug) { UseShellExecute = true });
  670. }
  671. private void ShowHelp_OnClick(object sender, RoutedEventArgs e)
  672. {
  673. ShowHelp("Database_Configuration");
  674. }
  675. }
  676. }