#include <gnome.h>
#include <errno.h>

#include "options.h"
#include "support.h"
#include "utils.h"

extern GtkWidget *window1;
extern GtkWidget *dlgConfig;
extern Options *options;

extern int confRowCount;

void freeData(gpointer data);

/* Update the combobox */
void updateComboBox(void) {
  int r;
  GList *items=NULL;
  GtkWidget *comboBox;
  comboBox=lookup_widget(window1,"comboHostname");
  for(r=0;r<options->entries->numEntries;r++)
    items=g_list_append(items,options->entries->entries[r].hostname);
  gtk_combo_set_popdown_strings(GTK_COMBO(comboBox),items);
}

/* Update the configuration dialog list box */
void updateHostBox(void) {
  GtkCList *hostList=NULL;
  SSH_Entry *new_entry=NULL;
  gchar *tmp[1];
  int r;
  hostList = GTK_CLIST(lookup_widget(dlgConfig,"lstHosts"));
  confRowCount=options->entries->numEntries;
  gtk_clist_freeze(GTK_CLIST(hostList));
  gtk_clist_clear(GTK_CLIST(hostList));
  for(r=0;r<confRowCount;r++) {
    tmp[0]=options->entries->entries[r].hostname;
    gtk_clist_append(GTK_CLIST(hostList),tmp);
    new_entry=malloc(sizeof(SSH_Entry));
    memcpy(new_entry,&options->entries->entries[r],sizeof(SSH_Entry));
    gtk_clist_set_row_data_full(hostList,r,new_entry,(void*)freeData);
  }
  gtk_clist_thaw(GTK_CLIST(hostList));

}

/* Add a new entry to the hostList box */
int hostList_addEntry(char **entry,int position) {
  GtkCList *hostList=NULL;
  SSH_Entry *new_entry=NULL;
  int pos;
  hostList = GTK_CLIST(lookup_widget(dlgConfig,"lstHosts"));
  pos=gtk_clist_append(hostList,entry);
  new_entry=malloc(sizeof(SSH_Entry));
  new_entry->hostname = malloc(sizeof(gchar)*strlen(entry[0]));
  new_entry->username=NULL;
  new_entry->usePort=0;
  new_entry->port=22;
  new_entry->forwardLocalDest=NULL;
  new_entry->forwardRemoteDest=NULL;
  new_entry->forwardX11=NULL;
  new_entry->forwardAuth=NULL;
  new_entry->cipherType=NULL;
  new_entry->customEscape=NULL;
  new_entry->identityFile=NULL;
  new_entry->allocatePseudoTTY=NULL;
  new_entry->customOptions=NULL;
  new_entry->remoteCmd=NULL;
  strcpy(new_entry->hostname,entry[0]);
  gtk_clist_set_row_data_full(hostList,pos,new_entry,(void*)freeData);
  return pos+1;
}

void freeData(gpointer data) {
if(data) free(data);
}

/* Open a terminal with ssh */
int openTerminal(char *host, char *user,int port,Options *opts,int pos) {
  char ssh_cmd[512];
  switch(fork()) {
    case -1:
      perror("Fork error");
      exit(1);
      break;
    case 0:
      if(port>0)
        sprintf(ssh_cmd,"%s -l %s %s -p %d",opts->ssh_cmd,user,host,port);
      else
        sprintf(ssh_cmd,"%s -l %s %s",opts->ssh_cmd,user,host);
      if(options->entries->entries[pos].useExtra)
        strcat(ssh_cmd,sshExtraOptions(&opts->entries->entries[pos]));
      printf("ssh command: %s\n",ssh_cmd);
      execlp(opts->term_cmd,opts->term_cmd,"-e",ssh_cmd,NULL);
      fprintf(stderr,"Error ! Cannot open console !\nCommand line was %s -e %s\n",opts->term_cmd,ssh_cmd);
      perror("gssh");
      exit(1);
      break;
    default:
      exit(0);
  }
  return 0;
}

char *sshExtraOptions(SSH_Entry *opts) {
  gchar *ssh_opts;
  ssh_opts=(gchar*)malloc(sizeof(gchar)*450);
  strcpy(ssh_opts," ");
  if(opts->forwardLocal)
    sprintf(ssh_opts,"%s-L %d:%s:%d ",ssh_opts,opts->forwardLocalPortC,opts->forwardLocalDest,opts->forwardLocalPort);
  if(opts->forwardRemote)
    sprintf(ssh_opts,"%s-R %d:%s:%d ",ssh_opts,opts->forwardRemotePortC,opts->forwardRemoteDest,opts->forwardRemotePort);
  if(strcmp(opts->forwardX11,"Default"))
    if(strcmp(opts->forwardX11,"Enabled")) strcat(ssh_opts,"-x "); else sprintf(ssh_opts,"-X "); /* -x = disable , -X = enable */
  if(strcmp(opts->forwardAuth,"Default"))
    if(strcmp(opts->forwardX11,"Enabled")) strcat(ssh_opts,"-a "); else sprintf(ssh_opts,"-A "); /* -a = disable , -A = enable */
  if(opts->allowRemoteConnect) strcat(ssh_opts,"-g ");
  if(opts->disableKerberosForwarding) strcat(ssh_opts,"-k ");

  if(strcmp(opts->cipherType,"Default"))
    sprintf(ssh_opts,"%s-c %s ",ssh_opts,opts->cipherType);
  if(opts->useCustomEscape)
    sprintf(ssh_opts,"%s-e %s ",ssh_opts,opts->customEscape);
  if(opts->identityFile)
    if(strlen(opts->identityFile) && strncmp(opts->identityFile,"(null)",6)) sprintf(ssh_opts,"%s-i %s ",ssh_opts,opts->identityFile);
  if(opts->useNonPrivilegedPort) strcat(ssh_opts,"-P ");
  if(strcmp(opts->allocatePseudoTTY,"Default"))
    if(strcmp(opts->forwardX11,"Force")) strcat(ssh_opts,"-T "); else sprintf(ssh_opts,"-t "); /* -t = force , -T = disable */
  if(opts->compress) strcat(ssh_opts,"-C ");
  if(opts->customOptions!=NULL)
    if(strlen(opts->customOptions) && strncmp(opts->customOptions,"(null)",6)) sprintf(ssh_opts,"%s %s ",ssh_opts,opts->customOptions);

  if(opts->forkToBg) strcat(ssh_opts,"-f ");
  if(opts->devNull) strcat(ssh_opts,"-n ");
  if(opts->execCmd) strcat(ssh_opts,"-N ");
  if(opts->remoteCmd)
    if(strlen(opts->remoteCmd) && strncmp(opts->remoteCmd,"(null)",6)) strcat(ssh_opts,opts->remoteCmd);
  return ssh_opts;
}
