File: frmmain.pp

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (446 lines) | stat: -rw-r--r-- 10,769 bytes parent folder | download | duplicates (8)
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
unit frmmain;
// Define USESYNAPSE if you want to force use of synapse
{ $DEFINE USESYNAPSE}

// For version 2.6.4, synapse is the only option.
{$IFDEF VER2_6}
{$DEFINE USESYNAPSE}
{$ENDIF}

{$mode objfpc}{$H+}

interface

uses
  googlegmail, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ComCtrls, synautil, IniFiles, googlebase, googleservice, googleclient;

Const
  ILeaf      = 0;
  ILeafEmpty = 1;

type

  { TMainForm }
  TAccessTokenState = (acsWaiting,acsOK,acsCancel);

  TMainForm = class(TForm)
    BCancel: TButton;
    BSetAccess: TButton;
    BRefreshFolders: TButton;
    BRefreshFiles: TButton;
    EAccessCode: TEdit;
    GBAccess: TGroupBox;
    LLabels: TLabel;
    LVMessages: TListView;
    LMails: TLabel;
    LEAccess: TLabel;
    SDDownload: TSaveDialog;
    TVLabels: TTreeView;
    procedure BCancelClick(Sender: TObject);
    procedure BRefreshFilesClick(Sender: TObject);
    procedure BSetAccessClick(Sender: TObject);
    procedure BRefreshFoldersClick(Sender: TObject);
    Procedure DoUserConsent(Const AURL : String; Out AAuthCode : String) ;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure LVMessagesDblClick(Sender: TObject);
    procedure TVLabelsSelectionChanged(Sender: TObject);
  private
    { private declarations }
    FAccessState : TAccessTokenState;
    FClient : TGoogleClient;
    FGmailAPI: TGmailAPI;
    procedure AddLabels;
    procedure ClearMailListView;
    procedure ClearTreeView;
    function CreateNodeWithTextPath(TextPath: string): TTreeNode;
    procedure LoadAuthConfig;
    procedure SaveRefreshToken;
    procedure ShowLabel(ALabelID: String);
  public
    { public declarations }
  end;

var
  MainForm: TMainForm;

implementation


uses
  strutils,
  ssl_openssl,
  jsonparser, // needed
  fpjson,
  fpoauth2,
  lclintf,
  fpwebclient,
  {$IFDEF USESYNAPSE}
    ssl_openssl,
    synapsewebclient
  {$ELSE}
    fphttpwebclient
  {$ENDIF}
  ;

{$R *.lfm}

{ TMainForm }

procedure TMainForm.FormCreate(Sender: TObject);
begin
  // Register Tasks resources.
  TGmailAPI.RegisterAPIResources;
  // Set up google client.
  FClient:=TGoogleClient.Create(Self);
  {$IFDEF USESYNAPSE}
    FClient.WebClient:=TSynapseWebClient.Create(Self);
  {$ELSE}
    FClient.WebClient:=TFPHTTPWebClient.Create(Self);
  {$ENDIF}
  FClient.WebClient.RequestSigner:=FClient.AuthHandler;
  FClient.WebClient.LogFile:='requests.log';
  FClient.AuthHandler.WebClient:=FClient.WebClient;
  FClient.AuthHandler.Config.AccessType:=atOffLine;
  // We want to enter a code.
  FClient.OnUserConsent:=@DoUserConsent;
  // Create a Tasks API and connect it to the client.
  FGmailAPI:=TGmailAPI.Create(Self);
  FGmailAPI.GoogleClient:=FClient;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
end;

procedure TMainForm.LVMessagesDblClick(Sender: TObject);

Var
  Entry : TMessage;
  Request : TWebClientRequest;
  Response: TWebClientResponse;
  S,URL,LFN: String;
  D : TJSONEnum;
begin
  If Not (Assigned(LVMessages.Selected) and Assigned(LVMessages.Selected.Data)) then
    Exit;
  Entry:=TMessage(LVMessages.Selected.Data);
  // Add some code here to show the message
end;

procedure TMainForm.TVLabelsSelectionChanged(Sender: TObject);
begin
  BRefreshFilesClick(Sender)
end;

procedure TMainForm.LoadAuthConfig;

Var
  ini:TIniFile;

