unit plugmgr;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, ComCtrls, StdCtrls,
  CM2GenPlugTypes, CM2VisPlugTypes, CM2MMRB, PlugInInfoDlg, CM2RegisterTools,
  EasyReg, ImgList, Menus, CM2CMLxPlugTypes, InstallingPlugInForm, Registry, ShellApi;

type
  TPlugMgrDlg = class(TForm)
    ListView1: TListView;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    OpenDialog1: TOpenDialog;
    EasyReg: TEasyReg;
    ImageList1: TImageList;
    PopupMenu1: TPopupMenu;
    Setup1: TMenuItem;
    N1: TMenuItem;
    Properties1: TMenuItem;
    procedure Button1Click(Sender: TObject);
    Function Execute: Boolean;
    Procedure LoadPlugInfo;
    procedure Button2Click(Sender: TObject);
    procedure ListView1DblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Setup1Click(Sender: TObject);
  private
    { Private declarations }
  public
   GPL: TGenPlugInList;
   VPL: TVisPlugInList;
   CPL: TCMLxPlugInList;
  end;

var
  PlugMgrDlg: TPlugMgrDlg;

implementation

{$R *.dfm}

procedure TPlugMgrDlg.Button1Click(Sender: TObject);
Var
 RA: TRegisterAs;
 I: integer;
 IPForm: TIPForm;
 RT: TRegisterTools;
