-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainScreen.cs
357 lines (308 loc) · 11.4 KB
/
MainScreen.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
using GoogleVoice;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace GVDialer
{
public partial class MainScreen : Form
{
Account account;
public MainScreen()
{
InitializeComponent();
mPictureBox.Size = mPictureBox.Image.Size;
var cue = "Enter number";
SendMessage(mCallBox.Control.Handle, EM_SETCUEBANNER, 0, cue);
SendMessage(mSmsBox.Control.Handle, EM_SETCUEBANNER, 0, cue);
this.account = null;
}
void ReportException(Exception exception, string format, params object[] parameters)
{
var message = string.Format(format, parameters);
var dialog = new ErrorDialog(message, exception);
dialog.ShowDialog();
}
private void Login()
{
var email = Properties.Settings.Default.Email;
var password = Properties.Settings.Default.Password;
if (email == string.Empty || password == string.Empty)
{
return;
}
this.account = new Account(email, password);
try
{
this.account.Login();
PopulateRecentCalls();
PopulateRecentSMS();
PopulateContacts();
mCallMenuItem.Enabled = true;
mSmsMenuItem.Enabled = true;
mInboxMenuItem.Enabled = true;
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainScreen));
mTrayIcon.Icon = global::GVDialer.Properties.Resources.ActiveIcon;
}
catch (Exception ex)
{
ReportException(ex, "There was an error logging into your Google voice account." +
" Please make sure that you have entered the correct email" +
" address and password.");
this.account = null;
}
}
private void PopulateRecentCalls()
{
try
{
var placedCalls = this.account.GetPlacedCalls().Root.Element("messages").Elements();
foreach (var call in placedCalls)
{
AddRecent((string)call.Element("displayNumber"), (string)call.Element("phoneNumber"), mCallRecentMenuItem);
}
}
catch (Exception ex)
{
ReportException(ex, "There was an error loading recent call list.");
}
}
private void PopulateRecentSMS()
{
try
{
var sentSMSs = this.account.GetSMSMessages().Root.Element("messages").Elements();
foreach (var sms in sentSMSs)
{
AddRecent((string)sms.Element("displayNumber"), (string)sms.Element("phoneNumber"), mSmsRecentMenuItem);
}
}
catch (Exception ex)
{
ReportException(ex, "There was an error loading recent SMS list.");
}
}
private void PopulateContacts()
{
try
{
var data = this.account.GetContacts();
var contacts = from contact in data.Root.Element("messages").Elements()
select contact.Elements().First();
foreach (var contact in contacts)
{
AddContact((string)contact.Element("displayNumber"), (string)contact.Element("phoneNumber"));
}
}
catch (Exception ex)
{
ReportException(ex, "There was an error loading contact list.");
}
}
private void Logout()
{
mCallMenuItem.Enabled = false;
mSmsMenuItem.Enabled = false;
mInboxMenuItem.Enabled = false;
mCallRecentMenuItem.DropDownItems.Clear();
mSmsRecentMenuItem.DropDownItems.Clear();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainScreen));
mTrayIcon.Icon = global::GVDialer.Properties.Resources.InactiveIcon;
if (this.account != null)
{
this.account.Logout();
}
}
private void Dial(string number)
{
try
{
this.account.Dial(number, Properties.Settings.Default.CallbackNumber);
var formatted = FormatNumber(number);
AddRecent(formatted, formatted, mCallRecentMenuItem);
}
catch (Exception ex)
{
ReportException(ex, "There was an error placing your call." +
" This may be caused by an incorrect callback number." +
" Make sure that your callback number matches one of the" +
" phones registered to your Google Voice account.");
}
}
private void SMS(string number)
{
var formatted = FormatNumber(number);
var form = new SMSDialog(formatted);
if (form.ShowDialog() == DialogResult.OK)
{
try
{
this.account.SMS(number, form.Message);
AddRecent(formatted, formatted, mCallRecentMenuItem);
}
catch (Exception ex)
{
ReportException(ex, "There was an error sending the SMS message.");
}
}
}
private void AddRecent(string caption, string number, ToolStripMenuItem parent)
{
foreach (ToolStripMenuItem item in parent.DropDownItems)
{
if (item.Text == caption)
{
// Move to end of list
parent.DropDownItems.Remove(item);
parent.DropDownItems.Add(item);
return;
}
}
var recent = CreateEntry(caption, number);
parent.DropDownItems.Add(recent);
parent.Enabled = true;
if (parent.DropDownItems.Count > Properties.Settings.Default.RecentCount)
{
parent.DropDownItems.RemoveAt(0);
}
}
private void AddContact(string caption, string number)
{
var call = CreateEntry(caption, number);
mCallContactsMenuItem.DropDownItems.Add(call);
mCallContactsMenuItem.Enabled = true;
mCallContactsMenuItem.Visible = true;
var sms = CreateEntry(caption, number);
mSmsContactsMenuItem.DropDownItems.Add(sms);
mSmsContactsMenuItem.Enabled = true;
mSmsContactsMenuItem.Visible = true;
}
private ToolStripMenuItem CreateEntry(string caption, string number)
{
var entry = new ToolStripMenuItem(caption);
entry.Tag = FormatNumber(number);
entry.Click += new EventHandler(HandleEntryClick);
return entry;
}
private static string FormatNumber(string number)
{
var result = new StringBuilder();
foreach (var c in number)
{
if (char.IsDigit(c))
{
result.Append(c);
}
}
var numeric = result.ToString();
var offset = -1;
if (numeric.Length == 10)
{
offset = 0;
}
else if (numeric.Length == 11 && numeric[0] == '1')
{
offset = 1;
}
else
{
return number;
}
var area = numeric.Substring(offset, 3);
var branch = numeric.Substring(offset + 3, 3);
var final = numeric.Substring(offset + 3 + 3, 4);
return string.Format("({0}) {1}-{2}", area, branch, final);
}
private void HandleShown(object sender, EventArgs e)
{
Login();
}
private void HandleSettingsClick(object sender, EventArgs e)
{
var form = new SettingsDialog();
if (form.ShowDialog() == DialogResult.OK)
{
Logout();
Login();
}
}
private void HandleExitClick(object sender, EventArgs e)
{
Logout();
Close();
}
private void HandleEntryClick(object sender, EventArgs e)
{
var item = (ToolStripMenuItem)sender;
string number = (string)item.Tag;
if (item.OwnerItem.OwnerItem == mCallMenuItem)
{
Dial(number);
}
else
{
SMS(number);
}
}
private void HandleBoxKeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
var box = sender as ToolStripTextBox;
string number = box.Text;
if (box.OwnerItem == mCallMenuItem)
{
Dial(number);
}
else
{
SMS(number);
}
mContextMenu.Hide();
}
}
private void HandleInboxClick(object sender, EventArgs e)
{
string url;
byte[] data;
string headers;
this.account.GetInboxUrl(out url, out data, out headers);
var form = new WebDialog(url, data, headers);
form.Show();
}
private void HandleTimerTick(object sender, EventArgs e)
{
mTimer.Enabled = false;
Hide();
if (string.IsNullOrEmpty(Properties.Settings.Default.Email) ||
string.IsNullOrEmpty(Properties.Settings.Default.Password))
{
var form = new SettingsDialog();
if (form.ShowDialog() == DialogResult.OK)
{
Logout();
Login();
}
}
}
private void HandleCallOpening(object sender, EventArgs e)
{
mCallBox.Text = string.Empty;
}
private void HandleSmsOpening(object sender, EventArgs e)
{
mSmsBox.Text = string.Empty;
}
private void HandleAboutClick(object sender, EventArgs e)
{
var form = new AboutDialog();
form.ShowDialog();
}
private const int EM_SETCUEBANNER = 0x1501;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);
}
}