home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Commun⁄Network / NewsWatcher 2.0d17 Source / source / subscribe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-20  |  5.7 KB  |  214 lines  |  [TEXT/KAHL]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     subscribe.c
  4.  
  5.     This module handles subscribing and unsubscribing to groups.
  6.     
  7.     Portions copyright © 1990, Apple Computer.
  8.     Portions copyright © 1993, Northwestern University.
  9.  
  10. ----------------------------------------------------------------------------*/
  11.  
  12. #include <string.h>
  13.  
  14. #include "glob.h"
  15. #include "dlgutil.h"
  16. #include "child.h"
  17. #include "close.h"
  18. #include "resize.h"
  19. #include "subscribe.h"
  20. #include "util.h"
  21. #include "nntp.h"
  22. #include "wind.h"
  23.  
  24.  
  25.  
  26. /*----------------------------------------------------------------------------
  27.     AddNewGroup 
  28.     
  29.     Adds a new group to a user group window.
  30.     
  31.     Entry:    nameOffset = offset in gGroupNames of group name to add.
  32.             wind = pointer to user group window.
  33.             pos = position of new user group list entry (row number
  34.                 of new cell). Pass 0x7fff to add at end of list.
  35.  
  36.     Exit:    function result = 
  37.                 0 if new group added.
  38.                 1 if group already present in window.
  39.                 2 if some other error.
  40. ----------------------------------------------------------------------------*/
  41.  
  42. static short AddNewGroup (long nameOffset, WindowPtr wind, short pos)
  43. {
  44.     ListHandle theList;
  45.     Cell theCell;
  46.     TWindow **info;
  47.     TGroup **groupArray, newGroup;
  48.     short numCells, cellData, cellDataLen, newGroupIndex;
  49.     Handle strings;
  50.     TUnread **unread;
  51.     CStr255 groupName;
  52.  
  53.     info = (TWindow**)GetWRefCon(wind);
  54.     theList = (**info).theList;
  55.     numCells = (**theList).dataBounds.bottom;
  56.     groupArray = (**info).groupArray;
  57.     strings = (**info).strings;
  58.     strcpy(groupName, *strings + nameOffset);
  59.  
  60.     theCell.h = 0;
  61.     for (theCell.v = 0; theCell.v < numCells; theCell.v++) {
  62.         cellDataLen = 2;
  63.         LGetCell(&cellData, &cellDataLen, theCell, theList);
  64.         if (strcmp(groupName, *strings + (*groupArray)[cellData].nameOffset) == 0) return 1;
  65.     }
  66.     
  67.     newGroup.nameOffset = nameOffset;
  68.     newGroup.firstMess = newGroup.lastMess = newGroup.numUnread = 0;
  69.     newGroup.onlyRedrawCount = false;
  70.     if (GetGroupArticleRange(&newGroup) == 2) return 2;
  71.     if (newGroup.firstMess <= newGroup.lastMess) {
  72.         unread = (TUnread**)MyNewHandle(sizeof(TUnread));
  73.         (**unread).firstUnread = newGroup.firstMess;
  74.         (**unread).lastUnread = newGroup.lastMess;
  75.         (**unread).next = nil;
  76.         newGroup.unread = unread;
  77.     } else {
  78.         newGroup.unread = nil;
  79.     }
  80.  
  81.     MySetHandleSize((Handle)groupArray, GetHandleSize((Handle)groupArray) + sizeof(TGroup));
  82.     newGroupIndex = (**info).numGroups;
  83.     (**info).numGroups++;
  84.     (*groupArray)[newGroupIndex] = newGroup;
  85.  
  86.     LDoDraw(false, theList);
  87.     pos = LAddRow(1, pos, theList);
  88.     SetPt(&theCell, 0, pos);
  89.     LSetCell(&newGroupIndex, 2, theCell, theList);
  90.     LDoDraw(true, theList);
  91.  
  92.     SetPort(wind);
  93.     InvalRect(&wind->portRect);
  94.     
  95.     (**info).changed = true;
  96.     return 0;
  97. }
  98.  
  99.  
  100.  
  101. /*----------------------------------------------------------------------------
  102.     SubscribeSelected 
  103.     
  104.     Subscribes to all selected newsgroups.
  105.     
  106.     Entry:    srcWindow = pointer to source window.
  107.             destWindow = pointer to destination window.
  108.             pos = position of new user group list entries in destination
  109.                 window (starting row number of new cells). 
  110.                 Pass 0x7fff to add at end of list.
  111. ----------------------------------------------------------------------------*/
  112.  
  113. void SubscribeSelected (WindowPtr srcWindow, WindowPtr destWindow, short pos)
  114. {
  115.     Cell srcCell;
  116.     TWindow **srcInfo;
  117.     TGroup **srcGroupArray;
  118.     ListHandle srcList;
  119.     short cellData, cellDataLen;
  120.     short result;
  121.     short numSelected=0, numSubscribed=0;
  122.     
  123.     srcInfo = (TWindow**)GetWRefCon(srcWindow);
  124.     srcList = (**srcInfo).theList;
  125.     srcGroupArray = (**srcInfo).groupArray;
  126.     SetPt(&srcCell,0,0);
  127.     while (LGetSelect(true, &srcCell, srcList)) {
  128.         numSelected++;
  129.         cellDataLen = 2;
  130.         LGetCell(&cellData, &cellDataLen, srcCell, srcList);
  131.         result = AddNewGroup((*srcGroupArray)[cellData].nameOffset , destWindow, pos);
  132.         if (result == 2) return;
  133.         if (result == 0) numSubscribed++;
  134.         srcCell.v++;
  135.         pos++;
  136.     }
  137.     if (numSubscribed > 0) {
  138.         if (gPrefs.zoomWindows) {
  139.             DoZoom(destWindow, inZoomOut);
  140.         } else {
  141.             SetWindowNeedsZooming(destWindow);
  142.         }
  143.     }
  144.     if (numSelected == numSubscribed) return;
  145.     if (numSelected == 1) {
  146.         ErrorMessage("That group has already been subscribed to.");
  147.     } else if (numSubscribed > 0) {
  148.         ErrorMessage("One or more of those groups have already been subscribed to.");
  149.     } else {
  150.         ErrorMessage("All of those groups have already been subscribed to.");
  151.     }
  152.     return;
  153. }
  154.  
  155.  
  156.  
  157. /*----------------------------------------------------------------------------
  158.     DoSubscribe 
  159.     
  160.     Handles the Subscribe command.
  161.     
  162.     Entry:    wind = pointer to group list window.
  163. ----------------------------------------------------------------------------*/
  164.  
  165. void DoSubscribe (WindowPtr wind)
  166. {
  167.     TWindow **info, **destInfo;
  168.     WindowPtr destWindow;
  169.     
  170.     info = (TWindow**)GetWRefCon(wind);
  171.         
  172.     destWindow = (WindowPtr) ((WindowPeek)wind)->nextWindow;
  173.     while (destWindow != nil) {
  174.         destInfo = (TWindow**)GetWRefCon(destWindow);
  175.         if ((**destInfo).kind == kUserGroup) break;
  176.         destWindow = (WindowPtr)((WindowPeek)destWindow)->nextWindow;
  177.     }
  178.     if (!destWindow) return;
  179.     
  180.     SubscribeSelected(wind, destWindow, 0x7fff);
  181. }
  182.  
  183.  
  184.  
  185. /*----------------------------------------------------------------------------
  186.     DoUnsubscribe 
  187.     
  188.     Handles the Unsubscribe command.
  189.     
  190.     Entry:    wind = pointer to user group list window.
  191. ----------------------------------------------------------------------------*/
  192.  
  193. void DoUnsubscribe (WindowPtr wind)
  194. {
  195.     WindowPtr child;
  196.     TWindow **info;
  197.     ListHandle theList;
  198.     Cell theCell;
  199.     short numDel = 0;
  200.     
  201.     info = (TWindow**)GetWRefCon(wind);
  202.     theList = (**info).theList;
  203.     
  204.     SetPt(&theCell,0,0);
  205.     while (LGetSelect(true, &theCell, theList)) {
  206.         if ((child = FindChild(wind, theCell)) != nil) DoCloseWindow(child);
  207.         LDelRow(1, theCell.v, theList);
  208.         (**info).changed = true;
  209.         numDel++;
  210.     }
  211.     if (numDel) SetWindowNeedsZooming(wind);
  212. }
  213.  
  214.