/**********************************************************************
 * File menu module for xtechinfo
 *
 * $Author: brlewis $
 * $Source: /afs/net.mit.edu/dev/project/techinfodev/src/xtechinfo/RCS/xtifile.c,v $
 * $Header: /afs/net.mit.edu/dev/project/techinfodev/src/xtechinfo/RCS/xtifile.c,v 4.1 94/07/01 22:01:44 brlewis Exp $
 *
 * Copyright 1993 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 **********************************************************************/
#include <mit-copyright.h>

static char rcsid_file_c[] = "$Header: /afs/net.mit.edu/dev/project/techinfodev/src/xtechinfo/RCS/xtifile.c,v 4.1 94/07/01 22:01:44 brlewis Exp $";

#include <string.h>
#include <errno.h>
#include <Xm/Xm.h>
#include <Xm/Text.h>
#include <Xm/Scale.h>
#include <Xm/ToggleBG.h>
#include <X11/Wc/WcCreate.h>
#include <X11/Xmp/Xmp.h>
#include <ti/ti.h>
#include <ti/memory.h>
#include <com_err.h>
#include "xtechinfo.h"
#include "xti.h"
#include "bitmaps/bigdoc"
#include "bitmaps/biggif"
#include "bitmaps/bigfolder"
#include "bitmaps/logo.xbm"

#define ABORT { com_err("xtechinfo", code, "\n%s", ti_context); return; }
#define NULLPIXMAP ((Pixmap) 0)

/**********************************************************************
 * Show Info module for xtechinfo
 **********************************************************************/

