Skip to content

Commit

Permalink
avatar colors for users
Browse files Browse the repository at this point in the history
  • Loading branch information
High-Voltaged committed Feb 6, 2022
1 parent 23f8686 commit 55f9f7b
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 40 deletions.
1 change: 1 addition & 0 deletions client/inc/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion client/src/gui/build_chat_screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions client/src/gui/build_rightbar_chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion client/src/gui/update_chat_field.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions client/src/request_handlers/get_chat_msgs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions client/src/request_handlers/get_user_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions client/src/request_handlers/login.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ 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;
}

*user = malloc(sizeof(t_user));
(*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;

}
Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion client/src/request_handlers/send_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions client/src/request_handlers/server_updates.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions client/src/request_handlers/signup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 12 additions & 12 deletions client/src/request_handlers/update_user_image.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,36 @@ 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;

// 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;
}
Expand All @@ -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;
}
Expand All @@ -80,16 +80,16 @@ 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);

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;
Expand Down
1 change: 1 addition & 0 deletions server/inc/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion server/src/api/handle_search_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ",
Expand Down
10 changes: 7 additions & 3 deletions server/src/api/handle_usr_login.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions server/src/api/handle_usr_signup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion server/src/database_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,"
Expand Down

0 comments on commit 55f9f7b

Please sign in to comment.