HTAlert.c
Upload User: zlh9724
Upload Date: 2007-01-04
Package Size: 1991k
Code Size: 6k
Category:

Browser Client

Development Platform:

Unix_Linux

  1. /*       HTAlert.c
  2. ** DIALOG MANAGER
  3. **
  4. ** (c) COPYRIGHT MIT 1995.
  5. ** Please first read the full copyright statement in the file COPYRIGH.
  6. **
  7. ** REPLACE THIS MODULE with a GUI version in a GUI environment!
  8. **
  9. ** History:
  10. **    Jun 92 Created May 1992 By C.T. Barker
  11. **    Feb 93 Simplified, portablised TBL
  12. **    Sep 93 Corrected 3 bugs in HTConfirm() :-( AL
  13. **    Nov 95 Rewritten using callbacks HFN
  14. */
  15. /* Library include files */
  16. #include "WWWLib.h"
  17. #include "HTError.h"  /* Implemented here */
  18. #include "HTAlert.h"  /* Implemented here */
  19. typedef struct _HTAlert {
  20.     HTAlertCallback * cbf;
  21.     HTAlertOpcode opcode;
  22. } HTAlert;
  23. struct _HTAlertPar {
  24.     char * message;
  25.     char * secret;
  26.     void * output;
  27. };
  28. PRIVATE BOOL HTInteractive=YES;     /* Any prompts from the Library? */
  29. PRIVATE HTList * HTMessages = NULL;    /* Global list of alert functions */
  30. /* ------------------------------------------------------------------------- */
  31. /*
  32. ** All messaging can be turned on or off as you like
  33. */
  34. PUBLIC void HTAlert_setInteractive (BOOL interactive)
  35. {
  36.     HTInteractive = interactive;
  37. }
  38. PUBLIC BOOL HTAlert_interactive (void)
  39. {
  40.     return HTInteractive;
  41. }
  42. /* HTAlertCall_add
  43. ** ---------------
  44. ** Register a call back function that is to be called when generating
  45. ** messages, dialog, prompts, progress reports etc.
  46. **
  47. ** The opcode signifies which call back function to call depending of the 
  48. ** type of the message. Opcode can be one of the enumerations defined
  49. ** by HTAlertOpcode.
  50. */
  51. PUBLIC BOOL HTAlertCall_add (HTList * list, HTAlertCallback * cbf,
  52.      HTAlertOpcode opcode)
  53. {
  54.     if (WWWTRACE) 
  55. TTYPrint(TDEST, "Alert Add... HTAlertCallback %pn", (void *) cbf);
  56.     if (list && cbf) {
  57. HTAlert *me;
  58. if ((me = (HTAlert  *) HT_CALLOC(1, sizeof(HTAlert))) == NULL)
  59.     HT_OUTOFMEM("HTAlertCall_add");
  60. me->cbf = cbf;
  61. me->opcode = opcode;
  62. return HTList_addObject(list, (void *) me);
  63.     }
  64.     return NO;
  65. }
  66. /* HTAlertCall_delete
  67. ** ------------------
  68. ** Unregister a call back function from a list
  69. */
  70. PUBLIC BOOL HTAlertCall_delete (HTList * list, HTAlertCallback *cbf)
  71. {
  72.     if (WWWTRACE) 
  73. TTYPrint(TDEST, "Call delete HTAlertCallback %pn", (void *) cbf);
  74.     if (list && cbf) {
  75. HTList *cur = list;
  76. HTAlert *pres;
  77. while ((pres = (HTAlert *) HTList_nextObject(cur))) {
  78.     if (pres->cbf == cbf) {
  79. HTList_removeObject(list, (void *) pres);
  80. HT_FREE(pres);
  81. return YES;
  82.     }
  83. }
  84.     }
  85.     return NO;
  86. }
  87. /* HTAlertCall_deleteAll
  88. ** ---------------------
  89. ** Unregisters all call back functions
  90. */
  91. PUBLIC BOOL HTAlertCall_deleteAll (HTList * list)
  92. {
  93.     if (WWWTRACE) 
  94. TTYPrint(TDEST, "Call delete All callback functionsn");
  95.     if (list) {
  96. HTList *cur = list;
  97. HTAlert *pres;
  98. while ((pres = (HTAlert *) HTList_nextObject(cur))) {
  99.     HTList_removeObject(list, (void *) pres);
  100.     HT_FREE(pres);
  101. }
  102. HTList_delete(list);
  103. return YES;
  104.     }
  105.     return NO;
  106. }
  107. /* HTAlertCall_find
  108. ** ----------------
  109. ** Finds a callback function corresponding to the opcode. If none has
  110. ** been registered then NULL is returned.
  111. */
  112. PUBLIC HTAlertCallback * HTAlertCall_find (HTList * list, HTAlertOpcode opcode)
  113. {
  114.     if (list && HTInteractive) {
  115. HTAlert * pres;
  116. while ((pres = (HTAlert *) HTList_nextObject(list)) != NULL) {
  117.     if (pres->opcode & opcode)
  118. return pres->cbf;
  119. }
  120. if (WWWTRACE)
  121.     TTYPrint(TDEST, "Alert Find.. No entry found for opcode %dn",opcode);
  122.     }
  123.     return NULL;
  124. }
  125. /*
  126. ** Global List of Alert functions. list can be NULL
  127. */
  128. PUBLIC void HTAlert_setGlobal (HTList * list)
  129. {
  130.     HTMessages = list;
  131. }
  132. PUBLIC HTList * HTAlert_global (void)
  133. {
  134.     return HTMessages;
  135. }
  136. PUBLIC BOOL HTAlert_add (HTAlertCallback * cbf, HTAlertOpcode opcode)
  137. {
  138.     if (!HTMessages) HTMessages = HTList_new();
  139.     return HTAlertCall_add(HTMessages, cbf, opcode);
  140. }
  141. PUBLIC BOOL HTAlert_delete (HTAlertCallback * cbf)
  142. {
  143.     if (!HTMessages) HTMessages = HTList_new();
  144.     return HTAlertCall_delete(HTMessages, cbf);
  145. }
  146. /* HTAlert_find
  147. ** ------------
  148. ** Finds a global callback function corresponding to the opcode
  149. */
  150. PUBLIC HTAlertCallback * HTAlert_find (HTAlertOpcode opcode)
  151. {
  152.     return HTAlertCall_find(HTMessages, opcode);
  153. }
  154. PUBLIC HTAlertPar * HTAlert_newReply (void)
  155. {
  156.     HTAlertPar * me;
  157.     if ((me = (HTAlertPar  *) HT_CALLOC(1, sizeof(HTAlertPar))) == NULL)
  158.         HT_OUTOFMEM("HTAlert_newReply");
  159.     return me;
  160. }
  161. /* HTAlert_deleteReply
  162. ** -------------------
  163. ** Delete reply structure but don't touch the replies themselves.
  164. */
  165. PUBLIC void HTAlert_deleteReply (HTAlertPar * old)
  166. {
  167.     if (old) HT_FREE(old);
  168. }
  169. PUBLIC char * HTAlert_replyMessage (HTAlertPar * me)
  170. {
  171.     return me ? me->message : NULL;
  172. }
  173. PUBLIC BOOL HTAlert_setReplyMessage (HTAlertPar * me, CONST char * message)
  174. {
  175.     if (me && message) {
  176. StrAllocCopy(me->message, message);
  177. return YES;
  178.     }
  179.     return NO;
  180. }
  181. PUBLIC BOOL HTAlert_assignReplyMessage (HTAlertPar * me, char * message)
  182. {
  183.     if (me) me->message = message;
  184.     return YES;
  185. }
  186. PUBLIC char * HTAlert_replySecret (HTAlertPar * me)
  187. {
  188.     return me ? me->secret : NULL;
  189. }
  190. PUBLIC BOOL HTAlert_setReplySecret (HTAlertPar * me, CONST char * secret)
  191. {
  192.     if (me && secret) {
  193. StrAllocCopy(me->secret, secret);
  194. return YES;
  195.     }
  196.     return NO;
  197. }
  198. PUBLIC void * HTAlert_replyOutput (HTAlertPar * me)
  199. {
  200.     return me ? me->output : NULL;
  201. }
  202. PUBLIC BOOL HTAlert_setReplyOutput (HTAlertPar * me, void * output)
  203. {
  204.     if (me) {
  205. me->output = output;
  206. return YES;
  207.     }
  208.     return NO;
  209. }
  210. /* TTYPrint
  211. ** --------
  212. ** Single function through which all trace messages must pass - EGP
  213. */
  214. #if WWWTRACE_MODE == WWWTRACE_TTY && !defined(WWW_WIN_WINDOW)
  215. int TTYPrint(FILE* file, CONST char* fmt, ...)
  216. {
  217.     int len;
  218.     char space[513];
  219.     char* pArgs;
  220.     pArgs = (char*)(&fmt + 1);
  221.     len = vsprintf(space, (char*)fmt, (char*)pArgs);
  222.     fprintf(file, space);
  223.     return (len);
  224. }
  225. /* otherwise handled in www.c with the rest of the window stuff */
  226. #endif