/*ARGSUSED*/
void ShowInfoCB(w, unusedstring, unusedptr)
     Widget w;
     char *unusedstring;
     XtPointer unusedptr;
{
  long code;
  static Pixmap biggif, bigfolder, bigdoc, biglogo;
  Pixmap icon;
  ti_node *chosen;
  Pixel fg, bg;
  unsigned int depth;
  char tmpstring[256];
  ti_sourceinfo sourceinfo;
  static XmString *items = NULL;
  char infotext[8000];
  char *FMT = "%16s: [%s]\n";
  char *FMTn = "%16s: [%ld]\n";	/* numeric format */

  /* no item selected? ignore */
  if (!(chosen = SelectedNode())) return;

  if (!glow.infoIcon)
    {
      /* intialize global widget variables */
      glow.infoIcon = WcFullNameToWidget(w, "*infoDialog.infoIcon");
      glow.infoLabel = WcFullNameToWidget(w, "*infoDialog.infoLabel");
      glow.infoText = WcFullNameToWidget(w, "*infoDialog*infoText");

      /* get fg, bg, depth of widget */
      depth = DefaultDepth(XtDisplay(glow.infoIcon),
			   XScreenNumberOfScreen(XtScreen(glow.infoIcon)));
      XtVaGetValues(glow.infoIcon,
		    XmNforeground, &fg,
		    XmNbackground, &bg,
		    NULL);

      /* initialize pixmaps */
      biggif =
	XCreatePixmapFromBitmapData(XtDisplay(glow.infoIcon),
				    XtWindow(glow.infoIcon),
				    biggif_bits,
				    biggif_width, biggif_height,
				    fg, bg, depth);
      bigdoc =
	XCreatePixmapFromBitmapData(XtDisplay(glow.infoIcon),
				    XtWindow(glow.infoIcon),
				    bigdoc_bits,
				    bigdoc_width, bigdoc_height,
				    fg, bg, depth);
      bigfolder =
	XCreatePixmapFromBitmapData(XtDisplay(glow.infoIcon),
				    XtWindow(glow.infoIcon),
				    bigfolder_bits,
				    bigfolder_width, bigfolder_height,
				    fg, bg, depth);
      biglogo =
	XCreatePixmapFromBitmapData(XtDisplay(glow.infoIcon),
				    XtWindow(glow.infoIcon),
				    logo_bits,
				    logo_width, logo_height,
				    fg, bg, depth);
    }

  /* choose correct icon */
  icon = bigdoc;
  if (ti_ismenu(chosen)) icon = bigfolder;
  if (ti_isimage(chosen)) icon = biggif;
  if (CurrentMenu->type == TI_MWWT) icon = biglogo;
  XtVaSetValues(glow.infoIcon, XmNlabelPixmap, icon, NULL);

  /* set label */
  XtVaSetValues(glow.infoLabel, XmNlabelString,
		XmStringCreateSimple(ti_title(chosen)), NULL);

  if (CurrentMenu->type == TI_MWWT)
    {
      /* server info */
      sprintf(infotext, FMT, GlobalResources.str25, ti_wwhost(chosen));
      sprintf(tmpstring, FMTn, GlobalResources.str26, ti_wwport(chosen));
      strcat(infotext, tmpstring);
    }
  else
    {
      /* get source info */
      if (code = ti_getsrcbyid(global.menus[global.current]->tp,
			       ti_id(chosen), &sourceinfo))
	{
	  sprintf(infotext, "%s\n%s\n", error_message(code), ti_context);
	}
      else
	{
	  sprintf(infotext, FMT, GlobalResources.str27, sourceinfo.lsource);
	  sprintf(tmpstring, FMT, GlobalResources.str28, sourceinfo.name);
	  strcat(infotext, tmpstring);
	  sprintf(tmpstring, FMT, GlobalResources.str29, sourceinfo.email);
	  strcat(infotext, tmpstring);
	  sprintf(tmpstring, FMT, GlobalResources.str30, sourceinfo.phone);
	  strcat(infotext, tmpstring);
	}
      sprintf(tmpstring, FMT, GlobalResources.str31, ti_source(chosen));
      strcat(strcat(infotext, tmpstring), "\n");

      /* Node info */
      sprintf(tmpstring, FMT, GlobalResources.str32, ti_topic(chosen));
      strcat(infotext, tmpstring);
      sprintf(tmpstring, FMT, GlobalResources.str33, 
	      DateString(ti_date(chosen)));
      strcat(infotext, tmpstring);
      sprintf(tmpstring, FMTn, GlobalResources.str34, ti_id(chosen));
      strcat(infotext, tmpstring);
      if (*ti_locker(chosen))
	{
	  sprintf(tmpstring, FMT, GlobalResources.str35, ti_locker(chosen));
	  strcat(infotext, tmpstring);
	}
      if (*ti_file(chosen))
	{
	  ti_recvinfo rcvinfo;

	  sprintf(tmpstring, FMT, GlobalResources.str36, ti_file(chosen));
	  strcat(strcat(infotext, tmpstring), "\n");

	  /* get size info */
	  if (code = ti_recvbuf(CurrentMenu->tp, ti_id(chosen), 0, 1,
				tmpstring, &rcvinfo))
	    {
	      sprintf(tmpstring, "%s\n%s\n", error_message(code), ti_context);
	      strcat(infotext, tmpstring);
	    }
	  else
	    {
	      sprintf(tmpstring, FMTn, GlobalResources.str37, rcvinfo.total);
	      strcat(strcat(infotext, tmpstring), rcvinfo.modified);
	    }
	}
    }

  /* put text into widget */
  XmTextSetString(glow.infoText, infotext);

  return;
}

/**********************************************************************
 * Provider Authentication module for xtechinfo
 **********************************************************************/

