RTL.TetheringIniFileStorage (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This unit defines an implementation of TTetheringCustomStorage that uses an INI file for persisting the pairing information of a tethering manager between sessions.

You can use this unit in your tethering-enabled applications to provide persistence for pairing information for your application.

To use this unit:

  1. Create a tethering-enabled application. See Adding App Tethering to Your Application.
  2. Add a new unit to your project with the code below. You can name this new unit TetheringIniFileStorage.pas.
  3. In the unit of your project that contains the TTetheringManager component:
    1. Add a reference to the TetheringIniFileStorage unit.
    2. Select your TTetheringManager component and, on the Object Inspector, double-click the OnRequestStorage event.
    3. Implement its event handler as follows:
AStorage := TTetheringIniFileStorage.Create(TPath.GetDocumentsPath + PathDelim + 'file.ini');
Notes:
  • The path to the INI file in the code above is just an example.
  • To use system path functions such as GetDocumentsPath, include a reference to the System.IOUtils unit in your uses clause.

Your TTetheringManager component now persists its data to the specified INI file. For more information, see TTetheringCustomStorage.

Code

unit TetheringIniFileStorage;

interface

uses
  System.Generics.Collections, System.IniFiles, System.SysUtils, System.Classes,
  System.Tether.Manager;

type

  TTetheringIniFileStorage = class(TTetheringCustomStorage)
  private
    const RemoteSection = 'Remote Managers';
    const LocalSection = 'Local Managers';
    const KeyIdentifier = 'Identifier';
  private
    FIniFile: TIniFile;
  public
    constructor Create(const AStorage: TFileName);
    destructor Destroy; override;
    function LoadManagerGUID: string; override;
    procedure SaveManagerGUID(const AIdentifier: string); override;
    procedure LoadRemoteManagersGUIDs(const AGUIDPassList: TStringList); override;
    procedure SaveRemoteManagersGUIDs(const AGUIDPassList: TStringList); override;
  end;

implementation

uses
  System.IOUtils;

{ TTetheringIniFileStorage }

constructor TTetheringIniFileStorage.Create(const AStorage: TFileName);
begin
  FIniFile := TIniFile.Create(string(AStorage));
end;

destructor TTetheringIniFileStorage.Destroy;
begin
  FIniFile.Free;
end;

function TTetheringIniFileStorage.LoadManagerGUID: string;
begin
  Result := FIniFile.ReadString(LocalSection, KeyIdentifier, '');
  if Result = '' then
  begin
    Result := TGUID.NewGuid.ToString;
    SaveManagerGUID(Result);
  end;
end;

procedure TTetheringIniFileStorage.LoadRemoteManagersGUIDs(const AGUIDPassList: TStringList);
begin
  FIniFile.ReadSectionValues(RemoteSection, AGUIDPassList);
end;

procedure TTetheringIniFileStorage.SaveManagerGUID(const AIdentifier: string);
begin
  FIniFile.WriteString(LocalSection, KeyIdentifier, AIdentifier);
  FIniFile.UpdateFile;
end;

procedure TTetheringIniFileStorage.SaveRemoteManagersGUIDs(const AGUIDPassList: TStringList);
var
  I: Integer;
begin

  if AGUIDPassList <> nil then
  begin
    FIniFile.EraseSection(RemoteSection);
    for I := 0 to AGUIDPassList.Count - 1 do
      if AGUIDPassList.Names[I] <> '' then
       FIniFile.WriteString(RemoteSection, AGUIDPassList.Names[I], AGUIDPassList.ValueFromIndex[I]);
    FIniFile.UpdateFile;
  end;
end;

end.

Uses

See Also