Skip to content

Commit

Permalink
Add: libnotify for notification
Browse files Browse the repository at this point in the history
Add: Customize logs window
Fix Some bug's
  • Loading branch information
badcast committed Mar 24, 2024
1 parent 5961b08 commit 2356d54
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 87 deletions.
9 changes: 6 additions & 3 deletions client-frontend/dragon-tea/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pkg_check_modules(gtk3ui REQUIRED gtk+-3.0)
pkg_check_modules(libcurl REQUIRED libcurl)
pkg_check_modules(json REQUIRED json-c)
pkg_check_modules(gthread REQUIRED gthread-2.0)
pkg_check_modules(libnotify REQUIRED libnotify)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
Expand Down Expand Up @@ -48,10 +49,12 @@ target_sources(dragontea PUBLIC ${DRAGONTEA_SOURCES})
target_include_directories(
dragontea
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include/" ${gtk3ui_INCLUDE_DIRS}
${gthread_INCLUDE_DIRS} ${libcurl_INCLUDE_DIRS} ${json_INCLUDE_DIRS})
${gthread_INCLUDE_DIRS} ${libcurl_INCLUDE_DIRS} ${json_INCLUDE_DIRS}
${libnotify_INCLUDE_DIRS})

target_link_libraries(dragontea ${gtk3ui_LIBRARIES} ${libcurl_LIBRARIES}
${json_LIBRARIES} ${gthread_LIBRARIES})
target_link_libraries(
dragontea ${gtk3ui_LIBRARIES} ${libcurl_LIBRARIES} ${json_LIBRARIES}
${gthread_LIBRARIES} ${libnotify_LIBRARIES})

target_compile_definitions(dragontea
PUBLIC DRAGON_TEA_VERSION="${DRAGON_TEA_VERSION}")
Expand Down
3 changes: 2 additions & 1 deletion client-frontend/dragon-tea/include/tea.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ typedef DWORD uid_t;
#include <glib.h>
#include <gtk/gtk.h>
#include <json-c/json.h>
#include <libnotify/notify.h>

#include "tea_structs.h"
#include "tea_api.h"
Expand Down Expand Up @@ -58,7 +59,7 @@ typedef DWORD uid_t;

extern struct tea_net_stats net_stats;
extern struct tea_server_info cur_server;
extern struct tea_settings app_settings;
extern struct tea_settings env;
extern struct tea_app_widgets widgets;

void tea_init();
Expand Down
3 changes: 3 additions & 0 deletions client-frontend/dragon-tea/include/tea_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ struct tea_settings
char servers[32][255];
// show log window
int show_logs;
// remove old notification
int old_notify_remove;
// User messages
GArray *local_msg_db;
// Log I/O
Expand Down Expand Up @@ -164,6 +166,7 @@ struct tea_app_widgets
GtkWidget *top_label_user_state;
guint timeout_periodic_sync;
gboolean chat_synched;
gboolean firstTime;
} chat_tab;

struct
Expand Down
2 changes: 0 additions & 2 deletions client-frontend/dragon-tea/include/ui_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

void tea_on_authenticate(const struct tea_id_info *user_info);

void tea_on_logouted();

void ui_on_close_window(GtkWidget *window, gpointer data);

void ui_on_notebook_switch_page(GtkNotebook *notebook, GtkWidget *page, guint page_num, gpointer user_data);
Expand Down
41 changes: 27 additions & 14 deletions client-frontend/dragon-tea/src/core/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ void tea_ui_init()
tea_try_login();
tea_ui_focus_tab(UI_TAB_AUTH);

if(app_settings.show_logs)
// libnotify init
notify_init("Dragon Tea Messenger");

if(env.show_logs)
show_log_dialog();
}