/*ARGSUSED*/
void Authenticate(button_w, client_data, cbs)
     Widget button_w;
     XtPointer client_data;
     XmAnyCallbackStruct *cbs;
{
  long code;
  char *username, *password;
  Widget passW;

  /* get username, password */
  XtVaGetValues(WcFullNameToWidget(button_w, "*providerTable.input1"),
		XmNvalue, &username, NULL);
  XtVaGetValues(passW=WcFullNameToWidget(button_w, "*providerTable.input2"),
		XmNvalue, &password, NULL);

  /* clear password field after we're done with it */
  XtVaSetValues(passW, XmNvalue, "", NULL);

  /* Try to authenticate */
  if (code = ti_auth(global.tp, username, password)) ABORT;

  /* allocate node to modify */
  if (code = ti_newnode(&(global.to_modify))) ABORT;

  /* Map provider menu */
  XtManageChild(WcFullNameToWidget(button_w, "*provider"));
  global.am_provider = True;

  if (code=BuildListWidget(&global)) ABORT;

  return;
}

/**********************************************************************
 * Kerberos Provider Authentication module for xtechinfo
 **********************************************************************/

/*ARGSUSED*/
void KrbAuthenticate(button_w, client_data, cbs)
     Widget button_w;
     XtPointer client_data;
     XmAnyCallbackStruct *cbs;
{
  long code;

  /* Try to authenticate */
  if (code = ti_auth(global.tp, TI_KERBEROS, "")) ABORT;

  /* allocate node to modify */
  if (code = ti_newnode(&(global.to_modify))) ABORT;

  /* Map provider menu */
  XtManageChild(WcFullNameToWidget(button_w, "*provider"));
  global.am_provider = True;

  if (code=BuildListWidget(&global)) ABORT;

  return;
}

/*ARGSUSED*/
void ChooseAuth(button_w, client_data, cbs)
     Widget button_w;
     XtPointer client_data;
     XmAnyCallbackStruct *cbs;
{
  if (GlobalResources.kerberos)
    {
      KrbAuthenticate(button_w, client_data, cbs);
      return;
    }

  if (!WcFullNameToWidget(glow.appshell, "*providerDialog"))
    WcCreatePopup(glow.appshell, "providerDialog");

  XtManageChild(WcFullNameToWidget(button_w, "*providerDialog"));

  XmProcessTraversal
    (WcFullNameToWidget(glow.appshell, "*providerTable.input1"),
     XmTRAVERSE_CURRENT);

  return;
}

/**********************************************************************
 * Prefrences module for xtechinfo
 **********************************************************************/

/*ARGSUSED*/
void PrefPrep(w, client_data, cbs)
     Widget w;
     XtPointer client_data;
     XmAnyCallbackStruct *cbs;
{
  /* initialize global widget variables */
  if (!glow.prefSearch)
    {
      glow.prefKrb = WcFullNameToWidget(w, "*preferenceDialog*krb");
      glow.prefSearch = WcFullNameToWidget(w, "*preferenceDialog*current");
      glow.prefDate = WcFullNameToWidget(w, "*preferenceDialog*showdate");
      glow.prefSource = WcFullNameToWidget(w, "*preferenceDialog*showsource");
      glow.prefNormal = WcFullNameToWidget(w, "*preferenceDialog*normal");
      glow.prefOutline = WcFullNameToWidget(w, "*preferenceDialog*outline");
    }

  /* set buttons according to preferences */
  XmToggleButtonGadgetSetState(glow.prefKrb,
			       GlobalResources.kerberos, True);
  XmToggleButtonGadgetSetState(glow.prefSearch,
			       !(GlobalResources.frommain), True);
  XmToggleButtonGadgetSetState(glow.prefDate,
			       GlobalResources.showdate, True);
  XmToggleButtonGadgetSetState(glow.prefSource,
			       GlobalResources.showsource, True);

  /* set scale values */
  XmScaleSetValue(glow.prefNormal, GlobalResources.ndepth);
  XmScaleSetValue(glow.prefOutline, GlobalResources.odepth);

  return;
}

