1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 15:03:27 +01:00

convert bytes for file transfer message, general fixes

This commit is contained in:
Jfreegman 2014-08-07 19:24:56 -04:00
parent 396d08f0d2
commit bb85f31bb2
9 changed files with 121 additions and 110 deletions

View File

@ -222,9 +222,9 @@ static void chat_onAction(ToxWindow *self, Tox *m, int32_t num, const char *acti
write_to_log(action, nick, ctx->log, true);
if (self->active_box != -1)
box_notify2(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS, self->active_box, "* %s %s", nick, action );
box_notify2(self, generic_message, NT_WNDALERT_1 | NT_NOFOCUS, self->active_box, "* %s %s", nick, action );
else
box_notify(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS, &self->active_box, self->name, "* %s %s", nick, action );
box_notify(self, generic_message, NT_WNDALERT_1 | NT_NOFOCUS, &self->active_box, self->name, "* %s %s", nick, action );
}
static void chat_onNickChange(ToxWindow *self, Tox *m, int32_t num, const char *nick, uint16_t len)
@ -280,9 +280,11 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t
/* holds the lone filename */
char filename_nopath[MAX_STR_SIZE];
get_file_name(filename_nopath, sizeof(filename_nopath), pathname);
char sizestr[32];
bytes_convert_str(sizestr, sizeof(sizestr), filesize);
int len = strlen(filename_nopath);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer request for '%s' (%llu bytes).",
filename_nopath, (long long unsigned int) filesize);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer request for '%s' (%s)",
filename_nopath, sizestr);
if (filenum >= MAX_FILES) {
errmsg = "Too many pending file requests; discarding.";
@ -341,10 +343,10 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t
strcpy(Friends.list[num].file_receiver[filenum].filename, filename);
if (self->active_box != -1)
box_notify2(self, transfer_pending, NT_WNDALERT_2 | NT_NOFOCUS, self->active_box,
box_notify2(self, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS, self->active_box,
"Incoming file: %s", filename );
else
box_notify(self, transfer_pending, NT_WNDALERT_2 | NT_NOFOCUS, &self->active_box, self->name,
box_notify(self, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS, &self->active_box, self->name,
"Incoming file: %s", filename );
}

View File

@ -250,7 +250,11 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
file_senders[i].size = filesize;
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
tox_file_data_size(m, self->num), file_to_send);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Sending file [%d]: '%s'", filenum, filename);
char sizestr[32];
bytes_convert_str(sizestr, sizeof(sizestr), filesize);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0,
"Sending file [%d]: '%s' (%s)", filenum, filename, sizestr);
++num_active_file_senders;

View File

@ -38,10 +38,6 @@ uint8_t max_file_senders_index;
uint8_t num_active_file_senders;
extern _Friends Friends;
#define KiB 1024
#define MiB 1048576 /* 1024 ^ 2 */
#define GiB 1073741824 /* 1024 ^ 3 */
/* creates initial progress line that will be updated during file transfer.
Assumes progline is of size MAX_STR_SIZE */
void prep_prog_line(char *progline)
@ -70,23 +66,10 @@ void print_progress_bar(ToxWindow *self, int idx, int friendnum, double pct_done
line_id = Friends.list[friendnum].file_receiver[idx].line_id;
}
const char *unit;
if (bps < KiB) {
unit = "B/s";
} else if (bps < MiB) {
unit = "KiB/s";
bps /= (double) KiB;
} else if (bps < GiB) {
unit = "MiB/s";
bps /= (double) MiB;
} else {
unit = "GiB/s";
bps /= (double) GiB;
}
char msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "%.1f %s [", bps, unit);
bytes_convert_str(msg, sizeof(msg), bps);
strcat(msg, "/s [");
int n = pct_done / (100 / NUM_PROG_MARKS);
int i, j;

View File

@ -26,6 +26,10 @@
#include "toxic.h"
#include "windows.h"
#define KiB 1024
#define MiB 1048576 /* 1024 ^ 2 */
#define GiB 1073741824 /* 1024 ^ 3 */
#define FILE_PIECE_SIZE 2048 /* must be >= (MAX_CRYPTO_DATA_SIZE - 2) in toxcore/net_crypto.h */
#define MAX_FILES 32
#define TIMEOUT_FILESENDER 120

View File

