-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainForm.pas
619 lines (549 loc) · 15.9 KB
/
mainForm.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
unit mainForm;
interface
// tab order requires custom FMX.TabControl:
// procedure TTabItem.DoAddObject(const AObject: TFmxObject); contains no AddToTabList(AObject); call
// Yes, I know about TClipboard, embarcadero.rs_xe4/rad/Cutting,_Copying,_and_Pasting_Text.html
uses
System.SysUtils, System.Types, System.UITypes, System.Rtti, System.Classes,
System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs,
FMX.StdCtrls, FMX.TabControl, FMX.Layouts, FMX.Memo, FMX.Edit, FMX.ListBox,
FMX.ExtCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, IdStack, IdHeaderList, IdException, Web.HTTPApp, IdIntercept,
IdLogBase, IdLogDebug, IdGlobal, shellapi,
FMX.Platform.Win, AnsiStrings,
System.Win.ComObj, System.Win.Registry,
Winapi.Windows, Winapi.Messages, Winapi.ShlObj,
Character, FMX.Objects,
System.IniFiles;
const
WM_TRAYMSG = WM_USER + 250;
APP_CAPTION = 'pastebin.org';
DEV_KEY = '8013dbc557b58bad3ffbb2135cb8ac48';
PASTEBIN_URL = 'http://pastebin.com/api/api_post.php';
HK_VIEW = 0;
HK_EDIT = 1;
CFG_MAINSECTION = 'Settings';
CFG_DEVKEY = 'DevKey';
CFG_TRAY = 'Tray';
CFG_DEBUG = 'Debug';
CFG_HOTKEY = 'Hotkey';
CFG_FORMAT = 'Format';
CFG_KEEP = 'Keep';
CFG_VISIBILITY = 'Visibility';
resourcestring
STR_ENTER_HOTKEY = 'Нажмите сочетание клавиш или Esc для отмены';
STR_COPIED_TO_CLIPBOARD = ' скопировано в буфер обмена';
STR_PROTOCOL_ERROR = 'Ошибка протокола. HTTP статус: %s %s';
STR_SOCKET_ERROR = 'Сокет вернул ошибку. Код: %s %s';
STR_GENERAL_EXC = 'Общее исключение в классе: %s %s';
STR_NOTHING_TO_SEND = 'Нечего посылать';
STR_TITLE_IS_EMPTY = 'Не заполнено название';
STR_SENDING = 'Отправляем...';
STR_CONNECTION_CLOSED = 'Connection was closed gracefully.';
STR_UNKNOWN_EXC = 'Неизвестное исключение ';
STR_HTTP_HEADERS = '--------------------<HTTP header>---------------------';
STR_HTTP_HEADERS_END =
'--------------------</HTTP header>--------------------';
type
TPasteHotKey = record
FMod: TShiftState;
FKey: Word;
end;
TStatusLevel = (slInfo, slError, slOk);
TfrmPaste = class(TForm)
TabControl1: TTabControl;
TabItem1: TTabItem;
TabItem2: TTabItem;
pOptionMain: TPanel;
lName: TLabel;
eTitle: TEdit;
cbFormat: TComboBox;
lFormat: TLabel;
lExpiration: TLabel;
lVisibility: TLabel;
cbVisibility: TComboBox;
btnPaste: TButton;
cbKeep: TComboBox;
mPaste: TMemo;
IdLogDebug1: TIdLogDebug;
Client: TIdHTTP;
cbCloseType: TCheckBox;
eDevkey: TEdit;
cbDebug: TCheckBox;
TabItem3: TTabItem;
mLog: TMemo;
lHotkeys: TLabel;
StatusBar1: TStatusBar;
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
StyleBook2: TStyleBook;
lStatus: TText;
procedure FormCreate(Sender: TObject);
procedure btnPasteClick(Sender: TObject);
procedure IdLogDebug1Send(ASender: TIdConnectionIntercept;
var ABuffer: TIdBytes);
procedure FormDestroy(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure cbDebugChange(Sender: TObject);
procedure lHotkeysClick(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char;
Shift: TShiftState);
procedure ClientHeadersAvailable(Sender: TObject; AHeaders: TIdHeaderList;
var VContinue: Boolean);
private
{ Private declarations }
FNID: TNotifyIconData;
Msg_Handler: HWND;
StartUp: Boolean;
FHotKey: TPasteHotKey;
F_OldHotKey: TPasteHotKey;
procedure ShowTray();
procedure LoadFormats();
procedure HideFromTaskbar();
procedure MESSAGE_WND_PROC(var _message: TMessage);
procedure CopyToClipboard(const str: String);
function CopyFromClipboard(): String;
procedure UpdateHKOnForm();
procedure UpdateHKSequence(var KeyChar: Char; var Key: Word;
Shift: TShiftState);
procedure UpdateStatus(statusText: String; Level: TStatusLevel = slInfo);
procedure Send();
procedure RegisterHK();
procedure HK_NewPaste();
procedure ReadSettings();
procedure WriteSettings();
function PackHotkey(hk: TPasteHotKey): Integer;
function UnPackHotkey(packedHK: Integer): TPasteHotKey;
public
{ Public declarations }
end;
TString = class(TObject)
public
value: String;
constructor Create(val: String);
end;
var
frmPaste: TfrmPaste;
implementation
{$R *.fmx}
procedure TfrmPaste.cbDebugChange(Sender: TObject);
begin
TabItem3.Visible := cbDebug.IsChecked
end;
procedure TfrmPaste.ClientHeadersAvailable(Sender: TObject;
AHeaders: TIdHeaderList; var VContinue: Boolean);
var
headers: TStringsEnumerator;
begin
headers := AHeaders.GetEnumerator;
mLog.Lines.Add(STR_HTTP_HEADERS);
while (headers.MoveNext) do
mLog.Lines.Add(headers.GetCurrent);
mLog.Lines.Add(STR_HTTP_HEADERS_END);
VContinue := true;
end;
procedure TfrmPaste.CopyToClipboard(const str: String);
var
buff: THandle;
len: Integer;
begin
len := (str.Length + 1) * SizeOf(Char);
buff := GlobalAlloc(GMEM_MOVEABLE + GMEM_DDESHARE, len);
Move(PChar(str)^, GlobalLock(buff)^, len);
GlobalUnlock(buff);
OpenClipboard(WindowHandleToPlatform(self.Handle).Wnd);
EmptyClipboard();
SetClipboardData(CF_UNICODETEXT, buff);
CloseClipboard;
end;
function TfrmPaste.CopyFromClipboard: String;
var
Handle: HWND;
begin
OpenClipboard(WindowHandleToPlatform(self.Handle).Wnd);
Handle := GetClipboardData(CF_UNICODETEXT);
CloseClipboard;
if (Handle = null) then
result := EmptyStr
else
begin
result := PChar(Handle)
end;
end;
procedure TfrmPaste.FormActivate(Sender: TObject);
begin
if StartUp then
begin
hide;
StartUp := false;
end;
end;
procedure TfrmPaste.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
if cbCloseType.IsChecked then
begin
CanClose := false;
hide;
end;
end;
procedure TfrmPaste.FormCreate(Sender: TObject);
begin
Msg_Handler := AllocateHWnd(MESSAGE_WND_PROC);
UpdateStatus('');
LoadFormats();
ReadSettings();
if (FHotKey.FKey = 0) then
begin
FHotKey.FMod := [ssAlt, ssShift, ssCtrl];
FHotKey.FKey := vkC;
end;
lHotkeys.StyleLookup := '';
UpdateHKOnForm;
RegisterHK;
//Prevents form from infinite loop (see onActivate handler)
StartUp := true;
ShowTray();
HideFromTaskbar();
end;
procedure TfrmPaste.FormDestroy(Sender: TObject);
begin
Shell_NotifyIcon(NIM_DELETE, @FNID);
DeallocateHWnd(Msg_Handler);
WriteSettings;
end;
procedure TfrmPaste.lHotkeysClick(Sender: TObject);
procedure ResetHotKey(var hk: TPasteHotKey);
begin
hk.FMod := [];
hk.FKey := 0;
lHotkeys.Text := STR_ENTER_HOTKEY;
end;
begin
if lHotkeys.Tag = HK_VIEW then
begin
lHotkeys.StyleLookup := 'lHotkeysStyle1';
lHotkeys.Tag := HK_EDIT;
F_OldHotKey := FHotKey;
ResetHotKey(FHotKey);
end;
end;
procedure TfrmPaste.UpdateHKOnForm();
var
caption: String;
begin
caption := '';
with FHotKey do
begin
if ssCtrl in FMod then
caption := caption + 'Ctrl+';
if ssAlt in FMod then
caption := caption + 'Alt+';
if ssShift in FMod then
caption := caption + 'Shift+';
lHotkeys.Text := caption + Char(FKey);
end;
end;
procedure TfrmPaste.UpdateHKSequence(var KeyChar: Char; var Key: Word;
Shift: TShiftState);
begin
if ((Shift = []) and (Key = vkEscape)) then
begin
lHotkeys.Tag := HK_VIEW;
lHotkeys.StyleLookup := '';
FHotKey := F_OldHotKey;
end
else
begin
case Key of
vkShift:
FHotKey.FMod := FHotKey.FMod + [ssShift];
vkControl:
FHotKey.FMod := FHotKey.FMod + [ssCtrl];
vkMenu:
FHotKey.FMod := FHotKey.FMod + [ssAlt];
else
// lStatus.Text := r.ToString(r) + ' : ' + KeyChar + ' : ' + Key.ToString(Key) + ' : ' + r.ToString(Lo(r));
if (Key = 0) then
FHotKey.FKey := Lo(VkKeyScanEx(KeyChar, GetKeyboardLayout(0)))
else
FHotKey.FKey := Key;
lHotkeys.Tag := HK_VIEW;
lHotkeys.StyleLookup := '';
RegisterHK();
end;
end;
UpdateHKOnForm;
end;
procedure TfrmPaste.UpdateStatus(statusText: String; Level: TStatusLevel);
begin
if (Level = slOk) then
lStatus.Color := TAlphaColorRec.Darkgreen
else if (Level = slError) then
lStatus.Color := TAlphaColorRec.Crimson
else
lStatus.Color := TAlphaColorRec.Blue;
lStatus.Text := statusText;
end;
procedure TfrmPaste.WriteSettings;
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(ChangeFileExt(ParamStr(0), '.ini'));
try
with Ini do
begin
WriteString(CFG_MAINSECTION, CFG_DEVKEY, eDevkey.Text);
WriteBool(CFG_MAINSECTION, CFG_TRAY, cbCloseType.IsChecked);
WriteBool(CFG_MAINSECTION, CFG_DEBUG, cbDebug.IsChecked);
WriteInteger(CFG_MAINSECTION, CFG_HOTKEY, PackHotkey(FHotKey));
WriteInteger(CFG_MAINSECTION, CFG_FORMAT, cbFormat.ItemIndex);
WriteInteger(CFG_MAINSECTION, CFG_KEEP, cbKeep.ItemIndex);
WriteInteger(CFG_MAINSECTION, CFG_VISIBILITY, cbVisibility.ItemIndex);
end
finally
Ini.Free;
end;
end;
procedure TfrmPaste.ReadSettings;
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(ChangeFileExt(ParamStr(0), '.ini'));
try
with Ini do
begin
eDevkey.Text := ReadString(CFG_MAINSECTION, CFG_DEVKEY, '');
cbCloseType.IsChecked := ReadBool(CFG_MAINSECTION, CFG_TRAY, True);
cbDebug.IsChecked := ReadBool(CFG_MAINSECTION, CFG_DEBUG, False);
cbDebug.OnChange(nil);
FHotKey := UnPackHotkey( ReadInteger(CFG_MAINSECTION, CFG_HOTKEY, 0));
cbFormat.ItemIndex := ReadInteger(CFG_MAINSECTION, CFG_FORMAT, 0);
cbKeep.ItemIndex := ReadInteger(CFG_MAINSECTION, CFG_KEEP, 0);
cbVisibility.ItemIndex := ReadInteger(CFG_MAINSECTION, CFG_VISIBILITY, 0);
end;
finally
Ini.Free;
end;
end;
procedure TfrmPaste.FormKeyDown(Sender: TObject; var Key: Word;
var KeyChar: Char; Shift: TShiftState);
begin
if lHotkeys.Tag = HK_EDIT then
UpdateHKSequence(KeyChar, Key, Shift)
else if ((ssCtrl in Shift) and (Key = VK_RETURN)) then
btnPasteClick(nil);
end;
procedure TfrmPaste.RegisterHK();
var
mods: Integer;
begin
if F_OldHotKey.FKey > 0 then
UnregisterHotKey(Msg_Handler, F_OldHotKey.FKey);
with FHotKey do
begin
mods := 0;
// any way to iterate through set that buit on constant instead of itemtype?
if (ssShift in FMod) then
mods := mods or MOD_SHIFT;
if (ssCtrl in FMod) then
mods := mods or MOD_CONTROL;
if (ssAlt in FMod) then
mods := mods or MOD_ALT;
RegisterHotKey(Msg_Handler, FKey, mods, FKey and $FF)
end;
end;
procedure TfrmPaste.HideFromTaskbar;
begin
// AllocateHWnd
ShowWindow(GetWindow(WindowHandleToPlatform(self.Handle).Wnd,
GW_OWNER), SW_HIDE);
with CreateComObject(CLSID_TaskbarList) as ITaskbarList do
begin
HrInit;
DeleteTab(WindowHandleToPlatform(frmPaste.Handle).Wnd);
end;
end;
procedure TfrmPaste.HK_NewPaste;
var
caption: String;
begin
TabControl1.ActiveTab := TabItem1;
mPaste.Text := CopyFromClipboard();
for caption in mPaste.Lines do
if (not caption.Trim.IsEmpty) then
begin
eTitle.Text := caption.Trim;
eTitle.SelectAll;
break;
end;
end;
procedure TfrmPaste.LoadFormats();
var
resource: TStringList;
I: Integer;
arr: TArray<String>;
rs: TResourceStream;
begin
cbFormat.Clear;
resource := TStringList.Create;
rs := TResourceStream.Create(hInstance, 'RC_formats', RT_RCDATA);
resource.LoadFromStream(rs);
for I := 0 to resource.Count - 1 do
begin
arr := resource.Strings[I].Split(['=']);
cbFormat.Items.AddObject(arr[1], TString.Create(arr[0]));
end;
cbFormat.ItemIndex := 0;
rs.Free;
cbKeep.Clear;
rs := TResourceStream.Create(hInstance, 'RC_expire', RT_RCDATA);
resource.LoadFromStream(rs);
for I := 0 to resource.Count - 1 do
begin
arr := resource.Strings[I].Split(['=']);
cbKeep.Items.AddObject(arr[1], TString.Create(arr[0]));
end;
cbKeep.ItemIndex := 0;
resource.Free;
rs.Free;
end;
function TfrmPaste.PackHotkey(hk: TPasteHotKey): Integer;
begin
Result := 0;
if (ssShift in hk.FMod) then
Result := Result or MOD_SHIFT;
if (ssCtrl in hk.FMod) then
Result := Result or MOD_CONTROL;
if (ssAlt in hk.FMod) then
Result := Result or MOD_ALT;
Result := (Result shl 8) or (hk.FKey and $FF);
end;
function TfrmPaste.UnPackHotkey(packedHK: Integer): TPasteHotKey;
var
tmp: Integer;
begin
tmp := packedHK;
Result.FKey := tmp and $FF;
tmp := (tmp shr 8) and $FF;
if (tmp and MOD_SHIFT > 0) then
Result.FMod := Result.FMod + [ssShift];
if (tmp and MOD_ALT > 0) then
Result.FMod := Result.FMod + [ssAlt];
if (tmp and MOD_CONTROL > 0) then
Result.FMod := Result.FMod + [ssCtrl];
end;
procedure TfrmPaste.IdLogDebug1Send(ASender: TIdConnectionIntercept;
var ABuffer: TIdBytes);
begin
mLog.Lines.Append(TEncoding.UTF8.GetString(TBytes(ABuffer)))
end;
procedure TfrmPaste.Send;
var
expiration, highlight, visibility: String;
params: TStringList;
status: TStatusLevel;
str: string;
function getCBValue(cb: TComboBox): String;
begin
result := TString(cb.Items.Objects[cb.ItemIndex]).value;
end;
begin
UpdateStatus(STR_SENDING);
expiration := getCBValue(cbKeep);
highlight := getCBValue(cbFormat);
visibility := cbVisibility.ItemIndex.ToString;
params := TStringList.Create;
with params do
begin
Add('api_option=paste');
Add('api_paste_private=' + visibility);
if eTitle.Text.Length > 0 then
Add('api_paste_name=' + String(HTTPEncode(AnsiString(eTitle.Text))));
Add('api_dev_key=' + eDevkey.Text);
Add('api_paste_expire_date=' + expiration);
Add('api_paste_format=' + highlight);
Add('api_paste_code=' + mPaste.Text);
end;
status := slError;
try
str := Client.Post(PASTEBIN_URL, params);
CopyToClipboard(str);
str := str + STR_COPIED_TO_CLIPBOARD;
status := slOk;
except
on E: EIdHTTPProtocolException do
str := String.Format(STR_PROTOCOL_ERROR, [IntToStr(E.ErrorCode),
E.Message]);
on E: EIdConnClosedGracefully do
str := STR_CONNECTION_CLOSED;
on E: EIdSocketError do
str := String.Format(STR_SOCKET_ERROR, [IntToStr(E.LastError),
E.Message]);
on E: EIdException do
str := String.Format(STR_GENERAL_EXC, [E.ClassName, E.Message]);
on E: Exception do
str := STR_UNKNOWN_EXC + E.Message;
end;
UpdateStatus(str, status);
params.Free;
end;
procedure TfrmPaste.ShowTray;
begin
// http://stackoverflow.com/questions/20109686/fmx-trayicon-message-handling
// http://www.cyberforum.ru/delphi-firemonkey/thread626503.html
with FNID do
begin
cbSize := SizeOf;
Wnd := Msg_Handler;
uID := 1;
uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
uCallbackMessage := WM_TRAYMSG;
hIcon := LoadImage(hInstance, PWideChar('RC_TRAY_ICON'), IMAGE_ICON,
32, 32, 0);
StrPCopy(szTip, APP_CAPTION);
end;
Shell_NotifyIcon(NIM_ADD, @FNID);
end;
procedure TfrmPaste.MESSAGE_WND_PROC(var _message: TMessage);
procedure PopupMainForm();
begin
frmPaste.Show;
frmPaste.TopMost := true;
frmPaste.TopMost := false;
frmPaste.SetActive(true);
eTitle.SetFocus;
end;
begin
if _message.Msg = WM_HOTKEY then
begin
HK_NewPaste();
PopupMainForm();
end
else
case _message.lParam of
WM_LBUTTONDOWN:
PopupMainForm();
WM_RBUTTONDOWN:
begin
UnregisterHotKey(Msg_Handler, FHotKey.FKey);
Application.Terminate;
end;
end;
end;
procedure TfrmPaste.btnPasteClick(Sender: TObject);
begin
if (mPaste.Text.Trim.IsEmpty) then
UpdateStatus(STR_NOTHING_TO_SEND, slError)
else if (eTitle.Text.Trim.IsEmpty) then
UpdateStatus(STR_TITLE_IS_EMPTY, slError)
else
Send();
end;
{ TString }
constructor TString.Create(val: String);
begin
value := val
end;
end.