-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfrmmain_full.pas
1948 lines (1644 loc) · 51.5 KB
/
frmmain_full.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
992
993
994
995
996
997
998
999
1000
unit frmMain_Full;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
ComCtrls, ExtCtrls, Grids, blcksock, fpjson, jsonConf, LMessages, EditBtn,
DBCtrls, Menus, md5, google_drive;
const
WM_AFTER_SHOW = WM_USER + 300;
type
{ TMainform }
TMainform = class(TForm)
btGetAccess: TButton;
btGetFileList: TButton;
btSendMail: TButton;
btRemoveTokens: TButton;
btClearLog: TButton;
btnSimpleUpload: TButton;
btnUploadWithResume: TButton;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
btGetAppointments: TButton;
btClearDebug: TButton;
Button6: TButton;
Button8: TButton;
btGetContacts: TButton;
listmthd: TCheckBox;
ckHideFolders: TCheckBox;
CheckGroup1: TCheckGroup;
Edit4: TEdit;
edLocation: TEdit;
edStart: TDateEdit;
edEnd: TDateEdit;
edBody: TMemo;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
edTitle: TEdit;
edDescription: TEdit;
edRecipient: TEdit;
edSender: TEdit;
edSubject: TEdit;
ImageList1: TImageList;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
ListView1: TListView;
ListView2: TListView;
MenuItem1: TMenuItem;
exportmenu: TMenuItem;
MenuItem2: TMenuItem;
MenuItem3: TMenuItem;
MenuItem4: TMenuItem;
PageControl6: TPageControl;
Panel2: TPanel;
Panel3: TPanel;
PopupMenu1: TPopupMenu;
ProgressBar2: TProgressBar;
Splitter1: TSplitter;
StatusBar1: TStatusBar;
StringGrid4: TStringGrid;
Summary: TLabel;
ListBox1: TListBox;
Memo1: TMemo;
Memo2: TMemo;
PageControl1: TPageControl;
PageControl2: TPageControl;
PageControl3: TPageControl;
PageControl4: TPageControl;
PageControl5: TPageControl;
Panel1: TPanel;
ProgressBar1: TProgressBar;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
StringGrid3: TStringGrid;
Summary1: TLabel;
Summary2: TLabel;
Summary3: TLabel;
Summary4: TLabel;
TabSheet1: TTabSheet;
TabSheet10: TTabSheet;
TabSheet11: TTabSheet;
TabSheet12: TTabSheet;
TabSheet13: TTabSheet;
TabSheet14: TTabSheet;
TabSheet15: TTabSheet;
TabSheet16: TTabSheet;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
TabSheet4: TTabSheet;
TabSheet5: TTabSheet;
TabSheet6: TTabSheet;
TabSheet7: TTabSheet;
TabSheet8: TTabSheet;
TabSheet9: TTabSheet;
TreeView1: TTreeView;
procedure btGetAccessClick(Sender: TObject);
procedure btGetContactsClick(Sender: TObject);
procedure btGetFileListClick(Sender: TObject);
procedure btSendMailClick(Sender: TObject);
procedure btRemoveTokensClick(Sender: TObject);
procedure btClearLogClick(Sender: TObject);
procedure btGetAppointmentsClick(Sender: TObject);
procedure btClearDebugClick(Sender: TObject);
procedure btnSimpleUploadClick(Sender: TObject);
procedure btnUploadWithResumeClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure ckHideFoldersClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure ListView1Click(Sender: TObject);
procedure ListView1DblClick(Sender: TObject);
procedure ListView2DblClick(Sender: TObject);
procedure MenuItem1Click(Sender: TObject);
procedure MenuItem2Click(Sender: TObject);
procedure MenuItem4Click(Sender: TObject);
procedure StringGrid1DblClick(Sender: TObject);
procedure StringGrid3KeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
procedure StringGrid4DblClick(Sender: TObject);
procedure TabSheet12Show(Sender: TObject);
procedure TreeView1Click(Sender: TObject);
procedure TreeView1SelectionChanged(Sender: TObject);
private
{ private declarations }
protected
procedure AfterShow(var Msg: TLMessage); message WM_AFTER_SHOW;
public
{ public declarations }
procedure ExporttoFile(Sender: TObject);
procedure AddToLog(Str: string);
procedure CheckTokenFile;
function GetJSONParam(filename, param: string): string;
procedure SetJSONParam(filename, param, Value: string);
procedure DeleteJSONPath(filename, param: string);
// function Download_Gdrive_File(id,auth, target: string): Boolean;
procedure FillDriveGrid_old;
procedure FillDriveView;
procedure UploadWithResume(fileid: string = ''; settings: TUploadSettings = []);
procedure FillDriveView2;
end;
var
Mainform: TMainform;
implementation
uses
DB,
google_oauth2,
google_calendar,
smtpsend,
httpsend,
synautil,
Windows,
comobj;
{$R *.lfm}
{ TMainform }
var
client_id: string = '504681931309-gc0n3bqtr0dgp6se1d7ee6pcean7heho.apps.googleusercontent.com';
client_secret: string = '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'; // only valid for my own test-user ( 2023-01-12 )
var
JDrive: Tgoogledrive;
function Areyousure: boolean;
var
i, j, k: integer;
var
s1, s2: string;
begin
randomize;
i := random(10);
j := random(10);
k := i + j;
s1 := IntToStr(k);
inputquery('Question', 'What is the result of ' + IntToStr(i) + ' + ' + IntToStr(j) + ' ?', s2);
if s1 <> s2 then
begin
ShowMessage('Not correct !!!');
Result := False;
end
else
Result := True;
end;
procedure TMainform.AddToLog(Str: string);
begin
Memo1.Lines.Add(Str);
end;
procedure TMainform.CheckTokenFile;
begin
if FileExists('tokens.dat') then // already tokens
begin
CheckGroup1.Enabled := False;
CheckGroup1.Caption := 'Access (scope) remove tokens.dat first to get new access';
btGetAccess.Caption := 'Check access';
end
else
begin
CheckGroup1.Enabled := True;
CheckGroup1.Caption := 'Access (scope)';
btGetAccess.Caption := 'Get access';
end;
end;
procedure TMainform.FormCreate(Sender: TObject);
var
Cfg: TJSONConfig;
begin
Memo1.Clear;
Memo2.Clear;
ListView1.Clear;
Treeview1.Items.Clear;
Cfg := TJSONConfig.Create(nil);
try
cfg.Filename:= 'client.json';
client_id := cfg.GetValue('installed/client_id', client_id);
client_secret := cfg.GetValue('installed/client_secret', client_secret);
finally
Cfg.Free;
end;
if Pos('504681931309', client_id) = 1 then // default client_id
begin
AddToLog('Using client_id from sourcecode (' + client_id + ')');
AddToLog('You need to create your own project and download the client.json');
AddToLog('See README.md for information');
end
else
begin
AddToLog('Using client_id from file client.json (' + client_id + ')');
end;
Jdrive := TGoogleDrive.Create(Self, client_id, client_secret);
Jdrive.Progress := ProgressBar1;
Jdrive.LogMemo := Memo1;
Width := round(Screen.Width * 0.6);
Height := round(Screen.Height * 0.9) - 100;
Top := 100;
edStart.Date := Now;
edEnd.Date := Now;
CheckGroup1.Checked[0] := True;
CheckGroup1.Checked[1] := True;
CheckGroup1.Checked[2] := True;
CheckGroup1.CheckEnabled[0] := False;
CheckGroup1.CheckEnabled[1] := False;
PageControl1.ActivePageIndex := 0;
CheckTokenFile;
end;
procedure TMainform.FormDestroy(Sender: TObject);
begin
Jdrive.Free;
end;
procedure TMainform.AfterShow(var Msg: TLMessage);
begin
//if FileExists('Pendingupload.txt') then
//begin
// PageControl1.ActivePage := TabSheet3;
// PageControl5.ActivePage := TabSheet12;
// btnUploadWithResume.Click;
//end;
end;
procedure TMainform.FormShow(Sender: TObject);
begin
PostMessage(Self.Handle, WM_AFTER_SHOW, 0, 0);
end;
function assignTgdexport(mimetype: string): tgdexportarray;
begin
setlength(Result, 0);
if mimeType='application/vnd.google-apps.document' then result := GoogleDocumentsExport;
if mimeType='application/vnd.google-apps.drawing' then result := GoogleDrawingsExport;
if mimeType='application/vnd.google-apps.spreadsheet' then result := GoogleSpreadsheetsExport;
if mimeType='application/vnd.google-apps.presentation' then result := GooglePresentationsExport;
end;
procedure TMainform.ExporttoFile(Sender: TObject);
var
FileId, mimetype, filename: string;
var
exp: tgdExportArray;
var
fileextension, exportmt: string;
var
index: integer;
begin
index := Listview1.ItemIndex;
if index < 0 then exit;
if Jdrive.gOAuth2.EMail = '' then exit;
FileId := JDrive.Files[index].fileid;
mimeType := JDrive.Files[index].mimeType;
FileName := JDrive.Files[index].Name;
exp := assignTgdexport(mimetype);
fileextension := exp[(Sender as tmenuitem).tag].FileExtension;
exportmt := exp[(Sender as tmenuitem).tag].MimeType;
JDrive.DownloadFile(fileid, filename + fileextension, '', exportmt);
end;
procedure TMainform.ListView1Click(Sender: TObject);
var
i: tmenuitem;
var
j, index: integer;
var
FileId, mimetype: string;
var
exp: tgdExportArray;
begin
index := Listview1.ItemIndex;
if index < 0 then exit;
if Jdrive.gOAuth2.EMail = '' then exit;
FileId := JDrive.Files[index].fileid;
mimeType := JDrive.Files[index].mimeType;
popupmenu1.Items[0].Enabled := True;
if Pos('application/vnd.google-apps', mimetype) > 0 then popupmenu1.Items[0].Enabled := False;
popupmenu1.Items[1].Enabled := False;
if FileId <> '' then
begin
if mimeType = 'application/vnd.google-apps.folder' then exit;
end;
exp := assignTgdexport(mimetype);
if length(exp) = 0 then exit;
with popupmenu1.Items[1] do
begin
Clear;
for j := 0 to length(exp) - 1 do
begin
i := Tmenuitem.Create(popupmenu1.Items[1]);
i.Caption := 'Export to ' + exp[j].Description;
i.Tag := j;
i.OnClick := @exporttofile;
popupmenu1.Items[1].Add(i);
end;
end;
popupmenu1.Items[1].Enabled := True;
end;
procedure TMainform.StringGrid1DblClick(Sender: TObject);
var
Filename: string;
FileId: string;
A: TGFileRevisions;
prop: TCustomproperties;
Rev: integer;
begin
if Jdrive.gOAuth2.EMail = '' then exit;
StringGrid4.Options := StringGrid4.Options + [goRowSelect];
Stringgrid4.colcount := 9;
Stringgrid4.rowcount := 1;
StringGrid4.Cells[1, 0] := 'Title';
StringGrid4.Cells[2, 0] := 'Created';
StringGrid4.Cells[3, 0] := 'Modified';
StringGrid4.Cells[4, 0] := 'Filename';
StringGrid4.Cells[5, 0] := 'Size';
StringGrid4.Cells[6, 0] := 'FileId';
StringGrid4.Cells[7, 0] := 'MimeType';
StringGrid4.Cells[8, 0] := 'RevisionId';
with TStringGrid(Sender) do
begin
FileId := cells[6, Row];
Filename := cells[4, Row];
if Filename = '' then Filename := cells[1, Row]; // title
end;
Filename := Extractfilepath(ParamStr(0)) + Filename;
// check for valid filename
try
with Jdrive do
begin
ClearAllCustomProperties;
AddCustomProperty(CustomBodyProperties, 'name', 'Filename Test', AsString);
AddCustomProperty(CustomBodyProperties, 'originalFilename', 'Filename Test', AsString);
AddCustomProperty(CustomBodyProperties, 'trashed', 'false');
AddCustomProperty(CustomBodyProperties, 'description', 'Desciption test', AsString);
AddCustomProperty(CustomBodyProperties, 'starred', 'true');
AddCustomProperty(CustomQueryProperties, 'keepRevisionForever', 'true');
SetFileProperties(Fileid);
end;
except
end;
try
// JDrive.DownloadFile(FileId, Filename);
A := JDrive.GetRevisions(FileId);
stringgrid4.rowcount := Length(A) + 1;
Memo1.Lines.add(IntToStr(length(A)) + ' revisions found');
if Length(A) > 0 then
for Rev := 0 to Length(A) - 1 do
begin
;
StringGrid4.Cells[8, Rev + 1] := A[Rev].revisionid;
StringGrid4.Cells[6, Rev + 1] := A[Rev].id;
StringGrid4.Cells[4, Rev + 1] := A[Rev].originalFileName;
StringGrid4.Cells[7, Rev + 1] := A[Rev].mimetype;
StringGrid4.Cells[3, Rev + 1] := A[Rev].modifiedTime;
Memo1.Lines.Add(A[Rev].revisionid + ' - ' + A[Rev].mimetype + ' - ' + A[Rev].modifiedTime);
end
else
Memo1.Lines.Add('no revisions');
except
ShowMessage('Could not save ' + Filename);
end;
end;
procedure TMainform.StringGrid3KeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
var
fileid, revisionid: string;
begin
if Jdrive.gOAuth2.EMail = '' then exit;
if key = VK_DELETE then
with TStringGrid(Sender) do
begin
FileId := cells[6, Row];
Revisionid := cells[8, Row];
end;
end;
procedure TMainform.StringGrid4DblClick(Sender: TObject);
var
fileid, revisionid, filename: string;
begin
with TStringGrid(Sender) do
begin
filename := cells[4, Row];
FileId := cells[6, Row];
revisionid := cells[8, Row];
end;
Jdrive.DownloadFile(fileid, filename, revisionid);
end;
procedure TMainform.btGetAccessClick(Sender: TObject);
var
gOAuth2: TGoogleOAuth2;
Scopes: GoogleScopeSet;
begin
// Onetime authentication
// Save tokens to tokens.dat
gOAuth2 := TGoogleOAuth2.Create(client_id, client_secret);
try
Scopes := [];
if CheckGroup1.Checked[2] then Include(Scopes, goMail);
if CheckGroup1.Checked[3] then Include(Scopes, goContacts);
if CheckGroup1.Checked[4] then Include(Scopes, goCalendar);
if CheckGroup1.Checked[5] then Include(Scopes, goDrive);
gOAuth2.LogMemo := Memo1;
gOAuth2.DebugMemo := Memo2;
gOAuth2.GetAccess(Scopes, True); // <- get from file
if gOAuth2.EMail <> '' then
begin
edSender.Text := format('%s <%s>', [gOAuth2.FullName, gOAuth2.EMail]);
if (edRecipient.Text = '') or (edRecipient.Text = 'recipient@valid_domain.com') then
edRecipient.Text := format('%s', [gOAuth2.EMail]);
end;
CheckTokenFile;
finally
gOAuth2.Free;
end;
end;
procedure TMainform.btGetContactsClick(Sender: TObject);
begin
// not implemented yet
end;
procedure TMainform.btRemoveTokensClick(Sender: TObject);
begin
if not FileExists('tokens.dat') then
begin
AddToLog('tokens.dat does not exist');
exit;
end;
Deletefile('tokens.dat');
if not FileExists('tokens.dat') then
AddToLog('tokens.dat deleted')
else
AddToLog('error while removing tokens.dat');
CheckTokenFile;
end;
// -----------------------------------------------------
// Little hack for TSMTPSend to give the command XOAUTH2
// -----------------------------------------------------
type
TmySMTPSend = class helper for TSMTPSend
public
function DoXOAuth2(const Value: string): boolean;
function ChallengeError(): string;
end;
function TmySMTPSend.DoXOAuth2(const Value: string): boolean;
var
x: integer;
s: string;
begin
Sock.SendString('AUTH XOAUTH2 ' + Value + CRLF);
repeat
s := Sock.RecvString(FTimeout);
if Sock.LastError <> 0 then
Break;
until Pos('-', s) <> 4;
x := StrToIntDef(Copy(s, 1, 3), 0);
Result := (x = 235);
end;
function TmySMTPSend.ChallengeError(): string;
var
s: string;
begin
Result := '';
Sock.SendString('' + CRLF);
repeat
s := Sock.RecvString(FTimeout);
if Sock.LastError <> 0 then
Break;
if Result <> '' then
Result := Result + CRLF;
Result := Result + s;
until Pos('-', s) <> 4;
end;
// -----------------------------------------------------
// -----------------------------------------------------
procedure TMainform.btSendMailClick(Sender: TObject);
var
gOAuth2: TGoogleOAuth2;
smtp: TSMTPSend;
msg_lines: TStringList;
begin
if (edRecipient.Text = '') or (edRecipient.Text = 'recipient@valid_domain.com') then
begin
Memo1.Lines.Add('Please change the recipient');
exit;
end;
if not FileExists('tokens.dat') then
begin
// first get all access clicked on Groupbox
btGetAccess.Click;
end;
gOAuth2 := TGoogleOAuth2.Create(client_id, client_secret);
smtp := TSMTPSend.Create;
msg_lines := TStringList.Create;
try
btSendMail.Enabled := False;
// first get oauthToken
gOAuth2.LogMemo := Memo1;
gOAuth2.DebugMemo := Memo2;
gOAuth2.GetAccess([], True); // <- get from file
// no need for scope because we should already have access
// via the btGetAccess for all the scopes in Groupbox
if gOAuth2.EMail = '' then
exit;
CheckTokenFile;
edSender.Text := format('%s <%s>', [gOAuth2.FullName, gOAuth2.EMail]);
msg_lines.Add('From: ' + edSender.Text);
msg_lines.Add('To: ' + edRecipient.Text);
msg_lines.Add('Subject: ' + edSubject.Text);
msg_lines.Add('');
msg_lines.Add(edBody.Text);
smtp.TargetHost := 'smtp.gmail.com';
smtp.TargetPort := '587';
AddToLog('SMTP Login');
if not smtp.Login() then
begin
AddToLog('SMTP ERROR: Login:' + smtp.EnhCodeString);
exit;
end;
if not smtp.StartTLS() then
begin
AddToLog('SMTP ERROR: StartTLS:' + smtp.EnhCodeString);
exit;
end;
AddToLog('XOAUTH2');
if not smtp.DoXOAuth2(gOAuth2.GetXOAuth2Base64) then
begin
AddToLog('XOAUTH2 ERROR: ' + CRLF + smtp.ChallengeError());
exit;
end;
AddToLog('SMTP Mail');
if not smtp.MailFrom(gOAuth2.EMail, Length(gOAuth2.EMail)) then
begin
AddToLog('SMTP ERROR: MailFrom:' + smtp.EnhCodeString);
exit;
end;
if not smtp.MailTo(edRecipient.Text) then
begin
AddToLog('SMTP ERROR: MailTo:' + smtp.EnhCodeString);
exit;
end;
if not smtp.MailData(msg_lines) then
begin
AddToLog('SMTP ERROR: MailData:' + smtp.EnhCodeString);
exit;
end;
AddToLog('SMTP Logout');
if not smtp.Logout() then
begin
AddToLog('SMTP ERROR: Logout:' + smtp.EnhCodeString);
exit;
end;
AddToLog('OK !');
finally
gOAuth2.Free;
smtp.Free;
msg_lines.Free;
btSendMail.Enabled := True;
end;
end;
procedure TMainform.btClearLogClick(Sender: TObject);
begin
Memo1.Clear;
end;
// Bubblesort Integer
const
// Define the Separator
TheSeparator = #254;
procedure BubbleSort_int(Items: TStrings);
var
done: boolean;
ThePosition, ThePosition2, i, n: integer;
TempString, TempString2, MyString, Mystring2, Dummy: string;
begin
n := Items.Count;
repeat
done := True;
for i := 0 to n - 2 do
begin
MyString := items[i];
MyString2 := items[i + 1];
ThePosition := Pos(TheSeparator, MyString);
ThePosition2 := Pos(TheSeparator, MyString2);
TempString := Copy(MyString, 1, ThePosition);
TempString2 := Copy(MyString2, 1, ThePosition2);
if AnsiCompareText(TempString, TempString2) < 0 then
begin
Dummy := Items[i];
Items[i] := Items[i + 1];
Items[i + 1] := Dummy;
done := False;
end;
end;
until done;
end;
procedure SortStringGrid(var GenStrGrid: TStringGrid; ThatCol: integer);
var
CountItem, I, J, K, ThePosition: integer;
MyList: TStringList;
MyString, TempString: string;
begin
// Give the number of rows in the StringGrid
CountItem := GenStrGrid.RowCount;
//Create the List
MyList := TStringList.Create;
MyList.Sorted := False;
try
begin
for I := 1 to (CountItem - 1) do
MyList.Add(GenStrGrid.Rows[I].Strings[ThatCol] + TheSeparator +
GenStrGrid.Rows[I].Text);
//Sort the List
//Mylist.Sort; INSTEAD
BubbleSort_int(Mylist);
for K := 1 to Mylist.Count do
begin
//Take the String of the line (K – 1)
MyString := MyList.Strings[(K - 1)];
//Find the position of the Separator in the String
ThePosition := Pos(TheSeparator, MyString);
TempString := '';
{Eliminate the Text of the column on which we have sorted the StringGrid}
TempString := Copy(MyString, (ThePosition + 1), Length(MyString));
MyList.Strings[(K - 1)] := '';
MyList.Strings[(K - 1)] := TempString;
end;
// Refill the StringGrid
for J := 1 to (CountItem - 1) do
GenStrGrid.Rows[J].Text := MyList.Strings[(J - 1)];
end;
finally
//Free the List
MyList.Free;
end;
end;
procedure TMainform.btGetAppointmentsClick(Sender: TObject);
var
Response: TStringList;
Q: integer;
StartDt: string;
EndDt: string;
nwWidth: integer;
ds: TGoogleCalendar;
begin
Response := TStringList.Create;
ds := TGoogleCalendar.Create(Self, client_id, client_secret);
try
btGetAppointments.Enabled := False;
ds.gOAuth2.LogMemo := Memo1;
ds.gOAuth2.DebugMemo := Memo2;
ds.gOAuth2.GetAccess([goCalendar], True);
CheckTokenFile;
if ds.gOAuth2.EMail = '' then
exit;
ds.Open;
ds.Populate();
StringGrid1.Options := StringGrid1.Options + [goRowSelect];
StringGrid1.ColCount := 5;
StringGrid1.RowCount := 2;
StringGrid1.Cells[1, 0] := 'Start';
StringGrid1.Cells[2, 0] := 'Eind';
StringGrid1.Cells[3, 0] := 'Afspraak';
StringGrid1.Cells[4, 0] := 'Link';
AddToLog('Busy filling grid');
SendMessage(StringGrid1.Handle, WM_SETREDRAW, 0, 0);
try
ds.First;
while not ds.EOF do
begin
with StringGrid1 do
begin
Cells[1, StringGrid1.RowCount - 1] := ds.FieldByName('start').AsString;
Cells[2, StringGrid1.RowCount - 1] := ds.FieldByName('end').AsString;
Cells[3, StringGrid1.RowCount - 1] := ds.FieldByName('summary').AsString;
Cells[4, StringGrid1.RowCount - 1] := ds.FieldByName('htmllink').AsString;
end;
for Q := 1 to 4 do
begin
nwWidth := StringGrid1.Canvas.TextWidth(
StringGrid1.Cells[Q, StringGrid1.RowCount - 1]);
if nwWidth > StringGrid1.ColWidths[Q] then
StringGrid1.ColWidths[Q] := nwWidth + 20;
end;
Application.ProcessMessages;
StringGrid1.RowCount := StringGrid1.RowCount + 1;
ds.Next;
end;
AddToLog('Sorting');
SortStringGrid(StringGrid1, 1);
StringGrid1.ColWidths[0] := 10;
StringGrid1.ColWidths[4] := 0; // <- also not -1
// StringGrid1.Columns[4].Visible := false; // <- why does this give an error ?
while (StringGrid1.RowCount > 2) and (StringGrid1.Cells[3, 1] = '') do
StringGrid1.DeleteRow(1);
AddToLog('Done filling grid');
finally
SendMessage(StringGrid1.Handle, WM_SETREDRAW, 1, 0);
StringGrid1.Repaint;
StringGrid1.SetFocus;
end;
finally
Response.Free;
ds.Free;
btGetAppointments.Enabled := True;
end;
end;
procedure TMainform.Button5Click(Sender: TObject);
var
gOAuth2: TGoogleOAuth2;
HTTP: THTTPSend;
URL: string;
json: TJSONObject;
dt_start, dt_end: TJSONObject;
begin
gOAuth2 := TGoogleOAuth2.Create(client_id, client_secret);
try
gOAuth2.LogMemo := Memo1;
gOAuth2.DebugMemo := Memo2;
gOAuth2.GetAccess([goCalendar], True);
CheckTokenFile;
if gOAuth2.EMail = '' then exit;
HTTP := THTTPSend.Create;
try
json := TJSONObject.Create;
dt_start := TJSONObject.Create;
dt_end := TJSONObject.Create;
try
json.Add('summary', edTitle.Text);
json.Add('location', edLocation.Text);
json.Add('description', edDescription.Text);
dt_start.Add('dateTime', FormatDateTime('yyyy-mm-dd', edStart.Date) + 'T' + FormatDateTime('hh:nn:ss', Now));
dt_start.Add('timeZone', 'Europe/Amsterdam');
dt_end.Add('dateTime', FormatDateTime('yyyy-mm-dd', edStart.Date) + 'T' + FormatDateTime('hh:nn:ss', Now));
dt_end.Add('timeZone', 'Europe/Amsterdam');
json.Add('start', dt_start);
json.Add('end', dt_end);
WriteStrToStream(HTTP.Document, ansistring(json.AsJSON));
finally
json.Free;
// dt_start.Free; nope, added to json
// dt_end.Free; nope, added to json
end;
URL := 'https://www.googleapis.com/calendar/v3/calendars/' + gOAuth2.EMail + '/events';
HTTP.Headers.Add('Authorization: Bearer ' + gOAuth2.Access_token);
HTTP.MimeType := 'application/json; charset=UTF-8';
if HTTP.HTTPMethod('POST', URL) then
begin
if HTTP.ResultCode = 200 then
Memo1.Lines.Add('event inserted')
else
Memo1.Lines.Add('error inserting');
Memo2.Lines.LoadFromStream(HTTP.Document);
end
else
begin
Memo1.Lines.Add('error');
Memo1.Lines.Add(HTTP.Headers.Text);
end;
finally
HTTP.Free;
end;
finally
gOAuth2.Free;
end;
end;
procedure TMainform.Button6Click(Sender: TObject);
begin
Jdrive.cancelcurrent := True;
end;
procedure TMainform.Button7Click(Sender: TObject);
begin
end;
procedure TMainform.ckHideFoldersClick(Sender: TObject);
begin
FillDriveGrid_old;
end;
procedure TMainform.FillDriveGrid_old;
var
Q: integer;
nwWidth: integer;
begin
StringGrid3.RowCount := 2;
AddToLog('Busy filling grid');
SendMessage(StringGrid3.Handle, WM_SETREDRAW, 0, 0);
try
Jdrive.First;
while not Jdrive.EOF do
begin
if not ckHideFolders.Checked or not Jdrive.FieldByName('IsFolder').AsBoolean then
begin
with StringGrid3 do
begin
Cells[1, StringGrid3.RowCount - 1] := Jdrive.FieldByName('title').AsString;
Cells[2, StringGrid3.RowCount - 1] := Jdrive.FieldByName('created').AsString;
Cells[3, StringGrid3.RowCount - 1] := Jdrive.FieldByName('modified').AsString;
Cells[4, StringGrid3.RowCount - 1] := Jdrive.FieldByName('filename').AsString;
Cells[5, StringGrid3.RowCount - 1] := Jdrive.FieldByName('filesize').AsString;
Cells[6, StringGrid3.RowCount - 1] := Jdrive.FieldByName('fileId').AsString;
Cells[7, StringGrid3.RowCount - 1] := Jdrive.FieldByName('mimeType').AsString;
if Jdrive.FieldByName('mimeType').AsString = 'application/vnd.google-apps.folder' then
Cells[7, StringGrid3.RowCount - 1] := '<dir>';
end;
StringGrid3.RowCount := StringGrid3.RowCount + 1;
end;
Jdrive.Next;
end;
if (StringGrid3.RowCount > 2) then
StringGrid3.RowCount := StringGrid3.RowCount - 1;
StringGrid3.AutoSizeColumns;
StringGrid3.ColWidths[0] := 10;