This repository was archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOutput.cs
More file actions
133 lines (114 loc) · 4.31 KB
/
FileOutput.cs
File metadata and controls
133 lines (114 loc) · 4.31 KB
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
using PasteToFile.DataTypes;
using PasteToFile.Helpers;
using System;
using System.IO;
using System.Windows.Forms;
namespace PasteToFile
{
public partial class FileOutput : Form
{
private readonly FileType _fileTypeFocused;
private readonly FilenameMask _mask = new FilenameMask();
private readonly Settings _settings;
private readonly string _outputDirectory;
private readonly Text _text = new Text();
private readonly Image _image = new Image();
public FileOutput(FileType fileType, string outputDirectory)
{
InitializeComponent();
_fileTypeFocused = fileType;
_outputDirectory = outputDirectory;
_settings = new Settings(_fileTypeFocused);
// Configure the UI
LoadSettings();
}
/// <summary>
/// Loads the settings and configures the UI.
/// </summary>
private void LoadSettings()
{
// Load UI data
Text = $"Paste to File [{_fileTypeFocused}]";
lblTitle.Text = _fileTypeFocused.ToString();
lblInformation.Text = $"One time output settings for detected file type of {_fileTypeFocused}.";
LoadExtensions();
// Load config
tboxFilename.Text = _mask.Generate(_mask.Generate(_settings.FilenameMask));
cboxExtension.Text = _settings.FilenameExtension;
}
/// <summary>
/// Loads the supported extensions for the selected file type.
/// </summary>
private void LoadExtensions()
{
var bindingSource = new BindingSource();
switch (_fileTypeFocused)
{
case FileType.Text:
bindingSource.DataSource = Output.TextExtensions();
break;
case FileType.Image:
bindingSource.DataSource = Output.ImageExtensions();
break;
default:
throw new Exception("Invalid File Type passed when fetching extensions.");
}
cboxExtension.DataSource = bindingSource;
}
/// <summary>
/// Writes out the file using the given configuration and closes the window.
/// </summary>
private void btnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(tboxFilename.Text))
{
MessageBox.Show("A filename must be provided to create the file.");
return;
}
SetAsDefault();
CreateFile();
Close();
}
private void CreateFile()
{
var fullFilePath = Path.Combine(_outputDirectory, $"{tboxFilename.Text}.{cboxExtension.Text}");
switch (_fileTypeFocused)
{
case FileType.Text:
_text.WriteOut(fullFilePath);
break;
case FileType.Image:
_image.WriteOut(fullFilePath);
break;
}
}
/// <summary>
/// Check if we should use these settings next time without confirming.
/// </summary>
private void SetAsDefault()
{
if (!chkSetAsDefault.Checked) return;
switch (_fileTypeFocused)
{
case FileType.Text:
Properties.Settings.Default.TextFilenameExtension = cboxExtension.Text;
Properties.Settings.Default.TextShowConfig = "false";
break;
case FileType.Image:
Properties.Settings.Default.ImageFilenameExtension = cboxExtension.Text;
Properties.Settings.Default.ImageShowConfig = "false";
break;
default:
throw new Exception("Invalid File Type passed when setting defaults.");
}
Properties.Settings.Default.Save();
}
/// <summary>
/// Close this window, and don't create file.
/// </summary>
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
}
}