
{                                                                              }
{  IB_Samples                                                                  }
{                                                                              }

{***************************************************************}
{                                                               }
{          IB Objects Components and Resources Library          }
{                                                               }
{          Copyright (C) 1996-2007 Jason Wharton                }
{          and Computer Programming Solutions, Mesa, AZ         }
{                                                               }
{  This source code unit is released under the terms of the     }
{  CPS Trustware License.                                       }
{  It may be distributed or deployed as source code or in       }
{  compiled form only in compliance with the terms and          }
{  conditions of the CPS Trustware License which may be         )
{  examined at http://www.ibobjects.com/ibo_trustware.html      )
{                                                               }
{***************************************************************}

{******************************************************************************}
{  CONTRIBUTED MODIFICATIONS                                                   }
{  Additions or modifications listed below using format:                       }
{                                                                              }
{  Joe Citizen <joe@joecitizen.com>                                            }
{  mm/dd/yyyy                                                                  }
{     xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                 }
{     xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                 }
{     xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx                 }
{            Copyright (C) 2001 Joe Citizen                                    }
{                                                                              }
{------------------------------------------------------------------------------}
{  Place your credits and information in the top of the section                }
{  below with any other descriptions or explanations desired.                  }
{------------------------------------------------------------------------------}
{                                                                              }
{  Ramil Khabibullin <khabibr@mail.ru>                                         }
{  05/02/2006                                                                  }
{                                                                              }
{            Copyright (C) 2006 Ramil Khabibullin                              }
{                                                                              }
{******************************************************************************}

unit
  IB_Samples;

interface

uses
  Registry,
  IniFiles,
  SysUtils,
  IB_Constants,
  IB_Utils,
  Windows,
  IB_Session,

  IB_Components;

const
  DefaultConnectSection='DefaultConnection';
  DefaultPasswordIdent='JumblePassword';
  DefaultServerIdent='DefaultServer';

procedure ReadDefaultPath(  IB_Connection: TIB_Connection;
                            const BaseIdent: string );
procedure WriteDefaultPath;
function DefaultMacroSubstitute( const ATextBlock: string;
                                 var   ATextResult: string ): boolean;
procedure SetConnection( const IB_Connection: TIB_Connection;
                         const BaseIdent: string );
function NeedToCreateDataBase: Boolean;
                            
implementation

var
  DefaultBaseIdent: String;
  DefaultIB_Connection: TIB_Connection;

procedure SetConnection( const IB_Connection: TIB_Connection;
                         const BaseIdent: String);
begin
  DefaultIB_Connection := IB_Connection;
  DefaultBaseIdent     := BaseIdent;
end;


procedure ReadDefaultPath(  IB_Connection: TIB_Connection;
                            const BaseIdent: String);
var
  MyReg: TRegINIFile;
  MyIni: TIniFile;
  DefServer,
  DefDataBase,
  JumPwd: String;
begin
  if not Assigned( IB_Connection ) then
    raise EIB_StatementError.Create( E_NO_CONNECTION );
  SetConnection( IB_Connection, BaseIdent );

  // try to read from registry
  MyReg := TRegIniFile.Create( IBO_BASEKEY );
  try
    DefDataBase := MyReg.ReadString( DefaultConnectSection, DefaultBaseIdent, '' );
    DefServer := MyReg.ReadString( DefaultConnectSection, DefaultServerIdent, '' );
    JumPwd := MyReg.ReadString( DefaultConnectSection, DefaultPasswordIdent, '' )
  finally
    MyReg.Free;
  end;

  // try to read from INI file
  MyIni := TIniFile.Create('..\..\SamplesConfig.ini');
  try
    if DefDataBase = '' then
      DefDataBase := MyIni.ReadString( DefaultConnectSection, DefaultBaseIdent, '');
    if DefServer = '' then
      DefServer := MyIni.ReadString(DefaultConnectSection, DefaultServerIdent, '');
    if JumPwd = '' then
      JumPwd := MyIni.ReadString(DefaultConnectSection, DefaultPasswordIdent, '');
  finally
    MyIni.Free;
  end;

  with IB_Connection do
  begin
    if DefDataBase <> '' then
      Database := DefDataBase
    else
      Database := ExpandFileName( Path );

    if ( DefServer <> '' ) and ( Protocol = cpLocal ) then
      Database := DefServer + ':' + Path;

    if JumPwd <> '' then
      Password := UnJumbleString( JumPwd, GetJumbleKey );
  end;
end;

//---------------------------------------------------------------------------

procedure WriteDefaultPath;
var
  MyReg: TRegINIFile;
begin
  if not Assigned( DefaultIB_Connection ) then
    raise EIB_StatementError.Create( E_NO_CONNECTION );
    
  MyReg := TRegIniFile.Create( IBO_BASEKEY );
  try
    with DefaultIB_Connection do
    begin
      MyReg.WriteString( DefaultConnectSection, DefaultBaseIdent, Database );
      MyReg.WriteString( DefaultConnectSection, DefaultPasswordIdent,
        JumbleString( Password, GetJumbleKey) );
    end;
  finally
    MyReg.Free;
  end;
end;

//---------------------------------------------------------------------------

function DefaultMacroSubstitute( const ATextBlock: String;
                                   var ATextResult: String ): Boolean;
begin
  if not Assigned( DefaultIB_Connection ) then
    raise EIB_StatementError.Create( E_NO_CONNECTION );

  Result:=True;
  if ATextBlock = 'basepath' then
    ATextResult := '''' + DefaultIB_Connection.Database + ''''
  else
  if ATextBlock = 'password' then
    ATextResult := '''' + DefaultIB_Connection.Password + ''''
  else
  begin
    Result := False;
    ATextResult := ATextBlock;
  end;
end;

function NeedToCreateDataBase: Boolean;
var
  LocalMachine: array [0..MAX_COMPUTERNAME_LENGTH] of char;
  Size: DWORD;
  tmpF: string;
begin
  Size := Sizeof( LocalMachine );
  if not GetComputerName( LocalMachine, Size ) then
    LocalMachine := '';

  Result := false;  
  with DefaultIB_Connection do
    if ( Protocol = cpLocal ) or
       ( UpperCase(Server) = 'LOCALHOST' ) or
       ( UpperCase(Server) = LocalMachine ) then
    begin
      if not FileExists( Path ) then
      begin
        tmpF := ExtractFileName( Path );
        tmpF := ExpandFileName( tmpF );
        if FileExists( tmpF ) then
          Path := tmpF
        else
          Result := true; 
      end;
    end;
end;


end.