@ -54,14 +54,14 @@ static uint8_t blocklist_view = 0; /* 0 if we're in friendlist view, 1 if we'r
_Friends Friends;
static struct _Blocked_Contacts {
static struct _Blocked {
int num_selected;
int max_idx;
int num_blocked;
int *index;
BlockedFriend *list;
} Blocked_Contacts;
} Blocked;
static struct _pendingDel {
int num;
@ -92,21 +92,21 @@ static void realloc_friends(int n)
static void realloc_blocklist(int n)
{
if (n <= 0) {
free(Blocked_Contacts.list);
free(Blocked_Contacts.index);
Blocked_Contacts.list = NULL;
Blocked_Contacts.index = NULL;
free(Blocked.list);
free(Blocked.index);
Blocked.list = NULL;
Blocked.index = NULL;
return;
}
BlockedFriend *b = realloc(Blocked_Contacts.list, n * sizeof(BlockedFriend));
int *b_idx = realloc(Blocked_Contacts.index, n * sizeof(int));
BlockedFriend *b = realloc(Blocked.list, n * sizeof(BlockedFriend));
int *b_idx = realloc(Blocked.index, n * sizeof(int));
if (b == NULL || b_idx == NULL)
exit_toxic_err("failed in realloc_blocklist", FATALERR_MEMORY);
Blocked_Contacts.list = b;
Blocked_Contacts.index = b_idx;
Blocked.list = b;
Blocked.index = b_idx;
}
void kill_friendlist(void)
@ -123,7 +123,7 @@ static int save_blocklist(char *path)
if (path == NULL)
return -1;
int len = sizeof(BlockedFriend) * Blocked_Contacts.num_blocked;
int len = sizeof(BlockedFriend) * Blocked.num_blocked;
char *data = malloc(len);
if (data == NULL)
@ -132,19 +132,19 @@ static int save_blocklist(char *path)
int i;
int count = 0;
for (i = 0; i < Blocked_Contacts.max_idx; ++i) {
if (count > Blocked_Contacts.num_blocked)
for (i = 0; i < Blocked.max_idx; ++i) {
if (count > Blocked.num_blocked)
return -1;
if (Blocked_Contacts.list[i].active) {
if (Blocked.list[i].active) {
BlockedFriend tmp;
memset(&tmp, 0, sizeof(BlockedFriend));
tmp.namelength = htons(Blocked_Contacts.list[i].namelength);
memcpy(tmp.name, Blocked_Contacts.list[i].name, Blocked_Contacts.list[i].namelength + 1);
memcpy(tmp.pub_key, Blocked_Contacts.list[i].pub_key, TOX_CLIENT_ID_SIZE);
tmp.namelength = htons(Blocked.list[i].namelength);
memcpy(tmp.name, Blocked.list[i].name, Blocked.list[i].namelength + 1);
memcpy(tmp.pub_key, Blocked.list[i].pub_key, TOX_CLIENT_ID_SIZE);
uint8_t lastonline[sizeof(uint64_t)];
memcpy(lastonline, &Blocked_Contacts.list[i].last_on, sizeof(uint64_t));
memcpy(lastonline, &Blocked.list[i].last_on, sizeof(uint64_t));
host_to_net(lastonline, sizeof(uint64_t));
memcpy(&tmp.last_on, lastonline, sizeof(uint64_t));
@ -206,28 +206,28 @@ int load_blocklist(char *path)
}
int num = len / sizeof(BlockedFriend);
Blocked_Contacts.max_idx = num;
Blocked.max_idx = num;
realloc_blocklist(num);
int i;
for (i = 0; i < num; ++i) {
memset(&Blocked_Contacts.list[i], 0, sizeof(BlockedFriend));
memset(&Blocked.list[i], 0, sizeof(BlockedFriend));
BlockedFriend tmp;
memcpy(&tmp, data + i * sizeof(BlockedFriend), sizeof(BlockedFriend));
Blocked_Contacts.list[i].active = true;
Blocked_Contacts.list[i].num = i;
Blocked_Contacts.list[i].namelength = ntohs(tmp.namelength);
memcpy(Blocked_Contacts.list[i].name, tmp.name, Blocked_Contacts.list[i].namelength + 1);
memcpy(Blocked_Contacts.list[i].pub_key, tmp.pub_key, TOX_CLIENT_ID_SIZE);
Blocked.list[i].active = true;
Blocked.list[i].num = i;
Blocked.list[i].namelength = ntohs(tmp.namelength);
memcpy(Blocked.list[i].name, tmp.name, Blocked.list[i].namelength + 1);
memcpy(Blocked.list[i].pub_key, tmp.pub_key, TOX_CLIENT_ID_SIZE);
uint8_t lastonline[sizeof(uint64_t)];
memcpy(lastonline, &tmp.last_on, sizeof(uint64_t));
net_to_host(lastonline, sizeof(uint64_t));
memcpy(&Blocked_Contacts.list[i].last_on, lastonline, sizeof(uint64_t));
memcpy(&Blocked.list[i].last_on, lastonline, sizeof(uint64_t));
++Blocked_Contacts.num_blocked;
++Blocked.num_blocked;
}
free(data);
@ -265,7 +265,7 @@ void sort_friendlist_index(void)
static int index_name_cmp_block(const void *n1, const void *n2)
{
return qsort_strcasecmp_hlpr(Blocked_Contacts.list[*(int *) n1].name, Blocked_Contacts.list[*(int *) n2].name);
return qsort_strcasecmp_hlpr(Blocked.list[*(int *) n1].name, Blocked.list[*(int *) n2].name);
}
static void sort_blocklist_index(void)
@ -273,12 +273,12 @@ static void sort_blocklist_index(void)
int i;
int n = 0;
for (i = 0; i < Blocked_Contacts.max_idx; ++i) {
if (Blocked_Contacts.list[i].active)
Blocked_Contacts.index[n++] = Blocked_Contacts.list[i].num;
for (i = 0; i < Blocked.max_idx; ++i) {
if (Blocked.list[i].active)
Blocked.index[n++] = Blocked.list[i].num;
}
qsort(Blocked_Contacts.index, Blocked_Contacts.num_blocked, sizeof(int), index_name_cmp_block);
qsort(Blocked.index, Blocked.num_blocked, sizeof(int), index_name_cmp_block);
}
static void update_friend_last_online(int32_t num, uint64_t timestamp)
@ -422,10 +422,10 @@ static void friendlist_add_blocked(Tox *m, int32_t fnum, int32_t bnum)
Friends.list[i].chatwin = -1;
Friends.list[i].status = TOX_USERSTATUS_NONE;
Friends.list[i].logging_on = (bool) user_settings_->autolog == AUTOLOG_ON;
Friends.list[i].namelength = Blocked_Contacts.list[bnum].namelength;
update_friend_last_online(i, Blocked_Contacts.list[bnum].last_on);
memcpy(Friends.list[i].name, Blocked_Contacts.list[bnum].name, Friends.list[i].namelength + 1);
memcpy(Friends.list[i].pub_key, Blocked_Contacts.list[bnum].pub_key, TOX_CLIENT_ID_SIZE);
Friends.list[i].namelength = Blocked.list[bnum].namelength;
update_friend_last_online(i, Blocked.list[bnum].last_on);
memcpy(Friends.list[i].name, Blocked.list[bnum].name, Friends.list[i].namelength + 1);
memcpy(Friends.list[i].pub_key, Blocked.list[bnum].pub_key, TOX_CLIENT_ID_SIZE);
if (i == Friends.max_idx)
++Friends.max_idx;
@ -468,14 +468,6 @@ static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int32_t num, const
if (Friends.list[num].chatwin == -1) {
if (get_num_active_windows() < MAX_WINDOWS_NUM) {
Friends.list[num].chatwin = add_window(m, new_chat(m, Friends.list[num].num));
if (self->active_box != -1)
box_notify2(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS, self->active_box,
"You are invited to join group" );
else
box_notify(self, generic_message, NT_WNDALERT_0 | NT_NOFOCUS, &self->active_box, self->name,
"You are invited to join group" );
} else {
char nick[TOX_MAX_NAME_LENGTH];
get_nick_truncate(m, nick, num);
@ -577,7 +569,7 @@ static void draw_del_popup(void)
if (blocklist_view == 0)
wprintw(pendingdelete.popup, "%s", Friends.list[pendingdelete.num].name);
else
wprintw(pendingdelete.popup, "%s", Blocked_Contacts.list[pendingdelete.num].name);
wprintw(pendingdelete.popup, "%s", Blocked.list[pendingdelete.num].name);
wattroff(pendingdelete.popup, A_BOLD);
wprintw(pendingdelete.popup, "? y/n");
@ -588,22 +580,22 @@ static void draw_del_popup(void)
/* deletes contact from blocked list */
static void delete_blocked_friend(int32_t bnum)
{
memset(&Blocked_Contacts.list[bnum], 0, sizeof(BlockedFriend));
memset(&Blocked.list[bnum], 0, sizeof(BlockedFriend));
int i;
for (i = Blocked_Contacts.max_idx; i > 0; --i) {
if (Blocked_Contacts.list[i - 1].active)
for (i = Blocked.max_idx; i > 0; --i) {
if (Blocked.list[i - 1].active)
break;
}
--Blocked_Contacts.num_blocked;
Blocked_Contacts.max_idx = i;
--Blocked.num_blocked;
Blocked.max_idx = i;
realloc_blocklist(i);
save_blocklist(BLOCK_FILE);
if (Blocked_Contacts.num_blocked && Blocked_Contacts.num_selected == Blocked_Contacts.num_blocked)
--Blocked_Contacts.num_selected;
if (Blocked.num_blocked && Blocked.num_selected == Blocked.num_blocked)
--Blocked.num_selected;
}
/* deletes contact from friendlist and puts in blocklist */
@ -612,26 +604,26 @@ void block_friend(Tox *m, int32_t fnum)
if (Friends.num_friends <= 0)
return;
realloc_blocklist(Blocked_Contacts.max_idx + 1);
memset(&Blocked_Contacts.list[Blocked_Contacts.max_idx], 0, sizeof(BlockedFriend));
realloc_blocklist(Blocked.max_idx + 1);
memset(&Blocked.list[Blocked.max_idx], 0, sizeof(BlockedFriend));
int i;
for (i = 0; i <= Blocked_Contacts.max_idx; ++i) {
if (Blocked_Contacts.list[i].active)
for (i = 0; i <= Blocked.max_idx; ++i) {
if (Blocked.list[i].active)
continue;
Blocked_Contacts.list[i].active = true;
Blocked_Contacts.list[i].num = i;
Blocked_Contacts.list[i].namelength = Friends.list[fnum].namelength;
Blocked_Contacts.list[i].last_on = Friends.list[fnum].last_online.last_on;
memcpy(Blocked_Contacts.list[i].pub_key, Friends.list[fnum].pub_key, TOX_CLIENT_ID_SIZE);
memcpy(Blocked_Contacts.list[i].name, Friends.list[fnum].name, Friends.list[fnum].namelength + 1);
Blocked.list[i].active = true;
Blocked.list[i].num = i;
Blocked.list[i].namelength = Friends.list[fnum].namelength;
Blocked.list[i].last_on = Friends.list[fnum].last_online.last_on;
memcpy(Blocked.list[i].pub_key, Friends.list[fnum].pub_key, TOX_CLIENT_ID_SIZE);
memcpy(Blocked.list[i].name, Friends.list[fnum].name, Friends.list[fnum].namelength + 1);
++Blocked_Contacts.num_blocked;
++Blocked.num_blocked;
if (i == Blocked_Contacts.max_idx)
++Blocked_Contacts.max_idx;
if (i == Blocked.max_idx)
++Blocked.max_idx;
delete_friend(m, fnum);
save_blocklist(BLOCK_FILE);
@ -645,10 +637,10 @@ void block_friend(Tox *m, int32_t fnum)
/* removes friend from blocklist, puts back in friendlist */
static void unblock_friend(Tox *m, int32_t bnum)
{
if (Blocked_Contacts.num_blocked <= 0)
if (Blocked.num_blocked <= 0)
return;
int32_t friendnum = tox_add_friend_norequest(m, (uint8_t *) Blocked_Contacts.list[bnum].pub_key);
int32_t friendnum = tox_add_friend_norequest(m, (uint8_t *) Blocked.list[bnum].pub_key);
if (friendnum == -1) {
line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to unblock friend");
@ -677,13 +669,13 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
if (!blocklist_view && !Friends.num_friends && (key != KEY_RIGHT && key != KEY_LEFT))
return;
if (blocklist_view && !Blocked_Contacts.num_blocked && (key != KEY_RIGHT && key != KEY_LEFT))
if (blocklist_view && !Blocked.num_blocked && (key != KEY_RIGHT && key != KEY_LEFT))
return;
int f = 0;
if (blocklist_view == 1 && Blocked_Contacts.num_blocked)
f = Blocked_Contacts.index[Blocked_Contacts.num_selected];
if (blocklist_view == 1 && Blocked.num_blocked)
f = Blocked.index[Blocked.num_selected];
else if (Friends.num_friends)
f = Friends.index[Friends.num_selected];
@ -737,7 +729,7 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
if (blocklist_view == 0)
select_friend(self, key, &Friends.num_selected, Friends.num_friends);
else
select_friend(self, key, &Blocked_Contacts.num_selected, Blocked_Contacts.num_blocked);
select_friend(self, key, &Blocked.num_selected, Blocked.num_blocked);
break;
}
}
@ -749,7 +741,7 @@ static void blocklist_onDraw(ToxWindow *self, Tox *m, int y2, int x2)
wattron(self->window, A_BOLD);
wprintw(self->window, " Blocked: ");
wattroff(self->window, A_BOLD);
wprintw(self->window, "%d\n\n", Blocked_Contacts.num_blocked);
wprintw(self->window, "%d\n\n", Blocked.num_blocked);
if ((y2 - FLIST_OFST) <= 0)
return;
@ -757,17 +749,17 @@ static void blocklist_onDraw(ToxWindow *self, Tox *m, int y2, int x2)
int selected_num = 0;
/* Determine which portion of friendlist to draw based on current position */
int page = Blocked_Contacts.num_selected / (y2 - FLIST_OFST);
int page = Blocked.num_selected / (y2 - FLIST_OFST);
int start = (y2 - FLIST_OFST) * page;
int end = y2 - FLIST_OFST + start;
int i;
for (i = start; i < Blocked_Contacts.num_blocked && i < end; ++i) {
int f = Blocked_Contacts.index[i];
for (i = start; i < Blocked.num_blocked && i < end; ++i) {
int f = Blocked.index[i];
bool f_selected = false;
if (i == Blocked_Contacts.num_selected) {
if (i == Blocked.num_selected) {
wattron(self->window, A_BOLD);
wprintw(self->window, " > ");
wattroff(self->window, A_BOLD);
@ -785,7 +777,7 @@ static void blocklist_onDraw(ToxWindow *self, Tox *m, int y2, int x2)
wattron(self->window, COLOR_PAIR(BLUE));
wattron(self->window, A_BOLD);
wprintw(self->window, " %s\n", Blocked_Contacts.list[f].name);
wprintw(self->window, " %s\n", Blocked.list[f].name);
wattroff(self->window, A_BOLD);
if (f_selected)
@ -795,7 +787,7 @@ static void blocklist_onDraw(ToxWindow *self, Tox *m, int y2, int x2)
wprintw(self->window, "\n");
self->x = x2;
if (Blocked_Contacts.num_blocked) {
if (Blocked.num_blocked) {
wmove(self->window, y2 - 1, 1);
wattron(self->window, A_BOLD);
@ -805,7 +797,7 @@ static void blocklist_onDraw(ToxWindow *self, Tox *m, int y2, int x2)
int i;
for (i = 0; i < TOX_CLIENT_ID_SIZE; ++i)
wprintw(self->window, "%02X", Blocked_Contacts.list[selected_num].pub_key[i] & 0xff);
wprintw(self->window, "%02X", Blocked.list[selected_num].pub_key[i] & 0xff);
}
wrefresh(self->window);

View File

@ -77,7 +77,7 @@ typedef struct {
typedef struct {
int num_selected;
int max_idx; /* 1 + the index of the last friend in friends array */
int max_idx; /* 1 + the index of the last friend in list */
int num_friends;
int *index;
ToxicFriend *list;

View File

@ -31,6 +31,7 @@
#include "windows.h"
#include "misc_tools.h"
#include "settings.h"
#include "file_senders.h"
extern ToxWindow *prompt;
extern struct user_settings *user_settings_;
@ -261,3 +262,25 @@ int char_rfind(const char *s, char ch, int len)
return i;
}
/* Converts bytes to appropriate unit and puts in buf as a string */
void bytes_convert_str(char *buf, int size, uint64_t bytes)
{
double conv = bytes;
const char *unit;
if (conv < KiB) {
unit = "Bytes";
} else if (conv < MiB) {
unit = "KiB";
conv /= (double) KiB;
} else if (conv < GiB) {
unit = "MiB";
conv /= (double) MiB;
} else {
unit = "GiB";
conv /= (double) GiB;
}
snprintf(buf, size, "%.1f %s", conv, unit);
}

View File

@ -103,4 +103,7 @@ int char_find(int idx, const char *s, char ch);
returns 0 if char not found */
int char_rfind(const char *s, char ch, int len);
/* Converts bytes to appropriate unit and puts in buf as a string */
void bytes_convert_str(char *buf, int size, uint64_t bytes);
#endif /* #define _misc_tools_h */

View File

@ -496,9 +496,6 @@ int get_num_active_windows(void)
/* destroys all chat and groupchat windows (should only be called on shutdown) */
void kill_all_windows(Tox *m)
{
kill_prompt_window(prompt);
kill_friendlist();
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
@ -507,4 +504,7 @@ void kill_all_windows(Tox *m)
else if (windows[i].is_groupchat)
kill_groupchat_window(&windows[i]);
}
kill_prompt_window(prompt);
kill_friendlist();
}