-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDlgChat.pas
991 lines (716 loc) · 27.2 KB
/
DlgChat.pas
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
{===============================================================================
Program ID : DlgChat.pas
Program Name : 채팅(Chatting) 클라이언트
Program Desc. : 다이얼로그 담당자정보에 IP가 저장된 상호 User간 1:1 TCP 기반
통신을 지원하는 Client.
Author : Lee, Se-Ha
Date : 2013.08.29
===============================================================================}
unit DlgChat;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, ScktComp, ComCtrls, StdCtrls, ExtCtrls, Grids, BaseGrid, AdvGrid,
ExtDlgs, Variants, AdvObj;
const
// Chat Send Columns
C_SD_TEXT = 0;
C_SD_BUTTON = 1;
// Chat 박스 Columns
C_CH_MYCOL = 0;
C_CH_TEXT = 1;
C_CH_YOURCOL = 2;
C_CH_HIDDEN = 3;
type
TClient = class(TForm)
Bevel1: TBevel;
Bevel2: TBevel;
MemoChat: TMemo;
ButtonConnect: TButton;
StatusBar: TStatusBar;
ButtonChangeNick: TButton;
ClientSocket: TClientSocket;
EditNick: TEdit;
EditIp: TEdit;
EditPort: TEdit;
asg_Chat: TAdvStringGrid;
EditSay: TEdit;
ButtonSend: TButton;
asg_ChatSend: TAdvStringGrid;
pm_Chat: TPopupMenu;
mi_Emoti: TMenuItem;
OpenPictureDialog1: TOpenPictureDialog;
Image1: TImage;
mi_FileSend: TMenuItem;
od_File: TOpenDialog;
procedure ButtonSendClick(Sender: TObject);
procedure ConnectDisconnect1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);
procedure ButtonConnectClick(Sender: TObject);
procedure EditSayKeyPress(Sender: TObject; var Key: Char);
procedure FormDestroy(Sender: TObject);
procedure asg_ChatSendButtonClick(Sender: TObject; ACol,
ARow: Integer);
procedure asg_ChatSendKeyPress(Sender: TObject; var Key: Char);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure mi_EmotiClick(Sender: TObject);
procedure ClientSocketError(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure mi_FileSendClick(Sender: TObject);
procedure asg_ChatButtonClick(Sender: TObject; ACol, ARow: Integer);
private
{ Private declarations }
Reciving : Boolean;
DataSize : integer;
Data : TMemoryStream;
iChatRowCnt : Integer;
IsEmotiSend : Boolean;
IsFileSend : Boolean;
//---------------------------------------------------------------------------
// FTP 업로드
// - 소스출처 : MDN191F2.pas
//---------------------------------------------------------------------------
function FileUpLoad(TargetFile, DelFile: String; var S_IP: String): Boolean;
public
{ Public declarations }
end;
var
Client: TClient;
implementation
uses
CComFunc,
VarCom,
TuxCom,
MainDialog,
ShellApi, // ShellExec 사용관련 추가 @ 2013.08.22 LSH
WinSock;
{$R *.DFM}
procedure TClient.ButtonSendClick(Sender: TObject);
begin
ClientSocket.Socket.SendText(EditSay.Text + '&[' + EditNick.Text + ']');
asg_Chat.InsertRows(iChatRowCnt, 1);
//asg_Chat.MergeCells(C_CH_TEXT, iChatRowCnt, 2, 1);
asg_Chat.Cells[C_CH_MYCOL, iChatRowCnt] := '[' + EditNick.Text + '] ';
asg_Chat.Cells[C_CH_TEXT, iChatRowCnt] := EditSay.Text;
asg_Chat.Row := iChatRowCnt;
asg_Chat.Alignments[C_CH_TEXT, iChatRowCnt] := taLeftJustify;
asg_Chat.AutoSizeRow(iChatRowCnt);
Inc(iChatRowCnt);
EditSay.Text := '';
end;
procedure TClient.ConnectDisconnect1Click(Sender: TObject);
begin
ClientSocket.Active := not ClientSocket.Active;
end;
procedure TClient.FormShow(Sender: TObject);
begin
//---------------------------------------------------------
// [Chat Box] Frame Set.
//---------------------------------------------------------
with asg_Chat do
begin
ColWidths[C_CH_MYCOL] := 70;
ColWidths[C_CH_TEXT] := 220;
ColWidths[C_CH_YOURCOL] := 75;
// ColWidths[C_CH_HIDDEN] := 0;
end;
//---------------------------------------------------------
// [Chat 전송] Frame Set.
//---------------------------------------------------------
with asg_ChatSend do
begin
ColWidths[C_SD_TEXT] := 318;
// ColWidths[C_SD_BUTTON := 227;
AddButton(C_SD_BUTTON, 0, ColWidths[C_SD_BUTTON]-5, 20, '전송', haBeforeText, vaCenter); // 전송 Button
end;
// 파일 및 이모티콘 전송 모드 Off
IsEmotiSend := False;
IsFileSend := False;
end;
procedure TClient.ClientSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Self.Caption := '챗(Chat) :: ' + ClientSocket.Socket.RemoteHost + ' 대화중';
StatusBar.SimpleText := 'Status: Connected with ' + ClientSocket.Socket.RemoteHost;
end;
procedure TClient.ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
try
Self.Caption := '챗(Chat) :: ' + ClientSocket.Socket.RemoteHost + ' 대화종료';
StatusBar.SimpleText := 'Status: just Disconnected.';
ClientSocket.Close;
except
on E : Exception do
ShowMessage(E.ClassName+' error raised by ClientSocket.active off, with message : ' + E.Message);
end;
{ ------ 험난한 Debugging 끝에 일단, 오픈은 가능한 상태 !!!
try
if (ClientSocket.Active) then
begin
try
ClientSocket.Active := False;
except
on E : Exception do
ShowMessage(E.ClassName+' error raised by ClientSocket.active off, with message : ' + E.Message);
end;
end;
except
on E : Exception do
ShowMessage(E.ClassName+' error raised by ClientSocket, with message : ' + E.Message);
end;
}
end;
procedure TClient.ClientSocketRead(Sender: TObject;
Socket: TCustomWinSocket);
var
sl, sReceived, tmpOrgReceived : string;
sRead : string;
Buffer: Array[0..10000] of byte;
iReceived: Integer;
hThreadID: Cardinal;
FocusWnd : THandle;
begin
//-----------------------------------------------------------------
// Window API : 창을 비활성화 --> 활성화 @ 2014.02.28 LSH
// - 델마당 참조
//-----------------------------------------------------------------
ShowWindow(Self.Handle, SW_RESTORE);
{ -- Chat 클라이언트는 윈도우가 비활성화 (화면 좌측하단)되기때문에
SetForeGroundWindow가 먹질 않아서.. 그냥 매번 ShowWindow를 통해
Restore 하도록 처리함.
if IsWindowEnabled(Self.Handle) then
begin
hThreadID:= GetWindowThreadProcessId((GetForegroundWindow), nil);
AttachThreadInput(GetCurrentThreadID, hThreadID, True);
SetForegroundWindow(Self.Handle);
AttachThreadInput(GetCurrentThreadID, hThreadID, False);
end
else
begin
Application.Restore;
end;
}
sRead := '';
sRead := socket.ReceiveText;
Inc(iChatRowCnt);
asg_Chat.RowCount := iChatRowCnt; // Grid Index out of Range 방지위해 반드시 필요 !
if (PosByte('just connected', sRead) > 0) or
(PosByte('just disconnected', sRead) > 0) or
(PosByte('[대화중]', sRead) > 0) then
begin
asg_Chat.InsertRows(iChatRowCnt, 1);
asg_Chat.Cells[C_CH_TEXT, iChatRowCnt] := sRead;
asg_Chat.Row := iChatRowCnt;
asg_Chat.Alignments[C_CH_TEXT, iChatRowCnt] := taCenter;
asg_Chat.FontColors[C_CH_TEXT, iChatRowCnt] := $009589CC;
asg_Chat.FontStyles[C_CH_TEXT, iChatRowCnt] := [fsBold];
asg_Chat.AutoSizeRow(iChatRowCnt);
end
//-----------------------------------------------------------
// 이모티콘(BMP 이미지) 수신모드
//-----------------------------------------------------------
else if (PosByte('[E]', sRead) > 0) then
begin
// 원본 수신내역 Temp 파일에 저장 (Client User 이름 추출위함)
tmpOrgReceived := sRead;
DataSize := 0;
sl := '';
if not Reciving then
begin
// Now we need to get the LengthByte of the data stream.
SetLength(sl, StrLen(PChar(sRead)) +1); // +1 for the null terminator
StrLCopy(@sl[1], PChar(sRead), LengthByte(sl)- 13); // 이름태그(8) + &(1) + [E] (3) + 기존 Null (1)
DataSize := StrToInt(sl);
Data := TMemoryStream.Create;
Data.Size := 0;
// DeleteByte the size information from the data.
DeleteByte(sRead, 1, LengthByte(sl));
Reciving:= True;
end;
// Store the data to the file, until we've received all the data.
try
Data.Write(sRead[1], LengthByte(sRead));
if Data.Size = DataSize then
begin
Data.Position:= 0;
try
// 1줄 추가
asg_Chat.InsertRows(iChatRowCnt, 1);
except
on e : Exception do
showmessage('Client Read ERROR : ' + e.Message);
end;
try
// 수신한 Data-Stream을 Grid에 표기
asg_Chat.CreatePicture(C_CH_TEXT, iChatRowCnt, True, ShrinkWithAspectRatio, 0, haRight, vaTop).Bitmap.LoadFromStream(Data);
except
asg_Chat.Cells[C_CH_TEXT, iChatRowCnt] := '[이미지 수신오류]';
end;
// Data 송신자 표기
asg_Chat.Cells[C_CH_YOURCOL, iChatRowCnt] := CopyByte(tmpOrgReceived, PosByte('&', tmpOrgReceived) + 1, 8);
// Scroll 조절
asg_Chat.Row := iChatRowCnt;
// 송신자 컬럼 Align
asg_Chat.Alignments[C_CH_YOURCOL, iChatRowCnt] := taLeftJustify;
// 해당 Row Auto-Sizing
asg_Chat.AutoSizeRow(iChatRowCnt);
// 메모리 해제
Data.Free;
Reciving:= False;
end;
except
on e : Exception do
begin
showmessage('Client Read Data.Write ERROR : ' + e.Message);
Data.Free;
end;
end;
end
//-----------------------------------------------------------
// 파일 수신모드
//-----------------------------------------------------------
else if (PosByte('[A]', sRead) > 0) then
begin
asg_Chat.InsertRows(iChatRowCnt, 1);
asg_Chat.AddButton(C_CH_MYCOL,iChatRowCnt, asg_Chat.ColWidths[C_CH_MYCOL]-20, 20, '다운', haBeforeText, vaCenter);
asg_Chat.Cells[C_CH_TEXT, iChatRowCnt] := CopyByte(sRead, 1, PosByte('$', sRead) - 1); //CopyByte(sReceived, PosByte('$', sReceived) + 1, PosByte('[', sReceived) - PosByte('$', sReceived) -1); //
asg_Chat.Cells[C_CH_YOURCOL, iChatRowCnt] := CopyByte(sRead, PosByte('&', sRead) + 1, 8);
asg_Chat.Cells[C_CH_HIDDEN, iChatRowCnt] := CopyByte(sRead, PosByte('$', sRead) + 1, PosByte('[', sRead) - PosByte('$', sRead) -1);
asg_Chat.Row := iChatRowCnt;
asg_Chat.Alignments[C_CH_YOURCOL, iChatRowCnt] := taLeftJustify;
if LengthByte(asg_Chat.Cells[C_CH_TEXT, iChatRowCnt]) < 36 then
asg_Chat.Alignments[C_CH_TEXT, iChatRowCnt] := taRightJustify
else
asg_Chat.Alignments[C_CH_TEXT, iChatRowCnt] := taLeftJustify;
//asg_Chat.AutoSizeRow(iChatRowCnt);
end
//-----------------------------------------------------------
// 일반 Text 수신모드
//-----------------------------------------------------------
else
begin
asg_Chat.InsertRows(iChatRowCnt, 1);
//asg_Chat.MergeCells(C_CH_MYCOL, iChatRowCnt, 2, 1);
asg_Chat.Cells[C_CH_YOURCOL, iChatRowCnt] := CopyByte(sRead, PosByte('&', sRead) + 1, 10);
asg_Chat.Cells[C_CH_TEXT, iChatRowCnt] := CopyByte(sRead, 1, PosByte('&', sRead)-1);
asg_Chat.Row := iChatRowCnt;
asg_Chat.Alignments[C_CH_YOURCOL, iChatRowCnt] := taLeftJustify;
if LengthByte(asg_Chat.Cells[C_CH_TEXT, iChatRowCnt]) < 36 then
asg_Chat.Alignments[C_CH_TEXT, iChatRowCnt] := taRightJustify
else
asg_Chat.Alignments[C_CH_TEXT, iChatRowCnt] := taLeftJustify;
asg_Chat.AutoSizeRow(iChatRowCnt);
end;
end;
procedure TClient.ButtonConnectClick(Sender: TObject);
begin
ClientSocket.Host := EditIp.Text;
ClientSocket.Port := StrToInt(MainDlg.DelChar(CopyByte(MainDlg.asg_Master.Cells[C_M_USERIP, MainDlg.asg_Master.Row], LengthByte(MainDlg.asg_Master.Cells[C_M_USERIP, MainDlg.asg_Master.Row])-3, 4), '.'));
ClientSocket.Active := True;
end;
procedure TClient.EditSayKeyPress(Sender: TObject; var Key: Char);
begin
if (Key = #13) and (EditSay.Text <> '') then
ButtonSendClick(Sender);
end;
procedure TClient.FormDestroy(Sender: TObject);
begin
Client := Nil;
end;
//------------------------------------------------------------------------------
// [Chat 전송] AdvStringGrid onButtonClick Event Handler
//
// Author : Lee, Se-Ha
// Date : 2013.09.09
//------------------------------------------------------------------------------
procedure TClient.asg_ChatSendButtonClick(Sender: TObject; ACol,
ARow: Integer);
var
ms : TMemoryStream;
sFileName, sHideFile, sServerIp : String;
begin
if (ACol = C_SD_BUTTON) then
begin
Inc(iChatRowCnt);
asg_Chat.RowCount := iChatRowCnt; // Grid Index out of Range 방지위해 반드시 필요 !
//----------------------------------------------------------
// 이모티콘(Bitmap) 전송 모드
//----------------------------------------------------------
if (IsEmotiSend) then
begin
ms := TMemoryStream.Create;
try
// 현재 불러온 이미지를 Memory-Stream으로 저장
asg_ChatSend.GetPicture(C_SD_TEXT, 0).Bitmap.SaveToStream(ms);
if ms.Size > 3000 then
begin
//
MessageBox(self.Handle,
PChar('이모티콘용 Bitmap 이미지는 3KB 이하만 가능합니다.'),
'챗(Chat) :: 이모티콘(Bitmap) 전송 Size 제한',
MB_OK + MB_ICONERROR);
// 메모리 해제
ms.Free;
// 입력창 이미지 제거
asg_ChatSend.RemovePicture(C_SD_TEXT, 0);
// 상태 Chagne
IsEmotiSend := False;
Exit;
end;
ms.Position := 0;
// Data Stream 사이즈 정보 및 구분 Flag + 전송자 이름
ClientSocket.Socket.SendText(IntToStr(ms.Size) + '[E]' + '&[' + EditNick.Text + ']' + #0);
// 서버로 Data-Stream 전송
ClientSocket.Socket.SendStream(ms);
try
// 1줄 추가
asg_Chat.InsertRows(iChatRowCnt, 1);
except
on e : Exception do
showmessage(e.Message);
end;
try
// 전송창에 불러온 이미지를 표기
asg_Chat.CreatePicture(C_CH_TEXT, iChatRowCnt, True, ShrinkWithAspectRatio, 0, haleft, vaTop).Bitmap.LoadFromFile(OpenPictureDialog1.FileName);
except
asg_Chat.Cells[C_CH_TEXT, iChatRowCnt] := '[이미지 전송실패]';
end;
// 전송자 표기
asg_Chat.Cells[C_CH_MYCOL, iChatRowCnt] := '[' + EditNick.Text + '] ';
// Scroll 조절
asg_Chat.Row := iChatRowCnt;
// 입력창 이미지 제거
asg_ChatSend.RemovePicture(C_SD_TEXT, 0);
// 상태 Change
IsEmotiSend := False;
except
ms.Free;
end;
end
//----------------------------------------------------------
// File 전송 모드
//----------------------------------------------------------
else if (IsFileSend) then
begin
sFileName := '';
sHideFile := '';
sServerIp := '';
// 첨부파일 Upload
if (Trim(asg_ChatSend.Cells[C_SD_TEXT, 0]) <> '') then
begin
sFileName := ExtractFileName(Trim(asg_ChatSend.Cells[C_SD_TEXT, 0]));
sHideFile := 'CHATAPPEND' + MainDlg.DelChar(CopyByte(MainDlg.asg_Master.Cells[C_M_USERIP, MainDlg.asg_Master.Row], LengthByte(MainDlg.asg_Master.Cells[C_M_USERIP, MainDlg.asg_Master.Row])-3, 4), '.') +
FormatDateTime('YYYYMMDDHHNNSS', Now) + '.KDIALFILE';
if Not FileUpLoad(sHideFile, asg_ChatSend.Cells[C_SD_TEXT, 0], sServerIp) then
begin
Showmessage('첨부파일 UpLoad 중 에러가 발생했습니다.' + #13#10 + #13#10 +
'다시한번 시도해 주시기 바랍니다.');
Exit;
end;
end;
ClientSocket.Socket.SendText(sFileName + '$' + sHideFile + '[A]' + '&[' + EditNick.Text + ']');
asg_Chat.InsertRows(iChatRowCnt, 1);
asg_Chat.Cells[C_CH_MYCOL, iChatRowCnt] := '[' + EditNick.Text + ']';
asg_Chat.Cells[C_CH_TEXT, iChatRowCnt] := sFileName;
asg_Chat.Cells[C_CH_HIDDEN, iChatRowCnt] := sHideFile;
asg_Chat.Cells[C_CH_YOURCOL, iChatRowCnt] := '<전송됨>';
//asg_Chat.AddButton(C_CH_YOURCOL, iChatRowCnt, asg_Chat.ColWidths[C_CH_YOURCOL]-20, 20, '다운', haBeforeText, vaCenter);
asg_Chat.Row := iChatRowCnt;
asg_Chat.Alignments[C_CH_TEXT, iChatRowCnt] := taLeftJustify;
//asg_Chat.AutoSizeRow(iChatRowCnt);
asg_ChatSend.Cells[C_SD_TEXT, 0] := '';
IsFileSend := False;
end
//----------------------------------------------------------
// 일반 Text 전송 모드
//----------------------------------------------------------
else
begin
ClientSocket.Socket.SendText(asg_ChatSend.Cells[C_SD_TEXT, 0] + '&[' + EditNick.Text + ']');
asg_Chat.InsertRows(iChatRowCnt, 1);
asg_Chat.Cells[C_CH_MYCOL, iChatRowCnt] := '[' + EditNick.Text + ']';
asg_Chat.Cells[C_CH_TEXT, iChatRowCnt] := asg_ChatSend.Cells[C_SD_TEXT, 0];
asg_Chat.Row := iChatRowCnt;
asg_Chat.Alignments[C_CH_TEXT, iChatRowCnt] := taLeftJustify;
asg_Chat.AutoSizeRow(iChatRowCnt);
asg_ChatSend.Cells[C_SD_TEXT, 0] := '';
end;
{
SendToAllClients(asg_ChatSend.Cells[C_SD_TEXT, 0] + '&[' + FsUserNm + ']');
asg_Chat.InsertRows(iChatRowCnt, 1);
asg_Chat.Cells[C_CH_MYCOL, iChatRowCnt] := '[' + FsUserNm + '] ';
asg_Chat.Cells[C_CH_TEXT, iChatRowCnt] := asg_ChatSend.Cells[C_SD_TEXT, 0];
asg_Chat.Row := iChatRowCnt;
asg_Chat.Alignments[C_CH_TEXT, iChatRowCnt] := taLeftJustify;
asg_Chat.AutoSizeRow(iChatRowCnt);
}
end;
end;
//------------------------------------------------------------------------------
// [Chat 전송] AdvStringGrid onKeyPress Event Handler
//
// Author : Lee, Se-Ha
// Date : 2013.09.09
//------------------------------------------------------------------------------
procedure TClient.asg_ChatSendKeyPress(Sender: TObject; var Key: Char);
begin
if (Key = #13) and (asg_ChatSend.Cells[C_SD_TEXT, 0] <> '') then
asg_ChatSendButtonClick(Sender, C_SD_BUTTON, 0);
end;
//------------------------------------------------------------------------------
// [종료] Form onClose Event Handler
//
// Author : Lee, Se-Ha
// Date : 2013.09.02
//------------------------------------------------------------------------------
procedure TClient.FormClose(Sender: TObject; var Action: TCloseAction);
begin
{----------------> Debugging
try
ClientSocket.Active := False;
except
on E : Exception do
ShowMessage(E.ClassName+' error raised by ClientSocket.active off, with message : ' + E.Message);
end;
}
{
try
ClientSocket.Socket.Disconnect(ClientSocket.Socket.SocketHandle);
//ServerSocket.Socket.Disconnect(ServerSocket.Socket.SocketHandle);
//ServerSocket.Socket.Close;
//Action := caFree;
except
on E : Exception do
ShowMessage(E.ClassName+' error raised by FormClose(Client), with message : ' + E.Message);
end;
}
Action := caFree;
end;
//------------------------------------------------------------------------------
// [팝업메뉴] Chat 이모티콘 전송
//
// Author : Lee, Se-Ha
// Date : 2013.09.09
//------------------------------------------------------------------------------
procedure TClient.mi_EmotiClick(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
begin
with asg_ChatSend do
begin
CreatePicture(C_SD_TEXT, 0, True, StretchWithAspectRatio {ShrinkWithAspectRatio}, 0, haLeft, vaTop).LoadFromFile(OpenPictureDialog1.FileName);
end;
end;
if asg_ChatSend.GetPicture(C_SD_TEXT, 0) <> nil then
begin
// 이모티콘 전송모드 True
IsEmotiSend := True;
// 자동 전송
asg_ChatSendButtonClick(Sender, C_SD_BUTTON, 0);
end;
end;
//------------------------------------------------------------------------------
// ClientSocket onError Event Handler
//
// Author : Lee, Se-Ha
// Date : 2013.09.02
//------------------------------------------------------------------------------
procedure TClient.ClientSocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
if (ErrorCode = 10053) then
begin
// ErrorCode = 0으로 두면, '응답없음'으로 강제 종료됨.. 버그 잡아야 함..
ErrorCode := 1;
MessageBox(self.Handle,
PChar('ClientSocketError(' + EditNick.Text + '): ' + Socket.RemoteHost + '(' + Socket.RemoteAddress + ') 로부터 잘못된 Connection Error 발생(소켓핸들값: ' + IntToStr(Socket.SocketHandle) + ')'),
'챗(Chat) :: 잘못된 Socket 통신 오류',
MB_OK + MB_ICONERROR);
end;
end;
//------------------------------------------------------------------------------
// [팝업메뉴] Chat 파일 전송
//
// Author : Lee, Se-Ha
// Date : 2013.09.09
//------------------------------------------------------------------------------
procedure TClient.mi_FileSendClick(Sender: TObject);
begin
if od_File.Execute then
asg_ChatSend.Cells[C_SD_TEXT, 0] := ExtractFileName(od_File.FileName);
if asg_ChatSend.Cells[C_SD_TEXT, 0] <> '' then
begin
// 파일 전송모드 True
IsFileSend := True;
// 자동 전송
asg_ChatSendButtonClick(Sender, C_SD_BUTTON, 0);
end;
end;
//------------------------------------------------------------------------------
// [Chat 첨부] AdvStringGrid onButtonClick Event Handler
//
// Author : Lee, Se-Ha
// Date : 2013.09.09
//------------------------------------------------------------------------------
procedure TClient.asg_ChatButtonClick(Sender: TObject; ACol,
ARow: Integer);
var
sLocalFile, sRemoteFile : String;
sServerIp, sFtpUserID, sFtpPasswd, sFtpHostName, sFtpDir : String;
begin
//---------------------------------------------
// 첨부파일 Click시 Event 처리
//---------------------------------------------
with asg_Chat do
begin
if ((ACol = C_CH_MYCOL) and (Cells[C_CH_HIDDEN, ARow] <> '')) then
begin
// 파일 업/다운로드를 위한 정보 조회
if not GetBinUploadInfo(sServerIp, sFtpUserID, sFtpPasswd, sFtpHostName, sFtpDir) then
begin
MessageDlg('파일 저장을 위한 사용자 정보 조회중, 오류가 발생했습니다.', Dialogs.mtError, [Dialogs.mbOK], 0);
exit;
end;
//실제 서버에 저장되어 있는 파일명 지정
if PosByte('/ftpspool/CHATFILE/',Cells[C_CH_HIDDEN, ARow]) > 0 then
sRemoteFile := Cells[C_CH_HIDDEN, ARow]
else
sRemoteFile := '/ftpspool/CHATFILE/' + Cells[C_CH_HIDDEN, ARow];
// C:\DEV --> G_HOMEDIR 전환 @ 2016.07.26 LSH
sLocalFile := G_HOMEDIR + 'TEMP\SPOOL\' + Cells[C_CH_TEXT, ARow]; //lb_Filename.Caption;
if (GetBINFTP(sServerIp, sFtpUserID, sFtpPasswd, sRemoteFile, sLocalFile, False)) then
begin
// ShowMessage('정상적으로 저장되었습니다.');
end;
Screen.Cursor := crDefault;
try
if (PosByte('.exe', sLocalFile) > 0) or
(PosByte('.zip', sLocalFile) > 0) or
(PosByte('.rar', sLocalFile) > 0) then
begin
MessageBox(self.Handle,
PChar('첨부파일(' + sLocalFile + ') 다운로드가 완료되었습니다.' + #13#10 + #13#10 +
'※ 임시 다운로드 폴더 --> C:\KUMC(_DEV)\TEMP\SPOOL\'),
'첨부파일 다운로드 실행 완료',
MB_OK + MB_ICONINFORMATION);
end
else
begin
ShellExecute(HANDLE, 'open',
PCHAR(Cells[C_CH_TEXT, ARow]),
PCHAR(''),
// C:\DEV --> G_HOMEDIR 전환 @ 2016.07.26 LSH
PCHAR(G_HOMEDIR + 'TEMP\SPOOL\'),
SW_SHOWNORMAL);
end;
except
MessageBox(self.Handle,
PChar('해당 프로그램 다운로드중 오류가 발생하였습니다.' + #13#10 + #13#10 +
'프로그램 종료 후 다시 실행해 주시기 바랍니다.'),
'첨부파일 다운로드 중 오류',
MB_OK + MB_ICONERROR);
Exit;
end;
end;
end;
end;
//------------------------------------------------------------------------------
// [FTP] Chat-File Upload function Event Handler
//
// Date : 2013.09.04
// Author : Lee, Se-Ha
//------------------------------------------------------------------------------
function TClient.FileUpLoad(TargetFile, DelFile: String; var S_IP: String):Boolean;
var
sServerIp, sFtpUserId, sFtpPasswd, sFtpHostName, sFtpDir: String;
begin
Result := True;
// 파일 업로드를 위한 정보 조회
if not GetBinUploadInfo(sServerIp, sFtpUserID, sFtpPasswd, sFtpHostName, sFtpDir) then
begin
ShowMessage('파일 저장을 위한 사용자 정보 조회중, 오류가 발생했습니다.');
Result := False;
exit;
end;
try
if id_Ftp.Connected then
nm_Ftp.Disconnect;
except
Showmessage('초기설정 에러');
Result := False;
Exit;
end;
nm_Ftp.Host := sServerIp;
nm_Ftp.UserID := sFtpUserId;
nm_Ftp.Password := sFtpPasswd;
try
nm_Ftp.Connect;
except
Showmessage('FTP 연결에러');
Result := False;
Exit;
end;
try
nm_Ftp.ChangeDir('/ftpspool/CHATFILE');
except
Showmessage('ChangeDir 에러');
Result := False;
Exit;
end;
try
nm_Ftp.mode(MODE_BYTE);
except
Showmessage('Mode 변경 에러');
Result := False;
Exit;
end;
// -- 첨부내용 수정기능 미지원.. 주석처리 @ 2013.08.28 LSH
{
if DelFile <> '' then
begin
if Trim(fed_Attached.Text) <> DelFile then
begin
try
nm_Ftp.Delete(DelFile);
except
Showmessage('파일삭제 에러');
Result := False;
Exit;
end;
end;
end;
}
if Trim(asg_ChatSend.Cells[C_SD_TEXT, 0]) <> '' then
begin
try
nm_Ftp.Upload(Trim(asg_ChatSend.Cells[C_SD_TEXT, 0]), TargetFile);
except
Showmessage('파일전송에러');
Result := False;
Exit;
end;
try
nm_Ftp.DoCommand('SITE CHMOD 644 ' + TargetFile)
except
Showmessage('SITE CHMOD Error');
Result := False;
Exit;
end;
end;
try
nm_Ftp.Disconnect;
except
Showmessage('객체해제 에러');
Result := False;
Exit;
end;
S_IP := sServerIp;
end;
end.