patch-from-shadow-to-jaltman-bkbox-20031120
[openafs.git] / src / WINNT / afsclass / c_notify.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include <WINNT/afsclass.h>
16 #include "internal.h"
17
18
19 /*
20  * DEFINITIONS ________________________________________________________________
21  *
22  */
23
24 #define cREALLOC_NOTIFY  4      // allocate space for 4 notifies at once
25
26
27 /*
28  * VARIABLES __________________________________________________________________
29  *
30  */
31
32 size_t            NOTIFYCALLBACK::nNotifyList = 0;
33 LPNOTIFYCALLBACK *NOTIFYCALLBACK::aNotifyList = NULL;
34
35
36 /*
37  * PROTOTYPES _________________________________________________________________
38  *
39  */
40
41
42 /*
43  * ROUTINES ___________________________________________________________________
44  *
45  */
46
47 NOTIFYCALLBACK::NOTIFYCALLBACK (NOTIFYCALLBACKPROC procUser, LPARAM lpUser)
48 {
49    procSupplied = procUser;
50    lpSupplied = lpUser;
51
52    for (size_t iNotify = 0; iNotify < nNotifyList; ++iNotify)
53       {
54       if (aNotifyList[ iNotify ] == NULL)
55          break;
56       }
57
58    if (iNotify >= nNotifyList)
59       {
60       REALLOC (aNotifyList, nNotifyList, 1+iNotify, cREALLOC_NOTIFY );
61       }
62
63    if (iNotify < nNotifyList)
64       {
65       aNotifyList[ iNotify ] = this;
66       }
67 }
68
69
70 NOTIFYCALLBACK::~NOTIFYCALLBACK (void)
71 {
72    for (size_t iNotify = 0; iNotify < nNotifyList; ++iNotify)
73       {
74       if (aNotifyList[ iNotify ] == this)
75          aNotifyList[ iNotify ] = NULL;
76       }
77 }
78
79
80 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, ULONG status)
81 {
82    return SendNotificationToAll (evt, NULL, NULL, NULL, NULL, 0, status);
83 }
84
85 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, LPIDENT lpi1, ULONG status)
86 {
87    return SendNotificationToAll (evt, lpi1, NULL, NULL, NULL, 0, status);
88 }
89
90 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, LPTSTR psz1, ULONG status)
91 {
92    return SendNotificationToAll (evt, NULL, NULL, psz1, NULL, 0, status);
93 }
94
95 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, LPIDENT lpi1, LPTSTR psz1, ULONG status)
96 {
97    return SendNotificationToAll (evt, lpi1, NULL, psz1, NULL, 0, status);
98 }
99
100 BOOL NOTIFYCALLBACK::SendNotificationToAll (NOTIFYEVENT evt, LPIDENT lpi1, LPIDENT lpi2, LPTSTR psz1, LPTSTR psz2, DWORD dw1, ULONG status)
101 {
102    BOOL rc = TRUE;
103
104    NOTIFYPARAMS Params;
105    memset (&Params, 0x00, sizeof(Params));
106    Params.lpi1 = lpi1;
107    Params.lpi2 = lpi2;
108    lstrcpy (Params.sz1, (psz1) ? psz1 : TEXT(""));
109    lstrcpy (Params.sz2, (psz2) ? psz2 : TEXT(""));
110    Params.dw1 = dw1;
111    Params.status = status;
112
113    for (size_t iNotify = 0; iNotify < nNotifyList; ++iNotify)
114       {
115       if (aNotifyList[ iNotify ] != NULL)
116          {
117          Params.lpUser = aNotifyList[ iNotify ]->lpSupplied;
118          if (!aNotifyList[ iNotify ]->SendNotification (evt, &Params))
119             rc = FALSE;
120          }
121       }
122
123    return rc;
124 }
125
126
127 BOOL NOTIFYCALLBACK::SendNotification (NOTIFYEVENT evt, PNOTIFYPARAMS pParams)
128 {
129    BOOL rc = TRUE;
130
131    if (procSupplied != NULL) {
132       try {
133          if (!(*procSupplied)( evt, pParams ))
134             rc = FALSE;
135       } catch(...) {
136          // whoops--never trust a callback.
137 #ifdef DEBUG
138          DebugBreak();
139 #endif
140          rc = FALSE;
141       }
142    }
143
144    return rc;
145 }
146