begin
  ini:=TIniFile.Create('google.ini');
  try
    // Registered application needs tasks scope
    FClient.AuthHandler.Config.ClientID:=ini.ReadString('Credentials','ClientID','');;
    FClient.AuthHandler.Config.ClientSecret:=ini.ReadString('Credentials','ClientSecret','');
    FClient.AuthHandler.Config.AuthScope:=ini.ReadString('Credentials','Scope','https://www.googleapis.com/auth/drive');
    // We are offline.
    FClient.AuthHandler.Config.RedirectUri:='urn:ietf:wg:oauth:2.0:oob';
    // Session data
    FClient.AuthHandler.Session.RefreshToken:=ini.ReadString('Session','RefreshToken','');
    FClient.AuthHandler.Session.AccessToken:=ini.ReadString('Session','AccesToken','');
    FClient.AuthHandler.Session.AuthTokenType:=ini.ReadString('Session','TokenType','');
    FClient.AuthHandler.Session.AuthExpires:=ini.ReadDateTime('Session','AuthExpires',0);
    FClient.AuthHandler.Session.AuthExpiryPeriod:=Ini.ReadInteger('Session','AuthPeriod',0);
  finally
    Ini.Free;
  end;
end;

procedure TMainForm.SaveRefreshToken;

Var
  ini:TIniFile;

begin
  // We save the refresh token for later use.
  if FClient.AuthHandler.Session.RefreshToken<>'' then
    begin
    ini:=TIniFile.Create('google.ini');
    try
      ini.WriteString('Session','RefreshToken',FClient.AuthHandler.Session.RefreshToken);
      ini.WriteString('Session','AccessToken',FClient.AuthHandler.Session.AccessToken);
      ini.WriteString('Session','TokenType',FClient.AuthHandler.Session.AuthTokenType);
      ini.WriteDateTime('Session','AuthExpires',FClient.AuthHandler.Session.AuthExpires);
      ini.WriteInteger('Session','AuthPeriod',FClient.AuthHandler.Session.AuthExpiryPeriod);
    finally
      Ini.Free;
    end;
    end;
end;

procedure TMainForm.ClearTreeView;

Var
  I : Integer;

begin
  With TVLabels.Items do
    begin
    BeginUpdate;
    try
      For I:=0 to Count-1 do
        TObject(Item[i].Data).Free;
      Clear;
    finally
      EndUpdate;
    end;
    end;
end;

function TMainForm.CreateNodeWithTextPath(TextPath: string): TTreeNode;

var
  p: SizeInt;
  CurText: String;
  AParent : TTreeNode;

begin
  Result:=nil;
  AParent:=Nil;
  repeat
    p:=System.Pos('/',TextPath);
    if p>0 then
      begin
      CurText:=LeftStr(TextPath,p-1);
      System.Delete(TextPath,1,p);
      end
    else
      begin
      CurText:=TextPath;
      TextPath:='';
      end;
    //debugln(['TTreeNodes.FindNodeWithTextPath CurText=',CurText,' Rest=',TextPath]);
    if AParent=nil then
      Result:=TVLabels.Items.FindTopLvlNode(CurText)
    else
      Result:=AParent.FindNode(CurText);
    if (Result=Nil) Then
      Result:=TVLabels.Items.AddChild(AParent,CurText);
    AParent:=Result;
  until (Result=nil) or (TextPath='');
end;

procedure TMainForm.AddLabels;

var
  EF,Entry: googlegmail.TLabel;
  Resource : TUsersLabelsResource;
  EN : String;
  List : TListLabelsResponse;
  i : integer;
  PN,N : TTreeNode;
  ShowThisLabel : boolean;

begin
  Resource:=Nil;
  List:=Nil;
  Entry:=Nil;
  try
    Resource:=FGmailAPI.CreateUsersLabelsResource(Self);
    // Search for folders of indicated folder only.
    List:=Resource.list('me');
    SaveRefreshToken;
    With TVLabels.Items do
      begin
      BeginUpdate;
      try
        I:=0;
        if Assigned(List) then
          for Entry in List.Labels do
            begin
            List.Labels[i]:=Nil;
            Inc(I);
            ShowThisLabel:=False;
            Case lowercase(Entry.labelListVisibility) of
              'labelhide' : ShowThisLabel:=False;
              'labelshow' : ShowThisLabel:=True;
              'labelshowifunread' : ShowThisLabel:=Entry.messagesUnread>0;
            end;
            if not ShowThisLabel then
              begin
              EF:=Entry;
              FreeAndNil(EF);
              end
            else
              begin
              N:=CreateNodeWithTextPath(Entry.Name);
              N.Data:=Entry;
              end;
            end;
      finally
        EndUpdate;
      end;
      end;
    Application.ProcessMessages;
  finally
    FreeAndNil(List);
    FreeAndNil(Resource);
  end;
