
/* Info.c: information panels, Legal and ye olde hande holdere 
 *
 *  This file is part of Aileron, a WINGs email client. 
 *
 *  Copyright (c) 1999 - 2001  Nwanua Elumeze
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include "Aileron.h"



struct {
  WMWindow *window;
} InfoPanel;
 

void closeInfoPanel(WMWidget *self, void *client)
{
  WMUnmapWidget(InfoPanel.window);
}

void RunInfoPanel(void)
{
  WMLabel *mailbox, *version, *title, *desc, *credits, *contact;
  WMFrame *line;
  RColor blendcolor;
  WMColor *textcolor;
  WMPixmap *pic;
  WMFont *font;
  char *path;
  

  /* if it exists, raise it to the foreground, otherwise create it */
  if(Aileron.infoCreated) {
    XRaiseWindow(Aileron.display, WMWidgetXID(InfoPanel.window));
    WMMapWidget(InfoPanel.window);
    return;
  }

  textcolor = WMGrayColor(Aileron.screen);
  blendcolor.red = WMRedComponentOfColor(textcolor) >> 8;
  blendcolor.green = WMGreenComponentOfColor(textcolor) >> 8;
  blendcolor.blue = WMBlueComponentOfColor(textcolor) >> 8;
  blendcolor.alpha = 0;
  WMReleaseColor(textcolor);
  
  textcolor = WMDarkGrayColor(Aileron.screen);
  
  InfoPanel.window = WMCreateWindowWithStyle(Aileron.screen, "Aileron: Info", 
    WMTitledWindowMask|WMClosableWindowMask);
  WMSetWindowTitle(InfoPanel.window, "Aileron: Info");
  WMSetWindowCloseAction(InfoPanel.window, closeInfoPanel, NULL);
  WMResizeWidget(InfoPanel.window, 380, 150);
  
  mailbox = WMCreateLabel(InfoPanel.window);
  
  path = WMPathForResourceOfType("info-image.tif", "images/infopanel");
  if(path) {
    pic = WMCreateBlendedPixmapFromFile(Aileron.screen, path, &blendcolor);
    WMSetLabelImage(mailbox, pic);
    WMSetLabelImagePosition(mailbox, WIPImageOnly);
    WMResizeWidget(mailbox, 163, 148);
    WMMoveWidget(mailbox, 5, 3);
    WMReleasePixmap(pic);
    free(path);
  }
  
  font = WMSystemFontOfSize(Aileron.screen, 10);
    version = WMCreateLabel(InfoPanel.window);
  WMSetLabelText(version, VERSION);
  WMSetLabelFont(version, font);
  WMSetLabelTextColor(version, textcolor);
  WMResizeWidget(version, 30, 12);
  WMMoveWidget(version, 336, 47);
  WMReleaseFont(font);

  title = WMCreateLabel(InfoPanel.window);
    WMSetLabelTextAlignment(title, WACenter);
  path = WMPathForResourceOfType("info-title.tif", "images/infopanel");
  if(path) {
    pic = WMCreateBlendedPixmapFromFile(Aileron.screen, path, &blendcolor);
    WMSetLabelImage(title, pic);
    WMSetLabelImagePosition(title, WIPImageOnly);
    WMResizeWidget(title, 180, 58);
    WMMoveWidget(title, 157, 8);
    WMReleasePixmap(pic);
    free(path);
  }
  
  desc = WMCreateLabel(InfoPanel.window);
    WMSetLabelTextAlignment(desc, WACenter);
  font = WMSystemFontOfSize(Aileron.screen, 24);
  WMSetLabelText(desc, "WINGs email client");
  WMSetLabelFont(desc, font);
  WMResizeWidget(desc, 220, 24);
  WMMoveWidget(desc, 150, 65);
  WMReleaseFont(font);
  
  line = WMCreateFrame(InfoPanel.window);
  WMSetFrameRelief(line, WRGroove);
  WMResizeWidget(line, 225, 2);
  WMMoveWidget(line, 150, 95);
  
  credits = WMCreateLabel(InfoPanel.window);
  font = WMSystemFontOfSize(Aileron.screen, 10);
  WMSetLabelText(credits, "Nwanua Elumeze");
  WMSetLabelFont(credits, font);
  WMResizeWidget(credits, 100, 12);
  WMMoveWidget(credits, 171, 107);
  
  contact = WMCreateLabel(InfoPanel.window);
  WMSetLabelText(contact, "nwanua@windowmaker.org");
  WMSetLabelFont(contact, font);
  WMSetLabelTextColor(contact, textcolor);
  WMResizeWidget(contact, 200, 12);
  WMMoveWidget(contact, 171, 122);
  WMReleaseFont(font);
  WMReleaseColor(textcolor);
  
  WMRealizeWidget(InfoPanel.window);
  
  //XSetWMHints(Aileron.display, WMWidgetXID(InfoPanel.window), HINTS);
  
  WMMapSubwidgets(InfoPanel.window);

  WMAppAddWindow(Aileron.appContext, WMWidgetXID(InfoPanel.window));
  WMHashInsert(Aileron.windowTable, "_INFO_PANEL_", &InfoPanel);
  
  WMMapWidget(InfoPanel.window);

  Aileron.infoCreated = True;
}



