billiob pushed a commit to branch master.

http://git.enlightenment.org/apps/terminology.git/commit/?id=7465f46a9f7acb8af89eb3e4b33e7bde99f82f34

commit 7465f46a9f7acb8af89eb3e4b33e7bde99f82f34
Author: Boris Faure <bill...@gmail.com>
Date:   Wed Nov 11 22:51:51 2020 +0100

    utils: add open_url() to avoid code duplication
---
 src/bin/options_colors.c | 46 ++++---------------------------------------
 src/bin/termio.c         | 37 +----------------------------------
 src/bin/utils.c          | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/bin/utils.h          |  3 +++
 4 files changed, 59 insertions(+), 78 deletions(-)

diff --git a/src/bin/options_colors.c b/src/bin/options_colors.c
index dc3523c..e6f6223 100644
--- a/src/bin/options_colors.c
+++ b/src/bin/options_colors.c
@@ -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;
-
-   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);
+
+   open_url(ctx->config, csi->cs->md.website);
+
    ctx->ctxpopup = NULL;
    evas_object_del(obj);
 }
diff --git a/src/bin/termio.c b/src/bin/termio.c
index 26234a9..1892129 100644
--- a/src/bin/termio.c
+++ b/src/bin/termio.c
@@ -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);
 }
diff --git a/src/bin/utils.c b/src/bin/utils.c
index 3452223..0a90aaa 100644
--- a/src/bin/utils.c
+++ b/src/bin/utils.c
@@ -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);
+}
diff --git a/src/bin/utils.h b/src/bin/utils.h
index c08ec43..3706212 100644
--- a/src/bin/utils.h
+++ b/src/bin/utils.h
@@ -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

-- 


Reply via email to