2014-02-25 08:28:24 +01:00
|
|
|
/* friendlist.c
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Toxic All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Toxic is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2013-07-30 21:47:40 +02:00
|
|
|
*/
|
|
|
|
|
2013-08-23 09:50:04 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-30 21:47:40 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
2013-08-28 20:47:47 +02:00
|
|
|
#include <stdlib.h>
|
2014-03-14 22:48:52 +01:00
|
|
|
#include <time.h>
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-08-24 04:15:15 +02:00
|
|
|
#include <tox/tox.h>
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
#include "chat.h"
|
2013-08-13 00:50:43 +02:00
|
|
|
#include "friendlist.h"
|
2013-11-24 23:12:24 +01:00
|
|
|
#include "misc_tools.h"
|
2014-03-25 13:21:50 +01:00
|
|
|
#include "line_info.h"
|
2014-03-16 20:28:46 +01:00
|
|
|
|
|
|
|
#ifdef _SUPPORT_AUDIO
|
2014-03-07 03:14:04 +01:00
|
|
|
#include "audio_call.h"
|
2014-03-16 20:28:46 +01:00
|
|
|
#endif
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-08-28 11:46:09 +02:00
|
|
|
extern char *DATA_FILE;
|
2013-09-07 01:59:45 +02:00
|
|
|
extern ToxWindow *prompt;
|
|
|
|
|
2013-11-09 08:50:32 +01:00
|
|
|
static int max_friends_index = 0; /* marks the index of the last friend in friends array */
|
2013-07-31 19:20:03 +02:00
|
|
|
static int num_selected = 0;
|
2014-02-08 05:28:17 +01:00
|
|
|
static int num_friends = 0;
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2014-03-13 11:06:53 +01:00
|
|
|
extern struct _Winthread Winthread;
|
2013-11-30 00:52:21 +01:00
|
|
|
ToxicFriend friends[MAX_FRIENDS_NUM];
|
2013-11-24 23:12:24 +01:00
|
|
|
static int friendlist_index[MAX_FRIENDS_NUM] = {0};
|
|
|
|
|
2014-03-15 22:18:23 +01:00
|
|
|
struct _pendingDel {
|
|
|
|
int num;
|
|
|
|
bool active;
|
|
|
|
} pendingdelete;
|
2014-03-09 05:57:21 +01:00
|
|
|
|
2013-12-14 02:57:32 +01:00
|
|
|
#define S_WEIGHT 100
|
|
|
|
|
2013-11-26 00:49:31 +01:00
|
|
|
static int index_name_cmp(const void *n1, const void *n2)
|
2013-11-24 23:12:24 +01:00
|
|
|
{
|
2013-12-08 07:18:10 +01:00
|
|
|
int res = qsort_strcasecmp_hlpr(friends[*(int *) n1].name, friends[*(int *) n2].name);
|
2013-11-25 00:22:48 +01:00
|
|
|
|
|
|
|
/* Use weight to make qsort always put online friends before offline */
|
2013-12-14 02:57:32 +01:00
|
|
|
res = friends[*(int *) n1].online ? (res - S_WEIGHT) : (res + S_WEIGHT);
|
|
|
|
res = friends[*(int *) n2].online ? (res + S_WEIGHT) : (res - S_WEIGHT);
|
2013-11-25 00:22:48 +01:00
|
|
|
|
|
|
|
return res;
|
2013-11-24 23:12:24 +01:00
|
|
|
}
|
2013-11-09 08:50:32 +01:00
|
|
|
|
2013-11-25 00:22:48 +01:00
|
|
|
/* sorts friendlist_index first by connection status then alphabetically */
|
2014-03-14 04:30:44 +01:00
|
|
|
void sort_friendlist_index(void)
|
2013-11-09 08:50:32 +01:00
|
|
|
{
|
|
|
|
int i;
|
2013-11-25 04:31:58 +01:00
|
|
|
int n = 0;
|
2013-11-09 08:50:32 +01:00
|
|
|
|
|
|
|
for (i = 0; i < max_friends_index; ++i) {
|
2013-11-25 00:22:48 +01:00
|
|
|
if (friends[i].active)
|
2013-11-25 04:31:58 +01:00
|
|
|
friendlist_index[n++] = friends[i].num;
|
2013-11-09 08:50:32 +01:00
|
|
|
}
|
|
|
|
|
2014-02-08 05:28:17 +01:00
|
|
|
qsort(friendlist_index, num_friends, sizeof(int), index_name_cmp);
|
2013-11-09 08:50:32 +01:00
|
|
|
}
|
|
|
|
|
2014-03-19 02:48:26 +01:00
|
|
|
static void update_friend_last_online(int32_t num, uint64_t timestamp)
|
2014-03-15 22:18:23 +01:00
|
|
|
{
|
|
|
|
friends[num].last_online.last_on = timestamp;
|
|
|
|
friends[num].last_online.tm = *localtime(×tamp);
|
2014-03-19 08:14:08 +01:00
|
|
|
|
|
|
|
/* if the format changes make sure TIME_STR_SIZE is the correct size */
|
2014-03-15 22:18:23 +01:00
|
|
|
strftime(friends[num].last_online.hour_min_str, TIME_STR_SIZE, "%I:%M %p",
|
|
|
|
&friends[num].last_online.tm);
|
|
|
|
}
|
|
|
|
|
2014-03-19 02:48:26 +01:00
|
|
|
static void friendlist_onMessage(ToxWindow *self, Tox *m, int32_t num, uint8_t *str, uint16_t len)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-11-30 11:35:25 +01:00
|
|
|
if (num >= max_friends_index)
|
2013-08-16 19:11:09 +02:00
|
|
|
return;
|
|
|
|
|
2013-11-30 11:35:25 +01:00
|
|
|
if (friends[num].chatwin == -1) {
|
2014-03-07 01:39:57 +01:00
|
|
|
if (get_num_active_windows() < MAX_WINDOWS_NUM) {
|
2013-11-30 11:35:25 +01:00
|
|
|
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num));
|
|
|
|
} else {
|
2014-04-01 04:00:17 +02:00
|
|
|
str[len] = '\0';
|
|
|
|
|
2013-11-30 11:35:25 +01:00
|
|
|
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
|
2014-04-01 09:53:12 +02:00
|
|
|
int n_len = tox_get_name(m, num, nick);
|
2014-04-01 10:43:52 +02:00
|
|
|
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH-1);
|
2014-04-01 04:00:17 +02:00
|
|
|
nick[n_len] = '\0';
|
2013-11-30 11:35:25 +01:00
|
|
|
|
2014-03-25 13:21:50 +01:00
|
|
|
uint8_t timefrmt[TIME_STR_SIZE];
|
|
|
|
get_time_str(timefrmt);
|
2013-11-30 11:35:25 +01:00
|
|
|
|
2014-03-25 13:21:50 +01:00
|
|
|
line_info_add(prompt, timefrmt, nick, NULL, str, IN_MSG, 0, 0);
|
|
|
|
|
|
|
|
uint8_t *msg = "* Warning: Too many windows are open.";
|
|
|
|
line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, RED);
|
2013-11-30 11:35:25 +01:00
|
|
|
alert_window(prompt, WINDOW_ALERT_1, true);
|
|
|
|
}
|
|
|
|
}
|
2013-07-31 20:20:16 +02:00
|
|
|
}
|
|
|
|
|
2014-03-19 02:48:26 +01:00
|
|
|
static void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int32_t num, uint8_t status)
|
2013-09-05 03:25:59 +02:00
|
|
|
{
|
2013-11-30 11:35:25 +01:00
|
|
|
if (num >= max_friends_index)
|
2013-09-05 03:25:59 +02:00
|
|
|
return;
|
|
|
|
|
2014-03-25 13:21:50 +01:00
|
|
|
friends[num].online = status;
|
2014-03-15 22:18:23 +01:00
|
|
|
update_friend_last_online(num, get_unix_time());
|
2014-03-14 22:48:52 +01:00
|
|
|
store_data(m, DATA_FILE);
|
2014-03-14 04:30:44 +01:00
|
|
|
sort_friendlist_index();
|
2013-09-05 03:25:59 +02:00
|
|
|
}
|
|
|
|
|
2014-03-19 02:48:26 +01:00
|
|
|
static void friendlist_onNickChange(ToxWindow *self, Tox *m, int32_t num, uint8_t *str, uint16_t len)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-11-30 11:35:25 +01:00
|
|
|
if (len > TOX_MAX_NAME_LENGTH || num >= max_friends_index)
|
2013-08-16 19:11:09 +02:00
|
|
|
return;
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2014-04-01 10:43:52 +02:00
|
|
|
len = MIN(len, TOXIC_MAX_NAME_LENGTH-1);
|
2014-04-01 04:00:17 +02:00
|
|
|
|
|
|
|
str[len] = '\0';
|
2014-03-28 08:46:00 +01:00
|
|
|
strcpy(friends[num].name, str);
|
2013-09-18 01:11:23 +02:00
|
|
|
friends[num].namelength = len;
|
2014-03-14 04:30:44 +01:00
|
|
|
sort_friendlist_index();
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2014-03-19 02:48:26 +01:00
|
|
|
static void friendlist_onStatusChange(ToxWindow *self, Tox *m, int32_t num, uint8_t status)
|
2013-09-05 03:25:59 +02:00
|
|
|
{
|
2013-11-30 11:35:25 +01:00
|
|
|
if (num >= max_friends_index)
|
2013-09-05 03:25:59 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
friends[num].status = status;
|
|
|
|
}
|
|
|
|
|
2014-03-19 02:48:26 +01:00
|
|
|
static void friendlist_onStatusMessageChange(ToxWindow *self, int32_t num, uint8_t *str, uint16_t len)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-11-30 11:35:25 +01:00
|
|
|
if (len > TOX_MAX_STATUSMESSAGE_LENGTH || num >= max_friends_index)
|
2013-08-16 19:11:09 +02:00
|
|
|
return;
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2014-03-28 08:46:00 +01:00
|
|
|
strcpy(friends[num].statusmsg, str);
|
2014-04-01 04:00:17 +02:00
|
|
|
friends[num].statusmsg[len] = '\0';
|
2013-09-10 10:04:13 +02:00
|
|
|
friends[num].statusmsg_len = len;
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2014-03-19 02:48:26 +01:00
|
|
|
void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int32_t num, bool sort)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-11-09 08:50:32 +01:00
|
|
|
if (max_friends_index < 0 || max_friends_index >= MAX_FRIENDS_NUM)
|
2013-11-15 20:59:49 +01:00
|
|
|
return;
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-28 11:46:09 +02:00
|
|
|
int i;
|
|
|
|
|
2013-11-09 08:50:32 +01:00
|
|
|
for (i = 0; i <= max_friends_index; ++i) {
|
2013-08-28 11:46:09 +02:00
|
|
|
if (!friends[i].active) {
|
|
|
|
friends[i].num = num;
|
|
|
|
friends[i].active = true;
|
|
|
|
friends[i].chatwin = -1;
|
2013-09-05 03:25:59 +02:00
|
|
|
friends[i].online = false;
|
2013-09-05 07:34:23 +02:00
|
|
|
friends[i].status = TOX_USERSTATUS_NONE;
|
2013-11-29 16:14:59 +01:00
|
|
|
friends[i].namelength = tox_get_name(m, num, friends[i].name);
|
2014-02-22 23:58:36 +01:00
|
|
|
tox_get_client_id(m, num, friends[i].pub_key);
|
2014-03-15 22:18:23 +01:00
|
|
|
update_friend_last_online(i, tox_get_last_online(m, i));
|
2013-09-04 03:31:50 +02:00
|
|
|
|
2013-09-18 01:11:23 +02:00
|
|
|
if (friends[i].namelength == -1 || friends[i].name[0] == '\0') {
|
2013-11-19 00:52:46 +01:00
|
|
|
strcpy(friends[i].name, (uint8_t *) UNKNOWN_NAME);
|
2014-04-01 04:00:17 +02:00
|
|
|
friends[i].namelength = strlen(UNKNOWN_NAME);
|
2013-10-19 05:08:37 +02:00
|
|
|
} else { /* Enforce toxic's maximum name length */
|
2014-04-01 04:00:17 +02:00
|
|
|
friends[i].namelength = MIN(friends[i].namelength, TOXIC_MAX_NAME_LENGTH);
|
|
|
|
friends[i].name[friends[i].namelength] = '\0';
|
2013-09-18 01:11:23 +02:00
|
|
|
}
|
2013-09-04 03:31:50 +02:00
|
|
|
|
2014-02-08 05:28:17 +01:00
|
|
|
num_friends = tox_count_friendlist(m);
|
|
|
|
|
2013-11-09 08:50:32 +01:00
|
|
|
if (i == max_friends_index)
|
|
|
|
++max_friends_index;
|
|
|
|
|
2013-12-04 07:08:26 +01:00
|
|
|
if (sort)
|
2014-03-14 04:30:44 +01:00
|
|
|
sort_friendlist_index();
|
2013-12-04 07:08:26 +01:00
|
|
|
|
2013-11-15 20:59:49 +01:00
|
|
|
return;
|
2013-08-28 11:46:09 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2014-03-19 02:48:26 +01:00
|
|
|
static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t filenum,
|
2013-10-16 23:59:56 +02:00
|
|
|
uint64_t filesize, uint8_t *filename, uint16_t filename_len)
|
2013-10-11 06:23:39 +02:00
|
|
|
{
|
2013-11-30 11:35:25 +01:00
|
|
|
if (num >= max_friends_index)
|
2013-10-11 06:23:39 +02:00
|
|
|
return;
|
|
|
|
|
2013-11-30 11:35:25 +01:00
|
|
|
if (friends[num].chatwin == -1) {
|
2014-03-07 01:39:57 +01:00
|
|
|
if (get_num_active_windows() < MAX_WINDOWS_NUM) {
|
2013-11-30 11:35:25 +01:00
|
|
|
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num));
|
|
|
|
} else {
|
|
|
|
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
|
|
|
|
2014-04-01 04:00:17 +02:00
|
|
|
uint8_t nick[TOX_MAX_NAME_LENGTH];
|
2014-04-01 09:53:12 +02:00
|
|
|
int n_len = tox_get_name(m, num, nick);
|
2014-04-01 10:43:52 +02:00
|
|
|
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH-1);
|
2014-04-01 04:00:17 +02:00
|
|
|
nick[n_len] = '\0';
|
2013-11-30 11:35:25 +01:00
|
|
|
|
2014-03-25 13:21:50 +01:00
|
|
|
uint8_t msg[MAX_STR_SIZE];
|
|
|
|
snprintf(msg, sizeof(msg), "* File transfer from %s failed: too many windows are open.", nick);
|
|
|
|
line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, RED);
|
2013-11-30 11:35:25 +01:00
|
|
|
|
|
|
|
alert_window(prompt, WINDOW_ALERT_1, true);
|
|
|
|
}
|
|
|
|
}
|
2013-10-11 06:23:39 +02:00
|
|
|
}
|
|
|
|
|
2014-03-19 02:48:26 +01:00
|
|
|
static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int32_t num, uint8_t *group_pub_key)
|
2013-11-10 03:43:56 +01:00
|
|
|
{
|
2013-11-30 11:35:25 +01:00
|
|
|
if (num >= max_friends_index)
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
|
2013-11-30 11:35:25 +01:00
|
|
|
if (friends[num].chatwin == -1) {
|
2014-03-07 01:39:57 +01:00
|
|
|
if (get_num_active_windows() < MAX_WINDOWS_NUM) {
|
2013-11-30 11:35:25 +01:00
|
|
|
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num));
|
|
|
|
} else {
|
2014-04-01 04:00:17 +02:00
|
|
|
uint8_t nick[TOX_MAX_NAME_LENGTH];
|
2014-04-01 09:53:12 +02:00
|
|
|
int n_len = tox_get_name(m, num, nick);
|
2014-04-01 10:43:52 +02:00
|
|
|
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH-1);
|
2014-04-01 04:00:17 +02:00
|
|
|
nick[n_len] = '\0';
|
2013-11-30 11:35:25 +01:00
|
|
|
|
2014-03-25 13:21:50 +01:00
|
|
|
uint8_t msg[MAX_STR_SIZE];
|
|
|
|
snprintf(msg, sizeof(msg), "* Group chat invite from %s failed: too many windows are open.", nick);
|
|
|
|
line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, RED);
|
2013-11-30 11:35:25 +01:00
|
|
|
|
|
|
|
alert_window(prompt, WINDOW_ALERT_1, true);
|
|
|
|
}
|
|
|
|
}
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 07:54:22 +01:00
|
|
|
static void select_friend(ToxWindow *self, Tox *m, wint_t key)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
if (key == KEY_UP) {
|
2013-11-09 08:50:32 +01:00
|
|
|
if (--num_selected < 0)
|
|
|
|
num_selected = num_friends - 1;
|
2013-08-16 19:11:09 +02:00
|
|
|
} else if (key == KEY_DOWN) {
|
2013-11-09 08:50:32 +01:00
|
|
|
num_selected = (num_selected + 1) % num_friends;
|
|
|
|
}
|
2013-08-28 11:46:09 +02:00
|
|
|
}
|
|
|
|
|
2014-03-19 02:48:26 +01:00
|
|
|
static void delete_friend(Tox *m, int32_t f_num)
|
2013-08-28 11:46:09 +02:00
|
|
|
{
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_del_friend(m, f_num);
|
2013-11-29 23:48:08 +01:00
|
|
|
memset(&friends[f_num], 0, sizeof(ToxicFriend));
|
2013-08-28 11:46:09 +02:00
|
|
|
|
|
|
|
int i;
|
|
|
|
|
2013-11-09 08:50:32 +01:00
|
|
|
for (i = max_friends_index; i > 0; --i) {
|
2013-08-28 11:53:47 +02:00
|
|
|
if (friends[i-1].active)
|
2013-08-28 11:46:09 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-09 08:50:32 +01:00
|
|
|
max_friends_index = i;
|
2014-02-08 05:28:17 +01:00
|
|
|
num_friends = tox_count_friendlist(m);
|
2013-09-05 07:34:23 +02:00
|
|
|
|
2013-11-18 05:14:27 +01:00
|
|
|
/* make sure num_selected stays within num_friends range */
|
|
|
|
if (num_friends && num_selected == num_friends)
|
2013-11-18 04:28:22 +01:00
|
|
|
--num_selected;
|
|
|
|
|
2014-03-14 04:30:44 +01:00
|
|
|
sort_friendlist_index();
|
2013-09-05 07:34:23 +02:00
|
|
|
store_data(m, DATA_FILE);
|
2013-08-28 11:46:09 +02:00
|
|
|
}
|
|
|
|
|
2014-03-09 05:57:21 +01:00
|
|
|
/* activates delete friend popup */
|
2014-03-19 02:48:26 +01:00
|
|
|
static void del_friend_activate(ToxWindow *self, Tox *m, int32_t f_num)
|
2014-03-09 05:57:21 +01:00
|
|
|
{
|
|
|
|
int x2, y2;
|
|
|
|
getmaxyx(self->window, y2, x2);
|
|
|
|
self->popup = newwin(3, 22 + TOXIC_MAX_NAME_LENGTH, 8, 8);
|
|
|
|
|
|
|
|
pendingdelete.active = true;
|
|
|
|
pendingdelete.num = f_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* deactivates delete friend popup and deletes friend if instructed */
|
|
|
|
static void del_friend_deactivate(ToxWindow *self, Tox *m, wint_t key)
|
|
|
|
{
|
|
|
|
if (key == 'y')
|
|
|
|
delete_friend(m, pendingdelete.num);
|
|
|
|
|
2014-03-15 22:18:23 +01:00
|
|
|
memset(&pendingdelete, 0, sizeof(pendingdelete));
|
2014-03-09 05:57:21 +01:00
|
|
|
delwin(self->popup);
|
|
|
|
self->popup = NULL;
|
|
|
|
clear();
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2014-03-09 07:02:54 +01:00
|
|
|
static void draw_popup(ToxWindow *self, Tox *m)
|
2014-03-09 05:57:21 +01:00
|
|
|
{
|
|
|
|
if (self->popup == NULL)
|
|
|
|
return;
|
|
|
|
|
2014-03-09 07:02:54 +01:00
|
|
|
wattron(self->popup, A_BOLD);
|
|
|
|
box(self->popup, ACS_VLINE, ACS_HLINE);
|
|
|
|
wattroff(self->popup, A_BOLD);
|
|
|
|
|
2014-03-09 05:57:21 +01:00
|
|
|
wmove(self->popup, 1, 1);
|
|
|
|
wprintw(self->popup, "Delete contact ");
|
|
|
|
wattron(self->popup, A_BOLD);
|
|
|
|
wprintw(self->popup, "%s", friends[pendingdelete.num].name);
|
|
|
|
wattroff(self->popup, A_BOLD);
|
|
|
|
wprintw(self->popup, "? y/n");
|
2014-03-09 07:02:54 +01:00
|
|
|
|
2014-03-09 05:57:21 +01:00
|
|
|
wrefresh(self->popup);
|
|
|
|
}
|
|
|
|
|
2014-03-30 22:40:13 +02:00
|
|
|
static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
|
2013-08-28 11:46:09 +02:00
|
|
|
{
|
2014-02-08 05:28:17 +01:00
|
|
|
if (num_friends == 0)
|
2013-11-18 04:28:22 +01:00
|
|
|
return;
|
|
|
|
|
2013-11-09 08:50:32 +01:00
|
|
|
int f = friendlist_index[num_selected];
|
|
|
|
|
2014-03-09 05:57:21 +01:00
|
|
|
/* lock screen and force decision on deletion popup */
|
|
|
|
if (pendingdelete.active) {
|
|
|
|
if (key == 'y' || key == 'n')
|
|
|
|
del_friend_deactivate(self, m, key);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-30 22:40:13 +02:00
|
|
|
if (key != ltr) {
|
|
|
|
if (key == '\n') {
|
|
|
|
/* Jump to chat window if already open */
|
|
|
|
if (friends[f].chatwin != -1) {
|
|
|
|
set_active_window(friends[f].chatwin);
|
|
|
|
} else if (get_num_active_windows() < MAX_WINDOWS_NUM) {
|
|
|
|
friends[f].chatwin = add_window(m, new_chat(m, friends[f].num));
|
|
|
|
set_active_window(friends[f].chatwin);
|
|
|
|
} else {
|
|
|
|
uint8_t *msg = "* Warning: Too many windows are open.";
|
|
|
|
line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, RED);
|
|
|
|
|
|
|
|
alert_window(prompt, WINDOW_ALERT_1, true);
|
|
|
|
}
|
|
|
|
} else if (key == KEY_DC) {
|
|
|
|
del_friend_activate(self, m, f);
|
2013-11-30 11:35:25 +01:00
|
|
|
} else {
|
2014-03-30 22:40:13 +02:00
|
|
|
select_friend(self, m, key);
|
2013-08-05 07:57:29 +02:00
|
|
|
}
|
2013-11-18 04:28:22 +01:00
|
|
|
}
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2013-12-15 01:29:45 +01:00
|
|
|
#define FLIST_OFST 4 /* Accounts for the lines at top */
|
2013-12-03 00:23:04 +01:00
|
|
|
|
2013-08-23 23:03:44 +02:00
|
|
|
static void friendlist_onDraw(ToxWindow *self, Tox *m)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
curs_set(0);
|
|
|
|
werase(self->window);
|
2013-12-03 00:23:04 +01:00
|
|
|
int x2, y2;
|
|
|
|
getmaxyx(self->window, y2, x2);
|
2013-11-15 23:03:24 +01:00
|
|
|
|
2014-03-14 04:56:46 +01:00
|
|
|
uint64_t cur_time = get_unix_time();
|
2014-03-15 22:18:23 +01:00
|
|
|
struct tm cur_loc_tm = *localtime(&cur_time);
|
2014-03-14 04:56:46 +01:00
|
|
|
|
2013-12-03 00:23:04 +01:00
|
|
|
bool fix_statuses = x2 != self->x; /* true if window x axis has changed */
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-12-15 01:29:45 +01:00
|
|
|
wattron(self->window, COLOR_PAIR(CYAN));
|
|
|
|
wprintw(self->window, " Open a chat window with the");
|
|
|
|
wattron(self->window, A_BOLD);
|
|
|
|
wprintw(self->window, " Enter ");
|
|
|
|
wattroff(self->window, A_BOLD);
|
|
|
|
wprintw(self->window, "key. Delete a friend with the");
|
|
|
|
wattron(self->window, A_BOLD);
|
|
|
|
wprintw(self->window, " Delete ");
|
|
|
|
wattroff(self->window, A_BOLD);
|
|
|
|
wprintw(self->window, "key.\n\n");
|
|
|
|
wattroff(self->window, COLOR_PAIR(CYAN));
|
|
|
|
|
2014-03-13 11:43:53 +01:00
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
|
|
|
int nf = tox_get_num_online_friends(m);
|
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
|
|
|
|
2013-12-15 01:29:45 +01:00
|
|
|
wattron(self->window, A_BOLD);
|
2014-03-15 09:14:46 +01:00
|
|
|
wprintw(self->window, " Online: ");
|
2013-12-15 01:29:45 +01:00
|
|
|
wattroff(self->window, A_BOLD);
|
2014-03-15 09:14:46 +01:00
|
|
|
wprintw(self->window, "%d/%d \n\n", nf, num_friends);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-12-03 09:44:02 +01:00
|
|
|
if ((y2 - FLIST_OFST) <= 0) /* don't allow division by zero */
|
2013-12-03 00:34:14 +01:00
|
|
|
return;
|
|
|
|
|
2013-12-03 00:23:04 +01:00
|
|
|
/* Determine which portion of friendlist to draw based on current position */
|
|
|
|
int page = num_selected / (y2 - FLIST_OFST);
|
|
|
|
int start = (y2 - FLIST_OFST) * page;
|
|
|
|
int end = y2 - FLIST_OFST + start;
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
int i;
|
|
|
|
|
2013-12-03 00:23:04 +01:00
|
|
|
for (i = start; i < num_friends && i < end; ++i) {
|
2013-11-09 08:50:32 +01:00
|
|
|
int f = friendlist_index[i];
|
2013-11-18 04:28:22 +01:00
|
|
|
bool f_selected = false;
|
2013-11-09 08:50:32 +01:00
|
|
|
|
|
|
|
if (friends[f].active) {
|
2013-11-18 01:45:53 +01:00
|
|
|
if (i == num_selected) {
|
|
|
|
wattron(self->window, A_BOLD);
|
2013-09-02 04:11:47 +02:00
|
|
|
wprintw(self->window, " > ");
|
2013-11-18 01:45:53 +01:00
|
|
|
wattroff(self->window, A_BOLD);
|
|
|
|
f_selected = true;
|
|
|
|
} else {
|
2013-09-02 04:11:47 +02:00
|
|
|
wprintw(self->window, " ");
|
2013-11-18 01:45:53 +01:00
|
|
|
}
|
2013-09-08 09:18:34 +02:00
|
|
|
|
2013-11-09 08:50:32 +01:00
|
|
|
if (friends[f].online) {
|
2014-03-19 02:48:26 +01:00
|
|
|
uint8_t status = friends[f].status;
|
2013-09-05 06:47:33 +02:00
|
|
|
int colour = WHITE;
|
2013-09-02 04:11:47 +02:00
|
|
|
|
2013-11-12 07:50:04 +01:00
|
|
|
switch (status) {
|
2013-09-02 04:11:47 +02:00
|
|
|
case TOX_USERSTATUS_NONE:
|
2013-09-05 06:47:33 +02:00
|
|
|
colour = GREEN;
|
2013-09-02 04:11:47 +02:00
|
|
|
break;
|
|
|
|
case TOX_USERSTATUS_AWAY:
|
2013-09-05 06:47:33 +02:00
|
|
|
colour = YELLOW;
|
2013-09-02 04:11:47 +02:00
|
|
|
break;
|
|
|
|
case TOX_USERSTATUS_BUSY:
|
2013-09-05 06:47:33 +02:00
|
|
|
colour = RED;
|
2013-09-02 04:11:47 +02:00
|
|
|
break;
|
2014-03-18 10:16:42 +01:00
|
|
|
case TOX_USERSTATUS_INVALID:
|
|
|
|
colour = MAGENTA;
|
|
|
|
break;
|
2013-09-02 04:11:47 +02:00
|
|
|
}
|
|
|
|
|
2013-09-05 07:34:23 +02:00
|
|
|
wattron(self->window, COLOR_PAIR(colour) | A_BOLD);
|
2014-03-15 09:14:46 +01:00
|
|
|
wprintw(self->window, "O ");
|
2013-09-05 07:34:23 +02:00
|
|
|
wattroff(self->window, COLOR_PAIR(colour) | A_BOLD);
|
2013-11-18 01:45:53 +01:00
|
|
|
|
|
|
|
if (f_selected)
|
2014-03-15 09:14:46 +01:00
|
|
|
wattron(self->window, COLOR_PAIR(BLUE));
|
2013-11-18 01:45:53 +01:00
|
|
|
|
2014-03-15 09:14:46 +01:00
|
|
|
wattron(self->window, A_BOLD);
|
2013-11-18 01:45:53 +01:00
|
|
|
wprintw(self->window, "%s", friends[f].name);
|
2014-03-15 09:14:46 +01:00
|
|
|
wattroff(self->window, A_BOLD);
|
2013-11-18 01:45:53 +01:00
|
|
|
|
|
|
|
if (f_selected)
|
2014-03-15 09:14:46 +01:00
|
|
|
wattroff(self->window, COLOR_PAIR(BLUE));
|
2013-09-10 10:04:13 +02:00
|
|
|
|
2013-11-09 08:50:32 +01:00
|
|
|
/* Reset friends[f].statusmsg on window resize */
|
2013-11-15 23:03:24 +01:00
|
|
|
if (fix_statuses) {
|
2013-09-13 08:02:49 +02:00
|
|
|
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
|
2014-03-14 04:30:44 +01:00
|
|
|
|
2014-03-13 11:06:53 +01:00
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
2014-04-01 04:00:17 +02:00
|
|
|
uint16_t s_len = tox_get_status_message(m, friends[f].num, statusmsg,
|
|
|
|
TOX_MAX_STATUSMESSAGE_LENGTH);
|
2014-03-13 11:06:53 +01:00
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
2014-03-14 04:30:44 +01:00
|
|
|
|
2014-04-01 04:00:17 +02:00
|
|
|
friends[f].statusmsg_len = s_len;
|
|
|
|
friends[f].statusmsg[s_len] = '\0';
|
2014-03-13 11:06:53 +01:00
|
|
|
snprintf(friends[f].statusmsg, sizeof(friends[f].statusmsg), "%s", statusmsg);
|
2013-09-13 08:02:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Truncate note if it doesn't fit on one line */
|
2014-03-15 09:14:46 +01:00
|
|
|
uint16_t maxlen = x2 - getcurx(self->window) - 2;
|
2013-11-09 08:50:32 +01:00
|
|
|
if (friends[f].statusmsg_len > maxlen) {
|
2013-11-15 23:03:24 +01:00
|
|
|
friends[f].statusmsg[maxlen-3] = '\0';
|
|
|
|
strcat(friends[f].statusmsg, "...");
|
2013-11-09 08:50:32 +01:00
|
|
|
friends[f].statusmsg[maxlen] = '\0';
|
|
|
|
friends[f].statusmsg_len = maxlen;
|
2013-09-10 10:04:13 +02:00
|
|
|
}
|
|
|
|
|
2014-03-14 22:48:52 +01:00
|
|
|
if (friends[f].statusmsg[0])
|
2014-03-15 09:14:46 +01:00
|
|
|
wprintw(self->window, " %s", friends[f].statusmsg);
|
2014-03-14 22:48:52 +01:00
|
|
|
|
|
|
|
wprintw(self->window, "\n");
|
2013-09-02 04:11:47 +02:00
|
|
|
} else {
|
2014-03-15 09:14:46 +01:00
|
|
|
wprintw(self->window, "o ");
|
2013-11-18 01:45:53 +01:00
|
|
|
|
|
|
|
if (f_selected)
|
2014-03-15 09:14:46 +01:00
|
|
|
wattron(self->window, COLOR_PAIR(BLUE));
|
2013-11-18 01:45:53 +01:00
|
|
|
|
2014-03-15 09:14:46 +01:00
|
|
|
wattron(self->window, A_BOLD);
|
2014-03-14 04:56:46 +01:00
|
|
|
wprintw(self->window, "%s", friends[f].name);
|
2014-03-15 09:14:46 +01:00
|
|
|
wattroff(self->window, A_BOLD);
|
2013-11-18 01:45:53 +01:00
|
|
|
|
|
|
|
if (f_selected)
|
2014-03-15 09:14:46 +01:00
|
|
|
wattroff(self->window, COLOR_PAIR(YELLOW));
|
2014-03-14 22:48:52 +01:00
|
|
|
|
2014-03-15 22:18:23 +01:00
|
|
|
uint64_t last_seen = friends[f].last_online.last_on;
|
2014-03-14 22:48:52 +01:00
|
|
|
|
|
|
|
if (last_seen != 0) {
|
2014-03-15 22:18:23 +01:00
|
|
|
int day_dist = (cur_loc_tm.tm_yday - friends[f].last_online.tm.tm_yday) % 365;
|
|
|
|
const uint8_t *hourmin = friends[f].last_online.hour_min_str;
|
2014-03-14 22:48:52 +01:00
|
|
|
|
|
|
|
switch (day_dist) {
|
|
|
|
case 0:
|
2014-03-15 22:18:23 +01:00
|
|
|
wprintw(self->window, " Last seen: Today %s\n", hourmin);
|
2014-03-14 22:48:52 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
2014-03-15 22:18:23 +01:00
|
|
|
wprintw(self->window, " Last seen: Yesterday %s\n", hourmin);
|
2014-03-14 22:48:52 +01:00
|
|
|
break;
|
|
|
|
default:
|
2014-03-15 09:14:46 +01:00
|
|
|
wprintw(self->window, " Last seen: %d days ago\n", day_dist);
|
2014-03-14 22:48:52 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2014-03-15 09:14:46 +01:00
|
|
|
wprintw(self->window, " Last seen: Never\n");
|
2014-03-14 22:48:52 +01:00
|
|
|
}
|
2013-09-02 04:11:47 +02:00
|
|
|
}
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
2013-09-05 03:25:59 +02:00
|
|
|
|
2013-12-03 00:23:04 +01:00
|
|
|
self->x = x2;
|
2013-08-16 19:11:09 +02:00
|
|
|
wrefresh(self->window);
|
2014-03-09 07:02:54 +01:00
|
|
|
draw_popup(self, m);
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2014-03-19 02:48:26 +01:00
|
|
|
void disable_chatwin(int32_t f_num)
|
2013-08-07 00:27:51 +02:00
|
|
|
{
|
2013-08-16 19:11:09 +02:00
|
|
|
friends[f_num].chatwin = -1;
|
2013-08-05 07:57:29 +02:00
|
|
|
}
|
|
|
|
|
2014-03-07 03:14:04 +01:00
|
|
|
#ifdef _SUPPORT_AUDIO
|
|
|
|
static void friendlist_onAv(ToxWindow *self, ToxAv *av)
|
|
|
|
{
|
|
|
|
int id = toxav_get_peer_id(av, 0);
|
2014-03-11 23:22:27 +01:00
|
|
|
/*id++;*/
|
2014-03-12 00:25:13 +01:00
|
|
|
if ( id != ErrorInternal && id >= max_friends_index)
|
2014-03-07 03:14:04 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
Tox* m = toxav_get_tox(av);
|
|
|
|
|
|
|
|
if (friends[id].chatwin == -1) {
|
2014-03-11 01:04:53 +01:00
|
|
|
if (get_num_active_windows() < MAX_WINDOWS_NUM) {
|
2014-03-07 03:14:04 +01:00
|
|
|
friends[id].chatwin = add_window(m, new_chat(m, friends[id].num));
|
|
|
|
} else {
|
|
|
|
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
|
2014-04-01 09:53:12 +02:00
|
|
|
int n_len = tox_get_name(m, id, nick);
|
2014-04-01 04:00:17 +02:00
|
|
|
|
2014-04-01 10:43:52 +02:00
|
|
|
n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH-1);
|
2014-04-01 04:00:17 +02:00
|
|
|
nick[n_len] = '\0';
|
2014-03-25 08:17:22 +01:00
|
|
|
|
2014-03-25 13:21:50 +01:00
|
|
|
uint8_t msg[MAX_STR_SIZE];
|
|
|
|
snprintf(msg, sizeof(msg), "Audio action from: %s!", nick);
|
|
|
|
line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
|
|
|
|
|
|
|
uint8_t *errmsg = "* Warning: Too many windows are open.";
|
|
|
|
line_info_add(prompt, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED);
|
2014-03-07 03:14:04 +01:00
|
|
|
|
|
|
|
alert_window(prompt, WINDOW_ALERT_0, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* _SUPPORT_AUDIO */
|
|
|
|
|
2013-11-03 01:32:35 +01:00
|
|
|
ToxWindow new_friendlist(void)
|
2013-08-16 19:11:09 +02:00
|
|
|
{
|
|
|
|
ToxWindow ret;
|
|
|
|
memset(&ret, 0, sizeof(ret));
|
|
|
|
|
2013-11-30 11:35:25 +01:00
|
|
|
ret.active = true;
|
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
ret.onKey = &friendlist_onKey;
|
|
|
|
ret.onDraw = &friendlist_onDraw;
|
2013-11-15 20:59:49 +01:00
|
|
|
ret.onFriendAdded = &friendlist_onFriendAdded;
|
2013-08-16 19:11:09 +02:00
|
|
|
ret.onMessage = &friendlist_onMessage;
|
2013-09-05 03:25:59 +02:00
|
|
|
ret.onConnectionChange = &friendlist_onConnectionChange;
|
2014-03-11 23:22:27 +01:00
|
|
|
ret.onAction = &friendlist_onMessage; /* Action has identical behaviour to message */
|
2013-08-16 19:11:09 +02:00
|
|
|
ret.onNickChange = &friendlist_onNickChange;
|
2013-09-05 03:25:59 +02:00
|
|
|
ret.onStatusChange = &friendlist_onStatusChange;
|
2013-09-03 05:27:34 +02:00
|
|
|
ret.onStatusMessageChange = &friendlist_onStatusMessageChange;
|
2013-10-11 06:23:39 +02:00
|
|
|
ret.onFileSendRequest = &friendlist_onFileSendRequest;
|
2013-11-10 03:43:56 +01:00
|
|
|
ret.onGroupInvite = &friendlist_onGroupInvite;
|
2014-03-07 03:14:04 +01:00
|
|
|
|
|
|
|
#ifdef _SUPPORT_AUDIO
|
|
|
|
ret.onInvite = &friendlist_onAv;
|
|
|
|
ret.onRinging = &friendlist_onAv;
|
|
|
|
ret.onStarting = &friendlist_onAv;
|
|
|
|
ret.onEnding = &friendlist_onAv;
|
|
|
|
ret.onError = &friendlist_onAv;
|
|
|
|
ret.onStart = &friendlist_onAv;
|
|
|
|
ret.onCancel = &friendlist_onAv;
|
|
|
|
ret.onReject = &friendlist_onAv;
|
|
|
|
ret.onEnd = &friendlist_onAv;
|
2014-03-14 23:08:08 +01:00
|
|
|
ret.onRequestTimeout = &friendlist_onAv;
|
|
|
|
ret.onPeerTimeout = &friendlist_onAv;
|
2014-03-07 03:14:04 +01:00
|
|
|
#endif /* _SUPPORT_AUDIO */
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-09-06 00:24:58 +02:00
|
|
|
strcpy(ret.name, "friends");
|
2013-08-16 19:11:09 +02:00
|
|
|
return ret;
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|