RTL.TetheringIniFileStorage

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');
Note:
  • 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.

Delphi 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.

C++ Code

#include <fmx.h>
#include <System.IOUtils.hpp>
#include <System.Inifiles.hpp>
#include <System.Tether.Manager.hpp>
 
class TTetheringIniFileStorage : public System::Tether::Manager::TTetheringCustomStorage
{
  typedef System::Tether::Manager::TTetheringCustomStorage inherited;
 
private:
  const String RemoteSection;
  const String LocalSection;
  const String KeyIdentifier;
  System::Inifiles::TIniFile* FIniFile;
 
public:
  __fastcall TTetheringIniFileStorage(const System::Sysutils::TFileName AStorage);
  __fastcall virtual ~TTetheringIniFileStorage();
  virtual System::UnicodeString __fastcall LoadManagerGUID();
  virtual void __fastcall SaveManagerGUID(const System::UnicodeString AIdentifier);
  virtual void __fastcall LoadRemoteManagersGUIDs(System::Classes::TStringList* const AGUIDPassList);
  virtual void __fastcall SaveRemoteManagersGUIDs(System::Classes::TStringList* const AGUIDPassList);
};
 
__fastcall TTetheringIniFileStorage::TTetheringIniFileStorage(const System::Sysutils::TFileName AStorage):
       TTetheringCustomStorage(),
       RemoteSection("Remote Managers"), LocalSection("Local Managers"), KeyIdentifier("Identifier")
{
  FIniFile = new TIniFile(AStorage);
}
 
__fastcall TTetheringIniFileStorage::~TTetheringIniFileStorage()
{
  delete FIniFile;
}
 
System::UnicodeString __fastcall TTetheringIniFileStorage::LoadManagerGUID()
{
  System::UnicodeString Result = FIniFile->ReadString(LocalSection, KeyIdentifier, L"");
  if (Result.IsEmpty())
  {
    TGUID LGuid;
    CreateGUID(LGuid);
    Result = GUIDToString(LGuid);
    SaveManagerGUID(Result);
  }
  return Result;
}
 
void __fastcall TTetheringIniFileStorage::SaveManagerGUID(const System::UnicodeString AIdentifier)
{
  FIniFile->WriteString(LocalSection, KeyIdentifier, AIdentifier);
  FIniFile->UpdateFile();
}
 
void __fastcall TTetheringIniFileStorage::LoadRemoteManagersGUIDs(System::Classes::TStringList* const AGUIDPassList)
{
  FIniFile->ReadSectionValues(RemoteSection, AGUIDPassList);
}
 
void __fastcall TTetheringIniFileStorage::SaveRemoteManagersGUIDs(System::Classes::TStringList* const AGUIDPassList)
{
  if (AGUIDPassList != NULL)
  {
    FIniFile->EraseSection(RemoteSection);
 
    for (int i = 0; i < AGUIDPassList->Count; i++)
    if (!AGUIDPassList->Names[i].IsEmpty())
      FIniFile->WriteString(RemoteSection, AGUIDPassList->Names[i], AGUIDPassList->ValueFromIndex[i]);
    FIniFile->UpdateFile();
  }
}

Uses

See Also