-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cs
194 lines (163 loc) · 6.94 KB
/
MainWindow.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security;
using System.Configuration;
using System.Threading;
using Sharpnote;
namespace Simplisticky {
public partial class MainWindow : Form {
private ApplicationController app;
private SecureCredential cred;
private SecureString secureEmail, securePassword;
private String encryptedEmail, encryptedPassword;
private Thread syncThread;
delegate void SetTextCallback(bool result, Exception error);
public MainWindow(int tab, bool show, ApplicationController _app) {
InitializeComponent();
app = _app;
this.Location = Properties.Settings.Default.mainLocation;
if (!show)
this.Hide();
else {
this.MainWindowTabControl.SelectedTab = this.MainWindowTabControl.TabPages[tab];
this.Show();
}
cred = new SecureCredential();
}
#region buttonClickEvents
private void syncButton_Click(object sender, EventArgs e) {
if (emailField.Text == "" || passwordField.Text == "") {
syncStatusMessage.ForeColor = Color.Red;
syncStatusMessage.Text = "Invalid email or password";
syncStatusMessage.Show();
}
else {
syncThread = new Thread(new ThreadStart(this.ThreadProcSafe));
syncThread.Name = "MainWindow Sync Thread";
syncThread.Start();
}
}
private void closeToTaskbar_Click(object sender, EventArgs e) {
Properties.Settings.Default.showmain = false;
Properties.Settings.Default.Save();
this.Hide();
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) {
this.Show();
this.Activate();
foreach (StickyNote n in app.Notelist) {
n.Activate();
}
Properties.Settings.Default.showmain = true;
Properties.Settings.Default.Save();
}
private void sysTrayQuit_Click(object sender, EventArgs e) {
app.Xml.Write(app.Notelist);
notifyIcon.Visible = false;
Application.Exit();
}
private void quitButton_Click(object sender, EventArgs e) {
app.Xml.Write(app.Notelist);
notifyIcon.Visible = false;
Application.Exit();
}
#endregion
// Simplenote API Call Methods
private void ThreadProcSafe() {
try {
Sharpnote.Sharpnote.Instance.Login(emailField.Text, passwordField.Text);
var simplenotes = Sharpnote.Sharpnote.Instance.SearchNotes<Note>();
this.processLoginResults(true, null);
}
catch (Exception error) {
this.processLoginResults(false, error);
}
}
// This delegate processes the results from the sync thread on the main thread
private void processLoginResults(bool result, Exception error) {
if (this.syncStatusMessage.InvokeRequired) {
SetTextCallback d = new SetTextCallback(processLoginResults);
this.Invoke(d, new object[] { result,error });
}
else {
if (result) {
syncStatusMessage.ForeColor = Color.LimeGreen;
syncStatusMessage.Text = "Sync Successful";
syncStatusMessage.Show();
secureEmail = cred.ToSecureString(emailField.Text);
securePassword = cred.ToSecureString(passwordField.Text);
encryptedEmail = cred.EncryptString(secureEmail);
encryptedPassword = cred.EncryptString(securePassword);
Properties.Settings.Default.email = encryptedEmail;
Properties.Settings.Default.password = encryptedPassword;
Properties.Settings.Default.firstrun = false;
Properties.Settings.Default.Save();
}
else {
System.Console.WriteLine(error.Message);
// Find a more elegant solution to check for error type
if (error.Message == "The remote server returned an error: (400) Bad Request.") {
syncStatusMessage.ForeColor = Color.Red;
syncStatusMessage.Text = "Invalid email or password";
}
if (error.Message == "Unable to connect to the remote server" || error.Message == "The remote name could not be resolved: 'simple-note.appspot.com'") {
syncStatusMessage.ForeColor = Color.Red;
syncStatusMessage.Text = "Unable to connect.\nCheck your internet connection";
}
syncStatusMessage.Show();
}
}
}
// Clean up everything below this line
private void sysTrayShowNotelist_Click(object sender, EventArgs e) {
this.MainWindowTabControl.SelectedTab = this.MainWindowTabControl.TabPages[0];
this.Show();
Properties.Settings.Default.showmain = true;
Properties.Settings.Default.Save();
}
private void MainWindowMoved(object sender, EventArgs e) {
Properties.Settings.Default.mainLocation = this.Location;
Properties.Settings.Default.Save();
}
protected override void OnFormClosing(FormClosingEventArgs e) {
FormClosing(this,e);
}
private void FormClosing(object sender,FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing && ((!sender.Equals(sysTrayQuit) && !sender.Equals(quitButton)) || sender.Equals(this))) {
e.Cancel = true;
this.Hide();
}
else {
app.Xml.Write(app.Notelist);
Application.Exit();
}
}
private void newNoteButton_Click_1(object sender, EventArgs e) {
StickyNote newNote;
try {
if (app.Notelist.Count > 0) {
newNote = app.Notelist[app.Notelist.Count - 1];
if (newNote != null) {
newNote.spawnNewNote();
}
}
else {
newNote = new StickyNote(app, null);
app.Notelist.Add(newNote);
newNote.Location = new Point(190, 210);
newNote.Show();
}
}
catch (Exception errorCreatingNewNote) {
System.Console.WriteLine(errorCreatingNewNote.Message);
}
}
}
}