begin
 if not opendialog1.execute then exit;
 Screen.Cursor := crHourglass;
 Update;
 RT := TRegisterTools.Create;
 IPForm := TIPForm.Create(self);
 IPForm.Label1.Caption := 'Installing Plugin ...';
 Self.Enabled := False;
 IPForm.Show;
 IPForm.Update;
 RA := raUnknown;
 For I := 0 to Opendialog1.Files.Count - 1 do begin
  RT.InstallPlugIn(Pchar(opendialog1.Files[I]), ra);
  If RA = raUnknown then begin
   MessageDlg('Unable to install'+#13+#10+'"' + opendialog1.files[I] + '"'+#13+#10+'The DLL was not recognized as any know CM2 plugin type.', mtError, [mbOK], 0);
  end;
 End;
 EasyReg.Write('DefaultBrowseFolder', extractfilepath(opendialog1.Files[0]));
 LoadPlugInfo;
 IpForm.Hide;
 FreeAndNil(IPForm);
 FreeAndNil(RT);
 SetForeGroundWindow(self.handle);
 Self.Enabled := true;
 Screen.Cursor := crDefault;
end;

Procedure TPlugMgrDlg.LoadPlugInfo;
Var
 I: Integer;
 LI: TListItem;
 Reg: TRegistry;
 FName: String;
 Count: Integer;
 Icon: TIcon;
 LargeIcon, SmallIcon: HIcon;
begin
 listview1.items.clear;
 If GPL.Count > 0 then GPL.UnloadAll;
 GPL.LoadAll;
 Reg := TRegistry.Create;
 Reg.RootKey := HKEY_CLASSES_ROOT;
 For I := 0 to GPL.Count - 1 do begin
  LI := Listview1.Items.add;
  LI.Caption := GPL[I].Name + ' (General)';
  LI.SubItems.Add(CM2MMRB.WordToStr(GPL[I].Version));
  LI.SubItems.Add('G' + IntToStr(I));
  LI.ImageIndex := 1;
  If Reg.OpenKey('\CLSID\' + GUIDToString(GPL[I].GUID) + '\InProcServer32', false) then begin
   FName := Reg.ReadString('');
   If fileexists(FName) Then begin
    Count := ExtractIconEx(PChar(FName), -1, LargeIcon, SmallIcon, 1);
    if Count > 0 then begin
     Icon := TIcon.Create;
     ExtractIconEx(PChar(FName), 0, LargeIcon, SmallIcon, 1);
     Icon.Handle := SmallIcon;
     LI.ImageIndex := ImageList1.AddIcon(Icon);
     Icon.Free;
    end;
   end;
  end;
 End;
 If VPL.Count > 0 Then VPL.UnLoadAll;
 VPL.LoadAll;
 For I := 0 to VPL.Count - 1 do begin
  LI := Listview1.Items.add;
  LI.Caption := VPL[I].Name + ' (Visual)';
  LI.SubItems.Add(CM2MMRB.WordToStr(VPL[I].Version));
  LI.SubItems.Add('V' + IntToStr(I));
  LI.ImageIndex := 0;
  If Reg.OpenKey('\CLSID\' + GUIDToString(VPL[I].GUID) + '\InProcServer32', false) then begin
   FName := Reg.ReadString('');
   If fileexists(FName) Then begin
    Count := ExtractIconEx(PChar(FName), -1, LargeIcon, SmallIcon, 1);
    if Count > 0 then begin
     Icon := TIcon.Create;
     ExtractIconEx(PChar(FName), 0, LargeIcon, SmallIcon, 1);
     Icon.Handle := SmallIcon;
     LI.ImageIndex := ImageList1.AddIcon(Icon);
     Icon.Free;
    end;
   end;
  end;
 End;
 If CPL.Count > 0 then CPL.UnLoadAll;
 CPL.LoadAll;
 For I := 0 to CPL.Count - 1 do begin
  LI := ListView1.Items.Add;
  LI.Caption := CPL[I].Name + ' (cmlX)';
  LI.SubItems.Add(CM2MMRB.WordToStr(CPL[I].Version));
  LI.SubItems.Add('C' + IntToStr(I));
  LI.ImageIndex := 2;
  If Reg.OpenKey('\CLSID\' + GUIDToString(CPL[I].GUID) + '\InProcServer32', false) then begin
   FName := Reg.ReadString('');
   If fileexists(FName) Then begin
    Count := ExtractIconEx(PChar(FName), -1, LargeIcon, SmallIcon, 1);
    if Count > 0 then begin
     Icon := TIcon.Create;
     ExtractIconEx(PChar(FName), 0, LargeIcon, SmallIcon, 1);
     Icon.Handle := SmallIcon;
     LI.ImageIndex := ImageList1.AddIcon(Icon);
     Icon.Free;
    end;
   end;
  end;
 end;
end;

function TPlugMgrDlg.Execute: Boolean;
begin
 Result := Showmodal = mrOk;
end;

procedure TPlugMgrDlg.Button2Click(Sender: TObject);
Var
 Sel: Integer;
 GUID: TGUID;
 IPForm: TIPForm;
 RT: TRegisterTools;
begin
 if listview1.Selected = nil then exit;
 If MessageDlg('Uninstall Plugin ?', mtConfirmation, [mbYes,mbNo], 0) = mrNo then exit;
 RT := TRegisterTools.Create;
 IPForm := TIPForm.Create(self);
 IPForm.Label1.Caption := 'Uninstalling Plugin ...';
 Self.Enabled := False;
 IPForm.Show;
 IPForm.Update;
 Sel := StrToInt(Copy(Listview1.Selected.SubItems[1], 2, length(Listview1.Selected.SubItems[1])));
 If Listview1.Selected.SubItems[1][1] = 'G' then begin
  // GENERAL
  GUID := GPL[Sel].GUID;
  GPL.UnloadAll;
  Try
   RT.UninstallPlugIn(GUID);
  Except
   On EInUse do begin
    MessageDlg('Plugin is in use --- Please shutdown CoolMon 2 before '+#13+#10+'uninstalling a plugin', mtError, [mbAbort], 0);
   end;
  End;
  GPL.LoadAll;
 end;
 If ListView1.Selected.SubItems[1][1] = 'V' then begin
  // VISUAL
  GUID := VPL[Sel].GUID;
  VPL.UnloadAll;
  Try
   RT.UninstallPlugIn(GUID);
  Except
   On EInUse do begin
    MessageDlg('Plugin is in use --- Please shutdown CoolMon 2 before '+#13+#10+'uninstalling a plugin', mtError, [mbAbort], 0);
   end;
  End;
  VPL.LoadAll;
 end;
 If ListView1.Selected.SubItems[1][1] = 'C' then begin
  // CML X
  GUID := CPL[Sel].GUID;
  CPL.UnloadAll;
  Try
   RT.UninstallPlugIn(GUID);
  Except
   On EInUse do begin
    MessageDlg('Plugin is in use --- Please shutdown CoolMon 2 before '+#13+#10+'uninstalling a plugin', mtError, [mbAbort], 0);
   end;
  End;
  CPL.LoadAll;
 end;
 LoadPlugInfo;
 IpForm.Hide;
 FreeAndNil(IPForm);
 FreeAndNil(RT);
 SetForeGroundWindow(self.handle);
 Self.Enabled := true;
end;

procedure TPlugMgrDlg.ListView1DblClick(Sender: TObject);
Var
 Sel: Integer;
 PlugInInfoDlg: TForm5;
begin
 Sel := StrToInt(Copy(Listview1.Selected.SubItems[1], 2, length(Listview1.Selected.SubItems[1])));
 Self.Enabled := false;
 If Listview1.Selected.SubItems[1][1] = 'G' then begin
  // GENERAL
  PlugInInfoDlg := TForm5.Create(self);
  Try
   PlugInInfoDlg.Execute(GPL[Sel]);
  Finally
   PlugInInfoDlg.Free;
  end;
 end;
 If Listview1.Selected.SubItems[1][1] = 'V' then begin
  // VISUAL
  PlugInInfoDlg := TForm5.Create(self);
  Try
   PlugInInfoDlg.Execute(VPL[Sel]);
  Finally
   PlugInInfoDlg.Free;
  end;
 end;
 If Listview1.Selected.SubItems[1][1] = 'C' then begin
  // CMLX
  PlugInInfoDlg := TForm5.Create(self);
  Try
   PlugInInfoDlg.Execute(CPL[Sel]);
  Finally
   PlugInInfoDlg.Free;
  end;
 end;
 Self.Enabled := true;
end;

procedure TPlugMgrDlg.FormCreate(Sender: TObject);
Var
 N: String;
begin
 N := EasyReg.Read('DefaultBrowseFolder');
 If DirectoryExists(N) then opendialog1.InitialDir := N;
 GPL := TGenPlugInList.Create(handle);
 VPL := TvisPlugInList.Create;
 CPL := TCMLxPlugInList.Create(handle);
 LoadPlugInfo;
 ShowWindow(handle, SW_HIDE);
end;

procedure TPlugMgrDlg.Setup1Click(Sender: TObject);
Var
 Sel: Integer;
begin
 Sel := StrToInt(Copy(Listview1.Selected.SubItems[1], 2, length(Listview1.Selected.SubItems[1])));
 Self.Enabled := false;
 If Listview1.Selected.SubItems[1][1] = 'G' then begin
  // GENERAL
  GPL[SEl].Setup;
 end;
 If Listview1.Selected.SubItems[1][1] = 'V' then begin
  // VISUAL
  VPL[Sel].Setup;
 end;
 If Listview1.Selected.SubItems[1][1] = 'C' then begin
  // CML X
  CPL[Sel].Setup;
 end;
 Self.Enabled := true;
end;

end.
