1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using System.Windows; |
---|
6 | using System.Windows.Controls; |
---|
7 | using System.Windows.Data; |
---|
8 | using System.Windows.Documents; |
---|
9 | using System.Windows.Input; |
---|
10 | using System.Windows.Media; |
---|
11 | using System.Windows.Media.Imaging; |
---|
12 | using System.Windows.Navigation; |
---|
13 | using System.Windows.Shapes; |
---|
14 | using System.Windows.Threading; |
---|
15 | //using System.Windows.Forms; |
---|
16 | |
---|
17 | |
---|
18 | namespace FileWatcher |
---|
19 | { |
---|
20 | /// <summary> |
---|
21 | /// Interaction logic for MainWindow.xaml |
---|
22 | /// </summary> |
---|
23 | public partial class MainWindow : Window |
---|
24 | { |
---|
25 | public const string DIRECTORY_DEFAULT_TEXT = @"C:\MyParentFolder\MyFolder"; |
---|
26 | public const string EXTENSIONS_DEFAULT_TEXT = ".mp3, .avi, .txt, .*"; |
---|
27 | |
---|
28 | public static ScrollViewer MessageWindow; |
---|
29 | public static ListBox ProgramWindow; |
---|
30 | public static TextBox Directory; |
---|
31 | public static TextBox Extensions; |
---|
32 | public static CheckBox CreationBox; |
---|
33 | public static CheckBox DeletionBox; |
---|
34 | public static CheckBox ChangeBox; |
---|
35 | public static CheckBox RenameBox; |
---|
36 | public static CheckBox SubfolderBox; |
---|
37 | public static Button StartButton; |
---|
38 | public static Button StopButton; |
---|
39 | public static Button ResetButton; |
---|
40 | public static Button LoadButton; |
---|
41 | public static Button DirectoryBrowseButton; |
---|
42 | public static Dispatcher UIDispatcher = Dispatcher.CurrentDispatcher; |
---|
43 | |
---|
44 | public MainWindow() |
---|
45 | { |
---|
46 | InitializeComponent(); |
---|
47 | MessageWindow = messageWindow; |
---|
48 | ProgramWindow = programList; |
---|
49 | CreationBox = creationBox; |
---|
50 | DeletionBox = deletionBox; |
---|
51 | ChangeBox = changeBox; |
---|
52 | RenameBox = renameBox; |
---|
53 | Directory = directory; |
---|
54 | Extensions = extensions; |
---|
55 | SubfolderBox = subdirectoryCheckbox; |
---|
56 | StartButton = startButton; |
---|
57 | StopButton = stopButton; |
---|
58 | ResetButton = clearButton; |
---|
59 | LoadButton = loadSettings; |
---|
60 | DirectoryBrowseButton = directorySelection; |
---|
61 | Program.Initialize(); |
---|
62 | } |
---|
63 | |
---|
64 | private void directorySelection_Click(object sender, RoutedEventArgs e) |
---|
65 | { |
---|
66 | var dialog = new System.Windows.Forms.FolderBrowserDialog(); |
---|
67 | System.Windows.Forms.DialogResult result = dialog.ShowDialog(); |
---|
68 | Program.SelectedFolder = dialog.SelectedPath; |
---|
69 | directory.Text = dialog.SelectedPath; |
---|
70 | directory.Foreground = Brushes.Black; |
---|
71 | } |
---|
72 | |
---|
73 | private void directory_TextChanged(object sender, TextChangedEventArgs e) |
---|
74 | { |
---|
75 | if (directory.Text != DIRECTORY_DEFAULT_TEXT) |
---|
76 | { |
---|
77 | Program.SelectedFolder = directory.Text; |
---|
78 | } |
---|
79 | if (directory.Text == String.Empty) Program.SelectedFolder = null; |
---|
80 | } |
---|
81 | |
---|
82 | /// <summary> |
---|
83 | /// Hakemistotekstiboksia klikataan |
---|
84 | /// </summary> |
---|
85 | /// <param name="sender"></param> |
---|
86 | /// <param name="e"></param> |
---|
87 | private void directory_GotFocus(object sender, RoutedEventArgs e) |
---|
88 | { |
---|
89 | if (directory.Text == DIRECTORY_DEFAULT_TEXT) |
---|
90 | { |
---|
91 | directory.Text = ""; |
---|
92 | directory.Foreground = Brushes.Black; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | private void directory_LostFocus(object sender, RoutedEventArgs e) |
---|
97 | { |
---|
98 | if (directory.Text == "") |
---|
99 | { |
---|
100 | directory.Text = DIRECTORY_DEFAULT_TEXT; |
---|
101 | directory.Foreground = Brushes.Silver; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | private void directory_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) |
---|
106 | { |
---|
107 | if (e.Key == Key.Enter) |
---|
108 | { |
---|
109 | Program.SelectedFolder = directory.Text; |
---|
110 | directory.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down)); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | private void extensions_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) |
---|
115 | { |
---|
116 | if (e.Key == Key.Enter) |
---|
117 | { |
---|
118 | Program.SelectedFileExtensions = ErotteleTiedostotyypit(extensions.Text); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | /// <summary> |
---|
123 | /// Erotellaan merkkijonosta tiedostopäätteet erilleen. |
---|
124 | /// </summary> |
---|
125 | /// <param name="tyypit">Tiedostotyypit eroteltuna pilkulla.</param> |
---|
126 | private String[] ErotteleTiedostotyypit(String tyypit) |
---|
127 | { |
---|
128 | String[] päätteet; |
---|
129 | StringBuilder sb = new StringBuilder(); |
---|
130 | |
---|
131 | // poistetaan välilyönnit |
---|
132 | for (int i = 0; i < tyypit.Length; i++) |
---|
133 | { |
---|
134 | if (tyypit[i] != ' ') sb.Append(tyypit[i]); |
---|
135 | } |
---|
136 | |
---|
137 | päätteet = sb.ToString().Split(','); |
---|
138 | |
---|
139 | for (int i = 0; i < päätteet.Length; i++) |
---|
140 | { |
---|
141 | päätteet[i] = päätteet[i].Insert(0, "*"); |
---|
142 | } |
---|
143 | |
---|
144 | return päätteet; |
---|
145 | } |
---|
146 | |
---|
147 | private void extensions_LostFocus(object sender, RoutedEventArgs e) |
---|
148 | { |
---|
149 | if (extensions.Text == "") |
---|
150 | { |
---|
151 | extensions.Text = EXTENSIONS_DEFAULT_TEXT; |
---|
152 | extensions.Foreground = Brushes.Silver; |
---|
153 | } |
---|
154 | else Program.SelectedFileExtensions = ErotteleTiedostotyypit(extensions.Text); |
---|
155 | } |
---|
156 | |
---|
157 | private void extensions_GotFocus(object sender, RoutedEventArgs e) |
---|
158 | { |
---|
159 | if (extensions.Text == EXTENSIONS_DEFAULT_TEXT) |
---|
160 | { |
---|
161 | extensions.Text = ""; |
---|
162 | extensions.Foreground = Brushes.Black; |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | private void startButton_Click(object sender, RoutedEventArgs e) |
---|
167 | { |
---|
168 | bool onnistuiko = Program.Start(); |
---|
169 | if (onnistuiko) |
---|
170 | { |
---|
171 | extensions.IsEnabled = false; |
---|
172 | directory.IsEnabled = false; |
---|
173 | startButton.IsEnabled = false; |
---|
174 | stopButton.IsEnabled = true; |
---|
175 | clearButton.IsEnabled = false; |
---|
176 | |
---|
177 | subdirectoryCheckbox.IsEnabled = false; |
---|
178 | CreationBox.IsEnabled = false; |
---|
179 | DeletionBox.IsEnabled = false; |
---|
180 | ChangeBox.IsEnabled = false; |
---|
181 | RenameBox.IsEnabled = false; |
---|
182 | LoadButton.IsEnabled = false; |
---|
183 | DirectoryBrowseButton.IsEnabled = false; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | private void executeAdd_Click(object sender, RoutedEventArgs e) |
---|
188 | { |
---|
189 | ProgramSelection selectionWindow; |
---|
190 | /*if (MainWindow.ProgramWindow.SelectedIndex != -1) |
---|
191 | { |
---|
192 | selectionWindow = new ProgramSelection(MainWindow.ProgramWindow.SelectedIndex, true); |
---|
193 | } |
---|
194 | else */selectionWindow = new ProgramSelection(); |
---|
195 | selectionWindow.Show(); |
---|
196 | } |
---|
197 | |
---|
198 | private void executeRemove_Click(object sender, RoutedEventArgs e) |
---|
199 | { |
---|
200 | int i = programList.SelectedIndex; |
---|
201 | if (i == -1) return; |
---|
202 | |
---|
203 | Program.ExecutablePrograms.RemoveAt(i); |
---|
204 | Program.UpdateExecutableList(); |
---|
205 | } |
---|
206 | |
---|
207 | private void executeEdit_Click(object sender, RoutedEventArgs e) |
---|
208 | { |
---|
209 | if (programList.SelectedIndex == -1) return; |
---|
210 | ProgramSelection selectionWindow = new ProgramSelection(Program.ExecutablePrograms[programList.SelectedIndex].Path, Program.ExecutablePrograms[programList.SelectedIndex].Arguments, Program.ExecutablePrograms[programList.SelectedIndex].HasFileAsFirstParameter, programList.SelectedIndex); |
---|
211 | selectionWindow.Show(); |
---|
212 | } |
---|
213 | |
---|
214 | private void stopButton_Click(object sender, RoutedEventArgs e) |
---|
215 | { |
---|
216 | Program.StopWatching(); |
---|
217 | stopButton.IsEnabled = false; |
---|
218 | startButton.IsEnabled = true; |
---|
219 | extensions.IsEnabled = true; |
---|
220 | directory.IsEnabled = true; |
---|
221 | clearButton.IsEnabled = true; |
---|
222 | |
---|
223 | subdirectoryCheckbox.IsEnabled = true; |
---|
224 | CreationBox.IsEnabled = true; |
---|
225 | DeletionBox.IsEnabled = true; |
---|
226 | ChangeBox.IsEnabled = true; |
---|
227 | RenameBox.IsEnabled = true; |
---|
228 | DirectoryBrowseButton.IsEnabled = true; |
---|
229 | LoadButton.IsEnabled = true; |
---|
230 | } |
---|
231 | |
---|
232 | private void optionsButton_Click(object sender, RoutedEventArgs e) |
---|
233 | { |
---|
234 | Options options = new Options(); |
---|
235 | options.Show(); |
---|
236 | } |
---|
237 | |
---|
238 | public static void UpdateVisuals() |
---|
239 | { |
---|
240 | if (Program.SelectedFolder != "" && Program.SelectedFolder != null) |
---|
241 | { |
---|
242 | Directory.Text = Program.SelectedFolder; |
---|
243 | Directory.Foreground = Brushes.Black; |
---|
244 | } |
---|
245 | |
---|
246 | if (Program.SelectedFileExtensions != null) |
---|
247 | { |
---|
248 | string result = ""; |
---|
249 | foreach (String extension in Program.SelectedFileExtensions) |
---|
250 | { |
---|
251 | result += extension; |
---|
252 | } |
---|
253 | |
---|
254 | if (result != "") |
---|
255 | { |
---|
256 | Extensions.Text = result; |
---|
257 | Extensions.Foreground = Brushes.Black; |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | if (Program.ExecutablePrograms != null) |
---|
262 | Program.UpdateExecutableList(); |
---|
263 | |
---|
264 | CreationBox.IsChecked = Program.WatchCreation; |
---|
265 | DeletionBox.IsChecked = Program.WatchDeletion; |
---|
266 | ChangeBox.IsChecked = Program.WatchChanges; |
---|
267 | RenameBox.IsChecked = Program.WatchRenaming; |
---|
268 | SubfolderBox.IsChecked = Program.WatchSubdirectories; |
---|
269 | } |
---|
270 | |
---|
271 | private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) |
---|
272 | { |
---|
273 | if (Program.SaveSettingsAutomatically) |
---|
274 | { |
---|
275 | Program.SaveSettings(); |
---|
276 | Program.LogWriter.WriteLine(); |
---|
277 | Program.LogWriter.WriteLine(); |
---|
278 | Program.LogWriter.Flush(); |
---|
279 | Program.LogWriter.Close(); |
---|
280 | Program.LogWriter.Dispose(); |
---|
281 | } |
---|
282 | Program.StopWatching(); |
---|
283 | } |
---|
284 | |
---|
285 | private void saveSettings_Click(object sender, RoutedEventArgs e) |
---|
286 | { |
---|
287 | Program.SaveSettings(); |
---|
288 | Program.AddMessage("Saved current settings."); |
---|
289 | } |
---|
290 | |
---|
291 | private void loadSettings_Click(object sender, RoutedEventArgs e) |
---|
292 | { |
---|
293 | OptionsFile of = Program.LoadSettings(); |
---|
294 | Program.UseSettings(of); |
---|
295 | UpdateVisuals(); |
---|
296 | Program.AddMessage("Loaded previous settings."); |
---|
297 | } |
---|
298 | |
---|
299 | private void clearButton_Click(object sender, RoutedEventArgs e) |
---|
300 | { |
---|
301 | directory.Text = DIRECTORY_DEFAULT_TEXT; |
---|
302 | directory.Foreground = Brushes.Silver; |
---|
303 | Program.SelectedFolder = ""; |
---|
304 | extensions.Text = EXTENSIONS_DEFAULT_TEXT; |
---|
305 | extensions.Foreground = Brushes.Silver; |
---|
306 | Program.SelectedFileExtensions = null; |
---|
307 | creationBox.IsChecked = true; |
---|
308 | deletionBox.IsChecked = false; |
---|
309 | changeBox.IsChecked = false; |
---|
310 | renameBox.IsChecked = false; |
---|
311 | if (Program.ExecutablePrograms != null) |
---|
312 | { |
---|
313 | Program.ExecutablePrograms.Clear(); |
---|
314 | Program.UpdateExecutableList(); |
---|
315 | } |
---|
316 | Program.ClearMessages(); |
---|
317 | } |
---|
318 | |
---|
319 | private void subdirectoryCheckbox_Checked(object sender, RoutedEventArgs e) |
---|
320 | { |
---|
321 | Program.WatchSubdirectories = true; |
---|
322 | } |
---|
323 | |
---|
324 | private void subdirectoryCheckbox_Unchecked(object sender, RoutedEventArgs e) |
---|
325 | { |
---|
326 | Program.WatchSubdirectories = false; |
---|
327 | } |
---|
328 | } |
---|
329 | } |
---|