#include #include #include "limox.h" // the GTK application is available as global variable static GtkApplication* app; // all GTK widgets are accessible in this file via global variables static GtkWidget* window; static GtkWidget* stack; // connector page static GtkStackPage* connector_page; static GtkWidget* connector_box; static GtkWidget* connector_jid_label; static GtkWidget* connector_jid_entry; static GtkWidget* connector_pwd_label; static GtkWidget* connector_pwd_entry; static GtkWidget* connector_button; // roster page static GtkStackPage* roster_page; static GtkWidget* roster_layout_box; static GtkWidget* roster_scrolled; static GtkWidget* roster_content_box; static GtkWidget* roster_button; static void quit_cb(void) { limox_quit(); g_application_quit(G_APPLICATION(app)); } static void connect_cb(void) { const char* jid_text = gtk_editable_get_text(GTK_EDITABLE(connector_jid_entry)); const char* pwd_text = gtk_editable_get_text(GTK_EDITABLE(connector_pwd_entry)); gtk_stack_set_visible_child(GTK_STACK(stack), roster_layout_box); // just dummy output printf("Connecting with:\nJID: %s\nPWD: %s\n", jid_text, pwd_text); limox_connect(jid_text, pwd_text); } static void disconnect_cb(void) { gtk_stack_set_visible_child(GTK_STACK(stack), connector_box); // just dummy output printf("Disconnected!\n"); limox_disconnect(); } void send_message() { //// get recipient and message text //const char* recipient = "dummy@example.com"; // TODO reimplement this //const char* text = gtk_editable_get_text(GTK_EDITABLE(chat->text_entry)); //// execute dummy XMPP send TODO //printf("Sending to %s:\n> %s\n", recipient, text); //// add message content to the chat //GtkWidget* message = gtk_label_new(text); //gtk_box_append(GTK_BOX(chat->chat_content_box), message); //// clear text input //GtkEntryBuffer* empty_buffer = gtk_entry_buffer_new("", 0); //gtk_entry_set_buffer(GTK_ENTRY(chat->text_entry), empty_buffer); } void add_incoming_text_message(char* sender_jid, char* content) { printf("Trying to add message '%s' to chat with '%s'.\n", content, sender_jid); // do not add message if list of chats is empty //if (chats == NULL) { // return; //} // find chat with corresponding JID //struct chat* chat = chats; //while (true) { // const char* jid = gtk_button_get_label(GTK_BUTTON(chat->roster_item)); // if (strcmp(jid, sender_jid) == 0) { // break; // this seems to be the correct chat // } else if (chat->next == NULL) { // return; // no matching chat found - cannot add message // } else { // chat = chat->next; // go to next chat in linked list // } //} // add given message content to the chat //GtkWidget* message = gtk_label_new(content); //gtk_box_append(GTK_BOX(chat->chat_content_box), message); } static void build_static_widgets(void) { // main window with stack window = gtk_window_new(); gtk_window_set_title(GTK_WINDOW(window), "LimoX"); gtk_window_set_default_size(GTK_WINDOW(window), 800, 600); stack = gtk_stack_new(); gtk_window_set_child(GTK_WINDOW(window), stack); // connector page connector_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 20); connector_page = gtk_stack_add_child(GTK_STACK(stack), connector_box); connector_jid_label = gtk_label_new("XMPP address (JID)"); gtk_box_append(GTK_BOX(connector_box), connector_jid_label); connector_jid_entry = gtk_entry_new(); gtk_box_append(GTK_BOX(connector_box), connector_jid_entry); connector_pwd_label = gtk_label_new("Password"); gtk_box_append(GTK_BOX(connector_box), connector_pwd_label); connector_pwd_entry = gtk_password_entry_new(); gtk_box_append(GTK_BOX(connector_box), connector_pwd_entry); connector_button = gtk_button_new_with_label("Connect"); gtk_box_append(GTK_BOX(connector_box), connector_button); // roster page roster_layout_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 20); roster_page = gtk_stack_add_child(GTK_STACK(stack), roster_layout_box); roster_button = gtk_button_new_with_label("Disconnect"); gtk_box_append(GTK_BOX(roster_layout_box), roster_button); roster_scrolled = gtk_scrolled_window_new(); gtk_widget_set_vexpand(roster_scrolled, TRUE); roster_content_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(roster_scrolled), roster_content_box); gtk_box_append(GTK_BOX(roster_layout_box), roster_scrolled); } static void activate(void) { build_static_widgets(); // configure widgets gtk_window_set_application(GTK_WINDOW(window), app); g_signal_connect_swapped(window, "close-request", G_CALLBACK(quit_cb), app); g_signal_connect_swapped( connector_button, "clicked", G_CALLBACK(connect_cb), NULL); g_signal_connect_swapped( roster_button, "clicked", G_CALLBACK(disconnect_cb), NULL); gtk_widget_show(window); } static void idle_cb(void) { limox_run_once(); } void gui_run(void) { #ifdef GTK_SRCDIR g_chdir(GTK_SRCDIR); #endif app = gtk_application_new("eu.xengineering.limox", G_APPLICATION_FLAGS_NONE); g_signal_connect(app, "activate", G_CALLBACK(activate), NULL); g_idle_add(G_SOURCE_FUNC(idle_cb), NULL); g_application_run(G_APPLICATION(app), 0, NULL); g_object_unref(app); } void* gui_add_chat(void) { // create chat page (GtkBox) and add it to the stack GtkWidget* chat_layout_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 20); gtk_stack_add_child(GTK_STACK(stack), chat_layout_box); // create and add back button GtkWidget* back = gtk_button_new_with_label("back"); gtk_box_append(GTK_BOX(chat_layout_box), back); //g_signal_connect_swapped(back, "clicked", G_CALLBACK(to_roster), NULL); TODO // create and add scrolled window GtkWidget* scrolled = gtk_scrolled_window_new(); gtk_widget_set_vexpand(scrolled, TRUE); gtk_box_append(GTK_BOX(chat_layout_box), scrolled); // create and add content box for dynamic message widgets GtkWidget* chat_content_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5); gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scrolled), chat_content_box); // create and add text input field GtkWidget* text_entry = gtk_entry_new(); gtk_box_append(GTK_BOX(chat_layout_box), text_entry); // create and add send button GtkWidget* send_button = gtk_button_new_with_label("send"); gtk_box_append(GTK_BOX(chat_layout_box), send_button); //g_signal_connect_swapped(send_button, "clicked", G_CALLBACK(send_message), // chat); // TODO // return chat page as void pointer for future reference return (void*)chat_layout_box; } void gui_add_message(const char* sender_jid, const char* content) { printf("Received from %s:\n%s\n", sender_jid, content); } void* gui_add_roster_item(const char* jid, const char* sub, const char* name) { // print debug message if (name) { printf("roster item: %s, %s, sub:%s\n", name, jid, sub); } else { printf("roster item: (no name), %s, sub:%s\n", jid, sub); } // create widget for roster item GtkWidget* roster_item_widget; if (name == NULL || strcmp(name, "") == 0) { roster_item_widget = gtk_button_new_with_label(name); } else { roster_item_widget = gtk_button_new_with_label(jid); } // add roster item widget to roster page gtk_box_append(GTK_BOX(roster_content_box), roster_item_widget); //g_signal_connect_swapped(roster_item_widget, "clicked", G_CALLBACK(to_chat), // chat->chat_layout_box); TODO this should be realized again return (void*)roster_item_widget; }