Don't use a browser as the default browser if it's not installed
[browser-switch] / launcher.c
index b01b48b..935db63 100644 (file)
@@ -51,6 +51,7 @@ struct browser_launcher {
        char *name;
        void (*launcher)(struct swb_context *, char *);
        char *other_browser_cmd;
+       char *binary;
 };
 
 #ifdef FREMANTLE
@@ -602,35 +603,14 @@ static void launch_other_browser(struct swb_context *ctx, char *uri) {
 
 /* The list of known browsers and how to launch them */
 static struct browser_launcher browser_launchers[] = {
-       { "microb", launch_microb, NULL }, /* First entry is the default! */
-       { "tear", launch_tear, NULL },
-       { "fennec", NULL, "fennec %s" },
-       { "opera", NULL, "opera %s" },
-       { "midori", NULL, "midori %s" },
-       { NULL, NULL, NULL },
+       { "microb", launch_microb, NULL, NULL }, /* First entry is the default! */
+       { "tear", launch_tear, NULL, "/usr/bin/tear" },
+       { "fennec", NULL, "fennec %s", "/usr/bin/fennec" },
+       { "opera", NULL, "opera %s", "/usr/bin/opera" },
+       { "midori", NULL, "midori %s", "/usr/bin/midori" },
+       { NULL, NULL, NULL, NULL },
 };
 
-/* Use launch_other_browser as the default browser launcher, with the string
-   passed in as the other_browser_cmd
-   Resulting other_browser_cmd is always safe to free(), even if a pointer
-   to a string constant is passed in */
-static void use_other_browser_cmd(struct swb_context *ctx, char *cmd) {
-       size_t len = strlen(cmd);
-
-       free(ctx->other_browser_cmd);
-       ctx->other_browser_cmd = calloc(len+1, sizeof(char));
-       if (!ctx->other_browser_cmd) {
-               log_msg("malloc failed!\n");
-               /* Ideally, we'd configure the built-in default here -- but
-                  it's possible we could be called in that path */
-               exit(1);
-       } else {
-               ctx->other_browser_cmd = strncpy(ctx->other_browser_cmd,
-                                                cmd, len+1);
-               ctx->default_browser_launcher = launch_other_browser;
-       }
-}
-
 static void use_launcher_as_default(struct swb_context *ctx,
                                    struct browser_launcher *browser) {
        if (!ctx || !browser)
@@ -638,8 +618,20 @@ static void use_launcher_as_default(struct swb_context *ctx,
 
        if (browser->launcher)
                ctx->default_browser_launcher = browser->launcher;
-       else if (browser->other_browser_cmd)
-               use_other_browser_cmd(ctx, browser->other_browser_cmd);
+       else if (browser->other_browser_cmd) {
+               free(ctx->other_browser_cmd);
+
+               /* Make a copy of the string constant so that
+                  ctx->other_browser_cmd is safe to free() */
+               ctx->other_browser_cmd = strdup(browser->other_browser_cmd);
+               if (!ctx->other_browser_cmd) {
+                       log_msg("malloc failed!\n");
+                       /* Ideally, we'd configure the built-in default here --
+                          but it's possible we could be called in that path */
+                       exit(1);
+               } else
+                       ctx->default_browser_launcher = launch_other_browser;
+       }
 
        return;
 }
@@ -662,8 +654,14 @@ void update_default_browser(struct swb_context *ctx, char *default_browser) {
           it matches */
        for (browser = browser_launchers; browser->name; ++browser)
                if (!strcmp(default_browser, browser->name)) {
-                       use_launcher_as_default(ctx, browser);
-                       return;
+                       /* Make sure the user's choice is installed on the
+                          system */
+                       if (browser->binary && !access(browser->binary, X_OK)) {
+                               use_launcher_as_default(ctx, browser);
+                               return;
+                       } else
+                               log_msg("%s appears not to be installed\n",
+                                       default_browser);
                }
 
        /* Deal with default_browser = "other" */