Expand All @@ -85,10 +88,10 @@ void tea_ui_update_settings()
int selServer = 0;
for(int x = 0; x < 32; ++x)
{
if(strlen(app_settings.servers[x]) == 0)
if(strlen(env.servers[x]) == 0)
break;

if(tea_get_server_id(app_settings.servers[x]) == app_settings.active_server)
if(tea_get_server_id(env.servers[x]) == env.active_server)
{
selServer = x;
break;
Expand Down Expand Up @@ -127,7 +130,9 @@ void tea_ui_chat_interactable(int value)
void tea_ui_chat_sync()
{
widgets.chat_tab.chat_synched = FALSE;
on_chat_message_handler_async(NULL);

if(tea_is_connected())
on_chat_message_handler_async(NULL);
}

void tea_ui_chat_status_text(const char *status_text)
Expand Down Expand Up @@ -180,23 +185,34 @@ void tea_ui_chat_push_block(const struct tea_message_id *message)
static const char format_message_block[] = "\n\n>>%s@[%s]: %s\n";

gchar *date_time;
GDateTime *prevgtime, *_gdatetime = g_date_time_new_from_unix_local(message->time_received);
GDateTime *prevgtime, *_gdatetime = g_date_time_new_from_unix_utc(message->time_received);

if(app_settings.local_msg_db->len == 0)
if(env.local_msg_db->len == 0)
{
prevgtime = _gdatetime;
}
else
{
prevgtime = g_date_time_new_from_unix_local(
g_array_index(app_settings.local_msg_db, struct tea_message_id, (app_settings.local_msg_db->len - 1)).time_received);
prevgtime = g_date_time_new_from_unix_utc(
g_array_index(env.local_msg_db, struct tea_message_id, (env.local_msg_db->len - 1)).time_received);
}

// TODO: sort by date

date_time = g_date_time_format(prevgtime, "%d.%m.%Y");
tea_ui_chat_push_text_raw(date_time, -1);
g_free(date_time);
if(prevgtime == _gdatetime ||
!(g_date_time_get_day_of_month(prevgtime) == g_date_time_get_day_of_month(_gdatetime) &&
g_date_time_get_month(prevgtime) == g_date_time_get_month(_gdatetime) &&
g_date_time_get_year(prevgtime) == g_date_time_get_year(_gdatetime)))
{
date_time = g_date_time_format(prevgtime, "\n\n\t::::%d.%m.%Y::::\n");

tea_ui_chat_push_text_raw(date_time, -1);

g_free(date_time);
}

if(prevgtime != _gdatetime)
g_free(prevgtime);

date_time = g_date_time_format(_gdatetime, "%H:%M");
int require_size = snprintf(NULL, 0, format_message_block, message->sent_user_name, date_time, message->message_text);
Expand All @@ -206,9 +222,6 @@ void tea_ui_chat_push_block(const struct tea_message_id *message)

snprintf(text_buffer, require_size, format_message_block, message->sent_user_name, date_time, message->message_text);

if(prevgtime != _gdatetime)
g_free(prevgtime);

g_free(_gdatetime);
g_free(date_time);

Expand Down
64 changes: 45 additions & 19 deletions client-frontend/dragon-tea/src/core/ui_callbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ gpointer async_send(const char *text)

int len = strlen(text);
// send message to
net_api_write_message(&app_settings.id_info, -1, text, len, &last_send_result);
net_api_write_message(&env.id_info, -1, text, len, &last_send_result);
// set work complete
thread_send_msg = NULL;

Expand All @@ -33,17 +33,17 @@ gpointer async_reply(gpointer)
// Последнее сообщение на локальной машине (когда на сервере она устарело)
int last_local_msg_id;

if(app_settings.local_msg_db->len == 0)
if(env.local_msg_db->len == 0)
{
last_local_msg_id = 0;
}
else
{
last_local_msg_id = (&g_array_index(app_settings.local_msg_db, struct tea_message_id, app_settings.local_msg_db->len - 1))->msg_id;
last_local_msg_id = (&g_array_index(env.local_msg_db, struct tea_message_id, env.local_msg_db->len - 1))->msg_id;
}

// Читаем сообщение других пользователей и поддерживаем коммуникацию
net_api_read_messages(&app_settings.id_info, -1, last_local_msg_id, MESSAGES_PER_REQUEST, &last_read_result);
net_api_read_messages(&env.id_info, -1, last_local_msg_id, MESSAGES_PER_REQUEST, &last_read_result);

thread_reply_msg = NULL;

Expand Down Expand Up @@ -106,6 +106,12 @@ gboolean on_chat_sending_async(const gchar *text)
return worked == FALSE;
}

void notify_action(NotifyNotification *notification, const char *action, gpointer user_data)
{
ui_error(action);
gtk_window_set_focus(widgets.main_window, NULL);
}

gboolean on_chat_message_handler_async(gpointer)
{
char buffer[128];
Expand Down Expand Up @@ -138,10 +144,14 @@ gboolean on_chat_message_handler_async(gpointer)
if(last_chance_state != CHANCE_TO_LOGOUT)
strcpy(buffer, _("Your network has been restored. "));
else
buffer[0] = 0;
buffer[0] = NULL;

strcat(buffer, _("You're online."));
tea_ui_chat_status_text(buffer);

if(widgets.chat_tab.firstTime)
widgets.chat_tab.firstTime = FALSE;

break;
}

Expand All @@ -152,15 +162,30 @@ gboolean on_chat_message_handler_async(gpointer)
break;
}

