-
Notifications
You must be signed in to change notification settings - Fork 0
/
asistonto.cs
241 lines (225 loc) · 8.2 KB
/
asistonto.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
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Resources;
using System.Reflection;
using System.Diagnostics;
using Microsoft.Win32;
[assembly: AssemblyTitle("asistonto lite")]
[assembly: AssemblyDescription("miguel.simoni@gmail.com")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Miguel.Simoni")]
[assembly: AssemblyProduct("asistonto lite")]
[assembly: AssemblyCopyright("Copyright © Miguel.Simoni 2008")]
[assembly: AssemblyVersion("10.5.27.1530")]
[assembly: AssemblyFileVersion("2.1.0.0")]
namespace asistonto_lite
{
public class mainForm : Form
{
private NotifyIcon notifyIcon = new NotifyIcon();
private ContextMenu contextMenu = new ContextMenu();
private string dataFile;
public mainForm()
{
notifyIcon.Icon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("app.ico"));
notifyIcon.Visible = true;
notifyIcon.Text = Application.ProductName + " " + Application.ProductVersion;
notifyIcon.ContextMenu = contextMenu;
notifyIcon.Click += new EventHandler(notifyIcon_Click);
notifyIcon.DoubleClick += new EventHandler(notifyIcon_DoubleClick);
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
this.Load += new EventHandler(mainForm_Activated);
this.Activated += new EventHandler(mainForm_Activated);
dataFile = Application.StartupPath + @"\data.txt";
if(!File.Exists(dataFile))
File.CreateText(dataFile).Close();
}
[STAThread]
static void Main(string[] args)
{
if(args.Length > 0)
{
string arg = args[0].ToLower().Trim().Substring(0, 2);
switch(arg)
{
case "/s":
RunOnStartup(true);
break;
case "/n":
RunOnStartup(false);
break;
default:
break;
}
}
string moduleName = Process.GetCurrentProcess().MainModule.ModuleName;
string processName = Path.GetFileNameWithoutExtension(moduleName);
Process[] appProcess = Process.GetProcessesByName(processName);
if(appProcess.Length > 1)
{
MessageBox.Show(Application.ProductName + " ya se está ejecutando.", "oops!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
Application.EnableVisualStyles();
Application.Run(new mainForm());
}
}
private void mainForm_Activated(object sender, System.EventArgs e)
{
this.Hide();
}
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
Process.Start("notepad", dataFile);
}
public void loadData()
{
MenuItem menuItem = null;
contextMenu.MenuItems.Clear();
if(File.Exists(dataFile))
{
StreamReader input = new StreamReader(dataFile, Encoding.Default);
string item;
while((item = input.ReadLine()) != null)
{
menuItem = null;
if(item.IndexOf("|") > 0)
{
string submenu = item.Split('|')[0];
foreach(MenuItem mit in contextMenu.MenuItems)
{
if(mit.Text == submenu)
{
menuItem = mit;
}
}
if(menuItem == null)
{
menuItem = new MenuItem(submenu);
}
item = item.Substring(item.IndexOf("|") + 1);
menuItem.MenuItems.Add(new MenuItem(item, new EventHandler(mitGeneric_Click)));
}
else
{
menuItem = new MenuItem(item, new EventHandler(mitGeneric_Click));
}
if(!contextMenu.MenuItems.Contains(menuItem))
contextMenu.MenuItems.Add(menuItem);
}
input.Close();
}
contextMenu.MenuItems.Add("-");
menuItem = new MenuItem("[ editar... ]", new EventHandler(notifyIcon_DoubleClick));
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("[ web... ]", new EventHandler(mitSite_Click));
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("[ salir ]", new EventHandler(mitExit_Click));
contextMenu.MenuItems.Add(menuItem);
}
private void notifyIcon_Click(object sender, EventArgs e)
{
loadData();
}
private void mitSite_Click(object sender, EventArgs e)
{
Process.Start("http://sites.google.com/site/asistonto");
}
private void mitExit_Click(object sender, EventArgs e)
{
notifyIcon.Dispose();
Application.Exit();
}
private void mitGeneric_Click(object sender, EventArgs e)
{
string value = ((MenuItem)sender).Text;
if(value.IndexOf("\t") > -1)
value = value.Split('\t')[1];
doAction(value);
}
private static void doAction(string value)
{
string patternUrl = @"^(((https?|ftp)\://)?((\[?(\d{1,3}\.){3}\d{1,3}\]?)|(([-a-zA-Z0-9]+\.)+[a-zA-Z]{2,4}))(\:\d+)?(/[-a-zA-Z0-9._?,'+&%$#=~\\]+)*/?)$";
string patternMailto = @"^mailto\:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
string patternLocalUrl = @"^\\\\\w+$";
string patternPath = @"^[a-zA-Z]:[\\\w*(\.\w+)+]*$";
if(Regex.Match(value, patternUrl).Success)
{
try
{
Process.Start(value);
}
catch(Exception ex)
{
MessageBox.Show("Dirección inválida\n" + ex.Message, "oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
if(Regex.Match(value, patternMailto).Success)
{
try
{
Process.Start(value);
}
catch(Exception ex)
{
MessageBox.Show("E-mail inválido\n" + ex.Message, "oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
if(Regex.Match(value, patternLocalUrl).Success)
{
try
{
Process.Start(value);
}
catch(Exception ex)
{
MessageBox.Show("Dirección de red inválida\n" + ex.Message, "oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
if(Regex.Match(value, patternPath).Success)
{
try
{
Process.Start(value);
}
catch(Exception ex)
{
MessageBox.Show("Dirección local inválida\n" + ex.Message, "oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
Clipboard.SetDataObject(value, true);
}
}
private static void RunOnStartup(bool run)
{
RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
if(regKey != null)
{
try
{
if(run)
regKey.SetValue("asistonto_lite", Assembly.GetExecutingAssembly().Location);
else
regKey.DeleteValue("asistonto_lite");
}
catch(Exception ex)
{
MessageBox.Show("No se pudo configurar la aplicación para correr al inicio.\n\n" + ex.Message);
}
finally
{
regKey.Close();
}
}
}
}
}