/* This is a LJ plugin for GAIM which lets you update your BLOG(XMLRPC compliant) without having to open a separate client.This was written with an intention to ease the trouble to post chat conversations on the blogs.

Written by Pradyumna Sampath (prady@prady.net).Livejournal ID :prady
 
Copyright (C) 2003 Pradyumna Sampath 

 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <xmlrpc.h>
#include <xmlrpc_client.h>

#include "lj.h"

#define NAME       "Livejournal GAIM Client"
#define VERSION    "0.1"
#define SERVER_URL "http://livejournal.com/interface/xmlrpc"


GtkWidget *text1;
  GtkWidget *window1;
  GtkWidget *vbox2;
  GtkWidget *hbox4;
  GtkWidget *label2;
  GtkWidget *entry3;
  GtkWidget *label3;
  GtkWidget *entry4;
  GtkWidget *entry5;
  GtkWidget *label4;
  GtkWidget *vbox3;
  GtkWidget *alignment1;
  GtkWidget *scrolledwindow1;
  GtkWidget *hbox3;
  GtkWidget *button7;
  GtkWidget *button6;
  char buffer[1024];


void if_fault_occurred (xmlrpc_env *env)
{
    /* Check our error-handling environment for an XML-RPC fault. */
    if (env->fault_occurred) {
        fprintf(stderr, "XML-RPC Fault: %s (%d)\n",
                env->fault_string, env->fault_code);
       /* exit(1);*/
    }
}

void lj_submit(GtkWidget *widget)
{
	time_t now;
	struct tm *l_time;
	xmlrpc_env env;
	xmlrpc_value *result;
	char *uname,*pass,*subj;

	now=time(0);
	l_time=localtime(&now);
	uname=gtk_entry_get_text(GTK_ENTRY(entry3));
	pass=gtk_entry_get_text(GTK_ENTRY(entry4));
	subj=gtk_entry_get_text(GTK_ENTRY(entry5));
	
/*	printf("%d\n %d\n ",l_time->tm_year+1900,l_time->tm_mon+1);*/
	xmlrpc_client_init(XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION);
	xmlrpc_env_init(&env);
	
	result = xmlrpc_client_call(&env, SERVER_URL,
    "LJ.XMLRPC.postevent", "({s:s,s:s,s:s,s:s,s:i,s:i,s:i,s:i,s:i})",
   "username",uname,"password",pass,"subject",subj,
    "event",buffer,"year",l_time->tm_year+1900,"mon",l_time->tm_mon+1,"day",l_time->tm_mday,"hour",l_time->tm_hour,"min",l_time->tm_min);
  if_fault_occurred(&env);
                                                                                
                                                                                
    xmlrpc_env_clean(&env);
    xmlrpc_client_cleanup();

    
}

void get_file_name(GtkWidget *widget,GtkFileSelection *fs)
{
	char *filename;
	
	GdkFont *fixed_font;
	
	FILE *infile;
	
	fixed_font = gdk_font_load("-misc-fixed-medium-r-*-*-*-110-*-*-*-*-*-*");
	filename=gtk_file_selection_get_filename(GTK_FILE_SELECTION(fs));
	gtk_widget_realize(text1);
	gtk_text_freeze(text1);
	
	infile=fopen(filename,"r");
	
	if(infile){
			
			int nchars;
		
		while(1)
		{
				nchars=fread(buffer,1,1024,infile);
				gtk_text_insert(GTK_TEXT(text1),fixed_font,NULL,NULL,buffer,nchars);
		 if(nchars<1024)
			 break;
		 }		
		fclose(infile);
	}gtk_text_thaw(text1);
}

GtkWidget* create_fileselection1 (void)
{
  GtkWidget *fileselection1;
  GtkWidget *ok_button1;
  GtkWidget *cancel_button1;

  fileselection1 = gtk_file_selection_new (_("Select File"));
  gtk_object_set_data (GTK_OBJECT (fileselection1), "fileselection1", fileselection1);
  gtk_container_set_border_width (GTK_CONTAINER (fileselection1), 10);

  ok_button1 = GTK_FILE_SELECTION (fileselection1)->ok_button;
  gtk_object_set_data (GTK_OBJECT (fileselection1), "ok_button1", ok_button1);
  gtk_widget_show (ok_button1);
  gtk_signal_connect(GTK_OBJECT(ok_button1),"clicked",GTK_SIGNAL_FUNC(get_file_name),GTK_OBJECT(fileselection1));
  GTK_WIDGET_SET_FLAGS (ok_button1, GTK_CAN_DEFAULT);

  cancel_button1 = GTK_FILE_SELECTION (fileselection1)->cancel_button;
  gtk_object_set_data (GTK_OBJECT (fileselection1), "cancel_button1", cancel_button1);
  gtk_widget_show (cancel_button1);
  gtk_signal_connect_object(GTK_OBJECT(cancel_button1),"clicked",GTK_SIGNAL_FUNC(gtk_widget_destroy),GTK_OBJECT(fileselection1));
  GTK_WIDGET_SET_FLAGS (cancel_button1, GTK_CAN_DEFAULT);
   
  gtk_widget_show(fileselection1);
	
  return fileselection1;
	
}



