46572b06e38e79216a832f02e3a106b1ace197b1
[browser-switch] / main.c
1 /*
2  * main.c -- config file parsing and main loop for browser-switchboard
3  *
4  * Copyright (C) 2009 Steven Luo
5  * Derived from a Python implementation by Jason Simpson and Steven Luo
6  *
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.
11  *
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.
16  *
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,
20  * USA.
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <regex.h>
31 #include <dbus/dbus-glib.h>
32
33 #include "browser-switchboard.h"
34 #include "launcher.h"
35 #include "dbus-server-bindings.h"
36 #include "configfile.h"
37
38 struct swb_context ctx;
39
40 static void set_config_defaults(struct swb_context *ctx) {
41         if (!ctx)
42                 return;
43         free(ctx->other_browser_cmd);
44         ctx->continuous_mode = 0;
45         ctx->default_browser_launcher = NULL;
46         ctx->other_browser_cmd = NULL;
47 }
48
49 static void waitforzombies(int signalnum) {
50         while (waitpid(-1, NULL, WNOHANG) > 0)
51                 printf("Waited for a zombie\n");
52 }
53
54 static void read_config(int signalnum) {
55         FILE *fp;
56         int continuous_mode_seen = 0;
57         struct swb_config_line line;
58         char *default_browser = NULL;
59
60         set_config_defaults(&ctx);
61
62         if (!(fp = open_config_file()))
63                 goto out_noopen;
64
65         /* Parse the config file
66            TODO: should we handle errors differently than EOF? */
67         if (!parse_config_file_begin())
68                 goto out;
69         while (!parse_config_file_line(fp, &line)) {
70                 if (line.parsed) {
71                         if (!strcmp(line.key, "continuous_mode")) {
72                                 if (!continuous_mode_seen) {
73                                         ctx.continuous_mode = atoi(line.value);
74                                         continuous_mode_seen = 1;
75                                 }
76                                 free(line.value);
77                         } else if (!strcmp(line.key, "default_browser")) {
78                                 if (!default_browser)
79                                         default_browser = line.value;
80                         } else if (!strcmp(line.key, "other_browser_cmd")) {
81                                 if (!ctx.other_browser_cmd)
82                                         ctx.other_browser_cmd = line.value;
83                         } else {
84                                 /* Don't need this line's contents */
85                                 free(line.value);
86                         }
87                 }
88                 free(line.key);
89         }
90         parse_config_file_end();
91
92         printf("continuous_mode: %d\n", ctx.continuous_mode);
93         printf("default_browser: '%s'\n", default_browser?default_browser:"NULL");
94         printf("other_browser_cmd: '%s'\n", ctx.other_browser_cmd?ctx.other_browser_cmd:"NULL");
95
96 out:
97         fclose(fp);
98 out_noopen:
99         update_default_browser(&ctx, default_browser);
100         free(default_browser);
101         return;
102 }
103
104 int main() {
105         OssoBrowser *obj_osso_browser, *obj_osso_browser_req;
106         GMainLoop *mainloop;
107         GError *error = NULL;
108
109         read_config(0);
110
111         if (ctx.continuous_mode) {
112                 /* Install signal handlers */
113                 struct sigaction act;
114                 act.sa_flags = SA_RESTART;
115                 sigemptyset(&(act.sa_mask));
116
117                 /* SIGCHLD -- clean up after zombies */
118                 act.sa_handler = waitforzombies;
119                 if (sigaction(SIGCHLD, &act, NULL) == -1) {
120                         printf("Installing signal handler failed\n");
121                         return 1;
122                 }
123
124                 /* SIGHUP -- reread config file */
125                 act.sa_handler = read_config;
126                 if (sigaction(SIGHUP, &act, NULL) == -1) {
127                         printf("Installing signal handler failed\n");
128                         return 1;
129                 }
130         }
131
132         g_type_init();
133
134         dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
135                         &dbus_glib_osso_browser_object_info);
136
137         /* Get a connection to the D-Bus session bus */
138         ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
139         if (!ctx.session_bus) {
140                 printf("Couldn't get a D-Bus bus connection\n");
141                 return 1;
142         }
143         ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
144                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
145                         "org.freedesktop.DBus");
146         if (!ctx.dbus_proxy) {
147                 printf("Couldn't get an org.freedesktop.DBus proxy\n");
148                 return 1;
149         }
150
151         dbus_request_osso_browser_name(&ctx);
152
153         /* Register ourselves to handle the osso_browser D-Bus methods */
154         obj_osso_browser = g_object_new(OSSO_BROWSER_TYPE, NULL);
155         obj_osso_browser_req = g_object_new(OSSO_BROWSER_TYPE, NULL);
156         dbus_g_connection_register_g_object(ctx.session_bus,
157                         "/com/nokia/osso_browser", G_OBJECT(obj_osso_browser));
158         dbus_g_connection_register_g_object(ctx.session_bus,
159                         "/com/nokia/osso_browser/request",
160                         G_OBJECT(obj_osso_browser_req));
161
162         mainloop = g_main_loop_new(NULL, FALSE);
163         printf("Starting main loop\n");
164         g_main_loop_run(mainloop);
165         printf("Main loop completed\n");
166
167         return 0;
168 }