Refactoring.

master
Håvard Pettersson 10 years ago
parent f03103fa8f
commit e02d2c9b0e

@ -255,7 +255,7 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer,
}
char hex_address[TOX_CLIENT_ID_SIZE * 2 + 1];
tox_weechat_bin2hex(request->address,
tox_weechat_bin2hex(request->tox_id,
TOX_CLIENT_ID_SIZE,
hex_address);
@ -294,7 +294,7 @@ tox_weechat_cmd_friend(void *data, struct t_gui_buffer *buffer,
request = request->next_request)
{
char hex_address[TOX_CLIENT_ID_SIZE * 2 + 1];
tox_weechat_bin2hex(request->address,
tox_weechat_bin2hex(request->tox_id,
TOX_CLIENT_ID_SIZE,
hex_address);

@ -92,7 +92,7 @@ tox_weechat_friend_request_remove(struct t_tox_weechat_friend_request *request)
}
/**
* Accept a friend request. Removes and frees the request.
* Accept a friend request. Remove and free the request.
*/
void
tox_weechat_accept_friend_request(struct t_tox_weechat_friend_request *request)
@ -101,6 +101,15 @@ tox_weechat_accept_friend_request(struct t_tox_weechat_friend_request *request)
tox_weechat_friend_request_remove(request);
}
/**
* Decline a friend request. Remove and free the request.
*/
void
tox_weechat_decline_friend_request(struct t_tox_weechat_friend_request *request)
{
tox_weechat_friend_request_remove(request);
}
/**
* Return the friend request from the identity with the number num.
*/

@ -46,6 +46,9 @@ tox_weechat_friend_request_add(struct t_tox_weechat_identity *identity,
void
tox_weechat_accept_friend_request(struct t_tox_weechat_friend_request *request);
void
tox_weechat_decline_friend_request(struct t_tox_weechat_friend_request *request);
struct t_tox_weechat_friend_request *
tox_weechat_friend_request_with_num(struct t_tox_weechat_identity *identity,
unsigned int num);

@ -34,6 +34,7 @@
#include "tox-weechat-chats.h"
#include "tox-weechat-tox-callbacks.h"
#include "tox-weechat-utils.h"
#include "tox-weechat-json.h"
#include "tox-weechat-identities.h"
@ -229,11 +230,19 @@ tox_weechat_identity_connect(struct t_tox_weechat_identity *identity)
}
weechat_printf(identity->buffer,
"%s%s: identity %s connecting",
"%s%s: identity %s connecting...",
weechat_prefix("network"),
weechat_plugin->name,
identity->name);
if (identity->friend_request_count > 0)
{
weechat_printf(identity->buffer,
"%sYou have %d pending friend requests.",
weechat_prefix("network"),
identity->friend_request_count);
}
// create Tox
identity->tox = tox_new(NULL);
@ -253,14 +262,8 @@ tox_weechat_identity_connect(struct t_tox_weechat_identity *identity)
(uint8_t *)name, strlen(name));
}
// initialize friend requests
tox_weechat_friend_request_init_identity(identity);
if (identity->friend_request_count > 0)
weechat_printf(identity->buffer,
"%sYou have %d pending friend requests.",
weechat_prefix("network"),
identity->friend_request_count);
// load JSON data
tox_weechat_json_identity_load(identity);
// bootstrap DHT
int max_bootstrap_nodes = 5;
@ -289,6 +292,9 @@ tox_weechat_identity_disconnect(struct t_tox_weechat_identity *identity)
if (!identity->tox)
return;
// save JSON data
tox_weechat_json_identity_save(identity);
// save and kill tox
int result = tox_weechat_save_identity_data_file(identity);
tox_kill(identity->tox);
@ -415,7 +421,6 @@ void
tox_weechat_identity_free(struct t_tox_weechat_identity *identity)
{
// save friend requests
tox_weechat_friend_request_save_identity(identity);
tox_weechat_friend_request_free_identity(identity);
// disconnect

@ -38,12 +38,11 @@ struct t_tox_weechat_identity
struct t_config_option *options[TOX_WEECHAT_IDENTITY_NUM_OPTIONS];
struct Tox *tox;
int tox_online;
struct t_gui_buffer *buffer;
struct t_hook *tox_do_timer;
int tox_online;
struct t_tox_weechat_chat *chats;
struct t_tox_weechat_chat *last_chat;

@ -34,6 +34,8 @@ const char *tox_weechat_json_key_friend_requests = "friend_requests";
const char *tox_weechat_json_friend_request_key_client_id = "client_id";
const char *tox_weechat_json_friend_request_key_message = "message";
json_t *tox_weechat_json_data = NULL;
/**
* Return the full path to the JSON data file.
*/
@ -60,15 +62,15 @@ tox_weechat_json_get_identity_key(struct t_tox_weechat_identity *identity)
}
/**
* Save an identity's data to a JSON object.
* Save an identity's data.
*/
json_t *
void
tox_weechat_json_identity_save(struct t_tox_weechat_identity *identity)
{
json_t *json_data = json_object();
json_t *friend_request_array = json_array();
if (!json_data || !friend_request_array)
return NULL;
return;
for (struct t_tox_weechat_friend_request *request = identity->friend_requests;
request;
@ -102,17 +104,24 @@ tox_weechat_json_identity_save(struct t_tox_weechat_identity *identity)
friend_request_array);
json_decref(friend_request_array);
return json_data;
char *identity_key = tox_weechat_json_get_identity_key(identity);
json_object_set(tox_weechat_json_data, identity_key, json_data);
free(identity_key);
json_decref(json_data);
}
/**
* Load an identity's data from a JSON object.
*/
void
tox_weechat_json_identity_load(struct t_tox_weechat_identity *identity,
json_t *data)
tox_weechat_json_identity_load(struct t_tox_weechat_identity *identity)
{
json_t *friend_request_array = json_object_get(data, tox_weechat_json_key_friend_requests);
char *identity_key = tox_weechat_json_get_identity_key(identity);
json_t *identity_data = json_object_get(tox_weechat_json_data, identity_key);
free(identity_key);
json_t *friend_request_array = json_object_get(identity_data,
tox_weechat_json_key_friend_requests);
if (friend_request_array)
{
@ -141,7 +150,7 @@ tox_weechat_json_identity_load(struct t_tox_weechat_identity *identity,
}
/**
* Load the JSON data on disk into the in-memory identity objects.
* Load the JSON data on disk into memory.
*/
void
tox_weechat_json_load()
@ -149,22 +158,17 @@ tox_weechat_json_load()
char *full_path = tox_weechat_json_data_file_path();
json_error_t error;
json_t *json_data = json_load_file(full_path, 0, &error);
tox_weechat_json_data = json_load_file(full_path, 0, &error);
free(full_path);
if (json_data)
if (!tox_weechat_json_data)
{
for (struct t_tox_weechat_identity *identity = tox_weechat_identities;
identity;
identity = identity->next_identity)
{
char *hex_id = tox_weechat_json_get_identity_key(identity);
json_t *identity_data = json_object_get(json_data, hex_id);
weechat_printf(NULL,
"%s%s: could not load on-disk data",
weechat_prefix("error"),
weechat_plugin->name);
if (identity_data)
tox_weechat_json_identity_load(identity, identity_data);
}
json_decref(json_data);
tox_weechat_json_data = json_object();
}
}
@ -175,28 +179,21 @@ tox_weechat_json_load()
int
tox_weechat_json_save()
{
json_t *json_data = json_object();
if (!json_data)
return -1;
for (struct t_tox_weechat_identity *identity = tox_weechat_identities;
identity;
identity = identity->next_identity)
{
char *hex_id = tox_weechat_json_get_identity_key(identity);
json_t *identity_data = tox_weechat_json_identity_save(identity);
json_object_set(json_data, hex_id, identity_data);
json_decref(identity_data);
}
char *full_path = tox_weechat_json_data_file_path();
int rc = json_dump_file(json_data,
int rc = json_dump_file(tox_weechat_json_data,
full_path,
0);
free(full_path);
json_decref(json_data);
return rc;
}
/**
* Free in-memory JSON data.
*/
void
tox_weechat_json_free()
{
json_decref(tox_weechat_json_data);
}

@ -26,10 +26,21 @@ extern const char *tox_weechat_json_key_friend_requests;
extern const char *tox_weechat_json_friend_request_key_client_id;
extern const char *tox_weechat_json_friend_request_key_message;
struct json_t;
void
tox_weechat_json_load();
void
tox_weechat_json_identity_save(struct t_tox_weechat_identity *identity);
void
tox_weechat_json_identity_load(struct t_tox_weechat_identity *identity);
int
tox_weechat_json_save();
void
tox_weechat_json_free();
#endif // TOX_WEECHAT_JSON_H

Loading…
Cancel
Save