Repository navigation
Expand file tree
/
Copy pathVolumeSelection.cs
More file actions
149 lines (130 loc) · 4.88 KB
/
Copy pathVolumeSelection.cs
File metadata and controls
149 lines (130 loc) · 4.88 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Management;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace PortableUwfManager
{
internal sealed class VolumeSelection
{
public bool IsAll;
public readonly List<string> Volumes = new List<string>();
public string DisplayText()
{
if (IsAll)
{
return "all";
}
return String.Join(", ", Volumes.ToArray());
}
}
internal static class VolumeSelectionParser
{
public static bool TryParse(string text, bool allowAll, out VolumeSelection selection, out string error)
{
selection = null;
error = String.Empty;
if (String.IsNullOrWhiteSpace(text))
{
error = UiText.T("C: 또는 C:,D: 같은 드라이브 문자를 입력하세요.", "Use drive letters such as C: or C:,D:.");
return false;
}
var tokens = text.Split(new char[] { ',', ';', ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
if (tokens.Length == 0)
{
error = UiText.T("C: 또는 C:,D: 같은 드라이브 문자를 입력하세요.", "Use drive letters such as C: or C:,D:.");
return false;
}
var parsed = new VolumeSelection();
for (int i = 0; i < tokens.Length; i++)
{
var token = tokens[i].Trim();
if (String.Equals(token, "all", StringComparison.OrdinalIgnoreCase))
{
if (!allowAll)
{
error = UiText.T("all은 볼륨 보호에만 사용할 수 있습니다. 보호 해제는 C:,D:처럼 볼륨을 직접 지정하세요.",
"all is supported only for protecting volumes. For unprotect, specify volumes such as C:,D:.");
return false;
}
if (tokens.Length > 1)
{
error = UiText.T("all은 다른 볼륨과 함께 입력할 수 없습니다.", "all cannot be combined with other volumes.");
return false;
}
parsed.IsAll = true;
selection = parsed;
return true;
}
var volume = NormalizeVolume(token);
if (volume == null)
{
error = UiText.T("볼륨은 C: 또는 D: 형식이어야 합니다. 여러 개는 C:,D:처럼 입력하세요.",
"Volumes must be in C: or D: form. Enter multiple volumes as C:,D:.");
return false;
}
AddUnique(parsed.Volumes, volume);
}
if (parsed.Volumes.Count == 0)
{
error = UiText.T("보호할 볼륨을 입력하세요.", "Enter at least one volume.");
return false;
}
selection = parsed;
return true;
}
private static string NormalizeVolume(string volume)
{
if (String.IsNullOrWhiteSpace(volume))
{
return null;
}
volume = volume.Trim();
if (volume.Length == 1 && Char.IsLetter(volume[0]))
{
return Char.ToUpperInvariant(volume[0]) + ":";
}
if (volume.Length == 2 && Char.IsLetter(volume[0]) && volume[1] == ':')
{
return Char.ToUpperInvariant(volume[0]) + ":";
}
return null;
}
private static void AddUnique(List<string> volumes, string volume)
{
for (int i = 0; i < volumes.Count; i++)
{
if (String.Equals(volumes[i], volume, StringComparison.OrdinalIgnoreCase))
{
return;
}
}
volumes.Add(volume);
}
}
internal static class TextBoxCompat
{
private const int EmSetCueBanner = 0x1501;
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, string lParam);
public static void PlaceholderTextSafe(this TextBox textBox, string text)
{
if (textBox.IsHandleCreated)
{
SendMessage(textBox.Handle, EmSetCueBanner, IntPtr.Zero, text);
return;
}
textBox.HandleCreated += delegate
{
SendMessage(textBox.Handle, EmSetCueBanner, IntPtr.Zero, text);
};
}
}
}