void on_buy_ticket_table_row_activated( GtkTreeView *treeview,/*{{{*/ GtkTreePath *path, GtkTreeViewColumn *col, gpointer userdata) { GtkTreeIter iter; GtkTreeModel *model = gtk_tree_view_get_model(treeview); gchar *id; if(gtk_tree_model_get_iter(model, &iter, path)) { gtk_tree_model_get(model, &iter, 0, &id, -1); GtkMessageDialog *dialog; if(refund(id, conn) == 0) { update_buy_table(); update_search_result(); dialog = GTK_MESSAGE_DIALOG(gtk_message_dialog_new( login_win,GTK_DIALOG_MODAL,GTK_MESSAGE_INFO,GTK_BUTTONS_CLOSE,"Refund successfully" )); } else { dialog = GTK_MESSAGE_DIALOG(gtk_message_dialog_new( login_win,GTK_DIALOG_MODAL,GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE,"Refund Failed" )); } gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(GTK_WIDGET(dialog)); g_free(id); } }/*}}}*/
state_t * ship_fail() { printf("Failed to ship parts...\n"); refund(); updateStats(LOST); return &accepting; }
void handle_client(conn_t *conn) { conn->user = NULL; for(; ;) { request_t *rqst = parse_args(conn->input); if(rqst != NULL) { if(IS_PROTOCOL(rqst, P_LOGIN)) { if(login(rqst->argv[1], rqst->argv[2]) == 0) { conn->user = (t_user*) malloc(sizeof(t_user)); memcpy(conn->user, find_user_by_id(rqst->argv[1]), sizeof(t_user)); simple_response(0, conn); } else { simple_response(1, conn); } } else if(IS_PROTOCOL(rqst, P_SIGNUP)) { if(signup(rqst->argv[1], rqst->argv[2]) == 0 ) { simple_response(0, conn); } else { simple_response(1, conn); } } else if(IS_PROTOCOL(rqst, P_EXIT)) { simple_response(0, conn); return; } else if(conn->user != NULL) { //authorized user if(IS_PROTOCOL(rqst, P_BUY)) { if(buy(conn->user->id, rqst->argv[1]) == 0) { simple_response(0, conn); } else { simple_response(1, conn); } } else if(IS_PROTOCOL(rqst, P_QUERY)) { t_ticket_list *list = query(rqst->argv[1], rqst->argv[2]); write(conn->dfd, "*", 1); char *tmp = ltoa(list->num); int tmplen = strlen(tmp); write(conn->dfd, tmp, tmplen); write(conn->dfd, CRLF, CLLEN); free(tmp); tmp = ltoa(sizeof(t_ticket)); tmplen = strlen(tmp); int i; for (i = 0; i < list->num; ++i) { write(conn->dfd, "?", 1); write(conn->dfd, tmp, tmplen); write(conn->dfd, CRLF, CLLEN); write(conn->dfd, &list->data[i], sizeof(t_ticket)); } free(tmp); } else if(IS_PROTOCOL(rqst, P_USER_INFO)) { char *tmp = ltoa(sizeof(t_user)); int tmplen = strlen(tmp); write(conn->dfd, "?", 1); write(conn->dfd, tmp, tmplen); write(conn->dfd, CRLF, CLLEN); write(conn->dfd, conn->user, sizeof(t_user)); } else if(IS_PROTOCOL(rqst, P_USER_UPD)) { if(update_user_info(conn->user->id, rqst->argv[1], rqst->argv[2], rqst->argv[3]) == 0) { memcpy(conn->user, find_user_by_id(conn->user->id), sizeof(t_user)); simple_response(0, conn); } else { simple_response(1, conn); } } else if(IS_PROTOCOL(rqst, P_QUERY_BUY)) { t_ticket_list *list = query_buy(conn->user->id); write(conn->dfd, "*", 1); char *tmp = ltoa(list->num); int tmplen = strlen(tmp); write(conn->dfd, tmp, tmplen); write(conn->dfd, CRLF, CLLEN); free(tmp); tmp = ltoa(sizeof(t_ticket)); tmplen = strlen(tmp); int i; for (i = 0; i < list->num; ++i) { write(conn->dfd, "?", 1); write(conn->dfd, tmp, tmplen); write(conn->dfd, CRLF, CLLEN); write(conn->dfd, &list->data[i], sizeof(t_ticket)); } free(tmp); } else if(IS_PROTOCOL(rqst, P_REFUND)) { if(refund(conn->user->id, rqst->argv[1]) == 0) { simple_response(0, conn); } else { simple_response(1, conn); } } else if(IS_PROTOCOL(rqst, P_TICKET) && conn->user->type == 1) { t_ticket *tkt = load_ticket(rqst->argv[1]); if(tkt == NULL) { simple_response(1, conn); } else { char *tmp = ltoa(sizeof(t_ticket)); write(conn->dfd, "?", 1); write(conn->dfd, tmp, strlen(tmp));write(conn->dfd, CRLF, CLLEN); write(conn->dfd, tkt, sizeof(t_ticket)); free(tmp); } } else if(IS_PROTOCOL(rqst, P_TKT_UPDATE) && conn->user->type == 1) { if(add_update_ticket(rqst->argv[1], rqst->argv[2], rqst->argv[3], rqst->argv[4], rqst->argv[5], rqst->argv[6], rqst->argv[7], rqst->argv[8], rqst->argv[9] ) == 0) { simple_response(0, conn); } else { simple_response(1, conn); } } else if(IS_PROTOCOL(rqst, P_STN_ADD) && conn->user->type == 1) { if(add_station(rqst->argv[1]) == 0) { simple_response(0, conn); } else { simple_response(1, conn); } } else if(IS_PROTOCOL(rqst, P_STN_DEL) && conn->user->type == 1) { if(del_station(rqst->argv[1]) == 0) { simple_response(0, conn); } else { simple_response(1, conn); } } else if(IS_PROTOCOL(rqst, P_STN_ALL) && conn->user->type == 1) { t_station_list *list = all_station(); write(conn->dfd, "*", 1); char *tmp = ltoa(list->num); int tmplen = strlen(tmp); write(conn->dfd, tmp, tmplen); write(conn->dfd, CRLF, CLLEN); free(tmp); tmp = ltoa(sizeof(t_station)); tmplen = strlen(tmp); int i; for (i = 0; i < list->num; ++i) { write(conn->dfd, "?", 1); write(conn->dfd, tmp, tmplen); write(conn->dfd, CRLF, CLLEN); write(conn->dfd, &list->data[i], sizeof(t_station)); } free(tmp); } } else { simple_response(1, conn); } } } }