|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Xml; |
| 7 | +using System.Xml.Linq; |
| 8 | + |
| 9 | +namespace SyncPlayWPF.Common { |
| 10 | + public class Settings { |
| 11 | + |
| 12 | + |
| 13 | + private static XElement basics = new XElement("Basics", |
| 14 | + new XElement("Address", ""), |
| 15 | + new XElement("Username", ""), |
| 16 | + new XElement("Password", ""), |
| 17 | + new XElement("RoomName", ""), |
| 18 | + new XElement("PathToMediaPlayer", ""), |
| 19 | + new XElement("PathToVideo", ""), |
| 20 | + new XElement("AdditionalArguments", "") |
| 21 | + ); |
| 22 | + |
| 23 | + private static XElement behaviour = new XElement("Behaviour", |
| 24 | + new XElement("SetMeAsReadyToSwtichByDefault", "False"), |
| 25 | + new XElement("PauseWhenAUserLeavesOrGetDisconnected", "True"), |
| 26 | + new XElement("SyncMyReadyToWatchStatusWithPlayState", "True"), |
| 27 | + new XElement("NeverSlowDownOrRewindOther", "False"), |
| 28 | + new XElement("FastForwardIfLaggingBehind", "True") |
| 29 | + ); |
| 30 | + |
| 31 | + private static XElement chatbehaviour = new XElement("Chat", |
| 32 | + new XElement("ShowMessagesInPlayerWindow", "True"), |
| 33 | + new XElement("PlayNotificationSound", "False"), |
| 34 | + new XElement("DisableAnimations", "False"), |
| 35 | + new XElement("AutoplayGifs", "False"), |
| 36 | + new XElement("PreviewLinks", "False"), |
| 37 | + new XElement("EnableOSDMessages", ""), |
| 38 | + new XElement("ShowEventsInYourRoom", "True"), |
| 39 | + new XElement("ShowEventsFromNonOperatorsInManagedRooms", "False"), |
| 40 | + new XElement("ShowEventsInOtherRooms", "False"), |
| 41 | + new XElement("ShowSlowingDownRevertingNotifications", "True"), |
| 42 | + new XElement("ShowWarnings", "True") |
| 43 | + ); |
| 44 | + |
| 45 | + private static XElement securitySection = new XElement("Security", |
| 46 | + new XElement("EnableTLS", "True"), |
| 47 | + new XElement("HashFileNamesBeforeSending", "False"), |
| 48 | + new XElement("HashPasswords", "True") |
| 49 | + ); |
| 50 | + |
| 51 | + private static XDocument NewConfig = new XDocument( |
| 52 | + new XElement("Config", |
| 53 | + new XComment("Basics and Stuff"), |
| 54 | + basics, |
| 55 | + new XComment("Syncplay Behaviour Settings"), |
| 56 | + behaviour, |
| 57 | + new XComment("Chat Behaviour Settings"), |
| 58 | + chatbehaviour, |
| 59 | + new XComment("Security Settings. Sekuriti"), |
| 60 | + securitySection |
| 61 | + ) |
| 62 | + ); |
| 63 | + |
| 64 | + public static void RestoreDefaultConfiguration() { |
| 65 | + if (System.IO.File.Exists("SyncPlayConfig.xml")) { |
| 66 | + XDocument doc = XDocument.Parse(System.IO.File.ReadAllText("SyncPlayConfig.xml")); |
| 67 | + foreach (var sectiontag in NewConfig.Element("Config").Elements()) { |
| 68 | + foreach (var proptag in sectiontag.Elements()) { |
| 69 | + doc.Element("Config").Element(sectiontag.Name).Element(proptag.Name).Value = proptag.Value; |
| 70 | + } |
| 71 | + } |
| 72 | + System.IO.File.WriteAllText("SyncPlayConfig.xml", doc.ToString()); |
| 73 | + } else { |
| 74 | + var stringOut = NewConfig.ToString(); |
| 75 | + System.IO.File.WriteAllText("SyncPlayConfig.xml", stringOut); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public static void UpdateAttribute(XDocument doc, string section, string attribute, string value) { |
| 80 | + var sectionTag = doc.Element("Config").Element(section); |
| 81 | + if (sectionTag == null) { |
| 82 | + foreach (var sec in NewConfig.Element("Config").Elements()) { |
| 83 | + if (sec.Name == section) { |
| 84 | + doc.Element("Config").Add(sec); |
| 85 | + sectionTag = doc.Element("Config").Element(section); |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + if (value == null) value = ""; |
| 91 | + sectionTag.Element(attribute).Value = value; |
| 92 | + } |
| 93 | + |
| 94 | + private static string ConvertBoolean(bool? c) { |
| 95 | + return (bool)c ? "True" : "False"; |
| 96 | + } |
| 97 | + |
| 98 | + private static string GetStringValue(XDocument doc, string section, string attribute) { |
| 99 | + return doc.Element("Config").Element(section).Element(attribute).Value; |
| 100 | + } |
| 101 | + |
| 102 | + private static bool GetBooleanValue(XDocument doc, string section, string attribute) { |
| 103 | + return doc.Element("Config").Element(section).Element(attribute).Value == "True" ? true : false; |
| 104 | + } |
| 105 | + |
| 106 | + public static void WriteConfigurationToView(Pages.SettingsPage Page) { |
| 107 | + XDocument doc = XDocument.Parse(System.IO.File.ReadAllText("SyncPlayConfig.xml")); |
| 108 | + |
| 109 | + Page.ServerAddressField.Text = GetStringValue(doc, "Basics", "Address"); |
| 110 | + Page.UsernameField.Text = GetStringValue(doc, "Basics", "Username"); |
| 111 | + //Page.PasswordField.ActualPassword = "BLANKPASSWORDS"; |
| 112 | + Page.RoomNameField.Text = GetStringValue(doc, "Basics", "RoomName"); |
| 113 | + Page.PathToMediaPlayer.Text = GetStringValue(doc, "Basics", "PathToMediaPlayer"); |
| 114 | + Page.PathToVideo.Text = GetStringValue(doc, "Basics", "PathToVideo"); |
| 115 | + Page.AdditionalArguments.Text = GetStringValue(doc, "Basics", "AdditionalArguments"); |
| 116 | + |
| 117 | + Page.SetAsReadyToPlayByDefault.IsChecked = GetBooleanValue(doc, "Behaviour", "SetMeAsReadyToSwtichByDefault"); |
| 118 | + Page.PauseWhenUserLeaves.IsChecked = GetBooleanValue(doc, "Behaviour", "PauseWhenAUserLeavesOrGetDisconnected"); |
| 119 | + Page.SyncReadyToPlayWithPauseState.IsChecked = GetBooleanValue(doc, "Behaviour", "SyncMyReadyToWatchStatusWithPlayState"); |
| 120 | + Page.NeverSlowDownOrRewindOthers.IsChecked = GetBooleanValue(doc, "Behaviour", "NeverSlowDownOrRewindOther"); |
| 121 | + Page.FastForwardIfLagginingBehind.IsChecked = GetBooleanValue(doc, "Behaviour", "FastForwardIfLaggingBehind"); |
| 122 | + |
| 123 | + Page.ShowMessagesInPlayerWindow.IsChecked = GetBooleanValue(doc, "Chat", "ShowMessagesInPlayerWindow"); |
| 124 | + Page.PlayNotificationSound.IsChecked = GetBooleanValue(doc, "Chat", "PlayNotificationSound"); |
| 125 | + Page.DisableAnimations.IsChecked = GetBooleanValue(doc, "Chat", "DisableAnimations"); |
| 126 | + Page.AutoPlayGIFs.IsChecked = GetBooleanValue(doc, "Chat", "AutoplayGifs"); |
| 127 | + Page.PreviewLinks.IsChecked = GetBooleanValue(doc, "Chat", "PreviewLinks"); |
| 128 | + Page.EnableOSDMessages.IsChecked = GetBooleanValue(doc, "Chat", "EnableOSDMessages"); |
| 129 | + Page.OSD_ShowEventsInYourRooms.IsChecked = GetBooleanValue(doc, "Chat", "ShowEventsFromNonOperatorsInManagedRooms"); |
| 130 | + Page.OSD_ShowEventsInOtherRooms.IsChecked = GetBooleanValue(doc, "Chat", "ShowEventsInOtherRooms"); |
| 131 | + Page.OSD_ShowSlowingNotifications.IsChecked = GetBooleanValue(doc, "Chat", "ShowSlowingDownRevertingNotifications"); |
| 132 | + Page.OSD_ShowWarnings.IsChecked = GetBooleanValue(doc, "Chat", "ShowWarnings"); |
| 133 | + |
| 134 | + Page.EnableTLS.IsChecked = GetBooleanValue(doc, "Security", "EnableTLS"); |
| 135 | + Page.HashFileNamesBeforeSending.IsChecked = GetBooleanValue(doc, "Security", "HashFileNamesBeforeSending"); |
| 136 | + Page.HashPasswords.IsChecked = GetBooleanValue(doc, "Security", "HashPasswords"); |
| 137 | + |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + public static void ReadConfigurationFromView(Pages.SettingsPage Page) { |
| 142 | + XDocument doc = XDocument.Parse(System.IO.File.ReadAllText("SyncPlayConfig.xml")); |
| 143 | + |
| 144 | + UpdateAttribute(doc, "Basics", "Address", Page.ServerAddressField.Text); |
| 145 | + UpdateAttribute(doc, "Basics", "Username", Page.UsernameField.Text); |
| 146 | + UpdateAttribute(doc, "Basics", "Password", Page.PasswordField.ActualPassword); |
| 147 | + UpdateAttribute(doc, "Basics", "RoomName", Page.RoomNameField.Text); |
| 148 | + UpdateAttribute(doc, "Basics", "PathToMediaPlayer", Page.PathToMediaPlayer.Text); |
| 149 | + UpdateAttribute(doc, "Basics", "PathToVideo", Page.PathToVideo.Text); |
| 150 | + UpdateAttribute(doc, "Basics", "AdditionalArguments", Page.AdditionalArguments.Text); |
| 151 | + |
| 152 | + UpdateAttribute(doc, "Behaviour", "SetMeAsReadyToSwtichByDefault", ConvertBoolean(Page.SetAsReadyToPlayByDefault.IsChecked)); |
| 153 | + UpdateAttribute(doc, "Behaviour", "PauseWhenAUserLeavesOrGetDisconnected", ConvertBoolean(Page.PauseWhenUserLeaves.IsChecked)); |
| 154 | + UpdateAttribute(doc, "Behaviour", "SyncMyReadyToWatchStatusWithPlayState", ConvertBoolean(Page.SyncReadyToPlayWithPauseState.IsChecked)); |
| 155 | + UpdateAttribute(doc, "Behaviour", "NeverSlowDownOrRewindOther", ConvertBoolean(Page.NeverSlowDownOrRewindOthers.IsChecked)); |
| 156 | + UpdateAttribute(doc, "Behaviour", "FastForwardIfLaggingBehind", ConvertBoolean(Page.FastForwardIfLagginingBehind.IsChecked)); |
| 157 | + |
| 158 | + UpdateAttribute(doc, "Chat", "ShowMessagesInPlayerWindow", ConvertBoolean(Page.ShowMessagesInPlayerWindow.IsChecked)); |
| 159 | + UpdateAttribute(doc, "Chat", "PlayNotificationSound", ConvertBoolean(Page.PlayNotificationSound.IsChecked)); |
| 160 | + UpdateAttribute(doc, "Chat", "DisableAnimations", ConvertBoolean(Page.DisableAnimations.IsChecked)); |
| 161 | + UpdateAttribute(doc, "Chat", "AutoplayGifs", ConvertBoolean(Page.AutoPlayGIFs.IsChecked)); |
| 162 | + UpdateAttribute(doc, "Chat", "PreviewLinks", ConvertBoolean(Page.PreviewLinks.IsChecked)); |
| 163 | + UpdateAttribute(doc, "Chat", "EnableOSDMessages", ConvertBoolean(Page.EnableOSDMessages.IsChecked)); |
| 164 | + UpdateAttribute(doc, "Chat", "ShowEventsInYourRoom", ConvertBoolean(Page.OSD_ShowEventsInYourRooms.IsChecked)); |
| 165 | + UpdateAttribute(doc, "Chat", "ShowEventsFromNonOperatorsInManagedRooms", ConvertBoolean(Page.OSD_ShowEventsFromManagedRooms.IsChecked)); |
| 166 | + UpdateAttribute(doc, "Chat", "ShowEventsInOtherRooms", ConvertBoolean(Page.OSD_ShowEventsInOtherRooms.IsChecked)); |
| 167 | + UpdateAttribute(doc, "Chat", "ShowSlowingDownRevertingNotifications", ConvertBoolean(Page.OSD_ShowSlowingNotifications.IsChecked)); |
| 168 | + UpdateAttribute(doc, "Chat", "ShowWarnings", ConvertBoolean(Page.OSD_ShowWarnings.IsChecked)); |
| 169 | + |
| 170 | + UpdateAttribute(doc, "Security", "EnableTLS", ConvertBoolean(Page.EnableTLS.IsChecked)); |
| 171 | + UpdateAttribute(doc, "Security", "HashFileNamesBeforeSending", ConvertBoolean(Page.HashFileNamesBeforeSending.IsChecked)); |
| 172 | + UpdateAttribute(doc, "Security", "HashPasswords", ConvertBoolean(Page.HashPasswords.IsChecked)); |
| 173 | + |
| 174 | + System.IO.File.WriteAllText("SyncPlayConfig.xml", doc.ToString()); |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + } |
| 179 | +} |
0 commit comments