-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrmMain.cs
More file actions
325 lines (283 loc) · 9.21 KB
/
frmMain.cs
File metadata and controls
325 lines (283 loc) · 9.21 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using System.Collections.Specialized;
using System.IO;
namespace CmdQueue
{
public partial class frmMain : Form
{
private bool queueStarted = false;
private bool processInProgress = false;
public frmMain() {
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
StringCollection commandHistory = AppSettings.Instance.CommandHistory;
if ( commandHistory != null ) {
foreach( string command in commandHistory ) {
cmbNewCommand.Items.Add( command );
}
}
openFileDialog.Filter = "Programs|*.exe|Batch files|*.bat;*.cmd|All files|*.*";
lblStatus.Text = "Waiting...";
lblVersion.Text = "v" + Application.ProductVersion;
RefreshListbox();
ProcessArguments( Program.PassedArguments );
}
public void ProcessArguments( string[] args ) {
// check for passed arguments
if ( args.Length > 0 ) {
string argument = "";
string nextArgument = "";
bool skipNext = false;
string newCommand = "";
string newName = "";
string newStartIn = "";
bool doStartQueue = false;
bool doStopQueue = false;
bool doAdd = false;
for( int index = 0; index < args.Length; index++ ) {
skipNext = false;
argument = args[index].ToUpper();
if ( args.Length > index + 1 ) {
nextArgument = args[index + 1];
} else {
nextArgument = "";
}
if ( argument == "/NAME" ) {
newName = nextArgument;
skipNext = true;
}
if ( argument == "/STARTIN" ) {
newStartIn = nextArgument;
skipNext = true;
}
if ( argument == "/START" ) {
doStartQueue = true;
doStopQueue = false;
}
if ( argument == "/STOP" ) {
doStopQueue = true;
doStartQueue = false;
}
if ( argument == "/ADD" ) {
// all of the other arguments after this one form the command
newCommand = "";
if ( nextArgument != "" ) {
for ( int index2 = index + 1; index2 < args.Length; index2++ ) {
string thisArgument = args[index2];
if ( thisArgument.Contains(" ") ) {
thisArgument = "\"" + thisArgument + "\"";
}
newCommand += thisArgument + " ";
}
doAdd = true;
break;
}
}
if ( skipNext ) {
index++;
}
}
if ( doAdd && newCommand != "" ) {
AddNewJob( newCommand, newName, newStartIn );
}
if ( ( doStartQueue && !queueStarted ) || ( doStopQueue && queueStarted ) ) {
RefreshListbox();
StartStopQueue();
}
}
}
protected override void WndProc( ref Message m ) {
if ( m.Msg == NativeMethods.WM_COPYDATA ) {
// Extract the file name
NativeMethods.COPYDATASTRUCT copyData =
(NativeMethods.COPYDATASTRUCT)Marshal.PtrToStructure
( m.LParam, typeof( NativeMethods.COPYDATASTRUCT ) );
int dataType = (int)copyData.dwData;
if ( dataType == 2 ) {
string argsString = Marshal.PtrToStringAnsi( copyData.lpData );
string[] args = JsonConvert.DeserializeObject<string[]>( argsString );
if ( args.Length > 0 ) {
ProcessArguments( args );
}
} else {
MessageBox.Show( String.Format( "Unrecognized data type = {0}.",
dataType ), "CmdQueue Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
} else {
base.WndProc( ref m );
}
}
private void btnAdd_Click( object sender, EventArgs e ) {
string Command = cmbNewCommand.Text;
string Name = txtName.Text;
string StartIn = txtStartIn.Text;
if ( Command.Trim() != "" ) {
bool addResult = AddNewJob( Command, Name, StartIn );
if ( addResult ) {
if ( !cmbNewCommand.Items.Contains( Command ) ) {
cmbNewCommand.Items.Add( Command );
StringCollection commandHistory = new StringCollection();
foreach ( object command in cmbNewCommand.Items ) {
commandHistory.Add( command.ToString() );
// limit to 20 last used commands
if ( commandHistory.Count > 20 ) {
break;
}
}
AppSettings.Instance.CommandHistory = null;
AppSettings.Instance.CommandHistory = commandHistory;
AppSettings.Instance.Save();
}
cmbNewCommand.Text = "";
txtName.Text = "";
txtStartIn.Text = "";
cmbNewCommand.Focus();
}
}
}
private bool AddNewJob( string Command, string Name = "", string StartIn = "" ) {
if ( StartIn != "" && !Directory.Exists( StartIn ) ) {
MessageBox.Show( "The Start In directory specified does not exist!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
return false;
}
Job job = new Job();
job.SetCommandFromString( Command );
job.StartDirectory = StartIn;
job.Name = Name;
Program.JobQueue.AddItem( job );
RefreshListbox();
return true;
}
private void btnDelete_Click( object sender, EventArgs e ) {
ListBox.SelectedIndexCollection indices = lstCurrentQueue.SelectedIndices;
foreach( int index in indices ) {
Program.JobQueue.RemoveItem( index );
}
RefreshListbox();
}
private void btnMoveUp_Click( object sender, EventArgs e ) {
ListBox.SelectedIndexCollection indices = lstCurrentQueue.SelectedIndices;
foreach ( int index in indices ) {
Program.JobQueue.MoveItemUp( index );
}
RefreshListbox( indices, -1 );
}
private void btnMoveDown_Click( object sender, EventArgs e ) {
ListBox.SelectedIndexCollection indices = lstCurrentQueue.SelectedIndices;
foreach ( int index in indices ) {
Program.JobQueue.MoveItemDown( index );
}
RefreshListbox( indices, 1 );
}
private void lstCurrentQueue_DoubleClick( object sender, EventArgs e ) {
if ( lstCurrentQueue.SelectedIndex != null ) {
frmJobDetails frm = new frmJobDetails( lstCurrentQueue.SelectedIndex );
frm.ShowDialog();
RefreshListbox();
}
}
private void lstCurrentQueue_SelectedIndexChanged( object sender, EventArgs e ) {
if ( queueStarted && lstCurrentQueue.SelectedIndex == 0 ) {
lstCurrentQueue.SelectedIndex = -1;
}
}
private void RefreshListbox( ListBox.SelectedIndexCollection indices = null, int offset = 0 ) {
lstCurrentQueue.DataSource = null;
lstCurrentQueue.DataSource = Program.JobQueue.Jobs;
lstCurrentQueue.Refresh();
if ( indices != null && offset != 0 ) {
int[] array = indices.Cast<int>().ToArray();
for ( int index = 0; index < lstCurrentQueue.Items.Count; index++ ) {
lstCurrentQueue.SetSelected( index, false );
}
foreach( int index in array ) {
if ( index > -1 && index < lstCurrentQueue.Items.Count ) {
lstCurrentQueue.SetSelected( index + offset, true );
}
}
}
}
private void btnStart_Click( object sender, EventArgs e ) {
StartStopQueue();
}
private void StartStopQueue() {
if ( !queueStarted ) {
queueStarted = true;
btnStart.Text = "Stop";
ProcessNextJob();
} else {
queueStarted = false;
btnStart.Text = "Start";
}
}
private void ProcessNextJob() {
if ( Program.JobQueue.Jobs.Count > 0 ) {
if ( !processInProgress && Program.JobQueue.HasIndex( 0 ) ) {
Job job = Program.JobQueue.Jobs[0];
Process process = Program.JobQueue.GetProcess( 0 );
process.Exited += new EventHandler( handleProcessExited );
Program.JobQueue.RemoveItem( 0 );
RefreshListbox();
try {
process.Start();
processInProgress = true;
lblStatus.Text = "Running: " + process.StartInfo.FileName + " " + process.StartInfo.Arguments;
} catch ( Exception e ) {
ProcessNextJob();
}
}
} else {
queueStarted = false;
btnStart.Text = "Start";
lblStatus.Text = "";
}
}
private void handleProcessExited( object sender, EventArgs e ) {
processInProgress = false;
if ( queueStarted ) {
ProcessNextJob();
} else {
lblStatus.Text = "";
}
}
private void frmMain_Load( object sender, EventArgs e ) {
NativeMethods.CHANGEFILTERSTRUCT changeFilter = new NativeMethods.CHANGEFILTERSTRUCT();
changeFilter.size = (uint)Marshal.SizeOf( changeFilter );
changeFilter.info = 0;
if ( !NativeMethods.ChangeWindowMessageFilterEx
( this.Handle, NativeMethods.WM_COPYDATA,
NativeMethods.ChangeWindowMessageFilterExAction.Allow, ref changeFilter ) ) {
int error = Marshal.GetLastWin32Error();
MessageBox.Show( String.Format( "The error {0} occurred.", error ) );
}
}
private void lblWebURL_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e ) {
Process.Start( "https://gazchap.com/cmdqueue/" );
}
private void btnBrowseStartIn_Click( object sender, EventArgs e ) {
if ( txtStartIn.Text != "" && Directory.Exists( txtStartIn.Text ) ) {
folderBrowserDialog.SelectedPath = txtStartIn.Text;
}
if ( folderBrowserDialog.ShowDialog() == DialogResult.OK ) {
txtStartIn.Text = folderBrowserDialog.SelectedPath;
}
}
private void btnOpenFile_Click( object sender, EventArgs e ) {
if ( cmbNewCommand.Text != "" ) {
openFileDialog.FileName = Path.GetFileName( cmbNewCommand.Text );
openFileDialog.InitialDirectory = Path.GetDirectoryName( cmbNewCommand.Text );
} else {
openFileDialog.FileName = "";
}
if ( openFileDialog.ShowDialog() == DialogResult.OK ) {
cmbNewCommand.Text = openFileDialog.FileName;
}
}
}
}