-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
282 lines (246 loc) · 10.6 KB
/
MainForm.cs
File metadata and controls
282 lines (246 loc) · 10.6 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* Evrika: Main form
* (c) 2019, Petros Kyladitis <http://www.multipetros.gr>
*
* This is free software distributed under the GNU GPL 3, for license details see at license.txt
* file, distributed with this program source, or see at <http://www.gnu.org/licenses/>
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Resources;
using System.Globalization;
using System.ComponentModel;
using UsnJournal;
using Multipetros.Config;
namespace Evrika{
public partial class MainForm : Form{
//constant strings representing resources keys
private const string RES_FRM_TITLE = "FRM_TITLE" ;
private const string RES_BUTTON_SEARCH = "BUTTON_SEARCH" ;
private const string RES_OPT_FILES_FOLDERS = "OPT_FILES_FOLDERS" ;
private const string RES_OPT_FILES_ONLY = "OPT_FILES_ONLY" ;
private const string RES_OPT_FOLDERS_ONLY = "OPT_FOLDERS_ONLY" ;
private const string RES_MATCH_EXACT = "MATCH_EXACT" ;
private const string RES_MATCH_CONTAINS = "MATCH_CONTAINS" ;
private const string RES_MATCH_STARTS = "MATCH_STARTS" ;
private const string RES_MATCH_ENDS = "MATCH_ENDS" ;
private const string RES_MENU_ABOUT = "MENU_ABOUT" ;
private const string RES_MENU_FILE = "MENU_FILE" ;
private const string RES_MENU_EXIT = "MENU_EXIT" ;
private const string RES_MENU_LANGUANGE = "MENU_LANGUANGE" ;
private const string RES_MENU_REFRESH = "MENU_REFRESH" ;
private const string RES_MENU_EXIT_TIP = "MENU_EXIT_TIP" ;
private const string RES_MENU_REFRESH_TIP = "MENU_REFRESH_TIP" ;
private const string RES_MENU_LANGUAGE_TIP = "MENU_LANGUAGE_TIP" ;
private const string RES_MENU_ABOUT_TIP = "MENU_ABOUT_TIP" ;
private const string RES_COL_ELEMENT = "COL_ELEMENT" ;
private const string RES_COL_PATH = "COL_PATH" ;
private const string RES_STATUS_READY = "STATUS_READY" ;
private const string RES_STATUS_SEARCHING = "STATUS_SEARCHING" ;
private const string RES_STATUS_TIME = "STATUS_TIME" ;
private const string RES_STATUS_FOUND = "STATUS_FOUND" ;
private const string RES_MSG_TITLE_ERROR = "MSG_TITLE_ERROR" ;
private const string RES_MSG_NOFILTER = "MSG_NOFILTER" ;
private const string RES_MSG_FILENOTFOUND = "MSG_FILENOTFOUND" ;
private const string RES_MSG_NODRIVE = "MSG_NODRIVE" ;
//available language resources
private readonly string[] AVAILABLE_LANGS = new string[]{"en","el"} ;
private const int LANG_EN = 0 ;
private const int LANG_EL = 1 ;
//constant strings representing registry setting key names
private const string INI_WND_TOP = "wndtop" ;
private const string INI_WND_LEFT = "wndleft" ;
private const string INI_LANG = "lang" ;
private const string INI_TYPE = "type" ;
private const string INI_MATCH = "match" ;
//init vars
private ResourceManager resmgr ;
private CultureInfo ci ;
private RegistryIni ini ;
public MainForm(){
// The InitializeComponent() call is required for Windows Forms designer support.
InitializeComponent();
resmgr = new ResourceManager(typeof(MainForm)) ;
ini = new RegistryIni(Application.CompanyName, Application.ProductName) ;
}
private void LoadNtfsDrives(){
comboBoxDrive.Items.Clear() ;
foreach(DriveInfo dInf in DriveInfo.GetDrives()){
if(dInf.IsReady && dInf.DriveFormat == "NTFS")
comboBoxDrive.Items.Add(dInf.Name) ;
}
if(comboBoxDrive.Items.Count > 0)
comboBoxDrive.SelectedIndex = 0 ;
}
private string GetRes(string key){
return resmgr.GetString(key, ci) ;
}
private void LoadRes(){
this.Text = GetRes(RES_FRM_TITLE) ;
buttonSearch.Text = GetRes(RES_BUTTON_SEARCH) ;
columnHeaderName.Text = GetRes(RES_COL_ELEMENT) ;
columnHeaderPath.Text = GetRes(RES_COL_PATH) ;
fileToolStripMenuItem.Text = GetRes(RES_MENU_FILE) ;
exitToolStripMenuItem.Text = GetRes(RES_MENU_EXIT) ;
langToolStripMenuItem.Text = GetRes(RES_MENU_LANGUANGE) ;
aboutToolStripMenuItem.Text = GetRes(RES_MENU_ABOUT) ;
refreshToolStripMenuItem.Text = GetRes(RES_MENU_REFRESH) ;
int curId = comboBoxType.SelectedIndex ;
comboBoxType.Items.Clear() ;
comboBoxType.Items.Add(GetRes(RES_OPT_FILES_FOLDERS)) ;
comboBoxType.Items.Add(GetRes(RES_OPT_FILES_ONLY)) ;
comboBoxType.Items.Add(GetRes(RES_OPT_FOLDERS_ONLY)) ;
comboBoxType.SelectedIndex = curId < 0 ? 0 : curId ;
curId = comboBoxMatch.SelectedIndex ;
comboBoxMatch.Items.Clear() ;
comboBoxMatch.Items.Add(GetRes(RES_MATCH_EXACT)) ;
comboBoxMatch.Items.Add(GetRes(RES_MATCH_CONTAINS)) ;
comboBoxMatch.Items.Add(GetRes(RES_MATCH_STARTS)) ;
comboBoxMatch.Items.Add(GetRes(RES_MATCH_ENDS)) ;
comboBoxMatch.SelectedIndex = curId < 0 ? 0 : curId ;
toolStripStatusLabel1.Text = GetRes(RES_STATUS_READY) ;
toolStripStatusLabel2.Visible = false ;
toolStripStatusLabel3.Visible = false ;
}
private void LoadSettings(){
//load ini params and use them to init sizes, languanges, etc.
int left ;
int.TryParse(ini[INI_WND_LEFT], out left) ;
if(left > 0) this.Left = left ;
int top ;
int.TryParse(ini[INI_WND_TOP], out top) ;
if(top > 0) this.Top = top ;
int type ;
int.TryParse(ini[INI_TYPE], out type) ;
if(type < 0 || type > 2) type = 0 ;
comboBoxType.SelectedIndex = type ;
int macth ;
int.TryParse(ini[INI_MATCH], out macth) ;
if(macth < 0 || macth > 3) macth = 0 ;
comboBoxMatch.SelectedIndex = macth ;
}
private void LoadLangSetting(){
string lang = ini[INI_LANG] ;
if(lang != "" && (Array.IndexOf(AVAILABLE_LANGS, lang) >= 0)){
ci = new CultureInfo(lang) ;
}else{
lang = CultureInfo.CurrentCulture.TwoLetterISOLanguageName ;
if(Array.IndexOf(AVAILABLE_LANGS, lang) > -1){
ci = CultureInfo.CurrentCulture ;
}else{
lang = AVAILABLE_LANGS[LANG_EN] ;
ci = new CultureInfo(lang) ;
}
}
}
private void StoreSettings(){
ini[INI_LANG] = this.ci.TwoLetterISOLanguageName ;
ini[INI_TYPE] = comboBoxType.SelectedIndex.ToString() ;
ini[INI_MATCH] = comboBoxMatch.SelectedIndex.ToString() ;
ini[INI_WND_LEFT] = this.Left.ToString() ;
ini[INI_WND_TOP] = this.Top.ToString() ;
}
void ButtonSearchClick(object sender, EventArgs e){
if(comboBoxDrive.SelectedIndex < 0){
MessageBox.Show(GetRes(RES_MSG_NODRIVE), GetRes(RES_MSG_TITLE_ERROR), MessageBoxButtons.OK, MessageBoxIcon.Exclamation) ;
}else if(textBoxFilter.Text.Length < 1){
MessageBox.Show(GetRes(RES_MSG_NOFILTER), GetRes(RES_MSG_TITLE_ERROR), MessageBoxButtons.OK, MessageBoxIcon.Exclamation) ;
}else{
toolStripStatusLabel1.Text = GetRes(RES_STATUS_SEARCHING) ;
TimeSpan elapsedTime ;
DateTime startTime ;
startTime = DateTime.Now;
listViewFiles.Items.Clear() ;
this.Refresh() ;
listViewFiles.BeginUpdate() ;
try {
NtfsUsnJournal.SearchType sType = (NtfsUsnJournal.SearchType)comboBoxType.SelectedIndex ;
NtfsUsnJournal.ComparisonType cType = (NtfsUsnJournal.ComparisonType)comboBoxMatch.SelectedIndex ;
UsnJournal.FileSet[] results ;
NtfsUsnJournal ntfs = new NtfsUsnJournal(new DriveInfo(comboBoxDrive.SelectedItem.ToString())) ;
NtfsUsnJournal.UsnJournalReturnCode code = ntfs.GetFilesMatchingName(out results, textBoxFilter.Text, sType, cType) ;
if(code == NtfsUsnJournal.UsnJournalReturnCode.USN_JOURNAL_SUCCESS){
int itemsCount = results.Length ;
ListViewItem[] lvItems = new ListViewItem[itemsCount] ;
for(int i=0; i<itemsCount; i++){
lvItems[i] = new ListViewItem(new String[]{results[i].Name, results[i].Path}) ;
}
listViewFiles.Items.AddRange(lvItems) ;
}else{
throw new Win32Exception((int)code);
}
}catch(Exception exc) {
MessageBox.Show(exc.Message, GetRes(RES_MSG_TITLE_ERROR), MessageBoxButtons.OK, MessageBoxIcon.Error) ;
}
listViewFiles.EndUpdate() ;
elapsedTime = DateTime.Now - startTime;
toolStripStatusLabel1.Text = GetRes(RES_STATUS_READY) ;
toolStripStatusLabel2.Visible = true ;
toolStripStatusLabel2.Text = GetRes(RES_STATUS_TIME) + elapsedTime.Minutes.ToString("00") + ":" + elapsedTime.Seconds.ToString("00") + "." + elapsedTime.Milliseconds.ToString() ;
toolStripStatusLabel3.Visible = true ;
toolStripStatusLabel3.Text = GetRes(RES_STATUS_FOUND) + listViewFiles.Items.Count ;
}
}
void ListViewFilesDoubleClick(object sender, EventArgs e){
string file = listViewFiles.SelectedItems[0].SubItems[1].Text ;
if(File.Exists(file) || Directory.Exists(file)){
ProcessStartInfo info = new ProcessStartInfo() ;
info.FileName = "explorer";
info.Arguments = string.Format("/e, /select, \"{0}\"", file) ;
try{
Process.Start(info) ;
}catch(Exception exc){
MessageBox.Show(exc.Message, GetRes(RES_MSG_TITLE_ERROR), MessageBoxButtons.OK, MessageBoxIcon.Error) ;
}
}else{
MessageBox.Show(GetRes(RES_MSG_FILENOTFOUND), GetRes(RES_MSG_TITLE_ERROR), MessageBoxButtons.OK, MessageBoxIcon.Exclamation) ;
}
}
void ExitToolStripMenuItemClick(object sender, EventArgs e){
Application.Exit() ;
}
void GreekToolStripMenuItemClick(object sender, EventArgs e){
ci = new CultureInfo(AVAILABLE_LANGS[LANG_EL]) ;
LoadRes() ;
}
void EnglishToolStripMenuItemClick(object sender, EventArgs e){
ci = new CultureInfo(AVAILABLE_LANGS[LANG_EN]) ;
LoadRes() ;
}
void AboutToolStripMenuItemClick(object sender, EventArgs e){
new AboutForm(ci).ShowDialog() ;
}
void MainFormFormClosed(object sender, FormClosedEventArgs e){
StoreSettings() ;
}
void MainFormLoad(object sender, EventArgs e){
LoadLangSetting() ;
Hide() ;
new SplashForm(ci).ShowDialog() ;
Show() ;
LoadNtfsDrives() ;
LoadRes() ;
LoadSettings() ;
}
void RefreshToolStripMenuItemClick(object sender, EventArgs e){
LoadNtfsDrives() ;
}
void ToolStripMenuItemMouseLeave(object sender, EventArgs e){
toolStripStatusLabel4.Text = string.Empty ;
}
void RefreshToolStripMenuItemMouseEnter(object sender, EventArgs e){
toolStripStatusLabel4.Text = GetRes(RES_MENU_REFRESH_TIP) ;
}
void ExitToolStripMenuItemMouseEnter(object sender, EventArgs e){
toolStripStatusLabel4.Text = GetRes(RES_MENU_EXIT_TIP) ;
}
void LangToolStripMenuItemMouseEnter(object sender, EventArgs e){
toolStripStatusLabel4.Text = GetRes(RES_MENU_LANGUAGE_TIP) ;
}
void AboutToolStripMenuItemMouseEnter(object sender, EventArgs e){
toolStripStatusLabel4.Text = GetRes(RES_MENU_ABOUT_TIP) ;
}
}
}