//******************************************************************************
//  XBMC USB Controller for Windows.
//  Copyright © 2011 Dilshan R Jayakody.
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//******************************************************************************

unit uSystem;

interface

uses
  Windows, Classes, SysUtils, IniFiles;

type
  TLogType = (lgError, lgInformation);
  TPlayMode = (plPlay, plStop, plPause, plIdle);
  TPlayerStage = (psAudio, psVideo, psPicture, psNone);

  PKeyInfo = ^TKeyInfo;
  TKeyInfo = record
    ID : String;
    Mode : Integer;
    Data : Integer;
  end;

  TXBMCInfo = record
    address : string;
    port : string;
  end;

  TUSBDeviceInfo = record
    VID : word;
    PID : word;
  end;

  TPlayerData = record
    Title : string;
    Attrib1 : string;
    PlayMode :TPlayMode;
    ActivePlayer : TPlayerStage;
  end;

  function Root() : string;
  function GetUSBDeviceInfo() : TUSBDeviceInfo;
  function GetXBMCConfigInfo() : TXBMCInfo;
  procedure ClearLog();
  procedure LogMsg(log_message : string; logmode : TLogType = lgInformation);
  function StartXBMC() : Boolean;

implementation

function Root() : string;
begin
  result := ExcludeTrailingBackslash(ExtractFilePath(ParamStr(0)));
end;

function GetUSBDeviceInfo() : TUSBDeviceInfo;
var config_ini : TIniFile;
begin
  config_ini := TIniFile.Create(Root+'\mcci.ini');
  result.PID := config_ini.ReadInteger('device','pid',1);
  result.VID := config_ini.ReadInteger('device','vid',1);
  freeandnil(config_ini);
end;

function GetXBMCConfigInfo() : TXBMCInfo;
var config_ini : TIniFile;
begin
  config_ini := TIniFile.Create(Root+'\mcci.ini');
  result.address := config_ini.ReadString('xbmc','address','127.0.0.1');
  result.port := config_ini.ReadString('xbmc','port','80');
  freeandnil(config_ini);
end;

procedure ClearLog();
var str1: TStringList;
begin
  str1 := TStringList.Create;
  str1.SaveToFile(Root + '\mcci.log');
  freeandnil(str1);
end;

procedure LogMsg(log_message : string; logmode : TLogType = lgInformation);
var str1: TStringList; time_, mode_ : String;
begin
  str1 := TStringList.Create;
  if FileExists(Root + '\mcci.log') then
    str1.LoadFromFile(Root + '\mcci.log');
  DateTimeToString(time_, 'yyyy-mm-dd hh:nn:ss', now);
  mode_ := 'ERROR: ';
  if(logmode = lgInformation)then
    mode_ := 'INFORMATION: ';
  str1.Add('['+time_+'] '+mode_+log_message);
  str1.SaveToFile(Root + '\mcci.log');
  freeandnil(str1);
end;

function StartXBMC() : Boolean;
var config_ini : TIniFile;
begin
  result := false;
  if(FindWindow('xbmc', nil)=0) then begin
    config_ini := TIniFile.Create(Root+'\mcci.ini');
    if fileexists(config_ini.ReadString('xbmc','location','')) then begin
      result := true;
      WinExec(pchar(config_ini.ReadString('xbmc','location','')),1);
    end;
    freeandnil(config_ini);
  end else
    result := true;
end;

end.
