summaryrefslogtreecommitdiff
path: root/gtk.c
blob: af24a73ab025679c8a6646f328262e03e243fa93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272


#include <gtk-4.0/gtk/gtk.h>
#include <stdio.h>

#include "net.h"
#include "data.h"


typedef struct _chat_widget_t {
	GtkWidget* page;
	GtkWidget* content;
	GtkWidget* entry;
} chat_widget_t;


// 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) {
	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
	fprintf(stderr, "Connecting with:\nJID: %s\nPWD: %s\n", jid_text, pwd_text);

	net_connect(jid_text, pwd_text);
}

static void disconnect_cb(void) {

	gtk_stack_set_visible_child(GTK_STACK(stack), connector_box);

	// just dummy output
	fprintf(stderr, "Disconnected!\n");

	net_disconnect();
}

void send_message(chat_t* chat) {

	// cast void* to GUI-specific chat_widget_t*
	chat_widget_t* chat_widget = (chat_widget_t*)chat->widget;

	// get text of message and own JID
	const char* text = gtk_editable_get_text(GTK_EDITABLE(chat_widget->entry));
	const char* self = gtk_editable_get_text(GTK_EDITABLE(connector_jid_entry));

	// add message to datastructure and send it
	data_add_outgoing_message(self, text, chat);
	net_send_message(self, text, chat->jid);

	// clear text input
	GtkEntryBuffer* buffer;
	buffer = gtk_entry_get_buffer(GTK_ENTRY(chat_widget->entry));
	gtk_entry_buffer_delete_text(buffer, 0, -1);
}

/* Set credentials from environment variables if given */
static void set_debug_credentials(GtkWidget* user, GtkWidget* pwd) {

	char* user_str = getenv("LIMOX_USER");
	if (user_str != NULL) {
		GtkEntryBuffer* buffer;
		buffer = gtk_entry_get_buffer(GTK_ENTRY(user));
		gtk_entry_buffer_set_text(buffer, user_str, -1);
	}

	char* pwd_str = getenv("LIMOX_PWD");
	if (pwd_str != NULL) {
		gtk_editable_set_text(GTK_EDITABLE(pwd), pwd_str);
	}

}

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);
	set_debug_credentials(connector_jid_entry, connector_pwd_entry);

	// 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) {

	net_run_once();

}

void gui_run(void) {

#ifdef GTK_SRCDIR
	g_chdir(GTK_SRCDIR);
#endif

	app = gtk_application_new("eu.xengineering.limox", G_APPLICATION_DEFAULT_FLAGS);
	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);

}

static void to_roster(void) {

	gtk_stack_set_visible_child(GTK_STACK(stack), roster_layout_box);

}

void gui_add_chat_widget(chat_t* chat) {

	// allocate chat_widget_t and save as void* in given chat_t
	chat_widget_t* chat_widget = malloc(sizeof(chat_widget_t));
	chat->widget = (void*)chat_widget;

	// 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);
	chat_widget->page = 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);

	// 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);
	chat_widget->content = 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);
	chat_widget->entry = 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);

}

void gui_add_message_widget(message_t* message, chat_t* chat) {

	const char* connector = "\n";
	char* label_content = malloc(sizeof(char)*(strlen(message->sender_jid)
	                                         + strlen(connector)
	                                         + strlen(message->content)));
	sprintf(label_content, "%s%s%s", message->sender_jid, connector,
	        message->content);
	GtkWidget* label = gtk_label_new(label_content);
	gtk_label_set_wrap(GTK_LABEL(label), TRUE);
	gtk_label_set_selectable(GTK_LABEL(label), TRUE);
	message->widget = (void*)label;
	chat_widget_t* chat_widget = (chat_widget_t*)chat->widget;
	gtk_box_append(GTK_BOX(chat_widget->content), label);
	free(label_content);

}

static void to_chat(chat_t* chat) {

	chat_widget_t* chat_widget = (chat_widget_t*)chat->widget;
	gtk_stack_set_visible_child(GTK_STACK(stack), chat_widget->page);

}

void gui_add_roster_item_widget(roster_item_t* item) {

	// print debug message
	if (item->name) {
		fprintf(stderr, "roster item: %s, %s, sub:%d\n", item->name, item->jid,
		       item->sub);
	} else {
		fprintf(stderr, "roster item: (no name), %s, sub:%d\n", item->jid, item->sub);
	}

	// create widget for roster item
	GtkWidget* roster_item_widget;
	if (item->name == NULL || strcmp(item->name, "") == 0) {
		roster_item_widget = gtk_button_new_with_label(item->jid);
	} else {
		roster_item_widget = gtk_button_new_with_label(item->name);
	}

	// 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),
	                         item->chat);

}