/*
 * Initial main.c file generated by Glade. Edit as required.
 * Glade will not overwrite this file.
 */

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

#include <gnome.h>
#include <unistd.h>
#include "interface.h"
#include "support.h"
#include "eggtrayicon.h"



EggTrayIcon *tray_icon = NULL;
GtkWidget *icon = NULL;
GtkWidget *menu = NULL;
GtkWidget *wnddf = NULL;


void mount()

{

	int retval;
	retval = system("mount /mnt/usb");
	printf("mounting\n");
}

void unmount()

{
	int retval;
	retval = system("umount /mnt/usb");
	
	printf("unmounting\n");
}

static void show_df()
{
	wnddf = create_UM_main();
	gtk_widget_show (wnddf);
}



static void clicked (GtkWidget *widget, GdkEventButton *event, gpointer data)

{
    gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL, event->button, event->time);
	
}

// trayicon creat_menu//

static void create_menu ()
{
	GtkWidget *menuitem;

	menu = gtk_menu_new ();

	menuitem = gtk_menu_item_new_with_label ("MOUNT");
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
	g_signal_connect (G_OBJECT (menuitem), "activate", G_CALLBACK (mount), 0);

	menuitem = gtk_menu_item_new_with_label ("UNMOUNT");
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
	g_signal_connect (G_OBJECT (menuitem), "activate", G_CALLBACK (unmount), 0);

	
	menuitem = gtk_menu_item_new_with_label ("open");
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
	g_signal_connect (G_OBJECT (menuitem), "activate", G_CALLBACK (show_df), 0);

	menuitem = gtk_separator_menu_item_new ();
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);

	menuitem = gtk_menu_item_new_with_label ("Exit");
	g_signal_connect (G_OBJECT (menuitem), "activate", G_CALLBACK (gtk_main_quit), 0);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
	
	gtk_widget_show_all (menu);
}
static void tray_icon_load ()
{
	GdkPixbuf *pixbuf;

	pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (icon));

	if (pixbuf)
		g_object_unref (G_OBJECT (pixbuf));

	pixbuf = gdk_pixbuf_new_from_file ("../pixmaps/usb.png", NULL);

	gtk_image_set_from_pixbuf (GTK_IMAGE (icon), pixbuf);
}

// trayicon _create//

void tray_create ()
{
	GtkWidget *box;

	if (!menu)
		create_menu ();

	tray_icon = egg_tray_icon_new ("UM");
	box = gtk_event_box_new ();
	icon = gtk_image_new ();

	/*add icon pix*/
	gtk_container_add (GTK_CONTAINER (box), icon);
	gtk_container_add (GTK_CONTAINER (tray_icon), box);
	gtk_widget_show_all (GTK_WIDGET (tray_icon));

	/*event handler*/
	g_signal_connect (G_OBJECT (box), "button-press-event", G_CALLBACK (clicked), NULL);

	/*load pix*/
	tray_icon_load ();
}

	
void tray_destroy ()
{
	GdkPixbuf *pixbuf;

	pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (icon));

	if (pixbuf)
		g_object_unref (G_OBJECT (pixbuf));

	gtk_widget_destroy (icon);

	gtk_widget_destroy (GTK_WIDGET (tray_icon));
}
int
main (int argc, char *argv[])
{
 
#ifdef ENABLE_NLS
  bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);
#endif

  gnome_program_init (PACKAGE, VERSION, LIBGNOMEUI_MODULE,
                      argc, argv,
                      GNOME_PARAM_APP_DATADIR, PACKAGE_DATA_DIR,
                      NULL);

  /*
   * The following code was added by Glade to create one of each component
   * (except popup menus), just so that you see something after building
   * the project. Delete any components that you don't want shown initially.
   */
  gtk_init (&argc, &argv);
  tray_create ();

  gtk_main ();
  
  tray_destroy ();
  return 0;
}
