forked from canneverbe/Ketarin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplicationJobsListView.cs
397 lines (335 loc) · 12.8 KB
/
ApplicationJobsListView.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using CDBurnerXP.Controls;
using CDBurnerXP.IO;
using Ketarin.Forms;
using Ketarin.Properties;
using ContentAlignment = System.Drawing.ContentAlignment;
using TextBox = Ketarin.Forms.TextBox;
namespace Ketarin
{
public class ApplicationJobsListView : ObjectListView
{
private readonly SearchPanel searchPanel = new SearchPanel();
private readonly TextBox searchTextBox = new TextBox();
private List<ApplicationJob> preSearchList;
private readonly CheckBox enabledJobsCheckbox = new CheckBox();
public const string DefaultEmptyMessage = "No applications have been added yet.";
/// <summary>
/// Fires when the filter of the ListView has changed.
/// </summary>
public event EventHandler FilterChanged;
#region Properties
/// <summary>
/// Gets whether or not the default filter (that is, none) is active.
/// </summary>
[Browsable(false)]
public bool IsDefaultFilter
{
get
{
return string.IsNullOrEmpty(searchTextBox.Text) && (enabledJobsCheckbox.CheckState == CheckState.Indeterminate);
}
}
/// <summary>
/// Gets the currently selected applications.
/// </summary>
[Browsable(false)]
public ApplicationJob[] SelectedApplications
{
get
{
return this.SelectedObjects.Cast<ApplicationJob>().ToArray();
}
}
#endregion
#region ProgressRenderer
public class ProgressRenderer : BarRenderer
{
private readonly Updater m_Updater;
public ProgressRenderer(Updater updater, int min, int max)
: base(min, max)
{
m_Updater = updater;
}
public override void Render(Graphics g, Rectangle r)
{
ApplicationJob job = RowObject as ApplicationJob;
// Do not draw anything if the updater is not currently working
if (m_Updater.GetProgress(job) == -1)
{
this.DrawBackground(g, r);
return;
}
base.Render(g, r);
long fileSize = m_Updater.GetDownloadSize(job);
// No file size has been determined yet
if (fileSize == -2) return;
using (Brush fontBrush = new SolidBrush(SystemColors.WindowText))
{
StringFormat format = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
string text = FormatFileSize.Format(fileSize);
if (fileSize < 0)
{
text = "(unknown)";
}
g.DrawString(text, new Font(this.Font, FontStyle.Bold), fontBrush, r, format);
}
}
}
#endregion
#region Search panel
private class SearchPanel : Panel
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (Pen pen = new Pen(VisualStyleInformation.TextControlBorder))
{
e.Graphics.DrawLine(pen, 0, 0, Width, 0);
e.Graphics.DrawLine(Pens.White, 0, 1, Width, 1);
}
}
}
private class CloseButton : Panel
{
private Bitmap drawImage = Resources.CloseSearch;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawImage(drawImage, new Point(0, 0));
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
drawImage = Resources.CloseSearchHover;
Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
drawImage = Resources.CloseSearchDown;
Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
drawImage = Resources.CloseSearch;
Invalidate();
}
}
#endregion
public void Initialize()
{
searchPanel.Dock = DockStyle.Bottom;
searchPanel.AutoSize = true;
searchPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
searchPanel.Visible = false;
searchPanel.BackColor = SystemColors.Control;
Label searchLabel = new Label
{
Text = "&Search: ",
Location = new Point(25, 7),
AutoSize = true
};
searchTextBox.Width = 200;
searchTextBox.Location = new Point(searchLabel.GetPreferredSize(searchLabel.Size).Width + 25, 4);
searchTextBox.TextChanged += this.searchTextBox_TextChanged;
CloseButton closeButton = new CloseButton
{
Size = new Size(16, 16),
Location = new Point(3, 6)
};
closeButton.Click += this.closeButton_Click;
enabledJobsCheckbox.Text = "Show &enabled applications";
enabledJobsCheckbox.ThreeState = true;
enabledJobsCheckbox.Location = new Point(searchTextBox.Right + 6, 6);
enabledJobsCheckbox.TextAlign = ContentAlignment.MiddleLeft;
enabledJobsCheckbox.AutoSize = true;
enabledJobsCheckbox.CheckState = CheckState.Indeterminate;
enabledJobsCheckbox.CheckStateChanged += this.enabledJobsCheckbox_CheckStateChanged;
searchPanel.Controls.Add(closeButton);
searchPanel.Controls.Add(searchLabel);
searchPanel.Controls.Add(searchTextBox);
searchPanel.Controls.Add(enabledJobsCheckbox);
this.Controls.Add(searchPanel);
}
public override void SetObjects(IEnumerable collection)
{
base.SetObjects(collection);
this.EmptyListMsg = DefaultEmptyMessage;
}
private void enabledJobsCheckbox_CheckStateChanged(object sender, EventArgs e)
{
RefreshFilter();
}
private void closeButton_Click(object sender, EventArgs e)
{
HideSearch();
}
private void searchTextBox_TextChanged(object sender, EventArgs e)
{
RefreshFilter();
}
private void RefreshFilter()
{
// Restore original list if no search text is given
if (IsDefaultFilter && this.preSearchList != null)
{
SetObjects(this.preSearchList.ToArray());
OnFilterChanged();
return;
}
// No search if not visible
if (!this.searchPanel.Visible)
{
return;
}
if (preSearchList == null)
{
preSearchList = this.Objects.Cast<ApplicationJob>().ToList();
}
// Nothing to do if empty
if (preSearchList == null || preSearchList.Count == 0)
{
return;
}
List<ApplicationJob> filteredList = new List<ApplicationJob>();
// Cache some data
Dictionary<string, string> customColumns = SettingsDialog.CustomColumns;
string[] searchText = searchTextBox.Text.ToLower().Split(' ');
foreach (ApplicationJob job in preSearchList)
{
// Filter job by enabled status
if (enabledJobsCheckbox.CheckState != CheckState.Indeterminate)
{
if (enabledJobsCheckbox.Checked != job.Enabled)
{
continue;
}
}
if (job.MatchesSearchCriteria(searchText, customColumns))
{
filteredList.Add(job);
}
}
this.SetObjects(filteredList.ToArray());
OnFilterChanged();
}
protected virtual void OnFilterChanged()
{
if (FilterChanged != null)
{
FilterChanged(this, null);
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
int openVariable = 0;
switch (keyData)
{
case Keys.Control | Keys.F:
ShowSearch();
return true;
case Keys.Escape:
if (this.searchPanel.Visible)
{
HideSearch();
return true;
}
break;
// Open specific variable in browser
case Keys.Control | Keys.D1: openVariable = 1; break;
case Keys.Control | Keys.D2: openVariable = 2; break;
case Keys.Control | Keys.D3: openVariable = 3; break;
case Keys.Control | Keys.D4: openVariable = 4; break;
case Keys.Control | Keys.D5: openVariable = 5; break;
case Keys.Control | Keys.D6: openVariable = 6; break;
case Keys.Control | Keys.D7: openVariable = 7; break;
case Keys.Control | Keys.D8: openVariable = 8; break;
case Keys.Control | Keys.D9: openVariable = 9; break;
}
// Open specific variable in browser
if (openVariable > 0)
{
ApplicationJob job = SelectedObject as ApplicationJob;
if (job != null)
{
int count = 0;
foreach (UrlVariable variable in job.Variables.Values)
{
count++;
if (count == openVariable)
{
try
{
Process.Start(variable.VariableType == UrlVariable.Type.Textual
? variable.GetExpandedTextualContent(DateTime.MinValue)
: variable.ExpandedUrl);
}
catch (Exception) { }
break;
}
}
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
/// <summary>
/// Shows the search bar and sets the focus to it.
/// </summary>
public void ShowSearch()
{
// Begin/EndUpdate for Mono
this.BeginUpdate();
this.searchPanel.Visible = true;
this.EndUpdate();
this.searchTextBox.Focus();
this.EmptyListMsg = "No applications match your search criteria.";
}
private void HideSearch()
{
this.EmptyListMsg = DefaultEmptyMessage;
this.searchPanel.Visible = false;
this.searchTextBox.Text = string.Empty;
this.enabledJobsCheckbox.CheckState = CheckState.Indeterminate;
this.preSearchList = null;
}
/// <summary>
/// Deletes all selected applications after user confirmation.
/// </summary>
/// <returns>true, if applications have been deleted</returns>
public bool DeleteSelectedApplications()
{
if (SelectedObjects.Count == 0)
{
return false;
}
if (DeleteApplicationDialog.Show(this, SelectedObjects))
{
if (preSearchList != null)
{
foreach (ApplicationJob job in SelectedObjects)
{
preSearchList.Remove(job);
}
}
RemoveObjects(SelectedObjects);
return true;
}
return false;
}
}
}