diff --git a/client/inc/types.h b/client/inc/types.h index 57b5c4b..de92975 100644 --- a/client/inc/types.h +++ b/client/inc/types.h @@ -11,6 +11,7 @@ typedef struct s_user { char* name; char* password; char* avatar_path; + t_avatar_color avatar_color; } t_user; typedef struct s_client_utils { diff --git a/client/src/gui/build_chat_screen.c b/client/src/gui/build_chat_screen.c index 954f94c..00c3fd8 100644 --- a/client/src/gui/build_chat_screen.c +++ b/client/src/gui/build_chat_screen.c @@ -70,7 +70,7 @@ void build_leftbar(GtkWidget *chat_screen) GtkWidget *user_avatar = gtk_drawing_area_new(); gtk_widget_set_size_request(GTK_WIDGET(user_avatar), 27, 27); - g_signal_connect(G_OBJECT(user_avatar), "draw", G_CALLBACK(draw_user_avatar), NULL); // Получить avatar пользовтеля + // g_signal_connect(G_OBJECT(user_avatar), "draw", G_CALLBACK(draw_user_avatar), NULL); // Получить avatar пользовтеля gtk_widget_set_halign(user_avatar, GTK_ALIGN_START); gtk_widget_set_valign(user_avatar, GTK_ALIGN_CENTER); gtk_box_pack_start(GTK_BOX(leftbar_footer), user_avatar, FALSE, FALSE, 0); diff --git a/client/src/gui/build_rightbar_chat.c b/client/src/gui/build_rightbar_chat.c index 66d8b31..6b70c5c 100644 --- a/client/src/gui/build_rightbar_chat.c +++ b/client/src/gui/build_rightbar_chat.c @@ -35,8 +35,6 @@ void send_button_click(GtkWidget *widget, gpointer new_message_field) { gtk_entry_set_text(new_message_field, ""); - printf("%s\n", new_message); - if (handle_send_msg_request(new_message) == R_SUCCESS) { add_message(mx_get_last_msg_node(utils->current_chat->messages)); diff --git a/client/src/gui/update_chat_field.c b/client/src/gui/update_chat_field.c index 4ca4185..588243e 100644 --- a/client/src/gui/update_chat_field.c +++ b/client/src/gui/update_chat_field.c @@ -14,7 +14,7 @@ void add_message(t_msg *message) { GtkWidget *avatar = gtk_drawing_area_new(); gtk_widget_set_size_request(GTK_WIDGET(avatar), 27, 27); - g_signal_connect(G_OBJECT(avatar), "draw", G_CALLBACK(draw_message_avatar), message); // Получить avatar пользовтеля + // g_signal_connect(G_OBJECT(avatar), "draw", G_CALLBACK(draw_message_avatar), message); // Получить avatar пользовтеля gtk_widget_set_halign(avatar, cur_user ? GTK_ALIGN_START : GTK_ALIGN_END); gtk_widget_set_valign(avatar, GTK_ALIGN_START); if (!cur_user) { diff --git a/client/src/request_handlers/get_chat_msgs.c b/client/src/request_handlers/get_chat_msgs.c index 38bd125..a4e1fb6 100644 --- a/client/src/request_handlers/get_chat_msgs.c +++ b/client/src/request_handlers/get_chat_msgs.c @@ -20,10 +20,10 @@ t_response_code add_msg_to_msglist(cJSON* json) { t_msg* new_msg = mx_create_msg(msg_id->valueint, sender_id->valueint, sender_name->valuestring, chat_id->valueint, text->valuestring, get_string_time(date->valueint)); - if (new_msg->sender_id != utils->current_user->user_id) - handle_get_user_image(new_msg->sender_id, &new_msg->avatar_path); - else - new_msg->avatar_path = mx_strdup(utils->current_user->avatar_path); + // if (new_msg->sender_id != utils->current_user->user_id) + // handle_get_user_image(new_msg->sender_id, &new_msg->avatar_path); + // else + // new_msg->avatar_path = mx_strdup(utils->current_user->avatar_path); mx_msg_push_back(&chat_by_id->messages, new_msg); diff --git a/client/src/request_handlers/get_user_image.c b/client/src/request_handlers/get_user_image.c index 649d0a6..5ef52b5 100644 --- a/client/src/request_handlers/get_user_image.c +++ b/client/src/request_handlers/get_user_image.c @@ -37,7 +37,7 @@ t_response_code handle_get_user_image(int user_id, char** avatar_path) { FILE *fp; char* file_path = get_file_path_for(user_id); if ((fp = fopen(file_path, "wb")) == NULL) { - printf("Cannot open image file\n"); + handle_error("Cannot open image file"); mx_strdel(&file_path); return R_FILE_ERROR; } @@ -46,7 +46,7 @@ t_response_code handle_get_user_image(int user_id, char** avatar_path) { int len_encoded = 0; usleep(500000); if(recv(utils->server_fd, &len_encoded, sizeof(int), 0) == 0) { - printf("Error while receiving length\n"); + handle_error("Error while receiving length"); mx_strdel(&file_path); return R_FILE_ERROR; } @@ -65,13 +65,13 @@ t_response_code handle_get_user_image(int user_id, char** avatar_path) { // Writing decoded image to created file fwrite(decoded, flen, 1, fp); if (ferror(fp)) { - printf("fwrite() failed\n"); + handle_error("fwrite() failed"); mx_strdel(&file_path); return R_FILE_ERROR; } int r; if ((r = fclose(fp)) == EOF) { - printf("Cannot close file handler\n"); + handle_error("Cannot close file handler"); mx_strdel(&file_path); return R_FILE_ERROR; } diff --git a/client/src/request_handlers/login.c b/client/src/request_handlers/login.c index 364ebd7..ada4ea0 100644 --- a/client/src/request_handlers/login.c +++ b/client/src/request_handlers/login.c @@ -5,9 +5,10 @@ void set_current_user(t_user** user, const cJSON* user_json) { const cJSON* id_json = cJSON_GetObjectItem(user_json, "id"); const cJSON* name_json = cJSON_GetObjectItemCaseSensitive(user_json, "username"); const cJSON* pass_json = cJSON_GetObjectItemCaseSensitive(user_json, "password"); + const cJSON* color_json = cJSON_GetObjectItem(user_json, "avatar_color"); if (!cJSON_IsNumber(id_json) || !cJSON_IsString(name_json) || - !cJSON_IsString(pass_json)) { + !cJSON_IsString(pass_json) || !cJSON_IsNumber(color_json)) { return; } @@ -15,6 +16,7 @@ void set_current_user(t_user** user, const cJSON* user_json) { (*user)->user_id = id_json->valueint; (*user)->name = mx_strdup(name_json->valuestring); (*user)->password = mx_strdup(pass_json->valuestring); + (*user)->avatar_color = color_json->valueint; (*user)->avatar_path = NULL; } @@ -64,7 +66,7 @@ t_response_code handle_login_request(const char* user_name, const char* user_pas if (error_code == R_SUCCESS) { - handle_get_user_image(utils->current_user->user_id, &utils->current_user->avatar_path); + // handle_get_user_image(utils->current_user->user_id, &utils->current_user->avatar_path); handle_get_chats_request(); utils->log_name = get_log_name(); diff --git a/client/src/request_handlers/send_message.c b/client/src/request_handlers/send_message.c index 62ae826..7a1a89c 100644 --- a/client/src/request_handlers/send_message.c +++ b/client/src/request_handlers/send_message.c @@ -7,7 +7,7 @@ t_response_code add_to_global_msglist(t_msg* new_msg) { return R_CHAT_NOENT; } - new_msg->avatar_path = mx_strdup(utils->current_user->avatar_path); + // new_msg->avatar_path = mx_strdup(utils->current_user->avatar_path); mx_msg_push_back(&curr_chat->messages, new_msg); curr_chat->last_new_msg = mx_get_last_msg_node(curr_chat->messages); update_chatlist_item_info(curr_chat); diff --git a/client/src/request_handlers/server_updates.c b/client/src/request_handlers/server_updates.c index 4954f21..814d3fd 100644 --- a/client/src/request_handlers/server_updates.c +++ b/client/src/request_handlers/server_updates.c @@ -13,7 +13,7 @@ void set_messages_as_read_for(t_chat* chat) { new_msg->chat_id, new_msg->text, new_msg->date_str); t_msg* msg_to_add = mx_get_last_msg_node(chat->messages); - msg_to_add->avatar_path = mx_strdup(new_msg->avatar_path); + // msg_to_add->avatar_path = mx_strdup(new_msg->avatar_path); if (chat->new_msg_count >= 1) chat->new_msg_count -= 1; @@ -49,7 +49,7 @@ static int handle_new_message(t_chat* curr_chat, int message_id, bool is_current client_log("You're reading an incoming message", INFO_LOG); if (!curr_chat->new_messages) { - handle_get_user_image(new_msg->sender_id, &new_msg->avatar_path); + // handle_get_user_image(new_msg->sender_id, &new_msg->avatar_path); mx_msg_push_back(&curr_chat->messages, new_msg); curr_chat->last_new_msg = mx_get_last_msg_node(curr_chat->messages); @@ -71,7 +71,7 @@ static int handle_new_message(t_chat* curr_chat, int message_id, bool is_current } else if (message_id > last_new_msg_id) { client_log("You have an incoming message", INFO_LOG); - handle_get_user_image(new_msg->sender_id, &new_msg->avatar_path); + // handle_get_user_image(new_msg->sender_id, &new_msg->avatar_path); mx_msg_push_back(&curr_chat->new_messages, new_msg); curr_chat->last_new_msg = mx_get_last_msg_node(curr_chat->new_messages); curr_chat->new_msg_count += 1; diff --git a/client/src/request_handlers/signup.c b/client/src/request_handlers/signup.c index ecdf8b0..d7e2795 100644 --- a/client/src/request_handlers/signup.c +++ b/client/src/request_handlers/signup.c @@ -16,6 +16,7 @@ t_response_code handle_signup_request(const char* user_name, const char* user_pa cJSON *json = cJSON_CreateObject(); cJSON_AddStringToObject(json, "name", user_name); cJSON_AddStringToObject(json, "password", user_password); + cJSON_AddNumberToObject(json, "avatar_color", get_avatar_color()); cJSON_AddNumberToObject(json, "type", REQ_USR_SIGNUP); char* json_str = cJSON_PrintUnformatted(json); cJSON_Delete(json); diff --git a/client/src/request_handlers/update_user_image.c b/client/src/request_handlers/update_user_image.c index a1f6d8d..fed4001 100644 --- a/client/src/request_handlers/update_user_image.c +++ b/client/src/request_handlers/update_user_image.c @@ -26,7 +26,7 @@ t_response_code handle_update_user_image(char *path) { // Open image to read FILE *fp; if((fp = fopen(path, "rb")) == NULL) { - printf("Cannot open file\n"); + handle_error("Cannot open file"); return R_FILE_ERROR; } int r; @@ -34,28 +34,28 @@ t_response_code handle_update_user_image(char *path) { // Get the length of the file data fseek(fp, 0, SEEK_END); if (ferror(fp)) { - printf("fseek() failed\n"); + handle_error("fseek() failed"); if ((r = fclose(fp)) == EOF) { - printf("Cannot close file\n"); + handle_error("Cannot close file"); } return R_FILE_ERROR; } long flen = ftell(fp); if (flen == -1) { - printf("ftell() failed\n"); + handle_error("ftell() failed"); if ((r = fclose(fp)) == EOF) { - printf("Cannot close file\n"); + handle_error("Cannot close file"); } return R_FILE_ERROR; } fseek(fp, 0, SEEK_SET); if (ferror(fp)) { - printf("fseek() failed\n"); + handle_error("fseek() failed"); r = fclose(fp); if (r == EOF) { - printf("Cannot close file\n"); + handle_error("Cannot close file"); } return R_FILE_ERROR; } @@ -64,9 +64,9 @@ t_response_code handle_update_user_image(char *path) { unsigned char *read_data = malloc((unsigned)flen + 1); fread(read_data, flen, 1, fp); if (ferror(fp)) { - printf("fread() failed\n"); + handle_error("fread() failed"); if ((r = fclose(fp)) == EOF) { - printf("Cannot close file\n"); + handle_error("Cannot close file"); } return R_FILE_ERROR; } @@ -80,8 +80,8 @@ t_response_code handle_update_user_image(char *path) { free(read_data); int len_encoded = strlen((char *)out_b64); - // printf("%d\n", len_encoded); - // printf("%s\n", out_b64); + // handle_error("%d\n", len_encoded); + // handle_error("%s\n", out_b64); // Send all to server send(utils->server_fd, &len_encoded, sizeof(int), 0); send_image_to_server(&utils->server_fd, out_b64, len_encoded); @@ -89,7 +89,7 @@ t_response_code handle_update_user_image(char *path) { free(out_b64); if ((r = fclose(fp)) == EOF) { - printf("Cannot close file\n"); + handle_error("Cannot close file"); return R_FILE_ERROR; } utils->is_suspended = false; diff --git a/server/inc/types.h b/server/inc/types.h index 08ddf8c..e6ae3f4 100644 --- a/server/inc/types.h +++ b/server/inc/types.h @@ -8,6 +8,7 @@ typedef struct s_user { int user_id; char* name; char* password; + t_avatar_color avatar_color; struct s_user* next; } t_user; diff --git a/server/src/api/handle_search_chats.c b/server/src/api/handle_search_chats.c index 415ae73..2cac737 100644 --- a/server/src/api/handle_search_chats.c +++ b/server/src/api/handle_search_chats.c @@ -15,7 +15,7 @@ cJSON* get_search_chats_array(const cJSON* chat_info, t_server_utils* utils) { mx_strcat(search_str, "%%"); mx_strcat(search_str, search_pattern->valuestring); mx_strcat(search_str, "%%"); - sqlite3_prepare_v2(db, "SELECT chats.id, chats.name FROM chats " + sqlite3_prepare_v2(db, "SELECT chats.id, chats.name, chats.avatar_color FROM chats " "WHERE chats.name LIKE ? " "AND chats.id NOT IN (SELECT `chat_id` FROM `members` WHERE `user_id` = ?) " "ORDER BY chats.date DESC ", diff --git a/server/src/api/handle_usr_login.c b/server/src/api/handle_usr_login.c index 7dabf51..257fba7 100644 --- a/server/src/api/handle_usr_login.c +++ b/server/src/api/handle_usr_login.c @@ -7,6 +7,7 @@ char* get_json_formatted_user(const t_user* user) { cJSON_AddNumberToObject(json, "id", user->user_id); cJSON_AddStringToObject(json, "username", user->name); cJSON_AddStringToObject(json, "password", user->password); + cJSON_AddNumberToObject(json, "avatar_color", user->avatar_color); cJSON_AddNumberToObject(json, "error_code", R_SUCCESS); char* user_info = cJSON_PrintUnformatted(json); cJSON_Delete(json); @@ -21,6 +22,7 @@ void set_user_account_data(sqlite3_stmt* stmt, t_server_utils* utils) { utils->user = mx_create_user(sqlite3_column_int64(stmt, 0), utils->client_socket, utils->ssl); utils->user->name = mx_strdup((const char*)sqlite3_column_text(stmt, 1)); utils->user->password = mx_strdup((const char*)sqlite3_column_text(stmt, 2)); + utils->user->avatar_color = sqlite3_column_int64(stmt, 3); } sqlite3_finalize(stmt); @@ -31,7 +33,8 @@ t_response_code set_user_by_username(const char* username, const char* password, sqlite3* db = open_database(); sqlite3_stmt* stmt; - sqlite3_prepare_v2(db, "SELECT * FROM `users` WHERE `username` = ?", -1, &stmt, NULL); + sqlite3_prepare_v2(db, "SELECT users.id, users.username, users.password, users.avatar_color " + "FROM `users` WHERE `username` = ?", -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, username, -1, NULL); set_user_account_data(stmt, utils); @@ -50,9 +53,10 @@ t_response_code set_user_by_username(const char* username, const char* password, free(response); char result_to_log[QUERY_LEN]; - sprintf(result_to_log, "Logged in user info: id: %d, name: %s", + sprintf(result_to_log, "Logged in user info: id: %d, name: %s, color: %d", utils->user->user_id, - utils->user->name); + utils->user->name, + utils->user->avatar_color); logger(result_to_log, INFO_LOG); return R_SUCCESS; diff --git a/server/src/api/handle_usr_signup.c b/server/src/api/handle_usr_signup.c index d739e9d..929de67 100644 --- a/server/src/api/handle_usr_signup.c +++ b/server/src/api/handle_usr_signup.c @@ -4,8 +4,9 @@ t_response_code db_add_user(const cJSON* user_info) { const cJSON *user_name = cJSON_GetObjectItemCaseSensitive(user_info, "name"); const cJSON *user_password = cJSON_GetObjectItemCaseSensitive(user_info, "password"); + const cJSON *avatar_color = cJSON_GetObjectItemCaseSensitive(user_info, "avatar_color"); - if (!cJSON_IsString(user_name) || !cJSON_IsString(user_password)) { + if (!cJSON_IsString(user_name) || !cJSON_IsString(user_password) || !cJSON_IsNumber(avatar_color)) { return R_JSON_FAILURE; } @@ -25,8 +26,8 @@ t_response_code db_add_user(const cJSON* user_info) { } char query[QUERY_LEN]; - sprintf(query, "INSERT INTO `users` (`username`, `password`) VALUES('%s', '%s')", - user_name->valuestring, user_password->valuestring); + sprintf(query, "INSERT INTO `users` (`username`, `password`, `avatar_color`) VALUES('%s', '%s', '%d')", + user_name->valuestring, user_password->valuestring, avatar_color->valueint); if (db_execute_query(query) != 0) { return R_DB_FAILURE; @@ -47,8 +48,8 @@ void handle_usr_signup(const cJSON* user_info, t_server_utils* utils) { send_server_response(utils->ssl, error_code, REQ_USR_SIGNUP); return; } - const cJSON *user_name = cJSON_GetObjectItemCaseSensitive(user_info, "name"); - handle_set_default_user_image("server/data/default_image.png", db_get_id_by_username(user_name->valuestring)); + // const cJSON *user_name = cJSON_GetObjectItemCaseSensitive(user_info, "name"); + // handle_set_default_user_image("server/data/default_image.png", db_get_id_by_username(user_name->valuestring)); send_server_response(utils->ssl, R_SUCCESS, REQ_USR_SIGNUP); diff --git a/server/src/database_utils.c b/server/src/database_utils.c index ffa173f..d976200 100644 --- a/server/src/database_utils.c +++ b/server/src/database_utils.c @@ -26,13 +26,14 @@ int database_init() { "`id` INTEGER PRIMARY KEY AUTOINCREMENT," "`username` VARCHAR(32) NOT NULL," "`password` VARCHAR(32) NOT NULL," + "`avatar_color` TINYINT NOT NULL," "`image` BLOB);" "CREATE TABLE `chats` (" "`id` INTEGER PRIMARY KEY AUTOINCREMENT," "`name` VARCHAR(32) NOT NULL," "`date` INTEGER NOT NULL," - "`avatar_color` TINYINT NOT NULL);" + "`avatar_color` TINYINT NOT NULL);" "CREATE TABLE `members` (" "`id` INTEGER PRIMARY KEY AUTOINCREMENT,"