-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
141 lines (126 loc) · 4.05 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace ETitcombe.EpubCreator
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
this.Font = SystemFonts.IconTitleFont;
_outputFileNameTextBox.Text = "MyEbook.epub";
_sourceFolderTextBox.Text = GetAppSetting("defaultSourceFolder", "");
_creatorTextBox.Text = Environment.UserName;
_languageTextBox.Text = GetAppSetting("defaultLanguage", "en");
_rightsTextBox.Text = GetAppSetting("defaultRights", "Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)");
_sourceTextBox.Text = GetAppSetting("defaultSource", "");
_titleTextBox.Text = GetAppSetting("defaultTitle", "");
}
private void BrowseButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.ShowNewFolderButton = false;
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
_sourceFolderTextBox.Text = folderBrowserDialog.SelectedPath;
}
}
private void CreateButton_Click(object sender, EventArgs e)
{
if (!this.ValidateChildren())
{
return;
}
DisableControls();
try
{
ConfigurationSettings configurationSettings = new ConfigurationSettings();
configurationSettings.OutputFileName = _outputFileNameTextBox.Text.Trim();
configurationSettings.OutputFolder = GetAppSetting("outputPath", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
configurationSettings.SourceFolder = _sourceFolderTextBox.Text.Trim();
configurationSettings.TempFolder = Path.Combine(Path.GetTempPath(), "epubcreator");
configurationSettings.Creator = _creatorTextBox.Text.Trim();
configurationSettings.Language = _languageTextBox.Text.Trim();
configurationSettings.Rights = _rightsTextBox.Text.Trim();
configurationSettings.Title = _titleTextBox.Text.Trim();
configurationSettings.TitlePage = GetAppSetting("defaultTitlePage", String.Empty);
EpubCreator epubCreator = new EpubCreator(configurationSettings);
epubCreator.Create();
if (!GetAppSetting("preserveTempFiles", "false").Equals("true", StringComparison.OrdinalIgnoreCase))
{
Directory.Delete(configurationSettings.TempFolder);
}
}
finally
{
EnableControls();
}
}
private void MainForm_KeyPress(object sender, KeyPressEventArgs e)
{
// Escape closes the form.
if (e.KeyChar == (Char)Keys.Escape)
{
this.Close();
}
}
private void SourceFolderTextBox_Validating(object sender, CancelEventArgs e)
{
String errorMessage = String.Empty;
if (String.IsNullOrEmpty(_sourceFolderTextBox.Text.Trim()))
{
e.Cancel = true;
errorMessage = "A source folder must be specified.";
}
else
{
if (!Directory.Exists(_sourceFolderTextBox.Text.Trim()))
{
e.Cancel = true;
errorMessage = "The source folder must exist.";
}
}
_errorProvider.SetError(_sourceFolderTextBox, errorMessage);
}
private void DisableControls()
{
_outputFileNameTextBox.Enabled = false;
_sourceFolderTextBox.Enabled = false;
_browseButton.Enabled = false;
_creatorTextBox.Enabled = false;
_languageTextBox.Enabled = false;
_rightsTextBox.Enabled = false;
_sourceTextBox.Enabled = false;
_titleTextBox.Enabled = false;
_createButton.Enabled = false;
}
private void EnableControls()
{
_outputFileNameTextBox.Enabled = true;
_sourceFolderTextBox.Enabled = true;
_browseButton.Enabled = true;
_creatorTextBox.Enabled = true;
_languageTextBox.Enabled = true;
_rightsTextBox.Enabled = true;
_sourceTextBox.Enabled = true;
_titleTextBox.Enabled = true;
_createButton.Enabled = true;
}
private String GetAppSetting(String appSettingKey, String defaultValue)
{
String appSettingValue = ConfigurationManager.AppSettings[appSettingKey];
if (String.IsNullOrEmpty(appSettingValue))
{
return defaultValue;
}
else
{
return appSettingValue;
}
}
}
}