static char rcsid[] = "@(#)$Id: termcharset.c,v 1.10 2006/04/09 07:37:37 hurtta Exp $";

/******************************************************************************
 *  The Elm (ME+) Mail System  -  $Revision: 1.10 $   $State: Exp $
 *
 *  Author: Kari Hurtta <hurtta+elm@posti.FMI.FI> (was hurtta+elm@ozone.FMI.FI)
 *****************************************************************************/

#include "def_screen.h"

DEBUG_VAR(Debug,__FILE__,"elmpage");

charset_t target_display_charset = NULL;
screen_info_p  last_display_state = NULL;


charset_t * give_display_charsets(storage,max)
     charset_t *storage;
     int max;
{
    int n = 0,i;
    max--;

    storage[n++] = display_charset;

    if (system_charset != display_charset &&
	n < max)
	storage[n++] = system_charset;

    if (allow_charset_switching)
	terminal_can_switch(terminal_type,storage,&n,max);
    storage[n] = NULL;

    SIGDPRINT(Debug,4,(&Debug, 
		    "Possible sets:"));
    for (i = 0; i < n; i++) {
	SIGDPRINT(Debug,4,(&Debug, 
			" %s%c",
			storage[i]->MIME_name ? storage[i]->MIME_name : 
			"<no MIME name>",
			i < n-1 ? ',' : '\n'));
    }

    return storage;
}


int set_display_charset(set,silent)
     charset_t set; 
     int silent;
{   
    if (!terminal_can_switch_to(terminal_type,set,silent))
	return 0;

    target_display_charset = set;

    SIGDPRINT(Debug,4,(&Debug, 
		       "set_display_charset: Initializes target_display_charset to %s\n",
		       target_display_charset->MIME_name ?
		       target_display_charset->MIME_name : ""));


    switch_display_charset(1);
    return 1;
}

void init_system_charset() 
{
    char *str;

    /* Changes system_charset to type unknown if system supports 
       wide characters and there is DW flag on terminal.info
    */
    terminal_change_system_charset(terminal_type);

    target_display_charset = system_charset;
    display_charset        = system_charset;

    if (!last_display_state) {
	SIGDPRINT(Debug,4,(&Debug,
			   "init_system_charset: Creating terminal state\n"));

	last_display_state = create_terminal_info();
    }

    /* if buffer is set to NULL, malloces result */
    str = terminal_set_info(terminal_type,system_charset,last_display_state);

    if (str) {
	int i;

	DPRINT(Debug,4,(&Debug, 
			   "init_system_charset: Extra initialization for %s (",
			   system_charset->MIME_name ? 
			   system_charset->MIME_name : "<no MIME name>"));
	for (i = 0; str[i]; i++) {
	    DPRINT(Debug,4,(&Debug, 
			       "%s%02X",
			       i ? " " : "", str[i]));
	}
	DPRINT(Debug,4,(&Debug,
			   ") for terminal %s\n",terminal_type));
	
	for (i = 0; str[i]; i++) {
	    outchar(str[i]);
	}
	
	free(str);
    }    
}


void switch_display_charset(to_display)
     int to_display;
{
    char buffer[256];

    charset_t t;
    char * str;
    
    if (!target_display_charset) {
	target_display_charset = display_charset;
	SIGDPRINT(Debug,4,(&Debug, 
			   "switch_display_charset: Initializes target_display_charset to %s\n",
			   target_display_charset->MIME_name ?
			   target_display_charset->MIME_name : ""));
    }    

    if (!last_display_state) {
	SIGDPRINT(Debug,4,(&Debug,
			   "switch_display_charset: Creating terminal state\n"));
       	last_display_state = create_terminal_info();
    }



    t = to_display ? target_display_charset : system_charset;
    
    if (!t) {
	SIGDPRINT(Debug,4,(&Debug, 
			   "switch_display_charset: target charset is NULL\n"));
	return;
    }

    str = terminal_switch_to(terminal_type,t,last_display_state,
			     buffer, sizeof buffer, 1);

    if (str) {
	int i;

	SIGDPRINT(Debug,4,(&Debug, 
			"Setting display charset to %s (",
			t->MIME_name ? t->MIME_name : "<no MIME name>"));
	for (i = 0; str[i]; i++) {
	    SIGDPRINT(Debug,4,(&Debug, 
			    "%s%02X",
			    i ? " " : "", str[i]));
	}
	SIGDPRINT(Debug,4,(&Debug,
		  ") [terminal %s]\n",terminal_type));
	
	for (i = 0; str[i]; i++) {
	    outchar(str[i]);
	}

	display_charset = t;
      
    } else {
	SIGDPRINT(Debug,4,(&Debug,
			    "No way to set terminal %s to charset %s\n",
			    terminal_type,
			    t->MIME_name ? t->MIME_name : "<no MIME name>"));
		  
    }
}

/*
 * Local Variables:
 *  mode:c
 *  c-basic-offset:4
 *  buffer-file-coding-system: iso-8859-1
 * End:
 */


syntax highlighted by Code2HTML, v. 0.9.1