aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheJackiMonster <thejackimonster@gmail.com>2021-11-14 00:38:00 +0100
committerTheJackiMonster <thejackimonster@gmail.com>2021-11-14 00:38:00 +0100
commitb085cf0e3025a513ebd8e5ff072f0a1fc19210f7 (patch)
tree64cf5b2eade3dbdbb14bea76ee1174027e4d6a77
parent4647c3361fa682e2de8f513442b41861ccfae77b (diff)
downloadmessenger-gtk-b085cf0e3025a513ebd8e5ff072f0a1fc19210f7.tar.gz
messenger-gtk-b085cf0e3025a513ebd8e5ff072f0a1fc19210f7.zip
Implemented actual sending and receiving of text messages via chat interface
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r--resources/ui/message-sent.ui1
-rw-r--r--resources/ui/message.ui1
-rw-r--r--src/application.c27
-rw-r--r--src/application.h8
-rw-r--r--src/chat/messenger.c19
-rw-r--r--src/event.c76
-rw-r--r--src/event.h11
-rw-r--r--src/ui/messenger.c57
8 files changed, 175 insertions, 25 deletions
diff --git a/resources/ui/message-sent.ui b/resources/ui/message-sent.ui
index a046788..6fd9e04 100644
--- a/resources/ui/message-sent.ui
+++ b/resources/ui/message-sent.ui
@@ -62,6 +62,7 @@ Author: Tobias Frisch
62 <property name="label" translatable="yes">My evil comment text is awesome! This is coming to the Pinephone which will be extremely awesome and this text may only cut some borders in certain areas!!!</property> 62 <property name="label" translatable="yes">My evil comment text is awesome! This is coming to the Pinephone which will be extremely awesome and this text may only cut some borders in certain areas!!!</property>
63 <property name="wrap">True</property> 63 <property name="wrap">True</property>
64 <property name="max-width-chars">64</property> 64 <property name="max-width-chars">64</property>
65 <property name="xalign">0</property>
65 </object> 66 </object>
66 <packing> 67 <packing>
67 <property name="expand">True</property> 68 <property name="expand">True</property>
diff --git a/resources/ui/message.ui b/resources/ui/message.ui
index 41c2284..ceb84f4 100644
--- a/resources/ui/message.ui
+++ b/resources/ui/message.ui
@@ -61,6 +61,7 @@ Author: Tobias Frisch
61 <property name="label" translatable="yes">My evil comment text is awesome! This is coming to the Pinephone which will be extremely awesome and this text may only cut some borders in certain areas!!!</property> 61 <property name="label" translatable="yes">My evil comment text is awesome! This is coming to the Pinephone which will be extremely awesome and this text may only cut some borders in certain areas!!!</property>
62 <property name="wrap">True</property> 62 <property name="wrap">True</property>
63 <property name="max-width-chars">64</property> 63 <property name="max-width-chars">64</property>
64 <property name="xalign">0</property>
64 </object> 65 </object>
65 <packing> 66 <packing>
66 <property name="expand">True</property> 67 <property name="expand">True</property>
diff --git a/src/application.c b/src/application.c
index 0db05bb..e73d9de 100644
--- a/src/application.c
+++ b/src/application.c
@@ -79,6 +79,8 @@ application_init(MESSENGER_Application *app,
79 79
80 app->ui.mobile = FALSE; 80 app->ui.mobile = FALSE;
81 81
82 app->ui.bindings = g_hash_table_new(g_direct_hash, g_direct_equal);
83
82 g_application_add_main_option( 84 g_application_add_main_option(
83 G_APPLICATION(app->application), 85 G_APPLICATION(app->application),
84 "mobile", 86 "mobile",
@@ -138,6 +140,8 @@ application_run(MESSENGER_Application *app)
138 140
139 pthread_join(app->chat.tid, NULL); 141 pthread_join(app->chat.tid, NULL);
140 142
143 g_hash_table_destroy(app->ui.bindings);
144
141 notify_uninit(); 145 notify_uninit();
142 146
143 g_object_unref(app->application); 147 g_object_unref(app->application);
@@ -147,7 +151,8 @@ typedef struct MESSENGER_ApplicationEventCall
147{ 151{
148 MESSENGER_Application *app; 152 MESSENGER_Application *app;
149 MESSENGER_ApplicationEvent event; 153 MESSENGER_ApplicationEvent event;
150 void *cls; 154 int argc;
155 void **argv;
151} MESSENGER_ApplicationEventCall; 156} MESSENGER_ApplicationEventCall;
152 157
153static gboolean 158static gboolean
@@ -156,7 +161,10 @@ _application_event_call(gpointer user_data)
156 MESSENGER_ApplicationEventCall *call; 161 MESSENGER_ApplicationEventCall *call;
157 162
158 call = (MESSENGER_ApplicationEventCall*) user_data; 163 call = (MESSENGER_ApplicationEventCall*) user_data;
159 call->event(call->app, call->cls); 164 call->event(call->app, call->argc, call->argv);
165
166 if (call->argc > 0)
167 GNUNET_free(call->argv);
160 168
161 GNUNET_free(call); 169 GNUNET_free(call);
162 return FALSE; 170 return FALSE;
@@ -165,7 +173,8 @@ _application_event_call(gpointer user_data)
165void 173void
166application_call_event(MESSENGER_Application *app, 174application_call_event(MESSENGER_Application *app,
167 MESSENGER_ApplicationEvent event, 175 MESSENGER_ApplicationEvent event,
168 void *cls) 176 int argc,
177 void **argv)
169{ 178{
170 MESSENGER_ApplicationEventCall *call; 179 MESSENGER_ApplicationEventCall *call;
171 180
@@ -175,7 +184,17 @@ application_call_event(MESSENGER_Application *app,
175 184
176 call->app = app; 185 call->app = app;
177 call->event = event; 186 call->event = event;
178 call->cls = cls; 187 call->argc = argc;
188 call->argv = NULL;
189
190 if (call->argc > 0)
191 {
192 call->argv = GNUNET_new_array(call->argc, void*);
193
194 for (int i = 0; i < call->argc; i++) {
195 call->argv[i] = argv[i];
196 }
197 }
179 198
180 g_idle_add(_application_event_call, call); 199 g_idle_add(_application_event_call, call);
181} 200}
diff --git a/src/application.h b/src/application.h
index 61aed2a..ff9875d 100644
--- a/src/application.h
+++ b/src/application.h
@@ -61,6 +61,8 @@ typedef struct MESSENGER_Application
61 int status; 61 int status;
62 gboolean mobile; 62 gboolean mobile;
63 63
64 GHashTable *bindings;
65
64 UI_MESSENGER_Handle messenger; 66 UI_MESSENGER_Handle messenger;
65 67
66 UI_NEW_PLATFORM_Handle new_platform; 68 UI_NEW_PLATFORM_Handle new_platform;
@@ -76,12 +78,14 @@ void
76application_run(MESSENGER_Application *app); 78application_run(MESSENGER_Application *app);
77 79
78typedef void (*MESSENGER_ApplicationEvent) (MESSENGER_Application *app, 80typedef void (*MESSENGER_ApplicationEvent) (MESSENGER_Application *app,
79 void *cls); 81 int argc,
82 void **argv);
80 83
81void 84void
82application_call_event(MESSENGER_Application *app, 85application_call_event(MESSENGER_Application *app,
83 MESSENGER_ApplicationEvent event, 86 MESSENGER_ApplicationEvent event,
84 void *cls); 87 int argc,
88 void **argv);
85 89
86void 90void
87application_exit(MESSENGER_Application *app, 91application_exit(MESSENGER_Application *app,
diff --git a/src/chat/messenger.c b/src/chat/messenger.c
index 72ba93a..fc28490 100644
--- a/src/chat/messenger.c
+++ b/src/chat/messenger.c
@@ -52,7 +52,7 @@ _chat_messenger_idle(void *cls)
52 52
53static int 53static int
54_chat_messenger_message(void *cls, 54_chat_messenger_message(void *cls,
55 UNUSED struct GNUNET_CHAT_Context *context, 55 struct GNUNET_CHAT_Context *context,
56 const struct GNUNET_CHAT_Message *message) 56 const struct GNUNET_CHAT_Message *message)
57{ 57{
58 MESSENGER_Application *app = (MESSENGER_Application*) cls; 58 MESSENGER_Application *app = (MESSENGER_Application*) cls;
@@ -72,16 +72,23 @@ _chat_messenger_message(void *cls,
72 switch (kind) 72 switch (kind)
73 { 73 {
74 case GNUNET_CHAT_KIND_LOGIN: 74 case GNUNET_CHAT_KIND_LOGIN:
75 application_call_event(app, event_update_profile, NULL); 75 {
76 break; 76 application_call_event(app, event_update_profile, 0, NULL);
77 case GNUNET_CHAT_KIND_TEXT:
78 printf("text: %s\n", GNUNET_CHAT_message_get_text(message));
79 break; 77 break;
78 }
80 case GNUNET_CHAT_KIND_JOIN: 79 case GNUNET_CHAT_KIND_JOIN:
80 {
81 if (GNUNET_YES == GNUNET_CHAT_message_is_sent(message)) 81 if (GNUNET_YES == GNUNET_CHAT_message_is_sent(message))
82 application_call_event(app, event_update_chats, context); 82 application_call_event(app, event_update_chats, 1, (void**) &context);
83 83
84 break; 84 break;
85 }
86 case GNUNET_CHAT_KIND_TEXT:
87 {
88 void* event_data [2] = { context, &message };
89 application_call_event(app, event_receive_message, 2, event_data);
90 break;
91 }
85 default: 92 default:
86 break; 93 break;
87 } 94 }
diff --git a/src/event.c b/src/event.c
index 5ad3016..d11b8cf 100644
--- a/src/event.c
+++ b/src/event.c
@@ -74,7 +74,8 @@ _clear_each_widget(GtkWidget *widget,
74 74
75void 75void
76event_update_profile(MESSENGER_Application *app, 76event_update_profile(MESSENGER_Application *app,
77 UNUSED void *cls) 77 UNUSED int argc,
78 UNUSED void **argv)
78{ 79{
79 UI_MESSENGER_Handle *ui = &(app->ui.messenger); 80 UI_MESSENGER_Handle *ui = &(app->ui.messenger);
80 CHAT_MESSENGER_Handle *chat = &(app->chat.messenger); 81 CHAT_MESSENGER_Handle *chat = &(app->chat.messenger);
@@ -105,9 +106,13 @@ event_update_profile(MESSENGER_Application *app,
105 106
106void 107void
107event_update_chats(MESSENGER_Application *app, 108event_update_chats(MESSENGER_Application *app,
108 void *cls) 109 int argc,
110 void **argv)
109{ 111{
110 struct GNUNET_CHAT_Context *context = (struct GNUNET_CHAT_Context*) cls; 112 if (argc < 1)
113 return;
114
115 struct GNUNET_CHAT_Context *context = (struct GNUNET_CHAT_Context*) argv[0];
111 116
112 if (GNUNET_CHAT_context_get_user_pointer(context)) 117 if (GNUNET_CHAT_context_get_user_pointer(context))
113 return; 118 return;
@@ -120,4 +125,69 @@ event_update_chats(MESSENGER_Application *app,
120 125
121 // TODO: put something better here to attach it! 126 // TODO: put something better here to attach it!
122 GNUNET_CHAT_context_set_user_pointer(context, ui); 127 GNUNET_CHAT_context_set_user_pointer(context, ui);
128
129 g_hash_table_insert(
130 app->ui.bindings,
131 app->ui.messenger.send_record_button,
132 context
133 );
134
135 g_hash_table_insert(
136 app->ui.bindings,
137 app->ui.messenger.send_text_view,
138 context
139 );
140}
141
142void
143event_receive_message(MESSENGER_Application *app,
144 int argc,
145 void **argv)
146{
147 if (argc < 2)
148 return;
149
150 struct GNUNET_CHAT_Context *context = (struct GNUNET_CHAT_Context*) argv[0];
151
152 if (!GNUNET_CHAT_context_get_user_pointer(context))
153 return;
154
155 const struct GNUNET_CHAT_Message *msg;
156 msg = *((const struct GNUNET_CHAT_Message**) argv[1]);
157
158 const int sent = GNUNET_CHAT_message_is_sent(msg);
159
160 UI_MESSAGE_Handle *message = ui_message_new(app, GNUNET_YES == sent);
161
162 if (GNUNET_YES != sent)
163 {
164 const struct GNUNET_CHAT_Contact *contact = GNUNET_CHAT_message_get_sender(
165 msg
166 );
167
168 const char *sender = GNUNET_CHAT_contact_get_name(contact);
169
170 hdy_avatar_set_text(message->sender_avatar, sender? sender : "");
171 gtk_label_set_text(message->sender_label, sender? sender : "");
172 }
173
174 struct GNUNET_TIME_Absolute timestamp = GNUNET_CHAT_message_get_timestamp(
175 msg
176 );
177
178 const char *text = GNUNET_CHAT_message_get_text(msg);
179 const char *time = GNUNET_STRINGS_absolute_time_to_string(timestamp);
180
181 gtk_label_set_text(message->text_label, text? text : "");
182 gtk_label_set_text(message->timestamp_label, time? time : "");
183
184 if (message->read_receipt_image)
185 // TODO: check read receipt
186
187 gtk_container_add(
188 GTK_CONTAINER(app->ui.messenger.messages_listbox),
189 message->message_box
190 );
191
192 g_free(message); // TODO: this is just a test!
123} 193}
diff --git a/src/event.h b/src/event.h
index 6b5dce3..f59c793 100644
--- a/src/event.h
+++ b/src/event.h
@@ -29,10 +29,17 @@
29 29
30void 30void
31event_update_profile(MESSENGER_Application *app, 31event_update_profile(MESSENGER_Application *app,
32 void *cls); 32 int argc,
33 void **argv);
33 34
34void 35void
35event_update_chats(MESSENGER_Application *app, 36event_update_chats(MESSENGER_Application *app,
36 void *cls); 37 int argc,
38 void **argv);
39
40void
41event_receive_message(MESSENGER_Application *app,
42 int argc,
43 void **argv);
37 44
38#endif /* EVENT_H_ */ 45#endif /* EVENT_H_ */
diff --git a/src/ui/messenger.c b/src/ui/messenger.c
index c62cad9..9976e18 100644
--- a/src/ui/messenger.c
+++ b/src/ui/messenger.c
@@ -24,6 +24,8 @@
24 24
25#include "messenger.h" 25#include "messenger.h"
26 26
27#include <gtk-3.0/gdk/gdkkeys.h>
28
27#include "message.h" 29#include "message.h"
28#include "new_platform.h" 30#include "new_platform.h"
29#include "../application.h" 31#include "../application.h"
@@ -125,7 +127,7 @@ handle_send_text_buffer_changed(GtkTextBuffer *buffer,
125} 127}
126 128
127static void 129static void
128handle_send_record_button_click(UNUSED GtkButton *button, 130handle_send_record_button_click(GtkButton *button,
129 gpointer user_data) 131 gpointer user_data)
130{ 132{
131 MESSENGER_Application *app = (MESSENGER_Application*) user_data; 133 MESSENGER_Application *app = (MESSENGER_Application*) user_data;
@@ -142,14 +144,12 @@ handle_send_record_button_click(UNUSED GtkButton *button,
142 144
143 if (0 < g_utf8_strlen(text, 1)) 145 if (0 < g_utf8_strlen(text, 1))
144 { 146 {
145 // TODO: Actually send the message with current chat context! 147 struct GNUNET_CHAT_Context *context = g_hash_table_lookup(
146 148 app->ui.bindings, button
147 UI_MESSAGE_Handle *message = ui_message_new(app, TRUE); 149 );
148
149 gtk_label_set_text(message->text_label, text);
150 150
151 gtk_container_add(GTK_CONTAINER(app->ui.messenger.messages_listbox), message->message_box); 151 if (context)
152 g_free(message); // TODO: this is just a test! 152 GNUNET_CHAT_context_send_text(context, text);
153 } 153 }
154 else 154 else
155 { 155 {
@@ -159,6 +159,40 @@ handle_send_record_button_click(UNUSED GtkButton *button,
159 gtk_text_buffer_delete(buffer, &start, &end); 159 gtk_text_buffer_delete(buffer, &start, &end);
160} 160}
161 161
162static gboolean
163handle_send_text_key_press (GtkWidget *widget,
164 GdkEventKey *event,
165 gpointer user_data)
166{
167 MESSENGER_Application *app = (MESSENGER_Application*) user_data;
168
169 if ((event->state & GDK_SHIFT_MASK) ||
170 ((event->keyval != GDK_KEY_Return) &&
171 (event->keyval != GDK_KEY_KP_Enter)))
172 return FALSE;
173
174 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (widget));
175
176 GtkTextIter start, end;
177 gtk_text_buffer_get_start_iter(buffer, &start);
178 gtk_text_buffer_get_end_iter(buffer, &end);
179
180 const gchar *text = gtk_text_buffer_get_text(buffer, &start, &end, TRUE);
181
182 if (0 == g_utf8_strlen(text, 1))
183 return FALSE;
184
185 struct GNUNET_CHAT_Context *context = g_hash_table_lookup(
186 app->ui.bindings, widget
187 );
188
189 if (context)
190 GNUNET_CHAT_context_send_text(context, text);
191
192 gtk_text_buffer_delete(buffer, &start, &end);
193 return TRUE;
194}
195
162static void 196static void
163handle_main_window_destroy(UNUSED GtkWidget *window, 197handle_main_window_destroy(UNUSED GtkWidget *window,
164 gpointer user_data) 198 gpointer user_data)
@@ -412,6 +446,13 @@ ui_messenger_init(MESSENGER_Application *app,
412 app 446 app
413 ); 447 );
414 448
449 g_signal_connect(
450 handle->send_text_view,
451 "key-press-event",
452 G_CALLBACK(handle_send_text_key_press),
453 app
454 );
455
415 gtk_widget_show(GTK_WIDGET(handle->main_window)); 456 gtk_widget_show(GTK_WIDGET(handle->main_window));
416 457
417 g_signal_connect( 458 g_signal_connect(