unit SunriseChatCoreUtils;

interface

uses
  Windows, SysUtils, WinSock;

type
  TArrayOfString = array of string;

  function LocalHostName: string;
  function GetUserName: string;
  function Explode(Separator: Char; Data: string): TArrayOfString;
  function SecondsIdle: Cardinal;
  function GetWindowsVersionStr: string;

implementation

var
  GetLastInputInfo2: function(var plii: TLastInputInfo): BOOL; stdcall;
  LibHandle: HInst;   //dll handle

function GetWindowsVersionStr: string;
begin
  case Win32Platform of
    1: case Win32MajorVersion of
      4: case Win32MinorVersion of
        0: Result:= '95';
        10: Result:= '98';
        90: Result:= 'Me';
      end;
    end;
    2: case Win32MajorVersion of
      4: case Win32MinorVersion of
        0: Result:= 'NT 4.0';
      end;
      5: case Win32MinorVersion of
        0: Result:= '2000';
        1: Result:= 'XP';
        2: Result:= 'Server 2003';
      end;
      6: case Win32MinorVersion of
        0: Result := 'Vista';
      end
    end;
  end;
  Result:= 'Windows ' + Result;
end;

function GetUserName: string;
const
  MaxUsernameLength = 256;
var
  L : LongWord;
begin
  L := MaxUsernameLength + 2;
  SetLength(Result, L);
  if Windows.GetUserName(PChar(Result), L) and (L > 0) then
    SetLength(Result, StrLen(PChar(Result))) else Result := '';
end;

function Explode(Separator: Char; Data: string): TArrayOfString;
begin
  SetLength(Result, 0);
  while Pos(Separator, Data) > 0 do begin
    SetLength(Result, Length(Result) + 1);
    Result[High(Result)] := Copy(Data, 1, Pos(Separator, Data) - 1);
    Delete(Data, 1, Pos(Separator, Data));
  end;
  SetLength(Result, Length(Result) + 1);
  Result[High(Result)] := Data;
end;

function SecondsIdle: Cardinal;
var
  liInfo: TLastInputInfo;
begin
  // Try to load indirectly function GetLastInputInfo for compatibility with Win9x
  LibHandle := LoadLibrary('user32.dll');
  if LibHandle = 0 then begin
    GetLastInputInfo2 := nil;
  end else begin
    @GetLastInputInfo2 := GetProcAddress(LibHandle,'GetLastInputInfo');
    if (@GetLastInputInfo2 = nil) then FreeLibrary(LibHandle);
  end;

  // Only WinNT+
 if Assigned(GetLastInputInfo2) then begin
    liInfo.cbSize := SizeOf(TLastInputInfo) ;
    GetLastInputInfo2(liInfo) ;
    Result := (GetTickCount - liInfo.dwTime) DIV 1000;
  end else Result:= 0;
//  MainWindow.RichEdit1.Lines.Add(IntToStr(Result));
//  SysUtils.Beep;
//  Result:= IdleTrackerGetLastTickCount div 1000;
//  Button1.Caption:= IntToStr(Result);
//  Result:= 0;
end;

function LocalHostName: String;
var
  Buf: array[0..255] of Char;
begin
//  if gethostname(@Buf, Sizeof(Buf)) <> 0 then
//    raise Exception.Create('LocalHostName not available');
//  Result := PChar(@Buf);
  Result := '';
end;

end.
