Skip to content

Commit

Permalink
Handle Thunderbird as a special case for mail with attachment.
Browse files Browse the repository at this point in the history
Thunderbird no longer accepts the mailto URI. This breaks Nautilus
right-click > Email, for example.

Creates a special case for Thunberbird then, using the -compose option.
  • Loading branch information
nteodosio committed Oct 13, 2023
1 parent 2bf8482 commit db7179c
Showing 1 changed file with 95 additions and 37 deletions.
132 changes: 95 additions & 37 deletions src/email.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,66 +34,124 @@ compose_mail_mailto (GAppInfo *info,
{
g_autofree char *enc_subject = NULL;
g_autofree char *enc_body = NULL;
g_autofree char *arg = NULL;
g_autoptr(GString) url = NULL;
g_autoptr(GList) uris = NULL;
GString *tb_arg = NULL;
int i;
gboolean success;
const char *sep;
const char *is_tb_snap;
const char *old_cmdline;

enc_subject = g_uri_escape_string (subject ? subject : "", NULL, FALSE);
enc_body = g_uri_escape_string (body ? body : "", NULL, FALSE);

url = g_string_new ("mailto:");

sep = "?";

if (addrs)
{
for (i = 0; addrs[i]; i++)
{
if (i > 0)
g_string_append (url, ",");
g_string_append_printf (url, "%s", addrs[i]);
/* The commandline can be complicated, e.g.
* env BAMF_DESKTOP_FILE_HINT=/var/lib/snapd/.../thunderbird_thunderbird.desktop /snap/bin/thunderbird %u
* so just match for thunderbird.
*/
old_cmdline = g_app_info_get_commandline(info);
if(strstr(old_cmdline, "thunderbird")) {
/* Construct the argument to -compose. */
tb_arg = g_string_new("");
if (addrs) {
g_string_append_printf(tb_arg, "to='");
for (i = 0; addrs[i]; i++){
g_string_append_printf(tb_arg, "%s,", addrs[i]);
}
g_string_append_printf(tb_arg, "',");
}
if (cc) {
g_string_append_printf(tb_arg, "cc='");
for (i = 0; cc[i]; i++){
g_string_append_printf(tb_arg, "%s,", cc[i]);
}
g_string_append_printf(tb_arg, "',");
}
if (bcc) {
g_string_append_printf(tb_arg, "bcc='");
for (i = 0; bcc[i]; i++){
g_string_append_printf(tb_arg, "%s,", bcc[i]);
}
g_string_append_printf(tb_arg, "',");
}
g_string_append_printf (tb_arg, "subject='%s',", enc_subject);
g_string_append_printf (tb_arg, "body='%s',", enc_body);
if (attachments) {
g_string_append_printf(tb_arg, "attachment='");
for (i = 0; attachments[i]; i++){
if (i != 0) g_string_append_printf (tb_arg, ",");
gchar *enc_att = (g_uri_escape_string(attachments[i], "/", 1));
g_string_append_printf (tb_arg, "file://%s", enc_att);
}
g_string_append_printf(tb_arg, "',");
}

if (cc)
{
g_string_append_printf (url, "%scc=", sep);
sep = "&";

for (i = 0; cc[i]; i++)
arg = g_string_free_and_steal(tb_arg);
is_tb_snap = strstr(old_cmdline, "/snap/bin/thunderbird");
gchar *cmdline[] = {
is_tb_snap ? "/snap/bin/thunderbird" : "thunderbird",
"-compose",
arg,
NULL
};
g_debug("Launching: %s %s %s", cmdline[0], cmdline[1], cmdline[2]);
success = g_spawn_async(NULL, cmdline, NULL, G_SPAWN_SEARCH_PATH, NULL,
NULL, NULL, error);
} else {
url = g_string_new ("mailto:");

sep = "?";

if (addrs)
{
for (i = 0; addrs[i]; i++)
{
if (i > 0)
g_string_append (url, ",");
g_string_append_printf (url, "%s", cc[i]);
g_string_append_printf (url, "%s", addrs[i]);
}
}
}

if (bcc)
{
g_string_append_printf (url, "%sbcc=", sep);
sep = "&";
if (cc)
{
g_string_append_printf (url, "%scc=", sep);
sep = "&";

for (i = 0; cc[i]; i++)
{
if (i > 0)
g_string_append (url, ",");
g_string_append_printf (url, "%s", cc[i]);
}
}

for (i = 0; bcc[i]; i++)
{
if (i > 0)
g_string_append (url, ",");
g_string_append_printf (url, "%s", bcc[i]);
}
}
if (bcc)
{
g_string_append_printf (url, "%sbcc=", sep);
sep = "&";

for (i = 0; bcc[i]; i++)
{
if (i > 0)
g_string_append (url, ",");
g_string_append_printf (url, "%s", bcc[i]);
}
}

g_string_append_printf (url, "%ssubject=%s", sep, enc_subject);
g_string_append_printf (url, "&body=%s", enc_body);
g_string_append_printf (url, "%ssubject=%s", sep, enc_subject);
g_string_append_printf (url, "&body=%s", enc_body);

for (i = 0; attachments[i]; i++)
g_string_append_printf (url, "&attachment=%s", attachments[i]);
for (i = 0; attachments[i]; i++)
g_string_append_printf (url, "&attachment=%s", attachments[i]);

uris = g_list_append (uris, url->str);
uris = g_list_append (uris, url->str);

g_debug ("Launching: %s\n", url->str);
g_debug ("Launching: %s\n", url->str);

success = g_app_info_launch_uris (info, uris, NULL, error);
success = g_app_info_launch_uris (info, uris, NULL, error);
}

return success;
}
Expand Down

0 comments on commit db7179c

Please sign in to comment.