-
Notifications
You must be signed in to change notification settings - Fork 0
/
OperationsProgressDialog.cs
534 lines (493 loc) · 20.4 KB
/
OperationsProgressDialog.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProgressDialogs
{
public class OperationsProgressDialog : Component
{
const string CLSID_ProgressDialog = "{F8383852-FCD3-11d1-A6B9-006097DF5BD4}";
const string IDD_ProgressDialog = "0C9FB851-E5C9-43EB-A370-F0677B13874C";
const string CLSID_IShellItem = "{2559a1f3-21d7-11d4-bdaf-00c04f60b9f0}";
private Type _progressDialogType;
private Type _IShellItemType;
private IOperationsProgressDialog _nativeProgressDialog;
private PROGDLG dialogFlags;
private SPACTION operationFlags;
private PDMODE modeFlags;
private long currentProgress;
private long totalProgress;
private long currentSize;
private long totalSize;
private long currentItems;
private long totalItems;
private bool estimateValue;
private IShellItem sourceItem;
private IShellItem destItem;
private IShellItem currentItem;
private DIALOGSTATUS dialogStatus;
public enum DIALOGSTATUS
{
DLG_NOTSTARTED = 0,
DLG_RUNNING = 1,
DLG_DISPOSED = 2,
DLG_ERRORED = 3,
}
public DIALOGSTATUS IsDialogActive
{
get { return dialogStatus; }
}
public PROGDLG DialogFlags
{
set { dialogFlags = value; }
get { return dialogFlags; }
}
public SPACTION OperationFlags
{
set { operationFlags = value; }
get { return operationFlags; }
}
public PDMODE ModeFlags
{
set { modeFlags = value; }
get { return modeFlags; }
}
public long ProgressBarValue
{
get { return currentProgress; }
set { currentProgress = value; UpdateProgress(); }
}
public long ProgressBarMaxValue
{
get { return totalProgress; }
set { totalProgress = value; UpdateProgress(); }
}
public long ProgressDialogSizeValue
{
get { return currentSize; }
set { currentSize = value; UpdateProgress(); }
}
public long ProgressDialogSizeMaxValue
{
get { return totalSize; }
set { totalSize = value; UpdateProgress(); }
}
public long ProgressDialogItemsValue
{
get { return currentItems; }
set { currentItems = value; UpdateProgress(); }
}
public long ProgressDialogItemsMaxValue
{
get { return totalItems; }
set { totalItems = value; UpdateProgress(); }
}
/// <summary>
/// Checks if the dialog has reached 100%.
/// </summary>
public bool isFinished
{
get { return currentProgress == totalProgress; }
}
/// <summary>
/// Returns a boolean of if the user has cancelled or not.
/// </summary>
public bool hasUserCancelled
{
get { if (_nativeProgressDialog != null) { return _nativeProgressDialog.GetOperationStatus() == PDOPSTATUS.PDOPS_CANCELLED; } else { return false; } }
}
/// <summary>
/// Estimates the progress based on the item count.
/// </summary>
public bool EstimateValue
{
set { estimateValue = value; }
}
/// <summary>
/// Closes the dialog.
/// </summary>
public async void Close()
{
if (_nativeProgressDialog != null && dialogStatus != DIALOGSTATUS.DLG_DISPOSED)
{
_nativeProgressDialog.StopProgressDialog();
dialogStatus = DIALOGSTATUS.DLG_DISPOSED;
await Task.Delay(900);
Marshal.FinalReleaseComObject(_nativeProgressDialog);
_nativeProgressDialog = null;
}
}
private void UpdateProgress()
{
if (estimateValue)
{
currentProgress = currentItems;
totalProgress = totalItems;
}
if (_nativeProgressDialog != null && dialogStatus != DIALOGSTATUS.DLG_DISPOSED)
{
_nativeProgressDialog.UpdateProgress((ulong)currentProgress, (ulong)totalProgress, (ulong)currentSize, (ulong)totalSize, (ulong)currentItems, (ulong)totalItems);
_nativeProgressDialog.UpdateLocations(sourceItem, destItem, currentItem);
}
}
/// <summary>
/// Initializes the dialog.
/// </summary>
public OperationsProgressDialog()
{
dialogStatus = DIALOGSTATUS.DLG_NOTSTARTED;
_progressDialogType = Type.GetTypeFromCLSID(new Guid(CLSID_ProgressDialog));
_IShellItemType = Type.GetTypeFromCLSID(new Guid(CLSID_IShellItem));
dialogFlags = PROGDLG.PROGDLG_NORMAL;
operationFlags = SPACTION.SPACTION_NONE;
modeFlags = PDMODE.PDM_DEFAULT;
currentProgress = 0;
totalProgress = 100;
currentSize = 0;
totalSize = 100;
currentItems = 0;
totalItems = 100;
estimateValue = false;
SHCreateItemFromParsingName("https://andai.heliohost.org/packages.php", IntPtr.Zero, typeof(IShellItem).GUID, out sourceItem);
SHCreateItemFromParsingName(Environment.CurrentDirectory, IntPtr.Zero, typeof(IShellItem).GUID, out destItem);
SHCreateItemFromParsingName(Environment.CurrentDirectory, IntPtr.Zero, typeof(IShellItem).GUID, out currentItem);
}
public OperationsProgressDialog(IContainer container)
: this() {
container.Add(this);
}
/// <summary>
/// Shows the dialog with the specified parent. If the parent is null, uses the active form.
/// </summary>
/// <param name="parent"></param>
public void Show(IWin32Window parent)
{
if (parent == null) parent = Form.ActiveForm;
IntPtr handle = (parent == null) ? IntPtr.Zero : parent.Handle;
_nativeProgressDialog = (IOperationsProgressDialog)Activator.CreateInstance(_progressDialogType);
_nativeProgressDialog.StartProgressDialog(handle, dialogFlags);
_nativeProgressDialog.SetOperation(operationFlags);
_nativeProgressDialog.SetMode(modeFlags);
UpdateProgress();
dialogStatus = DIALOGSTATUS.DLG_RUNNING;
}
[ComImport, Guid(IDD_ProgressDialog), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IOperationsProgressDialog
{
void StartProgressDialog(IntPtr hwndOwner, PROGDLG flags);
void StopProgressDialog();
void SetOperation(SPACTION action);
void SetMode(PDMODE mode);
void UpdateProgress(ulong ullPointsCurrent, ulong ullPointsTotal, ulong ullSizeCurrent, ulong ullSizeTotal, ulong ullItemsCurrent, ulong ullItemsTotal);
void UpdateLocations(IShellItem psiSource, IShellItem psiTarget, IShellItem psiItem);
void ResetTimer();
void PauseTimer();
void ResumeTimer();
void GetMilliseconds(ulong pullElapsed, ulong pullRemaining);
PDOPSTATUS GetOperationStatus();
}
/// <summary>
/// Shows the dialog (if no settings were modified, uses default).
/// </summary>
internal void Show()
{
Show(null);
}
/// <summary>
/// Specifies what the dialog is currently doing.
/// </summary>
/// <param name="operation"></param>
internal void SetOperation(SPACTION operation)
{
if(_nativeProgressDialog != null && dialogStatus != DIALOGSTATUS.DLG_DISPOSED)
{
_nativeProgressDialog.SetOperation(operation);
}
}
/// <summary>
/// Specifies the status of the current operation.
/// </summary>
/// <param name="mode"></param>
internal void SetMode(PDMODE mode)
{
if (_nativeProgressDialog != null && dialogStatus!=DIALOGSTATUS.DLG_DISPOSED)
{
_nativeProgressDialog.SetMode(mode);
}
}
/// <summary>
/// Specifies the items being used.
/// </summary>
/// <param name="source"></param>
/// <param name="destination"></param>
/// <param name="item"></param>
internal void UpdateLocations(string source, string destination, string item)
{
try
{
SHCreateItemFromParsingName(source, IntPtr.Zero, typeof(IShellItem).GUID, out sourceItem);
SHCreateItemFromParsingName(destination, IntPtr.Zero, typeof(IShellItem).GUID, out destItem);
SHCreateItemFromParsingName(item, IntPtr.Zero, typeof(IShellItem).GUID, out currentItem);
}
catch (FileNotFoundException ex)
{
throw new IOperationsProgressDialogException("One or more of the locations do not exist.", ex);
}
UpdateProgress();
}
internal void UpdateLocations(string source, string destination)
{
try
{
System.Diagnostics.Debug.Print(Environment.CurrentDirectory + " " + source + " " + destination);
SHCreateItemFromParsingName(source, IntPtr.Zero, typeof(IShellItem).GUID, out sourceItem);
SHCreateItemFromParsingName(destination, IntPtr.Zero, typeof(IShellItem).GUID, out destItem);
}
catch (FileNotFoundException ex)
{
throw new IOperationsProgressDialogException("One or more of the locations do not exist.", ex);
}
UpdateProgress();
}
internal void UpdateLocations(string item)
{
try
{
SHCreateItemFromParsingName(item.Replace("/", "\\"), IntPtr.Zero, typeof(IShellItem).GUID, out currentItem);
}
catch (FileNotFoundException ex)
{
try
{
SHCreateItemFromParsingName(Environment.CurrentDirectory + "\\" + item.Replace("/", "\\"), IntPtr.Zero, typeof(IShellItem).GUID, out currentItem);
}
catch (FileNotFoundException)
{
throw new IOperationsProgressDialogException("One or more of the locations do not exist.", ex);
}
}
UpdateProgress();
}
public enum PROGDLG : uint
{
/// <summary>
/// Default, normal progress dialog behavior.
/// </summary>
PROGDLG_NORMAL = 0x00000000,
/// <summary>
/// The dialog is modal to its hwndOwner. The default setting is modeless.
/// </summary>
PROGDLG_MODAL = 0x00000001,
/// <summary>
/// Update "Line3" text with the time remaining. This flag does not need to be implicitly set because progress dialogs started by IOperationsProgressDialog::StartProgressDialog automatically display the time remaining.
/// </summary>
PROGDLG_AUTOTIME = 0x00000002,
/// <summary>
/// Do not show the time remaining. We do not recommend setting this flag through IOperationsProgressDialog because it goes against the purpose of the dialog.
/// </summary>
PROGDLG_NOTIME = 0x00000004,
/// <summary>
/// Do not display the minimize button.
/// </summary>
PROGDLG_NOMINIMIZE = 0x00000008,
/// <summary>
/// Do not display the progress bar.
/// </summary>
PROGDLG_NOPROGRESSBAR = 0x00000010,
/// <summary>
/// This flag is invalid in this method. To set the progress bar to marquee mode, use the flags in IOperationsProgressDialog::SetMode.
/// </summary>
PROGDLG_MARQUEEPROGRESS = 0x00000020,
/// <summary>
/// Do not display a cancel button because the operation cannot be canceled. Use this value only when absolutely necessary.
/// </summary>
PROGDLG_NOCANCEL = 0x00000040,
/// <summary>
/// Windows 7 and later. Indicates default, normal operation progress dialog behavior.
/// </summary>
OPPROGDLG_DEFAULT = 0x00000000,
/// <summary>
/// Display a pause button. Use this only in situations where the operation can be paused.
/// </summary>
OPPROGDLG_ENABLEPAUSE = 0x00000080,
/// <summary>
/// The operation can be undone through the dialog. The Stop button becomes Undo. If pressed, the Undo button then reverts to Stop.
/// </summary>
OPPROGDLG_ALLOWUNDO = 0x00000100,
/// <summary>
/// Do not display the path of source file in the progress dialog.
/// </summary>
OPPROGDLG_DONTDISPLAYSOURCEPATH = 0x00000200,
/// <summary>
/// Do not display the path of the destination file in the progress dialog.
/// </summary>
OPPROGDLG_DONTDISPLAYDESTPATH = 0x00000400,
/// <summary>
/// Windows 7 and later. If the estimated time to completion is greater than one day, do not display the time.
/// </summary>
OPPROGDLG_NOMULTIDAYESTIMATES = 0x00000800,
/// <summary>
/// Windows 7 and later. Do not display the location line in the progress dialog.
/// </summary>
OPPROGDLG_DONTDISPLAYLOCATIONS = 0x00001000
}
public enum PDMODE : uint
{
/// <summary>
/// Use the default progress dialog operations mode.
/// </summary>
PDM_DEFAULT = 0x00000000,
/// <summary>
/// The operation is running.
/// </summary>
PDM_RUN = 0x00000001,
/// <summary>
/// The operation is gathering data before it begins, such as calculating the predicted operation time.
/// </summary>
PDM_PREFLIGHT = 0x00000002,
/// <summary>
/// The operation is rolling back due to an Undo command from the user.
/// </summary>
PDM_UNDOING = 0x00000004,
/// <summary>
/// Error dialogs are blocking progress from continuing.
/// </summary>
/// <remarks>
/// Appears to only work when progress has already begun.
/// </remarks>
PDM_ERRORSBLOCKING = 0x00000008,
/// <summary>
/// The length of the operation is indeterminate. Do not show a timer and display the progress bar in marquee mode.
/// </summary>
PDM_INDETERMINATE = 0x00000010
}
public enum SPACTION : uint
{
/// <summary>
/// No action is being performed.
/// </summary>
SPACTION_NONE = 0,
/// <summary>
/// Files are being moved.
/// </summary>
SPACTION_MOVING,
/// <summary>
/// Files are being copied.
/// </summary>
SPACTION_COPYING,
/// <summary>
/// Files are being deleted.
/// </summary>
SPACTION_RECYCLING,
/// <summary>
/// A set of attributes are being applied to files.
/// </summary>
SPACTION_APPLYINGATTRIBS,
/// <summary>
/// A file is being downloaded from a remote source.
/// </summary>
SPACTION_DOWNLOADING,
/// <summary>
/// An Internet search is being performed.
/// </summary>
SPACTION_SEARCHING_INTERNET,
/// <summary>
/// A calculation is being performed.
/// </summary>
SPACTION_CALCULATING,
/// <summary>
/// A file is being uploaded to a remote source.
/// </summary>
SPACTION_UPLOADING,
/// <summary>
/// A local search is being performed.
/// </summary>
SPACTION_SEARCHING_FILES,
/// <summary>
/// Windows Vista and later. A deletion is being performed.
/// </summary>
SPACTION_DELETING,
/// <summary>
/// Windows Vista and later. A renaming action is being performed.
/// </summary>
SPACTION_RENAMING,
/// <summary>
/// Windows Vista and later. A formatting action is being performed.
/// </summary>
SPACTION_FORMATTING,
/// <summary>
/// Windows 7 and later. A copy or move action is being performed.
/// </summary>
SPACTION_COPY_MOVING
}
public enum PDOPSTATUS : uint
{
/// <summary>
/// Operation is running, no user intervention.
/// </summary>
PDOPS_RUNNING = 1,
/// <summary>
/// Operation has been paused by the user.
/// </summary>
PDOPS_PAUSED = 2,
/// <summary>
/// Operation has been canceled by the user - now go undo.
/// </summary>
PDOPS_CANCELLED = 3,
/// <summary>
/// Operation has been stopped by the user - terminate completely.
/// </summary>
PDOPS_STOPPED = 4,
/// <summary>
/// Operation has gone as far as it can go without throwing error dialogs.
/// </summary>
PDOPS_ERRORS = 5
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")]
public interface IShellItem
{
void BindToHandler(IntPtr pbc,
[MarshalAs(UnmanagedType.LPStruct)]Guid bhid,
[MarshalAs(UnmanagedType.LPStruct)]Guid riid,
out IntPtr ppv);
void GetParent(out IShellItem ppsi);
void GetDisplayName(SIGDN sigdnName, out IntPtr ppszName);
void GetAttributes(uint sfgaoMask, out uint psfgaoAttribs);
void Compare(IShellItem psi, uint hint, out int piOrder);
};
[DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
public static extern void SHCreateItemFromParsingName([In][MarshalAs(UnmanagedType.LPWStr)] string pszPath, [In] IntPtr pbc, [In][MarshalAs(UnmanagedType.LPStruct)]Guid riid, [Out][MarshalAs(UnmanagedType.Interface, IidParameterIndex = 2)] out IShellItem ppv);
public enum SIGDN : uint
{
NORMALDISPLAY = 0,
PARENTRELATIVEPARSING = 0x80018001,
PARENTRELATIVEFORADDRESSBAR = 0x8001c001,
DESKTOPABSOLUTEPARSING = 0x80028000,
PARENTRELATIVEEDITING = 0x80031001,
DESKTOPABSOLUTEEDITING = 0x8004c000,
FILESYSPATH = 0x80058000,
URL = 0x80068000
}
}
[Serializable]
internal class IOperationsProgressDialogException : Exception
{
public IOperationsProgressDialogException()
{
}
public IOperationsProgressDialogException(string message) : base(message)
{
}
public IOperationsProgressDialogException(string message, Exception innerException) : base(message, innerException)
{
}
protected IOperationsProgressDialogException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}