#include "ToolManager.h"

/* Add one program to tool list */
BOOL AddToolNode(BPTR dir, char *name, char *realname)
{
 static ULONG ToolID=0; /* Counter for Tool ID */
 struct ToolNode *tn;

 if (ToolCount>=99) goto e1; /* Not more than 99 Tools :-) possible */

 /* Get memory for ToolNode */
 if (!(tn=malloc(sizeof(struct ToolNode))))
  goto e1;

 /* Get memory for menu item name and copy it */
 if (!(tn->tn_Node.ln_Name=strdup(name)))
  goto e2;

 /* Does the program have a real name? */
 if (realname)
  {
   /* Get memory for real program name and copy it */
   if (!(tn->tn_RealName=strdup(realname)))
    goto e3;
  }
 else
  tn->tn_RealName=NULL; /* Otherwise: menu item name == real program name */

 /* Add a new menu entry */
 if (!(tn->tn_MenuItem=AddAppMenuItem(ToolID+1,NULL,tn->tn_Node.ln_Name,
                                      MyMP,NULL)))
  goto e4;

 if (!(tn->tn_DirLock=DupLock(dir))) /* Duplicate directory lock */
  goto e5;

 /* Add node to tool list */
 tn->tn_ID=++ToolID;    /* Set Tool ID */
 ToolCount++;           /* Increment active tool count */
 DetachToolList();      /* Detach list from ListView gadget */
 AddTail(&ToolList,tn);
 AttachToolList();      /* Attach list to ListView gadget */

 /* Node successfully added to list! */
 return(TRUE);

 /* Something went wrong.... */
e5: RemoveAppMenuItem(tn->tn_MenuItem);
e4: if (tn->tn_RealName) free(tn->tn_RealName);
e3: free(tn->tn_Node.ln_Name);
e2: free(tn);
e1: return(FALSE);
}

/* Scan Workbench parameters and add them to tool list */
BOOL WBAddToolNode(struct WBArg *wbarg, int numargs)
{
 int i;
 BOOL rc=TRUE;
 BPTR fl;
 struct DiskObject *dobj;

 for (i=0; i<numargs; i++, wbarg++) /* Scan all parameters */
  if ((wbarg->wa_Lock) && (strlen(wbarg->wa_Name)!=0)) /* Sanity check */
   {
    fl=CurrentDir(wbarg->wa_Lock);          /* Change to icon's directory */
    if (dobj=GetDiskObject(wbarg->wa_Name)) /* Get icon data */
     {
      switch(dobj->do_Type) /* Perform action depending on icon type */
       {
        case WBTOOL:    /* Icon is a Tool. Menu entry == real program name */
         rc&=AddToolNode((struct FileLock *) wbarg->wa_Lock,wbarg->wa_Name,
                         NULL);
         break;
        case WBPROJECT: /* Icon is a Project. Menu entry = icon name,
                           real program name = default tool name */
         rc&=AddToolNode((struct FileLock *) wbarg->wa_Lock,wbarg->wa_Name,
                         dobj->do_DefaultTool);
         break;
        default:        /* Every other icon type is erroneous */
         rc&=FALSE;
         break;
       }
      FreeDiskObject(dobj); /* Release icon data */
     }
    else
     rc&=FALSE; /* Error: Can't get icon data */

    CurrentDir(fl); /* Change to old directory */
   }
  else
   rc&=FALSE; /* Error: Bad Workbench parameter */

 return(rc); /* Return TRUE if no error */
}

/* Remove ONE node from the tool list */
void RemToolNode(struct ToolNode *tn)
{
 Remove(tn);                                 /* Remove node from list */
 UnLock(tn->tn_DirLock);                     /* Free directory */
 RemoveAppMenuItem(tn->tn_MenuItem);         /* Remove menu entry */
 if (tn->tn_RealName) free(tn->tn_RealName); /* Free memory */
 free(tn->tn_Node.ln_Name);
 free(tn);
 ToolCount--;                                /* decrement active tool count */
}

/* Remove ALL nodes from the tool list */
void RemoveTools(void)
{
 struct ToolNode *tn1,*tn2=GetHead(&ToolList); /* Begin of list */

 while (tn1=tn2)
  {
   tn2=GetSucc(tn1); /* Next in list */
   RemToolNode(tn1); /* Remove node */
  }
}
