diff --git a/telegram-purple.c b/telegram-purple.c index 07c5d88..6935e11 100644 --- a/telegram-purple.c +++ b/telegram-purple.c @@ -820,6 +820,11 @@ static void tgprpl_init (PurplePlugin *plugin) { TGP_KEY_HISTORY_RETRIEVAL_THRESHOLD, TGP_DEFAULT_HISTORY_RETRIEVAL_THRESHOLD); prpl_info.protocol_options = g_list_append (prpl_info.protocol_options, opt); + // Media + opt = purple_account_option_int_new (_("Autoload media size (kb)"), TGP_KEY_MEDIA_SIZE, + TGP_DEFAULT_MEDIA_SIZE); + prpl_info.protocol_options = g_list_append (prpl_info.protocol_options, opt); + // Chats opt = purple_account_option_bool_new (_("Add all group chats to buddy list"), TGP_KEY_JOIN_GROUP_CHATS, TGP_DEFAULT_JOIN_GROUP_CHATS); diff --git a/telegram-purple.h b/telegram-purple.h index 5f986ce..f8e5ab2 100644 --- a/telegram-purple.h +++ b/telegram-purple.h @@ -67,6 +67,9 @@ #define TGP_MAX_MSG_SIZE 4096 #define TGP_DEFAULT_MAX_MSG_SPLIT_COUNT 4 +#define TGP_DEFAULT_MEDIA_SIZE 8192 +#define TGP_KEY_MEDIA_SIZE "media-size-threshold" + #define TGP_KEY_PASSWORD_TWO_FACTOR "password-two-factor" #define TGP_DEFAULT_ACCEPT_SECRET_CHATS "ask" diff --git a/tgp-2prpl.c b/tgp-2prpl.c index 7caaa85..242d2a3 100644 --- a/tgp-2prpl.c +++ b/tgp-2prpl.c @@ -36,6 +36,11 @@ connection_data *tls_get_data (struct tgl_state *TLS) { return TLS->ev_base; } +int tls_get_media_threshold (struct tgl_state *TLS) { + return purple_account_get_int (tls_get_pa (TLS), + TGP_KEY_MEDIA_SIZE, TGP_DEFAULT_MEDIA_SIZE) << 10; +} + connection_data *gc_get_data (PurpleConnection *gc) { return purple_connection_get_protocol_data (gc); } diff --git a/tgp-2prpl.h b/tgp-2prpl.h index af34881..3025aca 100644 --- a/tgp-2prpl.h +++ b/tgp-2prpl.h @@ -27,6 +27,7 @@ PurpleAccount *tls_get_pa (struct tgl_state *TLS); PurpleConnection *tls_get_conn (struct tgl_state *TLS); connection_data *tls_get_data (struct tgl_state *TLS); +int tls_get_media_threshold (struct tgl_state *TLS); connection_data *gc_get_data (PurpleConnection *gc); connection_data *pa_get_data (PurpleAccount *pa); connection_data *pbn_get_data (PurpleBlistNode *node); diff --git a/tgp-msg.c b/tgp-msg.c index c15eb00..edd3f63 100644 --- a/tgp-msg.c +++ b/tgp-msg.c @@ -379,6 +379,15 @@ int tgp_msg_send (struct tgl_state *TLS, const char *message, tgl_peer_id_t to) return 0; } +static char *tgp_msg_file_display (const char *filename, const char *caption) { +#ifndef __ADIUM_ + return g_strdup_printf ("%s", g_markup_escape_text (filename, -1), + g_markup_escape_text (caption, -1)); +#else + return g_strdup_printf ("file:///%s", g_uri_escape_string (filename, NULL, TRUE)); +#endif +} + static char *tgp_msg_photo_display (struct tgl_state *TLS, const char *filename, int *flags) { connection_data *conn = TLS->ev_base; int img = p2tgl_imgstore_add_with_id (filename); @@ -416,7 +425,6 @@ static char *tgp_msg_sticker_display (struct tgl_state *TLS, tgl_peer_id_t from, } static void tgp_msg_display (struct tgl_state *TLS, struct tgp_msg_loading *C) { - connection_data *conn = TLS->ev_base; struct tgl_message *M = C->msg; char *text = NULL; int flags = 0; @@ -490,21 +498,28 @@ static void tgp_msg_display (struct tgl_state *TLS, struct tgp_msg_loading *C) { if (M->media.document->flags & TGLDF_STICKER) { g_return_if_fail(C->data != NULL); text = tgp_msg_sticker_display (TLS, M->from_id, C->data, &flags); + } else if (M->media.document->flags & TGLDF_ANIMATED && C->data) { + text = tgp_msg_file_display (C->data, _("[animation]")); } else if (M->media.document->flags & TGLDF_IMAGE) { g_return_if_fail(C->data != NULL); text = tgp_msg_photo_display (TLS, C->data, &flags); } else { - if (! tgp_our_msg(TLS, M)) { - tgprpl_recv_file (conn->gc, tgp_blist_lookup_purple_name (TLS, M->from_id), M); + if (! tgp_our_msg (TLS, M)) { + tgprpl_recv_file (tls_get_conn (TLS), tgp_blist_lookup_purple_name (TLS, M->from_id), M); } return; } break; - + case tgl_message_media_video: case tgl_message_media_audio: { - if (! tgp_our_msg(TLS, M)) { - tgprpl_recv_file (conn->gc, tgp_blist_lookup_purple_name (TLS, M->from_id), M); + if (! tgp_our_msg (TLS, M)) { + if (C->data) { + text = tgp_msg_file_display (C->data, + M->media.type == tgl_message_media_audio ? _("[audio]") : _("[video]")); + } else { + tgprpl_recv_file (tls_get_conn (TLS), tgp_blist_lookup_purple_name (TLS, M->from_id), M); + } } } break; @@ -518,7 +533,7 @@ static void tgp_msg_display (struct tgl_state *TLS, struct tgp_msg_loading *C) { text = tgp_msg_photo_display (TLS, C->data, &flags); } else { if (! tgp_our_msg(TLS, M)) { - tgprpl_recv_file (conn->gc, tgp_blist_lookup_purple_name (TLS, M->to_id), M); + tgprpl_recv_file (tls_get_conn (TLS), tgp_blist_lookup_purple_name (TLS, M->to_id), M); } return; } @@ -815,15 +830,43 @@ void tgp_msg_recv (struct tgl_state *TLS, struct tgl_message *M, GList *before) } // documents that are stickers or images will be displayed just like regular photo messages - // and need to be lodaed beforehand + // and need to be loaded beforehand case tgl_message_media_document: - case tgl_message_media_video: - case tgl_message_media_audio: - if (M->media.document->flags & TGLDF_STICKER || M->media.document->flags & TGLDF_IMAGE) { + if (M->media.document->flags & (TGLDF_STICKER | TGLDF_IMAGE)) { ++ C->pending; tgl_do_load_document (TLS, M->media.document, tgp_msg_on_loaded_document, C); + } else if (M->media.document->flags & (TGLDF_ANIMATED | TGLDF_VIDEO | TGLDF_AUDIO)) { + + // adium doesn't support file links, autoloading media would mean that it + // wouldn't be possible to show a usable link to the user +#ifndef __ADIUM_ + if (M->media.document->size <= tls_get_media_threshold (TLS)) { // 8mb auto loading threshold + ++ C->pending; + if (M->media.document->flags & TGLDF_AUDIO) { + tgl_do_load_audio (TLS, M->media.document, tgp_msg_on_loaded_document, C); + } else if (M->media.document->flags & TGLDF_VIDEO) { + tgl_do_load_video (TLS, M->media.document, tgp_msg_on_loaded_document, C); + } else { + tgl_do_load_document (TLS, M->media.document, tgp_msg_on_loaded_document, C); + } + } +#endif } break; +#ifndef __ADIUM_ + case tgl_message_media_video: + if (M->media.document->size <= tls_get_media_threshold (TLS)) { + ++ C->pending; + tgl_do_load_video (TLS, M->media.document, tgp_msg_on_loaded_document, C); + } + break; + case tgl_message_media_audio: + if (M->media.document->size <= tls_get_media_threshold (TLS)) { + ++ C->pending; + tgl_do_load_audio (TLS, M->media.document, tgp_msg_on_loaded_document, C); + } + break; +#endif case tgl_message_media_document_encr: if (M->media.encr_document->flags & TGLDF_STICKER || M->media.encr_document->flags & TGLDF_IMAGE) { ++ C->pending;