Exemplo n.º 1
0
static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t receive_send,
                               uint8_t filenum, uint8_t control_type, const char *data, uint16_t length)
{
    if (self->num != num)
        return;

    const char *filename;
    char msg[MAX_STR_SIZE] = {0};
    int i = 0;   /* file_sender index */

    if (receive_send == 0) {
        filename = friends[num].file_receiver.filenames[filenum];
    } else {
        for (i = 0; i < MAX_FILES; ++i) {
            if (file_senders[i].filenum == filenum)
                break;
        }

        filename = file_senders[i].pathname;
    }

    switch (control_type) {
        case TOX_FILECONTROL_ACCEPT:
            if (receive_send == 1) {
                const char *r_msg =  "File transfer for '%s' accepted.";
                line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, r_msg, filename);

                /* prep progress bar line */
                char progline[MAX_STR_SIZE];
                prep_prog_line(progline);
                line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, progline);
                file_senders[i].line_id = self->chatwin->hst->line_end->id + 2;
                notify(self, silent, NT_NOFOCUS | NT_BEEP | NT_WNDALERT_2);
            }

            break;

        case TOX_FILECONTROL_KILL:
            snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", filename);

            if (receive_send == 0)
                chat_close_file_receiver(num, filenum);

            notify(self, error, NT_NOFOCUS | NT_WNDALERT_2);
            break;

        case TOX_FILECONTROL_FINISHED:
            if (receive_send == 0) {
                snprintf(msg, sizeof(msg), "File transfer for '%s' complete.", filename);
                chat_close_file_receiver(num, filenum);
                notify(self, transfer_completed, NT_NOFOCUS | NT_WNDALERT_2);
            }

            break;
    }

    if (msg[0])
        line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg);
}
Exemplo n.º 2
0
void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
    if (argc < 1) {
        line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File ID required.");
        return;
    }

    uint8_t filenum = atoi(argv[1]);

    if ((filenum == 0 && strcmp(argv[1], "0")) || filenum >= MAX_FILES) {
        line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with that ID.");
        return;
    }

    if (!Friends.list[self->num].file_receiver[filenum].pending) {
        line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with that ID.");
        return;
    }

    const char *filename = Friends.list[self->num].file_receiver[filenum].filename;

    if (tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) {
        line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Saving file [%d] as: '%s'", filenum, filename);

        /* prep progress bar line */
        char progline[MAX_STR_SIZE];
        prep_prog_line(progline);
        line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", progline);
        Friends.list[self->num].file_receiver[filenum].line_id = self->chatwin->hst->line_end->id + 2;

        if ((Friends.list[self->num].file_receiver[filenum].file = fopen(filename, "a")) == NULL) {
            line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, "* Error writing to file.");
            tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
        } else {
            Friends.list[self->num].file_receiver[filenum].active = true;
            ++Friends.list[self->num].active_file_receivers;
        }
    } else {
        line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed.");
    }

    Friends.list[self->num].file_receiver[filenum].pending = false;
}