Your First Application (Google Drive)

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

Your First Application (Google Drive)

Previous pageReturn to chapter overviewNext page

This topic describes creation of a very simple application, which will show Google Drive user's file tree in TMemo control. The topic is written for new NG-ConnectionPack users, and the application is simplified as much as possible. For more advanced, real-world apps, please refer to NG-ConnectionPack demos.

 

1. General Google Drive Access

 

First of all, please make sure that you have Google account, and can access Google Drive via its primary web interface:

 

https://drive.google.com

 

 

For testing purposes please make sure, that some files are available in your Google Drive. We uploaded NG-ConnectionPack sources, for example.

 

2. Registering your Application

 

Please follow the procedure, described in Google Registration topic. Don't forget to enable Google Drive API:

 

 

3. Creating VCL Forms Application

 

The next step will be to create new VCL application in Delphi. Run Delphi IDE, create new VCL application, as usual, and place TLabel, TMemo, TButton and TNGGDrive components, like shown below:

 

 

We will use the memo to show downloaded files tree, and the label to show downloading progress. As a result of application registration, performed in the previous step, you received ClientID and Secret string values. Please copy-paste them to the corresponding properties of NGGDrive1 component using Delphi's Object Inspector:

 

 

Double click the button on the form to create its OnClick event handler and write the following code in it:

 

procedure TForm3.Button1Click(Sender: TObject);
begin
  FFileCount := 0;
 
  Label1.Caption := 'Working...';
  Label1.Refresh;
 
  NGGDrive1.Root.Files(FilesProc);
 
  Label1.Caption := 'Done.';
end;

 

Declare FFileCount, FIdent fields and FilesProc method in the form's class as shown below:

 

type
  TForm3 = class(TForm)
    Label1: TLabel;
    Memo1: TMemo;
    Button1: TButton;
    NGGDrive1: TNGGDrive;
    procedure Button1Click(Sender: TObject);
  private
    FFileCount: Integer;
    FIdent: string;
    procedure FilesProc(F: TNGCloudFile);
  end;

 

Press Ctrl+Shift+C to auto-create FilesProc body in the implementation section, and write the following code in it:

 

procedure TForm3.FilesProc(F: TNGCloudFile);
var
  oldspc: string;
begin
  Memo1.Lines.Add(FSpaces + F.Name);
 
  if F.IsFolder then
  begin
    oldspc  := FSpaces;
    FSpaces := FSpaces + '    ';
 
    F.Files(FilesProc); // Retrieve child files.
    FSpaces := oldspc;
  end;
 
  Inc(FFileCount);
  if (FFileCount mod 10) = 0 then // Speed up.
  begin
    Label1.Caption := Format('Retrieved %d files', [FFileCount]);
    Label1.Refresh;
  end;
end;

 

Note, that this callback procedure will be called for each file. In the procedure body the file name is added in the Memo1, and if the file is a folder, the process is executed for its child files recursively.

 

The progress label is also updated, but to speed up things, the label is updated only for each 10'th file.

 

4. Running the Application

 

Thats all! You can run the application. Since the authentication state is not saved/loaded for simplicity, the application will ask to authenticate the user at every run. Use your own Google Account credentials to grant the application rights to your Google Drive data. The application, then will retrieve full file tree of your Google Drive files:

 

 

Congratulations! You've created your first application, which interacts with web based REST service via Internet, using LMD NG-ConnectionPack components.