ec1a04213a57187d0621576cce4a4e77dd8bbedb
[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 <dbus/dbus-glib.h>
31
32 #include "browser-switchboard.h"
33 #include "launcher.h"
34 #include "dbus-server-bindings.h"
35
36 #define DEFAULT_HOMEDIR "/home/user"
37 #define CONFIGFILE_LOC "/.config/browser-switchboard"
38 #define MAXLINE 1024
39
40 struct swb_context ctx;
41
42 static void set_config_defaults(struct swb_context * ctx) {
43         if (!ctx)
44                 return;
45         free(ctx->other_browser_cmd);
46         ctx->continuous_mode = 0;
47         ctx->default_browser_launcher = NULL;
48         ctx->other_browser_cmd = NULL;
49 }
50
51 static void waitforzombies(int signalnum) {
52         while (waitpid(-1, NULL, WNOHANG) > 0)
53                 printf("Waited for a zombie\n");
54 }
55
56 static void read_config(int signalnum) {
57         char * homedir;
58         char * configfile;
59         size_t len;
60         char buf[MAXLINE];
61         char * tmp;
62         char * value;
63         char * default_browser = NULL;
64         FILE * fp;
65
66         set_config_defaults(&ctx);
67
68         if (!(homedir = getenv("HOME")))
69                 homedir = DEFAULT_HOMEDIR;
70         len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
71         if (!(configfile = calloc(len, sizeof(char))))
72                 goto out_noopen;
73         strncpy(configfile, homedir, strlen(homedir));
74         strncat(configfile, CONFIGFILE_LOC, strlen(CONFIGFILE_LOC));
75
76         if (!(fp = fopen(configfile, "r")))
77                 goto out_noopen;
78
79         /* Read in the config file one line at a time and parse it
80            XXX doesn't deal with lines longer than MAXLINE */
81         while (fgets(buf, MAXLINE, fp)) {
82                 /* skip comments */
83                 if (buf[0] == '#')
84                         continue;
85                 /* look for the = in the line */
86                 if (!(tmp = strchr(buf, '=')))
87                         continue;
88
89                 /* split the line into parameter (before =, in buf) and
90                    value (after =, in value) */
91                 if (!(value = calloc(strlen(tmp+1)+1, sizeof(char))))
92                         goto out;
93                 strncpy(value, tmp+1, strlen(tmp+1));
94                 value[strlen(tmp+1)] = '\0';
95                 /* scribble over the = in buf with a \0 -- that makes buf
96                    just the parameter name */
97                 *tmp = '\0';
98                 /* if we find a newline in value, replace that with a \0 too */
99                 if ((tmp = strchr(value, '\n')))
100                         *tmp = '\0';
101
102                 if (!strcmp(buf, "continuous_mode")) {
103                         ctx.continuous_mode = atoi(value);
104                         free(value);
105                 } else if (!strcmp(buf, "default_browser")) {
106                         if (!default_browser)
107                                 default_browser = value;
108                 } else if (!strcmp(buf, "other_browser_cmd")) {
109                         if (!ctx.other_browser_cmd)
110                                 ctx.other_browser_cmd = value;
111                 } else {
112                         /* Don't need this line's contents */
113                         free(value);
114                 }
115                 value = NULL;
116         }
117
118         printf("continuous_mode: %d\n", ctx.continuous_mode);
119         printf("default_browser: '%s'\n", default_browser?default_browser:"NULL");
120         printf("other_browser_cmd: '%s'\n", ctx.other_browser_cmd?ctx.other_browser_cmd:"NULL");
121
122 out:
123         fclose(fp);
124 out_noopen:
125         update_default_browser(&ctx, default_browser);
126         free(configfile);
127         free(default_browser);
128         return;
129 }
130
131 int main() {
132         OssoBrowser * obj;
133         GMainLoop * mainloop;
134         GError * error = NULL;
135
136         read_config(0);
137
138         if (ctx.continuous_mode) {
139                 struct sigaction act;
140                 act.sa_flags = SA_RESTART;
141
142                 act.sa_handler = waitforzombies;
143                 if (sigaction(SIGCHLD, &act, NULL) == -1) {
144                         printf("Installing signal handler failed\n");
145                         return 1;
146                 }
147
148                 act.sa_handler = read_config;
149                 if (sigaction(SIGHUP, &act, NULL) == -1) {
150                         printf("Installing signal handler failed\n");
151                         return 1;
152                 }
153         }
154
155         g_type_init();
156
157         dbus_g_object_type_install_info(OSSO_BROWSER_TYPE,
158                         &dbus_glib_osso_browser_object_info);
159
160         ctx.session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
161         if (!ctx.session_bus) {
162                 printf("Couldn't get a D-Bus bus connection\n");
163                 return 1;
164         }
165         ctx.dbus_proxy = dbus_g_proxy_new_for_name(ctx.session_bus,
166                         "org.freedesktop.DBus", "/org/freedesktop/DBus",
167                         "org.freedesktop.DBus");
168         if (!ctx.dbus_proxy) {
169                 printf("Couldn't get an org.freedesktop.DBus proxy\n");
170                 return 1;
171         }
172
173         dbus_request_osso_browser_name(&ctx);
174
175         obj = g_object_new(OSSO_BROWSER_TYPE, NULL);
176         dbus_g_connection_register_g_object(ctx.session_bus,
177                         "/com/nokia/osso_browser", G_OBJECT(obj));
178         dbus_g_connection_register_g_object(ctx.session_bus,
179                         "/com/nokia/osso_browser/request", G_OBJECT(obj));
180
181         mainloop = g_main_loop_new(NULL, FALSE);
182         printf("Starting main loop\n");
183         g_main_loop_run(mainloop);
184         printf("Main loop completed\n");
185
186         return 0;
187 }