//Warning: This code has been marked up for HTML

{/***************************************************************************
$name: sendevtSvr.pas
$version: 1.0 
$date_modified: 121298 
$description: Pascal source file
$owner: GroupWise SDK Team Lead
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          sendevtSvr;

interface

uses
  OleAuto,
  Ole2,
  Windows,
  Registry,
  sendevtC3PO,
  Dialogs,
  C3poinc;

type
{***********************************************************
   Object Name:  C3POServer sub classed from TC3POServer.

   Description:  Every C3PO must support this interface.  It is used to
                 initialize the C3PO.

************************************************************}
  C3POServer = class(TC3POServer)
  private
    { Private declarations }
    function GetCmdFact: Variant;
    function GetDescription : string;
    function GetEventMonitor : variant;
    function GetIconFactory : variant;
  automated
    { Automated declarations }
    property CommandFactory : Variant read GetCmdFact;
    property Description: string read GetDescription;
    property EventMonitor: variant read GetEventMonitor;
    property IconFactory: variant read GetIconFactory;
    function CanShutdown: TOleBool;
    procedure DeInit;
    procedure Init(Manager: variant);
end;

function RegisterServer : HResult;
function DllUnregisterServer : HResult;
function GWEventNotify (dwID : Cardinal; dwNotification : Cardinal): longint  export stdcall;

exports
       DllGetClassObject index 1,
       DllCanUnloadNow index 2,
       DllRegisterServer index 3 name 'DllRegisterServer',
       DllUnregisterServer index 4 name 'DllUnregisterServer',
       GWEventNotify index 5 name 'GWEventNotify';

implementation
var
   g_CommandFactory : CommandFactory;                 // Create global CommandFactory object

   g_IconFactory : IconFactory;                       // Create global IconFactory object

   g_EventMonitor : EventMonitor;                     // Create global EventMonitor object


{***********************************************************
   Name:       C3POServer.GetCmdFact

   Description:  This property returns the CommandFactory for the C3PO.  The
                 C3PO server may return NULL.


   In:           none

   Out:          CommandFactory object or null in not supported

   Comments:

************************************************************}
function C3POServer.GetCmdFact : Variant;
class='delphiKeyword'>begin
     Result := g_CommandFactory.OleObject;            // Return Ole object g_CommandFactory

end;

{***********************************************************
   Name:         C3POServer.GetDescription

   Description:  Returns a human readable description of the C3PO server.

   In:           none

   Out:          string

   Comments:

************************************************************}
function C3POServer.GetDescription : string;
class='delphiKeyword'>begin
  Result := 'Put any comment here you want';                // Return a description of your C3po

end;

{***********************************************************
   Name:         C3POServer.GetEventMonitor

   Description:  This property returns the event monitor for the C3PO.  The
                 C3PO server may return NULL.


   In:           none

   Out:          EventMonitor object or null if not supported

   Comments:

************************************************************}
function C3POServer.GetEventMonitor : Variant;
class='delphiKeyword'>begin
     Result := g_EventMonitor.OleObject;              // return Oleobject g_EventMonitor

end;

{***********************************************************
   Name:       C3POServer.CanShutdown

   Description:  This method is invoked to query whether the C3PO server can
                 shutdown.  Typically, this is used when the C3PO server has a
                 window open on the UI screen class='delphiKeyword'>and is not in a state where shutdown
                 is possible.  Each C3PO server is guaranteed to have CanShutdown
                 queried at least once before the C3PO system shuts down.

                 Semantically, CanShutdown is a request from the C3POManager to
                 a given C3PO.  It's not a request for shutting down a C3PO class='delphiKeyword'>as
                 much class='delphiKeyword'>as it is a request to shutdown the C3POManager class='delphiKeyword'>and consequently
                 disconnect the C3PO.  Typically, a C3PO would shutdown along
                 with the C3POManager, but this is not strictly required.

                 No particular C3PO order can be relied on for querying the CanShutdown property.

   In:           none

   Out:          TRUE if it is OK to shutdown
                 FALSE if it is not OK to shutdown

   Comments:

************************************************************}
function C3POServer.CanShutdown: TOleBool;
class='delphiKeyword'>begin
     Result := TRUE;                                  // return it is OK to shutdown

end;


{***********************************************************
   Name:       C3POServer.GetIconFactory

   Description:  This property returns the IconFactory for the C3PO.  The C3PO
                 server may return NULL.


   In:           none

   Out:          IconFactory object or null if not supported


   Comments:

************************************************************}
function C3POServer.GetIconFactory : Variant;
class='delphiKeyword'>begin
    Result := g_IconFactory.OleObject;                // return OleObject g_IconFactory

end;

{***********************************************************
   Name:         TC3POServer.DeInit

   Description:  Terminates the relationship of the C3PO Manager with the C3PO
                 server.  Note that this is a separate issue from the shutdown
                 sequence (including shutdown events).  For example, a C3PO may
                 be unloaded from memory class='delphiKeyword'>as a runtime optimization.  In that
                 scenario, the C3PO server first receives CanShutdown() calls
                 followed by DeInit() – the C3PO is unloaded, but the client
                 application has not necessarilly terminated.

                 The C3POManager pointer passed in to C3POServer::Init() is
                 still valid during this call.  However, when DeInit() returns,
                 the C3POManager pointer is not guaranteed to be valid.  The
                 C3POServer must release all holds to the C3POManager during
                 the DeInit call.

                 One DeInit call will be issued to the C3POServer for each C3PO
                 Client using the C3PO system.  C3POs that wish to be capable of
                 loading into multiple Clients (irrespective of process boundaries)
                 should be multiple-instance OLE servers.  That is, a new C3POServer
                 object should be created for each C3POManager that wished to
                 use the services of the C3PO.  By tracking the Manager pointer
                 for each C3POServer, the C3PO can sort out which requests are
                 being issued from which clients.


   In:           none

   Out:          none

   Comments:

************************************************************}
procedure C3POServer.DeInit;
class='delphiKeyword'>begin
     g_C3POManager := unassigned;
end;

{***********************************************************
   Name:       C3POServer.Init

   Description:  This method is the first method invoked in the C3POServer object
                 when loading a C3PO server.  If the server fails this call
                 (via the HRESULT), the C3PO server is unloaded.

                 One Init call will be issued to the C3POServer for each C3PO
                 Client using the C3PO system.  C3POs that wish to be capable of
                 loading into multiple Clients (irrespective of process boundaries)
                 should be multiple-instance OLE servers.  That is, a new C3POServer
                 object should be created for each C3POManager that wished to use
                 the services of the C3PO.  By tracking the Manager pointer for
                 each C3POServer, the C3PO can sort out which requests are being
                 issued from which clients.

                 The Manager object is valid until a future DeInit() call.
                 That is, the C3PO does not need to AddRef() this object but can
                 simply store it for the life of the C3PO (until DeInit is called).



   In:           Manager: variant - C3POManager object

   Out:          none
                                                           
   Comments:

************************************************************}
procedure C3POServer.Init(Manager: variant);
class='delphiKeyword'>begin
  g_C3POManager := Manager;                           // save C3POMonitor object

  OleInitialize(Nil);                                 // Init Ole

end;

{***********************************************************
   Name:       RegisterC3POServer

   Description: This routine registers the C3POServer.

   In:          none

   Out:         none

   Comments:

************************************************************}
procedure RegisterC3POServer;
const
  AutoClassInfo: TAutoClassInfo = (
    AutoClass: C3POServer;
    ProgID: 'sendevt';
    ClassID: '{A485EE06-EA82-11D1-B2D9-00805FC1102C}';
    Description: 'C3po Automation';
    Instancing: acMultiInstance);

class='delphiKeyword'>begin
   Automation.RegisterClass(AutoClassInfo);
   RegisterServer;

end;


{***********************************************************
   Name:       GWEventNotify

   Description:  This procedure is exported, class='delphiKeyword'>and is used by the
                 client in response to events requested by this
                 C3PO for a compose view.

   In:           dwID:  32 bit value passed to the GW client class='delphiKeyword'>as part
                 of the EventNoticeRegister token.  This can be an ID
                 or a pointer to memory needed by this routine.

                 dwNotification: Flag indicating which event has taken place.
                      EVT_GROUPWISE_SEND_ITEM            = $1;
                      EVT_GROUPWISE_CANCEL_ITEM            = $2;
                      EVT_GROUPWISE_CLOSE_ITEM            = $4;
                      EVT_GROUPWISE_PACKAGING_ITEM         = $8;
                      EVT_GROUPWISE_PACKAGING_ITEM_CANCELED    = $10;
                      EVT_GROUPWISE_SEND_ITEM_COMPLETED         = $20;


   Out:          None

   Comments:

************************************************************}
function GWEventNotify (dwID : Cardinal; dwNotification : Cardinal): longint; export stdcall;
var
   iDDEReturn : integer;
   sReturn    : string;
   sMessage   : string;
   iMsgRet    : integer;
   lFncRet    : longint;

class='delphiKeyword'>begin
     // Register the notify event procedure here.

//     iDDEReturn := GWCommander.Execute ('EventNoticeUnRegister (2543)', sReturn);

       lFncRet := 0;

       class='delphiKeyword'>case dwNotification of
            EVT_GROUPWISE_SEND_ITEM:
            class='delphiKeyword'>begin
                 sReturn := 'EVT_GROUPWISE_SEND_ITEM received';
            end;
            EVT_GROUPWISE_CANCEL_ITEM:
            class='delphiKeyword'>begin
                 sReturn := 'EVT_GROUPWISE_CANCEL_ITEM received';
            end;
            EVT_GROUPWISE_CLOSE_ITEM:
            class='delphiKeyword'>begin
                 sReturn := 'EVT_GROUPWISE_CLOSE_ITEM received';
            end;
            EVT_GROUPWISE_PACKAGING_ITEM:
            class='delphiKeyword'>begin
                 sReturn := 'EVT_GROUPWISE_PACKAGING_ITEM received';
                 iMsgRet := MessageDlg ('Send Message?', mtInformation, [mbYes, mbNo], 0);
                 if iMsgRet = IDNO then lFncRet := -1;
            end;
            EVT_GROUPWISE_PACKAGING_ITEM_CANCELED:
            class='delphiKeyword'>begin
                 sReturn := 'EVT_GROUPWISE_PACKAGING_ITEM_CANCELLED received';
            end;
            EVT_GROUPWISE_SEND_ITEM_COMPLETED:
            class='delphiKeyword'>begin
                 sReturn := 'EVT_GROUPWISE_SEND_ITEM_COMPLETED received';
            end;
       end;
     MessageDlg (sReturn, mtInformation, [mbOk], 0);
     GWEventNotify := lFncRet;
end;


function RegisterServer : HResult;
var
   Reg : TRegistry;
   sRegKeyName : string [120];
   sAppName : string [120];

class='delphiKeyword'>begin

  sAppName := 'sendevt';
  Reg := TRegistry.Create;
  Reg.RootKey := HKEY_LOCAL_MACHINE;
  sRegKeyName := '\SOFTWARE\Novell\GroupWise\5.0\C3PO\DataTypes\GW.CLIENT\sendevt';
  Reg.OpenKey (sRegKeyName, TRUE);
  Reg.OpenKey ('Objects', TRUE);
  Reg.WriteString ('EventMonitor', '');
  Reg.OpenKey (sRegKeyName, TRUE);
  Reg.OpenKey ('Events', TRUE);
  Reg.WriteString ('OnReady', '');
  Reg.WriteString ('OnShutdown', '');

  sRegKeyName := '\SOFTWARE\Novell\GroupWise\5.0\C3PO\DataTypes\GW.MESSAGE.MAIL\sendevt';
  Reg.OpenKey (sRegKeyName, TRUE);
  Reg.OpenKey ('Objects', TRUE);
  Reg.WriteString ('CommandFactory', '');
  Reg.OpenKey (sRegKeyName, TRUE);
  Reg.OpenKey ('Events', TRUE);
  Reg.WriteString (eGW_CMDID_COMPOSE, '');


  Result := S_OK;
end;

function DllUnregisterServer : HResult;
var
   Reg : TRegistry;
   sRegKeyName : string [120];
   sAppName : string [120];

class='delphiKeyword'>begin

  sAppName := 'sendevt';
  Reg := TRegistry.Create;
  Reg.RootKey := HKEY_LOCAL_MACHINE;
  sRegKeyName := '\SOFTWARE\Novell\GroupWise\5.0\C3PO\DataTypes\GW.CLIENT\sendevt';
  Reg.DeleteKey(sRegKeyName);

  sRegKeyName := '\SOFTWARE\Novell\GroupWise\5.0\C3PO\DataTypes\GW.MESSAGE.MAIL\sendevt';
  Reg.DeleteKey(sRegKeyName);


  Result := S_OK;
end;

{Create OleObjects}
initialization
class='delphiKeyword'>begin
  RegisterC3POServer;
  g_CommandFactory := CommandFactory.Create;
  g_IconFactory := IconFactory.Create;
  g_EventMonitor := EventMonitor.Create;
end;

{Release OleObjects}
finalization
class='delphiKeyword'>begin
  g_CommandFactory.Release;
  g_IconFactory.Release;
  g_EventMonitor.Release;
end;


end.