NotifyNotification *gnotify;
struct tea_message_id *message;
// Отображение собщений
for(int x = 0; x < last_read_result.messages->len; ++x)
{
tea_ui_chat_push_block(&g_array_index(last_read_result.messages, struct tea_message_id, x));
g_array_append_val(app_settings.local_msg_db, (g_array_index(last_read_result.messages, struct tea_message_id, x)));
message = &g_array_index(last_read_result.messages, struct tea_message_id, x);
tea_ui_chat_push_block(message);
if(!widgets.chat_tab.firstTime && message->sent_user_id != env.id_info.user_id &&
!gtk_window_is_active(widgets.main_window))
{
gnotify = notify_notification_new(
message->sent_user_name, message->message_text, gtk_window_get_icon_name(widgets.main_window));
notify_notification_add_action(gnotify, "custom_action", _("Read message"), notify_action, NULL, NULL);

if(!env.old_notify_remove)
notify_notification_set_timeout(gnotify, 0);

notify_notification_show(gnotify, NULL);
g_object_unref(gnotify);
}
g_array_append_val(env.local_msg_db, *message);
}

// Добавление сообщений из загруженных в локальные
g_array_append_vals(app_settings.local_msg_db, last_read_result.messages->data, last_read_result.messages->len);
// Have notify

// Освобождение старого массива
g_array_free(last_read_result.messages, TRUE);
Expand Down Expand Up @@ -212,7 +237,7 @@ gboolean on_chat_message_handler_async(gpointer)
thread_reply_msg = g_thread_new(NULL, (GThreadFunc) async_reply, NULL);
}
}
return TRUE; // EVERYTHING
return tea_is_connected(); // EVERYTHING
}

void ui_on_close_window(GtkWidget *window, gpointer data)
Expand Down Expand Up @@ -258,7 +283,7 @@ void on_chat_send_button(GtkWidget *widget, gpointer data)
void tea_on_authenticate(const struct tea_id_info *user_info)
{
char buffer[300], buf2[32];
app_settings.connected = TRUE;
env.connected = TRUE;

tea_ui_chat_clear();

Expand Down Expand Up @@ -305,19 +330,20 @@ void tea_on_authenticate(const struct tea_id_info *user_info)

// reset chances
check_chance_logouting = CHANCE_TO_LOGOUT;
widgets.chat_tab.firstTime = TRUE;

// sync
tea_ui_chat_sync();

widgets.chat_tab.timeout_periodic_sync = g_timeout_add(INTERVAL_CHAT_SYNC, (GSourceFunc) on_chat_message_handler_async, NULL);
}

void tea_on_logouted()
void tea_logout()
{
if(!app_settings.connected)
if(!env.connected)
return;

app_settings.connected = FALSE;
env.connected = FALSE;

// remove message handler
if(widgets.chat_tab.timeout_periodic_sync)
Expand All @@ -344,16 +370,16 @@ void tea_on_logouted()
free(g_array_index(last_read_result.messages, struct tea_message_id, x).message_text);
}
g_array_free(last_read_result.messages, TRUE);
memset(&last_read_result, 0, sizeof(last_read_result));
}
memset(&last_read_result, 0, sizeof(last_read_result));

// Clear local messages
for(size_t x = 0; x < app_settings.local_msg_db->len; ++x)
for(size_t x = 0; x < env.local_msg_db->len; ++x)
{
free(g_array_index(app_settings.local_msg_db, struct tea_message_id, x).message_text);
free(g_array_index(env.local_msg_db, struct tea_message_id, x).message_text);
}
g_array_free(app_settings.local_msg_db, TRUE);
app_settings.local_msg_db = g_array_new(FALSE, FALSE, sizeof(struct tea_message_id));
g_array_free(env.local_msg_db, TRUE);
env.local_msg_db = g_array_new(FALSE, FALSE, sizeof(struct tea_message_id));

#ifdef TEA_OS_LINUX
// Release RESIDENT SET SIZE (RSS) and put to System
Expand Down
12 changes: 6 additions & 6 deletions client-frontend/dragon-tea/src/core/ui_logs.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ void tea_log(const char *message)
printf("%s\n", message);