struct {
  WMWindow *window;
} GPLPanel;
 

void closeGPLPanel(WMWidget *self, void *client)
{
  WMUnmapWidget(GPLPanel.window);
}

void runAlt(WMText *tPtr)
{
  WMAppendTextStream(tPtr,  
"<htmL><p><i><b>Aileron</b></i> is a WINGs email client that has been \
designed for speed, simplicity, and above all, beauty 
copyright (C) 1999 - 2001 : <u>nwanua@windowmaker.org</u>  <html>");
}


void RunGPLPanel(void)
{
  WMText *text;
  FILE *fp;
  struct stat filestat;
  char *html = NULL;


  if(Aileron.gplCreated) {
    XRaiseWindow(Aileron.display, WMWidgetXID(GPLPanel.window));
    WMMapWidget(GPLPanel.window);
    return;
  }   
  
  GPLPanel.window = WMCreateWindowWithStyle(Aileron.screen, "Aileron: Legal",
    WMTitledWindowMask | WMClosableWindowMask);
  WMSetWindowTitle(GPLPanel.window, "Aileron: Legal");
  WMSetWindowCloseAction(GPLPanel.window, closeGPLPanel, NULL);
  WMResizeWidget(GPLPanel.window, 550, 300);

  text = WMCreateTextForDocumentType(GPLPanel.window, HTMLParser, NULL);
    WMSetTextEditable(text, False);
    WMSetTextHasVerticalScroller(text, True);
  WMResizeWidget(text, 530, 280);
  WMMoveWidget(text, 10, 10);

  WMSetTextBackgroundColor(text, 
    WMCreateNamedColor(Aileron.screen, "gray", False));
  
  WMFreezeText(text);

  fp = fopen("AUTHORS.html", "r");
  if(!fp) {
    fp = fopen("AUTHORS", "r");
    if(!fp) { 
      runAlt(text);
      goto _RealizePanel;
    }
  }

  if(stat("AUTHORS.html", &filestat) != 0) {
    if(stat("AUTHORS", &filestat) != 0) {
      fclose(fp); 
      runAlt(text);
      goto _RealizePanel;
    }
  }

  html = wmalloc(filestat.st_size + 1);
  if(!html) { 
    runAlt(text);
    goto _RealizePanel;
  }

  fread(html, 1, filestat.st_size, fp);
  fclose(fp);
  html[filestat.st_size] = 0;
  WMAppendTextStream(text, html); 
  wfree(html);
                 

_RealizePanel:
  WMRealizeWidget(GPLPanel.window);
  WMMapSubwidgets(GPLPanel.window);
  WMAppAddWindow(Aileron.appContext, WMWidgetXID(GPLPanel.window));
  WMHashInsert(Aileron.windowTable, "_GPL_PANEL_", &GPLPanel);
  WMMapWidget(GPLPanel.window);
  WMThawText(text);

  return;
}



struct {
  WMWindow *window;
  WMText *text;
  WMPopUpButton *pagep;
  int page;
  unsigned short doneForIntro:1;
  unsigned short RESERVED:15;
} HelpPanel;



void CloseHelpPanel(void)
{
    if(!Aileron.helpCreated) 
      return;

  WMUnmapWidget(HelpPanel.window);
    if(HelpPanel.page == 255)
      QuitAileron();
    if(HelpPanel.page == 254) {
      HelpPanel.doneForIntro = True;
      //RunPrefsPanel(True);
    }

}

static void closeHelpPanel(WMWidget *self, void *client)
{
  CloseHelpPanel();
}

static void bcloseHelpPanel(WMWidget *w, void *client)
{
  CloseHelpPanel();
}

static void
changeForItemType(WMWidget *w, void *data)
{
  HelpPanel.page = WMGetPopUpButtonSelectedItem((WMPopUpButton*) w);
  RunHelpPanel(HelpPanel.page);
}

static void
runHelpPanel(void) 
{
  WMButton *b;

  HelpPanel.window = WMCreateWindowWithStyle(Aileron.screen, "Aileron: Help",
    WMTitledWindowMask|WMClosableWindowMask);

  WMSetWindowCloseAction(HelpPanel.window, closeHelpPanel, NULL);
  WMSetWindowTitle(HelpPanel.window, "Aileron: Help");
  WMResizeWidget(HelpPanel.window, 550, 325);

  HelpPanel.text = WMCreateTextForDocumentType(HelpPanel.window,
      HTMLParser, NULL);
    WMSetTextHasVerticalScroller(HelpPanel.text, True);
  WMResizeWidget(HelpPanel.text, 530, 270);
  WMMoveWidget(HelpPanel.text, 10, 10);
    WMSetTextEditable(HelpPanel.text, False);
    
  HelpPanel.pagep = WMCreatePopUpButton(HelpPanel.window);
  WMResizeWidget(HelpPanel.pagep, 165, 25);
  WMMoveWidget(HelpPanel.pagep, 300, 290);
  WMAddPopUpButtonItem(HelpPanel.pagep, "Introduction");
  WMAddPopUpButtonItem(HelpPanel.pagep, "Reading Mail");
  WMAddPopUpButtonItem(HelpPanel.pagep, "Composing Mail");
  WMAddPopUpButtonItem(HelpPanel.pagep, "Transfering Mail");
  WMAddPopUpButtonItem(HelpPanel.pagep, "Creating Mailboxes");
  WMAddPopUpButtonItem(HelpPanel.pagep, "Using the Find Panel");
  WMSetPopUpButtonText(HelpPanel.pagep, "Introduction");
  WMSetPopUpButtonAction(HelpPanel.pagep, changeForItemType, NULL);
#if 0
  WMSetPopUpButtonAction(HelpPanel.pagep, buttonGenericAction, NULL);
#endif



  b = WMCreateCommandButton(HelpPanel.window);
  WMResizeWidget(b, 65, 25);
  WMMoveWidget(b, 475, 290);
  WMSetButtonText(b, "Close");
  WMSetButtonImageDefault(b);
  WMSetButtonImagePosition(b, WIPRight);
  WMSetButtonAction(b, bcloseHelpPanel, NULL);

  WMRealizeWidget(HelpPanel.window);
  //XSetWMHints(Aileron.display, WMWidgetXID(HelpPanel.window), HINTS);
  WMMapSubwidgets(HelpPanel.window);
  WMAppAddWindow(Aileron.appContext, WMWidgetXID(HelpPanel.window));
  WMHashInsert(Aileron.windowTable, "_HELP_PANEL_", &HelpPanel);
  WMMapWidget(HelpPanel.window);
    Aileron.helpCreated = True;   
}
  
  

/* TODO: put help text into files... don't need to compile it all in here */
void RunHelpPanel(int page)
{  
  if(Aileron.helpCreated) {
    XRaiseWindow(Aileron.display, WMWidgetXID(HelpPanel.window));
    WMMapWidget(HelpPanel.window);
    if(page == 0)
      page = HelpPanel.page;
  } else {
    runHelpPanel();
  }

  HelpPanel.page = page;
    WMFreezeText(HelpPanel.text);
  WMAppendTextStream(HelpPanel.text, NULL);

  switch(page) { 
    case 0:
      WMAppendTextStream(HelpPanel.text, 
    "<Font color=blue><b>Introduction to Aileron</b></FONT>"
    "<p><p><font color=red>NOTE: help is not fully functional <i>yet</i>"
    "<p><p>If you want to open an existing mailbox that is not listed "
    "in the Mailboxes Panel, drag the panel to twice its width and "
    "\"Link\" it into the panel"
    "<p><p>With <b>Aileron</b>, you can manipulate local, pop3, imap4 "
    "without realizing the difference. The only times you'll notice is "
    "the fact that actions may take longer with a network mailbox, "
    "particularly if you're pulling 50kB messages over a modem.");
    break;

    case 1:
      WMAppendTextStream(HelpPanel.text, 
    "<Font color=blue><b>Reading Mail</b></FONT><p><p>"
    "<font color=red>NOTE: help is not fully functional <i>yet</i>. I plan"
    " to add pictures here...</font><p><p>"
    "To open the next message, press Return or the down arrow key.<p><p>"
    "To open the previous message, hold down Shift and press Return,"
    "or press the up arrow key.<p><p>"
    "You can show none, selected or all headers that are available for a "
    "message. To select one of the three, press 'h'.<p><p>"
    "Hold down Shift and press the down arrow key to scroll down "
    "within a message. When you get to the end of a message, "
    "Shift-down arrow opens the next message. Press Shift-Up arrow "
    "to similarly scroll to the previous line or message.<p><p>"
    "Press the Page Down key to scroll to the next \"page\" in a message, "
    "and Page Up to scroll to the previous page or message.<p><p>"
    "To delete a message, press Delete; this will also open "
    "the next message. To undelete, hold down the Shift key while pressing "
    "Delete; this does not open the next message, however.<p><p>"
    "To mark a message that you've opened as unread, press 'u'.<p><p>"
    "Double click on an icon to run an application to open the file.<p>"
    "<font color=red>NOTE: Currently no application is run; the file is "
    "always saved. </FONT>");
    break;
   
    case 2:
      WMAppendTextStream(HelpPanel.text, 
    "<Font color=blue><b>Composing Mail</b></FONT><p><p>"
    "<font color=red>NOTE: help is not fully functional <i>yet</i>. I plan"
    " to add pictures here...</font><p><p>");
    break;

    case 3:
      WMAppendTextStream(HelpPanel.text, 
    "<Font color=blue><b>Transfering Mail</b></FONT><p><p>"
    "<font color=red>NOTE: help is not fully functional <i>yet</i>. I plan"
    " to add pictures here...</font><p><p>"
    "To transfer messages from one mailbox to another, initially choose "
    "the mailbox you want to transfer to from the Mailboxes panel. "
    "Next, select the message(s) you wish to move, and make sure that "
    "mailbox was the last one that the mouse was in. Finally, press Transfer, " 
    "and watch the titlebar of the mailbox being transferred from :-)p><p>"
    "<font color=red>NOTE: Although it doesn't matter if the mailbox you "
    "are transferring to is open, but it must not have the mouse focus, "
    "otherwise, Aileron will try to transfer from a mailbox to the "
    "<i>same</i> mailbox; an action it will refuse to do.</font");
    break;

    case 4:
      WMAppendTextStream(HelpPanel.text, 
    "<Font color=blue><b>Creating Mailboxes</b></FONT><p><p>"
    "<font color=red>NOTE: help is not fully functional <i>yet</i>. I plan"
    " to add pictures here...</font><p><p>"
    "To create a mailbox, simply choose the folder you want it in, type in "
    "its name, then press <b>New</b>. Mailboxes are not opened automatically "
    "when created; this is useful if all you wanted to do was transfer "
    "messages to this newly created mailbox."
    "<p><p>To create a folder, simply add "
    "a slash '<b>/</b>' in the name before creating it; this does not apply "
    "if you are within the <b>Network</b> folder.<p><p><p>"
    "<Font color=blue><b>Creating Network Mailboxes:</b></FONT><p><p>"
    "To create mailboxes that you wish to access via a network connection "
    "(imap4 or pop3), simply make sure you are within the <b>Network</b> "
    "folder before you create it.<p><FONT color=red>NOTE: all the information "
    "sent over a network connection is currently <i>not</i> secure, so please "
    "be careful.</font>"); 
    break;

    case 5:
      WMAppendTextStream(HelpPanel.text, 
    "<Font color=blue><b>Using the Find Panel</b></FONT><p><p>"
    "<font color=red>NOTE: help is not fully functional <i>yet</i>. I plan"
    " to add pictures here...</font><p><p>");
    break;

    case 253:
      WMAppendTextStream(HelpPanel.text, 
    "Depending on the nature of the error, this could mean that the "
    "mailbox was not found on the file system, or you don't have "
    "the necessary permissions to open it. It could also mean that, "
    "for remote mailboxes, the authentication failed. There is also "
    "slight possibility that the mailbox is in a format unsupported "
    "by Aileron.    ...more useful help needed, ie. how to rectify");
    break;

    case 254:
      WMAppendTextStream(HelpPanel.text, 
    "<FONT color=red>When you close this help panel, two more panels "
    "will be opened: one for configuring your mailboxes, and the "
    "other for program-wide preferences.</font><p><p>"
    "The Mailboxes panel has been expanded to allow you to change "
    "where local mail is received, as well as where to store them.<p>"
    "<p><b>Active.mbox </b> is the main spool file, although if you use "
    "procmail or any such program that can create multiple mailboxes¸ "
    "you can essentially have many INBOXes."
    "<p><p>There can be only one <b>Mailboxes </b> folder, recognized by "
    "Aileron to contain mailboxes, which can be stored hierarchically. "
    "The only folder that is not hiererchical is the <b>Network </b> "
    "folder, which contains IMAP or POP mailboxes. The "
    "<b>Active.mbox</b>, <b>Outgoing</b> and <b>Drafts</b> mailboxes "
    "(created as needed), exist only at the toplevel. "
    "In addition, you can link other top-level folders "
    "that can temporarily act as if they were part of the Mailboxes " 
    "structure.<p><p>The next time you use Aileron, the Mailboxes panel "
    "will be its standard size, showing only available mailboxes. "
    "If you wish to change where you receive mail, enlarge the "
    "panel to show all its contents.<p><p>"
    "<FONT COLOR=red>"
    "To begin using Aileron, press the <b>Set</b> button at the "
    "bottom right of the Preferences panel.<p><p></FONT>");
      WMUnmapWidget(HelpPanel.pagep);
      HelpPanel.doneForIntro = False;
      while(!HelpPanel.doneForIntro) { 
        XEvent event;
        WMNextEvent(Aileron.display, &event);
        WMHandleEvent(&event);
      }
      WMMapWidget(HelpPanel.pagep);
    break;
 
    case 255:
      WMAppendTextStream(HelpPanel.text, 
    "This application uses WINGs, a widget set that comes with "
    "Window Maker. After installation, certain directories are "
    "created for user configurations. If these cannot be found, "
    "it either means the installation was not complete, or the "
    "directories were removed after being installed.<p>"
    "Either way, you will have to resolve this by reading the "
    "documentation that came with Window Maker called <b>INSTALL</b>.<p>"
    "When you close this window, Aileron will quit.");
    break;

  }  

  WMThawText(HelpPanel.text);
}   




