-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
164 lines (139 loc) · 4.65 KB
/
MainWindow.xaml.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.IO;
using System.Windows;
using AutoRename.Services;
namespace AutoRename
{
public partial class MainWindow : Window
{
private readonly MainViewModel model;
private readonly FileNameProcessor fileNameProcessor;
private readonly Persistence persistence;
public MainWindow()
{
InitializeComponent();
fileNameProcessor = new FileNameProcessor();
persistence = new Persistence(Properties.Resources.ConfigFile);
DataContext = model = new MainViewModel(fileNameProcessor);
string appName = Utility.CurrentApplication.Name;
Version appVersion = Utility.CurrentApplication.Version;
Title = $"{appName} {appVersion.Major}{(appVersion.Minor > 0 ? "." + appVersion.Minor : "")}{(appVersion.Build > 0 ? "." + appVersion.Build : "")}";
LoadSettings();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
bool renameAutomatically = false;
ProcessArgv(ref renameAutomatically);
if (renameAutomatically)
{
model.RenameAll();
}
else
{
Updater updater = new Updater(Properties.Resources.UpdateFile)
{
UpdateAvailableAction = OnUpdateAvailable
};
updater.CheckForUpdateAvailableAsync();
}
}
private void ProcessArgv(ref bool renameAutomatically)
{
string[] argv = Environment.GetCommandLineArgs();
bool showHelpBox = false;
for (int i = 1; i < argv.Length; i++)
{
string arg = argv[i];
switch (arg.ToLower().Replace('/', '-'))
{
case "-b":
model.RemoveBrackets = true;
break;
case "-uc":
model.StartWithUpperCase = true;
break;
case "-sn":
model.RemoveStartingNumber = true;
break;
case "-f":
model.ForceOverwrite = true;
break;
case "-y":
renameAutomatically = true;
break;
case "-e":
model.ExitAfterRename = true;
break;
case "/?":
case "-help":
case "--help":
showHelpBox = true;
break;
default:
if (File.Exists(arg) || Directory.Exists(arg))
{
model.AddFile(arg);
}
else
{
showHelpBox = true;
}
break;
}
}
if (showHelpBox)
{
MessageBox.Show(Properties.Resources.CmdParametersHelp, "Command line parameters", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
private void LoadSettings()
{
if (!File.Exists(persistence.FilePath))
{
return;
}
try
{
persistence.LoadSettings(model, this);
}
catch
{
// ignored
}
}
private void SaveSettings()
{
try
{
persistence.SaveSettings(model, this);
}
catch
{
// ignored
}
}
private void OnUpdateAvailable()
{
model.WebsiteButton = Properties.Resources.WebsiteButtonUpdate;
}
private void Window_Closed(object sender, EventArgs e)
{
SaveSettings();
}
private void Window_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
{
e.Effects = DragDropEffects.All;
}
}
private void Window_Drop(object sender, DragEventArgs e)
{
string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
if (files != null)
{
model.AddFiles(files);
}
}
}
}