This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WSLNotify.cs
179 lines (160 loc) · 5.36 KB
/
WSLNotify.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
/*
* Microsoft (R) Visual C# Compiler version 4.8.4084.0 for C# 5
* csc /platform:x64 /win32icon:WSLNotify.ico /o+ /w:4 /nologo WSLNotify.cs
*/
using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
///////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Emulates some features of <c>notify-send</c>, a Linux program which is used
/// to send desktop notifications.
/// </summary>
///////////////////////////////////////////////////////////////////////////////
class WSLNotify
{
const string helpOption = "-?";
const string BallonTipIconOption = "-i";
ToolTipIcon BalloonTipIcon;
string BalloonTipTitle = " ";
string BalloonTipText = " ";
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Set the contents of the notification balloon.
/// </summary>
///////////////////////////////////////////////////////////////////////////
void setBalloonTip(string text)
{
if(text == "")
{
return;
}
if(BalloonTipTitle == " ")
{
BalloonTipTitle = text;
}
else if(BalloonTipText == " ")
{
BalloonTipText = text;
}
else
{
Console.WriteLine("Invalid number of options");
Environment.Exit(1);
}
}
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Set an icon for the notification balloon.
/// </summary>
///////////////////////////////////////////////////////////////////////////
void setBalloonTipIcon(string text)
{
switch(text)
{
case "dialog-error":
BalloonTipIcon = ToolTipIcon.Error;
break;
case "dialog-warning":
BalloonTipIcon = ToolTipIcon.Warning;
break;
case "dialog-information":
BalloonTipIcon = ToolTipIcon.Info;
break;
default:
BalloonTipIcon = ToolTipIcon.None;
break;
}
}
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Display usage information.
/// <summary>
///////////////////////////////////////////////////////////////////////////
void showHelp()
{
Console.WriteLine("Usage:");
Console.WriteLine(" " + Assembly.GetExecutingAssembly().GetName().Name + " [OPTIONS] <SUMMARY> [BODY]\n");
Console.WriteLine("Help Options:");
Console.WriteLine(" -? Show help\n");
Console.WriteLine("Application Options:");
Console.WriteLine(" -i ICON Stock icon (\"dialog-information\", \"dialog-warning\", \"dialog-error\")\n");
Environment.Exit(0);
}
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Read the command line arguments into class member variables.
/// Apparently, there is no out-of-the-box support for parsing command line
/// arguments. But there aren't too many arguments to process, so a
// quick-and-dirty implementation shouldn't hurt.
/// </summary>
///////////////////////////////////////////////////////////////////////////
void parseArguments(string[] args)
{
for(int i = 0; i < args.Length; ++i)
{
if(!args[i].StartsWith("-"))
{
setBalloonTip(args[i]);
continue;
}
switch(args[i])
{
case BallonTipIconOption:
++i;
setBalloonTipIcon(args[i]);
break;
case helpOption:
showHelp();
break;
default:
Console.WriteLine("Unknown option " + args[i]);
Environment.Exit(1);
break;
}
}
}
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Check the parsed information for errors.
/// </summary>
///////////////////////////////////////////////////////////////////////////
void validateParsed()
{
if(BalloonTipTitle == " ")
{
Console.WriteLine("No summary specified");
Environment.Exit(1);
}
}
///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Main function.
/// <summary>
///////////////////////////////////////////////////////////////////////////
static void Main(string[] args)
{
WSLNotify wNotify = new WSLNotify();
try
{
wNotify.parseArguments(args);
}
catch(IndexOutOfRangeException)
{
Console.WriteLine("Missing argument");
Environment.Exit(1);
}
wNotify.validateParsed();
NotifyIcon notifyIcon = new NotifyIcon
{
Visible = true,
Icon = Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location),
BalloonTipTitle = wNotify.BalloonTipTitle,
BalloonTipText = wNotify.BalloonTipText,
BalloonTipIcon = wNotify.BalloonTipIcon,
Text = "WSLNotify"
};
notifyIcon.ShowBalloonTip(8000);
}
}