home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d527 / toolmanager.lha / ToolManager / Source / toollist.c < prev    next >
C/C++ Source or Header  |  1991-08-05  |  8KB  |  280 lines

  1. /*
  2.  * toollist.c   V1.4
  3.  *
  4.  * tool list handling
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include "ToolManager.h"
  10.  
  11. /* Add one program to tool list */
  12. BOOL AddToolNode(struct ConfigBlock *cb, BPTR fl)
  13. {
  14.  register struct ToolNode *tn;
  15.  BOOL ndummy;
  16.  
  17.  /* This limitation exists because of AppAddMenuItem */
  18.  if (ToolCount>=99) goto e1; /* Not more than 99 Tools :-) possible */
  19.  
  20.  /* Get memory for ToolNode */
  21.  if (!(tn=malloc(sizeof(struct ToolNode))))
  22.   goto e1;
  23.  
  24.  /* Initialize the other parameters */
  25.  tn->tn_Type=cb->cb_Type;
  26.  tn->tn_Flags=cb->cb_Flags;
  27.  tn->tn_Stack=cb->cb_Stack;
  28.  if (tn->tn_Stack<STACKMIN) tn->tn_Stack=STACKMIN; /* Sanity check */
  29.  ndummy=tn->tn_Type!=TNTYPE_DUMMY;
  30.  
  31.  /* No alias? */
  32.  if (cb->cb_Alias[0]=='\0')
  33.   {
  34.    /* Yes, copy real name and set real name to null */
  35.    strcpy(cb->cb_Alias,cb->cb_RealName);
  36.    if (cb->cb_Alias[0]=='\0') goto e2;   /* Empty name --> error */
  37.    cb->cb_RealName[0]='\0';
  38.   }
  39.  
  40.  /* Get memory for menu item name and copy it */
  41.  if (!(tn->tn_Node.ln_Name=strdup(cb->cb_Alias)))
  42.   goto e2;
  43.  
  44.  /* Does the program have a real name? */
  45.  if (ndummy && (cb->cb_RealName[0]!='\0'))
  46.   {
  47.    /* Get memory for real program name and copy it */
  48.    if (!(tn->tn_RealName=strdup(cb->cb_RealName)))
  49.     goto e3;
  50.   }
  51.  else
  52.   tn->tn_RealName=NULL; /* Otherwise: menu item name == real program name */
  53.  
  54.  /* Does the user want a hotkey for the tool? */
  55.  if (ndummy && (cb->cb_HotKey[0]!='\0'))
  56.   {
  57.    /* Get memory for hot key string and copy it */
  58.    if (!(tn->tn_HotKey=strdup(cb->cb_HotKey)))
  59.     goto e4;
  60.  
  61.    /* Build the commodities object from the hot key string */
  62.    if (!(tn->tn_CxObj=HotKey(tn->tn_HotKey,MyBrokerPort,(ULONG) tn)))
  63.     goto e5;
  64.  
  65.    /* Attach it to to our broker */
  66.    AttachCxObj(MyBroker,tn->tn_CxObj);
  67.    if (CxObjError(MyBroker)) goto e6;
  68.   }
  69.  else
  70.   {
  71.    tn->tn_HotKey=NULL;
  72.    tn->tn_CxObj=NULL;
  73.   }
  74.  
  75.  /* If the user wants a menu, add new menu entry, ID == Address of ToolNode */
  76.  if (tn->tn_Flags&TNFLAGS_MENU)
  77.   {
  78.    if (!(tn->tn_MenuItem=AddAppMenuItem((ULONG) tn,NULL,tn->tn_Node.ln_Name,
  79.                                         MyMP,TAG_DONE)))
  80.     goto e6;
  81.   }
  82.  else tn->tn_MenuItem=NULL;
  83.  
  84.  /* User supplied working directory? */
  85.  if (ndummy && (cb->cb_WorkDir[0]!='\0'))
  86.   {
  87.    if (!(tn->tn_WorkDir=strdup(cb->cb_WorkDir)))
  88.     goto e7;
  89.  
  90.    tn->tn_DirLock=Lock(tn->tn_WorkDir,ACCESS_READ);
  91.   }
  92.  else
  93.   {
  94.    tn->tn_WorkDir=NULL;
  95.    tn->tn_DirLock=NULL;
  96.   }
  97.  
  98.  /* Valid directory? No --> duplicate supplied DirLock */
  99.  if (!tn->tn_DirLock && !(tn->tn_DirLock=DupLock(fl)))
  100.   goto e8;
  101.  
  102.  /* If the user wants an icon, add new AppIcon, ID == Address of ToolNode */
  103.  if (ndummy && (tn->tn_Flags&TNFLAGS_ICON))
  104.   {
  105.    BPTR oldfl;
  106.    char *cp;
  107.  
  108.    /* Go to tools directory */
  109.    oldfl=CurrentDir(tn->tn_DirLock);
  110.  
  111.    /* Get icon file name */
  112.    tn->tn_IconFile=NULL;
  113.    if (cb->cb_IconFile[0]!='\0')
  114.     {
  115.      if (!(cp=tn->tn_IconFile=strdup(cb->cb_IconFile)))
  116.       goto e9;
  117.     }
  118.    else if (tn->tn_RealName) cp=tn->tn_RealName;
  119.    else cp=tn->tn_Node.ln_Name;
  120.  
  121.    /* Get icon data */
  122.    if (tn->tn_Flags&TNFLAGS_DOBJ)
  123.     tn->tn_Icon=GetDiskObject(cp);
  124.    else tn->tn_Icon=LoadIFFIcon(cp);
  125.  
  126.    /* Go back to old directory */
  127.    CurrentDir(oldfl);
  128.  
  129.    /* Icon data loaded? */
  130.    if (!tn->tn_Icon)
  131.     goto e10;
  132.  
  133.    /* Create AppIcon */
  134.    tn->tn_Icon->do_CurrentX=cb->cb_IconX;
  135.    tn->tn_Icon->do_CurrentY=cb->cb_IconY;
  136.    if (!(tn->tn_AppIcon=AddAppIcon((ULONG) tn,NULL,tn->tn_Node.ln_Name,MyMP,
  137.                                    NULL,tn->tn_Icon,TAG_DONE)))
  138.     goto e11;
  139.   }
  140.  else
  141.   {
  142.    tn->tn_IconFile=NULL;
  143.    tn->tn_Icon=NULL;
  144.    tn->tn_AppIcon=NULL;
  145.   }
  146.  
  147.  /* Add node to tool list */
  148.  ToolCount++;           /* Increment active tool count */
  149.  DetachToolList();      /* Detach list from ListView gadget */
  150.  AddTail(&ToolList,(struct Node *) tn);
  151.  AttachToolList();      /* Attach list to ListView gadget */
  152.  
  153.  /* Node successfully added to list! */
  154.  return(TRUE);
  155.  
  156.  /* Something went wrong.... */
  157. e11: if (tn->tn_Icon)
  158.       if (tn->tn_Flags&TNFLAGS_DOBJ)
  159.        FreeDiskObject(tn->tn_Icon);
  160.       else
  161.        FreeIFFIcon(tn->tn_Icon);
  162. e10:  if (tn->tn_IconFile) free(tn->tn_IconFile);
  163. e9:  UnLock(tn->tn_DirLock);
  164. e8:  if (tn->tn_WorkDir) free(tn->tn_WorkDir);
  165. e7:  if (tn->tn_MenuItem) RemoveAppMenuItem(tn->tn_MenuItem);
  166. e6:  if (tn->tn_CxObj)
  167.       {
  168.        DeleteCxObjAll(tn->tn_CxObj);
  169. /*       ClearCxObjError(MyBroker); */   /* reset error flag */
  170.       }
  171. e5:  if (tn->tn_HotKey) free(tn->tn_HotKey);
  172. e4:  if (tn->tn_RealName) free(tn->tn_RealName);
  173. e3:  free(tn->tn_Node.ln_Name);
  174. e2:  free(tn);
  175. e1:  return(FALSE);
  176. }
  177.  
  178. /* Scan Workbench parameters and add them to tool list */
  179. BOOL WBAddToolNode(struct WBArg *wbarg, int numargs)
  180. {
  181.  struct ConfigBlock *cb;
  182.  int i;
  183.  BOOL rc=TRUE;
  184.  BPTR fl;
  185.  struct DiskObject *dobj;
  186.  
  187.  /* Get memory */
  188.  if (!(cb=malloc(sizeof(struct ConfigBlock)))) return(rc);
  189.  
  190.  /* Init config block */
  191.  InitConfigBlock(cb);
  192.  
  193.  for (i=numargs; i; i--,wbarg++)            /* Scan all parameters */
  194.   /* Sanity check */
  195.   if (wbarg->wa_Lock && wbarg->wa_Name && (strlen(wbarg->wa_Name)!=0))
  196.    {
  197.     /* Change to icon's directory */
  198.     fl=CurrentDir(wbarg->wa_Lock);
  199.  
  200.     /* Get name of directory */
  201.     if (!NameFromLock(wbarg->wa_Lock,cb->cb_WorkDir,BUFLEN))
  202.      cb->cb_WorkDir[0]='\0'; /* No directory found */
  203.  
  204.     if (dobj=GetDiskObject(wbarg->wa_Name)) /* Get icon data */
  205.      {
  206.       switch (dobj->do_Type) /* Perform action depending on icon type */
  207.        {
  208.         case WBTOOL:    /* Icon is a Tool or */
  209.         case WBPROJECT: /* Icon is a Project, add it as WB tool, no HotKey  */
  210.          cb->cb_Type=TNTYPE_WB;
  211.          strncpy(cb->cb_Alias,wbarg->wa_Name,BUFLEN-1);
  212.          rc&=AddToolNode(cb,wbarg->wa_Lock);
  213.          break;
  214.         default:        /* Every other icon type is erroneous */
  215.          rc&=FALSE;
  216.          break;
  217.        }
  218.       FreeDiskObject(dobj); /* Release icon data */
  219.      }
  220.     else /* Can't get icon data, so we add it as a CLI tool, no HotKey */
  221.      {
  222.       cb->cb_Type=TNTYPE_CLI;
  223.       strncpy(cb->cb_Alias,wbarg->wa_Name,BUFLEN-1);
  224.       rc&=AddToolNode(cb,wbarg->wa_Lock);
  225.      }
  226.  
  227.     CurrentDir(fl); /* Change to old directory */
  228.    }
  229.   else
  230.    rc&=FALSE; /* Error: Bad Workbench parameter */
  231.  
  232.  free(cb);
  233.  return(rc); /* Return TRUE if no error */
  234. }
  235.  
  236. /* Remove ONE node from the tool list */
  237. void RemToolNode(struct ToolNode *tn)
  238. {
  239.  Remove((struct Node *) tn);                 /* Remove node from list */
  240.  
  241.  /* Free Icon stuff */
  242.  if (tn->tn_Flags&TNFLAGS_ICON)
  243.   {
  244.    if (tn->tn_AppIcon) RemoveAppIcon(tn->tn_AppIcon); /* Remove AppIcon */
  245.    if (tn->tn_Flags&TNFLAGS_DOBJ)            /* Free icon data */
  246.     FreeDiskObject(tn->tn_Icon);
  247.    else
  248.     FreeIFFIcon(tn->tn_Icon);
  249.    if (tn->tn_IconFile) free(tn->tn_IconFile); /* Free icon file name */
  250.   }
  251.  
  252.  if (tn->tn_WorkDir) free(tn->tn_WorkDir);   /* Free directory name */
  253.  UnLock(tn->tn_DirLock);                     /* Free directory lock */
  254.  if (tn->tn_MenuItem) RemoveAppMenuItem(tn->tn_MenuItem); /* Rem. menu entry */
  255.  
  256.  /* Free HotKey stuff */
  257.  if (tn->tn_CxObj)
  258.   {
  259.    DeleteCxObjAll(tn->tn_CxObj);             /* Free commodities objects */
  260.    free(tn->tn_HotKey);                      /* Free HotKey string */
  261.   }
  262.  
  263.  if (tn->tn_RealName) free(tn->tn_RealName); /* Free memory */
  264.  free(tn->tn_Node.ln_Name);
  265.  free(tn);
  266.  ToolCount--;                                /* decrement active tool count */
  267. }
  268.  
  269. /* Remove ALL nodes from the tool list */
  270. void RemoveTools(void)
  271. {
  272.  struct ToolNode *tn1,*tn2=GetHead(&ToolList); /* Begin of list */
  273.  
  274.  while (tn1=tn2)
  275.   {
  276.    tn2=GetSucc(tn1); /* Next in list */
  277.    RemToolNode(tn1); /* Remove node */
  278.   }
  279. }
  280.