From 7a831376de93f61ab7234089a0aea7315d987e94 Mon Sep 17 00:00:00 2001 From: badcast Date: Thu, 11 Jan 2024 02:01:45 +0600 Subject: [PATCH] Fix client API Fix some bug Signed-off-by: badcast --- CMakeLists.txt | 5 + client-frontend/dragon-tea/CMakeLists.txt | 5 - client-frontend/dragon-tea/include/tea.h | 3 +- client-frontend/dragon-tea/include/tea_api.h | 9 +- .../dragon-tea/include/tea_structs.h | 24 ++- client-frontend/dragon-tea/src/core/ui.c | 4 +- client-frontend/dragon-tea/src/core/ui_chat.c | 14 -- client-frontend/dragon-tea/src/core/ui_logs.c | 6 +- .../dragon-tea/src/core/ui_settings.c | 5 +- .../dragon-tea/src/net/tea_net_api.c | 191 +++++++++--------- client-frontend/dragon-tea/src/tea.c | 19 +- 11 files changed, 158 insertions(+), 127 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8cd58be..f2b892a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,8 @@ cmake_minimum_required(VERSION 3.10) +project( + DragonTea + VERSION ${DRAGON_TEA_VERSION} + LANGUAGES C) + add_subdirectory(client-frontend/dragon-tea) diff --git a/client-frontend/dragon-tea/CMakeLists.txt b/client-frontend/dragon-tea/CMakeLists.txt index 26e3417..3996b45 100644 --- a/client-frontend/dragon-tea/CMakeLists.txt +++ b/client-frontend/dragon-tea/CMakeLists.txt @@ -3,11 +3,6 @@ file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" DRAGON_TEA_VERSION) string(STRIP "${DRAGON_TEA_VERSION}" DRAGON_TEA_VERSION) -project( - DragonTea - VERSION ${DRAGON_TEA_VERSION} - LANGUAGES C) - find_package(PkgConfig REQUIRED) pkg_check_modules(gtk3ui REQUIRED gtk+-3.0) pkg_check_modules(libcurl REQUIRED libcurl) diff --git a/client-frontend/dragon-tea/include/tea.h b/client-frontend/dragon-tea/include/tea.h index 35bf193..f6e6f1b 100644 --- a/client-frontend/dragon-tea/include/tea.h +++ b/client-frontend/dragon-tea/include/tea.h @@ -57,6 +57,7 @@ typedef DWORD uid_t; #endif extern struct tea_net_stats net_stats; +extern struct tea_server_info cur_server; extern struct tea_settings app_settings; extern struct tea_app_widgets widgets; @@ -114,8 +115,6 @@ int net_api_signin(tea_id_t user_id, tea_login_result *output); int net_api_signup(const char *nickname, tea_register_result *output); -int net_api_server_info(struct tea_server_info *serverInfo); - void tea_log(const char *message); void tea_clear_log(); diff --git a/client-frontend/dragon-tea/include/tea_api.h b/client-frontend/dragon-tea/include/tea_api.h index 0083049..b2943e7 100644 --- a/client-frontend/dragon-tea/include/tea_api.h +++ b/client-frontend/dragon-tea/include/tea_api.h @@ -25,10 +25,11 @@ enum TeaErrorStatus }; const char *tea_url_server(); -const char *tea_url_server_auth(); -const char *tea_url_server_register(); -const char *tea_url_server_message_handler(); -const char *tea_url_server_info(); +int tea_switch_server(int newServerID); + +int tea_fetch_server(); + +void tea_read_urls(struct tea_server_urls *wrData); #endif diff --git a/client-frontend/dragon-tea/include/tea_structs.h b/client-frontend/dragon-tea/include/tea_structs.h index a7b9e68..578f694 100644 --- a/client-frontend/dragon-tea/include/tea_structs.h +++ b/client-frontend/dragon-tea/include/tea_structs.h @@ -66,6 +66,24 @@ struct tea_net_stats int error_req; }; +struct tea_server_urls +{ + char url[255]; + char url_auth[255]; + char url_reg[255]; + char url_msg_handler[255]; + char url_info[255]; +}; + +// Server features +enum +{ + // Server can get information + SV_VERFI = 1, + // Server can get First or Last MsgID + SV_MSGFL = 2, +}; + struct tea_server_info { struct @@ -76,7 +94,11 @@ struct tea_server_info } server_version; char maintainer[32]; - char license[32]; + char license[16]; + + int features; + + struct tea_server_urls urls; }; typedef struct diff --git a/client-frontend/dragon-tea/src/core/ui.c b/client-frontend/dragon-tea/src/core/ui.c index f8cbd37..0fc76c1 100644 --- a/client-frontend/dragon-tea/src/core/ui.c +++ b/client-frontend/dragon-tea/src/core/ui.c @@ -16,8 +16,6 @@ void tea_ui_init() g_object_unref(icon_buf); } - show_log_dialog(); - // Init main window GtkWidget *main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(main_window), _("Dragon Tea")); @@ -77,6 +75,8 @@ void tea_ui_init() // Probe auto sign in if the exists tea_try_login(); tea_ui_focus_tab(UI_TAB_AUTH); + + show_log_dialog(); } void tea_ui_update_settings() diff --git a/client-frontend/dragon-tea/src/core/ui_chat.c b/client-frontend/dragon-tea/src/core/ui_chat.c index 0f3048c..ed0b4db 100644 --- a/client-frontend/dragon-tea/src/core/ui_chat.c +++ b/client-frontend/dragon-tea/src/core/ui_chat.c @@ -3,7 +3,6 @@ void on_entry_edit_message(GtkEntry *entry, gpointer data) { - // Кнопка отправить доступна, когда длина текста сообщений больше 0 gtk_widget_set_sensitive(widgets.chat_tab.button_send, gtk_entry_get_text_length(entry) > 0); } @@ -14,18 +13,12 @@ void on_button_logout(GtkButton *button, gpointer data) GtkWidget *create_chat_widget() { - // Создание верхнего уровня окна GtkWidget *place_widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); - // Создание верхнего статуса GtkWidget *top_horz_box = widgets.chat_tab.top_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); - // Создание вертикального контейнера (GtkBox) GtkWidget *vertical_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); - // Создание горизонтального контейнера (GtkBox) GtkWidget *horizontal_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); - // Создание контейнер статуса GtkWidget *status_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); - // Создание верхнего поля отображение информаций gtk_box_pack_start( GTK_BOX(top_horz_box), widgets.chat_tab.top_button_logout = gtk_button_new_from_icon_name("system-log-out", GTK_ICON_SIZE_BUTTON), @@ -37,7 +30,6 @@ GtkWidget *create_chat_widget() gtk_box_pack_start(GTK_BOX(top_horz_box), widgets.chat_tab.top_label_user_state = gtk_label_new(NULL), FALSE, FALSE, 5); gtk_box_pack_start(GTK_BOX(place_widget), top_horz_box, FALSE, TRUE, 0); - // Создание текстового поля (GtkTextView) и установка его в вертикальный контейнер GtkWidget *scrolled_text = widgets.chat_tab.scrolled_window_chat = gtk_scrolled_window_new(NULL, NULL); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_text), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_text), GTK_SHADOW_IN); @@ -47,32 +39,26 @@ GtkWidget *create_chat_widget() gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text_view), GTK_WRAP_CHAR); gtk_text_view_set_editable(GTK_TEXT_VIEW(text_view), FALSE); - // init tags gtk_container_add(GTK_CONTAINER(scrolled_text), text_view); gtk_box_pack_start(GTK_BOX(vertical_box), scrolled_text, TRUE, TRUE, 10); - // Создание поля ввода текста (GtkEntry) GtkWidget *entry = widgets.chat_tab.entry_message_set = gtk_entry_new(); gtk_box_pack_start(GTK_BOX(horizontal_box), entry, TRUE, TRUE, 0); gtk_entry_set_max_length(GTK_ENTRY(entry), TEA_MAXLEN_MESSAGE - 1); g_signal_connect(entry, "activate", G_CALLBACK(on_chat_send_button), entry); g_signal_connect(entry, "changed", G_CALLBACK(on_entry_edit_message), NULL); - // Создание кнопки для отправки GtkWidget *button = widgets.chat_tab.button_send = gtk_button_new(); gtk_widget_set_sensitive(button, FALSE); gtk_button_set_image(GTK_BUTTON(button), gtk_image_new_from_icon_name("mail-send-symbolic", GTK_ICON_SIZE_BUTTON)); gtk_box_pack_start(GTK_BOX(horizontal_box), button, FALSE, FALSE, 0); - // Подключение обработчика сигнала "clicked" (нажатие кнопки) g_signal_connect(button, "clicked", G_CALLBACK(on_chat_send_button), entry); - // Добавление вертикального контейнера в окно gtk_box_pack_end(GTK_BOX(place_widget), vertical_box, TRUE, TRUE, 0); - // Добавление горизонтального контейнера в вертикальный контейнер gtk_box_pack_start(GTK_BOX(vertical_box), horizontal_box, FALSE, FALSE, 0); gtk_box_pack_end(GTK_BOX(vertical_box), status_box, FALSE, FALSE, 0); diff --git a/client-frontend/dragon-tea/src/core/ui_logs.c b/client-frontend/dragon-tea/src/core/ui_logs.c index 895943a..849d5fc 100644 --- a/client-frontend/dragon-tea/src/core/ui_logs.c +++ b/client-frontend/dragon-tea/src/core/ui_logs.c @@ -29,9 +29,13 @@ void tea_ui_clear_buffer(GtkWidget *, gpointer) void show_log_dialog() { + gint x, y, w; + gtk_window_get_position(widgets.main_window, &x, &y); + gtk_window_get_size(widgets.main_window, &w, NULL); + GtkWindow *log_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size(log_window, 300, 400); - + gtk_window_move(log_window, x + w, y); GtkBox *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); GtkWidget *scrolled_text = gtk_scrolled_window_new(NULL, NULL); diff --git a/client-frontend/dragon-tea/src/core/ui_settings.c b/client-frontend/dragon-tea/src/core/ui_settings.c index edb983f..8b5f7d3 100644 --- a/client-frontend/dragon-tea/src/core/ui_settings.c +++ b/client-frontend/dragon-tea/src/core/ui_settings.c @@ -5,7 +5,10 @@ void apply_changes() tea_logout(); int selected = gtk_combo_box_get_active(GTK_COMBO_BOX(widgets.settings_tab.combx_server_list)); - app_settings.active_server = tea_get_server_id(app_settings.servers[selected]); + + // Switch server + tea_switch_server(tea_get_server_id(app_settings.servers[selected])); + tea_ui_focus_tab(UI_TAB_AUTH); tea_try_login(); } diff --git a/client-frontend/dragon-tea/src/net/tea_net_api.c b/client-frontend/dragon-tea/src/net/tea_net_api.c index 84b5fa9..cebfdc3 100644 --- a/client-frontend/dragon-tea/src/net/tea_net_api.c +++ b/client-frontend/dragon-tea/src/net/tea_net_api.c @@ -1,18 +1,8 @@ #include +#include #include "tea.h" -struct tea_net_stats net_stats; -struct tea_server_info current_server_info; - -struct -{ - char SAUTH[255]; - char SREGISTER[255]; - char SMHANDLER[255]; - char SINFO[255]; -} slocal_vars; - struct net_responce_t { size_t size; @@ -21,7 +11,10 @@ struct net_responce_t char *json_data; }; +struct tea_net_stats net_stats; +struct tea_server_info cur_server; GMutex nmutex; + void net_init() { g_mutex_init(&nmutex); @@ -75,7 +68,7 @@ int curl_xferinfo(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_ return CURLE_OK; } -int net_send(const char *url, const char *body, size_t len, struct net_responce_t *receiver) +int net_send(const char *url, const char *body, size_t len, struct net_responce_t *receiver, int find_json) { CURL *curl; CURLcode net_result; @@ -128,16 +121,19 @@ int net_send(const char *url, const char *body, size_t len, struct net_responce_ if(net_result == CURLE_OK) { - char *p = receiver->raw_data; - char *pe = p + receiver->size; - for(; p < pe;) + if(find_json) { - if(*p == '{') + char *p = receiver->raw_data; + char *pe = p + receiver->size; + for(; p < pe;) { - receiver->json_data = p; - break; + if(*p == '{') + { + receiver->json_data = p; + break; + } + ++p; } - ++p; } } else @@ -210,7 +206,7 @@ int net_api_read_messages( json_object_object_add(json_request, "max_messages", json_object_new_int(max_messages)); jstr = json_object_to_json_string_length(json_request, JSON_C_TO_STRING_PLAIN, &_size); - if((result = net_send(tea_url_server_message_handler(), jstr, _size, &input)) && input.raw_data) + if((result = net_send(cur_server.urls.url_msg_handler, jstr, _size, &input, 1)) && input.raw_data) { json_result = json_tokener_parse(input.json_data); @@ -304,7 +300,7 @@ int net_api_write_message( json_object_object_add(json_request, "message", json_object_new_string_len(message, len)); json_serialized = json_object_to_json_string_length(json_request, JSON_C_TO_STRING_PLAIN, &_size); - if((net = net_send(tea_url_server_message_handler(), json_serialized, _size, &input)) && input.raw_data) + if((net = net_send(cur_server.urls.url_msg_handler, json_serialized, _size, &input, 1)) && input.raw_data) { json_result = json_tokener_parse(input.json_data); @@ -354,7 +350,7 @@ int net_api_signin(tea_id_t user_id, tea_login_result *output) json_object_object_add(json_request, "user_id", json_object_new_int64(user_id)); json_serialized = json_object_to_json_string_length(json_request, JSON_C_TO_STRING_PLAIN, &_size); - if((net = net_send(tea_url_server_auth(), json_serialized, _size, &input)) && input.raw_data) + if((net = net_send(cur_server.urls.url_auth, json_serialized, _size, &input, 1)) && input.raw_data) { jresult = json_tokener_parse(input.json_data); @@ -419,7 +415,7 @@ int net_api_signup(const char *nickname, tea_register_result *output) json_serialized = json_object_to_json_string_length(json_request, JSON_C_TO_STRING_PLAIN, &_size); - if((net = net_send(tea_url_server_register(), json_serialized, _size, &input)) && input.raw_data) + if((net = net_send(cur_server.urls.url_reg, json_serialized, _size, &input, 1)) && input.raw_data) { jresult = json_tokener_parse(input.json_data); @@ -466,52 +462,6 @@ int net_api_signup(const char *nickname, tea_register_result *output) return net; } -int net_api_server_info(struct tea_server_info *serverInfo) -{ - int net; - struct net_responce_t responce; - - net = net_send(tea_url_server_info(), NULL, 0, &responce); - // OK - if(net) - { - sscanf( - responce.raw_data, - "%d.%d.%d", - &serverInfo->server_version.major, - &serverInfo->server_version.minor, - &serverInfo->server_version.patch); - - /* - * 1.0.0 - no supported get first/last message id - * 1.0.1 - supported get first/last message id - */ - - // calculate supported version - if(serverInfo->server_version.major == 1) - { - // MAJOR = 1 - } - - if(serverInfo->server_version.minor == 0) - { - // MINOR = 0 - } - - if(serverInfo->server_version.patch == 1) - { - // PATCH = 1 - } - } - - if(responce.raw_data) - { - free(responce.raw_data); - } - - return net; -} - const char *tea_url_server() { int s = -1; @@ -534,41 +484,96 @@ const char *tea_url_server() return app_settings.servers[s]; } -const char *tea_url_server_auth() +int tea_switch_server(int newServerID) { - const char *server = tea_url_server(); - if(!server) - return server; - snprintf(slocal_vars.SAUTH, sizeof(slocal_vars.SAUTH), "%sapi/auth.php", server); - return slocal_vars.SAUTH; + app_settings.active_server = newServerID; + + // init new server ID + tea_read_urls(&cur_server.urls); + tea_fetch_server(); + return 0; } -const char *tea_url_server_register() +int tea_fetch_server() { + int result; const char *server = tea_url_server(); - if(!server) - return server; - snprintf(slocal_vars.SREGISTER, sizeof(slocal_vars.SREGISTER), "%sapi/register.php", server); - return slocal_vars.SREGISTER; -} -const char *tea_url_server_message_handler() -{ + if(server != NULL) + { + struct net_responce_t nrwp; - const char *server = tea_url_server(); - if(!server) - return server; - snprintf(slocal_vars.SMHANDLER, sizeof(slocal_vars.SMHANDLER), "%sapi/messageHandler.php", server); - return slocal_vars.SMHANDLER; + if((result = net_send(cur_server.urls.url_info, NULL, 0, &nrwp, 0))) + { + if(nrwp.raw_data != NULL) + { + int rfeatures = 0; + sscanf( + nrwp.raw_data, + "%" SCNd8 ".%" SCNd8 ".%" SCNd8 "\n%s\n%s", + &cur_server.server_version.major, + &cur_server.server_version.minor, + &cur_server.server_version.patch, + &cur_server.maintainer, + &cur_server.license); + + /* + * 1.0.0 - no supported get first/last message id + * 1.1.0: - supported get first/last message id + * - supported get server info + */ + + // calculate supported version + if(cur_server.server_version.major == 1) + { + // MAJOR = 1 + + if(cur_server.server_version.minor == 1) + { + rfeatures |= SV_VERFI; + rfeatures |= SV_MSGFL; + } + } + + if(cur_server.server_version.minor == 1) + { + // MINOR = 0 + } + + if(cur_server.server_version.patch == 0) + { + // PATCH = 1 + } + + // Set features + cur_server.features = rfeatures; + + // put unused resource + free(nrwp.raw_data); + } + } + } + else + result = 0; + + return result; } -const char *tea_url_server_info() +void tea_read_urls(struct tea_server_urls *wrData) { const char *server = tea_url_server(); - if(!server) - return server; - snprintf(slocal_vars.SINFO, sizeof(slocal_vars.SINFO), "%sapi/TEA_SERVER_VERSION", server); - return slocal_vars.SINFO; + if(server == NULL) + { + printf("server == NULL"); + return; + } + + strncpy(wrData->url, server, sizeof(wrData->url)); + + snprintf(wrData->url_auth, sizeof(wrData->url_auth), "%sapi/auth.php", server); + snprintf(wrData->url_reg, sizeof(wrData->url_reg), "%sapi/register.php", server); + snprintf(wrData->url_msg_handler, sizeof(wrData->url_msg_handler), "%sapi/messageHandler.php", server); + snprintf(wrData->url_info, sizeof(wrData->url_info), "%sapi/TEA_SERVER_VERSION", server); } diff --git a/client-frontend/dragon-tea/src/tea.c b/client-frontend/dragon-tea/src/tea.c index 1e1f49c..d048b9b 100644 --- a/client-frontend/dragon-tea/src/tea.c +++ b/client-frontend/dragon-tea/src/tea.c @@ -36,7 +36,7 @@ void tea_save(); const char *tea_error_string(int error_code) { #define MACRO_PUT_CASE(X) \ -case X: \ + case X: \ result_value = #X; \ break; const char *result_value = NULL; @@ -55,9 +55,9 @@ case X: \ MACRO_PUT_CASE(TEA_STATUS_INTERNAL_SERVER_ERROR); MACRO_PUT_CASE(TEA_STATUS_NETWORK_ERROR); - default: - result_value = "Unknown"; - break; + default: + result_value = "Unknown"; + break; } return result_value; #undef MACRO_PUT_CASE @@ -141,6 +141,9 @@ void tea_load() // Init logs app_settings.log_buffer = gtk_text_buffer_new(NULL); + + // switching server + tea_switch_server(app_settings.active_server); } void tea_save() @@ -316,3 +319,11 @@ void ui_error_fail(const char *str) ui_error(str); exit(EXIT_FAILURE); } + +int tea_server_version() +{ + if(strlen(cur_server.urls.url) == 0) + return -1; + + return (int) (cur_server.server_version.major | cur_server.server_version.minor << 8 | cur_server.server_version.patch << 16); +}