This repository has been archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.pas
597 lines (505 loc) · 15.4 KB
/
main.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
unit Main;
{$mode objfpc}{$H+}
interface
uses
Classes, Forms, Controls, Dialogs, StdCtrls, ComCtrls, Menus, ActnList,
ExtCtrls, LCLIntf, LCLType, IRC, ChannelList, SysUtils, IRCViewIntf;
type
{ TMainForm }
TMainForm = class(TForm, IIRCView)
ActionExit: TAction;
ActionPart: TAction;
ActionCloseTab: TAction;
ActionChat: TAction;
ActionCloseChat: TAction;
ActionConfig: TAction;
ActionConnect: TAction;
ActionDisconnect: TAction;
ActionList: TActionList;
EditFilter: TEdit;
EditInput: TEdit;
ImageListTrayIcons: TImageList;
MainMenu: TMainMenu;
MemoServidor: TMemo;
MenuItemExit: TMenuItem;
MenuItemConnect: TMenuItem;
MenuItemDisconnect: TMenuItem;
MenuItemCloseChannel: TMenuItem;
MenuItemCloseChat: TMenuItem;
MenuItemChat: TMenuItem;
MenuItemCloseTab: TMenuItem;
MenuItemCloseActiveTab: TMenuItem;
MenuItemConfig: TMenuItem;
MenuItemServer: TMenuItem;
PageControl: TPageControl;
PanelRoot: TPanel;
PanelLeft: TPanel;
PanelRight: TPanel;
PopupMenuTray: TPopupMenu;
PopupMenuPageControl: TPopupMenu;
PopupMenuTreeView: TPopupMenu;
StatusBar: TStatusBar;
TabServer: TTabSheet;
TimerPing: TTimer;
TimerConnection: TTimer;
TrayIcon: TTrayIcon;
TreeViewUsers: TTreeView;
procedure ActionChatExecute(Sender: TObject);
procedure ActionPartExecute(Sender: TObject);
procedure ActionCloseChatExecute(Sender: TObject);
procedure ActionCloseTabExecute(Sender: TObject);
procedure ActionConnectExecute(Sender: TObject);
procedure ActionConfigExecute(Sender: TObject);
procedure ActionDisconnectExecute(Sender: TObject);
procedure ActionExitExecute(Sender: TObject);
procedure EditFilterKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
procedure EditFilterKeyUp(Sender: TObject; var Key: word; Shift: TShiftState);
procedure EditInputKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
procedure EditInputKeyUp(Sender: TObject; var Key: word; Shift: TShiftState);
procedure ApplicationActivate(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormShow(Sender: TObject);
procedure FormWindowStateChange(Sender: TObject);
procedure PageControlChange(Sender: TObject);
procedure PageControlMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: integer);
procedure PopupMenuTreeViewPopup(Sender: TObject);
procedure TimerConnectionTimer(Sender: TObject);
procedure TimerPingTimer(Sender: TObject);
procedure TrayIconClick(Sender: TObject);
procedure TreeViewUsersDblClick(Sender: TObject);
procedure TreeViewUsersSelectionChanged(Sender: TObject);
private
FIRC: TIRC;
FChannelList: TChannelList;
procedure AddNicksToChannel(const Channel: TChannel; const List: TStrings);
procedure AddNicksToTreeView(const Channel: TChannel);
procedure AddUserToTreeView(const User: TUser; const Channel: TChannel);
procedure ConfigureMemo(var Memo: TMemo);
procedure SetColors;
function GetTab(const ACaption: string): TObject;
function GetNode(const ACaption: string; ParentNode: TObject): TObject;
function IsActiveTabChannel: boolean;
function IsChatTabOpen(const Nome: string): boolean;
function IsSelectedNodeUser: boolean;
function IsSelectedNodeChannel: boolean;
procedure MostrarConfig;
procedure OnNickListReceived(const ChannelName: string; List: TStrings);
procedure OnMessageReceived(const Channel, Message: string; OwnMessage: boolean);
procedure UpdateNodeText(Node: TObject; AText: string);
procedure UpdateTabCaption(Tab: TObject; ACaption: string);
procedure RemoveChannelFromList(const Channel: string);
procedure RemoveNickFromChannelList(const Nick: string; const ChannelName: string);
procedure OnShowPopup(const Msg: string);
function GetTabByName(const Channel: string): TTabSheet;
function NewChannelTab(const Channel: string): TTabSheet;
procedure SelectChannelTab;
procedure SetFocusEditInput;
procedure StopTrayIconAnimation;
procedure ServerMessage(const AText: string);
procedure NotifyChanged;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
end;
var
MainForm: TMainForm;
implementation
uses FileUtil, ConfigForm, config, StringUtils, IRCUtils, TreeviewHelper;
{$R *.lfm}
const
DefaultFontSize = 11;
BackGroundColor = $00222827;
FontColor = $00D0D0D0;
LightColor = $00D0D0D0;
procedure TMainForm.ActionDisconnectExecute(Sender: TObject);
begin
FIRC.Disconnect;
end;
procedure TMainForm.ActionExitExecute(Sender: TObject);
begin
Close;
end;
procedure TMainForm.EditFilterKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
begin
if (Key = VK_RETURN) and IsSelectedNodeUser then
ActionChat.Execute;
end;
procedure TMainForm.EditFilterKeyUp(Sender: TObject; var Key: word; Shift: TShiftState);
begin
if Key = VK_RETURN then
Exit;
TreeViewUsers.BeginUpdate;
try
TreeViewUsers.FilterNodes(EditFilter.Text);
TreeViewUsers.FullExpand;
finally
TreeViewUsers.EndUpdate;
end;
end;
procedure TMainForm.EditInputKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
var
word, Nick: string;
CarretPos: TPoint;
begin
if (key <> VK_TAB) or (EditInput.Text = '') then
Exit;
word := TStringUtils.GetWordAtCursor(EditInput.Text, EditInput.CaretPos.x);
Nick := FChannelList.AutoComplete(FIRC.ActiveChannel, word);
if Nick <> '' then
begin
EditInput.Text := StringReplace(EditInput.Text, word, Nick, []);
CarretPos.x := Pos(Nick, EditInput.Text) + Length(Nick);
CarretPos.y := EditInput.CaretPos.y;
EditInput.CaretPos := CarretPos;
end;
Key := VK_UNDEFINED;
end;
procedure TMainForm.ActionConnectExecute(Sender: TObject);
begin
FIRC.Connect;
end;
procedure TMainForm.ActionConfigExecute(Sender: TObject);
begin
MostrarConfig;
end;
procedure TMainForm.ActionCloseChatExecute(Sender: TObject);
begin
GetTabByName(TIRCUtils.RemoveOPVoicePrefix(TreeViewUsers.Selected.Text)).Free;
end;
procedure TMainForm.ActionCloseTabExecute(Sender: TObject);
var
TabToClose: TTabSheet;
begin
if PageControl.ActivePage = nil then
Exit;
if Sender is TTabSheet then
TabToClose := TTabSheet(Sender)
else
TabToClose := PageControl.ActivePage;
if TabToClose = TabServer then
Exit;
if IsActiveTabChannel then
FIRC.Part(PageControl.ActivePage.Caption)
else
GetTabByName(PageControl.ActivePage.Caption).Free;
end;
procedure TMainForm.ActionChatExecute(Sender: TObject);
begin
SelectChannelTab;
end;
procedure TMainForm.ActionPartExecute(Sender: TObject);
begin
FIRC.Part(TreeViewUsers.Selected.Text);
end;
procedure TMainForm.EditInputKeyUp(Sender: TObject; var Key: word; Shift: TShiftState);
begin
if Key <> VK_RETURN then
Exit;
FIRC.SendMessage(EditInput.Text);
EditInput.Clear;
end;
procedure TMainForm.ApplicationActivate(Sender: TObject);
begin
StopTrayIconAnimation;
end;
procedure TMainForm.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
FIRC.Disconnect;
end;
procedure TMainForm.FormShow(Sender: TObject);
begin
if FIRC.IsConnected then
Exit;
if not FileExistsUTF8(DefaultConfigFile) then
MostrarConfig;
FIRC.Connect;
end;
procedure TMainForm.FormWindowStateChange(Sender: TObject);
begin
AutoScroll := WindowState <> wsMinimized;
end;
procedure TMainForm.OnMessageReceived(const Channel, Message: string; OwnMessage: boolean);
var
Tab: TTabSheet;
Memo: TMemo;
function NickNameInMessage: boolean;
begin
Result := (Pos(FIRC.NickName, Message) > 0);
end;
function AnimateTrayIcon: boolean;
begin
Result := (not OwnMessage) and (not Application.Active) and ((Channel[1] <> '#') or NickNameInMessage);
end;
begin
if Message = '' then
Exit;
if AnimateTrayIcon then
TrayIcon.Animate := True; //Should not toggle, more messages may have triggered this
Tab := GetTab(Channel) as TTabSheet;
Memo := Tab.Components[0] as TMemo;
Memo.Lines.Add(Message);
end;
procedure TMainForm.UpdateTabCaption(Tab: TObject; ACaption: string);
begin
(Tab as TTabSheet).Caption := ACaption;
end;
procedure TMainForm.UpdateNodeText(Node: TObject; AText: string);
begin
(Node as TTreeNode).Text := AText;
end;
procedure TMainForm.MostrarConfig;
var
F: TFormConfig;
begin
F := TFormConfig.Create(nil);
try
F.ShowModal;
finally
F.Free;
end;
end;
procedure TMainForm.ConfigureMemo(var Memo: TMemo);
begin
Memo.Align := alClient;
Memo.ScrollBars := ssVertical;
Memo.ReadOnly := True;
Memo.Cursor := crArrow;
Memo.Font.Size := DefaultFontSize;
Memo.TabStop := False;
Memo.BorderStyle := bsSingle;
Memo.Color := BackGroundColor;
Memo.Font.Color := FontColor;
Memo.Invalidate;
end;
procedure TMainForm.SetColors;
begin
EditFilter.Color := LightColor;
EditInput.Color := BackGroundColor;
;
EditInput.Font.Color := FontColor;
TreeViewUsers.BackgroundColor := LightColor;
end;
function TMainForm.GetTab(const ACaption: string): TObject;
var
ChannelName: string;
begin
ChannelName := TIRCUtils.RemoveOPVoicePrefix(ACaption);
Result := GetTabByName(ChannelName);
if Result = nil then
Result := NewChannelTab(ChannelName);
end;
function TMainForm.GetNode(const ACaption: string; ParentNode: TObject): TObject;
begin
if ParentNode = nil then
Result := TreeViewUsers.Items.Add(nil, ACaption)
else
Result := TreeViewUsers.Items.AddChild(ParentNode as TTreeNode, ACaption);
TreeViewUsers.AlphaSort;
end;
function TMainForm.IsActiveTabChannel: boolean;
begin
Result := TreeViewUsers.Items.FindTopLvlNode(PageControl.ActivePage.Caption) <> nil;
end;
function TMainForm.IsChatTabOpen(const Nome: string): boolean;
begin
Result := GetTabByName(TIRCUtils.RemoveOPVoicePrefix(Nome)) <> nil;
end;
function TMainForm.IsSelectedNodeUser: boolean;
begin
Result := (TreeViewUsers.Selected <> nil) and (TreeViewUsers.Selected.Parent <> nil);
end;
function TMainForm.IsSelectedNodeChannel: boolean;
begin
Result := (TreeViewUsers.Selected <> nil) and (TreeViewUsers.Selected.Parent = nil);
end;
procedure TMainForm.AddNicksToChannel(const Channel: TChannel; const List: TStrings);
var
Nick: string;
begin
for Nick in List do
Channel.Users.Add(TUser.Create(Nick));
end;
procedure TMainForm.AddNicksToTreeView(const Channel: TChannel);
var
User: TUser;
begin
TreeViewUsers.BeginUpdate;
try
Channel.Node := TreeViewUsers.Items.FindTopLvlNode(Channel.Name);
for User in Channel.Users do
User.Node := TreeViewUsers.Items.AddChild(TTreeNode(Channel.Node), User.NickNameInChannel);
TreeViewUsers.AlphaSort;
finally
TreeViewUsers.EndUpdate;
end;
end;
procedure TMainForm.AddUserToTreeView(const User: TUser; const Channel: TChannel);
begin
User.Node := TreeViewUsers.Items.AddChild(TTreeNode(Channel.Node), User.NickNameInChannel);
TreeViewUsers.AlphaSort;
end;
procedure TMainForm.PageControlChange(Sender: TObject);
begin
if PageControl.ActivePage = TabServer then
FIRC.ActiveChannel := ''
else
FIRC.ActiveChannel := PageControl.ActivePage.Caption;
SetFocusEditInput;
end;
procedure TMainForm.PageControlMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: integer);
var
TabIndex: integer;
begin
if Button <> mbRight then
Exit;
TabIndex := PageControl.IndexOfTabAt(X, Y);
if TabIndex >= 0 then
PageControl.ActivePage := PageControl.Pages[TabIndex];
end;
procedure TMainForm.PopupMenuTreeViewPopup(Sender: TObject);
var
IsChatOpen: boolean;
begin
IsChatOpen := IsChatTabOpen(TreeViewUsers.Selected.Text);
ActionPart.Visible := IsSelectedNodeChannel;
ActionChat.Visible := IsSelectedNodeUser and (not IsChatOpen);
ActionCloseChat.Visible := IsSelectedNodeUser and IsChatOpen;
end;
procedure TMainForm.TimerConnectionTimer(Sender: TObject);
begin
//TODO: Timer and this logic must be on the IRC class
if FIRC.IsConnected then
StatusBar.Panels[0].Text := Format('Connected. %s@%s', [FIRC.NickName, FIRC.HostName])
else
begin
StatusBar.Panels[0].Text := 'Disconnected. Reconnecting... \o/';
FIRC.Disconnect;
FIRC.Connect;
end;
end;
procedure TMainForm.TimerPingTimer(Sender: TObject);
begin
if FIRC.IsConnected then
FIRC.Ping;
end;
procedure TMainForm.TrayIconClick(Sender: TObject);
begin
StopTrayIconAnimation;
if WindowState = wsMinimized then
Application.Restore
else
Application.Minimize;
end;
procedure TMainForm.TreeViewUsersDblClick(Sender: TObject);
begin
SelectChannelTab;
end;
procedure TMainForm.TreeViewUsersSelectionChanged(Sender: TObject);
var
Selected: TTreeNode;
Tab: TTabSheet;
begin
Selected := TreeViewUsers.Selected;
if Selected = nil then
Exit;
Tab := GetTabByName(TIRCUtils.RemoveOPVoicePrefix(Selected.Text));
if Tab = nil then
Exit;
PageControl.ActivePage := Tab;
end;
procedure TMainForm.RemoveChannelFromList(const Channel: string);
begin
FChannelList.ChannelByName(Channel).Node.Free;
end;
procedure TMainForm.RemoveNickFromChannelList(const Nick: string; const ChannelName: string);
var
User: TUser;
Channel: TChannel;
begin
Channel := FChannelList.ChannelByName(ChannelName);
if Channel = nil then
Exit;
User := Channel.Users.UserByNick(Nick);
if (User <> nil) then
User.Node.Free;
end;
procedure TMainForm.OnShowPopup(const Msg: string);
begin
ShowMessage(Msg);
end;
function TMainForm.GetTabByName(const Channel: string): TTabSheet;
var
I: integer;
begin
for I := 0 to PageControl.PageCount - 1 do
begin
Result := PageControl.Pages[I];
if UpperCase(Result.Caption) = UpperCase(Channel) then
Exit;
end;
Result := nil;
end;
function TMainForm.NewChannelTab(const Channel: string): TTabSheet;
var
Memo: TMemo;
begin
Result := TTabSheet.Create(PageControl);
Result.PageControl := PageControl;
Result.Caption := Channel;
Memo := TMemo.Create(Result);
Memo.Parent := Result;
ConfigureMemo(Memo);
end;
procedure TMainForm.SelectChannelTab;
begin
if (TreeViewUsers.Selected = nil) or (TreeViewUsers.Selected.Parent = nil) then
Exit;
PageControl.ActivePage := GetTab(TreeViewUsers.Selected.Text) as TTabSheet;
end;
procedure TMainForm.SetFocusEditInput;
begin
if EditInput.CanFocus then
EditInput.SetFocus;
end;
procedure TMainForm.StopTrayIconAnimation;
begin
TrayIcon.Animate := False;
TrayIcon.Icon.Assign(Application.Icon);
end;
procedure TMainForm.ServerMessage(const AText: string);
begin
MemoServidor.Append(AText);
end;
procedure TMainForm.NotifyChanged;
begin
TreeViewUsers.Invalidate;
end;
procedure TMainForm.OnNickListReceived(const ChannelName: string; List: TStrings);
var
Channel: TChannel;
begin
Channel := FChannelList.ChannelByName(ChannelName);
AddNicksToChannel(Channel, List);
AddNicksToTreeView(Channel);
end;
constructor TMainForm.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
Application.OnActivate := @ApplicationActivate;
TrayIcon.Icons := ImageListTrayIcons;
TrayIcon.AnimateInterval := 1250;
FChannelList := TChannelList.Create(Self);
FIRC := TIRC.Create(FChannelList);
FIRC.OnMessageReceived := @OnMessageReceived;
FIRC.OnNickListReceived := @OnNickListReceived;
FIRC.OnShowPopup := @OnShowPopup;
ConfigureMemo(MemoServidor);
SetColors;
end;
destructor TMainForm.Destroy;
begin
FIRC.Free;
FChannelList.Free;
inherited Destroy;
end;
end.