GtkTextIter eIter;
gtk_text_buffer_get_end_iter(app_settings.log_buffer, &eIter);
gtk_text_buffer_insert(app_settings.log_buffer, &eIter, message, -1);
gtk_text_buffer_get_end_iter(env.log_buffer, &eIter);
gtk_text_buffer_insert(env.log_buffer, &eIter, message, -1);

gtk_text_buffer_get_end_iter(app_settings.log_buffer, &eIter);
gtk_text_buffer_insert(app_settings.log_buffer, &eIter, "\n", -1);
gtk_text_buffer_get_end_iter(env.log_buffer, &eIter);
gtk_text_buffer_insert(env.log_buffer, &eIter, "\n", -1);
}

void tea_clear_log()
{
gtk_text_buffer_set_text(app_settings.log_buffer, "", 0);
gtk_text_buffer_set_text(env.log_buffer, "", 0);
}

void tea_ui_clear_buffer(GtkWidget *, gpointer)
Expand All @@ -46,7 +46,7 @@ void show_log_dialog()
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_text), GTK_SHADOW_IN);
gtk_scrolled_window_set_kinetic_scrolling(GTK_SCROLLED_WINDOW(scrolled_text), TRUE);

GtkWidget *textView = gtk_text_view_new_with_buffer(app_settings.log_buffer);
GtkWidget *textView = gtk_text_view_new_with_buffer(env.log_buffer);
gtk_text_view_set_editable(GTK_TEXT_VIEW(textView), FALSE);

gtk_container_add(GTK_CONTAINER(scrolled_text), textView);
Expand Down
21 changes: 13 additions & 8 deletions client-frontend/dragon-tea/src/core/ui_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ void apply_changes()
int selected = gtk_combo_box_get_active(GTK_COMBO_BOX(widgets.settings_tab.combx_server_list));

// Switch server
tea_switch_server(tea_get_server_id(app_settings.servers[selected]));
tea_switch_server(tea_get_server_id(env.servers[selected]));

tea_ui_focus_tab(UI_TAB_AUTH);
tea_try_login();
}

void on_check_button_show_log(GtkWidget *widget, gpointer data)
void toggle_button(GtkWidget *widget, gpointer data)
{
app_settings.show_logs = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
(*(int *) data) = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
}

void on_button_clk_show_log(GtkWidget *widget, gpointer data)
Expand All @@ -33,13 +33,13 @@ GtkWidget *create_settings_widget()
GtkListStore *server_list = gtk_list_store_new(1, G_TYPE_STRING);
GtkTreeIter iter;

for(int x = 0; x < sizeof(app_settings.servers[0]); ++x)
for(int x = 0; x < sizeof(env.servers[0]); ++x)
{
if(strlen(app_settings.servers[x]) == 0)
if(strlen(env.servers[x]) == 0)
break;

gtk_list_store_append(server_list, &iter);
gtk_list_store_set(server_list, &iter, 0, app_settings.servers[x], -1);
gtk_list_store_set(server_list, &iter, 0, env.servers[x], -1);
}

gtk_box_pack_start(GTK_BOX(widgetbox), gtk_label_new(_("Switch server: ")), TRUE, FALSE, 0);
Expand All @@ -56,8 +56,13 @@ GtkWidget *create_settings_widget()
gtk_box_pack_start(GTK_BOX(widgetbox), combobox, TRUE, FALSE, 0);

GtkWidget *currentWidget = gtk_check_button_new_with_label(_("Show log window on startup"));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(currentWidget), app_settings.show_logs);
g_signal_connect(G_OBJECT(currentWidget), "toggled", G_CALLBACK(on_check_button_show_log), NULL);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(currentWidget), env.show_logs);
g_signal_connect(G_OBJECT(currentWidget), "toggled", G_CALLBACK(toggle_button), &env.show_logs);
gtk_box_pack_start(GTK_BOX(widgetbox), currentWidget, TRUE, FALSE, 0);

currentWidget = gtk_check_button_new_with_label("Remove old notifications");
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(currentWidget), env.old_notify_remove);
g_signal_connect(G_OBJECT(currentWidget), "clicked", G_CALLBACK(toggle_button), &env.old_notify_remove);
gtk_box_pack_start(GTK_BOX(widgetbox), currentWidget, TRUE, FALSE, 0);

currentWidget = gtk_button_new_with_label("Show log");
Expand Down
Loading

0 comments on commit 2356d54

Please sign in to comment.