Unit MainForm;

{
  Tento soubor implementuje obsluhu hlavniho formulare programu. Stara
  se o inicializaci (nacteni ovladace do pameti a nacteni jmenneho prostoru
  spravce objektu), zobrazovani a uklid (uvolneni ovladace objview.sys p
  pameti).
}

Interface

Uses
  Windows, Messages, SysUtils, Variants, Classes,
  Graphics, Controls, Forms, Dialogs, ComCtrls,
  StdCtrls, DirectoryTree, Menus;

Type
  TForm1 = Class (TForm)
    DirTreeView: TTreeView;
    DirContentListView: TListView;
    MainMenu1: TMainMenu;
    Action1: TMenuItem;
    Refresh1: TMenuItem;
    N1: TMenuItem;
    Exit1: TMenuItem;
    DirContentPopupMenu: TPopupMenu;
    ObjectInfoItem: TMenuItem;
    DirTreePopupMenu: TPopupMenu;
    ObjectInfo2: TMenuItem;
    Procedure FormClose(Sender: TObject; var Action: TCloseAction);
    Procedure FormCreate(Sender: TObject);
    Procedure DirTreeViewClick(Sender: TObject);
    Procedure Exit1Click(Sender: TObject);
    Procedure Refresh1Click(Sender: TObject);
    Procedure DirContentListViewCompare(Sender: TObject; Item1, Item2: TListItem;
      Data: Integer; var Compare: Integer);
    Procedure DirContentListViewColumnClick(Sender: TObject; Column: TListColumn);
    Procedure DirTreeViewCompare(Sender: TObject; Node1, Node2: TTreeNode;
      Data: Integer; var Compare: Integer);
    procedure ObjectInfoItemClick(Sender: TObject);
    procedure ObjectInfo2Click(Sender: TObject);
  Private
    FSortColumn : Integer;
    FSortOrder : (stAscend, stDescend);
    Procedure BuildDirectoryTree;
    Procedure WalkDirectoryTree(Entry:PTreeEntry; Node:TTreeNode);
    Procedure ShowDirectoryContents(Directory:PTreeEntry);
  end;

Var
  Form1: TForm1;

Implementation

{$R *.DFM}

Uses
  Objects, Utils, ObjectInfoForm, DriverCommands;

Procedure TForm1.ShowDirectoryContents(Directory:PTreeEntry);
Var
  I : Integer;
begin
DirContentListView.Clear;
DirContentListView.Items.BeginUpdate;
for I := Low(Directory.Childs) To High(Directory.Childs) Do
  begin
  With DirContentListView.Items.Add Do
    begin
    Caption := Directory.Childs[I].Name;
    SubItems.Add(Directory.Childs[I].TypeName);
    Data := Pointer(Directory.Childs[I]);
    end;
  end;

DirContentListView.Items.EndUpdate;
end;

Procedure TForm1.WalkDirectoryTree(Entry:PTreeEntry; Node:TTreeNode);
Var
  EntryNode : TTreeNode;
  I : Integer;
begin
For I := Low(Entry.Childs) To High(Entry.Childs) Do
  begin
  If Entry.Childs[I].TypeName = 'Directory' Then
    begin
    EntryNode := DirTreeView.Items.AddChild(Node, Entry.Childs[I].Name);
    EntryNode.Data := Pointer(Entry.Childs[I]);
    WalkDirectoryTree(Entry.Childs[I], EntryNode);
    end;
  end;
end;

Procedure TForm1.BuildDirectoryTree;
Var
  RootEntry : PTreeEntry;
  RootNode : TTreeNode;
  Ret : Boolean;
begin
DirTreeView.Items.Clear;
Ret := DirectoryTreeInit;
If Ret Then
  begin
  RootEntry := DirectoryTreeRoot;
  RootNode := DirTreeView.Items.AddChild(Nil, RootEntry.Name);
  RootNode.Data := Pointer(RootEntry);
  Ret := Assigned(RootNode);
  If Ret Then
    WalkDirectoryTree(RootEntry, RootNode);
  end;
end;

Procedure TForm1.Exit1Click(Sender: TObject);
begin
Close;
end;

Procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ObjviewUnload;
ObjviewFinit;
end;

Procedure TForm1.FormCreate(Sender: TObject);
Var
  Ret : Boolean;
  X : TStrings;
  I : Integer;
begin
FSortColumn := 0;
FSortOrder := stAscend;
Ret := ObjViewInit;
If Ret Then
  begin
  Ret := ObjviewLoad;
  If Ret Then
    Refresh1Click(Nil)
  Else Error(Format('Failed to load the driver. Error: %s.', [SysErrorMessage(GetLastError)]));
  end;
end;

Procedure TForm1.ObjectInfo2Click(Sender: TObject);
Var
  Entry : PTreeEntry;
  SelectedNode : TTreeNode;
  InfoForm : TForm2;
begin
SelectedNode := DirTreeView.Selected;
If Assigned(SelectedNode) Then
  begin
  Entry := PTreeEntry(SelectedNode.Data);
  Try
    InfoForm := TForm2.Create(Application, Entry.FullName);
    InfoForm.ShowModal;
    InfoForm.Free;
  Except
    Error(Format('Nepodarilo se ziskat informace o objektu %s', [Entry.FullName]));
    end;
  end;
end;

procedure TForm1.ObjectInfoItemClick(Sender: TObject);
Var
  Entry : PTreeEntry;
  InfoForm : TForm2;
  SelectedItem : TListItem;
begin
SelectedItem := DirContentListView.Selected;
If Assigned(SelectedItem) Then
  begin
  Entry := PTreeEntry(SelectedItem.Data);
  Try
    InfoForm := TForm2.Create(Application, Entry.FullName);
    InfoForm.ShowModal;
    InfoForm.Free;
  Except
    Error(Format('Nepodarilo se ziskat informace o objektu %s', [Entry.FullName]));
    end;
  end;
end;

Procedure TForm1.DirContentListViewColumnClick(Sender: TObject; Column: TListColumn);
begin
DirContentListView.SortType := stNone;
If Column.Index <> FSortColumn Then
  begin
  FSortColumn := Column.Index;
  FSortOrder := stAscend;
  end
Else begin
  If FSortOrder = stAscend Then
    FSortOrder := stDescend
  Else FSortOrder := stAscend;
  end;

DirContentListView.SortType := stText;
end;

Procedure TForm1.DirContentListViewCompare(Sender: TObject; Item1, Item2: TListItem;
  Data: Integer; var Compare: Integer);
Var
  D1, D2 : PTreeEntry;
begin
D1 := PTreeEntry(Item1.Data);
D2 := PTreeEntry(Item2.Data);
Case FSortColumn Of
  0 : begin
    If FSortOrder = stAscend Then
      Compare := StrComp(PWideChar(D1.Name), PWideChar(D2.Name))
    Else Compare := -StrComp(PWideChar(D1.Name), PWideChar(D2.Name))
    end;
  1 : begin
    If FSortOrder = stAscend Then
      Compare := StrComp(PWideChar(D1.TypeName), PWideChar(D2.TypeName))
    Else Compare := -StrComp(PWideChar(D1.TypeName), PWideChar(D2.TypeName))
    end;
  end;
end;

Procedure TForm1.Refresh1Click(Sender: TObject);
begin
BuildDirectoryTree;
DirTreeView.Select(DirTreeView.Items[0]);
DirTreeViewClick(Nil);
DirTreeView.CustomSort(Nil, 0);
end;

Procedure TForm1.DirTreeViewClick(Sender: TObject);
Var
  SelectedNode : TTreeNode;
begin
SelectedNode := DirTreeView.Selected;
If Assigned(SelectedNode) Then
  ShowDirectoryContents(PTreeEntry(SelectedNode.Data));
end;

Procedure TForm1.DirTreeViewCompare(Sender: TObject; Node1, Node2: TTreeNode;
  Data: Integer; var Compare: Integer);
Var
  D1, D2 : PTreeEntry;
begin
D1 := PTreeEntry(Node1.Data);
D2 := PTreeEntry(Node2.Data);
Compare := StrComp(PWideChar(D1.Name), PWideChar(D2.Name));
end;

End.