GtkWidget* create_window1 (void)
{
  
/*  GtkWidget *window1;
  GtkWidget *vbox2;
  GtkWidget *hbox4;
  GtkWidget *label2;
  GtkWidget *entry3;
  GtkWidget *label3;
  GtkWidget *entry4;
  GtkWidget *vbox3;
  GtkWidget *alignment1;
  GtkWidget *scrolledwindow1;
  GtkWidget *hbox3;
  GtkWidget *button7;
  GtkWidget *button6;*/



  window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_usize(window1,400,200);	
  gtk_object_set_data (GTK_OBJECT (window1), "window1", window1);
  gtk_window_set_title (GTK_WINDOW (window1), _("window1"));

  vbox2 = gtk_vbox_new (FALSE, 0);
  gtk_widget_ref (vbox2);
  gtk_object_set_data_full (GTK_OBJECT (window1), "vbox2", vbox2,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (vbox2);
  gtk_container_add (GTK_CONTAINER (window1), vbox2);

  hbox4 = gtk_hbox_new (FALSE, 0);
  gtk_widget_ref (hbox4);
  gtk_object_set_data_full (GTK_OBJECT (window1), "hbox4", hbox4,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (hbox4);
  gtk_box_pack_start (GTK_BOX (vbox2), hbox4, TRUE, TRUE, 0);

  label2 = gtk_label_new (_("Username "));
  gtk_widget_ref (label2);
  gtk_object_set_data_full (GTK_OBJECT (window1), "label2", label2,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (label2);
  gtk_box_pack_start (GTK_BOX (hbox4), label2, FALSE, FALSE, 0);

  entry3 = gtk_entry_new ();
  gtk_widget_ref (entry3);
  gtk_object_set_data_full (GTK_OBJECT (window1), "entry3", entry3,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (entry3);
  gtk_box_pack_start (GTK_BOX (hbox4), entry3, TRUE, TRUE, 0);

  label3 = gtk_label_new (_("Password "));
  gtk_widget_ref (label3);
  gtk_object_set_data_full (GTK_OBJECT (window1), "label3", label3,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (label3);
  gtk_box_pack_start (GTK_BOX (hbox4), label3, FALSE, FALSE, 0);

  entry4 = gtk_entry_new ();
  gtk_widget_ref (entry4);
  gtk_object_set_data_full (GTK_OBJECT (window1), "entry4", entry4,
                            (GtkDestroyNotify) gtk_widget_unref);
  label4=gtk_label_new("Subject");
  gtk_widget_show(label4);
  gtk_box_pack_start(GTK_BOX(vbox2),label4,FALSE,FALSE,0);
  
  entry5=gtk_entry_new();
  gtk_widget_show(entry5);
  gtk_box_pack_start(GTK_BOX(vbox2),entry5,FALSE,FALSE,0);

  gtk_widget_show (entry4);
  gtk_box_pack_start (GTK_BOX (hbox4), entry4, TRUE, TRUE, 0);
  gtk_entry_set_visibility (GTK_ENTRY (entry4), FALSE);

  vbox3 = gtk_vbox_new (FALSE, 0);
  gtk_widget_ref (vbox3);
  gtk_object_set_data_full (GTK_OBJECT (window1), "vbox3", vbox3,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (vbox3);
  gtk_box_pack_start (GTK_BOX (vbox2), vbox3, TRUE, TRUE, 0);

  alignment1 = gtk_alignment_new (0.5, 0.5, 1, 1);
  gtk_widget_ref (alignment1);
  gtk_object_set_data_full (GTK_OBJECT (window1), "alignment1", alignment1,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (alignment1);
  gtk_box_pack_start (GTK_BOX (vbox3), alignment1, TRUE, TRUE, 0);

  scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
  gtk_widget_ref (scrolledwindow1);
  gtk_object_set_data_full (GTK_OBJECT (window1), "scrolledwindow1", scrolledwindow1,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (scrolledwindow1);
  gtk_container_add (GTK_CONTAINER (alignment1), scrolledwindow1);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);

  text1 = gtk_text_new (NULL, NULL);
  gtk_text_set_editable(GTK_TEXT(text1),TRUE);
  gtk_widget_ref (text1);
  gtk_object_set_data_full (GTK_OBJECT (window1), "text1", text1,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (text1);
  gtk_container_add (GTK_CONTAINER (scrolledwindow1), text1);

  hbox3 = gtk_hbox_new (FALSE, 0);
  gtk_widget_ref (hbox3);
  gtk_object_set_data_full (GTK_OBJECT (window1), "hbox3", hbox3,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (hbox3);
  gtk_box_pack_start (GTK_BOX (vbox3), hbox3, FALSE, FALSE, 0);

  button7 = gtk_button_new_with_label (_("Load Log File"));
  gtk_widget_ref (button7);
  gtk_object_set_data_full (GTK_OBJECT (window1), "button7", button7,
                            (GtkDestroyNotify) gtk_widget_unref);
  gtk_widget_show (button7);
  gtk_signal_connect(GTK_OBJECT(button7),"clicked",GTK_SIGNAL_FUNC(create_fileselection1),NULL);
  
  gtk_box_pack_start (GTK_BOX (hbox3), button7, FALSE, FALSE, 0);

  button6 = gtk_button_new_with_label (_("Submit "));
  gtk_widget_ref (button6);
  gtk_object_set_data_full (GTK_OBJECT (window1), "button6", button6,
                            (GtkDestroyNotify) gtk_widget_unref);
   gtk_widget_show (button6);
  gtk_signal_connect(GTK_OBJECT(button6),"clicked",GTK_SIGNAL_FUNC(lj_submit),
		NULL);	

  gtk_box_pack_start (GTK_BOX (hbox3), button6, FALSE, FALSE, 0);

  return window1;
}

