utils: add open_url() to avoid code duplication

This commit is contained in:
Boris Faure 2020-11-11 22:51:51 +01:00
parent 6122b12e4f
commit 7465f46a9f
Signed by: borisfaure
GPG Key ID: 35C0410516166BE8
4 changed files with 57 additions and 76 deletions

View File

@ -8,6 +8,7 @@
#include "colors.h"
#include "options_colors.h"
#include "options_themepv.h"
#include "utils.h"
typedef struct _Color_Scheme_Ctx
{
@ -48,10 +49,6 @@ _cb_ctxp_del(void *data,
Color_Scheme_Info *csi = data;
EINA_SAFETY_ON_NULL_RETURN(csi);
csi->ctx->ctxpopup = NULL;
/* Force refocus */
//term_unfocus(sd->term);
//term_focus(sd->term);
}
static void
@ -72,47 +69,12 @@ _cb_ctxp_open_website(void *data,
{
Color_Scheme_Info *csi = data;
Color_Scheme_Ctx *ctx;
Config *config;
char buf[PATH_MAX], *s = NULL, *escaped = NULL;
const char *cmd;
const char *prefix = "http://";
Eina_Strbuf *sb = NULL;
EINA_SAFETY_ON_NULL_RETURN(csi);
ctx = csi->ctx;
config = ctx->config;
if (!(config->helper.url.general) ||
!(config->helper.url.general[0]))
goto end;
cmd = config->helper.url.general;
open_url(ctx->config, csi->cs->md.website);
sb = eina_strbuf_new();
if (!sb)
goto end;
eina_strbuf_append(sb, csi->cs->md.website);
eina_strbuf_trim(sb);
s = eina_str_escape(eina_strbuf_string_get(sb));
if (!s)
goto end;
if (casestartswith(s, "http://") ||
casestartswith(s, "https://"))
prefix = "";
escaped = ecore_file_escape_name(s);
if (!escaped)
goto end;
snprintf(buf, sizeof(buf), "%s %s%s", cmd, prefix, escaped);
WRN("trying to launch '%s'", buf);
ecore_exe_run(buf, NULL);
end:
eina_strbuf_free(sb);
free(escaped);
free(s);
ctx->ctxpopup = NULL;
evas_object_del(obj);
}

View File

@ -2686,53 +2686,18 @@ _cb_ctxp_sel_open_as_url(void *data,
{
Evas_Object *term = data;
Termio *sd = evas_object_smart_data_get(term);
char buf[PATH_MAX], *s = NULL, *escaped = NULL;
const char *cmd;
const char *prefix = "http://";
Config *config;
Eina_Strbuf *sb = NULL;
EINA_SAFETY_ON_NULL_RETURN(sd);
config = sd->config;
termio_take_selection(data, ELM_SEL_TYPE_PRIMARY);
if (!sd->have_sel || !sd->sel_str)
goto end;
if (!(config->helper.url.general) ||
!(config->helper.url.general[0]))
goto end;
cmd = config->helper.url.general;
sb = eina_strbuf_new();
if (!sb)
goto end;
eina_strbuf_append(sb, sd->sel_str);
eina_strbuf_trim(sb);
s = eina_str_escape(eina_strbuf_string_get(sb));
if (!s)
goto end;
if (casestartswith(s, "http://") ||
casestartswith(s, "https://") ||
casestartswith(s, "ftp://") ||
casestartswith(s, "mailto:"))
prefix = "";
escaped = ecore_file_escape_name(s);
if (!escaped)
goto end;
snprintf(buf, sizeof(buf), "%s %s%s", cmd, prefix, escaped);
WRN("trying to launch '%s'", buf);
ecore_exe_run(buf, NULL);
open_url(sd->config, sd->sel_str);
end:
eina_strbuf_free(sb);
free(escaped);
free(s);
sd->ctxpopup = NULL;
evas_object_del(obj);
}

View File

@ -1,5 +1,8 @@
#include "private.h"
#include "utils.h"
#include "sb.h"
#include <Ecore.h>
#include <Ecore_File.h>
#include <unistd.h>
#include <pwd.h>
@ -21,3 +24,51 @@ homedir_get(char *buf, size_t size)
return eina_strlcpy(buf, home, size) < size;
}
void
open_url(const Config *config, const char *url)
{
char buf[PATH_MAX], *s = NULL, *escaped = NULL;
const char *cmd;
const char *prefix = "http://";
Eina_Strbuf *sb = NULL;
EINA_SAFETY_ON_NULL_RETURN(config);
if (!(config->helper.url.general) ||
!(config->helper.url.general[0]))
return;
if (!url || url[0] == '\0')
return;
cmd = config->helper.url.general;
sb = eina_strbuf_new();
if (!sb)
return;
eina_strbuf_append(sb, url);
eina_strbuf_trim(sb);
s = eina_str_escape(eina_strbuf_string_get(sb));
if (!s)
goto end;
if (casestartswith(s, "http://") ||
casestartswith(s, "https://") ||
casestartswith(s, "ftp://") ||
casestartswith(s, "mailto:"))
prefix = "";
escaped = ecore_file_escape_name(s);
if (!escaped)
goto end;
snprintf(buf, sizeof(buf), "%s %s%s", cmd, prefix, escaped);
WRN("trying to launch '%s'", buf);
ecore_exe_run(buf, NULL);
end:
eina_strbuf_free(sb);
free(escaped);
free(s);
}

View File

@ -2,6 +2,9 @@
#define _UTILS_H__
#include <Eina.h>
#include "config.h"
Eina_Bool homedir_get(char *buf, size_t size);
void open_url(const Config *config, const char *url);
#endif