public static SubsonicSettings ReadSettingsFromFile(string settingsFilename) { var settings = new SubsonicSettings(); try { if (!File.Exists(settingsFilename)) { return(Subsonic.GetCurrentSettings()); } using (var reader = new StreamReader(settingsFilename)) { var protocolText = AesEncryption.Decrypt(reader.ReadLine(), Passphrase); settings.Protocol = protocolText.Equals("HTTP") ? SubsonicSettings.ConnectionProtocol.Http : SubsonicSettings.ConnectionProtocol.Https; settings.Host = AesEncryption.Decrypt(reader.ReadLine(), Passphrase); settings.Port = AesEncryption.Decrypt(reader.ReadLine(), Passphrase); settings.BasePath = AesEncryption.Decrypt(reader.ReadLine(), Passphrase); settings.Username = AesEncryption.Decrypt(reader.ReadLine(), Passphrase); settings.Password = AesEncryption.Decrypt(reader.ReadLine(), Passphrase); settings.Transcode = AesEncryption.Decrypt(reader.ReadLine(), Passphrase) == "Y"; settings.Auth = AesEncryption.Decrypt(reader.ReadLine(), Passphrase) == "HexPass" ? SubsonicSettings.AuthMethod.HexPass : SubsonicSettings.AuthMethod.Token; settings.BitRate = AesEncryption.Decrypt(reader.ReadLine(), Passphrase); if (string.IsNullOrEmpty(settings.BitRate)) { settings.BitRate = "Unlimited"; } settings.UseIndexCache = AesEncryption.Decrypt(reader.ReadLine(), Passphrase) == "Y"; return(settings); } } catch (Exception ex) { const string caption = "Error while trying to load settings"; MessageBox.Show($@"An error occurred while trying to load the settings file! Reverting to defaults... Exception: {ex}", caption, MessageBoxButtons.OK, MessageBoxIcon.Error); return(Subsonic.GetCurrentSettings()); } }
public static bool IsSettingChanged(SubsonicSettings newSettings, SubsonicSettings oldSettings) { newSettings = SanitizeSettings(newSettings); var result = !newSettings.Host.Equals(oldSettings.Host) || !newSettings.Port.Equals(oldSettings.Port) || !newSettings.BasePath.Equals(oldSettings.BasePath) || !newSettings.Username.Equals(oldSettings.Username) || !newSettings.Password.Equals(oldSettings.Password) || !newSettings.Protocol.Equals(oldSettings.Protocol) || !newSettings.Auth.Equals(oldSettings.Auth) || !newSettings.Transcode.Equals(oldSettings.Transcode) || !newSettings.BitRate.Equals(oldSettings.BitRate); return(result); }
public static SubsonicSettings SanitizeSettings(SubsonicSettings settings) { settings.Host = settings.Host.Trim(); if (settings.Host.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) { settings.Host = settings.Host.Substring(7); } else if (settings.Host.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) { settings.Host = settings.Host.Substring(8); } settings.Port = settings.Port.Trim(); settings.BasePath = settings.BasePath.Trim(); if (!settings.BasePath.EndsWith("/")) { settings.BasePath += "/"; } return(settings); }
// called by MusicBee when the user clicks Apply or Save in the MusicBee Preferences screen. // its up to you to figure out whether anything has changed and needs updating public void SaveSettings() { var settings = new SubsonicSettings { Host = _host.Text, Port = _port.Text, BasePath = _basePath.Text, Username = _username.Text, Password = _password.Text, Transcode = _transcode.Checked, Protocol = _protocol.SelectedItem.ToString().Equals("HTTP") ? SubsonicSettings.ConnectionProtocol.Http : SubsonicSettings.ConnectionProtocol.Https, Auth = _authMethodBox.SelectedIndex.Equals(0) ? SubsonicSettings.AuthMethod.Token : SubsonicSettings.AuthMethod.HexPass, //Null reference. Issue #36 - https://github.com/midwan/MB_SubSonic/issues/36. If there is no bitrate, set it to a default. BitRate = String.IsNullOrEmpty((String)_bitRate.SelectedItem) ? "128K" : _bitRate.SelectedItem.ToString() }; var setHostSuccess = Subsonic.SetHost(settings); if (setHostSuccess) { DeleteCacheFile(); Refresh(); return; } var error = Subsonic.GetError(); var message = error?.Message; if (!string.IsNullOrEmpty(message)) { MessageBox.Show(_host, $@"Error: {message}", @"Subsonic Plugin", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
public static bool SaveSettingsToFile(SubsonicSettings settings, string filename) { try { using (var writer = new StreamWriter(filename)) { writer.WriteLine( AesEncryption.Encrypt( settings.Protocol == SubsonicSettings.ConnectionProtocol.Http ? "HTTP" : "HTTPS", Passphrase)); writer.WriteLine(AesEncryption.Encrypt(settings.Host, Passphrase)); writer.WriteLine(AesEncryption.Encrypt(settings.Port, Passphrase)); writer.WriteLine(AesEncryption.Encrypt(settings.BasePath, Passphrase)); writer.WriteLine(AesEncryption.Encrypt(settings.Username, Passphrase)); writer.WriteLine(AesEncryption.Encrypt(settings.Password, Passphrase)); writer.WriteLine(settings.Transcode ? AesEncryption.Encrypt("Y", Passphrase) : AesEncryption.Encrypt("N", Passphrase)); writer.WriteLine( AesEncryption.Encrypt( settings.Auth == SubsonicSettings.AuthMethod.HexPass ? "HexPass" : "Token", Passphrase)); writer.WriteLine(AesEncryption.Encrypt(settings.BitRate, Passphrase)); writer.WriteLine(settings.UseIndexCache ? AesEncryption.Encrypt("Y", Passphrase) : AesEncryption.Encrypt("N", Passphrase)); return(true); } } catch (Exception ex) { const string caption = "Error while trying to save settings"; MessageBox.Show($@"An error occurred while trying to save the settings file! Exception: {ex}", caption, MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } }