From 69acd905121abb5d3e7d9344b2eb454b0699083c Mon Sep 17 00:00:00 2001 From: mjentsch Date: Mon, 25 Jan 2016 00:59:56 +0100 Subject: [PATCH] Support deleting contacts, handle deletions of chats and channels When a user deletes a buddy list entry, ask if the chat or channel should be destroyed / left or the contact should be deleted. In many cases, deleting a buddy from the blist would have no effect, since the buddy would show up again on the next startup, which is most likely confusing to the user. Just destroying the chat or the channel is dangerous too, so ask for confirmation every time this happens to let the user choose what to do with that entry. --- telegram-purple.c | 2 +- tgp-request.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++ tgp-request.h | 2 + 3 files changed, 116 insertions(+), 1 deletion(-) diff --git a/telegram-purple.c b/telegram-purple.c index 7977df2..c50605a 100644 --- a/telegram-purple.c +++ b/telegram-purple.c @@ -777,7 +777,7 @@ static PurplePluginProtocolInfo prpl_info = { NULL, // change_passwd tgprpl_add_buddy, NULL, // add_buddies - tgprpl_remove_buddy, + tgprpl_request_delete_contact, NULL, // remove_buddies NULL, // add_permit NULL, // add_deny diff --git a/tgp-request.c b/tgp-request.c index 0519318..ae763e6 100644 --- a/tgp-request.c +++ b/tgp-request.c @@ -310,3 +310,116 @@ void request_value (struct tgl_state *TLS, enum tgl_value_type type, const char break; } } + + +// delete contact + +static void request_delete_contact_ok (struct request_values_data *data, PurpleRequestFields* fields) { + tgl_peer_t *P = data->arg; + g_return_if_fail(P); + + switch (tgl_get_peer_type (P->id)) { + case TGL_PEER_CHAT: + g_warn_if_fail (P->chat.flags & TGLCF_LEFT); + leave_and_delete_chat (data->TLS, P); + break; + + case TGL_PEER_ENCR_CHAT: + tgl_do_discard_secret_chat (data->TLS, &P->encr_chat, NULL, NULL); + break; + + case TGL_PEER_USER: + g_warn_if_fail(P->user.flags & TGLUF_CONTACT); + tgl_do_del_contact (data->TLS, P->id, tgp_notify_on_error_gw, NULL); + break; + + case TGL_PEER_CHANNEL: + g_warn_if_fail(P->channel.flags & TGLCHF_CREATOR); + if (! (P->channel.flags & TGLCHF_LEFT)) { + tgl_do_leave_channel (data->TLS, P->id, tgp_notify_on_error_gw, NULL); + } + break; + + default: + g_warn_if_reached(); + break; + } + + free (data); +} + +static void request_delete_contact_cancel (struct request_values_data *data, PurpleRequestFields* fields) { + free (data); +} + +void tgprpl_request_delete_contact (PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group) { + const char *title1 = NULL, + *title2 = NULL, + *msg = NULL; + + g_return_if_fail(buddy); + + struct tgl_state *TLS = gc_get_tls (gc); + + tgl_peer_t *P = tgp_blist_buddy_get_peer (buddy); + g_return_if_fail(P); + + switch (tgl_get_peer_type (P->id)) { + case TGL_PEER_CHAT: + if (! (P->chat.flags & TGLCF_LEFT)) { + title1 = _("Leave Chat"); + title2 = title1; + msg = _("Do you want to leave this chat permantently?"); + } + break; + + case TGL_PEER_ENCR_CHAT: + title1 = _("Abort Secret Chat"); + title2 = title1; + msg = _("Do you want to terminate this secret chat permantently?"); + break; + + case TGL_PEER_USER: + if (P->user.flags & TGLUF_CONTACT) { + title1 = _("Delete Contact"); + title2 = title1; + msg = _("Do you want to delete the contact from all your other devices?"); + } + break; + + case TGL_PEER_CHANNEL: + if (P->channel.flags & TGLCHF_CREATOR) { + /* + FIXME: Support destorying channels + + title1 = _("Destroy Channel"); + title2 = title1; + msg = _("You are admin of this channel, do you want to delete it permanently?"); + */ + } else { + if (! (P->channel.flags & TGLCHF_LEFT)) { + title1 = _("Leave Channel"); + title2 = title1; + msg = _("Do you want to leave this channel?"); + } + } + break; + + default: + g_warn_if_reached(); + break; + } + + if (msg) { + purple_request_ok_cancel(tls_get_conn (TLS), title1, title2, msg, 0, tls_get_pa (TLS), + tgp_blist_lookup_purple_name (TLS, P->id), NULL, (void *) request_values_data_init (TLS, 0, P, 0), + request_delete_contact_ok, request_delete_contact_cancel); + } +} + + +// add new contact + +void request_add_contact (struct tgl_state *TLS) { + +} diff --git a/tgp-request.h b/tgp-request.h index e76234b..e6e2158 100644 --- a/tgp-request.h +++ b/tgp-request.h @@ -43,4 +43,6 @@ void request_value (struct tgl_state *TLS, enum tgl_value_type type, const char void request_accept_secret_chat (struct tgl_state *TLS, struct tgl_secret_chat *U); void request_create_chat (struct tgl_state *TLS, const char *subject); +void tgprpl_request_delete_contact (PurpleConnection *gc, PurpleBuddy *buddy, PurpleGroup *group); + #endif