end;

procedure TMainForm.BRefreshFoldersClick(Sender: TObject);


begin
  LoadAuthConfig;
  ClearTreeView;
  AddLabels;
  TVLabels.SortType:=stText;
end;

procedure TMainForm.BSetAccessClick(Sender: TObject);
begin
  FAccessState:=acsOK;
  GBAccess.Visible:=False;
end;

procedure TMainForm.BCancelClick(Sender: TObject);
begin
  FAccessState:=acsCancel;
  GBAccess.Visible:=False;
end;

procedure TMainForm.ClearMailListView;

Var
  I : Integer;

begin
  With LVMessages.Items do
    begin
    BeginUpdate;
    try
      For I:=0 to Count-1 do
        TObject(Item[i].Data).Free;
      Clear;
    finally
      EndUpdate;
    end;
    end;
end;

procedure TMainForm.BRefreshFilesClick(Sender: TObject);

begin
  if (TVLabels.Selected=Nil) or (TVLabels.Selected.Data=Nil) then
    ShowLabel('root')
  else
    ShowLabel(googlegmail.TLabel(TVLabels.Selected.Data).id);
end;

procedure TMainForm.ShowLabel(ALabelID: String);

Type
  TMailDescription = Record
    Subject : String;
    Sender : String;
    From : String;
    Recipient : String;
    Received : String;
    Snippet : String;
  end;

  Procedure CreateDesc(E : TMessage; var Desc : TMailDescription);

  Var
    H : TMessagePartHeader;

  begin
    Desc.Subject:='';
    Desc.Sender:='';
    Desc.Received:='';
    Desc.from:='';
    Desc.Recipient:='';
    Desc.Snippet:=E.snippet;
    If Assigned(E.payload) then
      For H in E.payload.headers do
        Case LowerCase(h.name) of
          'subject' : Desc.Subject:=H.value;
          'sender' : Desc.Sender:=H.value;
          'received' : Desc.Received:=H.Value;
          'date' : Desc.Received:=H.Value;
          'from' : Desc.from:=H.Value;
          'to' : Desc.Recipient:=H.Value;
        end;
  end;

var
  Msg,Entry: Tmessage;
  EN : String;
  i:integer;
  Q : TUsersMessagesListOptions;
  Resource : TUsersMessagesResource;
  List : TListMessagesResponse;
  LI : TListItem;
  Desc : TMailDescription;

begin
  ClearMailListView;
  Resource:=Nil;
  try
    Resource:=FGmailAPI.CreateusersMessagesResource(Self);
    // Search for files of indicated folder only.
    Q.labelIds:=ALabelID;
    List:=Resource.list('me',Q);
    SaveRefreshToken;
    With LVMessages.Items do
      begin
      BeginUpdate;
      try
        Clear;
        if Assigned(List) then
          for Msg in List.messages do
            begin
            Entry:=Resource.Get(Msg.id,'me','format=full');
            LI:=Add;
            CreateDesc(Entry,Desc);
            LI.Caption:=Desc.Subject;
            With LI.SubItems do
              begin
              Add(Desc.From);
              Add(Desc.Received);
              Add(Desc.Recipient);
              Add(Desc.Sender);
              Add(Desc.Snippet);
              end;
            Li.Data:=Entry;
            Application.ProcessMessages;
            end;
      finally
        EndUpdate;
      end;
      end;
  Finally
    Resource.Free;
  end;
end;

Procedure TMainForm.DoUserConsent(Const AURL: String; Out AAuthCode: String);

begin
  GBAccess.Visible:=True;
  EAccessCode.Text:='<enter code here>';
  FAccessState:=acsWaiting;
  OpenUrl(AURL);
  While (FAccessState=acsWaiting) do
    Application.ProcessMessages;
  if FAccessState=acsOK then
    AAuthCode:=EAccessCode.Text;
  GBAccess.Visible:=False;
end;

end.