2019-12-13 16:11:55 -05:00

97 lines
3.1 KiB
C#

using System;
using System.IO;
using System.Windows.Forms;
using Dinah.Core;
using FileManager;
namespace LibationWinForms.Dialogs
{
public partial class SettingsDialog : Form
{
Configuration config { get; } = Configuration.Instance;
Func<string, string> desc { get; } = Configuration.GetDescription;
public SettingsDialog()
{
InitializeComponent();
}
private void SettingsDialog_Load(object sender, EventArgs e)
{
this.decryptKeyTb.Text = config.DecryptKey;
this.decryptKeyDescLbl.Text = desc(nameof(config.DecryptKey));
this.booksLocationTb.Text
= !string.IsNullOrWhiteSpace(config.Books)
? config.Books
: Path.GetDirectoryName(Exe.FileLocationOnDisk);
this.booksLocationDescLbl.Text = desc(nameof(config.Books));
this.audibleLocaleCb.Text
= !string.IsNullOrWhiteSpace(config.LocaleCountryCode)
? config.LocaleCountryCode
: "us";
this.downloadsInProgressDescLbl.Text = desc(nameof(config.DownloadsInProgressEnum));
var winTempDownloadsInProgress = Path.Combine(Configuration.WinTemp, "DownloadsInProgress");
this.downloadsInProgressWinTempRb.Text = "In your Windows temporary folder\r\n" + winTempDownloadsInProgress;
switch (config.DownloadsInProgressEnum)
{
case "LibationFiles":
downloadsInProgressLibationFilesRb.Checked = true;
break;
case "WinTemp":
default:
downloadsInProgressWinTempRb.Checked = true;
break;
}
this.decryptInProgressDescLbl.Text = desc(nameof(config.DecryptInProgressEnum));
var winTempDecryptInProgress = Path.Combine(Configuration.WinTemp, "DecryptInProgress");
this.decryptInProgressWinTempRb.Text = "In your Windows temporary folder\r\n" + winTempDecryptInProgress;
switch (config.DecryptInProgressEnum)
{
case "LibationFiles":
decryptInProgressLibationFilesRb.Checked = true;
break;
case "WinTemp":
default:
decryptInProgressWinTempRb.Checked = true;
break;
}
}
private void booksLocationSearchBtn_Click(object sender, EventArgs e) => selectFolder("Search for books location", this.booksLocationTb);
private static void selectFolder(string desc, TextBox textbox)
{
using var dialog = new FolderBrowserDialog { Description = desc, SelectedPath = "" };
dialog.ShowDialog();
if (!string.IsNullOrWhiteSpace(dialog.SelectedPath))
textbox.Text = dialog.SelectedPath;
}
private void saveBtn_Click(object sender, EventArgs e)
{
config.DecryptKey = this.decryptKeyTb.Text;
config.LocaleCountryCode = this.audibleLocaleCb.Text;
config.DownloadsInProgressEnum = downloadsInProgressLibationFilesRb.Checked ? "LibationFiles" : "WinTemp";
config.DecryptInProgressEnum = decryptInProgressLibationFilesRb.Checked ? "LibationFiles" : "WinTemp";
var newBooks = this.booksLocationTb.Text;
if (!Directory.Exists(newBooks))
{
MessageBox.Show($"Not saving change to Books location. This folder does not exist:\r\n{newBooks}");
return;
}
else
config.Books = newBooks;
this.DialogResult = DialogResult.OK;
this.Close();
}
private void cancelBtn_Click(object sender, EventArgs e) => this.Close();
}
}