Table of Contents

Files and folders

Once connected, TTMSFNCCloudGoogleDrive browses the account's files and folders. Drive addresses every item by a unique ID rather than a path, and every request is asynchronous: you call a method, the component performs the HTTP request in the background, and the result arrives in a completion event carrying a TTMSFNCCloudBaseRequestResult. This chapter covers listing, reading item metadata, and that event-driven result model.

The async result model

No Drive method blocks waiting for the network. Each one has a matching On... event that fires when the request finishes; check ARequestResult.Success first, and read ARequestResult.ResultString for the raw response or error detail. Returned items are TTMSFNCCloudGoogleDriveItem objects exposing FileName, ID, Size, MimeType, Description, Shared and the link properties.

Listing a folder

GetFolderList lists a folder — pass nil (or no argument) for the account root, or a folder item to list its contents. The items arrive in OnGetFolderList.

procedure TForm1.ListRootFolder;
begin
  TMSFNCCloudGoogleDrive1.OnGetFolderList := DriveFolderListed;
  TMSFNCCloudGoogleDrive1.GetFolderList;   // pass nil / no argument for the root
end;

procedure TForm1.DriveFolderListed(Sender: TObject;
  const AFolderList: TTMSFNCCloudItems;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
var
  i: Integer;
begin
  if not ARequestResult.Success then
  begin
    ShowMessage('Drive error: ' + ARequestResult.ResultString);
    Exit;
  end;

  ListBox1.Clear;
  for i := 0 to AFolderList.Count - 1 do
    // Drive items are addressed by ID, not path.
    ListBox1.Items.AddObject(AFolderList[i].FileName, TObject(AFolderList[i]));
end;

For a recursive tree, GetFolderListHierarchical walks subfolders and reports completion through OnGetFolderListComplete. GetFileByID resolves a single item from its Drive ID.

Reading item metadata

GetFileInfo (and GetFolderInfo) request metadata for one item by its Drive ID; the result arrives in OnGetFileInfo as a TTMSFNCCloudGoogleDriveItem. The item's MimeType tells you whether it is a binary file or a Google-native document (which must be exported rather than downloaded — see Uploading and exporting).

procedure TForm1.ShowFileInfo(const AFileID: string);
begin
  TMSFNCCloudGoogleDrive1.OnGetFileInfo := DriveFileInfo;
  TMSFNCCloudGoogleDrive1.GetFileInfo(AFileID);   // Drive uses item IDs
end;

procedure TForm1.DriveFileInfo(Sender: TObject;
  const AFile: TTMSFNCCloudGoogleDriveItem;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    Memo1.Lines.Add(Format('%s — %s, %d bytes',
      [AFile.FileName, AFile.MimeType, AFile.Size]));
end;

Combining a listing with a follow-up request

Because each step is event-driven, you chain operations by starting the next one from the previous completion event. This lists the root, then requests info for the first item by its ID once the listing arrives:

procedure TForm1.BrowseAndInspect;
begin
  TMSFNCCloudGoogleDrive1.OnGetFolderList := ListThenInspect;
  TMSFNCCloudGoogleDrive1.OnGetFileInfo := DriveFileInfo;
  TMSFNCCloudGoogleDrive1.GetFolderList;   // root
end;

procedure TForm1.ListThenInspect(Sender: TObject;
  const AFolderList: TTMSFNCCloudItems;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if not ARequestResult.Success then
    Exit;

  // Use the item's Drive ID to request its full metadata.
  if AFolderList.Count > 0 then
    TMSFNCCloudGoogleDrive1.GetFileInfo(AFolderList[0].ID);
end;

procedure TForm1.DriveFileInfo(Sender: TObject;
  const AFile: TTMSFNCCloudGoogleDriveItem;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  if ARequestResult.Success then
    Memo1.Lines.Add(AFile.FileName + ' (' + AFile.MimeType + ')');
end;

Common mistakes

  • Using a path where an ID is expected. Drive operations take item IDs, not paths — keep the ID from a listing or search result to address an item later.
  • Reading results outside the event. A method returns before the network call completes — use the On... event, not the method's return value, and always check ARequestResult.Success.

See also