jjacky avatar jjacky committed 0da1c93

added option CmdLineLink: Misc/Command line to open links (in news)

Instead of hard-coded use of xdg-open, one can now define the command line to be used when a link is clicked. Variable $URL will be replaced by the URL to be opened.
Additionally an error message will now be shown in case of failure.

Comments (0)

Files changed (6)

                     config->confirm_post = (*value == '1');
                     debug ("config: confirm postsysupgrade: %d", config->confirm_post);
                 }
+                else if (strcmp (key, "CmdLineLink") == 0)
+                {
+                    setstringoption (value, "cmdline_link", &(config->cmdline_link));
+                }
                 #endif
                 else if (strcmp (key, "AurIgnore") == 0)
                 {
     
     char            *news_last;
     alpm_list_t     *news_read;
+    #ifndef DISBALE_UPDATER
+    char            *cmdline_link;
+    #endif
     
     gboolean         is_curl_init;
 } config_t;
 
 =back
 
+=item I<Command line to open links (in news)>
+
+The command line to be executed when a link (on news) is clicked. Use variable
+B<$URL> as placeholder for the full URL to be opened.
+
 =back
 
 =head1 SYSTEM UPGRADE
     config->on_dbl_click = DO_SYSUPGRADE;
     config->sane_sort_order = TRUE;
     config->check_pacman_conflict = TRUE;
+    config->cmdline_link = strdup ("xdg-open '$URL'");
     
     config->tpl_upgrades = calloc (1, sizeof (templates_t));
     config->tpl_upgrades->title = strdup ("$NB updates available (D: $DL; N: $NET)");
 }
 
 static gboolean
-event_after_cb (GtkTextView *textview, GdkEvent *ev)
+event_after_cb (GtkTextView *textview, GdkEvent *ev, GtkWidget *window)
 {
     gint x, y;
     GSList *tags = NULL, *t;
         gchar *link = g_object_get_data (G_OBJECT (tag), "link");
         if (link)
         {
-            gchar buf[1024];
-            snprintf (buf, 1024, "xdg-open '%s'", link);
-            g_spawn_command_line_async (buf, NULL);
+            GError *error = NULL;
+            gchar buf[1024], *b, *s, *ss;
+            size_t len = strlen (link);
+            
+            debug ("click on link: %s", link);
+            
+            if (len + strlen (config->cmdline_link) >= 1024)
+            {
+                b = malloc (sizeof (*b) * (len + strlen (config->cmdline_link)));
+            }
+            else
+            {
+                b = buf;
+            }
+            
+            for (s = config->cmdline_link, ss = b; s && *s != '\0'; ++s, ++ss)
+            {
+                if (*s == '$' && *(s + 1) == 'U' && *(s + 2) == 'R' && *(s + 3) == 'L')
+                {
+                    memcpy (ss, link, len);
+                    ss += len - 1;
+                    s += 3;
+                }
+                else
+                {
+                    *ss = *s;
+                }
+            }
+            *ss = '\0';
+            debug ("cmdline: %s", buf);
+            
+            if (!g_spawn_command_line_async (buf, &error))
+            {
+                show_error ("Unable to open link", error->message, GTK_WINDOW (window));
+                g_clear_error (&error);
+            }
+            
+            if (b != buf)
+            {
+                free (b);
+            }
             break;
         }
     }
     g_signal_connect (*textview, "motion-notify-event",
                       G_CALLBACK (motion_notify_event_cb), NULL);
     g_signal_connect (*textview, "event-after",
-                        G_CALLBACK (event_after_cb), NULL);
+                        G_CALLBACK (event_after_cb), (gpointer) *window);
     /* to provide URL in tooltip for links */
     g_object_set (G_OBJECT (*textview), "has-tooltip", TRUE, NULL);
     g_signal_connect (*textview, "query-tooltip",
 static GtkWidget *syncdbs_in_tooltip        = NULL;
 static GtkWidget *on_sgl_click              = NULL;
 static GtkWidget *on_dbl_click              = NULL;
+static GtkWidget *cmdline_link_entry        = NULL;
 
 /* we keep a copy of templates like so, so that we can use it when refreshing
  * the different templates. that is, values shown when a template is not set
     new_config.notif_icon_user  = NULL;
     new_config.cmdline          = NULL;
     new_config.cmdline_aur      = NULL;
+    new_config.cmdline_link     = NULL;
     #ifndef DISABLE_UPDATER
     new_config.cmdline_post     = NULL;
     #endif
         add_to_conf ("OnDblClick = NOTHING\n");
     }
     
+    s = (char *) gtk_entry_get_text (GTK_ENTRY (cmdline_link_entry));
+    if (s == NULL || *s == '\0')
+    {
+        error_on_page (6, "You need to specify the command-line to open links.");
+    }
+    else if (!strstr (s, "$URL"))
+    {
+        error_on_page (6, "You need to use $URL on the command line to open links.");
+    }
+    add_to_conf ("CmdLineLink = %s\n", s);
+    new_config.cmdline_link = strdup (s);
+    
     /* ** TEMPLATES ** */
     
     /* Upgrades */
     free (config->notif_icon_user);
     free (config->cmdline);
     free (config->cmdline_aur);
+    free (config->cmdline_link);
     #ifndef DISABLE_UPDATER
     FREELIST (config->cmdline_post);
     #endif
     free (new_config.notif_icon_user);
     free (new_config.cmdline);
     free (new_config.cmdline_aur);
+    free (new_config.cmdline_link);
     #ifndef DISABLE_UPDATER
     if (new_config.cmdline_post)
     {
         gtk_combo_box_set_active (GTK_COMBO_BOX (on_dbl_click), 0);
     }
     
+    ++top;
+    label = gtk_label_new ("Command line to open links (in news) :");
+    gtk_grid_attach (GTK_GRID (grid), label, 0, top, 1, 1);
+    gtk_widget_show (label);
+    
+    cmdline_link_entry = gtk_entry_new ();
+    gtk_widget_set_tooltip_markup (cmdline_link_entry,
+        "Use variable <b>$URL</b> for the URL to open");
+    if (config->cmdline_link != NULL)
+    {
+        gtk_entry_set_text (GTK_ENTRY (cmdline_link_entry), config->cmdline_link);
+    }
+    gtk_grid_attach (GTK_GRID (grid), cmdline_link_entry, 1, top, 1, 1);
+    gtk_widget_show (cmdline_link_entry);
+    
     /* add page */
     gtk_widget_show (grid);
     gtk_notebook_append_page (GTK_NOTEBOOK (notebook), grid, lbl_page);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.