#import <ObjXMPP/ObjXMPP.h>
#include <gtk/gtk.h>

#import "JubGtkUI.h"
#import "JubGtkRosterUI.h"

void on_roster_window_destroy(GObject *object, gpointer user_data)
{
	gtk_main_quit();
}

@implementation JubGtkUI
- init
{
	self = [super init];

	@try {
		int *argc;
		char ***argv;

		[[OFApplication sharedApplication] getArgumentCount: &argc
						  andArgumentValues: &argv];

		gtk_init(argc, argv);

		builder = gtk_builder_new();
		gtk_builder_add_from_file(builder, "data/gtk/roster.ui", NULL);

		rosterUI = [[JubGtkRosterUI alloc] initWithBuilder: builder];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[rosterUI release];
	g_object_unref(G_OBJECT(builder));

	[super dealloc];
}

- (void)startUIThread
{
	GtkWidget *roster_window;

	roster_window =
	    GTK_WIDGET(gtk_builder_get_object(builder, "RosterWindow"));

	gtk_builder_connect_signals(builder, NULL);

	gtk_widget_show(roster_window);

	[[OFThread threadWithBlock: ^(void){
		gtk_main();
		[OFApplication terminate];

		return nil;
	}] start];
}

- (id<XMPPRosterDelegate>)rosterDelegate
{
	return rosterUI;
}
@end
