2 * launcher.c -- functions for launching web browsers for browser-switchboard
4 * Copyright (C) 2009 Steven Luo
5 * Derived from a Python implementation by Jason Simpson and Steven Luo
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
27 #include <sys/types.h>
29 #include <dbus/dbus-glib.h>
31 #include "browser-switchboard.h"
33 #include "dbus-server-bindings.h"
35 #define LAUNCH_DEFAULT_BROWSER launch_tear
37 static void launch_tear(struct swb_context * ctx, char * uri) {
39 static DBusGProxy * tear_proxy = NULL;
40 GError * error = NULL;
46 printf("launch_tear with uri '%s'\n", uri);
48 status = system("pidof tear > /dev/null");
49 if (!WIFEXITED(status))
51 if (!WEXITSTATUS(status)) {
53 tear_proxy = dbus_g_proxy_new_for_name(ctx->session_bus,
54 "com.nokia.tear", "/com/nokia/tear",
56 dbus_g_proxy_call(tear_proxy, "OpenAddress",
60 if (!ctx->continuous_mode)
63 if (ctx->continuous_mode) {
64 if ((pid = fork()) != 0) {
65 /* Parent process or error in fork() */
66 printf("child: %d\n", (int)pid);
72 execl("/usr/bin/tear", "/usr/bin/tear", uri, (char *)NULL);
76 void launch_microb(struct swb_context * ctx, char * uri) {
77 int kill_browserd = 0;
84 status = system("pidof /usr/sbin/browserd > /dev/null");
85 if (!WIFEXITED(status))
87 if (WEXITSTATUS(status)) {
89 system("/usr/sbin/browserd -d");
92 dbus_release_osso_browser_name(ctx);
94 if ((pid = fork()) == -1) {
100 waitpid(pid, &status, 0);
103 if (!strcmp(uri, "new_window")) {
104 execl("/usr/bin/maemo-invoker",
105 "browser", (char *)NULL);
107 execl("/usr/bin/maemo-invoker",
108 "browser", "--url", uri, (char *)NULL);
113 system("kill `pidof /usr/sbin/browserd`");
115 if (!ctx || !ctx->continuous_mode)
118 dbus_request_osso_browser_name(ctx);
121 static void launch_other_browser(struct swb_context * ctx, char * uri) {
124 size_t cmdlen, urilen;
126 if (!uri || !strcmp(uri, "new_window"))
128 urilen = strlen(uri);
133 /* urilen+3 = length of URI + 2x \' + \0 */
134 if (!(quoted_uri = calloc(urilen+3, sizeof(char))))
136 strncpy(quoted_uri+1, uri, urilen);
137 quoted_uri[0] = quoted_uri[urilen+1] = '\'';
138 /* calloc zeroes the memory, so string is automatically
141 /* If there are any 's in the original URI, URL-escape them
142 (replace them with %27) */
143 quote = quoted_uri + 1;
144 while ((quote = strchr(quote, '\'')) &&
145 (quote-quoted_uri) < strlen(quoted_uri)-1) {
146 /* 3 = strlen("%27")-strlen("'") + \0 */
147 if (!(quoted_uri = realloc(quoted_uri,
148 strlen(quoted_uri)+3)))
150 /* Move the string after the ', including the \0,
152 memmove(quote+3, quote+1, strlen(quote)+1);
153 memcpy(quote, "%27", 3);
156 urilen = strlen(quoted_uri);
160 cmdlen = strlen(ctx->other_browser_cmd);
162 /* cmdlen+urilen+1 is normally two bytes longer than we need (uri will
163 replace "%s"), but is needed in the case other_browser_cmd has no %s
165 if (!(command = calloc(cmdlen+urilen+1, sizeof(char))))
167 snprintf(command, cmdlen+urilen+1, ctx->other_browser_cmd, quoted_uri);
168 printf("command: '%s'\n", command);
170 if (ctx->continuous_mode) {
172 /* Parent process or error in fork() */
181 execl("/bin/sh", "/bin/sh", "-c", command, (char *)NULL);
184 static void use_other_browser_cmd(struct swb_context * ctx, char * cmd) {
185 size_t len = strlen(cmd);
187 free(ctx->other_browser_cmd);
188 ctx->other_browser_cmd = calloc(len+1, sizeof(char));
189 if (!ctx->other_browser_cmd) {
190 printf("malloc failed!\n");
191 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
193 ctx->other_browser_cmd = strncpy(ctx->other_browser_cmd,
195 ctx->other_browser_cmd[len] = '\0';
196 ctx->default_browser_launcher = launch_other_browser;
200 void update_default_browser(struct swb_context * ctx, char * default_browser) {
204 if (!default_browser) {
205 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
209 if (!strcmp(default_browser, "tear"))
210 ctx->default_browser_launcher = launch_tear;
211 else if (!strcmp(default_browser, "microb"))
212 ctx->default_browser_launcher = launch_microb;
213 else if (!strcmp(default_browser, "fennec"))
214 use_other_browser_cmd(ctx, "fennec %s");
215 else if (!strcmp(default_browser, "midori"))
216 use_other_browser_cmd(ctx, "midori %s");
217 else if (!strcmp(default_browser, "other")) {
218 if (ctx->other_browser_cmd)
219 ctx->default_browser_launcher = launch_other_browser;
221 printf("default_browser is 'other', but no other_browser_cmd set -- using default\n");
222 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
225 printf("Unknown default_browser %s, using default", default_browser);
226 ctx->default_browser_launcher = LAUNCH_DEFAULT_BROWSER;
230 void launch_browser(struct swb_context * ctx, char * uri) {
231 if (ctx && ctx->default_browser_launcher)
232 ctx->default_browser_launcher(ctx, uri);