From 6a44e188537ee2d70226ad9dba4ae89c15437e2b Mon Sep 17 00:00:00 2001 From: MarcoPolo-PasTonMolo Date: Thu, 26 May 2022 17:49:34 +0300 Subject: Add `/avatar set` command to publish avatar Use `/avatar set ` where is an image file to upload a new avatar for the current user. When the avatar is too big it gets scaled down. Scaling code copied from dino. Fixes https://github.com/profanity-im/profanity/issues/1687 --- tests/unittests/xmpp/stub_avatar.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'tests/unittests') diff --git a/tests/unittests/xmpp/stub_avatar.c b/tests/unittests/xmpp/stub_avatar.c index ccd9cf4d..453d1863 100644 --- a/tests/unittests/xmpp/stub_avatar.c +++ b/tests/unittests/xmpp/stub_avatar.c @@ -8,3 +8,8 @@ avatar_get_by_nick(const char* nick) { return TRUE; } +gboolean +avatar_set(const char* path) +{ + return FALSE; +} -- cgit 1.4.1-2-gfad0 From 0cff111249ae60f7b9b6662fc7beaed1b5ec8dde Mon Sep 17 00:00:00 2001 From: MarcoPolo-PasTonMolo Date: Thu, 26 May 2022 21:06:27 +0300 Subject: Add checks for whether gdk-pixbuf exists before using avatar set --- configure.ac | 11 +++++++++++ src/command/cmd_funcs.c | 4 ++++ src/main.c | 6 ++++++ src/xmpp/avatar.c | 6 ++++-- src/xmpp/avatar.h | 2 ++ tests/unittests/xmpp/stub_avatar.c | 5 ++++- 6 files changed, 31 insertions(+), 3 deletions(-) (limited to 'tests/unittests') diff --git a/configure.ac b/configure.ac index 158878b0..5124f2ed 100644 --- a/configure.ac +++ b/configure.ac @@ -65,6 +65,8 @@ AC_ARG_WITH([themes], [AS_HELP_STRING([--with-themes[[=PATH]]], [install themes (default yes)])]) AC_ARG_ENABLE([icons-and-clipboard], [AS_HELP_STRING([--enable-icons-and-clipboard], [enable GTK tray icons and clipboard paste support])]) +AC_ARG_ENABLE([gdk-pixbuf], + [AS_HELP_STRING([--enable-gdk-pixbuf], [enable GDK Pixbuf support])]) # Required dependencies @@ -306,6 +308,15 @@ if test "x$enable_otr" != xno; then AM_COND_IF([BUILD_OTR], [AC_DEFINE([HAVE_LIBOTR], [1], [Have libotr])]) fi +dnl feature: pixbuf +AS_IF([test "x$enable_pixbuf" != xno], + [PKG_CHECK_MODULES([gdk_pixbuf], [gdk-pixbuf-2.0 >= 2.4], + [AC_DEFINE([HAVE_PIXBUF], [1], [gdk-pixbuf module])], + [AS_IF([test "x$enable_pixbuf" = xyes], + [AC_MSG_ERROR([gdk-pixbuf-2.0 >= 2.4 is required for GDK Pixbuf support])], + [AC_MSG_NOTICE([gdk-pixbuf-2.0 >= 2.4 not found, GDK Pixbuf support not enabled])])])]) + + dnl feature: omemo AM_CONDITIONAL([BUILD_OMEMO], [false]) if test "x$enable_omemo" != xno; then diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index 26eb9c3e..dfe96971 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9196,9 +9196,13 @@ cmd_avatar(ProfWin* window, const char* const command, gchar** args) } if (g_strcmp0(args[0], "set") == 0) { +#ifdef HAVE_PIXBUF if (avatar_set(args[1])) { cons_show("Avatar updated successfully"); } +#else + cons_show("This version of Profanity has not been built with GDK Pixbuf support enabled"); +#endif } else if (g_strcmp0(args[0], "get") == 0) { avatar_get_by_nick(args[1], false); } else if (g_strcmp0(args[0], "open") == 0) { diff --git a/src/main.c b/src/main.c index 15e4948a..42629370 100644 --- a/src/main.c +++ b/src/main.c @@ -173,6 +173,12 @@ main(int argc, char** argv) g_print("GTK icons/clipboard: Disabled\n"); #endif +#ifdef HAVE_PIXBUF + g_print("GDK Pixbuf: Enabled\n"); +#else + g_print("GDK Pixbuf: Disabled\n"); +#endif + return 0; } diff --git a/src/xmpp/avatar.c b/src/xmpp/avatar.c index d826dd90..15ec7f1a 100644 --- a/src/xmpp/avatar.c +++ b/src/xmpp/avatar.c @@ -95,6 +95,7 @@ avatar_pep_subscribe(void) shall_open = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); } +#ifdef HAVE_PIXBUF gboolean avatar_set(const char* path) { @@ -104,7 +105,7 @@ avatar_set(const char* path) GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file(expanded_path, &err); if (pixbuf == NULL) { - cons_show("An error occurred while opening %s: %s.", expanded_path, err ? err->message : "No error message given"); + cons_show_error("An error occurred while opening %s: %s.", expanded_path, err ? err->message : "No error message given"); return FALSE; } free(expanded_path); @@ -129,7 +130,7 @@ avatar_set(const char* path) gsize len = -1; if (!gdk_pixbuf_save_to_buffer(pixbuf, &img_data, &len, "png", &err, NULL)) { - cons_show("Unable to scale and convert avatar."); + cons_show_error("Unable to scale and convert avatar."); return FALSE; } @@ -146,6 +147,7 @@ avatar_set(const char* path) return TRUE; } +#endif gboolean avatar_get_by_nick(const char* nick, gboolean open) diff --git a/src/xmpp/avatar.h b/src/xmpp/avatar.h index 1767a2b2..c65328ad 100644 --- a/src/xmpp/avatar.h +++ b/src/xmpp/avatar.h @@ -40,6 +40,8 @@ void avatar_pep_subscribe(void); gboolean avatar_get_by_nick(const char* nick, gboolean open); +#ifdef HAVE_PIXBUF gboolean avatar_set(const char* path); +#endif #endif diff --git a/tests/unittests/xmpp/stub_avatar.c b/tests/unittests/xmpp/stub_avatar.c index 453d1863..9f07c334 100644 --- a/tests/unittests/xmpp/stub_avatar.c +++ b/tests/unittests/xmpp/stub_avatar.c @@ -8,8 +8,11 @@ avatar_get_by_nick(const char* nick) { return TRUE; } + +#ifdef HAVE_PIXBUF gboolean avatar_set(const char* path) { - return FALSE; + return TRUE; } +#endif -- cgit 1.4.1-2-gfad0 From d510f3a4309cf0a42fc135bbf0905f476757a9c8 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Fri, 27 May 2022 10:39:49 +0200 Subject: Final touches for `/avatar set` --- configure.ac | 7 +++---- src/command/cmd_defs.c | 2 +- src/command/cmd_funcs.c | 2 +- src/xmpp/avatar.c | 5 +++-- tests/unittests/xmpp/stub_avatar.c | 2 -- 5 files changed, 8 insertions(+), 10 deletions(-) (limited to 'tests/unittests') diff --git a/configure.ac b/configure.ac index 5124f2ed..dad9cc28 100644 --- a/configure.ac +++ b/configure.ac @@ -66,7 +66,7 @@ AC_ARG_WITH([themes], AC_ARG_ENABLE([icons-and-clipboard], [AS_HELP_STRING([--enable-icons-and-clipboard], [enable GTK tray icons and clipboard paste support])]) AC_ARG_ENABLE([gdk-pixbuf], - [AS_HELP_STRING([--enable-gdk-pixbuf], [enable GDK Pixbuf support])]) + [AS_HELP_STRING([--enable-gdk-pixbuf], [enable GDK Pixbuf support to scale avatars before uploading])]) # Required dependencies @@ -308,15 +308,14 @@ if test "x$enable_otr" != xno; then AM_COND_IF([BUILD_OTR], [AC_DEFINE([HAVE_LIBOTR], [1], [Have libotr])]) fi -dnl feature: pixbuf +dnl feature: pixbuf / used for scaling avatars before uploading via `/avatar set` AS_IF([test "x$enable_pixbuf" != xno], [PKG_CHECK_MODULES([gdk_pixbuf], [gdk-pixbuf-2.0 >= 2.4], [AC_DEFINE([HAVE_PIXBUF], [1], [gdk-pixbuf module])], [AS_IF([test "x$enable_pixbuf" = xyes], - [AC_MSG_ERROR([gdk-pixbuf-2.0 >= 2.4 is required for GDK Pixbuf support])], + [AC_MSG_ERROR([gdk-pixbuf-2.0 >= 2.4 is required to scale avatars before uploading])], [AC_MSG_NOTICE([gdk-pixbuf-2.0 >= 2.4 not found, GDK Pixbuf support not enabled])])])]) - dnl feature: omemo AM_CONDITIONAL([BUILD_OMEMO], [false]) if test "x$enable_omemo" != xno; then diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index ffc4ec83..599262c1 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -2452,7 +2452,7 @@ static struct cmd_t command_defs[] = { "If nothing happens after using this command the user either doesn't have an avatar set at all " "or doesn't use XEP-0084 to publish it.") CMD_ARGS( - { "set ", "Set avatar to the img at ." }, + { "set ", "Set avatar to the image at ." }, { "get ", "Download the avatar. barejid is the JID to download avatar from." }, { "open ", "Download avatar and open it with command." }) CMD_EXAMPLES( diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index dfe96971..2a8331d1 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9201,7 +9201,7 @@ cmd_avatar(ProfWin* window, const char* const command, gchar** args) cons_show("Avatar updated successfully"); } #else - cons_show("This version of Profanity has not been built with GDK Pixbuf support enabled"); + cons_show("Profanity has not been built with GDK Pixbuf support enabled which is needed to scale the avatar when uploading."); #endif } else if (g_strcmp0(args[0], "get") == 0) { avatar_get_by_nick(args[1], false); diff --git a/src/xmpp/avatar.c b/src/xmpp/avatar.c index 15ec7f1a..9345ba3a 100644 --- a/src/xmpp/avatar.c +++ b/src/xmpp/avatar.c @@ -36,12 +36,13 @@ #include "config.h" #include +#ifdef HAVE_PIXBUF #include +#endif #include #include #include #include -#include #include #include "log.h" @@ -61,7 +62,7 @@ typedef struct avatar_metadata static GHashTable* looking_for = NULL; // contains nicks/barejids from who we want to get the avatar static GHashTable* shall_open = NULL; // contains a list of nicks that shall not just downloaded but also opened -const int MAX_PIXEL = 192; +const int MAX_PIXEL = 192; // max pixel width/height for an avatar static void _avatar_request_item_by_id(const char* jid, avatar_metadata* data); static int _avatar_metadata_handler(xmpp_stanza_t* const stanza, void* const userdata); diff --git a/tests/unittests/xmpp/stub_avatar.c b/tests/unittests/xmpp/stub_avatar.c index 9f07c334..fd63293e 100644 --- a/tests/unittests/xmpp/stub_avatar.c +++ b/tests/unittests/xmpp/stub_avatar.c @@ -9,10 +9,8 @@ avatar_get_by_nick(const char* nick) return TRUE; } -#ifdef HAVE_PIXBUF gboolean avatar_set(const char* path) { return TRUE; } -#endif -- cgit 1.4.1-2-gfad0