forked from KSP-CKAN/CKAN-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.cs
123 lines (103 loc) · 3.96 KB
/
User.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
// Communicate with the user (status messages, yes/no questions, etc)
// This class will proxy to either the GUI or cmdline functionality.
using System;
namespace CKAN
{
public delegate int DisplaySelectionDialog(string message, params object[] args);
public delegate void DisplayMessage(string message, params object[] args);
public delegate bool DisplayYesNoDialog(string message);
public delegate void DisplayError(string message, params object[] args);
public delegate void ReportProgress(string format, int percent);
public delegate void DownloadsComplete(Uri[] urls, string[] filenames, Exception[] errors);
public interface IUser
{
event DisplayYesNoDialog AskUser;
event DisplaySelectionDialog AskUserForSelection;
event DisplayMessage Message;
event DisplayError Error;
event ReportProgress Progress;
event DownloadsComplete DownloadsComplete;
int WindowWidth { get; }
bool Headless { get; }
int RaiseSelectionDialog(string message, params object[] args);
void RaiseMessage(string message, params object[] url);
void RaiseProgress(string message, int percent);
bool RaiseYesNoDialog(string question);
void RaiseError(string message, params object[] args);
void RaiseDownloadsCompleted(Uri[] file_urls, string[] file_paths, Exception[] errors);
}
//Can be used in tests to supress output or as a base class for other types of user.
//It supplies no opp event handlers so that subclasses can avoid null checks.
public class NullUser : IUser
{
public static readonly IUser User = new NullUser();
public NullUser()
{
AskUser += DisplayYesNoDialog;
AskUserForSelection += DisplaySelectionDialog;
Message += DisplayMessage;
Error += DisplayError;
Progress += ReportProgress;
DownloadsComplete += ReportDownloadsComplete;
}
public event DisplayYesNoDialog AskUser;
public event DisplaySelectionDialog AskUserForSelection;
public event DisplayMessage Message;
public event DisplayError Error;
public event ReportProgress Progress;
public event DownloadsComplete DownloadsComplete;
public virtual bool Headless
{
get { return false; }
}
protected virtual bool DisplayYesNoDialog(string message)
{
return true;
}
protected virtual void DisplayMessage(string message, params object[] args)
{
}
protected virtual int DisplaySelectionDialog(string message, params object[] args)
{
return 0;
}
protected virtual void DisplayError(string message, params object[] args)
{
}
protected virtual void ReportProgress(string format, int percent)
{
}
protected virtual void ReportDownloadsComplete(Uri[] urls, string[] filenames, Exception[] errors)
{
}
public virtual int WindowWidth
{
get { return -1; }
}
public void RaiseMessage(string message, params object[] url)
{
Message(message, url);
}
public void RaiseProgress(string message, int percent)
{
Progress(message, percent);
}
public bool RaiseYesNoDialog(string question)
{
//Return value will be from last handler added.
return AskUser(question);
}
public int RaiseSelectionDialog(string message, params object[] args)
{
return AskUserForSelection(message, args);
}
public void RaiseError(string message, params object[] args)
{
Error(message, args);
}
public void RaiseDownloadsCompleted(Uri[] file_urls, string[] file_paths, Exception[] errors)
{
DownloadsComplete(file_urls, file_paths, errors);
}
}
}