home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / NotificationCenter.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.4 KB  |  215 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* 
  19.    NotificationCenter.cpp -- Callback mechanism for XFE
  20.    Created: Chris Toshok <toshok@netscape.com>, 7-Aug-96.
  21.    */
  22.  
  23.  
  24.  
  25. #include "NotificationCenter.h"
  26. #include "xp_mem.h"
  27. #include "xp_str.h"
  28. #include "xpassert.h"
  29.  
  30. XFE_NotificationCenter::XFE_NotificationCenter()
  31. {
  32.     m_hashtable = NULL;
  33.  
  34.     m_forwarder = NULL;
  35.     m_numlists = 0;
  36. }
  37.  
  38. XFE_NotificationCenter::~XFE_NotificationCenter()
  39. {
  40.     if (!m_hashtable)
  41.         return;
  42.  
  43.     /* use the enumerator to step down and destroy all the callback lists. */
  44.     PR_HashTableEnumerateEntries(m_hashtable,
  45.                                  (PRHashEnumerator)XFE_NotificationCenter::destroyHashEnumerator,
  46.                                  NULL);
  47.  
  48.     PR_HashTableDestroy(m_hashtable);
  49. }
  50.  
  51. int
  52. XFE_NotificationCenter::destroyHashEnumerator(PRHashEntry *he, int, void *)
  53. {
  54.     XFE_NotificationList *list = (XFE_NotificationList*)he->value;
  55.  
  56.     XP_FREE(list->notification_type);
  57.     XP_FREE(list->callbacks);
  58.     XP_FREE(list);
  59.  
  60.     return HT_ENUMERATE_NEXT | HT_ENUMERATE_REMOVE;
  61. }
  62.  
  63. XFE_NotificationList *
  64. XFE_NotificationCenter::getNotificationListForName(const char *name)
  65. {
  66.     XFE_NotificationList *list = (XFE_NotificationList*)PR_HashTableLookup(m_hashtable, name);
  67.  
  68.     return list;
  69. }
  70.  
  71. XFE_NotificationList *
  72. XFE_NotificationCenter::addNewNotificationList(const char *name)
  73. {
  74.     XFE_NotificationList *new_list = XP_NEW_ZAP(XFE_NotificationList);
  75.  
  76.     new_list->notification_type = XP_STRDUP(name);
  77.     new_list->num_interested = 0;
  78.     new_list->num_alloced = 5; // start with 5 and realloc if necessary.
  79.  
  80.     new_list->callbacks = 
  81.         (XFE_CallbackElement*)XP_CALLOC(new_list->num_alloced, 
  82.                                         sizeof(XFE_CallbackElement));
  83.     
  84.     PR_HashTableAdd(m_hashtable, name, new_list);
  85.  
  86.     m_numlists ++;
  87.  
  88.     return new_list;
  89. }
  90.  
  91. void 
  92. XFE_NotificationCenter::registerInterest(const char *notification_name,
  93.                                          XFE_NotificationCenter *obj,
  94.                                          XFE_FunctionNotification notification_func,
  95.                                          void *clientData)
  96. {
  97.     XFE_NotificationList *list;
  98.  
  99.     if (!m_hashtable)
  100.         m_hashtable = PR_NewHashTable(5, PR_HashString, PR_CompareStrings, PR_CompareValues, NULL, NULL);
  101.     
  102.     list = getNotificationListForName(notification_name);
  103.     
  104.     if (!list)
  105.         list = addNewNotificationList(notification_name);
  106.     
  107.     if (list->num_alloced == list->num_interested)
  108.         {
  109.             list->num_alloced *= 2;
  110.             
  111.             list->callbacks = (XFE_CallbackElement*)XP_REALLOC(list->callbacks,
  112.                                                                sizeof(XFE_CallbackElement) * list->num_alloced);
  113.         }
  114.     
  115.     list->callbacks[ list->num_interested ].obj = obj;
  116.     list->callbacks[ list->num_interested ].callbackFunction = notification_func;
  117.     list->callbacks[ list->num_interested ].clientData = clientData;
  118.     
  119.     list->num_interested ++;
  120. }
  121.  
  122. void 
  123. XFE_NotificationCenter::unregisterInterest(const char *notification_name,
  124.                                            XFE_NotificationCenter *obj,
  125.                                            XFE_FunctionNotification notification_func,
  126.                                            void *clientData)
  127. {
  128.     int j,k;
  129.     XFE_NotificationList *list;
  130.  
  131.     if (!m_hashtable)
  132.         return;
  133.  
  134.     list = getNotificationListForName(notification_name);
  135.     
  136.     if (list)
  137.         {
  138.             for (j = 0; j < list->num_interested; j ++)
  139.                 {
  140.                     if ( list->callbacks[ j ].obj == obj
  141.                          && list->callbacks[ j ].callbackFunction == notification_func
  142.                          && list->callbacks[ j ].clientData == clientData )
  143.                         {
  144.                             for (k = j; k < list->num_interested - 1; k ++)
  145.                                 list->callbacks[ k ] = list->callbacks[ k + 1 ];
  146.                             
  147.                             list->num_interested --;
  148.                             return;
  149.                         }
  150.                 }
  151.         }
  152. }
  153.  
  154. XP_Bool
  155. XFE_NotificationCenter::hasInterested(const char *notification_center)
  156. {
  157.     if (!m_hashtable)
  158.         {
  159.             return FALSE;
  160.         }
  161.     else
  162.         {
  163.             XFE_NotificationList *list = getNotificationListForName(notification_center);
  164.             return (list != NULL && list->num_interested >= 1);
  165.         }
  166. }
  167.  
  168. void
  169. XFE_NotificationCenter::setForwarder(XFE_NotificationCenter *obj)
  170. {
  171.     m_forwarder = obj;
  172. }
  173.  
  174. XFE_NotificationCenter *
  175. XFE_NotificationCenter::getForwarder()
  176. {
  177.     return m_forwarder;
  178. }
  179.  
  180. void 
  181. XFE_NotificationCenter::notifyInterested(const char *notification_name,
  182.                      void *callData)
  183. {
  184.     if (m_forwarder && m_forwarder != this)
  185.         {
  186.             m_forwarder->notifyInterested(notification_name, callData);
  187.         }
  188.     else
  189.         {
  190.             int j;
  191.             XFE_NotificationList *list;
  192.             
  193.             if (!m_hashtable)
  194.                 return;
  195.  
  196.             list = getNotificationListForName(notification_name);
  197.             
  198.             if (list)
  199.                 {
  200.                     for (j = 0; j < list->num_interested; j ++)
  201.                         {
  202.                             XP_ASSERT(list->callbacks[j].callbackFunction);
  203.                             
  204.                             if (list->callbacks[j].callbackFunction)
  205.                                 {
  206.                                     (*list->callbacks[j].callbackFunction)
  207.                                         (this, list->callbacks[j].obj, list->callbacks[j].clientData, callData);
  208.                                 }
  209.                         }
  210.                 }
  211.         }
  212. }
  213.   
  214.  
  215.