/*
 * WebGet: Acorn Web Fetcher and rewriter
 * Status window code
 *
 *  Joseph Heenan, 1998
 * All rights reserved.
 *
 * This source is not for release
 *
 * $Log: status,v $
 * Revision 1.4  1999/04/05 14:16:49  joseph
 * Fixed bug when config_maxsess was set greater than MAXSESS
 * All tag values read from config file now passed through OS_GSTrans
 *
 * Revision 1.3  1998/09/14 22:19:37  joseph
 * Fixed calculation of megabytes figure (was 3 times too small)
 *
 * Revision 1.2  1998/08/11 17:44:31  joseph
 * Merged the wimp_setictext_int into wimpclib and removed it from here.
 *
 * Revision 1.1  1998/07/01 21:53:25  joseph
 * Added status window
 *
 *
 */

#include "defines.h"

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include "kernel.h"
#include "swis.h"
#include "wimplib.h"
#include "config.h"
#include "http.h"

#include "IconNames.h"

#include "wimpclib.h"

#include "status.h"

static status_winhan = -1;

static const status_urlicons[MAXSESS] =
{
  STATUS_1,
  STATUS_2,
  STATUS_3,
  STATUS_4,
  STATUS_5,
  STATUS_6,
  STATUS_7,
  STATUS_8,
  STATUS_9,
  STATUS_A,
  STATUS_B,
  STATUS_C,
  STATUS_D,
  STATUS_E,
  STATUS_F
};


/* adjusts the vertical extent of the status window so that it only has room for the
 * number of url's we can fetch concurrently
 */
static void status_setextent( void )
{
  WimpGetIconStateBlock state;
  WimpGetWindowInfoBlock win;
  BBox *extent = &win.window_data.extent;

  win.window_handle = status_winhan;
  wimp_get_window_info( (WimpGetWindowInfoBlock *) (((int)&win)+1) );

  state.window_handle = status_winhan;
  state.icon_handle   = status_urlicons[ MAXSESS - config_maxfetchers ];
  if ( config_maxfetchers >= MAXSESS )
    state.icon_handle = status_urlicons[ 0 ];

  E_CHECK_RETURN( , wimp_get_icon_state( &state ) );

  extent->ymax = state.icon.bbox.ymax + 4;

  E_CHECK_RETURN( , wimp_set_extent( status_winhan, extent) );
}


void status_clearurls( void )
{
  int x;

  for ( x = 0; x < MAXSESS; x++ )
    wimpc_seticontext( status_winhan, status_urlicons[x], "" );
}

void status_init( int winhan )
{
  status_winhan = winhan;
  /* clear window */
  wimpc_seticontext( winhan, STATUS_BYTES,    "" );
  wimpc_seticontext( winhan, STATUS_SPEED,    "" );
  wimpc_seticontext( winhan, STATUS_WAITING,  "" );
  wimpc_seticontext( winhan, STATUS_FETCHING, "" );
  wimpc_seticontext( winhan, STATUS_FETCHED,  "" );
  status_clearurls();
  status_setextent();
}


void status_seturl( int no, char *url )
{
  /* There are MAXSESS entries in status_urlicons
     There are config_maxfetches icons on screen.
  */
  int toupdate = MAXSESS - config_maxfetchers + no;
  if ( config_maxfetchers >= MAXSESS )
    toupdate = no;
  if ( toupdate >= MAXSESS )
    return;

  wimpc_seticontext( status_winhan, status_urlicons[toupdate], url );
}


void status_update( void )
{
  static unsigned int lasttime = 0;
  unsigned int now;
  static int lastbytes = 0;
  int speed;
  char buf[128];

  sprintf( buf, "%d", http_waiting );
  wimpc_seticontext( status_winhan, STATUS_WAITING, buf );

  sprintf( buf, "%d", http_fetching );
  wimpc_seticontext( status_winhan, STATUS_FETCHING, buf );

  sprintf( buf, "%d", http_fetched );
  wimpc_seticontext( status_winhan, STATUS_FETCHED, buf );

  if ( http_bytesrxed < 3*1024 )
    sprintf( buf, "%d b", http_bytesrxed );
  else if ( http_bytesrxed < 3*1024*1024 )
    sprintf( buf, "%.1f K", (double) http_bytesrxed / (double) 1024 );
  else
    sprintf( buf, "%.1f M", (double) http_bytesrxed / (double) (1024*1024) );
  wimpc_seticontext( status_winhan, STATUS_BYTES, buf );


  if ( _swix( OS_ReadMonotonicTime, _OUT(0), &now ) ) return;

  if ( http_bytesrxed == 0 || lastbytes > http_bytesrxed )
  {
    lastbytes = 0;
    lasttime  = now;
    return;
  }

  if ( now - lasttime < 200 )
    return; /* wait for at least two seconds worth of data */

  /* now & lasttime are in centiseconds */
  speed = ((http_bytesrxed - lastbytes) * 100) / (now - lasttime);
  if ( speed < 1024 )
   sprintf( buf, "%d cps", speed );
  else
   sprintf( buf, "%.1f K/s", ((double)speed) / (double)1024 );
  wimpc_seticontext( status_winhan, STATUS_SPEED, buf );
  lasttime  = now;
  lastbytes = http_bytesrxed;
}


