//Sample code file: AddGroup/Select.pas

//Warning: This code has been marked up for HTML
(***************************************************************************
$name: Select.pas
$version: 1.0
$date_modified: 122298
$description: Example of how to manipulate group membership in DS.
$owner: Novell Libraries for Delphi Product Manager
Copyright (c) 1998 Novell, Inc. All Rights Reserved.

THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT
ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS THIS WORK.
PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A
ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS
PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET,
DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S
PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S
CUSTOMERS WITH RESPECT TO THIS CODE.
****************************************************************************)
unit Select;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, CalWin32, NetWin32;

type
  TSelectObjectForm = class(TForm)
    btnOK: TButton;
    btnCancel: TButton;
    fraObjects: TGroupBox;
    btnSetContext: TButton;
    lblContext: TLabel;
    lstObjects: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure btnSetContextClick(Sender: TObject);
  private
    { Private declarations }
    hContext: NWDSContextHandle;
    FObjectClass: string;
    function GetObjectClass: string;
    procedure SetObjectClass(aValue: string);
  public
    { Public declarations }
    procedure ChangeToContext(aContext: NWDSContextHandle);
    procedure CanonicalizeNames;
    property ObjectClass: string read GetObjectClass write SetObjectClass;
  end;

var
  SelectObjectForm: TSelectObjectForm;

implementation

uses NWHelper, Context;

{$R *.DFM}


////////////////////////////////////////////////////////////////////////////////

//  { Private Methods }

function TSelectObjectForm.GetObjectClass: string;
class='delphiKeyword'>begin
  GetObjectClass := FObjectClass;
end;  // TSelectObjectForm.GetObjectClass()


procedure TSelectObjectForm.SetObjectClass(aValue: string);
class='delphiKeyword'>begin
  if (FObjectClass <> aValue) then
  class='delphiKeyword'>begin
    FObjectClass := aValue;
    lstObjects.Items.Clear;
    lstObjects.Items.AddStrings(GetObjectList(hContext, FObjectClass));
  end;  // if (FObjectClass <> aValue)

end;  // TSelectObjectForm.SetObjectClass()



////////////////////////////////////////////////////////////////////////////////

//  { Public Methods }

procedure TSelectObjectForm.ChangeToContext(aContext: NWDSContextHandle);
var
  Context: class='delphiKeyword'>array[0..MAX_DN_BYTES] of Char;
class='delphiKeyword'>begin
  try
    NWCheckAndThrow( NWDSDuplicateContextHandle(aContext, hContext) );
    NWCheckAndThrow( NWDSGetContext(hContext, DCK_NAME_CONTEXT, @Context) );
    lblContext.Caption := Context;
    lstObjects.Items.Clear;
    lstObjects.Items.AddStrings(GetObjectList(hContext, FObjectClass));
  except
    on E:ENWException do
      E.Show();
  end;  // try-except

end;  // TSelectObjectForm.ChangeToContext()


procedure TSelectObjectForm.CanonicalizeNames;
var
  ShortName, CanonName: class='delphiKeyword'>array[0..MAX_DN_BYTES] of Char;
  Index: Integer;
  bChecked: Boolean;
class='delphiKeyword'>begin
  for Index := 0 to lstObjects.Items.Count - 1 do
  class='delphiKeyword'>begin
    bChecked := lstObjects.Selected[Index];          // Preserve setting

    StrPCopy (ShortName, lstObjects.Items[Index]);
    try
      NWCheckAndThrow( NWDSCanonicalizeName(hContext, @ShortName, @CanonName) );
    except
      on E:ENWException do
        E.Show();
    end;  // try-except

    lstObjects.Items[Index] := '.' + string(CanonName);
    lstObjects.Selected[Index] := bChecked;
  end;  // for

end;  // TSelectObjectForm.CanonicalizeNames



////////////////////////////////////////////////////////////////////////////////

//  { Delphi-Generated Methods }

procedure TSelectObjectForm.FormCreate(Sender: TObject);
class='delphiKeyword'>begin
  FObjectClass := '';    // Show all objects

  hContext := GetContextHandle();
end;

procedure TSelectObjectForm.btnSetContextClick(Sender: TObject);
var
  ContextDlg: TContextForm;
  NameContext: class='delphiKeyword'>array[0..MAX_DN_BYTES] of Char;
class='delphiKeyword'>begin
  // Initialize

  ContextDlg := TContextForm.Create(self);
  ContextDlg.ChangeToContext(hContext);

  // Show the selection dialog

  if (ContextDlg.ShowModal = mrOk) then
  class='delphiKeyword'>begin
    // Get the new Name Context

    StrPCopy (NameContext, ContextDlg.NameContext);
    NWCheckAndThrow( NWDSSetContext(hContext, DCK_NAME_CONTEXT, @NameContext) );
    lstObjects.Items.Clear;
    lstObjects.Items.AddStrings(GetObjectList(hContext, FObjectClass));
  end;  // OK hit

  ContextDlg.Free;
end;

end.