99
1010namespace ClipSync {
1111
12+ /// <summary>
13+ /// Mail Home Form of ClipSync
14+ /// </summary>
1215 public partial class ClipSyncControlForm : Form {
1316
17+ /// <summary>
18+ /// time interval for checking copying data
19+ /// </summary>
1420 public string mTime = DateTime . Now . ToLongTimeString ( ) ;
1521
22+ /// <summary>
23+ /// Global Helper Instance
24+ /// </summary>
1625 private readonly GlobalHelper globalHelper ;
1726
27+ /// <summary>
28+ /// Signal R Hub Proxy
29+ /// </summary>
1830 public IHubProxy _hub ;
1931
32+ /// <summary>
33+ /// signal R Disposable Hub object
34+ /// </summary>
2035 private IDisposable signalRDisposable { get ; set ; }
2136
2237 bool isSignalRConnected = false ;
2338
39+ /// <summary>
40+ /// User Id
41+ /// </summary>
2442 public string uid = "" ;
2543
44+ /// <summary>
45+ /// Default Constructor
46+ /// </summary>
2647 internal ClipSyncControlForm ( ) {
2748 InitializeComponent ( ) ;
2849 this . globalHelper = new GlobalHelper ( ) ;
2950 }
3051
31- private void LoginSignUpForm_Load ( object sender , EventArgs e ) {
52+ private void ClipSyncControlForm_Load ( object sender , EventArgs e ) {
3253 try {
3354
3455 this . LogWriter ( "-------------------" ) ;
@@ -42,13 +63,48 @@ private void LoginSignUpForm_Load(object sender, EventArgs e) {
4263 this . connectUidTextBox . Text = new Random ( ) . Next ( 1000 , 9999 ) . ToString ( ) ;
4364
4465
45- }
46- catch ( Exception ex ) {
66+ } catch ( Exception ex ) {
4767 this . LogWriter ( ex . ToString ( ) ) ;
4868 }
4969 }
5070
51- private void LoginButtonClick ( object sender , EventArgs e ) {
71+ /// <summary>
72+ /// Server Port text box key press event
73+ /// </summary>
74+ /// <param name="sender"></param>
75+ /// <param name="e"></param>
76+ private void serverPortTextBox_KeyPress ( object sender , KeyPressEventArgs e ) {
77+ if ( ! ( Char . IsDigit ( e . KeyChar ) || ( e . KeyChar == ( char ) Keys . Back ) ) )
78+ e . Handled = true ;
79+ }
80+
81+ /// <summary>
82+ /// Connect To Server Port Key Press Event
83+ /// </summary>
84+ /// <param name="sender"></param>
85+ /// <param name="e"></param>
86+ private void connectServerPortTextBox_KeyPress ( object sender , KeyPressEventArgs e ) {
87+ if ( ! ( Char . IsDigit ( e . KeyChar ) || ( e . KeyChar == ( char ) Keys . Back ) ) )
88+ e . Handled = true ;
89+ }
90+
91+ /// <summary>
92+ /// Connect Uid Key Press Event
93+ /// </summary>
94+ /// <param name="sender"></param>
95+ /// <param name="e"></param>
96+ private void connectUidTextBox_KeyPress ( object sender , KeyPressEventArgs e ) {
97+
98+ if ( ! ( Char . IsDigit ( e . KeyChar ) || ( e . KeyChar == ( char ) Keys . Back ) ) )
99+ e . Handled = true ;
100+ }
101+
102+ /// <summary>
103+ /// Login Button Click
104+ /// </summary>
105+ /// <param name="sender"></param>
106+ /// <param name="e"></param>
107+ private void LoginButton_Click ( object sender , EventArgs e ) {
52108
53109 Login_Button . Enabled = false ;
54110
@@ -68,17 +124,15 @@ private void LoginButtonClick(object sender, EventArgs e) {
68124 this . _hub = connection . CreateHubProxy ( ConfigurationManager . AppSettings [ "hub_name" ] ) ;
69125 connection . Start ( ) . Wait ( ) ;
70126 isSignalRConnected = true ;
71- }
72- catch ( Exception ex ) {
127+ } catch ( Exception ex ) {
73128 isSignalRConnected = false ;
74129 Login_Button . Enabled = true ;
75130 this . LogWriter ( "Exception in connecting to SignalR Hub : " + ex . ToString ( ) ) ;
76131 }
77132
78133 if ( ! isSignalRConnected ) {
79134 MessageBox . Show ( "ClipSync Server is not running, so Please close the whole app and try again after sometime." , "Warning" ) ;
80- }
81- else {
135+ } else {
82136
83137 MessageBox . Show ( "ClipSync Server is now connected, You need to connect to this uid: " + uid + " from all your devices. Now you can minimise this window" , "Success" ) ;
84138 this . LogWriter ( "ClipSync Server is now connected, You need to connect to this uid: " + uid + " from all your devices. Now you can minimise this window" ) ;
@@ -101,6 +155,11 @@ private void LoginButtonClick(object sender, EventArgs e) {
101155
102156 }
103157
158+ /// <summary>
159+ /// Start Server Button Click
160+ /// </summary>
161+ /// <param name="sender"></param>
162+ /// <param name="e"></param>
104163 private void StartServerButton_Click ( object sender , EventArgs e ) {
105164 this . startServerButton . Enabled = false ;
106165 this . LogWriter ( "Starting server on " ) ;
@@ -117,20 +176,55 @@ private void StartServerButton_Click(object sender, EventArgs e) {
117176 this . connectServerAddressTextBox . Text = globalHelper . GetMachineIpAddress ( ) ;
118177 this . connectServerPortTextBox . Text = this . serverPortTextBox . Text ;
119178 this . startServerButton . Enabled = false ;
120- }
121- catch ( System . Reflection . TargetInvocationException ex ) {
179+
180+ this . OpenPortButton . PerformClick ( ) ;
181+ } catch ( System . Reflection . TargetInvocationException ex ) {
122182 this . LogWriter ( "You need to run as administrator" ) ;
123183 this . LogWriter ( "Only server address localhost is allowed without administrator permissions" ) ;
124184 MessageBox . Show ( "You need to run as administrator" , "Error" ) ;
125185 Console . WriteLine ( ex . ToString ( ) ) ;
126186 this . startServerButton . Enabled = true ;
127- }
128- catch ( Exception ex ) {
187+ } catch ( Exception ex ) {
129188 this . LogWriter ( ex . ToString ( ) ) ;
130189 this . startServerButton . Enabled = true ;
131190 }
132191 }
133192
193+ /// <summary>
194+ /// Open Port Button Click
195+ /// </summary>
196+ /// <param name="sender"></param>
197+ /// <param name="e"></param>
198+ private void OpenPortButton_Click ( object sender , EventArgs e ) {
199+ try {
200+
201+ this . OpenPortButton . Enabled = false ;
202+ int serverPort = Convert . ToInt32 ( this . serverPortTextBox . Text ) ;
203+
204+ this . LogWriter ( "Checking the status of inbound port: " + serverPort + " Please wait..." ) ;
205+ if ( this . globalHelper . IsPortOpened ( serverPort , "ClipSync" ) ) {
206+ this . LogWriter ( "Port " + serverPort + " is already open" ) ;
207+ } else {
208+ this . LogWriter ( "Opening the inbound port: " + serverPort + " Please wait..." ) ;
209+ if ( this . globalHelper . OpenInboundFirewallPort ( serverPort , "ClipSync" , serverPort . ToString ( ) ) ) {
210+ this . LogWriter ( "Successfully opened the inbound port : " + serverPort ) ;
211+ } else {
212+ this . LogWriter ( "Failed to open the inbound port : " + serverPort ) ;
213+ this . LogWriter ( "Try running as administrator" ) ;
214+ MessageBox . Show ( "Try running as administrator" , "Error" ) ;
215+ }
216+ }
217+ this . OpenPortButton . Enabled = true ;
218+ } catch ( System . Reflection . TargetInvocationException ex ) {
219+ this . LogWriter ( "You need to run as administrator" ) ;
220+ MessageBox . Show ( "You need to run as administrator" , "Error" ) ;
221+ Console . WriteLine ( ex . ToString ( ) ) ;
222+ } catch ( Exception ex ) {
223+ this . OpenPortButton . Enabled = true ;
224+ this . LogWriter ( ex . ToString ( ) ) ;
225+ }
226+ }
227+
134228 /// <summary>
135229 /// Log Writer on Form
136230 /// </summary>
@@ -143,26 +237,24 @@ internal void LogWriter(string t) {
143237 ) ) ;
144238 return ;
145239 }
146- consoleTextBox . AppendText ( Environment . NewLine + t ) ;
240+ consoleTextBox . AppendText ( Environment . NewLine + Environment . NewLine + t ) ;
147241 consoleTextBox . SelectionStart = consoleTextBox . Text . Length ;
148242 consoleTextBox . ScrollToCaret ( ) ;
149243
150244 }
151245
246+ /// <summary>
247+ /// Adds ClipBoard Listener to the Window
248+ /// </summary>
152249 private void AddClipBoardListener ( ) {
153250 //NativeMethods.SetParent(Handle, NativeMethods.HWND_MESSAGE);
154251 NativeMethods . AddClipboardFormatListener ( Handle ) ;
155252 }
156253
157- private void websocket_MessageReceived ( object sender , EventArgs e ) {
158- this . LogWriter ( "websocket_MessageReceived" ) ;
159- }
160-
161- private void websocket_Closed ( object sender , EventArgs e ) {
162- this . LogWriter ( "websocket_Closed" ) ;
163- }
164-
165-
254+ /// <summary>
255+ /// WindProc for getting ClipBoard Data
256+ /// </summary>
257+ /// <param name="m"></param>
166258 protected override void WndProc ( ref Message m ) {
167259 if ( m . Msg == NativeMethods . WM_CLIPBOARDUPDATE ) {
168260
@@ -179,8 +271,7 @@ protected override void WndProc(ref Message m) {
179271 _hub . Invoke ( ConfigurationManager . AppSettings [ "send_copied_text_signalr_method_name" ] , copied_content ) ;
180272 }
181273 }
182- }
183- else if ( iData . GetDataPresent ( DataFormats . Bitmap ) ) {
274+ } else if ( iData . GetDataPresent ( DataFormats . Bitmap ) ) {
184275 //Bitmap image = (Bitmap)iData.GetData(DataFormats.Bitmap); // Clipboard image
185276 //do something with it
186277 }
@@ -189,6 +280,7 @@ protected override void WndProc(ref Message m) {
189280 base . WndProc ( ref m ) ;
190281 }
191282
283+
192284 }
193285
194286 internal static class NativeMethods {
0 commit comments