/HTCHome2.1/HTC Home/Options.xaml.cs

# · C# · 169 lines · 139 code · 27 blank · 3 comment · 4 complexity · 737e4690ee027fd47833a6c36a19d0a4 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. using System.Windows.Interop;
  14. using HTCHome.Core;
  15. using System.IO;
  16. using Microsoft.Win32;
  17. using System.Reflection;
  18. namespace HTCHome
  19. {
  20. /// <summary>
  21. /// Interaction logic for Options.xaml
  22. /// </summary>
  23. public partial class Options : Window
  24. {
  25. private IntPtr handle;
  26. private List<string> localeCodes = new List<string>();
  27. public Options()
  28. {
  29. InitializeComponent();
  30. }
  31. private void Window_SourceInitialized(object sender, EventArgs e)
  32. {
  33. handle = new WindowInteropHelper(this).Handle;
  34. WinAPI.MARGINS margins = new WinAPI.MARGINS();
  35. margins.cyTopHeight = 24;
  36. HwndSource.FromHwnd(handle).CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);
  37. WinAPI.ExtendGlassFrame(handle, ref margins);
  38. System.Reflection.Assembly _AsmObj = System.Reflection.Assembly.GetExecutingAssembly();
  39. System.Reflection.AssemblyName _CurrAsmName = _AsmObj.GetName();
  40. string _Major = _CurrAsmName.Version.Major.ToString();
  41. string _Minor = _CurrAsmName.Version.Minor.ToString();
  42. string _Build = _CurrAsmName.Version.Build.ToString();
  43. VersionString.Text = string.Format("Version {0}.{1} Build {2}", _Major, _Minor, _Build);
  44. TranslatedBy.Text = App.LocaleManager.GetString("LocaleAuthor");
  45. OkButton.Content = App.LocaleManager.GetString("OK");
  46. CancelButton.Content = App.LocaleManager.GetString("Cancel");
  47. ApplyButton.Content = App.LocaleManager.GetString("Apply");
  48. Title = App.LocaleManager.GetString("Options");
  49. AutostartCheckBox.Content = App.LocaleManager.GetString("Autostart");
  50. EnableGlassCheckBox.Content = App.LocaleManager.GetString("EnableGlass");
  51. CheckForUpdatesCheckBox.Content = App.LocaleManager.GetString("CheckForUpdates");
  52. LangTextBlock.Text = App.LocaleManager.GetString("Language");
  53. RestartText.Text = App.LocaleManager.GetString("RestartText");
  54. EnableGlassCheckBox.IsChecked = App.sett.EnableGlass;
  55. AutostartCheckBox.IsChecked = App.sett.Autostart;
  56. CheckForUpdatesCheckBox.IsChecked = App.sett.EnableUpdates;
  57. if (Directory.Exists(App.Path + "\\Localization"))
  58. {
  59. foreach (string f in Directory.GetFiles(App.Path + "\\Localization", "*.xaml"))
  60. {
  61. LangComboBox.Items.Add(LocaleManager.GetLocaleName(f));
  62. localeCodes.Add(LocaleManager.GetLocaleCode(f));
  63. }
  64. }
  65. LangComboBox.SelectedIndex = localeCodes.IndexOf(Core.Environment.Locale);
  66. GeneralTab.Header = App.LocaleManager.GetString("General");
  67. AboutTab.Header = App.LocaleManager.GetString("About");
  68. }
  69. private void CheckBox_Click(object sender, RoutedEventArgs e)
  70. {
  71. ApplyButton.IsEnabled = true;
  72. }
  73. private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
  74. {
  75. ApplyButton.IsEnabled = true;
  76. }
  77. private void OkButton_Click(object sender, RoutedEventArgs e)
  78. {
  79. ApplySettings();
  80. this.Close();
  81. }
  82. private void CancelButton_Click(object sender, RoutedEventArgs e)
  83. {
  84. this.Close();
  85. }
  86. private void ApplyButton_Click(object sender, RoutedEventArgs e)
  87. {
  88. ApplySettings();
  89. ApplyButton.IsEnabled = false;
  90. }
  91. private void ContactString_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  92. {
  93. WinAPI.ShellExecute(IntPtr.Zero, "open", ContactString.Text, string.Empty, string.Empty, 0);
  94. }
  95. private void ApplySettings()
  96. {
  97. App.sett.EnableGlass = (bool)EnableGlassCheckBox.IsChecked;
  98. App.sett.Autostart = (bool)AutostartCheckBox.IsChecked;
  99. App.sett.Locale = localeCodes[LangComboBox.SelectedIndex];
  100. App.sett.EnableUpdates = (bool)CheckForUpdatesCheckBox.IsChecked;
  101. HTCHome.Core.Environment.Locale = localeCodes[LangComboBox.SelectedIndex];
  102. App.LocaleManager.LoadLocale(localeCodes[LangComboBox.SelectedIndex]);
  103. if (AutostartCheckBox.IsChecked == true)
  104. {
  105. try
  106. {
  107. using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", RegistryKeyPermissionCheck.ReadWriteSubTree).OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Run", true))
  108. {
  109. key.SetValue("HTC Home", "\"" + Assembly.GetExecutingAssembly().Location + "\"", RegistryValueKind.String);
  110. key.Close();
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. Core.Logger.Log(ex.ToString());
  116. }
  117. }
  118. else
  119. {
  120. try
  121. {
  122. using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", RegistryKeyPermissionCheck.ReadWriteSubTree).OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Run", true))
  123. {
  124. key.DeleteValue("HTC Home", false);
  125. key.Close();
  126. }
  127. }
  128. catch (Exception ex)
  129. {
  130. Core.Logger.Log(ex.ToString());
  131. }
  132. }
  133. App.sett.LoadedWidgets = new List<string>();
  134. foreach (Widget w in App.widgets)
  135. {
  136. if (w.IsWidgetLoaded)
  137. App.sett.LoadedWidgets.Add(w.WidgetName);
  138. }
  139. App.sett.Write(App.Path + "\\Config\\Home.conf");
  140. }
  141. }
  142. }