DataBaseConfiguration.xaml.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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. var debug = string.Equals(CoreUtils.GetVersion(), "???");
  180. var hasLocal = HasLocalServer();
  181. var databaseType = settings.DatabaseType;
  182. if (!debug && databaseType == DatabaseType.Standalone)
  183. databaseType = DatabaseType.Networked;
  184. if (!hasLocal && databaseType == DatabaseType.Local)
  185. databaseType = DatabaseType.Local;
  186. StandaloneOption.IsChecked = databaseType == DatabaseType.Standalone;
  187. if (StandaloneOption.IsChecked == true)
  188. {
  189. DBLocation.Text = settings.FileName;
  190. ServerURLs.Text = "";
  191. Protocol.SelectedIndex = -1;
  192. LocalServerName.SelectedItem = null;
  193. }
  194. NetworkOption.IsChecked = databaseType == DatabaseType.Networked;
  195. if (NetworkOption.IsChecked == true)
  196. {
  197. DBLocation.Text = "";
  198. ServerURLs.Text = String.Join(";",settings.URLs);
  199. var prot = Math.Max(0, Math.Min((int)settings.Protocol, Protocol.Items.Count - 1));
  200. Protocol.SelectedIndex = prot;
  201. LocalServerName.SelectedItem = null;
  202. }
  203. LocalOption.IsChecked = databaseType == DatabaseType.Local;
  204. if (LocalOption.IsChecked == true)
  205. {
  206. DBLocation.Text = "";
  207. ServerURLs.Text = "";
  208. Protocol.SelectedIndex = -1;
  209. LocalServerName.SelectedItem = _servers.Where(x => x.Key == settings.LocalServerName).FirstOrDefault();
  210. }
  211. SelectOption(databaseType);
  212. LoginType.SelectedIndex = (int)settings.LoginType;
  213. SelectLogin(settings.LoginType);
  214. UserID.Text = settings.LoginType == Comal.Classes.LoginType.UserID ? settings.UserID : "";
  215. Password.Password = settings.LoginType == Comal.Classes.LoginType.UserID ? settings.Password : "";
  216. AutoLogin.IsChecked = settings.LoginType == Comal.Classes.LoginType.UserID ? settings.Autologin : false;
  217. //UpdateType.SelectedIndex = settings.UpdateType == AutoUpdateType.Url ? 0 : 1;
  218. //UpdateLocation.Text = settings.UpdateLocation;
  219. //UpdateAdmin.SelectedIndex = settings.UpdateAdmin ? 0 : 1;
  220. //UpdateChannel.SelectedIndex = settings.UpdateChannel == Comal.Classes.AutoUpdateChannel.Stable ? 0 : 1;
  221. LibraryLocation.Text = settings.LibraryLocation;
  222. GoogleAPIKey.Text = settings.GoogleAPIKey;
  223. JobPrefix.Text = settings.JobPrefix;
  224. PurchaseorderPrefix.Text = settings.PurchaseOrderPrefix;
  225. bLoading = false;
  226. }
  227. private static string MakeValidFileName(string name)
  228. {
  229. var invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
  230. var invalidRegStr = string.Format(@"([{0}]*\.+$)|([{0}]+)", invalidChars);
  231. return Regex.Replace(name, invalidRegStr, "_");
  232. }
  233. private bool ShortcutExists(string profile)
  234. {
  235. var filename = Path.Combine(
  236. Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
  237. string.Format("{0}.lnk", MakeValidFileName(profile))
  238. );
  239. return File.Exists(filename);
  240. }
  241. private void DeleteShortcut(string profile)
  242. {
  243. var filename = Path.Combine(
  244. Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
  245. string.Format("{0}.lnk", MakeValidFileName(profile))
  246. );
  247. if (File.Exists(filename))
  248. File.Delete(filename);
  249. }
  250. private void CreateShortcut(string profile)
  251. {
  252. string filename = Path.Combine(
  253. Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
  254. String.Format("{0}.lnk", MakeValidFileName(profile))
  255. );
  256. if (System.IO.File.Exists(filename))
  257. return;
  258. using var shortcut = new WindowsShortcut
  259. {
  260. Path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName,
  261. Arguments = String.Format("/profile=\"{0}\"", profile),
  262. Description = String.Format("PRS - {0}",profile)
  263. };
  264. shortcut.Save(filename);
  265. //MessageBox.Show("Not Yet Implemented in .NET6!");
  266. // string filename = Path.Combine(
  267. // Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
  268. // String.Format("{0}.lnk", MakeValidFileName(profile))
  269. // );
  270. //
  271. // if (System.IO.File.Exists(filename))
  272. // return;
  273. //
  274. // WshShell shell = new WshShell();
  275. //
  276. // IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(filename);
  277. // shortcut.Description = String.Format("PRS - {0}",profile);
  278. // shortcut.TargetPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
  279. // shortcut.Arguments = String.Format("/profile=\"{0}\"", profile);
  280. // shortcut.Save();
  281. }
  282. private void UnloadSettings()
  283. {
  284. if (bLoading)
  285. return;
  286. if (!_settings.ContainsKey(_current))
  287. return;
  288. var settings = _settings[_current];
  289. settings.IsActive = IsActiveCheck.IsChecked == true;
  290. settings.Logo = (byte[])SplashSelect.Tag;
  291. settings.ColorScheme = ColorScheme.SelectedColor.ToString();
  292. settings.DatabaseType = StandaloneOption.IsChecked == true ? DatabaseType.Standalone :
  293. NetworkOption.IsChecked == true ? DatabaseType.Networked :
  294. LocalOption.IsChecked == true ? DatabaseType.Local :
  295. DatabaseType.Networked;
  296. settings.FileName = DBLocation.Text;
  297. settings.URLs = ServerURLs.Text.Split(';');
  298. settings.Protocol = (SerializerProtocol)Protocol.SelectedIndex;
  299. settings.LocalServerName = LocalServerName.SelectedItem != null ? ((KeyValuePair<string, Server>)LocalServerName.SelectedItem).Key : "";
  300. settings.LoginType = (LoginType)LoginType.SelectedIndex;
  301. settings.Autologin = settings.LoginType == Comal.Classes.LoginType.UserID ? AutoLogin.IsChecked == true : false;
  302. settings.UserID = settings.LoginType == Comal.Classes.LoginType.UserID ? UserID.Text : "";
  303. settings.Password = settings.LoginType == Comal.Classes.LoginType.UserID ? Password.Password : "";
  304. //settings.UpdateType = UpdateType.SelectedIndex == 0 ? AutoUpdateType.Url : AutoUpdateType.File;
  305. //settings.UpdateLocation = UpdateLocation.Text;
  306. //settings.UpdateAdmin = UpdateAdmin.SelectedIndex < 1;
  307. //settings.UpdateChannel = UpdateChannel.SelectedIndex < 1 ? Comal.Classes.AutoUpdateChannel.Stable : Comal.Classes.AutoUpdateChannel.PreRelease;
  308. settings.LibraryLocation = LibraryLocation.Text;
  309. settings.Provider = DatabaseProvider.SQLite;
  310. settings.GoogleAPIKey = GoogleAPIKey.Text;
  311. settings.JobPrefix = JobPrefix.Text;
  312. settings.PurchaseOrderPrefix = PurchaseorderPrefix.Text;
  313. }
  314. private void ProfileName_TextChanged(object sender, TextChangedEventArgs e)
  315. {
  316. if (bLoading)
  317. return;
  318. bLoading = true;
  319. var defset = _settings[_current];
  320. _settings.Remove(_current);
  321. _settings[ProfileName.Text] = defset;
  322. var options = Profiles.ItemsSource as ObservableCollection<string>;
  323. var cur = options.IndexOf(_current);
  324. options[cur] = ProfileName.Text;
  325. if (_current != null && ShortcutExists(_current))
  326. {
  327. DeleteShortcut(_current);
  328. CreateShortcut(ProfileName.Text);
  329. }
  330. _current = ProfileName.Text;
  331. bLoading = false;
  332. }
  333. #region DatabaseSettings
  334. private void SelectOption(DatabaseType databaseType)
  335. {
  336. DBLocation.IsEnabled = databaseType == DatabaseType.Standalone; // && (DbProvider.SelectedIndex == 1);
  337. DBButton.IsEnabled = databaseType == DatabaseType.Standalone; // && (DbProvider.SelectedIndex != 1);
  338. ColorScheme.IsEnabled = databaseType == DatabaseType.Standalone;
  339. SplashView.IsEnabled = databaseType == DatabaseType.Standalone;
  340. SplashSelect.IsEnabled = databaseType == DatabaseType.Standalone;
  341. ServerURLs.IsEnabled = databaseType == DatabaseType.Networked;
  342. Protocol.IsEnabled = databaseType == DatabaseType.Networked;
  343. LocalServerName.IsEnabled = databaseType == DatabaseType.Local;
  344. }
  345. private void DBButton_Click(object sender, RoutedEventArgs e)
  346. {
  347. using (var sfd = new SaveFileDialog())
  348. {
  349. //var provider = (DatabaseProvider)DbProvider.SelectedIndex;
  350. //if (provider == DatabaseProvider.SQLite)
  351. //{
  352. sfd.Filter = "SQLite Database Files (*.dbs)|*.dbs";
  353. sfd.DefaultExt = "dbs";
  354. //}
  355. //else
  356. //{
  357. // ofd.Filter = "LiteDB Database Files (*.db; *.dbl)|*.db;*.dbl";
  358. // ofd.DefaultExt = "dbl";
  359. //}
  360. sfd.InitialDirectory = Path.GetDirectoryName(DBLocation.Text);
  361. sfd.FileName = Path.GetFileName(DBLocation.Text);
  362. sfd.CheckFileExists = false;
  363. var result = sfd.ShowDialog();
  364. if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(sfd.FileName))
  365. {
  366. DBLocation.Text = sfd.FileName;
  367. UnloadSettings();
  368. }
  369. }
  370. }
  371. private void Option_Checked(object sender, RoutedEventArgs e)
  372. {
  373. var databaseType = sender == StandaloneOption ? DatabaseType.Standalone :
  374. sender == NetworkOption ? DatabaseType.Networked :
  375. DatabaseType.Local;
  376. SelectOption(databaseType);
  377. UnloadSettings();
  378. }
  379. private void StandaloneLabel_Click(object sender, MouseButtonEventArgs e)
  380. {
  381. StandaloneOption.IsChecked = true;
  382. }
  383. private void NetworkLabel_Click(object sender, MouseButtonEventArgs e)
  384. {
  385. NetworkOption.IsChecked = true;
  386. }
  387. private void LocalLabel_Click(object sender, MouseButtonEventArgs e)
  388. {
  389. LocalOption.IsChecked = true;
  390. }
  391. private void LocalServerName_SelectionChanged(object sender, SelectionChangedEventArgs e)
  392. {
  393. UnloadSettings();
  394. }
  395. private void ServerPort_TextChanged(object sender, TextChangedEventArgs e)
  396. {
  397. UnloadSettings();
  398. }
  399. private void ServerURL_TextChanged(object sender, TextChangedEventArgs e)
  400. {
  401. UnloadSettings();
  402. }
  403. private void Protocol_SelectionChanged(object sender, SelectionChangedEventArgs e)
  404. {
  405. UnloadSettings();
  406. }
  407. #endregion
  408. private void SelectLogin(LoginType Login)
  409. {
  410. UserID.IsEnabled = Login == Comal.Classes.LoginType.UserID;
  411. Password.IsEnabled = Login == Comal.Classes.LoginType.UserID;
  412. AutoLogin.IsEnabled = Login == Comal.Classes.LoginType.UserID;
  413. }
  414. //private void UpdateButton_Click(object sender, RoutedEventArgs e)
  415. //{
  416. // if (UpdateType.SelectedIndex != 1)
  417. // return;
  418. // using (var ofd = new OpenFileDialog())
  419. // {
  420. // ofd.Filter = "Executable Files (*.exe)|*.exe";
  421. // ofd.InitialDirectory = !String.IsNullOrEmpty(UpdateLocation.Text) ? Path.GetDirectoryName(UpdateLocation.Text) : "";
  422. // ofd.FileName = UpdateLocation.Text;
  423. // ofd.Multiselect = false;
  424. // ofd.CheckFileExists = false;
  425. // DialogResult result = ofd.ShowDialog();
  426. // if ((result == System.Windows.Forms.DialogResult.OK) && (!string.IsNullOrWhiteSpace(ofd.FileName)))
  427. // {
  428. // UpdateLocation.Text = ofd.FileName;
  429. // UnloadSettings();
  430. // }
  431. // }
  432. //}
  433. public static AutoDiscoverySettings? AutoDiscoverServer()
  434. {
  435. Progress.Show("Looking for available Servers...");
  436. try
  437. {
  438. var Client = new UdpClient();
  439. Client.Client.SendTimeout = 10000;
  440. Client.Client.ReceiveTimeout = 20000;
  441. var RequestData = Encoding.ASCII.GetBytes("");
  442. var ServerEp = new IPEndPoint(IPAddress.Any, 0);
  443. Client.EnableBroadcast = true;
  444. Client.Send(RequestData, RequestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888));
  445. Progress.SetMessage("Scanning local network...");
  446. var ServerResponseData = Client.Receive(ref ServerEp);
  447. var ServerResponse = Encoding.ASCII.GetString(ServerResponseData);
  448. var autodiscover = Serialization.Deserialize<AutoDiscoverySettings>(ServerResponse);
  449. Client.Close();
  450. Progress.Close();
  451. return autodiscover;
  452. }
  453. catch (Exception err)
  454. {
  455. Logger.Send(LogType.Error, "", $"No server found: {CoreUtils.FormatException(err)}");
  456. return null;
  457. }
  458. }
  459. private void AutoDiscover_Click(object sender, RoutedEventArgs e)
  460. {
  461. Progress.Show("Looking for available Servers...");
  462. var autodiscover = AutoDiscoverServer();
  463. Progress.Close();
  464. if(autodiscover is not null)
  465. {
  466. ProfileName.Text = autodiscover.Name;
  467. IsActiveCheck.IsChecked = true;
  468. SplashSelect.Tag = autodiscover.Logo;
  469. StandaloneOption.IsChecked = false;
  470. DBLocation.Text = "";
  471. NetworkOption.IsChecked = true;
  472. Protocol.SelectedIndex = Math.Max(0, Math.Min((int)autodiscover.Protocol, Protocol.Items.Count - 1));
  473. ServerURLs.Text = String.Join(";",autodiscover.URLs);
  474. //UpdateType.SelectedIndex = autodiscover.UpdateType == AutoUpdateType.Url ? 0 : 1;
  475. //UpdateLocation.Text = autodiscover.UpdateLocation;
  476. //UpdateAdmin.SelectedIndex = autodiscover.UpdateAdmin ? 0 : 1;
  477. //UpdateChannel.SelectedIndex = autodiscover.UpdateChannel == Comal.Classes.AutoUpdateChannel.Stable ? 0 : 1;
  478. LibraryLocation.Text = autodiscover.LibraryLocation;
  479. GoogleAPIKey.Text = autodiscover.GoogleAPIKey;
  480. MessageBox.Show(String.Format("Server found at {0}", String.Join(";",autodiscover.URLs)));
  481. UnloadSettings();
  482. }
  483. else
  484. {
  485. MessageBox.Show("No Server Found!");
  486. }
  487. }
  488. private void LoginType_SelectionChanged(object sender, SelectionChangedEventArgs e)
  489. {
  490. SelectLogin((LoginType)LoginType.SelectedIndex);
  491. UnloadSettings();
  492. }
  493. private void LibraryButton_Click(object sender, RoutedEventArgs e)
  494. {
  495. using (var ofd = new FolderBrowserDialog())
  496. {
  497. if (Directory.Exists(LibraryLocation.Text))
  498. ofd.SelectedPath = LibraryLocation.Text;
  499. var result = ofd.ShowDialog();
  500. if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(ofd.SelectedPath))
  501. {
  502. LibraryLocation.Text = ofd.SelectedPath;
  503. UnloadSettings();
  504. }
  505. }
  506. }
  507. //private void UpdateType_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  508. //{
  509. // UpdateButton.Visibility = UpdateType.SelectedIndex == 1 ? Visibility.Visible : Visibility.Collapsed;
  510. // UnloadSettings();
  511. //}
  512. //private void UpdateAdmin_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  513. //{
  514. // UnloadSettings();
  515. //}
  516. //private void UpdateChannel_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  517. //{
  518. // UnloadSettings();
  519. //}
  520. private void IsActive_Checked(object sender, RoutedEventArgs e)
  521. {
  522. UnloadSettings();
  523. }
  524. private void UserID_TextChanged(object sender, TextChangedEventArgs e)
  525. {
  526. UnloadSettings();
  527. }
  528. private void Password_KeyUp(object sender, KeyEventArgs e)
  529. {
  530. UnloadSettings();
  531. }
  532. private void AutoLogin_Checked(object sender, RoutedEventArgs e)
  533. {
  534. UnloadSettings();
  535. }
  536. //private void UpdateLocation_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
  537. //{
  538. // UnloadSettings();
  539. //}
  540. private void LibraryLocation_TextChanged(object sender, TextChangedEventArgs e)
  541. {
  542. UnloadSettings();
  543. }
  544. private void UpdateAdmin_Checked(object sender, RoutedEventArgs e)
  545. {
  546. UnloadSettings();
  547. }
  548. private void GoogleAPIKey_TextChanged(object sender, TextChangedEventArgs e)
  549. {
  550. UnloadSettings();
  551. }
  552. private void JobPrefix_TextChanged(object sender, TextChangedEventArgs e)
  553. {
  554. UnloadSettings();
  555. }
  556. private void PurchaseOrderPrefix_TextChanged(object sender, TextChangedEventArgs e)
  557. {
  558. UnloadSettings();
  559. }
  560. private void ColorScheme_OnSelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<Color?> e)
  561. {
  562. UnloadSettings();
  563. }
  564. private void SplashSelect_Click(object sender, RoutedEventArgs e)
  565. {
  566. using (var ofd = new OpenFileDialog())
  567. {
  568. ofd.Filter = "Image Files (*.bmp; *.jpg; *.jpeg; *.png)|*.bmp;*.jpg;*.jpeg;*.png";
  569. ofd.DefaultExt = "png";
  570. ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  571. ofd.Multiselect = false;
  572. ofd.CheckFileExists = true;
  573. var result = ofd.ShowDialog();
  574. if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(ofd.FileName))
  575. {
  576. var image = System.Drawing.Image.FromFile(ofd.FileName);
  577. var size = image.Size.Adjust(450, 450);
  578. image = new Bitmap(image, size);
  579. SplashSelect.Tag = new ImageConverter().ConvertTo(image, typeof(byte[]));
  580. UnloadSettings();
  581. }
  582. }
  583. }
  584. private void SplashView_Click(object sender, RoutedEventArgs e)
  585. {
  586. var data = SplashSelect.Tag != null ? (byte[])SplashSelect.Tag : null;
  587. if (data == null || !data.Any())
  588. return;
  589. var type = ImageUtils.GetImageType(data);
  590. var file = Path.ChangeExtension(Path.GetTempFileName(), type.ToString());
  591. File.WriteAllBytes(file, data);
  592. var gsProcessInfo = new ProcessStartInfo();
  593. gsProcessInfo.Verb = "open";
  594. gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  595. gsProcessInfo.FileName = file;
  596. gsProcessInfo.UseShellExecute = true;
  597. Process.Start(gsProcessInfo);
  598. }
  599. private void CreateLink_Click(object sender, RoutedEventArgs e)
  600. {
  601. var button = sender as Button;
  602. var action = button.Content as string;
  603. if (string.Equals("Delete Link", action))
  604. {
  605. DeleteShortcut(_current);
  606. button.Content = "Create Link";
  607. }
  608. else
  609. {
  610. CreateShortcut(_current);
  611. button.Content = "Delete Link";
  612. }
  613. }
  614. private void OKButton_Click(object sender, RoutedEventArgs e)
  615. {
  616. // Delete All
  617. var olds = new LocalConfiguration<DatabaseSettings>().Sections();
  618. foreach (var old in olds)
  619. new LocalConfiguration<DatabaseSettings>(old).Delete();
  620. foreach (var key in _settings.Keys)
  621. new LocalConfiguration<DatabaseSettings>(key).Save(_settings[key]);
  622. DialogResult = true;
  623. Close();
  624. }
  625. private void CancelButton_Click(object sender, RoutedEventArgs e)
  626. {
  627. DialogResult = false;
  628. Close();
  629. }
  630. private void Export_Click(object sender, RoutedEventArgs e)
  631. {
  632. using (var sfd = new SaveFileDialog())
  633. {
  634. sfd.Filter = "Settings Files (*.settings)|*.settings";
  635. sfd.DefaultExt = "settings";
  636. sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  637. var result = sfd.ShowDialog();
  638. if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(sfd.FileName))
  639. {
  640. var json = Serialization.Serialize(_settings);
  641. File.WriteAllText(sfd.FileName, json);
  642. MessageBox.Show("Settings exported!");
  643. }
  644. }
  645. }
  646. private void Import_Click(object sender, RoutedEventArgs e)
  647. {
  648. using (var ofd = new OpenFileDialog())
  649. {
  650. ofd.Filter = "Settings Files (*.settings)|*.settings";
  651. ofd.DefaultExt = "settings";
  652. ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  653. ofd.Multiselect = false;
  654. ofd.CheckFileExists = true;
  655. var result = ofd.ShowDialog();
  656. if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(ofd.FileName))
  657. {
  658. var json = File.ReadAllText(ofd.FileName);
  659. _settings = Serialization.Deserialize<Dictionary<string, DatabaseSettings>>(json);
  660. Setup();
  661. }
  662. }
  663. }
  664. private void AutoUpdate_Click(object sender, RoutedEventArgs e)
  665. {
  666. var editable = App.AutoUpdateSettings.ToEditable();
  667. var buttons = new DynamicEditorButtons()
  668. {
  669. new DynamicEditorButton(
  670. "",
  671. PRSDesktop.Resources.help.AsBitmapImage(),
  672. null,
  673. (o, e) => ShowHelp("Automatic_Updates"))
  674. };
  675. var propertyEditor = new DynamicEditorForm(typeof(EditableAutoUpdateSettings), null, buttons);
  676. propertyEditor.OnDefineLookups += editor =>
  677. {
  678. var values = (editor.EditorDefinition as ILookupEditor).Values(typeof(EditableAutoUpdateSettings), editor.ColumnName);
  679. editor.LoadLookups(values);
  680. };
  681. propertyEditor.OnDefineEditor += PropertyEditor_OnDefineEditor;
  682. propertyEditor.Items = new BaseObject[] { editable };
  683. if (propertyEditor.ShowDialog() == true)
  684. {
  685. App.AutoUpdateSettings.FromEditable(editable);
  686. new LocalConfiguration<AutoUpdateSettings>().Save(App.AutoUpdateSettings);
  687. }
  688. }
  689. private BaseEditor? PropertyEditor_OnDefineEditor(object item, DynamicGridColumn column)
  690. {
  691. if (!string.Equals(column.ColumnName, "Elevated"))
  692. return new NullEditor();
  693. return null;
  694. }
  695. private void ShowHelp(string slug)
  696. {
  697. Process.Start(new ProcessStartInfo("https://prsdigital.com.au/wiki/index.php/" + slug) { UseShellExecute = true });
  698. }
  699. private void ShowHelp_OnClick(object sender, RoutedEventArgs e)
  700. {
  701. ShowHelp("Database_Configuration");
  702. }
  703. }
  704. }