/*ARGSUSED*/
void PrefApply(w, client_data, cbs)
     Widget w;
     XtPointer client_data;
     XmAnyCallbackStruct *cbs;
{
  long code;

  /* set preferences according to buttons */
  GlobalResources.kerberos = XmToggleButtonGadgetGetState(glow.prefKrb);
  GlobalResources.frommain = !(XmToggleButtonGadgetGetState(glow.prefSearch));
  GlobalResources.showdate = XmToggleButtonGadgetGetState(glow.prefDate);
  GlobalResources.showsource = XmToggleButtonGadgetGetState(glow.prefSource);

  /* get scale values */
  XmScaleGetValue(glow.prefNormal, &(GlobalResources.ndepth));
  XmScaleGetValue(glow.prefOutline, &(GlobalResources.odepth));

  if (code=BuildListWidget(&global)) ABORT;
  return;
}

/**********************************************************************
 * Comment module for xtechinfo
 **********************************************************************/

#define ABORT { com_err("xtechinfo", code, "\n%s", ti_context); return; }

/*ARGSUSED*/
void CommentPrep(w, client_data, cbs)
     Widget w;
     XtPointer client_data;
     XmAnyCallbackStruct *cbs;
{
  long code;
  char initial_text[4096];
  char tmpstring[256];
  ti_node *chosen;
  ti_sourceinfo sourceinfo;

  /* no item selected? ignore */
  if (!(chosen = SelectedNode())) return;

  /* get source info */
  if (code = ti_getsrcbyid(CurrentTI, ti_id(chosen), &sourceinfo))
    ABORT;

  /* simple check of validity of email address */
  if (!index(sourceinfo.email, '@'))
    {
      strcpy(ti_context, sourceinfo.email);
      code = XTI_ERR_BADEMAIL;
      ABORT;
    }

  /* find text widget */
  if (!glow.commentText)
    glow.commentText = WcFullNameToWidget(w, "*commentText");

  /* set up To: and Subject: fields */
  sprintf(initial_text, "To: %s\nSubject: %s\n\n",
	  sourceinfo.email, ti_title(chosen));

  /* tell what we're connected to, etc. */
  sprintf(tmpstring, "(%s %s %s %s via %s %s)\n",
	  GlobalResources.str38, ti_host(CurrentTI), 
	  GlobalResources.str39, ti_port(CurrentTI),
	 XTECHINFO_TITLE, XTECHINFO_VERSION);
  strcat(initial_text, tmpstring);

  /* tell what we're looking at */
  sprintf(tmpstring, "%s %s", GlobalResources.str40,ti_title(global.nodes[0]));
  strcat(initial_text, tmpstring);
  if (ti_id(global.nodes[0]))
    {
      sprintf(tmpstring, " (id %d)", ti_id(global.nodes[0]));
      strcat(initial_text, tmpstring);
    }

  /* tell what item is currently selected */
  sprintf(tmpstring, "\n%s%s", GlobalResources.str41, ti_title(chosen));
  strcat(initial_text, tmpstring);
  if (ti_id(chosen))
    {
      sprintf(tmpstring, " (id %d)", ti_id(chosen));
      strcat(initial_text, tmpstring);
    }
  strcat(initial_text, "\n\n");

  /* set initial text */
  XmTextSetString(glow.commentText, initial_text);
  XmTextSetInsertionPosition(glow.commentText, strlen(initial_text));

  return;
}


/*ARGSUSED*/
void CommentSend(w, client_data, cbs)
     Widget w;
     XtPointer client_data;
     XmAnyCallbackStruct *cbs;
{
  long code;
  char *text;
  FILE *pipefp;

  if (!(pipefp = popen(SENDMAIL, "w")))
    {
      code = errno;
      strcpy(ti_context, SENDMAIL);
      ABORT;
    }
  text = XmTextGetString(glow.commentText);
  fprintf(pipefp, "%s\n.\n", text);
  XtFree(text);

  if (code = (long) pclose(pipefp))
    {
      sprintf(ti_context, "%ld", code);
      code = XTI_ERR_SENDMAIL;
      ABORT;
    }
  return;
}
